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