Skip to content

Commit 2cccb23

Browse files
committed
Move .tree to Reflection.Symbol
1 parent 10091bd commit 2cccb23

File tree

12 files changed

+42
-89
lines changed

12 files changed

+42
-89
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,16 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
14581458

14591459
def Symbol_flags(self: Symbol)(given Context): Flags = self.flags
14601460

1461+
def Symbol_tree(self: Symbol)(given Context): Tree = {
1462+
assert(!self.is(Case))
1463+
FromSymbol.definitionFromSym(self)
1464+
}
1465+
1466+
def Symbol_pattern(self: Symbol)(given ctx: Context): Pattern = {
1467+
assert(self.is(Case))
1468+
FromSymbol.definitionFromSym(self)
1469+
}
1470+
14611471
def Symbol_privateWithin(self: Symbol)(given Context): Option[Type] = {
14621472
val within = self.privateWithin
14631473
if (within.exists && !self.is(core.Flags.Protected)) Some(within.typeRef)
@@ -1511,9 +1521,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15111521
def matchPackageDefSymbol(symbol: Symbol)(given Context): Option[PackageDefSymbol] =
15121522
if (symbol.is(core.Flags.Package)) Some(symbol) else None
15131523

1514-
def PackageDefSymbol_tree(self: PackageDefSymbol)(given Context): PackageDef =
1515-
FromSymbol.packageDefFromSym(self)
1516-
15171524
type TypeSymbol = core.Symbols.TypeSymbol
15181525

15191526
def matchTypeSymbol(symbol: Symbol)(given Context): Option[TypeSymbol] =
@@ -1524,9 +1531,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15241531
def matchClassDefSymbol(symbol: Symbol)(given Context): Option[ClassDefSymbol] =
15251532
if (symbol.isClass) Some(symbol.asClass) else None
15261533

1527-
def ClassDefSymbol_tree(self: ClassDefSymbol)(given Context): ClassDef =
1528-
FromSymbol.classDef(self)
1529-
15301534
def ClassDefSymbol_fields(self: Symbol)(given Context): List[Symbol] =
15311535
self.unforcedDecls.filter(isField)
15321536

@@ -1590,8 +1594,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15901594
def matchTypeDefSymbol(symbol: Symbol)(given Context): Option[TypeDefSymbol] =
15911595
if (symbol.isType) Some(symbol.asType) else None
15921596

1593-
def TypeDefSymbol_tree(self: TypeDefSymbol)(given Context): TypeDef =
1594-
FromSymbol.typeDefFromSym(self)
15951597
def TypeDefSymbol_isTypeParam(self: TypeDefSymbol)(given Context): Boolean =
15961598
self.isTypeParam
15971599

@@ -1600,9 +1602,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
16001602
def matchTypeBindSymbol(symbol: Symbol)(given Context): Option[TypeBindSymbol] =
16011603
if (symbol.isType && symbol.is(core.Flags.Case)) Some(symbol.asType) else None
16021604

1603-
def TypeBindSymbol_tree(self: TypeBindSymbol)(given Context): TypeBind =
1604-
FromSymbol.typeBindFromSym(self)
1605-
16061605
type TermSymbol = core.Symbols.TermSymbol
16071606

16081607
def matchTermSymbol(symbol: Symbol)(given Context): Option[TermSymbol] =
@@ -1613,9 +1612,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
16131612
def matchDefDefSymbol(symbol: Symbol)(given Context): Option[DefDefSymbol] =
16141613
if (symbol.isTerm && symbol.is(core.Flags.Method)) Some(symbol.asTerm) else None
16151614

1616-
def DefDefSymbol_tree(self: DefDefSymbol)(given Context): DefDef =
1617-
FromSymbol.defDefFromSym(self)
1618-
16191615
def DefDefSymbol_signature(self: DefDefSymbol)(given Context): Signature =
16201616
self.signature
16211617

@@ -1624,9 +1620,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
16241620
def matchValDefSymbol(symbol: Symbol)(given Context): Option[ValDefSymbol] =
16251621
if (symbol.isTerm && !symbol.is(core.Flags.Method) && !symbol.is(core.Flags.Case)) Some(symbol.asTerm) else None
16261622

1627-
def ValDefSymbol_tree(self: ValDefSymbol)(given Context): ValDef =
1628-
FromSymbol.valDefFromSym(self)
1629-
16301623
def ValDefSymbol_moduleClass(self: ValDefSymbol)(given Context): Option[ClassDefSymbol] = {
16311624
val sym = self.moduleClass
16321625
if (sym.exists) Some(sym.asClass) else None
@@ -1642,9 +1635,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
16421635
def matchBindSymbol(symbol: Symbol)(given Context): Option[BindSymbol] =
16431636
if (symbol.isTerm && symbol.is(core.Flags.Case)) Some(symbol.asTerm) else None
16441637

1645-
def BindSymbol_tree(self: BindSymbol)(given Context): Bind =
1646-
FromSymbol.bindFromSym(self)
1647-
16481638
type NoSymbol = core.Symbols.NoSymbol.type
16491639

16501640
def matchNoSymbol(symbol: Symbol)(given Context): Boolean = symbol eq core.Symbols.NoSymbol

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,10 @@ trait CompilerInterface {
12061206
/** Flags of this symbol */
12071207
def Symbol_flags(self: Symbol)(given ctx: Context): Flags
12081208

1209+
def Symbol_tree(self: Symbol)(given ctx: Context): Tree
1210+
1211+
def Symbol_pattern(self: Symbol)(given ctx: Context): Pattern
1212+
12091213
def Symbol_isLocalDummy(self: Symbol)(given ctx: Context): Boolean
12101214

12111215
def Symbol_isRefinementClass(self: Symbol)(given ctx: Context): Boolean
@@ -1250,8 +1254,6 @@ trait CompilerInterface {
12501254

12511255
def matchPackageDefSymbol(symbol: Symbol)(given ctx: Context): Option[PackageDefSymbol]
12521256

1253-
def PackageDefSymbol_tree(self: PackageDefSymbol)(given ctx: Context): PackageDef
1254-
12551257
type TypeSymbol <: Symbol
12561258

12571259
def matchTypeSymbol(symbol: Symbol)(given ctx: Context): Option[TypeSymbol]
@@ -1261,9 +1263,6 @@ trait CompilerInterface {
12611263

12621264
def matchClassDefSymbol(symbol: Symbol)(given ctx: Context): Option[ClassDefSymbol]
12631265

1264-
/** ClassDef tree of this definition */
1265-
def ClassDefSymbol_tree(self: ClassDefSymbol)(given ctx: Context): ClassDef
1266-
12671266
/** Fields directly declared in the class */
12681267
def ClassDefSymbol_fields(self: Symbol)(given ctx: Context): List[Symbol]
12691268

@@ -1303,17 +1302,11 @@ trait CompilerInterface {
13031302

13041303
def TypeDefSymbol_isTypeParam(self: TypeDefSymbol)(given ctx: Context): Boolean
13051304

1306-
/** TypeDef tree of this definition */
1307-
def TypeDefSymbol_tree(self: TypeDefSymbol)(given ctx: Context): TypeDef
1308-
13091305
/** Symbol representing a bind definition. */
13101306
type TypeBindSymbol <: TypeSymbol
13111307

13121308
def matchTypeBindSymbol(symbol: Symbol)(given ctx: Context): Option[TypeBindSymbol]
13131309

1314-
/** TypeBind pattern of this definition */
1315-
def TypeBindSymbol_tree(self: TypeBindSymbol)(given ctx: Context): TypeBind
1316-
13171310
type TermSymbol <: Symbol
13181311

13191312
def matchTermSymbol(symbol: Symbol)(given ctx: Context): Option[TermSymbol]
@@ -1323,9 +1316,6 @@ trait CompilerInterface {
13231316

13241317
def matchDefDefSymbol(symbol: Symbol)(given ctx: Context): Option[DefDefSymbol]
13251318

1326-
/** DefDef tree of this definition */
1327-
def DefDefSymbol_tree(self: DefDefSymbol)(given ctx: Context): DefDef
1328-
13291319
/** Signature of this definition */
13301320
def DefDefSymbol_signature(self: DefDefSymbol)(given ctx: Context): Signature
13311321

@@ -1334,9 +1324,6 @@ trait CompilerInterface {
13341324

13351325
def matchValDefSymbol(symbol: Symbol)(given ctx: Context): Option[ValDefSymbol]
13361326

1337-
/** ValDef tree of this definition */
1338-
def ValDefSymbol_tree(self: ValDefSymbol)(given ctx: Context): ValDef
1339-
13401327
/** The class symbol of the companion module class */
13411328
def ValDefSymbol_moduleClass(self: ValDefSymbol)(given ctx: Context): Option[ClassDefSymbol]
13421329

@@ -1347,9 +1334,6 @@ trait CompilerInterface {
13471334

13481335
def matchBindSymbol(symbol: Symbol)(given ctx: Context): Option[BindSymbol]
13491336

1350-
/** Bind pattern of this definition */
1351-
def BindSymbol_tree(self: BindSymbol)(given ctx: Context): Bind
1352-
13531337
/** No symbol available. */
13541338
type NoSymbol <: Symbol
13551339

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ trait Printers
12741274
val name = arg.name
12751275
arg.symbol.owner match {
12761276
case IsDefDefSymbol(sym) if sym.name == "<init>" =>
1277-
val ClassDef(_, _, _, _, _, body) = sym.owner.asClassDef.tree
1277+
val ClassDef(_, _, _, _, _, body) = sym.owner.tree
12781278
body.collectFirst {
12791279
case IsValDef(vdef @ ValDef(`name`, _, _)) if vdef.symbol.flags.is(Flags.ParamAccessor) =>
12801280
if (!vdef.symbol.flags.is(Flags.Local)) {

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ trait SymbolOps extends Core {
2020
/** This symbol is protected within the resulting type */
2121
def protectedWithin(given ctx: Context): Option[Type] = internal.Symbol_protectedWithin(self)
2222

23+
2324
/** The name of this symbol */
2425
def name(given ctx: Context): String = internal.Symbol_name(self)
2526

@@ -70,6 +71,14 @@ trait SymbolOps extends Core {
7071
case _ => throw new Exception("not a BindSymbol")
7172
}
7273

74+
/** Tree of this definition */
75+
def tree(given ctx: Context): Tree =
76+
internal.Symbol_tree(self)
77+
78+
/** Pattern of this definition */
79+
def pattern(given ctx: Context): Pattern =
80+
internal.Symbol_pattern(self)
81+
7382
/** Annotations attached to this symbol */
7483
def annots(given ctx: Context): List[Term] = internal.Symbol_annots(self)
7584

@@ -97,11 +106,6 @@ trait SymbolOps extends Core {
97106
internal.matchPackageDefSymbol(symbol)
98107
}
99108

100-
implicit class PackageDefSymbolAPI(self: PackageDefSymbol) {
101-
def tree(given ctx: Context): PackageDef =
102-
internal.PackageDefSymbol_tree(self)
103-
}
104-
105109
// TypeSymbol
106110

107111
object IsTypeSymbol {
@@ -123,9 +127,6 @@ trait SymbolOps extends Core {
123127
}
124128

125129
implicit class ClassDefSymbolAPI(self: ClassDefSymbol) {
126-
/** ClassDef tree of this defintion */
127-
def tree(given ctx: Context): ClassDef =
128-
internal.ClassDefSymbol_tree(self)
129130

130131
/** Fields directly declared in the class */
131132
def fields(given ctx: Context): List[Symbol] =
@@ -176,10 +177,6 @@ trait SymbolOps extends Core {
176177
}
177178

178179
implicit class TypeDefSymbolAPI(self: TypeDefSymbol) {
179-
/** TypeDef tree of this definition */
180-
def tree(given ctx: Context): TypeDef =
181-
internal.TypeDefSymbol_tree(self)
182-
183180
def isTypeParam(given ctx: Context): Boolean =
184181
internal.TypeDefSymbol_isTypeParam(self)
185182
}
@@ -191,12 +188,6 @@ trait SymbolOps extends Core {
191188
internal.matchTypeBindSymbol(symbol)
192189
}
193190

194-
implicit class TypeBindSymbolAPI(self: TypeBindSymbol) {
195-
/** TypeBind pattern of this definition */
196-
def tree(given ctx: Context): TypeBind =
197-
internal.TypeBindSymbol_tree(self)
198-
}
199-
200191
// TermSymbol
201192

202193
object IsTermSymbol {
@@ -212,10 +203,6 @@ trait SymbolOps extends Core {
212203
}
213204

214205
implicit class DefDefSymbolAPI(self: DefDefSymbol) {
215-
/** DefDef tree of this defintion */
216-
def tree(given ctx: Context): DefDef =
217-
internal.DefDefSymbol_tree(self)
218-
219206
/** Signature of this defintion */
220207
def signature(given ctx: Context): Signature =
221208
internal.DefDefSymbol_signature(self)
@@ -229,10 +216,6 @@ trait SymbolOps extends Core {
229216
}
230217

231218
implicit class ValDefSymbolAPI(self: ValDefSymbol) {
232-
/** ValDef tree of this defintion */
233-
def tree(given ctx: Context): ValDef =
234-
internal.ValDefSymbol_tree(self)
235-
236219
/** The class symbol of the companion module class */
237220
def moduleClass(given ctx: Context): Option[ClassDefSymbol] =
238221
internal.ValDefSymbol_moduleClass(self)
@@ -248,12 +231,6 @@ trait SymbolOps extends Core {
248231
internal.matchBindSymbol(symbol)
249232
}
250233

251-
implicit class BindSymbolAPI(self: BindSymbol) {
252-
/** Bind pattern of this definition */
253-
def tree(given ctx: Context): Bind =
254-
internal.BindSymbol_tree(self)
255-
}
256-
257234
// NoSymbol
258235

259236
object NoSymbol {

tests/run-custom-args/Yretain-trees/tasty-definitions-2/Macro_1.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ object Foo {
99
def inspectBodyImpl(x: Expr[Int])(given qctx: QuoteContext): Expr[String] = {
1010
import qctx.tasty._
1111
def definitionString(tree: Tree): Expr[String] = tree.symbol match {
12-
case IsDefDefSymbol(sym) => sym.tree.showExtractors
13-
case IsValDefSymbol(sym) => sym.tree.showExtractors
14-
case IsBindSymbol(sym) => sym.tree.showExtractors
12+
case IsBindSymbol(sym) => sym.pattern.showExtractors
13+
case sym => sym.tree.showExtractors
1514
}
1615
x.unseal match {
1716
case Inlined(None, Nil, arg) => definitionString(arg)

tests/run-custom-args/Yretain-trees/tasty-definitions-3/Macro_1.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ object Foo {
99
def inspectBodyImpl(x: Expr[Int])(given qctx: QuoteContext): Expr[String] = {
1010
import qctx.tasty._
1111
def definitionString(tree: Tree): Expr[String] = tree.symbol match {
12-
case IsDefDefSymbol(sym) => sym.tree.showExtractors
13-
case IsValDefSymbol(sym) => sym.tree.showExtractors
14-
case IsBindSymbol(sym) => sym.tree.showExtractors
12+
case IsBindSymbol(sym) => sym.pattern.showExtractors
13+
case sym => sym.tree.showExtractors
1514
}
1615
x.unseal match {
1716
case Inlined(None, Nil, arg) => definitionString(arg)

tests/run-macros/inferred-repeated-result/test_1.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ object Macros {
1111
val methods =
1212
tree.tpe.classSymbol.get.classMethods.map { m =>
1313
val name = m.show
14-
val returnType = m.tree.returnTpt.tpe.show
14+
val IsDefDef(ddef) = m.tree
15+
val returnType = ddef.returnTpt.tpe.show
1516
s"$name : $returnType"
1617
}.sorted
1718

tests/run-macros/tasty-eval/quoted_1.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ object Macros {
2222

2323
e.unseal.tpe match {
2424
case Type.IsTermRef(pre) if pre.termSymbol.isValDef =>
25-
pre.termSymbol.asValDef.tree.tpt.tpe match {
25+
val IsValDef(t) = pre.termSymbol.tree
26+
t.tpt.tpe match {
2627
case Type.ConstantType(Constant(i: Int)) => Some(i)
2728
case _ => None
2829
}

0 commit comments

Comments
 (0)