@@ -52,24 +52,32 @@ object NameOps {
52
52
implicit class NameDecorator [N <: Name ](val name : N ) extends AnyVal {
53
53
import nme ._
54
54
55
+ def testSimple (f : SimpleTermName => Boolean ): Boolean = name match {
56
+ case name : SimpleTermName => f(name)
57
+ case name : TypeName => name.toTermName.testSimple(f)
58
+ case _ => false
59
+ }
60
+
55
61
def likeTyped (n : PreName ): N =
56
62
(if (name.isTermName) n.toTermName else n.toTypeName).asInstanceOf [N ]
57
63
58
64
def isConstructorName = name == CONSTRUCTOR || name == TRAIT_CONSTRUCTOR
59
65
def isStaticConstructorName = name == STATIC_CONSTRUCTOR
60
66
def isLocalDummyName = name startsWith LOCALDUMMY_PREFIX
61
- def isReplWrapperName = name.toSimpleName containsSlice INTERPRETER_IMPORT_WRAPPER
67
+ def isReplWrapperName = name.toString contains str. INTERPRETER_IMPORT_WRAPPER
62
68
def isSetterName = name endsWith SETTER_SUFFIX
63
- def isScala2LocalSuffix = name .endsWith(" " )
64
- def isSelectorName = name .startsWith(" _" ) && name.tail .forall(_.isDigit)
69
+ def isScala2LocalSuffix = testSimple(_ .endsWith(" " ) )
70
+ def isSelectorName = testSimple(n => n .startsWith(" _" ) && n.drop( 1 ) .forall(_.isDigit) )
65
71
66
72
/** Is name a variable name? */
67
- def isVariableName : Boolean = name.length > 0 && {
68
- val first = name.head
69
- (((first.isLower && first.isLetter) || first == '_' )
70
- && (name != false_)
71
- && (name != true_)
72
- && (name != null_))
73
+ def isVariableName : Boolean = testSimple { n =>
74
+ n.length > 0 && {
75
+ val first = n.head
76
+ (((first.isLower && first.isLetter) || first == '_' )
77
+ && (n != false_)
78
+ && (n != true_)
79
+ && (n != null_))
80
+ }
73
81
}
74
82
75
83
def isOpAssignmentName : Boolean = name match {
@@ -91,11 +99,10 @@ object NameOps {
91
99
* method needs to work on mangled as well as unmangled names because
92
100
* it is also called from the backend.
93
101
*/
94
- def stripModuleClassSuffix : Name = name.toTermName match {
95
- case n : SimpleTermName if n.endsWith(" $" ) =>
96
- name.unmangleClassName.exclude(ModuleClassName )
97
- case _ =>
98
- name.exclude(ModuleClassName )
102
+ def stripModuleClassSuffix : N = likeTyped {
103
+ val name1 =
104
+ if (name.isSimple && name.endsWith(" $" )) name.unmangleClassName else name
105
+ name.exclude(ModuleClassName )
99
106
}
100
107
101
108
/** If flags is a ModuleClass but not a Package, add module class suffix */
@@ -159,56 +166,13 @@ object NameOps {
159
166
}
160
167
}
161
168
162
- def unmangleClassName : N =
163
- if (name.isSimple && name.isTypeName)
164
- if (name.endsWith(MODULE_SUFFIX ) && ! tpnme.falseModuleClassNames.contains(name.asTypeName))
165
- likeTyped(name.dropRight(MODULE_SUFFIX .length).moduleClassName)
166
- else name
167
- else name
168
-
169
- /** Translate a name into a list of simple TypeNames and TermNames.
170
- * In all segments before the last, type/term is determined by whether
171
- * the following separator char is '.' or '#'. The last segment
172
- * is of the same type as the original name.
173
- *
174
- * Examples:
175
- *
176
- * package foo {
177
- * object Lorax { object Wog ; class Wog }
178
- * class Lorax { object Zax ; class Zax }
179
- * }
180
- *
181
- * f("foo.Lorax".toTermName) == List("foo": Term, "Lorax": Term) // object Lorax
182
- * f("foo.Lorax".toTypeName) == List("foo": Term, "Lorax": Type) // class Lorax
183
- * f("Lorax.Wog".toTermName) == List("Lorax": Term, "Wog": Term) // object Wog
184
- * f("Lorax.Wog".toTypeName) == List("Lorax": Term, "Wog": Type) // class Wog
185
- * f("Lorax#Zax".toTermName) == List("Lorax": Type, "Zax": Term) // object Zax
186
- * f("Lorax#Zax".toTypeName) == List("Lorax": Type, "Zax": Type) // class Zax
187
- *
188
- * Note that in actual scala syntax you cannot refer to object Zax without an
189
- * instance of Lorax, so Lorax#Zax could only mean the type. One might think
190
- * that Lorax#Zax.type would work, but this is not accepted by the parser.
191
- * For the purposes of referencing that object, the syntax is allowed.
192
- */
193
- def segments : List [Name ] = {
194
- def mkName (name : Name , follow : Char ): Name =
195
- if (follow == '.' ) name.toTermName else name.toTypeName
196
-
197
- name.indexWhere(ch => ch == '.' || ch == '#' ) match {
198
- case - 1 =>
199
- if (name.isEmpty) scala.Nil else name :: scala.Nil
200
- case idx =>
201
- mkName(name take idx, name(idx)) :: (name drop (idx + 1 )).segments
202
- }
203
- }
204
-
205
169
/** Is a synthetic function name
206
170
* - N for FunctionN
207
171
* - N for ImplicitFunctionN
208
172
* - (-1) otherwise
209
173
*/
210
174
def functionArity : Int =
211
- functionArityFor(tpnme .Function ) max functionArityFor(tpnme .ImplicitFunction )
175
+ functionArityFor(str .Function ) max functionArityFor(str .ImplicitFunction )
212
176
213
177
/** Is a function name
214
178
* - FunctionN for N >= 0
@@ -221,20 +185,20 @@ object NameOps {
221
185
* - ImplicitFunctionN for N >= 0
222
186
* - false otherwise
223
187
*/
224
- def isImplicitFunction : Boolean = functionArityFor(tpnme .ImplicitFunction ) >= 0
188
+ def isImplicitFunction : Boolean = functionArityFor(str .ImplicitFunction ) >= 0
225
189
226
190
/** Is a synthetic function name
227
191
* - FunctionN for N > 22
228
192
* - ImplicitFunctionN for N >= 0
229
193
* - false otherwise
230
194
*/
231
195
def isSyntheticFunction : Boolean = {
232
- functionArityFor(tpnme .Function ) > MaxImplementedFunctionArity ||
233
- functionArityFor(tpnme .ImplicitFunction ) >= 0
196
+ functionArityFor(str .Function ) > MaxImplementedFunctionArity ||
197
+ functionArityFor(str .ImplicitFunction ) >= 0
234
198
}
235
199
236
200
/** Parsed function arity for function with some specific prefix */
237
- private def functionArityFor (prefix : Name ): Int = {
201
+ private def functionArityFor (prefix : String ): Int = {
238
202
if (name.startsWith(prefix))
239
203
try name.toString.substring(prefix.length).toInt
240
204
catch { case _ : NumberFormatException => - 1 }
@@ -285,6 +249,13 @@ object NameOps {
285
249
/** If name length exceeds allowable limit, replace part of it by hash */
286
250
def compactified (implicit ctx : Context ): TermName = termName(compactify(name.toString))
287
251
252
+ def unmangleClassName : N = name.toTermName match {
253
+ case name : SimpleTermName
254
+ if name.endsWith(str.MODULE_SUFFIX ) && ! nme.falseModuleClassNames.contains(name) =>
255
+ likeTyped(name.dropRight(str.MODULE_SUFFIX .length).moduleClassName)
256
+ case _ => name
257
+ }
258
+
288
259
def unmangle (kind : NameKind ): N = likeTyped {
289
260
name rewrite {
290
261
case unmangled : SimpleTermName =>
@@ -306,11 +277,11 @@ object NameOps {
306
277
implicit class TermNameDecorator (val name : TermName ) extends AnyVal {
307
278
import nme ._
308
279
309
- def setterName : TermName = name.exclude(FieldName ) ++ SETTER_SUFFIX
280
+ def setterName : TermName = name.exclude(FieldName ) ++ str. SETTER_SUFFIX
310
281
311
282
def getterName : TermName =
312
283
name.exclude(FieldName ).mapLast(n =>
313
- if (n.endsWith(SETTER_SUFFIX )) n.take(n.length - SETTER_SUFFIX .length).asSimpleName
284
+ if (n.endsWith(SETTER_SUFFIX )) n.take(n.length - str. SETTER_SUFFIX .length).asSimpleName
314
285
else n)
315
286
316
287
def fieldName : TermName =
@@ -324,7 +295,7 @@ object NameOps {
324
295
else FieldName (name)
325
296
326
297
def stripScala2LocalSuffix : TermName =
327
- if (name.isScala2LocalSuffix) name.init.asTermName else name
298
+ if (name.isScala2LocalSuffix) name.asSimpleName.dropRight( 1 ) else name
328
299
329
300
/** The name unary_x for a prefix operator x */
330
301
def toUnaryName : TermName = name match {
0 commit comments