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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
** **
** $Id$
\* */
package scalac.symtab.classfile;
import ch.epfl.lamp.util.Position;
import scalac.*;
import scalac.symtab.*;
import scalac.util.*;
import java.util.*;
public class AttributeParser implements ClassfileConstants {
/** the classfile input buffer
*/
protected AbstractFileReader in;
/** the constant pool
*/
protected ConstantPool pool;
protected ClassfileParser parser;
/** constructor
*/
public AttributeParser(AbstractFileReader in, ConstantPool pool, ClassfileParser parser) {
this.in = in;
this.pool = pool;
this.parser = parser;
}
public int nameToId(Name name) {
if (name == SOURCEFILE_N)
return SOURCEFILE_ATTR;
if (name == SYNTHETIC_N)
return SYNTHETIC_ATTR;
if (name == DEPRECATED_N)
return DEPRECATED_ATTR;
if (name == CODE_N)
return CODE_ATTR;
if (name == EXCEPTIONS_N)
return EXCEPTIONS_ATTR;
if (name == CONSTANT_VALUE_N)
return CONSTANT_VALUE_ATTR;
if (name == LINE_NUM_TABLE_N)
return LINE_NUM_TABLE_ATTR;
if (name == LOCAL_VAR_TABLE_N)
return LOCAL_VAR_TABLE_ATTR;
if (name == INNERCLASSES_N)
return INNERCLASSES_ATTR;
if (name == META_N)
return META_ATTR;
if (name == SCALA_N)
return SCALA_ATTR;
return BAD_ATTR;
}
public Symbol readAttributes(Symbol def, Type type, int attrs) {
char nattr = in.nextChar();
for (int i = 0; i < nattr; i++) {
Name attrName = (Name)pool.readPool(in.nextChar());
int attr = nameToId(attrName);
int attrLen = in.nextInt();
if ((attrs & attr) == 0) {
//System.out.println("# skipping " + attrName + " of " + def);
in.skip(attrLen);
} else {
//System.out.println("# reading " + attrName + " of " + def);
readAttribute(def, type, attr, attrLen);
}
}
return def;
}
public void readAttribute(Symbol sym, Type type, int attr, int attrLen) {
switch (attr) {
// class attributes
case SCALA_ATTR:
new UnPickle(sym, in.nextBytes(attrLen), Name.fromString(in.path));
return;
case INNERCLASSES_ATTR:
/* int n = in.nextChar();
for (int i = 0; i < n; i++) {
Symbol inner = (Symbol)pool.readPool(in.nextChar());
Symbol outer = (Symbol)pool.readPool(in.nextChar());
Name name = (Name)pool.readPool(in.nextChar());
int flags = in.nextChar();
if (name != null) {
inner.owner(outer);
inner.mangled(name);
inner.flags = flags;
}
} */
in.skip(attrLen);
return;
// method attributes
case CODE_ATTR:
in.skip(attrLen);
return;
case EXCEPTIONS_ATTR:
//int nexceptions = in.nextChar();
//Type[] thrown = new Type[nexceptions];
//for (int j = 0; j < nexceptions; j++)
// thrown[j] = make.classType(reader.readClassName(in.nextChar()));
//((MethodType)def.type).thrown = thrown;
in.skip(attrLen);
return;
case LINE_NUM_TABLE_ATTR:
in.skip(attrLen);
return;
case LOCAL_VAR_TABLE_ATTR:
in.skip(attrLen);
return;
// general attributes
case SYNTHETIC_ATTR:
sym.flags |= Modifiers.SYNTHETIC;
return;
case DEPRECATED_ATTR:
sym.flags |= Modifiers.DEPRECATED;
return;
case CONSTANT_VALUE_ATTR:
// Type ctype = (Type)reader.readPool(in.nextChar());
// def.type = types.coerce(ctype, def.type);
in.skip(attrLen);
return;
case META_ATTR:
//System.out.println("parsing meta data for " + sym);
String meta = pool.readPool(in.nextChar()).toString().trim();
sym.setInfo(
new MetaParser(meta, tvars, sym, type).parse(),
Symbol.FIRST_ID);
return;
default:
in.skip(attrLen);
return;
}
}
Scope tvars = new Scope();
class MetaParser {
Symbol owner;
StringTokenizer scanner;
Type defaultType;
String token;
Scope tvars;
Scope locals;
MetaParser(String meta, Scope tvars, Symbol owner, Type defaultType) {
//System.out.println("meta = " + meta);
this.scanner = new StringTokenizer(meta, "()[], \t<;", true);
this.defaultType = defaultType;
this.owner = owner;
this.tvars = tvars;
}
private Symbol getTVar(String name) {
return getTVar(name, parser.c.primaryConstructor());
}
private Symbol getTVar(String name, Symbol owner) {
if (name.startsWith("?")) {
Symbol s = ((locals != null) ? locals : tvars)
.lookup(Name.fromString(name).toTypeName());
if (s != Symbol.NONE)
return s;
else if (locals != null) {
s = tvars.lookup(Name.fromString(name).toTypeName());
if (s != Symbol.NONE)
return s;
}
s = new AbsTypeSymbol(
Position.NOPOS,
Name.fromString(token).toTypeName(),
owner,
Modifiers.PARAM);
s.setInfo(parser.defs.ANY_TYPE, Symbol.FIRST_ID);
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(), Symbol.FIRST_ID);
}
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(
parser.ctype, Symbol.type(smbls));
Symbol constr = parser.c.primaryConstructor();
switch (constr.rawInfo()) {
case MethodType(Symbol[] vparams, _):
constr.setInfo(
Type.PolyType(
smbls, Type.MethodType(vparams, clazztype)),
Symbol.FIRST_ID);
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):
res = Type.compoundType(
(Type[])basetpes.toArray(new Type[basetpes.size()]),
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();
Symbol clazz = parser.defs.getClass(Name.fromString(name));
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.TypeRef(clazz.owner().thisType(), clazz, args).unalias();
} else {
return clazz.typeConstructor().unalias();
}
}
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 = new AbsTypeSymbol(
Position.NOPOS,
Name.fromString(token).toTypeName(),
owner,
Modifiers.PARAM);
s.setInfo(parser.defs.ANY_TYPE, Symbol.FIRST_ID);
locals.enter(s);
nextToken();
if (token.equals("<")) {
nextToken();
s.setInfo(parseType(), Symbol.FIRST_ID);
}
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 = Modifiers.PARAM;
if ("def".equals(token)) {
nextToken();
flags |= Modifiers.DEF;
}
params.add(new TermSymbol(
Position.NOPOS,
Name.fromString("x" + (i++)),
owner,
flags).setInfo(parseType(), Symbol.FIRST_ID));
//System.out.println(" + " + token);
} while (token.equals(","));
assert ")".equals(token);
nextToken();
//System.out.println("+++ method " + token);
Type restpe = parseType();
assert ";".equals(token);
if (smbls == null)
return Type.MethodType(
(Symbol[])params.toArray(new Symbol[params.size()]),
restpe);
else
return Type.PolyType(
smbls,
Type.MethodType(
(Symbol[])params.toArray(new Symbol[params.size()]),
restpe));
} else {
Type res = parseType();
assert ";".equals(token);
if (smbls == null)
return Type.PolyType(Symbol.EMPTY_ARRAY, res);
else
return Type.PolyType(smbls, res);
}
} catch (NoSuchElementException e) {
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;
params.add(new TermSymbol(
Position.NOPOS,
Name.fromString("x" + (i++)),
owner,
Modifiers.PARAM).setInfo(parseType(), Symbol.FIRST_ID));
//System.out.println(" + " + token);
} while (token.equals(","));
assert ")".equals(token);
nextToken();
assert ";".equals(token);
return Type.MethodType(
(Symbol[])params.toArray(new Symbol[params.size()]),
parser.ctype);
} else {
assert ";".equals(token);
return Type.PolyType(Symbol.EMPTY_ARRAY, parser.ctype);
}
} catch (NoSuchElementException e) {
return defaultType;
}
}
}
}
|