@@ -144,6 +144,11 @@ object Scopes {
144
144
final def toText (printer : Printer ): Text = printer.toText(this )
145
145
146
146
def checkConsistent ()(implicit ctx : Context ) = ()
147
+
148
+ /** Hook for transforming a name before it is used in a lookup or creation.
149
+ * Used to mangle names in package scopes.
150
+ */
151
+ protected def normalize (name : Name ): Name = name
147
152
}
148
153
149
154
/** A subclass of Scope that defines methods for entering and
@@ -155,6 +160,7 @@ object Scopes {
155
160
class MutableScope protected [Scopes ](initElems : ScopeEntry , initSize : Int , val nestingLevel : Int = 0 )
156
161
extends Scope {
157
162
163
+ /** Scope shares elements with `base` */
158
164
protected [Scopes ] def this (base : Scope )(implicit ctx : Context ) = {
159
165
this (base.lastEntry, base.size, base.nestingLevel + 1 )
160
166
ensureCapacity(MinHash )(ctx) // WTH? it seems the implicit is not in scope for a secondary constructor call.
@@ -178,6 +184,8 @@ object Scopes {
178
184
*/
179
185
private var elemsCache : List [Symbol ] = null
180
186
187
+ protected def newScopeLikeThis () = new MutableScope ()
188
+
181
189
/** Clone scope, taking care not to force the denotations of any symbols in the scope.
182
190
*/
183
191
def cloneScope (implicit ctx : Context ): MutableScope = {
@@ -187,7 +195,7 @@ object Scopes {
187
195
entries += e
188
196
e = e.prev
189
197
}
190
- val scope = newScope
198
+ val scope = newScopeLikeThis()
191
199
for (i <- entries.length - 1 to 0 by - 1 ) {
192
200
val e = entries(i)
193
201
scope.newScopeEntry(e.name, e.sym)
@@ -198,7 +206,7 @@ object Scopes {
198
206
/** create and enter a scope entry with given name and symbol */
199
207
protected def newScopeEntry (name : Name , sym : Symbol )(implicit ctx : Context ): ScopeEntry = {
200
208
ensureCapacity(if (hashTable ne null ) hashTable.length else MinHash )
201
- val e = new ScopeEntry (name, sym, this )
209
+ val e = new ScopeEntry (normalize( name) , sym, this )
202
210
e.prev = lastEntry
203
211
lastEntry = e
204
212
if (hashTable ne null ) enterInHash(e)
@@ -234,7 +242,7 @@ object Scopes {
234
242
enter(sym)
235
243
}
236
244
237
- private def ensureCapacity (tableSize : Int )(implicit ctx : Context ): Unit =
245
+ protected def ensureCapacity (tableSize : Int )(implicit ctx : Context ): Unit =
238
246
if (size >= tableSize * FillFactor ) createHash(tableSize * 2 )
239
247
240
248
private def createHash (tableSize : Int )(implicit ctx : Context ): Unit =
@@ -310,15 +318,16 @@ object Scopes {
310
318
/** Lookup a symbol entry matching given name.
311
319
*/
312
320
override def lookupEntry (name : Name )(implicit ctx : Context ): ScopeEntry = {
321
+ val normalized = normalize(name)
313
322
var e : ScopeEntry = null
314
323
if (hashTable ne null ) {
315
- e = hashTable(name .hashCode & (hashTable.length - 1 ))
316
- while ((e ne null ) && e.name != name ) {
324
+ e = hashTable(normalized .hashCode & (hashTable.length - 1 ))
325
+ while ((e ne null ) && e.name != normalized ) {
317
326
e = e.tail
318
327
}
319
328
} else {
320
329
e = lastEntry
321
- while ((e ne null ) && e.name != name ) {
330
+ while ((e ne null ) && e.name != normalized ) {
322
331
e = e.prev
323
332
}
324
333
}
@@ -394,12 +403,23 @@ object Scopes {
394
403
}
395
404
}
396
405
397
- class PackageScope extends MutableScope {
398
- override final def newScopeEntry (name : Name , sym : Symbol )(implicit ctx : Context ): ScopeEntry =
399
- super .newScopeEntry(name.mangled, sym)
406
+ /** The scope of a package. This is different from a normal scope
407
+ * in that names of scope entries are kept in mangled form.
408
+ */
409
+ class PackageScope protected [Scopes ](initElems : ScopeEntry , initSize : Int , nestingLevel : Int )
410
+ extends MutableScope (initElems, initSize, nestingLevel) {
411
+
412
+ /** Scope shares elements with `base` */
413
+ def this (base : Scope )(implicit ctx : Context ) = {
414
+ this (base.lastEntry, base.size, base.nestingLevel + 1 )
415
+ ensureCapacity(MinHash )(ctx) // WTH? it seems the implicit is not in scope for a secondary constructor call.
416
+ }
417
+
418
+ def this () = this (null , 0 , 0 )
419
+
420
+ override def newScopeLikeThis () = new PackageScope ()
400
421
401
- override final def lookupEntry (name : Name )(implicit ctx : Context ): ScopeEntry =
402
- super .lookupEntry(name.mangled)
422
+ override protected def normalize (name : Name ) = name.mangled
403
423
}
404
424
405
425
/** Create a new scope */
0 commit comments