summaryrefslogtreecommitdiff
path: root/sources/scalac/transformer/UnCurry.java
blob: f088ca6943173e5d54d5da88a0861b63aee00ed0 (plain) (blame)
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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.transformer;

import java.io.*;
import java.util.*;
import scalac.*;
import scalac.util.*;
import scalac.ast.*;
import scalac.symtab.*;
import Tree.*;

/** - uncurry all symbol and tree types (@see UnCurryPhase)
 *  - for every curried parameter list:  (ps_1) ... (ps_n) ==> (ps_1, ..., ps_n)
 *  - for every curried application: f(args_1)...(args_n) ==> f(args_1, ..., args_n)
 *  - for every type application: f[Ts] ==> f[Ts]() unless followed by parameters
 *  - for every use of a parameterless function: f ==> f()  and  q.f ==> q.f()
 *  - for every def-parameter:  def x: T ==> x: () => T
 *  - for every use of a def-parameter: x ==> x.apply()
 *  - for every argument to a def parameter `def x: T':
 *      if argument is not a reference to a def parameter:
 *        convert argument `e' to (expansion of) `() => e'
 *  - for every argument list that corresponds to a repeated parameter
 *       (a_1, ..., a_n) => (Sequence(a_1, ..., a_n))
 *  - for every argument list that is an escaped sequence
 *       (a_1:_*) => (a_1)
 */
public class UnCurry extends OwnerTransformer
                     implements Modifiers {

    UnCurryPhase descr;
    Unit unit;

    public UnCurry(Global global, UnCurryPhase descr) {
        super(global);
	this.descr = descr;
    }

    public void apply(Unit unit) {
	this.unit = unit;
	super.apply(unit);
    }

    /** (ps_1) ... (ps_n) => (ps_1, ..., ps_n)
     */
    ValDef[][] uncurry(ValDef[][] params) {
	int n = 0;
	for (int i = 0; i < params.length; i++)
	    n = n + params[i].length;
	ValDef[] ps = new ValDef[n];
	int j = 0;
	for (int i = 0; i < params.length; i++) {
	    System.arraycopy(params[i], 0, ps, j, params[i].length);
	    j = j + params[i].length;
	}
	return new ValDef[][]{ps};
    }

    /** tree of non-method type T ==> same tree with method type ()T
     */
    Tree asMethod(Tree tree) {
	switch (tree.type) {
	case MethodType(_, _):
	    return tree;
	default:
	    return tree.setType(
		Type.MethodType(Symbol.EMPTY_ARRAY, tree.type.widen()));
	}
    }

    /** apply parameterless functions and def parameters
     */
    Tree applyDef(Tree tree1) {
	assert tree1.symbol() != null : tree1;
	switch (tree1.symbol().type()) {
	case PolyType(Symbol[] tparams, Type restp):
	    if (tparams.length == 0 && !(restp instanceof Type.MethodType)) {
		return gen.Apply(asMethod(tree1), new Tree[0]);
	    } else {
		return tree1;
	    }
	default:
	    if (tree1.symbol().isDefParameter()) {
		tree1.type = global.definitions.FUNCTION_TYPE(
		    Type.EMPTY_ARRAY, tree1.type.widen());
		return gen.Apply(gen.Select(tree1, global.definitions.FUNCTION_APPLY(0)));
	    } else {
		return tree1;
	    }
	}
    }

    /** - uncurry all symbol and tree types (@see UnCurryPhase)
     *  - for every curried parameter list:  (ps_1) ... (ps_n) ==> (ps_1, ..., ps_n)
     *  - for every curried application: f(args_1)...(args_n) ==> f(args_1, ..., args_n)
     *  - for every type application: f[Ts] ==> f[Ts]() unless followed by parameters
     *  - for every use of a parameterless function: f ==> f()  and  q.f ==> q.f()
     *  - for every def-parameter:  def x: T ==> x: () => T
     *  - for every use of a def-parameter: x ==> x.apply()
     *  - for every argument to a def parameter `def x: T':
     *      if argument is not a reference to a def parameter:
     *        convert argument `e' to (expansion of) `() => e'
     *  - for every argument list that corresponds to a repeated parameter
     *       (a_1, ..., a_n) => (Sequence(a_1, ..., a_n))
     */
    public Tree transform(Tree tree) {
        System.out.flush();
	//uncurry type and symbol
	Type prevtype = tree.type;
	if (prevtype != null) tree.type = descr.uncurry(prevtype);
        switch (tree) {
	case ClassDef(_, _, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Template impl):
	    return copy.ClassDef(
		tree, tree.symbol(), tparams,
		uncurry(transform(vparams, tree.symbol())),
		tpe,
		transform(impl, tree.symbol()));

	case DefDef(_, _, AbsTypeDef[] tparams, ValDef[][] vparams, Tree tpe, Tree rhs):
	    Symbol sym = tree.symbol();
	    if (descr.isUnaccessedConstant(sym))
                return gen.mkUnitLit(tree.pos);
	    Tree rhs1 = transform(rhs, sym);
	    return copy.DefDef(
		tree, sym, tparams, uncurry(transform(vparams, sym)), tpe, rhs1);

	case ValDef(_, _, Tree tpe, Tree rhs):
	    Symbol sym = tree.symbol();
	    if (descr.isUnaccessedConstant(sym))
                return gen.mkUnitLit(tree.pos);
	    if (sym.isDefParameter()) {
		Type newtype = global.definitions.FUNCTION_TYPE(Type.EMPTY_ARRAY, tpe.type);
		Tree tpe1 = gen.mkType(tpe.pos, newtype);
                return copy.ValDef(tree, tpe1, rhs).setType(newtype);
	    } else {
		return super.transform(tree);
	    }

	case TypeApply(Tree fn, Tree[] args):
	    Tree tree1 = asMethod(super.transform(tree));
	    return gen.Apply(tree1, new Tree[0]);


	case Apply(Tree fn, Tree[] args):
	    // f(x)(y) ==> f(x, y)
	    // argument to parameterless function e => ( => e)
	    Type ftype = fn.type;
	    Tree fn1 = transform(fn);
            boolean myInArray = TreeInfo.methSymbol(fn1) == global.definitions.PREDEF_ARRAY();
            inArray = myInArray;
	    Tree[] args1 = transformArgs(tree.pos, args, ftype);
            if( myInArray )
                  switch( fn1 ) {
                  case Apply( TypeApply( Select(Tree fn2, _), Tree[] targs), _ ):
                        return gen.mkBlock(args1[0].pos, fn2, args1[0]);
                  default:
                        assert false : "dead";
                  }
	    if (TreeInfo.methSymbol(fn1) == global.definitions.ANY_MATCH &&
		!(args1[0] instanceof Tree.Visitor)) {
		switch (TreeInfo.methPart(fn1)) {
		case Select(Tree qual, Name name):
		    assert name == Names.match;
		    return gen.postfixApply(qual, args1[0], currentOwner);
		default:
		    throw new ApplicationError("illegal prefix for match: " + tree);
		}

	    } else {
		switch (fn1) {
		case Apply(Tree fn2, Tree[] args2):
		    Tree[] newargs = new Tree[args1.length + args2.length];
		    System.arraycopy(args2, 0, newargs, 0, args2.length);
		    System.arraycopy(args1, 0, newargs, args2.length, args1.length);
		    return copy.Apply(tree, fn2, newargs);
		default:
		    return copy.Apply(tree, fn1, args1);
		}
	    }

	case Select(_, _):
	    return applyDef(super.transform(tree));

	case Ident(Name name):
	    if (name == TypeNames.WILDCARD_STAR) {
		unit.error(tree.pos, " argument does not correspond to `*'-parameter");
		return tree;
	    } else if (tree.symbol() == global.definitions.PATTERN_WILDCARD) {
		return tree;
	    } else {
		return applyDef(super.transform(tree));
	    }

        case CaseDef(Tree pat, Tree guard, Tree body):
              inPattern = true;
              Tree pat1 = transform( pat );
              inPattern = false;
              Tree guard1 = transform( guard );
              Tree body1 = transform( body );
              return copy.CaseDef( tree, pat1, guard1, body1 );

	default:
	    return super.transform(tree);
	}
    }

      boolean inPattern = false;
      boolean inArray = false;
//    java.util.HashSet visited = new java.util.HashSet();//DEBUG

    /** Transform arguments `args' to method with type `methtype'.
     */
    private Tree[] transformArgs(int pos, Tree[] args, Type methtype) {
//	if (args.length != 0 && visited.contains(args)) {
//	    new scalac.ast.printer.TextTreePrinter().print("dup args: ").print(make.Block(pos, args)).println().end();//DEBUG
//	    assert false;
//	}
//	visited.add(args);//DEBUG

	switch (methtype) {
	case MethodType(Symbol[] params, _):
	    if (params.length > 0 &&
		(params[params.length-1].flags & REPEATED) != 0) {
		args = toSequence(pos, params, args);
	    }
	    Tree[] args1 = args;
	    for (int i = 0; i < args.length; i++) {
		Tree arg = args[i];
		Tree arg1 = transformArg(arg, params[i]);
		if (arg1 != arg && args1 == args) {
		    args1 = new Tree[args.length];
		    System.arraycopy(args, 0, args1, 0, i);
		}
		args1[i] = arg1;
	    }
	    return args1;
	case PolyType(_, Type restp):
	    return transformArgs(pos, args, restp);
	default:
	    if (args.length == 0) return args; // could be arguments of nullary case pattern
	    else throw new ApplicationError(methtype);
	}
    }

    /** converts `a_1,...,a_n' to Seq(a_1,...,a_n)
     *  if a_n is an escaped sequence x:_*, takes care of escaping
     *
     *  precondition: params[params.length-1].flags & REPEATED != 0
     */
    private Tree[] toSequence( int pos, Symbol[] params, Tree[] args ) {
	Tree[] result = new Tree[params.length];
        System.arraycopy(args, 0, result, 0, params.length - 1);
        /*
	for (int i = 0; i < params.length - 1; i++)
	    result[i] = args[i];
        */
	assert (args.length != params.length
		|| !(args[params.length-1] instanceof Tree.Sequence)
		|| TreeInfo.isSequenceValued(args[params.length-1]));
 	if (args.length == params.length) {
            switch (args[params.length-1]) {
            case Typed(Tree arg, Ident(TypeNames.WILDCARD_STAR)): // seq:_* escape
		result[params.length-1] = arg;
		return result;
            }
        }
	Tree[] args1 = args;
	if (params.length != 1) {
	    args1 = new Tree[args.length - (params.length - 1)];
	    System.arraycopy(args, params.length - 1, args1, 0, args1.length);
	}
        Type theType = params[params.length-1].type();
        if( inPattern )
              result[params.length-1] =
                    make.Sequence(pos, args1).setType( theType );
        else if( inArray ) {
              result[params.length-1] = gen.mkNewArray(pos,
                                                       theType.typeArgs()[0],
                                                       args1,
                                                       currentOwner);

        } else
              result[params.length-1] =
                    gen.mkNewList(pos,
                                  theType.typeArgs()[0],
                                  args1);
	return result;
    }

    /** for every argument to a def parameter `def x: T':
     *    if argument is not a reference to a def parameter:
     *      convert argument `e' to (expansion of) `() => e'
     */
    private Tree transformArg(Tree arg, Symbol formal) {
	if ((formal.flags & DEF) != 0) {
	    Symbol sym = arg.symbol();
	    if (sym != null && (sym.flags & DEF) != 0) {
		Tree arg1 = transform(arg);
		switch (arg1) {
		case Apply(Select(Tree qual, Name name), Tree[] args1):
		    assert name == Names.apply && args1.length == 0;
		    return qual;
		default:
		    System.err.println(arg1);//debug
		    throw new ApplicationError();
		}
	    }
	    return transform(
		gen.mkUnitFunction(arg, descr.uncurry(arg.type.widen()), currentOwner));
	} else {
	    return transform(arg);
	}
    }
}