2008-07-12 03:10:17 +08:00
|
|
|
//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
|
2008-07-03 12:20:39 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2008-11-12 17:44:48 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-07-08 15:23:12 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
2008-07-08 13:49:43 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2008-07-08 13:13:58 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-07-03 12:20:39 +08:00
|
|
|
using namespace clang;
|
2008-07-12 02:11:29 +08:00
|
|
|
using llvm::APSInt;
|
2008-08-22 08:06:13 +08:00
|
|
|
using llvm::APFloat;
|
2008-07-03 12:20:39 +08:00
|
|
|
|
2008-07-12 06:52:41 +08:00
|
|
|
/// EvalInfo - This is a private struct used by the evaluator to capture
|
|
|
|
/// information about a subexpression as it is folded. It retains information
|
|
|
|
/// about the AST context, but also maintains information about the folded
|
|
|
|
/// expression.
|
|
|
|
///
|
|
|
|
/// If an expression could be evaluated, it is still possible it is not a C
|
|
|
|
/// "integer constant expression" or constant expression. If not, this struct
|
|
|
|
/// captures information about how and why not.
|
|
|
|
///
|
|
|
|
/// One bit of information passed *into* the request for constant folding
|
|
|
|
/// indicates whether the subexpression is "evaluated" or not according to C
|
|
|
|
/// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
|
|
|
|
/// evaluate the expression regardless of what the RHS is, but C only allows
|
|
|
|
/// certain things in certain situations.
|
|
|
|
struct EvalInfo {
|
|
|
|
ASTContext &Ctx;
|
|
|
|
|
2008-12-01 00:38:33 +08:00
|
|
|
/// EvalResult - Contains information about the evaluation.
|
|
|
|
Expr::EvalResult &EvalResult;
|
2008-12-01 02:26:25 +08:00
|
|
|
|
|
|
|
/// ShortCircuit - will be greater than zero if the current subexpression has
|
|
|
|
/// will not be evaluated because it's short-circuited (according to C rules).
|
|
|
|
unsigned ShortCircuit;
|
2008-12-01 00:38:33 +08:00
|
|
|
|
|
|
|
EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
|
2008-12-01 02:26:25 +08:00
|
|
|
EvalResult(evalresult), ShortCircuit(0) {}
|
2008-07-12 06:52:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
static bool EvaluateLValue(const Expr *E, APValue &Result, EvalInfo &Info);
|
2008-07-12 06:52:41 +08:00
|
|
|
static bool EvaluatePointer(const Expr *E, APValue &Result, EvalInfo &Info);
|
|
|
|
static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
|
2008-08-22 08:06:13 +08:00
|
|
|
static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
|
2009-01-29 06:24:07 +08:00
|
|
|
static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info);
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
|
|
|
|
if (E->getType()->isIntegralType()) {
|
|
|
|
APSInt IntResult;
|
|
|
|
if (!EvaluateInteger(E, IntResult, Info))
|
|
|
|
return false;
|
|
|
|
Result = IntResult != 0;
|
|
|
|
return true;
|
|
|
|
} else if (E->getType()->isRealFloatingType()) {
|
|
|
|
APFloat FloatResult(0.0);
|
|
|
|
if (!EvaluateFloat(E, FloatResult, Info))
|
|
|
|
return false;
|
|
|
|
Result = !FloatResult.isZero();
|
|
|
|
return true;
|
|
|
|
} else if (E->getType()->isPointerType()) {
|
|
|
|
APValue PointerResult;
|
|
|
|
if (!EvaluatePointer(E, PointerResult, Info))
|
|
|
|
return false;
|
|
|
|
// FIXME: Is this accurate for all kinds of bases? If not, what would
|
|
|
|
// the check look like?
|
|
|
|
Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
|
|
|
|
APFloat &Value, ASTContext &Ctx) {
|
|
|
|
unsigned DestWidth = Ctx.getIntWidth(DestType);
|
|
|
|
// Determine whether we are converting to unsigned or signed.
|
|
|
|
bool DestSigned = DestType->isSignedIntegerType();
|
|
|
|
|
|
|
|
// FIXME: Warning for overflow.
|
|
|
|
uint64_t Space[4];
|
|
|
|
bool ignored;
|
|
|
|
(void)Value.convertToInteger(Space, DestWidth, DestSigned,
|
|
|
|
llvm::APFloat::rmTowardZero, &ignored);
|
|
|
|
return APSInt(llvm::APInt(DestWidth, 4, Space), !DestSigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
|
|
|
|
APFloat &Value, ASTContext &Ctx) {
|
|
|
|
bool ignored;
|
|
|
|
APFloat Result = Value;
|
|
|
|
Result.convert(Ctx.getFloatTypeSemantics(DestType),
|
|
|
|
APFloat::rmNearestTiesToEven, &ignored);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
|
|
|
|
APSInt &Value, ASTContext &Ctx) {
|
|
|
|
unsigned DestWidth = Ctx.getIntWidth(DestType);
|
|
|
|
APSInt Result = Value;
|
|
|
|
// Figure out if this is a truncate, extend or noop cast.
|
|
|
|
// If the input is signed, do a sign extend, noop, or truncate.
|
|
|
|
Result.extOrTrunc(DestWidth);
|
|
|
|
Result.setIsUnsigned(DestType->isUnsignedIntegerType());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
|
|
|
|
APSInt &Value, ASTContext &Ctx) {
|
|
|
|
|
|
|
|
APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
|
|
|
|
Result.convertFromAPInt(Value, Value.isSigned(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LValue Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN LValueExprEvaluator
|
|
|
|
: public StmtVisitor<LValueExprEvaluator, APValue> {
|
|
|
|
EvalInfo &Info;
|
|
|
|
public:
|
|
|
|
|
|
|
|
LValueExprEvaluator(EvalInfo &info) : Info(info) {}
|
|
|
|
|
|
|
|
APValue VisitStmt(Stmt *S) {
|
2008-11-13 05:52:46 +08:00
|
|
|
#if 0
|
2008-11-12 17:44:48 +08:00
|
|
|
// FIXME: Remove this when we support more expressions.
|
|
|
|
printf("Unhandled pointer statement\n");
|
|
|
|
S->dump();
|
2008-11-13 05:52:46 +08:00
|
|
|
#endif
|
2008-11-12 17:44:48 +08:00
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
2008-11-24 12:41:22 +08:00
|
|
|
APValue VisitDeclRefExpr(DeclRefExpr *E);
|
2008-11-12 17:44:48 +08:00
|
|
|
APValue VisitPredefinedExpr(PredefinedExpr *E) { return APValue(E, 0); }
|
|
|
|
APValue VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
|
|
|
|
APValue VisitMemberExpr(MemberExpr *E);
|
|
|
|
APValue VisitStringLiteral(StringLiteral *E) { return APValue(E, 0); }
|
2008-11-17 03:01:22 +08:00
|
|
|
APValue VisitArraySubscriptExpr(ArraySubscriptExpr *E);
|
2008-11-12 17:44:48 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateLValue(const Expr* E, APValue& Result, EvalInfo &Info) {
|
|
|
|
Result = LValueExprEvaluator(Info).Visit(const_cast<Expr*>(E));
|
|
|
|
return Result.isLValue();
|
|
|
|
}
|
|
|
|
|
2008-11-24 12:41:22 +08:00
|
|
|
APValue LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E)
|
|
|
|
{
|
|
|
|
if (!E->hasGlobalStorage())
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
return APValue(E, 0);
|
|
|
|
}
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
if (E->isFileScope())
|
|
|
|
return APValue(E, 0);
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
|
|
|
|
APValue result;
|
|
|
|
QualType Ty;
|
|
|
|
if (E->isArrow()) {
|
|
|
|
if (!EvaluatePointer(E->getBase(), result, Info))
|
|
|
|
return APValue();
|
|
|
|
Ty = E->getBase()->getType()->getAsPointerType()->getPointeeType();
|
|
|
|
} else {
|
|
|
|
result = Visit(E->getBase());
|
|
|
|
if (result.isUninit())
|
|
|
|
return APValue();
|
|
|
|
Ty = E->getBase()->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
RecordDecl *RD = Ty->getAsRecordType()->getDecl();
|
|
|
|
const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
|
2008-12-21 07:49:58 +08:00
|
|
|
|
|
|
|
FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
|
|
|
|
if (!FD) // FIXME: deal with other kinds of member expressions
|
|
|
|
return APValue();
|
2008-11-12 17:44:48 +08:00
|
|
|
|
|
|
|
// FIXME: This is linear time.
|
2008-12-12 00:49:14 +08:00
|
|
|
unsigned i = 0;
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
|
|
|
Field != FieldEnd; (void)++Field, ++i) {
|
|
|
|
if (*Field == FD)
|
2008-11-12 17:44:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.setLValue(result.getLValueBase(),
|
|
|
|
result.getLValueOffset() + RL.getFieldOffset(i) / 8);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-11-17 03:01:22 +08:00
|
|
|
APValue LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E)
|
|
|
|
{
|
|
|
|
APValue Result;
|
|
|
|
|
|
|
|
if (!EvaluatePointer(E->getBase(), Result, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
APSInt Index;
|
|
|
|
if (!EvaluateInteger(E->getIdx(), Index, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
uint64_t ElementSize = Info.Ctx.getTypeSize(E->getType()) / 8;
|
|
|
|
|
|
|
|
uint64_t Offset = Index.getSExtValue() * ElementSize;
|
|
|
|
Result.setLValue(Result.getLValueBase(),
|
|
|
|
Result.getLValueOffset() + Offset);
|
|
|
|
return Result;
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pointer Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-08 22:30:00 +08:00
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN PointerExprEvaluator
|
|
|
|
: public StmtVisitor<PointerExprEvaluator, APValue> {
|
2008-07-12 06:52:41 +08:00
|
|
|
EvalInfo &Info;
|
2008-07-08 22:30:00 +08:00
|
|
|
public:
|
|
|
|
|
2008-07-12 06:52:41 +08:00
|
|
|
PointerExprEvaluator(EvalInfo &info) : Info(info) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-07-08 22:30:00 +08:00
|
|
|
APValue VisitStmt(Stmt *S) {
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
APValue VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
APValue VisitCastExpr(const CastExpr* E);
|
2008-11-12 17:44:48 +08:00
|
|
|
APValue VisitUnaryOperator(const UnaryOperator *E);
|
|
|
|
APValue VisitObjCStringLiteral(ObjCStringLiteral *E)
|
|
|
|
{ return APValue(E, 0); }
|
2009-01-25 09:21:06 +08:00
|
|
|
APValue VisitAddrLabelExpr(AddrLabelExpr *E)
|
|
|
|
{ return APValue(E, 0); }
|
2009-01-25 09:54:01 +08:00
|
|
|
APValue VisitCallExpr(CallExpr *E);
|
2008-11-12 17:44:48 +08:00
|
|
|
APValue VisitConditionalOperator(ConditionalOperator *E);
|
2008-07-08 22:30:00 +08:00
|
|
|
};
|
2008-07-12 02:11:29 +08:00
|
|
|
} // end anonymous namespace
|
2008-07-08 22:30:00 +08:00
|
|
|
|
2008-07-12 06:52:41 +08:00
|
|
|
static bool EvaluatePointer(const Expr* E, APValue& Result, EvalInfo &Info) {
|
2008-07-12 02:11:29 +08:00
|
|
|
if (!E->getType()->isPointerType())
|
|
|
|
return false;
|
2008-07-12 06:52:41 +08:00
|
|
|
Result = PointerExprEvaluator(Info).Visit(const_cast<Expr*>(E));
|
2008-07-12 02:11:29 +08:00
|
|
|
return Result.isLValue();
|
|
|
|
}
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
APValue PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
2008-07-08 23:34:11 +08:00
|
|
|
if (E->getOpcode() != BinaryOperator::Add &&
|
|
|
|
E->getOpcode() != BinaryOperator::Sub)
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
const Expr *PExp = E->getLHS();
|
|
|
|
const Expr *IExp = E->getRHS();
|
|
|
|
if (IExp->getType()->isPointerType())
|
2008-07-12 02:11:29 +08:00
|
|
|
std::swap(PExp, IExp);
|
2008-07-08 23:34:11 +08:00
|
|
|
|
|
|
|
APValue ResultLValue;
|
2008-07-12 06:52:41 +08:00
|
|
|
if (!EvaluatePointer(PExp, ResultLValue, Info))
|
2008-07-08 23:34:11 +08:00
|
|
|
return APValue();
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
llvm::APSInt AdditionalOffset(32);
|
2008-07-12 06:52:41 +08:00
|
|
|
if (!EvaluateInteger(IExp, AdditionalOffset, Info))
|
2008-07-08 23:34:11 +08:00
|
|
|
return APValue();
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
QualType PointeeType = PExp->getType()->getAsPointerType()->getPointeeType();
|
|
|
|
uint64_t SizeOfPointee = Info.Ctx.getTypeSize(PointeeType) / 8;
|
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
uint64_t Offset = ResultLValue.getLValueOffset();
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
if (E->getOpcode() == BinaryOperator::Add)
|
2008-11-12 17:44:48 +08:00
|
|
|
Offset += AdditionalOffset.getLimitedValue() * SizeOfPointee;
|
2008-07-08 23:34:11 +08:00
|
|
|
else
|
2008-11-12 17:44:48 +08:00
|
|
|
Offset -= AdditionalOffset.getLimitedValue() * SizeOfPointee;
|
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
return APValue(ResultLValue.getLValueBase(), Offset);
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
|
|
|
APValue PointerExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
|
|
|
if (E->getOpcode() == UnaryOperator::Extension) {
|
|
|
|
// FIXME: Deal with warnings?
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (E->getOpcode() == UnaryOperator::AddrOf) {
|
|
|
|
APValue result;
|
|
|
|
if (EvaluateLValue(E->getSubExpr(), result, Info))
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return APValue();
|
|
|
|
}
|
2008-12-05 13:24:13 +08:00
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
APValue PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
|
2008-07-08 23:34:11 +08:00
|
|
|
const Expr* SubExpr = E->getSubExpr();
|
|
|
|
|
|
|
|
// Check for pointer->pointer cast
|
|
|
|
if (SubExpr->getType()->isPointerType()) {
|
|
|
|
APValue Result;
|
2008-07-12 06:52:41 +08:00
|
|
|
if (EvaluatePointer(SubExpr, Result, Info))
|
2008-07-08 23:34:11 +08:00
|
|
|
return Result;
|
2008-07-12 02:11:29 +08:00
|
|
|
return APValue();
|
2008-07-08 23:34:11 +08:00
|
|
|
}
|
|
|
|
|
2008-07-27 13:46:18 +08:00
|
|
|
if (SubExpr->getType()->isIntegralType()) {
|
2008-07-08 23:34:11 +08:00
|
|
|
llvm::APSInt Result(32);
|
2008-07-12 06:52:41 +08:00
|
|
|
if (EvaluateInteger(SubExpr, Result, Info)) {
|
|
|
|
Result.extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
|
2008-07-08 23:34:11 +08:00
|
|
|
return APValue(0, Result.getZExtValue());
|
|
|
|
}
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
|
|
|
if (SubExpr->getType()->isFunctionType() ||
|
|
|
|
SubExpr->getType()->isArrayType()) {
|
|
|
|
APValue Result;
|
|
|
|
if (EvaluateLValue(SubExpr, Result, Info))
|
|
|
|
return Result;
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
//assert(0 && "Unhandled cast");
|
2008-07-08 23:34:11 +08:00
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
2009-01-25 09:54:01 +08:00
|
|
|
APValue PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
|
|
|
|
if (E->isBuiltinCall() == Builtin::BI__builtin___CFStringMakeConstantString)
|
|
|
|
return APValue(E, 0);
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
APValue PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
|
|
|
|
bool BoolResult;
|
|
|
|
if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
|
|
|
|
|
|
|
|
APValue Result;
|
|
|
|
if (EvaluatePointer(EvalExpr, Result, Info))
|
|
|
|
return Result;
|
|
|
|
return APValue();
|
|
|
|
}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Vector Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN VectorExprEvaluator
|
|
|
|
: public StmtVisitor<VectorExprEvaluator, APValue> {
|
|
|
|
EvalInfo &Info;
|
|
|
|
public:
|
|
|
|
|
|
|
|
VectorExprEvaluator(EvalInfo &info) : Info(info) {}
|
|
|
|
|
|
|
|
APValue VisitStmt(Stmt *S) {
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
APValue VisitCastExpr(const CastExpr* E);
|
|
|
|
APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
|
|
|
|
APValue VisitInitListExpr(const InitListExpr *E);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
|
|
|
|
if (!E->getType()->isVectorType())
|
|
|
|
return false;
|
|
|
|
Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
|
|
|
|
return !Result.isUninit();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
|
|
|
|
const Expr* SE = E->getSubExpr();
|
|
|
|
|
|
|
|
// Check for vector->vector bitcast.
|
|
|
|
if (SE->getType()->isVectorType())
|
|
|
|
return this->Visit(const_cast<Expr*>(SE));
|
|
|
|
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue
|
|
|
|
VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
|
|
|
return this->Visit(const_cast<Expr*>(E->getInitializer()));
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue
|
|
|
|
VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
|
|
|
const VectorType *VT = E->getType()->getAsVectorType();
|
|
|
|
unsigned NumInits = E->getNumInits();
|
|
|
|
|
|
|
|
if (!VT || VT->getNumElements() != NumInits)
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
QualType EltTy = VT->getElementType();
|
|
|
|
llvm::SmallVector<APValue, 4> Elements;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NumInits; i++) {
|
|
|
|
if (EltTy->isIntegerType()) {
|
|
|
|
llvm::APSInt sInt(32);
|
|
|
|
if (!EvaluateInteger(E->getInit(i), sInt, Info))
|
|
|
|
return APValue();
|
|
|
|
Elements.push_back(APValue(sInt));
|
|
|
|
} else {
|
|
|
|
llvm::APFloat f(0.0);
|
|
|
|
if (!EvaluateFloat(E->getInit(i), f, Info))
|
|
|
|
return APValue();
|
|
|
|
Elements.push_back(APValue(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return APValue(&Elements[0], Elements.size());
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Integer Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN IntExprEvaluator
|
2008-07-12 03:10:17 +08:00
|
|
|
: public StmtVisitor<IntExprEvaluator, bool> {
|
2008-07-12 06:52:41 +08:00
|
|
|
EvalInfo &Info;
|
2008-07-12 03:10:17 +08:00
|
|
|
APSInt &Result;
|
2008-07-12 02:11:29 +08:00
|
|
|
public:
|
2008-07-12 06:52:41 +08:00
|
|
|
IntExprEvaluator(EvalInfo &info, APSInt &result)
|
|
|
|
: Info(info), Result(result) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-07-12 03:24:49 +08:00
|
|
|
unsigned getIntTypeSizeInBits(QualType T) const {
|
2008-07-12 08:14:42 +08:00
|
|
|
return (unsigned)Info.Ctx.getIntWidth(T);
|
|
|
|
}
|
|
|
|
|
2008-12-01 02:14:57 +08:00
|
|
|
bool Extension(SourceLocation L, diag::kind D, const Expr *E) {
|
2008-12-01 00:38:33 +08:00
|
|
|
Info.EvalResult.DiagLoc = L;
|
|
|
|
Info.EvalResult.Diag = D;
|
2008-12-01 02:14:57 +08:00
|
|
|
Info.EvalResult.DiagExpr = E;
|
2008-07-12 08:14:42 +08:00
|
|
|
return true; // still a constant.
|
|
|
|
}
|
|
|
|
|
2008-12-01 02:14:57 +08:00
|
|
|
bool Error(SourceLocation L, diag::kind D, const Expr *E) {
|
2008-07-12 08:14:42 +08:00
|
|
|
// If this is in an unevaluated portion of the subexpression, ignore the
|
|
|
|
// error.
|
2008-12-01 02:26:25 +08:00
|
|
|
if (Info.ShortCircuit) {
|
2008-11-12 15:43:42 +08:00
|
|
|
// If error is ignored because the value isn't evaluated, get the real
|
|
|
|
// type at least to prevent errors downstream.
|
2008-12-01 02:14:57 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2008-07-12 08:14:42 +08:00
|
|
|
return true;
|
2008-11-12 15:43:42 +08:00
|
|
|
}
|
2008-07-12 08:14:42 +08:00
|
|
|
|
2008-11-12 15:43:42 +08:00
|
|
|
// Take the first error.
|
2008-12-01 00:38:33 +08:00
|
|
|
if (Info.EvalResult.Diag == 0) {
|
|
|
|
Info.EvalResult.DiagLoc = L;
|
|
|
|
Info.EvalResult.Diag = D;
|
2008-12-01 02:14:57 +08:00
|
|
|
Info.EvalResult.DiagExpr = E;
|
2008-11-12 15:43:42 +08:00
|
|
|
}
|
2008-07-12 08:14:42 +08:00
|
|
|
return false;
|
2008-07-12 03:24:49 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-11-12 15:43:42 +08:00
|
|
|
|
|
|
|
bool VisitStmt(Stmt *) {
|
|
|
|
assert(0 && "This should be called on integers, stmts are not integers");
|
|
|
|
return false;
|
|
|
|
}
|
2008-07-12 03:24:49 +08:00
|
|
|
|
2008-11-12 15:43:42 +08:00
|
|
|
bool VisitExpr(Expr *E) {
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-07-12 08:38:25 +08:00
|
|
|
bool VisitIntegerLiteral(const IntegerLiteral *E) {
|
|
|
|
Result = E->getValue();
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitCharacterLiteral(const CharacterLiteral *E) {
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result = E->getValue();
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
2008-10-24 16:07:57 +08:00
|
|
|
// Per gcc docs "this built-in function ignores top level
|
|
|
|
// qualifiers". We need to use the canonical version to properly
|
|
|
|
// be able to strip CRV qualifiers from the type.
|
|
|
|
QualType T0 = Info.Ctx.getCanonicalType(E->getArgType1());
|
|
|
|
QualType T1 = Info.Ctx.getCanonicalType(E->getArgType2());
|
|
|
|
Result = Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
|
|
|
|
T1.getUnqualifiedType());
|
2008-07-12 08:38:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *E);
|
|
|
|
bool VisitCallExpr(const CallExpr *E);
|
2008-07-12 03:10:17 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
bool VisitUnaryOperator(const UnaryOperator *E);
|
2008-11-17 03:28:31 +08:00
|
|
|
bool VisitConditionalOperator(const ConditionalOperator *E);
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
bool VisitCastExpr(CastExpr* E);
|
2008-11-12 01:56:53 +08:00
|
|
|
bool VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E);
|
|
|
|
|
2008-11-17 03:01:22 +08:00
|
|
|
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
|
2008-11-17 06:46:56 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
2008-11-17 03:01:22 +08:00
|
|
|
Result = E->getValue();
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-22 06:39:40 +08:00
|
|
|
bool VisitGNUNullExpr(const GNUNullExpr *E) {
|
|
|
|
Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-17 03:01:22 +08:00
|
|
|
bool VisitCXXZeroInitValueExpr(const CXXZeroInitValueExpr *E) {
|
|
|
|
Result = APSInt::getNullValue(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-06 04:52:13 +08:00
|
|
|
bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result = E->Evaluate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-12 05:24:13 +08:00
|
|
|
private:
|
2009-01-25 05:53:27 +08:00
|
|
|
unsigned GetAlignOfExpr(const Expr *E);
|
|
|
|
unsigned GetAlignOfType(QualType T);
|
2008-07-12 02:11:29 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-07-12 06:52:41 +08:00
|
|
|
static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
|
|
|
|
return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
2008-07-12 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 08:38:25 +08:00
|
|
|
bool IntExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
|
|
|
|
// Enums are integer constant exprs.
|
|
|
|
if (const EnumConstantDecl *D = dyn_cast<EnumConstantDecl>(E->getDecl())) {
|
|
|
|
Result = D->getInitVal();
|
2008-12-08 10:21:03 +08:00
|
|
|
// FIXME: This is an ugly hack around the fact that enums don't set their
|
|
|
|
// signedness consistently; see PR3173
|
|
|
|
Result.setIsUnsigned(!E->getType()->isSignedIntegerType());
|
2008-07-12 08:38:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, random variable references are not constants.
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
|
|
|
|
2008-10-06 14:40:35 +08:00
|
|
|
/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
|
|
|
|
/// as GCC.
|
|
|
|
static int EvaluateBuiltinClassifyType(const CallExpr *E) {
|
|
|
|
// The following enum mimics the values returned by GCC.
|
|
|
|
enum gcc_type_class {
|
|
|
|
no_type_class = -1,
|
|
|
|
void_type_class, integer_type_class, char_type_class,
|
|
|
|
enumeral_type_class, boolean_type_class,
|
|
|
|
pointer_type_class, reference_type_class, offset_type_class,
|
|
|
|
real_type_class, complex_type_class,
|
|
|
|
function_type_class, method_type_class,
|
|
|
|
record_type_class, union_type_class,
|
|
|
|
array_type_class, string_type_class,
|
|
|
|
lang_type_class
|
|
|
|
};
|
|
|
|
|
|
|
|
// If no argument was supplied, default to "no_type_class". This isn't
|
|
|
|
// ideal, however it is what gcc does.
|
|
|
|
if (E->getNumArgs() == 0)
|
|
|
|
return no_type_class;
|
|
|
|
|
|
|
|
QualType ArgTy = E->getArg(0)->getType();
|
|
|
|
if (ArgTy->isVoidType())
|
|
|
|
return void_type_class;
|
|
|
|
else if (ArgTy->isEnumeralType())
|
|
|
|
return enumeral_type_class;
|
|
|
|
else if (ArgTy->isBooleanType())
|
|
|
|
return boolean_type_class;
|
|
|
|
else if (ArgTy->isCharType())
|
|
|
|
return string_type_class; // gcc doesn't appear to use char_type_class
|
|
|
|
else if (ArgTy->isIntegerType())
|
|
|
|
return integer_type_class;
|
|
|
|
else if (ArgTy->isPointerType())
|
|
|
|
return pointer_type_class;
|
|
|
|
else if (ArgTy->isReferenceType())
|
|
|
|
return reference_type_class;
|
|
|
|
else if (ArgTy->isRealType())
|
|
|
|
return real_type_class;
|
|
|
|
else if (ArgTy->isComplexType())
|
|
|
|
return complex_type_class;
|
|
|
|
else if (ArgTy->isFunctionType())
|
|
|
|
return function_type_class;
|
|
|
|
else if (ArgTy->isStructureType())
|
|
|
|
return record_type_class;
|
|
|
|
else if (ArgTy->isUnionType())
|
|
|
|
return union_type_class;
|
|
|
|
else if (ArgTy->isArrayType())
|
|
|
|
return array_type_class;
|
|
|
|
else if (ArgTy->isUnionType())
|
|
|
|
return union_type_class;
|
|
|
|
else // FIXME: offset_type_class, method_type_class, & lang_type_class?
|
|
|
|
assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-12 08:38:25 +08:00
|
|
|
bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
|
2008-10-06 13:28:25 +08:00
|
|
|
switch (E->isBuiltinCall()) {
|
|
|
|
default:
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
2008-10-06 13:28:25 +08:00
|
|
|
case Builtin::BI__builtin_classify_type:
|
2008-10-06 14:40:35 +08:00
|
|
|
Result.setIsSigned(true);
|
|
|
|
Result = EvaluateBuiltinClassifyType(E);
|
2008-10-06 13:28:25 +08:00
|
|
|
return true;
|
|
|
|
|
2008-11-24 12:21:33 +08:00
|
|
|
case Builtin::BI__builtin_constant_p:
|
2008-10-06 13:28:25 +08:00
|
|
|
// __builtin_constant_p always has one operand: it returns true if that
|
|
|
|
// operand can be folded, false otherwise.
|
2008-11-24 12:21:33 +08:00
|
|
|
Result = E->getArg(0)->isEvaluatable(Info.Ctx);
|
2008-10-06 13:28:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
2008-11-13 14:09:17 +08:00
|
|
|
if (E->getOpcode() == BinaryOperator::Comma) {
|
2008-12-01 10:07:06 +08:00
|
|
|
if (!Visit(E->getRHS()))
|
|
|
|
return false;
|
2008-12-01 14:44:05 +08:00
|
|
|
|
|
|
|
if (!Info.ShortCircuit) {
|
|
|
|
// If we can't evaluate the LHS, it must be because it has
|
|
|
|
// side effects.
|
|
|
|
if (!E->getLHS()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
|
2008-12-01 02:37:00 +08:00
|
|
|
return Extension(E->getOperatorLoc(), diag::note_comma_in_ice, E);
|
2008-12-01 14:44:05 +08:00
|
|
|
}
|
2008-11-12 15:04:29 +08:00
|
|
|
|
2008-12-01 10:07:06 +08:00
|
|
|
return true;
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (E->isLogicalOp()) {
|
|
|
|
// These need to be handled specially because the operands aren't
|
|
|
|
// necessarily integral
|
2008-12-01 00:51:17 +08:00
|
|
|
bool lhsResult, rhsResult;
|
2008-11-23 05:04:56 +08:00
|
|
|
|
2008-12-01 00:51:17 +08:00
|
|
|
if (HandleConversionToBool(E->getLHS(), lhsResult, Info)) {
|
2008-11-23 05:04:56 +08:00
|
|
|
// We were able to evaluate the LHS, see if we can get away with not
|
|
|
|
// evaluating the RHS: 0 && X -> 0, 1 || X -> 1
|
2008-12-01 00:51:17 +08:00
|
|
|
if (lhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
|
|
|
|
!lhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
|
2008-11-24 12:21:33 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2008-12-01 00:51:17 +08:00
|
|
|
Result = lhsResult;
|
2008-11-24 12:21:33 +08:00
|
|
|
|
2008-12-01 02:26:25 +08:00
|
|
|
Info.ShortCircuit++;
|
2008-12-01 00:51:17 +08:00
|
|
|
bool rhsEvaluated = HandleConversionToBool(E->getRHS(), rhsResult, Info);
|
2008-12-01 02:26:25 +08:00
|
|
|
Info.ShortCircuit--;
|
|
|
|
|
2008-12-01 00:51:17 +08:00
|
|
|
if (rhsEvaluated)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: Return an extension warning saying that the RHS could not be
|
|
|
|
// evaluated.
|
2008-11-24 12:21:33 +08:00
|
|
|
return true;
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
2008-12-01 00:51:17 +08:00
|
|
|
if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
|
2008-11-24 12:21:33 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
if (E->getOpcode() == BinaryOperator::LOr)
|
2008-12-01 00:51:17 +08:00
|
|
|
Result = lhsResult || rhsResult;
|
2008-11-24 12:21:33 +08:00
|
|
|
else
|
2008-12-01 00:51:17 +08:00
|
|
|
Result = lhsResult && rhsResult;
|
2008-11-24 12:21:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
2008-12-01 00:51:17 +08:00
|
|
|
if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
|
2008-11-24 12:21:33 +08:00
|
|
|
// We can't evaluate the LHS; however, sometimes the result
|
|
|
|
// is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
|
2008-12-01 00:51:17 +08:00
|
|
|
if (rhsResult == (E->getOpcode() == BinaryOperator::LOr) ||
|
|
|
|
!rhsResult == (E->getOpcode() == BinaryOperator::LAnd)) {
|
2008-11-24 12:21:33 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2008-12-01 00:51:17 +08:00
|
|
|
Result = rhsResult;
|
|
|
|
|
|
|
|
// Since we werent able to evaluate the left hand side, it
|
|
|
|
// must have had side effects.
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
|
2008-11-24 12:21:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2008-11-12 15:04:29 +08:00
|
|
|
}
|
2008-07-12 08:14:42 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
QualType LHSTy = E->getLHS()->getType();
|
|
|
|
QualType RHSTy = E->getRHS()->getType();
|
2009-01-29 14:43:41 +08:00
|
|
|
|
|
|
|
if (LHSTy->isAnyComplexType()) {
|
|
|
|
assert(RHSTy->isAnyComplexType() && "Invalid comparison");
|
|
|
|
APValue LHS, RHS;
|
|
|
|
|
|
|
|
if (!EvaluateComplex(E->getLHS(), LHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (LHS.isComplexFloat()) {
|
|
|
|
APFloat::cmpResult CR_r =
|
|
|
|
LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
|
|
|
|
APFloat::cmpResult CR_i =
|
|
|
|
LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
|
|
|
|
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
if (E->getOpcode() == BinaryOperator::EQ)
|
|
|
|
Result = (CR_r == APFloat::cmpEqual &&
|
|
|
|
CR_i == APFloat::cmpEqual);
|
|
|
|
else if (E->getOpcode() == BinaryOperator::NE)
|
|
|
|
Result = ((CR_r == APFloat::cmpGreaterThan ||
|
|
|
|
CR_r == APFloat::cmpLessThan) &&
|
|
|
|
(CR_i == APFloat::cmpGreaterThan ||
|
|
|
|
CR_i == APFloat::cmpLessThan));
|
|
|
|
else
|
|
|
|
assert(0 && "Invalid complex compartison.");
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
if (E->getOpcode() == BinaryOperator::EQ)
|
|
|
|
Result = (LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
|
|
|
|
LHS.getComplexIntImag() == RHS.getComplexIntImag());
|
|
|
|
else if (E->getOpcode() == BinaryOperator::NE)
|
|
|
|
Result = (LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
|
|
|
|
LHS.getComplexIntImag() != RHS.getComplexIntImag());
|
|
|
|
else
|
|
|
|
assert(0 && "Invalid complex compartison.");
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-11-16 15:17:21 +08:00
|
|
|
|
|
|
|
if (LHSTy->isRealFloatingType() &&
|
|
|
|
RHSTy->isRealFloatingType()) {
|
|
|
|
APFloat RHS(0.0), LHS(0.0);
|
|
|
|
|
|
|
|
if (!EvaluateFloat(E->getRHS(), RHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!EvaluateFloat(E->getLHS(), LHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
APFloat::cmpResult CR = LHS.compare(RHS);
|
2008-11-17 06:46:56 +08:00
|
|
|
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid binary operator!");
|
|
|
|
case BinaryOperator::LT:
|
|
|
|
Result = CR == APFloat::cmpLessThan;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::GT:
|
|
|
|
Result = CR == APFloat::cmpGreaterThan;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::LE:
|
|
|
|
Result = CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::GE:
|
|
|
|
Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
Result = CR == APFloat::cmpEqual;
|
|
|
|
break;
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
Result = CR == APFloat::cmpGreaterThan || CR == APFloat::cmpLessThan;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-17 03:01:22 +08:00
|
|
|
if (E->getOpcode() == BinaryOperator::Sub) {
|
2008-11-17 06:46:56 +08:00
|
|
|
if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
|
2008-11-17 03:01:22 +08:00
|
|
|
APValue LHSValue;
|
|
|
|
if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
APValue RHSValue;
|
|
|
|
if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Is this correct? What if only one of the operands has a base?
|
|
|
|
if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const QualType Type = E->getLHS()->getType();
|
|
|
|
const QualType ElementType = Type->getAsPointerType()->getPointeeType();
|
|
|
|
|
|
|
|
uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
|
|
|
|
D /= Info.Ctx.getTypeSize(ElementType) / 8;
|
|
|
|
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
2008-11-17 06:46:56 +08:00
|
|
|
Result = D;
|
2008-11-17 03:01:22 +08:00
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-11-16 15:17:21 +08:00
|
|
|
if (!LHSTy->isIntegralType() ||
|
|
|
|
!RHSTy->isIntegralType()) {
|
2008-11-13 14:09:17 +08:00
|
|
|
// We can't continue from here for non-integral types, and they
|
|
|
|
// could potentially confuse the following operations.
|
|
|
|
// FIXME: Deal with EQ and friends.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The LHS of a constant expr is always evaluated and needed.
|
|
|
|
llvm::APSInt RHS(32);
|
|
|
|
if (!Visit(E->getLHS())) {
|
|
|
|
return false; // error in subexpression.
|
|
|
|
}
|
2008-07-27 13:46:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
// FIXME Maybe we want to succeed even where we can't evaluate the
|
|
|
|
// right side of LAnd/LOr?
|
|
|
|
// For example, see http://llvm.org/bugs/show_bug.cgi?id=2525
|
2008-07-12 08:14:42 +08:00
|
|
|
if (!EvaluateInteger(E->getRHS(), RHS, Info))
|
2008-07-12 03:10:17 +08:00
|
|
|
return false;
|
2008-11-13 14:09:17 +08:00
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
switch (E->getOpcode()) {
|
2008-11-12 15:43:42 +08:00
|
|
|
default:
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 08:14:42 +08:00
|
|
|
case BinaryOperator::Mul: Result *= RHS; return true;
|
|
|
|
case BinaryOperator::Add: Result += RHS; return true;
|
|
|
|
case BinaryOperator::Sub: Result -= RHS; return true;
|
|
|
|
case BinaryOperator::And: Result &= RHS; return true;
|
|
|
|
case BinaryOperator::Xor: Result ^= RHS; return true;
|
|
|
|
case BinaryOperator::Or: Result |= RHS; return true;
|
2008-07-08 22:35:21 +08:00
|
|
|
case BinaryOperator::Div:
|
2008-07-12 08:14:42 +08:00
|
|
|
if (RHS == 0)
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
|
2008-07-12 03:10:17 +08:00
|
|
|
Result /= RHS;
|
2008-11-12 15:43:42 +08:00
|
|
|
break;
|
2008-07-08 22:35:21 +08:00
|
|
|
case BinaryOperator::Rem:
|
2008-07-12 08:14:42 +08:00
|
|
|
if (RHS == 0)
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getOperatorLoc(), diag::note_expr_divide_by_zero, E);
|
2008-07-08 22:35:21 +08:00
|
|
|
Result %= RHS;
|
2008-11-12 15:43:42 +08:00
|
|
|
break;
|
2008-07-08 22:35:21 +08:00
|
|
|
case BinaryOperator::Shl:
|
2008-07-12 08:14:42 +08:00
|
|
|
// FIXME: Warn about out of range shift amounts!
|
2008-07-12 03:10:17 +08:00
|
|
|
Result <<= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
|
2008-07-08 22:35:21 +08:00
|
|
|
break;
|
|
|
|
case BinaryOperator::Shr:
|
2008-07-12 03:10:17 +08:00
|
|
|
Result >>= (unsigned)RHS.getLimitedValue(Result.getBitWidth()-1);
|
2008-07-08 22:35:21 +08:00
|
|
|
break;
|
2008-07-12 03:10:17 +08:00
|
|
|
|
2008-07-12 03:29:32 +08:00
|
|
|
case BinaryOperator::LT:
|
|
|
|
Result = Result < RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::GT:
|
|
|
|
Result = Result > RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::LE:
|
|
|
|
Result = Result <= RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::GE:
|
|
|
|
Result = Result >= RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
Result = Result == RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
Result = Result != RHS;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
2008-07-12 08:14:42 +08:00
|
|
|
case BinaryOperator::LAnd:
|
|
|
|
Result = Result != 0 && RHS != 0;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
|
|
|
case BinaryOperator::LOr:
|
|
|
|
Result = Result != 0 || RHS != 0;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
break;
|
2008-11-13 10:13:11 +08:00
|
|
|
}
|
2008-07-08 22:35:21 +08:00
|
|
|
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2008-07-12 03:10:17 +08:00
|
|
|
return true;
|
2008-07-08 22:35:21 +08:00
|
|
|
}
|
|
|
|
|
2008-11-17 03:28:31 +08:00
|
|
|
bool IntExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
|
2008-11-17 06:06:39 +08:00
|
|
|
bool Cond;
|
|
|
|
if (!HandleConversionToBool(E->getCond(), Cond, Info))
|
2008-11-17 03:28:31 +08:00
|
|
|
return false;
|
|
|
|
|
2008-11-17 06:06:39 +08:00
|
|
|
return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
|
2008-11-17 03:28:31 +08:00
|
|
|
}
|
|
|
|
|
2009-01-25 05:53:27 +08:00
|
|
|
unsigned IntExprEvaluator::GetAlignOfType(QualType T) {
|
2009-01-25 05:09:06 +08:00
|
|
|
const Type *Ty = Info.Ctx.getCanonicalType(T).getTypePtr();
|
|
|
|
|
|
|
|
// __alignof__(void) = 1 as a gcc extension.
|
|
|
|
if (Ty->isVoidType())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// GCC extension: alignof(function) = 4.
|
|
|
|
// FIXME: AlignOf shouldn't be unconditionally 4! It should listen to the
|
|
|
|
// attribute(align) directive.
|
|
|
|
if (Ty->isFunctionType())
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty))
|
|
|
|
return GetAlignOfType(QualType(ASQT->getBaseType(), 0));
|
|
|
|
|
|
|
|
// alignof VLA/incomplete array.
|
|
|
|
if (const ArrayType *VAT = dyn_cast<ArrayType>(Ty))
|
|
|
|
return GetAlignOfType(VAT->getElementType());
|
|
|
|
|
|
|
|
// sizeof (objc class)?
|
|
|
|
if (isa<ObjCInterfaceType>(Ty))
|
|
|
|
return 1; // FIXME: This probably isn't right.
|
|
|
|
|
|
|
|
// Get information about the alignment.
|
|
|
|
unsigned CharSize = Info.Ctx.Target.getCharWidth();
|
|
|
|
return Info.Ctx.getTypeAlign(Ty) / CharSize;
|
|
|
|
}
|
|
|
|
|
2009-01-25 05:53:27 +08:00
|
|
|
unsigned IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
|
|
|
|
E = E->IgnoreParens();
|
|
|
|
|
|
|
|
// alignof decl is always accepted, even if it doesn't make sense: we default
|
|
|
|
// to 1 in those cases.
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
|
|
|
return Info.Ctx.getDeclAlign(DRE->getDecl());
|
|
|
|
|
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
|
|
|
return Info.Ctx.getDeclAlign(ME->getMemberDecl());
|
|
|
|
|
2009-01-25 05:09:06 +08:00
|
|
|
return GetAlignOfType(E->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
/// VisitSizeAlignOfExpr - Evaluate a sizeof or alignof with a result as the
|
|
|
|
/// expression's type.
|
|
|
|
bool IntExprEvaluator::VisitSizeOfAlignOfExpr(const SizeOfAlignOfExpr *E) {
|
|
|
|
QualType DstTy = E->getType();
|
2008-07-12 05:24:13 +08:00
|
|
|
// Return the result in the right width.
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(DstTy));
|
|
|
|
Result.setIsUnsigned(DstTy->isUnsignedIntegerType());
|
|
|
|
|
2009-01-25 05:09:06 +08:00
|
|
|
// Handle alignof separately.
|
|
|
|
if (!E->isSizeOf()) {
|
|
|
|
if (E->isArgumentType())
|
|
|
|
Result = GetAlignOfType(E->getArgumentType());
|
|
|
|
else
|
|
|
|
Result = GetAlignOfExpr(E->getArgumentExpr());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
QualType SrcTy = E->getTypeOfArgument();
|
|
|
|
|
2008-07-12 05:24:13 +08:00
|
|
|
// sizeof(void) and __alignof__(void) = 1 as a gcc extension.
|
2008-11-12 17:44:48 +08:00
|
|
|
if (SrcTy->isVoidType()) {
|
2008-07-12 05:24:13 +08:00
|
|
|
Result = 1;
|
2008-11-12 17:44:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-07-12 05:24:13 +08:00
|
|
|
|
|
|
|
// sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
|
2009-01-25 05:09:06 +08:00
|
|
|
if (!SrcTy->isConstantSizeType())
|
2008-07-12 05:24:13 +08:00
|
|
|
return false;
|
2009-01-16 09:42:12 +08:00
|
|
|
|
2008-07-12 05:24:13 +08:00
|
|
|
// GCC extension: sizeof(function) = 1.
|
|
|
|
if (SrcTy->isFunctionType()) {
|
2009-01-25 05:09:06 +08:00
|
|
|
Result = 1;
|
2008-07-12 05:24:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-25 06:19:05 +08:00
|
|
|
|
|
|
|
if (SrcTy->isObjCInterfaceType()) {
|
|
|
|
// Slightly unusual case: the size of an ObjC interface type is the
|
|
|
|
// size of the class. This code intentionally falls through to the normal
|
|
|
|
// case.
|
|
|
|
ObjCInterfaceDecl *OI = SrcTy->getAsObjCInterfaceType()->getDecl();
|
|
|
|
RecordDecl *RD = const_cast<RecordDecl*>(Info.Ctx.addRecordToClass(OI));
|
|
|
|
SrcTy = Info.Ctx.getTagDeclType(static_cast<TagDecl*>(RD));
|
|
|
|
}
|
|
|
|
|
2009-01-25 05:09:06 +08:00
|
|
|
// Get information about the size.
|
2008-07-12 06:52:41 +08:00
|
|
|
unsigned CharSize = Info.Ctx.Target.getCharWidth();
|
2009-01-25 05:09:06 +08:00
|
|
|
Result = Info.Ctx.getTypeSize(SrcTy) / CharSize;
|
2008-07-12 05:24:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
2008-07-12 08:38:25 +08:00
|
|
|
// Special case unary operators that do not need their subexpression
|
|
|
|
// evaluated. offsetof/sizeof/alignof are all special.
|
2008-07-12 06:15:16 +08:00
|
|
|
if (E->isOffsetOfOp()) {
|
2008-07-12 08:38:25 +08:00
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
2008-07-12 06:52:41 +08:00
|
|
|
Result = E->evaluateOffsetOf(Info.Ctx);
|
2008-07-12 06:15:16 +08:00
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-13 14:09:17 +08:00
|
|
|
|
|
|
|
if (E->getOpcode() == UnaryOperator::LNot) {
|
|
|
|
// LNot's operand isn't necessarily an integer, so we handle it specially.
|
|
|
|
bool bres;
|
|
|
|
if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
|
|
|
|
return false;
|
|
|
|
Result.zextOrTrunc(getIntTypeSizeInBits(E->getType()));
|
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
|
|
|
Result = !bres;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-12 06:52:41 +08:00
|
|
|
// Get the operand value into 'Result'.
|
|
|
|
if (!Visit(E->getSubExpr()))
|
2008-07-12 06:15:16 +08:00
|
|
|
return false;
|
2008-07-08 22:35:21 +08:00
|
|
|
|
2008-07-12 06:15:16 +08:00
|
|
|
switch (E->getOpcode()) {
|
2008-07-12 08:38:25 +08:00
|
|
|
default:
|
2008-07-12 06:15:16 +08:00
|
|
|
// Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
|
|
|
|
// See C99 6.6p3.
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getOperatorLoc(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 06:15:16 +08:00
|
|
|
case UnaryOperator::Extension:
|
2008-07-12 08:38:25 +08:00
|
|
|
// FIXME: Should extension allow i-c-e extension expressions in its scope?
|
|
|
|
// If so, we could clear the diagnostic ID.
|
2008-07-12 06:15:16 +08:00
|
|
|
case UnaryOperator::Plus:
|
2008-07-12 08:38:25 +08:00
|
|
|
// The result is always just the subexpr.
|
2008-07-12 06:15:16 +08:00
|
|
|
break;
|
|
|
|
case UnaryOperator::Minus:
|
|
|
|
Result = -Result;
|
|
|
|
break;
|
|
|
|
case UnaryOperator::Not:
|
|
|
|
Result = ~Result;
|
|
|
|
break;
|
2008-07-08 13:49:43 +08:00
|
|
|
}
|
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
Result.setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2008-07-12 03:10:17 +08:00
|
|
|
return true;
|
2008-07-08 22:35:21 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 09:15:53 +08:00
|
|
|
/// HandleCast - This is used to evaluate implicit or explicit casts where the
|
|
|
|
/// result type is integer.
|
2009-01-29 14:16:07 +08:00
|
|
|
bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
|
2008-12-01 02:14:57 +08:00
|
|
|
Expr *SubExpr = E->getSubExpr();
|
|
|
|
QualType DestType = E->getType();
|
|
|
|
|
2008-07-12 03:24:49 +08:00
|
|
|
unsigned DestWidth = getIntTypeSizeInBits(DestType);
|
2008-07-08 13:49:43 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
if (DestType->isBooleanType()) {
|
|
|
|
bool BoolResult;
|
|
|
|
if (!HandleConversionToBool(SubExpr, BoolResult, Info))
|
|
|
|
return false;
|
|
|
|
Result.zextOrTrunc(DestWidth);
|
|
|
|
Result.setIsUnsigned(DestType->isUnsignedIntegerType());
|
|
|
|
Result = BoolResult;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
// Handle simple integer->integer casts.
|
2008-11-13 14:09:17 +08:00
|
|
|
if (SubExpr->getType()->isIntegralType()) {
|
2008-07-12 09:15:53 +08:00
|
|
|
if (!Visit(SubExpr))
|
2008-07-12 03:10:17 +08:00
|
|
|
return false;
|
2009-01-29 14:16:07 +08:00
|
|
|
|
|
|
|
Result = HandleIntToIntCast(DestType, SubExpr->getType(), Result, Info.Ctx);
|
2008-07-12 09:15:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Clean this up!
|
|
|
|
if (SubExpr->getType()->isPointerType()) {
|
2008-07-08 22:35:21 +08:00
|
|
|
APValue LV;
|
2008-07-12 06:52:41 +08:00
|
|
|
if (!EvaluatePointer(SubExpr, LV, Info))
|
2008-07-12 03:10:17 +08:00
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
if (LV.getLValueBase())
|
2008-07-12 03:10:17 +08:00
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2008-07-09 00:49:00 +08:00
|
|
|
Result.extOrTrunc(DestWidth);
|
|
|
|
Result = LV.getLValueOffset();
|
2008-07-12 09:15:53 +08:00
|
|
|
Result.setIsUnsigned(DestType->isUnsignedIntegerType());
|
|
|
|
return true;
|
2008-07-08 22:30:00 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2008-07-12 09:15:53 +08:00
|
|
|
if (!SubExpr->getType()->isRealFloatingType())
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 09:15:53 +08:00
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
APFloat F(0.0);
|
|
|
|
if (!EvaluateFloat(SubExpr, F, Info))
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
|
2008-07-12 09:15:53 +08:00
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
Result = HandleFloatToIntCast(DestType, SubExpr->getType(), F, Info.Ctx);
|
2008-07-12 03:10:17 +08:00
|
|
|
return true;
|
2008-07-08 22:35:21 +08:00
|
|
|
}
|
2008-07-08 22:30:00 +08:00
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Float Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN FloatExprEvaluator
|
|
|
|
: public StmtVisitor<FloatExprEvaluator, bool> {
|
|
|
|
EvalInfo &Info;
|
|
|
|
APFloat &Result;
|
|
|
|
public:
|
|
|
|
FloatExprEvaluator(EvalInfo &info, APFloat &result)
|
|
|
|
: Info(info), Result(result) {}
|
|
|
|
|
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
2008-10-06 13:28:25 +08:00
|
|
|
bool VisitCallExpr(const CallExpr *E);
|
2008-08-22 08:06:13 +08:00
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
bool VisitUnaryOperator(const UnaryOperator *E);
|
2008-08-22 08:06:13 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
bool VisitFloatingLiteral(const FloatingLiteral *E);
|
2008-11-12 17:44:48 +08:00
|
|
|
bool VisitCastExpr(CastExpr *E);
|
|
|
|
bool VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
|
2008-08-22 08:06:13 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
|
|
|
|
return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
|
|
|
}
|
|
|
|
|
2008-10-06 13:28:25 +08:00
|
|
|
bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
|
|
|
switch (E->isBuiltinCall()) {
|
2008-10-06 13:53:16 +08:00
|
|
|
default: return false;
|
2008-10-06 13:28:25 +08:00
|
|
|
case Builtin::BI__builtin_huge_val:
|
|
|
|
case Builtin::BI__builtin_huge_valf:
|
|
|
|
case Builtin::BI__builtin_huge_vall:
|
|
|
|
case Builtin::BI__builtin_inf:
|
|
|
|
case Builtin::BI__builtin_inff:
|
2008-10-14 13:41:12 +08:00
|
|
|
case Builtin::BI__builtin_infl: {
|
|
|
|
const llvm::fltSemantics &Sem =
|
|
|
|
Info.Ctx.getFloatTypeSemantics(E->getType());
|
2008-10-06 13:53:16 +08:00
|
|
|
Result = llvm::APFloat::getInf(Sem);
|
|
|
|
return true;
|
2008-10-14 13:41:12 +08:00
|
|
|
}
|
2008-10-06 14:31:58 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_nan:
|
|
|
|
case Builtin::BI__builtin_nanf:
|
|
|
|
case Builtin::BI__builtin_nanl:
|
|
|
|
// If this is __builtin_nan("") turn this into a simple nan, otherwise we
|
|
|
|
// can't constant fold it.
|
|
|
|
if (const StringLiteral *S =
|
|
|
|
dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts())) {
|
|
|
|
if (!S->isWide() && S->getByteLength() == 0) { // empty string.
|
2008-10-14 13:41:12 +08:00
|
|
|
const llvm::fltSemantics &Sem =
|
|
|
|
Info.Ctx.getFloatTypeSemantics(E->getType());
|
2008-10-06 14:31:58 +08:00
|
|
|
Result = llvm::APFloat::getNaN(Sem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2008-10-16 11:51:50 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_fabs:
|
|
|
|
case Builtin::BI__builtin_fabsf:
|
|
|
|
case Builtin::BI__builtin_fabsl:
|
|
|
|
if (!EvaluateFloat(E->getArg(0), Result, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Result.isNegative())
|
|
|
|
Result.changeSign();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Builtin::BI__builtin_copysign:
|
|
|
|
case Builtin::BI__builtin_copysignf:
|
|
|
|
case Builtin::BI__builtin_copysignl: {
|
|
|
|
APFloat RHS(0.);
|
|
|
|
if (!EvaluateFloat(E->getArg(0), Result, Info) ||
|
|
|
|
!EvaluateFloat(E->getArg(1), RHS, Info))
|
|
|
|
return false;
|
|
|
|
Result.copySign(RHS);
|
|
|
|
return true;
|
|
|
|
}
|
2008-10-06 13:28:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
2008-11-20 01:44:31 +08:00
|
|
|
if (E->getOpcode() == UnaryOperator::Deref)
|
|
|
|
return false;
|
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
if (!EvaluateFloat(E->getSubExpr(), Result, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case UnaryOperator::Plus:
|
|
|
|
return true;
|
|
|
|
case UnaryOperator::Minus:
|
|
|
|
Result.changeSign();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-10-06 13:28:25 +08:00
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
|
|
|
// FIXME: Diagnostics? I really don't understand how the warnings
|
|
|
|
// and errors are supposed to work.
|
2008-10-16 11:51:50 +08:00
|
|
|
APFloat RHS(0.0);
|
2008-08-22 08:06:13 +08:00
|
|
|
if (!EvaluateFloat(E->getLHS(), Result, Info))
|
|
|
|
return false;
|
|
|
|
if (!EvaluateFloat(E->getRHS(), RHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: return false;
|
|
|
|
case BinaryOperator::Mul:
|
|
|
|
Result.multiply(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
|
|
|
case BinaryOperator::Add:
|
|
|
|
Result.add(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
|
|
|
case BinaryOperator::Sub:
|
|
|
|
Result.subtract(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
|
|
|
case BinaryOperator::Div:
|
|
|
|
Result.divide(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
|
|
|
case BinaryOperator::Rem:
|
|
|
|
Result.mod(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
|
|
|
|
Result = E->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
|
|
|
|
Expr* SubExpr = E->getSubExpr();
|
2009-01-18 11:20:47 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
if (SubExpr->getType()->isIntegralType()) {
|
|
|
|
APSInt IntResult;
|
|
|
|
if (!EvaluateInteger(E, IntResult, Info))
|
|
|
|
return false;
|
2009-01-29 14:16:07 +08:00
|
|
|
Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
|
|
|
|
IntResult, Info.Ctx);
|
2008-11-12 17:44:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (SubExpr->getType()->isRealFloatingType()) {
|
|
|
|
if (!Visit(SubExpr))
|
|
|
|
return false;
|
2009-01-29 14:16:07 +08:00
|
|
|
Result = HandleFloatToFloatCast(E->getType(), SubExpr->getType(),
|
|
|
|
Result, Info.Ctx);
|
2008-11-12 17:44:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FloatExprEvaluator::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
|
|
|
|
Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-17 04:27:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-01-29 06:24:07 +08:00
|
|
|
// Complex Evaluation (for float and integer)
|
2008-11-17 04:27:53 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-01-29 06:24:07 +08:00
|
|
|
class VISIBILITY_HIDDEN ComplexExprEvaluator
|
|
|
|
: public StmtVisitor<ComplexExprEvaluator, APValue> {
|
2008-11-17 04:27:53 +08:00
|
|
|
EvalInfo &Info;
|
|
|
|
|
|
|
|
public:
|
2009-01-29 06:24:07 +08:00
|
|
|
ComplexExprEvaluator(EvalInfo &info) : Info(info) {}
|
2008-11-17 04:27:53 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
APValue VisitStmt(Stmt *S) {
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
|
|
|
|
APValue VisitImaginaryLiteral(ImaginaryLiteral *E) {
|
2009-01-29 06:24:07 +08:00
|
|
|
Expr* SubExpr = E->getSubExpr();
|
|
|
|
|
|
|
|
if (SubExpr->getType()->isRealFloatingType()) {
|
|
|
|
APFloat Result(0.0);
|
|
|
|
|
|
|
|
if (!EvaluateFloat(SubExpr, Result, Info))
|
|
|
|
return APValue();
|
2008-11-17 04:27:53 +08:00
|
|
|
|
2009-01-29 09:32:56 +08:00
|
|
|
return APValue(APFloat(Result.getSemantics(), APFloat::fcZero, false),
|
2009-01-29 06:24:07 +08:00
|
|
|
Result);
|
|
|
|
} else {
|
|
|
|
assert(SubExpr->getType()->isIntegerType() &&
|
|
|
|
"Unexpected imaginary literal.");
|
|
|
|
|
|
|
|
llvm::APSInt Result;
|
|
|
|
if (!EvaluateInteger(SubExpr, Result, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
|
|
|
|
Zero = 0;
|
|
|
|
return APValue(Zero, Result);
|
|
|
|
}
|
2008-11-17 04:27:53 +08:00
|
|
|
}
|
|
|
|
|
2008-11-17 05:51:21 +08:00
|
|
|
APValue VisitCastExpr(CastExpr *E) {
|
|
|
|
Expr* SubExpr = E->getSubExpr();
|
2009-01-29 14:16:07 +08:00
|
|
|
QualType EltType = E->getType()->getAsComplexType()->getElementType();
|
|
|
|
QualType SubType = SubExpr->getType();
|
2008-11-17 05:51:21 +08:00
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
if (SubType->isRealFloatingType()) {
|
2008-11-17 05:51:21 +08:00
|
|
|
APFloat Result(0.0);
|
|
|
|
|
|
|
|
if (!EvaluateFloat(SubExpr, Result, Info))
|
|
|
|
return APValue();
|
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
// Apply float conversion if necessary.
|
|
|
|
Result = HandleFloatToFloatCast(EltType, SubType, Result, Info.Ctx);
|
2009-01-25 03:08:01 +08:00
|
|
|
return APValue(Result,
|
2009-01-29 09:32:56 +08:00
|
|
|
APFloat(Result.getSemantics(), APFloat::fcZero, false));
|
2009-01-29 14:16:07 +08:00
|
|
|
} else if (SubType->isIntegerType()) {
|
2009-01-29 06:24:07 +08:00
|
|
|
APSInt Result;
|
|
|
|
|
|
|
|
if (!EvaluateInteger(SubExpr, Result, Info))
|
|
|
|
return APValue();
|
2009-01-29 14:16:07 +08:00
|
|
|
|
|
|
|
// Apply integer conversion if necessary.
|
|
|
|
Result = HandleIntToIntCast(EltType, SubType, Result, Info.Ctx);
|
2009-01-29 06:24:07 +08:00
|
|
|
llvm::APSInt Zero(Result.getBitWidth(), !Result.isSigned());
|
|
|
|
Zero = 0;
|
|
|
|
return APValue(Result, Zero);
|
2009-01-29 14:16:07 +08:00
|
|
|
} else if (const ComplexType *CT = SubType->getAsComplexType()) {
|
|
|
|
APValue Src;
|
|
|
|
|
|
|
|
if (!EvaluateComplex(SubExpr, Src, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
QualType SrcType = CT->getElementType();
|
|
|
|
|
|
|
|
if (Src.isComplexFloat()) {
|
|
|
|
if (EltType->isRealFloatingType()) {
|
|
|
|
return APValue(HandleFloatToFloatCast(EltType, SrcType,
|
|
|
|
Src.getComplexFloatReal(),
|
|
|
|
Info.Ctx),
|
|
|
|
HandleFloatToFloatCast(EltType, SrcType,
|
|
|
|
Src.getComplexFloatImag(),
|
|
|
|
Info.Ctx));
|
|
|
|
} else {
|
|
|
|
return APValue(HandleFloatToIntCast(EltType, SrcType,
|
|
|
|
Src.getComplexFloatReal(),
|
|
|
|
Info.Ctx),
|
|
|
|
HandleFloatToIntCast(EltType, SrcType,
|
|
|
|
Src.getComplexFloatImag(),
|
|
|
|
Info.Ctx));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(Src.isComplexInt() && "Invalid evaluate result.");
|
|
|
|
if (EltType->isRealFloatingType()) {
|
|
|
|
return APValue(HandleIntToFloatCast(EltType, SrcType,
|
|
|
|
Src.getComplexIntReal(),
|
|
|
|
Info.Ctx),
|
|
|
|
HandleIntToFloatCast(EltType, SrcType,
|
|
|
|
Src.getComplexIntImag(),
|
|
|
|
Info.Ctx));
|
|
|
|
} else {
|
|
|
|
return APValue(HandleIntToIntCast(EltType, SrcType,
|
|
|
|
Src.getComplexIntReal(),
|
|
|
|
Info.Ctx),
|
|
|
|
HandleIntToIntCast(EltType, SrcType,
|
|
|
|
Src.getComplexIntImag(),
|
|
|
|
Info.Ctx));
|
|
|
|
}
|
|
|
|
}
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle more casts.
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
|
2008-11-17 04:27:53 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-01-29 06:24:07 +08:00
|
|
|
static bool EvaluateComplex(const Expr *E, APValue &Result, EvalInfo &Info)
|
2008-11-17 04:27:53 +08:00
|
|
|
{
|
2009-01-29 06:24:07 +08:00
|
|
|
Result = ComplexExprEvaluator(Info).Visit(const_cast<Expr*>(E));
|
|
|
|
assert((!Result.isComplexFloat() ||
|
|
|
|
(&Result.getComplexFloatReal().getSemantics() ==
|
|
|
|
&Result.getComplexFloatImag().getSemantics())) &&
|
|
|
|
"Invalid complex evaluation.");
|
|
|
|
return Result.isComplexFloat() || Result.isComplexInt();
|
2008-11-17 04:27:53 +08:00
|
|
|
}
|
|
|
|
|
2009-01-29 06:24:07 +08:00
|
|
|
APValue ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E)
|
2008-11-17 05:51:21 +08:00
|
|
|
{
|
|
|
|
APValue Result, RHS;
|
|
|
|
|
2009-01-29 06:24:07 +08:00
|
|
|
if (!EvaluateComplex(E->getLHS(), Result, Info))
|
2008-11-17 05:51:21 +08:00
|
|
|
return APValue();
|
|
|
|
|
2009-01-29 06:24:07 +08:00
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info))
|
2008-11-17 05:51:21 +08:00
|
|
|
return APValue();
|
2009-01-29 06:24:07 +08:00
|
|
|
|
2009-01-29 09:32:56 +08:00
|
|
|
assert(Result.isComplexFloat() == RHS.isComplexFloat() &&
|
|
|
|
"Invalid operands to binary operator.");
|
2008-11-17 05:51:21 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: return APValue();
|
|
|
|
case BinaryOperator::Add:
|
2009-01-29 06:24:07 +08:00
|
|
|
if (Result.isComplexFloat()) {
|
|
|
|
Result.getComplexFloatReal().add(RHS.getComplexFloatReal(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatImag().add(RHS.getComplexFloatImag(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
} else {
|
|
|
|
Result.getComplexIntReal() += RHS.getComplexIntReal();
|
|
|
|
Result.getComplexIntImag() += RHS.getComplexIntImag();
|
|
|
|
}
|
2009-01-29 09:32:56 +08:00
|
|
|
break;
|
2008-11-17 05:51:21 +08:00
|
|
|
case BinaryOperator::Sub:
|
2009-01-29 06:24:07 +08:00
|
|
|
if (Result.isComplexFloat()) {
|
|
|
|
Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
} else {
|
|
|
|
Result.getComplexIntReal() -= RHS.getComplexIntReal();
|
|
|
|
Result.getComplexIntImag() -= RHS.getComplexIntImag();
|
|
|
|
}
|
2009-01-29 09:32:56 +08:00
|
|
|
break;
|
|
|
|
case BinaryOperator::Mul:
|
|
|
|
if (Result.isComplexFloat()) {
|
|
|
|
APValue LHS = Result;
|
|
|
|
APFloat &LHS_r = LHS.getComplexFloatReal();
|
|
|
|
APFloat &LHS_i = LHS.getComplexFloatImag();
|
|
|
|
APFloat &RHS_r = RHS.getComplexFloatReal();
|
|
|
|
APFloat &RHS_i = RHS.getComplexFloatImag();
|
|
|
|
|
|
|
|
APFloat Tmp = LHS_r;
|
|
|
|
Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatReal() = Tmp;
|
|
|
|
Tmp = LHS_i;
|
|
|
|
Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatReal().subtract(Tmp, APFloat::rmNearestTiesToEven);
|
|
|
|
|
|
|
|
Tmp = LHS_r;
|
|
|
|
Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatImag() = Tmp;
|
|
|
|
Tmp = LHS_i;
|
|
|
|
Tmp.multiply(RHS_r, APFloat::rmNearestTiesToEven);
|
|
|
|
Result.getComplexFloatImag().add(Tmp, APFloat::rmNearestTiesToEven);
|
|
|
|
} else {
|
|
|
|
APValue LHS = Result;
|
|
|
|
Result.getComplexIntReal() =
|
|
|
|
(LHS.getComplexIntReal() * RHS.getComplexIntReal() -
|
|
|
|
LHS.getComplexIntImag() * RHS.getComplexIntImag());
|
|
|
|
Result.getComplexIntImag() =
|
|
|
|
(LHS.getComplexIntReal() * RHS.getComplexIntImag() +
|
|
|
|
LHS.getComplexIntImag() * RHS.getComplexIntReal());
|
|
|
|
}
|
|
|
|
break;
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-11-17 05:24:15 +08:00
|
|
|
// Top level Expr::Evaluate method.
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-11-17 05:24:15 +08:00
|
|
|
/// Evaluate - Return true if this is a constant which we can fold using
|
2008-10-06 13:28:25 +08:00
|
|
|
/// any crazy technique (that has nothing to do with language standards) that
|
|
|
|
/// we want to. If this function returns true, it returns the folded constant
|
|
|
|
/// in Result.
|
2008-12-01 00:58:53 +08:00
|
|
|
bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
|
|
|
|
EvalInfo Info(Ctx, Result);
|
2008-12-01 00:38:33 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
if (getType()->isVectorType()) {
|
|
|
|
if (!EvaluateVector(this, Result.Val, Info))
|
|
|
|
return false;
|
|
|
|
} else if (getType()->isIntegerType()) {
|
2008-08-22 08:06:13 +08:00
|
|
|
llvm::APSInt sInt(32);
|
2008-11-23 05:50:49 +08:00
|
|
|
if (!EvaluateInteger(this, sInt, Info))
|
|
|
|
return false;
|
|
|
|
|
2008-12-01 00:58:53 +08:00
|
|
|
Result.Val = APValue(sInt);
|
2008-08-22 08:06:13 +08:00
|
|
|
} else if (getType()->isPointerType()) {
|
2008-12-01 00:58:53 +08:00
|
|
|
if (!EvaluatePointer(this, Result.Val, Info))
|
2008-11-23 05:50:49 +08:00
|
|
|
return false;
|
2008-08-22 08:06:13 +08:00
|
|
|
} else if (getType()->isRealFloatingType()) {
|
|
|
|
llvm::APFloat f(0.0);
|
2008-11-23 05:50:49 +08:00
|
|
|
if (!EvaluateFloat(this, f, Info))
|
|
|
|
return false;
|
|
|
|
|
2008-12-01 00:58:53 +08:00
|
|
|
Result.Val = APValue(f);
|
2009-01-29 06:24:07 +08:00
|
|
|
} else if (getType()->isAnyComplexType()) {
|
|
|
|
if (!EvaluateComplex(this, Result.Val, Info))
|
2008-11-23 05:50:49 +08:00
|
|
|
return false;
|
2009-01-29 06:24:07 +08:00
|
|
|
} else
|
2008-11-23 06:56:32 +08:00
|
|
|
return false;
|
2008-11-23 05:50:49 +08:00
|
|
|
|
2008-12-01 00:58:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-17 05:24:15 +08:00
|
|
|
/// isEvaluatable - Call Evaluate to see if this expression can be constant
|
2008-10-06 14:49:02 +08:00
|
|
|
/// folded, but discard the result.
|
|
|
|
bool Expr::isEvaluatable(ASTContext &Ctx) const {
|
2008-12-01 14:44:05 +08:00
|
|
|
EvalResult Result;
|
|
|
|
return Evaluate(Result, Ctx) && !Result.HasSideEffects;
|
2008-10-06 14:49:02 +08:00
|
|
|
}
|
2008-11-23 05:04:56 +08:00
|
|
|
|
|
|
|
APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
|
2008-12-20 04:58:05 +08:00
|
|
|
EvalResult EvalResult;
|
|
|
|
bool Result = Evaluate(EvalResult, Ctx);
|
2009-01-16 02:32:35 +08:00
|
|
|
Result = Result;
|
2008-11-23 05:04:56 +08:00
|
|
|
assert(Result && "Could not evaluate expression");
|
2008-12-20 04:58:05 +08:00
|
|
|
assert(EvalResult.Val.isInt() && "Expression did not evaluate to integer");
|
2008-11-23 05:04:56 +08:00
|
|
|
|
2008-12-20 04:58:05 +08:00
|
|
|
return EvalResult.Val.getInt();
|
2008-11-23 05:04:56 +08:00
|
|
|
}
|