Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dos65 committed Apr 6, 2021
1 parent 6e63022 commit 5fd30c7
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 157 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import xerial.sbt.Sonatype._
lazy val commonSettings = Seq(
scalaVersion := "2.12.12",
organization := "io.github.dos65",
version := "0.0.31-SNAPSHOT",
version := "0.0.35-SNAPSHOT",
crossScalaVersions := Seq("2.12.12", "2.13.3"),
libraryDependencies ++= {
if (is213(scalaVersion.value))
Expand Down
32 changes: 32 additions & 0 deletions modules/core/src/main/scala/make/internal/DebugStateMacro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package make.internal

import scala.reflect.macros.whitebox
import scala.collection.mutable

abstract class DebugStateMacro(val c: whitebox.Context) {
import c.universe._

val debugState = MacroState.getOrElseUpdate[DebugSt](c.universe, new DebugSt)

sealed trait ResolveSt
object ResolveSt {
case object InProgress extends ResolveSt
case class Resolved(tree: Tree) extends ResolveSt
}

class DebugSt(
var debug: Boolean = false,
val stack: mutable.ListBuffer[Type] = mutable.ListBuffer.empty,
val resolveCache: mutable.HashMap[Type, ResolveSt] = mutable.HashMap.empty,
val reverseTraces: mutable.HashMap[Type, List[Type]] =
mutable.HashMap.empty
) {

def registerFailedReason(targetTpe: Type, prev: Type): Unit = {
val curr = reverseTraces.getOrElse(prev, List.empty)
val next = targetTpe :: curr
reverseTraces.update(prev, next)
}

}
}
6 changes: 3 additions & 3 deletions modules/core/src/main/scala/make/internal/DepMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DepMacro(val c: whitebox.Context) {
}

if (root) {
println(state.cache.mkString("\n"))
//println(state.cache.mkString("\n"))
c.internal.removeAttachment[State](c.macroApplication)

val clsName = TypeName(c.freshName("Dep"))
Expand Down Expand Up @@ -76,7 +76,7 @@ class DepMacro(val c: whitebox.Context) {
}
_root_.make.Dep.apply[${ftpe.tpe}, ${atpe.tpe}]((new $clsName).$primaryName)
"""
//pprint.pprintln(tree)
//pprint.pprintln(tree, 100, Int.MaxValue)
c.Expr[Dep[F, A]](tree)
} else {
out
Expand Down Expand Up @@ -125,7 +125,7 @@ class DepMacro(val c: whitebox.Context) {
): Option[(TermName, Type)] = {
from match {
case (head, otpe) :: tl =>
if (sym == head && tpe =:= otpe){
if (sym == head && tpe == otpe){
Some(to.head)
} else findReplacement(sym, tpe, tl, to.tail)
case Nil => None
Expand Down
49 changes: 13 additions & 36 deletions modules/core/src/main/scala/make/internal/MakeMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import make.Make
import make.Debug
import scala.collection.mutable

class MakeMacro(val c: whitebox.Context) {
class MakeMacro(val ctx: whitebox.Context) extends DebugStateMacro(ctx){

import c.universe._

val state = MacroState.getOrElseUpdate[DebugSt](c.universe, new DebugSt)

def debug[F[_], A](implicit
ftpe: WeakTypeTag[F[X] forSome { type X }],
atpe: WeakTypeTag[A]
Expand All @@ -19,13 +17,13 @@ class MakeMacro(val c: whitebox.Context) {
val makeTc = weakTypeOf[Make[F, _]].typeConstructor
val searchType = appliedType(makeTc, ftpe.tpe, atpe.tpe)

state.debug = true
debugState.debug = true
val out = c.inferImplicitValue(searchType)
state.debug = false
debugState.debug = false
out match {
case EmptyTree =>
val st = extractInstanceSt(atpe.tpe, state.reverseTraces)
println(s"ST: ${st}")
val st = extractInstanceSt(atpe.tpe, debugState.reverseTraces)
//println(s"ST: ${st}")
val message = renderInstanceSt(st)
c.abort(c.enclosingPosition, s"Make for ${atpe.tpe} not found\n" + message)
case tree =>
Expand All @@ -43,22 +41,22 @@ class MakeMacro(val c: whitebox.Context) {
atpe: WeakTypeTag[A]
): c.Expr[Debug[Make[F, A]]] = {

if (!state.debug) c.abort(c.enclosingPosition, "debug is not enabled")
state.resolveCache.get(atpe.tpe) match {
if (!debugState.debug) c.abort(c.enclosingPosition, "debug is not enabled")
debugState.resolveCache.get(atpe.tpe) match {
case Some(ResolveSt.InProgress) => c.abort(c.enclosingPosition, "skip")
case Some(ResolveSt.Resolved(tree)) => c.abort(c.enclosingPosition, "skip")
case None =>
val makeTc = weakTypeOf[Make[F, _]].typeConstructor
val makeTpe = appliedType(makeTc, ftpe.tpe, atpe.tpe)
state.stack.append(atpe.tpe)
val pos = state.stack.size
state.resolveCache.update(atpe.tpe, ResolveSt.InProgress)
debugState.stack.append(atpe.tpe)
val pos = debugState.stack.size
debugState.resolveCache.update(atpe.tpe, ResolveSt.InProgress)
val tree = c.inferImplicitValue(makeTpe)
state.resolveCache.update(atpe.tpe, ResolveSt.Resolved(tree))
state.stack.remove(pos - 1)
debugState.resolveCache.update(atpe.tpe, ResolveSt.Resolved(tree))
debugState.stack.remove(pos - 1)
tree match {
case EmptyTree =>
state.stack.lastOption.foreach(state.registerFailedReason(atpe.tpe, _))
debugState.stack.lastOption.foreach(debugState.registerFailedReason(atpe.tpe, _))
}
c.abort(c.enclosingPosition, "skip")
case _ => c.abort(c.enclosingPosition, "skip")
Expand Down Expand Up @@ -124,25 +122,4 @@ class MakeMacro(val c: whitebox.Context) {
case class FailedTraces(tpe: Type, traces: List[FailedTrace]) extends InstanceSt
}

sealed trait ResolveSt
object ResolveSt {
case object InProgress extends ResolveSt
case class Resolved(tree: Tree) extends ResolveSt
}

class DebugSt(
var debug: Boolean = false,
val stack: mutable.ListBuffer[Type] = mutable.ListBuffer.empty,
val resolveCache: mutable.HashMap[Type, ResolveSt] = mutable.HashMap.empty,
val reverseTraces: mutable.HashMap[Type, List[Type]] =
mutable.HashMap.empty
) {

def registerFailedReason(targetTpe: Type, prev: Type): Unit = {
val curr = reverseTraces.getOrElse(prev, List.empty)
val next = targetTpe :: curr
reverseTraces.update(prev, next)
}

}
}
Loading

0 comments on commit 5fd30c7

Please sign in to comment.