Skip to content

Commit 319a0a3

Browse files
committed
Systematic use and caching of mangled
1 parent d97734b commit 319a0a3

File tree

8 files changed

+46
-21
lines changed

8 files changed

+46
-21
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
544544
def toTermName: Name = n.toTermName
545545
def dropModule: Name = n.stripModuleClassSuffix
546546

547-
def len: Int = n.toSimpleName.length
548-
def offset: Int = n.toSimpleName.start
547+
def len: Int = n.mangled.toSimpleName.length
548+
def offset: Int = n.mangled.toSimpleName.start
549549
def isTermName: Boolean = n.isTermName
550550
def startsWith(s: String): Boolean = n.startsWith(s)
551551
}

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>
157157
}
158158

159159
/** Is name a left-associative operator? */
160-
def isLeftAssoc(operator: Name) = !operator.isEmpty && (operator.toSimpleName.last != ':')
160+
def isLeftAssoc(operator: Name) = !operator.isEmpty && (operator.lastPart.last != ':')
161161

162162
/** can this type be a type pattern? */
163163
def mayBeTypePat(tree: untpd.Tree): Boolean = unsplice(tree) match {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ object NameKinds {
174174
private val FalseSuper = termName("$$super")
175175
private val FalseSuperLength = FalseSuper.length
176176

177+
/** Mangle all names in the expanded prefix.
178+
* We do this since we cannot reliably unmange names in the prefix, so
179+
* we leave the prefix in mangled form instead.
180+
*/
181+
override def apply(prefix: TermName, name: SimpleTermName) = {
182+
def mangleAll(prefix: TermName): TermName = prefix rewrite {
183+
case ExpandPrefixName(prefix1, name1) =>
184+
val prefix2 = mangleAll(prefix1)
185+
val name2 = name1.mangled
186+
if (prefix2 == prefix1 && name2 == name1) prefix
187+
else ExpandPrefixName(prefix2, name2)
188+
case prefix: SimpleTermName =>
189+
prefix.mangled
190+
}
191+
super.apply(mangleAll(prefix), name)
192+
}
193+
177194
override def unmangle(name: SimpleTermName): TermName = {
178195
var i = name.lastIndexOfSlice(str.EXPAND_SEPARATOR)
179196
if (i < 0) name

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,16 @@ object Names {
6666
def isSimple: Boolean
6767
def asSimpleName: SimpleTermName
6868
def toSimpleName: SimpleTermName
69-
def mangled: Name
69+
70+
@sharable // because it's just a cache for performance
71+
private[this] var myMangled: Name = null
72+
73+
def mangle: ThisName
74+
75+
def mangled: ThisName = {
76+
if (myMangled == null) myMangled = mangle
77+
myMangled.asInstanceOf[ThisName]
78+
}
7079

7180
def rewrite(f: PartialFunction[Name, Name]): ThisName
7281
def collect[T](f: PartialFunction[Name, T]): Option[T]
@@ -265,7 +274,9 @@ object Names {
265274
def isSimple = true
266275
def asSimpleName = this
267276
def toSimpleName = this
268-
def mangled = this
277+
final def mangle = this
278+
279+
final override def mangled: SimpleTermName = this.asSimpleName
269280

270281
def rewrite(f: PartialFunction[Name, Name]): ThisName =
271282
if (f.isDefinedAt(this)) likeSpaced(f(this)) else this
@@ -310,7 +321,7 @@ object Names {
310321
def isSimple = toTermName.isSimple
311322
def asSimpleName = toTermName.asSimpleName
312323
def toSimpleName = toTermName.toSimpleName
313-
def mangled = toTermName.toSimpleName.toTypeName
324+
final def mangle = toTermName.mangle.toTypeName
314325

315326
def rewrite(f: PartialFunction[Name, Name]): ThisName = toTermName.rewrite(f).toTypeName
316327
def collect[T](f: PartialFunction[Name, T]): Option[T] = toTermName.collect(f)
@@ -346,12 +357,8 @@ object Names {
346357
def isSimple = false
347358
def asSimpleName = throw new UnsupportedOperationException(s"$debugString is not a simple name")
348359

349-
private[this] var simpleName: SimpleTermName = null
350-
def toSimpleName = {
351-
if (simpleName == null) simpleName = termName(toString)
352-
simpleName
353-
}
354-
def mangled = toSimpleName
360+
def toSimpleName = termName(toString)
361+
final def mangle = toSimpleName
355362

356363
def rewrite(f: PartialFunction[Name, Name]): ThisName =
357364
if (f.isDefinedAt(this)) likeSpaced(f(this))

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ object StdNames {
2323
final val INITIALIZER_PREFIX = "initial$"
2424
final val SHADOWED_PREFIX = "(shadowed)"
2525
final val AVOID_CLASH_SUFFIX = "$_avoid_name_clash_$"
26-
final val MODULE_SUFFIX = NameTransformer.MODULE_SUFFIX_STRING
26+
final val MODULE_SUFFIX = "$"
27+
final val NAME_JOIN = "$"
2728
final val DEFAULT_GETTER = "$default$"
2829
final val LOCALDUMMY_PREFIX = "<local " // owner of local blocks
2930
final val ANON_CLASS = "$anon"
@@ -128,8 +129,7 @@ object StdNames {
128129
val EXPAND_SEPARATOR: N = str.EXPAND_SEPARATOR
129130
val IMPL_CLASS_SUFFIX: N = "$class"
130131
val IMPORT: N = "<import>"
131-
val MODULE_SUFFIX: N = NameTransformer.MODULE_SUFFIX_STRING
132-
val NAME_JOIN: N = NameTransformer.NAME_JOIN_STRING
132+
val MODULE_SUFFIX: N = str.MODULE_SUFFIX
133133
val OPS_PACKAGE: N = "<special-ops>"
134134
val OVERLOADED: N = "<overloaded>"
135135
val PACKAGE: N = "package"
@@ -257,9 +257,10 @@ object StdNames {
257257
val REIFY_FREE_THIS_SUFFIX: N = "$this"
258258
val REIFY_FREE_VALUE_SUFFIX: N = "$value"
259259
val REIFY_SYMDEF_PREFIX: N = "symdef$"
260-
val MODULE_INSTANCE_FIELD: N = NameTransformer.MODULE_INSTANCE_NAME // "MODULE$"
260+
val MODULE_INSTANCE_FIELD: N = "MODULE$"
261261
val OUTER: N = "$outer"
262262
val REFINE_CLASS: N = "<refinement>"
263+
val REFINE_CLASS_ENCODED: N = "$lessrefinement$greater"
263264
val ROOTPKG: N = "_root_"
264265
val SELECTOR_DUMMY: N = "<unapply-selector>"
265266
val SELF: N = "$this"

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class ClassfileParser(
9292
val nameIdx = in.nextChar
9393
currentClassName = pool.getClassName(nameIdx)
9494

95-
if (currentIsTopLevel && currentClassName != classRoot.fullName.toSimpleName)
95+
if (currentIsTopLevel && currentClassName != classRoot.fullName.mangled.toTermName)
9696
mismatchError(currentClassName)
9797

9898
addEnclosingTParams()
@@ -649,7 +649,7 @@ class ClassfileParser(
649649
*/
650650
private def enterOwnInnerClasses()(implicit ctx: Context): Unit = {
651651
def className(name: Name): Name = {
652-
val name1 = name.toSimpleName
652+
val name1 = name.mangled.toSimpleName
653653
name1.drop(name1.lastIndexOf('.') + 1)
654654
}
655655

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
157157
withoutPos(super.toText(tp))
158158
case tp: SelectionProto =>
159159
return "?{ " ~ toText(tp.name) ~
160-
(" " provided !tp.name.toSimpleName.decode.last.isLetterOrDigit) ~
160+
(" " provided !tp.name.lastPart.last.isLetterOrDigit) ~
161161
": " ~ toText(tp.memberProto) ~ " }"
162162
case tp: ViewProto =>
163163
return toText(tp.argType) ~ " ?=>? " ~ toText(tp.resultType)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import core.Contexts._
88
import core.Types._
99
import core.Flags._
1010
import core.Decorators._
11-
import core.StdNames.nme
11+
import core.StdNames.{str, nme}
1212
import core.Names._
1313
import core.NameOps._
1414
import core.Phases._
@@ -22,7 +22,7 @@ import collection.{ mutable, immutable }
2222
import collection.mutable.{ HashMap, HashSet, LinkedHashMap, LinkedHashSet, TreeSet }
2323

2424
object LambdaLift {
25-
private val NJ = NameTransformer.NAME_JOIN_STRING
25+
private val NJ = str.NAME_JOIN
2626
private class NoPath extends Exception
2727
}
2828

0 commit comments

Comments
 (0)