summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/classfile/Pickle.java
blob: bdc552e507808e477093b6c70a80932df442f19f (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
465
466
467
468
469
470
471
472
473
474
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.symtab.classfile;

import ch.epfl.lamp.util.Position;
import java.util.HashMap;
import java.io.*;
import scalac.Global;
import scalac.ApplicationError;
import scalac.util.*;
import scalac.symtab.*;
import Symbol.*;
import Type.*;

public class Pickle implements Kinds, Modifiers, EntryTags {

    final static boolean debug = false;

/***************************************************
 * Symbol table attribute format: see EntryTags.java
 */
    public byte[] bytes;
    private int bp;

    private Name rootname;
    private Symbol rootowner;
    private HashMap index;
    private Object[] entries;
    private int ep;

    /** Write symbol table info for root.
     *  root must be either a module or a class.
     */
    public Pickle() {
	index = new HashMap();
	entries = new Object[256];
	ep = 0;
    }

    /** Pickle all symbols descending from `root'.
     */
    public void add(Symbol root) {
	if (!root.isExternal()) {
	    if (Global.instance.debug) System.out.println("pickling " + root);
	    if (index.get(root) == null) {
		this.rootname = root.name.toTermName();
		this.rootowner = root.owner();
		putSymbol(root);
	    }
	}
    }

    /** Finalize pickler with given fullname.
     */
    public void pickle() {
	bytes = new byte[4096];
	bp = 0;
	writeAttr();
	this.index = null;
	this.entries = null;
    }

    /** The number of elements defined in `bytes'.
     */
    public int size() {
	return bp;
    }

/* **************************************************
 * Phase 1: Build entry table
 ************************************************* */

    /** Is root in symbol.owner*?
     */
    private boolean isLocal(Symbol sym) {
	return
	    sym.name.toTermName() == rootname && sym.owner() == rootowner
	    ||
	    sym.isConstructor() && isLocal(sym.constructorClass())
	    ||
	    (sym.kind != NONE && isLocal(sym.owner()));
    }

    /** Store entry `e' in index at next available position unless it it
     *  already there. Return true iff entry is new.
     */
    private boolean putEntry(Object e) {
	Integer n = (Integer) index.get(e);
	if (n == null) {
	    //System.out.println("entry " + e);//DEBUG
	    if (ep == entries.length) {
		Object[] entries1 = new Object[ep * 2];
		System.arraycopy(entries, 0, entries1, 0, ep);
		entries = entries1;
	    }
	    entries[ep] = e;
	    index.put(e, new Integer(ep));
	    ep++;
	    return true;
	} else {
	    return false;
	}
    }

    /** Store symbol in index. If symbol is local, also store
     *  everything it refers to.
     */
    private void putSymbol(Symbol sym) {
	if (putEntry(sym)) {
	    if (debug) System.out.println("put " + sym);
	    if (isLocal(sym)) {
		putEntry(sym.name);
		putSymbol(sym.owner());
		putType(sym.info());
		switch (sym.kind) {
		case TYPE:
		    putType(sym.loBound());
		    break;
		case ALIAS:
		    putSymbol(sym.allConstructors());
		    break;
		case CLASS:
		    putType(sym.typeOfThis());
		    putSymbol(sym.allConstructors());
		    for (Scope.SymbolIterator it = sym.members().iterator();
			 it.hasNext();)
			putSymbol(it.next());
		    break;
		case VAL:
		    if (sym.isPrimaryConstructor())
			putSymbol(sym.constructorClass());
		    else if (sym.isModule())
			putSymbol(sym.moduleClass());
		    break;
		default:
		    throw new ApplicationError();
		}
	    } else if (sym.kind != NONE) {
		putEntry(sym.isModuleClass() ? sym.name.toTermName() : sym.name);
		if (sym.owner() != Global.instance.definitions.ROOT_CLASS)
		    putSymbol(sym.owner());
	    }
	}
    }

    private void putSymbols(Symbol[] syms) {
	for (int i = 0; i < syms.length; i++)
	    putSymbol(syms[i]);
    }

    /** Store type and everythig it refers to in index.
     */
    private void putType(Type tp) {
	if (putEntry(tp)) {
	    switch (tp) {
	    case NoType:
		break;
	    case ThisType(Symbol sym):
		putSymbol(sym);
		break;
	    case SingleType(Type pre, Symbol sym):
		putType(pre);
		putSymbol(sym);
		break;
	    case TypeRef(Type pre, Symbol sym, Type[] args):
		putType(pre);
		putSymbol(sym);
		putTypes(args);
		break;
	    case CompoundType(Type[] parents, Scope members):
		if (!tp.symbol().isCompoundSym())
		    putSymbol(tp.symbol());
		putTypes(parents);
		break;
	    case MethodType(Symbol[] vparams, Type result):
		putType(result);
		for (int i = 0; i < vparams.length; i++) {
		    Type ptype = vparams[i].type();
		    putType(ptype);
		    int pflags = vparams[i].flags;
		    if ((pflags & (REPEATED | DEF)) != 0)
			putEntry(new FlagsAndType(encodeFlags(pflags), ptype));
		}
		break;
	    case PolyType(Symbol[] tparams, Type result):
		putType(result);
		putSymbols(tparams);
		break;
	    case OverloadedType(Symbol[] alts, Type[] alttypes):
		for (int i = 0; i < alts.length; i++) alts[i].flags |= ALTERNATIVE;
		putSymbols(alts);
		putTypes(alttypes);
		break;
	    default:
		throw new ApplicationError();
	    }
	}
    }

    private void putTypes(Type[] tps) {
	for (int i = 0; i < tps.length; i++)
	    putType(tps[i]);
    }

/* **************************************************
 * Phase 2: Write byte array
 ************************************************* */

    private void resizeTo(int size) {
	byte[] bytes1 = new byte[size];
	System.arraycopy(bytes, 0, bytes1, 0, bp);
	bytes = bytes1;
    }

    /** Write a byte of data
     */
    private void writeByte(int b) {
	if (bp == bytes.length) resizeTo(bytes.length * 2);
	bytes[bp++] = (byte)b;
	if (debug) System.out.print(b + " ");
    }

    /** Write a natural number in big endian format, base 128.
     *  All but the last digits have bit 0x80 set.
     */
    private void writeNat(int x) {
	int y = x >>> 7;
	if (y != 0) writeNatPrefix(y);
	writeByte(x & 0x7f);
    }

    private void writeNatPrefix(int x) {
	int y = x >>> 7;
	if (y != 0) writeNatPrefix(y);
	writeByte((x & 0x7f) | 0x80);
    }

    /** Write a natural number at `pos'
     *  If number is more than one byte, shift rest of array to make space.
     */
    private void patchNat(int pos, int x) {
	bytes[pos] = (byte) (x & 0x7f);
	int y = x >>> 7;
	if (y != 0) patchNatPrefix(pos, y);
    }

    private void patchNatPrefix(int pos, int x) {
	writeByte(0);
	System.arraycopy(bytes, pos, bytes, pos+1, bp - (pos+1));
	bytes[pos] = (byte) ((x & 0x7f) | 0x80);
	int y = x >>> 7;
	if (y != 0) patchNatPrefix(pos, y);
    }

    /** Write a reference to object, i.e., the object's number in the index.
     */
    private void writeRef(Object ref) {
	Integer i = (Integer) index.get(ref);
	assert i != null : ref + " " + ref.getClass();
	writeNat(i.intValue());
    }

    private void writeRefs(Object[] es) {
	for (int i = 0; i < es.length; i++) writeRef(es[i]);
    }

    /** Write a name entry. Names are stored in Utf8 format.
     */
    private void writeName(Name name) {
	writeByte(name.isTermName() ? TERMname : TYPEname);
	writeByte(0); // space for length
	while (bp + name.length() > bytes.length) resizeTo(bytes.length * 2);
	name.copyAscii(bytes, bp);
	if (debug) System.out.print(name);
	bp = bp + name.length();
    }

    /** Write a symbol entry.
     */
    private void writeSymbol(Symbol sym) {
	if (debug) System.out.println("write " + sym);
	if (isLocal(sym)) {
	    switch (sym.kind) {
	    case TYPE:
		writeByte(TYPEsym);
		break;
	    case ALIAS:
		writeByte(ALIASsym);
		break;
	    case CLASS:
		writeByte(CLASSsym);
		break;
	    case VAL:
		writeByte(VALsym);
		break;
	    default:
		throw new ApplicationError();
	    }
	    writeByte(0); // space for length
	    writeRef(sym.name);
	    writeRef(sym.owner());
	    writeNat(sym.flags);
	    writeRef(sym.info());
	    switch (sym.kind) {
	    case TYPE:
		writeRef(sym.loBound());
		break;
	    case ALIAS:
		writeRef(sym.allConstructors());
		break;
	    case CLASS:
		writeRef(sym.typeOfThis());
		writeRef(sym.allConstructors());
		break;
	    case VAL:
		if (sym.isPrimaryConstructor())
		    writeRef(sym.constructorClass());
		else if (sym.isModule())
		    writeRef(sym.moduleClass());
		break;
	    }
	} else if (sym.kind == NONE) {
	    writeByte(NONEsym);
	    writeByte(0); // space for length
	} else {
	    if (sym.isModuleClass()) {
		writeByte(EXTMODCLASSref);
		writeByte(0); // space for length
		writeRef(sym.name.toTermName());
	    } else {
		writeByte(EXTref);
		writeByte(0); // space for length
		assert !sym.isConstructor() : sym;
		writeRef(sym.name);
	    }
	    if (sym.owner() != Global.instance.definitions.ROOT_CLASS)
		writeRef(sym.owner());
	}
	sym.flags &= ~ALTERNATIVE;
    }

    /** Write a type entry.
     */
    private void writeType(Type tp) {
	switch (tp) {
	case NoType:
	    writeByte(NOtpe);
	    writeByte(0); // space for length
	    break;

	case ThisType(Symbol sym):
	    writeByte(THIStpe);
	    writeByte(0); // space for length
	    writeRef(sym);
	    break;

	case SingleType(Type pre, Symbol sym):
	    writeByte(SINGLEtpe);
	    writeByte(0); // space for length
	    writeRef(pre);
	    writeRef(sym);
	    break;

	case TypeRef(Type pre, Symbol sym, Type[] args):
	    writeByte(TYPEREFtpe);
	    writeByte(0); // space for length
	    writeRef(pre);
	    writeRef(sym);
	    writeRefs(args);
	    break;

	case CompoundType(Type[] parents, Scope members):
	    writeByte(COMPOUNDtpe);
	    writeByte(0); // space for length
	    if (!tp.symbol().isCompoundSym())
		writeRef(tp.symbol());
	    writeRefs(parents);
	    break;

	case MethodType(Symbol[] vparams, Type result):
	    writeByte(METHODtpe);
	    writeByte(0); // space for length
	    writeRef(result);
	    for (int i = 0; i < vparams.length; i++) {
		Type ptype = vparams[i].type();
		int pflags = vparams[i].flags;
		if ((pflags & (REPEATED | DEF)) != 0)
		    writeRef(new FlagsAndType(encodeFlags(pflags), ptype));
		else
		    writeRef(ptype);
	    }
	    break;

	case PolyType(Symbol[] tparams, Type result):
	    writeByte(POLYtpe);
	    writeByte(0); // space for length
	    writeRef(result);
	    writeRefs(tparams);
	    break;

	case OverloadedType(Symbol[] alts, Type[] alttypes):
	    writeByte(OVERLOADEDtpe);
	    writeByte(0); // space for length
	    writeRefs(alts);
	    writeRefs(alttypes);
	    break;

	default:
	    throw new ApplicationError();
	}
    }

    private void writeFlagsAndType(FlagsAndType ft) {
	writeByte(FLAGGEDtpe);
	writeByte(0); // space for length
	writeNat(ft.flags);
	writeRef(ft.type);
    }

    private void writeEntry(Object e) {
	int startpos = bp;
	if (e instanceof Symbol) writeSymbol((Symbol) e);
	else if (e instanceof Type) writeType((Type) e);
	else if (e instanceof Name) writeName((Name) e);
	else if (e instanceof FlagsAndType) writeFlagsAndType((FlagsAndType) e);
	else throw new ApplicationError();
	patchNat(startpos + 1, bp - (startpos + 2));
    }

    private void writeAttr() {
	writeNat(ep);
	for (int i = 0; i < ep; i++) {
	    if (debug) System.out.print(i + "," + bp + ": ");
	    writeEntry(entries[i]);
	    if (debug) System.out.print("(" + entries[i] + ")");
	    if (debug) System.out.println();
	}
    }

    private static int encodeFlags(int flags) {
	int n = 0;
	if ((flags & REPEATED) != 0) n |= REPEATEDflag;
	if ((flags & DEF) != 0) n |= DEFflag;
	return n;
    }

    static class FlagsAndType {
	int flags;
	Type type;
	FlagsAndType(int flags, Type type) {
	    this.flags = flags;
	    this.type = type;
	}
	public boolean equals(Object other) {
	    if (other instanceof FlagsAndType) {
		FlagsAndType that = (FlagsAndType) other;
		return this.type.equals(that.type) &&
		    this.flags == that.flags;
	    } else {
		return false;
	    }
	}
	public int hashCode() {
	    return 37 + ((flags * 41) ^ type.hashCode());
	}
    }
}