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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
/* NSC -- new Scala compiler
* Copyright 2005-2009 LAMP/EPFL
* @author Martin Odersky
*/
// $Id$
package scala.tools.nsc.typechecker
import symtab.Flags._
import scala.tools.nsc.util.{Position,NoPosition}
import scala.collection.mutable.ListBuffer
/** This trait ...
*
* @author Martin Odersky
* @version 1.0
*/
trait Contexts { self: Analyzer =>
import global._
val NoContext = new Context {
override def implicitss: List[List[ImplicitInfo]] = List()
outer = this
}
NoContext.enclClass = NoContext
NoContext.enclMethod = NoContext
private val startContext = {
NoContext.make(
global.Template(List(), emptyValDef, List()) setSymbol global.NoSymbol setType global.NoType,
global.definitions.RootClass,
global.definitions.RootClass.info.decls)
}
/** List of objects and packages to import from in
* a root context. This list is sensitive to the
* compiler settings.
*/
protected def rootImports(unit: CompilationUnit, tree: Tree): List[Symbol] = {
import definitions._
val imps = new ListBuffer[Symbol]
if (!settings.noimports.value) {
assert(isDefinitionsInitialized)
imps += JavaLangPackage
if (!unit.isJava) {
assert(ScalaPackage ne null, "Scala package is null")
imps += ScalaPackage
if (!(treeInfo.isPredefUnit(unit.body) || treeInfo.containsLeadingPredefImport(List(unit.body))))
imps += PredefModule
}
}
imps.toList
}
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) {
assert(pkg ne null)
val qual = gen.mkAttributedStableRef(pkg)
sc = sc.makeNewImport(
Import(qual, List((nme.WILDCARD, null)))
.setSymbol(NoSymbol.newImport(NoPosition).setFlag(SYNTHETIC).setInfo(ImportType(qual)))
.setType(NoType))
sc.depth += 1
}
for (imp <- rootImports(unit, tree))
addImport(imp)
val c = sc.make(unit, tree, sc.owner, sc.scope, sc.imports)
c.reportAmbiguousErrors = !erasedTypes
c.reportGeneralErrors = !erasedTypes
c.implicitsEnabled = !erasedTypes
c
}
def resetContexts() {
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 private[typechecker] {
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,
// not inherited to child contexts
var depth: Int = 0
var imports: List[ImportInfo] = List() // currently visible imports
var openImplicits: List[Type] = List() // types for which implicit arguments
// are currently searched
var prefix: Type = NoPrefix
var inConstructorSuffix = false // are we in a secondary constructor
// after the this constructor call?
var returnsSeen = false // for method context: were returns encountered?
var reportAmbiguousErrors = false
var reportGeneralErrors = false
var implicitsEnabled = false
var checking = false
var retyping = false
var savedTypeBounds: List[(Symbol, Type)] = List() // saved type bounds
// for type parameters which are narrowed in a GADT
def intern0 : Context = {
if (this eq NoContext) return this
val txt = new Context
txt.unit = unit
txt.tree = tree
txt.owner = owner
txt.scope = scope
assert(outer ne this) // stupid
txt.outer = outer // already interned
def fix(what : Context) =
if (what eq this) txt
else what
txt.enclClass = fix(enclClass)
txt.enclMethod = fix(enclMethod)
txt.implicitsEnabled = implicitsEnabled
txt.variance = variance
txt._undetparams = _undetparams
txt.depth = depth
txt.imports = imports
txt.openImplicits = openImplicits
txt.prefix = prefix
txt.inConstructorSuffix = inConstructorSuffix
txt.returnsSeen = returnsSeen
txt.reportGeneralErrors = reportGeneralErrors
txt.checking = checking
txt.retyping = retyping
txt.savedTypeBounds = savedTypeBounds
txt
}
override def equals(that: Any): Boolean = that match {
case that: AnyRef if (this eq that) =>
true
case that if !inIDE =>
super.equals(that)
//case NoContext => false
case that: Context =>
if (that eq NoContext) return this eq NoContext
assert(that ne NoContext)
if (this eq NoContext) return false
assert(inIDE)
def eq[T](x : T, y : T) = x == y
val a0 = {
if ((tree ne null) && (tree ne EmptyTree)) tree.setType(null)
if ((tree eq null) || (that.tree eq null)) tree == that.tree else
tree equalsStructure that.tree;
}
val a1 = eq(owner, that.owner)
val a2 = eq(scope, that.scope)
def fix(txt0 : Context, txt1 : Context) =
((this eq txt0) && (that eq txt1)) || (txt0 eq txt1)
val a3 = fix(outer, that.outer)
val a4 = fix(enclClass, that.enclClass)
val a5 = fix(enclMethod, that.enclMethod)
val a6 = eq(variance, that.variance)
val a7 = eq(_undetparams, that._undetparams)
val a8 = eq(depth, that.depth)
val a9 = eq(imports, that.imports)
val a10 = eq(openImplicits, that.openImplicits)
val a11 = eq(prefix, that.prefix)
val a12 = eq(inConstructorSuffix, that.inConstructorSuffix)
val a13 = eq(implicitsEnabled, that.implicitsEnabled)
val a14 = eq(checking, that.checking)
val a15 = eq(retyping, that.retyping)
val a16 = eq(savedTypeBounds, that.savedTypeBounds)
val a17 = eq(unit, that.unit)
val ret = a0 && a1 && a2 && a3 && a4 && a5 && a6 && a7 && a8 && a9 && a10 && a11 && a12 && a13 && a14 && a15 && a16 && a17
val a18 = {
if (implicitsRunId > that.implicitsRunId) {
that.implicitsRunId = NoRunId
that.implicitsCache = null
}
else if (that.implicitsRunId > implicitsRunId) {
implicitsRunId = NoRunId
implicitsCache = null
}
implicitsCache == that.implicitsCache
}
if (ret) {
if (!a18) {
//assert(this.implicitsCache == null || that.implicitsCache == null)
}
}
ret
case _ => false
}
def undetparams = _undetparams
def undetparams_=(ps: List[Symbol]) = {
//System.out.println("undetparams = " + ps);//debug
_undetparams = ps
}
def extractUndetparams() = {
val tparams = undetparams
undetparams = List()
tparams
}
/**
* @param unit ...
* @param tree ...
* @param owner ...
* @param scope ...
* @param imports ...
* @return ...
*/
def make(unit: CompilationUnit, tree: Tree, owner: Symbol,
scope: Scope, imports: List[ImportInfo]): Context = {
val c = new Context
c.unit = unit
c.tree = sanitize(tree)
c.owner = owner
c.scope = scope
c.outer = intern(this)
def internIf(txt : Context) = {
if (txt eq this) c.outer // already interned!
else txt
}
tree match {
case Template(_, _, _) | PackageDef(_, _) =>
c.enclClass = c
c.prefix = c.owner.thisType
c.inConstructorSuffix = false
case _ =>
c.enclClass = internIf(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 = internIf(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.retyping = this.retyping
c.openImplicits = this.openImplicits
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 = {
if (tree == this.tree && owner == this.owner && scope == this.scope) this
else make0(tree, owner, scope)
}
private def make0(tree : Tree, owner : Symbol, scope : Scope) : Context = {
make(unit, tree, owner, scope, imports)
}
def makeNewScope(tree: Tree, owner: Symbol)(implicit kind : ScopeKind): Context =
make(tree, owner, scopeFor(scope, tree, kind))
// IDE stuff: distinguish between scopes created for typing and scopes created for naming.
def make(tree: Tree, owner: Symbol): Context =
make0(tree, owner, scope)
def make(tree: Tree): Context =
make(tree, owner)
def makeSilent(reportAmbiguousErrors: Boolean): Context = {
val c = make(tree)
c.reportGeneralErrors = false
c.reportAmbiguousErrors = reportAmbiguousErrors
c
}
def makeImplicit(reportAmbiguousErrors: Boolean) = {
val c = makeSilent(reportAmbiguousErrors)
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)(Constructor0ScopeKind)
argContext.reportGeneralErrors = this.reportGeneralErrors
argContext.reportAmbiguousErrors = this.reportAmbiguousErrors
def enterElems(c: Context) {
def enterLocalElems(e: ScopeEntry) {
if (e != null && e.owner == c.scope) {
enterLocalElems(e.next)
argContext.scope enter e.sym
}
}
if (c.owner.isTerm && !c.owner.isLocalDummy) {
enterElems(c.outer)
enterLocalElems(c.scope.elems)
}
}
enterElems(this)
argContext
}
//todo: remove
def makeConstructorSuffixContext = {
val c = make(tree)
c.inConstructorSuffix = true
c
}
def error(pos: Position, err: Error) {
val msg = err.getMessage()
if (reportGeneralErrors || inIDE)
unit.error(pos, if (checking) "**** ERROR DURING INTERNAL CHECKING ****\n" + msg else msg)
else
throw err
}
def error(pos: Position, msg: String) {
if (reportGeneralErrors)
unit.error(pos, if (checking) "**** ERROR DURING INTERNAL CHECKING ****\n" + msg else msg)
else
throw new TypeError(pos, msg)
}
def warning(pos: Position, msg: String) {
if (reportGeneralErrors) unit.warning(pos, msg)
}
/**
* @param pos ...
* @param pre ...
* @param sym1 ...
* @param sym2 ...
* @param rest ...
*/
def ambiguousError(pos: Position, pre: Type, sym1: Symbol,
sym2: Symbol, rest: String) {
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(pos, 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()
}
/** Return closest enclosing context that defines a superclass of `clazz', or a
* companion module of a superclass of `clazz', or NoContext if none exists */
def enclosingSuperClassContext(clazz: Symbol): Context = {
var c = this.enclClass
while (c != NoContext &&
!clazz.isNonBottomSubClass(c.owner) &&
!(c.owner.isModuleClass && clazz.isNonBottomSubClass(c.owner.linkedClassOfModule)))
c = c.outer.enclClass
c
}
/** Return closest enclosing context that defines a subclass of `clazz', or NoContext
* if none exists */
def enclosingSubClassContext(clazz: Symbol): Context = {
var c = this.enclClass
while (c != NoContext && !c.owner.isNonBottomSubClass(clazz))
c = c.outer.enclClass
c
}
/** Is <code>sym</code> accessible as a member of tree `site' with type
* <code>pre</code> in current context?
*
* @param sym ...
* @param pre ...
* @param superAccess ...
* @return ...
*/
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 (false && inIDE) // XXX: we didn't get to update these syms....
assert(c.owner.fullNameString != owner.fullNameString)
if (c.outer eq null) assert(false, "accessWithin(" + owner + ") " + c);//debug
if (c.outer.enclClass eq 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 =
enclosingSuperClassContext(clazz) != NoContext
def isSubThisType(pre: Type, clazz: Symbol): Boolean = pre match {
case ThisType(pclazz) => pclazz isNonBottomSubClass clazz
case _ => false
}
(pre == NoPrefix) || {
val ab = sym.accessBoundary(sym.owner)
((ab.isTerm || ab == definitions.RootClass)
||
(accessWithin(ab) || accessWithin(ab.linkedClassOfClass)) &&
(!sym.hasFlag(LOCAL) ||
(sym hasFlag PROTECTED) && isSubThisType(pre, sym.owner) ||
pre =:= sym.owner.thisType)
||
(sym hasFlag PROTECTED) &&
(superAccess ||
(pre.widen.typeSymbol.isNonBottomSubClass(sym.owner) &&
(isSubClassOfEnclosing(pre.widen.typeSymbol) || phase.erasedTypes))))
// note: phase.erasedTypes disables last test, because after addinterfaces
// implementation classes are not in the superclass chain. If we enable the
// test, bug780 fails.
}
}
def pushTypeBounds(sym: Symbol) {
savedTypeBounds = (sym, sym.info) :: savedTypeBounds
}
def restoreTypeBounds(tp: Type): Type = {
var current = tp
for ((sym, info) <- savedTypeBounds) {
if (settings.debug.value) log("resetting " + sym + " to " + info);
sym.info match {
case TypeBounds(lo, hi) if (hi <:< lo && lo <:< hi) =>
current = current.instantiateTypeParams(List(sym), List(lo))
//@M TODO: when higher-kinded types are inferred, probably need a case PolyType(_, TypeBounds(...)) if ... =>
case _ =>
}
sym.setInfo(info)
}
savedTypeBounds = List()
current
}
private var implicitsCache: List[List[ImplicitInfo]] = null
private var implicitsRunId = NoRunId
def resetCache : Unit = {
implicitsRunId = NoRunId
implicitsCache = null
if (outer != null && outer != this) outer.resetCache
}
private def collectImplicits(syms: List[Symbol], pre: Type): List[ImplicitInfo] =
for (sym <- syms if 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[(Name, Name)]): List[ImplicitInfo] = sels match {
case List() => List()
case List((nme.WILDCARD, _)) => collectImplicits(pre.implicitMembers, pre)
case (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]] = {
val nextOuter =
if (owner.isConstructor) {
if (outer.tree.isInstanceOf[Template]) outer.outer.outer
else outer.outer
} else outer
// can we can do something smarter to bring back the implicit cache?
if (implicitsRunId != currentRunId) {
implicitsRunId = currentRunId
implicitsCache = List()
val newImplicits: List[ImplicitInfo] =
if (owner != nextOuter.owner && owner.isClass && !owner.isPackageClass) {
if (!owner.isInitialized) return nextOuter.implicitss
if (settings.debug.value)
log("collect member implicits " + owner + ", implicit members = " +
owner.thisType.implicitMembers)//debug
val savedEnclClass = enclClass
this.enclClass = this
val res = collectImplicits(owner.thisType.implicitMembers, owner.thisType)
this.enclClass = savedEnclClass
res
} else if (scope != nextOuter.scope && !owner.isPackageClass) {
if (settings.debug.value)
log("collect local implicits " + scope.toList)//debug
collectImplicits(scope.toList, NoPrefix)
} else if (imports != nextOuter.imports) {
assert(imports.tail == nextOuter.imports)
collectImplicitImports(imports.head)
} else List()
implicitsCache = if (newImplicits.isEmpty) nextOuter.implicitss
else newImplicits :: nextOuter.implicitss
}
implicitsCache
}
override def hashCode = {
var hc = 0
implicit def b2i(b : Boolean) = if (b) 1 else 0
// assum enclClass/enclMethod/outer are all interned already.
hc += tree.hashCodeStructure
def f(txt : Context) = if (txt eq this) 0 else System.identityHashCode(txt)
hc += f(enclClass)
hc += f(enclMethod)
hc += f(outer)
hc += owner.hashCode
hc += scope.hashCode
hc += variance.hashCode
hc += _undetparams.hashCode
hc += depth
hc += imports.hashCode
hc += prefix.hashCode
hc += inConstructorSuffix
hc += checking
hc += retyping
hc += savedTypeBounds.hashCode
hc += (if (unit eq null) 0 else unit.hashCode)
hc
}
}
class ImportInfo(val tree: Import, val depth: Int) {
/** The prefix expression */
def qual: Tree = tree.symbol.info match {
case ImportType(expr) => expr
case ErrorType => tree
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 <code>name</code> imported from import clause
* <code>tree</code>.
*/
def importedSymbol(name: Name): Symbol = {
var result: Symbol = NoSymbol
var renamed = false
var selectors = tree.selectors
while (selectors != Nil && result == NoSymbol) {
if (selectors.head._1 != nme.WILDCARD)
notifyImport(name, qual.tpe, selectors.head._1, selectors.head._2)
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()
override def hashCode = tree.hashCodeStructure + depth
override def equals(that : Any) = that match {
case that : ImportInfo =>
depth == that.depth && (tree equalsStructure that.tree)
case _ => false
}
}
case class ImportType(expr: Tree) extends Type {
override def equals(that : Any) = that match {
case ImportType(expr) =>
if (inIDE) this.expr equalsStructure expr
else this.expr == expr
case _ => false
}
override def hashCode = if (inIDE) expr.hashCodeStructure else expr.hashCode
override def safeToString = "ImportType("+expr+")"
}
protected def intern(txt : Context) = txt
}
|