summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/symtab/Names.scala
blob: 5fedb67ce42177e068e641d7ccd8f81aed50f192 (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
/* NSC -- new scala compiler
 * Copyright 2005 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$
package scala.tools.nsc.symtab;

import scala.tools.util.UTF8Codec;
import scala.tools.nsc.util.NameTransformer;

class Names {

// Operations -------------------------------------------------------------

  private val HASH_SIZE = 0x8000;
  private val HASH_MASK = 0x7FFF;
  private val NAME_SIZE = 0x20000;

  final val nameDebug = false;

  /** memory to store all names sequentially
   */
  var chrs: Array[char] = new Array[char](NAME_SIZE);
  private var nc = 0;

  /** hashtable for finding term names quickly
   */
  private val termHashtable = new Array[Name](HASH_SIZE);

  /** hashtable for finding type names quickly
   */
  private val typeHashtable = new Array[Name](HASH_SIZE);

  /** the hashcode of a name
   */
  private def hashValue(cs: Array[char], offset: int, len: int): int =
    if (len > 0)
      (len * (41 * 41 * 41) +
       cs(offset) * (41 * 41) +
       cs(offset + len - 1) * 41 +
       cs(offset + (len >> 1)))
    else 0;

  /** is (the ascii representation of) name at given index equal to
   *  cs[offset..offset+len-1]?
   */
  private def equals(index: int, cs: Array[char], offset: int, len: int): boolean = {
    var i = 0;
    while ((i < len) && (chrs(index + i) == cs(offset + i)))
      i = i + 1;
    i == len
  }

  /** enter characters into chrs array
   */
  private def enterChars(cs: Array[char], offset: int, len: int): unit = {
    var i = 0;
    while (i < len) {
      if (nc + i == chrs.length) {
        val newchrs = new Array[char](chrs.length * 2);
        System.arraycopy(chrs, 0, newchrs, 0, chrs.length);
        chrs = newchrs;
      }
      chrs(nc + i) = cs(offset + i);
      i = i + 1
    }
    if (len == 0) nc = nc + 1
    else nc = nc + len
  }

  /** create a term name from the characters in cs[offset..offset+len-1].
   */
  def newTermName(cs: Array[char], offset: int, len: int): Name = {
    val h = hashValue(cs, offset, len) & HASH_MASK;
    var n = termHashtable(h);
    while ((n != null) && (n.length != len || !equals(n.start, cs, offset, len))) {
      n = n.next;
    }
    if (n == null) {
      n = new TermName(nc, len, h);
      enterChars(cs, offset, len);
    }
    n
  }

  /** create a term name from string
   */
  def newTermName(s: String): Name =
    newTermName(s.toCharArray(), 0, s.length());

  /** create a term name from the UTF8 encoded bytes in bs[offset..offset+len-1].
   */
  def newTermName(bs: Array[byte], offset: int, len: int): Name = {
    val cs = new Array[char](bs.length);
    val nchrs = UTF8Codec.decode(bs, offset, cs, 0, len);
    newTermName(cs, 0, nchrs)
  }

  /** create a type name from the characters in cs[offset..offset+len-1].
   */
  def newTypeName(cs: Array[char], offset: int, len: int): Name =
    newTermName(cs, offset, len).toTypeName;

  /** create a type name from string
   */
  def newTypeName(s: String): Name =
    newTermName(s).toTypeName;

  /** create a type name from the UTF8 encoded bytes in bs[offset..offset+len-1].
   */
  def newTypeName(bs: Array[byte], offset: int, len: int): Name =
    newTermName(bs, offset, len).toTypeName;


  def nameChars: Array[char] = chrs;

  implicit def view(s: String): Name = newTermName(s);

// Classes ----------------------------------------------------------------------

  /** The name class */
  abstract class Name(index: int, len: int) extends Function1[int, char] {

    /** Index into name table */
    def start: int = index;

    /** next name in the same hash bucket
     */
    var next: Name = null;

    /** return the length of this name
     */
    final def length: int = len;

    final def isEmpty = length == 0;

    def isTermName: boolean;
    def isTypeName: boolean;
    def toTermName: Name;
    def toTypeName: Name;


    /** copy bytes of this name to buffer cs, starting at offset
     */
    final def copyChars(cs: Array[char], offset: int) =
      System.arraycopy(chrs, index, cs, offset, len);

    /** return the ascii representation of this name
     */
    final def toChars = {
      val cs = new Array[char](len);
      copyChars(cs, 0);
      cs
    }

    /** return the string representation of this name
     */
    final override def toString(): String = new String(chrs, index, len);

    /** Write to UTF8 representation of this name to given character array.
     *  Start copying to index `to'. Return index of next free byte in array.
     *  Array must have enough remaining space for all bytes
     *  (i.e. maximally 3*length bytes).
     */
    final def copyUTF8(bs: Array[byte], offset: int): int =
      UTF8Codec.encode(chrs, index, bs, offset, len);

    /** return the hash value of this name
     */
    final override def hashCode(): int = index;

    /** return the i'th char of this name
     */
    final def apply(i: int): char = chrs(index + i);

    /** return the index of first occurrence of char c in this name, length if not found */
    final def pos(c: char): int = pos(c, 0);

    /** return the index of first occurrence of char c in this name, length if not found */
    final def pos(s: String): int = pos(s, 0);

    /** return the index of first occurrence of char c in this name from `start',
     *  length if not found */
    final def pos(c: char, start: int): int = {
      var i = start;
      while (i < len && chrs(index + i) != c) i = i + 1;
      i
    }

    /** return the index of first occurrence of nonempty string s in this name from `start',
     *  length if not found */
    final def pos(s: String, start: int): int = {
      var i = pos(s.charAt(0), start);
      while (i + s.length() <= len) {
        var j = 1;
        while (s.charAt(j) == chrs(index + i + j)) {
          j = j + 1;
          if (j == s.length()) return i;
        }
        i = pos(s.charAt(0), i + 1)
      }
      len
    }

    /** return the index of last occurrence of char c in this name, -1 if not found.
     */
    final def lastPos(c: char): int = lastPos(c, len - 1);

    final def lastPos(s: String): int = lastPos(s, len - s.length());

    /** return the index of last occurrence of char c in this name from `start',
     *  -1 if not found
     */
    final def lastPos(c: char, start: int): int = {
      var i = start;
      while (i >= 0 && chrs(index + i) != c) i = i - 1;
      i
    }

    /** return the index of last occurrence of string s in this name from `start',
     *  -1 if not found
     */
    final def lastPos(s: String, start: int): int = {
      var i = lastPos(s.charAt(0), start);
      while (i >= 0) {
        var j = 1;
        while (s.charAt(j) == chrs(index + i + j)) {
          j = j + 1;
          if (j == s.length()) return i;
        }
        i = lastPos(s.charAt(0), i - 1)
      }
      -s.length()
    }

    /** does this name start with prefix?
     */
    final def startsWith(prefix: Name): boolean = startsWith(prefix, 0);

    /** does this name start with prefix at given start index?
     */
    final def startsWith(prefix: Name, start: int): boolean = {
      var i = 0;
      while (i < prefix.length && start + i < len && chrs(index + start + i) == chrs(prefix.start + i))
        i = i + 1;
      i == prefix.length
    }

    /** does this name end with suffix?
     */
    final def endsWith(suffix: Name): boolean = endsWith(suffix, len);

    /** does this name end with suffix just before given end index?
     */
    final def endsWith(suffix: Name, end: int): boolean = {
      var i = 1;
      while (i <= suffix.length && i <= end && chrs(index + end - i) == chrs(suffix.start + suffix.length - i))
        i = i + 1;
      i > suffix.length
    }

    /** the subname with characters from start to end-1
     */
    def subName(from: int, to: int): Name;

    /** replace all occurrences of `from' by `to' in name.
     *  result is always a term name.
     */
    def replace(from: char, to: char): Name = {
      val cs = new Array[char](len);
      var i = 0;
      while (i < len) {
        val ch = this(i);
        cs(i) = if (ch == from) to else ch;
        i = i + 1
      }
      newTermName(cs, 0, len)
    }

    /** Replace operator symbols by corresponding "$op_name" */
    def encode: Name = {
      val str = toString();
      val res = NameTransformer.encode(str);
      if (res == str) this
      else if (isTypeName) newTypeName(res)
      else newTermName(res)
    }

    /** Replace $op_name by corresponding operator symbol */
    def decode: String = (
      NameTransformer.decode(toString()) +
      (if (nameDebug && isTypeName) "!" else ""));//debug
  }

  private class TermName(index: int, len: int, hash: int) extends Name(index, len) {
    next = termHashtable(hash);
    termHashtable(hash) = this;
    def isTermName: boolean = true;
    def isTypeName: boolean = false;
    def toTermName: Name = this;
    def toTypeName = {
      val h = hashValue(chrs, index, len) & HASH_MASK;
      var n = typeHashtable(h);
      while (n != null && n.start != index)
        n = n.next;
      if (n == null)
	n = new TypeName(index, len, h);
      n
    }
    def subName(from: int, to: int): Name =
      newTermName(chrs, start + from, to - from);
  }

  private class TypeName(index: int, len: int, hash: int) extends Name(index, len) {
    next = typeHashtable(hash);
    typeHashtable(hash) = this;
    def isTermName: boolean = false;
    def isTypeName: boolean = true;
    def toTermName: Name = {
      val h = hashValue(chrs, index, len) & HASH_MASK;
      var n = termHashtable(h);
      while (n != null && n.start != index)
        n = n.next;
      if (n == null)
	n = new TermName(index, len, h);
      n
    }
    def toTypeName: Name = this;
    def subName(from: int, to: int): Name =
      newTypeName(chrs, start + from, to - from);
  }
}