summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scalai/Environment.java
blob: c307929f1a3ceae91250c6bca8319c41aef036f0 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $OldId: Environment.java,v 1.13 2002/10/01 16:14:07 paltherr Exp $
// $Id$

package scalai;

import java.util.Map;
import java.util.HashMap;

import scalac.ast.Tree.ClassDef;
import scalac.symtab.Symbol;
import scalac.symtab.Type;
import scalac.symtab.Scope;
import scalac.symtab.Scope.SymbolIterator;
import scalac.util.Names;
import scalac.util.Debug;

public class Environment {

    //########################################################################
    // Private Fields

    private final Compiler compiler;
    private final JavaMirror mirror;
    private final Map/*<Symbol,ClassDef>*/ classdefs;
    private final Map/*<Symbol,Template>*/ templates;
    private final Map/*<Symbol,Function>*/ functions;
    private final Map/*<Symbol,Variable>*/ variables;
    private final Map/*<Symbol,Override>*/ overrides;

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

    public Environment(Compiler compiler, JavaMirror mirror) {
        this.compiler = compiler;
        this.mirror = mirror;
        this.classdefs = new HashMap();
        this.templates = new HashMap();
        this.functions = new HashMap();
        this.variables = new HashMap();
        this.overrides = new HashMap();
    }

    //########################################################################
    // Public Methods - insert

    public ClassDef insertClassDef(Symbol symbol, ClassDef classdef) {
        assert symbol.isType() : Debug.show(symbol);
        assert Debug.log("insert classdef: ", symbol);
        Object value = classdefs.put(symbol, classdef);
        assert value == null : Debug.show(symbol) + " -> " + value;
        assert !templates.containsKey(symbol) : Debug.show(symbol);
        return classdef;
    }

    private Template insertTemplate(Symbol symbol, Template template) {
        assert symbol.isType() : Debug.show(symbol);
        assert Debug.log("insert template: ", symbol);
        Object value = templates.put(symbol, template);
        assert !classdefs.containsKey(symbol) : Debug.show(symbol);
        assert value == null : Debug.show(symbol) + " -> " + value;
        return template;
    }

    public Function insertFunction(Symbol symbol, Function function) {
        assert symbol.isTerm() : Debug.show(symbol);
        assert Debug.log("insert function: ", symbol);
        Object value = functions.put(symbol, function);
        assert value == null : Debug.show(symbol) + " -> " + value;
        return function;
    }

    public Variable insertVariable(Symbol symbol, Variable variable) {
        assert symbol.isTerm() : Debug.show(symbol);
        assert Debug.log("insert variable: ", symbol);
        Object value = variables.put(symbol, variable);
        assert value == null : Debug.show(symbol) + " -> " + value;
        return variable;
    }

    public Override insertOverride(Symbol symbol, Override override) {
        assert symbol.isTerm() : Debug.show(symbol);
        assert Debug.log("insert override: ", symbol);
        Object value = overrides.put(symbol, override);
        assert value == null : Debug.show(symbol) + " -> " + value;
        return override;
    }

    //########################################################################
    // Public Methods - lookup

    public Template lookupTemplate(Symbol symbol) {
        assert symbol.isType() : Debug.show(symbol);
        Object value = templates.get(symbol);
        if (value != null) return (Template)value;
        if (symbol.isExternal()) {
            Template template = Template.JavaClass(mirror.getClass(symbol));
            return insertTemplate(symbol, template);
        } else {
            return loadTemplate(symbol);
        }
    }

    public Function lookupFunction(Symbol symbol) {
        assert symbol.isTerm() : Debug.show(symbol);
        Object value = functions.get(symbol);
        if (value != null) return (Function)value;
        if (symbol.isExternal()) {
            Function function = (symbol.name == Names.CONSTRUCTOR) ?
                Function.JavaConstructor(mirror.getConstructor(symbol)) :
                Function.JavaMethod(mirror.getMethod(symbol));
            return insertFunction(symbol, function);
        } else {
            return (Function)loadOwnerThenGet("function", symbol, functions);
        }
    }

    public Variable lookupVariable(Symbol symbol) {
        assert symbol.isTerm() : Debug.show(symbol);
        Object value = variables.get(symbol);
        if (value != null) return (Variable)value;
        if (symbol.isJava() && symbol.isModule()) {
            // !!! This should never happen. This value will never be used.
            Class clasz = mirror.getClass(symbol.moduleClass());
            Variable variable = Variable.Global(clasz);
            return insertVariable(symbol, variable);
        } else if (symbol.isExternal()) {
            Variable variable = Variable.JavaField(mirror.getField(symbol));
            return insertVariable(symbol, variable);
        } else {
            return (Variable)loadOwnerThenGet("variable", symbol, variables);
        }
    }

    public Override lookupOverride(Symbol symbol) {
        assert symbol.isTerm() : Debug.show(symbol);
        Object value = overrides.get(symbol);
        if (value != null) return (Override)value;
        return loadOwnerOverridesThenGet(symbol);
    }

    //########################################################################
    // Private Methods - template loading

    private Object loadOwnerThenGet(String what, Symbol symbol, Map table) {
        loadOwner(what, symbol);
        Object value = table.get(symbol);
        assert value != null : Debug.show(symbol) + " -> " + value;
        return value;
    }

    private void loadOwner(String what, Symbol symbol) {
        assert Debug.log("search ", what, ": ", symbol);
        assert symbol.owner().isType() : Debug.show(symbol);
        assert!symbol.owner().isExternal() : Debug.show(symbol);
        loadTemplate(symbol.owner());
    }

    private Template loadTemplate(Symbol symbol) {
        Object test1 = classdefs.remove(symbol);
        if (test1 != null) return loadTemplate(symbol, (ClassDef)test1);
        loadOwner("template", symbol);
        Object test2 = classdefs.remove(symbol);
        if (test2 != null) return loadTemplate(symbol, (ClassDef)test2);
        Object value = templates.get(symbol);
        assert value != null : Debug.show(symbol) + " -> " + value;
        return (Template)value;
    }

    private Template loadTemplate(Symbol symbol, ClassDef classdef) {
        Template template = Template.Global(compiler.load(symbol, classdef));
        return insertTemplate(symbol, template);
    }

    //########################################################################
    // Private Methods - override loading

    private Override loadOwnerOverridesThenGet(Symbol symbol) {
        assert Debug.log("search override: ", symbol);
        assert symbol.owner().isType() : Debug.show(symbol);
        loadTemplateOverrides(symbol.owner());
        Object value = overrides.get(symbol);
        assert value != null : Debug.show(symbol) + " -> " + value;
        return (Override)value;
    }

    private void loadTemplateOverrides(Symbol symbol) {
        Type[] bases = symbol.parents();
        SymbolIterator i = symbol.members().iterator(true);
        while (i.hasNext()) loadMethodOverride(bases, i.next());
    }

    private void loadMethodOverride(Type[] bases, Symbol symbol) {
        if (!symbol.isMethod()) return;
        Override override = Override.empty().insert(symbol);
        if (!symbol.isInitializer()) {
            if (symbol.isExternal()) override.insert(mirror.getMethod(symbol));
            for (int i = 0; i < bases.length; i++) {
                Symbol overridden = symbol.overriddenSymbol(bases[i], true);
                if (overridden == Symbol.NONE) continue;
                assert Debug.log("update override: ",symbol," <- ",overridden);
                override.insert(lookupOverride(overridden));
            }
        }
        insertOverride(symbol, override);
    }

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