summaryrefslogblamecommitdiff
path: root/sources/scala/tools/nsc/typechecker/RefChecks.scala
blob: f7e5b3e32dcefbb586d7cf1467c3801f3ae0cda5 (plain) (tree)
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











































































































































































































































































































































































































































































































































































































































































































                                                                                                                  
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$
package scala.tools.nsc.typechecker;

import symtab.Flags._;
import collection.mutable.HashMap;
import util.ListBuffer;
import transform.Transform;

/** Post-attribution checking and transformation.
 *  //todo: check whether we always check type parameter bounds.
 *
 *  This phase performs the following checks.
 *
 *   - All overrides conform to rules.
 *   - All type arguments conform to bounds.
 *   - All type variable uses conform to variance annotations.
 *   - No forward reference to a term symbol extends beyond a value definition.
 *
 *  It performs the following transformations.
 *
 *   - Local modules are replaced by variables and classes
 *   - caseArity, caseElement implementations added to case classes
 *   - equals, and hashCode and toString methods are added to case classes,
 *       unless they are defined in the class or a baseclass
 *       different from java.lang.Object
 *   - toString method is added to case objects,
 *       unless they are defined in the class or a baseclass
 *       different from java.lang.Object
 *   - Calls to case factory methods are replaced by new's.
 *   - Function nodes are eliminated.
 */
abstract class RefChecks extends Transform {

  import global._;
  import definitions._;
  import typer.{typed, typedOperator, atOwner};
  import posAssigner.atPos;

  /** the following two members override abstract members in Transform */
  protected val phaseName: String = "refchecks";
  protected def newTransformer(unit: CompilationUnit): Transformer = new RefCheckTransformer(unit);

  class RefCheckTransformer(unit: CompilationUnit) extends Transformer {

// Override checking ------------------------------------------------------------

    /** 1. Check all members of class `clazz' for overriding conditions.
     *  That is for overriding member M and overridden member O:
     *
     *    1.1. M must have the same or stronger access privileges as O.
     *    1.2. O must not be final.
     *    1.3. O is deferred, or M has `override' modifier.
     *    1.4. If O is an immutable value, then so is M.
     *    1.5. Neither M nor O are a parameterized type alias
     *    1.6. If O is a type alias, then M is an alias of O.
     *    1.7. If O is an abstract type then
     *         either M is an abstract type, and M's bounds are sharper than O's bounds.
     *         or M is an unparameterized type alias or class which conforms to O's bounds.
     *    1.8. If O and M are values, then M's type is a subtype of O's type.
     *  2. Check that only abstract classes have deferred members
     *  3. Check that every member with an `override' modifier
     *     overrides some other member.
     */
    private def checkAllOverrides(clazz: Symbol): unit = {

      val self = clazz.thisType;

      def infoString(sym: Symbol) =
	sym.toString() +
	(if (sym.owner == clazz) ""
	 else (sym.locationString +
	       (if (sym.isAliasType) ", which equals " + self.memberInfo(sym)
		else if (sym.isAbstractType) " with bounds " +  self.memberInfo(sym)
		else if (sym.isTerm) " of type " + self.memberInfo(sym)
		else "")));

      /* Check that all conditions for overriding `other' by `member' are met. */
      def checkOverride(clazz: Symbol, member: Symbol, other: Symbol): unit = {
	val pos = if (member.owner == clazz) member.pos else clazz.pos;

	def overrideError(msg: String): unit =
	  if (other.tpe != ErrorType && member.tpe != ErrorType)
	    unit.error(pos, "error overriding " + infoString(other) +
		       ";\n " + infoString(member) + " " + msg);

	def overrideTypeError(): unit = {
	  if (other.tpe != ErrorType && member.tpe != ErrorType) {
	    overrideError("has incompatible type");
	    explainTypes(member.tpe, other.tpe);
	  }
	}

	//System.out.println(infoString(member) + " overrides " + infoString(other) + " in " + clazz);//DEBUG

	// return if we already checked this combination elsewhere
	if (member.owner != clazz) {
	  if ((member.owner isSubClass other.owner) &&
	      ((member hasFlag DEFERRED) || !(other hasFlag DEFERRED))) {
		//System.out.println(infoString(member) + " shadows1 " + infoString(other) " in " + clazz);//DEBUG
		return;
	      }
	  if (clazz.info.parents exists (parent =>
	    (parent.symbol isSubClass other.owner) && (parent.symbol isSubClass member.owner) &&
	    ((member hasFlag DEFERRED) || !(other hasFlag DEFERRED)))) {
	      //System.out.println(infoString(member) + " shadows2 " + infoString(other) + " in " + clazz);//DEBUG
		return;
	    }
	  if (clazz.info.parents forall (parent =>
	    (parent.symbol isSubClass other.owner) == (parent.symbol isSubClass member.owner))) {
	      //System.out.println(infoString(member) + " shadows " + infoString(other) + " in " + clazz);//DEBUG
	      return;
	    }
	}

	if (member hasFlag PRIVATE) { // (1.1)
	  overrideError("has weaker access privileges; it should not be private");
	} else if ((member hasFlag PROTECTED) && !(other hasFlag PROTECTED)) { // 1
	  overrideError("has weaker access privileges; it should not be protected");
	} else if (other hasFlag FINAL) { // (1.2)
	  overrideError("cannot override final member");
	} else if (!(other hasFlag DEFERRED) && !(member hasFlag (OVERRIDE | ABSOVERRIDE))) { // (1.3)
	  overrideError("needs `override' modifier");
	} else if (other.isStable && !member.isStable) { // (1.4)
	  overrideError("needs to be an immutable value");
	} else {
	  if (other.isAliasType) {
	    if (!member.typeParams.isEmpty) // (1.5)
	      overrideError("may not be parameterized");
	    if (!other.typeParams.isEmpty) // (1.5)
	      overrideError("may not override parameterized type");
	    if (!(self.memberType(member) =:= self.memberType(other))) // (1.6)
	      overrideTypeError();
	  } else if (other.isAbstractType) {
	    if (!member.typeParams.isEmpty) // (1.7)
	      overrideError("may not be parameterized");
	    if (!(self.memberInfo(other).bounds containsType self.memberInfo(member))) // (1.7)
	      overrideTypeError();
	  } else if (other.isTerm) {
	    if (!(self.memberInfo(member) <:< (self.memberInfo(other)))) // 8
	      overrideTypeError();
	  }
	}
      }

      // 1. Check all members for overriding conditions.
      for (val bc <- clazz.info.baseClasses.tail; val other <- bc.info.decls.toList)
	if (!other.isClass && !(other hasFlag PRIVATE) && !other.isConstructor) {
	  val member = clazz.tpe.member(other.name) filter
	    (sym => sym.isType || (self.memberType(sym) matches self.memberType(other)));
	  if (member hasFlag OVERLOADED) {
	    val alt1 = member.alternatives.head;
	    val alt2 = member.alternatives.tail.head;
	    val pos = if (alt1.owner == clazz) alt1.pos
		      else if (alt2.owner == clazz) alt2.pos
		      else clazz.pos;
	    unit.error(pos,
	      "ambiguous override: both " + infoString(alt1) +
	      "\n and " + infoString(alt2) +
	      "\n override " + infoString(other));
	  } else if (member != other && !(member hasFlag LOCAL)) {
	    member.flags = member.flags | ACCESSED;
	    checkOverride(clazz, member, other);
	  }
	}

      // 2. Check that only abstract classes have deferred members
      if (clazz.isClass && !(clazz hasFlag ABSTRACT)) {
	def abstractClassError(msg: String): unit = {
	  unit.error(clazz.pos,
	    (if (clazz.isAnonymousClass || clazz.isModuleClass) "object creation impossible"
	     else clazz.toString() + " needs to be abstract") + ", since " + msg);
	  clazz.setFlag(ABSTRACT);
	}
	for (val member <- clazz.tpe.members)
	  if (member hasFlag DEFERRED) {
	    abstractClassError(
	      infoString(member) + " is not defined" +
	      (if (member hasFlag MUTABLE)
		"\n(Note that variables need to be initialized to be defined)" else ""))
	  } else if (member.isIncompleteIn(clazz)) {
	    val other = member.superSymbol(clazz);
	    abstractClassError(
	      infoString(member) + " is marked `abstract' and `override'" +
	      (if (other != NoSymbol)
		" and overrides incomplete superclass member " + infoString(other)
	       else ""))
	  }
      }

      // 3. Check that every defined member with an `override' modifier overrides some other member.
      for (val member <- clazz.info.decls.toList)
	if ((member hasFlag (OVERRIDE | ABSOVERRIDE)) &&
	    (clazz.info.baseClasses.tail forall (bc => member.overriddenSymbol(bc) == NoSymbol))) {
	  System.out.println(clazz.info.baseClasses.tail);//debug
	  unit.error(member.pos, member.toString() + " overrides nothing");
	  member resetFlag OVERRIDE
	}
    }

  // Basetype Checking --------------------------------------------------------

    /** 1. Check that later type instances in the base-type sequence
     *     are subtypes of earlier type instances of the same trait.
     *  2. Check that case classes do not inherit from case classes.
     *  3. Check that at most one base type is a case-class.
     */
    private def validateBaseTypes(clazz: Symbol): unit = {
      val seenTypes = new Array[Type](clazz.info.closure.length);
      var seenCaseClass = if (clazz hasFlag CASE) clazz else NoSymbol;

      def validateTypes(tps: List[Type], includeSuper: boolean): unit = {
	if (!tps.isEmpty) {
	  for (val tp <- tps.tail.reverse) validateType(tp, false);
	  if (includeSuper) validateType(tps.head, true);
	}
      }

      def validateType(tp: Type, includeSuper: boolean): unit = {
	val baseClass = tp.symbol;
	if (baseClass.isClass) {
	  val index = clazz.info.closurePos(baseClass);
	  if (index >= 0) {
	    if (seenTypes(index) != null && !(seenTypes(index) <:< tp))
	      unit.error(clazz.pos, "illegal inheritance;\n " + clazz +
			 " inherits different type instances of " + baseClass +
			 ":\n" + tp + " and " + seenTypes(index));
	    seenTypes(index) = tp;
	    // check that case classes do not inherit from case classes
	    if (baseClass hasFlag CASE) {
	      if (seenCaseClass != NoSymbol && seenCaseClass != baseClass)
		unit.error(clazz.pos, "illegal combination of case " +
			   seenCaseClass + " and case " + baseClass + " in one object");
	      seenCaseClass = baseClass
	    }
	  }
	  validateTypes(tp.parents, includeSuper);
	}
      }

      validateTypes(clazz.info.parents, true);
    }

  // Variance Checking --------------------------------------------------------

    private val ContraVariance = -1;
    private val NoVariance = 0;
    private val CoVariance = 1;
    private val AnyVariance = 2;

    /** Check variance of type variables in this type
     */
    private def validateVariance(base: Symbol, all: Type, variance: int): unit = {

      def varianceString(variance: int): String =
	if (variance == 1) "covariant"
	else if (variance == -1) "contravariant"
	else "invariant";

      def relativeVariance(tvar: Symbol): int = {
	val clazz = tvar.owner;
	var sym = base;
	var state = CoVariance;
	while (sym != clazz && state != AnyVariance) {
	  //System.out.println("flip: " + sym + " " + sym.isParameter());//DEBUG
	  if ((sym hasFlag PARAM) && !sym.owner.isConstructor) state = -state;
	  else if (!sym.owner.isClass) state = AnyVariance;
	  else if (sym.isAliasType) state = NoVariance;
	  sym = sym.owner;
	}
	state
      }

      def validateVariance(tp: Type, variance: int): unit = tp match {
	case ErrorType => ;
	case WildcardType => ;
	case NoType => ;
	case NoPrefix => ;
	case ThisType(_) => ;
	case ConstantType(_, _) => ;
	case SingleType(pre, sym) =>
	  validateVariance(pre, variance)
	case TypeRef(pre, sym, args) =>
	  if (sym.variance != NoVariance) {
	    val v = relativeVariance(sym);
	    if (v != AnyVariance && sym.variance != v * variance) {
	      //System.out.println("relativeVariance(" + base + "," + sym + ") = " + v);//DEBUG
	      unit.error(base.pos,
			 varianceString(sym.variance) + " " + sym +
			 " occurs in " + varianceString(v * variance) +
			 " position in type " + all + " of " + base);
	    }
	  }
	  validateVariance(pre, variance);
	  validateVarianceArgs(args, variance, sym.typeParams);
	case ClassInfoType(parents, decls, symbol) =>
	  validateVariances(parents, variance);
	case RefinedType(parents, decls) =>
	  validateVariances(parents, variance);
	case TypeBounds(lo, hi) =>
	  validateVariance(lo, -variance);
	  validateVariance(hi, variance);
	case MethodType(formals, result) =>
	  validateVariance(result, variance);
	case PolyType(tparams, result) =>
	  validateVariance(result, variance);
      }

      def validateVariances(tps: List[Type], variance: int): unit =
	tps foreach (tp => validateVariance(tp, variance));

      def validateVarianceArgs(tps: List[Type], variance: int, tparams: List[Symbol]): unit =
	(tps zip tparams) foreach {
	  case Pair(tp, tparam) => validateVariance(tp, variance * tparam.variance)
	}

      validateVariance(all, variance)
    }

// Forward reference checking ---------------------------------------------------

    class LevelInfo(val outer: LevelInfo) {
      val scope: Scope = if (outer == null) new Scope() else new Scope(outer.scope);
      var maxindex: int = Integer.MIN_VALUE;
      var refpos: int = _;
      var refsym: Symbol = _;
    }

    private var currentLevel: LevelInfo = null;
    private val symIndex = new HashMap[Symbol, int];

    private def pushLevel(): unit =
      currentLevel = new LevelInfo(currentLevel);

    private def popLevel(): unit =
      currentLevel = currentLevel.outer;

    private def enterSyms(stats: List[Tree]): unit = {
      var index = -1;
      for (val stat <- stats) {
	index = index + 1;
	stat match {
          case ClassDef(_, _, _, _, _) | DefDef(_, _, _, _, _, _) if (stat.symbol.isLocal) =>
	    currentLevel.scope.enter(newScopeEntry(stat.symbol, currentLevel.scope));
	    symIndex(stat.symbol) = index;
          case _ =>
	}
      }
    }

    private def enterReference(pos: int, sym: Symbol): unit =
      if (sym.isLocal && sym.isTerm) {
	val e = currentLevel.scope.lookupEntry(sym.name);
	if (e != null && sym == e.sym) {
          var l = currentLevel;
          while (l.scope != e.owner) l = l.outer;
	  val symindex = symIndex(sym);
	  if (l.maxindex < symindex) {
	    l.refpos = pos;
	    l.refsym = sym;
	    l.maxindex = symindex;
	  }
	}
      }

// Adding synthetic methods --------------------------------------------------------------

    private def addSyntheticMethods(templ: Template, clazz: Symbol): Template = {

      def hasImplementation(name: Name): boolean = {
	val sym = clazz.info.nonPrivateMember(name);
	sym.isTerm &&
	(sym.owner == clazz ||
	 !(ObjectClass isSubClass sym.owner) && !(sym hasFlag DEFERRED));
      }

      def syntheticMethod(name: Name, flags: int, tpe: Type) = {
	val method = clazz.newMethod(clazz.pos, name) setFlag (flags | OVERRIDE) setInfo tpe;
	clazz.info.decls.enter(method);
	method
      }

      def caseElementMethod: Tree = {
	val method = syntheticMethod(
	  nme.caseElement, FINAL, MethodType(List(IntClass.tpe), AnyClass.tpe));
	val caseFields = clazz.caseFieldAccessors map gen.mkRef;
	typed(
	  DefDef(method, vparamss =>
	    if (caseFields.isEmpty) Literal(null)
	    else {
	      var i = caseFields.length;
	      var cases = List(CaseDef(Ident(nme.WILDCARD), EmptyTree, Literal(null)));
	      for (val field <- caseFields.reverse) {
		i = i - 1; cases = CaseDef(Literal(i), EmptyTree, field) :: cases
	      }
	      Match(Ident(vparamss.head.head), cases)
	    }))
      }

      def caseArityMethod: Tree = {
	val method = syntheticMethod(nme.caseArity, FINAL, PolyType(List(), IntClass.tpe));
	typed(DefDef(method, vparamss => Literal(clazz.caseFieldAccessors.length)))
      }

      def caseNameMethod: Tree = {
	val method = syntheticMethod(nme.caseName, FINAL, PolyType(List(), StringClass.tpe));
	typed(DefDef(method, vparamss => Literal(clazz.name.decode)))
      }

      def moduleToStringMethod: Tree = {
	val method = syntheticMethod(nme.toString_, FINAL, MethodType(List(), StringClass.tpe));
	typed(DefDef(method, vparamss => Literal(clazz.name.decode)))
      }

      def tagMethod: Tree = {
	val method = syntheticMethod(nme.tag, FINAL, MethodType(List(), IntClass.tpe));
	typed(DefDef(method, vparamss => Literal(clazz.tag)))
      }

      def forwardingMethod(name: Name): Tree = {
	val target = getMember(CaseOpsModule, "_" + name);
	val method = syntheticMethod(
	  name, 0, MethodType(target.tpe.paramTypes.tail, target.tpe.resultType));
	typed(DefDef(method, vparamss =>
	  Apply(gen.mkRef(target), This(clazz) :: (vparamss.head map Ident))));
      }

      def readResolveMethod: Tree = {
	// !!! the synthetic method "readResolve" should be private,
	// but then it is renamed !!!
	val method = syntheticMethod(nme.readResolve, PROTECTED, MethodType(List(), ObjectClass.tpe));
	typed(DefDef(method, vparamss => gen.mkRef(clazz.sourceModule)))
      }

      val ts = new ListBuffer[Tree];
      if (clazz hasFlag CASE) {
	ts += tagMethod;
	if (clazz.isModuleClass) {
	  ts += moduleToStringMethod;
	  if (clazz.isSubClass(SerializableClass)) {
	    // If you serialize a singleton and then deserialize it twice,
	    // you will have two instances of your singleton, unless you implement
	    // the readResolve() method (see http://www.javaworld.com/javaworld/
	    // jw-04-2003/jw-0425-designpatterns_p.html)
	    ts += readResolveMethod;
	  }
	} else {
	  ts += caseElementMethod;
	  ts += caseArityMethod;
	  ts += caseNameMethod;
	  if (!hasImplementation(nme.equals_)) ts += forwardingMethod(nme.equals_);
	  if (!hasImplementation(nme.hashCode_)) ts += forwardingMethod(nme.hashCode_);
	  if (!hasImplementation(nme.toString_)) ts += forwardingMethod(nme.toString_);
	}
      }
      val synthetics = ts.toList;
      copy.Template(
	templ, templ.parents, if (synthetics.isEmpty) templ.body else templ.body ::: synthetics)
    }

// Transformation ------------------------------------------------------------

    override def transformStats(stats: List[Tree], exprOwner: Symbol): List[Tree] = {
      pushLevel();
      enterSyms(stats);
      var index = -1;
      val stats1 = stats flatMap { stat => index = index + 1; transformStat(stat, index) }
      popLevel();
      stats1
    }

    def transformStat(tree: Tree, index: int): List[Tree] = tree match {
      case ModuleDef(_, name, impl) =>
	val sym = tree.symbol;
        //val localTyper = typer.atOwner(currentOwner);
	val cdef = typed(ClassDef(sym.moduleClass, impl));
	if (sym.isStatic) List(transform(cdef))
	else {
          val moduleType = sym.tpe;

          // var m$: T = null; or, if class member: local var m$: T = _;
          val mvar = currentOwner.newVariable(sym.pos, name.toString() + "$") setInfo moduleType;
          if (currentOwner.isClass) {
	    mvar setFlag (PRIVATE | LOCAL | SYNTHETIC);
	    sym.owner.info.decls.enter(mvar);
	  }
          val vdef = typed(ValDef(mvar, if (sym.isLocal) Literal(null) else EmptyTree));

          // def m: T = { if (m$ == null) m$ = new m$class; m$ }
          sym.setFlag(METHOD | STABLE);
          sym.setInfo(PolyType(List(), moduleType));
          val ddef = typed(
            DefDef(sym, vparamss =>
              Block(
		List(
		  If(
                    Apply(Select(Ident(mvar), nme.EQ), List(Literal(null))),
                    Assign(Ident(mvar), New(TypeTree(moduleType), List(List()))),
                    EmptyTree)),
		Ident(mvar))));
          transformTrees(List(cdef, vdef, ddef))
	}

      case ValDef(_, _, _, _) =>
	val tree1 = transform(tree); // important to do before forward reference check
	//todo: handle variables
	if (tree.symbol.isLocal && index <= currentLevel.maxindex) {
	  if (settings.debug.value) System.out.println(currentLevel.refsym);
	  unit.error(currentLevel.refpos, "forward reference extends over definition of " + tree.symbol);
	}
	List(tree1)

      case _ =>
	List(transform(tree))
    }

    override def transform(tree: Tree): Tree = try {

      /* Convert a reference of a case factory to a new of the class it produces. */
      def toConstructor: Tree = {
	var tpe = tree.tpe;
	while (!tpe.symbol.isClass) tpe = tpe.resultType;
	assert(tpe.symbol hasFlag CASE);
	typedOperator(atPos(tree.pos)(Select(New(TypeTree(tpe)), tpe.symbol.primaryConstructor)))
      }

      /* Check whether argument types conform to bounds of type parameters */
      def checkBounds(tparams: List[Symbol], argtps: List[Type]): unit = try {
	infer.checkBounds(tree.pos, tparams, argtps, "");
      } catch {
	case ex: TypeError => unit.error(tree.pos, ex.getMessage());
      }

      val sym = tree.symbol;
      var result = tree;
      tree match {
	case ClassDef(mods, name, tparams, tpe, impl) =>
	  validateVariance(sym, sym.info, CoVariance);
	  validateVariance(sym, sym.typeOfThis, CoVariance);
	  result = copy.ClassDef(
	    tree, mods, name, tparams, tpe, addSyntheticMethods(impl, tree.symbol))

	case DefDef(_, _, _, _, _, _) =>
	  validateVariance(sym, sym.tpe, CoVariance);

	case ValDef(_, _, _, _) =>
	  validateVariance(sym, sym.tpe, if ((sym.flags & MUTABLE) != 0) NoVariance else CoVariance);

	case AbsTypeDef(_, _, _, _) =>
	  validateVariance(sym, sym.info, CoVariance);

	case AliasTypeDef(_, _, _, _) =>
	  validateVariance(sym, sym.info, CoVariance);

	case Template(_, _) =>
	  validateBaseTypes(currentOwner);
	  checkAllOverrides(currentOwner);

	case TypeTree() =>
	  new TypeTraverser {
	    def traverse(tp: Type) = tp match {
	      case TypeRef(pre, sym, args) => checkBounds(sym.typeParams, args); this
	      case _ => this
	    }
	  } traverse tree.tpe

	case TypeApply(fn, args) =>
	  checkBounds(fn.tpe.typeParams, args map (.tpe));

	case Function(vparams, body) =>
	  /*  Transform a function node (x_1,...,x_n) => body of type FunctionN[T_1, .., T_N, R] to
	   *
	   *    class $anon() extends Object() with FunctionN[T_1, .., T_N, R] with ScalaObject {
	   *      def apply(x_1: T_1, ..., x_N: T_n): R = body
	   *    }
	   *    new $anon()
	   *
	   *  transform a function node (x => body) of type PartialFunction[T, R] where
	   *    body = x match { case P_i if G_i => E_i }_i=1..n
	   *  to:
	   *
	   *    class $anon() extends Object() with PartialFunction[T, R] with ScalaObject {
	   *      def apply(x: T): R = body;
	   *      def isDefinedAt(x: T): boolean = x match {
	   *        case P_1 if G_1 => true
	   *        ...
	   *        case P_n if G_n => true
	   *        case _ => false
	   *      }
	   *    }
	   *    new $anon()
	   *
	   *  However, if one of the patterns P_i if G_i is a default pattern, generate instead
	   *
	   *      def isDefinedAt(x: T): boolean = true
	   */
	  result = {
	    val anonClass =
	      currentOwner.newAnonymousFunctionClass(tree.pos) setFlag (FINAL | SYNTHETIC);
	    anonClass setInfo ClassInfoType(
	      List(ObjectClass.tpe, tree.tpe, ScalaObjectClass.tpe), new Scope(), anonClass);
	    val targs = tree.tpe.typeArgs;
	    val applyMethod = anonClass.newMethod(tree.pos, nme.apply)
	      setFlag FINAL setInfo MethodType(targs.init, targs.last);
	    anonClass.info.decls enter applyMethod;
	    for (val vparam <- vparams)
	      vparam.symbol.owner = applyMethod;
	    var members = List(
	      DefDef(FINAL, nme.apply, List(), List(vparams), TypeTree(targs.last), body)
		setSymbol applyMethod);
	    if (tree.tpe.symbol == PartialFunctionClass) {
	      val isDefinedAtMethod = anonClass.newMethod(tree.pos, nme.isDefinedAt)
		setFlag FINAL setInfo MethodType(targs.init, BooleanClass.tpe);
	      anonClass.info.decls enter isDefinedAtMethod;
	      def idbody(idparam: Symbol) = body match {
		case Match(_, cases) =>
		  val substParam = new TreeSymSubstituter(List(vparams.head.symbol), List(idparam));
		  def transformCase(cdef: CaseDef): CaseDef =
		    CaseDef(substParam(cdef.pat), substParam(cdef.guard), Literal(true));
		  def isDefaultCase(cdef: CaseDef) = cdef match {
		    case CaseDef(Ident(nme.WILDCARD), EmptyTree, _) => true
		    case CaseDef(Bind(_, Ident(nme.WILDCARD)), EmptyTree, _) => true
		    case _ => false
		  }
		  if (cases exists isDefaultCase) Literal(true)
		  else Match(
		    Ident(idparam),
		    (cases map transformCase) :::
		      List(CaseDef(Ident(nme.WILDCARD), EmptyTree, Literal(false))))
	      }
	      members = DefDef(isDefinedAtMethod, vparamss => idbody(vparamss.head.head)) :: members;
	    }
	    typed(
	      atPos(tree.pos)(
		Block(
		  List(ClassDef(anonClass, List(List()), List(List()), members)),
		  New(TypeTree(anonClass.tpe), List(List())))))
	  }

	case New(tpt) =>
	  enterReference(tree.pos, tpt.tpe.symbol);

	case Ident(name) =>
	  if (sym.isMethod && sym.hasFlag(CASE))
	    result = toConstructor
	  else if (name != nme.WILDCARD && name != nme.WILDCARD_STAR.toTypeName) {
	    sym setFlag ACCESSED;
	    assert(sym != NoSymbol, tree);//debug
	    enterReference(tree.pos, sym);
	  }

	case Select(qual, name) =>
	  if (sym.isMethod && sym.hasFlag(CASE))
	    result = toConstructor
	  else {
	    sym setFlag ACCESSED;
	    if (!treeInfo.isSelf(qual, currentOwner.enclClass)) sym.flags = sym.flags | SELECTOR;
	    if (sym hasFlag DEFERRED) {
	      qual match {
		case Super(qualifier, mixin) =>
		  val base = currentOwner.enclClass;
		  val member = sym.overridingSymbol(base);
		  if (mixin != nme.EMPTY.toTypeName || member == NoSymbol ||
		      !((member hasFlag ABSOVERRIDE) && member.isIncompleteIn(base)))
		    unit.error(tree.pos, "symbol accessed from super may not be abstract");
		case _ =>
	      }
	    }
	  }

	case _ =>
      }
      super.transform(result)
    } catch {
      case ex: TypeError =>
	if (settings.debug.value) ex.printStackTrace();
	unit.error(tree.pos, ex.getMessage());
	tree
    }
  }
}