Skip to content

Commit

Permalink
Merge branch 'master-3.4.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rssh committed Jul 7, 2024
2 parents 4c7b8b0 + ac8a949 commit 69a289b
Show file tree
Hide file tree
Showing 25 changed files with 1,260 additions and 70 deletions.
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val dottyVersion = "3.3.3"
//val dottyVersion = "3.5.0-RC1-bin-SNAPSHOT"
//val dottyVersion = "3.5.1-RC1-bin-SNAPSHOT"


ThisBuild/version := "0.9.22-SNAPSHOT"
Expand Down Expand Up @@ -126,8 +126,8 @@ lazy val compilerPlugin = project.in(file("compiler-plugin"))
"org.scala-lang" %% "scala3-compiler" % scalaVersion.value % "provided",
"com.github.sbt" % "junit-interface" % "0.13.3" % "test",
("org.scala-js" %% "scalajs-linker" % "1.16.0").cross(CrossVersion.for3Use2_13) % "test",
("org.scala-js" %% "scalajs-ir" % "1.16.0").cross(CrossVersion.for3Use2_13) % "test",
("org.scala-js" %% "scalajs-library" % "1.16.0").cross(CrossVersion.for3Use2_13) % "test",
//("org.scala-js" %% "scalajs-ir" % "1.16.0").cross(CrossVersion.for3Use2_13) % "test",
//("org.scala-js" %% "scalajs-library" % "1.16.0").cross(CrossVersion.for3Use2_13) % "test",
("org.scala-js" %% "scalajs-env-nodejs" % "1.4.0").cross(CrossVersion.for3Use2_13) % "test",
),
// TODO: split test into subdirectories.
Expand Down
6 changes: 5 additions & 1 deletion compiler-plugin/src/main/scala/cps/plugin/CpsPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CpsPlugin extends StandardPlugin {
//


def init(options: List[String]): List[PluginPhase] = {
override def init(options: List[String]): List[PluginPhase] = {
val settings = parseOptions(options)
val selectedNodes = new SelectedNodes()
List(
Expand All @@ -35,6 +35,10 @@ class CpsPlugin extends StandardPlugin {
if (option.startsWith("debugLevel=")) {
val level = option.substring("debugLevel=".length).toInt
settings.debugLevel = level
} else if (option == "printTree") {
settings.printTree = true
} else if (option == "printCode") {
settings.printCode = true
} else if (option == "useLoom") {
settings.useLoom = true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cps.plugin

class CpsPluginSettings(var useLoom: Boolean = true,
var debugLevel: Int = 0,
var printTree: Boolean = false,
var printCode: Boolean = false,
var withShiftReplaceStages: Boolean = false,
var transformDirectContextLambda: Boolean = false,

Expand Down
44 changes: 31 additions & 13 deletions compiler-plugin/src/main/scala/cps/plugin/DebugSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ case class DebugSettings(
object DebugSettings {


def make(from:Tree)(using Context):DebugSettings = {
def make(from:Tree, pluginSettings: CpsPluginSettings)(using Context):DebugSettings = {
val debugLevelAnSym = Symbols.requiredClass("cps.plugin.annotation.CpsDebugLevel")
val scopeOwner =
if (from.symbol != NoSymbol) then
from.symbol
else
summon[Context].owner
val debugLevel: Int = findEnclosingAnnotation(scopeOwner, debugLevelAnSym) match
case Some(an) =>
an.argument(0) match
case Some(Literal(Constant(v:Int))) => v
case other =>
throw CpsTransformException(s"CpsDebugLevelAnnotation should have literal constant as argument, we have $other",an.tree.srcPos)
case None =>
0
val debugLevel: Int =
if (pluginSettings.debugLevel != 0)
pluginSettings.debugLevel
else
findEnclosingAnnotation(scopeOwner, debugLevelAnSym) match
case Some(an) =>
an.argument(0) match
case Some(Literal(Constant(v:Int))) => v
case other =>
throw CpsTransformException(s"CpsDebugLevelAnnotation should have literal constant as argument, we have $other",an.tree.srcPos)
case None =>
0
if (false) {
//don't work after 'cc' stage (always show name)
//TODO: look after inlinging or ask user to enable retain-tree
Expand All @@ -56,10 +60,24 @@ object DebugSettings {
case None =>
0
}
val printCodeTpe = Symbols.requiredClass("cps.plugin.settings.PrintCode").typeRef
val printCode = CpsTransformHelper.findImplicitInstance(printCodeTpe, summon[Context].tree.span).isDefined
val printTreeTpe = Symbols.requiredClass("cps.plugin.settings.PrintTree").typeRef
val printTree = CpsTransformHelper.findImplicitInstance(printTreeTpe, summon[Context].tree.span).isDefined
val printCode = {
pluginSettings.printCode || {
val oldPrintCodeTpe = Symbols.requiredClass("cps.macros.flags.PrintCode").typeRef
val printCodeTpe = Symbols.requiredClass("cps.plugin.settings.PrintCode").typeRef
println(s"context = ${summon[Context].tree.show}, phase = ${summon[Context].phase}")
CpsTransformHelper.findImplicitInstance(printCodeTpe, summon[Context].tree.span).isDefined ||
CpsTransformHelper.findImplicitInstance(oldPrintCodeTpe, summon[Context].tree.span).isDefined
}
}
val printTree = {
pluginSettings.printTree || {
val oldPrintTreeTpe = Symbols.requiredClass("cps.macros.flags.PrintTree").typeRef
val printTreeTpe = Symbols.requiredClass("cps.plugin.settings.PrintTree").typeRef
CpsTransformHelper.findImplicitInstance(printTreeTpe, summon[Context].tree.span).isDefined ||
CpsTransformHelper.findImplicitInstance(oldPrintTreeTpe, summon[Context].tree.span).isDefined

}
}
DebugSettings(debugLevel = debugLevel, printTree = printTree, printCode = printCode)
}

Expand Down
43 changes: 30 additions & 13 deletions compiler-plugin/src/main/scala/cps/plugin/PhaseCps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ class PhaseCps(settings: CpsPluginSettings,


def transformDefDefInternal(tree: DefDef, selectRecord: DefDefSelectRecord, optTopLevelContext:Option[CpsTopLevelContext]=None)(using Context): DefDef = {
val debugSettings = optTopLevelContext.map(_.debugSettings).getOrElse(DebugSettings.make(tree))
val debugSettings = optTopLevelContext.map(_.debugSettings).getOrElse(DebugSettings.make(tree, settings))
selectRecord.debugLevel = debugSettings.debugLevel
if (debugSettings.printCode) then
report.log("transforming tree:", tree.srcPos)
val retval = selectRecord.kind match
case USING_CONTEXT_PARAM(cpsMonadContextArg) =>
val cpsMonadContext = ref(cpsMonadContextArg.symbol)
Expand All @@ -82,10 +80,14 @@ class PhaseCps(settings: CpsPluginSettings,
//selectRecord.changedReturnType = nTpt
given CpsTopLevelContext = tc
val ctx1: Context = summon[Context].withOwner(tree.symbol)
if (debugSettings.printCode) then
Log.info(s"transformDefDefIntenal: ${tree.show}",0, tree.srcPos)
val transformedRhs = RootTransform(tree.rhs,tree.symbol, 0)(using ctx1, tc).transformed
val nRhs = Block(monadValDef::Nil,transformedRhs)(using ctx1)
val adoptedRhs = Scaffolding.adoptUncpsedRhs(nRhs, tree.tpt.tpe, tc.monadType)
val retval = cpy.DefDef(tree)(tree.name, tree.paramss, tree.tpt, adoptedRhs)
if (debugSettings.printCode) then
Log.info(s"transformDefDefInternal: transformed: ${retval.show}",0, tree.srcPos)
retval
case RETURN_CONTEXT_FUN(internalKind) =>
val cpsDirectContext = ref(selectRecord.kind.getCpsDirectContext.symbol)
Expand All @@ -104,12 +106,17 @@ class PhaseCps(settings: CpsPluginSettings,
tree
case _ =>
throw CpsTransformException("Lambda function was expected, we have $tree",tree.srcPos)
if (debugSettings.printCode) then
report.log(s"transforned: ${retval.show}",tree.srcPos)
report.log(s"transforned: ${retval}",tree.srcPos)
retval
}

override def prepareForApply(tree: tpd.Apply)(using Context): Context = {
if (summon[Context].phase != this) {
println(s"PhaseCps::prepareForApply, invalid phase = ${summon[Context].phase}")
summon[Context].withPhase(this)
} else {
summon[Context]
}
}

override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree = {
try
Expand Down Expand Up @@ -187,11 +194,11 @@ class PhaseCps(settings: CpsPluginSettings,
case Some(tree) =>
val (tc, monadValDef) = tree match
case CpsDirectHelper.ByInclusionCall(tf, tg, fctx, fginc) =>
makeCpsTopLevelContext(fctx, summon[Context].owner, a.srcPos, DebugSettings.make(a), CpsTransformHelper.cpsMonadContextClassSymbol)
makeCpsTopLevelContext(fctx, summon[Context].owner, a.srcPos, DebugSettings.make(a, settings), CpsTransformHelper.cpsMonadContextClassSymbol)
case CpsDirectHelper.NewCall(fctx) =>
makeCpsTopLevelContext(fctx, summon[Context].owner, a.srcPos, DebugSettings.make(a), CpsTransformHelper.cpsMonadContextClassSymbol)
makeCpsTopLevelContext(fctx, summon[Context].owner, a.srcPos, DebugSettings.make(a, settings), CpsTransformHelper.cpsMonadContextClassSymbol)
case other =>
makeCpsTopLevelContext(other, summon[Context].owner, a.srcPos, DebugSettings.make(a), CpsTransformHelper.cpsDirectAliasSymbol)
makeCpsTopLevelContext(other, summon[Context].owner, a.srcPos, DebugSettings.make(a, settings), CpsTransformHelper.cpsDirectAliasSymbol)
val nTree = {
given CpsTopLevelContext = tc
RootTransform(a, summon[Context].owner, 0).transformed
Expand All @@ -206,7 +213,8 @@ class PhaseCps(settings: CpsPluginSettings,
super.transformApply(tree)
case Apply(Apply(TypeApply(deferredAsyncCn, List(tp,mtp,mctp)), List(applyTerm)), List(ctx))
if (deferredAsyncCn.symbol == Symbols.requiredMethod("cps.plugin.scaffolding.deferredAsync")) =>
val (tc, monadValDef) = makeCpsTopLevelContext(ctx,summon[Context].owner, tree.srcPos, DebugSettings.make(tree), CpsTransformHelper.cpsMonadContextClassSymbol)
val (tc, monadValDef) = makeCpsTopLevelContext(ctx,summon[Context].owner, tree.srcPos,
DebugSettings.make(tree, settings), CpsTransformHelper.cpsMonadContextClassSymbol)
val nApplyTerm = {
given CpsTopLevelContext = tc
RootTransform(applyTerm, summon[Context].owner, 0).transformed
Expand Down Expand Up @@ -234,7 +242,12 @@ class PhaseCps(settings: CpsPluginSettings,
val contextParam = cpsMonadContext match
case vd: ValDef => ref(vd.symbol)
case _ => throw CpsTransformException(s"excepted that cpsMonadContext is ValDef, but we have ${cpsMonadContext.show}", asyncCallTree.srcPos)
val (tctx, monadValDef) = makeCpsTopLevelContext(contextParam, ddef.symbol, asyncCallTree.srcPos, DebugSettings.make(asyncCallTree), CpsTransformHelper.cpsMonadContextClassSymbol)
val (tctx, monadValDef) = makeCpsTopLevelContext(contextParam, ddef.symbol, asyncCallTree.srcPos,
DebugSettings.make(ddef, settings), CpsTransformHelper.cpsMonadContextClassSymbol)
if (tctx.debugSettings.printCode) {
Log.info(s"transformDefDefInsideAsync: ${ddef.show}", 0, ddef.srcPos)(using ctx, tctx)
Log.info(s"transformDefDefInsideAsync: body: ${ddef.rhs.show}", 0, ddef.srcPos)(using ctx, tctx)
}
val ddefCtx = ctx.withOwner(ddef.symbol)
val nRhsCps = RootTransform(ddef.rhs, ddef.symbol, 0)(using ddefCtx, tctx)
val nRhsTerm = wrapTopLevelCpsTree(nRhsCps)(using ddefCtx, tctx)
Expand All @@ -259,7 +272,10 @@ class PhaseCps(settings: CpsPluginSettings,
// case Some(tree) => println(s"err::symbol still have old owner: ${tree.show}")
// case None =>
//}

if (tctx.debugSettings.printCode) {
Log.info(s"transformDefDefInsideAsync: transformed: ${nDefDef.show}", 0, ddef.srcPos)(using ctx, tctx)
Log.info(s"transformDefDefInsideAsync: transformed body: ${nDefDef.rhs.show}", 0, ddef.srcPos)(using ctx, tctx)
}
nDefDef
}

Expand Down Expand Up @@ -313,7 +329,8 @@ class PhaseCps(settings: CpsPluginSettings,
Inlined(call, env, transformDefDef2InsideCpsAsyncStreamApply(body, ctxRef))
case Block((ddef: DefDef)::Nil, closure: Closure) if (ddef.symbol == closure.meth.symbol) =>
//val monadType = CpsTransformHelper.extractMonadType(cpsMonadContext.tpe.widen, CpsTransformHelper.cpsMonadContextClassSymbol, asyncCallTree.srcPos)
val (tctx, monadValDef) = makeCpsTopLevelContext(ctxRef, ddef.symbol, ddef.rhs.srcPos, DebugSettings.make(ddef), CpsTransformHelper.cpsMonadContextClassSymbol)
val (tctx, monadValDef) = makeCpsTopLevelContext(ctxRef, ddef.symbol, ddef.rhs.srcPos,
DebugSettings.make(ddef, settings), CpsTransformHelper.cpsMonadContextClassSymbol)
val ddefContext = ctx.withOwner(ddef.symbol)
val nRhsCps = RootTransform(ddef.rhs, ddef.symbol, 0)(using ddefContext, tctx)
val nRhs = Block(monadValDef.changeOwner(monadValDef.symbol.owner,ddef.symbol)::Nil, nRhsCps.transformed(using ddefContext, tctx))
Expand Down
Loading

0 comments on commit 69a289b

Please sign in to comment.