Skip to content

TASTy reflect use complete definition trees #4968

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 1 commit into from
Oct 2, 2018
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
36 changes: 24 additions & 12 deletions compiler/src/dotty/tools/dotc/tastyreflect/FromSymbol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dotty.tools.dotc.core.Types._
object FromSymbol {

def definitionFromSym(sym: Symbol)(implicit ctx: Context): tpd.Tree = {
assert(sym.exists)
if (sym.is(Package)) packageDefFromSym(sym)
else if (sym.isClass) classDef(sym.asClass)
else if (sym.isType) typeDefFromSym(sym.asType)
Expand All @@ -19,21 +20,32 @@ object FromSymbol {

def packageDefFromSym(sym: Symbol)(implicit ctx: Context): PackageDefinition = PackageDefinitionImpl(sym)

def classDef(cls: ClassSymbol)(implicit ctx: Context): tpd.TypeDef = {
val constrSym = cls.unforcedDecls.find(_.isPrimaryConstructor).orElse(
// Dummy constructor for classes such as `<refinement>`
ctx.newSymbol(cls, nme.CONSTRUCTOR, EmptyFlags, NoType)
)
val constr = tpd.DefDef(constrSym.asTerm)
val parents = cls.classParents.map(tpd.TypeTree(_))
val body = cls.unforcedDecls.filter(!_.isPrimaryConstructor).map(s => definitionFromSym(s))
tpd.ClassDefWithParents(cls, constr, parents, body)
def classDef(cls: ClassSymbol)(implicit ctx: Context): tpd.TypeDef = cls.defTree match {
case tree: tpd.TypeDef => tree
case tpd.EmptyTree =>
val constrSym = cls.unforcedDecls.find(_.isPrimaryConstructor).orElse(
// Dummy constructor for classes such as `<refinement>`
ctx.newSymbol(cls, nme.CONSTRUCTOR, EmptyFlags, NoType)
)
val constr = tpd.DefDef(constrSym.asTerm)
val parents = cls.classParents.map(tpd.TypeTree(_))
val body = cls.unforcedDecls.filter(!_.isPrimaryConstructor).map(s => definitionFromSym(s))
tpd.ClassDefWithParents(cls, constr, parents, body)
}

def typeDefFromSym(sym: TypeSymbol)(implicit ctx: Context): tpd.TypeDef = tpd.TypeDef(sym)
def typeDefFromSym(sym: TypeSymbol)(implicit ctx: Context): tpd.TypeDef = sym.defTree match {
case tree: tpd.TypeDef => tree
case tpd.EmptyTree => tpd.TypeDef(sym)
}

def defDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.DefDef = tpd.DefDef(sym)
def defDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.DefDef = sym.defTree match {
case tree: tpd.DefDef => tree
case tpd.EmptyTree => tpd.DefDef(sym)
}

def valDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.ValDef = tpd.ValDef(sym)
def valDefFromSym(sym: TermSymbol)(implicit ctx: Context): tpd.ValDef = sym.defTree match {
case tree: tpd.ValDef => tree
case tpd.EmptyTree => tpd.ValDef(sym)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
}

object Inlined extends InlinedExtractor {
def unapply(x: Term)(implicit ctx: Context): Option[(Option[Term], List[Statement], Term)] = x match {
def unapply(x: Term)(implicit ctx: Context): Option[(Option[Parent], List[Statement], Term)] = x match {
case x: tpd.Inlined =>
Some((optional(x.call), x.bindings, x.expansion))
case _ => None
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class CompilationTests extends ParallelTesting {

@Test def runAll: Unit = {
implicit val testGroup: TestGroup = TestGroup("runAll")
compileFilesInDir("tests/run-custom-args/Yretain-trees", defaultOptions and "-Yretain-trees") +
compileFilesInDir("tests/run", defaultOptions)
}.checkRuns()

Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ trait TreeOps extends TastyCore {

val Inlined: InlinedExtractor
abstract class InlinedExtractor {
def unapply(tree: Tree)(implicit ctx: Context): Option[(Option[Term], List[Definition], Term)]
def unapply(tree: Tree)(implicit ctx: Context): Option[(Option[Parent], List[Definition], Term)]
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, Parent is the type of the tree in the parent list. Using here, it's a little difficult to understand.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I will change Parent to TermOrTypeTree in another PR. When bootstrapped it will be Term | TypeTree.

}

val SelectOuter: SelectOuterExtractor
Expand Down
14 changes: 9 additions & 5 deletions library/src/scala/tasty/util/ShowExtractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
case Term.Repeated(elems) =>
this += "Term.Repeated(" ++= elems += ")"
case Term.Inlined(call, bindings, expansion) =>
this += "Term.Inlined(" += call += ", " ++= bindings += ", " += expansion += ")"
this += "Term.Inlined("
visitOption(call, visitParent)
this += ", " ++= bindings += ", " += expansion += ")"
case ValDef(name, tpt, rhs) =>
this += "ValDef(\"" += name += "\", " += tpt += ", " += rhs += ")"
case DefDef(name, typeParams, paramss, returnTpt, rhs) =>
Expand All @@ -78,10 +80,7 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "TypeDef(\"" += name += "\", " += rhs += ")"
case ClassDef(name, constr, parents, self, body) =>
this += "ClassDef(\"" += name += "\", " += constr += ", "
visitList[Parent](parents, {
case IsTerm(parent) => this += parent
case IsTypeTree(parent) => this += parent
})
visitList[Parent](parents, visitParent)
this += ", " += self += ", " ++= body += ")"
case PackageDef(name, owner) =>
this += "PackageDef(\"" += name += "\", " += owner += ")"
Expand Down Expand Up @@ -142,6 +141,11 @@ class ShowExtractors[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += "Pattern.TypeTest(" += tpt += ")"
}

def visitParent(x: Parent): Buffer = x match {
case IsTerm(parent) => this += parent
case IsTypeTree(parent) => this += parent
}

def visitConstant(x: Constant): Buffer = x match {
case Constant.Unit() => this += "Constant.Unit()"
case Constant.Null() => this += "Constant.Null()"
Expand Down
2 changes: 2 additions & 0 deletions tests/run-custom-args/Yretain-trees/tasty-definitions-2.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(1)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(2))))))
ValDef("bar", TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(2)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(3))))))
2 changes: 2 additions & 0 deletions tests/run-custom-args/Yretain-trees/tasty-definitions-3.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(1)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(2))))))
ValDef("bar", TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(2)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(3))))))
27 changes: 27 additions & 0 deletions tests/run-custom-args/Yretain-trees/tasty-extractors-owners.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
foo
DefDef("main", Nil, List(List(ValDef("args", TypeTree.Applied(TypeTree.TypeIdent("Array"), List(TypeTree.TypeIdent("String"))), None))), TypeTree.TypeIdent("Unit"), Some(Term.Block(Nil, Term.Inlined(Some(TypeTree.TypeIdent("Macros$")), Nil, Term.Select(Term.Apply(Term.Apply(Term.TypeApply(Term.Ident("impl"), List(TypeTree.Synthetic())), List(Term.Apply(Term.TypeApply(Term.Ident("apply"), List(TypeTree.Synthetic())), List(Term.Inlined(None, Nil, Term.Block(List(DefDef("foo", Nil, Nil, TypeTree.Synthetic(), Some(Term.Block(List(DefDef("bar", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(1)))), ValDef("bar2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(2))))), Term.Typed(Term.Ident("bar"), TypeTree.Synthetic())))), ValDef("foo2", TypeTree.Synthetic(), Some(Term.Block(List(DefDef("baz", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(3)))), ValDef("baz2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(4))))), Term.Typed(Term.Ident("baz"), TypeTree.Synthetic())))), ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.TypeIdent("Int")), DefDef("b", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(5)))), ValDef("b2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(6))))))), Term.Literal(Constant.Unit()))))))), List(Term.Ident("macroContext"))), "unary_~", Some(Signature(Nil, java.lang.Object)))))))

bar
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), Some(Term.Block(List(DefDef("bar", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(1)))), ValDef("bar2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(2))))), Term.Typed(Term.Ident("bar"), TypeTree.Synthetic()))))

bar2
DefDef("foo", Nil, Nil, TypeTree.Synthetic(), Some(Term.Block(List(DefDef("bar", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(1)))), ValDef("bar2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(2))))), Term.Typed(Term.Ident("bar"), TypeTree.Synthetic()))))

foo2
DefDef("main", Nil, List(List(ValDef("args", TypeTree.Applied(TypeTree.TypeIdent("Array"), List(TypeTree.TypeIdent("String"))), None))), TypeTree.TypeIdent("Unit"), Some(Term.Block(Nil, Term.Inlined(Some(TypeTree.TypeIdent("Macros$")), Nil, Term.Select(Term.Apply(Term.Apply(Term.TypeApply(Term.Ident("impl"), List(TypeTree.Synthetic())), List(Term.Apply(Term.TypeApply(Term.Ident("apply"), List(TypeTree.Synthetic())), List(Term.Inlined(None, Nil, Term.Block(List(DefDef("foo", Nil, Nil, TypeTree.Synthetic(), Some(Term.Block(List(DefDef("bar", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(1)))), ValDef("bar2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(2))))), Term.Typed(Term.Ident("bar"), TypeTree.Synthetic())))), ValDef("foo2", TypeTree.Synthetic(), Some(Term.Block(List(DefDef("baz", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(3)))), ValDef("baz2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(4))))), Term.Typed(Term.Ident("baz"), TypeTree.Synthetic())))), ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.TypeIdent("Int")), DefDef("b", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(5)))), ValDef("b2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(6))))))), Term.Literal(Constant.Unit()))))))), List(Term.Ident("macroContext"))), "unary_~", Some(Signature(Nil, java.lang.Object)))))))

baz
ValDef("foo2", TypeTree.Synthetic(), Some(Term.Block(List(DefDef("baz", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(3)))), ValDef("baz2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(4))))), Term.Typed(Term.Ident("baz"), TypeTree.Synthetic()))))

baz2
ValDef("foo2", TypeTree.Synthetic(), Some(Term.Block(List(DefDef("baz", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(3)))), ValDef("baz2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(4))))), Term.Typed(Term.Ident("baz"), TypeTree.Synthetic()))))

<init>
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.TypeIdent("Int")), DefDef("b", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(5)))), ValDef("b2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(6))))))

b
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.TypeIdent("Int")), DefDef("b", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(5)))), ValDef("b2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(6))))))

b2
ClassDef("A", DefDef("<init>", Nil, List(Nil), TypeTree.Synthetic(), None), List(Term.Apply(Term.Select(Term.New(TypeTree.Synthetic()), "<init>", Some(Signature(Nil, java.lang.Object))), Nil)), None, List(TypeDef("B", TypeTree.TypeIdent("Int")), DefDef("b", Nil, Nil, TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(5)))), ValDef("b2", TypeTree.Synthetic(), Some(Term.Literal(Constant.Int(6))))))

2 changes: 2 additions & 0 deletions tests/run-custom-args/Yretain-trees/tasty-load-tree-1.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(1)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(2))))))
ValDef("bar", TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(2)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(3))))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import scala.quoted._

import scala.tasty._

object Foo {

inline def inspectBody(i: => Int): String =
~inspectBodyImpl('(i))

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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

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-custom-args/Yretain-trees/tasty-load-tree-2.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DefDef("foo", Nil, Nil, TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(1)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(2))))))
ValDef("bar", TypeTree.TypeIdent("Int"), Some(Term.Apply(Term.Select(Term.Literal(Constant.Int(2)), "+", Some(Signature(List(scala.Int), scala.Int))), List(Term.Literal(Constant.Int(3))))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.quoted._

import scala.tasty._

object Foo {

inline def inspectBody(i: => Int): String =
~inspectBodyImpl('(i))

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
}
}
}
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
}
2 changes: 0 additions & 2 deletions tests/run/tasty-definitions-2.check

This file was deleted.

2 changes: 0 additions & 2 deletions tests/run/tasty-definitions-3.check

This file was deleted.

27 changes: 0 additions & 27 deletions tests/run/tasty-extractors-owners.check

This file was deleted.