Skip to content

Commit 98daf03

Browse files
committed
Overhauled local/getter/setter name logic.
Sifted through extraneous methods trying to find consistency, consolidating and deprecating as I went. The original motivation for all this was the restoration of LOCAL_SUFFIX to originalName, because: It looks like in an attempt to make originalName print consistently with decodedName, I went a little too far and stripped invisible trailing spaces from originalName. This meant outer fields would have an originalName of '$outer' instead of '$outer ', which in turn could have caused them to be mis-recognized as outer accessors, because the logic of outerSource hinges upon "originalName == nme.OUTER". I don't know if this affected anything - I noticed it by inspection, improbably enough. Deprecated originalName - original, compared to what? - in favor of unexpandedName, which has a more obvious complement. Introduced string_== for the many spots where people have given up and are comparing string representations of names. A light dusting of types is still better than nothing. Editoral note: LOCAL_SUFFIX is the worst. Significant trailing whitespace! It's a time bomb.
1 parent 1187c98 commit 98daf03

File tree

24 files changed

+216
-200
lines changed

24 files changed

+216
-200
lines changed

src/compiler/scala/reflect/reify/phases/Reshape.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ trait Reshape {
254254
case _ => rhs // unit or trait case
255255
}
256256
val DefDef(mods0, name0, _, _, tpt0, rhs0) = ddef
257-
val name1 = nme.dropLocalSuffix(name0)
257+
val name1 = name0.dropLocal
258258
val Modifiers(flags0, privateWithin0, annotations0) = mods0
259259
val flags1 = (flags0 & GetterFlags) & ~(STABLE | ACCESSOR | METHOD)
260260
val mods1 = Modifiers(flags1, privateWithin0, annotations0) setPositions mods0.positions
@@ -273,7 +273,9 @@ trait Reshape {
273273
if (defdef.name.startsWith(prefix)) {
274274
val name = defdef.name.toString.substring(prefix.length)
275275
def uncapitalize(s: String) = if (s.length == 0) "" else { val chars = s.toCharArray; chars(0) = chars(0).toLower; new String(chars) }
276-
def findValDef(name: String) = (symdefs.values collect { case vdef: ValDef if nme.dropLocalSuffix(vdef.name).toString == name => vdef }).headOption
276+
def findValDef(name: String) = symdefs.values collectFirst {
277+
case vdef: ValDef if vdef.name.dropLocal string_== name => vdef
278+
}
277279
val valdef = findValDef(name).orElse(findValDef(uncapitalize(name))).orNull
278280
if (valdef != null) accessors(valdef) = accessors.getOrElse(valdef, Nil) :+ defdef
279281
}
@@ -297,7 +299,7 @@ trait Reshape {
297299
mods
298300
}
299301
val mods2 = toPreTyperModifiers(mods1, vdef.symbol)
300-
val name1 = nme.dropLocalSuffix(name)
302+
val name1 = name.dropLocal
301303
val vdef1 = ValDef(mods2, name1.toTermName, tpt, rhs)
302304
if (reifyDebug) println("resetting visibility of field: %s => %s".format(vdef, vdef1))
303305
Some(vdef1) // no copyAttrs here, because new ValDef and old symbols are now out of sync

src/compiler/scala/tools/nsc/backend/jvm/GenASM.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ abstract class GenASM extends SubComponent with BytecodeWriters with GenJVMASM {
290290
def inameToSymbol(iname: String): Symbol = {
291291
val name = global.newTypeName(iname)
292292
val res0 =
293-
if (nme.isModuleName(name)) rootMirror.getModule(nme.stripModuleSuffix(name))
293+
if (nme.isModuleName(name)) rootMirror.getModule(name.dropModule)
294294
else rootMirror.getClassByName(name.replace('/', '.')) // TODO fails for inner classes (but this hasn't been tested).
295295
assert(res0 != NoSymbol)
296296
val res = jsymbol(res0)

src/compiler/scala/tools/nsc/backend/opt/Inliners.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ abstract class Inliners extends SubComponent {
382382
val shouldWarn = hasInline(i.method)
383383

384384
def warnNoInline(reason: String): Boolean = {
385-
def msg = "Could not inline required method %s because %s.".format(i.method.originalName.decode, reason)
385+
def msg = "Could not inline required method %s because %s.".format(i.method.unexpandedName.decode, reason)
386386
if (settings.debug.value)
387387
inlineLog("fail", i.method.fullName, reason)
388388
if (shouldWarn)
@@ -565,7 +565,7 @@ abstract class Inliners extends SubComponent {
565565
while (retry && count < MAX_INLINE_RETRY)
566566

567567
for(inlFail <- tfa.warnIfInlineFails) {
568-
warn(inlFail.pos, "At the end of the day, could not inline @inline-marked method " + inlFail.method.originalName.decode)
568+
warn(inlFail.pos, "At the end of the day, could not inline @inline-marked method " + inlFail.method.unexpandedName.decode)
569569
}
570570

571571
m.normalize()

src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ abstract class ClassfileParser {
185185
if (in.buf(start).toInt != CONSTANT_CLASS) errorBadTag(start)
186186
val name = getExternalName(in.getChar(start + 1))
187187
if (nme.isModuleName(name))
188-
c = rootMirror.getModuleByName(nme.stripModuleSuffix(name))
188+
c = rootMirror.getModuleByName(name.dropModule)
189189
else
190190
c = classNameToSymbol(name)
191191

@@ -238,7 +238,7 @@ abstract class ClassfileParser {
238238
if (f == NoSymbol)
239239
f = rootMirror.getModuleByName(name dropRight 1)
240240
} else {
241-
val origName = nme.originalName(name)
241+
val origName = nme.unexpandedName(name)
242242
val owner = if (static) ownerTpe.typeSymbol.linkedClassOfClass else ownerTpe.typeSymbol
243243
// println("\t" + owner.info.member(name).tpe.widen + " =:= " + tpe)
244244
f = owner.info.findMember(origName, 0, 0, stableOnly = false).suchThat(_.tpe.widen =:= tpe)
@@ -1185,7 +1185,7 @@ abstract class ClassfileParser {
11851185

11861186
innerClasses.get(externalName) match {
11871187
case Some(entry) =>
1188-
val outerName = nme.stripModuleSuffix(entry.outerName)
1188+
val outerName = entry.outerName.dropModule
11891189
val sym = classSymbol(outerName)
11901190
val s =
11911191
// if loading during initialization of `definitions` typerPhase is not yet set.

src/compiler/scala/tools/nsc/symtab/classfile/ICodeReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ abstract class ICodeReader extends ClassfileParser {
164164
rootMirror.getClassByName(name)
165165
}
166166
else if (nme.isModuleName(name)) {
167-
val strippedName = nme.stripModuleSuffix(name)
167+
val strippedName = name.dropModule
168168
forceMangledName(newTermName(strippedName.decode), module = true) orElse rootMirror.getModuleByName(strippedName)
169169
}
170170
else {

src/compiler/scala/tools/nsc/transform/Constructors.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
9494
val paramAccessors = clazz.constrParamAccessors
9595

9696
// The constructor parameter corresponding to an accessor
97-
def parameter(acc: Symbol): Symbol =
98-
parameterNamed(nme.getterName(acc.originalName.toTermName))
97+
def parameter(acc: Symbol): Symbol = parameterNamed(acc.unexpandedName.getterName)
9998

10099
// The constructor parameter with given name. This means the parameter
101100
// has given name, or starts with given name, and continues with a `$` afterwards.
@@ -191,8 +190,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
191190
stat match {
192191
case ValDef(mods, name, _, _) if (mods hasFlag PRESUPER) =>
193192
// stat is the constructor-local definition of the field value
194-
val fields = presupers filter (
195-
vdef => nme.localToGetter(vdef.name) == name)
193+
val fields = presupers filter (_.getterName == name)
196194
assert(fields.length == 1)
197195
val to = fields.head.symbol
198196
if (!to.tpe.isInstanceOf[ConstantType])
@@ -314,10 +312,8 @@ abstract class Constructors extends Transform with ast.TreeDSL {
314312

315313
def specializedAssignFor(sym: Symbol): Option[Tree] =
316314
specializedStats find {
317-
case Assign(sel @ Select(This(_), _), rhs) =>
318-
( (sel.symbol hasFlag SPECIALIZED)
319-
&& (nme.unspecializedName(nme.localToGetter(sel.symbol.name.toTermName)) == nme.localToGetter(sym.name.toTermName))
320-
)
315+
case Assign(sel @ Select(This(_), _), _) =>
316+
sel.symbol.isSpecialized && (nme.unspecializedName(sel.symbol.getterName) == sym.getterName)
321317
case _ => false
322318
}
323319

@@ -432,17 +428,15 @@ abstract class Constructors extends Transform with ast.TreeDSL {
432428
}
433429

434430
def addGetter(sym: Symbol): Symbol = {
435-
val getr = addAccessor(
436-
sym, nme.getterName(sym.name.toTermName), getterFlags(sym.flags))
431+
val getr = addAccessor(sym, sym.getterName, getterFlags(sym.flags))
437432
getr setInfo MethodType(List(), sym.tpe)
438433
defBuf += localTyper.typedPos(sym.pos)(DefDef(getr, Select(This(clazz), sym)))
439434
getr
440435
}
441436

442437
def addSetter(sym: Symbol): Symbol = {
443438
sym setFlag MUTABLE
444-
val setr = addAccessor(
445-
sym, nme.getterToSetter(nme.getterName(sym.name.toTermName)), setterFlags(sym.flags))
439+
val setr = addAccessor(sym, sym.setterName, setterFlags(sym.flags))
446440
setr setInfo MethodType(setr.newSyntheticValueParams(List(sym.tpe)), UnitClass.tpe)
447441
defBuf += localTyper.typed {
448442
//util.trace("adding setter def for "+setr) {

src/compiler/scala/tools/nsc/transform/LazyVals.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ abstract class LazyVals extends Transform with TypingTransformers with ast.TreeD
183183

184184
if (bmps.isEmpty) rhs else rhs match {
185185
case Block(assign, l @ LabelDef(name, params, _))
186-
if name.toString == ("_" + methSym.name) && isMatch(params) =>
186+
if (name string_== "_" + methSym.name) && isMatch(params) =>
187187
Block(assign, deriveLabelDef(l)(rhs => typed(prependStats(bmps, rhs))))
188188

189189
case _ => prependStats(bmps, rhs)

src/compiler/scala/tools/nsc/transform/Mixin.scala

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,6 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
185185
newSym updateInfo (mixinMember.info cloneInfo newSym)
186186
}
187187

188-
def needsExpandedSetterName(field: Symbol) = !field.isLazy && (
189-
if (field.isMethod) field.hasStableFlag
190-
else !field.isMutable
191-
)
192-
193188
/** Add getters and setters for all non-module fields of an implementation
194189
* class to its interface unless they are already present. This is done
195190
* only once per class. The mixedin flag is used to remember whether late
@@ -207,19 +202,19 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
207202
// println("creating new getter for "+ field +" : "+ field.info +" at "+ field.locationString+(field hasFlag MUTABLE))
208203
val newFlags = field.flags & ~PrivateLocal | ACCESSOR | lateDEFERRED | ( if (field.isMutable) 0 else STABLE )
209204
// TODO preserve pre-erasure info?
210-
clazz.newMethod(nme.getterName(field.name.toTermName), field.pos, newFlags) setInfo MethodType(Nil, field.info)
205+
clazz.newMethod(field.getterName, field.pos, newFlags) setInfo MethodType(Nil, field.info)
211206
}
212207

213208
/* Create a new setter. Setters are never private or local. They are
214209
* always accessors and deferred. */
215210
def newSetter(field: Symbol): Symbol = {
216211
//println("creating new setter for "+field+field.locationString+(field hasFlag MUTABLE))
217-
val setterName = nme.getterToSetter(nme.getterName(field.name.toTermName))
212+
val setterName = field.setterName
218213
val newFlags = field.flags & ~PrivateLocal | ACCESSOR | lateDEFERRED
219214
val setter = clazz.newMethod(setterName, field.pos, newFlags)
220215
// TODO preserve pre-erasure info?
221216
setter setInfo MethodType(setter.newSyntheticValueParams(List(field.info)), UnitClass.tpe)
222-
if (needsExpandedSetterName(field))
217+
if (field.needsExpandedSetterName)
223218
setter.name = nme.expandedSetterName(setter.name, clazz)
224219

225220
setter
@@ -237,7 +232,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
237232
val getter = member.getter(clazz)
238233
if (getter == NoSymbol) addMember(clazz, newGetter(member))
239234
if (!member.tpe.isInstanceOf[ConstantType] && !member.isLazy) {
240-
val setter = member.setter(clazz, needsExpandedSetterName(member))
235+
val setter = member.setter(clazz)
241236
if (setter == NoSymbol) addMember(clazz, newSetter(member))
242237
}
243238
}
@@ -315,7 +310,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
315310
// carries over the current entry in the type history)
316311
val sym = enteringErasure {
317312
// so we have a type history entry before erasure
318-
clazz.newValue(nme.getterToLocal(mixinMember.name.toTermName), mixinMember.pos).setInfo(mixinMember.tpe.resultType)
313+
clazz.newValue(mixinMember.localName, mixinMember.pos).setInfo(mixinMember.tpe.resultType)
319314
}
320315
sym updateInfo mixinMember.tpe.resultType // info at current phase
321316

@@ -1236,10 +1231,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
12361231
case Assign(Apply(lhs @ Select(qual, _), List()), rhs) =>
12371232
// assign to fields in some implementation class via an abstract
12381233
// setter in the interface.
1239-
def setter = lhs.symbol.setter(
1240-
toInterface(lhs.symbol.owner.tpe).typeSymbol,
1241-
needsExpandedSetterName(lhs.symbol)
1242-
) setPos lhs.pos
1234+
def setter = lhs.symbol.setter(toInterface(lhs.symbol.owner.tpe).typeSymbol) setPos lhs.pos
12431235

12441236
typedPos(tree.pos)((qual DOT setter)(rhs))
12451237

src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,20 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
322322
/** Specialize name for the two list of types. The first one denotes
323323
* specialization on method type parameters, the second on outer environment.
324324
*/
325-
private def specializedName(name: Name, types1: List[Type], types2: List[Type]): TermName = {
326-
if (nme.INITIALIZER == name || (types1.isEmpty && types2.isEmpty))
325+
private def specializedName(name: Name, types1: List[Type], types2: List[Type]): TermName = (
326+
if (name == nme.CONSTRUCTOR || (types1.isEmpty && types2.isEmpty))
327327
name.toTermName
328328
else if (nme.isSetterName(name))
329-
nme.getterToSetter(specializedName(nme.setterToGetter(name.toTermName), types1, types2))
329+
specializedName(name.getterName, types1, types2).setterName
330330
else if (nme.isLocalName(name))
331-
nme.getterToLocal(specializedName(nme.localToGetter(name.toTermName), types1, types2))
331+
specializedName(name.getterName, types1, types2).localName
332332
else {
333333
val (base, cs, ms) = nme.splitSpecializedName(name)
334334
newTermName(base.toString + "$"
335335
+ "m" + ms + types1.map(t => definitions.abbrvTag(t.typeSymbol)).mkString("", "", "")
336336
+ "c" + cs + types2.map(t => definitions.abbrvTag(t.typeSymbol)).mkString("", "", "$sp"))
337337
}
338-
}
338+
)
339339

340340
lazy val specializableTypes = ScalaValueClasses map (_.tpe) sorted
341341

@@ -714,7 +714,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
714714
// debuglog("m: " + m + " isLocal: " + nme.isLocalName(m.name) + " specVal: " + specVal.name + " isLocal: " + nme.isLocalName(specVal.name))
715715

716716
if (nme.isLocalName(m.name)) {
717-
val specGetter = mkAccessor(specVal, nme.localToGetter(specVal.name.toTermName)) setInfo MethodType(Nil, specVal.info)
717+
val specGetter = mkAccessor(specVal, specVal.getterName) setInfo MethodType(Nil, specVal.info)
718718
val origGetter = overrideIn(sClass, m.getter(clazz))
719719
info(origGetter) = Forward(specGetter)
720720
enterMember(specGetter)
@@ -729,7 +729,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
729729
}
730730

731731
if (specVal.isVariable && m.setter(clazz) != NoSymbol) {
732-
val specSetter = mkAccessor(specVal, nme.getterToSetter(specGetter.name))
732+
val specSetter = mkAccessor(specVal, specGetter.setterName)
733733
.resetFlag(STABLE)
734734
specSetter.setInfo(MethodType(specSetter.newSyntheticValueParams(List(specVal.info)),
735735
UnitClass.tpe))
@@ -1805,7 +1805,7 @@ abstract class SpecializeTypes extends InfoTransform with TypingTransformers {
18051805
*/
18061806
def initializesSpecializedField(f: Symbol) = (
18071807
(f.name endsWith nme.SPECIALIZED_SUFFIX)
1808-
&& clazz.info.member(nme.originalName(f.name)).isPublic
1808+
&& clazz.info.member(f.unexpandedName).isPublic
18091809
&& clazz.info.decl(f.name).suchThat(_.isGetter) != NoSymbol
18101810
)
18111811

src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,15 @@ trait MethodSynthesis {
471471
}
472472
}
473473
case class Setter(tree: ValDef) extends DerivedSetter {
474-
def name = nme.getterToSetter(tree.name)
474+
def name = tree.setterName
475475
def category = SetterTargetClass
476476
def flagsMask = SetterFlags
477477
def flagsExtra = ACCESSOR
478478

479479
override def derivedSym = basisSym.setter(enclClass)
480480
}
481481
case class Field(tree: ValDef) extends DerivedFromValDef {
482-
def name = nme.getterToLocal(tree.name)
482+
def name = tree.localName
483483
def category = FieldTargetClass
484484
def flagsMask = FieldFlags
485485
def flagsExtra = PrivateLocal

0 commit comments

Comments
 (0)