Skip to content

Commit 655a24f

Browse files
committed
Move Embedded to ReifyQuotes
1 parent 40621e8 commit 655a24f

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import dotty.tools.dotc.util.SourcePosition
5757
*/
5858
class ReifyQuotes extends MacroTransformWithImplicits {
5959
import tpd._
60-
import Staging._
60+
import ReifyQuotes._
6161

6262
override def phaseName: String = ReifyQuotes.name
6363

@@ -199,7 +199,7 @@ class ReifyQuotes extends MacroTransformWithImplicits {
199199
else if (body.symbol == defn.DoubleClass) tag("DoubleTag")
200200
else pickleAsTasty()
201201
}
202-
else Staging.toValue(body) match {
202+
else ReifyQuotes.toValue(body) match {
203203
case Some(value) => pickleAsValue(value)
204204
case _ => pickleAsTasty()
205205
}
@@ -386,6 +386,40 @@ class ReifyQuotes extends MacroTransformWithImplicits {
386386

387387

388388
object ReifyQuotes {
389+
389390
val name: String = "reifyQuotes"
391+
392+
def toValue(tree: tpd.Tree): Option[Any] = tree match {
393+
case Literal(Constant(c)) => Some(c)
394+
case Block(Nil, e) => toValue(e)
395+
case Inlined(_, Nil, e) => toValue(e)
396+
case _ => None
397+
}
398+
399+
class Embedded(trees: mutable.ListBuffer[tpd.Tree] = mutable.ListBuffer.empty, map: mutable.Map[Symbol, tpd.Tree] = mutable.Map.empty) {
400+
/** Adds the tree and returns it's index */
401+
def addTree(tree: tpd.Tree, liftedSym: Symbol): Int = {
402+
trees += tree
403+
if (liftedSym ne NoSymbol)
404+
map.put(liftedSym, tree)
405+
trees.length - 1
406+
}
407+
408+
/** Type used for the hole that will replace this splice */
409+
def getHoleType(splice: tpd.Select)(implicit ctx: Context): Type = {
410+
// For most expressions the splice.tpe but there are some types that are lost by lifting
411+
// that can be recoverd from the original tree. Currently the cases are:
412+
// * Method types: the splice represents a method reference
413+
map.get(splice.qualifier.symbol).map(_.tpe.widen).getOrElse(splice.tpe)
414+
}
415+
416+
def isLiftedSymbol(sym: Symbol)(implicit ctx: Context): Boolean = map.contains(sym)
417+
418+
/** Get the list of embedded trees */
419+
def getTrees: List[tpd.Tree] = trees.toList
420+
421+
override def toString: String = s"Embedded($trees, $map)"
422+
423+
}
390424
}
391425

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

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Staging extends MacroTransformWithImplicits {
4949
if (ctx.compilationUnit.needsStaging) super.run
5050

5151
protected def newTransformer(implicit ctx: Context): Transformer =
52-
new Reifier(inQuote = false, null, 0, new LevelInfo, new Embedded, ctx)
52+
new Reifier(inQuote = false, null, 0, new LevelInfo, ctx)
5353

5454
private class LevelInfo {
5555
/** A map from locally defined symbols to the staging levels of their definitions */
@@ -63,18 +63,16 @@ class Staging extends MacroTransformWithImplicits {
6363
* The initial level is 0, a level `l` where `l > 0` implies code has been quoted `l` times
6464
* and `l == -1` is code inside a top level splice (in an inline method).
6565
* @param levels a stacked map from symbols to the levels in which they were defined
66-
* @param embedded a list of embedded quotes (if `inSplice = true`) or splices (if `inQuote = true`
6766
* @param rctx the contex in the destination lifted lambda
6867
*/
6968
private class Reifier(inQuote: Boolean, val outer: Reifier, val level: Int, levels: LevelInfo,
70-
val embedded: Embedded, val rctx: Context) extends ImplicitsTransformer {
69+
val rctx: Context) extends ImplicitsTransformer {
7170
import levels._
7271
assert(level >= -1)
7372

7473
/** A nested reifier for a quote (if `isQuote = true`) or a splice (if not) */
7574
def nested(isQuote: Boolean)(implicit ctx: Context): Reifier = {
76-
val nestedEmbedded = if (level > 1 || (level == 1 && isQuote)) embedded else new Embedded
77-
new Reifier(isQuote, this, if (isQuote) level + 1 else level - 1, levels, nestedEmbedded, ctx)
75+
new Reifier(isQuote, this, if (isQuote) level + 1 else level - 1, levels, ctx)
7876
}
7977

8078
/** We are in a `~(...)` context that is not shadowed by a nested `'(...)` */
@@ -451,39 +449,5 @@ class Staging extends MacroTransformWithImplicits {
451449
}
452450

453451
object Staging {
454-
455452
val name: String = "staging"
456-
457-
def toValue(tree: tpd.Tree): Option[Any] = tree match {
458-
case Literal(Constant(c)) => Some(c)
459-
case Block(Nil, e) => toValue(e)
460-
case Inlined(_, Nil, e) => toValue(e)
461-
case _ => None
462-
}
463-
464-
class Embedded(trees: mutable.ListBuffer[tpd.Tree] = mutable.ListBuffer.empty, map: mutable.Map[Symbol, tpd.Tree] = mutable.Map.empty) {
465-
/** Adds the tree and returns it's index */
466-
def addTree(tree: tpd.Tree, liftedSym: Symbol): Int = {
467-
trees += tree
468-
if (liftedSym ne NoSymbol)
469-
map.put(liftedSym, tree)
470-
trees.length - 1
471-
}
472-
473-
/** Type used for the hole that will replace this splice */
474-
def getHoleType(splice: tpd.Select)(implicit ctx: Context): Type = {
475-
// For most expressions the splice.tpe but there are some types that are lost by lifting
476-
// that can be recoverd from the original tree. Currently the cases are:
477-
// * Method types: the splice represents a method reference
478-
map.get(splice.qualifier.symbol).map(_.tpe.widen).getOrElse(splice.tpe)
479-
}
480-
481-
def isLiftedSymbol(sym: Symbol)(implicit ctx: Context): Boolean = map.contains(sym)
482-
483-
/** Get the list of embedded trees */
484-
def getTrees: List[tpd.Tree] = trees.toList
485-
486-
override def toString: String = s"Embedded($trees, $map)"
487-
488-
}
489453
}

0 commit comments

Comments
 (0)