summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/ExplicitOuterClassesPhase.java
blob: 828bedbd07722dda77722aa5a473f861a2b94ad8 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    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.Symbol;
import scalac.symtab.TermSymbol;
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.
 */
public class ExplicitOuterClassesPhase extends Phase {

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

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

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

    /** Applies this phase to the given type for the given symbol. */
    public Type transformInfo(Symbol symbol, Type type) {
        if (symbol.isPackage()) return type;
        // if (!symbol.isJava() && symbol.isConstructor()) // !!!
        //System.out.println("!!! " + Debug.show(symbol) + ": " + type + " -> " + typeTransformer.apply(type));
        type = typeTransformer.apply(type);
        if (symbol.isJava()) return type;
        if (symbol.isConstructor()) {
            Symbol[] tparams = type.typeParams();
            Symbol[] vparams = type.valueParams();
            Type result = type.resultType();
            // Add outer value link
            if (hasOuterValueLink(symbol)) {
                Name name = Names.OUTER(symbol);
                Symbol vlink = new TermSymbol(symbol.pos, name, symbol, 0);
                vlink.setInfo(getOuterClass(symbol).typeOfThis()); // !!!
                vparams = Symbol.cloneArray(1, vparams);
                vparams[0] = vlink;
            }
            // Add outer type links
            if (hasOuterTypeLinks(symbol)) {
                Symbol[] oldtparams = getOuterClass(symbol).nextTypeParams();
                Symbol[] tlinks = Symbol.cloneArray(oldtparams);
                for (int i = 0; i < tlinks.length; i++) {
                    tlinks[i] = oldtparams[i].cloneSymbol(symbol);
                    tlinks[i].name = Names.OUTER(symbol, oldtparams[i]);
                }
                tparams = Symbol.concat(tlinks, tparams);
                result = Type.getSubst(oldtparams, tlinks, true).apply(result);
            }
            type = Type.MethodType(vparams, result);
            if (tparams.length != 0) type = Type.PolyType(tparams, type);
        } else {
            Symbol owner = symbol.owner().isConstructor()
                ? symbol.owner()
                : symbol.enclClass();
            //System.out.println("!!! debug2 = " + Debug.show(symbol) + " - " + Debug.show(owner) + " --- " + (owner == Symbol.NONE) + " -- #" + owner.name + "#");
            //if (onwer.isJava() && owner != Symbol.NONE && owner.name.length() > 0) // !!!
            if (owner.isType() || owner.isConstructor())
            type = getOuterTypeSubst(owner, true).apply(type);
        }

        /*

        String s1 = Debug.show(symbol);
        String s2 = symbol.info().toString();
        symbol.updateInfo(type);
        global.nextPhase();
        String s3 = type.toString();
        global.prevPhase();
        System.out.println("!!! symbol = " + s1);
        System.out.println("!!! type   = " + s2 + " -- " + System.identityHashCode(s2));
        System.out.println("!!! new    = " + s3 + " -- " + System.identityHashCode(s3));
        System.out.println("!!!");

        */

        return type;
    }

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

    //########################################################################
    // Private Methods - Outer class

    /** Returns the outer class of the given type or constructor. */
    private Symbol getOuterClass(Symbol symbol) {
        assert symbol.isType() || symbol.isConstructor(): Debug.show(symbol);
        return symbol.owner();
    }

    /** Has the given class or constructor an outer value link? */
    private boolean hasOuterValueLink(Symbol symbol) {
        assert symbol.isClass() || symbol.isConstructor(): Debug.show(symbol);
        return !symbol.isJava() && getOuterClass(symbol).isClass();
    }

    /** Returns the outer link of the given class or constructor. */
    private Symbol getOuterValueLink(Symbol symbol) {
        if (!hasOuterValueLink(symbol)) return Symbol.NONE;
        if (symbol.isClass()) symbol = symbol.primaryConstructor();
        return symbol.nextValueParams()[0];
    }

    /** Has the given type or constructor outer type links? */
    private boolean hasOuterTypeLinks(Symbol symbol) {
        assert symbol.isType() || symbol.isConstructor(): Debug.show(symbol);
        if (symbol.isJava()) return false;
        Symbol outer = getOuterClass(symbol);
        return outer.isType() && outer.nextTypeParams().length != 0;
    }

    /** Returns the type substitution for the given class or constructor. */
    private Type.Map getOuterTypeSubst(Symbol symbol, boolean update) {
        if (!hasOuterTypeLinks(symbol)) return Type.IdMap;
        Symbol[] oldtparams = getOuterTypeParams(symbol);
        Symbol[] newtparams = symbol.nextTypeParams();
        Symbol[] tlinks = new Symbol[oldtparams.length];
        for (int i = 0; i < tlinks.length; i++) tlinks[i] = newtparams[i];
        return Type.getSubst(oldtparams, tlinks, update);
    }

    /** Returns the outer type parameters of the given class or constructor. */
    private Symbol[] getOuterTypeParams(Symbol symbol) {
        Symbol outer = getOuterClass(symbol);
        Symbol[] tparams = Symbol.cloneArray(outer.nextTypeParams());
        for (int i = tparams.length; i != 0; outer = getOuterClass(outer)) {
            Symbol[] symbols = outer.typeParams();
            for (int j = symbols.length; j != 0; ) tparams[--i] = symbols[--j];
        }
        return tparams;
    }

    /**
     * Extracts from given prefix the outer type arguments for the
     * given class or constructor.
     */
    private Type[] getOuterTypeArgs(Type prefix, Symbol symbol) {
        if (!hasOuterTypeLinks(symbol)) return Type.EMPTY_ARRAY;
        Symbol outer = getOuterClass(symbol);
        global.nextPhase();
        Type[] targs = prefix.baseType(outer).widen().typeArgs();
        global.prevPhase();
        assert targs.length == outer.nextTypeParams().length:
            "\nsymbol = " + Debug.show(symbol) +
            "\nprefix = " + prefix;
        return targs;
    }

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

    /** The type transformer */
    private final Type.Map typeTransformer = new Type.MapOnlyTypes() {
        public Type apply(Type type) {
            switch (type) {
            case TypeRef(Type prefix, Symbol symbol, Type[] targs):
                if (!symbol.owner().isType()) break;
                prefix = apply(prefix);
                targs = map(targs);
                targs = Type.concat(getOuterTypeArgs(prefix, symbol), targs);
                if (symbol.isClassType()) {
                    // !!! add assertions ?
                    prefix = Type.localThisType;
                } else {
                    // !!! replace outer ThisTypes by SingleTypes to outer link
                }
                return Type.TypeRef(prefix, symbol, targs);
            }
            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());
                Tree[] parents = transform(impl.parents);
                Tree[] body = transform(impl.body);
                body = Tree.concat(body, genSuperMethods());
                context = context.outer;
                if (context != null) clasz.flags |= Modifiers.STATIC;
                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);
                else
                    context.inMethod = true;
                rhs = transform(rhs);
                context.inMethod = false;
                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());

            case Select(Tree qualifier, _):
                Symbol symbol = tree.symbol();
                switch (qualifier) {
                case Super(_, _):
                    Symbol clasz = qualifier.symbol();
                    if (clasz == context.clasz) {
                        qualifier = gen.Super(tree.pos, qualifier.symbol());
                    } else {
                        qualifier = genOuterRef(qualifier.pos, clasz);
                        symbol = getSuperMethod(clasz, symbol);
                    }
                    break;
                default:
                    qualifier = transform(qualifier);
                    break;
                }
                return gen.Select(tree.pos, qualifier, symbol);

            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 || context.inMethod) {
                        Tree qualifier = genOuterRef(tree.pos, clasz);
                        return gen.Select(qualifier, symbol);
                    }
                }
                return gen.Ident(tree.pos, symbol);

            case TypeTerm():
                Type type = typeTransformer.apply(tree.getType());
                if (context != null) type = context.subst.apply(type);
                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();
            targs = transform(targs);
            vargs = transform(vargs);
            switch (transform(tree)) {
            case Select(Tree qualifier, _):
                if (hasOuterValueLink(symbol)) {
                    vargs = Tree.cloneArray(1, vargs);
                    vargs[0] = qualifier;
                }
                Type[] types = getOuterTypeArgs(qualifier.getType(), symbol);
                if (types.length != 0) {
                    targs = Tree.cloneArray(types.length, targs);
                    for (int i = 0; i < types.length; i++)
                        targs[i] = gen.mkType(tapply.pos, types[i]);
                }
            }
            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 forwarding "super" method of the given class
         * that invokes the given method. If the symbol does not yet
         * exist, it is created and added to the class members.
         */
        private Symbol getSuperMethod(Symbol clasz, Symbol method) {
            Context context = this.context;
            for (; context.clasz != clasz; context = context.outer)
                assert context.outer != null : Debug.show(clasz);
            Symbol forward = (Symbol)context.supers.get(method);
            if (forward == null) {
                Name name = Names.SUPER(method);
                int flags = Modifiers.PRIVATE | Modifiers.FINAL;
                forward = new TermSymbol(method.pos, name, clasz, flags);
                forward.setInfo(method.nextType().cloneType(method, forward));
                context.supers.put(method, forward);
                clasz.nextInfo().members().enter(forward);
                assert Debug.log("created forwarding method: ", forward);
            }
            return forward;
        }

        /** Generates the trees of the forwarding "super" methods. */
        private Tree[] genSuperMethods() {
            if (context.supers.size() == 0) return Tree.EMPTY_ARRAY;
            Tree[] trees = new Tree[context.supers.size()];
            Iterator entries = context.supers.entrySet().iterator();
            for (int i = 0; i < trees.length; i++) {
                Map.Entry entry = (Map.Entry)entries.next();
                Symbol method = (Symbol)entry.getKey();
                Symbol forward = (Symbol)entry.getValue();
                int pos = forward.pos;
                Tree[] targs = gen.mkTypeRefs(pos, forward.nextTypeParams());
                Tree[] vargs = gen.mkRefs(pos, forward.nextValueParams());
                Tree fun = gen.Select(gen.Super(pos, context.clasz), method);
                trees[i] = gen.DefDef(forward, gen.mkApplyTV(fun,targs,vargs));
            }
            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);
            Tree tree = context.inMethod
                ? gen.Select(gen.This(pos, context.clasz), context.link)
                : gen.Ident(pos, context.link);
            for (Context context = this.context.outer;;context =context.outer){
                assert context != null: Debug.show(clasz);
                if (context.clasz == clasz) return tree;
                tree = gen.Select(tree, context.link);
            }
        }

    };

    //########################################################################
    // 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 super methods (maps invoked to forwarding methods) */
        public final Map/*<Symbol,Symbol>*/ supers;
        /** The outer type parameter substitution */
        public final Type.Map subst;
        /** The link to the outer class */
        public final Symbol link;
        /**  True if in a method of current class */
        public boolean inMethod;

        /** Initializes this instance. */
        public Context(Context outer, Symbol symbol, Map supers) {
            this.outer = outer;
            this.clasz = symbol.constructorClass();
            this.supers = supers;
            this.subst = getOuterTypeSubst(symbol, false);
            this.link = getOuterValueLink(symbol);
            this.inMethod = false;
        }

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

    }

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