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

// $Id$
// $OldId: ExplicitOuterClasses.java,v 1.22 2002/10/17 12:31:56 schinz Exp $

package scalac.transformer;

import java.util.*;

import scalac.*;
import scalac.ast.*;
import scalac.util.*;
import scalac.parser.*;
import scalac.symtab.*;
import scalac.typechecker.*;
import Tree.*;

/**
 * Make links from nested classes to their enclosing class explicit.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class ExplicitOuterClasses extends Transformer {
    // Mapping from class constructor symbols to owner field symbols.
    protected HashMap/*<Symbol,Symbol>*/ outerMap;

    public ExplicitOuterClasses(Global global) {
        super(global);
        outerMap = global.PHASE.EXPLICITOUTER.outerMap;
    }

    protected Type addValueParam(Type oldType, Symbol newValueParam) {
        switch (oldType) {
        case MethodType(Symbol[] vparams, Type result): {
            Symbol[] newVParams = new Symbol[vparams.length + 1];
            newVParams[0] = newValueParam;
            System.arraycopy(vparams, 0, newVParams, 1, vparams.length);
            return new Type.MethodType(newVParams, result);
        }

        case PolyType(Symbol[] tparams, Type result):
            return new Type.PolyType(tparams, addValueParam(result, newValueParam));

        default:
            throw global.fail("invalid type", oldType);
        }
    }

    protected Symbol outerSym(Symbol constSym) {
        if (! outerMap.containsKey(constSym)) {
            Symbol ownerSym = constSym.enclClass();
            Symbol outerSym =
                new TermSymbol(constSym.pos, freshOuterName(), constSym, 0);
            outerSym.setInfo(ownerSym.type());

            outerMap.put(constSym, outerSym);
        }
        return (Symbol)outerMap.get(constSym);
    }

    protected Type newConstType(Symbol constSym) {
        return addValueParam(constSym.info(), outerSym(constSym));
    }

    protected LinkedList/*<Symbol>*/ classStack = new LinkedList();
    protected LinkedList/*<Symbol>*/ outerLinks = new LinkedList();

    protected Name freshOuterName() {
        return global.freshNameCreator.newName(Names.OUTER_PREFIX);
    }

    // Return the given class with an explicit link to its outer
    // class, if any.
    protected Tree addOuterLink(ClassDef classDef) {
        Symbol classSym = classDef.symbol();
        Symbol constSym = classSym.constructor();
        Symbol outerSym = outerSym(constSym);
        assert (outerSym.owner() == constSym) : outerSym;
        outerLinks.addFirst(outerSym);

        // Add the outer parameter, both to the type and the tree.
        Type newConstType = newConstType(constSym);
        ValDef[][] vparams = classDef.vparams;
        ValDef[] newVParamsI;
        if (vparams.length == 0)
            newVParamsI = new ValDef[] { gen.ValDef(outerSym) };
        else {
            newVParamsI = new ValDef[vparams[0].length + 1];
            newVParamsI[0] = gen.ValDef(outerSym);
            System.arraycopy(vparams[0], 0, newVParamsI, 1, vparams[0].length);
        }
        ValDef[][] newVParams = new ValDef[][] { newVParamsI };

        constSym.updateInfo(newConstType);

        int newMods = classDef.mods | Modifiers.STATIC;
        classSym.flags |= Modifiers.STATIC;

        return copy.ClassDef(classDef,
                             newMods,
                             classDef.name,
                             transform(classDef.tparams),
                             transform(newVParams),
                             transform(classDef.tpe),
                             (Template)transform((Tree)classDef.impl));
    }

    // Return the number of outer links to follow to find the given
    // symbol.
    protected int outerLevel(Symbol sym) {
        Iterator classIt = classStack.iterator();
        for (int level = 0; classIt.hasNext(); ++level) {
            Symbol classSym = (Symbol)classIt.next();
            if (classSym == sym)
                return level;
        }
        return -1;
    }

    // Return a tree referencing the "level"th outer class.
    protected Tree outerRef(int level) {
        assert level >= 0 : level;

        if (level == 0) {
            Symbol thisSym = (Symbol)classStack.getFirst();
            return gen.This(thisSym.pos, thisSym);
        } else {
            Iterator outerIt = outerLinks.iterator();
            Tree root = gen.Ident((Symbol)outerIt.next());

            for (int l = 1; l < level; ++l) {
                Symbol outerSym = (Symbol)outerIt.next();
                root = gen.mkStable(gen.Select(root, outerSym));
            }

            return root;
        }
    }

    public Tree transform(Tree tree) {
        switch (tree) {
        case ClassDef(int mods, _, _, _, _, _) : {
            // Add outer link
            ClassDef classDef = (ClassDef) tree;

            Symbol sym = classDef.symbol();
            Tree newTree;

            classStack.addFirst(sym);
            if (classStack.size() == 1 || Modifiers.Helper.isStatic(mods)) {
                outerLinks.addFirst(null);
                newTree = super.transform(tree);
            } else
                newTree = addOuterLink(classDef);
            outerLinks.removeFirst();
            classStack.removeFirst();

            return newTree;
        }

        case Ident(Name name): {
            if (! name.isTermName())
                return super.transform(tree);

            // Follow "outer" links to fetch data in outer classes.
            int level = outerLevel(tree.symbol().classOwner());
            if (level > 0) {
                Tree root = outerRef(level);
                return gen.mkStable(gen.Select(root, tree.symbol()));
            } else {
                return super.transform(tree);
            }
        }

        case This(Tree qualifier): {
            // If "this" refers to some outer class, replace it by
            // explicit reference to it.
            int level = qualifier.hasSymbol() ? outerLevel(qualifier.symbol()) : 0;
            if (level > 0)
                return outerRef(level);
            else
                return super.transform(tree);
        }

        case Apply(Tree fun, Tree[] args): {
            // Add outer parameter to constructor calls.
            Tree realFun;
            Tree[] typeArgs;

            switch (fun) {
            case TypeApply(Tree fun2, Tree[] tArgs):
                realFun = fun2; typeArgs = tArgs; break;
            default:
                realFun = fun; typeArgs = null; break;
            }

            Tree newFun = null, newArg = null;

            if (realFun.hasSymbol() && realFun.symbol().isPrimaryConstructor()) {
                switch (transform(realFun)) {
                case Select(Tree qualifier, Name selector): {
                    if (! (qualifier.hasSymbol()
                           && Modifiers.Helper.isNoVal(qualifier.symbol().flags))) {
                        newFun = make.Ident(qualifier.pos, selector)
                            .setType(realFun.type())
                            .setSymbol(realFun.symbol());
                        newArg = qualifier;
                    }
                } break;

                case Ident(Name name): {
                    int level = outerLevel(realFun.symbol().owner());
                    if (level >= 0) {
                        newFun = realFun;
                        newArg = outerRef(level);
                    }
                } break;

                default:
                    throw global.fail("unexpected node in constructor call");
                }

                if (newFun != null && newArg != null) {
                    Tree[] newArgs = new Tree[args.length + 1];
                    newArgs[0] = newArg;
                    System.arraycopy(args, 0, newArgs, 1, args.length);

                    Tree finalFun;
                    if (typeArgs != null)
                        finalFun = copy.TypeApply(fun, newFun, typeArgs);
                    else
                        finalFun = newFun;

                    finalFun.type = addValueParam(finalFun.type,
                                                  outerSym(realFun.symbol()));
                    return copy.Apply(tree, finalFun, transform(newArgs));
                } else
                    return super.transform(tree);

            } else
                return super.transform(tree);
        }

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