Skip to content

Commit fa7698e

Browse files
committed
Drop MutableSymbolMap as a separate class
It's now an alias of util.IdentityHashMap[Symbol, _]
1 parent c75a555 commit fa7698e

21 files changed

+116
-129
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Symbols._
2222
import Phases._
2323

2424
import dotty.tools.dotc.util
25-
import dotty.tools.dotc.util.Spans
25+
import dotty.tools.dotc.util.{Spans, ReadOnlyMap}
2626
import dotty.tools.dotc.report
2727

2828
import Decorators._
@@ -36,7 +36,7 @@ import Names.TermName
3636
import Annotations.Annotation
3737
import Names.Name
3838

39-
class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap: Map[Symbol, Set[ClassSymbol]])(using val ctx: Context) {
39+
class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap: ReadOnlyMap[Symbol, Set[ClassSymbol]])(using val ctx: Context) {
4040

4141
private val desugared = new java.util.IdentityHashMap[Type, tpd.Select]
4242

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import dotty.tools.io._
3636
class GenBCode extends Phase {
3737
def phaseName: String = GenBCode.name
3838

39-
private val superCallsMap = newMutableSymbolMap[Set[ClassSymbol]]
39+
private val superCallsMap = new MutableSymbolMap[Set[ClassSymbol]]
4040
def registerSuperCall(sym: Symbol, calls: ClassSymbol): Unit = {
4141
val old = superCallsMap.getOrElse(sym, Set.empty)
4242
superCallsMap.update(sym, old + calls)
@@ -51,10 +51,8 @@ class GenBCode extends Phase {
5151
}
5252

5353
def run(using Context): Unit =
54-
new GenBCodePipeline(
55-
new DottyBackendInterface(
56-
outputDir, superCallsMap.toMap
57-
)
54+
GenBCodePipeline(
55+
DottyBackendInterface(outputDir, superCallsMap)
5856
).run(ctx.compilationUnit.tpdTree)
5957

6058

compiler/src/dotty/tools/backend/jvm/scalaPrimitives.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Names.TermName, StdNames._
99
import Types.{JavaArrayType, UnspecifiedErrorType, Type}
1010
import Symbols.{Symbol, NoSymbol}
1111
import dotc.report
12+
import dotc.util.ReadOnlyMap
1213

1314
import scala.annotation.threadUnsafe
1415
import scala.collection.immutable
@@ -34,7 +35,7 @@ import scala.collection.immutable
3435
class DottyPrimitives(ictx: Context) {
3536
import dotty.tools.backend.ScalaPrimitivesOps._
3637

37-
@threadUnsafe private lazy val primitives: immutable.Map[Symbol, Int] = init
38+
@threadUnsafe private lazy val primitives: ReadOnlyMap[Symbol, Int] = init
3839

3940
/** Return the code for the given symbol. */
4041
def getPrimitive(sym: Symbol): Int = {
@@ -118,12 +119,12 @@ class DottyPrimitives(ictx: Context) {
118119
}
119120

120121
/** Initialize the primitive map */
121-
private def init: immutable.Map[Symbol, Int] = {
122+
private def init: ReadOnlyMap[Symbol, Int] = {
122123

123124
given Context = ictx
124125

125126
import Symbols.defn
126-
val primitives = Symbols.newMutableSymbolMap[Int]
127+
val primitives = Symbols.MutableSymbolMap[Int](512)
127128

128129
/** Add a primitive operation to the map */
129130
def addPrimitive(s: Symbol, code: Int): Unit = {
@@ -394,7 +395,7 @@ class DottyPrimitives(ictx: Context) {
394395
addPrimitives(DoubleClass, nme.UNARY_-, NEG)
395396

396397

397-
primitives.toMap
398+
primitives
398399
}
399400

400401
def isPrimitive(sym: Symbol): Boolean =

compiler/src/dotty/tools/backend/sjs/JSPrimitives.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Symbols._
1010
import dotty.tools.dotc.ast.tpd._
1111
import dotty.tools.backend.jvm.DottyPrimitives
1212
import dotty.tools.dotc.report
13+
import dotty.tools.dotc.util.ReadOnlyMap
1314

1415
import scala.collection.mutable
1516

@@ -55,7 +56,7 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
5556
import JSPrimitives._
5657
import dotty.tools.backend.ScalaPrimitivesOps._
5758

58-
private lazy val jsPrimitives: Map[Symbol, Int] = initJSPrimitives(using ictx)
59+
private lazy val jsPrimitives: ReadOnlyMap[Symbol, Int] = initJSPrimitives(using ictx)
5960

6061
override def getPrimitive(sym: Symbol): Int =
6162
jsPrimitives.getOrElse(sym, super.getPrimitive(sym))
@@ -70,9 +71,9 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
7071
jsPrimitives.contains(fun.symbol(using ictx)) || super.isPrimitive(fun)
7172

7273
/** Initialize the primitive map */
73-
private def initJSPrimitives(using Context): Map[Symbol, Int] = {
74+
private def initJSPrimitives(using Context): ReadOnlyMap[Symbol, Int] = {
7475

75-
val primitives = newMutableSymbolMap[Int]
76+
val primitives = MutableSymbolMap[Int]()
7677

7778
// !!! Code duplicate with DottyPrimitives
7879
/** Add a primitive operation to the map */
@@ -120,7 +121,7 @@ class JSPrimitives(ictx: Context) extends DottyPrimitives(ictx) {
120121
addPrimitive(jsdefn.ReflectSelectable_selectDynamic, REFLECT_SELECTABLE_SELECTDYN)
121122
addPrimitive(jsdefn.ReflectSelectable_applyDynamic, REFLECT_SELECTABLE_APPLYDYN)
122123

123-
primitives.toMap
124+
primitives
124125
}
125126

126127
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package core
44

55
import ast.{ untpd, tpd }
66
import Decorators._, Symbols._, Contexts._
7-
import util.SourceFile
7+
import util.{SourceFile, ReadOnlyMap}
88
import util.Spans._
99
import util.CommentParsing._
1010
import util.Property.Key
@@ -23,11 +23,11 @@ object Comments {
2323
*/
2424
class ContextDocstrings {
2525

26-
private val _docstrings: MutableSymbolMap[Comment] = newMutableSymbolMap
26+
private val _docstrings: MutableSymbolMap[Comment] = MutableSymbolMap[Comment](512) // FIXME: 2nd [Comment] needed or "not a class type"
2727

2828
val templateExpander: CommentExpander = new CommentExpander
2929

30-
def docstrings: Map[Symbol, Comment] = _docstrings.toMap
30+
def docstrings: ReadOnlyMap[Symbol, Comment] = _docstrings
3131

3232
def docstring(sym: Symbol): Option[Comment] = _docstrings.get(sym)
3333

@@ -180,7 +180,7 @@ object Comments {
180180
protected def superComment(sym: Symbol)(using Context): Option[String] =
181181
allInheritedOverriddenSymbols(sym).iterator map (x => cookedDocComment(x)) find (_ != "")
182182

183-
private val cookedDocComments = newMutableSymbolMap[String]
183+
private val cookedDocComments = MutableSymbolMap[String]()
184184

185185
/** The raw doc comment of symbol `sym`, minus usecase and define sections, augmented by
186186
* missing sections of an inherited doc comment.

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

+2-56
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import reporting.Message
3030
import collection.mutable
3131
import io.AbstractFile
3232
import language.implicitConversions
33-
import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos}
33+
import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos, IdentityHashMap}
3434
import scala.collection.JavaConverters._
3535
import scala.annotation.internal.sharable
3636
import config.Printers.typr
@@ -495,61 +495,7 @@ object Symbols {
495495
/** The current class */
496496
def currentClass(using Context): ClassSymbol = ctx.owner.enclosingClass.asClass
497497

498-
/* Mutable map from symbols any T */
499-
class MutableSymbolMap[T](private[Symbols] val value: java.util.IdentityHashMap[Symbol, T]) extends AnyVal {
500-
501-
def apply(sym: Symbol): T = value.get(sym)
502-
503-
def get(sym: Symbol): Option[T] = Option(value.get(sym))
504-
505-
def getOrElse[U >: T](sym: Symbol, default: => U): U = {
506-
val v = value.get(sym)
507-
if (v != null) v else default
508-
}
509-
510-
def getOrElseUpdate(sym: Symbol, op: => T): T = {
511-
val v = value.get(sym)
512-
if (v != null) v
513-
else {
514-
val v = op
515-
assert(v != null)
516-
value.put(sym, v)
517-
v
518-
}
519-
}
520-
521-
def update(sym: Symbol, x: T): Unit = {
522-
assert(x != null)
523-
value.put(sym, x)
524-
}
525-
def put(sym: Symbol, x: T): T = {
526-
assert(x != null)
527-
value.put(sym, x)
528-
}
529-
530-
def -=(sym: Symbol): Unit = value.remove(sym)
531-
def remove(sym: Symbol): Option[T] = Option(value.remove(sym))
532-
533-
def contains(sym: Symbol): Boolean = value.containsKey(sym)
534-
535-
def isEmpty: Boolean = value.isEmpty
536-
537-
def clear(): Unit = value.clear()
538-
539-
def filter(p: ((Symbol, T)) => Boolean): Map[Symbol, T] =
540-
value.asScala.toMap.filter(p)
541-
542-
def iterator: Iterator[(Symbol, T)] = value.asScala.iterator
543-
544-
def keysIterator: Iterator[Symbol] = value.keySet().asScala.iterator
545-
546-
def toMap: Map[Symbol, T] = value.asScala.toMap
547-
548-
override def toString: String = value.asScala.toString()
549-
}
550-
551-
inline def newMutableSymbolMap[T]: MutableSymbolMap[T] =
552-
new MutableSymbolMap(new java.util.IdentityHashMap[Symbol, T]())
498+
type MutableSymbolMap[T] = IdentityHashMap[Symbol, T]
553499

554500
// ---- Factory methods for symbol creation ----------------------
555501
//

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class TreePickler(pickler: TastyPickler) {
4242
import pickler.nameBuffer.nameIndex
4343
import tpd._
4444

45-
private val symRefs = Symbols.newMutableSymbolMap[Addr]
46-
private val forwardSymRefs = Symbols.newMutableSymbolMap[List[Addr]]
45+
private val symRefs = Symbols.MutableSymbolMap[Addr](256)
46+
private val forwardSymRefs = Symbols.MutableSymbolMap[List[Addr]]()
4747
private val pickledTypes = util.IdentityHashMap[Type, Addr]()
4848

4949
/** A list of annotation trees for every member definition, so that later

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import core._
@@ -23,7 +24,7 @@ abstract class AccessProxies {
2324
import AccessProxies._
2425

2526
/** accessor -> accessed */
26-
private val accessedBy = newMutableSymbolMap[Symbol]
27+
private val accessedBy = MutableSymbolMap[Symbol]()
2728

2829
/** Given the name of an accessor, is the receiver of the call to accessed obtained
2930
* as a parameterer?
@@ -35,7 +36,7 @@ abstract class AccessProxies {
3536
* So a second call of the same method will yield the empty list.
3637
*/
3738
private def accessorDefs(cls: Symbol)(using Context): Iterator[DefDef] =
38-
for (accessor <- cls.info.decls.iterator; accessed <- accessedBy.remove(accessor)) yield
39+
for (accessor <- cls.info.decls.iterator; accessed <- accessedBy.remove(accessor).toOption) yield
3940
polyDefDef(accessor.asTerm, tps => argss => {
4041
def numTypeParams = accessed.info match {
4142
case info: PolyType => info.paramNames.length

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Bridges(root: ClassSymbol, thisPhase: DenotTransformer)(using Context) {
3636
private var toBeRemoved = immutable.Set[Symbol]()
3737
private val bridges = mutable.ListBuffer[Tree]()
3838
private val bridgesScope = newScope
39-
private val bridgeTarget = newMutableSymbolMap[Symbol]
39+
private val bridgeTarget = MutableSymbolMap[Symbol]()
4040

4141
def bridgePosFor(member: Symbol): SrcPos =
4242
(if (member.owner == root && member.span.exists) member else root).srcPos

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import core._
@@ -115,13 +116,13 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
115116
&& (((cls.owner.name eq nme.DOLLAR_NEW) && cls.owner.isAllOf(Private|Synthetic)) || cls.owner.isAllOf(EnumCase))
116117
&& cls.owner.owner.linkedClass.derivesFromJavaEnum
117118

118-
private val enumCaseOrdinals: MutableSymbolMap[Int] = newMutableSymbolMap
119+
private val enumCaseOrdinals = MutableSymbolMap[Int]()
119120

120121
private def registerEnumClass(cls: Symbol)(using Context): Unit =
121-
cls.children.zipWithIndex.foreach(enumCaseOrdinals.put)
122+
cls.children.zipWithIndex.foreach(enumCaseOrdinals.update)
122123

123124
private def ordinalFor(enumCase: Symbol): Int =
124-
enumCaseOrdinals.remove(enumCase).get
125+
enumCaseOrdinals.remove(enumCase).nn
125126

126127
/** 1. If this is an enum class, add $name and $ordinal parameters to its
127128
* parameter accessors and pass them on to the java.lang.Enum constructor.

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,21 @@ class ExtensionMethods extends MiniPhase with DenotTransformer with FullParamete
140140
extensionMeth
141141
}
142142

143-
private val extensionDefs = newMutableSymbolMap[mutable.ListBuffer[Tree]]
144-
// TODO: this is state and should be per-run
143+
private val extensionDefs = MutableSymbolMap[mutable.ListBuffer[Tree]]()
145144
// todo: check that when transformation finished map is empty
146145

147146
override def transformTemplate(tree: tpd.Template)(using Context): tpd.Tree =
148-
if (isDerivedValueClass(ctx.owner))
147+
if isDerivedValueClass(ctx.owner) then
149148
/* This is currently redundant since value classes may not
150149
wrap over other value classes anyway.
151150
checkNonCyclic(ctx.owner.pos, Set(), ctx.owner) */
152151
tree
153-
else if (ctx.owner.isStaticOwner)
154-
extensionDefs remove tree.symbol.owner match {
155-
case Some(defns) if defns.nonEmpty =>
156-
cpy.Template(tree)(body = tree.body ++
157-
defns.map(transformFollowing(_)))
152+
else if ctx.owner.isStaticOwner then
153+
extensionDefs.remove(tree.symbol.owner) match
154+
case defns: mutable.ListBuffer[Tree] if defns.nonEmpty =>
155+
cpy.Template(tree)(body = tree.body ++ defns.map(transformFollowing(_)))
158156
case _ =>
159157
tree
160-
}
161158
else tree
162159

163160
override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree =

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class NonLocalReturns extends MiniPhase {
3535
nonLocalReturnControl.appliedTo(argtype)
3636

3737
/** A hashmap from method symbols to non-local return keys */
38-
private val nonLocalReturnKeys = newMutableSymbolMap[TermSymbol]
38+
private val nonLocalReturnKeys = MutableSymbolMap[TermSymbol]()
3939

4040
/** Return non-local return key for given method */
4141
private def nonLocalReturnKey(meth: Symbol)(using Context) =
@@ -83,10 +83,9 @@ class NonLocalReturns extends MiniPhase {
8383
}
8484

8585
override def transformDefDef(tree: DefDef)(using Context): Tree =
86-
nonLocalReturnKeys.remove(tree.symbol) match {
87-
case Some(key) => cpy.DefDef(tree)(rhs = nonLocalReturnTry(tree.rhs, key, tree.symbol))
88-
case _ => tree
89-
}
86+
nonLocalReturnKeys.remove(tree.symbol) match
87+
case key: TermSymbol => cpy.DefDef(tree)(rhs = nonLocalReturnTry(tree.rhs, key, tree.symbol))
88+
case null => tree
9089

9190
override def transformReturn(tree: Return)(using Context): Tree =
9291
if isNonLocalReturn(tree) then

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ object OverridingPairs {
6464
decls
6565
}
6666

67-
private val subParents = {
68-
val subParents = newMutableSymbolMap[BitSet]
67+
private val subParents =
68+
val subParents = MutableSymbolMap[BitSet]()
6969
for (bc <- base.info.baseClasses)
7070
subParents(bc) = BitSet(parents.indices.filter(parents(_).derivesFrom(bc)): _*)
7171
subParents
72-
}
7372

7473
private def hasCommonParentAsSubclass(cls1: Symbol, cls2: Symbol): Boolean =
7574
(subParents(cls1) intersect subParents(cls2)).nonEmpty

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ object PatternMatcher {
9292
/** A map from variable symbols to their defining trees
9393
* and from labels to their defining plans
9494
*/
95-
private val initializer = newMutableSymbolMap[Tree]
95+
private val initializer = MutableSymbolMap[Tree]()
9696

9797
private def newVar(rhs: Tree, flags: FlagSet): TermSymbol =
9898
newSymbol(ctx.owner, PatMatStdBinderName.fresh(), Synthetic | Case | flags,

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import dotty.tools.dotc.ast.{Trees, tpd}
@@ -59,7 +60,7 @@ class SuperAccessors(thisPhase: DenotTransformer) {
5960
ctx.owner.enclosingClass != invalidEnclClass
6061

6162
/** List buffers for new accessor definitions, indexed by class */
62-
private val accDefs = newMutableSymbolMap[mutable.ListBuffer[Tree]]
63+
private val accDefs = MutableSymbolMap[mutable.ListBuffer[Tree]]()
6364

6465
/** A super accessor call corresponding to `sel` */
6566
private def superAccessorCall(sel: Select, mixName: Name = nme.EMPTY)(using Context) = {
@@ -205,7 +206,7 @@ class SuperAccessors(thisPhase: DenotTransformer) {
205206
def wrapTemplate(tree: Template)(op: Template => Template)(using Context): Template = {
206207
accDefs(currentClass) = new mutable.ListBuffer[Tree]
207208
val impl = op(tree)
208-
val accessors = accDefs.remove(currentClass).get
209+
val accessors = accDefs.remove(currentClass).nn
209210
if (accessors.isEmpty) impl
210211
else {
211212
val (params, rest) = impl.body span {

0 commit comments

Comments
 (0)