11package dotty .tools .dotc .tastyreflect
22
3- import dotty .tools .dotc .ast .{Trees , tpd }
3+ import dotty .tools .dotc .ast .{Trees , tpd , untpd }
4+ import dotty .tools .dotc .core .Contexts
45import dotty .tools .dotc .core .Decorators ._
6+ import dotty .tools .dotc .core .StdNames .nme
57
68trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
79
10+ def ValueDeco (value : Value ): Pattern .ValueAPI = new Pattern .ValueAPI {
11+ def value (implicit ctx : Context ): Term = value
12+ }
13+ def BindDeco (bind : Bind ): Pattern .BindAPI = new Pattern .BindAPI {
14+ def name (implicit ctx : Context ): String = bind.name.toString
15+ def pattern (implicit ctx : Context ): Pattern = bind.body
16+ }
17+ def UnapplyDeco (unapply : Unapply ): Pattern .UnapplyAPI = new Pattern .UnapplyAPI {
18+ def fun (implicit ctx : Context ): Term = unapply.fun
19+ def implicits (implicit ctx : Context ): List [Term ] = unapply.implicits
20+ def patterns (implicit ctx : Context ): List [Pattern ] = effectivePatterns(unapply.patterns)
21+
22+ private def effectivePatterns (patterns : List [Pattern ]): List [Pattern ] = patterns match {
23+ case patterns0 :+ Trees .SeqLiteral (elems, _) => patterns0 ::: elems
24+ case _ => patterns
25+ }
26+ }
27+ def AlternativeDeco (alternatives : Alternatives ): Pattern .AlternativesAPI = new Pattern .AlternativesAPI {
28+ def patterns (implicit ctx : Context ): List [Pattern ] = alternatives.trees
29+ }
30+ def TypeTestDeco (typeTest : TypeTest ): Pattern .TypeTestAPI = new Pattern .TypeTestAPI {
31+ def tpt (implicit ctx : Context ): TypeTree = typeTest.tpt
32+ }
33+
834 // ----- Patterns -------------------------------------------------
935
1036 def PatternDeco (pattern : Pattern ): PatternAPI = new PatternAPI {
@@ -14,45 +40,104 @@ trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
1440
1541 object Pattern extends PatternModule {
1642
17- object Value extends ValueModule {
18- def unapply (x : Pattern )(implicit ctx : Context ): Option [Term ] = x match {
43+ object IsValue extends IsValueModule {
44+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Value ] = pattern match {
1945 case lit : tpd.Literal => Some (lit)
2046 case ref : tpd.RefTree if ref.isTerm => Some (ref)
2147 case ths : tpd.This => Some (ths)
2248 case _ => None
2349 }
2450 }
2551
52+ object Value extends ValueModule {
53+ def apply (term : Term )(implicit ctx : Context ): Value = term match {
54+ case lit : tpd.Literal => lit
55+ case ref : tpd.RefTree if ref.isTerm => ref
56+ case ths : tpd.This => ths
57+ }
58+ def copy (original : Value )(term : Term )(implicit ctx : Context ): Value = term match {
59+ case lit : tpd.Literal => tpd.cpy.Literal (original)(lit.const)
60+ case ref : tpd.RefTree if ref.isTerm => tpd.cpy.Ref (original.asInstanceOf [tpd.RefTree ])(ref.name)
61+ case ths : tpd.This => tpd.cpy.This (original)(ths.qual)
62+ }
63+ def unapply (x : Pattern )(implicit ctx : Context ): Option [Term ] = IsValue .unapply(x)
64+ }
65+
66+ object IsBind extends IsBindModule {
67+ def unapply (x : Pattern )(implicit ctx : Context ): Option [Bind ] = x match {
68+ case x : tpd.Bind if x.name.isTermName => Some (x)
69+ case _ => None
70+ }
71+ }
72+
2673 object Bind extends BindModule {
27- def unapply (x : Pattern )(implicit ctx : Context ): Option [(String , Pattern )] = x match {
28- case x : tpd.Bind if x.name.isTermName => Some (x.name.toString, x.body)
74+
75+ def copy (original : Bind )(name : String , pattern : Pattern )(implicit ctx : Context ): Bind =
76+ tpd.cpy.Bind (original)(name.toTermName, pattern)
77+
78+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [(String , Pattern )] = pattern match {
79+ case IsBind (pattern) => Some ((pattern.name.toString, pattern.body))
80+ case _ => None
81+ }
82+ }
83+
84+ object IsUnapply extends IsUnapplyModule {
85+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Unapply ] = pattern match {
86+ case pattern @ Trees .UnApply (_, _, _) => Some (pattern)
87+ case Trees .Typed (pattern @ Trees .UnApply (_, _, _), _) => Some (pattern)
2988 case _ => None
3089 }
3190 }
3291
3392 object Unapply extends UnapplyModule {
93+
94+ def copy (original : Unapply )(fun : Term , implicits : List [Term ], patterns : List [Pattern ])(implicit ctx : Context ): Unapply =
95+ tpd.cpy.UnApply (original)(fun, implicits, patterns)
96+
3497 def unapply (x : Pattern )(implicit ctx : Context ): Option [(Term , List [Term ], List [Pattern ])] = x match {
35- case Trees .UnApply (fun, implicits, patterns) => Some ((fun, implicits, effectivePatterns(patterns)))
36- case Trees .Typed (Trees .UnApply (fun, implicits, patterns), _) => Some ((fun, implicits, effectivePatterns(patterns)))
98+ case IsUnapply (x) => Some ((x.fun, x.implicits, UnapplyDeco (x).patterns))
3799 case _ => None
38100 }
39- private def effectivePatterns (patterns : List [Pattern ]): List [Pattern ] = patterns match {
40- case patterns0 :+ Trees .SeqLiteral (elems, _) => patterns0 ::: elems
41- case _ => patterns
101+ }
102+
103+ object IsAlternatives extends IsAlternativesModule {
104+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Alternatives ] = pattern match {
105+ case pattern : tpd.Alternative => Some (pattern)
106+ case _ => None
42107 }
43108 }
44109
45- object Alternative extends AlternativeModule {
110+ object Alternatives extends AlternativesModule {
111+ def apply (patterns : List [Pattern ])(implicit ctx : Context ): Alternatives =
112+ tpd.Alternative (patterns)
113+
114+ def copy (original : Alternatives )(patterns : List [Pattern ])(implicit ctx : Context ): Alternatives =
115+ tpd.cpy.Alternative (original)(patterns)
116+
46117 def unapply (x : Pattern )(implicit ctx : Context ): Option [List [Pattern ]] = x match {
47118 case x : tpd.Alternative => Some (x.trees)
48119 case _ => None
49120 }
50121 }
51122
123+ object IsTypeTest extends IsTypeTestModule {
124+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [TypeTest ] = pattern match {
125+ case Trees .Typed (_ : tpd.UnApply , _) => None
126+ case pattern : tpd.Typed => Some (pattern)
127+ case _ => None
128+ }
129+ }
130+
52131 object TypeTest extends TypeTestModule {
132+ def apply (tpt : TypeTree )(implicit ctx : Context ): TypeTest =
133+ tpd.Typed (untpd.Ident (nme.WILDCARD ).withType(tpt.tpe), tpt)
134+
135+ def copy (original : TypeTest )(tpt : TypeTree )(implicit ctx : Context ): TypeTest =
136+ tpd.cpy.Typed (original)(untpd.Ident (nme.WILDCARD ).withType(tpt.tpe), tpt)
137+
53138 def unapply (x : Pattern )(implicit ctx : Context ): Option [TypeTree ] = x match {
54139 case Trees .Typed (Trees .UnApply (_, _, _), _) => None
55- case Trees .Typed (_ , tpt) => Some (tpt)
140+ case Trees .Typed (expr , tpt) => Some (tpt)
56141 case _ => None
57142 }
58143 }
0 commit comments