KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xquery > util > OrderedMap


1 package gnu.xquery.util;
2 import gnu.lists.*;
3 import gnu.mapping.*;
4 import gnu.expr.*;
5 import gnu.bytecode.*;
6
7 /** A procedure used to represent a FLWOR expression with
8  * an {@code order by} clause.
9  * ({@link gnu.kawa.functions.ValuesMap} is used for FLWOR expression
10  * that don't have an {@code order by} clause.)
11  *
12  * As returned by the parser:
13  * <pre>
14  * for $x1 in exp1, $x2 in exp2 where cond order by comparator1 ... return body
15  * </pre>
16  * is represented as
17  * <pre>
18  * ordered-map(tuple-sequence, body-function,
19  * comparator-function1, flags1, collation1, ...)
20  * </pre>
21  * Here tuple-sequence is an expression that returns a sequence of tuples,
22  * which are currently implemnted as Java Object[] arrays.
23  * After inlining we get:
24  * <pre>
25  * ordered-map(tuple-sequence.
26  * OrderedTuples.make$V(body-function,
27  * new Object[]{comparator-function1, flags1, collation1, ...}))
28  * </pre>
29  *
30  * A future optimization would be to create an instance of a new sub-class
31  * of OrderedTuples. Then the body-function and comparator-functions
32  * could be compiled as methods to that class. That wins especially
33  * if it saves us having to create extra frame classes.
34  */

35
36 public class OrderedMap extends MethodProc
37   implements CanInline, Inlineable
38 {
39   public static final OrderedMap orderedMap = new OrderedMap();
40
41   public static Object JavaDoc[] makeTuple$V (Object JavaDoc[] values)
42   {
43     return values;
44   }
45
46   static final ClassType typeTuples
47     = ClassType.make("gnu.xquery.util.OrderedTuples");
48
49   public Expression inline (ApplyExp exp, ExpWalker walker)
50   {
51     Expression[] args = exp.getArgs();
52     if (args.length > 2)
53       {
54         Expression[] rargs = new Expression[args.length-1];
55         System.arraycopy(args, 1, rargs, 0, rargs.length);
56         Expression[] xargs = new Expression[2];
57         Method makeTupleMethod = typeTuples.getDeclaredMethod("make$V", 2);
58         xargs[0] = args[0];
59         xargs[1] = new ApplyExp(makeTupleMethod, rargs);
60         return new ApplyExp(this, xargs);
61       }
62     return exp;
63   }
64
65   public void apply (CallContext ctx) throws Throwable JavaDoc
66   {
67     Consumer out = ctx.consumer;
68     Object JavaDoc[] args = ctx.getArgs();
69     Object JavaDoc values = args[0];
70     OrderedTuples tuples;
71     if (args.length == 2)
72       {
73         tuples = (OrderedTuples) args[1];
74       }
75     else
76       {
77         Object JavaDoc[] comps = new Object JavaDoc[args.length-2];
78         System.arraycopy(args, 2, comps, 0, comps.length);
79         tuples = OrderedTuples.make$V((Procedure) args[1], comps);
80       }
81     Values.writeValues(values, tuples);
82     tuples.run$X(ctx);
83   }
84
85   public void compile (ApplyExp exp, Compilation comp, Target target)
86   {
87     Expression[] args = exp.getArgs();
88     if (args.length != 2)
89       {
90         ApplyExp.compile(exp, comp, target);
91         return;
92       }
93     CodeAttr code = comp.getCode();
94     Scope scope = code.pushScope();
95     Variable consumer = scope.addVariable(code, typeTuples, null);
96     args[1].compile(comp, Target.pushValue(typeTuples));
97     code.emitStore(consumer);
98     ConsumerTarget ctarget = new ConsumerTarget(consumer);
99     args[0].compile(comp, ctarget);
100     Method mm = typeTuples.getDeclaredMethod("run$X", 1);
101     code.emitLoad(consumer);
102     PrimProcedure.compileInvoke(comp, mm, target, exp.isTailCall(),
103                                 182/*invokevirtual*/, Type.pointer_type);
104     code.popScope();
105   }
106
107   public gnu.bytecode.Type getReturnType (Expression[] args)
108   {
109     return Type.pointer_type; // FIXME
110
}
111 }
112
Popular Tags