@@ -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 */
@@ -126,56 +133,13 @@ object NameOps {
126
133
}
127
134
}
128
135
129
- def unmangleClassName : N =
130
- if (name.isSimple && name.isTypeName)
131
- if (name.endsWith(MODULE_SUFFIX ) && ! tpnme.falseModuleClassNames.contains(name.asTypeName))
132
- likeTyped(name.dropRight(MODULE_SUFFIX .length).moduleClassName)
133
- else name
134
- else name
135
-
136
- /** Translate a name into a list of simple TypeNames and TermNames.
137
- * In all segments before the last, type/term is determined by whether
138
- * the following separator char is '.' or '#'. The last segment
139
- * is of the same type as the original name.
140
- *
141
- * Examples:
142
- *
143
- * package foo {
144
- * object Lorax { object Wog ; class Wog }
145
- * class Lorax { object Zax ; class Zax }
146
- * }
147
- *
148
- * f("foo.Lorax".toTermName) == List("foo": Term, "Lorax": Term) // object Lorax
149
- * f("foo.Lorax".toTypeName) == List("foo": Term, "Lorax": Type) // class Lorax
150
- * f("Lorax.Wog".toTermName) == List("Lorax": Term, "Wog": Term) // object Wog
151
- * f("Lorax.Wog".toTypeName) == List("Lorax": Term, "Wog": Type) // class Wog
152
- * f("Lorax#Zax".toTermName) == List("Lorax": Type, "Zax": Term) // object Zax
153
- * f("Lorax#Zax".toTypeName) == List("Lorax": Type, "Zax": Type) // class Zax
154
- *
155
- * Note that in actual scala syntax you cannot refer to object Zax without an
156
- * instance of Lorax, so Lorax#Zax could only mean the type. One might think
157
- * that Lorax#Zax.type would work, but this is not accepted by the parser.
158
- * For the purposes of referencing that object, the syntax is allowed.
159
- */
160
- def segments : List [Name ] = {
161
- def mkName (name : Name , follow : Char ): Name =
162
- if (follow == '.' ) name.toTermName else name.toTypeName
163
-
164
- name.indexWhere(ch => ch == '.' || ch == '#' ) match {
165
- case - 1 =>
166
- if (name.isEmpty) scala.Nil else name :: scala.Nil
167
- case idx =>
168
- mkName(name take idx, name(idx)) :: (name drop (idx + 1 )).segments
169
- }
170
- }
171
-
172
136
/** Is a synthetic function name
173
137
* - N for FunctionN
174
138
* - N for ImplicitFunctionN
175
139
* - (-1) otherwise
176
140
*/
177
141
def functionArity : Int =
178
- functionArityFor(tpnme .Function ) max functionArityFor(tpnme .ImplicitFunction )
142
+ functionArityFor(str .Function ) max functionArityFor(str .ImplicitFunction )
179
143
180
144
/** Is a function name
181
145
* - FunctionN for N >= 0
@@ -188,20 +152,20 @@ object NameOps {
188
152
* - ImplicitFunctionN for N >= 0
189
153
* - false otherwise
190
154
*/
191
- def isImplicitFunction : Boolean = functionArityFor(tpnme .ImplicitFunction ) >= 0
155
+ def isImplicitFunction : Boolean = functionArityFor(str .ImplicitFunction ) >= 0
192
156
193
157
/** Is a synthetic function name
194
158
* - FunctionN for N > 22
195
159
* - ImplicitFunctionN for N >= 0
196
160
* - false otherwise
197
161
*/
198
162
def isSyntheticFunction : Boolean = {
199
- functionArityFor(tpnme .Function ) > MaxImplementedFunctionArity ||
200
- functionArityFor(tpnme .ImplicitFunction ) >= 0
163
+ functionArityFor(str .Function ) > MaxImplementedFunctionArity ||
164
+ functionArityFor(str .ImplicitFunction ) >= 0
201
165
}
202
166
203
167
/** Parsed function arity for function with some specific prefix */
204
- private def functionArityFor (prefix : Name ): Int = {
168
+ private def functionArityFor (prefix : String ): Int = {
205
169
if (name.startsWith(prefix))
206
170
try name.toString.substring(prefix.length).toInt
207
171
catch { case _ : NumberFormatException => - 1 }
@@ -252,6 +216,13 @@ object NameOps {
252
216
/** If name length exceeds allowable limit, replace part of it by hash */
253
217
def compactified (implicit ctx : Context ): TermName = termName(compactify(name.toString))
254
218
219
+ def unmangleClassName : N = name.toTermName match {
220
+ case name : SimpleTermName
221
+ if name.endsWith(str.MODULE_SUFFIX ) && ! nme.falseModuleClassNames.contains(name) =>
222
+ likeTyped(name.dropRight(str.MODULE_SUFFIX .length).moduleClassName)
223
+ case _ => name
224
+ }
225
+
255
226
def unmangle (kind : NameKind ): N = likeTyped {
256
227
name rewrite {
257
228
case unmangled : SimpleTermName =>
@@ -273,11 +244,11 @@ object NameOps {
273
244
implicit class TermNameDecorator (val name : TermName ) extends AnyVal {
274
245
import nme ._
275
246
276
- def setterName : TermName = name.exclude(FieldName ) ++ SETTER_SUFFIX
247
+ def setterName : TermName = name.exclude(FieldName ) ++ str. SETTER_SUFFIX
277
248
278
249
def getterName : TermName =
279
250
name.exclude(FieldName ).mapLast(n =>
280
- if (n.endsWith(SETTER_SUFFIX )) n.take(n.length - SETTER_SUFFIX .length).asSimpleName
251
+ if (n.endsWith(SETTER_SUFFIX )) n.take(n.length - str. SETTER_SUFFIX .length).asSimpleName
281
252
else n)
282
253
283
254
def fieldName : TermName =
@@ -291,7 +262,7 @@ object NameOps {
291
262
else FieldName (name)
292
263
293
264
def stripScala2LocalSuffix : TermName =
294
- if (name.isScala2LocalSuffix) name.init.asTermName else name
265
+ if (name.isScala2LocalSuffix) name.asSimpleName.dropRight( 1 ) else name
295
266
296
267
/** The name unary_x for a prefix operator x */
297
268
def toUnaryName : TermName = name match {
0 commit comments