From b595a98a5b51f3b328f1d69e6afc56f19129666d Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 29 Mar 2017 18:56:29 +0200 Subject: New unmangling for ExpandedName --- compiler/src/dotty/tools/dotc/core/NameKinds.scala | 20 +++++++- compiler/src/dotty/tools/dotc/core/NameOps.scala | 57 +++++++--------------- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 8 +-- 3 files changed, 40 insertions(+), 45 deletions(-) (limited to 'compiler/src/dotty/tools/dotc/core') diff --git a/compiler/src/dotty/tools/dotc/core/NameKinds.scala b/compiler/src/dotty/tools/dotc/core/NameKinds.scala index 2c5e3ee3e..eea5aefe3 100644 --- a/compiler/src/dotty/tools/dotc/core/NameKinds.scala +++ b/compiler/src/dotty/tools/dotc/core/NameKinds.scala @@ -151,9 +151,27 @@ object NameKinds { val QualifiedName = new QualifiedNameKind(QUALIFIED, ".") val FlattenedName = new QualifiedNameKind(FLATTENED, "$") - val ExpandedName = new QualifiedNameKind(EXPANDED, str.EXPAND_SEPARATOR) val TraitSetterName = new QualifiedNameKind(TRAITSETTER, str.TRAIT_SETTER_SEPARATOR) + val ExpandedName = new QualifiedNameKind(EXPANDED, str.EXPAND_SEPARATOR) { + private val FalseSuper = "$$super".toTermName + private val FalseSuperLength = FalseSuper.length + + override def unmangle(name: SimpleTermName): TermName = { + var i = name.lastIndexOfSlice(str.EXPAND_SEPARATOR) + if (i < 0) name + else { + // Hack to make super accessors from traits work. They would otherwise fail because of #765 + // The problem is that in `x$$super$$plus` the expansion prefix needs to be `x` + // instead of `x$$super`. + if (i > FalseSuperLength && name.slice(i - FalseSuperLength, i) == FalseSuper) + i -= FalseSuper.length + + apply(name.take(i).asTermName, name.drop(i + str.EXPAND_SEPARATOR.length).asSimpleName) + } + } + } + val UniqueName = new UniqueNameKind("$") { override def mkString(underlying: TermName, info: ThisInfo) = if (underlying.isEmpty) "$" + info.num + "$" else super.mkString(underlying, info) diff --git a/compiler/src/dotty/tools/dotc/core/NameOps.scala b/compiler/src/dotty/tools/dotc/core/NameOps.scala index f936e5a34..8d5344d8a 100644 --- a/compiler/src/dotty/tools/dotc/core/NameOps.scala +++ b/compiler/src/dotty/tools/dotc/core/NameOps.scala @@ -151,17 +151,6 @@ object NameOps { name.rewrite { case ExpandedName(_, unexp) => unexp } } - def unexpandedNameOfMangled: N = likeTyped { - var idx = name.lastIndexOfSlice(nme.EXPAND_SEPARATOR) - - // Hack to make super accessors from traits work. They would otherwise fail because of #765 - // TODO: drop this once we have more robust name handling - if (idx > FalseSuperLength && name.slice(idx - FalseSuperLength, idx) == FalseSuper) - idx -= FalseSuper.length - - if (idx < 0) name else (name drop (idx + nme.EXPAND_SEPARATOR.length)) - } - def expandedPrefix: N = likeTyped { name.exclude(ExpandedName) } def expandedPrefixOfMangled: N = { @@ -170,15 +159,6 @@ object NameOps { likeTyped(name.take(idx)) } - def unmangleExpandedName: N = - if (name.isSimple) { - val unmangled = unexpandedNameOfMangled - if (name eq unmangled) name - else likeTyped( - ExpandedName(expandedPrefixOfMangled.toTermName, unmangled.asSimpleName)) - } - else name - def implClassName: N = likeTyped(name ++ tpnme.IMPL_CLASS_SUFFIX) def errorName: N = likeTyped(name ++ nme.ERROR) @@ -354,6 +334,23 @@ object NameOps { /** If name length exceeds allowable limit, replace part of it by hash */ def compactified(implicit ctx: Context): TermName = termName(compactify(name.toString)) + + def unmangle(kind: NameKind): N = likeTyped { + name rewrite { + case unmangled: SimpleTermName => + kind.unmangle(unmangled) + case ExpandedName(prefix, last) => + kind.unmangle(last) rewrite { + case kernel: SimpleTermName => + ExpandedName(prefix, kernel) + } + } + } + + def unmangle(kinds: List[NameKind]): N = { + val unmangled = (name /: kinds)(_.unmangle(_)) + if (unmangled eq name) name else unmangled.unmangle(kinds) + } } // needed??? @@ -473,26 +470,6 @@ object NameOps { case NO_NAME => primitivePostfixMethodName case name => name } - - def unmangleSuperName: TermName = - if (name.isSimple && name.startsWith(str.SUPER_PREFIX)) - SuperAccessorName(name.drop(str.SUPER_PREFIX.length).asTermName) - else name - - def unmangle(kind: NameKind): TermName = name rewrite { - case unmangled: SimpleTermName => - kind.unmangle(unmangled) - case ExpandedName(prefix, last) => - kind.unmangle(last) rewrite { - case kernel: SimpleTermName => - ExpandedName(prefix, kernel) - } - } - - def unmangle(kinds: List[NameKind]): TermName = { - val unmangled = (name /: kinds)(_.unmangle(_)) - if (unmangled eq name) name else unmangled.unmangle(kinds) - } } private final val FalseSuper = "$$super".toTermName diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index e4b6faa4b..608c77a8e 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -9,7 +9,7 @@ import java.lang.Double.longBitsToDouble import Contexts._, Symbols._, Types._, Scopes._, SymDenotations._, Names._, NameOps._ import StdNames._, Denotations._, NameOps._, Flags._, Constants._, Annotations._ -import NameKinds.Scala2MethodNameKinds +import NameKinds.{Scala2MethodNameKinds, SuperAccessorName, ExpandedName} import dotty.tools.dotc.typer.ProtoTypes.{FunProtoTyped, FunProto} import util.Positions._ import dotty.tools.dotc.ast.{tpd, Trees, untpd}, ast.tpd._ @@ -439,12 +439,12 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas if (name == nme.TRAIT_CONSTRUCTOR) nme.CONSTRUCTOR else name.asTermName.unmangle(Scala2MethodNameKinds) } - if (flags is Scala2ExpandedName) { - name = name.unmangleExpandedName + if ((flags is Scala2ExpandedName) && name.isSimple) { + name = name.unmangle(ExpandedName) flags = flags &~ Scala2ExpandedName } if (flags is Scala2SuperAccessor) { - name = name.asTermName.unmangleSuperName + name = name.asTermName.unmangle(SuperAccessorName) flags = flags &~ Scala2SuperAccessor } -- cgit v1.2.3