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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
import java.io._;
import java.util.HashMap;
import scalac.{Global => scalac_Global};
import scalac._;
import scalac.ast._;
import scalac.symtab._;
import scalac.util._; // Names
import scalac.transformer.{ OwnerTransformer => scalac_transformer_OwnerTransformer };
//import scalac.transformer.matching.PatternMatcher ;
//import scalac.transformer.matching.AlgebraicMatcher ;
/** A transformer for expanding match expressions into
* flat sequences of .is and .as method calls
*
* @author Matthias Zenger, Burak Emir
* @version 1.1
*/
package scala.tools.scalac.transformer {
import matching.PatternMatcher ;
import matching.PartialMatcher ;
//import matching.FullRegularTranslator ;
class TransMatch( global:scalac_Global )
extends scalac_transformer_OwnerTransformer( global ) {
import Tree._ ;
var cunit:CompilationUnit = null;
//def debugLog(s:String) = {
//if(currentOwner.toString().indexOf("fetchToken") != -1) {
//Console.println(s);
//}
//}
override def apply( cunit:CompilationUnit ):unit = {
this.cunit = cunit;
super.apply( cunit );
}
def isRegular(ps:Array[Tree]):Boolean = {
var res = false;
val pats = scala.Iterator.fromArray( ps );
while (!res && pats.hasNext )
res = isRegular( pats.next );
res
}
def isRegular(pat:Tree):Boolean = pat match {
//case Alternative(_) => true
case Alternative(trees) =>
isRegular(trees)
case Bind( n, pat1 ) =>
TreeInfo.isNameOfStarPattern( n )
|| TreeInfo.isEmptySequence( pat1 )
|| isRegular( pat1 )
case Ident(n) => false
case Sequence( trees ) =>
( trees.length == 0 ) || isRegular( trees );
case Apply( fn, trees ) =>
isRegular( trees ) &&
!((trees.length == 1) && TreeInfo.isEmptySequence( trees( 0 )))
case Literal(_) => false;
case Select(_,_) => false;
case Typed(_,_) => false;
case _ => error("in TransMatch.isRegular phase: unknown node"+pat.getClass());
}
def nilVariables( pat:Tree ) = {
var res:List[Symbol] = Nil;
def getNilVars1( ps:Array[Tree] ):scala.Unit = {
val z:Seq[Tree] = ps; z.elements.foreach( x => getNilVars( x ));
}
def getNilVars( p:Tree ):scala.Unit = p match {
case Alternative( _ ) => /* no bind allowed! */
case Bind( _, pat ) =>
getNilVars(pat);
if( TreeInfo.isEmptySequence( pat ) )
res = p.symbol() :: res;
case Ident(_) =>
case Sequence( trees ) => getNilVars1( trees )
case Apply( _, args ) => getNilVars1( args )
case Literal(_) =>
case Select(_,_) =>
case Typed(_,_) =>
case _ => error("in TransMatch.nilVariables: unknown node"+pat.getClass());
}
getNilVars( pat );
res
}
// 2do: remove binds from pattern
def handleNilVariables( cse: CaseDef ): Unit = {
val nilvars = nilVariables(cse.pat);
if( !nilvars.isEmpty ) {
val newBody = new Array[Tree]( nilvars.length );
var j=0;
for( val v <- nilvars.elements ) {
val n = gen.mkNil( cse.pos );
newBody( j ) = gen.ValDef( v, n );
j = j + 1;
}
cse.body = gen.mkBlock( newBody, cse.body );
}
}
/** removes alternative notes by unfolding them, adding casedefs,
* and duplication righthand sides
*
* @pre cases are all nonregular
*/
def removeAlterns(cases: Array[CaseDef]) = {
//debugLog("removeAlterns called![ currentOwner = "+currentOwner+" [");
// var jjj = 0; while (jjj<cases.length) {
// debugLog(cases(jjj).toString());
// jjj = jjj + 1;
//}
//debugLog("]");
def lst2arr(l:List[Tree]):Array[Tree] = {
val res = new Array[Tree](l.length);
val it = l.elements; var i = 0; while(it.hasNext) {
res(i) = it.next;
i = i + 1;
}
res
}
def select(j:int, expanded:Array[List[Tree]]): List[List[Tree]] = {
if( j == expanded.length )
List(scala.Nil);
else {
val rest:List[List[Tree]] = select(j+1, expanded);
//expanded(j) map { x:Tree => rest map { xs:List[Tree] => x :: xs }}
for(val t <- expanded(j);
val zs <- rest)
yield t :: zs;
}
}
def newCloner() = {
val sc = new SymbolCloner();
sc.owners.put(currentOwner,currentOwner);
new GenTreeCloner(global, Type.IdMap, sc) {
override def getSymbolFor(tree:Tree): Symbol = {
//Console.println("getSymbolFor "+tree.getClass());
tree match {
case Bind(_,_) =>
val symbol = cloner.cloneSymbol(tree.symbol());
//System.out.println("TreeCloner: Bind"+symbol);
//System.out.println("TreeCloner: Bind - old owner "+tree.symbol().owner());
//System.out.println("TreeCloner: Bind - new owner "+symbol.owner());
//Console.println("in TreeCloner: type = "+symbol.getType());
symbol.setType(transform(symbol.getType()));
//Console.println("in TreeCloner: type (post) = "+symbol.getType());
//System.out.println("done TreeCloner: Bind"+symbol);
return symbol;
case _ : ClassDef =>
throw new ApplicationError("sorry, cannot compile this correctly in this scala version. Move your classes, fundefs etc out of the pattern body, please, and wait for nsc.");
case _ : DefDef =>
throw new ApplicationError("sorry, cannot compile this correctly in this scala version. Move your classes, fundefs etc out of the pattern body, please, and wait for nsc.");
// tree.symbol();
case Ident(_) if tree.symbol() == global.definitions.PATTERN_WILDCARD =>
tree.symbol()
case _ =>
super.getSymbolFor(tree);
}
}
}
}
def remove(pat:Tree): List[Tree] = {
//Console.println("remove("+pat+"), currentOwner = "+currentOwner);
val res = pat match {
case Alternative( branches ) => // no bind allowed!
val z: Seq[Tree] = branches;
List.flatten(z.toList.map( remove ));
case b @ Bind( n, pat ) =>
//Console.println("remove-Bind sym-owner: "+b.symbol().owner());
remove(pat) match {
case List(r) if (r eq pat) =>
List(b);
case zs =>
//Console.println("remove-Bind sym: "+b.symbol());
//Console.println("remove-Bind sym-type: "+b.symbol().getType());
//Console.println("rb-(case zs)"+zs);
zs map { x => //Console.println(b.symbol());
val r = new ExtBind(b.symbol(),x);
r.setType(b.getType());
//Console.println("rrrrr ="+ r.getType());
r
};
}
case Sequence( trees ) =>
val expanded = new Array[List[Tree]](trees.length);
var i = 0; while( i < trees.length) {
expanded(i) = remove(trees(i));
i = i + 1;
}
select(0, expanded) map { x:List[Tree] =>
//Console.println("remove:Sequence, type = "+pat.getType());
val res = Sequence(lst2arr(x)).setType(pat.getType());
//Console.println("remove:Sequence RETURNS "+res);
res
}
case Apply( fn, trees ) =>
val expanded = new Array[List[Tree]](trees.length);
var i = 0; while( i < trees.length) {
expanded(i) = remove(trees(i));
i = i + 1;
}
select(0, expanded) map { x => new Apply(fn, lst2arr(x))
.setType(pat.getType()) }
case Ident(_) => List(pat);
case Literal(_) => List(pat);
case Select(_,_) => List(pat);
case Typed(_,_) =>
List(pat);
case _ => error("in TransMatch.nilVariables: unknown node"+pat.getClass());
}
//Console.println("end remove("+pat+")");
res
}
val zs:Seq[CaseDef] = cases;
val ncases: List[List[Tree]] = zs.toList map {
x => x match {
case CaseDef(pat,guard,body) =>
//debugLog("removeAlterns - ("+x+")");
remove(pat) match {
case List(p) => List(x);
case pats =>
//debugLog("removal yields patterns (before trans) in "+cunit);
//debugLog( pats.mkString("","\n","") );
pats map {
npat =>
val tc = newCloner();
//Console.println("start cloning! casedef in "+cunit);
val res = tc.transform(new CaseDef(npat,
guard.duplicate(),
body.duplicate()));
/*Console.println(sc.owners);*/res}
}
}
}
lst2arr(List.flatten(ncases));
}
//val bsf = new scala.util.automaton.BerrySethi[ matching.PatternTest ]( pe );
def transform( root:Tree, cases:Array[CaseDef], restpe:Type ):Tree = {
if( global.newMatch ) {
//val fm = new FullRegularTranslator( global );
//val gram = fm.MakeGrammar( scala.Iterator.fromArray( cases ) );
//Console.println("writing out the grammar to /tmp/encodedGrammar.bin");
////val f = new FileOutputStream(new File("/tmp/encodedGrammar.bin"));
////f.write( gram.encode );
////f.close();
//// val gram = Predef.decode( Predef.Array[] );
//Console.println( gram.encode);
throw new ApplicationError("not impl.");
};
var containsReg = false;
var i = 0;
while (i < cases.length) {
if(isRegular(cases( i ).pat)) {
containsReg = true;
handleNilVariables(cases( i ));
}
i = i+1;
}
//Console.println("TransMatch: ");
//for(val ci <- cases) Console.println(ci);
if( containsReg ) {
//Console.println("TransMatch: isRegular!");
/*
val pe = new matching.PatternExp(global.definitions); // TEST
var j = 0;
val pat = new Array[pe.RegExp](cases.length);
while( j < cases.length) {
pat(j) = pe.fromTree(cases(j).pat);
j = j + 1;
} // TEST
*/
//debugLog("containsReg!");
val am = new matching.AlgebraicMatcher( cunit );
val matcher = new PartialMatcher( currentOwner, root, restpe );
am.construct( matcher, cases.asInstanceOf[ Array[Tree] ] );
matcher.tree
} else {
//Console.println("TransMatch: NOT regular");
val pm = new matching.PatternMatcher( cunit );
pm.initialize(root, currentOwner, restpe, true );
try{
val ncases = removeAlterns(cases);
//if(ncases.length > cases.length) {
// debugLog("did some removal!");
// var kk = 0; while (kk<ncases.length) {
// debugLog(ncases(kk).toString());
// kk = kk + 1;
// }
//} else
//else debugLog("did NOT do removal!");
pm.construct( ncases.asInstanceOf[Array[Tree]] );
} catch {
case e:Throwable =>
e.printStackTrace();
Console.print("failed on pats "+scala.Iterator.fromArray(cases).toList.mkString("","\n","")+", message\n"+e.getMessage());
Console.print(" unit "+cunit);
Console.println(" with exception:"+e.getMessage());
//e.printStackTrace();
System.exit(-1); //Debug.abort()
}
if (global.log()) {
global.log("internal pattern matching structure");
pm.print();
}
pm.toTree();
}
}
/** evil hack. OwnerTransformer should have this function */
def transform1(ts:Array[CaseDef]):Array[CaseDef] = {
var i = 0;
while( i < ts.length ) {
val t = transform(ts( i ));
if (t != ts( i )) {
val res = new Array[CaseDef](ts.length);
System.arraycopy(ts, 0, res, 0, i);
res( i ) = t.asInstanceOf[CaseDef];
i = i + 1;
while(i < ts.length) {
res( i ) = transform(ts( i )).asInstanceOf[CaseDef];
i = i + 1
};
return res;
};
i = i + 1;
}
return ts;
}
override def transform( tree: Tree ):Tree = {
if (tree == null)
return null;
else
tree match {
case Apply(Select( receiver, Names._match ), args) =>
if ((args != null) && (args.length == 1))
args( 0 ) match {
case Visitor( cases ) =>
return transform(transform(receiver), transform1(cases), tree.getType());
}
return tree;
case Apply(TypeApply(Select( receiver, Names._match ), targs), args) =>
if ((args != null) && (args.length == 1))
args( 0 ) match {
case Visitor( cases ) =>
return transform(transform(receiver), transform1(cases), tree.getType());
}
return tree;
case _ =>
super.transform(tree);
}
}
}
}
|