Skip to content

Commit e4b8544

Browse files
committed
wip
1 parent 9e3fdb9 commit e4b8544

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+501
-379
lines changed

compiler/src/dotty/tools/dotc/quoted/ExprCompilationUnit.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@ import dotty.tools.dotc.util.NoSource
66
import scala.quoted.Expr
77

88
/* Compilation unit containing the contents of a quoted expression */
9-
class ExprCompilationUnit(val expr: Expr[_]) extends CompilationUnit(NoSource) {
10-
override def toString: String = s"Expr($expr)"
11-
}
9+
class ExprCompilationUnit(val expr: scala.quoted.QuoteContext => Expr[_]) extends CompilationUnit(NoSource)

compiler/src/dotty/tools/dotc/quoted/QuoteCompiler.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class QuoteCompiler extends Compiler {
5050
case exprUnit: ExprCompilationUnit =>
5151
val tree =
5252
if (putInClass) inClass(exprUnit.expr)
53-
else PickledQuotes.quotedExprToTree(exprUnit.expr)
53+
else PickledQuotes.quotedExprToTree(exprUnit.expr.apply(new QuoteContext(ctx)))
5454
val source = SourceFile.virtual("<quoted.Expr>", "")
5555
CompilationUnit(source, tree, forceTrees = true)
5656
case typeUnit: TypeCompilationUnit =>
@@ -65,7 +65,7 @@ class QuoteCompiler extends Compiler {
6565
* with the following format.
6666
* `package __root__ { class ' { def apply: Any = <expr> } }`
6767
*/
68-
private def inClass(expr: Expr[_])(implicit ctx: Context): Tree = {
68+
private def inClass(expr: scala.quoted.QuoteContext => Expr[_])(implicit ctx: Context): Tree = {
6969
val pos = Span(0)
7070
val assocFile = new VirtualFile("<quote>")
7171

@@ -74,7 +74,7 @@ class QuoteCompiler extends Compiler {
7474
cls.enter(ctx.newDefaultConstructor(cls), EmptyScope)
7575
val meth = ctx.newSymbol(cls, nme.apply, Method, ExprType(defn.AnyType), coord = pos).entered
7676

77-
val quoted = PickledQuotes.quotedExprToTree(expr)(ctx.withOwner(meth))
77+
val quoted = PickledQuotes.quotedExprToTree(expr.apply(new QuoteContext(ctx)))(ctx.withOwner(meth))
7878

7979
val run = DefDef(meth, quoted)
8080
val classTree = ClassDef(cls, DefDef(cls.primaryConstructor.asTerm), run :: Nil)
@@ -85,7 +85,7 @@ class QuoteCompiler extends Compiler {
8585
}
8686

8787
class ExprRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
88-
def compileExpr(expr: Expr[_]): Unit = {
88+
def compileExpr(expr: scala.quoted.QuoteContext => Expr[_]): Unit = {
8989
val units = new ExprCompilationUnit(expr) :: Nil
9090
compileUnits(units)
9191
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dotty.tools.dotc
2+
package quoted
3+
4+
import dotty.tools.dotc.ast.tpd._
5+
import dotty.tools.dotc.core.Contexts._
6+
import dotty.tools.dotc.core.quoted.PickledQuotes
7+
8+
import scala.annotation.implicitNotFound
9+
10+
@implicitNotFound("Could not find implicit scala.quoted.QuoteContext. TODO")
11+
class QuoteContext(ctx: Context) extends scala.quoted.QuoteContext {
12+
13+
def show[T](expr: scala.quoted.Expr[T]): String =
14+
doShow(PickledQuotes.quotedExprToTree(expr)(ctx), ctx)
15+
16+
def show[T](tpe: scala.quoted.Type[T]): String =
17+
doShow(PickledQuotes.quotedTypeToTree(tpe)(ctx), ctx)
18+
19+
private def doShow(tree: Tree, ctx: Context): String = {
20+
implicit val c: Context = ctx
21+
val tree1 =
22+
if (ctx.settings.YshowRawQuoteTrees.value) tree
23+
else (new TreeCleaner).transform(tree)
24+
tastyreflect.ReflectionImpl.showTree(tree1)
25+
}
26+
}

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class QuoteDriver(appClassloader: ClassLoader) extends Driver {
2020

2121
private[this] val contextBase: ContextBase = new ContextBase
2222

23-
def run[T](expr: Expr[T], settings: Toolbox.Settings): T = {
23+
def run[T](expr: scala.quoted.QuoteContext => Expr[T], settings: Toolbox.Settings): T = {
2424
val outDir: AbstractFile = settings.outDir match {
2525
case Some(out) =>
2626
val dir = Directory(out)
@@ -55,23 +55,23 @@ class QuoteDriver(appClassloader: ClassLoader) extends Driver {
5555
ReflectionImpl.showTree(tree1)
5656
}
5757

58-
def show(expr: Expr[_], settings: Toolbox.Settings): String =
59-
withTree(expr, doShow, settings)
60-
61-
def show(tpe: Type[_], settings: Toolbox.Settings): String =
62-
withTypeTree(tpe, doShow, settings)
63-
64-
def withTree[T](expr: Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
65-
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)
66-
67-
var output: Option[T] = None
68-
def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
69-
assert(output.isEmpty)
70-
output = Some(f(tree, ctx))
71-
}
72-
new QuoteDecompiler(registerTree).newRun(ctx).compileExpr(expr)
73-
output.getOrElse(throw new Exception("Could not extract " + expr))
74-
}
58+
// def show(expr: scala.quoted.QuoteContext => Expr[_], settings: Toolbox.Settings): String =
59+
// withTree(expr, doShow, settings)
60+
//
61+
// def show(tpe: Type[_], settings: Toolbox.Settings): String =
62+
// withTypeTree(tpe, doShow, settings)
63+
//
64+
// def withTree[T](expr: scala.quoted.QuoteContext => Expr[_], f: (Tree, Context) => T, settings: Toolbox.Settings): T = {
65+
// val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)
66+
//
67+
// var output: Option[T] = None
68+
// def registerTree(tree: tpd.Tree)(ctx: Context): Unit = {
69+
// assert(output.isEmpty)
70+
// output = Some(f(tree, ctx))
71+
// }
72+
// new QuoteDecompiler(registerTree).newRun(ctx).compileExpr(expr)
73+
// output.getOrElse(throw new Exception("Could not extract " + expr))
74+
// }
7575

7676
def withTypeTree[T](tpe: Type[_], f: (TypTree, Context) => T, settings: Toolbox.Settings): T = {
7777
val ctx = setToolboxSettings(setup(settings.compilerArgs.toArray :+ "dummy.scala", initCtx.fresh)._2.fresh, settings)

compiler/src/dotty/tools/dotc/quoted/ToolboxImpl.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@ object ToolboxImpl {
1919

2020
private[this] val driver: QuoteDriver = new QuoteDriver(appClassloader)
2121

22-
def run[T](expr: Expr[T]): T = expr match {
23-
case expr: LiftedExpr[T] =>
24-
expr.value
25-
case expr: TastyTreeExpr[Tree] @unchecked =>
26-
throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
22+
def run[T](expr: scala.quoted.QuoteContext => Expr[T]): T = expr match {
23+
// TODO move logic into driver
24+
// case expr: LiftedExpr[T] =>
25+
// expr.value
26+
// case expr: TastyTreeExpr[Tree] @unchecked =>
27+
// throw new Exception("Cannot call `Expr.run` on an `Expr` that comes from a macro argument.")
2728
case _ =>
2829
synchronized(driver.run(expr, settings))
2930
}
3031

31-
def show[T](expr: Expr[T]): String = synchronized(driver.show(expr, settings))
32-
33-
def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
32+
// def show[T](expr: Expr[T]): String = synchronized(driver.show(_ => expr, settings))
33+
//
34+
// def show[T](tpe: Type[T]): String = synchronized(driver.show(tpe, settings))
3435
}
3536

3637
}

library/src-3.x/scala/quoted/Expr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package quoted {
99
* May throw a FreeVariableError on expressions that came from a macro.
1010
*/
1111
@deprecated("Use scala.quoted.run", "")
12-
final def run(implicit toolbox: Toolbox): T = toolbox.run(this)
12+
final def run(implicit toolbox: Toolbox): T = toolbox.run(_ => this)
1313

1414
}
1515

@@ -19,7 +19,7 @@ package quoted {
1919

2020
implicit class ExprOps[T](expr: Expr[T]) {
2121
/** Show a source code like representation of this expression */
22-
def show(implicit toolbox: Toolbox): String = toolbox.show(expr)
22+
def show(implicit qctx: QuoteContext): String = qctx.show(expr)
2323
}
2424

2525
/** Converts a tuple `(T1, ..., Tn)` to `(Expr[T1], ..., Expr[Tn])` */

library/src-3.x/scala/quoted/Type.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package quoted {
1212

1313
implicit object TypeOps {
1414
/** Show a source code like representation of this type */
15-
def (tpe: Type[T]) show[T] given Toolbox: String = the[Toolbox].show(tpe.asInstanceOf[Type[Any]])
15+
def (tpe: Type[T]) show[T] given QuoteContext: String = the[QuoteContext].show(tpe.asInstanceOf[Type[Any]])
1616
}
1717

1818
implicit val UnitTag: Type[Unit] = new TaggedType[Unit]

library/src-3.x/scala/quoted/package.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ package object quoted {
1414
*
1515
* May throw a FreeVariableError on expressions that came from a macro.
1616
*/
17-
def run[T](expr: Expr[T]) given (toolbox: Toolbox): T = toolbox.run(expr)
17+
def run[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): T = toolbox.run(expr given _)
18+
19+
def show[T](expr: given QuoteContext => Expr[T]) given (toolbox: Toolbox): String = run(expr.show.toExpr)
1820

1921
object autolift {
2022
implicit def autoToExpr[T: Liftable](x: T): Expr[T] = x.toExpr
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala.quoted
2+
3+
trait QuoteContext {
4+
5+
def show[T](expr: Expr[T]): String
6+
7+
def show[T](tpe: Type[T]): String
8+
9+
}

library/src/scala/quoted/Toolbox.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import scala.annotation.implicitNotFound
44

55
@implicitNotFound("Could not find implicit quoted.Toolbox.\n\nDefault toolbox can be instantiated with:\n `implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)`\n\n")
66
trait Toolbox {
7-
def run[T](expr: Expr[T]): T
8-
def show[T](expr: Expr[T]): String
9-
def show[T](tpe: Type[T]): String
7+
def run[T](expr: QuoteContext => Expr[T]): T
8+
9+
// @deprecated("TODO", "")
10+
// def show[T](expr: Expr[T]): String
11+
//
12+
// @deprecated("TODO", "")
13+
// def show[T](tpe: Type[T]): String
1014
}
1115

1216
object Toolbox {

0 commit comments

Comments
 (0)