@@ -45,9 +45,9 @@ object QuoteContextImpl {
45
45
46
46
}
47
47
48
- class QuoteContextImpl private (ctx : Context ) extends QuoteContext :
48
+ class QuoteContextImpl private (ctx : Context ) extends QuoteContext , scala.internal.quoted. CompilerInterface :
49
49
50
- object reflect extends scala.tasty.Reflection , scala.internal.tasty. CompilerInterface :
50
+ object reflect extends scala.tasty.Reflection :
51
51
52
52
def rootContext : Context = ctx
53
53
@@ -2615,70 +2615,74 @@ class QuoteContextImpl private (ctx: Context) extends QuoteContext:
2615
2615
private def withDefaultPos [T <: Tree ](fn : Context ?=> T ): T =
2616
2616
fn(using ctx.withSource(Position .ofMacroExpansion.source)).withSpan(Position .ofMacroExpansion.span)
2617
2617
2618
- def unpickleTerm (pickledQuote : PickledQuote ): Term =
2619
- PickledQuotes .unpickleTerm(pickledQuote)
2620
-
2621
- def unpickleTypeTree (pickledQuote : PickledQuote ): TypeTree =
2622
- PickledQuotes .unpickleTypeTree(pickledQuote)
2618
+ end reflect
2623
2619
2624
- def termMatch (scrutinee : Term , pattern : Term ): Option [Tuple ] =
2625
- treeMatch(scrutinee, pattern)
2620
+ def unpickleExpr (pickledQuote : PickledQuote ): scala.quoted.Expr [Any ] =
2621
+ val tree = PickledQuotes .unpickleTerm(pickledQuote)(using reflect.rootContext)
2622
+ new scala.internal.quoted.Expr (tree, hash)
2626
2623
2627
- def typeTreeMatch (scrutinee : TypeTree , pattern : TypeTree ): Option [Tuple ] =
2628
- treeMatch(scrutinee, pattern)
2624
+ def unpickleType (pickledQuote : PickledQuote ): scala.quoted.Type [? ] =
2625
+ val tree = PickledQuotes .unpickleTypeTree(pickledQuote)(using reflect.rootContext)
2626
+ new scala.internal.quoted.Type (tree, hash)
2629
2627
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 )
2628
+ def exprMatch (scrutinee : scala.quoted.Expr [Any ], pattern : scala.quoted.Expr [Any ]): Option [Tuple ] =
2629
+ treeMatch(scrutinee.unseal(using this ), pattern.unseal(using this ))
2646
2630
2647
- val (pat1, typeHoles) = extractTypeHoles(pattern)
2631
+ def typeMatch (scrutinee : scala.quoted.Type [? ], pattern : scala.quoted.Type [? ]): Option [Tuple ] =
2632
+ treeMatch(scrutinee.unseal(using this ), pattern.unseal(using this ))
2648
2633
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 }]
2634
+ private def treeMatch (scrutinee : reflect.Tree , pattern : reflect.Tree ): Option [Tuple ] = {
2635
+ import reflect ._
2636
+ given Context = rootContext
2637
+ def isTypeHoleDef (tree : Tree ): Boolean =
2638
+ tree match
2639
+ case tree : TypeDef =>
2640
+ tree.symbol.hasAnnotation(dotc.core.Symbols .defn.InternalQuotedPatterns_patternTypeAnnot )
2641
+ case _ => false
2658
2642
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
- }
2643
+ def extractTypeHoles (pat : Term ): (Term , List [Symbol ]) =
2644
+ pat match
2645
+ case tpd.Inlined (_, Nil , pat2) => extractTypeHoles(pat2)
2646
+ case tpd.Block (stats @ ((typeHole : TypeDef ) :: _), expr) if isTypeHoleDef(typeHole) =>
2647
+ val holes = stats.takeWhile(isTypeHoleDef).map(_.symbol)
2648
+ val otherStats = stats.dropWhile(isTypeHoleDef)
2649
+ (tpd.cpy.Block (pat)(otherStats, expr), holes)
2650
+ case _ =>
2651
+ (pat, Nil )
2652
+
2653
+ val (pat1, typeHoles) = extractTypeHoles(pattern)
2654
+
2655
+ val ctx1 =
2656
+ if typeHoles.isEmpty then ctx
2657
+ else
2658
+ val ctx1 = ctx.fresh.setFreshGADTBounds.addMode(dotc.core.Mode .GadtConstraintInference )
2659
+ ctx1.gadt.addToConstraint(typeHoles)
2660
+ ctx1
2661
+
2662
+ val qctx1 = dotty.tools.dotc.quoted.QuoteContextImpl ()(using ctx1)
2663
+ .asInstanceOf [QuoteContext & scala.internal.quoted.CompilerInterface ]
2664
+
2665
+ val matcher = new Matcher .QuoteMatcher [qctx1.type ](qctx1) {
2666
+ def patternHoleSymbol : qctx1.reflect.Symbol = dotc.core.Symbols .defn.InternalQuotedPatterns_patternHole .asInstanceOf
2667
+ def higherOrderHoleSymbol : qctx1.reflect.Symbol = dotc.core.Symbols .defn.InternalQuotedPatterns_higherOrderHole .asInstanceOf
2668
+ }
2663
2669
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
- }
2670
+ val matchings =
2671
+ if pat1.isType then matcher.termMatch(scrutinee.asInstanceOf [matcher.qctx.reflect.Term ], pat1.asInstanceOf [matcher.qctx.reflect.Term ])
2672
+ else matcher.termMatch(scrutinee.asInstanceOf [matcher.qctx.reflect.Term ], pat1.asInstanceOf [matcher.qctx.reflect.Term ])
2673
+
2674
+ // val matchings = matcher.termMatch(scrutinee, pattern)
2675
+ if typeHoles.isEmpty then matchings
2676
+ else {
2677
+ // After matching and doing all subtype checks, we have to approximate all the type bindings
2678
+ // that we have found, seal them in a quoted.Type and add them to the result
2679
+ def typeHoleApproximation (sym : Symbol ) =
2680
+ ctx1.gadt.approximation(sym, ! sym.hasAnnotation(dotc.core.Symbols .defn.InternalQuotedPatterns_fromAboveAnnot )).asInstanceOf [qctx1.reflect.TypeRepr ].seal
2681
+ matchings.map { tup =>
2682
+ Tuple .fromIArray(typeHoles.map(typeHoleApproximation).toArray.asInstanceOf [IArray [Object ]]) ++ tup
2678
2683
}
2679
2684
}
2680
-
2681
- end reflect
2685
+ }
2682
2686
2683
2687
private [this ] val hash = QuoteContextImpl .scopeId(using ctx)
2684
2688
override def hashCode : Int = hash
0 commit comments