Beispiel #1
0
  /**
   * Evaluates a constant expression.
   *
   * <p>Should apply conversions to result.
   *
   * @param expr
   * @return
   * @throws SyntaxException
   * @throws UnsourcedException if expr is not a constant expression
   */
  private Value evaluateHelper(ExpressionNode expr) throws SyntaxException, UnsourcedException {
    if (expr instanceof AlignOfNode) {
      return alignofValue(((AlignOfNode) expr).getArgument().getType());
    } else if (expr instanceof ArrowNode) {
      ArrowNode arrowNode = (ArrowNode) expr;
      ExpressionNode structOrUnionPointer = arrowNode.getStructurePointer();
      IdentifierNode fieldIdentifier = arrowNode.getFieldName();
      Field field = (Field) fieldIdentifier.getEntity();
      Value structOrUnionValue = evaluateDereference(structOrUnionPointer);

      return evaluateMemberAccess(structOrUnionValue, field);
    } else if (expr instanceof CastNode) {
      CastNode castNode = (CastNode) expr;

      return evaluateCast(castNode.getCastType().getType(), evaluate(castNode.getArgument()));
    } else if (expr instanceof CharacterConstantNode) {
      return ((CharacterConstantNode) expr).getConstantValue();
    } else if (expr instanceof CompoundLiteralNode) {
      return evaluateCompoundLiteral((CompoundLiteralNode) expr);
    } else if (expr instanceof ConstantNode) {
      return ((ConstantNode) expr).getConstantValue();
    } else if (expr instanceof DotNode) {
      DotNode dotNode = (DotNode) expr;
      ExpressionNode structOrUnion = dotNode.getStructure();
      IdentifierNode fieldIdentifier = dotNode.getFieldName();
      Field field = (Field) fieldIdentifier.getEntity();
      Value structOrUnionValue = evaluate(structOrUnion);

      return evaluateMemberAccess(structOrUnionValue, field);
    } else if (expr instanceof OperatorNode) {
      OperatorNode opNode = (OperatorNode) expr;
      Operator operator = opNode.getOperator();

      if (operator == Operator.ADDRESSOF) return addressValue(opNode.getArgument(0));
      else { // evaluate arguments and call apply
        int numArgs = opNode.getNumberOfArguments();
        Value[] argValues = new Value[numArgs];
        Type type = expr.getInitialType();

        for (int i = 0; i < numArgs; i++) {
          ExpressionNode arg = opNode.getArgument(i);
          Value argValue = evaluate(arg);

          argValues[i] = applyConversions(arg, argValue);
        }
        return apply(type, operator, argValues);
      }
    } else if (expr instanceof SizeofNode) {
      return sizeofValue((((SizeofNode) expr).getArgument()).getType());
    } else if (expr instanceof StringLiteralNode) {
      return ((StringLiteralNode) expr).getConstantValue();
    } else if (expr instanceof EnumerationConstantNode) {
      IdentifierNode identNode = ((EnumerationConstantNode) expr).getName();
      Entity entity = identNode.getEntity();

      if (entity.getEntityKind() == EntityKind.ENUMERATOR) {
        return ((Enumerator) entity).getValue();
      }
      return null;
    }
    return null;
  }