diff options
author | Anders Carlsson <andersca@mac.com> | 2008-07-03 04:20:39 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2008-07-03 04:20:39 +0000 |
commit | 7a241baf2fad7719bc57acd2cf2fe400256b26f6 (patch) | |
tree | 982d05cc2635ae65e4b654c314b2c9571558203f /clang/lib/AST/ExprConstant.cpp | |
parent | ac1d1d1c34e2a2bce1c233e281c3c71b445a2b78 (diff) | |
download | bcm5719-llvm-7a241baf2fad7719bc57acd2cf2fe400256b26f6.tar.gz bcm5719-llvm-7a241baf2fad7719bc57acd2cf2fe400256b26f6.zip |
Shuffle things around in preparation for integrating Eli's constant evaluator.
llvm-svn: 53074
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp new file mode 100644 index 00000000000..ea9b106b69d --- /dev/null +++ b/clang/lib/AST/ExprConstant.cpp @@ -0,0 +1,60 @@ +//===--- Expr.cpp - Expression Constant Evaluator -------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the Expr constant evaluator. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/APValue.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" + +using namespace clang; + + +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. + if (const ParenExpr *PE = dyn_cast<ParenExpr>(Expr)) + return CalcFakeICEVal(PE->getSubExpr(), Result, Context); + + if (const CastExpr *CE = dyn_cast<CastExpr>(Expr)) { + QualType CETy = CE->getType(); + if ((CETy->isIntegralType() && !CETy->isBooleanType()) || + CETy->isPointerType()) { + if (CalcFakeICEVal(CE->getSubExpr(), Result, Context)) { + Result.extOrTrunc(Context.getTypeSize(CETy)); + // FIXME: This assumes pointers are signed. + Result.setIsSigned(CETy->isSignedIntegerType() || + CETy->isPointerType()); + return true; + } + } + } + + if (Expr->getType()->isIntegralType()) + return Expr->isIntegerConstantExpr(Result, Context); + + return false; +} + +bool Expr::tryEvaluate(APValue& Result, ASTContext &Ctx) const +{ + llvm::APSInt sInt(1); + + if (CalcFakeICEVal(this, sInt, Ctx)) { + Result = APValue(sInt); + return true; + } + + return false; +} |