Skip to content

Commit a2f4c91

Browse files
committed
Move Symbol fields to SymDenotation
1 parent e52a3c9 commit a2f4c91

File tree

3 files changed

+106
-46
lines changed

3 files changed

+106
-46
lines changed

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

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Trees.Literal
1717
import Variances.Variance
1818
import annotation.tailrec
1919
import util.SimpleIdentityMap
20-
import util.{SrcPos, SourcePosition, Stats}
20+
import util.{SrcPos, SourcePosition, SourceFile, NoSource, Stats}
2121
import util.Spans.*
2222
import java.util.WeakHashMap
2323
import scala.util.control.NonFatal
@@ -31,11 +31,37 @@ import scala.annotation.internal.sharable
3131

3232
object SymDenotations {
3333

34+
type TreeOrProvider = tpd.TreeProvider | tpd.Tree
35+
36+
class SymCommon(
37+
//private[SymDenotations]
38+
var coord: Coord,
39+
//private[SymDenotations]
40+
val id: Int,
41+
//private[SymDenotations]
42+
val nestingLevel: Int):
43+
44+
//private[SymDenotations]
45+
var defTree: tpd.Tree | Null = null
46+
//private[SymDenotations]
47+
def asClass: ClassCommon = asInstanceOf[ClassCommon]
48+
end SymCommon
49+
50+
class ClassCommon(coord: Coord, id: Int, nestingLevel: Int,
51+
//private[SymDenotations]
52+
val assocFile: AbstractFile | Null = null)
53+
extends SymCommon(coord, id, nestingLevel):
54+
//private[SymDenotations]
55+
var treeOrProvider: TreeOrProvider = tpd.EmptyTree
56+
//private[SymDenotations]
57+
var source: SourceFile = NoSource
58+
3459
/** A sym-denotation represents the contents of a definition
3560
* during a period.
3661
*/
3762
class SymDenotation private[SymDenotations] (
3863
symbol: Symbol,
64+
final val common: SymCommon,
3965
final val maybeOwner: Symbol,
4066
final val name: Name,
4167
initFlags: FlagSet,
@@ -576,6 +602,17 @@ object SymDenotations {
576602

577603
myTargetName.nn
578604

605+
/** The source or class file from which this class or
606+
* the class containing this symbol was generated, null if not applicable.
607+
* Note that this the returned classfile might be the top-level class
608+
* containing this symbol instead of the directly enclosing class.
609+
* Overridden in ClassSymbol
610+
*/
611+
def associatedFile(using Context): AbstractFile | Null =
612+
topLevelClass.associatedFile
613+
614+
// ----- Symbol ops --------------------------------------------
615+
579616
// ----- Tests -------------------------------------------------
580617

581618
/** Is this denotation a class? */
@@ -1606,7 +1643,7 @@ object SymDenotations {
16061643
val privateWithin1 = if (privateWithin != null) privateWithin else this.privateWithin
16071644
val annotations1 = if (annotations != null) annotations else this.annotations
16081645
val rawParamss1 = if rawParamss != null then rawParamss else this.rawParamss
1609-
val d = SymDenotation(symbol, owner, name, initFlags1, info1, privateWithin1)
1646+
val d = SymDenotation(symbol, symbol.lastKnownDenotation.common, owner, name, initFlags1, info1, privateWithin1)
16101647
d.annotations = annotations1
16111648
d.rawParamss = rawParamss1
16121649
d.registeredCompanion = registeredCompanion
@@ -1777,12 +1814,13 @@ object SymDenotations {
17771814
*/
17781815
class ClassDenotation private[SymDenotations] (
17791816
symbol: Symbol,
1817+
common: ClassCommon,
17801818
maybeOwner: Symbol,
17811819
name: Name,
17821820
initFlags: FlagSet,
17831821
initInfo: Type,
17841822
initPrivateWithin: Symbol)
1785-
extends SymDenotation(symbol, maybeOwner, name, initFlags, initInfo, initPrivateWithin) {
1823+
extends SymDenotation(symbol, common, maybeOwner, name, initFlags, initInfo, initPrivateWithin) {
17861824

17871825
import util.EqHashMap
17881826

@@ -2422,19 +2460,26 @@ object SymDenotations {
24222460

24232461
override def registeredCompanion_=(c: Symbol) =
24242462
myCompanion = c
2463+
2464+
/** The source or class file from which this class was generated, null if not applicable. */
2465+
override def associatedFile(using Context): AbstractFile | Null =
2466+
val af = common.assocFile
2467+
if af != null || this.is(Package) || this.owner.is(Package) then af
2468+
else super.associatedFile
24252469
}
24262470

24272471
/** The denotation of a package class.
24282472
* It overrides ClassDenotation to take account of package objects when looking for members
24292473
*/
24302474
final class PackageClassDenotation private[SymDenotations] (
24312475
symbol: Symbol,
2476+
common: ClassCommon,
24322477
ownerIfExists: Symbol,
24332478
name: Name,
24342479
initFlags: FlagSet,
24352480
initInfo: Type,
24362481
initPrivateWithin: Symbol)
2437-
extends ClassDenotation(symbol, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
2482+
extends ClassDenotation(symbol, common, ownerIfExists, name, initFlags, initInfo, initPrivateWithin) {
24382483

24392484
private var packageObjsCache: List[ClassDenotation] = _
24402485
private var packageObjsRunId: RunId = NoRunId
@@ -2595,7 +2640,7 @@ object SymDenotations {
25952640
}
25962641

25972642
@sharable object NoDenotation
2598-
extends SymDenotation(NoSymbol, NoSymbol, "<none>".toTermName, Permanent, NoType) {
2643+
extends SymDenotation(NoSymbol, SymCommon(NoCoord, 0, 0), NoSymbol, "<none>".toTermName, Permanent, NoType) {
25992644
override def isTerm: Boolean = false
26002645
override def exists: Boolean = false
26012646
override def owner: Symbol = throw new AssertionError("NoDenotation.owner")
@@ -2609,6 +2654,7 @@ object SymDenotations {
26092654
override def filterWithPredicate(p: SingleDenotation => Boolean): SingleDenotation = this
26102655
override def filterDisjoint(denots: PreDenotation)(using Context): SingleDenotation = this
26112656
override def filterWithFlags(required: FlagSet, excluded: FlagSet)(using Context): SingleDenotation = this
2657+
override def associatedFile(using Context): AbstractFile | Null = NoSource.file
26122658

26132659
NoSymbol.denot = this
26142660
validFor = Period.allInRun(NoRunId)
@@ -2630,16 +2676,20 @@ object SymDenotations {
26302676
*/
26312677
def SymDenotation(
26322678
symbol: Symbol,
2679+
common: SymCommon,
26332680
owner: Symbol,
26342681
name: Name,
26352682
initFlags: FlagSet,
26362683
initInfo: Type,
26372684
initPrivateWithin: Symbol = NoSymbol)(using Context): SymDenotation = {
26382685
val result =
2639-
if (symbol.isClass)
2640-
if (initFlags.is(Package)) new PackageClassDenotation(symbol, owner, name, initFlags, initInfo, initPrivateWithin)
2641-
else new ClassDenotation(symbol, owner, name, initFlags, initInfo, initPrivateWithin)
2642-
else new SymDenotation(symbol, owner, name, initFlags, initInfo, initPrivateWithin)
2686+
if symbol.isClass then
2687+
if initFlags.is(Package) then
2688+
new PackageClassDenotation(symbol, common.asClass, owner, name, initFlags, initInfo, initPrivateWithin)
2689+
else
2690+
new ClassDenotation(symbol, common.asClass, owner, name, initFlags, initInfo, initPrivateWithin)
2691+
else
2692+
new SymDenotation(symbol, common, owner, name, initFlags, initInfo, initPrivateWithin)
26432693
result.validFor = currentStablePeriod
26442694
result
26452695
}

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

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,45 @@ object Symbols {
4343
* @param coord The coordinates of the symbol (a position or an index)
4444
* @param id A unique identifier of the symbol (unique per ContextBase)
4545
*/
46-
class Symbol private[Symbols] (private var myCoord: Coord, val id: Int, val nestingLevel: Int)
46+
class Symbol private[Symbols] ()
4747
extends ParamInfo, SrcPos, printing.Showable {
4848

4949
type ThisName <: Name
5050

51+
util.Stats.record(s"new ${getClass}")
52+
53+
var initialDenot: SymDenotation = _
54+
5155
//assert(id != 723)
5256

53-
def coord: Coord = myCoord
57+
/** A unique identifier of the symbol (unique per ContextBase) */
58+
def id = initialDenot.common.id
59+
60+
def nestingLevel = initialDenot.common.nestingLevel
61+
62+
/** The coordinates of the symbol (a position or an index) */
63+
def coord: Coord = initialDenot.common.coord
5464

5565
/** Set the coordinate of this class, this is only useful when the coordinate is
5666
* not known at symbol creation. This is the case for root symbols
5767
* unpickled from TASTY.
5868
*
5969
* @pre coord == NoCoord
6070
*/
61-
private[core] def coord_=(c: Coord): Unit = {
71+
private[core] def coord_=(c: Coord): Unit =
6272
// assert(myCoord == NoCoord)
6373
// This assertion fails for CommentPickling test.
6474
// TODO: figure out what's wrong in the setup of CommentPicklingTest and re-enable assertion.
65-
myCoord = c
66-
}
67-
68-
private var myDefTree: Tree | Null = null
75+
initialDenot.common.coord = c
6976

7077
/** The tree defining the symbol at pickler time, EmptyTree if none was retained */
7178
def defTree: Tree =
72-
if (myDefTree == null) tpd.EmptyTree else myDefTree.nn
79+
val dt = initialDenot.common.defTree
80+
if dt == null then tpd.EmptyTree else dt.nn
7381

7482
/** Set defining tree if this symbol retains its definition tree */
7583
def defTree_=(tree: Tree)(using Context): Unit =
76-
if (retainsDefTree) myDefTree = tree
84+
if retainsDefTree then initialDenot.common.defTree = tree
7785

7886
/** Does this symbol retain its definition tree?
7987
* A good policy for this needs to balance costs and benefits, where
@@ -266,7 +274,7 @@ object Symbols {
266274
* Overridden in ClassSymbol
267275
*/
268276
def associatedFile(using Context): AbstractFile | Null =
269-
lastDenot.topLevelClass.associatedFile
277+
lastDenot.associatedFile
270278

271279
/** The class file from which this class was generated, null if not applicable. */
272280
final def binaryFile(using Context): AbstractFile | Null = {
@@ -392,22 +400,21 @@ object Symbols {
392400
type TermSymbol = Symbol { type ThisName = TermName }
393401
type TypeSymbol = Symbol { type ThisName = TypeName }
394402

395-
class ClassSymbol private[Symbols] (coord: Coord, val assocFile: AbstractFile | Null, id: Int, nestingLevel: Int)
396-
extends Symbol(coord, id, nestingLevel) {
403+
class ClassSymbol private[Symbols] extends Symbol {
404+
405+
util.Stats.record("ClassSymbol")
397406

398407
type ThisName = TypeName
399408

400409
type TreeOrProvider = tpd.TreeProvider | tpd.Tree
401410

402-
private var myTree: TreeOrProvider = tpd.EmptyTree
403-
404411
/** If this is a top-level class and `-Yretain-trees` (or `-from-tasty`) is set.
405412
* Returns the TypeDef tree (possibly wrapped inside PackageDefs) for this class, otherwise EmptyTree.
406413
* This will force the info of the class.
407414
*/
408415
def rootTree(using Context): Tree = rootTreeContaining("")
409416

410-
/** Same as `tree` but load tree only if `id == ""` or the tree might contain `id`.
417+
/** Same as `rootTree` but load tree only if `id == ""` or the tree might contain `id`.
411418
* For Tasty trees this means consulting whether the name table defines `id`.
412419
* For already loaded trees, we maintain the referenced ids in an attachment.
413420
*/
@@ -416,11 +423,11 @@ object Symbols {
416423
case _: NoCompleter =>
417424
case _ => denot.ensureCompleted()
418425
}
419-
myTree match {
426+
rootTreeOrProvider match {
420427
case fn: TreeProvider =>
421428
if (id.isEmpty || fn.mightContain(id)) {
422429
val tree = fn.tree
423-
myTree = tree
430+
rootTreeOrProvider = tree
424431
tree
425432
}
426433
else tpd.EmptyTree
@@ -429,10 +436,10 @@ object Symbols {
429436
}
430437
}
431438

432-
def rootTreeOrProvider: TreeOrProvider = myTree
439+
def rootTreeOrProvider: TreeOrProvider = initialDenot.common.asClass.treeOrProvider
433440

434441
private[dotc] def rootTreeOrProvider_=(t: TreeOrProvider)(using Context): Unit =
435-
myTree = t
442+
initialDenot.common.asClass.treeOrProvider = t
436443

437444
private def mightContain(tree: Tree, id: String)(using Context): Boolean = {
438445
val ids = tree.getAttachment(Ids) match {
@@ -451,30 +458,26 @@ object Symbols {
451458
ids.binarySearch(id) >= 0
452459
}
453460

454-
/** The source or class file from which this class was generated, null if not applicable. */
455-
override def associatedFile(using Context): AbstractFile | Null =
456-
if assocFile != null || this.is(Package) || this.owner.is(Package) then assocFile
457-
else super.associatedFile
458-
459-
private var mySource: SourceFile = NoSource
461+
def assocFile: AbstractFile | Null = initialDenot.common.asClass.assocFile
460462

461463
final def sourceOfClass(using Context): SourceFile = {
462-
if !mySource.exists && !denot.is(Package) then
464+
val common = initialDenot.common.asClass
465+
if !common.source.exists && !denot.is(Package) then
463466
// this allows sources to be added in annotations after `sourceOfClass` is first called
464467
val file = associatedFile
465468
if file != null && file.extension != "class" then
466-
mySource = ctx.getSource(file)
469+
common.source = ctx.getSource(file)
467470
else
468-
mySource = defn.patchSource(this)
469-
if !mySource.exists then
470-
mySource = atPhaseNoLater(flattenPhase) {
471+
common.source = defn.patchSource(this)
472+
if !common.source.exists then
473+
common.source = atPhaseNoLater(flattenPhase) {
471474
denot.topLevelClass.unforcedAnnotation(defn.SourceFileAnnot) match
472475
case Some(sourceAnnot) => sourceAnnot.argumentConstant(0) match
473476
case Some(Constant(path: String)) => ctx.getSource(path)
474477
case none => NoSource
475478
case none => NoSource
476479
}
477-
mySource
480+
common.source
478481
}
479482

480483
final def classDenot(using Context): ClassDenotation =
@@ -483,7 +486,11 @@ object Symbols {
483486
override protected def prefixString: String = "ClassSymbol"
484487
}
485488

486-
@sharable object NoSymbol extends Symbol(NoCoord, 0, 0) {
489+
@sharable object NoSymbol extends Symbol {
490+
override def coord = NoCoord
491+
override def id = 0
492+
override def nestingLevel = 0
493+
override def defTree = tpd.EmptyTree
487494
override def associatedFile(using Context): AbstractFile | Null = NoSource.file
488495
override def recomputeDenot(lastd: SymDenotation)(using Context): SymDenotation = NoDenotation
489496
}
@@ -517,8 +524,9 @@ object Symbols {
517524
privateWithin: Symbol = NoSymbol,
518525
coord: Coord = NoCoord,
519526
nestingLevel: Int = ctx.nestingLevel): Symbol { type ThisName = N } = {
520-
val sym = new Symbol(coord, ctx.base.nextSymId, nestingLevel).asInstanceOf[Symbol { type ThisName = N }]
521-
val denot = SymDenotation(sym, owner, name, flags, info, privateWithin)
527+
val sym = new Symbol().asInstanceOf[Symbol { type ThisName = N }]
528+
val denot = SymDenotation(sym, SymCommon(coord, ctx.base.nextSymId, nestingLevel), owner, name, flags, info, privateWithin)
529+
sym.initialDenot = denot
522530
sym.denot = denot
523531
sym
524532
}
@@ -535,9 +543,11 @@ object Symbols {
535543
coord: Coord = NoCoord,
536544
assocFile: AbstractFile | Null = null)(using Context): ClassSymbol
537545
= {
538-
val cls = new ClassSymbol(coord, assocFile, ctx.base.nextSymId, ctx.nestingLevel)
539-
val denot = SymDenotation(cls, owner, name, flags, infoFn(cls), privateWithin)
546+
val cls = new ClassSymbol()
547+
val denot = SymDenotation(cls, ClassCommon(coord, ctx.base.nextSymId, ctx.nestingLevel, assocFile), owner, name, flags, NoType, privateWithin)
548+
cls.initialDenot = denot
540549
cls.denot = denot
550+
denot.info = infoFn(cls)
541551
cls
542552
}
543553

compiler/src/dotty/tools/dotc/interactive/Completion.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ object Completion {
475475
case _: (ExprType | MethodOrPoly) => Method
476476
case _ => EmptyFlags
477477
val symbol = newSymbol(owner = NoSymbol, name, flags, info)
478-
val denot = SymDenotation(symbol, NoSymbol, name, flags, info)
478+
val denot = SymDenotation(symbol, symbol.denot.common, NoSymbol, name, flags, info)
479479
denot +: extractRefinements(parent)
480480
case tp: TypeProxy => extractRefinements(tp.superType)
481481
case _ => List.empty

0 commit comments

Comments
 (0)