Skip to content

Commit 17b1be4

Browse files
committed
Move methods from Definition to Symbol
1 parent 2ee909f commit 17b1be4

File tree

10 files changed

+67
-77
lines changed

10 files changed

+67
-77
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait ContextOpsImpl extends scala.tasty.reflect.ContextOps with TastyCoreImpl {
99
val rootContext: Contexts.Context
1010

1111
def ContextDeco(ctx: Context): ContextAPI = new ContextAPI {
12-
def owner: Definition = definitionFromSym(ctx.owner)(ctx)
12+
def owner: Symbol = ctx.owner
1313

1414
def source: java.nio.file.Path = ctx.compilationUnit.source.file.jpath
1515
}
Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
1-
package dotty.tools.dotc.tastyreflect
1+
package dotty.tools.dotc
2+
package tastyreflect
23

34
import dotty.tools.dotc.core.Symbols._
45

56
trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with TastyCoreImpl {
67

78
def SymbolDeco(symbol: Symbol): SymbolAPI = new SymbolAPI {
89

9-
def owner(implicit ctx: Context): Symbol = symbol.owner
10-
1110
def isEmpty(implicit ctx: Context): Boolean = symbol eq NoSymbol
1211
def isClass(implicit ctx: Context): Boolean = symbol.isClass
1312

1413
def flags(implicit ctx: Context): FlagSet = new FlagSet(symbol.flags)
1514

15+
def privateWithin(implicit ctx: Context): Option[Type] = {
16+
val within = symbol.privateWithin
17+
if (within.exists && !symbol.is(core.Flags.Protected)) Some(within.typeRef)
18+
else None
19+
}
20+
21+
def protectedWithin(implicit ctx: Context): Option[Type] = {
22+
val within = symbol.privateWithin
23+
if (within.exists && symbol.is(core.Flags.Protected)) Some(within.typeRef)
24+
else None
25+
}
26+
1627
def name(implicit ctx: Context): String = symbol.name.toString
1728
def fullName(implicit ctx: Context): String = symbol.fullName.toString
1829

19-
def localContext(implicit ctx: Context): Context = ctx.withOwner(symbol)
30+
def owner(implicit ctx: Context): Symbol = symbol.owner
31+
32+
def localContext(implicit ctx: Context): Context = {
33+
if (symbol.exists) ctx.withOwner(symbol)
34+
else ctx
35+
}
2036

2137
def tree(implicit ctx: Context): Option[Definition] =
2238
if (isEmpty) None else Some(FromSymbol.definitionFromSym(symbol))
2339

40+
def annots(implicit ctx: Context): List[Term] = {
41+
symbol.annotations.flatMap {
42+
case _: core.Annotations.LazyBodyAnnotation => Nil
43+
case annot => annot.tree :: Nil
44+
}
45+
}
46+
2447
}
2548

2649
}

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
6060
}
6161

6262
def DefinitionDeco(definition: Definition): DefinitionAPI = new DefinitionAPI {
63-
6463
def name(implicit ctx: Context): String = definition.symbol.name.toString
65-
66-
def owner(implicit ctx: Context): Definition = definitionFromSym(definition.symbol.owner)
67-
68-
def flags(implicit ctx: Context): FlagSet =
69-
new FlagSet(definition.symbol.flags)
70-
71-
def privateWithin(implicit ctx: Context): Option[Type] = {
72-
val within = definition.symbol.privateWithin
73-
if (within.exists && !definition.symbol.is(core.Flags.Protected)) Some(within.typeRef)
74-
else None
75-
}
76-
77-
def protectedWithin(implicit ctx: Context): Option[Type] = {
78-
val within = definition.symbol.privateWithin
79-
if (within.exists && definition.symbol.is(core.Flags.Protected)) Some(within.typeRef)
80-
else None
81-
}
82-
83-
def annots(implicit ctx: Context): List[Term] = {
84-
definition.symbol.annotations.flatMap {
85-
case _: core.Annotations.LazyBodyAnnotation => Nil
86-
case annot => annot.tree :: Nil
87-
}
88-
}
89-
90-
def localContext(implicit ctx: Context): Context =
91-
if (definition.hasType && definition.symbol.exists) ctx.withOwner(definition.symbol)
92-
else ctx
9364
}
9465

9566
// ClassDef

library/src/scala/tasty/reflect/ContextOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package reflect
44
trait ContextOps extends TastyCore {
55

66
trait ContextAPI {
7-
def owner: Definition
7+
def owner: Symbol
88

99
/** Returns the source file being compiled. The path is relative to the current working directory. */
1010
def source: java.nio.file.Path

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ trait SymbolOps extends TastyCore {
1313

1414
def flags(implicit ctx: Context): FlagSet
1515

16+
def privateWithin(implicit ctx: Context): Option[Type]
17+
def protectedWithin(implicit ctx: Context): Option[Type]
18+
1619
def name(implicit ctx: Context): String
1720
def fullName(implicit ctx: Context): String
1821

1922
def localContext(implicit ctx: Context): Context
2023

2124
def tree(implicit ctx: Context): Option[Definition]
2225

26+
def annots(implicit ctx: Context): List[Term]
27+
2328
}
2429
implicit def SymbolDeco(symbol: Symbol): SymbolAPI
2530

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ trait TreeOps extends TastyCore {
4646

4747
trait DefinitionAPI {
4848
def name(implicit ctx: Context): String
49-
def flags(implicit ctx: Context): FlagSet
50-
def privateWithin(implicit ctx: Context): Option[Type]
51-
def protectedWithin(implicit ctx: Context): Option[Type]
52-
def annots(implicit ctx: Context): List[Term]
53-
def owner(implicit ctx: Context): Definition
54-
def localContext(implicit ctx: Context): Context
5549
}
5650
implicit def DefinitionDeco(definition: Definition): DefinitionAPI
5751

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
7272
case tree @ PackageClause(name, stats) =>
7373
val stats1 = stats.collect {
7474
case IsPackageClause(stat) => stat
75-
case IsDefinition(stat) if !(stat.flags.isObject && stat.flags.isLazy) => stat
75+
case IsDefinition(stat) if !(stat.symbol.flags.isObject && stat.symbol.flags.isLazy) => stat
7676
case stat @ Import(_, _) => stat
7777
}
7878
name match {
@@ -93,14 +93,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
9393
case IsClassDef(cdef @ ClassDef(name, DefDef(_, targs, argss, _, _), parents, self, stats)) =>
9494
printDefAnnotations(cdef)
9595

96-
val flags = cdef.flags
96+
val flags = cdef.symbol.flags
9797
if (flags.isImplicit) this += "implicit "
9898
if (flags.isSealed) this += "sealed "
9999
if (flags.isFinal && !flags.isObject) this += "final "
100100
if (flags.isCase) this += "case "
101101

102102
if (name == "package$") {
103-
this += "package object " += cdef.owner.name.stripSuffix("$")
103+
this += "package object " += cdef.symbol.owner.name.stripSuffix("$")
104104
}
105105
else if (flags.isObject) this += "object " += name.stripSuffix("$")
106106
else if (flags.isTrait) this += "trait " += name
@@ -148,20 +148,20 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
148148
printSeparated(parents1)
149149

150150
def keepDefinition(d: Definition): Boolean = {
151-
val flags = d.flags
151+
val flags = d.symbol.flags
152152
def isCaseClassUnOverridableMethod: Boolean = {
153153
// Currently the compiler does not allow overriding some of the methods generated for case classes
154-
d.flags.isSynthetic &&
154+
d.symbol.flags.isSynthetic &&
155155
(d match {
156-
case DefDef("apply" | "unapply", _, _, _, _) if d.owner.flags.isObject => true
157-
case DefDef(n, _, _, _, _) if d.owner.flags.isCase =>
156+
case DefDef("apply" | "unapply", _, _, _, _) if d.symbol.owner.flags.isObject => true
157+
case DefDef(n, _, _, _, _) if d.symbol.owner.flags.isCase =>
158158
n == "copy" ||
159159
n.matches("copy\\$default\\$[1-9][0-9]*") || // default parameters for the copy method
160160
n.matches("_[1-9][0-9]*") // Getters from Product
161161
case _ => false
162162
})
163163
}
164-
def isInnerModuleObject = d.flags.isLazy && d.flags.isObject
164+
def isInnerModuleObject = d.symbol.flags.isLazy && d.symbol.flags.isObject
165165
!flags.isParam && !flags.isParamAccessor && !flags.isFieldAccessor && !isCaseClassUnOverridableMethod && !isInnerModuleObject
166166
}
167167
val stats1 = stats.collect {
@@ -207,14 +207,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
207207
case IsValDef(vdef @ ValDef(name, tpt, rhs)) =>
208208
printDefAnnotations(vdef)
209209

210-
val flags = vdef.flags
210+
val flags = vdef.symbol.flags
211211
if (flags.isImplicit) this += "implicit "
212212
if (flags.isOverride) this += "override "
213213

214214
printProtectedOrPrivate(vdef)
215215

216216
if (flags.isLazy) this += "lazy "
217-
if (vdef.flags.isMutable) this += "var "
217+
if (vdef.symbol.flags.isMutable) this += "var "
218218
else this += "val "
219219

220220
this += name += ": "
@@ -242,7 +242,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
242242

243243
val isConstructor = name == "<init>"
244244

245-
val flags = ddef.flags
245+
val flags = ddef.symbol.flags
246246
if (flags.isImplicit) this += "implicit "
247247
if (flags.isTransparent) this += "transparent "
248248
if (flags.isOverride) this += "override "
@@ -359,7 +359,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
359359

360360
case Term.Block(stats0, expr) =>
361361
val stats = stats0.filter {
362-
case IsValDef(tree) => !tree.flags.isObject
362+
case IsValDef(tree) => !tree.symbol.flags.isObject
363363
case _ => true
364364
}
365365

@@ -589,8 +589,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
589589
args match {
590590
case Nil =>
591591
case arg :: _ =>
592-
if (arg.flags.isErased) this += "erased "
593-
if (arg.flags.isImplicit) this += "implicit "
592+
if (arg.symbol.flags.isErased) this += "erased "
593+
if (arg.symbol.flags.isImplicit) this += "implicit "
594594
}
595595

596596
def printSeparated(list: List[ValDef]): Unit = list match {
@@ -620,20 +620,20 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
620620

621621
def printParamDef(arg: ValDef): Unit = {
622622
val name = arg.name
623-
arg.owner match {
624-
case DefDef("<init>", _, _, _, _) =>
625-
val ClassDef(_, _, _, _, body) = arg.owner.owner
623+
arg.symbol.owner.tree match {
624+
case Some(DefDef("<init>", _, _, _, _)) =>
625+
val Some(ClassDef(_, _, _, _, body)) = arg.symbol.owner.owner.tree
626626
body.collectFirst {
627-
case IsValDef(vdef @ ValDef(`name`, _, _)) if vdef.flags.isParamAccessor =>
628-
if (!vdef.flags.isLocal) {
627+
case IsValDef(vdef @ ValDef(`name`, _, _)) if vdef.symbol.flags.isParamAccessor =>
628+
if (!vdef.symbol.flags.isLocal) {
629629
var printedPrefix = false
630-
if (vdef.flags.isOverride) {
630+
if (vdef.symbol.flags.isOverride) {
631631
this += "override "
632632
printedPrefix = true
633633
}
634634
printedPrefix |= printProtectedOrPrivate(vdef)
635-
if (vdef.flags.isMutable) this += "var "
636-
else if (printedPrefix || !vdef.flags.isCaseAcessor) this += "val "
635+
if (vdef.symbol.flags.isMutable) this += "var "
636+
else if (printedPrefix || !vdef.symbol.flags.isCaseAcessor) this += "val "
637637
else this // val not explicitly needed
638638
}
639639
}
@@ -955,7 +955,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
955955
}
956956

957957
def printDefAnnotations(definition: Definition): Buffer = {
958-
val annots = definition.annots.filter {
958+
val annots = definition.symbol.annots.filter {
959959
case Annotation(annot, _) =>
960960
annot.tpe match {
961961
case Type.TypeRef(_, Type.SymRef(sym, _)) if sym.fullName == "scala.annotation.internal" => false
@@ -1059,16 +1059,16 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
10591059
this += sym.name
10601060
case _ => printFullClassName(within)
10611061
}
1062-
if (definition.flags.isProtected) {
1062+
if (definition.symbol.flags.isProtected) {
10631063
this += "protected"
1064-
definition.protectedWithin match {
1064+
definition.symbol.protectedWithin match {
10651065
case Some(within) =>
10661066
inSquare(printWithin(within))
10671067
case _ =>
10681068
}
10691069
prefixWasPrinted = true
10701070
} else {
1071-
definition.privateWithin match {
1071+
definition.symbol.privateWithin match {
10721072
case Some(within) =>
10731073
this += "private"
10741074
inSquare(printWithin(within))

tests/run/tasty-custom-show/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ object Macros {
2222
case IsDefinition(tree @ DefDef(name, _, _, _, _)) =>
2323
buff.append(name)
2424
buff.append("\n")
25-
buff.append(tree.owner.show)
25+
buff.append(tree.symbol.owner.tree.get.show)
2626
buff.append("\n\n")
2727
case IsDefinition(tree @ ValDef(name, _, _)) =>
2828
buff.append(name)
2929
buff.append("\n")
30-
buff.append(tree.owner.show)
30+
buff.append(tree.symbol.owner.tree.get.show)
3131
buff.append("\n\n")
3232
case _ =>
3333
}

tests/run/tasty-extractors-owners/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ object Macros {
1919
case IsDefinition(tree @ DefDef(name, _, _, _, _)) =>
2020
buff.append(name)
2121
buff.append("\n")
22-
buff.append(tree.owner.show)
22+
buff.append(tree.symbol.owner.tree.get.show)
2323
buff.append("\n\n")
2424
case IsDefinition(tree @ ValDef(name, _, _)) =>
2525
buff.append(name)
2626
buff.append("\n")
27-
buff.append(tree.owner.show)
27+
buff.append(tree.symbol.owner.tree.get.show)
2828
buff.append("\n\n")
2929
case _ =>
3030
}

tests/run/tasty-location/quoted_1.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ object Location {
1212
def impl(implicit tasty: Tasty): Expr[Location] = {
1313
import tasty._
1414

15-
def listOwnerNames(definition: Definition, acc: List[String]): List[String] = definition match {
16-
case ValDef(name, _, _) => listOwnerNames(definition.owner, name :: acc)
17-
case DefDef(name, _, _, _, _) => listOwnerNames(definition.owner, name :: acc)
18-
case ClassDef(name, _, _, _, _) => listOwnerNames(definition.owner, name :: acc)
19-
case _ => acc
20-
}
15+
def listOwnerNames(sym: Symbol, acc: List[String]): List[String] =
16+
if (sym == definitions.RootClass || sym == definitions.EmptyPackageClass) acc
17+
else listOwnerNames(sym.owner, sym.name :: acc)
2118

2219
val list = listOwnerNames(rootContext.owner, Nil)
2320
'(new Location(~list.toExpr))

0 commit comments

Comments
 (0)