summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/ExplicitOuterClassesPhase.java
blob: 4f0ed688f595dfe33c4f209bf9d32741ca42d180 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.transformer;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

import scalac.Global;
import scalac.Phase;
import scalac.PhaseDescriptor;
import scalac.Unit;
import scalac.ast.GenTransformer;
import scalac.ast.Tree;
import scalac.ast.Tree.Ident;
import scalac.ast.Tree.Template;
import scalac.symtab.Modifiers;
import scalac.symtab.Scope;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.util.Debug;
import scalac.util.Name;
import scalac.util.Names;

/**
 * This phase does the following:
 *
 * - In every nested class, adds to each of its constructor a new
 *   value parameter that contains a link to the outer class.
 *
 * - In every nested type, adds to each of its constructor a new type
 *   parameter for every type parameter appearing in outer types.
 *
 * - In every class, adds a forwarding "super" method for every method
 *   that is accessed via "super" in a nested class.
 *
 * - Replaces all prefixes of TypeRefs by localThisTypes.
 *
 * - Adds all missing qualifiers.
 */
// !!! needs to be cleaned
// !!! create outer fields lazyly
public class ExplicitOuterClassesPhase extends Phase {

    //########################################################################
    // Private Fields

    /** A map from class symbols to class contexts */
    private final Map/*<Symbol,ClassContext>*/ classes = new HashMap();

    /** A map from constructor symbols to type contexts */
    private final Map/*<Symbol,TypeContext>*/ contexts = new HashMap();

    //########################################################################
    // Public Constructors

    /** Initializes this instance. */
    public ExplicitOuterClassesPhase(Global global,PhaseDescriptor descriptor){
        super(global, descriptor);
    }

    //########################################################################
    // Public Methods

    /** Applies this phase to the given compilation units. */
    public void apply(Unit[] units) {
        treeTransformer.apply(units);
    }

    private boolean show = false; // !!!

    /** Applies this phase to the given type for the given symbol. */
    public Type transformInfo(Symbol symbol, Type type) {
        if (show && !symbol.isPackageClass()) System.out.println("!!! <<< transformInfo - symbol: " + Debug.show(symbol));
        if (show && !symbol.isPackageClass()) System.out.println("!!! <<< transformInfo - type  : " + Debug.show(type));
        if (symbol.isPackageClass()) return type; // !!!
        TypeContext context = getTypeContextFor(symbol);
        if (symbol.isConstructor() && symbol.constructorClass().isClassType()) { // !!! isClassType -> isClass ?
            Symbol clasz = symbol.constructorClass();
            Symbol[] tparams = type.typeParams();
            Symbol[] vparams = type.valueParams();
            Type result = type.resultType();
            result = context.transformer.apply(result);
            if (context.vlink != null) {
                vparams = Symbol.cloneArray(1, vparams);
                vparams[0] = context.vlink;
            }
            Type prefix = clasz.owner().thisType();
            Type[] args = Symbol.type(tparams);
            // !!! use getNewTypeArgs ?
            Type self = Type.typeRef(prefix, clasz, args);
            Type s = self;
            self = context.transformer.apply(self);
            tparams = Type.symbol(self.typeArgs());
            for (int i = 0; i < tparams.length; i++)
                assert tparams[i].isParameter() && tparams[i].owner() == symbol:
                    Debug.show(symbol, clasz, self);
            type = Type.MethodType(vparams, result);
            if (tparams.length != 0) type = Type.PolyType(tparams, type);
        } else {
            Type t = type;
            type = context.transformer.apply(type);
            assert type != null: Debug.show(symbol) + " -- " + t;
        }
        if (show && !symbol.isPackageClass()) System.out.println("!!! >>> transformInfo - symbol: " + Debug.show(symbol));
        if (show && !symbol.isPackageClass()) System.out.println("!!! >>> transformInfo - type  : " + Debug.show(type));
        return type;
    }

    //########################################################################
    // Private Methods

    /** Returns the class context for the given symbol. */
    private ClassContext getClassContext(Symbol clasz, TypeContext[] outers) {
        assert clasz.isClassType(): Debug.show(clasz);
        ClassContext context = (ClassContext)classes.get(clasz);
        if (context == null) {
            context = createClassContext(clasz, outers);
            classes.put(clasz, context);
        }
        return context;
    }

    /** Creates the context for the given class. */
    private ClassContext createClassContext(Symbol clasz, TypeContext[] outers) {
        // create outer value link
        Symbol vfield = null;
        if (outers.length > 0 && (outers[0].vlink != null || !outers[0].isStable)) {
            int index = 0;
            while (outers[index].isStable) index++;
            int vflags = Modifiers.SYNTHETIC | Modifiers.PRIVATE | Modifiers.STABLE;
            vfield = clasz.newField(clasz.pos, vflags, Names.OUTER(clasz));
            vfield.setInfo(outers[index].clasz.thisType());
            clasz.members().enter(vfield);
        }
        return new ClassContext(vfield);
    }

    /** Returns the type context for the given symbol. */
    private TypeContext getTypeContextFor(Symbol symbol) {
        while (!symbol.isClassType() && !(symbol.isConstructor() && symbol.constructorClass().isClassType())) // !!! isClassType -> isClass ?
            symbol = symbol.owner();
        if (symbol.isClassType())
            symbol = symbol.primaryConstructor();
        TypeContext context = (TypeContext)contexts.get(symbol);
        if (context == null) {
            context = createTypeContext(symbol);
            contexts.put(symbol, context);
        }
        return context;
    }

    /** Creates the context for the given constructor. */
    private TypeContext createTypeContext(Symbol constructor) {
        Symbol clasz = constructor.constructorClass();

        // get outer contexts
        TypeContext[] outers;
        if (clasz.isRoot()) {
            outers = new TypeContext[0];
        } else {
            TypeContext outer = getTypeContextFor(clasz.owner());
            outers = new TypeContext[1 + outer.outers.length];
            outers[0] = outer;
            for (int i = 1; i < outers.length; i++)
                outers[i] = outer.outers[i - 1];
        }

        // create outer type links
        Symbol[] tlinks = new Symbol[outers.length];
        int tflags = Modifiers.PARAM | Modifiers.COVARIANT | Modifiers.SYNTHETIC | Modifiers.STABLE;
        for (int i = 0; i < outers.length; i++) {
            if (outers[i].isStable) continue;
            Name tname = Names.OUTER(constructor, outers[i].clasz);
            tlinks[i] = constructor.newTParam(
                constructor.pos, tflags, tname, outers[i].clasz.type());
        }

        // create outer value link
        Symbol vlink = null;
        ClassContext context = null;
        if (outers.length > 0 && (outers[0].vlink != null || !outers[0].isStable)) {
            int index = 0;
            while (outers[index].isStable) index++;
            int vflags = Modifiers.SYNTHETIC;
            Name vname = Names.OUTER(constructor);
            vlink = constructor.newVParam(constructor.pos, vflags, vname);
            vlink.setInfo(outers[index].clasz.thisType());
            context = getClassContext(clasz, outers);
        }

        // create new type parameters
        Map tparams = new HashMap();
        for (int o = 0; o < outers.length; o++) {
            Symbol[] oldtparams = outers[o].oldtparams;
            for (int i = 0; i < oldtparams.length; i++) {
                Symbol oldtparam = oldtparams[i];
                Symbol newtparam = oldtparam.cloneSymbol(constructor);
                newtparam.name = Names.OUTER(constructor, oldtparam);
                tparams.put(oldtparam, newtparam.type());
            }
        }
        // !!! duplicated code
        Symbol[] oldtparams = constructor.typeParams();
        for (int i = 0; i < oldtparams.length; i++) {
            Symbol oldtparam = oldtparams[i];
            Symbol newtparam = oldtparam;
            tparams.put(oldtparam, newtparam.type());
        }

        return new TypeContext(clasz, outers, tlinks, vlink, context, constructor.typeParams(), tparams);
    }


    /** !!! */
    // !!! where is not used
    // !!! prefix is an old type
    // !!! args are old types
    // !!! returns old types
    private Type[] getNewArgsOf(TypeContext where, Type prefix, Symbol clasz, Type[] args) {
        TypeContext context = getTypeContextFor(clasz);
        int vlinks = 0; // !!!
        for (int i = 0; i < context.outers.length; i++)
            if (!context.outers[i].isStable) vlinks++;
        Type[] types = new Type[context.transformer.tparams.size() + vlinks];
        int p = types.length;
        for (int i = args.length; 0 < i; ) types[--p] = args[--i];
        for (int o = 0; o < context.outers.length - 1; o++) {
            if (!context.outers[o].isStable)
                types[--p] = prefix;
            else if (context.outers[o].clasz.isPackageClass()) break;
            Type base = prefix.baseType(context.outers[o].clasz);
            assert base.symbol() == context.outers[o].clasz:
                prefix + " -- " + Debug.show(clasz) + " -- " + context.outers[o].clasz + " -- " + base;
            prefix = base.prefix();
            args = base.typeArgs();
            for (int i = args.length; 0 < i; ) types[--p] = args[--i];
        }
        // !!! assert p == 0: p;
        for (int i = 0; i < types.length; i++) { // !!!
            assert types[i] != null:
                "\nprefix = " + prefix +
                "\nclasz  = " + Debug.show(clasz) +
                "\nargs   = " + Debug.show(args) +
                "\ntypes  = " + Debug.show(types) +
                "\ncontext= " + context;
        }
        return types;
    }

    //########################################################################
    // Private Class - Class context

    private class ClassContext {

        /** The outer value field (null if all outer contexts are stable) */
        private final Symbol vfield;

        /** !!! */
        public ClassContext(Symbol vfield) {
            this.vfield = vfield;
        }

    }

    //########################################################################
    // Private Class - Type transformer context

    private class TypeContext {

        /** The context class */
        private final Symbol clasz;
        /** Is this context class stable? */
        private final boolean isStable;
        /** The outer contexts (from innermost to outermost) */
        private final TypeContext[] outers;
        /** The outer type links (null for stable outer contexts) */
        private final Symbol[] tlinks;
        /** The outer value link (null if all outer contexts are stable) */
        private final Symbol vlink;
        /** !!! */
        private final ClassContext context;
        /** The old type parameters of the context class */
        private Symbol[] oldtparams;

        /** !!! */
        private TypeTransformer transformer; // !!! type

        /** !!! */
        public TypeContext(Symbol clasz, TypeContext[] outers, Symbol[] tlinks, Symbol vlink, ClassContext context, Symbol[] oldtparams, Map tparams) {
            this.clasz = clasz;
            this.isStable = clasz.isPackageClass();
            this.outers = outers;
            this.tlinks = tlinks;
            this.vlink = vlink;
            this.context = context;
            this.oldtparams = oldtparams;
            this.transformer = new TypeTransformer(this, tparams);
        }

        /** !!! */
        public Type getTypeLink(int level) {
            if (outers[level].clasz.isPackageClass()) return Type.NoPrefix;
            assert tlinks[level] != null: level + " - " + Debug.show(clasz);
            return tlinks[level].type();
        }

        public String toString() {
            return
                "\nclasz    = " + Debug.show(clasz) +
                "\nisStable = " + isStable +
                "\ntlinks   = " + Debug.show(tlinks) +
                "\nvlink    = " + Debug.show(vlink) +
                "\noldparams= " + Debug.show(oldtparams) +
                "\ntparams  = " + Debug.show(transformer.tparams) +
                (outers.length > 0
                    ? ("\nouter    : " + "\n" + outers[0])
                    : "");
        }

    }

    //########################################################################
    // Private Class - Type transformer

    /** The type transformer */
    private final class TypeTransformer extends Type.MapOnlyTypes {

        private TypeContext context;
        private Map/*<Symbol,Type>*/ tparams;

        public TypeTransformer(TypeContext context, Map tparams) {
            this.context = context;
            this.tparams = tparams;
        }

        public Type apply(Type type) {
            switch (type) {
            case TypeRef(Type prefix, Symbol symbol, Type[] args):
                if (symbol.isParameter() && symbol.owner().isConstructor()) {
                    assert prefix == Type.NoPrefix: type;
                    assert args.length == 0: type;
                    Object value = tparams.get(symbol);
                    return value != null ? (Type)value : type;
                }
                if (symbol.isClass()) {
                    args = map(getNewArgsOf(context, prefix, symbol, args));
                    prefix = Type.NoPrefix;
                    return Type.typeRef(prefix, symbol, args);
                }
                if (symbol.isPackageClass()) {
                    args = Type.EMPTY_ARRAY;
                    prefix = Type.NoPrefix;
                    return Type.typeRef(prefix, symbol, args);
                }
                return Type.typeRef(apply(prefix), symbol, map(args));
            case SingleType(Type prefix, Symbol symbol):
                return Type.singleType(apply(prefix), symbol);
            case ThisType(Symbol clasz):
                if (clasz == context.clasz) return type;
                for (int i = 0; i < context.outers.length; i++)
                    if (clasz == context.outers[i].clasz)
                        return context.getTypeLink(i);
                throw Debug.abort("illegal ThisType", type);
            case CompoundType(Type[] parents, Scope members):
                // !!! this case should not be needed
                return Type.compoundType(map(parents), members, type.symbol());
            default:
                return map(type);
            }
        }
    }

    //########################################################################
    // Private Class - Tree transformer

    /** The tree transformer */
    private final GenTransformer treeTransformer = new GenTransformer(global) {

        /** The current context */
        private Context context;

        /** Transforms the given tree. */
        public Tree transform(Tree tree) {
            switch (tree) {

            case ClassDef(_, _, _, _, _, Template impl):
                Symbol clasz = tree.symbol();
                context = new Context(context, clasz, new HashMap(), new HashMap());
                Tree[] parents = transform(impl.parents);
                Tree[] body = transform(impl.body);
                body = Tree.concat(body, genAccessMethods(false));
                body = Tree.concat(body, genAccessMethods(true));
                if (context.context.vlink != null) {
                    body = Tree.cloneArray(1, body);
                    body[0] = gen.ValDef(
                        context.context.context.vfield,
                        gen.Ident(
                            context.context.context.vfield.pos,
                            context.context.vlink));
                }
                context = context.outer;
                return gen.ClassDef(clasz, parents, impl.symbol(), body);

            case DefDef(_, _, _, _, _, Tree rhs):
                Symbol method = tree.symbol();
                Context backup = context;
                if (method.isConstructor())
                    context = context.getConstructorContext(method);
                context.method = method;
                rhs = transform(rhs);
                context.method = null;
                context = backup;
                return gen.DefDef(method, rhs);

                // !!!
            case AbsTypeDef(_, _, _, _):
            case AliasTypeDef(_, _, _, _):
                // eliminate // !!!
                return Tree.Empty;

            case Typed(Tree expr, Tree tpe):
                // eliminate // !!!
                return transform(expr);

            case Apply(Tree vfun, Tree[] vargs):
                switch (vfun) {
                case TypeApply(Tree tfun, Tree[] targs):
                    if (!tfun.symbol().isConstructor()) break;
                    return transform(tree, vargs, vfun, targs, tfun);
                default:
                    if (!vfun.symbol().isConstructor()) break;
                    return transform(tree, vargs, vfun, Tree.EMPTY_ARRAY,vfun);
                }
                return super.transform(tree);

            case This(_):
                return genOuterRef(tree.pos, tree.symbol());
//                 Symbol clasz = tree.symbol();
//                 return clasz.isRoot() ? tree : genOuterRef(tree.pos, clasz);

            case Select(Tree qualifier, _):
                Symbol symbol = tree.symbol();
                if (symbol.owner().isStaticOwner()) // !!! qualifier ignored
                    return gen.mkGlobalRef(tree.pos, symbol);
                Symbol access;
                switch (qualifier) {
                case Super(_, _):
                    Symbol clasz = qualifier.symbol();
                    if (clasz == context.clasz) {
                        access = symbol;
                        qualifier = gen.Super(tree.pos, qualifier.symbol());
                    } else {
                        access = getAccessSymbol(symbol, clasz);
                        qualifier = genOuterRef(qualifier.pos, clasz);
                    }
                    break;
                default:
                    access = getAccessSymbol(symbol, null);
                    qualifier = transform(qualifier);
                    break;
                }
                tree = gen.Select(tree.pos, qualifier, access);
                if (access != symbol && !symbol.isMethod())
                    tree = gen.mkApply__(tree);
                return tree;

            case Ident(_):
                Symbol symbol = tree.symbol();
                Symbol owner = symbol.owner();
                if (owner.isClass()) {
                    // !!! A this node is missing here. This should
                    // never happen if all trees were correct.
                    Tree qualifier = genOuterRef(tree.pos, owner);
                    return gen.Select(qualifier, symbol);
                }
                if (owner.isPrimaryConstructor()) {
                    Symbol clasz = owner.constructorClass();
                    if (clasz != context.clasz) {
                        Tree qualifier = genOuterRef(tree.pos, clasz);
                        return gen.Select(qualifier, symbol);
                    }
                }
                return gen.Ident(tree.pos, symbol);

            case TypeTerm():
                Type type = context.context.transformer.apply(tree.getType());
                return gen.TypeTerm(tree.pos, type);

            default:
                return super.transform(tree);
            }
        }

        /* Add outer type and value arguments to constructor calls. */
        private Tree transform(Tree vapply, Tree[] vargs, Tree tapply,
            Tree[] targs, Tree tree)
        {
            Symbol symbol = tree.symbol();
            vargs = transform(vargs);
            switch (transform(tree)) {
            case Select(Tree qualifier, _):
                if (getTypeContextFor(symbol).vlink != null) {
                    vargs = Tree.cloneArray(1, vargs);
                    vargs[0] = qualifier;

                    Type prefix;
                    // !!! this is done to avoid types like "vlink.type"
                    switch (tree) {
                    case Select(Tree qualifier1, _):
                        prefix = qualifier1.getType();
                        break;
                    default:
                        throw Debug.abort("illegal case", tree);
                    }
                    Type[] newtargs = getNewArgsOf(context.context, prefix, symbol, Tree.typeOf(targs));
                    targs = Tree.cloneArray(newtargs.length - targs.length, targs);
                    for (int i = 0; i < newtargs.length; i++)
                        targs[i] = gen.mkType(tapply.pos, newtargs[i]);

                }
            }
            targs = transform(targs);
            tree = gen.Ident(tree.pos, symbol);
            if (targs.length != 0) tree = gen.TypeApply(tapply.pos,tree,targs);
            return gen.Apply(vapply.pos, tree, vargs);
        }

        /**
         * Returns the symbol to access the specified member from the
         * current context. If "svper" is non null, the member is
         * selected from the superclass of the specified class. The
         * returned symbol may be the same as the given one.
         */
        private Symbol getAccessSymbol(Symbol member, Symbol svper) {
            if (member.isPublic() && svper == null) return member;
            Context context = this.context;
            for (; context != null; context = context.outer)
                if (svper != null
                    ? context.clasz == svper
                    : member.isPrivate()
                    ? context.clasz == member.owner()
                    // !!! This is incorrect without static access methods
                    : context.clasz.isSubClass(member.owner())) break;
            assert context != null: Debug.show(this.context, member);
            if (context == this.context) return member;
            Map table = svper != null ? context.supers : context.selfs;
            Symbol access = (Symbol)table.get(member);
            if (access == null) {
                // !!! generate static access methods ?
                Name name = Names.ACCESS(member, svper != null);
                access = context.clasz.newAccessMethod(context.clasz.pos,name);
                global.nextPhase();
                Type info = member.isMethod()
                    ? member.info().cloneType(member, access)
                    : Type.MethodType(Symbol.EMPTY_ARRAY, member.info());
                access.setInfo(info);
                global.prevPhase();
                table.put(member, access);
                context.clasz.nextInfo().members().enter(access);
                assert Debug.log("created access method: ", access);
            }
            return access;
        }

        /** Generates the trees of the access methods. */
        private Tree[] genAccessMethods(boolean withSuper) {
            Map table = withSuper ? context.supers : context.selfs;
            if (table.size() == 0) return Tree.EMPTY_ARRAY;
            Tree[] trees = new Tree[table.size()];
            Iterator entries = table.entrySet().iterator();
            for (int i = 0; i < trees.length; i++) {
                Map.Entry entry = (Map.Entry)entries.next();
                Symbol member = (Symbol)entry.getKey();
                Symbol access = (Symbol)entry.getValue();
                int pos = access.pos;
                Tree qualifier = withSuper
                    ? gen.Super(pos, context.clasz)
                    : gen.This(pos, context.clasz);
                Tree select = gen.Select(qualifier, member);
                Tree[] targs = gen.mkTypeRefs(pos, access.nextTypeParams());
                Tree[] vargs = gen.mkLocalRefs(pos, access.nextValueParams());
                Tree body = member.isMethod()
                    ? gen.mkApplyTV(select, targs, vargs)
                    : select;
                trees[i] = gen.DefDef(access, body);
            }
            return trees;
        }

        /** Returns a tree referencing the given outer class. */
        private Tree genOuterRef(int pos, Symbol clasz) {
            if (context.clasz == clasz) return gen.This(pos, clasz);
            TypeContext tcontext = null;
            for (int i = 0; i < context.context.outers.length; i++)
                if (context.context.outers[i].clasz == clasz)
                    tcontext = context.context.outers[i];
            assert tcontext != null: Debug.show(clasz, context.clasz);
            if (tcontext.isStable) {
                throw Debug.abort(Debug.show(clasz, context.clasz));
                /*
                if (!clasz.owner().isPackageClass()) {
                    Tree qualifier = genOuterRef(pos,tcontext.outers[0].clasz);
                    return gen.Select(pos, qualifier, clasz.module());
                } else {
                    assert clasz.owner().isPackageClass(): Debug.show(clasz);
                    return gen.Ident(pos, clasz.module());
                }
                */
            } else {
                assert context.context.vlink != null:
                    Debug.show(clasz, context.clasz);
                Tree tree = context.method == null || context.method.isConstructor()
                    ? gen.Ident(pos, context.context.vlink)
                    : gen.Select(gen.This(pos, context.clasz), context.context.context.vfield);
                Context context = this.context;
                while (true) {
                    context = context.outer;
                    assert context != null:
                        Debug.show(clasz, this.context.clasz);
                    while (context.context.isStable) {
                        context = context.outer;
                        assert context != null:
                            Debug.show(clasz, this.context.clasz);
                    }
                    if (context.clasz == clasz) return tree;
                    Symbol access = getAccessSymbol(context.context.context.vfield, null);
                    assert access != context.context.context.vfield: Debug.show(access) + " - " + Debug.show(this.context.clasz);
                    tree = gen.Apply(gen.Select(tree, access));
                }
            }
        }
    };

    //########################################################################
    // Private Class - Tree transformer context

    /** This class represents the tree transformation context. */
    private class Context {

        /** The outer context */
        public final Context outer;
        /** The current class symbol */
        public final Symbol clasz;
        /** The self access methods (maps members to accessors) */
        public final Map/*<Symbol,Symbol>*/ selfs;
        /** The super access methods (maps members to accessors) */
        public final Map/*<Symbol,Symbol>*/ supers;

        public final TypeContext context;

        /** !!! The current method */
        public Symbol method;

        /** Initializes this instance. */
        public Context(Context outer, Symbol symbol, Map selfs, Map supers){
            this.context = getTypeContextFor(symbol);
            this.outer = outer;
            this.clasz = symbol.constructorClass();
            this.selfs = selfs;
            this.supers = supers;
        }

        /** Returns a context for the given constructor. */
        public Context getConstructorContext(Symbol constructor) {
            assert constructor.constructorClass() == clasz;
            return new Context(outer, constructor, selfs, supers);
        }

    }

    //########################################################################
}