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

// $OldId: ExpandMixins.java,v 1.24 2002/11/11 16:08:50 schinz Exp $
// $Id$

package scalac.transformer;

import scalac.*;
import scalac.util.*;
import scalac.ast.*;
import scalac.symtab.*;
import java.util.*;
import java.util.Arrays;
import Tree.*;

/**
 * A transformer to expand mixins using code copying. We assume that
 * links to outer classes have been made explicit by a previous phase.
 *
 * @author Michel Schinz
 * @version 1.0
 */

// [...] do not copy hidden members which are not accessible via
//       "super"
// [...] handle overloaded symbols

public class ExpandMixins extends Transformer {
    // Mapping from (class) symbols to their definition.
    protected final Map/*<Symbol,Tree>*/ classDefs;

    protected final FreshNameCreator freshNameCreator;
    protected final Map interfaceToClass;
    protected final Map classToInterface;

    protected final static int PRIVATE_FINAL = Modifiers.FINAL | Modifiers.PRIVATE;

    protected final TreeCopier treeCopier;
    protected final Definitions defs;

    public ExpandMixins(Global global, ExpandMixinsPhase descr) {
        super(global);
        defs = global.definitions;

        classToInterface = global.PHASE.ADDINTERFACES.classToInterface;
        interfaceToClass = global.PHASE.ADDINTERFACES.interfaceToClass;

        classDefs = descr.classDefs;

        freshNameCreator = global.freshNameCreator;

        treeCopier = new TreeCopier(global, global.make) {
                // Substitute symbols refering to this class only.
                public boolean mustSubstituteSymbol(Tree tree) {
                    switch (tree) {
                    case Ident(_):
                    case Select(This(_), _):
                        return true;

                    default:
                        return mustCopySymbol(tree);
                    }
                }
            };
    }

    public void apply() {
        ClassDefCollector collector = new ClassDefCollector(classDefs);

        for (int i = 0; i < global.units.length; i++) {
	    Unit unit = global.units[i];
	    for (int j = 0; j < unit.body.length; ++j)
		collector.traverse(unit.body[j]);
	}

        super.apply();
    }

    protected void typeSubst(Type type, ArrayList f, ArrayList a) {
        switch (type) {
        case TypeRef(Type pre, Symbol sym, Type[] args): {
            Symbol s;
            if (interfaceToClass.containsKey(sym))
                s = (Symbol)interfaceToClass.get(sym);
            else
                s = sym;

            f.addAll(Arrays.asList(s.typeParams()));
            a.addAll(Arrays.asList(args));
            typeSubst(pre, f, a);
        } break;
        default:
            ;                   // nothing to do
        }
    }

    protected Object[] typeSubst(Type type) {
        ArrayList/*<Symbol[]>*/ f = new ArrayList();
        ArrayList/*<Type[]>*/ a = new ArrayList();
        typeSubst(type, f, a);
        return new Object[] {
            f.toArray(new Symbol[f.size()]), a.toArray(new Type[a.size()])
        };
    }

    protected void getArgsSection(Tree tree, List s) {
        switch(tree) {
        case Apply(Tree fun, Tree[] args):
            getArgsSection(fun, s);
            s.add(args);
            break;

        default:
            ;                   // nothing to do
        }
    }

    protected Tree[][] getArgsSection(Tree tree) {
        List s = new ArrayList();
        getArgsSection(tree, s);
        return (Tree[][])s.toArray(new Tree[s.size()][]);
    }

    protected Symbol renameSymbol(Map symbolMap, Symbol oldSymbol) {
        Name newName = freshNameCreator.newName(oldSymbol.name);
        if (oldSymbol.name.isTypeName()) newName = newName.toTypeName();
        else if (oldSymbol.name.isConstrName()) newName = newName.toConstrName();
        Symbol newSymbol = oldSymbol.cloneSymbol();
        newSymbol.name = newName;
        symbolMap.put(oldSymbol, newSymbol);

        return newSymbol;
    }

    protected Map/*<Template,Template>*/ expansions = new HashMap();

    protected Template getMixinExpandedTemplate(Template tree, Symbol owner) {
        if (! expansions.containsKey(tree))
            expansions.put(tree, expandMixins(tree, owner));
        return (Template)expansions.get(tree);
    }

    protected Template expandMixins(Template tree, Symbol owner) {
        Type templType = tree.type;

        List/*<Tree>*/ newBody = new ArrayList();
	Scope newMembers = new Scope();

        Map mixedInSymbols/*<Symbol,Symbol>*/ = new HashMap();

        Symbol newTemplSymbol = tree.symbol().cloneSymbol();

        // Start by copying the statement sequence.
        Tree[] body = tree.body;
        for (int i = 0; i < body.length; ++i) {
            Tree stat = body[i];
            newBody.add(transform(stat));

            if (stat.hasSymbol()) {
		Symbol sym = stat.symbol();
		newMembers.enter(sym);
            }
        }

        Type[] baseTypes = tree.type.parents();
        global.log("baseTypes = <" + ArrayApply.toString(baseTypes) + ">");

        // Then go over the mixins and mix them in.
        for (int bcIndex = tree.parents.length - 1; bcIndex > 0; --bcIndex) {
            Tree bc = tree.parents[bcIndex];

            Symbol bcSym = baseTypes[bcIndex].symbol();
            Type bcType = bcSym.type();

            if ((bcSym.flags & Modifiers.INTERFACE) != 0)
                continue;

            assert classDefs.containsKey(bcSym) : bcSym;
            ClassDef bcDef = (ClassDef)classDefs.get(bcSym);

            Map symbolMap/*<Symbol,Symbol>*/ = new HashMap();

            // Create substitution for mixin's type parameters.
            Object[] ts = typeSubst(baseTypes[bcIndex]);
            assert ts.length == 2;
            final Symbol[] tpFormals = (Symbol[])ts[0];
            final Type[] tpActuals = (Type[])ts[1];
            assert tpFormals.length == tpActuals.length;
            Type.Map typeMap = new Type.Map() {
                    public Type apply(Type t) {
                        return t.subst(tpFormals, tpActuals);
                    }
                };

            // Create private fields for mixin's value parameters.
            Tree[][] actuals = getArgsSection(bc);
            assert bcDef.vparams.length == actuals.length;
            for (int s = 0; s < bcDef.vparams.length; ++s) {
                ValDef[] sectionF = bcDef.vparams[s];
                Tree[] sectionA = actuals[s];

                assert sectionF.length == sectionA.length;

                for (int p = 0; p < sectionF.length; ++p) {
                    // We do not need to copy the actual parameters,
                    // since they are removed from their original
                    // location anyway.
                    ValDef formal = sectionF[p];
                    Tree actual = sectionA[p];

                    Symbol memberSymbol =
                        renameSymbol(symbolMap, formal.symbol());
                    memberSymbol.setOwner(owner);
                    Type memberType = typeMap.apply(formal.tpe.type());
                    memberSymbol.updateInfo(memberType);

                    Tree memberDef = gen.ValDef(memberSymbol, actual);
                    newBody.add(memberDef);
                }
            }

            Template mixin = getMixinExpandedTemplate(bcDef.impl, bcSym);
            Tree[] mixinBody = mixin.body;
            Set/*<Tree>*/ leftOutMembers = new HashSet();

            // Pass 1: compute members to rename.
            for (int m = 0; m < mixinBody.length; ++m) {
                Tree member = mixinBody[m];

                if (!member.hasSymbol())
                    continue;

                Symbol memSym = member.symbol();
                Name memName = memSym.name;

                // Check if we have to import this member. To do this,
                // we lookup the member both in the template and in
                // the mixin, and if the result is the same, we import
                // the member (otherwise it means it's shadowed).

                Symbol memSymT = templType.lookupNonPrivate(memName);
                Symbol memSymM = bcType.lookupNonPrivate(memName);

                if (memSymT != memSymM) {
                    if ((memSym.flags & Modifiers.DEFERRED) != 0)
                        leftOutMembers.add(member);
                    else
                        renameSymbol(symbolMap, memSym);
                }
            }

            // Pass 2: copy members
            for (int m = 0; m < mixinBody.length; ++m) {
                Tree member = mixinBody[m];

                if (leftOutMembers.contains(member))
                    continue;

                treeCopier.pushSymbolSubst(symbolMap);
                treeCopier.pushTypeSubst(tpFormals, tpActuals);
                Tree newMember = treeCopier.copy(member);
                treeCopier.popTypeSubst();
                treeCopier.popSymbolSubst();

                newBody.add(newMember);

                if (newMember.hasSymbol()) {
		    Symbol sym = newMember.symbol();

                    sym.setOwner(owner);
                    newMembers.enter(sym);

                    mixedInSymbols.put(member.symbol(), newMember.symbol());
		}
            }
        }

	// Modify mixin base classes to refer to interfaces instead of
	// real classes.
	Type[] newBaseTypes = new Type[baseTypes.length];
	Tree[] newBaseClasses = new Tree[tree.parents.length];
        newBaseTypes[0] = baseTypes[0];
        newBaseClasses[0] = tree.parents[0];
	for (int i = 1; i < baseTypes.length; ++i) {
	    switch (baseTypes[i]) {
	    case TypeRef(Type pre, Symbol sym, Type[] args): {
		if (!Modifiers.Helper.isInterface(sym.flags) && i > 0) {
                    assert classToInterface.containsKey(sym) : sym;
                    sym = (Symbol)classToInterface.get(sym);
                }

                newBaseClasses[i] =
                    gen.mkParentConstr(tree.pos, new Type.TypeRef(pre, sym, args));
		newBaseTypes[i] = new Type.TypeRef(pre, sym, args);
	    } break;

	    default:
		throw global.fail("invalid base class type", baseTypes[i]);
	    }
	}

        // Use correct symbols for mixed-in members.
        SymbolFixer symbolFixer = new SymbolFixer(global, mixedInSymbols);
        Tree[] fixedBody =
            symbolFixer.transform((Tree[])newBody.toArray(new Tree[newBody.size()]));
        Template newTree = make.Template(tree.pos, newBaseClasses, fixedBody);
        newTree.setSymbol(newTemplSymbol);
	newTree.setType(Type.compoundType(newBaseTypes, newMembers, owner));

        return newTree;
    }

    public Tree transform(Tree tree) {
        switch (tree) {
        case ClassDef(int mods,
                      Name name,
                      TypeDef[] tparams,
                      ValDef[][] vparams,
                      Tree tpe,
                      Template impl):
            if (Modifiers.Helper.isInterface(mods))
                return super.transform(tree);
            else {
                global.log("expanding " + name);
		Tree.ClassDef newClass = (Tree.ClassDef)
                    copy.ClassDef(tree,
                                  mods,
                                  name,
                                  super.transform(tparams),
                                  super.transform(vparams),
                                  super.transform(tpe),
                                  getMixinExpandedTemplate(impl, tree.symbol()));
		newClass.symbol().updateInfo(newClass.impl.type);
		return newClass;
	    }

        default:
            Tree newTree = super.transform(tree);

            switch (newTree) {
            case Apply(Select(Tree qualifier, Name selector), Tree[] args): {
                Tree fun = ((Tree.Apply)newTree).fun;
                Symbol funOwnerSym = fun.symbol().owner();
                Symbol qualSym = qualifier.type.symbol().moduleClass();
                if (! (qualifier instanceof Tree.Super
                       || qualSym.isSubClass(funOwnerSym))) {
		    global.log("inserting cast from " + qualSym + " to " + funOwnerSym);//debug
                    Type ownerTp = funOwnerSym.type();
                    Tree castQualifier =
                        gen.Apply(gen.TypeApply(gen.Select(qualifier, defs.AS),
                                                new Tree[] {
                                                    gen.mkType(qualifier.pos, ownerTp)
                                                }),
                                  Tree.EMPTY_ARRAY);
                    return copy.Apply(newTree,
                                      copy.Select(fun, castQualifier, selector),
                                      args);
                } else
                    return newTree;
            }
            default:
                return newTree;
            }
        }
    }

    //########################################################################

    // Return a hash table associating class definitions to (class) symbols.
    protected static class ClassDefCollector extends Traverser {
        private Map map;

        public ClassDefCollector(Map map) {
            this.map = map;
        }

        public void traverse(Tree tree) {
            switch(tree) {
            case ClassDef(_, _, _, _, _, _):
                map.put(tree.symbol(), tree);
                break;

            default:
                ;               // nothing to do
            }
            super.traverse(tree);
        }
    }

    //########################################################################

    protected static class SymbolFixer extends Transformer {
        protected final Map/*<Symbol,Symbol>*/ mixedInSymbols;

        public SymbolFixer(Global global, Map mixedInSymbols) {
            super(global);
            this.mixedInSymbols = mixedInSymbols;
        }

        public Tree transform(Tree tree) {
            switch (tree) {
            case Ident(_): {
                Symbol sym = tree.symbol();
                if (mixedInSymbols.containsKey(sym))
                    return gen.Ident((Symbol)mixedInSymbols.get(sym));
                else
                    return super.transform(tree);
            }

            case Select(Super(Tree tpe), Name selector): {
                Symbol sym = tree.symbol();
                if (mixedInSymbols.containsKey(sym))
                    return gen.Ident((Symbol)mixedInSymbols.get(sym));
                else
                    return super.transform(tree);
            }

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