Skip to content

Commit e1cc241

Browse files
committed
Create Interfaces for Symbol and ClassSymbol
1 parent 88892fc commit e1cc241

File tree

1 file changed

+114
-3
lines changed

1 file changed

+114
-3
lines changed

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

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import util.{SourceFile, NoSource, Property, SourcePosition, SrcPos, EqHashMap}
3434
import scala.collection.JavaConverters._
3535
import scala.annotation.internal.sharable
3636
import config.Printers.typr
37+
import annotation.targetName
3738

3839
object Symbols {
3940

@@ -42,12 +43,120 @@ object Symbols {
4243
/** Tree attachment containing the identifiers in a tree as a sorted array */
4344
val Ids: Property.Key[Array[String]] = new Property.Key
4445

46+
abstract class SymbolDecl extends Designator, ParamInfo, SrcPos, printing.Showable:
47+
type ThisName <: Name
48+
49+
/** A unique id */
50+
def id: Int
51+
52+
/** The coordinate of this symbol (either an index or a span) */
53+
def coord: Coord
54+
55+
/** Set the coordinate of this symbol. This is only useful when the coordinate is
56+
* not known at symbol creation. This is the case for root symbols unpickled from TASTY.
57+
*
58+
* @pre coord == NoCoord
59+
*/
60+
private[core] def coord_=(c: Coord): Unit
61+
62+
/** The tree defining the symbol at pickler time, EmptyTree if none was retained */
63+
def defTree: Tree
64+
65+
/** Set defining tree if this symbol retains its definition tree */
66+
def defTree_=(tree: Tree)(using Context): Unit
67+
68+
/** Does this symbol retain its definition tree?
69+
* A good policy for this needs to balance costs and benefits, where
70+
* costs are mainly memoty leaks, in particular across runs.
71+
*/
72+
def retainsDefTree(using Context): Boolean
73+
74+
def denot(using Context): SymDenotation
75+
private[core] def denot_=(d: SymDenotation): Unit
76+
private[core] def invalidateDenotCache(): Unit
77+
78+
def originDenotation: SymDenotation
79+
def lastKnownDenotation: SymDenotation
80+
private[core] def defRunId: RunId
81+
def isDefinedInCurrentRun(using Context): Boolean
82+
def isValidInCurrentRun(using Context): Boolean
83+
84+
@targetName("invalidSymbol")
85+
def symbol(implicit ev: DontUseSymbolOnSymbol): Nothing
86+
87+
def isTerm(using Context): Boolean
88+
def isType(using Context): Boolean
89+
def asTerm(using Context): TermSymbol
90+
def asType(using Context): TypeSymbol
91+
def isClass: Boolean
92+
def asClass: ClassSymbol
93+
94+
def isPrivate(using Context): Boolean
95+
def isPatternBound(using Context): Boolean
96+
def isStatic(using Context): Boolean
97+
98+
def name(using Context): ThisName
99+
def signature(using Context): Signature
100+
101+
def span: Span
102+
def sourcePos(using Context): SourcePosition
103+
104+
def associatedFile(using Context): AbstractFile
105+
def binaryFile(using Context): AbstractFile
106+
def source(using Context): SourceFile
107+
108+
def sourceSymbol(using Context): Symbol
109+
110+
def entered(using Context): this.type
111+
def enteredAfter(phase: DenotTransformer)(using Context): this.type
112+
113+
def drop()(using Context): Unit
114+
def dropAfter(phase: DenotTransformer)(using Context): Unit
115+
116+
inline def orElse(inline that: Symbol)(using Context): Symbol
117+
def filter(p: Symbol => Boolean): Symbol
118+
119+
// ParamInfo types and methods
120+
def isTypeParam(using Context): Boolean
121+
def paramName(using Context): ThisName
122+
def paramInfo(using Context): Type
123+
def paramInfoAsSeenFrom(pre: Type)(using Context): Type
124+
def paramInfoOrCompleter(using Context): Type
125+
def paramVariance(using Context): Variance
126+
def paramRef(using Context): TypeRef
127+
128+
// Printing
129+
def toText(printer: Printer): Text
130+
def showLocated(using Context): String
131+
def showExtendedLocation(using Context): String
132+
def showDcl(using Context): String
133+
def showKind(using Context): String
134+
def showName(using Context): String
135+
def showFullName(using Context): String
136+
end SymbolDecl
137+
138+
trait ClassSymbolDecl extends SymbolDecl:
139+
type ThisName = TypeName
140+
type TreeOrProvider = tpd.TreeProvider | tpd.Tree
141+
142+
def classDenot(using Context): ClassDenotation
143+
144+
def assocFile: AbstractFile
145+
146+
def rootTree(using Context): Tree
147+
def rootTreeContaining(id: String)(using Context): Tree
148+
def rootTreeOrProvider: TreeOrProvider
149+
private[dotc] def rootTreeOrProvider_=(t: TreeOrProvider)(using Context): Unit
150+
151+
def sourceOfClass(using Context): SourceFile
152+
end ClassSymbolDecl
153+
45154
/** A Symbol represents a Scala definition/declaration or a package.
46155
* @param coord The coordinates of the symbol (a position or an index)
47156
* @param id A unique identifier of the symbol (unique per ContextBase)
48157
*/
49158
class Symbol private[Symbols] (private var myCoord: Coord, val id: Int)
50-
extends Designator, ParamInfo, SrcPos, printing.Showable {
159+
extends SymbolDecl {
51160

52161
type ThisName <: Name
53162

@@ -275,8 +384,8 @@ object Symbols {
275384
* the same as `x`.
276385
* With the given setup, all such calls will give implicit-not found errors
277386
*/
387+
@targetName("invalidSymbol")
278388
final def symbol(implicit ev: DontUseSymbolOnSymbol): Nothing = unsupported("symbol")
279-
type DontUseSymbolOnSymbol
280389

281390
final def source(using Context): SourceFile = {
282391
def valid(src: SourceFile): SourceFile =
@@ -369,7 +478,7 @@ object Symbols {
369478
type TypeSymbol = Symbol { type ThisName = TypeName }
370479

371480
class ClassSymbol private[Symbols] (coord: Coord, val assocFile: AbstractFile, id: Int)
372-
extends Symbol(coord, id) {
481+
extends Symbol(coord, id), ClassSymbolDecl {
373482

374483
type ThisName = TypeName
375484

@@ -506,6 +615,8 @@ object Symbols {
506615
def MutableSymbolMap[T](): EqHashMap[Symbol, T] = EqHashMap[Symbol, T]()
507616
def MutableSymbolMap[T](initialCapacity: Int): EqHashMap[Symbol, T] = EqHashMap[Symbol, T](initialCapacity)
508617

618+
type DontUseSymbolOnSymbol
619+
509620
// ---- Symbol creation methods ----------------------------------
510621

511622
/** Create a symbol from its fields (info may be lazy) */

0 commit comments

Comments
 (0)