1
1
package dotty .tools .dotc .tastyreflect
2
2
3
3
import dotty .tools .dotc .ast .{Trees , tpd }
4
+ import dotty .tools .dotc .core .Contexts
4
5
import dotty .tools .dotc .core .Decorators ._
5
6
6
7
trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
7
8
9
+ def ValueDeco (value : Value ): Pattern .ValueAPI = new Pattern .ValueAPI {
10
+ def value (implicit ctx : Context ): Term = ???
11
+ }
12
+ def BindDeco (bind : Bind ): Pattern .BindAPI = new Pattern .BindAPI {
13
+ def name (implicit ctx : Context ): String = ???
14
+ def pattern (implicit ctx : Context ): Pattern = ???
15
+ }
16
+ def UnapplyDeco (unapply : Unapply ): Pattern .UnapplyAPI = new Pattern .UnapplyAPI {
17
+ def fun (implicit ctx : Context ): Term = unapply.fun
18
+ def implicits (implicit ctx : Context ): List [Term ] = unapply.implicits
19
+ def patterns (implicit ctx : Context ): List [Pattern ] = effectivePatterns(unapply.patterns)
20
+
21
+ private def effectivePatterns (patterns : List [Pattern ]): List [Pattern ] = patterns match {
22
+ case patterns0 :+ Trees .SeqLiteral (elems, _) => patterns0 ::: elems
23
+ case _ => patterns
24
+ }
25
+ }
26
+ def AlternativeDeco (alternatives : Alternatives ): Pattern .AlternativesAPI = new Pattern .AlternativesAPI {
27
+ def patterns (implicit ctx : Context ): List [Pattern ] = alternatives.trees
28
+ }
29
+ def TypeTestDeco (typeTest : TypeTest ): Pattern .TypeTestAPI = new Pattern .TypeTestAPI {
30
+ def tpt (implicit ctx : Context ): TypeTree = typeTest.tpt
31
+ }
32
+
8
33
// ----- Patterns -------------------------------------------------
9
34
10
35
def PatternDeco (pattern : Pattern ): PatternAPI = new PatternAPI {
@@ -14,42 +39,79 @@ trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
14
39
15
40
object Pattern extends PatternModule {
16
41
17
- object Value extends ValueModule {
18
- def unapply (x : Pattern )(implicit ctx : Context ): Option [Term ] = x match {
42
+ object IsValue extends IsValueModule {
43
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Value ] = pattern match {
19
44
case lit : tpd.Literal => Some (lit)
20
45
case ref : tpd.RefTree if ref.isTerm => Some (ref)
21
46
case ths : tpd.This => Some (ths)
22
47
case _ => None
23
48
}
24
49
}
25
50
51
+ object Value extends ValueModule {
52
+ def apply (tpt : Term ): Value = ???
53
+ def copy (original : Value )(tpt : Term ): Value = ???
54
+ def unapply (x : Pattern )(implicit ctx : Context ): Option [Term ] = IsValue .unapply(x)
55
+ }
56
+
57
+ object IsBind extends IsBindModule {
58
+ def unapply (x : Pattern )(implicit ctx : Context ): Option [Bind ] = x match {
59
+ case x : tpd.Bind if x.name.isTermName => Some (x)
60
+ case _ => None
61
+ }
62
+ }
63
+
26
64
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)
65
+ def apply (name : String , pattern : Pattern ): Bind = ???
66
+ def copy (original : Bind )(name : String , pattern : Pattern ): Bind = ???
67
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [(String , Pattern )] = pattern match {
68
+ case IsBind (pattern) => Some ((pattern.name.toString, pattern.body))
69
+ case _ => None
70
+ }
71
+ }
72
+
73
+ object IsUnapply extends IsUnapplyModule {
74
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Unapply ] = pattern match {
75
+ case pattern @ Trees .UnApply (_, _, _) => Some (pattern)
76
+ case Trees .Typed (pattern @ Trees .UnApply (_, _, _), _) => Some (pattern)
29
77
case _ => None
30
78
}
31
79
}
32
80
33
81
object Unapply extends UnapplyModule {
82
+ def apply (fun : Term , implicits : List [Term ], patterns : List [Pattern ]): Unapply = ???
83
+ def copy (original : Unapply )(fun : Term , implicits : List [Term ], patterns : List [Pattern ]): Unapply =
84
+ tpd.cpy.UnApply (original)(fun, implicits, patterns)
34
85
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)))
86
+ case IsUnapply (x) => Some ((x.fun, x.implicits, UnapplyDeco (x).patterns))
37
87
case _ => None
38
88
}
39
- private def effectivePatterns (patterns : List [Pattern ]): List [Pattern ] = patterns match {
40
- case patterns0 :+ Trees .SeqLiteral (elems, _) => patterns0 ::: elems
41
- case _ => patterns
89
+ }
90
+
91
+ object IsAlternatives extends IsAlternativesModule {
92
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [Alternatives ] = pattern match {
93
+ case pattern : tpd.Alternative => Some (pattern)
94
+ case _ => None
42
95
}
43
96
}
44
97
45
- object Alternative extends AlternativeModule {
98
+ object Alternatives extends AlternativesModule {
99
+ def apply (patterns : List [Pattern ]): Alternatives = ???
100
+ def copy (original : Alternatives )(patterns : List [Pattern ]): Alternatives =
101
+ tpd.cpy.Alternative (original)(patterns)
46
102
def unapply (x : Pattern )(implicit ctx : Context ): Option [List [Pattern ]] = x match {
47
103
case x : tpd.Alternative => Some (x.trees)
48
104
case _ => None
49
105
}
50
106
}
51
107
108
+ object IsTypeTest extends IsTypeTestModule {
109
+ def unapply (pattern : Pattern )(implicit ctx : Context ): Option [TypeTest ] = ???
110
+ }
111
+
52
112
object TypeTest extends TypeTestModule {
113
+ def apply (tpt : List [TypeTree ]): TypeTest = ???
114
+ def copy (original : TypeTest )(tpt : List [TypeTree ]): TypeTest = ???
53
115
def unapply (x : Pattern )(implicit ctx : Context ): Option [TypeTree ] = x match {
54
116
case Trees .Typed (Trees .UnApply (_, _, _), _) => None
55
117
case Trees .Typed (_, tpt) => Some (tpt)
0 commit comments