1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/* NSC -- new Scala compiler
* Copyright 2005-2007 LAMP/EPFL
* @author Burak Emir
*/
// $Id$
package scala.tools.nsc.matching
/** Translation of pattern matching
*
* @author Burak Emir
*/
trait TransMatcher { self: transform.ExplicitOuter with PatternNodes with CodeFactory =>
import global._
import definitions._
import posAssigner.atPos
import symtab.Flags
import collection.mutable.ListBuffer
var cunit: CompilationUnit = _
def fresh = cunit.fresh
var resultType: Type = _
// cache these
final val settings_debug = settings.debug.value
final val settings_squeeze = settings.Xsqueeze.value == "on"
final val settings_useParallel = settings.Xmatchalgo.value != "incr"
final val settings_useIncr = settings.Xmatchalgo.value != "par"
final val settings_casetags = settings.Xcasetags.value == "on"
/** returns true if apply is a "sequence apply". analyzer inserts Sequence nodes if something is a
*
* - last update: discussion with Martin 2005-02-18
*
* - if true, tree.fn must be ignored. The analyzer ensures that the selector will be a subtype
* of fn; it thus assigns the expected type from the context (which is surely a subtype,
* but may have different flags etc.
*
* - so should be
* (( tree.args.length == 1 ) && tree.args(0).isInstanceOf[Sequence])
* but fails
*/
final def isSeqApply(tree: Apply): Boolean = {
tree match {
case Apply(_, List(ArrayValue(_,_))) => (tree.tpe.typeSymbol.flags & Flags.CASE) == 0
case _ => false;
}
}
/**
*/
final def hasRegularPattern(pats1: List[Tree]): Boolean = {
var pats = pats1; while(pats ne Nil) {
if(isRegularPattern(pats.head)) { return true; } else { pats = pats.tail }
}
return false
}
final def isRegularPattern(pat: Tree): Boolean = {
pat match {
case Alternative(trees) => hasRegularPattern(trees)
case Star(t) => true
case Ident(_) => false
case Bind(n, pat1) => isRegularPattern(pat1)
case Sequence(trees) => true // cause there are ArrayValues now
case ArrayValue(tt, trees) => hasRegularPattern(trees)
case Apply(fn, trees) => hasRegularPattern(trees)
case Literal(_) => false
case Select(_, _) => false
case Typed(_, _) => false
case UnApply(_,trees) => hasRegularPattern(trees)
}
}
// @todo: this should be isNotRegular :-/ premature opt src of all evil
// check special case Seq(p1,...,pk,_*) where pi not regular
protected def isRightIgnoring(p: ArrayValue): Boolean = {
def isDefaultStar(p: Tree): Boolean = p match {
case Bind(_, q) => isDefaultStar(q)
case Star(Ident(nme.WILDCARD)) => true
case _ => false
}
p match {
case ArrayValue(s, trees) =>
var ts = trees
var c: Tree = null
while ((ts ne Nil) && {c = ts.head; ts = ts.tail; !isRegularPattern(c)}) {}
(ts eq Nil) && isDefaultStar(c)
}
}
/** a casedef with sequence subpatterns like
*
* case ..x @ ().. => body
*
* should be replaced straight away with
*
* case .. () .. => val x = Nil; body
*/
def isRegular(pats: List[CaseDef]): (List[CaseDef],Boolean) = {
var existsReg = false
var isReg = false
var nilVars: List[Symbol] = null
def isRegular1(pat: Tree): Tree = pat match {
case Alternative(trees) =>
copy.Alternative(pat, trees map { isRegular1 })
case Star(t) =>
isReg = true; copy.Star(pat, isRegular1(t))
case Ident(_) =>
pat
case Bind(id, empt @ Sequence(List())) =>
nilVars = pat.symbol /*id.symbol()*/ :: nilVars
empt
case Bind(n, pat1) =>
copy.Bind(pat, n, isRegular1(pat1))
case Sequence(trees) =>
//isReg = isReg || ( trees.length == 0 );
isReg = true // cause there are ArrayValues now
copy.Sequence(pat, trees map { isRegular1 })
case UnApply(fn, args) => copy.UnApply(pat, fn, args map { isRegular1 })
// a pattern of the form List(foo@_*)
case app @ Apply(fn, List(pat2@ ArrayValue( tt, List(b @ Bind(id, Star(wc @ Ident(nme.WILDCARD))))))) if isSeqApply(app) =>
//Console.println("OPTIMIZING")
//Console.println(pat)
//Console.println(pat.tpe)
//Console.println(tt.tpe)
//Console.println("b.tpe "+b.tpe+" widened"+b.tpe.widen)
//Console.println("b.symbol.tpe "+b.symbol.tpe+" widened"+b.symbol.tpe.widen)
//Console.println("pat2.tpe "+pat2.tpe+" widened"+pat2.tpe.widen)
val tpe1:Type = pat2.tpe.widen.baseType( definitions.SeqClass ).typeArgs(0)
val tpe = appliedType(definitions.SeqClass.typeConstructor, List(tpe1))
b.symbol.setInfo(tpe)
b.setType(tpe)
val res = copy.Bind(b, id, wc)
//Console.println("====>")
//Console.println(res)
res
// a pattern of the form MyCaseConstructor(foo@_*)
case app @ Apply(fn, List(pat2@ ArrayValue( tt, List(b @ Bind(id, Star(wc @ Ident(nme.WILDCARD))))))) =>
//Console.println("OPTIMIZING")
//Console.println(pat)
//Console.println(pat.tpe)
//Console.println(tt.tpe)
//Console.println("b.tpe "+b.tpe+" widened"+b.tpe.widen)
//Console.println("b.symbol.tpe "+b.symbol.tpe+" widened"+b.symbol.tpe.widen)
//Console.println("pat2.tpe "+pat2.tpe+" widened"+pat2.tpe.widen)
val tpe1:Type = pat2.tpe.widen.baseType( definitions.SeqClass ).typeArgs(0)
val tpe = appliedType(definitions.SeqClass.typeConstructor, List(tpe1))
b.symbol.setInfo(tpe)
b.setType(tpe)
val res = copy.Apply(pat, fn, List(copy.Bind(b, id, wc)))
//Console.println("====>")
//Console.println(res)
res
case av @ ArrayValue(s, trees) =>
if (isRightIgnoring(av)) pat
else copy.ArrayValue(pat, s, (trees map { isRegular1 }))
case Apply(fn, List(Sequence(List()))) =>
pat
case Apply(fn, trees) =>
//Console.println(" IN isRegular, apply node "+pat.toString());
//Console.println(" trees are:"+(trees map {x => x.getClass().toString()}));
copy.Apply(pat, fn, (trees map { isRegular1 }))
case Literal(_) =>
pat
case Select(_, _) =>
pat
case Typed(_, _) =>
pat
case This(_) => // Sean's feature request #1134, compiled incorrectly
val stpe = mkThisType(pat.tpe.typeSymbol)
Typed(Ident(nme.WILDCARD) setType stpe, TypeTree(stpe))
//case _ =>
// Console.println(pat);
// Console.println(pat.getClass());
// scala.Predef.error(" what is this ? ")
}
var res = new ListBuffer[CaseDef]
val it = pats.elements; while (it.hasNext) {
nilVars = Nil
val cdef = it.next
val newt = isRegular1(cdef.pat)
existsReg = existsReg || isReg
val nbody = if (nilVars.isEmpty) cdef.body else
atPos(cdef.body.pos)(
Block(nilVars map {
x => ValDef(x, Ident(definitions.NilModule))
}, cdef.body)
)
res += copy.CaseDef(cdef, newt, cdef.guard, nbody)
}
(res.toList, existsReg)
}
/** handles all translation of pattern matching
*/
def handlePattern(sel: Tree, ocases: List[CaseDef], doCheckExhaustive: Boolean,
owner: Symbol, handleOuter: Tree => Tree, localTyper: Tree => Tree): Tree = {
// TEMPORARY
//new NewMatcher().toIR(sel, ocases)
//
// 1. is there a regular pattern?
val (cases, containsReg) = isRegular(ocases)
// @todo: remove unused variables
if (containsReg) {
cunit.error(sel.pos, "regular expressions not yet implemented")
//sel
EmptyTree
} else {
val pm = new PatternMatcher()
pm.initialize(sel, doCheckExhaustive, owner,handleOuter, localTyper)
pm.construct(cases)
//if (global.log()) {
// global.log("internal pattern matching structure");
// pm.print();
//}
pm.toTree()
}
}
}
|