Skip to content

Commit 2dbec65

Browse files
oderskyallanrenucci
authored andcommitted
Add underlyingArgument to Tasty reflection.
1 parent a5db091 commit 2dbec65

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
916916
def outerSelect(levels: Int, tp: Type)(implicit ctx: Context): Tree =
917917
untpd.Select(tree, OuterSelectName(EmptyTermName, levels)).withType(SkolemType(tp))
918918

919+
def underlyingArgument(implicit ctx: Context): Tree = mapToUnderlying.transform(tree)
920+
919921
// --- Higher order traversal methods -------------------------------
920922

921923
/** Apply `f` to each subtree of this tree */
@@ -942,6 +944,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
942944
}
943945
}
944946

947+
/** Map Inlined nodes and InlineProxy references to underlying arguments */
948+
object mapToUnderlying extends TreeMap {
949+
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
950+
case tree: Ident if tree.symbol.is(InlineProxy) =>
951+
tree.symbol.defTree.asInstanceOf[ValOrDefDef].rhs.underlyingArgument
952+
case Inlined(_, _, arg) =>
953+
arg.underlyingArgument
954+
case tree =>
955+
super.transform(tree)
956+
}
957+
}
958+
945959
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {
946960
def tpes: List[Type] = xs map (_.tpe)
947961
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with TastyCoreImpl with He
184184
def TermDeco(term: Term): TermAPI = new TermAPI {
185185
def pos(implicit ctx: Context): Position = term.pos
186186
def tpe(implicit ctx: Context): Types.Type = term.tpe
187+
def underlyingArgument(implicit ctx: Context): Term = {
188+
import tpd._
189+
term.underlyingArgument
190+
}
187191
}
188192

189193
object IsTerm extends IsTermExtractor {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ trait TreeOps extends TastyCore {
147147
trait TermAPI {
148148
def tpe(implicit ctx: Context): Type
149149
def pos(implicit ctx: Context): Position
150+
def underlyingArgument(implicit ctx: Context): Term
150151
}
151152
implicit def TermDeco(term: Term): TermAPI
152153

tests/disabled/run/xml-interpolation/XmlQuote_1.scala renamed to tests/run/xml-interpolation/XmlQuote_1.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ case class Xml(parts: String, args: List[Any])
77

88
// Ideally should be an implicit class but the implicit conversion
99
// has to be a inline method
10-
class XmlQuote(ctx: => StringContext) {
11-
inline def xml(args: => Any*): Xml = ~XmlQuote.impl('(ctx), '(args))
10+
class XmlQuote(val ctx: StringContext) {
11+
inline def xml(args: => Any*): Xml = ~XmlQuote.impl('(this), '(args))
1212
}
1313

1414
object XmlQuote {
15-
implicit inline def XmlQuote(ctx: => StringContext): XmlQuote = new XmlQuote(ctx)
15+
implicit inline def XmlQuote(ctx: StringContext): XmlQuote = new XmlQuote(ctx)
1616

17-
def impl(ctx: Expr[StringContext], args: Expr[Seq[Any]])
17+
def impl(receiver: Expr[XmlQuote], args: Expr[Seq[Any]])
1818
(implicit tasty: Tasty): Expr[Xml] = {
1919
import tasty._
2020
import Term._
@@ -42,18 +42,20 @@ object XmlQuote {
4242
}
4343

4444
// _root_.scala.StringContext.apply([p0, ...]: String*)
45-
val parts = ctx.toTasty match {
46-
case Inlined(_, _,
47-
Apply(
48-
Select(Select(Select(Ident("_root_"), "scala", _), "StringContext", _), "apply", _),
49-
List(Typed(Repeated(values), _)))) if values.forall(isStringConstant) =>
45+
val parts = receiver.toTasty.underlyingArgument match {
46+
case Apply(
47+
Select(New(_), _, _),
48+
List(
49+
Apply(
50+
Select(Select(Select(Ident("_root_"), "scala", _), "StringContext", _), "apply", _),
51+
List(Typed(Repeated(values), _))))) if values.forall(isStringConstant) =>
5052
values.collect { case Literal(Constant.String(value)) => value }
5153
case tree =>
5254
abort(s"String literal expected, but $tree found")
5355
}
5456

5557
// [a0, ...]: Any*
56-
val Inlined(_, _, Typed(Repeated(args0), _)) = args.toTasty
58+
val Typed(Repeated(args0), _) = args.toTasty.underlyingArgument
5759

5860
val string = parts.mkString("??")
5961
'(new Xml(~string.toExpr, ~liftListOfAny(args0)))

0 commit comments

Comments
 (0)