Skip to content

Commit 93e1b6d

Browse files
DarkDimiusOlivierBlanvillain
authored andcommitted
Collapse ctx and localCtx together
1 parent ba0caa8 commit 93e1b6d

15 files changed

+90
-69
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/BubbleUpNothing.scala

+11-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package transform.localopt
44
import core.Contexts.Context
55
import core.Symbols._
66
import ast.Trees._
7+
import dotty.tools.dotc.ast.tpd
78

89
/**
910
* If a block has a statement that evaluates to Nothing:
@@ -19,12 +20,16 @@ import ast.Trees._
1920
*
2021
* @author DarkDimius, OlivierBlanvillain
2122
*/
22-
class BubbleUpNothing(implicit val ctx: Context) extends Optimisation {
23+
class BubbleUpNothing extends Optimisation {
2324
import ast.tpd._
2425

25-
val visitor = NoVisitor
26+
def visitor(implicit ctx: Context) = NoVisitor
2627

27-
def transformer(localCtx: Context): Tree => Tree = {
28+
29+
/** Does the actual Tree => Tree transformation, possibly using a different
30+
* context from the one used in Optimisation.
31+
*/
32+
def transformer(implicit ctx: Context): (tpd.Tree) => tpd.Tree = {
2833
case t @ Apply(Select(Notathing(qual), _), args) =>
2934
Typed(qual, TypeTree(t.tpe))
3035
// This case leads to complications with multiple argument lists,
@@ -50,16 +55,16 @@ class BubbleUpNothing(implicit val ctx: Context) extends Optimisation {
5055
}
5156

5257
object Notathing {
53-
def unapply(t: Tree): Option[Tree] = Option(lookup(t))
54-
def lookup(t: Tree): Tree = t match {
58+
def unapply(t: Tree)(implicit ctx: Context): Option[Tree] = Option(lookup(t))
59+
def lookup(t: Tree)(implicit ctx: Context): Tree = t match {
5560
case x if x.tpe.derivesFrom(defn.NothingClass) => t
5661
case Typed(x, _) => lookup(x)
5762
case Block(_, x) => lookup(x)
5863
case _ => null
5964
}
6065
}
6166

62-
def notathing(t: Tree): Boolean = t match {
67+
def notathing(t: Tree)(implicit ctx: Context): Boolean = t match {
6368
case Notathing(_) => true
6469
case _ => false
6570
}

compiler/src/dotty/tools/dotc/transform/localopt/ConstantFold.scala

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import Simplify.desugarIdent
2020
*
2121
* @author DarkDimius, OlivierBlanvillain
2222
*/
23-
class ConstantFold(implicit val ctx: Context) extends Optimisation {
23+
class ConstantFold extends Optimisation {
2424
import ast.tpd._
2525

26-
val visitor = NoVisitor
26+
def visitor(implicit ctx: Context) = NoVisitor
2727

28-
def transformer(localCtx: Context): Tree => Tree = { x => preEval(x) match {
28+
def transformer(implicit ctx: Context): Tree => Tree = { x => preEval(x) match {
2929
// TODO: include handling of isInstanceOf similar to one in IsInstanceOfEvaluator
3030
// TODO: include methods such as Int.int2double(see ./tests/pos/harmonize.scala)
3131
case If(cond1, thenp, elsep) if isSimilar(thenp, elsep) =>
@@ -160,7 +160,7 @@ import Simplify.desugarIdent
160160
}
161161
}
162162

163-
def preEval(t: Tree) = {
163+
def preEval(t: Tree)(implicit ctx: Context) = {
164164
if (t.isInstanceOf[Literal] || t.isInstanceOf[CaseDef] || !isPureExpr(t)) t
165165
else {
166166
val s = ConstFold.apply(t)
@@ -171,7 +171,7 @@ import Simplify.desugarIdent
171171
}
172172
}
173173

174-
def isSimilar(t1: Tree, t2: Tree): Boolean = t1 match {
174+
def isSimilar(t1: Tree, t2: Tree)(implicit ctx: Context): Boolean = t1 match {
175175
case t1: Apply =>
176176
t2 match {
177177
case t2: Apply =>
@@ -209,7 +209,7 @@ import Simplify.desugarIdent
209209
case _ => false
210210
}
211211

212-
def isBool(tpe: Type): Boolean = tpe.derivesFrom(defn.BooleanClass)
213-
def isConst(tpe: Type): Boolean = tpe.isInstanceOf[ConstantType]
214-
def asConst(tpe: Type): ConstantType = tpe.asInstanceOf[ConstantType]
212+
def isBool(tpe: Type)(implicit ctx: Context): Boolean = tpe.derivesFrom(defn.BooleanClass)
213+
def isConst(tpe: Type)(implicit ctx: Context): Boolean = tpe.isInstanceOf[ConstantType]
214+
def asConst(tpe: Type)(implicit ctx: Context): ConstantType = tpe.asInstanceOf[ConstantType]
215215
}

compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import transform.SymUtils._
2020
* This phase has to be careful not to eliminate vals that are parts of other types
2121
* @author DarkDimius, OlivierBlanvillain
2222
* */
23-
class Devalify(implicit val ctx: Context) extends Optimisation {
23+
class Devalify extends Optimisation {
2424
import ast.tpd._
2525

2626
val timesUsed = mutable.HashMap[Symbol, Int]()
@@ -31,7 +31,7 @@ class Devalify(implicit val ctx: Context) extends Optimisation {
3131
// Either a duplicate or a read through series of immutable fields
3232
val copies = mutable.HashMap[Symbol, Tree]()
3333

34-
def visitType(tp: Type): Unit = {
34+
def visitType(tp: Type)(implicit ctx: Context): Unit = {
3535
tp.foreachPart(x => x match {
3636
case TermRef(NoPrefix, _) =>
3737
val b4 = timesUsedAsType.getOrElseUpdate(x.termSymbol, 0)
@@ -40,7 +40,7 @@ class Devalify(implicit val ctx: Context) extends Optimisation {
4040
})
4141
}
4242

43-
def doVisit(tree: Tree, used: mutable.HashMap[Symbol, Int]): Unit = tree match {
43+
def doVisit(tree: Tree, used: mutable.HashMap[Symbol, Int])(implicit ctx: Context): Unit = tree match {
4444
case valdef: ValDef if !valdef.symbol.is(Param | Mutable | Module | Lazy) &&
4545
valdef.symbol.exists && !valdef.symbol.owner.isClass =>
4646
defined += valdef.symbol
@@ -73,7 +73,7 @@ class Devalify(implicit val ctx: Context) extends Optimisation {
7373
case _ =>
7474
}
7575

76-
def visitor: Tree => Unit = { tree =>
76+
def visitor(implicit ctx: Context): Tree => Unit = { tree =>
7777
def crossingClassBoundaries(t: Tree): Boolean = t match {
7878
case _: New => true
7979
case _: Template => true
@@ -98,7 +98,7 @@ class Devalify(implicit val ctx: Context) extends Optimisation {
9898
doVisit(tree, timesUsed)
9999
}
100100

101-
def transformer(localCtx: Context): Tree => Tree = {
101+
override def transformer(implicit ctx: Context): Tree => Tree = {
102102
val valsToDrop = defined -- timesUsed.keySet -- timesUsedAsType.keySet
103103
val copiesToReplaceAsDuplicates = copies.filter { x =>
104104
val rhs = dropCasts(x._2)

compiler/src/dotty/tools/dotc/transform/localopt/DropGoodCasts.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import transform.SymUtils._
1919
*
2020
* @author DarkDimius, OlivierBlanvillain
2121
*/
22-
class DropGoodCasts(implicit val ctx: Context) extends Optimisation {
22+
class DropGoodCasts extends Optimisation {
2323
import ast.tpd._
2424

25-
val visitor = NoVisitor
25+
def visitor(implicit ctx: Context) = NoVisitor
2626

27-
def transformer(localCtx: Context): Tree => Tree = {
27+
def transformer(implicit ctx: Context): Tree => Tree = {
2828
case t @ If(cond, thenp, elsep) =>
2929
val newTypeTested = collectTypeTests(cond)
3030
val nullTested = collectNullTests(cond).toSet

compiler/src/dotty/tools/dotc/transform/localopt/DropNoEffects.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import Simplify.desugarIdent
1616
*
1717
* @author DarkDimius, OlivierBlanvillain
1818
*/
19-
class DropNoEffects(val simplifyPhase: Simplify)(implicit val ctx: Context) extends Optimisation {
19+
class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
2020
import ast.tpd._
2121

22-
val visitor = NoVisitor
22+
def visitor(implicit ctx: Context) = NoVisitor
2323

24-
def transformer(localCtx: Context): Tree => Tree = {
24+
def transformer(implicit ctx: Context): Tree => Tree = {
2525
// Remove empty blocks
2626
case Block(Nil, expr) => expr
2727

@@ -59,7 +59,7 @@ class DropNoEffects(val simplifyPhase: Simplify)(implicit val ctx: Context) exte
5959
case t => t
6060
}
6161

62-
val keepOnlySideEffects: Tree => Tree = {
62+
def keepOnlySideEffects(t: Tree)(implicit ctx: Context): Tree = t match {
6363
case l: Literal =>
6464
EmptyTree
6565

@@ -194,7 +194,7 @@ class DropNoEffects(val simplifyPhase: Simplify)(implicit val ctx: Context) exte
194194
)
195195

196196
/** Does this tree has side effects? This is an approximation awaiting real purity analysis... */
197-
def effectsDontEscape(t: Tree): Boolean = t match {
197+
def effectsDontEscape(t: Tree)(implicit ctx: Context): Boolean = t match {
198198
case Apply(fun, _) if fun.symbol.isConstructor && constructorWhiteList.contains(fun.symbol.owner.fullName.toString) =>
199199
true
200200
case Apply(fun, _) if methodsWhiteList.contains(fun.symbol.fullName.toString) =>

compiler/src/dotty/tools/dotc/transform/localopt/InlineCaseIntrinsics.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import dotty.tools.dotc.ast.tpd
2020
*
2121
* @author DarkDimius, OlivierBlanvillain
2222
*/
23-
class InlineCaseIntrinsics(implicit val ctx: Context) extends Optimisation {
23+
class InlineCaseIntrinsics extends Optimisation {
2424
import ast.tpd._
2525

26-
val visitor = NoVisitor
26+
def visitor(implicit ctx: Context): (tpd.Tree) => Unit = NoVisitor
2727

28-
def transformer(localCtx: Context): Tree => Tree = {
28+
def transformer(implicit localCtx: Context): Tree => Tree = {
2929
// For synthetic applies on case classes (both dotty/scalac)
3030
// - CC.apply(args) → new CC(args)
3131
case a: Apply
@@ -125,7 +125,7 @@ class InlineCaseIntrinsics(implicit val ctx: Context) extends Optimisation {
125125

126126
// Apply fun may be a side-effectful function. E.g. a block, see tests/run/t4859.scala
127127
// we need to maintain expressions that were in this block
128-
def evalreceiver(a: Apply, res: Tree) = {
128+
def evalreceiver(a: Apply, res: Tree)(implicit ctx: Context) = {
129129
def receiver(t: Tree): Tree = t match {
130130
case TypeApply(fun, targs) if fun.symbol eq t.symbol => receiver(fun)
131131
case Apply(fn, args) if fn.symbol == t.symbol => receiver(fn)

compiler/src/dotty/tools/dotc/transform/localopt/InlineLabelsCalledOnce.scala

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ import transform.SymUtils._
88
import scala.collection.mutable
99
import config.Printers.simplify
1010

11-
/** Inlines LabelDef which are used exactly once. */
12-
class InlineLabelsCalledOnce(implicit val ctx: Context) extends Optimisation {
11+
/** Inlines LabelDef which are used exactly once.
12+
*
13+
* @author DarkDimius, OlivierBlanvillain
14+
*
15+
* */
16+
class InlineLabelsCalledOnce extends Optimisation {
1317
import ast.tpd._
1418

1519
val timesUsed = mutable.HashMap[Symbol, Int]()
1620
val defined = mutable.HashMap[Symbol, DefDef]()
1721

18-
val visitor: Tree => Unit = {
22+
def visitor(implicit ctx: Context): Tree => Unit = {
1923
case d: DefDef if d.symbol.is(Label) =>
2024
var isRecursive = false
2125
d.rhs.foreachSubTree { x =>
@@ -32,12 +36,12 @@ class InlineLabelsCalledOnce(implicit val ctx: Context) extends Optimisation {
3236
case _ =>
3337
}
3438

35-
def transformer(localCtx: Context): Tree => Tree = {
39+
def transformer(implicit ctx: Context): Tree => Tree = {
3640
case a: Apply =>
3741
defined.get(a.symbol) match {
3842
case Some(defDef) if usedOnce(a) && a.symbol.info.paramInfoss == List(Nil) =>
3943
simplify.println(s"Inlining labeldef ${defDef.name}")
40-
defDef.rhs.changeOwner(defDef.symbol, localCtx.owner)
44+
defDef.rhs.changeOwner(defDef.symbol, ctx.owner)
4145

4246
case Some(defDef) if defDef.rhs.isInstanceOf[Literal] =>
4347
defDef.rhs
@@ -57,11 +61,11 @@ class InlineLabelsCalledOnce(implicit val ctx: Context) extends Optimisation {
5761
case t => t
5862
}
5963

60-
def usedN(t: Tree, n: Int): Boolean =
64+
def usedN(t: Tree, n: Int)(implicit ctx: Context): Boolean =
6165
t.symbol.is(Label) &&
6266
timesUsed.getOrElse(t.symbol, 0) == n &&
6367
defined.contains(t.symbol)
6468

65-
def usedOnce(t: Tree): Boolean = usedN(t, 1)
66-
def neverUsed(t: Tree): Boolean = usedN(t, 0)
69+
def usedOnce(t: Tree)(implicit ctx: Context): Boolean = usedN(t, 1)
70+
def neverUsed(t: Tree)(implicit ctx: Context): Boolean = usedN(t, 0)
6771
}

compiler/src/dotty/tools/dotc/transform/localopt/InlineLocalObjects.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import config.Printers.simplify
1717
* parameter value classes. The main motivation is to get ride of all the
1818
* intermediate tuples coming from pattern matching expressions.
1919
*/
20-
class InlineLocalObjects(implicit val ctx: Context) extends Optimisation {
20+
class InlineLocalObjects extends Optimisation {
2121
import ast.tpd._
2222

2323
// In the end only calls constructor. Reason for unconditional inlining
@@ -27,7 +27,7 @@ class InlineLocalObjects(implicit val ctx: Context) extends Optimisation {
2727
val forwarderWritesTo = mutable.HashMap[Symbol, Symbol]()
2828
val gettersCalled = mutable.HashSet[Symbol]()
2929

30-
def followTailPerfect(t: Tree, symbol: Symbol): Unit = {
30+
def followTailPerfect(t: Tree, symbol: Symbol)(implicit ctx: Context): Unit = {
3131
t match {
3232
case Block(_, expr) => followTailPerfect(expr, symbol)
3333
case If(_, thenp, elsep) => followTailPerfect(thenp, symbol); followTailPerfect(elsep, symbol);
@@ -43,7 +43,7 @@ class InlineLocalObjects(implicit val ctx: Context) extends Optimisation {
4343
}
4444
}
4545

46-
val visitor: Tree => Unit = {
46+
def visitor(implicit ctx: Context): Tree => Unit = {
4747
case vdef: ValDef if (vdef.symbol.info.classSymbol is CaseClass) &&
4848
!vdef.symbol.is(Lazy) &&
4949
!vdef.symbol.info.classSymbol.caseAccessors.exists(x => x.is(Mutable)) =>
@@ -63,7 +63,7 @@ class InlineLocalObjects(implicit val ctx: Context) extends Optimisation {
6363
case _ =>
6464
}
6565

66-
def transformer(localCtx: Context): Tree => Tree = {
66+
def transformer(implicit ctx: Context): Tree => Tree = {
6767
var hasChanged = true
6868
while(hasChanged) {
6969
hasChanged = false

compiler/src/dotty/tools/dotc/transform/localopt/InlineOptions.scala

+10-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import core.Flags._
99
import ast.Trees._
1010
import scala.collection.mutable
1111

12-
/** Inlines Option methods whose result is known statically. */
13-
class InlineOptions(implicit val ctx: Context) extends Optimisation {
12+
/** Inlines Option methods whose result is known statically.
13+
*
14+
*
15+
* @author DarkDimius, OlivierBlanvillain
16+
* */
17+
class InlineOptions extends Optimisation {
1418
import ast.tpd._
1519

16-
val somes = mutable.HashMap[Symbol, Tree]()
17-
val nones = mutable.HashSet[Symbol]()
20+
private val somes = mutable.HashMap[Symbol, Tree]()
21+
private val nones = mutable.HashSet[Symbol]()
1822

19-
val visitor: Tree => Unit = {
23+
def visitor(implicit ctx: Context): Tree => Unit = {
2024
case valdef: ValDef if !valdef.symbol.is(Mutable) &&
2125
valdef.rhs.isInstanceOf[Apply] && valdef.rhs.tpe.derivesFrom(defn.SomeClass) &&
2226
valdef.rhs.symbol.isPrimaryConstructor =>
@@ -29,7 +33,7 @@ class InlineOptions(implicit val ctx: Context) extends Optimisation {
2933
case _ =>
3034
}
3135

32-
def transformer(localCtx: Context): Tree => Tree = { tree =>
36+
def transformer(implicit ctx: Context): Tree => Tree = { tree =>
3337
def rewriteSelect(x: Tree) = x match {
3438
case Select(rec, nm) if nm == nme.get && somes.contains(rec.symbol) => somes(rec.symbol)
3539
case Select(rec, nm) if nm == nme.isDefined && somes.contains(rec.symbol) => Literal(Constant(true))

compiler/src/dotty/tools/dotc/transform/localopt/Jumpjump.scala

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import scala.collection.mutable
1111
import config.Printers.simplify
1212
import core.Flags._
1313

14-
/** Rewrites pairs of consecutive LabelDef jumps by jumping directly to the target. */
15-
class Jumpjump(implicit val ctx: Context) extends Optimisation {
14+
/** Rewrites pairs of consecutive LabelDef jumps by jumping directly to the target.
15+
*
16+
* @author DarkDimius, OlivierBlanvillain
17+
* */
18+
class Jumpjump extends Optimisation {
1619
import ast.tpd._
1720

1821
val defined = mutable.HashMap[Symbol, Symbol]()
1922

20-
val visitor: Tree => Unit = {
23+
def visitor(implicit ctx: Context): Tree => Unit = {
2124
case defdef: DefDef if defdef.symbol.is(Label) =>
2225
defdef.rhs match {
2326
case Apply(t, args)
@@ -34,7 +37,7 @@ class Jumpjump(implicit val ctx: Context) extends Optimisation {
3437
case _ =>
3538
}
3639

37-
def transformer(localCtx: Context): Tree => Tree = {
40+
def transformer(implicit ctx: Context): Tree => Tree = {
3841
case a: Apply if defined.contains(a.fun.symbol) =>
3942
defined.get(a.symbol) match {
4043
case None => a

compiler/src/dotty/tools/dotc/transform/localopt/Optimisation.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import ast.tpd.Tree
77
trait Optimisation {
88

99
/** Run first to gather information on Trees (using mutation) */
10-
def visitor: Tree => Unit
10+
def visitor(implicit ctx: Context): Tree => Unit
1111

1212
/** Does the actual Tree => Tree transformation, possibly using a different
1313
* context from the one used in Optimisation.
1414
*/
15-
def transformer(localCtx: Context): Tree => Tree
15+
def transformer(implicit ctx: Context): Tree => Tree
1616

1717
def name: String = this.getClass.getSimpleName
1818

0 commit comments

Comments
 (0)