Skip to content

Commit ad80ab6

Browse files
committed
WIP
1 parent e02a991 commit ad80ab6

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
231231
// be duplicated
232232
// 2. To enable correct pickling (calls can share symbols with the inlined code, which
233233
// would trigger an assertion when pickling).
234-
val callTrace = Ident(call.symbol.topLevelClass.typeRef).withPos(call.pos)
234+
val callTrace =
235+
if (call.symbol.is(Macro)) call
236+
else Ident(call.symbol.topLevelClass.typeRef).withPos(call.pos)
235237
cpy.Inlined(tree)(callTrace, transformSub(bindings), transform(expansion))
236238
case tree: Template =>
237239
withNoCheckNews(tree.parents.flatMap(newPart)) {

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ package dotty.tools.dotc
22
package transform
33

44
import core._
5-
import Decorators._, Flags._, Types._, Contexts._, Symbols._, Constants._
5+
import Decorators._
6+
import Flags._
7+
import Types._
8+
import Contexts._
9+
import Symbols._
10+
import Constants._
611
import Flags._
712
import ast.Trees._
813
import ast.{TreeTypeMap, untpd}
@@ -13,6 +18,7 @@ import MegaPhase.MiniPhase
1318
import SymUtils._
1419
import NameKinds._
1520
import dotty.tools.dotc.ast.tpd.Tree
21+
import dotty.tools.dotc.core.DenotTransformers.InfoTransformer
1622
import typer.Implicits.SearchFailureType
1723

1824
import scala.collection.mutable
@@ -57,7 +63,7 @@ import dotty.tools.dotc.core.quoted._
5763
* ```
5864
* and then performs the same transformation on `'{ ... x1$1.unary_~ ... x2$1.unary_~ ...}`.
5965
*/
60-
class ReifyQuotes extends MacroTransformWithImplicits {
66+
class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
6167
import ast.tpd._
6268

6369
override def phaseName: String = "reifyQuotes"
@@ -493,12 +499,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
493499
case tree: Select if tree.symbol.isSplice =>
494500
splice(tree)
495501
case tree: RefTree if isCaptured(tree, level) =>
496-
println("================")
497-
println(tree.show)
498-
println(tree)
499-
println()
500-
println()
501-
println()
502502
val capturer = capturers(tree.symbol)
503503
if (tree.symbol.is(Inline)) capturer(tree)
504504
else splice(capturer(tree).select(if (tree.isTerm) nme.UNARY_~ else tpnme.UNARY_~))
@@ -514,7 +514,20 @@ class ReifyQuotes extends MacroTransformWithImplicits {
514514
}
515515

516516
val tree1 =
517-
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body).withPos(tree.pos)))
517+
if (level == 0) {
518+
println("========================")
519+
println(tree.show)
520+
println()
521+
println(call.show)
522+
println(call)
523+
println()
524+
println()
525+
println()
526+
println()
527+
println()
528+
529+
cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body).withPos(tree.pos)))
530+
}
518531
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name))
519532
val tree2 = transform(tree1)
520533

@@ -526,18 +539,10 @@ class ReifyQuotes extends MacroTransformWithImplicits {
526539
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
527540
markDef(tree)
528541
val reifier = nested(isQuote = true)
529-
val b = reifier.transform(tree)
542+
reifier.transform(tree) // Ignore output, we only need the its embedding
530543
val macroLambda = reifier.embedded.head
531-
println(b.show)
532-
println()
533-
println()
534-
println(macroLambda.show)
535-
println()
536-
println()
537-
println()
538-
println()
539-
// check macro code as it if appeared in a quoted context
540-
cpy.DefDef(tree)(rhs = EmptyTree)
544+
// replace macro code by lambda used to evaluate the macro expansion
545+
cpy.DefDef(tree)(tpt = TypeTree(defn.ObjectType), rhs = macroLambda)
541546
case _ =>
542547
markDef(tree)
543548
checkLevel(mapOverTree(enteredSyms))
@@ -564,6 +569,22 @@ class ReifyQuotes extends MacroTransformWithImplicits {
564569
}
565570
}
566571
}
572+
573+
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type = {
574+
/** Transforms the return type
575+
* inline def foo(...): X = ~(...)
576+
* to
577+
* inline def foo(...): Object = (args: Seq[Any]) => ...
578+
*/
579+
def transform(tp: Type): Type = tp match {
580+
case tp: PolyType => PolyType(tp.paramNames, tp.paramInfos, transform(tp.resType))
581+
case tp: MethodType => MethodType(tp.paramNames, tp.paramInfos, transform(tp.resType))
582+
case _ => defn.ObjectType
583+
}
584+
transform(tp)
585+
}
586+
587+
override protected def mayChange(sym: Symbol)(implicit ctx: Context) = sym.is(Macro)
567588
}
568589

569590
object ReifyQuotes {

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ object Splicer {
2121
*/
2222
def splice(tree: Tree)(implicit ctx: Context): Tree = tree match {
2323
case Quoted(quotedTree) => quotedTree
24-
case _ => reflectiveSplice(tree)
24+
case _ =>
25+
println()
26+
println(tree.show)
27+
println()
28+
println()
29+
println()
30+
reflectiveSplice(tree)
2531
}
2632

2733
/** Splice the Tree for a Quoted expression which is constructed via a reflective call to the given method */

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ class RefChecks extends MiniPhase { thisPhase =>
947947

948948
override def transformDefDef(tree: DefDef)(implicit ctx: Context) = {
949949
checkDeprecatedOvers(tree)
950-
if (tree.symbol is Macro) EmptyTree else tree
950+
tree
951951
}
952952

953953
override def transformTemplate(tree: Template)(implicit ctx: Context) = try {

tests/run/quote-simple-macro/quoted_2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Macros._
33

44
object Test {
55
def main(args: Array[String]): Unit = {
6-
val x = 2
7-
println(foo(1, x))
6+
def x = 2
7+
println(foo(1, 2, x))
88
}
99
}

0 commit comments

Comments
 (0)