Skip to content

Commit 2a6ed56

Browse files
committed
Introduce Namespace field in named types.
1 parent 72353ad commit 2a6ed56

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

compiler/src/dotty/tools/dotc/core/Designators.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package dotty.tools
22
package dotc
33
package core
44

5-
import Names._
5+
import Names._, NameOps._
66
import Contexts.Context
7+
import Types.TypeRef
78

89
/** Defines a common superclass of Name and Symbol and its Term/Type variants
910
* Designators are used in reference type to identity what is referred to.
@@ -27,4 +28,10 @@ object Designators {
2728

2829
type TermDesignator = Designator { type ThisName = TermName }
2930
type TypeDesignator = Designator { type ThisName = TypeName }
31+
32+
case class QualifiedDesignator(qual: TypeRef, name: Name) extends Designator {
33+
def derivedDesignator(qual: TypeRef, name: Name) =
34+
if ((qual eq this.qual) && (name eq this.name)) this
35+
else QualifiedDesignator(qual, name)
36+
}
3037
}

compiler/src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Decorators.PreNamedString
99
import util.{Chars, NameTransformer}
1010
import Chars.isOperatorPart
1111
import Definitions._
12+
import Designators._
1213
import config.Config
1314

1415
object NameOps {
@@ -272,6 +273,9 @@ object NameOps {
272273
val unmangled = (name /: kinds)(_.unmangle(_))
273274
if (unmangled eq name) name else unmangled.unmangle(kinds)
274275
}
276+
277+
def withNameSpace(owner: Symbol)(implicit ctx: Context): Designator =
278+
if (owner.exists) QualifiedDesignator(owner.typeRef, name) else name
275279
}
276280

277281
implicit class TermNameDecorator(val name: TermName) extends AnyVal {

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,9 +1504,10 @@ object Types {
15041504

15051505
private[this] var myName: ThisName = _
15061506
private[this] var mySig: Signature = null
1507+
private[this] var myNameSpace: Symbol = NoSymbol
15071508

15081509
private[dotc] def init()(implicit ctx: Context): this.type = {
1509-
(designator: Designator) match { // dotty shortcoming: need the upcast
1510+
def decompose(designator: Designator): Unit = designator match {
15101511
case DerivedName(underlying, info: SignedName.SignedInfo) =>
15111512
myName = underlying.asInstanceOf[ThisName]
15121513
mySig = info.sig
@@ -1515,7 +1516,11 @@ object Types {
15151516
myName = designator.asInstanceOf[ThisName]
15161517
case designator: Symbol =>
15171518
uncheckedSetSym(designator)
1519+
case QualifiedDesignator(qual, name) =>
1520+
myNameSpace = qual.symbol
1521+
decompose(name)
15181522
}
1523+
decompose(designator)
15191524
this
15201525
}
15211526

@@ -1529,6 +1534,8 @@ object Types {
15291534
else if (isType || lastDenotation == null) Signature.NotAMethod
15301535
else denot.signature
15311536

1537+
final def nameSpace: Symbol = myNameSpace
1538+
15321539
private[this] var lastDenotation: Denotation = _
15331540
private[this] var lastSymbol: Symbol = _
15341541
private[this] var checkedPeriod = Nowhere
@@ -1734,7 +1741,7 @@ object Types {
17341741
}
17351742

17361743
private def withSig(sig: Signature)(implicit ctx: Context): NamedType =
1737-
TermRef(prefix, name.asTermName.withSig(sig))
1744+
TermRef(prefix, name.asTermName.withSig(sig).withNameSpace(nameSpace).asInstanceOf[TermDesignator])
17381745

17391746
protected def loadDenot(implicit ctx: Context): Denotation = {
17401747
val d = asMemberOf(prefix, allowPrivate = true)
@@ -1752,6 +1759,7 @@ object Types {
17521759

17531760
protected def asMemberOf(prefix: Type, allowPrivate: Boolean)(implicit ctx: Context): Denotation =
17541761
if (name.is(ShadowedName)) prefix.nonPrivateMember(name.exclude(ShadowedName))
1762+
else if (nameSpace.exists) prefix.decl(name)
17551763
else if (!allowPrivate) prefix.nonPrivateMember(name)
17561764
else prefix.member(name)
17571765

@@ -1925,8 +1933,8 @@ object Types {
19251933
*/
19261934
def shadowed(implicit ctx: Context): NamedType =
19271935
designator match {
1928-
case designator: Symbol => this
19291936
case designator: Name => NamedType(prefix, designator.derived(ShadowedName))
1937+
case _ => this
19301938
}
19311939

19321940
override def equals(that: Any) = that match {
@@ -1991,13 +1999,13 @@ object Types {
19911999
curSig
19922000
else
19932001
curSig.updateWith(symbol.info.asSeenFrom(prefix, symbol.owner).signature)
1994-
val candidate =
2002+
val designator1 =
19952003
if (newSig ne curSig) {
19962004
core.println(i"sig change at ${ctx.phase} for $this, pre = $prefix, sig: $curSig --> $newSig")
1997-
TermRef(prefix, name.withSig(newSig))
2005+
name.withSig(newSig).withNameSpace(nameSpace).asInstanceOf[TermDesignator]
19982006
}
1999-
else TermRef(prefix, designator)
2000-
fixDenot(candidate, prefix)
2007+
else designator
2008+
fixDenot(TermRef(prefix, designator1), prefix)
20012009
}
20022010

20032011
override def shadowed(implicit ctx: Context): NamedType =
@@ -2069,7 +2077,10 @@ object Types {
20692077
def apply(prefix: Type, name: TermName, denot: Denotation)(implicit ctx: Context): TermRef = {
20702078
if ((prefix eq NoPrefix) || denot.symbol.isReferencedSymbolically) apply(prefix, denot.symbol.asTerm)
20712079
else denot match {
2072-
case denot: SingleDenotation => apply(prefix, name.withSig(denot.signature))
2080+
case denot: SingleDenotation =>
2081+
var desig = name.withSig(denot.signature)
2082+
//if (denot.symbol.is(Private)) desig = desig.withNameSpace(denot.symbol.owner)
2083+
apply(prefix, desig)
20732084
case _ => apply(prefix, name)
20742085
}
20752086
} withDenot denot

compiler/src/dotty/tools/dotc/core/tasty/TastyFormat.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ Standard-Section: "ASTs" TopLevelStat*
127127
TERMREFdirect sym_ASTRef
128128
TERMREFsymbol sym_ASTRef qual_Type
129129
TERMREFpkg fullyQualified_NameRef
130+
TERMREFin Length possiblySigned_NameRef qual_Type namespace_Type
130131
TERMREF possiblySigned_NameRef qual_Type
131132
THIS clsRef_Type
132133
RECthis recType_ASTRef
@@ -151,7 +152,8 @@ Standard-Section: "ASTs" TopLevelStat*
151152
TYPEREFdirect sym_ASTRef
152153
TYPEREFsymbol sym_ASTRef qual_Type
153154
TYPEREFpkg fullyQualified_NameRef
154-
TYPEREF possiblySigned_NameRef qual_Type
155+
TYPEREFin Length NameRef qual_Type namespace_Type
156+
TYPEREF NameRef qual_Type
155157
RECtype parent_Type
156158
TYPEALIAS alias_Type
157159
SUPERtype Length this_Type underlying_Type
@@ -262,6 +264,7 @@ object TastyFormat {
262264
final val IMPLMETH = 64
263265

264266
// AST tags
267+
// Cat. 1: tag
265268

266269
final val UNITconst = 2
267270
final val FALSEconst = 3
@@ -294,6 +297,8 @@ object TastyFormat {
294297
final val DEFAULTparameterized = 30
295298
final val STABLE = 31
296299

300+
// Cat. 2: tag Nat
301+
297302
final val SHARED = 64
298303
final val TERMREFdirect = 65
299304
final val TYPEREFdirect = 66
@@ -311,6 +316,8 @@ object TastyFormat {
311316
final val IMPORTED = 78
312317
final val RENAMED = 79
313318

319+
// Cat. 3: tag AST
320+
314321
final val THIS = 96
315322
final val QUALTHIS = 97
316323
final val CLASSconst = 98
@@ -325,6 +332,8 @@ object TastyFormat {
325332
final val TYPEALIAS = 107
326333
final val SINGLETONtpt = 108
327334

335+
// Cat. 4: tag Nat AST
336+
328337
final val IDENT = 112
329338
final val IDENTtpt = 113
330339
final val SELECT = 114
@@ -335,6 +344,8 @@ object TastyFormat {
335344
final val TYPEREF = 119
336345
final val SELFDEF = 120
337346

347+
// Cat. 5: tag Length ...
348+
338349
final val PACKAGE = 128
339350
final val VALDEF = 129
340351
final val DEFDEF = 130
@@ -382,6 +393,8 @@ object TastyFormat {
382393
final val PARAMtype = 174
383394
final val ANNOTATION = 175
384395
final val TYPEARGtype = 176
396+
final val TERMREFin = 177
397+
final val TYPEREFin = 178
385398

386399
final val firstSimpleTreeTag = UNITconst
387400
final val firstNatTreeTag = SHARED
@@ -565,6 +578,8 @@ object TastyFormat {
565578
case SINGLETONtpt => "SINGLETONtpt"
566579
case SUPERtype => "SUPERtype"
567580
case TYPEARGtype => "TYPEARGtype"
581+
case TERMREFin => "TERMREFin"
582+
case TYPEREFin => "TYPEREFin"
568583
case REFINEDtype => "REFINEDtype"
569584
case REFINEDtpt => "REFINEDtpt"
570585
case APPLIEDtype => "APPLIEDtype"
@@ -593,7 +608,7 @@ object TastyFormat {
593608
*/
594609
def numRefs(tag: Int) = tag match {
595610
case VALDEF | DEFDEF | TYPEDEF | TYPEPARAM | PARAM | NAMEDARG | RETURN | BIND |
596-
SELFDEF | REFINEDtype => 1
611+
SELFDEF | REFINEDtype | TERMREFin | TYPEREFin => 1
597612
case RENAMED | PARAMtype => 2
598613
case POLYtype | METHODtype | TYPELAMBDAtype => -1
599614
case _ => 0

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ class TreePickler(pickler: TastyPickler) {
168168
writeByte(if (tpe.isType) TYPEREFsymbol else TERMREFsymbol)
169169
pickleSymRef(sym); pickleType(tpe.prefix)
170170
}
171+
else if (tpe.nameSpace.exists) {
172+
writeByte(if (tpe.isType) TYPEREFin else TERMREFin)
173+
withLength {
174+
pickleName(sym.name)
175+
pickleType(tpe.prefix)
176+
pickleType(tpe.nameSpace.typeRef)
177+
}
178+
}
171179
else {
172180
val name = tpe.designator match {
173181
case designator: Symbol =>

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package tasty
55

66
import Contexts._, Symbols._, Types._, Scopes._, SymDenotations._, Names._, NameOps._
77
import StdNames._, Denotations._, Flags._, Constants._, Annotations._
8-
import NameKinds._
8+
import NameKinds._, Designators._
99
import typer.Checking.checkNonCyclic
1010
import util.Positions._
1111
import ast.{tpd, Trees, untpd}
@@ -220,6 +220,16 @@ class TreeUnpickler(reader: TastyReader, nameAtRef: NameRef => TermName, posUnpi
220220

221221
val result =
222222
(tag: @switch) match {
223+
case TERMREFin =>
224+
val name = readName()
225+
val prefix = readType()
226+
val owner = readType()
227+
TermRef(prefix, name.withNameSpace(owner.typeSymbol).asInstanceOf[TermDesignator])
228+
case TYPEREFin =>
229+
val name = readName()
230+
val prefix = readType()
231+
val owner = readType()
232+
TypeRef(prefix, name.toTypeName.withNameSpace(owner.typeSymbol).asInstanceOf[TypeDesignator])
223233
case REFINEDtype =>
224234
var name: Name = readName()
225235
val parent = readType()

0 commit comments

Comments
 (0)