Skip to content

Commit 4552148

Browse files
committed
Fix #560 - refactor flatName
- Merge flatName and fullNameSeparated - Treat nested members of modules specially, to conform to scalac conventions - Use `~` as separator for term members.
1 parent 4b82e83 commit 4552148

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ class DottyBackendInterface()(implicit ctx: Context) extends BackendInterface{
554554
def fullName: String = sym.showFullName
555555
def simpleName: Name = sym.name
556556
def javaSimpleName: Name = toDenot(sym).name // addModuleSuffix(simpleName.dropLocal)
557-
def javaBinaryName: Name = toDenot(sym).fullNameSeparated('/') // addModuleSuffix(fullNameInternal('/'))
557+
def javaBinaryName: Name = toDenot(sym).fullNameSeparated("/") // addModuleSuffix(fullNameInternal('/'))
558558
def javaClassName: String = toDenot(sym).fullName.toString// addModuleSuffix(fullNameInternal('.')).toString
559559
def name: Name = sym.name
560560
def rawname: Name = sym.name // todo ????

src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ object NameOps {
150150
/** The expanded name of `name` relative to given class `base`.
151151
*/
152152
def expandedName(base: Symbol)(implicit ctx: Context): N =
153-
expandedName(if (base is Flags.ExpandedName) base.name else base.fullNameSeparated('$'))
153+
expandedName(if (base is Flags.ExpandedName) base.name else base.fullNameSeparated("$"))
154154

155155
/** The expanded name of `name` relative to `basename` with given `separator`
156156
*/

src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -289,39 +289,43 @@ object SymDenotations {
289289
}
290290

291291
/** The encoded full path name of this denotation, where outer names and inner names
292-
* are separated by `separator` characters.
292+
* are separated by `separator` strings.
293293
* Never translates expansions of operators back to operator symbol.
294-
* Drops package objects. Represents terms in the owner chain by a simple `separator`.
294+
* Drops package objects. Represents terms in the owner chain by a simple `~`.
295+
* (Note: scalac uses nothing to represent terms, which can cause name clashes
296+
* between same-named definitions in different enclosing methods. Before this commit
297+
* we used `$' but this can cause ambiguities with the class separator '$').
298+
* A separator "" means "flat name"; the real separator in this case is "$" and
299+
* enclosing packages do not form part of the name.
295300
*/
296-
def fullNameSeparated(separator: Char)(implicit ctx: Context): Name =
297-
if (symbol == NoSymbol || owner == NoSymbol || owner.isEffectiveRoot) name
301+
def fullNameSeparated(separator: String)(implicit ctx: Context): Name = {
302+
var sep = separator
303+
var stopAtPackage = false
304+
if (sep.isEmpty) {
305+
sep = "$"
306+
stopAtPackage = true
307+
}
308+
if (symbol == NoSymbol ||
309+
owner == NoSymbol ||
310+
owner.isEffectiveRoot ||
311+
stopAtPackage && owner.is(PackageClass)) name
298312
else {
299-
var owner = this
300-
var sep = ""
301-
do {
302-
owner = owner.owner
303-
sep += separator
304-
} while (!owner.isClass && !owner.isPackageObject)
305-
val fn = owner.fullNameSeparated(separator) ++ sep ++ name
313+
var encl = owner
314+
while (!encl.isClass && !encl.isPackageObject) {
315+
encl = encl.owner
316+
sep += "~"
317+
}
318+
if (owner.is(ModuleClass) && sep == "$") sep = "" // duplicate scalac's behavior: don't write a double '$$' for module class members.
319+
val fn = encl.fullNameSeparated(separator) ++ sep ++ name
306320
if (isType) fn.toTypeName else fn.toTermName
307321
}
322+
}
308323

309324
/** The encoded flat name of this denotation, where joined names are separated by `separator` characters. */
310-
def flatName(separator: Char = '$')(implicit ctx: Context): Name =
311-
if (symbol == NoSymbol || owner == NoSymbol || owner.isEffectiveRoot || (owner is PackageClass)) name
312-
else {
313-
var owner = this
314-
var sep = ""
315-
do {
316-
owner = owner.owner
317-
sep += separator
318-
} while (!owner.isClass && !owner.isPackageObject)
319-
val fn = owner.flatName(separator) ++ sep ++ name
320-
if (isType) fn.toTypeName else fn.toTermName
321-
}
325+
def flatName(implicit ctx: Context): Name = fullNameSeparated("")
322326

323327
/** `fullName` where `.' is the separator character */
324-
def fullName(implicit ctx: Context): Name = fullNameSeparated('.')
328+
def fullName(implicit ctx: Context): Name = fullNameSeparated(".")
325329

326330
// ----- Tests -------------------------------------------------
327331

@@ -1572,8 +1576,8 @@ object SymDenotations {
15721576
}
15731577
}
15741578

1575-
private[this] var fullNameCache: SimpleMap[Character, Name] = SimpleMap.Empty
1576-
override final def fullNameSeparated(separator: Char)(implicit ctx: Context): Name = {
1579+
private[this] var fullNameCache: SimpleMap[String, Name] = SimpleMap.Empty
1580+
override final def fullNameSeparated(separator: String)(implicit ctx: Context): Name = {
15771581
val cached = fullNameCache(separator)
15781582
if (cached != null) cached
15791583
else {

src/dotty/tools/dotc/transform/Flatten.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import DenotTransformers.SymTransformer
66
import Phases.Phase
77
import Contexts.Context
88
import Flags._
9-
import SymUtils._
109
import SymDenotations.SymDenotation
1110
import collection.mutable
1211
import TreeTransforms.MiniPhaseTransform
@@ -19,7 +18,7 @@ class Flatten extends MiniPhaseTransform with SymTransformer { thisTransform =>
1918
def transformSym(ref: SymDenotation)(implicit ctx: Context) = {
2019
if (ref.isClass && !ref.is(Package) && !ref.owner.is(Package)) {
2120
ref.copySymDenotation(
22-
name = ref.flatName(),
21+
name = ref.flatName,
2322
owner = ref.enclosingPackageClass)
2423
}
2524
else ref

src/dotty/tools/dotc/transform/SymUtils.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ class SymUtils(val self: Symbol) extends AnyVal {
8585
def field(implicit ctx: Context): Symbol =
8686
self.owner.info.decl(self.asTerm.name.fieldName).suchThat(!_.is(Method)).symbol
8787

88-
/** `fullName` where `$' is the separator character */
89-
def flatName(implicit ctx: Context): Name = self.flatName('$')
90-
9188
def initializer(implicit ctx: Context): TermSymbol =
9289
self.owner.info.decl(InitializerName(self.asTerm.name)).symbol.asTerm
9390

0 commit comments

Comments
 (0)