summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/classfile/MetaParser.java
blob: afc5f525e18fcb72dc16634605babb420f8fc1c1 (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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, 2003, 2004, LAMP/EPFL  **
** /_____/\____/\___/\____/____/                                        **
**                                                                      **
** $Id$
\*                                                                      */

package scalac.symtab.classfile;

import scala.tools.util.Position;
import scalac.util.Name;
import scalac.symtab.Modifiers;
import scalac.symtab.Symbol;
import scalac.symtab.Scope;
import scalac.symtab.Type;
import scalac.ApplicationError;
import scalac.util.Debug;

import java.util.Vector;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;

/** a parser class for parsing meta type information in classfiles
 *  generated by pico.
 */
class MetaParser {
    private final Symbol owner;
    private final StringTokenizer scanner;
    private final Type defaultType;
    private String token;
    private final Scope tvars;
    private Scope locals;
    private final Symbol clazz;
    private final Type ctype;
    private final JavaTypeFactory make;

    MetaParser(String meta, Scope tvars, Symbol owner, Type defaultType,
               Symbol clazz, Type ctype, JavaTypeFactory make) {
        this.scanner = new StringTokenizer(meta, "()[], \t<;", true);
        this.defaultType = defaultType;
        this.owner = owner;
        this.tvars = tvars;
        this.clazz = clazz;
        this.ctype = ctype;
        this.make = make;
    }

    private static Name getTypeName(String name) {
        return Name.fromString(name).toTypeName();
    }

    private Symbol getTVar(String name) {
        return getTVar(name, clazz.primaryConstructor());
    }

    private Symbol getTVar(String name, Symbol owner) {
        if (name.startsWith("?")) {
            Symbol s = (locals != null ? locals : tvars)
                .lookup(getTypeName(name));
            if (s != Symbol.NONE)
                return s;
            else if (locals != null) {
                s = tvars.lookup(getTypeName(name));
                if (s != Symbol.NONE)
                    return s;
            }
            s = owner.newTParam
                (Position.NOPOS, 0, getTypeName(token), make.anyType());
            tvars.enter(s);
            return s;
        } else
            return Symbol.NONE;
    }

    private String nextToken() {
        do {
            token = scanner.nextToken().trim();
        } while (token.length() == 0);
        return token;
    }

    protected Type parse() {
        if (scanner.hasMoreTokens()) {
            nextToken();
            if (!scanner.hasMoreTokens())
                return defaultType;
            if ("class".equals(token))
                return parseMetaClass();
            if ("method".equals(token))
                return parseMetaMethod();
            if ("field".equals(token))
                return parseMetaField();
            if ("constr".equals(token))
                return parseConstrField();
        }
        return defaultType;
    }

    protected Type parseMetaClass() {
        nextToken();
        //System.out.println("parse meta class " + token);//DEBUG
        if ("[".equals(token)) {
            try {
                Vector syms = new Vector();
                do {
                    nextToken();
                    int vflag = 0;
                    if (token.equals("+")) {
                        nextToken();
                        vflag = Modifiers.COVARIANT;
                    } else if (token.equals("-")) {
                        nextToken();
                        vflag = Modifiers.CONTRAVARIANT;
                    }
                    assert token.startsWith("?");
                    Symbol s = getTVar(token);
                    if (s == Symbol.NONE)
                        return defaultType;
                    s.flags |= vflag;
                    nextToken();
                    //System.out.println("new var " + s + ", " + token);//DEBUG
                    if (token.equals("<")) {
                        nextToken();
                        s.setInfo(parseType());
                    }
                    syms.add(s);
                } while (token.equals(","));
                assert "]".equals(token);
                nextToken();
                Symbol[] smbls = (Symbol[])syms.toArray(new Symbol[syms.size()]);
                //System.out.println("*** " + syms);//DEBUG
                Type clazztype = Type.appliedType(ctype, Symbol.type(smbls));
                Symbol constr = clazz.primaryConstructor();
                switch (constr.rawInfo()) {
                case MethodType(Symbol[] vparams, _):
                    constr.setInfo(Type.PolyType
                                   (smbls, Type.MethodType(vparams, clazztype)));
                    break;
                default:
                    throw new ApplicationError(constr.rawInfo());
                }
            } catch (NoSuchElementException e) {
            }
        }
        Type res = defaultType;
        if ("extends".equals(token)) {
            Vector basetpes = new Vector();
            do {
                nextToken();
                basetpes.add(parseType());
            } while (token.equals("with"));
            switch (defaultType) {
            case CompoundType(_, Scope scope):
                Type[] ts = (Type[])basetpes.toArray(new Type[basetpes.size()]);
                res = Type.compoundType(ts, scope, defaultType.symbol());
            }
        }
        assert ";".equals(token);
        return res;
    }

    protected Type parseType() {
        String name = token;
        Symbol s = getTVar(name);
        nextToken();
        if (s != Symbol.NONE)
            return s.type();
        Type clazztype = make.classType(name).unalias();
        if (token.equals("[")) {
            Vector types = new Vector();
            do {
                nextToken();
                types.add(parseType());
            } while (token.equals(","));
            assert "]".equals(token);
            nextToken();
            Type[] args = new Type[types.size()];
            types.toArray(args);
            return Type.appliedType(clazztype, args);
        } else {
            return clazztype;
        }
    }

    protected Type parseMetaMethod() {
        locals = new Scope();
        try {
            nextToken();
            Symbol[] smbls = null;
            //System.out.println("parse meta method " + token);
            if ("[".equals(token)) {
                Vector syms = new Vector();
                do {
                    nextToken();
                    if ("]".equals(token))
                        break;
                    assert token.startsWith("?");
                    Symbol s = owner.newTParam
                        (Position.NOPOS, 0, getTypeName(token), make.anyType());
                    locals.enter(s);
                    nextToken();
                    if (token.equals("<")) {
                        nextToken();
                        s.setInfo(parseType());
                    }
                    syms.add(s);
                } while (token.equals(","));
                assert "]".equals(token);
                nextToken();
                smbls = (Symbol[])syms.toArray(new Symbol[syms.size()]);
            }
            if ("(".equals(token)) {
                int i = 0;
                Vector params = new Vector();
                do {
                    nextToken();
                    if (")".equals(token))
                        break;
                    int flags = 0;
                    if ("def".equals(token)) {
                        nextToken();
                        flags |= Modifiers.DEF;
                    }
                    Symbol vp = owner.newVParam
                        (Position.NOPOS, flags, Name.fromString("x" + (i++)));
                    params.add(vp.setInfo(parseType()));
                    //System.out.println("  + " + token);
                } while (token.equals(","));
                assert ")".equals(token);
                nextToken();
                //System.out.println("+++ method " + token);
                Type restpe = parseType();
                assert ";".equals(token);
                Type mtype = Type.MethodType
                    ((Symbol[])params.toArray(new Symbol[params.size()]),
                     restpe);
                return smbls == null ? mtype : Type.PolyType(smbls, mtype);
            } else {
                Type res = parseType();
                assert ";".equals(token);
                return Type.PolyType
                    (smbls == null ? Symbol.EMPTY_ARRAY : smbls, res);
            }
        } catch (NoSuchElementException e) {
            e.printStackTrace();
            return defaultType;
        } finally {
            locals = null;
        }
    }

    protected Type parseMetaField() {
        nextToken();
        return parseType();
    }

    protected Type parseConstrField() {
        try {
            nextToken();
            //System.out.println("+++ constr " + token);
            if ("(".equals(token)) {
                int i = 0;
                Vector params = new Vector();
                do {
                    nextToken();
                    if (")".equals(token))
                        break;
                    Symbol vp = owner.newVParam
                        (Position.NOPOS, 0, Name.fromString("x" + (i++)));
                    params.add(vp.setInfo(parseType()));
                    //System.out.println("  + " + token);
                } while (token.equals(","));
                assert ")".equals(token);
                nextToken();
                assert ";".equals(token);
                return Type.MethodType((Symbol[])params.toArray(new Symbol[params.size()]),
                                       ctype);
            } else {
                assert ";".equals(token);
                return Type.PolyType(Symbol.EMPTY_ARRAY, ctype);
            }
        } catch (NoSuchElementException e) {
            return defaultType;
        }
    }
}