blob: 23a161756b91b98bc31f4f70b15106efef2ebb97 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package dotty.tools.dotc
package core
import Names._
import NameOps._
import StdNames._
/** Additional info associated with a name. At a minimum its kind and
* a way to turn it into a string.
*/
abstract class NameInfo extends util.DotClass {
def kind: NameInfo.Kind
def mkString(underlying: TermName): String
def map(f: SimpleTermName => SimpleTermName): NameInfo = this
}
object NameInfo {
type Kind = Int
val TermNameKind = 0
val QualifiedKind = 1
val DefaultGetterKind = 3
val VariantKind = 4
val SuperAccessorKind = 5
val InitializerKind = 6
val ModuleClassKind = 10
val qualifier: Map[String, SimpleTermName => Qualified] =
Map("." -> Select,
"$" -> Flatten,
str.EXPAND_SEPARATOR -> Expand,
str.TRAIT_SETTER_SEPARATOR -> TraitSetter)
def definesNewName(kind: Kind) = kind <= QualifiedKind
/** TermNames have the lowest possible kind */
val TermName = new NameInfo {
def kind = TermNameKind
def mkString(underlying: TermName) = underlying.toString // will cause an unsupported exception
}
trait Qualified extends NameInfo {
def name: SimpleTermName
def separator: String
def newLikeThis(name: SimpleTermName): Qualified // TODO: should use copy instead after bootstrap
def kind = QualifiedKind
override def map(f: SimpleTermName => SimpleTermName): NameInfo = newLikeThis(f(name))
def mkString(underlying: TermName) = s"$underlying$separator$name"
override def toString = s"${getClass.getSimpleName}($name)"
}
case class Select(val name: SimpleTermName) extends Qualified {
def separator = "."
def newLikeThis(name: SimpleTermName) = Select(name)
}
case class Flatten(val name: SimpleTermName) extends Qualified {
def separator = "$"
def newLikeThis(name: SimpleTermName) = Flatten(name)
}
case class Expand(val name: SimpleTermName) extends Qualified {
def separator = str.EXPAND_SEPARATOR
def newLikeThis(name: SimpleTermName) = Expand(name)
}
case class TraitSetter(val name: SimpleTermName) extends Qualified {
def separator = nme.TRAIT_SETTER_SEPARATOR.toString
def newLikeThis(name: SimpleTermName) = TraitSetter(name)
}
trait Numbered extends NameInfo {
def num: Int
override def toString = s"${getClass.getSimpleName}($num)"
}
case class DefaultGetter(val num: Int) extends Numbered {
def kind = DefaultGetterKind
def mkString(underlying: TermName) = {
val prefix = if (underlying.isConstructorName) nme.DEFAULT_GETTER_INIT else underlying
prefix.toString + nme.DEFAULT_GETTER + (num + 1)
}
}
case class Variant(val num: Int) extends Numbered {
def kind = VariantKind
def mkString(underlying: TermName) = varianceToPrefix(num).toString + underlying
}
val SuperAccessor = new NameInfo {
def kind = SuperAccessorKind
def mkString(underlying: TermName) =
underlying.mapLast(n => (nme.SUPER_PREFIX ++ n).asSimpleName).toString
override def toString = "SuperAccessor"
}
val Initializer = new NameInfo {
def kind = InitializerKind
def mkString(underlying: TermName) =
underlying.mapLast(n => (nme.INITIALIZER_PREFIX ++ n).asSimpleName).toString
override def toString = "Initializer"
}
val ModuleClass = new NameInfo {
def kind = ModuleClassKind
def mkString(underlying: TermName) = underlying + "$"
override def toString = "ModuleClass"
}
/** Map between variances and name prefixes */
val varianceToPrefix = Map(-1 -> '-', 0 -> '=', 1 -> '+')
val prefixToVariance = Map('-' -> -1, '=' -> 0, '+' -> 1)
}
|