@@ -8,42 +8,20 @@ import dotty.tools.dotc.core.Contexts._
8
8
import dotty .tools .dotc .core .StagingContext ._
9
9
import dotty .tools .dotc .core .Symbols ._
10
10
import dotty .tools .dotc .util .Property
11
- import dotty .tools .dotc .staging .StagingLevel
11
+ import dotty .tools .dotc .staging .StagingLevel . *
12
12
13
13
import scala .collection .mutable
14
14
15
- /** The main transformer class
16
- * @param level the current level, where quotes add one and splices subtract one level.
17
- * The initial level is 0, a level `l` where `l > 0` implies code has been quoted `l` times
18
- * and `l == -1` is code inside a top level splice (in an inline method).
19
- * @param levels a stacked map from symbols to the levels in which they were defined
20
- */
15
+ /** TreeMap that keeps track of staging levels using StagingLevel. */
21
16
abstract class TreeMapWithStages extends TreeMapWithImplicits {
22
17
import tpd ._
23
18
24
- /** A stack of entered symbols, to be unwound after scope exit */
25
- private [this ] var enteredSyms : List [Symbol ] = Nil
26
-
27
19
/** If we are inside a quote or a splice */
28
20
private [this ] var inQuoteOrSplice = false
29
21
30
- /** Locally defined symbols seen so far by `StagingTransformer.transform` */
31
- protected def localSymbols : List [Symbol ] = enteredSyms
32
-
33
22
/** If we are inside a quote or a splice */
34
23
protected def isInQuoteOrSplice : Boolean = inQuoteOrSplice
35
24
36
- /** Enter staging level of symbol defined by `tree` */
37
- private def markSymbol (sym : Symbol )(using Context ): Unit =
38
- if StagingLevel .markSymbol(sym) then
39
- enteredSyms = sym :: enteredSyms
40
-
41
- /** Enter staging level of symbol defined by `tree`, if applicable. */
42
- private def markDef (tree : Tree )(using Context ): Unit = tree match {
43
- case tree : DefTree => markSymbol(tree.symbol)
44
- case _ =>
45
- }
46
-
47
25
/** Transform the quote `quote` which contains the quoted `body`.
48
26
*
49
27
* - `quoted.runtime.Expr.quote[T](<body0>)` --> `quoted.runtime.Expr.quote[T](<body>)`
@@ -66,14 +44,6 @@ abstract class TreeMapWithStages extends TreeMapWithImplicits {
66
44
if (tree.source != ctx.source && tree.source.exists)
67
45
transform(tree)(using ctx.withSource(tree.source))
68
46
else reporting.trace(i " StagingTransformer.transform $tree at $level" , staging, show = true ) {
69
- def mapOverTree (lastEntered : List [Symbol ]) =
70
- try super .transform(tree)
71
- finally
72
- while (enteredSyms ne lastEntered) {
73
- StagingLevel .removeLevelOf(enteredSyms.head)
74
- enteredSyms = enteredSyms.tail
75
- }
76
-
77
47
def dropEmptyBlocks (tree : Tree ): Tree = tree match {
78
48
case Block (Nil , expr) => dropEmptyBlocks(expr)
79
49
case _ => tree
@@ -118,26 +88,30 @@ abstract class TreeMapWithStages extends TreeMapWithImplicits {
118
88
finally inQuoteOrSplice = old
119
89
120
90
case Block (stats, _) =>
121
- val last = enteredSyms
122
- stats.foreach(markDef)
123
- mapOverTree(last)
91
+ val defSyms = stats.collect { case defTree : DefTree => defTree.symbol }
92
+ super .transform(tree)(using symbolsInCurrentLevel(defSyms))
124
93
125
94
case CaseDef (pat, guard, body) =>
126
- val last = enteredSyms
127
- tpd.patVars(pat).foreach(markSymbol)
128
- mapOverTree(last)
95
+ super .transform(tree)(using symbolsInCurrentLevel(tpd.patVars(pat)))
129
96
130
97
case (_:Import | _:Export ) =>
131
98
tree
132
99
133
100
case _ : Template =>
134
- val last = enteredSyms
135
- tree.symbol.owner.info.decls.foreach(markSymbol)
136
- mapOverTree(last)
101
+ val decls = tree.symbol.owner.info.decls.toList
102
+ super .transform(tree)(using symbolsInCurrentLevel(decls))
103
+
104
+ case LambdaTypeTree (tparams, body) =>
105
+ super .transform(tree)(using symbolsInCurrentLevel(tparams.map(_.symbol)))
106
+
107
+ case tree : DefTree =>
108
+ val paramSyms = tree match
109
+ case tree : DefDef => tree.paramss.flatten.map(_.symbol)
110
+ case _ => Nil
111
+ super .transform(tree)(using symbolsInCurrentLevel(tree.symbol :: paramSyms))
137
112
138
113
case _ =>
139
- markDef(tree)
140
- mapOverTree(enteredSyms)
114
+ super .transform(tree)
141
115
}
142
116
}
143
117
}
0 commit comments