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 {

tests/run-with-compiler/i3823-b.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ object Test {
55
val z: $t = $x
66
}
77
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
println(f('{2})(Type.IntTag).show)
8+
run {
9+
println(f('{2})(Type.IntTag).show)
10+
'{}
11+
}
912
}
10-
}
13+
}

tests/run-with-compiler/i3823-c.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ object Test {
55
val z = $x
66
}
77
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
println(f('{2})(Type.IntTag).show)
8+
run {
9+
println(f('{2})(Type.IntTag).show)
10+
'{}
11+
}
912
}
1013
}
11-

tests/run-with-compiler/i3823.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ object Test {
55
val z: $t = $x
66
}
77
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
println(f('{2})('[Int]).show)
8+
run {
9+
println(f('{2})('[Int]).show)
10+
'{}
11+
}
912
}
1013
}

tests/run-with-compiler/i3847-b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ object Test {
1818
import Arrays._
1919
implicit val ct: Expr[ClassTag[Int]] = '{ClassTag.Int}
2020
val arr: Expr[Array[List[Int]]] = Array[List[Int]](List(1, 2, 3)).toExpr
21-
println(arr.show)
21+
println(run(arr.show.toExpr))
2222
}
2323
}

tests/run-with-compiler/i3847.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ object Test {
1818
import Arrays._
1919
implicit val ct: Expr[ClassTag[Int]] = '{ClassTag.Int}
2020
val arr: Expr[Array[Int]] = Array[Int](1, 2, 3).toExpr
21-
println(arr.show)
21+
println(run(arr.show.toExpr))
2222
}
23-
}
23+
}

tests/run-with-compiler/i3876-b.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ object Test {
99
def f(x: Int): Int = x + x
1010
f
1111
}
12-
println(run(f2(x)))
13-
println(f2(x).show)
12+
13+
run {
14+
val y = f2(x)
15+
'{
16+
println($y)
17+
println(${y.show.toExpr})
18+
}
19+
}
1420
}
1521
}

tests/run-with-compiler/i3876-c.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ object Test {
99
val f: (x: Int) => Int = x => x + x
1010
f
1111
}
12-
println(run(f3(x)))
13-
println(f3(x).show) // TODO improve printer
12+
13+
run {
14+
val y = f3(x)
15+
'{
16+
println($y)
17+
println(${y.show.toExpr})
18+
}
19+
}
1420
}
1521
}

tests/run-with-compiler/i3876-d.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ object Test {
88
val f4: Expr[Int => Int] = '{
99
inlineLambda
1010
}
11-
println(run(f4(x)))
12-
println(f4(x).show)
11+
12+
run {
13+
val y = f4(x)
14+
'{
15+
println($y)
16+
println(${y.show.toExpr})
17+
}
18+
}
1319
}
1420

1521
inline def inlineLambda <: Int => Int = x => x + x

tests/run-with-compiler/i3876-e.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
6
33
{
4-
val x$1: scala.Int = {
4+
val x$2: scala.Int = {
55
scala.Predef.println()
66
3
77
}
8-
x$1.+(x$1)
8+
x$2.+(x$2)
99
}

tests/run-with-compiler/i3876-e.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ object Test {
88
val f4: Expr[Int => Int] = '{
99
inlineLambda
1010
}
11-
println(run(f4(x)))
12-
println(f4(x).show)
11+
12+
run {
13+
val y = f4(x)
14+
'{
15+
println($y)
16+
println(${y.show.toExpr})
17+
}
18+
}
1319
}
1420

1521
inline def inlineLambda <: Int => Int = x => x + x

tests/run-with-compiler/i3876.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ object Test {
66
val x: Expr[Int] = '{3}
77

88
val f: Expr[Int => Int] = '{ (x: Int) => x + x }
9-
println(run(f(x)))
10-
println(f(x).show)
9+
10+
run {
11+
val y = f(x)
12+
'{
13+
println($y)
14+
println(${y.show.toExpr})
15+
}
16+
}
1117
}
1218
}

tests/run-with-compiler/i3946.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import scala.quoted._
22
object Test {
33
def main(args: Array[String]): Unit = {
44
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
5-
val u: Expr[Unit] = '{}
6-
println(u.show)
7-
println(run(u))
5+
run {
6+
val u: Expr[Unit] = '{}
7+
println(u.show)
8+
'{ println($u) }
9+
}
810
}
911
}

tests/run-with-compiler/i3947.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ object Test {
55

66
def main(args: Array[String]): Unit = {
77
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
8-
def test[T: Type](clazz: java.lang.Class[T]): Unit = {
8+
def test[T: Type](clazz: java.lang.Class[T]): Unit = run {
99
val lclazz = clazz.toExpr
1010
val name = '{ ($lclazz).getCanonicalName }
1111
println()
1212
println(name.show)
13-
println(run(name))
13+
'{ println($name) }
1414
}
1515

1616
// classOf[Object]

0 commit comments

Comments
 (0)