@@ -15,7 +15,7 @@ package tools.nsc
1515package transform
1616
1717import scala .annotation .{nowarn , tailrec }
18- import scala .collection .mutable
18+ import scala .collection .mutable , mutable .{ AnyRefMap , Buffer , ListBuffer }
1919import scala .tools .nsc .symtab .Flags
2020import scala .tools .nsc .Reporting .WarningCategory
2121import scala .util .chaining ._
@@ -75,16 +75,15 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
7575 override def keepsTypeParams = true
7676
7777 type TypeEnv = Map [Symbol , Type ]
78- def emptyEnv : TypeEnv = Map [Symbol , Type ]()
78+ def emptyEnv : TypeEnv = Map .empty [Symbol , Type ]
7979
8080 private implicit val typeOrdering : Ordering [Type ] = Ordering [String ] on (" " + _.typeSymbol.name)
8181
82-
83- /** TODO - this is a lot of maps.
84- */
85-
8682 /** For a given class and concrete type arguments, give its specialized class */
87- val specializedClass = perRunCaches.newAnyRefMap[Symbol , mutable.AnyRefMap [TypeEnv , Symbol ]]()
83+ val specializedClass = perRunCaches.newAnyRefMap[Symbol , AnyRefMap [TypeEnv , Symbol ]]()
84+
85+ // read-only map, where missing value defaults to empty immutable.Map
86+ def specializationOf (sym : Symbol ) = specializedClass.getOrElse(sym, Map .empty[TypeEnv , Symbol ])
8887
8988 /** Map a method symbol to a list of its specialized overloads in the same class. */
9089 private val overloads = perRunCaches.newMap[Symbol , List [Overload ]]() withDefaultValue Nil
@@ -334,7 +333,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
334333 if (isSpecializedAnyRefSubtype(tp, orig)) AnyRefTpe
335334 else tp
336335 )
337- specializedClass.getOrElse (sym, Map .empty[ TypeEnv , Symbol ] ).get(TypeEnv .fromSpecialization(sym, args1)) match {
336+ specializationOf (sym).get(TypeEnv .fromSpecialization(sym, args1)) match {
338337 case Some (sym1) => typeRef(pre1, sym1, survivingArgs(sym, args))
339338 case None => typeRef(pre1, sym, args)
340339 }
@@ -344,12 +343,9 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
344343
345344 def specializedFunctionName (sym : Symbol , args : List [Type ]) = exitingSpecialize {
346345 require(isFunctionSymbol(sym), sym)
347- val env : TypeEnv = TypeEnv .fromSpecialization(sym, args)
348- specializedClass.getOrElse(sym, Map .empty[TypeEnv , Symbol ]).get(env) match {
349- case Some (x) =>
350- x.name
351- case None =>
352- sym.name
346+ specializationOf(sym).get(TypeEnv .fromSpecialization(sym, args)) match {
347+ case Some (x) => x.name
348+ case None => sym.name
353349 }
354350 }
355351
@@ -463,12 +459,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
463459 case _ => false
464460 }
465461 def specializedTypeVars (tpes : List [Type ]): Set [Symbol ] = {
466- val result = mutable. ListBuffer .empty[Symbol ]
462+ val result = ListBuffer .empty[Symbol ]
467463 tpes.foreach(specializedTypeVarsBuffer(_, result))
468464 result.toSet
469465 }
470466 def specializedTypeVars (sym : Symbol ): Set [Symbol ] = {
471- val result = mutable. ListBuffer .empty[Symbol ]
467+ val result = ListBuffer .empty[Symbol ]
472468 specializedTypeVarsBuffer(sym, result)
473469 result.toSet
474470 }
@@ -480,12 +476,12 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
480476 * (arrays are considered as Array[@specialized T])
481477 */
482478 def specializedTypeVars (tpe : Type ): Set [Symbol ] = {
483- val result = new mutable. ListBuffer [Symbol ]()
479+ val result = ListBuffer .empty [Symbol ]
484480 specializedTypeVarsBuffer(tpe, result)
485481 result.toSet
486482 }
487483
488- def specializedTypeVarsBuffer (sym : Symbol , result : mutable. Buffer [Symbol ]): Unit =
484+ def specializedTypeVarsBuffer (sym : Symbol , result : Buffer [Symbol ]): Unit =
489485 if (! neverHasTypeParameters(sym))
490486 enteringTyper(specializedTypeVarsBuffer(sym.info, result))
491487
@@ -495,7 +491,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
495491 * - as arguments to type constructors in @specialized positions
496492 * (arrays are considered as Array[@specialized T])
497493 */
498- def specializedTypeVarsBuffer (tpe : Type , result : mutable. Buffer [Symbol ]): Unit = tpe match {
494+ def specializedTypeVarsBuffer (tpe : Type , result : Buffer [Symbol ]): Unit = tpe match {
499495 case TypeRef (pre, sym, args) =>
500496 if (sym.isAliasType)
501497 specializedTypeVarsBuffer(tpe.dealiasWiden, result)
@@ -601,7 +597,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
601597 * each combination of `stps`.
602598 */
603599 def specializeClass (clazz : Symbol , outerEnv : TypeEnv ): List [Symbol ] = {
604- def specializedClass (env0 : TypeEnv , normMembers : List [Symbol ]): Symbol = {
600+ def toSpecializedClass (env0 : TypeEnv , normMembers : List [Symbol ]): Symbol = {
605601 /* It gets hard to follow all the clazz and cls, and specializedClass
606602 * was both already used for a map and mucho long. So "sClass" is the
607603 * specialized subclass of "clazz" throughout this file.
@@ -642,7 +638,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
642638
643639 val env = mapAnyRefsInSpecSym(env0, clazz, sClass)
644640 typeEnv(sClass) = env
645- this . specializedClass.getOrElseUpdate(clazz, new mutable. AnyRefMap () ).update(env0, sClass)
641+ specializedClass.getOrElseUpdate(clazz, AnyRefMap .empty ).update(env0, sClass)
646642
647643 val decls1 = newScope // declarations of the newly specialized class 'sClass'
648644 var oldClassTParams : List [Symbol ] = Nil // original unspecialized type parameters
@@ -911,7 +907,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
911907 val subclasses = specializations(clazz.info.typeParams).filter(satisfiable(_))
912908 subclasses foreach {
913909 env =>
914- val spc = specializedClass (env, decls1)
910+ val spc = toSpecializedClass (env, decls1)
915911 val existing = clazz.owner.info.decl(spc.name)
916912
917913 // a symbol for the specialized class already exists if there's a classfile for it.
@@ -1541,10 +1537,10 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
15411537 }
15421538
15431539 /** Map a specializable method to its rhs, when not deferred. */
1544- val body = new mutable. AnyRefMap [Symbol , Tree ]()
1540+ val body = AnyRefMap .empty [Symbol , Tree ]
15451541
15461542 /** Map a specializable method to its value parameter symbols. */
1547- val parameters = new mutable. AnyRefMap [Symbol , List [Symbol ]]()
1543+ val parameters = AnyRefMap .empty [Symbol , List [Symbol ]]
15481544
15491545 /** Collect method bodies that are concrete specialized methods.
15501546 */
@@ -1746,7 +1742,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
17461742
17471743 case Template (parents, self, body) =>
17481744 def transformTemplate = {
1749- val specMembers = makeSpecializedMembers(tree.symbol.enclClass) ::: ( implSpecClasses(body) map localTyper.typed)
1745+ val specMembers = makeSpecializedMembers(tree.symbol.enclClass) ::: implSpecClasses(body). map( localTyper.typed)
17501746 if (! symbol.isPackageClass)
17511747 new CollectMethodBodies ()(tree)
17521748 // currentOwner.info.parents.map(tpe => TypeTree(tpe) setPos parents.head.pos)
@@ -1981,7 +1977,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
19811977 // add special overrides first
19821978// if (!specializedClass.hasFlag(SPECIALIZED))
19831979// for (m <- specialOverrides(specializedClass)) specializedClass.info.decls.enter(m)
1984- val mbrs = new mutable. ListBuffer [Tree ]
1980+ val mbrs = ListBuffer .empty [Tree ]
19851981 var hasSpecializedFields = false
19861982
19871983 for (m <- sClass.info.decls
@@ -2031,25 +2027,22 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
20312027 }
20322028
20332029 /** Create specialized class definitions */
2034- def implSpecClasses (trees : List [Tree ]): List [Tree ] = {
2035- trees flatMap {
2030+ def implSpecClasses (trees : List [Tree ]): List [Tree ] =
2031+ trees. flatMap {
20362032 case tree @ ClassDef (_, _, _, impl) =>
2037- tree.symbol.info // force specialization
2038- specializedClass.getOrNull(tree.symbol) match {
2039- case null => Nil
2040- case map =>
2041- val sym1 = tree.symbol
2042- map.iterator.map {
2043- case (env, specCls) =>
2044- debuglog(" created synthetic class: " + specCls + " of " + sym1 + " in " + pp(env))
2045- val parents = specCls.info.parents.map(TypeTree )
2046- ClassDef (specCls, atPos(impl.pos)(Template (parents, noSelfType, List ()))
2047- .setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
2048- }.toList
2049- }
2033+ val sym1 = tree.symbol
2034+ sym1.info // force specialization
2035+ val specMap = specializationOf(sym1)
2036+ if (specMap.isEmpty) Nil
2037+ else specMap.iterator.map {
2038+ case (env, specCls) =>
2039+ debuglog(s " created synthetic class: $specCls of $sym1 in ${pp(env)}" )
2040+ val parents = specCls.info.parents.map(TypeTree )
2041+ ClassDef (specCls, atPos(impl.pos)(Template (parents, noSelfType, List ()))
2042+ .setSymbol(specCls.newLocalDummy(sym1.pos))) setPos tree.pos
2043+ }.toList
20502044 case _ => Nil
2051- } sortBy (_.name.decoded)
2052- }
2045+ }.sortBy(_.name.decoded)
20532046 }
20542047
20552048 private def forwardCall (pos : scala.reflect.internal.util.Position , receiver : Tree , paramss : List [List [ValDef ]]): Tree = {
0 commit comments