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

// $Id$

package scalac.transformer;

import scalac.*;
import scalac.util.*;
import scalac.parser.*;
import scalac.symtab.*;
import scalac.checkers.*;
import java.util.ArrayList;

public class LambdaLiftPhase extends PhaseDescriptor implements Kinds, Modifiers {

    private Global global;
    int nextPhase;

    public void initialize(Global global, int id) {
        super.initialize(global, id);
        this.global = global;
        this.nextPhase = id + 1;
    }

    public String name () {
        return "lambdalift";
    }

    public String description () {
        return "lambda lifter";
    }

    public String taskDescription() {
        return "lambda lifting";
    }

    public void apply(Global global) {
        new LambdaLift(global, this).apply();
    }

	public void apply(Unit unit) {
		new LambdaLift(unit.global, this).apply(unit);
	}

    public Type transformInfo(Symbol sym, Type tp) {
        if (global.debug)
            global.log("transform info for " + sym + ":" + tp + sym.locationString());
        Type tp1 = tp;
        if (sym != Symbol.NONE) {
            switch (tp) {
            case MethodType(_, _):
            case PolyType(_, _):
                tp1 = transform(tp, sym);
                break;
            default:
                if (sym.kind == CLASS)
                    tp1 = transform(tp, sym);
                else
                    tp1 = transform(tp, sym.owner());
            }
        }
        if ((sym.flags & Modifiers.CAPTURED) != 0) return refType(tp1);
        else return tp1;
    }

    /** Add proxies as type arguments for propagated type parameters.
     */
    Type transform(Type tp, Symbol owner) {
        return transformTypeMap.setOwner(owner).apply(tp);
    }

    /** MapOnlyTypes => All symbols are mapped to themselves.
     */
    private class TransformTypeMap extends Type.MapOnlyTypes {
        Symbol owner;
//      ArrayList/*<Symbol>*/ excluded = new ArrayList();
        Type.Map setOwner(Symbol owner) { this.owner = owner; return this; }

        public Type apply(Type tp) {
            switch (tp) {
            case TypeRef(Type pre, Symbol sym, Type[] targs):
                switch (pre) {
                case ThisType(_):
                    if (sym.kind == CLASS && sym.constructor().isUpdated(nextPhase)) {
                        // !!! For some Java classes,
                        // Symbol.constructor() returns an Overloaded
                        // symbol. This is wrong as constructor()
                        // should return the primary constructor. Once
                        // this problem is solved, the following
                        // switch can be removed.
                        Type constrtype = sym.constructor().infoAt(nextPhase);
                        Symbol[] tparams;
                        switch (constrtype) {
                        case OverloadedType(_, _):
                            tparams = Symbol.EMPTY_ARRAY;
                            break;
                        default:
                            tparams = constrtype.typeParams();
                            break;
                        }
                        int i = tparams.length;
                        while (i > 0 && (tparams[i-1].flags & SYNTHETIC) != 0)
                            i--;
                        if (i < tparams.length) {
                            if (global.debug)
                                global.log("adding proxies for " + sym + ": " + ArrayApply.toString(tparams));

                            Type[] targs1 = new Type[tparams.length];
                            System.arraycopy(map(targs), 0, targs1, 0, targs.length);
                            while (i < tparams.length) {
                                targs1[i] = proxy(tparams[i], owner).type();
                                i++;
                            }
                            return Type.TypeRef(pre, sym, targs1);
                        }
                    } else if (sym.isLocal()) {
                        assert targs.length == 0;
                        return proxy(sym, owner).type();
                    }
                }
                break;
/*
            case PolyType(Symbol[] tparams, _):
                if (tparams.length != 0) {
                    int len = excluded.size();
                    for (int i = 0; i < tparams.length; i++)
                        excluded.add(tparams[i]);
                    Type tp1 = map(tp);
                    for (int i = 0; i < tparams.length; i++)
                        excluded.remove(excluded.size() - 1);
                    return tp1;
                }
*/
            }
            return map(tp);
        }
    }

    private TransformTypeMap transformTypeMap = new TransformTypeMap();

    /** Return closest enclosing (type)parameter that has same name as `fv',
     *  or `fv' itself if this is the closest definition.
     */
    Symbol proxy(Symbol fv, Symbol owner) {
        if (global.debug)
            global.log("proxy " + fv + " of " + fv.owner() + " in " + LambdaLift.asFunction(owner));
        Symbol o = owner;
        while (o.kind != NONE) {
            if (global.debug)
                global.log("looking in " +  LambdaLift.asFunction(o) + " " +
                    ArrayApply.toString(o.typeParams()));
            Symbol fowner = LambdaLift.asFunction(o);
            if (fowner.isMethod()) {
                if (fv.owner() == fowner) return fv;
                Type ft = (fowner.isUpdated(nextPhase)) ? fowner.typeAt(nextPhase)
                    : fowner.type();
                Symbol[] ownerparams = fv.isType() ? ft.typeParams()
                    : ft.firstParams();
                for (int i = 0; i < ownerparams.length; i++) {
                    if (ownerparams[i].name == fv.name)
                        return ownerparams[i];
                }
            }
            assert o.owner() != o;
            o = o.owner();
        }
        return fv;
        //throw new ApplicationError("proxy " + fv + " in " + owner);
    }

    /** The type scala.Ref[tp]
     */
    Type refType(Type tp) {
        Symbol refClass = global.definitions.getClass(Names.scala_Ref);
        assert refClass.kind == Kinds.CLASS;
        return Type.TypeRef(global.definitions.SCALA_TYPE, refClass, new Type[]{tp});
    }

    public Checker[] postCheckers(Global global) {
        return new Checker[] {
            new CheckSymbols(global),
            new CheckTypes(global),
            new CheckOwners(global),
            new CheckNames(global)
        };
    }
}