Skip to content

Commit dd3f0db

Browse files
committed
Polishings
1 parent a51a2cc commit dd3f0db

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ abstract class NameInfo extends util.DotClass {
1010
def kind: NameInfo.Kind
1111
def mkString(underlying: TermName): String
1212
def map(f: SimpleTermName => SimpleTermName): NameInfo = this
13-
def satisfies(p: SimpleTermName => Boolean): Boolean = false
14-
def ++(other: String): NameInfo = unsupported("++")
1513
}
1614

1715
object NameInfo {
@@ -34,8 +32,6 @@ object NameInfo {
3432
def kind = QualifiedKind
3533
def mkString(underlying: TermName) = s"$underlying$separator$name"
3634
override def map(f: SimpleTermName => SimpleTermName): NameInfo = Qualified(f(name), separator)
37-
override def satisfies(p: SimpleTermName => Boolean): Boolean = p(name)
38-
override def ++(other: String): NameInfo = Qualified(name ++ other, separator)
3935
override def toString = s"Qualified($name, $separator)"
4036
}
4137

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ object NameOps {
132132
/** If name ends in module class suffix, drop it */
133133
def stripModuleClassSuffix: Name =
134134
if (isModuleClassName)
135-
if (Config.semanticNames) name.without(NameInfo.ModuleClass.kind)
135+
if (Config.semanticNames) name.exclude(NameInfo.ModuleClass.kind)
136136
else name dropRight MODULE_SUFFIX.length
137137
else name
138138

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

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ object Names {
7171
def likeKinded(name: Name): ThisName
7272

7373
def derived(info: NameInfo): ThisName
74-
def without(kind: NameInfo.Kind): ThisName
74+
def select(name: SimpleTermName, sep: String) = derived(NameInfo.Qualified(name, sep))
75+
def exclude(kind: NameInfo.Kind): ThisName
7576
def is(kind: NameInfo.Kind): Boolean
7677
def debugString: String
7778

@@ -83,19 +84,22 @@ object Names {
8384
/** Replace operator symbols by corresponding \$op_name's. */
8485
def encode: Name
8586

87+
def firstPart: TermName
88+
def lastPart: TermName
89+
8690
/** A more efficient version of concatenation */
8791
def ++ (other: Name): ThisName = ++ (other.toString)
8892
def ++ (other: String): ThisName
8993

9094
def replace(from: Char, to: Char): ThisName = likeKinded(asSimpleName.replace(from, to))
9195

9296
def isEmpty: Boolean
93-
def startsWith(str: String): Boolean
97+
98+
def startsWith(str: String): Boolean = firstPart.startsWith(str)
9499
def startsWith(name: Name): Boolean = startsWith(name.toString)
95-
def endsWith(str: String): Boolean
100+
def endsWith(str: String): Boolean = lastPart.endsWith(str)
96101
def endsWith(name: Name): Boolean = endsWith(name.toString)
97102

98-
99103
override def equals(that: Any) = this eq that.asInstanceOf[AnyRef]
100104
}
101105

@@ -151,33 +155,31 @@ object Names {
151155
name
152156
}
153157

158+
private def add(info: NameInfo): TermName = synchronized {
159+
getDerived(info) match {
160+
case null => putDerived(info, new DerivedTermName(this, info))
161+
case derivedName => derivedName
162+
}
163+
}
164+
154165
/** Return derived name with given `info` and the current
155166
* name as underlying name.
156167
*/
157168
def derived(info: NameInfo): TermName = {
158-
def addIt() = synchronized {
159-
getDerived(info) match {
160-
case null => putDerived(info, new DerivedTermName(this, info))
161-
case derivedName => derivedName
162-
}
163-
}
164-
165169
val ownKind = this.info.kind
166-
if (NameInfo.definesNewName(info.kind)) addIt()
167-
else if (ownKind > info.kind)
168-
underlying.derived(info).derived(this.info)
169-
else if (ownKind == info.kind) {
170+
if (ownKind < info.kind || NameInfo.definesNewName(info.kind)) add(info)
171+
else if (ownKind > info.kind) underlying.derived(info).add(this.info)
172+
else {
170173
assert(info == this.info)
171174
this
172175
}
173-
else addIt()
174176
}
175177

176-
def without(kind: NameInfo.Kind): TermName = {
177-
val ownKind = info.kind
178+
def exclude(kind: NameInfo.Kind): TermName = {
179+
val ownKind = this.info.kind
178180
if (ownKind < kind || NameInfo.definesNewName(ownKind)) this
179-
else if (ownKind == kind) underlying.without(kind)
180-
else underlying.without(kind).derived(this.info)
181+
else if (ownKind > kind) underlying.exclude(kind).add(this.info)
182+
else underlying
181183
}
182184

183185
def is(kind: NameInfo.Kind): Boolean = {
@@ -205,13 +207,13 @@ object Names {
205207

206208
def isEmpty = length == 0
207209

208-
def startsWith(str: String): Boolean = {
210+
override def startsWith(str: String): Boolean = {
209211
var i = 0
210212
while (i < str.length && i < length && apply(i) == str(i)) i += 1
211213
i == str.length
212214
}
213215

214-
def endsWith(str: String): Boolean = {
216+
override def endsWith(str: String): Boolean = {
215217
var i = 1
216218
while (i <= str.length && i <= length && apply(length - i) == str(str.length - i)) i += 1
217219
i > str.length
@@ -238,6 +240,9 @@ object Names {
238240
def decode: SimpleTermName =
239241
if (contains('$')) termName(NameTransformer.decode(toString)) else this
240242

243+
def firstPart = this
244+
def lastPart = this
245+
241246
override def hashCode: Int = start
242247

243248
override def toString =
@@ -251,11 +256,11 @@ object Names {
251256
def ++ (other: String): ThisName = toTermName.++(other).toTypeName
252257

253258
def isEmpty = toTermName.isEmpty
254-
def startsWith(str: String): Boolean = toTermName.startsWith(str)
255-
def endsWith(str: String): Boolean = toTermName.endsWith(str)
256259

257-
def encode: Name = toTermName.encode.toTypeName
258-
def decode: Name = toTermName.decode.toTypeName
260+
def encode = toTermName.encode.toTypeName
261+
def decode = toTermName.decode.toTypeName
262+
def firstPart = toTermName.firstPart
263+
def lastPart = toTermName.lastPart
259264

260265
type ThisName = TypeName
261266
def isTypeName = true
@@ -272,7 +277,7 @@ object Names {
272277
def likeKinded(name: Name): TypeName = name.toTypeName
273278

274279
def derived(info: NameInfo): TypeName = toTermName.derived(info).toTypeName
275-
def without(kind: NameInfo.Kind): TypeName = toTermName.without(kind).toTypeName
280+
def exclude(kind: NameInfo.Kind): TypeName = toTermName.exclude(kind).toTypeName
276281
def is(kind: NameInfo.Kind) = toTermName.is(kind)
277282

278283
override def toString = toTermName.toString
@@ -284,12 +289,21 @@ object Names {
284289
*/
285290
case class DerivedTermName(override val underlying: TermName, override val info: NameInfo)
286291
extends TermName {
287-
def ++ (other: String): ThisName = derived(info ++ other)
288292
def isEmpty = false
289-
def startsWith(str: String): Boolean = underlying.startsWith(str)
290-
def endsWith(str: String): Boolean = info.satisfies(_.endsWith(str))
291293
def encode: Name = underlying.encode.derived(info.map(_.encode))
292294
def decode: Name = underlying.decode.derived(info.map(_.decode))
295+
def firstPart = info match {
296+
case NameInfo.Qualified(name, _) => name
297+
case _ => underlying.firstPart
298+
}
299+
def lastPart = info match {
300+
case NameInfo.Qualified(name, _) => name
301+
case _ => underlying.lastPart
302+
}
303+
def ++ (other: String): ThisName = info match {
304+
case NameInfo.Qualified(name, sep) => underlying.select(name ++ other, sep)
305+
case _ => (underlying ++ other).derived(info)
306+
}
293307
override def toString = info.mkString(underlying)
294308
override def debugString = s"${underlying.debugString}[$info]"
295309

@@ -455,7 +469,6 @@ object Names {
455469

456470
def seq: WrappedString = new WrappedString(name.toString)
457471
override protected[this] def thisCollection: WrappedString = seq
458-
def endsWith(name: Name): Boolean = endsWith(name.toString)
459472
def indexOfSlice(name: Name): Int = indexOfSlice(name.toString)
460473
def lastIndexOfSlice(name: Name): Int = lastIndexOfSlice(name.toString)
461474
def containsSlice(name: Name): Boolean = containsSlice(name.toString)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ object SymDenotations {
409409
if (Config.semanticNames) {
410410
if (sep == "$")
411411
// duplicate scalac's behavior: don't write a double '$$' for module class members.
412-
prefix = prefix.without(NameInfo.ModuleClassKind)
413-
name.mapSimpleCore(sn => prefix.derived(NameInfo.Qualified(sn, sep)))
412+
prefix = prefix.exclude(NameInfo.ModuleClassKind)
413+
name.mapSimpleCore(prefix.select(_, sep))
414414
}
415415
else {
416416
if (owner.is(ModuleClass, butNot = Package) && sep == "$")

0 commit comments

Comments
 (0)