private ConstantValue evaluateLogicalExpr(
        Expression lhs, Expression rhs, Type resultType, LogicalBinaryOperation operation) {
      // Evaluate the first subexpression
      final ConstantValue valueLhs = lhs.accept(this, null);

      // Get the factory for the result type
      final ConstantType resultConstantType = typeFactory.newConstantType(resultType);
      checkArgument(
          resultConstantType.getType() == ConstantType.Type.SIGNED_INTEGER,
          "result of logical AND or logical OR expression has not a signed integer type");
      final boolean resultValue;

      /* Determine the result - we use the fact the Java '&&' and '||'
      operators are evaluated lazily. */
      switch (operation) {
        case LOGICAL_AND:
          resultValue = valueLhs.logicalValue() && rhs.accept(this, null).logicalValue();
          break;
        case LOGICAL_OR:
          resultValue = valueLhs.logicalValue() || rhs.accept(this, null).logicalValue();
          break;
        default:
          throw new RuntimeException("unexpected logical binary operation '" + operation + "'");
      }

      return UnsignedIntegerConstantValue.getLogicalValue(resultValue).castTo(resultConstantType);
    }
Beispiel #2
0
  @Override
  public void accept(Visitor visitor) {
    a.accept(visitor);
    b.accept(visitor);

    visitor.visit(this);
  }
Beispiel #3
0
 public void childrenAccept(Visitor visitor) {
   condition.accept(visitor);
   if (ifTrue != null) {
     ifTrue.accept(visitor);
   }
   if (ifFalse != null) {
     ifFalse.accept(visitor);
   }
 }
 @Override
 public StorageResults fetch(Expression userQuery) {
   Expression expression = userQuery.normalize();
   Matcher matcher = expression.accept(new MatcherCreator());
   List<DataRecord> matchRecords = new LinkedList<DataRecord>();
   for (DataRecord dataRecord : storage) {
     if (matcher.match(dataRecord)) {
       matchRecords.add(dataRecord);
     }
   }
   List<DataRecord> filteredRecords = expression.accept(new Filter(matchRecords));
   return new InMemoryStorageResults(filteredRecords);
 }
    private ConstantValue evaluateBinaryExpr(
        Expression lhs, Expression rhs, Type resultType, ArithmeticBinaryOperation operation) {
      // Evaluate subexpressions
      ConstantValue valueLhs = lhs.accept(this, null);
      ConstantValue valueRhs = rhs.accept(this, null);

      // Convert to type implied by usual arithmetic conversions
      final ArithmeticType commonType =
          TypeUtils.doUsualArithmeticConversions(
              (ArithmeticType) lhs.getType().get(), (ArithmeticType) rhs.getType().get());
      final ConstantType commonConstantType = typeFactory.newConstantType(commonType);
      final ConstantType resultConstantType = typeFactory.newConstantType(resultType);
      valueLhs = valueLhs.castTo(commonConstantType);
      valueRhs = valueRhs.castTo(commonConstantType);

      // Perform the operation
      switch (operation) {
        case ADDITION:
          return valueLhs.add(valueRhs);
        case SUBTRACTION:
          return valueLhs.subtract(valueRhs);
        case MULTIPLICATION:
          return valueLhs.multiply(valueRhs);
        case DIVISION:
          return valueLhs.divide(valueRhs);
        case REMAINDER:
          return valueLhs.remainder(valueRhs);
        case BITWISE_AND:
          return valueLhs.bitwiseAnd(valueRhs);
        case BITWISE_OR:
          return valueLhs.bitwiseOr(valueRhs);
        case BITWISE_XOR:
          return valueLhs.bitwiseXor(valueRhs);
        case LESS:
          return valueLhs.less(valueRhs).castTo(resultConstantType);
        case LESS_OR_EQUAL:
          return valueLhs.lessOrEqual(valueRhs).castTo(resultConstantType);
        case GREATER:
          return valueLhs.greater(valueRhs).castTo(resultConstantType);
        case GREATER_OR_EQUAL:
          return valueLhs.greaterOrEqual(valueRhs).castTo(resultConstantType);
        case EQUAL:
          return valueLhs.equalTo(valueRhs).castTo(resultConstantType);
        case NOT_EQUAL:
          return valueLhs.notEqualTo(valueRhs).castTo(resultConstantType);
        default:
          throw new RuntimeException("unexpected arithmetic binary operation '" + operation + "'");
      }
    }
 @Test
 public void testExpression() {
   Expression expr = Template.parseExpression("5 + (a ? b : 10)");
   Template<Expression> template = Template.of(expr);
   Expression replacement = new FloatingPointLiteral().astDoubleValue(0.5);
   template.setStartPosition(10);
   template.replaceExpression("b", replacement, new Position(20, 30));
   Expression finish = template.finish();
   StructureFormatter sf = StructureFormatter.formatterWithPositions();
   finish.accept(new SourcePrinter(sf));
   assertEquals(
       literal(
           "[I BinaryExpression + (10-30)]",
           "    PROPERTY: operator = +",
           "    left: [I IntegralLiteral 5 (10-10)]",
           "        PROPERTY: value = 5",
           "    right: [I InlineIfExpression (10-30)]",
           "        condition: [I VariableReference (10-10)]",
           "            [I Identifier a (10-10)]",
           "                PROPERTY: name = a",
           "        ifTrue: [I FloatingPointLiteral 0.5 (20-30)]",
           "            PROPERTY: value = 0.5",
           "        ifFalse: [I IntegralLiteral 10 (30-30)]",
           "            PROPERTY: value = 10"),
       sf.finish());
 }
 @Override
 public void accept(IRVisitor visitor) {
   for (Expression e : expressions) {
     e.accept(visitor);
   }
   visitor.visit(this);
 }
 /*
  * @see ASTVisitor#visit(SuperMethodInvocation)
  */
 @Override
 public boolean visit(SuperMethodInvocation node) {
   if (node.getQualifier() != null) {
     node.getQualifier().accept(this);
     this.fBuffer.append("."); // $NON-NLS-1$
   }
   this.fBuffer.append("super."); // $NON-NLS-1$
   if (node.getAST().apiLevel() >= JLS3) {
     if (!node.typeArguments().isEmpty()) {
       this.fBuffer.append("<"); // $NON-NLS-1$
       for (Iterator<Type> it = node.typeArguments().iterator(); it.hasNext(); ) {
         Type t = it.next();
         t.accept(this);
         if (it.hasNext()) {
           this.fBuffer.append(","); // $NON-NLS-1$
         }
       }
       this.fBuffer.append(">"); // $NON-NLS-1$
     }
   }
   node.getName().accept(this);
   this.fBuffer.append("("); // $NON-NLS-1$
   for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(")"); // $NON-NLS-1$
   return false;
 }
 @Override
 public void visit(ExpressionList el) {
   for (Iterator iter = el.getExpressions().iterator(); iter.hasNext(); ) {
     Expression expression = (Expression) iter.next();
     expression.accept(this);
   }
 }
 public Object visitStmtAtomicBlock(StmtAtomicBlock stmt) {
   if (stmt.isCond()) {
     Expression ie = stmt.getCond();
     ie.accept(new UpgradeStarToInt(this, TypePrimitive.bittype, nres));
   }
   return super.visitStmtAtomicBlock(stmt);
 }
 /*
  * @see ASTVisitor#visit(ClassInstanceCreation)
  */
 @Override
 public boolean visit(ClassInstanceCreation node) {
   if (node.getExpression() != null) {
     node.getExpression().accept(this);
     this.fBuffer.append("."); // $NON-NLS-1$
   }
   this.fBuffer.append("new "); // $NON-NLS-1$
   if (node.getAST().apiLevel() >= JLS3) {
     if (!node.typeArguments().isEmpty()) {
       this.fBuffer.append("<"); // $NON-NLS-1$
       for (Iterator<Type> it = node.typeArguments().iterator(); it.hasNext(); ) {
         Type t = it.next();
         t.accept(this);
         if (it.hasNext()) {
           this.fBuffer.append(","); // $NON-NLS-1$
         }
       }
       this.fBuffer.append(">"); // $NON-NLS-1$
     }
     node.getType().accept(this);
   }
   this.fBuffer.append("("); // $NON-NLS-1$
   for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(")"); // $NON-NLS-1$
   if (node.getAnonymousClassDeclaration() != null) {
     node.getAnonymousClassDeclaration().accept(this);
   }
   return false;
 }
 public void accept(IASTVisitor visitor) {
   boolean visitChildren = visitor.visit(this);
   if (visitChildren) {
     expr.accept(visitor);
   }
   visitor.endVisit(this);
 }
 /*
  * @see ASTVisitor#visit(ConstructorInvocation)
  */
 @Override
 public boolean visit(ConstructorInvocation node) {
   if (node.getAST().apiLevel() >= JLS3) {
     if (!node.typeArguments().isEmpty()) {
       this.fBuffer.append("<"); // $NON-NLS-1$
       for (Iterator<Type> it = node.typeArguments().iterator(); it.hasNext(); ) {
         Type t = it.next();
         t.accept(this);
         if (it.hasNext()) {
           this.fBuffer.append(","); // $NON-NLS-1$
         }
       }
       this.fBuffer.append(">"); // $NON-NLS-1$
     }
   }
   this.fBuffer.append("this("); // $NON-NLS-1$
   for (Iterator<Expression> it = node.arguments().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append(");"); // $NON-NLS-1$
   return false;
 }
Beispiel #14
0
  public void visit(MethodCall p) throws ParseTreeException {
    if (isSameObject(p, original_methodcall)) {
      Expression expr = p.getReferenceExpr();
      TypeName reftype = p.getReferenceType();

      if (expr != null) {
        if (expr instanceof Leaf
            || expr instanceof ArrayAccess
            || expr instanceof FieldAccess
            || expr instanceof MethodCall
            || expr instanceof Variable) {
          expr.accept(this);
        } else {
          writeParenthesis(expr);
        }
      } else if (reftype != null) {
        reftype.accept(this);
      }

      // -------------------------------------------------------------
      mutated_line = line_num;
      out.print(mutant);
      writeLog(
          removeNewline(
              p.toString()
                  + " =>  "
                  + p.toString().substring(0, p.toString().length() - ".clone()".length())));
      // -------------------------------------------------------------

    } else {
      super.visit(p);
    }
  }
 @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;
 }
Beispiel #16
0
 public void visit(ArrayInitializerExpr n, Object arg) {
   if (n.getValues() != null) {
     for (Iterator<Expression> i = n.getValues().iterator(); i.hasNext(); ) {
       Expression expr = i.next();
       expr.accept(this, arg);
       if (i.hasNext()) {}
     }
   }
 }
 /*
  * @see ASTVisitor#visit(ForStatement)
  */
 @Override
 public boolean visit(ForStatement node) {
   this.fBuffer.append("for ("); // $NON-NLS-1$
   for (Iterator<Expression> it = node.initializers().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
   }
   this.fBuffer.append("; "); // $NON-NLS-1$
   if (node.getExpression() != null) {
     node.getExpression().accept(this);
   }
   this.fBuffer.append("; "); // $NON-NLS-1$
   for (Iterator<Expression> it = node.updaters().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
   }
   this.fBuffer.append(") "); // $NON-NLS-1$
   node.getBody().accept(this);
   return false;
 }
Beispiel #18
0
 public void visit(ForStmt n, Object arg) {
   if (n.getInit() != null) {
     for (Iterator<Expression> i = n.getInit().iterator(); i.hasNext(); ) {
       Expression e = i.next();
       e.accept(this, arg);
       if (i.hasNext()) {}
     }
   }
   if (n.getCompare() != null) {
     n.getCompare().accept(this, arg);
   }
   if (n.getUpdate() != null) {
     for (Iterator<Expression> i = n.getUpdate().iterator(); i.hasNext(); ) {
       Expression e = i.next();
       e.accept(this, arg);
       if (i.hasNext()) {}
     }
   }
   n.getBody().accept(this, arg);
 }
Beispiel #19
0
 @Override
 public Void visitReturnStatement(ReturnStatement node) {
   Expression expression = node.getExpression();
   if (expression == null) {
     writer.print("return;");
   } else {
     writer.print("return ");
     expression.accept(this);
     writer.print(";");
   }
   return null;
 }
Beispiel #20
0
 public void visit(MethodCallExpr n, Object arg) {
   if (n.getScope() != null) {
     n.getScope().accept(this, arg);
   }
   printTypeArgs(n.getTypeArgs(), arg);
   if (n.getArgs() != null) {
     for (Iterator<Expression> i = n.getArgs().iterator(); i.hasNext(); ) {
       Expression e = i.next();
       e.accept(this, arg);
       if (i.hasNext()) {}
     }
   }
 }
 /*
  * @see ASTVisitor#visit(ArrayInitializer)
  */
 @Override
 public boolean visit(ArrayInitializer node) {
   this.fBuffer.append("{"); // $NON-NLS-1$
   for (Iterator<Expression> it = node.expressions().iterator(); it.hasNext(); ) {
     Expression e = it.next();
     e.accept(this);
     if (it.hasNext()) {
       this.fBuffer.append(","); // $NON-NLS-1$
     }
   }
   this.fBuffer.append("}"); // $NON-NLS-1$
   return false;
 }
 /*
  * Visit the AST expression and get the ParseTree Expression.
  * This is used by the individual visits when parsing a tree.
  * It passes to the top method (createExpression), which can
  * handle the InvalidExpressionException.
  *
  * If any visit doesn't return an expression, then an invalid
  * expression exception will be thrown to indicate this. If the
  * incoming expression is <code>null</code>, then return of <code>null</code> is ok because
  * this would be for an optional expression which didn't exist.
  *
  * @return The new ParseTree Expression or <code>null</code> if incoming expression was null.
  *
  * @see createExpression(org.eclipse.jdt.core.dom.Expression)
  * @exception InvalidExpressionException
  * @since 1.0.0
  */
 protected final PTExpression perform(Expression astExpression) {
   if (astExpression != null) {
     expression = null;
     astExpression.accept(this);
     if (expression == null)
       throw new InvalidExpressionException(
           MessageFormat.format(
               WorkbenchUtilityMessages.ParseTreeCreationFromAST_ExpressionTooComplicated_EXC_,
               new Object[] {astExpression.toString()}));
     return expression;
   } else return null; // This is ok. It means an optional expression was being processed and the
   // expression didn't exist.
 }
  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;
  }
Beispiel #24
0
  public void visit(ArrayCreationExpr n, Object arg) {
    n.getType().accept(this, arg);
    printTypeArgs(n.getTypeArgs(), arg);

    if (n.getDimensions() != null) {
      for (Expression dim : n.getDimensions()) {
        dim.accept(this, arg);
      }
      for (int i = 0; i < n.getArrayCount(); i++) {}
    } else {
      for (int i = 0; i < n.getArrayCount(); i++) {}
      n.getInitializer().accept(this, arg);
    }
  }
Beispiel #25
0
  public void visit(CaseExpression caseExpression) throws Exception {
    if (caseExpression.getSwitchExpression() != null) {
      caseExpression.getSwitchExpression().accept(this);
    }
    if (caseExpression.getWhenClauses() != null) {
      for (Expression exp : caseExpression.getWhenClauses()) {
        exp.accept(this);
      }
    }

    if (caseExpression.getElseExpression() != null) {
      caseExpression.getElseExpression().accept(this);
    }
  }
    private ConstantValue evaluateShiftExpr(
        Expression lhs, Expression rhs, Type resultType, ShiftOperation operation) {
      // Evaluate subexpressions
      ConstantValue valueLhs = lhs.accept(this, null);
      ConstantValue valueRhs = rhs.accept(this, null);

      // Make integer promotions
      final ConstantType leftPromotedType = typeFactory.newConstantType(resultType);
      final ConstantType rightPromotedType =
          typeFactory.newConstantType(rhs.getType().get().promote());
      valueLhs = valueLhs.castTo(leftPromotedType);
      valueRhs = valueRhs.castTo(rightPromotedType);

      // Perform the operation
      switch (operation) {
        case LEFT_SHIFT:
          return valueLhs.shiftLeft(valueRhs);
        case RIGHT_SHIFT:
          return valueLhs.shiftRight(valueRhs);
        default:
          throw new RuntimeException("unexpected shift operation kind '" + operation + "'");
      }
    }
Beispiel #27
0
 public void visit(ExplicitConstructorInvocationStmt n, Object arg) {
   if (n.isThis()) {
     printTypeArgs(n.getTypeArgs(), arg);
   } else {
     if (n.getExpr() != null) {
       n.getExpr().accept(this, arg);
     }
     printTypeArgs(n.getTypeArgs(), arg);
   }
   if (n.getArgs() != null) {
     for (Iterator<Expression> i = n.getArgs().iterator(); i.hasNext(); ) {
       Expression e = i.next();
       e.accept(this, arg);
       if (i.hasNext()) {}
     }
   }
 }
Beispiel #28
0
  public void visit(EnumConstantDeclaration n, Object arg) {
    if (n.getJavaDoc() != null) {
      n.getJavaDoc().accept(this, arg);
    }
    printMemberAnnotations(n.getAnnotations(), arg);

    if (n.getArgs() != null) {
      for (Iterator<Expression> i = n.getArgs().iterator(); i.hasNext(); ) {
        Expression e = i.next();
        e.accept(this, arg);
        if (i.hasNext()) {}
      }
    }

    if (n.getClassBody() != null) {
      printMembers(n.getClassBody(), arg);
    }
  }
 /*
  * @see ASTVisitor#visit(InfixExpression)
  */
 @Override
 public boolean visit(InfixExpression node) {
   node.getLeftOperand().accept(this);
   this.fBuffer.append(' '); // for cases like x= i - -1; or x= i++ + ++i;
   this.fBuffer.append(node.getOperator().toString());
   this.fBuffer.append(' ');
   node.getRightOperand().accept(this);
   final List<Expression> extendedOperands = node.extendedOperands();
   if (extendedOperands.size() != 0) {
     this.fBuffer.append(' ');
     for (Iterator<Expression> it = extendedOperands.iterator(); it.hasNext(); ) {
       this.fBuffer.append(node.getOperator().toString()).append(' ');
       Expression e = it.next();
       e.accept(this);
     }
   }
   return false;
 }
Beispiel #30
0
  public void visit(ObjectCreationExpr n, Object arg) {
    if (n.getScope() != null) {
      n.getScope().accept(this, arg);
    }

    printTypeArgs(n.getTypeArgs(), arg);
    n.getType().accept(this, arg);

    if (n.getArgs() != null) {
      for (Iterator<Expression> i = n.getArgs().iterator(); i.hasNext(); ) {
        Expression e = i.next();
        e.accept(this, arg);
        if (i.hasNext()) {}
      }
    }

    if (n.getAnonymousClassBody() != null) {
      printMembers(n.getAnonymousClassBody(), arg);
    }
  }