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

package scalac.util;

public final class Name {

/** address in the name memory
 */
    public int index;

/** length of the name
 */
    private int len;

/** next name in the same hash bucket
 */
    private Name next;

    private final static int HASH_SIZE = 0x8000;
    private final static int HASH_MASK = 0x7FFF;
    private final static int NAME_SIZE = 0x20000;

/** memory to store all names sequentially
 */
    public static byte[] names = new byte[NAME_SIZE];
    private static int nc = 0;

/** hashtable for finding term names quickly
 */
    private static Name[] termHashtable = new Name[HASH_SIZE];

/** hashtable for finding type names quickly
 */
    private static Name[] typeHashtable = new Name[HASH_SIZE];

/** Constructor
 */
    Name(int index, int len, Name[] table, int hash) {
	this.index = index;
	this.len = len;
	this.next = table[hash];
	table[hash] = this;
    }

/** the hashcode of a name
 */
    private static int hashValue(byte cs[], int offset, int len) {
        if (len > 0)
            return
                len * (41 * 41 * 41) +
                cs[offset] * (41 * 41) +
                cs[offset + len - 1] * 41 +
                cs[offset + (len >> 1)];
        else
            return 0;
    }

/** is (the ascii representation of) name equal to
 *  cs[offset..offset+len-1]?
 */
    private static boolean equals(int index, byte cs[], int offset, int len) {
        int i = 0;
        while ((i < len) && (names[index + i] == cs[offset + i]))
            i++;
        return i == len;
    }

/** create a term name from the bytes in cs[offset..offset+len-1].
 *  assume that bytes are in ascii format.
 */
    public static Name fromAscii(byte cs[], int offset, int len) {
        int     h = hashValue(cs, offset, len) & HASH_MASK;
        Name    n = termHashtable[h];
        while ((n != null) && (n.len != len || !equals(n.index, cs, offset, len)))
            n = n.next;
        if (n == null) {
            n = new Name(nc, len, termHashtable, h);
            for (int i = 0; i < len; i++)
            {
                if (nc == names.length)
                {
                    byte[] newnames = new byte[names.length * 2];
                    System.arraycopy(names, 0, newnames, 0, names.length);
                    names = newnames;
                }
                names[nc++] = cs[offset + i];
            }
            if (len == 0)
                nc++;
        }
        return n;
    }

/** create a name from the bytes in cs[offset..offset+len-1];
 *  assume that characters are in source format
 */
    public static Name fromSource(byte cs[], int offset, int len) {
        byte[]  ascii = new byte[len * 2];
        int alen = SourceRepresentation.source2ascii(cs, offset, len, ascii);
        return fromAscii(ascii, 0, alen);
    }

/** create a name from the characters in string s
 */
    public static Name fromString(String s) {
        byte[] source = SourceRepresentation.string2source(s);
        return fromSource(source, 0, source.length);
    }

/** copy bytes of this name to buffer cs, starting at offset
 */
    public void copyAscii(byte cs[], int offset) {
        System.arraycopy(names, index, cs, offset, len);
    }

/** return the ascii representation of this name
 */
    public byte[] toAscii() {
        byte[]  ascii = new byte[len];
        System.arraycopy(names, index, ascii, 0, len);
        return ascii;
    }

/** return the source representation of this name
 */
    public byte[] toSource() {
        return SourceRepresentation.string2source(toString());
    }

/** return the string representation of this name
 */
    public String toString() {
	return SourceRepresentation.ascii2string(names, index, len);
    }

/** is this name a term name?
 */
    public boolean isTermName() {
	int  h = hashValue(names, index, len) & HASH_MASK;
	Name n = termHashtable[h];
        while (n != null && n != this)
	    n = n.next;
	return n == this;
    }

/** is this name a type name?
 */
    public boolean isTypeName() {
	int  h = hashValue(names, index, len) & HASH_MASK;
	Name n = typeHashtable[h];
        while (n != null && n != this)
	    n = n.next;
	return n == this;
    }

/** create a term name corresponding to this name
 */
    public Name toTermName() {
	int  h = hashValue(names, index, len) & HASH_MASK;
        Name n = termHashtable[h];
        while (n != null && n.index != index)
            n = n.next;
	if (n == null) {
	    n = new Name(index, len, termHashtable, h);
	}
	return n;
    }

/** create a type name corresponding to this name
 */
    public Name toTypeName() {
	int  h = hashValue(names, index, len) & HASH_MASK;
        Name n = typeHashtable[h];
        while (n != null && n.index != index)
            n = n.next;
	if (n == null) {
	    n = new Name(index, len, typeHashtable, h);
	}
	return n;
    }

/** return the string hash value of this name
 */
    public int hashCode() {
        return index;
    }

/** returns the length of this name
 */
    public int length() {
        return len;
    }

/** returns i'th byte of this name
 */
    public byte sub(int i) {
        return names[index + i];
    }

/** returns first occurrence of byte b in this name, len if not found
 */
    public int pos(byte b) {
	return pos(b, 0);
    }

/** returns first occurrence of byte b in this name from `start', len if not found
 */
    public int pos(byte b, int start) {
        int i = start;
        while (i < len && names[index + i] != b)
            i++;
        return i;
    }

/** returns last occurrence of byte b in this name, -1 if not found.
 */
    public int lastPos(byte b) {
        int i = len - 1;
        while (i >= 0 && names[index + i] != b)
            i--;
        return i;
    }

/** does this name start with prefix?
 */
    public boolean startsWith(Name prefix) {
        int i = 0;
        while ((i < prefix.len) &&
                (i < len) &&
                (names[index + i] == names[prefix.index + i]))
            i++;
        return i == prefix.len;
    }

/** does this name end with suffix?
 */
    public boolean endsWith(Name suffix) {
        int i = len - 1;
        int j = suffix.len - 1;
        while ((j >= 0) && (i >= 0) &&
                (names[index + i] == names[suffix.index + j])) {
                i--;
                j--;
        }
        return j < 0;
    }

/** returns the subName starting at position start, excluding position end
 */
    public Name subName(int start, int end) {
        if (end < start)
            end = start;
        byte[]  ascii = new byte[end - start];
        System.arraycopy(names, index + start, ascii, 0, end - start);
        return fromAscii(ascii, 0, ascii.length);
    }

/** replace all `from' characters with `to'
 */
    public Name replace(byte from, byte to) {
        byte[] ascii = new byte[len];
        copyAscii(ascii, 0);
        for (int i = 0; i < len; i++)
            if (ascii[i] == from) ascii[i] = to;
        return fromAscii(ascii, 0, len);
    }

/** returns the concatenation of this name and n
 */
    public Name append(Name n) {
        byte[] ascii = new byte[len + n.len];
        copyAscii(ascii, 0);
        n.copyAscii(ascii, len);
        return fromAscii(ascii, 0, ascii.length);
    }

/** returns the concatenation of all names in ns
 */
    public static Name concat(Name ns[]) {
        int len = 0;
        for (int i = 0; i < ns.length; i++)
            len = len + ns[i].len;
        byte[] ascii = new byte[len];
        len = 0;
        for (int i = 0; i < ns.length; i++) {
            ns[i].copyAscii(ascii, len);
            len = len + ns[i].len;
        }
        return fromAscii(ascii, 0, len);
    }

/** is this name an operator?
 */
    public boolean isOperator() {
        return !(((names[index] >= 'a') && (names[index] <= 'z')) ||
                 ((names[index] >= 'A') && (names[index] <= 'Z')) ||
                 (names[index] == '$') ||
                 (names[index] == '_'));
    }

/** is this name a variable identifier?
 */
    public boolean isVariable() {
        return ((names[index] >= 'a' && names[index] <= 'z') ||
		names[index] == '_') &&
	    this != Names.null_ &&
	    this != Names.true_ &&
	    this != Names.false_;
    }

    public static final Name ERROR = Name.fromString("<error>");

/** precedence of this name
 */
    public int precedence() {
        if (this == ERROR)
            return -1;
        byte ch = names[index];
        if (((ch >= 'A') && (ch <= 'Z')) ||
            ((ch >= 'a') && (ch <= 'z')))
            return 1;
        switch (ch) {
            case '|':
                return 2;
            case '^':
                return 3;
            case '&':
                return 4;
            case '<':
            case '>':
                return 5;
            case '=':
            case '!':
                return 6;
            case ':':
                return 7;
            case '+':
            case '-':
                return 8;
            case '*':
            case '/':
            case '%':
                return 9;
            default:
                return 10;
        }
    }

    public static final int TOP_PRECEDENCE = 11;

/** is this operator left associative
 */
    public boolean isLeftAssoc() {
        return names[index + len -1] != ':';
    }
}