[cfe-commits] r53463 - /cfe/trunk/lib/AST/ExprConstant.cpp
Chris Lattner
sabre at nondot.org
Fri Jul 11 11:11:30 PDT 2008
Author: lattner
Date: Fri Jul 11 13:11:29 2008
New Revision: 53463
URL: http://llvm.org/viewvc/llvm-project?rev=53463&view=rev
Log:
rearrange some code, no functionality changes.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=53463&r1=53462&r2=53463&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Jul 11 13:11:29 2008
@@ -17,14 +17,14 @@
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/Support/Compiler.h"
-
using namespace clang;
+using llvm::APSInt;
#define USE_NEW_EVALUATOR 0
-static bool CalcFakeICEVal(const Expr* Expr,
- llvm::APSInt& Result,
- ASTContext& Context) {
+static bool CalcFakeICEVal(const Expr *Expr,
+ llvm::APSInt &Result,
+ ASTContext &Context) {
// Calculate the value of an expression that has a calculatable
// value, but isn't an ICE. Currently, this only supports
// a very narrow set of extensions, but it can be expanded if needed.
@@ -51,22 +51,22 @@
return false;
}
+static bool EvaluatePointer(const Expr *E, APValue &Result, ASTContext &Ctx);
+static bool EvaluateInteger(const Expr *E, APSInt &Result, ASTContext &Ctx);
+
+
+//===----------------------------------------------------------------------===//
+// Pointer Evaluation
+//===----------------------------------------------------------------------===//
+
namespace {
class VISIBILITY_HIDDEN PointerExprEvaluator
: public StmtVisitor<PointerExprEvaluator, APValue> {
ASTContext &Ctx;
-
- PointerExprEvaluator(ASTContext &ctx)
- : Ctx(ctx) {}
-
public:
- static bool Evaluate(const Expr* E, APValue& Result, ASTContext &Ctx) {
- if (!E->getType()->isPointerType())
- return false;
- Result = PointerExprEvaluator(Ctx).Visit(const_cast<Expr*>(E));
- return Result.isLValue();
- }
+ PointerExprEvaluator(ASTContext &ctx) : Ctx(ctx) {}
+
APValue VisitStmt(Stmt *S) {
// FIXME: Remove this when we support more expressions.
printf("Unhandled pointer statement\n");
@@ -78,61 +78,17 @@
APValue VisitBinaryOperator(const BinaryOperator *E);
APValue VisitCastExpr(const CastExpr* E);
-
};
+} // end anonymous namespace
-class VISIBILITY_HIDDEN IntExprEvaluator
- : public StmtVisitor<IntExprEvaluator, APValue> {
- ASTContext &Ctx;
-
- IntExprEvaluator(ASTContext &ctx)
- : Ctx(ctx) {}
-
-public:
- static bool Evaluate(const Expr* E, llvm::APSInt& Result, ASTContext &Ctx) {
- APValue Value = IntExprEvaluator(Ctx).Visit(const_cast<Expr*>(E));
- if (!Value.isSInt())
- return false;
-
- Result = Value.getSInt();
- return true;
- }
-
- //===--------------------------------------------------------------------===//
- // Visitor Methods
- //===--------------------------------------------------------------------===//
- APValue VisitStmt(Stmt *S) {
- // FIXME: Remove this when we support more expressions.
- printf("unhandled int expression");
- S->dump();
- return APValue();
- }
-
- APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
-
- APValue VisitBinaryOperator(const BinaryOperator *E);
- APValue VisitUnaryOperator(const UnaryOperator *E);
-
- APValue HandleCast(const Expr* SubExpr, QualType DestType);
- APValue VisitCastExpr(const CastExpr* E) {
- return HandleCast(E->getSubExpr(), E->getType());
- }
- APValue VisitImplicitCastExpr(const ImplicitCastExpr* E) {
- return HandleCast(E->getSubExpr(), E->getType());
- }
- APValue VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E);
-
- APValue VisitIntegerLiteral(const IntegerLiteral *E) {
- llvm::APSInt Result(Ctx.getTypeSize(E->getType()));
-
- Result = E->getValue();
- return APValue(Result);
- }
+static bool EvaluatePointer(const Expr* E, APValue& Result, ASTContext &Ctx) {
+ if (!E->getType()->isPointerType())
+ return false;
+ Result = PointerExprEvaluator(Ctx).Visit(const_cast<Expr*>(E));
+ return Result.isLValue();
+}
-};
-
-APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
-{
+APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
if (E->getOpcode() != BinaryOperator::Add &&
E->getOpcode() != BinaryOperator::Sub)
return APValue();
@@ -140,13 +96,14 @@
const Expr *PExp = E->getLHS();
const Expr *IExp = E->getRHS();
if (IExp->getType()->isPointerType())
- std::swap(PExp, IExp);
+ std::swap(PExp, IExp);
APValue ResultLValue;
- if (!PointerExprEvaluator::Evaluate(PExp, ResultLValue, Ctx))
+ if (!EvaluatePointer(PExp, ResultLValue, Ctx))
return APValue();
+
llvm::APSInt AdditionalOffset(32);
- if (!IntExprEvaluator::Evaluate(IExp, AdditionalOffset, Ctx))
+ if (!EvaluateInteger(IExp, AdditionalOffset, Ctx))
return APValue();
uint64_t Offset = ResultLValue.getLValueOffset();
@@ -166,15 +123,14 @@
// Check for pointer->pointer cast
if (SubExpr->getType()->isPointerType()) {
APValue Result;
- if (PointerExprEvaluator::Evaluate(SubExpr, Result, Ctx))
+ if (EvaluatePointer(SubExpr, Result, Ctx))
return Result;
- else
- return APValue();
+ return APValue();
}
if (SubExpr->getType()->isArithmeticType()) {
llvm::APSInt Result(32);
- if (IntExprEvaluator::Evaluate(SubExpr, Result, Ctx)) {
+ if (EvaluateInteger(SubExpr, Result, Ctx)) {
Result.extOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(E->getType())));
return APValue(0, Result.getZExtValue());
}
@@ -184,14 +140,71 @@
return APValue();
}
+
+//===----------------------------------------------------------------------===//
+// Integer Evaluation
+//===----------------------------------------------------------------------===//
+
+
+namespace {
+class VISIBILITY_HIDDEN IntExprEvaluator
+ : public StmtVisitor<IntExprEvaluator, APValue> {
+ ASTContext &Ctx;
+
+public:
+ IntExprEvaluator(ASTContext &ctx) : Ctx(ctx) {}
+
+ //===--------------------------------------------------------------------===//
+ // Visitor Methods
+ //===--------------------------------------------------------------------===//
+ APValue VisitStmt(Stmt *S) {
+ // FIXME: Remove this when we support more expressions.
+ printf("unhandled int expression");
+ S->dump();
+ return APValue();
+ }
+
+ APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
+
+ APValue VisitBinaryOperator(const BinaryOperator *E);
+ APValue VisitUnaryOperator(const UnaryOperator *E);
+
+ APValue HandleCast(const Expr* SubExpr, QualType DestType);
+ APValue VisitCastExpr(const CastExpr* E) {
+ return HandleCast(E->getSubExpr(), E->getType());
+ }
+ APValue VisitImplicitCastExpr(const ImplicitCastExpr* E) {
+ return HandleCast(E->getSubExpr(), E->getType());
+ }
+ APValue VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E);
+
+ APValue VisitIntegerLiteral(const IntegerLiteral *E) {
+ llvm::APSInt Result(Ctx.getTypeSize(E->getType()));
+
+ Result = E->getValue();
+ return APValue(Result);
+ }
+};
+} // end anonymous namespace
+
+static bool EvaluateInteger(const Expr* E, APSInt &Result, ASTContext &Ctx) {
+ APValue Value = IntExprEvaluator(Ctx).Visit(const_cast<Expr*>(E));
+ if (!Value.isSInt())
+ return false;
+
+ Result = Value.getSInt();
+ return true;
+}
+
+
APValue IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
// The LHS of a constant expr is always evaluated and needed.
llvm::APSInt Result(32);
- if (!Evaluate(E->getLHS(), Result, Ctx))
+ if (!EvaluateInteger(E->getLHS(), Result, Ctx))
return APValue();
llvm::APSInt RHS(32);
- if (!Evaluate(E->getRHS(), RHS, Ctx))
+ if (!EvaluateInteger(E->getRHS(), RHS, Ctx))
return APValue();
switch (E->getOpcode()) {
@@ -280,7 +293,7 @@
} else {
// Get the operand value. If this is sizeof/alignof, do not evalute the
// operand. This affects C99 6.6p3.
- if (!Evaluate(E->getSubExpr(), Result, Ctx))
+ if (!EvaluateInteger(E->getSubExpr(), Result, Ctx))
return APValue();
switch (E->getOpcode()) {
@@ -320,7 +333,7 @@
// Handle simple integer->integer casts.
if (SubExpr->getType()->isIntegerType()) {
- if (!Evaluate(SubExpr, Result, Ctx))
+ if (!EvaluateInteger(SubExpr, Result, Ctx))
return APValue();
// Figure out if this is a truncate, extend or noop cast.
@@ -334,7 +347,7 @@
Result.extOrTrunc(DestWidth);
} else if (SubExpr->getType()->isPointerType()) {
APValue LV;
- if (!PointerExprEvaluator::Evaluate(SubExpr, LV, Ctx))
+ if (!EvaluatePointer(SubExpr, LV, Ctx))
return APValue();
if (LV.getLValueBase())
return APValue();
@@ -349,9 +362,8 @@
return APValue(Result);
}
-APValue IntExprEvaluator::VisitSizeOfAlignOfTypeExpr
- (const SizeOfAlignOfTypeExpr *E)
-{
+APValue IntExprEvaluator::
+ VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
llvm::APSInt Result(32);
// Return the result in the right width.
@@ -384,10 +396,11 @@
return APValue(Result);
}
-}
-
-bool Expr::tryEvaluate(APValue& Result, ASTContext &Ctx) const
-{
+//===----------------------------------------------------------------------===//
+// Top level TryEvaluate.
+//===----------------------------------------------------------------------===//
+
+bool Expr::tryEvaluate(APValue& Result, ASTContext &Ctx) const {
llvm::APSInt sInt(1);
#if USE_NEW_EVALUATOR
More information about the cfe-commits
mailing list