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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* NSC -- new scala compiler
* Copyright 2005 LAMP/EPFL
* @author Martin Odersky
*/
// $Id$
package scala.tools.nsc.typechecker;
import symtab.Flags._
import scala.tools.nsc.util.Position
trait Contexts requires Analyzer {
import global._
val NoContext = new Context {
override def implicitss: List[List[ImplicitInfo]] = List()
}
NoContext.enclClass = NoContext
NoContext.enclMethod = NoContext
private val startContext = NoContext.make(
Template(List(), List()) setSymbol NoSymbol setType NoType,
definitions.RootClass,
definitions.RootClass.info.decls)
def rootContext(unit: CompilationUnit): Context = rootContext(unit, EmptyTree, false)
def rootContext(unit: CompilationUnit, tree: Tree, erasedTypes: boolean): Context = {
import definitions._
var sc = startContext
def addImport(pkg: Symbol): unit = {
assert(pkg != null)
val qual = gen.mkAttributedStableRef(pkg)
sc = sc.makeNewImport(
Import(qual, List(Pair(nme.WILDCARD, null)))
.setSymbol(NoSymbol.newImport(Position.NOPOS).setInfo(ImportType(qual)))
.setType(NoType))
sc.depth = sc.depth + 1
}
if (!settings.noimports.value) {
assert(isDefinitionsInitialized)
addImport(JavaLangPackage)
assert(ScalaPackage != null, "Scala package is null")
addImport(ScalaPackage)
if (!settings.nopredefs.value/* || unit.source.file.name != "Predef.scala"*/)
addImport(PredefModule)
}
val c = sc.make(unit, tree, sc.owner, sc.scope, sc.imports)
c.reportAmbiguousErrors = !erasedTypes
c.reportGeneralErrors = !erasedTypes
c.implicitsEnabled = !erasedTypes
c
}
def resetContexts: unit = {
var sc = startContext
while (sc != NoContext) {
sc.tree match {
case Import(qual, _) => qual.tpe = singleType(qual.symbol.owner.thisType, qual.symbol)
case _ =>
}
sc = sc.outer
}
}
class Context {
var unit: CompilationUnit = _
var tree: Tree = _; // Tree associated with this context
var owner: Symbol = NoSymbol; // The current owner
var scope: Scope = _; // The current scope
var outer: Context = _; // The next outer context
var enclClass: Context = _; // The next outer context whose tree is a
// template or package definition
var enclMethod: Context = _; // The next outer context whose tree is a method
var variance: int = _; // Variance relative to enclosing class.
private var _undetparams: List[Symbol] = List(); // Undetermined type parameters
var depth: int = 0
var imports: List[ImportInfo] = List()
var prefix: Type = NoPrefix
var inConstructorSuffix = false; // are we in a secondary constructor
// after the this constructor call?
var reportAmbiguousErrors = false
var reportGeneralErrors = false
var implicitsEnabled = false
var checking = false
var savedTypeBounds: List[Pair[Symbol, Type]] = List()
def undetparams = _undetparams
def undetparams_=(ps: List[Symbol]) = {
//System.out.println("undetparams = " + ps);//debug
_undetparams = ps
}
def make(unit: CompilationUnit, tree: Tree, owner: Symbol, scope: Scope, imports: List[ImportInfo]): Context = {
val c = new Context
c.unit = unit
c.tree = tree
c.owner = owner
c.scope = scope
tree match {
case Template(_, _) | PackageDef(_, _) =>
c.enclClass = c;
c.prefix = skolemizedThisType(this.tree, this.prefix, c.owner)
c.inConstructorSuffix = false
case _ =>
c.enclClass = this.enclClass
c.prefix = if (c.owner != this.owner && c.owner.isTerm) NoPrefix else this.prefix
c.inConstructorSuffix = this.inConstructorSuffix
}
tree match {
case DefDef(_, _, _, _, _, _) =>
c.enclMethod = c
case _ =>
c.enclMethod = this.enclMethod
}
c.variance = this.variance
c.depth = if (scope == this.scope) this.depth else this.depth + 1
c.imports = imports
c.reportAmbiguousErrors = this.reportAmbiguousErrors
c.reportGeneralErrors = this.reportGeneralErrors
c.implicitsEnabled = this.implicitsEnabled
c.checking = this.checking
c.outer = this
c
}
def make(unit: CompilationUnit): Context = {
val c = make(unit, EmptyTree, owner, scope, imports)
c.reportAmbiguousErrors = true
c.reportGeneralErrors = true
c.implicitsEnabled = true
c
}
def makeNewImport(imp: Import): Context =
make(unit, imp, owner, scope, new ImportInfo(imp, depth) :: imports)
def make(tree: Tree, owner: Symbol, scope: Scope): Context =
make(unit, tree, owner, scope, imports)
def makeNewScope(tree: Tree, owner: Symbol): Context =
make(tree, owner, new Scope(scope))
def make(tree: Tree, owner: Symbol): Context =
make(tree, owner, scope)
def make(tree: Tree): Context =
make(tree, owner)
def makeImplicit(reportAmbiguousErrors: boolean) = {
val c = make(tree)
c.reportAmbiguousErrors = reportAmbiguousErrors
c.reportGeneralErrors = false
c.implicitsEnabled = false
c
}
def makeConstructorContext = {
var baseContext = enclClass.outer
//todo: find out why we need next line
while (baseContext.tree.isInstanceOf[Template]) baseContext = baseContext.outer
val argContext = baseContext.makeNewScope(tree, owner)
for (val sym <- scope.toList) argContext.scope enter sym
argContext
}
def makeConstructorSuffixContext = {
val c = make(tree)
c.inConstructorSuffix = true
c
}
def skolemizedThisType(encl: Tree, pre: Type, clazz: Symbol): Type = if (settings.Xgadt.value) {
encl match {
case ClassDef(_, _, tparamdefs, _, _) =>
System.out.println("sktt " + clazz)
if (!tparamdefs.isEmpty || pre.isInstanceOf[SingleType]) {
val tparams = clazz.unsafeTypeParams
val tskolems = tparamdefs map (.symbol)
System.out.println("sktt2 " + tparams + " " + tskolems)
val self = clazz.newThisSkolem setInfo clazz.typeOfThis.substSym(tparams, tskolems)
singleType(pre, self)
} else clazz.thisType
case _ =>
clazz.thisType
}
} else clazz.thisType
def error(pos: int, msg: String): unit =
if (reportGeneralErrors)
unit.error(pos, if (checking) "**** ERROR DURING INTERNAL CHECKING ****\n" + msg else msg)
else
throw new TypeError(msg)
def ambiguousError(pos: int, pre: Type, sym1: Symbol, sym2: Symbol, rest: String): unit = {
val msg =
("ambiguous reference to overloaded definition,\n" +
"both " + sym1 + sym1.locationString + " of type " + pre.memberType(sym1) +
"\nand " + sym2 + sym2.locationString + " of type " + pre.memberType(sym2) +
"\nmatch " + rest)
if (reportAmbiguousErrors) {
if (!pre.isErroneous && !sym1.isErroneous && !sym2.isErroneous)
unit.error(pos, msg)
} else throw new TypeError(msg)
}
def outerContext(clazz: Symbol): Context = {
var c = this
while (c != NoContext && c.owner != clazz) c = c.outer.enclClass
c
}
def isLocal(): boolean = tree match {
case Block(_,_) => true
case PackageDef(_, _) => false
case EmptyTree => false
case _ => outer.isLocal()
}
def nextEnclosing(p: Context => boolean): Context =
if (this == NoContext || p(this)) this else outer.nextEnclosing(p)
override def toString(): String = {
if (this == NoContext) "NoContext"
else owner.toString() + " @ " + tree.getClass() + " " + tree.toString() + ", scope = " + scope.hashCode() + " " + scope.toList + "\n:: " + outer.toString()
}
/** Is `sym' accessible as a member of tree `site' with type `pre' in current context?
*/
def isAccessible(sym: Symbol, pre: Type, superAccess: boolean): boolean = {
/** Are we inside definition of `owner'? */
def accessWithin(owner: Symbol): boolean = {
var c = this
while (c != NoContext && c.owner != owner) {
if (c.outer == null) assert(false, "accessWithin(" + owner + ") " + c);//debug
if (c.outer.enclClass == null) assert(false, "accessWithin(" + owner + ") " + c);//debug
c = c.outer.enclClass
}
c != NoContext
}
/** Is `clazz' a subclass of an enclosing class? */
def isSubClassOfEnclosing(clazz: Symbol): boolean = {
var c = this.enclClass
while (c != NoContext && !clazz.isSubClass(c.owner)) c = c.outer.enclClass
c != NoContext
}
( pre == NoPrefix
||
(!sym.hasFlag(PRIVATE | PROTECTED))
||
accessWithin(sym.owner) && (!sym.hasFlag(LOCAL) || pre =:= sym.owner.thisType)
||
(!sym.hasFlag(PRIVATE) &&
(superAccess ||
(pre.widen.symbol.isSubClass(sym.owner) && isSubClassOfEnclosing(pre.widen.symbol))))
) && (
sym.privateWithin == NoSymbol
||
phase.erasedTypes || accessWithin(sym.privateWithin)
)
}
def pushTypeBounds(sym: Symbol): unit = {
savedTypeBounds = Pair(sym, sym.info) :: savedTypeBounds
}
def restoreTypeBounds: unit = {
for (val Pair(sym, info) <- savedTypeBounds) {
if (settings.debug.value) log("resetting " + sym + " to " + info);
sym.setInfo(info)
}
savedTypeBounds = List()
}
private var implicitsCache: List[List[ImplicitInfo]] = null
private var implicitsRunId = NoRunId
private def collectImplicits(syms: List[Symbol], pre: Type): List[ImplicitInfo] =
for (val sym <- syms; sym.hasFlag(IMPLICIT) && isAccessible(sym, pre, false))
yield new ImplicitInfo(sym.name, pre, sym)
private def collectImplicitImports(imp: ImportInfo): List[ImplicitInfo] = {
val pre = imp.qual.tpe
def collect(sels: List[Pair[Name, Name]]): List[ImplicitInfo] = sels match {
case List() => List()
case List(Pair(nme.WILDCARD, _)) => collectImplicits(pre.implicitMembers, pre)
case Pair(from, to) :: sels1 =>
var impls = collect(sels1) filter (info => info.name != from)
if (to != nme.WILDCARD) {
val sym = imp.importedSymbol(to)
if (sym.hasFlag(IMPLICIT)) impls = new ImplicitInfo(to, pre, sym) :: impls
}
impls
}
if (settings.debug.value) log("collect implicit imports " + imp + "=" + collect(imp.tree.selectors));//debug
collect(imp.tree.selectors)
}
def implicitss: List[List[ImplicitInfo]] = {
if (implicitsRunId != currentRunId) {
implicitsRunId = currentRunId
val newImplicits: List[ImplicitInfo] =
if (owner != outer.owner && owner.isClass && !owner.isPackageClass) {
if (!owner.isInitialized) return outer.implicitss
if (settings.debug.value) log("collect member implicits " + owner + ", implicit members = " + owner.thisType.implicitMembers);//debug
collectImplicits(owner.thisType.implicitMembers, owner.thisType)
} else if (scope != outer.scope && !owner.isPackageClass) {
if (settings.debug.value) log("collect local implicits " + scope.toList);//debug
collectImplicits(scope.toList, NoPrefix)
} else if (imports != outer.imports) {
assert(imports.tail == outer.imports)
collectImplicitImports(imports.head)
} else List()
implicitsCache = if (newImplicits.isEmpty) outer.implicitss
else newImplicits :: outer.implicitss
}
implicitsCache
}
}
class ImportInfo(val tree: Import, val depth: int) {
/** The prefix expression */
def qual: Tree = tree.symbol.info match {
case ImportType(expr) => expr
case _ => throw new FatalError("symbol " + tree.symbol + " has bad type: " + tree.symbol.info);//debug
}
/** Is name imported explicitly, not via wildcard? */
def isExplicitImport(name: Name): boolean =
tree.selectors exists (._2.==(name.toTermName))
/** The symbol with name `name' imported from import clause `tree'.
*/
def importedSymbol(name: Name): Symbol = {
var result: Symbol = NoSymbol
var renamed = false
var selectors = tree.selectors
while (selectors != Nil && result == NoSymbol) {
if (selectors.head._2 == name.toTermName)
result = qual.tpe.member(
if (name.isTypeName) selectors.head._1.toTypeName else selectors.head._1)
else if (selectors.head._1 == name.toTermName)
renamed = true
else if (selectors.head._1 == nme.WILDCARD && !renamed)
result = qual.tpe.member(name)
selectors = selectors.tail
}
result
}
override def toString() = tree.toString()
}
class ImplicitInfo(val name: Name, pre: Type, val sym: Symbol) {
private var tpeCache: Type = null
def tpe: Type = {
if (tpeCache == null) tpeCache = pre.memberType(sym)
tpeCache
}
}
val NoImplicitInfo = new ImplicitInfo(null, null, null)
case class ImportType(expr: Tree) extends Type
}
|