OrderedMap.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package gnu.xquery.util;
  2. import gnu.mapping.*;
  3. import gnu.expr.*;
  4. import gnu.bytecode.*;
  5. /** A procedure used to represent a FLWOR expression with
  6. * an {@code order by} clause.
  7. * ({@link gnu.kawa.functions.ValuesMap} is used for FLWOR expression
  8. * that don't have an {@code order by} clause.)
  9. *
  10. * As returned by the parser:
  11. * <pre>
  12. * for $x1 in exp1, $x2 in exp2 where cond order by comparator1 ... return body
  13. * </pre>
  14. * is represented as
  15. * <pre>
  16. * ordered-map(tuple-sequence, body-function,
  17. * comparator-function1, flags1, collation1, ...)
  18. * </pre>
  19. * Here tuple-sequence is an expression that returns a sequence of tuples,
  20. * which are currently implemnted as Java Object[] arrays.
  21. * After inlining we get:
  22. * <pre>
  23. * ordered-map(tuple-sequence.
  24. * OrderedTuples.make$V(body-function,
  25. * new Object[]{comparator-function1, flags1, collation1, ...}))
  26. * </pre>
  27. *
  28. * A future optimization would be to create an instance of a new sub-class
  29. * of OrderedTuples. Then the body-function and comparator-functions
  30. * could be compiled as methods to that class. That wins especially
  31. * if it saves us having to create extra frame classes.
  32. */
  33. public class OrderedMap extends MethodProc
  34. implements Inlineable
  35. {
  36. public static final OrderedMap orderedMap = new OrderedMap();
  37. static {
  38. orderedMap.setProperty(Procedure.validateApplyKey,
  39. "gnu.xquery.util.CompileMisc:validateApplyOrderedMap");
  40. }
  41. public static Object[] makeTuple$V (Object[] values)
  42. {
  43. return values;
  44. }
  45. public void apply (CallContext ctx) throws Throwable
  46. {
  47. Object[] args = ctx.getArgs();
  48. Object values = args[0];
  49. OrderedTuples tuples;
  50. if (args.length == 2)
  51. {
  52. tuples = (OrderedTuples) args[1];
  53. }
  54. else
  55. {
  56. Object[] comps = new Object[args.length-2];
  57. System.arraycopy(args, 2, comps, 0, comps.length);
  58. tuples = OrderedTuples.make$V((Procedure) args[1], comps);
  59. }
  60. Values.writeValues(values, tuples);
  61. tuples.run$X(ctx);
  62. }
  63. public void compile (ApplyExp exp, Compilation comp, Target target)
  64. {
  65. CompileMisc.compileOrderedMap(exp, comp, target, this);
  66. }
  67. public gnu.bytecode.Type getReturnType (Expression[] args)
  68. {
  69. return Type.pointer_type; // FIXME
  70. }
  71. }