Skip to content

Commit 6777965

Browse files
committed
Make Symbol a subtype of SymDenotation
1 parent c2f2962 commit 6777965

File tree

3 files changed

+91
-68
lines changed

3 files changed

+91
-68
lines changed

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@ object SymDenotations {
3434
*/
3535
class SymDenotation private[core] (
3636
_symbol: SymbolImpl,
37-
final val _maybeOwner: SymbolImpl,
37+
_maybeOwner: SymbolImpl,
3838
final val name: Name,
3939
initFlags: FlagSet,
4040
initInfo: Type,
4141
initPrivateWithin: SymbolImpl = NoSymbol.toSymbolImpl) extends SingleDenotation(_symbol, initInfo) {
4242

4343
//assert(symbol.id != 4940, name)
4444

45-
final val maybeOwner: Symbol = _maybeOwner.fromSymbolImpl
45+
val maybeOwner: Symbol =
46+
if _maybeOwner == null then this.asInstanceOf[Symbol]
47+
else _maybeOwner.fromSymbolImpl
4648

4749
override def hasUniqueSym: Boolean = exists
4850

@@ -1471,7 +1473,8 @@ object SymDenotations {
14711473
else if myFlags.is(Method) then "method"
14721474
else "val"
14731475

1474-
override def toString: String = s"$kindString $name"
1476+
def denotString: String = s"$kindString $name"
1477+
override def toString: String = denotString
14751478

14761479
// ----- Sanity checks and debugging */
14771480

@@ -2393,25 +2396,7 @@ object SymDenotations {
23932396
ClassDenotationImpl(_symbol, _maybeOwner, name, initFlags, initInfo, initPrivateWithin),
23942397
PackageClassDenotation
23952398

2396-
@sharable object NoDenotation
2397-
extends SymDenotation(NoSymbol.toSymbolImpl, NoSymbol.toSymbolImpl, "<none>".toTermName, Permanent, NoType) {
2398-
override def isType: Boolean = false
2399-
override def isTerm: Boolean = false
2400-
override def exists: Boolean = false
2401-
override def owner: Symbol = throw new AssertionError("NoDenotation.owner")
2402-
override def computeAsSeenFrom(pre: Type)(using Context): SingleDenotation = this
2403-
override def mapInfo(f: Type => Type)(using Context): SingleDenotation = this
2404-
2405-
override def matches(other: SingleDenotation)(using Context): Boolean = false
2406-
override def targetName(using Context): Name = EmptyTermName
2407-
override def mapInherited(ownDenots: PreDenotation, prevDenots: PreDenotation, pre: Type)(using Context): SingleDenotation = this
2408-
override def filterWithPredicate(p: SingleDenotation => Boolean): SingleDenotation = this
2409-
override def filterDisjoint(denots: PreDenotation)(using Context): SingleDenotation = this
2410-
override def filterWithFlags(required: FlagSet, excluded: FlagSet)(using Context): SingleDenotation = this
2411-
2412-
NoSymbol.denot = this
2413-
validFor = Period.allInRun(NoRunId)
2414-
}
2399+
@sharable val NoDenotation: SymDenotation = NoSymbol.toSymbolImpl
24152400

24162401
/** Can a private symbol with given name and flags be inferred to be local,
24172402
* if all references to such symbols are via `this`?

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

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ object Symbols:
108108
def asType(using Context): TypeSymbol
109109

110110
def isClass: Boolean
111+
@targetName("Symbol_asClass")
111112
def asClass: ClassSymbol
112113

113114
def isPrivate(using Context): Boolean
@@ -116,7 +117,6 @@ object Symbols:
116117
def isStatic(using Context): Boolean
117118

118119
def symName(using Context): ThisName
119-
def signature(using Context): Signature
120120

121121
def span: Span
122122
def sourcePos(using Context): SourcePosition
@@ -179,8 +179,17 @@ object Symbols:
179179
* @param coord The coordinates of the symbol (a position or an index)
180180
* @param id A unique identifier of the symbol (unique per ContextBase)
181181
*/
182-
class SymbolImpl private[Symbols] (private var myCoord: Coord, val id: Int)
183-
extends SymbolDecl {
182+
class SymbolImpl private[Symbols] (
183+
maybeOwner: SymbolImpl,
184+
name: Name,
185+
initFlags: FlagSet,
186+
initInfo: Type,
187+
initPrivateWithin: SymbolImpl,
188+
private var myCoord: Coord,
189+
val id: Int)
190+
extends
191+
SymDenotation(null, maybeOwner, name, initFlags, initInfo, initPrivateWithin),
192+
SymbolDecl {
184193

185194
type ThisName <: Name
186195

@@ -234,6 +243,8 @@ object Symbols:
234243
checkedPeriod = Nowhere
235244
}
236245

246+
denot = this
247+
237248
/** The current denotation of this symbol */
238249
final def denot(using Context): SymDenotation = {
239250
util.Stats.record("Symbol.denot")
@@ -305,9 +316,8 @@ object Symbols:
305316
asInstanceOf[TypeSymbol]
306317
}
307318

308-
final def isClass: Boolean = isInstanceOf[ClassSymbol @unchecked]
309-
310-
final def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
319+
@targetName("Symbol_asClass")
320+
override def asClass: ClassSymbol = asInstanceOf[ClassSymbol]
311321

312322
/** Test whether symbol is private. This
313323
* conservatively returns `false` if symbol does not yet have a denotation, or denotation
@@ -323,17 +333,6 @@ object Symbols:
323333
final def isPatternBound(using Context): Boolean =
324334
!isClass && this.is(Case, butNot = Enum | Module)
325335

326-
/** The symbol's signature if it is completed or a method, NotAMethod otherwise. */
327-
final def signature(using Context): Signature =
328-
if (lastDenot != null && (lastDenot.isCompleted || lastDenot.is(Method)))
329-
denot.signature
330-
else
331-
Signature.NotAMethod
332-
333-
/** Special cased here, because it may be used on naked symbols in substituters */
334-
final def isStatic(using Context): Boolean =
335-
lastDenot != null && lastDenot.initial.isStatic
336-
337336
/** This symbol entered into owner's scope (owner must be a class). */
338337
final def entered(using Context): this.type = {
339338
if (this.owner.isClass) {
@@ -395,8 +394,7 @@ object Symbols:
395394
* containing this symbol instead of the directly enclosing class.
396395
* Overridden in ClassSymbol
397396
*/
398-
def associatedFile(using Context): AbstractFile =
399-
if (lastDenot == null) null else lastDenot.topLevelClass.associatedFile
397+
def associatedFile(using Context): AbstractFile = topLevelClass.associatedFile
400398

401399
/** The class file from which this class was generated, null if not applicable. */
402400
final def binaryFile(using Context): AbstractFile = {
@@ -485,22 +483,33 @@ object Symbols:
485483

486484
override def toString: String =
487485
if (lastDenot == null) s"Naked$prefixString#$id"
488-
else lastDenot.toString// + "#" + id // !!! DEBUG
486+
else lastDenot.denotString// + "#" + id // !!! DEBUG
489487

490-
def toText(printer: Printer): Text = printer.toText(this.fromSymbolImpl)
488+
override def toText(printer: Printer): Text = printer.toText(this.fromSymbolImpl)
491489

492490
def showLocated(using Context): String = ctx.printer.locatedText(this.fromSymbolImpl).show
493491
def showExtendedLocation(using Context): String = ctx.printer.extendedLocationText(this.fromSymbolImpl).show
494-
def showDcl(using Context): String = ctx.printer.dclText(this.fromSymbolImpl).show
492+
override def showDcl(using Context): String = ctx.printer.dclText(this.fromSymbolImpl).show
495493
def showKind(using Context): String = ctx.printer.kindString(this.fromSymbolImpl)
496494
def showName(using Context): String = ctx.printer.nameString(this.fromSymbolImpl)
497495
def showFullName(using Context): String = ctx.printer.fullNameString(this.fromSymbolImpl)
498496

499497
override def hashCode(): Int = id // for debugging.
500498
}
501499

502-
class ClassSymbolImpl private[Symbols] (coord: Coord, val assocFile: AbstractFile, id: Int)
503-
extends SymbolImpl(coord, id), ClassSymbolDecl {
500+
class ClassSymbolImpl private[Symbols] (
501+
maybeOwner: SymbolImpl,
502+
name: Name,
503+
initFlags: FlagSet,
504+
initInfo: Type,
505+
initPrivateWithin: SymbolImpl,
506+
initCoord: Coord,
507+
val assocFile: AbstractFile,
508+
id: Int)
509+
extends SymbolImpl(
510+
maybeOwner, name, initFlags, initInfo, initPrivateWithin, initCoord, id),
511+
ClassSymbolDecl,
512+
ClassDenotation {
504513

505514
type ThisName = TypeName
506515

@@ -591,14 +600,44 @@ object Symbols:
591600
}
592601

593602
/** Kept separate since it keeps only PackageClassDenotation */
594-
class PackageClassSymbolImpl(id: Int) extends ClassSymbolImpl(NoCoord, null, id)
603+
class PackageClassSymbolImpl private[Symbols] (
604+
maybeOwner: SymbolImpl,
605+
name: Name,
606+
initFlags: FlagSet,
607+
initInfo: Type,
608+
initPrivateWithin: SymbolImpl,
609+
id: Int)
610+
extends ClassSymbolImpl(
611+
maybeOwner, name, initFlags, initInfo, initPrivateWithin, NoCoord, null, id),
612+
PackageClassDenotation
595613

596614
@sharable
597-
val NoSymbol: Symbol = new SymbolImpl(NoCoord, 0) {
598-
override def associatedFile(using Context): AbstractFile = NoSource.file
599-
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
600-
}.fromSymbolImpl
601-
NoDenotation // force it in order to set `denot` field of NoSymbol
615+
val NoSymbol: Symbol = new SymbolImpl(
616+
null, "<none>".toTermName, Permanent, NoType, null, NoCoord, 0):
617+
618+
override def isType: Boolean = false
619+
override def isTerm: Boolean = false
620+
override def exists: Boolean = false
621+
override def owner: Symbol = throw new AssertionError("NoDenotation.owner")
622+
override def computeAsSeenFrom(pre: Type)(using Context): SingleDenotation = this
623+
override def mapInfo(f: Type => Type)(using Context): SingleDenotation = this
624+
625+
override def matches(other: SingleDenotation)(using Context): Boolean = false
626+
override def targetName(using Context): Name = EmptyTermName
627+
override def mapInherited(ownDenots: PreDenotation, prevDenots: PreDenotation, pre: Type)(using Context): SingleDenotation = this
628+
override def filterWithPredicate(p: SingleDenotation => Boolean): SingleDenotation = this
629+
override def filterDisjoint(denots: PreDenotation)(using Context): SingleDenotation = this
630+
override def filterWithFlags(required: FlagSet, excluded: FlagSet)(using Context): SingleDenotation = this
631+
632+
validFor = Period.allInRun(NoRunId)
633+
634+
override def associatedFile(using Context): AbstractFile = NoSource.file
635+
636+
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation =
637+
NoDenotation
638+
.fromSymbolImpl
639+
640+
// NoDenotation // force it in order to set `denot` field of NoSymbol
602641

603642
extension [N <: Name](sym: Symbol { type ThisName = N })(using Context) {
604643
/** Copy a symbol, overriding selective fields.
@@ -653,12 +692,11 @@ object Symbols:
653692
flags: FlagSet,
654693
info: Type,
655694
privateWithin: Symbol = NoSymbol,
656-
coord: Coord = NoCoord)(using Context): Symbol { type ThisName = N } = {
657-
val sym = new SymbolImpl(coord, ctx.base.nextSymId).asInstanceOf[Symbol { type ThisName = N }]
658-
val denot = SymDenotation(sym, owner, name, flags, info, privateWithin)
659-
sym.denot = denot
660-
sym
661-
}
695+
coord: Coord = NoCoord)(using Context): Symbol { type ThisName = N } =
696+
val symi = new SymbolImpl(
697+
owner.toSymbolImpl, name, flags, info, privateWithin.toSymbolImpl, coord, ctx.base.nextSymId)
698+
symi.validFor = currentStablePeriod
699+
symi.asInstanceOf[Symbol { type ThisName = N }]
662700

663701
/** Create a class symbol from its non-info fields and a function
664702
* producing its info (the produced info may be lazy).
@@ -670,18 +708,18 @@ object Symbols:
670708
infoFn: ClassSymbol => Type,
671709
privateWithin: Symbol = NoSymbol,
672710
coord: Coord = NoCoord,
673-
assocFile: AbstractFile = null)(using Context): ClassSymbol
674-
= {
675-
val cls = (
711+
assocFile: AbstractFile = null)(using Context): ClassSymbol =
712+
val clsi =
676713
if flags.is(Package) then
677-
new PackageClassSymbolImpl(ctx.base.nextSymId)
714+
new PackageClassSymbolImpl(
715+
owner.toSymbolImpl, name, flags, NoCompleter, privateWithin.toSymbolImpl, ctx.base.nextSymId)
678716
else
679-
new ClassSymbolImpl(coord, assocFile, ctx.base.nextSymId)
680-
).fromSymbolImpl.asClass
681-
val denot = SymDenotation(cls, owner, name, flags, infoFn(cls), privateWithin)
682-
cls.denot = denot
717+
new ClassSymbolImpl(
718+
owner.toSymbolImpl, name, flags, NoCompleter, privateWithin.toSymbolImpl, coord, assocFile, ctx.base.nextSymId)
719+
clsi.validFor = currentStablePeriod
720+
val cls = clsi.fromSymbolImpl.asClass
721+
cls.info = infoFn(cls)
683722
cls
684-
}
685723

686724
/** Create a class symbol from its non-info fields and the fields of its info. */
687725
def newCompleteClassSymbol(

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
25442544
}
25452545

25462546
def isTypeParam: Boolean = self.isTypeParam
2547-
def signature: Signature = self.signature
2547+
def signature: Signature = self.denot.signature
25482548
def moduleClass: Symbol = self.denot.moduleClass
25492549
def companionClass: Symbol = self.denot.companionClass
25502550
def companionModule: Symbol = self.denot.companionModule

0 commit comments

Comments
 (0)