Skip to content

Commit a0b7e47

Browse files
committed
Refactorings
Move logic from subclasses into NamedType. Use explicit initialization instead of laziness.
1 parent 02e268d commit a0b7e47

File tree

2 files changed

+42
-59
lines changed

2 files changed

+42
-59
lines changed

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

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,18 +1486,45 @@ object Types {
14861486
// --- NamedTypes ------------------------------------------------------------------
14871487

14881488
/** A NamedType of the form Prefix # name */
1489-
abstract class NamedType extends CachedProxyType with ValueType {
1489+
abstract class NamedType extends CachedProxyType with ValueType { self =>
1490+
1491+
type ThisType >: this.type <: NamedType
1492+
type ThisName <: Name
14901493

14911494
val prefix: Type
1492-
val designator: Designator
1495+
val designator: Designator { type ThisName = self.ThisName }
14931496

1494-
def designatorName: Name = designator.asInstanceOf[Name]
1497+
assert(prefix.isValueType || (prefix eq NoPrefix), s"invalid prefix $prefix")
14951498

1496-
def name(implicit ctx: Context): Name = designatorName
1499+
def isType = isInstanceOf[TypeRef]
1500+
def isTerm = isInstanceOf[TermRef]
14971501

1498-
type ThisType >: this.type <: NamedType
1502+
private[this] var myName: ThisName = _
1503+
private[this] var mySig: Signature = null
1504+
1505+
def designatorName: Name = designator.asInstanceOf[Name] // ### todo: remove
1506+
1507+
private[dotc] def init()(implicit ctx: Context): this.type = {
1508+
(designator: Designator) match { // dotty shortcoming: need the upcast
1509+
case DerivedName(underlying, info: SignedName.SignedInfo) =>
1510+
myName = underlying.asInstanceOf[ThisName]
1511+
mySig = info.sig
1512+
assert(mySig ne Signature.OverloadedSignature)
1513+
case designator: Name =>
1514+
myName = designator.asInstanceOf[ThisName]
1515+
case designator: Symbol =>
1516+
myName = designator.name.asInstanceOf[ThisName]
1517+
uncheckedSetSym(designator)
1518+
}
1519+
this
1520+
}
14991521

1500-
assert(prefix.isValueType || (prefix eq NoPrefix), s"invalid prefix $prefix")
1522+
final def name: ThisName = myName
1523+
1524+
final override def signature(implicit ctx: Context): Signature =
1525+
if (mySig != null) mySig
1526+
else if (isType || lastDenotation == null) Signature.NotAMethod
1527+
else denot.signature
15011528

15021529
private[this] var lastDenotation: Denotation = _
15031530
private[this] var lastSymbol: Symbol = _
@@ -1707,7 +1734,7 @@ object Types {
17071734
protected def loadDenot(implicit ctx: Context): Denotation = {
17081735
val d = asMemberOf(prefix, allowPrivate = true)
17091736
if (d.exists || ctx.phaseId == FirstPhaseId || !lastDenotation.isInstanceOf[SymDenotation])
1710-
if (hasExplicitSignature) d.atSignature(signature).checkUnique
1737+
if (mySig != null) d.atSignature(mySig).checkUnique
17111738
else d
17121739
else { // name has changed; try load in earlier phase and make current
17131740
val d = loadDenot(ctx.withPhase(ctx.phaseId - 1)).current
@@ -1746,9 +1773,6 @@ object Types {
17461773
else
17471774
denot.symbol
17481775

1749-
override def signature(implicit ctx: Context): Signature =
1750-
if (isType || lastDenotation == null) Signature.NotAMethod
1751-
else denot.signature
17521776

17531777
/** Retrieves currently valid symbol without necessarily updating denotation.
17541778
* Assumes that symbols do not change between periods in the same run.
@@ -1760,11 +1784,6 @@ object Types {
17601784

17611785
def info(implicit ctx: Context): Type = denot.info
17621786

1763-
def isType = isInstanceOf[TypeRef]
1764-
def isTerm = isInstanceOf[TermRef]
1765-
1766-
protected def hasExplicitSignature(implicit ctx: Context) = false
1767-
17681787
/** Guard against cycles that can arise if given `op`
17691788
* follows info. The problematic cases are a type alias to itself or
17701789
* bounded by itself or a val typed as itself:
@@ -1925,42 +1944,14 @@ object Types {
19251944
abstract case class TermRef(override val prefix: Type, designator: TermDesignator) extends NamedType with SingletonType {
19261945

19271946
type ThisType = TermRef
1947+
type ThisName = TermName
19281948

19291949
//assert(name.toString != "<local Coder>")
19301950
override def underlying(implicit ctx: Context): Type = {
19311951
val d = denot
19321952
if (d.isOverloaded) NoType else d.info
19331953
}
19341954

1935-
private var mySig: Signature = null
1936-
private var myName: TermName = null
1937-
1938-
private def ensureInitialized()(implicit ctx: Context): Unit =
1939-
if (myName == null)
1940-
designator match {
1941-
case DerivedName(underlying, info: SignedName.SignedInfo) =>
1942-
mySig = info.sig
1943-
assert(mySig ne Signature.OverloadedSignature)
1944-
myName = underlying
1945-
case designator: TermName =>
1946-
myName = designator
1947-
case designator: TermSymbol @unchecked =>
1948-
myName = designator.name
1949-
}
1950-
1951-
override final def name(implicit ctx: Context): TermName = {
1952-
ensureInitialized()
1953-
myName
1954-
}
1955-
1956-
override def hasExplicitSignature(implicit ctx: Context): Boolean = {
1957-
ensureInitialized()
1958-
mySig != null
1959-
}
1960-
1961-
override final def signature(implicit ctx: Context): Signature =
1962-
if (hasExplicitSignature) mySig else super.signature
1963-
19641955
override def isOverloaded(implicit ctx: Context) = denot.isOverloaded
19651956

19661957
private def rewrap(sd: SingleDenotation)(implicit ctx: Context) =
@@ -2005,17 +1996,7 @@ object Types {
20051996
abstract case class TypeRef(override val prefix: Type, designator: TypeDesignator) extends NamedType {
20061997

20071998
type ThisType = TypeRef
2008-
2009-
private var myName: TypeName = null
2010-
2011-
override def name(implicit ctx: Context): TypeName = {
2012-
if (myName == null)
2013-
myName = designator match {
2014-
case name: TypeName => name
2015-
case sym: TypeSymbol @unchecked => sym.name
2016-
}
2017-
myName
2018-
}
1999+
type ThisName = TypeName
20192000

20202001
override def underlying(implicit ctx: Context): Type = info
20212002

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ object Uniques {
5252
e
5353
}
5454

55-
def enterIfNew(prefix: Type, designator: Name): NamedType = {
55+
def enterIfNew(prefix: Type, designator: Name)(implicit ctx: Context): NamedType = {
5656
val h = doHash(designator, prefix)
5757
if (monitored) recordCaching(h, classOf[NamedType])
58-
def newType =
58+
def newType = {
5959
if (designator.isTypeName) new CachedTypeRef(prefix, designator.asTypeName, h)
6060
else new CachedTermRef(prefix, designator.asTermName, h)
61+
}.init()
6162
if (h == NotCached) newType
6263
else {
6364
val r = findPrevious(h, prefix, designator)
@@ -78,12 +79,13 @@ object Uniques {
7879
e
7980
}
8081

81-
def enterIfNew(prefix: Type, name: Name, sym: Symbol): NamedType = {
82+
def enterIfNew(prefix: Type, name: Name, sym: Symbol)(implicit ctx: Context): NamedType = {
8283
val h = doHash(sym, prefix)
8384
if (monitored) recordCaching(h, classOf[WithFixedSym])
84-
def newType =
85+
def newType = {
8586
if (name.isTypeName) new TypeRefWithFixedSym(prefix, name.asTypeName, sym.asInstanceOf[TypeSymbol], h)
8687
else new TermRefWithFixedSym(prefix, name.asTermName, sym.asInstanceOf[TermSymbol], h)
88+
}.init()
8789
if (h == NotCached) newType
8890
else {
8991
val r = findPrevious(h, prefix, sym)

0 commit comments

Comments
 (0)