Skip to content

Commit 70b46a8

Browse files
committed
Convert more phases to use stores
1 parent a5b2382 commit 70b46a8

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,12 @@ object Contexts {
485485
loc
486486
}
487487

488+
def addLocation[T](): Store.Location[T] = {
489+
val (loc, store1) = store.newLocation[T]()
490+
setStore(store1)
491+
loc
492+
}
493+
488494
def updateStore[T](loc: Store.Location[T], value: T): this.type =
489495
setStore(store.updated(loc, value))
490496

compiler/src/dotty/tools/dotc/transform/LambdaLift.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisPhase =>
501501
private def lifter(implicit ctx: Context) = ctx.store(Lifter)
502502

503503
override def initContext(ctx: FreshContext) =
504-
Lifter = ctx.addLocation[Lifter](null)
504+
Lifter = ctx.addLocation[Lifter]()
505505

506506
override def prepareForUnit(tree: Tree)(implicit ctx: Context) =
507507
ctx.fresh.updateStore(Lifter, new Lifter(thisPhase))

compiler/src/dotty/tools/dotc/transform/ShortcutImplicits.scala

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,9 @@ import core.NameOps._
1414
import core.NameKinds.DirectMethodName
1515
import ast.Trees._
1616
import ast.tpd
17-
import util.Property
17+
import util.Store
1818
import collection.mutable
1919

20-
object ShortcutImplicits {
21-
private type DirectMeths = mutable.HashMap[Symbol, Symbol]
22-
private val DirectMeth = new Property.Key[DirectMeths]
23-
24-
/** A map to cache mapping local methods to their direct counterparts.
25-
* A fresh map is created for each unit.
26-
*/
27-
private def directMeth(implicit ctx: Context) = ctx.property(DirectMeth).get
28-
}
29-
3020
/** This phase optimizes code using implicit function types, by applying two rewrite rules.
3121
* Let IF be the implicit function type
3222
*
@@ -57,12 +47,20 @@ object ShortcutImplicits {
5747
*/
5848
class ShortcutImplicits extends MiniPhase with IdentityDenotTransformer { thisPhase =>
5949
import tpd._
60-
import ShortcutImplicits._
6150

6251
override def phaseName: String = "shortcutImplicits"
6352

6453
override def changesMembers = true // the phase adds "direct" methods
6554

55+
/** A map to cache mapping local methods to their direct counterparts.
56+
* A fresh map is created for each unit.
57+
*/
58+
private var DirectMeth: Store.Location[mutable.HashMap[Symbol, Symbol]] = _
59+
private def directMeth(implicit ctx: Context) = ctx.store(DirectMeth)
60+
61+
override def initContext(ctx: FreshContext) =
62+
DirectMeth = ctx.addLocation[mutable.HashMap[Symbol, Symbol]]()
63+
6664
/** If this option is true, we don't specialize symbols that are known to be only
6765
* targets of monomorphic calls.
6866
* The reason for this option is that benchmarks show that on the JVM for monomorphic dispatch
@@ -72,7 +70,7 @@ class ShortcutImplicits extends MiniPhase with IdentityDenotTransformer { thisPh
7270
final val specializeMonoTargets = true
7371

7472
override def prepareForUnit(tree: Tree)(implicit ctx: Context) =
75-
ctx.fresh.setProperty(DirectMeth, new DirectMeths)
73+
ctx.fresh.updateStore(DirectMeth, new mutable.HashMap[Symbol, Symbol])
7674

7775
/** Should `sym` get a ..$direct companion?
7876
* This is the case if (1) `sym` is a method with an implicit function type as final result type.

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import StdNames._, Denotations._, Scopes._, Constants.Constant, SymUtils._
99
import NameKinds.DefaultGetterName
1010
import Annotations._
1111
import util.Positions._
12+
import util.Store
1213
import scala.collection.{ mutable, immutable }
1314
import ast._
1415
import Trees._
1516
import SuperPhase._
16-
import util.Property
1717
import config.Printers.{checks, noPrinter}
1818
import util.DotClass
1919
import scala.util.{Try, Success, Failure}
@@ -758,11 +758,6 @@ object RefChecks {
758758
}
759759

760760
val NoLevelInfo = new OptLevelInfo()
761-
762-
val LevelInfo = new Property.Key[OptLevelInfo]
763-
764-
def currentLevel(implicit ctx: Context): OptLevelInfo =
765-
ctx.property(LevelInfo).getOrElse(NoLevelInfo)
766761
}
767762
import RefChecks._
768763

@@ -807,9 +802,15 @@ class RefChecks extends MiniPhase { thisPhase =>
807802
// Needs to run after ElimRepeated for override checks involving varargs methods
808803
override def runsAfter = Set(classOf[ElimRepeated])
809804

805+
private var LevelInfo: Store.Location[OptLevelInfo] = _
806+
private def currentLevel(implicit ctx: Context): OptLevelInfo = ctx.store(LevelInfo)
807+
808+
override def initContext(ctx: FreshContext) =
809+
LevelInfo = ctx.addLocation(NoLevelInfo)
810+
810811
override def prepareForStats(trees: List[Tree])(implicit ctx: Context) =
811812
if (ctx.owner.isTerm)
812-
ctx.fresh.setProperty(LevelInfo, new LevelInfo(currentLevel.levelAndIndex, trees))
813+
ctx.fresh.updateStore(LevelInfo, new LevelInfo(currentLevel.levelAndIndex, trees))
813814
else ctx
814815

815816
override def transformValDef(tree: ValDef)(implicit ctx: Context) = {

compiler/src/dotty/tools/dotc/util/Store.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@ object Store {
1010
class Store(private val elems: Array[AnyRef]) extends AnyVal {
1111
import Store._
1212

13-
def newLocation[T](initial: T): (Location[T], Store) = {
13+
def newLocation[T](): (Location[T], Store) = {
1414
val elems1 = new Array[AnyRef](elems.length + 1)
1515
System.arraycopy(elems, 0, elems1, 0, elems.length)
16-
elems1(elems.length) = initial.asInstanceOf[AnyRef]
1716
(new Location(elems.length), new Store(elems1))
1817
}
1918

19+
def newLocation[T](initial: T): (Location[T], Store) = {
20+
val (loc, store) = newLocation[T]()
21+
store.elems(loc.idx) = initial.asInstanceOf[AnyRef]
22+
(loc, store)
23+
}
24+
2025
def updated[T](loc: Location[T], value: T): Store = {
2126
val elems1 = elems.clone
2227
elems1(loc.idx) = value.asInstanceOf[AnyRef]

0 commit comments

Comments
 (0)