Ejemplo n.º 1
0
  public int compareTo(Expression e) {
    if (this == e) return 0;

    if (e instanceof Aggregate) {
      Aggregate o = (Aggregate) e;

      int d = type.getName().compareTo(o.type.getName());
      if (d != 0) {
        return d;
      }

      d = compare(expressions, o.expressions);
      if (d != 0) {
        return d;
      }

      if (equal(function, o.function)) {
        return 0;
      } else {
        return order - o.order;
      }
    } else {
      return getClass().getName().compareTo(e.getClass().getName());
    }
  }
Ejemplo n.º 2
0
 public void visitAndExpr(OpAnd and) {
   if (predicates > 0) {
     Expression parent = and.getParent();
     if (!(parent instanceof PathExpr)) {
       LOG.warn("Parent expression of boolean operator is not a PathExpr: " + parent);
       return;
     }
     PathExpr path;
     Predicate predicate;
     if (parent instanceof Predicate) {
       predicate = (Predicate) parent;
       path = predicate;
     } else {
       path = (PathExpr) parent;
       parent = path.getParent();
       if (!(parent instanceof Predicate) || path.getLength() > 1) {
         LOG.warn(
             "Boolean operator is not a top-level expression in the predicate: "
                 + parent.getClass().getName());
         return;
       }
       predicate = (Predicate) parent;
     }
     if (LOG.isTraceEnabled())
       LOG.trace("Rewriting boolean expression: " + ExpressionDumper.dump(and));
     hasOptimized = true;
     LocationStep step = (LocationStep) predicate.getParent();
     Predicate newPred = new Predicate(context);
     newPred.add(and.getRight());
     step.insertPredicate(predicate, newPred);
     path.replaceExpression(and, and.getLeft());
   }
 }
Ejemplo n.º 3
0
  private Expression expressionSimplificationTester(
      Expression exp,
      java.lang.Class expectedClass,
      Type expectedType,
      int result,
      boolean testNumericValue) {
    ExpressionVisitor cfv = new ExpressionVisitor(new SymbolTable());
    Expression simplifiedExp = exp.accept(cfv);

    assertEquals(expectedType, simplifiedExp.type);
    assertEquals(expectedClass, simplifiedExp.getClass());
    if (testNumericValue) assertEquals(result, ((Number) simplifiedExp).id);
    return simplifiedExp;
  }
Ejemplo n.º 4
0
  public static BytecodeExpr transformLogicalExpression(
      Expression exp, CompilerTransformer compiler, Label label, boolean onTrue) {
    if (exp instanceof StaticMethodCallExpression) {
      StaticMethodCallExpression smce = (StaticMethodCallExpression) exp;
      MethodCallExpression mce =
          new MethodCallExpression(
              new ClassExpression(smce.getOwnerType()), smce.getMethod(), smce.getArguments());
      mce.setSourcePosition(smce);
      return transformLogicalExpression(mce, compiler, label, onTrue);
    }

    ExprTransformer t = transformers.get(exp.getClass());
    return t.transformLogical(exp, compiler, label, onTrue);
  }
Ejemplo n.º 5
0
  public static Expression transformExpression(final Expression exp, CompilerTransformer compiler) {
    if (exp instanceof BytecodeExpression) {
      if (exp instanceof BytecodeExpr) return exp;

      return new BytecodeExpr(exp, exp.getType()) {
        protected void compile(MethodVisitor mv) {
          ((BytecodeExpression) exp).visit(mv);
        }
      };
    }

    ExprTransformer t = transformers.get(exp.getClass());
    if (t == null) return compiler.transformImpl(exp);

    return t.transform(exp, compiler);
  }
 public Expression transform(Expression exp) {
   if (exp == null) return null;
   if (exp.getClass() == VariableExpression.class) {
     return transformVariableExpression((VariableExpression) exp);
   }
   if (exp.getClass() == BinaryExpression.class) {
     return transformBinaryExpression((BinaryExpression) exp);
   }
   if (exp.getClass() == PropertyExpression.class) {
     return transformPropertyExpression((PropertyExpression) exp);
   }
   if (exp.getClass() == MethodCallExpression.class) {
     return transformMethodCallExpression((MethodCallExpression) exp);
   }
   if (exp.getClass() == ClosureExpression.class) {
     return transformClosureExpression((ClosureExpression) exp);
   }
   if (exp.getClass() == ConstructorCallExpression.class) {
     return transformConstructorCallExpression((ConstructorCallExpression) exp);
   }
   if (exp.getClass() == ArgumentListExpression.class) {
     Expression result = exp.transformExpression(this);
     if (inPropertyExpression) {
       foundArgs = result;
     }
     return result;
   }
   if (exp instanceof ConstantExpression) {
     Expression result = exp.transformExpression(this);
     if (inPropertyExpression) {
       foundConstant = result;
     }
     if (inAnnotation && exp instanceof AnnotationConstantExpression) {
       ConstantExpression ce = (ConstantExpression) result;
       if (ce.getValue() instanceof AnnotationNode) {
         // replicate a little bit of AnnotationVisitor here
         // because we can't wait until later to do this
         AnnotationNode an = (AnnotationNode) ce.getValue();
         Map<String, Expression> attributes = an.getMembers();
         for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
           Expression attrExpr = transform(entry.getValue());
           entry.setValue(attrExpr);
         }
       }
     }
     return result;
   }
   return exp.transformExpression(this);
 }
 @Override
 public Expression visit(Sequence sequence) {
   Sequence result = new Sequence();
   for (Expression exp : sequence.getSequence()) {
     if (exp.getClass() == Choice.class) {
       String name = "seq" + Integer.toString(count);
       count++;
       Nonterminal nt = new Nonterminal(name);
       Expression expr = exp.accept(this);
       newRules.put(nt, expr);
       result.addExpr(nt);
     } else {
       result.addExpr(exp.accept(this));
     }
   }
   return result;
 }