From 2bfb2ca70c4588de00c12feba79ef7c0d68361a5 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 1 Apr 2017 18:51:11 +0200 Subject: Introduce mangled method Mangled is like toSimpleName, except that it keeps the term/type distinction. --- compiler/src/dotty/tools/dotc/core/Names.scala | 7 +++++++ compiler/src/dotty/tools/dotc/core/Scopes.scala | 4 ++-- compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala | 4 ++-- .../src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala | 4 ++-- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/Names.scala b/compiler/src/dotty/tools/dotc/core/Names.scala index 352fa6b6b..6caba33ec 100644 --- a/compiler/src/dotty/tools/dotc/core/Names.scala +++ b/compiler/src/dotty/tools/dotc/core/Names.scala @@ -66,6 +66,8 @@ object Names { def isSimple: Boolean def asSimpleName: SimpleTermName def toSimpleName: SimpleTermName + def mangled: Name + def rewrite(f: PartialFunction[Name, Name]): ThisName def collect[T](f: PartialFunction[Name, T]): Option[T] def mapLast(f: SimpleTermName => SimpleTermName): ThisName @@ -263,6 +265,8 @@ object Names { def isSimple = true def asSimpleName = this def toSimpleName = this + def mangled = this + def rewrite(f: PartialFunction[Name, Name]): ThisName = if (f.isDefinedAt(this)) likeSpaced(f(this)) else this def collect[T](f: PartialFunction[Name, T]): Option[T] = f.lift(this) @@ -306,6 +310,8 @@ object Names { def isSimple = toTermName.isSimple def asSimpleName = toTermName.asSimpleName def toSimpleName = toTermName.toSimpleName + def mangled = toTermName.toSimpleName.toTypeName + def rewrite(f: PartialFunction[Name, Name]): ThisName = toTermName.rewrite(f).toTypeName def collect[T](f: PartialFunction[Name, T]): Option[T] = toTermName.collect(f) def mapLast(f: SimpleTermName => SimpleTermName) = toTermName.mapLast(f).toTypeName @@ -345,6 +351,7 @@ object Names { if (simpleName == null) simpleName = termName(toString) simpleName } + def mangled = toSimpleName def rewrite(f: PartialFunction[Name, Name]): ThisName = if (f.isDefinedAt(this)) likeSpaced(f(this)) diff --git a/compiler/src/dotty/tools/dotc/core/Scopes.scala b/compiler/src/dotty/tools/dotc/core/Scopes.scala index 032442421..023fe35c5 100644 --- a/compiler/src/dotty/tools/dotc/core/Scopes.scala +++ b/compiler/src/dotty/tools/dotc/core/Scopes.scala @@ -396,10 +396,10 @@ object Scopes { class PackageScope extends MutableScope { override final def newScopeEntry(name: Name, sym: Symbol)(implicit ctx: Context): ScopeEntry = - super.newScopeEntry(name.toSimpleName, sym) + super.newScopeEntry(name.mangled, sym) override final def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = - super.lookupEntry(name.toSimpleName) + super.lookupEntry(name.mangled) } /** Create a new scope */ diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index 81519727c..7d08c958c 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -416,7 +416,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi val tag = readByte() val end = readEnd() var name: Name = readName() - val sname = name.toSimpleName + val mname = name.mangled if (tag == TYPEDEF || tag == TYPEPARAM) name = name.toTypeName skipParams() val ttag = nextUnsharedTag @@ -433,7 +433,7 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi def adjustIfModule(completer: LazyType) = if (flags is Module) ctx.adjustModuleCompleter(completer, name) else completer val sym = - roots.find(root => (root.owner eq ctx.owner) && root.name.toSimpleName == sname && root.isType == name.isTypeName) match { + roots.find(root => (root.owner eq ctx.owner) && root.name.mangled == mname) match { case Some(rootd) => pickling.println(i"overwriting ${rootd.symbol} # ${rootd.hashCode}") rootd.name = name diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 1e8fbe54b..f3bb99b27 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -448,8 +448,8 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas flags = flags &~ Scala2SuperAccessor } - val sname = name.toSimpleName - def nameMatches(rootName: Name) = sname == rootName.toSimpleName + val mname = name.mangled + def nameMatches(rootName: Name) = mname == rootName.mangled def isClassRoot = nameMatches(classRoot.name) && (owner == classRoot.owner) && !(flags is ModuleClass) def isModuleClassRoot = nameMatches(moduleClassRoot.name) && (owner == moduleClassRoot.owner) && (flags is Module) def isModuleRoot = nameMatches(moduleClassRoot.name.sourceModuleName) && (owner == moduleClassRoot.owner) && (flags is Module) -- cgit v1.2.3