summaryrefslogtreecommitdiff
path: root/sources/scala/tools/nsc/symtab/Symbols.scala
blob: 7d87f7e5525370aae668bf03d8d24a21ee5abc15 (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
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$
package scala.tools.nsc.symtab;

import scala.tools.util.Position;
import Flags._;

abstract class Symbols: SymbolTable {
  import definitions._;

  private var ids = 0;

  val emptySymbolArray = new Array[Symbol](0);

  /** The class for all symbols */
  abstract class Symbol(initOwner: Symbol, initPos: int, initName: Name) {

    var owner = initOwner;
    var name = initName;
    var pos = initPos;
    val id = { ids = ids + 1; ids }
    private var rawflags: long = 0;

// Creators -------------------------------------------------------------------

    final def newValue(pos: int, name: Name) =
      new TermSymbol(this, pos, name);
    final def newVariable(pos: int, name: Name) =
      newValue(pos, name).setFlag(MUTABLE);
    final def newValueParameter(pos: int, name: Name) =
      newValue(pos, name).setFlag(PARAM);
    final def newLocalDummy(pos: int) =
      newValue(pos, nme.LOCAL(owner)).setInfo(NoType);
    final def newMethod(pos: int, name: Name) =
      newValue(pos, name).setFlag(METHOD);
    final def newLabel(pos: int, name: Name) =
      newMethod(pos, name).setFlag(LABEL);
    final def newConstructor(pos: int) =
      newMethod(pos, nme.CONSTRUCTOR);
    final def newModule(pos: int, name: Name, clazz: ClassSymbol) =
      new ModuleSymbol(this, pos, name, clazz);
    final def newModule(pos: int, name: Name) =
      new ModuleSymbol(this, pos, name, null);
    final def newPackage(pos: int, name: Name) = {
      assert(isPackageClass);
      val m = newModule(pos, name);
      m.setFlag(JAVA | PACKAGE);
      m.moduleClass.setFlag(JAVA | PACKAGE);
      m
    }
    final def newThisSym(pos: int) = {
      newValue(pos, nme.this_).setFlag(SYNTHETIC);
    }
    final def newImport(pos: int) =
      newValue(pos, nme.IMPORT).setFlag(SYNTHETIC);
    final def newOverloaded(pre: Type, alternatives: List[Symbol]): Symbol =
      newValue(alternatives.head.pos, alternatives.head.name)
      .setFlag(OVERLOADED)
      .setInfo(OverloadedType(pre, alternatives));

    final def newErrorValue(name: Name) =
      newValue(pos, name).setFlag(SYNTHETIC | IS_ERROR).setInfo(ErrorType);
    final def newAliasType(pos: int, name: Name) =
      new TypeSymbol(this, pos, name);
    final def newAbstractType(pos: int, name: Name) =
      new TypeSymbol(this, pos, name).setFlag(DEFERRED);
    final def newTypeParameter(pos: int, name: Name) =
      newAbstractType(pos, name).setFlag(PARAM);
    final def newClass(pos: int, name: Name) =
      new ClassSymbol(this, pos, name);
    final def newAnonymousClass(pos: int) =
      newClass(pos, nme.ANON_CLASS_NAME.toTypeName);
    final def newRefinementClass(pos: int) =
      newClass(pos, nme.REFINE_CLASS_NAME.toTypeName);
    final def newErrorClass(name: Name) = {
      val clazz = newClass(pos, name).setFlag(SYNTHETIC | IS_ERROR);
      clazz.setInfo(ClassInfoType(List(), new ErrorScope(this), clazz));
      clazz
    }
    final def newErrorSymbol(name: Name) =
      if (name.isTypeName) newErrorClass(name) else newErrorValue(name);

// Tests ----------------------------------------------------------------------

    def isTerm = false;        //to be overridden
    def isType = false;        //to be overridden
    def isClass = false;       //to be overridden

    final def isValue = isTerm && !(isModule && hasFlag(PACKAGE | JAVA));
    final def isVariable = isTerm && hasFlag(MUTABLE);
    final def isGetter = isTerm && hasFlag(ACCESSOR) && !name.endsWith(nme._EQ);
    final def isSetter = isTerm && hasFlag(ACCESSOR) && name.endsWith(nme._EQ);
    final def isValueParameter = isTerm && hasFlag(PARAM);
    final def isLocalDummy = isTerm && (name startsWith nme.LOCAL_PREFIX);
    final def isMethod = isTerm && (flags & (METHOD | STABLE)) == METHOD;
    final def isLabel = isTerm && hasFlag(LABEL);
    final def isConstructor = isTerm && name == nme.CONSTRUCTOR;
    final def isModule = isTerm && hasFlag(MODULE);
    final def isPackage = isModule && hasFlag(PACKAGE);
    final def isThisSym = isTerm && name == nme.this_;
    final def isError = hasFlag(IS_ERROR);
    final def isTrait = isClass & hasFlag(TRAIT);
    final def isAliasType = isType && !isClass && !hasFlag(DEFERRED);
    final def isAbstractType = isType && !isClass && hasFlag(DEFERRED);
    final def isTypeParameter = isType && hasFlag(PARAM);
    final def isAnonymousClass = isClass && (name startsWith nme.ANON_CLASS_NAME); // startsWith necessary because name may grow when lifted
    final def isRefinementClass = isClass && name == nme.REFINE_CLASS_NAME; // no lifting for refinement classes
    final def isModuleClass = isClass && hasFlag(MODULE);
    final def isPackageClass = isClass && hasFlag(PACKAGE);
    final def isRoot = isPackageClass && name == nme.ROOT.toTypeName;
    final def isEmptyPackageClass = isPackageClass && name == nme.EMPTY_PACKAGE_NAME.toTypeName;

    /** Does this symbol denote a stable value? */
    final def isStable =
      isTerm && !hasFlag(MUTABLE) && (!hasFlag(METHOD) || hasFlag(STABLE));

    /** Does this symbol denote the primary constructor
     * of its enclosing class? */
    final def isPrimaryConstructor =
      isConstructor && owner.primaryConstructor == this;

    /** Is this symbol static (i.e. with no outer instance)? */
    final def isStatic: boolean = isRoot || owner.isStaticOwner;

    /** Does this symbol denote a class that defines static symbols? */
    final def isStaticOwner: boolean = isPackageClass || isStatic && isModuleClass;

    /** Is this symbol final?*/
    final def isFinal: boolean =
      hasFlag(FINAL) ||
      isTerm && (
        hasFlag(PRIVATE) || isLocal || owner.isClass && owner.hasFlag(FINAL | MODULE));

    /** Is this symbol a sealed class?*/
    final def isSealed: boolean =
      isClass && (hasFlag(SEALED) || isSubClass(AnyValClass) || isSubClass(ArrayClass));

    /** Is this symbol locally defined? I.e. not a member of a class or module */
    final def isLocal: boolean = owner.isTerm || hasFlag(LOCAL);

    /** Is this class locally defined?
     *  A class is local, if
     *   - it is anonymous, or
     *   - its owner is a value
     *   - it is defined within a local class
     */
    final def isLocalClass: boolean =
      isClass && (isAnonymousClass || isRefinementClass || isLocal ||
                  !owner.isPackageClass && owner.isLocalClass);

    /** Symbol was preloaded from package */
    final def isExternal: boolean = pos == Position.NOPOS;

    /** The variance of this symbol as an integer */
    final def variance: int =
      if (hasFlag(COVARIANT)) 1
      else if (hasFlag(CONTRAVARIANT)) -1
      else 0;

// Flags ----------------------------------------------------------------------------

    final def flags = rawflags;
    final def flags_=(fs: long) = rawflags = fs;
    final def setFlag(mask: long): this.type = { rawflags = rawflags | mask; this }
    final def resetFlag(mask: long): this.type = { rawflags = rawflags & ~mask; this }
    final def getFlag(mask: long): long = rawflags & mask;
    final def hasFlag(mask: long): boolean = (rawflags & mask) != 0;
    final def resetFlags: unit = { rawflags = rawflags & SOURCEFLAGS }

// Info and Type -------------------------------------------------------------------

    private var infos: TypeHistory = null;
    private var limit: Phase = NoPhase;

    /** Get type. The type of a symbol is:
     *  for a type symbol, the type corresponding to the symbol itself
     *  for a term symbol, its usual type
     */
    def tpe: Type = info;

    /** Get type info associated with symbol at current phase, after
     *  ensuring that symbol is initialized (i.e. type is completed).
     */
    final def info: Type = {
      var cnt = 0;
      while ((rawflags & INITIALIZED) == 0) {
        assert(infos != null, "infos null for " + this.name + " " + (this == definitions.EmptyPackageClass));//debug
	val tp = infos.info;
        if ((rawflags & LOCKED) != 0) {
          setInfo(ErrorType);
          throw CyclicReference(this, tp);
        }
        rawflags = rawflags | LOCKED;
	val current = phase;
	phase = infos.start;
        //System.out.println("completing " + this);//DEBUG
        tp.complete(this);
        rawflags = rawflags & ~LOCKED;
	phase = current;
	cnt = cnt + 1;
	// allow for two completions:
	//   one: sourceCompleter to LazyType, two: LazyType to completed type
	if (cnt == 2) throw new Error("no progress in completing " + this + ":" + tp);
      }
      rawInfo
    }

    /** Set initial info. */
    def setInfo(info: Type): this.type = {
      infos = new TypeHistory(phase, info, null);
      limit = phase;
      assert(info != null, "setInfo(null) for " + name + " at phase " + phase);//debug
      rawflags = if (info.isComplete) rawflags | INITIALIZED & ~LOCKED;
		 else rawflags & ~INITIALIZED & ~LOCKED;
      if (info.isInstanceOf[MethodType] || info.isInstanceOf[PolyType])
	assert(isClass || hasFlag(METHOD));
      this
    }

    /** Set new info valid from start of next phase. */
    final def updateInfo(info: Type): Symbol = {
      val current = phase;
      phase = phase.next;
      assert(infos.start.id <= phase.id);
      if (infos.start == phase) infos = infos.prev;
      infos = new TypeHistory(phase, info, infos);
      phase = current;
      this
    }

    /** Return info without checking for initialization or completing */
    final def rawInfo: Type = {
      if (limit.id < phase.id) {
	if ((rawflags & INITIALIZED) != 0) {
	  val current = phase;
          var itr = infoTransformers.nextFrom(limit);
          infoTransformers = itr; // caching optimization
          while (itr.phase != NoPhase && itr.phase.id < current.id) {
            phase = itr.phase;
            val info1 = itr.transform(this, infos.info);
	    if (!(info1 eq infos.info))
	      infos = new TypeHistory(phase.next, info1, infos);
            itr = itr.nextFrom(phase.next)
          }
          phase = current;
          limit = current;
	}
	assert(infos != null, name.toString() + " " + limit + " " + phase);//debug
	infos.info
      } else {
	var infos = this.infos;
	while (phase.id < infos.start.id && infos.prev != null) infos = infos.prev;
	infos.info
      }
    }

    /** Initialize the symbol */
    final def initialize: Symbol = {
      if ((rawflags & INITIALIZED) == 0) info;
      this
    }

    /** Was symbol's type updated during given phase? */
    final def isUpdatedAt(phase: Phase): boolean = {
      var infos = this.infos;
      while (infos != null && infos.start != phase.next) infos = infos.prev;
      infos != null
    }

    /** The type constructor of a symbol is:
     *  For a type symbol, the type corresponding to the symbol itself,
     *  excluding parameters.
     *  Not applicable for term symbols.
     */
    def typeConstructor: Type = throw new Error("typeConstructor inapplicable for " + this);

    /** The type parameters of this symbol */
    def typeParams: List[Symbol] = rawInfo.typeParams;

    /** Reset symbol to initial state
     */
    def reset(completer: Type): unit = {
      resetFlags;
      pos = Position.NOPOS;
      infos = null;
      setInfo(completer)
    }
// Comparisons ----------------------------------------------------------------

    /** A total ordering between symbols that refines the class
     *  inheritance graph (i.e. subclass.isLess(superclass) always holds).
     *  the ordering is given by: (isType, -|closure| for type symbols, id)
     */
    final def isLess(that: Symbol): boolean =
      if (this.isType)
	that.isType &&
	{val diff = this.info.closure.length - that.info.closure.length;
	 diff > 0 || diff == 0 && this.id < that.id}
      else
	that.isType || this.id < that.id;

    /** A partial ordering between symbols.
     *  (this isNestedIn that) holds iff this symbol is defined within
     *  a class or method defining that symbol
     */
    final def isNestedIn(that: Symbol): boolean =
      owner == that || owner != NoSymbol && (owner isNestedIn that);

    /** Is this class symbol a subclass of that symbol? */
    final def isSubClass(that: Symbol): boolean =
      this == that || this.isError || that.isError ||
      info.closurePos(that) >= 0 ||
      this == AllClass ||
      this == AllRefClass &&
	(that == AnyClass ||
         that != AllClass && (that isSubClass AnyRefClass));

// Overloaded Alternatives ---------------------------------------------------------

    def alternatives: List[Symbol] =
      if (hasFlag(OVERLOADED)) info.asInstanceOf[OverloadedType].alternatives
      else List(this);

    def filter(cond: Symbol => boolean): Symbol =
      if (hasFlag(OVERLOADED)) {
	val alts = alternatives;
	val alts1 = alts filter cond;
	if (alts1 eq alts) this
	else if (alts1.isEmpty) NoSymbol
	else if (alts1.tail.isEmpty) alts1.head
	else owner.newOverloaded(info.prefix, alts1)
      } else if (cond(this)) this
      else NoSymbol;

    def suchThat(cond: Symbol => boolean): Symbol = {
      val result = filter(cond);
      assert(!result.hasFlag(OVERLOADED));
      result
    }

// Cloneing -------------------------------------------------------------------

    /** A clone of this symbol */
    final def cloneSymbol: Symbol =
      cloneSymbol(owner);

    /** A clone of this symbol, but with given owner */
    final def cloneSymbol(owner: Symbol): Symbol =
      cloneSymbolImpl(owner).setInfo(info.cloneInfo(owner)).setFlag(flags);

    /** Internal method to clone a symbol's implementation without flags or type
     */
    def cloneSymbolImpl(owner: Symbol): Symbol;

// Access to related symbols --------------------------------------------------

    /** The next enclosing class */
    def enclClass: Symbol = if (isClass) this else owner.enclClass;

    /** The next enclosing method */
    def enclMethod: Symbol = if (isMethod) this else owner.enclMethod;

    /** The primary constructor of a class */
    def primaryConstructor: Symbol = info.decl(nme.CONSTRUCTOR).alternatives.head;

    /** The self symbol of a class with explicit self type, or else the symbol itself.
     */
    def thisSym: Symbol = this;

    /** The type of `this' in a class, or else the type of the symbol itself. */
    final def typeOfThis = thisSym.tpe;

    /** Sets the type of `this' in a class */
    def typeOfThis_=(tp: Type): unit = throw new Error("typeOfThis cannot be set for " + this);

    /** If symbol is a class, the type this.type in this class, otherwise NoPrefix */
    def thisType: Type = NoPrefix;

    /** Return every accessor of a primary constructor parameter in this case class */
    final def caseFieldAccessors: List[Symbol] = {
      assert(isClass && hasFlag(CASE));
      info.decls.toList.filter(sym => !sym.hasFlag(PARAM) && sym.hasFlag(PARAMACCESSOR))
    }

    /** The symbol accessed by this accessor function.
     */
    final def accessed: Symbol = {
      assert(hasFlag(ACCESSOR));
      val name1 = if (name.endsWith(nme._EQ)) name.subName(0, name.length - nme._EQ.length)
	          else name;
      owner.info.decl(name1.toString() + "$")
    }

    /** The class with the same name in the same package as this module or
     *  case class factory
     */
    final def linkedClass: Symbol = {
      if (owner.isPackageClass)
	owner.info.decl(name.toTypeName).suchThat(sym => sym.rawInfo != NoType)
      else NoSymbol;
    }

    /** The module or case class factory with the same name in the same
     *  package as this class.
     */
    final def linkedModule: Symbol =
      if (owner.isPackageClass)
	owner.info.decl(name.toTermName).suchThat(
          sym => (sym hasFlag MODULE) && (sym.rawInfo != NoType));
      else NoSymbol;

    /** For a module its linked class, for a class its linked module, NoSymbol otherwise */
    final def linkedSym: Symbol =
      if (isModule) linkedClass
      else if (isClass) linkedModule
      else NoSymbol;

    /** The module corresponding to this module class (note that this
     *  is not updated when a module is cloned).
     */
    def sourceModule: Symbol = NoSymbol;

    /** The module class corresponding to this module.
     */
    def moduleClass: Symbol = NoSymbol;

    /** The symbol overridden by this symbol in given base class */
    final def overriddenSymbol(base: Symbol): Symbol =
      base.info.nonPrivateDecl(name).suchThat(sym =>
        sym.isType || (owner.thisType.memberType(sym) matches tpe));

    /** The symbol overriding this symbol in given subclass */
    final def overridingSymbol(base: Symbol): Symbol =
      base.info.nonPrivateDecl(name).suchThat(sym =>
        sym.isType || (base.thisType.memberType(sym) matches base.thisType.memberType(this)));

// ToString -------------------------------------------------------------------

    /** A tag which (in the ideal case) uniquely identifies class symbols */
    final def tag: int = name.toString().hashCode();

    /** The simple name of this Symbol (this is always a term name) */
    final def simpleName: Name =
      if (isConstructor && !settings.debug.value) owner.name.toTermName else name;

    /** String representation of symbol's definition key word */
    final def keyString: String =
      if (isTrait)
        if (hasFlag(JAVA)) "interface" else "trait"
      else if (isClass) "class"
      else if (isType && !hasFlag(PARAM)) "type"
      else if (isVariable) "var"
      else if (isPackage) "package"
      else if (isModule) "object"
      else if (isMethod) "def"
      else if (isTerm && (!hasFlag(PARAM) || hasFlag(PARAMACCESSOR))) "val"
      else "";

    /** String representation of symbol's kind */
    final def kindString: String =
      if (isPackageClass)
        if (settings.debug.value) "package class" else "package"
      else if (isAnonymousClass) "<template>"
      else if (isRefinementClass) ""
      else if (isModuleClass) "singleton class"
      else if (isTrait) "trait"
      else if (isClass) "class"
      else if (isType) "type"
      else if (isVariable) "variable"
      else if (isPackage) "package"
      else if (isModule) "object"
      else if (isConstructor) "constructor"
      else if (isMethod) "method"
      else if (isTerm) "value"
      else "";

    /** String representation of symbol's simple name.
     *  If !settings.debug translates expansions of operators back to operator symbol.
     *  E.g. $eq => =.
     *  If settings.uniquId adds id.
     */
    final def nameString: String =
      simpleName.decode + idString;

    /** String representation of symbol's full name with `separator'
     *  between class names.
     *  Never translates expansions of operators back to operator symbol.
     *  Never adds id.
     */
    final def fullNameString(separator: char): String =
      if (owner.isRoot || owner.isEmptyPackageClass) simpleName.toString()
      else owner.fullNameString(separator) + separator + simpleName;

    final def fullNameString: String = fullNameString('.');

    /** If settings.uniqid is set, the symbol's id, else "" */
    final def idString: String =
      if (settings.uniqid.value) "#" + id else "";

    /** String representation, including symbol's kind
     *  e.g., "class Foo", "method Bar".
     */
    override def toString(): String =
      compose(List(kindString, nameString));

    /** String representation of location. */
    final def locationString: String =
      if (owner.isClass &&
          (!owner.isAnonymousClass && !owner.isRefinementClass || settings.debug.value))
	" in " + (if (owner.isModuleClass) "object " + owner.nameString  else owner)
      else "";

    /** String representation of symbol's definition following its name */
    final def infoString(tp: Type): String = {
      def typeParamsString: String = tp match {
	case PolyType(tparams, _) if (tparams.length != 0) =>
	  (tparams map (.defString)).mkString("[", ",", "]")
	case _ =>
	  ""
      }
      if (isClass)
	typeParamsString + " extends " + tp.resultType
      else if (isAliasType)
	typeParamsString + " = " + tp.resultType
      else if (isAbstractType)
	tp.match {
	  case TypeBounds(lo, hi) =>
	    (if (lo.symbol == AllClass) "" else " >: " + lo) +
	    (if (hi.symbol == AnyClass) "" else " <: " + hi)
	  case _ =>
	    "<: " + tp;
	}
      else if (isModule)
	moduleClass.infoString(tp)
      else
	tp match {
	  case PolyType(tparams, res) =>
	    typeParamsString + infoString(res)
	  case MethodType(pts, res) =>
	    pts.mkString("(", ",", ")") + infoString(res)
	  case _ =>
	    ": " + tp
	}
    }

    /** String representation of symbol's variance */
    private def varianceString: String =
      if (variance > 0) "+"
      else if (variance < 0) "-"
      else "";

    /** String representation of symbol's definition */
    final def defString: String =
      compose(List(flagsToString(flags & EXPLICITFLAGS),
		   keyString,
		   varianceString + nameString,
		   infoString(rawInfo)));

    /** Concatenate strings separated by spaces */
    private def compose(ss: List[String]): String =
      ss.filter("" !=).mkString("", " ", "");
  }

  /** A class for term symbols */
  class TermSymbol(initOwner: Symbol, initPos: int, initName: Name) extends Symbol(initOwner, initPos, initName) {
    override def isTerm = true;
    def cloneSymbolImpl(owner: Symbol): Symbol =
      new TermSymbol(owner, pos, name);
  }

  /** A class for module symbols */
  class ModuleSymbol(initOwner: Symbol, initPos: int, initName: Name, mclazz: ClassSymbol) extends TermSymbol(initOwner, initPos, initName) {
    setFlag(MODULE | FINAL);
    private val clazz: ClassSymbol =
      if (mclazz != null) mclazz else new ModuleClassSymbol(this);
    override def moduleClass: ClassSymbol = clazz;
    override def cloneSymbolImpl(owner: Symbol): Symbol =
      new ModuleSymbol(owner, pos, name, clazz);
  }

  /** A class of type symbols. Alias and abstract types are direct instances
   *  of this class. Classes are instances of a subclass.
   */
  class TypeSymbol(initOwner: Symbol, initPos: int, initName: Name) extends Symbol(initOwner, initPos, initName) {
    override def isType = true;
    private var tyconCache: Type = null;
    private var tpeCache: Type = _;
    private var valid: Phase = null;
    override def tpe: Type = {
      assert(tpeCache != NoType, this);
      if (valid != phase) {
        valid = phase;
        tpeCache = NoType;
        tpeCache = typeRef(if (isTypeParameter) NoPrefix else owner.thisType,
                           this, typeParams map (.tpe));
      }
      assert(tpeCache != null, "" + this + " " + phase);
      tpeCache
    }
    override def typeConstructor: Type = {
      if (tyconCache == null)
	tyconCache = typeRef(if (isTypeParameter) NoPrefix else owner.thisType,
			     this, List());
      tyconCache;
    }
    override def setInfo(tp: Type): this.type = {
      valid = null;
      tyconCache = null;
      super.setInfo(tp)
    }
    override def reset(completer: Type): unit = {
      super.reset(completer);
      valid = null;
      tyconCache = null;
    }
    def cloneSymbolImpl(owner: Symbol): Symbol =
      new TypeSymbol(owner, pos, name);
  }

  /** A class for class symbols */
  class ClassSymbol(initOwner: Symbol, initPos: int, initName: Name)
   extends TypeSymbol(initOwner, initPos, initName) {
    var sourceFile: String = null;
    private var thissym: Symbol = this;
    override def isClass: boolean = true;
    override def reset(completer: Type): unit = {
      super.reset(completer);
      thissym = this;
    }

    private var thisTypeCache: Type = null;

    /** the type this.type in this class */
    override def thisType: Type = {
      if (thisTypeCache == null) {
	thisTypeCache =
	  if (isModuleClass && !isRoot) {
	    assert(sourceModule != NoSymbol);
	    singleType(owner.thisType, sourceModule);
	  } else ThisType(this);
      }
      thisTypeCache
    }

    /** A symbol carrying the self type of the class as its type */
    override def thisSym: Symbol = thissym;

    /** Sets the self type of the class */
    override def typeOfThis_=(tp: Type): unit =
      thissym = newThisSym(pos).setInfo(tp);

    override def cloneSymbolImpl(owner: Symbol): Symbol = {
      assert(!isModuleClass);
      val clone = new ClassSymbol(owner, pos, name);
      if (thisSym != this) clone.typeOfThis = typeOfThis;
      clone
    }

    override def sourceModule = if (isModuleClass) owner.linkedModule else NoSymbol;
  }

  /** A class for module class symbols
   *  Note: Not all module classes are of this type; when unpickled, we get plain class symbols!
   */
  class ModuleClassSymbol(module: ModuleSymbol) extends ClassSymbol(module.owner, module.pos, module.name.toTypeName) {
    setFlag(module.getFlag(MODULE2CLASSFLAGS) | MODULE | FINAL);
    override def sourceModule = module;
  }

  /** An object repreesenting a missing symbol */
  object NoSymbol extends Symbol(null, Position.NOPOS, nme.NOSYMBOL) {
    super.setInfo(NoType);
    override def setInfo(info: Type): this.type = { assert(info == NoType); this }
    override def enclClass: Symbol = this;
    override def enclMethod: Symbol = this;
    override def owner: Symbol = throw new Error();
    override def alternatives: List[Symbol] = List();
    override def reset(completer: Type): unit = {}
    def cloneSymbolImpl(owner: Symbol): Symbol = throw new Error();
  }

  /** An exception for cyclic references of symbol definitions */
  case class CyclicReference(sym: Symbol, info: Type) extends TypeError("illegal cyclic reference involving " + sym);

  /** A class for type histories */
  private class TypeHistory(val start: Phase, val info: Type, val prev: TypeHistory);
}