Skip to content

Commit 0735a68

Browse files
committed
Use mangled string in backend
Avoid using toString directly.
1 parent 2c4ac2c commit 0735a68

File tree

6 files changed

+24
-35
lines changed

6 files changed

+24
-35
lines changed

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Decorators._
3030
import tpd._
3131

3232
import scala.tools.asm
33-
import StdNames.nme
33+
import StdNames.{nme, str}
3434
import NameOps._
3535
import NameKinds.DefaultGetterName
3636
import dotty.tools.dotc.core
@@ -161,7 +161,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
161161
def boxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses().map{x => // @darkdimius Are you sure this should be a def?
162162
(x, Erasure.Boxing.boxMethod(x.asClass))
163163
}.toMap
164-
def unboxMethods: Map[Symbol, Symbol] = defn.ScalaValueClasses().map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap
164+
def unboxMethods: Map[Symbol, Symbol] =
165+
defn.ScalaValueClasses().map(x => (x, Erasure.Boxing.unboxMethod(x.asClass))).toMap
165166

166167
override def isSyntheticArrayConstructor(s: Symbol) = {
167168
s eq defn.newArrayMethod
@@ -244,15 +245,15 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
244245
case ClazzTag => av.visit(name, const.typeValue.toTypeKind(bcodeStore)(innerClasesStore).toASMType)
245246
case EnumTag =>
246247
val edesc = innerClasesStore.typeDescriptor(const.tpe.asInstanceOf[bcodeStore.int.Type]) // the class descriptor of the enumeration class.
247-
val evalue = const.symbolValue.name.toString // value the actual enumeration value.
248+
val evalue = const.symbolValue.name.mangledString // value the actual enumeration value.
248249
av.visitEnum(name, edesc, evalue)
249250
}
250251
case t: TypeApply if (t.fun.symbol == Predef_classOf) =>
251252
av.visit(name, t.args.head.tpe.classSymbol.denot.info.toTypeKind(bcodeStore)(innerClasesStore).toASMType)
252253
case t: tpd.Select =>
253254
if (t.symbol.denot.is(Flags.Enum)) {
254255
val edesc = innerClasesStore.typeDescriptor(t.tpe.asInstanceOf[bcodeStore.int.Type]) // the class descriptor of the enumeration class.
255-
val evalue = t.symbol.name.toString // value the actual enumeration value.
256+
val evalue = t.symbol.name.mangledString // value the actual enumeration value.
256257
av.visitEnum(name, edesc, evalue)
257258
} else {
258259
assert(toDenot(t.symbol).name.is(DefaultGetterName)) // this should be default getter. do not emmit.
@@ -262,8 +263,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
262263
for(arg <- t.elems) { emitArgument(arrAnnotV, null, arg, bcodeStore)(innerClasesStore) }
263264
arrAnnotV.visitEnd()
264265

265-
case Apply(fun, args) if (fun.symbol == defn.ArrayClass.primaryConstructor ||
266-
(toDenot(fun.symbol).owner == defn.ArrayClass.linkedClass && fun.symbol.name == nme_apply)) =>
266+
case Apply(fun, args) if fun.symbol == defn.ArrayClass.primaryConstructor ||
267+
toDenot(fun.symbol).owner == defn.ArrayClass.linkedClass && fun.symbol.name == nme_apply =>
267268
val arrAnnotV: AnnotationVisitor = av.visitArray(name)
268269

269270
var actualArgs = if (fun.tpe.isInstanceOf[ImplicitMethodType]) {
@@ -311,7 +312,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
311312
private def emitAssocs(av: asm.AnnotationVisitor, assocs: List[(Name, Object)], bcodeStore: BCodeHelpers)
312313
(innerClasesStore: bcodeStore.BCInnerClassGen) = {
313314
for ((name, value) <- assocs)
314-
emitArgument(av, name.toString, value.asInstanceOf[Tree], bcodeStore)(innerClasesStore)
315+
emitArgument(av, name.mangledString, value.asInstanceOf[Tree], bcodeStore)(innerClasesStore)
315316
av.visitEnd()
316317
}
317318

@@ -360,9 +361,8 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
360361
else clazz.getName
361362
}
362363

363-
def requiredClass[T](implicit evidence: ClassTag[T]): Symbol = {
364+
def requiredClass[T](implicit evidence: ClassTag[T]): Symbol =
364365
ctx.requiredClass(erasureString(evidence.runtimeClass).toTermName)
365-
}
366366

367367
def requiredModule[T](implicit evidence: ClassTag[T]): Symbol = {
368368
val moduleName = erasureString(evidence.runtimeClass)
@@ -409,7 +409,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
409409
def newSet[K](): mutable.Set[K] = new mutable.HashSet[K]
410410
}
411411

412-
val MODULE_INSTANCE_FIELD: String = nme.MODULE_INSTANCE_FIELD.toString
412+
val MODULE_INSTANCE_FIELD: String = str.MODULE_INSTANCE_FIELD
413413

414414
def dropModule(str: String) =
415415
if (!str.isEmpty && str.last == '$') str.take(str.length - 1) else str
@@ -539,20 +539,21 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
539539
def isTypeName: Boolean = n.isTypeName
540540
def isTermName: Boolean = n.isTermName
541541
def startsWith(s: String): Boolean = n.startsWith(s)
542+
def mangledString: String = n.mangledString
542543
}
543544

544545
implicit def symHelper(sym: Symbol): SymbolHelper = new SymbolHelper {
545546
// names
546547
def fullName(sep: Char): String = sym.showFullName
547548
def fullName: String = sym.showFullName
548549
def simpleName: Name = sym.name
549-
def javaSimpleName: Name = toDenot(sym).name // addModuleSuffix(simpleName.dropLocal)
550+
def javaSimpleName: String = toDenot(sym).name.mangledString // addModuleSuffix(simpleName.dropLocal)
550551
def javaBinaryName: String = javaClassName.replace('.', '/') // TODO: can we make this a string? addModuleSuffix(fullNameInternal('/'))
551-
def javaClassName: String = toDenot(sym).fullName.toString// addModuleSuffix(fullNameInternal('.')).toString
552+
def javaClassName: String = toDenot(sym).fullName.mangledString // addModuleSuffix(fullNameInternal('.')).toString
552553
def name: Name = sym.name
553-
def rawname: Name = {
554+
def rawname: String = {
554555
val original = toDenot(sym).initial
555-
sym.name(ctx.withPhase(original.validFor.phaseId))
556+
sym.name(ctx.withPhase(original.validFor.phaseId)).mangledString
556557
}
557558

558559
// types
@@ -665,9 +666,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
665666
}
666667
def enclClass: Symbol = toDenot(sym).enclosingClass
667668
def linkedClassOfClass: Symbol = linkedClass
668-
def linkedClass: Symbol = {
669-
toDenot(sym)(ctx).linkedClass(ctx)
670-
} //exitingPickler(sym.linkedClassOfClass)
669+
def linkedClass: Symbol = toDenot(sym)(ctx).linkedClass(ctx) //exitingPickler(sym.linkedClassOfClass)
671670
def companionClass: Symbol = toDenot(sym).companionClass
672671
def companionModule: Symbol = toDenot(sym).companionModule
673672
def companionSymbol: Symbol = if (sym is Flags.Module) companionClass else companionModule

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ class GenBCode extends Phase {
5050
new PlainDirectory(new Directory(new JFile(ctx.settings.d.value)))
5151

5252
def run(implicit ctx: Context): Unit = {
53-
val saved = Names.useMangled
54-
Names.useMangled = true
55-
try new GenBCodePipeline(entryPoints.toList,
56-
new DottyBackendInterface(outputDir, superCallsMap.toMap)(ctx))(ctx).run(ctx.compilationUnit.tpdTree)
57-
finally Names.useMangled = saved
53+
new GenBCodePipeline(entryPoints.toList,
54+
new DottyBackendInterface(outputDir, superCallsMap.toMap)(ctx))(ctx).run(ctx.compilationUnit.tpdTree)
5855
entryPoints.clear()
5956
}
6057
}
@@ -205,7 +202,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
205202

206203
if (claszSymbol.isClass) // @DarkDimius is this test needed here?
207204
for (binary <- ctx.compilationUnit.pickled.get(claszSymbol.asClass)) {
208-
val dataAttr = new CustomAttr(nme.TASTYATTR.toString, binary)
205+
val dataAttr = new CustomAttr(nme.TASTYATTR.mangledString, binary)
209206
val store = if (mirrorC ne null) mirrorC else plainC
210207
store.visitAttribute(dataAttr)
211208
if (ctx.settings.emitTasty.value) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ object Denotations {
582582
def hasUniqueSym: Boolean
583583
protected def newLikeThis(symbol: Symbol, info: Type): SingleDenotation
584584

585-
final def signature(implicit ctx: Context): Signature = {
585+
final def signature(implicit ctx: Context): Signature =
586586
if (isType) Signature.NotAMethod // don't force info if this is a type SymDenotation
587587
else info match {
588588
case info: MethodicType =>
@@ -594,7 +594,6 @@ object Denotations {
594594
}
595595
case _ => Signature.NotAMethod
596596
}
597-
}
598597

599598
def derivedSingleDenotation(symbol: Symbol, info: Type)(implicit ctx: Context): SingleDenotation =
600599
if ((symbol eq this.symbol) && (info eq this.info)) this

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ import java.util.HashMap
2121
object Names {
2222
import NameKinds._
2323

24-
// Gross hack because the backend uses toString on arbitrary names, which means
25-
// that the NameHelper abstraction is leaky. As long as cannot get rid of this,
26-
// parallel compilation is impossible.
27-
@sharable var useMangled: Boolean = false
28-
2924
/** A common class for things that can be turned into names.
3025
* Instances are both names and strings, the latter via a decorator.
3126
*/
@@ -72,6 +67,7 @@ object Names {
7267
def asSimpleName: SimpleTermName
7368
def toSimpleName: SimpleTermName
7469
def mangled: Name
70+
def mangledString: String = mangled.toString
7571

7672
def rewrite(f: PartialFunction[Name, Name]): ThisName
7773
def collect[T](f: PartialFunction[Name, T]): Option[T]
@@ -293,10 +289,7 @@ object Names {
293289

294290
override def toString =
295291
if (length == 0) ""
296-
else {
297-
val n = if (useMangled) mangled.asSimpleName else this
298-
new String(chrs, n.start, n.length)
299-
}
292+
else new String(chrs, start, length)
300293

301294
def debugString: String = toString
302295
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ object StdNames {
3333
final val INTERPRETER_LINE_PREFIX = "line"
3434
final val INTERPRETER_VAR_PREFIX = "res"
3535
final val INTERPRETER_WRAPPER_SUFFIX = "$object"
36+
final val MODULE_INSTANCE_FIELD = NameTransformer.MODULE_INSTANCE_NAME // "MODULE$"
3637

3738
final val Function = "Function"
3839
final val ImplicitFunction = "ImplicitFunction"

0 commit comments

Comments
 (0)