1
1
package dotty .tools .dotc .tastyreflect
2
2
3
- import dotty .tools .dotc .ast .{Trees , tpd }
3
+ import dotty .tools .dotc .ast .{Trees , tpd , untpd }
4
+ import dotty .tools .dotc .core .Contexts
4
5
import dotty .tools .dotc .core .Decorators ._
6
+ import dotty .tools .dotc .core .StdNames .nme
5
7
6
8
trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
7
9
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
+
8
34
// ----- Patterns -------------------------------------------------
9
35
10
36
def PatternDeco (pattern : Pattern ): PatternAPI = new PatternAPI {
@@ -14,45 +40,106 @@ trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
14
40
15
41
object Pattern extends PatternModule {
16
42
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 {
19
45
case lit : tpd.Literal => Some (lit)
20
46
case ref : tpd.RefTree if ref.isTerm => Some (ref)
21
47
case ths : tpd.This => Some (ths)
22
48
case _ => None
23
49
}
24
50
}
25
51
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
+
26
73
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
+ def apply (name : String , pattern : Pattern )(implicit ctx : Context ): Bind = ???
75
+
76
+ def copy (original : Bind )(name : String , pattern : Pattern )(implicit ctx : Context ): Bind =
77
+ tpd.cpy.Bind (original)(name.toTermName, pattern)
78
+
79
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [(String , Pattern )] = pattern match {
80
+ case IsBind (pattern) => Some ((pattern.name.toString, pattern.body))
81
+ case _ => None
82
+ }
83
+ }
84
+
85
+ object IsUnapply extends IsUnapplyModule {
86
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Unapply ] = pattern match {
87
+ case pattern @ Trees .UnApply (_, _, _) => Some (pattern)
88
+ case Trees .Typed (pattern @ Trees .UnApply (_, _, _), _) => Some (pattern)
29
89
case _ => None
30
90
}
31
91
}
32
92
33
93
object Unapply extends UnapplyModule {
94
+ def apply (fun : Term , implicits : List [Term ], patterns : List [Pattern ])(implicit ctx : Context ): Unapply = ???
95
+
96
+ def copy (original : Unapply )(fun : Term , implicits : List [Term ], patterns : List [Pattern ])(implicit ctx : Context ): Unapply =
97
+ tpd.cpy.UnApply (original)(fun, implicits, patterns)
98
+
34
99
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)))
100
+ case IsUnapply (x) => Some ((x.fun, x.implicits, UnapplyDeco (x).patterns))
37
101
case _ => None
38
102
}
39
- private def effectivePatterns (patterns : List [Pattern ]): List [Pattern ] = patterns match {
40
- case patterns0 :+ Trees .SeqLiteral (elems, _) => patterns0 ::: elems
41
- case _ => patterns
103
+ }
104
+
105
+ object IsAlternatives extends IsAlternativesModule {
106
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Alternatives ] = pattern match {
107
+ case pattern : tpd.Alternative => Some (pattern)
108
+ case _ => None
42
109
}
43
110
}
44
111
45
- object Alternative extends AlternativeModule {
112
+ object Alternatives extends AlternativesModule {
113
+ def apply (patterns : List [Pattern ])(implicit ctx : Context ): Alternatives =
114
+ tpd.Alternative (patterns)
115
+
116
+ def copy (original : Alternatives )(patterns : List [Pattern ])(implicit ctx : Context ): Alternatives =
117
+ tpd.cpy.Alternative (original)(patterns)
118
+
46
119
def unapply (x : Pattern )(implicit ctx : Context ): Option [List [Pattern ]] = x match {
47
120
case x : tpd.Alternative => Some (x.trees)
48
121
case _ => None
49
122
}
50
123
}
51
124
125
+ object IsTypeTest extends IsTypeTestModule {
126
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [TypeTest ] = pattern match {
127
+ case Trees .Typed (_ : tpd.UnApply , _) => None
128
+ case pattern : tpd.Typed => Some (pattern)
129
+ case _ => None
130
+ }
131
+ }
132
+
52
133
object TypeTest extends TypeTestModule {
134
+ def apply (tpt : TypeTree )(implicit ctx : Context ): TypeTest =
135
+ tpd.Typed (untpd.Ident (nme.WILDCARD ).withType(tpt.tpe), tpt)
136
+
137
+ def copy (original : TypeTest )(tpt : TypeTree )(implicit ctx : Context ): TypeTest =
138
+ tpd.cpy.Typed (original)(untpd.Ident (nme.WILDCARD ).withType(tpt.tpe), tpt)
139
+
53
140
def unapply (x : Pattern )(implicit ctx : Context ): Option [TypeTree ] = x match {
54
141
case Trees .Typed (Trees .UnApply (_, _, _), _) => None
55
- case Trees .Typed (_ , tpt) => Some (tpt)
142
+ case Trees .Typed (expr , tpt) => Some (tpt)
56
143
case _ => None
57
144
}
58
145
}
0 commit comments