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
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
** **
** $Id$
\* */
package scalac.transformer.matching;
import scala.tools.util.Position;
import scalac.*;
import scalac.ast.*;
import scalac.atree.AConstant.*;
import scalac.util.*;
import scalac.symtab.*;
import PatternNode.*;
import Tree.*;
class CodeFactory extends PatternTool {
public int pos = Position.FIRSTPOS ;
public CodeFactory( CompilationUnit unit, int pos ) {
super( unit );
this.pos = pos;
}
// --------- these are new
/** a faked switch statement
*/
Tree Switch( Tree condition[],
Tree body[],
Tree defaultBody ) {
assert condition != null:"cond is null";
assert body != null:"body is null";
assert defaultBody != null:"defaultBody is null";
Tree result = defaultBody;
for( int i = condition.length-1; i >= 0; i-- )
result = gen.If(condition[i], body[i], result);
return result ;
}
/** returns `List[ Tuple2[ scala.Int, <elemType> ] ]' */
Type SeqTraceType( Type elemType ) {
return defs.LIST_TYPE(pairType(defs.int_TYPE(), elemType));
}
/** returns `Iterator[ elemType ]' */
Type _seqIterType( Type elemType ) {
return defs.ITERATOR_TYPE(elemType);
}
/** returns `<seqObj.elements>' */
Tree newIterator( Tree seqObj, Type elemType ) {
return gen.mkApply__(gen.Select(seqObj, defs.ITERABLE_ELEMENTS()));
}
/** returns code `<seqObj>.elements'
* the parameter needs to have type attribute `Sequence[<elemType>]'
*/
Tree newIterator( Tree seqObj ) {
return newIterator( seqObj, getElemType_Sequence( seqObj.getType() ));
}
// EXPERIMENTAL
Tree newRef( Tree init ) {
//System.out.println( "hello:"+refSym().type() );
return gen.New( gen.mkApplyTV( gen.mkPrimaryConstructorGlobalRef( pos, defs.REF_CLASS),
new Type[] { init.getType() },
new Tree[] { init } ));
}
/** returns A for T <: Sequence[ A ]
*/
Type getElemType_Sequence( Type tpe ) {
//System.err.println("getElemType_Sequence("+tpe.widen()+")");
Type tpe1 = tpe.widen().baseType( defs.SEQ_CLASS );
if( tpe1 == Type.NoType )
throw new ApplicationError("arg "+tpe+" not subtype of Seq[ A ]");
return tpe1.typeArgs()[ 0 ];
}
/** returns A for T <: Iterator[ A ]
*/
Type getElemType_Iterator( Type tpe ) {
//System.err.println("getElemType_Iterator("+tpe+")");
Type tpe1 = tpe.widen().baseType( defs.ITERATOR_CLASS );
switch( tpe1 ) {
case TypeRef(_,_,Type[] args):
return args[ 0 ];
default:
throw new ApplicationError("arg "+tpe+" not subtype of Iterator[ A ]");
}
}
/** `it.next()'
*/
public Tree _next( Tree iter ) {
return gen.mkApply__(gen.Select(iter, defs.ITERATOR_NEXT()));
}
/** `it.hasNext()'
*/
public Tree _hasNext( Tree iter ) {
return gen.mkApply__(gen.Select(iter, defs.ITERATOR_HASNEXT()));
}
/** `!it.hasCur()'
*/
public Tree _not_hasNext( Tree iter ) {
return gen.mkApply__(gen.Select(_hasNext(iter), defs.BOOLEAN_NOT()));
}
/** `trace.isEmpty'
*/
public Tree isEmpty( Tree iter ) {
return gen.mkApply__(gen.Select(iter, defs.LIST_ISEMPTY()));
}
Tree SeqTrace_headElem( Tree arg ) { // REMOVE SeqTrace
Tree t = gen.mkApply__(gen.Select(arg, defs.LIST_HEAD()));
return gen.mkApply__(gen.Select(t, defs.TUPLE_FIELD(2, 2)));
}
Tree SeqTrace_headState( Tree arg ) { // REMOVE SeqTrace
Tree t = gen.mkApply__(gen.Select(arg, defs.LIST_HEAD()));
return gen.mkApply__(gen.Select(t, defs.TUPLE_FIELD(2, 1)));
}
Tree SeqTrace_tail( Tree arg ) { // REMOVE SeqTrace
return gen.mkApply__(gen.Select(arg, defs.LIST_TAIL()));
}
/** `<seqlist>.head()'
*/
Tree SeqList_head( Tree arg ) {
return gen.mkApply__(gen.Select(arg, defs.LIST_HEAD()));
}
// unused
public Tree Negate(Tree tree) {
switch (tree) {
case Literal(BOOLEAN(boolean value)):
return gen.mkBooleanLit(tree.pos, !value);
}
return gen.mkApply__(gen.Select(tree, defs.BOOLEAN_NOT()));
}
protected Tree And(Tree left, Tree right) {
switch (left) {
case Literal(BOOLEAN(boolean value)):
return value ? right : left;
}
switch (right) {
case Literal(BOOLEAN(boolean value)):
if (value) return left;
}
return gen.mkApply_V(gen.Select(left, defs.BOOLEAN_AND()), new Tree[]{right});
}
protected Tree Or(Tree left, Tree right) {
switch (left) {
case Literal(BOOLEAN(boolean value)):
return value ? left : right;
}
switch (right) {
case Literal(BOOLEAN(boolean value)):
if (!value) return left;
}
return gen.mkApply_V(gen.Select(left, defs.BOOLEAN_OR()), new Tree[]{right});
}
// used by Equals
private Symbol getCoerceToInt(Type left) {
Symbol sym = left.lookupNonPrivate(Names.coerce);
assert sym != Symbol.NONE : Debug.show(left);
Symbol[] syms = sym.alternativeSymbols();
for (int i = 0; i < syms.length; i++) {
switch (syms[i].info()) {
case MethodType(Symbol[] vparams, Type restpe):
if (vparams.length == 0 && restpe.isSameAs(defs.INT_TYPE()))
return syms[i];
}
}
assert false : Debug.show(left);
return null;
}
// used by Equals
private Symbol getEqEq(Type left, Type right) {
Symbol sym = left.lookupNonPrivate(Names.EQEQ);
assert sym != Symbol.NONE
: Debug.show(left) + "::" + Debug.show(left.members());
Symbol fun = null;
Type ftype = defs.ANY_TYPE();
Symbol[] syms = sym.alternativeSymbols();
for (int i = 0; i < syms.length; i++) {
Symbol[] vparams = syms[i].valueParams();
if (vparams.length == 1) {
Type vptype = vparams[0].info();
if (right.isSubType(vptype) && vptype.isSubType(ftype)) {
fun = syms[i];
ftype = vptype;
}
}
}
assert fun != null : Debug.show(sym.info());
return fun;
}
protected Tree Equals(Tree left, Tree right) {
Type ltype = left.type.widen(), rtype = right.type.widen();
if (ltype.isSameAs(rtype)
&& (ltype.isSameAs(defs.CHAR_TYPE())
|| ltype.isSameAs(defs.BYTE_TYPE())
|| ltype.isSameAs(defs.SHORT_TYPE())))
{
right = gen.mkApply__(gen.Select(right, getCoerceToInt(rtype)));
rtype = defs.INT_TYPE();
}
Symbol eqsym = getEqEq(ltype, rtype);
return gen.mkApply_V(gen.Select(left, eqsym), new Tree[]{right});
}
protected Tree ThrowMatchError(int pos, Type type) {
return gen.mkApplyTV(
gen.mkGlobalRef(pos, defs.MATCHERROR_FAIL()),
new Tree[]{gen.mkType(pos, type)},
new Tree[]{
gen.mkStringLit(pos, unit.toString()),
gen.mkIntLit(pos, Position.line(pos))
});
}
protected Tree ThrowMatchError(int pos, Type type, Tree tree) {
return gen.mkApplyTV(
gen.mkGlobalRef(pos, defs.MATCHERROR_REPORT()),
new Tree[]{gen.mkType(pos, type)},
new Tree[]{
gen.mkStringLit(pos, unit.toString()),
gen.mkIntLit(pos, Position.line(pos)),
tree
});
}
protected Tree Error(int pos, Type type) {
return gen.mkApplyTV(
gen.mkGlobalRef(pos, defs.MATCHERROR_FAIL()),
new Tree[]{gen.mkType(pos, type)},
new Tree[]{
gen.mkStringLit(pos, unit.toString()),
gen.mkIntLit(pos, Position.line(pos))
});
}
Type pairType( Type left, Type right ) {
return defs.TUPLE_TYPE(new Type[] { left, right } );
}
Tree newPair( Tree left, Tree right ) {
return gen.New(gen.mkApplyTV( gen.mkPrimaryConstructorGlobalRef( pos, defs.TUPLE_CLASS[2]),
new Type[] { left.getType(), right.getType() },
new Tree[] { left, right }));
}
}
|