Skip to content

Add back symbols to TASTy reflect #4976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/SymbolOpsImpl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dotty.tools.dotc.tastyreflect

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

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

def SymbolDeco(symbol: Symbol): SymbolAPI = new SymbolAPI {
def isEmpty: Boolean = symbol eq NoSymbol
def localContext(implicit ctx: Context): Context = ctx.withOwner(symbol)
def tree(implicit ctx: Context): Option[Definition] =
if (isEmpty) None else Some(FromSymbol.definitionFromSym(symbol))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ trait TastyCoreImpl extends scala.tasty.reflect.TastyCore {
type Position = util.SourcePosition

type Constant = Constants.Constant


type Symbol = core.Symbols.Symbol

}
19 changes: 18 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/TastyImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@ package dotty.tools.dotc.tastyreflect

import dotty.tools.dotc.core._

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format the following line into several lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
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 SymbolOpsImpl
with TreeOpsImpl
with TypeOrBoundsTreesOpsImpl
with TypeOrBoundsOpsImpl
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He

def TreeDeco(tree: Tree): TreeAPI = new TreeAPI {
def pos(implicit ctx: Context): Position = tree.pos
def symbol(implicit ctx: Context): Symbol = tree.symbol

}

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

def PackageClauseDeco(pack: PackageClause): PackageClauseAPI = new PackageClauseAPI {
def definition(implicit ctx: Context): Definition = packageDefFromSym(pack.symbol)

}

// ----- Statements -----------------------------------------------
Expand Down
18 changes: 17 additions & 1 deletion library/src/scala/tasty/Tasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,20 @@ package scala.tasty

import scala.tasty.reflect._

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformat the following line to be shorter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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
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 SymbolOps
with TreeOps
with TypeOrBoundsTreeOps
with TypeOrBoundsOps
14 changes: 14 additions & 0 deletions library/src/scala/tasty/reflect/SymbolOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package scala.tasty
package reflect

/** Tasty reflect symbol */
trait SymbolOps extends TastyCore {

trait SymbolAPI {
def isEmpty: Boolean
def localContext(implicit ctx: Context): Context
def tree(implicit ctx: Context): Option[Definition]
}
implicit def SymbolDeco(symbol: Symbol): SymbolAPI

}
7 changes: 7 additions & 0 deletions library/src/scala/tasty/reflect/TastyCore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ package scala.tasty.reflect
*
* +- Constant
*
* +- Symbol
*
* ```
*/
trait TastyCore {
Expand Down Expand Up @@ -158,4 +160,9 @@ trait TastyCore {
/** Constant value represented as the constant itself */
type Constant

/** Symbol of a definition.
* Then can be compared with == to know if the definition is the same.
*/
type Symbol

}
3 changes: 2 additions & 1 deletion library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ trait TreeOps extends TastyCore {

trait TreeAPI {
def pos(implicit ctx: Context): Position
def symbol(implicit ctx: Context): Symbol
}
implicit def TreeDeco(tree: Tree): TreeAPI

Expand All @@ -19,7 +20,7 @@ trait TreeOps extends TastyCore {
}

trait PackageClauseAPI {
def definition(implicit ctx: Context): Definition

}
implicit def PackageClauseDeco(pack: PackageClause): PackageClauseAPI

Expand Down
4 changes: 2 additions & 2 deletions library/src/scala/tasty/util/TreeAccumulator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
private def foldParents(x: X, trees: Iterable[Parent])(implicit ctx: Context): X = (x /: trees)(foldOverParent)

def foldOverTree(x: X, tree: Tree)(implicit ctx: Context): X = {
def localCtx(definition: Definition): Context = definition.localContext
def localCtx(definition: Definition): Context = definition.symbol.localContext
tree match {
case Term.Ident(_) =>
x
Expand Down Expand Up @@ -74,7 +74,7 @@ abstract class TreeAccumulator[X, T <: Tasty with Singleton](val tasty: T) {
case Import(expr, selectors) =>
foldTree(x, expr)
case IsPackageClause(clause @ PackageClause(pid, stats)) =>
foldTrees(foldTree(x, pid), stats)(localCtx(clause.definition))
foldTrees(foldTree(x, pid), stats)(clause.symbol.localContext)
}
}

Expand Down
2 changes: 1 addition & 1 deletion project/scripts/cmdTests
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ clear_out "$OUT"
grep -qe "def main(args: scala.Array\[scala.Predef.String\]): scala.Unit =" "$tmp"

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


Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/run/tasty-definitions-2.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), None)
ValDef("bar", TypeTree.Synthetic(), None)
23 changes: 23 additions & 0 deletions tests/run/tasty-definitions-2/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.quoted._
import scala.tasty._

object Foo {

transparent def inspectBody(i: => Int): String =
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~

def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
import tasty._
def definitionString(tree: Tree): Expr[String] = tree.symbol.tree match {
case Some(definition) => definition.show.toExpr
case None => '("NO DEFINTION")
}
x.toTasty match {
case Term.Inlined(None, Nil, arg) => definitionString(arg)
case arg => definitionString(arg) // TODO should all by name parameters be in an inline node?
}
}

def foo: Int = 1 + 2
val bar: Int = 2 + 3
}
6 changes: 6 additions & 0 deletions tests/run/tasty-definitions-2/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
println(Foo.inspectBody(Foo.foo))
println(Foo.inspectBody(Foo.bar))
}
}
2 changes: 2 additions & 0 deletions tests/run/tasty-definitions-3.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), None)
ValDef("bar", TypeTree.Synthetic(), None)
20 changes: 20 additions & 0 deletions tests/run/tasty-definitions-3/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.quoted._
import scala.tasty._

object Foo {

transparent def inspectBody(i: => Int): String =
~inspectBodyImpl('(i))(TopLevelSplice.tastyContext) // FIXME infer TopLevelSplice.tastyContext within top level ~

def inspectBodyImpl(x: Expr[Int])(implicit tasty: Tasty): Expr[String] = {
import tasty._
def definitionString(tree: Tree): Expr[String] = tree.symbol.tree match {
case Some(definition) => definition.show.toExpr
case None => '("NO DEFINTION")
}
x.toTasty match {
case Term.Inlined(None, Nil, arg) => definitionString(arg)
case arg => definitionString(arg) // TODO should all by name parameters be in an inline node?
}
}
}
10 changes: 10 additions & 0 deletions tests/run/tasty-definitions-3/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test {

def main(args: Array[String]): Unit = {
println(Foo.inspectBody(foo))
println(Foo.inspectBody(bar))
}

def foo: Int = 1 + 2
val bar: Int = 2 + 3
}