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

package scalac.symtab.classfile;

import scalac.*;
import scalac.symtab.*;
import scalac.util.*;

public class ConstantPool implements ClassfileConstants {

    AbstractFileReader in;
    Signatures sigparser;

    /** the objects of the constant pool
     */
    protected Object[] poolObj;

    /** for every constant pool entry, an index into in.buf where the
     *  defining section of the entry is found
     */
    protected int[] poolIdx;

    /** constructor
     */
    protected ConstantPool(AbstractFileReader in, Signatures sigparser) {
        this.in = in;
        this.sigparser = sigparser;
    }

    /** index all constant pool entries, writing their start
     *  addresses into poolIdx
     */
    public void indexPool() {
        poolIdx = new int[in.nextChar()];
        poolObj = new Object[poolIdx.length];
        int i = 1;
        while (i < poolIdx.length) {
            poolIdx[i++] = in.bp;
            byte tag = in.nextByte();
            switch (tag) {
                case CONSTANT_UTF8:
                case CONSTANT_UNICODE: {
                    int len = in.nextChar();
                    in.skip(len);
                    break;
                }
                case CONSTANT_CLASS:
                case CONSTANT_STRING:
                    in.skip(2);
                    break;
                case CONSTANT_FIELDREF:
                case CONSTANT_METHODREF:
                case CONSTANT_INTFMETHODREF:
                case CONSTANT_NAMEANDTYPE:
                case CONSTANT_INTEGER:
                case CONSTANT_FLOAT:
                    in.skip(4);
                    break;
                case CONSTANT_LONG:
                case CONSTANT_DOUBLE:
                    in.skip(8);
                    i++;
                    break;
                default:
                    throw new RuntimeException("bad constant pool tag: " + tag +
                        " at " + (in.bp - 1));
            }
        }
    }

    /** if name is an array type or class signature, return the
     *  corresponding type; otherwise return a class definition with given name
     */
    protected Object classOrType(Name name) {
        if ((name.sub(0) == '[') || (name.sub(name.length() - 1) == ';')) {
            byte[] ascii = name.toAscii();
            return sigparser.sigToType(ascii, 0, ascii.length);
        } else
            return name;
    }

    /** read constant pool entry at start address i, use poolObj as a cache.
     */
    public Object readPool(int i) {
        if (poolObj[i] != null)
            return poolObj[i];
        int index = poolIdx[i];
        if (index == 0)
            return null;
        switch (in.byteAt(index)) {
            case CONSTANT_UTF8:
                poolObj[i] = Name.fromAscii(in.buf, index + 3, in.getChar(index + 1));
                break;

            case CONSTANT_UNICODE:
                throw new RuntimeException("can't read unicode strings in classfiles");

            case CONSTANT_CLASS:
                poolObj[i] = classOrType(readExternal(in.getChar(index + 1)));
                break;

            case CONSTANT_FIELDREF: {
                //Symbol owner = (Symbol)readPool(in.getChar(index + 1));
                //NameAndType nt = (NameAndType)readPool(in.getChar(index + 3));
                //poolObj[i] = new TermSymbol(Kinds.VAR, Position.NOPOS, nt.name, owner, 0)
                //    .type(sigparser.sigToType(Name.names, nt.sig.index, nt.sig.length()));
                throw new RuntimeException("can't read constant_fieldrefs in classfiles");
            }

            case CONSTANT_METHODREF:
            case CONSTANT_INTFMETHODREF: {
                //Symbol owner = (Symbol)readPool(in.getChar(index + 1));
                //NameAndType nt = (NameAndType)readPool(in.getChar(index + 3));
                //poolObj[i] = new TermSymbol(Kinds.FUN, Position.NOPOS, nt.name, owner, 0)
                //        .type(sigparser.sigToType(Name.names, nt.sig.index, nt.sig.length()));
                throw new RuntimeException("can't read constant_methodrefs in classfiles");
            }

            case CONSTANT_NAMEANDTYPE:
                poolObj[i] = new NameAndType((Name)readPool(in.getChar(index + 1)),
                                             readExternal(in.getChar(index + 3)));
                break;

            case CONSTANT_STRING:
            case CONSTANT_INTEGER:
            case CONSTANT_FLOAT:
            case CONSTANT_LONG:
            case CONSTANT_DOUBLE:
                throw new RuntimeException("can't read constants in classfiles");

            default:
                throw new RuntimeException("bad constant pool tag: " + in.byteAt(index));
        }
        return poolObj[i];
    }

    /** return internal representation of buf[offset..offset+len-1],
     *  converting '/' to '.'
     */
    public byte[] internalize(byte[] buf, int offset, int len) {
        byte[] translated = new byte[len];
        for (int j = 0; j < len; j++) {
            byte b = buf[offset + j];
            if (b == '/')
                translated[j] = '.';
            else
                translated[j] = b;
        }
        return translated;
    }

    /** read a constant pool string and convert to internal representation.
     */
    public Name readExternal(int i) {
        if (poolObj[i] == null) {
            int index = poolIdx[i];
            if (in.byteAt(index) == CONSTANT_UTF8) {
                int len = in.getChar(index + 1);
                byte[] translated = internalize(in.buf, index + 3, len);
                poolObj[i] = Name.fromAscii(translated, 0, len);
            }
        }
        return (Name)poolObj[i];
    }

    /** the name and type signature of a method or field
     */
    public static final class NameAndType {
        public Name name;
        public Name sig;

        public NameAndType(Name name, Name sig) {
            this.name = name;
            this.sig = sig;
        }
    }
}