@@ -16,6 +16,7 @@ object NameKinds {
16
16
17
17
@ sharable private val simpleNameKinds = new mutable.HashMap [Int , ClassifiedNameKind ]
18
18
@ sharable private val qualifiedNameKinds = new mutable.HashMap [Int , QualifiedNameKind ]
19
+ @ sharable private val numberedNameKinds = new mutable.HashMap [Int , NumberedNameKind ]
19
20
@ sharable private val uniqueNameKinds = new mutable.HashMap [String , UniqueNameKind ]
20
21
21
22
/** A class for the info stored in a derived name */
@@ -25,39 +26,65 @@ object NameKinds {
25
26
def map (f : SimpleName => SimpleName ): NameInfo = this
26
27
}
27
28
28
- /** The kind of a derived name info */
29
+ /** An abstract base class of classes that define the kind of a derived name info */
29
30
abstract class NameKind (val tag : Int ) extends DotClass { self =>
31
+
32
+ /** The info class defined by this kind */
30
33
type ThisInfo <: Info
34
+
35
+ /** A simple info type; some subclasses of Kind define more refined versions */
31
36
class Info extends NameInfo { this : ThisInfo =>
32
37
def kind = self
33
38
def mkString (underlying : TermName ) = self.mkString(underlying, this )
34
39
override def toString = infoString
35
40
}
41
+
42
+ /** Does this kind define logically a new name? Tested by the `rewrite` and `collect`
43
+ * combinators of names.
44
+ */
36
45
def definesNewName = false
46
+
47
+ /** Unmangle simple name `name` into a name of this kind, or return
48
+ * original name if this is not possible.
49
+ */
37
50
def unmangle (name : SimpleName ): TermName = name
51
+
52
+ /** Turn a name of this kind consisting of an `underlying` prefix
53
+ * and the given `info` into a string.
54
+ */
38
55
def mkString (underlying : TermName , info : ThisInfo ): String
56
+
57
+ /** A string used for displaying the structure of a name */
39
58
def infoString : String
40
59
}
41
60
61
+ /** The kind of SimpleNames */
42
62
object SimpleNameKind extends NameKind (UTF8 ) { self =>
43
63
type ThisInfo = Info
44
64
val info = new Info
45
65
def mkString (underlying : TermName , info : ThisInfo ) = unsupported(" mkString" )
46
66
def infoString = unsupported(" infoString" )
47
67
}
48
68
69
+ /** The kind of names that add a simple classification to an underlying name.
70
+ */
49
71
abstract class ClassifiedNameKind (tag : Int , val infoString : String ) extends NameKind (tag) {
50
72
type ThisInfo = Info
51
73
val info = new Info
52
- def apply (qual : TermName ) =
53
- qual.derived(info)
74
+
75
+ /** Build a new name of this kind from an underlying name */
76
+ def apply (underlying : TermName ) = underlying.derived(info)
77
+
78
+ /** Extractor operation for names of this kind */
54
79
def unapply (name : DerivedName ): Option [TermName ] = name match {
55
80
case DerivedName (underlying, `info`) => Some (underlying)
56
81
case _ => None
57
82
}
83
+
58
84
simpleNameKinds(tag) = this
59
85
}
60
86
87
+ /** The kind of names that get formed by adding a prefix to an underlying name */
61
88
class PrefixNameKind (tag : Int , prefix : String , optInfoString : String = " " )
62
89
extends ClassifiedNameKind (tag, if (optInfoString.isEmpty) s " Prefix $prefix" else optInfoString) {
63
90
def mkString (underlying : TermName , info : ThisInfo ) =
@@ -67,6 +94,7 @@ object NameKinds {
67
94
else name
68
95
}
69
96
97
+ /** The kind of names that get formed by appending a suffix to an underlying name */
70
98
class SuffixNameKind (tag : Int , suffix : String , optInfoString : String = " " )
71
99
extends ClassifiedNameKind (tag, if (optInfoString.isEmpty) s " Suffix $suffix" else optInfoString) {
72
100
def mkString (underlying : TermName , info : ThisInfo ) = underlying.toString ++ suffix
@@ -75,17 +103,24 @@ object NameKinds {
75
103
else name
76
104
}
77
105
106
+ /** A base trait for infos that define an additional selector name */
78
107
trait QualifiedInfo extends NameInfo {
79
108
val name : SimpleName
80
109
}
81
110
111
+ /** The kind of qualified names, consisting of an underlying name as a prefix,
112
+ * followed by a separator, followed by a simple selector name.
113
+ *
114
+ * A qualified names always constitutes a new name, different from its underlying name.
115
+ */
82
116
class QualifiedNameKind (tag : Int , val separator : String )
83
117
extends NameKind (tag) {
84
118
type ThisInfo = QualInfo
85
119
case class QualInfo (val name : SimpleName ) extends Info with QualifiedInfo {
86
120
override def map (f : SimpleName => SimpleName ): NameInfo = new QualInfo (f(name))
87
121
override def toString = s " $infoString $name"
88
122
}
123
+
89
124
def apply (qual : TermName , name : SimpleName ): TermName =
90
125
qual.derived(new QualInfo (name))
91
126
@@ -107,11 +142,13 @@ object NameKinds {
107
142
108
143
def mkString (underlying : TermName , info : ThisInfo ) =
109
144
s " $underlying$separator${info.name}"
145
+
110
146
def infoString = s " Qualified $separator"
111
147
112
148
qualifiedNameKinds(tag) = this
113
149
}
114
150
151
+ /** An extractor for qualified names of an arbitrary kind */
115
152
object AnyQualifiedName {
116
153
def unapply (name : DerivedName ): Option [(TermName , SimpleName )] = name match {
117
154
case DerivedName (qual, info : QualifiedInfo ) =>
@@ -120,10 +157,12 @@ object NameKinds {
120
157
}
121
158
}
122
159
160
+ /** A base trait for infos that contain a number */
123
161
trait NumberedInfo extends NameInfo {
124
162
def num : Int
125
163
}
126
164
165
+ /** The kind of numbered names consisting of an underlying name and a number */
127
166
abstract class NumberedNameKind (tag : Int , val infoString : String ) extends NameKind (tag) { self =>
128
167
type ThisInfo = NumberedInfo
129
168
case class NumberedInfo (val num : Int ) extends Info with NameKinds .NumberedInfo {
@@ -142,29 +181,40 @@ object NameKinds {
142
181
name.slice(i - separator.length, i).toString == separator) i
143
182
else - 1
144
183
}
184
+
185
+ numberedNameKinds(tag) = this
145
186
}
146
187
188
+ /** An extractor for numbered names of arbitrary kind */
147
189
object AnyNumberedName {
148
190
def unapply (name : DerivedName ): Option [(TermName , Int )] = name match {
149
191
case DerivedName (qual, info : NumberedInfo ) => Some ((qual, info.num))
150
192
case _ => None
151
193
}
152
194
}
153
195
196
+ /** The kind of unique names that consist of an underlying name (can be empty),
197
+ * a separator indicating the class of unique name, and a unique number.
198
+ *
199
+ * A unique names always constitutes a new name, different from its underlying name.
200
+ */
154
201
case class UniqueNameKind (val separator : String )
155
202
extends NumberedNameKind (UNIQUE , s " Unique $separator" ) {
156
203
override def definesNewName = true
204
+
157
205
def mkString (underlying : TermName , info : ThisInfo ) = {
158
206
val safePrefix = str.sanitize(underlying.toString + separator)
159
207
safePrefix + info.num
160
208
}
161
209
210
+ /** Generate fresh unique name of this kind with given prefix name */
162
211
def fresh (prefix : TermName = EmptyTermName )(implicit ctx : Context ): TermName =
163
212
ctx.freshNames.newName(prefix, this )
164
213
165
214
uniqueNameKinds(separator) = this
166
215
}
167
216
217
+ /** An extractor for unique names of arbitrary kind */
168
218
object AnyUniqueName {
169
219
def unapply (name : DerivedName ): Option [(TermName , String , Int )] = name match {
170
220
case DerivedName (qual, info : NumberedInfo ) =>
@@ -176,10 +226,16 @@ object NameKinds {
176
226
}
177
227
}
178
228
179
- val QualifiedName = new QualifiedNameKind (QUALIFIED , " ." )
180
- val FlatName = new QualifiedNameKind (FLATTENED , " $" )
181
- val ExpandPrefixName = new QualifiedNameKind (EXPANDPREFIX , " $" )
229
+ /** Names of the form `prefix . name` */
230
+ val QualifiedName = new QualifiedNameKind (QUALIFIED , " ." )
231
+
232
+ /** Names of the form `prefix $ name` that are constructed as a result of flattening */
233
+ val FlatName = new QualifiedNameKind (FLATTENED , " $" )
234
+
235
+ /** Names of the form `prefix $ name` that are prefixes of expanded names */
236
+ val ExpandPrefixName = new QualifiedNameKind (EXPANDPREFIX , " $" )
182
237
238
+ /** Expanded names of the form `prefix $$ name`. */
183
239
val ExpandedName = new QualifiedNameKind (EXPANDED , str.EXPAND_SEPARATOR ) {
184
240
private val FalseSuper = termName(" $$super" )
185
241
private val FalseSuperLength = FalseSuper .length
@@ -199,13 +255,16 @@ object NameKinds {
199
255
}
200
256
}
201
257
202
- val TraitSetterName = new QualifiedNameKind (TRAITSETTER , str.TRAIT_SETTER_SEPARATOR )
258
+ /** Expanded names of the form `prefix $_setter_$ name`. These only occur in Scala2. */
259
+ val TraitSetterName = new QualifiedNameKind (TRAITSETTER , str.TRAIT_SETTER_SEPARATOR )
203
260
261
+ /** Unique names of the form `prefix $ n` or `$ n $` */
204
262
val UniqueName = new UniqueNameKind (" $" ) {
205
263
override def mkString (underlying : TermName , info : ThisInfo ) =
206
264
if (underlying.isEmpty) " $" + info.num + " $" else super .mkString(underlying, info)
207
265
}
208
266
267
+ /** Other unique names */
209
268
val InlineAccessorName = new UniqueNameKind (" $_inlineAccessor_$" )
210
269
val TempResultName = new UniqueNameKind (" ev$" )
211
270
val EvidenceParamName = new UniqueNameKind (" evidence$" )
@@ -222,6 +281,9 @@ object NameKinds {
222
281
val SkolemName = new UniqueNameKind (" ?" )
223
282
val LiftedTreeName = new UniqueNameKind (" liftedTree" )
224
283
284
+ /** A kind of unique extension methods; Unlike other unique names, these can be
285
+ * unmangled.
286
+ */
225
287
val UniqueExtMethName = new UniqueNameKind (" $extension" ) {
226
288
override def unmangle (name : SimpleName ): TermName = {
227
289
val i = skipSeparatorAndNum(name, separator)
@@ -234,6 +296,7 @@ object NameKinds {
234
296
}
235
297
}
236
298
299
+ /** Kinds of unique names generated by the pattern matcher */
237
300
val PatMatStdBinderName = new UniqueNameKind (" x" )
238
301
val PatMatPiName = new UniqueNameKind (" pi" ) // FIXME: explain what this is
239
302
val PatMatPName = new UniqueNameKind (" p" ) // FIXME: explain what this is
@@ -242,6 +305,7 @@ object NameKinds {
242
305
val PatMatMatchFailName = new UniqueNameKind (" matchFail" )
243
306
val PatMatSelectorName = new UniqueNameKind (" selector" )
244
307
308
+ /** The kind of names of default argument getters */
245
309
object DefaultGetterName extends NumberedNameKind (DEFAULTGETTER , " DefaultGetter" ) {
246
310
def mkString (underlying : TermName , info : ThisInfo ) = {
247
311
val prefix = if (underlying.isConstructorName) nme.DEFAULT_GETTER_INIT else underlying
@@ -260,11 +324,9 @@ object NameKinds {
260
324
}
261
325
}
262
326
327
+ /** The kind of names that also encode a variance: 0 for contravariance, 1 for covariance. */
263
328
object VariantName extends NumberedNameKind (VARIANT , " Variant" ) {
264
- val varianceToPrefix = Map (- 1 -> '-' , 0 -> '=' , 1 -> '+' )
265
- def mkString (underlying : TermName , info : ThisInfo ) = {
266
- varianceToPrefix(info.num).toString + underlying
267
- }
329
+ def mkString (underlying : TermName , info : ThisInfo ) = " -+" (info.num).toString + underlying
268
330
}
269
331
270
332
/** Names of the form N_<outer>. Emitted by inliner, replaced by outer path
@@ -289,9 +351,9 @@ object NameKinds {
289
351
val ModuleVarName = new SuffixNameKind (OBJECTVAR , " $module" )
290
352
val ModuleClassName = new SuffixNameKind (OBJECTCLASS , " $" , optInfoString = " ModuleClass" )
291
353
354
+ /** A name together with a signature. Used in Tasty trees. */
292
355
object SignedName extends NameKind (63 ) {
293
356
294
- /** @param parts resultSig followed by paramsSig */
295
357
case class SignedInfo (sig : Signature ) extends Info {
296
358
override def toString = s " $infoString $sig"
297
359
}
@@ -308,10 +370,12 @@ object NameKinds {
308
370
def infoString : String = " Signed"
309
371
}
310
372
373
+ /** Possible name kinds of a method that comes from Scala2 pickling info. */
311
374
val Scala2MethodNameKinds : List [NameKind ] =
312
375
List (DefaultGetterName , ExtMethName , UniqueExtMethName , ProtectedAccessorName , ProtectedSetterName )
313
376
314
377
def simpleNameKindOfTag : collection.Map [Int , ClassifiedNameKind ] = simpleNameKinds
315
378
def qualifiedNameKindOfTag : collection.Map [Int , QualifiedNameKind ] = qualifiedNameKinds
379
+ def numberedNameKindOfTag : collection.Map [Int , NumberedNameKind ] = numberedNameKinds
316
380
def uniqueNameKindOfSeparator : collection.Map [String , UniqueNameKind ] = uniqueNameKinds
317
381
}
0 commit comments