Skip to content

Commit 2216759

Browse files
committed
Cleanup patmat.
1 parent 6b64c9a commit 2216759

File tree

1 file changed

+12
-83
lines changed

1 file changed

+12
-83
lines changed

src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 12 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
4646

4747
override def phaseName = "patternMatcher"
4848

49-
var _id = 0
49+
private var _id = 0 // left for debuging
5050

5151
override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
5252
val translated = new Translator()(ctx).translator.translateMatch(tree)
@@ -61,14 +61,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
6161

6262
class OptimizingMatchTranslator extends MatchOptimizer/*(val typer: analyzer.Typer)*/ with MatchTranslator
6363

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 {
7265
private var ctr = 0 // left for debugging
7366

7467
// assert(owner ne null); assert(owner ne NoSymbol)
@@ -91,7 +84,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
9184
def _isInstanceOf(b: Symbol, tp: Type): Tree
9285
def drop(tgt: Tree)(n: Int): Tree
9386
def index(tgt: Tree)(i: Int): Tree
94-
def mkZero(tp: Type): Tree
9587
def tupleSel(binder: Symbol)(i: Int): Tree
9688
}
9789

@@ -110,9 +102,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
110102
def codegen: AbsCodegen
111103

112104
abstract class CommonCodegen extends AbsCodegen {
113-
def fun(arg: TermSymbol, body: Tree): Tree =
114-
DefDef(arg, body)
115-
116105
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
117106
def index(tgt: Tree)(i: Int): Tree = {
118107
if (i > 0) tgt.select(defn.Seq_apply).appliedTo(Literal(Constant(i)))
@@ -138,12 +127,10 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
138127
// 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)
139128
def _asInstanceOf(b: Symbol, tp: Type): Tree = ref(b).ensureConforms(tp) // andType here breaks t1048
140129
def _isInstanceOf(b: Symbol, tp: Type): Tree = ref(b).select(defn.Any_isInstanceOf).appliedToType(tp)
141-
142-
def mkZero(tp: Type): Tree = initValue(tp)
143130
}
144131
}
145132

146-
trait TypedSubstitution extends MatchMonadInterface {
133+
147134
object Substitution {
148135
def apply(from: Symbol, to: Tree) = new Substitution(List(from), List(to))
149136
// requires sameLength(from, to)
@@ -152,13 +139,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
152139
}
153140

154141
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-
}
162142

163143
// We must explicitly type the trees that we replace inside some other tree, since the latter may already have been typed,
164144
// 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
167147
// since about half of the typedSubst's end up being no-ops, the check below shaves off 5% of the time spent in typedSubst
168148
/*if (!tree.exists { case i@Ident(_) => from contains i.symbol case _ => false}) tree
169149
else*/
170-
if(id == 1169) {
171-
println("here")
172-
}
173150

174151
var replaced = 0
175152
val toAdapted = (from zip to) map {
@@ -203,23 +180,13 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
203180

204181
ctx.debuglog(s"location: ${ctx.owner.showFullName}, size: ${treeSize}")
205182
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)*/
216183
}
217184

218185

219186
// the substitution that chains `other` before `this` substitution
220187
// forall t: Tree. this(other(t)) == (this >> other)(t)
221188
def >>(other: Substitution): Substitution = {
222-
if(other == EmptySubstitution) this
189+
if (other == EmptySubstitution) this
223190
else if (this == EmptySubstitution) other
224191
else {
225192
val (fromFiltered, toFiltered) = (from, to).zipped filter { (f, t) => !other.from.contains(f)}
@@ -233,16 +200,16 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
233200
override def apply(tree: Tree): Tree = tree
234201
override def >>(other: Substitution): Substitution = other
235202
}
236-
}
237203

238-
trait OptimizedCodegen extends CodegenCore with TypedSubstitution with MatchMonadInterface {
204+
205+
trait OptimizedCodegen extends CodegenCore {
239206
override def codegen: AbsCodegen = optimizedCodegen
240207

241208
// when we know we're targetting Option, do some inlining the optimizer won't do
242209
// for example, `o.flatMap(f)` becomes `if(o == None) None else f(o.get)`, similarly for orElse and guard
243210
// this is a special instance of the advanced inlining optimization that takes a method call on
244211
// 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 {
246213

247214
/** Inline runOrElse and get rid of Option allocations
248215
*
@@ -283,10 +250,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
283250
// scrutSym == NoSymbol when generating an alternatives matcher
284251
// val scrutDef = scrutSym.fold(List[Tree]())(ValDef(_, scrut) :: Nil) // for alternatives
285252

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])
290253
caseDefs
291254
}
292255

@@ -359,7 +322,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
359322
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
360323
// the making of the trees
361324
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
362-
trait TreeMakers extends TypedSubstitution with CodegenCore {
325+
trait TreeMakers extends CodegenCore {
363326
def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree])
364327
def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit = {}
365328

@@ -758,7 +721,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
758721
def outerTestNeeded = {
759722
val np = expectedTp.normalizedPrefix
760723
val ts = np.termSymbol
761-
(ts ne NoSymbol) && needsOuterTest(expectedTp, testedBinder.info, matchOwner)
724+
(ts ne NoSymbol) && needsOuterTest(expectedTp, testedBinder.info, ctx.owner)
762725

763726
}
764727

@@ -951,40 +914,6 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
951914
}
952915
}
953916
}
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-
}*/
988917
}
989918

990919
trait MatchOptimizer extends OptimizedCodegen with TreeMakers
@@ -1257,11 +1186,11 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
12571186
val pt = match_.tpe.widen //repeatedToSeq(origPt)
12581187

12591188
// 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")
12611190
selectorSym.setFlag(Flags.SyntheticCase)
12621191

12631192
// 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)
12651194

12661195
// if (Statistics.canEnable) Statistics.stopTimer(patmatNanos, start)
12671196
Block(List(ValDef(selectorSym,selector)), combined)
@@ -1606,7 +1535,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
16061535
// can't simplify this when subPatBinders.isEmpty, since UnitTpe is definitely
16071536
// wrong when isSeq, and resultInMonad should always be correct since it comes
16081537
// directly from the extractor's result type
1609-
val binder = freshSym(pos, pureType(resultInMonad))
1538+
val binder = freshSym(pos, resultInMonad)
16101539
val spb = subPatBinders
16111540
ExtractorTreeMaker(extractorApply, lengthGuard(binder), binder)(
16121541
spb,

0 commit comments

Comments
 (0)