@@ -46,7 +46,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
46
46
47
47
override def phaseName = " patternMatcher"
48
48
49
- var _id = 0
49
+ private var _id = 0 // left for debuging
50
50
51
51
override def transformMatch (tree : tpd.Match )(implicit ctx : Context , info : TransformerInfo ): tpd.Tree = {
52
52
val translated = new Translator ()(ctx).translator.translateMatch(tree)
@@ -61,14 +61,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
61
61
62
62
class OptimizingMatchTranslator extends MatchOptimizer /* (val typer: analyzer.Typer)*/ with MatchTranslator
63
63
64
- trait MatchMonadInterface {
65
- // val typer: Typer
66
- def matchOwner (implicit ctx : Context ) = ctx.owner
67
- def pureType (tp : Type ): Type = tp
68
-
69
- }
70
-
71
- trait CodegenCore extends MatchMonadInterface {
64
+ trait CodegenCore {
72
65
private var ctr = 0 // left for debugging
73
66
74
67
// assert(owner ne null); assert(owner ne NoSymbol)
@@ -91,7 +84,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
91
84
def _isInstanceOf (b : Symbol , tp : Type ): Tree
92
85
def drop (tgt : Tree )(n : Int ): Tree
93
86
def index (tgt : Tree )(i : Int ): Tree
94
- def mkZero (tp : Type ): Tree
95
87
def tupleSel (binder : Symbol )(i : Int ): Tree
96
88
}
97
89
@@ -110,9 +102,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
110
102
def codegen : AbsCodegen
111
103
112
104
abstract class CommonCodegen extends AbsCodegen {
113
- def fun (arg : TermSymbol , body : Tree ): Tree =
114
- DefDef (arg, body)
115
-
116
105
def tupleSel (binder : Symbol )(i : Int ): Tree = ref(binder).select(nme.productAccessorName(i)) // make tree that accesses the i'th component of the tuple referenced by binder
117
106
def index (tgt : Tree )(i : Int ): Tree = {
118
107
if (i > 0 ) tgt.select(defn.Seq_apply ).appliedTo(Literal (Constant (i)))
@@ -138,12 +127,10 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
138
127
// the force is needed mainly to deal with the GADT typing hack (we can't detect it otherwise as tp nor pt need contain an abstract type, we're just casting wildly)
139
128
def _asInstanceOf (b : Symbol , tp : Type ): Tree = ref(b).ensureConforms(tp) // andType here breaks t1048
140
129
def _isInstanceOf (b : Symbol , tp : Type ): Tree = ref(b).select(defn.Any_isInstanceOf ).appliedToType(tp)
141
-
142
- def mkZero (tp : Type ): Tree = initValue(tp)
143
130
}
144
131
}
145
132
146
- trait TypedSubstitution extends MatchMonadInterface {
133
+
147
134
object Substitution {
148
135
def apply (from : Symbol , to : Tree ) = new Substitution (List (from), List (to))
149
136
// requires sameLength(from, to)
@@ -152,13 +139,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
152
139
}
153
140
154
141
class Substitution (val from : List [Symbol ], val to : List [Tree ]) {
155
- val id = {
156
- _id += 1
157
- if (_id == 1168 ) {
158
- println(" here" )
159
- }
160
- _id
161
- }
162
142
163
143
// We must explicitly type the trees that we replace inside some other tree, since the latter may already have been typed,
164
144
// and will thus not be retyped. This means we might end up with untyped subtrees inside bigger, typed trees.
@@ -167,9 +147,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
167
147
// since about half of the typedSubst's end up being no-ops, the check below shaves off 5% of the time spent in typedSubst
168
148
/* if (!tree.exists { case i@Ident(_) => from contains i.symbol case _ => false}) tree
169
149
else*/
170
- if (id == 1169 ) {
171
- println(" here" )
172
- }
173
150
174
151
var replaced = 0
175
152
val toAdapted = (from zip to) map {
@@ -203,23 +180,13 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
203
180
204
181
ctx.debuglog(s " location: ${ctx.owner.showFullName}, size: ${treeSize}" )
205
182
res
206
- /* (new TreeTypeMap() {
207
- override def transform(tree: Tree)(implicit ctx: Context): Tree = {
208
-
209
-
210
- tree match {
211
- case Ident(_) => subst(from, to)
212
- case _ => super.transform(tree)
213
- }
214
- }
215
- }).transform(tree)*/
216
183
}
217
184
218
185
219
186
// the substitution that chains `other` before `this` substitution
220
187
// forall t: Tree. this(other(t)) == (this >> other)(t)
221
188
def >> (other : Substitution ): Substitution = {
222
- if (other == EmptySubstitution ) this
189
+ if (other == EmptySubstitution ) this
223
190
else if (this == EmptySubstitution ) other
224
191
else {
225
192
val (fromFiltered, toFiltered) = (from, to).zipped filter { (f, t) => ! other.from.contains(f)}
@@ -233,16 +200,16 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
233
200
override def apply (tree : Tree ): Tree = tree
234
201
override def >> (other : Substitution ): Substitution = other
235
202
}
236
- }
237
203
238
- trait OptimizedCodegen extends CodegenCore with TypedSubstitution with MatchMonadInterface {
204
+
205
+ trait OptimizedCodegen extends CodegenCore {
239
206
override def codegen : AbsCodegen = optimizedCodegen
240
207
241
208
// when we know we're targetting Option, do some inlining the optimizer won't do
242
209
// for example, `o.flatMap(f)` becomes `if(o == None) None else f(o.get)`, similarly for orElse and guard
243
210
// this is a special instance of the advanced inlining optimization that takes a method call on
244
211
// an object of a type that only has two concrete subclasses, and inlines both bodies, guarded by an if to distinguish the two cases
245
- object optimizedCodegen extends CommonCodegen { // import CODE._
212
+ object optimizedCodegen extends CommonCodegen {
246
213
247
214
/** Inline runOrElse and get rid of Option allocations
248
215
*
@@ -283,10 +250,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
283
250
// scrutSym == NoSymbol when generating an alternatives matcher
284
251
// val scrutDef = scrutSym.fold(List[Tree]())(ValDef(_, scrut) :: Nil) // for alternatives
285
252
286
- // the generated block is taken apart in TailCalls under the following assumptions
287
- // the assumption is once we encounter a case, the remainder of the block will consist of cases
288
- // the prologue may be empty, usually it is the valdef that stores the scrut
289
- // val (prologue, cases) = stats span (s => !s.isInstanceOf[LabelDef])
290
253
caseDefs
291
254
}
292
255
@@ -359,7 +322,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
359
322
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
360
323
// the making of the trees
361
324
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
362
- trait TreeMakers extends TypedSubstitution with CodegenCore {
325
+ trait TreeMakers extends CodegenCore {
363
326
def optimizeCases (prevBinder : Symbol , cases : List [List [TreeMaker ]], pt : Type ): (List [List [TreeMaker ]], List [Tree ])
364
327
def analyzeCases (prevBinder : Symbol , cases : List [List [TreeMaker ]], pt : Type , suppression : Suppression ): Unit = {}
365
328
@@ -758,7 +721,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
758
721
def outerTestNeeded = {
759
722
val np = expectedTp.normalizedPrefix
760
723
val ts = np.termSymbol
761
- (ts ne NoSymbol ) && needsOuterTest(expectedTp, testedBinder.info, matchOwner )
724
+ (ts ne NoSymbol ) && needsOuterTest(expectedTp, testedBinder.info, ctx.owner )
762
725
763
726
}
764
727
@@ -951,40 +914,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
951
914
}
952
915
}
953
916
}
954
-
955
- // TODO: do this during tree construction, but that will require tracking the current owner in treemakers
956
- // TODO: assign more fine-grained positions
957
- // fixes symbol nesting, assigns positions
958
- /* protected def fixerUpper(origOwner: Symbol, pos: Position) = new Traverser {
959
- currentOwner = origOwner
960
-
961
- override def traverse(t: Tree) {
962
- t match {
963
- case Function(_, _) if t.symbol == NoSymbol =>
964
- t.symbol = currentOwner.newAnonymousFunctionValue(t.pos)
965
- debug.patmat("new symbol for "+ ((t, t.symbol.ownerChain)))
966
- case Function(_, _) if (t.symbol.owner == NoSymbol) || (t.symbol.owner == origOwner) =>
967
- debug.patmat("fundef: "+ ((t, t.symbol.ownerChain, currentOwner.ownerChain)))
968
- t.symbol.owner = currentOwner
969
- case d : DefTree if (d.symbol != NoSymbol) && ((d.symbol.owner == NoSymbol) || (d.symbol.owner == origOwner)) => // don't indiscriminately change existing owners! (see e.g., pos/t3440, pos/t3534, pos/unapplyContexts2)
970
- debug.patmat("def: "+ ((d, d.symbol.ownerChain, currentOwner.ownerChain)))
971
-
972
- d.symbol.moduleClass andAlso (_.owner = currentOwner)
973
- d.symbol.owner = currentOwner
974
- // TODO DD:
975
- // case _ if (t.symbol != NoSymbol) && (t.symbol ne null) =>
976
- debug.patmat("untouched "+ ((t, t.getClass, t.symbol.ownerChain, currentOwner.ownerChain)))
977
- case _ =>
978
- }
979
- super.traverse(t)
980
- }
981
-
982
- // override def apply
983
- // debug.patmat("before fixerupper: "+ xTree)
984
- // currentRun.trackerFactory.snapshot()
985
- // debug.patmat("after fixerupper")
986
- // currentRun.trackerFactory.snapshot()
987
- }*/
988
917
}
989
918
990
919
trait MatchOptimizer extends OptimizedCodegen with TreeMakers
@@ -1257,11 +1186,11 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
1257
1186
val pt = match_.tpe.widen // repeatedToSeq(origPt)
1258
1187
1259
1188
// val packedPt = repeatedToSeq(typer.packedType(match_, context.owner))
1260
- val selectorSym = freshSym(selector.pos, pureType( selectorTp) , " selector" )
1189
+ val selectorSym = freshSym(selector.pos, selectorTp, " selector" )
1261
1190
selectorSym.setFlag(Flags .SyntheticCase )
1262
1191
1263
1192
// pt = Any* occurs when compiling test/files/pos/annotDepMethType.scala with -Xexperimental
1264
- val combined = combineCases(selector, selectorSym, nonSyntheticCases map translateCase(selectorSym, pt), pt, matchOwner , defaultOverride)
1193
+ val combined = combineCases(selector, selectorSym, nonSyntheticCases map translateCase(selectorSym, pt), pt, ctx.owner , defaultOverride)
1265
1194
1266
1195
// if (Statistics.canEnable) Statistics.stopTimer(patmatNanos, start)
1267
1196
Block (List (ValDef (selectorSym,selector)), combined)
@@ -1606,7 +1535,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
1606
1535
// can't simplify this when subPatBinders.isEmpty, since UnitTpe is definitely
1607
1536
// wrong when isSeq, and resultInMonad should always be correct since it comes
1608
1537
// directly from the extractor's result type
1609
- val binder = freshSym(pos, pureType( resultInMonad) )
1538
+ val binder = freshSym(pos, resultInMonad)
1610
1539
val spb = subPatBinders
1611
1540
ExtractorTreeMaker (extractorApply, lengthGuard(binder), binder)(
1612
1541
spb,
0 commit comments