Skip to content

Commit a5d1802

Browse files
committed
Add underlyingArgument to Tasty reflection.
1 parent 010fb42 commit a5d1802

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
@@ -910,6 +910,8 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
910910
def outerSelect(levels: Int, tp: Type)(implicit ctx: Context): Tree =
911911
untpd.Select(tree, OuterSelectName(EmptyTermName, levels)).withType(SkolemType(tp))
912912

913+
def underlyingArgument(implicit ctx: Context): Tree = mapToUnderlying.transform(tree)
914+
913915
// --- Higher order traversal methods -------------------------------
914916

915917
/** Apply `f` to each subtree of this tree */
@@ -936,6 +938,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
936938
}
937939
}
938940

941+
/** Map Inlined nodes and InlineProxy references to underlying arguments */
942+
object mapToUnderlying extends TreeMap {
943+
override def transform(tree: Tree)(implicit ctx: Context): Tree = tree match {
944+
case tree: Ident if tree.symbol.is(InlineProxy) =>
945+
tree.symbol.defTree.asInstanceOf[ValOrDefDef].rhs.underlyingArgument
946+
case Inlined(_, _, arg) =>
947+
arg.underlyingArgument
948+
case tree =>
949+
super.transform(tree)
950+
}
951+
}
952+
939953
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {
940954
def tpes: List[Type] = xs map (_.tpe)
941955
}

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)