forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SymbolOps.scala
249 lines (225 loc) · 10.2 KB
/
SymbolOps.scala
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.tools.nsc.tasty.bridge
import scala.annotation.tailrec
import scala.tools.nsc.tasty.{SafeEq, TastyUniverse, ForceKinds, TastyModes}, TastyModes._, ForceKinds._
import scala.tools.tasty.{TastyName, Signature, TastyFlags}, TastyName.SignedName, Signature.MethodSignature, TastyFlags._
import scala.tools.tasty.ErasedTypeRef
/**This layer deals with selecting a member symbol from a type using a `TastyName`,
* also contains factories for making type references to symbols.
*/
trait SymbolOps { self: TastyUniverse =>
import self.{symbolTable => u}
@inline final def noSymbol: Symbol = u.NoSymbol
@inline final def isSymbol(sym: Symbol): Boolean = sym ne u.NoSymbol
final def allowsOverload(sym: Symbol) = ( // TODO [tasty]: taken from Namer. Added module symbols
(sym.isSourceMethod || sym.isModule) && sym.owner.isClass && !sym.isTopLevel
)
final def declaringSymbolOf(sym: Symbol): Symbol =
if (sym.isModuleClass) sym.sourceModule else sym
private final def deepComplete(space: Type)(implicit ctx: Context): Unit = {
symOfType(space) match {
case u.NoSymbol =>
ctx.log(s"could not retrieve symbol from type ${showType(space)}")
case termSym if termSym.isTerm =>
if (termSym.is(Object)) {
termSym.ensureCompleted(SpaceForce)
termSym.moduleClass.ensureCompleted(DeepForce | SpaceForce)
}
else {
ctx.log(s"deep complete on non-module term ${showSym(termSym)}, not taking action")
}
case typeSym =>
typeSym.ensureCompleted(SpaceForce)
}
}
/** Fetch the symbol of a path type without forcing the symbol,
* `NoSymbol` if not a path.
*/
@tailrec
private[bridge] final def symOfType(tpe: Type): Symbol = tpe match {
case tpe: u.TypeRef => tpe.sym
case tpe: u.SingleType => tpe.sym
case tpe: u.ThisType => tpe.sym
case tpe: u.ConstantType => symOfType(tpe.value.tpe)
case tpe: u.ClassInfoType => tpe.typeSymbol
case tpe: u.RefinedType0 => tpe.typeSymbol
case tpe: u.ExistentialType => symOfType(tpe.underlying)
case _ => u.NoSymbol
}
implicit final class SymbolDecorator(val sym: Symbol) {
def isScala3Inline: Boolean = repr.tflags.is(Inline)
def isScala2Macro: Boolean = repr.tflags.is(FlagSets.Scala2Macro)
def isTraitParamAccessor: Boolean = sym.owner.isTrait && repr.tflags.is(FieldAccessor|ParamSetter)
def isParamGetter: Boolean =
sym.isMethod && sym.repr.tflags.is(FlagSets.ParamGetter)
/** A computed property that should only be called on a symbol which is known to have been initialised by the
* Tasty Unpickler and is not yet completed.
*
* @todo adapt callsites and type so that this property is more safe to call (barring mutation from uncontrolled code)
*/
def repr: TastyRepr = {
try sym.rawInfo.asInstanceOf[TastyRepr]
catch {
case err: ClassCastException =>
val raw = u.showRaw(sym.rawInfo)
val tastyRepr = u.typeOf[TastyRepr]
throw new AssertionError(s"$sym is already completed. Expected $tastyRepr, is $raw.")
}
}
def ensureCompleted(forceKinds: ForceKinds)(implicit ctx: Context): Unit = {
val raw = sym.rawInfo
if (raw.isInstanceOf[u.LazyType]) {
ctx.trace(traceForceInfo(sym, forceKinds)) {
sym.info
sym.annotations.foreach(_.completeInfo())
}
} else {
assert(!raw.isInstanceOf[TastyRepr], s"${showSym(sym)} has incorrectly initialised info $raw")
}
}
private def traceForceInfo(
sym: Symbol,
forceKinds: ForceKinds
)(implicit ctx: Context) = TraceInfo[Unit](
query = "force symbol info",
qual = s"${showSym(sym)} in context ${showSym(ctx.owner)}",
res = _ => s"${showSym(sym)} was forced",
modifiers = forceKinds.describe
)
def objectImplementation: Symbol = sym.moduleClass
def sourceObject: Symbol = sym.sourceModule
def ref: Type = u.appliedType(sym, Nil)
def safeOwner: Symbol = if (sym.owner eq sym) sym else sym.owner
}
/** Is this symbol annotated with `scala.annotation.experimental`? */
def symIsExperimental(sym: Symbol) = sym.hasAnnotation(defn.ExperimentalAnnotationClass)
/** if isConstructor, make sure it has one non-implicit parameter list */
def normalizeIfConstructor(termParamss: List[List[Symbol]], isConstructor: Boolean): List[List[Symbol]] =
if (isConstructor &&
(termParamss.isEmpty || termParamss.head.nonEmpty && termParamss.head.head.isImplicit))
Nil :: termParamss
else
termParamss
private[bridge] def lookupSymbol(space: Type, tname: TastyName)(implicit ctx: Context): Symbol = {
deepComplete(space)
tname match {
case SignedName(qual, sig, target) => lookupSigned(space, qual, sig.map(_.encode), target)
case _ => lookupSimple(space, tname)
}
}
private def lookupSimple(space: Type, tname: TastyName)(implicit ctx: Context): Symbol = {
// TODO [tasty]: dotty uses accessibleDenot which asserts that `fetched.isAccessibleFrom(pre)`,
// or else filters for non private.
// There should be an investigation to see what code makes that false, and what is an equivalent check.
val member = {
if (tname.isTypeName) {
val asTerm = tname.toTermName
if (asTerm.isObjectName) space.member(encodeTermName(asTerm)).moduleClass
else {
val selector = encodeTastyName(tname)
def lookInTypeCtor =
space.typeConstructor.typeParams.filter(selector === _.name).headOption.getOrElse(noSymbol)
space.member(selector).orElse(lookInTypeCtor)
}
}
else {
val firstTry = space.member(encodeTermName(tname))
if (firstTry.isOverloaded) firstTry.filter(!_.isPrivateLocal)
else firstTry
}
}
if (isSymbol(member) && hasType(member)) member
else errorMissing(space, tname)
}
private def hasType(member: Symbol)(implicit ctx: Context) = {
ctx.mode.is(ReadAnnotation) || ctx.mode.is(ReadMacro) && (member.info `ne` u.NoType) || (member.rawInfo `ne` u.NoType)
}
private def errorMissing[T](space: Type, tname: TastyName)(implicit ctx: Context) = {
val kind = if (tname.isTypeName) "type" else "term"
def typeToString(tpe: Type) = {
def inner(sb: StringBuilder, tpe: Type): StringBuilder = tpe match {
case u.ThisType(cls) => sb append cls.fullNameString
case u.SingleType(pre, sym) =>
if ((pre eq u.NoPrefix) || (pre eq u.NoType)) sb append sym.nameString
else inner(sb, pre) append '.' append sym.nameString
case u.TypeRef(pre, sym, _) =>
if ((pre eq u.NoPrefix) || (pre eq u.NoType)) sb append sym.nameString
else inner(sb, pre) append '.' append sym.nameString
case tpe => sb append tpe
}
inner(new StringBuilder(), tpe).toString
}
def addendum(name: String) = {
if (ctx.mode.is(ReadParents)) s"$kind in parents of ${location(if (ctx.owner.isLocalDummy) ctx.owner.owner else ctx.owner)}: $name"
else s"$kind required by ${location(ctx.owner)}: $name"
}
val missing = addendum(s"${typeToString(space)}.$tname")
typeError(s"can't find $missing; perhaps it is missing from the classpath.")
}
private def lookupSigned(
space: Type,
qual: TastyName,
sig: MethodSignature[ErasedTypeRef],
target: TastyName
)(implicit ctx: Context): Symbol = {
if (target ne qual) {
unsupportedError(s"selection of method $qual with @targetName(" + '"' + target + '"' + ")")
}
else {
ctx.trace(traceOverload(space, qual, sig)) {
val member = space.member(encodeTermName(qual))
if (!(isSymbol(member) && hasType(member))) errorMissing(space, qual)
val (tyParamCount, paramRefs) = {
val (tyParamCounts, params) = sig.params.partitionMap(identity)
if (tyParamCounts.length > 1) {
unsupportedError(s"method with unmergeable type parameters: $qual")
}
(tyParamCounts.headOption.getOrElse(0), params)
}
def compareSym(sym: Symbol): Boolean = sym match {
case sym: u.MethodSymbol =>
sym.ensureCompleted(OverloadedSym)
// TODO [tasty]: we should cache signatures for symbols and compare against `sig`
val meth0 = u.unwrapWrapperTypes(sym.tpe.asSeenFrom(space, sym.owner))
val paramSyms = meth0.paramss.flatten
val resTpe = meth0.finalResultType
val sameParamSize = paramSyms.length === paramRefs.length
def sameTyParamSize = tyParamCount === ({
// the signature of a class/mixin constructor includes
// type parameters, in nsc these come from the parent.
val tyParamOwner = if (qual.isConstructorName) member.owner else sym
tyParamOwner.typeParams.length
})
def sameParams = paramSyms.lazyZip(paramRefs).forall({
case (paramSym, paramRef) => sameErasure(sym)(paramSym.tpe, paramRef)
})
sameParamSize && sameTyParamSize && sameParams && sameErasure(sym)(resTpe, sig.result)
case _ =>
ctx.log(s"""! member[$space]("$qual") ${showSym(sym)} is not a method""")
false
}
member.asTerm.alternatives.find(compareSym).getOrElse(
typeError(s"No matching overload of $space.$qual with signature ${showSig(sig)}")
)
}
}
}
private def traceOverload(space: Type, tname: TastyName, sig: MethodSignature[ErasedTypeRef]) = TraceInfo[Symbol](
query = s"looking for overload",
qual = s"symbolOf[$space] @@ $tname: ${showSig(sig)}",
res = overload => s"selected overload ${showSym(overload)}"
)
def showSig(sig: MethodSignature[ErasedTypeRef]): String = sig.map(_.signature).show
def showSym(sym: Symbol): String = s"`(#${sym.id}) ${sym.accurateKindString} ${sym.name}`"
def showSymStable(sym: Symbol): String = s"#[${sym.id}, ${sym.name}]"
}