Skip to content

Commit 50114ec

Browse files
committed
WIP Extract quote reification from Staging
1 parent 4c5ca04 commit 50114ec

File tree

38 files changed

+896
-450
lines changed

38 files changed

+896
-450
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ class Compiler {
4545

4646
/** Phases dealing with TASTY tree pickling and unpickling */
4747
protected def picklerPhases: List[List[Phase]] =
48+
List(new Staging) :: // Check levels and expand macros
4849
List(new Pickler) :: // Generate TASTY info
49-
List(new Staging) :: // Expand macros and turn quoted trees into explicit run-time data structures
50+
List(new ReifyQuotes) :: // Turn quoted trees into explicit run-time data structures
5051
Nil
5152

5253
/** Phases dealing with the transformation from pickled trees to backend trees */

compiler/src/dotty/tools/dotc/ast/TreeTypeMap.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class TreeTypeMap(
162162
assert(!to.exists(substFrom contains _))
163163
assert(!from.exists(newOwners contains _))
164164
assert(!to.exists(oldOwners contains _))
165-
new TreeTypeMap(
165+
newTreeTypeMap(
166166
typeMap,
167167
treeMap,
168168
from ++ oldOwners,
@@ -171,6 +171,11 @@ class TreeTypeMap(
171171
to ++ substTo)
172172
}
173173

174+
protected def newTreeTypeMap(typeMap: Type => Type, treeMap: tpd.Tree => tpd.Tree,
175+
oldOwners: List[Symbol], newOwners: List[Symbol], substFrom: List[Symbol], substTo: List[Symbol]) = {
176+
new TreeTypeMap(typeMap, treeMap, oldOwners, newOwners, substFrom, substTo)
177+
}
178+
174179
/** Apply `typeMap` and `ownerMap` to given symbols `syms`
175180
* and return a treemap that contains the substitution
176181
* between original and mapped symbols.

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11131113
/** A key to be used in a context property that tracks enclosing inlined calls */
11141114
private val InlinedCalls = new Property.Key[List[Tree]]
11151115

1116+
/** A key to be used in a context property that tracks the quoteation level */
1117+
private val QuotationLevel = new Property.Key[Int]
1118+
11161119
/** Record an enclosing inlined call.
11171120
* EmptyTree calls (for parameters) cancel the next-enclosing call in the list instead of being added to it.
11181121
* We assume parameters are never nested inside parameters.
@@ -1134,6 +1137,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
11341137
def enclosingInlineds(implicit ctx: Context): List[Tree] =
11351138
ctx.property(InlinedCalls).getOrElse(Nil)
11361139

1140+
/** All enclosing calls that are currently inlined, from innermost to outermost. */
1141+
def quotationLevel(implicit ctx: Context): Int =
1142+
ctx.property(QuotationLevel).getOrElse(0)
1143+
1144+
/** Context with an incremented quotation level. */
1145+
def quoteContext(implicit ctx: Context): Context =
1146+
ctx.fresh.setProperty(QuotationLevel, quotationLevel + 1)
1147+
1148+
/** Context with a decremented quotation level. */
1149+
def spliceContext(implicit ctx: Context): Context =
1150+
ctx.fresh.setProperty(QuotationLevel, quotationLevel - 1)
1151+
11371152
/** The source file where the symbol of the `inline` method referred to by `call`
11381153
* is defined
11391154
*/

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
249249
protected def blockText[T >: Untyped](trees: List[Tree[T]]): Text =
250250
("{" ~ toText(trees, "\n") ~ "}").close
251251

252-
protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text =
253-
toTextLocal(tree.fun) ~ "[" ~ toTextGlobal(tree.args, ", ") ~ "]"
252+
protected def typeApplyText[T >: Untyped](tree: TypeApply[T]): Text = {
253+
if (tree.fun.hasType && tree.fun.symbol == defn.QuotedType_apply)
254+
keywordStr("'[") ~ toTextGlobal(tree.args, ", ") ~ keywordStr("]")
255+
else
256+
toTextLocal(tree.fun) ~ "[" ~ toTextGlobal(tree.args, ", ") ~ "]"
257+
}
254258

255259
protected def toTextCore[T >: Untyped](tree: Tree[T]): Text = {
256260
import untpd.{modsDeco => _, _}
@@ -318,7 +322,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
318322
if (name.isTypeName) typeText(txt)
319323
else txt
320324
case tree @ Select(qual, name) =>
321-
if (tree.hasType && tree.symbol == defn.QuotedExpr_~ || tree.symbol == defn.QuotedType_~) keywordStr("~(") ~ toTextLocal(qual) ~ keywordStr(")")
325+
if (tree.hasType && tree.symbol == defn.QuotedExpr_~) keywordStr("~(") ~ toTextLocal(qual) ~ keywordStr(")")
326+
else if (tree.hasType && tree.symbol == defn.QuotedType_~) typeText("~(") ~ toTextLocal(qual) ~ typeText(")")
322327
else if (qual.isType) toTextLocal(qual) ~ "#" ~ typeText(toText(name))
323328
else toTextLocal(qual) ~ ("." ~ nameIdText(tree) provided name != nme.CONSTRUCTOR)
324329
case tree: This =>
@@ -332,8 +337,6 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
332337
}
333338
else if (fun.hasType && fun.symbol == defn.QuotedExpr_apply)
334339
keywordStr("'{") ~ toTextGlobal(args, ", ") ~ keywordStr("}")
335-
else if (fun.hasType && fun.symbol == defn.QuotedType_apply)
336-
keywordStr("'[") ~ toTextGlobal(args, ", ") ~ keywordStr("]")
337340
else
338341
toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"
339342
case tree: TypeApply =>

compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with CoreImpl with Helpers
776776
tpd.Return(expr, ctx.owner)
777777

778778
def copy(original: Tree)(expr: Term)(implicit ctx: Context): Return =
779-
tpd.cpy.Return(original)(expr, tpd.EmptyTree)
779+
tpd.cpy.Return(original)(expr, tpd.ref(ctx.owner))
780780

781781
def unapply(x: Term)(implicit ctx: Context): Option[Term] = x match {
782782
case x: tpd.Return => Some(x.expr)

0 commit comments

Comments
 (0)