Skip to content

Commit 990e3d1

Browse files
committed
Implement extractor for quoted trees
1 parent 2bd3b6f commit 990e3d1

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import dotty.tools.dotc.core.Contexts._
88
import dotty.tools.dotc.core.Flags._
99
import dotty.tools.dotc.core.Decorators._
1010
import dotty.tools.dotc.core.Symbols._
11-
11+
import dotty.tools.dotc.quoted.Quoted
1212
import scala.reflect.ClassTag
1313
import java.net.URLClassLoader
1414

@@ -53,13 +53,9 @@ class Interpreter(implicit ctx: Context) {
5353
private def interpretTreeImpl(tree: Tree): Object = {
5454
try {
5555
tree match {
56-
case Apply(_, quote :: Nil) if tree.symbol eq defn.quoteMethod =>
57-
RawQuoted(quote)
58-
case TypeApply(_, quote :: Nil) if tree.symbol eq defn.typeQuoteMethod =>
59-
RawQuoted(quote)
56+
case Quoted(quotedTree) => RawQuoted(quotedTree)
6057

61-
case Literal(Constant(c)) =>
62-
c.asInstanceOf[AnyRef]
58+
case Literal(Constant(c)) => c.asInstanceOf[AnyRef]
6359

6460
case Apply(fn, args) if fn.symbol.isConstructor =>
6561
val cls = fn.symbol.owner
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools.dotc.quoted
2+
3+
import dotty.tools.dotc.ast.Trees.GenericApply
4+
import dotty.tools.dotc.ast.tpd
5+
import dotty.tools.dotc.core.Contexts.Context
6+
import dotty.tools.dotc.core.Types.Type
7+
import dotty.tools.dotc.transform.SymUtils._
8+
9+
/** Extractors for quotes */
10+
object Quoted {
11+
12+
/** Extracts the content of a quoted tree.
13+
* The result can be the contents of a term ot type quote, which
14+
* will return a term or type tree respectively.
15+
*/
16+
def unapply(tree: tpd.Tree)(implicit ctx: Context): Option[tpd.Tree] = tree match {
17+
case tree: GenericApply[Type] if tree.symbol.isQuote => Some(tree.args.head)
18+
case _ => None
19+
}
20+
}

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import NameKinds.OuterSelectName
1717
import scala.collection.mutable
1818
import dotty.tools.dotc.core.StdNames._
1919
import dotty.tools.dotc.quoted.PickledQuotes
20+
import dotty.tools.dotc.quoted.Quoted
2021

2122

2223
/** Translates quoted terms and types to `unpickle` method calls.
@@ -316,10 +317,8 @@ class ReifyQuotes extends MacroTransform {
316317
enteredSyms = enteredSyms.tail
317318
}
318319
tree match {
319-
case Apply(fn, arg :: Nil) if fn.symbol == defn.quoteMethod =>
320-
quotation(arg, tree)
321-
case TypeApply(fn, arg :: Nil) if fn.symbol == defn.typeQuoteMethod =>
322-
quotation(arg, tree)
320+
case Quoted(quotedTree) =>
321+
quotation(quotedTree, tree)
323322
case Select(body, _) if tree.symbol.isSplice =>
324323
splice(body, tree)
325324
case Block(stats, _) =>

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.ast.Trees._
77
import dotty.tools.dotc.ast.tpd
88
import dotty.tools.dotc.interpreter._
99
import dotty.tools.dotc.quoted.PickledQuotes
10+
import dotty.tools.dotc.quoted.Quoted
1011

1112
import scala.quoted
1213

@@ -19,8 +20,7 @@ object Splicer {
1920
* resulting expression is return as a `Tree`
2021
*/
2122
def splice(tree: Tree)(implicit ctx: Context): Tree = tree match {
22-
case Apply(quote, quoted :: Nil) if quote.symbol == defn.quoteMethod => quoted
23-
case TypeApply(quote, quoted :: Nil) if quote.symbol == defn.typeQuoteMethod => quoted
23+
case Quoted(quotedTree) => quotedTree
2424
case tree: RefTree => reflectiveSplice(tree)
2525
case tree: Apply => reflectiveSplice(tree)
2626
case tree: Inlined => reflectiveSplice(tree)

0 commit comments

Comments
 (0)