@@ -93,7 +93,7 @@ class Staging extends MacroTransformWithImplicits {
93
93
if (ctx.compilationUnit.needsStaging) super .run
94
94
95
95
protected def newTransformer (implicit ctx : Context ): Transformer =
96
- new Reifier (inQuote = false , null , 0 , new LevelInfo , new Embedded , ctx)
96
+ new Reifier (inQuote = false , null , 0 , new LevelInfo , ctx)
97
97
98
98
private class LevelInfo {
99
99
/** A map from locally defined symbols to the staging levels of their definitions */
@@ -110,15 +110,14 @@ class Staging extends MacroTransformWithImplicits {
110
110
* @param embedded a list of embedded quotes (if `inSplice = true`) or splices (if `inQuote = true`
111
111
* @param rctx the contex in the destination lifted lambda
112
112
*/
113
- private class Reifier (inQuote : Boolean , val outer : Reifier , val level : Int , levels : LevelInfo ,
114
- val embedded : Embedded , val rctx : Context ) extends ImplicitsTransformer {
113
+ private class Reifier (inQuote : Boolean , val outer : Reifier , val level : Int , levels : LevelInfo , val rctx : Context )
114
+ extends ImplicitsTransformer {
115
115
import levels ._
116
116
assert(level >= - 1 )
117
117
118
118
/** A nested reifier for a quote (if `isQuote = true`) or a splice (if not) */
119
119
def nested (isQuote : Boolean )(implicit ctx : Context ): Reifier = {
120
- val nestedEmbedded = if (level > 1 || (level == 1 && isQuote)) embedded else new Embedded
121
- new Reifier (isQuote, this , if (isQuote) level + 1 else level - 1 , levels, nestedEmbedded, ctx)
120
+ new Reifier (isQuote, this , if (isQuote) level + 1 else level - 1 , levels, ctx)
122
121
}
123
122
124
123
/** A map from type ref T to expressions of type `quoted.Type[T]`".
@@ -130,106 +129,6 @@ class Staging extends MacroTransformWithImplicits {
130
129
/** A stack of entered symbols, to be unwound after scope exit */
131
130
var enteredSyms : List [Symbol ] = Nil
132
131
133
- /** Assuming importedTags = `Type1 -> tag1, ..., TypeN -> tagN`, the expression
134
- *
135
- * { type <Type1> = <tag1>.unary_~
136
- * ...
137
- * type <TypeN> = <tagN>.unary_~
138
- * <expr>
139
- * }
140
- *
141
- * references to `TypeI` in `expr` are rewired to point to the locally
142
- * defined versions. As a side effect, prepend the expressions `tag1, ..., `tagN`
143
- * as splices to `embedded`.
144
- */
145
- private def addTags (expr : Tree )(implicit ctx : Context ): Tree = {
146
-
147
- if (importedTags.isEmpty) expr
148
- else {
149
- val itags = importedTags.toList
150
- // The tree of the tag for each tag comes from implicit search in `tryHeal`
151
- val healedTypes = for ((tref, tag) <- itags) yield {
152
- tag.select(tpnme.UNARY_~ )
153
- }
154
- importedTags.clear()
155
-
156
- // Maps type splices to type references of tags e.g., ~t -> some type T$1
157
- val map : Map [Type , Tree ] = {
158
- (itags.map(_._1) zip healedTypes)
159
- }.toMap
160
- val tpMap = new TypeMap () {
161
- override def apply (tp : Type ): Type = map.get(tp).fold(mapOver(tp))(_.tpe)
162
- }
163
-
164
- def trMap (tree : Tree ): Tree =
165
- if (tree.isType) map.getOrElse(tree.tpe, tree)
166
- else tree
167
-
168
-
169
- /** Type tree map that does not tag type at level 0 */
170
- class QuoteTreeTypeMap (
171
- typeMap : Type => Type = IdentityTypeMap ,
172
- treeMap : tpd.Tree => tpd.Tree = identity _,
173
- oldOwners : List [Symbol ] = Nil ,
174
- newOwners : List [Symbol ] = Nil ,
175
- substFrom : List [Symbol ] = Nil ,
176
- substTo : List [Symbol ] = Nil
177
- )(implicit ctx : Context ) extends TreeTypeMap (typeMap, treeMap, oldOwners, newOwners, substFrom, substTo) { self =>
178
-
179
- protected var level = 1 // TODO use context to keep track of the level
180
-
181
- override def transform (tree : tpd.Tree )(implicit ctx : Context ): tpd.Tree = {
182
- if (level == 0 ) {
183
- // Keep transforming but do not replace insert the taged types. Types in nested quotes are also not taged.
184
- val (sFrom, sTo) = substFrom.zip(substTo).filterNot(_._2.isSplice).unzip // TODO Syntetic is probably not enugh to distinguish added types
185
-
186
- new TreeTypeMap (
187
- oldOwners = oldOwners, newOwners = newOwners,
188
- substFrom = sFrom, substTo = sTo
189
- ).transform(tree)
190
- }
191
- else
192
- if (tree.symbol.isSplice) {
193
- level -= 1
194
- try {
195
- val tr = super .transform(tree)
196
- if (tr.isType) tr
197
- else {
198
- // Make sure that the tags are not used inside the splice
199
- // and that the non aliased type does not escape the splice
200
- val tp = tpMap(tr.tpe.widenTermRefExpr)
201
- tr.withType(tree.tpe) // FIXME tp may need tranfomation. Revert diff in tests/run/gestalt-optional-staging/Macro_1.scala
202
- }
203
- }
204
- //
205
- finally level += 1
206
- } else if (tree.symbol.isQuote) {
207
- level += 1
208
- try super .transform(tree)
209
- finally level -= 1
210
- }
211
- else super .transform(tree)
212
-
213
- }
214
-
215
- protected override def newTreeTypeMap (typeMap : Type => Type , treeMap : tpd.Tree => tpd.Tree ,
216
- oldOwners : List [Symbol ], newOwners : List [Symbol ],
217
- substFrom : List [Symbol ], substTo : List [Symbol ]) = {
218
- new QuoteTreeTypeMap (typeMap, treeMap, oldOwners, newOwners, substFrom, substTo) {
219
- level = self.level
220
- }
221
- }
222
- }
223
-
224
- new QuoteTreeTypeMap (
225
- treeMap = trMap,
226
- typeMap = tpMap,
227
- substFrom = itags.map(_._1.symbol),
228
- substTo = healedTypes.map(_.symbol)
229
- ).apply(expr)
230
- }
231
- }
232
-
233
132
/** Enter staging level of symbol defined by `tree`, if applicable. */
234
133
def markDef (tree : Tree )(implicit ctx : Context ): Unit = tree match {
235
134
case tree : DefTree =>
@@ -401,7 +300,7 @@ class Staging extends MacroTransformWithImplicits {
401
300
if (isType) ref(defn.QuotedType_apply ).appliedToType(body1.tpe.widen)
402
301
else ref(defn.QuotedExpr_apply ).appliedToType(body1.tpe.widen).appliedTo(body1)
403
302
} else {
404
- val body1 = nested(isQuote = true ).transformAndAddTags (body)
303
+ val body1 = nested(isQuote = true ).transform (body)
405
304
if (level == 0 && ! ctx.inInlineMethod) {
406
305
quote match {
407
306
case quote : Apply => cpy.Apply (quote)(quote.fun, body1 :: Nil )
@@ -445,13 +344,6 @@ class Staging extends MacroTransformWithImplicits {
445
344
}
446
345
}
447
346
448
- /** Transform `tree` followed by `addTags` transform. */
449
- private def transformAndAddTags (tree : Tree )(implicit ctx : Context ): Tree = {
450
- assert(inQuote)
451
- // addTags(transform(tree))
452
- transform(tree)
453
- }
454
-
455
347
override def transform (tree : Tree )(implicit ctx : Context ): Tree =
456
348
reporting.trace(i " reify $tree at $level" , show = true ) {
457
349
def mapOverTree (lastEntered : List [Symbol ]) =
@@ -547,40 +439,5 @@ class Staging extends MacroTransformWithImplicits {
547
439
}
548
440
549
441
object Staging {
550
- import tpd ._
551
-
552
442
val name : String = " staging"
553
-
554
- def toValue (tree : tpd.Tree ): Option [Any ] = tree match {
555
- case Literal (Constant (c)) => Some (c)
556
- case Block (Nil , e) => toValue(e)
557
- case Inlined (_, Nil , e) => toValue(e)
558
- case _ => None
559
- }
560
-
561
- class Embedded (trees : mutable.ListBuffer [tpd.Tree ] = mutable.ListBuffer .empty, map : mutable.Map [Symbol , tpd.Tree ] = mutable.Map .empty) {
562
- /** Adds the tree and returns it's index */
563
- def addTree (tree : tpd.Tree , liftedSym : Symbol ): Int = {
564
- trees += tree
565
- if (liftedSym ne NoSymbol )
566
- map.put(liftedSym, tree)
567
- trees.length - 1
568
- }
569
-
570
- /** Type used for the hole that will replace this splice */
571
- def getHoleType (splice : tpd.Select )(implicit ctx : Context ): Type = {
572
- // For most expressions the splice.tpe but there are some types that are lost by lifting
573
- // that can be recoverd from the original tree. Currently the cases are:
574
- // * Method types: the splice represents a method reference
575
- map.get(splice.qualifier.symbol).map(_.tpe.widen).getOrElse(splice.tpe)
576
- }
577
-
578
- def isLiftedSymbol (sym : Symbol )(implicit ctx : Context ): Boolean = map.contains(sym)
579
-
580
- /** Get the list of embedded trees */
581
- def getTrees : List [tpd.Tree ] = trees.toList
582
-
583
- override def toString : String = s " Embedded( $trees, $map) "
584
-
585
- }
586
443
}
0 commit comments