Skip to content

Commit e612d55

Browse files
Merge pull request #4976 from dotty-staging/add-symbols-to-tasty-reflect
Add back symbols to TASTy reflect
2 parents 3a01d5b + c5399da commit e612d55

File tree

19 files changed

+144
-8
lines changed

19 files changed

+144
-8
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dotty.tools.dotc.tastyreflect
2+
3+
import dotty.tools.dotc.core.Symbols._
4+
5+
trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with TastyCoreImpl {
6+
7+
def SymbolDeco(symbol: Symbol): SymbolAPI = new SymbolAPI {
8+
def isEmpty: Boolean = symbol eq NoSymbol
9+
def localContext(implicit ctx: Context): Context = ctx.withOwner(symbol)
10+
def tree(implicit ctx: Context): Option[Definition] =
11+
if (isEmpty) None else Some(FromSymbol.definitionFromSym(symbol))
12+
}
13+
14+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,7 @@ trait TastyCoreImpl extends scala.tasty.reflect.TastyCore {
5050
type Position = util.SourcePosition
5151

5252
type Constant = Constants.Constant
53-
53+
54+
type Symbol = core.Symbols.Symbol
55+
5456
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,21 @@ package dotty.tools.dotc.tastyreflect
22

33
import dotty.tools.dotc.core._
44

5-
class TastyImpl(val rootContext: Contexts.Context) extends scala.tasty.Tasty with TastyCoreImpl with CaseDefOpsImpl with ConstantOpsImpl with ContextOpsImpl with IdOpsImpl with ImportSelectorOpsImpl with QuotedOpsImpl with PatternOpsImpl with PositionOpsImpl with PrintersImpl with SignatureOpsImpl with StandardDefinitions with TreeOpsImpl with TypeOrBoundsTreesOpsImpl with TypeOrBoundsOpsImpl
5+
class TastyImpl(val rootContext: Contexts.Context)
6+
extends scala.tasty.Tasty
7+
with TastyCoreImpl
8+
with CaseDefOpsImpl
9+
with ConstantOpsImpl
10+
with ContextOpsImpl
11+
with IdOpsImpl
12+
with ImportSelectorOpsImpl
13+
with QuotedOpsImpl
14+
with PatternOpsImpl
15+
with PositionOpsImpl
16+
with PrintersImpl
17+
with SignatureOpsImpl
18+
with StandardDefinitions
19+
with SymbolOpsImpl
20+
with TreeOpsImpl
21+
with TypeOrBoundsTreesOpsImpl
22+
with TypeOrBoundsOpsImpl

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
1313

1414
def TreeDeco(tree: Tree): TreeAPI = new TreeAPI {
1515
def pos(implicit ctx: Context): Position = tree.pos
16+
def symbol(implicit ctx: Context): Symbol = tree.symbol
17+
1618
}
1719

1820
object IsPackageClause extends IsPackageClauseExtractor {
@@ -30,7 +32,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
3032
}
3133

3234
def PackageClauseDeco(pack: PackageClause): PackageClauseAPI = new PackageClauseAPI {
33-
def definition(implicit ctx: Context): Definition = packageDefFromSym(pack.symbol)
35+
3436
}
3537

3638
// ----- Statements -----------------------------------------------

library/src/scala/tasty/Tasty.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,20 @@ package scala.tasty
22

33
import scala.tasty.reflect._
44

5-
abstract class Tasty extends TastyCore with CaseDefOps with ConstantOps with ContextOps with IdOps with ImportSelectorOps with QuotedOps with PatternOps with PositionOps with Printers with SignatureOps with StandardDefinitions with TreeOps with TypeOrBoundsTreeOps with TypeOrBoundsOps
5+
abstract class Tasty
6+
extends TastyCore
7+
with CaseDefOps
8+
with ConstantOps
9+
with ContextOps
10+
with IdOps
11+
with ImportSelectorOps
12+
with QuotedOps
13+
with PatternOps
14+
with PositionOps
15+
with Printers
16+
with SignatureOps
17+
with StandardDefinitions
18+
with SymbolOps
19+
with TreeOps
20+
with TypeOrBoundsTreeOps
21+
with TypeOrBoundsOps
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package scala.tasty
2+
package reflect
3+
4+
/** Tasty reflect symbol */
5+
trait SymbolOps extends TastyCore {
6+
7+
trait SymbolAPI {
8+
def isEmpty: Boolean
9+
def localContext(implicit ctx: Context): Context
10+
def tree(implicit ctx: Context): Option[Definition]
11+
}
12+
implicit def SymbolDeco(symbol: Symbol): SymbolAPI
13+
14+
}

library/src/scala/tasty/reflect/TastyCore.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ package scala.tasty.reflect
9696
*
9797
* +- Constant
9898
*
99+
* +- Symbol
100+
*
99101
* ```
100102
*/
101103
trait TastyCore {
@@ -158,4 +160,9 @@ trait TastyCore {
158160
/** Constant value represented as the constant itself */
159161
type Constant
160162

163+
/** Symbol of a definition.
164+
* Then can be compared with == to know if the definition is the same.
165+
*/
166+
type Symbol
167+
161168
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ trait TreeOps extends TastyCore {
55

66
trait TreeAPI {
77
def pos(implicit ctx: Context): Position
8+
def symbol(implicit ctx: Context): Symbol
89
}
910
implicit def TreeDeco(tree: Tree): TreeAPI
1011

@@ -19,7 +20,7 @@ trait TreeOps extends TastyCore {
1920
}
2021

2122
trait PackageClauseAPI {
22-
def definition(implicit ctx: Context): Definition
23+
2324
}
2425
implicit def PackageClauseDeco(pack: PackageClause): PackageClauseAPI
2526

library/src/scala/tasty/util/TreeAccumulator.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
1818
private def foldParents(x: X, trees: Iterable[Parent])(implicit ctx: Context): X = (x /: trees)(foldOverParent)
1919

2020
def foldOverTree(x: X, tree: Tree)(implicit ctx: Context): X = {
21-
def localCtx(definition: Definition): Context = definition.localContext
21+
def localCtx(definition: Definition): Context = definition.symbol.localContext
2222
tree match {
2323
case Term.Ident(_) =>
2424
x
@@ -74,7 +74,7 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
7474
case Import(expr, selectors) =>
7575
foldTree(x, expr)
7676
case IsPackageClause(clause @ PackageClause(pid, stats)) =>
77-
foldTrees(foldTree(x, pid), stats)(localCtx(clause.definition))
77+
foldTrees(foldTree(x, pid), stats)(clause.symbol.localContext)
7878
}
7979
}
8080

project/scripts/cmdTests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ clear_out "$OUT"
5050
grep -qe "def main(args: scala.Array\[scala.Predef.String\]): scala.Unit =" "$tmp"
5151

5252
echo "testing scala.quoted.Expr.run from sbt dotr"
53-
"$SBT" ";dotty-compiler-bootstrapped/dotc -with-compiler tests/run-with-compiler/quote-run.scala; dotty-compiler-bootstrapped/dotr -with-compiler Test" > "$tmp"
53+
"$SBT" ";dotty-compiler-bootstrapped/dotc tests/run-with-compiler/quote-run.scala; dotty-compiler-bootstrapped/dotr -with-compiler Test" > "$tmp"
5454
grep -qe "val a: scala.Int = 3" "$tmp"
5555

5656

tests/run/tasty-definitions-2.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), None)
2+
ValDef("bar", TypeTree.Synthetic(), None)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object Foo {
5+
6+
transparent def inspectBody(i: => Int): String =
7+
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
9+
def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
10+
import tasty._
11+
def definitionString(tree: Tree): Expr[String] = tree.symbol.tree match {
12+
case Some(definition) => definition.show.toExpr
13+
case None => '("NO DEFINTION")
14+
}
15+
x.toTasty match {
16+
case Term.Inlined(None, Nil, arg) => definitionString(arg)
17+
case arg => definitionString(arg) // TODO should all by name parameters be in an inline node?
18+
}
19+
}
20+
21+
def foo: Int = 1 + 2
22+
val bar: Int = 2 + 3
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
println(Foo.inspectBody(Foo.foo))
4+
println(Foo.inspectBody(Foo.bar))
5+
}
6+
}

tests/run/tasty-definitions-3.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), None)
2+
ValDef("bar", TypeTree.Synthetic(), None)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object Foo {
5+
6+
transparent def inspectBody(i: => Int): String =
7+
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~
8+
9+
def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
10+
import tasty._
11+
def definitionString(tree: Tree): Expr[String] = tree.symbol.tree match {
12+
case Some(definition) => definition.show.toExpr
13+
case None => '("NO DEFINTION")
14+
}
15+
x.toTasty match {
16+
case Term.Inlined(None, Nil, arg) => definitionString(arg)
17+
case arg => definitionString(arg) // TODO should all by name parameters be in an inline node?
18+
}
19+
}
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(Foo.inspectBody(foo))
5+
println(Foo.inspectBody(bar))
6+
}
7+
8+
def foo: Int = 1 + 2
9+
val bar: Int = 2 + 3
10+
}

0 commit comments

Comments
 (0)