Skip to content

Commit 7cd3070

Browse files
committed
Simplify CompilerInterface abstraction mixin
1 parent 70bcb28 commit 7cd3070

File tree

11 files changed

+73
-83
lines changed

11 files changed

+73
-83
lines changed

compiler/src/dotty/tools/dotc/quoted/Matcher.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import scala.annotation.internal.sharable
44
import scala.annotation.{Annotation, compileTimeOnly}
55

66
import scala.quoted._
7-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
87

98
/** Matches a quoted tree against a quoted pattern tree.
109
* A quoted pattern tree may have type and term holes in addition to normal terms.
@@ -98,7 +97,7 @@ import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
9897
*/
9998
object Matcher {
10099

101-
abstract class QuoteMatcher[QCtx <: QuoteContext { val reflect: scala.internal.tasty.CompilerInterface } & Singleton](val qctx: QCtx) {
100+
abstract class QuoteMatcher[QCtx <: QuoteContext & scala.internal.quoted.CompilerInterface & Singleton](val qctx: QCtx) {
102101

103102
// TODO improve performance
104103

compiler/src/dotty/tools/dotc/quoted/QuoteContextImpl.scala

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ object QuoteContextImpl {
4545

4646
}
4747

48-
class QuoteContextImpl private (ctx: Context) extends QuoteContext:
48+
class QuoteContextImpl private (ctx: Context) extends QuoteContext, scala.internal.quoted.CompilerInterface:
4949

50-
object reflect extends scala.tasty.Reflection, scala.internal.tasty.CompilerInterface:
50+
object reflect extends scala.tasty.Reflection:
5151

5252
def rootContext: Context = ctx
5353

@@ -2615,70 +2615,72 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
26152615
private def withDefaultPos[T <: Tree](fn: Context ?=> T): T =
26162616
fn(using ctx.withSource(Position.ofMacroExpansion.source)).withSpan(Position.ofMacroExpansion.span)
26172617

2618-
def unpickleTerm(pickledQuote: PickledQuote): Term =
2619-
PickledQuotes.unpickleTerm(pickledQuote)
2620-
2621-
def unpickleTypeTree(pickledQuote: PickledQuote): TypeTree =
2622-
PickledQuotes.unpickleTypeTree(pickledQuote)
2618+
end reflect
26232619

2624-
def termMatch(scrutinee: Term, pattern: Term): Option[Tuple] =
2625-
treeMatch(scrutinee, pattern)
2620+
def unpickleTerm(pickledQuote: PickledQuote): reflect.Term =
2621+
PickledQuotes.unpickleTerm(pickledQuote)(using reflect.rootContext)
26262622

2627-
def typeTreeMatch(scrutinee: TypeTree, pattern: TypeTree): Option[Tuple] =
2628-
treeMatch(scrutinee, pattern)
2623+
def unpickleTypeTree(pickledQuote: PickledQuote): reflect.TypeTree =
2624+
PickledQuotes.unpickleTypeTree(pickledQuote)(using reflect.rootContext)
26292625

2630-
private def treeMatch(scrutinee: Tree, pattern: Tree): Option[Tuple] = {
2631-
def isTypeHoleDef(tree: Tree): Boolean =
2632-
tree match
2633-
case tree: TypeDef =>
2634-
tree.symbol.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_patternTypeAnnot)
2635-
case _ => false
2636-
2637-
def extractTypeHoles(pat: Term): (Term, List[Symbol]) =
2638-
pat match
2639-
case tpd.Inlined(_, Nil, pat2) => extractTypeHoles(pat2)
2640-
case tpd.Block(stats @ ((typeHole: TypeDef) :: _), expr) if isTypeHoleDef(typeHole) =>
2641-
val holes = stats.takeWhile(isTypeHoleDef).map(_.symbol)
2642-
val otherStats = stats.dropWhile(isTypeHoleDef)
2643-
(tpd.cpy.Block(pat)(otherStats, expr), holes)
2644-
case _ =>
2645-
(pat, Nil)
2626+
def termMatch(scrutinee: reflect.Term, pattern: reflect.Term): Option[Tuple] =
2627+
treeMatch(scrutinee, pattern)
26462628

2647-
val (pat1, typeHoles) = extractTypeHoles(pattern)
2629+
def typeTreeMatch(scrutinee: reflect.TypeTree, pattern: reflect.TypeTree): Option[Tuple] =
2630+
treeMatch(scrutinee, pattern)
26482631

2649-
val ctx1 =
2650-
if typeHoles.isEmpty then ctx
2651-
else
2652-
val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(dotc.core.Mode.GadtConstraintInference)
2653-
ctx1.gadt.addToConstraint(typeHoles)
2654-
ctx1
2655-
2656-
val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
2657-
.asInstanceOf[QuoteContext { val reflect: QuoteContextImpl.this.reflect.type }]
2632+
private def treeMatch(scrutinee: reflect.Tree, pattern: reflect.Tree): Option[Tuple] = {
2633+
import reflect._
2634+
given Context = rootContext
2635+
def isTypeHoleDef(tree: Tree): Boolean =
2636+
tree match
2637+
case tree: TypeDef =>
2638+
tree.symbol.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_patternTypeAnnot)
2639+
case _ => false
26582640

2659-
val matcher = new Matcher.QuoteMatcher[qctx1.type](qctx1) {
2660-
def patternHoleSymbol: Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_patternHole
2661-
def higherOrderHoleSymbol: Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_higherOrderHole
2662-
}
2641+
def extractTypeHoles(pat: Term): (Term, List[Symbol]) =
2642+
pat match
2643+
case tpd.Inlined(_, Nil, pat2) => extractTypeHoles(pat2)
2644+
case tpd.Block(stats @ ((typeHole: TypeDef) :: _), expr) if isTypeHoleDef(typeHole) =>
2645+
val holes = stats.takeWhile(isTypeHoleDef).map(_.symbol)
2646+
val otherStats = stats.dropWhile(isTypeHoleDef)
2647+
(tpd.cpy.Block(pat)(otherStats, expr), holes)
2648+
case _ =>
2649+
(pat, Nil)
2650+
2651+
val (pat1, typeHoles) = extractTypeHoles(pattern)
2652+
2653+
val ctx1 =
2654+
if typeHoles.isEmpty then ctx
2655+
else
2656+
val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(dotc.core.Mode.GadtConstraintInference)
2657+
ctx1.gadt.addToConstraint(typeHoles)
2658+
ctx1
2659+
2660+
val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl()(using ctx1)
2661+
.asInstanceOf[QuoteContext & scala.internal.quoted.CompilerInterface]
2662+
2663+
val matcher = new Matcher.QuoteMatcher[qctx1.type](qctx1) {
2664+
def patternHoleSymbol: qctx1.reflect.Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_patternHole.asInstanceOf
2665+
def higherOrderHoleSymbol: qctx1.reflect.Symbol = dotc.core.Symbols.defn.InternalQuotedPatterns_higherOrderHole.asInstanceOf
2666+
}
26632667

2664-
val matchings =
2665-
if pat1.isType then matcher.termMatch(scrutinee, pat1)
2666-
else matcher.termMatch(scrutinee, pat1)
2667-
2668-
// val matchings = matcher.termMatch(scrutinee, pattern)
2669-
if typeHoles.isEmpty then matchings
2670-
else {
2671-
// After matching and doing all subtype checks, we have to approximate all the type bindings
2672-
// that we have found, seal them in a quoted.Type and add them to the result
2673-
def typeHoleApproximation(sym: Symbol) =
2674-
ctx1.gadt.approximation(sym, !sym.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_fromAboveAnnot)).seal
2675-
matchings.map { tup =>
2676-
Tuple.fromIArray(typeHoles.map(typeHoleApproximation).toArray.asInstanceOf[IArray[Object]]) ++ tup
2677-
}
2668+
val matchings =
2669+
if pat1.isType then matcher.termMatch(scrutinee.asInstanceOf[matcher.qctx.reflect.Term], pat1.asInstanceOf[matcher.qctx.reflect.Term])
2670+
else matcher.termMatch(scrutinee.asInstanceOf[matcher.qctx.reflect.Term], pat1.asInstanceOf[matcher.qctx.reflect.Term])
2671+
2672+
// val matchings = matcher.termMatch(scrutinee, pattern)
2673+
if typeHoles.isEmpty then matchings
2674+
else {
2675+
// After matching and doing all subtype checks, we have to approximate all the type bindings
2676+
// that we have found, seal them in a quoted.Type and add them to the result
2677+
def typeHoleApproximation(sym: Symbol) =
2678+
ctx1.gadt.approximation(sym, !sym.hasAnnotation(dotc.core.Symbols.defn.InternalQuotedPatterns_fromAboveAnnot)).asInstanceOf[qctx1.reflect.TypeRepr].seal
2679+
matchings.map { tup =>
2680+
Tuple.fromIArray(typeHoles.map(typeHoleApproximation).toArray.asInstanceOf[IArray[Object]]) ++ tup
26782681
}
26792682
}
2680-
2681-
end reflect
2683+
}
26822684

26832685
private[this] val hash = QuoteContextImpl.scopeId(using ctx)
26842686
override def hashCode: Int = hash

library/src-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.internal.quoted
22

33
import scala.quoted._
4-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
4+
import scala.internal.quoted.CompilerInterface.quoteContextWithCompilerInterface
55

66
/** An Expr backed by a tree. Only the current compiler trees are allowed.
77
*
@@ -54,7 +54,7 @@ object Expr {
5454
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeExpr: scala.quoted.Expr[Any])
5555
(using patternExpr: scala.quoted.Expr[Any], qctx: QuoteContext): Option[Tup] = {
5656
val qctx1 = quoteContextWithCompilerInterface(qctx)
57-
qctx1.reflect.termMatch(scrutineeExpr.unseal, patternExpr.unseal).asInstanceOf[Option[Tup]]
57+
qctx1.termMatch(scrutineeExpr.unseal, patternExpr.unseal).asInstanceOf[Option[Tup]]
5858
}
5959

6060
/** Returns a null expresssion equivalent to `'{null}` */

library/src-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package scala.internal.quoted
22

33
import scala.quoted._
4-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
4+
import scala.internal.quoted.CompilerInterface.quoteContextWithCompilerInterface
55

66
/** Quoted type (or kind) `T` backed by a tree */
77
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {
@@ -37,7 +37,7 @@ object Type {
3737
def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutineeType: scala.quoted.Type[_])
3838
(using patternType: scala.quoted.Type[_], qctx: QuoteContext): Option[Tup] = {
3939
val qctx1 = quoteContextWithCompilerInterface(qctx)
40-
qctx1.reflect.typeTreeMatch(scrutineeType.unseal, patternType.unseal).asInstanceOf[Option[Tup]]
40+
qctx1.typeTreeMatch(scrutineeType.unseal, patternType.unseal).asInstanceOf[Option[Tup]]
4141
}
4242

4343

library/src-bootstrapped/scala/quoted/Expr.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scala.quoted
22

3-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
4-
53
/** Quoted expression of type `T` */
64
abstract class Expr[+T] private[scala] {
75

library/src-non-bootstrapped/scala/internal/quoted/Expr.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala.internal.quoted
22

33
import scala.quoted._
4-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
54

65
/** An Expr backed by a tree. Only the current compiler trees are allowed.
76
*

library/src-non-bootstrapped/scala/internal/quoted/Type.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala.internal.quoted
22

33
import scala.quoted._
4-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
54

65
/** Quoted type (or kind) `T` backed by a tree */
76
final class Type[Tree](val typeTree: Tree, val scopeId: Int) extends scala.quoted.Type[Any] {

library/src/scala/internal/tasty/CompilerInterface.scala renamed to library/src/scala/internal/quoted/CompilerInterface.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
package scala.internal.tasty
1+
package scala.internal.quoted
22

33
import scala.quoted.QuoteContext
44
import scala.tasty.reflect._
55
import scala.internal.quoted.PickledQuote
66

77
/** Part of the reflection interface that needs to be implemented by the compiler */
8-
trait CompilerInterface { self: scala.tasty.Reflection =>
8+
trait CompilerInterface { self: scala.quoted.QuoteContext =>
99

10-
//////////////////////
11-
// QUOTE UNPICKLING //
12-
//////////////////////
10+
import self.reflect._
1311

1412
/** Unpickle `repr` which represents a pickled `Expr` tree,
1513
* replacing splice nodes with `holes`
@@ -56,7 +54,7 @@ trait CompilerInterface { self: scala.tasty.Reflection =>
5654

5755
object CompilerInterface {
5856

59-
private[scala] def quoteContextWithCompilerInterface(qctx: QuoteContext): qctx.type { val reflect: qctx.reflect.type & scala.internal.tasty.CompilerInterface } =
60-
qctx.asInstanceOf[qctx.type { val reflect: qctx.reflect.type & scala.internal.tasty.CompilerInterface }]
57+
private[scala] def quoteContextWithCompilerInterface(qctx: QuoteContext): qctx.type { val reflect: qctx.reflect.type } & CompilerInterface =
58+
qctx.asInstanceOf[qctx.type { val reflect: qctx.reflect.type } & CompilerInterface]
6159

6260
}

library/src/scala/internal/quoted/PickledQuote.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala.internal.quoted
22

33
import scala.quoted._
4-
import scala.internal.tasty.CompilerInterface.quoteContextWithCompilerInterface
54

65
/** Pickled representation of a quoted expression or type */
76
trait PickledQuote:
@@ -18,13 +17,13 @@ trait PickledQuote:
1817
object PickledQuote:
1918

2019
def unpickleExpr[T](pickledQuote: PickledQuote): QuoteContext ?=> Expr[T] =
21-
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
22-
val tree = qctx.reflect.unpickleTerm(pickledQuote)
20+
val qctx = CompilerInterface.quoteContextWithCompilerInterface(summon[QuoteContext])
21+
val tree = qctx.unpickleTerm(pickledQuote)
2322
new scala.internal.quoted.Expr(tree, qctx.hashCode).asInstanceOf[Expr[T]]
2423

2524
def unpickleType[T](pickledQuote: PickledQuote): QuoteContext ?=> Type[T] =
26-
val qctx = quoteContextWithCompilerInterface(summon[QuoteContext])
27-
val tree = qctx.reflect.unpickleTypeTree(pickledQuote)
25+
val qctx = CompilerInterface.quoteContextWithCompilerInterface(summon[QuoteContext])
26+
val tree = qctx.unpickleTypeTree(pickledQuote)
2827
new scala.internal.quoted.Type(tree, qctx.hashCode).asInstanceOf[Type[T]]
2928

3029
/** Create an instance of PickledExpr from encoded tasty and sequence of labmdas to fill holes

library/src/scala/tasty/Reflection.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package scala.tasty
22

3-
import scala.internal.tasty.CompilerInterface
4-
53
import scala.quoted.QuoteContext
64
import scala.tasty.reflect._
75

tests/run-macros/quote-matcher-runtime/quoted_1.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ object Macros {
1313
s"Expr(${r.unseal.show})"
1414
case r: Type[_] =>
1515
s"Type(${r.unseal.show})"
16-
case r: String =>
17-
s"String($r)"
1816
}
1917
}
2018

0 commit comments

Comments
 (0)