summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/TypesAsValuesPhase.java
blob: 5f672841f24cea4879249d7ed81bb4d57ae28478 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    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.symtab.Definitions;
import scalac.symtab.Scope;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.atree.AConstant;
import scalac.ast.Transformer;
import scalac.ast.GenTransformer;
import scalac.ast.Tree;
import scalac.backend.Primitives;

import scalac.util.Name;
import scalac.util.Names;
import scalac.util.Debug;

/**
 * Turn types into values by applying the following transformations:
 *
 * - For all type member T of all classes, add an accessor method
 *   T$type returnining the type as a value (the accessor is abstract
 *   if the type member is abstract).
 *
 * - For all polymorphic methods/constructors, add a value parameter
 *   for each type parameter.
 *
 * - Add a method getType to every class, to obtain its type as a
 *   value.
 *
 * - Transform all type expressions into value expressions: type
 *   application is turned into value application, type selection into
 *   value selection, and so on.
 *
 * @author Michel Schinz
 * @version 1.0
 */

public class TypesAsValuesPhase extends Phase {
    private final TV_Transformer transformer;

    private static final HashMap/*<Symbol,Map<Symbol,Symbol>>*/ membersToAdd =
        new HashMap();
    private static final HashMap/*<Symbol,Map<Symbol,Symbol>>*/ paramsToAdd =
        new HashMap();
    private static final HashMap/*<Symbol,Symbol>*/ accessors =
        new HashMap();

    private final Definitions defs = global.definitions;
    private final Primitives prims = global.primitives;

    private final Type typeType = defs.TYPE_TYPE();
    private final Type.MethodType typeAccessorType =
        new Type.MethodType(new Symbol[]{}, typeType);

    private final Symbol singleTypeClass = defs.SINGLETYPE_CLASS;
    private final Symbol ARRAY_CONSTRUCTOR =
        defs.ARRAY_CLASS.primaryConstructor();

    private final Map/*<Symbol, Symbol>*/ basicTypes;

    public TypesAsValuesPhase(Global global, PhaseDescriptor descriptor) {
        super(global, descriptor);
        transformer = new TV_Transformer(global);

        basicTypes = new HashMap();
        basicTypes.put(defs.DOUBLE_CLASS,  defs.RTT_DOUBLE());
        basicTypes.put(defs.FLOAT_CLASS,   defs.RTT_FLOAT());
        basicTypes.put(defs.LONG_CLASS,    defs.RTT_LONG());
        basicTypes.put(defs.INT_CLASS,     defs.RTT_INT());
        basicTypes.put(defs.SHORT_CLASS,   defs.RTT_SHORT());
        basicTypes.put(defs.CHAR_CLASS,    defs.RTT_CHAR());
        basicTypes.put(defs.BYTE_CLASS,    defs.RTT_BYTE());
        basicTypes.put(defs.BOOLEAN_CLASS, defs.RTT_BOOLEAN());
    }

    /**
     * Return a map associating symbols for type accessors to the
     * symbol of their type.
     */
    private HashMap typeAccessors(Symbol classSym) {
        HashMap/*<Symbol, Symbol>*/ newSymbols =
            (HashMap)membersToAdd.get(classSym);

        if (newSymbols == null) {
            newSymbols = new HashMap();

            Scope.SymbolIterator membersIt = classSym.members().iterator(true);
            while (membersIt.hasNext()) {
                Symbol member = membersIt.next();
                if (member.isType() /*&& !member.isClass()*/) {
                    Symbol typeAccessorSym = getAccessorSymbol(member);
                    newSymbols.put(member, typeAccessorSym);
                }
            }
            membersToAdd.put(classSym, newSymbols);
        }
        return newSymbols;
    }

    private HashMap typeParams(Symbol funSym) {
        HashMap newSymbols = (HashMap)paramsToAdd.get(funSym);
        if (newSymbols == null) {
            Symbol[] tparams = funSym.typeParams();
            assert tparams.length > 0;

            newSymbols = new HashMap();
            for (int i = 0; i < tparams.length; ++i) {
                Symbol param = tparams[i];
                Symbol valueSym = getAccessorSymbol(param);
                newSymbols.put(param, valueSym);
            }
            paramsToAdd.put(funSym, newSymbols);
        }

        return newSymbols;
    }

    /**
     * Return the symbol of the accessor for the given type symbol.
     */
    private Symbol getAccessorSymbol(Symbol typeSym) {
        assert typeSym.isType();
        Symbol accessorSym = (Symbol)accessors.get(typeSym);
        if (accessorSym == null) {
            accessorSym = typeSym.owner().newVariable(typeSym.pos,
                                                      typeSym.flags,
                                                      Names.TYPE(typeSym));
            accessorSym.setInfo(typeSym.owner().isClass()
                                ? typeAccessorType
                                : typeType);
            accessors.put(typeSym, accessorSym);
        }
        return accessorSym;
    }

    public Type transformInfo(Symbol symbol, Type type) {
        if (symbol.isClass()) {
            // Class:
            // - add an accessor per type member.
            HashMap newSymbols = typeAccessors(symbol);
            if (newSymbols.isEmpty())
                return type;
            else {
                Scope newMembers = new Scope(symbol.members());
                Iterator newSymbolsIt = newSymbols.values().iterator();
                while (newSymbolsIt.hasNext())
                    newMembers.enterOrOverload((Symbol)newSymbolsIt.next());
                return Type.compoundType(type.parents(), newMembers, symbol);
            }
        } else if (type.typeParams().length > 0 && !isPrimitive(symbol)) {
            // Polymorphic method/constructor:
            // - add a value parameter for every type parameter.
            switch (type) {
            case PolyType(Symbol[] tparams, // :
                          Type.MethodType(Symbol[] vparams, Type result)):
                HashMap newVParams = typeParams(symbol);
                Symbol[] allVParams =
                    new Symbol[newVParams.size() + vparams.length];
                for (int i = 0; i < tparams.length; ++i)
                    allVParams[i] = (Symbol)newVParams.get(tparams[i]);
                System.arraycopy(vparams,
                                 0,
                                 allVParams,
                                 tparams.length,
                                 vparams.length);
                return new Type.PolyType(tparams,
                                         new Type.MethodType(allVParams,
                                                             result));

            default:
                throw Debug.abort("unexpected type: ", type);
            }
        } else
            return type;
    }

    private boolean isPrimitive(Symbol sym) {
        throw new Error();      // TODO
    }

    public void apply(Unit[] units) {
        transformer.apply(units);
    }

    private class TV_Transformer extends GenTransformer {
        private Symbol currentOwner;

        public TV_Transformer(Global global) {
            super(global);
        }

        public Tree transform(Tree tree) {
            switch (tree) {
            case ClassDef(int mods, //:
                          Name name,
                          Tree.AbsTypeDef[] tparams,
                          Tree.ValDef[][] vparams,
                          Tree tpe,
                          Tree.Template impl):
                Symbol sym = tree.symbol();
                Tree[] newBody = transform(impl.body, impl.symbol());

                // Add accessors for type members
                Tree[] finalBody;
                HashMap/*<Symbol, Symbol>*/ membersToAdd = typeAccessors(sym);
                if (!membersToAdd.isEmpty()) {
                    finalBody = new Tree[newBody.length + membersToAdd.size()];
                    for (int n = 0, nn = 0; n < newBody.length; ++n) {
                        finalBody[nn++] = newBody[n];
                        Symbol memberSym = newBody[n].symbol();
                        if (membersToAdd.containsKey(memberSym)) {
                            Symbol symToAdd =
                                (Symbol)membersToAdd.get(memberSym);
                            Tree rhs = memberSym.isAbstractType()
                                ? Tree.Empty
                                : typeAsValue(newBody[n].pos,
                                              memberSym.type(),
                                              symToAdd);
                            finalBody[nn++] = gen.DefDef(symToAdd, rhs);
                        }
                    }
                } else
                    finalBody = newBody;

                return gen.ClassDef(sym,
                                    transform(impl.parents),
                                    impl.symbol(),
                                    finalBody);

            case DefDef(_, _, _, _, _, Tree rhs):
                Symbol symbol = getSymbolFor(tree);
                return gen.DefDef(symbol, transform(rhs, symbol));

            case ValDef(_, _, Tree tpe, Literal(AConstant.ZERO)):
                // transform default values:
                //   val x: T = _
                // becomes
                //   val x: T = asValue(T).defaultValue()
                Symbol symbol = getSymbolFor(tree);
                Tree defaultValue =
                    gen.mkRef(tree.pos,
                              typeAsValue(tree.pos, tpe.type, currentOwner),
                              defs.TYPE_DEFAULTVALUE());
                Tree rhs = gen.mkApply__(tree.pos, defaultValue);
                return gen.ValDef(symbol, rhs);

            case ValDef(_, _, _, Tree rhs):
                Symbol symbol = getSymbolFor(tree);
                return gen.ValDef(symbol, transform(rhs, symbol));

            case New(Apply(TypeApply(Tree fun, Tree[] targs), Tree[] vargs)):
                if (fun.symbol() == ARRAY_CONSTRUCTOR) {
                    // Transform array creations:
                    //   new Array[T](size)
                    // becomes
                    //   asValue(T).newArray[T](size)
                    assert targs.length == 1;
                    assert vargs.length == 1;
                    Tree newArrayfun = gen.mkRef(tree.pos,
                                                 typeAsValue(targs[0].pos,
                                                             targs[0].type,
                                                             currentOwner),
                                                 defs.TYPE_NEWARRAY());
                    return gen.mkApplyTV(newArrayfun, targs, vargs);
                } else
                    return super.transform(tree);

            case Apply(TypeApply(Tree fun, Tree[] targs), Tree[] vargs):
                Symbol funSym = fun.symbol();

                if (funSym == defs.ANY_IS) {
                    // Transform instance tests:
                    //   e.isInstanceOf[T]
                    // becomes:
                    //   asValue(T).hasAsInstance(e)
                    // unless T is a "simple" type for which a Java
                    // instance test is sufficient, in which case the
                    // expression is left as is.
                    assert targs.length == 1 && vargs.length == 0;
                    Tree tp = targs[0];
                    if (isTrivialType(tp.type))
                        return super.transform(tree);
                    else {
                        Tree tpV = typeAsValue(tp.pos, tp.type, currentOwner);
                        Tree hasAsInst =
                            gen.Select(tp.pos, tpV, defs.TYPE_HASASINSTANCE());
                        return gen.mkApply_V(tree.pos,
                                             hasAsInst,
                                             new Tree[] {
                                                 extractQualifier(fun)
                                             });
                    }
                } else if (funSym == defs.ANY_AS) {
                    // Transform instance tests:
                    //   e.asInstanceOf[T]
                    // becomes:
                    //   asValue(T).checkCastability(e).asInstanceOf[T]
                    // unless T is a "simple" type for which a Java
                    // instance test is sufficient, in which case the
                    // expression is left as is.
                    assert targs.length == 1 && vargs.length == 0;
                    return super.transform(tree); // TODO
                } else {
                    // Transform applications to pass types as values:
                    //   f[T1, ...](v1, ...)
                    // becomes
                    //   f[T1, ...](asValue(T1), ..., v1, ...)
                    Tree[] newVArgs = transform(vargs);
                    Tree[] finalVArgs =
                        new Tree[newVArgs.length + targs.length];
                    for (int i = 0; i < targs.length; ++i)
                        finalVArgs[i] = typeAsValue(targs[i].pos,
                                                    targs[i].type,
                                                    currentOwner);
                    System.arraycopy(newVArgs, 0,
                                     finalVArgs, targs.length,
                                     newVArgs.length);
                    return gen.mkApplyTV(tree.pos,
                                         transform(fun),
                                         targs,
                                         finalVArgs);
                }

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

        private Tree transform(Tree tree, Symbol currentOwner) {
            Symbol bkpOwner = this.currentOwner;
            this.currentOwner = currentOwner;
            Tree newTree = transform(tree);
            this.currentOwner = bkpOwner;
            return newTree;
        }

        private Tree[] transform(Tree[] trees, Symbol currentOwner) {
            Symbol bkpOwner = this.currentOwner;
            this.currentOwner = currentOwner;
            Tree[] newTrees = transform(trees);
            this.currentOwner = bkpOwner;
            return newTrees;
        }

        /**
         * Return true iff the given type is "trivial", that is if it
         * is representable as a Java type without loss of
         * information.
         */
        private boolean isTrivialType(Type tp) {
            switch (tp) {
            case ConstantType(Type base, _):
                return isTrivialType(base);
            case TypeRef(_, Symbol sym, Type[] args):
                return sym.isStatic() && args.length == 0;
            case SingleType(_, _):
            case ThisType(_):   // TODO check
            case CompoundType(_, _): // TODO check
                return false;
            default:
                throw Debug.abort("unexpected type", tp);
            }
        }

        /**
         * Transform a type into a tree representing it.
         */
        private Tree typeAsValue(int pos, Type tp, Symbol owner) {
            switch (tp) {
            case ConstantType(Type base, _):
                return typeAsValue(pos, base, owner);

            case TypeRef(Type pre, Symbol sym, Type[] args): {
                Symbol symOwner = sym.owner();

                if (basicTypes.containsKey(sym)) {
                    return gen.mkGlobalRef(pos, (Symbol)basicTypes.get(sym));
                } else if (symOwner.isClass() || symOwner.isMethod()) {
                    // Reference to a "local" type.
                    Symbol accessor = getAccessorSymbol(sym);
                    if (accessor.isMethod())
                        return gen.mkApply__(gen.mkLocalRef(pos, accessor));
                    else
                        return gen.mkLocalRef(pos, accessor);
                } else {
                    // Reference to a "non-local" type.
                    Tree[] ctorArgs = new Tree[args.length + 2];
                    ctorArgs[0] = symOwner.isPackage()
                        ? gen.mkNullLit(pos)
                        : prefixAsValue(pos, pre);
                    ctorArgs[1] =
                        gen.mkStringLit(pos, prims.getJREClassName(sym));
                    for (int i = 0; i < args.length; ++i)
                        ctorArgs[i + 2] = typeAsValue(pos, args[i], owner);

                    Symbol typeConstr =
                        defs.CONSTRUCTEDTYPE_CTOR(args.length);

                    return gen.New(pos,
                                   gen.mkApply_V(gen.mkGlobalRef(pos,
                                                                 typeConstr),
                                                 ctorArgs));
                }
            }

            case SingleType(Type pre, Symbol sym): {
                Tree constr =
                    gen.mkPrimaryConstructorGlobalRef(pos, singleTypeClass);
                Tree[] args = new Tree[] { gen.mkRef(pos, pre, sym) };
                return gen.New(pos, gen.mkApply_V(constr, args));
            }

            default:
                throw global.fail("unexpected type: ", tp);
            }
        }

        /**
         * Extract qualifier from a tree, which must be a Select node.
         */
        private Tree extractQualifier(Tree tree) {
            switch (tree) {
            case Select(Tree qualifier, _): return qualifier;
            default: throw Debug.abort("cannot extract qualifier from ", tree);
            }
        }

        /**
         * Transform a prefix into a tree representing it.
         */
        private Tree prefixAsValue(int pos, Type pre) {
            switch (pre) {
            case ThisType(Symbol clazz):
                if (clazz.isPackage() || clazz.isNone())
                    return gen.mkNullLit(pos);
                else
                    return gen.This(pos, clazz);
            case SingleType(Type prefix, Symbol member):
                return gen.mkApply__(pos,
                                     gen.mkRef(pos,
                                               prefixAsValue(pos, prefix),
                                               member));
            default:
                throw Debug.abort("unexpected prefix", pre);
            }
        }
    }
}