Exemple #1
0
  @Override
  public Expr comp(final QueryContext ctx) throws QueryException {
    ts = checkUp(ts, ctx).comp(ctx);
    final Expr[] tmp = new Expr[cases.length];
    for (int i = 0; i < cases.length; ++i) tmp[i] = cases[i].expr;
    checkUp(ctx, tmp);

    // static condition: return branch in question
    if (ts.isValue()) {
      for (final TypeCase c : cases) {
        if (c.var.type == null || c.var.type.instance(ts.value(ctx)))
          return optPre(c.comp(ctx, (Value) ts).expr, ctx);
      }
    }

    // compile branches
    for (final TypeCase c : cases) c.comp(ctx);

    // return result if all branches are equal (e.g., empty)
    boolean eq = true;
    for (int i = 1; i < cases.length; ++i) {
      eq &= cases[i - 1].expr.sameAs(cases[i].expr);
    }
    if (eq) return optPre(null, ctx);

    // evaluate return type
    type = cases[0].type();
    for (int c = 1; c < cases.length; ++c) {
      type = type.intersect(cases[c].type());
    }
    return this;
  }
Exemple #2
0
  @Override
  public Expr compile(final QueryContext qc, final VarScope scp) throws QueryException {
    ts = ts.compile(qc, scp);
    // static condition: return branch in question
    if (ts.isValue()) {
      final Value val = ts.value(qc);
      for (final TypeCase tc : cases) {
        if (tc.matches(val)) return optPre(tc.compile(qc, scp, (Value) ts).expr, qc);
      }
    }
    // compile branches
    for (final TypeCase tc : cases) tc.compile(qc, scp);

    return optimize(qc, scp);
  }
Exemple #3
0
  @Override
  public Expr comp(final QueryContext ctx) throws QueryException {
    for (final Expr p : pred) checkUp(p, ctx);

    Expr e = this;
    for (int p = 0; p < pred.length; ++p) {
      Expr pr = pred[p].comp(ctx).compEbv(ctx);
      pr = Pos.get(CmpV.Op.EQ, pr, pr, input);

      if (pr.value()) {
        if (!pr.ebv(ctx, input).bool(input)) {
          ctx.compInfo(OPTREMOVE, desc(), pr);
          e = Empty.SEQ;
          break;
        }
        ctx.compInfo(OPTREMOVE, desc(), pr);
        pred = Array.delete(pred, p--);
      } else {
        pred[p] = pr;
      }
    }
    return e;
  }