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"
|
2010-01-12 01:06:35 +08:00
|
|
|
#include "clang/AST/CharUnits.h"
|
2009-07-19 03:43:29 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2008-07-08 15:23:12 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
2009-01-29 13:15:15 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2008-07-08 13:49:43 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-05-30 22:43:18 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-05-30 11:56:50 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
2010-05-11 07:27:23 +08:00
|
|
|
EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult)
|
|
|
|
: Ctx(ctx), EvalResult(evalresult) {}
|
2008-07-12 06:52:41 +08:00
|
|
|
};
|
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
namespace {
|
|
|
|
struct ComplexValue {
|
|
|
|
private:
|
|
|
|
bool IsInt;
|
|
|
|
|
|
|
|
public:
|
|
|
|
APSInt IntReal, IntImag;
|
|
|
|
APFloat FloatReal, FloatImag;
|
|
|
|
|
|
|
|
ComplexValue() : FloatReal(APFloat::Bogus), FloatImag(APFloat::Bogus) {}
|
|
|
|
|
|
|
|
void makeComplexFloat() { IsInt = false; }
|
|
|
|
bool isComplexFloat() const { return !IsInt; }
|
|
|
|
APFloat &getComplexFloatReal() { return FloatReal; }
|
|
|
|
APFloat &getComplexFloatImag() { return FloatImag; }
|
|
|
|
|
|
|
|
void makeComplexInt() { IsInt = true; }
|
|
|
|
bool isComplexInt() const { return IsInt; }
|
|
|
|
APSInt &getComplexIntReal() { return IntReal; }
|
|
|
|
APSInt &getComplexIntImag() { return IntImag; }
|
|
|
|
|
|
|
|
void moveInto(APValue &v) {
|
|
|
|
if (isComplexFloat())
|
|
|
|
v = APValue(FloatReal, FloatImag);
|
|
|
|
else
|
|
|
|
v = APValue(IntReal, IntImag);
|
|
|
|
}
|
|
|
|
};
|
2010-05-08 05:00:08 +08:00
|
|
|
|
|
|
|
struct LValue {
|
|
|
|
Expr *Base;
|
|
|
|
CharUnits Offset;
|
|
|
|
|
|
|
|
Expr *getLValueBase() { return Base; }
|
|
|
|
CharUnits getLValueOffset() { return Offset; }
|
|
|
|
|
|
|
|
void moveInto(APValue &v) {
|
|
|
|
v = APValue(Base, Offset);
|
|
|
|
}
|
|
|
|
};
|
2010-05-08 01:22:02 +08:00
|
|
|
}
|
2008-07-12 06:52:41 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info);
|
|
|
|
static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info);
|
2008-07-12 06:52:41 +08:00
|
|
|
static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
|
2009-10-29 07:59:40 +08:00
|
|
|
static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
|
|
|
|
EvalInfo &Info);
|
2008-08-22 08:06:13 +08:00
|
|
|
static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
|
2010-05-08 01:22:02 +08:00
|
|
|
static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Misc utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-15 01:07:14 +08:00
|
|
|
static bool IsGlobalLValue(const Expr* E) {
|
2010-05-11 07:27:23 +08:00
|
|
|
if (!E) return true;
|
|
|
|
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
|
|
|
|
if (isa<FunctionDecl>(DRE->getDecl()))
|
|
|
|
return true;
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
|
|
|
|
return VD->hasGlobalStorage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(E))
|
|
|
|
return CLE->isFileScope();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
static bool EvalPointerValueAsBool(LValue& Value, bool& Result) {
|
|
|
|
const Expr* Base = Value.Base;
|
2010-05-07 23:18:43 +08:00
|
|
|
|
2010-05-08 05:34:32 +08:00
|
|
|
// A null base expression indicates a null pointer. These are always
|
|
|
|
// evaluatable, and they are false unless the offset is zero.
|
|
|
|
if (!Base) {
|
|
|
|
Result = !Value.Offset.isZero();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-11 07:27:23 +08:00
|
|
|
// Require the base expression to be a global l-value.
|
2010-05-15 01:07:14 +08:00
|
|
|
if (!IsGlobalLValue(Base)) return false;
|
2010-05-11 07:27:23 +08:00
|
|
|
|
2010-05-08 05:34:32 +08:00
|
|
|
// We have a non-null base expression. These are generally known to
|
|
|
|
// be true, but if it'a decl-ref to a weak symbol it can be null at
|
|
|
|
// runtime.
|
|
|
|
Result = true;
|
2010-05-07 23:18:43 +08:00
|
|
|
|
2010-05-08 05:34:32 +08:00
|
|
|
const DeclRefExpr* DeclRef = dyn_cast<DeclRefExpr>(Base);
|
2010-05-07 23:18:43 +08:00
|
|
|
if (!DeclRef)
|
|
|
|
return true;
|
|
|
|
|
2010-05-08 05:34:32 +08:00
|
|
|
// If it's a weak symbol, it isn't constant-evaluable.
|
2010-05-07 23:18:43 +08:00
|
|
|
const ValueDecl* Decl = DeclRef->getDecl();
|
|
|
|
if (Decl->hasAttr<WeakAttr>() ||
|
|
|
|
Decl->hasAttr<WeakRefAttr>() ||
|
|
|
|
Decl->hasAttr<WeakImportAttr>())
|
|
|
|
return false;
|
|
|
|
|
2009-06-14 10:17:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-06 07:42:56 +08:00
|
|
|
static bool HandleConversionToBool(const Expr* E, bool& Result,
|
|
|
|
EvalInfo &Info) {
|
2010-06-16 08:17:44 +08:00
|
|
|
if (E->getType()->isIntegralOrEnumerationType()) {
|
2008-11-12 17:44:48 +08:00
|
|
|
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;
|
2009-03-23 12:38:34 +08:00
|
|
|
} else if (E->getType()->hasPointerRepresentation()) {
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue PointerResult;
|
2008-11-12 17:44:48 +08:00
|
|
|
if (!EvaluatePointer(E, PointerResult, Info))
|
|
|
|
return false;
|
2009-06-14 10:17:33 +08:00
|
|
|
return EvalPointerValueAsBool(PointerResult, Result);
|
2009-03-23 12:38:34 +08:00
|
|
|
} else if (E->getType()->isAnyComplexType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue ComplexResult;
|
2009-03-23 12:38:34 +08:00
|
|
|
if (!EvaluateComplex(E, ComplexResult, Info))
|
|
|
|
return false;
|
|
|
|
if (ComplexResult.isComplexFloat()) {
|
|
|
|
Result = !ComplexResult.getComplexFloatReal().isZero() ||
|
|
|
|
!ComplexResult.getComplexFloatImag().isZero();
|
|
|
|
} else {
|
|
|
|
Result = ComplexResult.getComplexIntReal().getBoolValue() ||
|
|
|
|
ComplexResult.getComplexIntImag().getBoolValue();
|
|
|
|
}
|
|
|
|
return true;
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
static APSInt HandleFloatToIntCast(QualType DestType, QualType SrcType,
|
2009-01-29 14:16:07 +08:00
|
|
|
APFloat &Value, ASTContext &Ctx) {
|
|
|
|
unsigned DestWidth = Ctx.getIntWidth(DestType);
|
|
|
|
// Determine whether we are converting to unsigned or signed.
|
|
|
|
bool DestSigned = DestType->isSignedIntegerType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-29 14:16:07 +08:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
static APFloat HandleFloatToFloatCast(QualType DestType, QualType SrcType,
|
2009-01-29 14:16:07 +08:00
|
|
|
APFloat &Value, ASTContext &Ctx) {
|
|
|
|
bool ignored;
|
|
|
|
APFloat Result = Value;
|
2009-09-09 23:08:12 +08:00
|
|
|
Result.convert(Ctx.getFloatTypeSemantics(DestType),
|
2009-01-29 14:16:07 +08:00
|
|
|
APFloat::rmNearestTiesToEven, &ignored);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
static APSInt HandleIntToIntCast(QualType DestType, QualType SrcType,
|
2009-01-29 14:16:07 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
static APFloat HandleIntToFloatCast(QualType DestType, QualType SrcType,
|
2009-01-29 14:16:07 +08:00
|
|
|
APSInt &Value, ASTContext &Ctx) {
|
|
|
|
|
|
|
|
APFloat Result(Ctx.getFloatTypeSemantics(DestType), 1);
|
|
|
|
Result.convertFromAPInt(Value, Value.isSigned(),
|
|
|
|
APFloat::rmNearestTiesToEven);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-10-28 06:09:17 +08:00
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class HasSideEffect
|
2009-10-28 06:09:17 +08:00
|
|
|
: public StmtVisitor<HasSideEffect, bool> {
|
|
|
|
EvalInfo &Info;
|
|
|
|
public:
|
|
|
|
|
|
|
|
HasSideEffect(EvalInfo &info) : Info(info) {}
|
|
|
|
|
|
|
|
// Unhandled nodes conservatively default to having side effects.
|
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *E) {
|
2009-11-04 07:25:48 +08:00
|
|
|
if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
|
2009-10-28 06:09:17 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We don't want to evaluate BlockExprs multiple times, as they generate
|
|
|
|
// a ton of code.
|
|
|
|
bool VisitBlockExpr(BlockExpr *E) { return true; }
|
|
|
|
bool VisitPredefinedExpr(PredefinedExpr *E) { return false; }
|
|
|
|
bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
|
|
|
|
{ return Visit(E->getInitializer()); }
|
|
|
|
bool VisitMemberExpr(MemberExpr *E) { return Visit(E->getBase()); }
|
|
|
|
bool VisitIntegerLiteral(IntegerLiteral *E) { return false; }
|
|
|
|
bool VisitFloatingLiteral(FloatingLiteral *E) { return false; }
|
|
|
|
bool VisitStringLiteral(StringLiteral *E) { return false; }
|
|
|
|
bool VisitCharacterLiteral(CharacterLiteral *E) { return false; }
|
|
|
|
bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { return false; }
|
|
|
|
bool VisitArraySubscriptExpr(ArraySubscriptExpr *E)
|
2009-10-30 04:48:09 +08:00
|
|
|
{ return Visit(E->getLHS()) || Visit(E->getRHS()); }
|
2009-10-28 06:09:17 +08:00
|
|
|
bool VisitChooseExpr(ChooseExpr *E)
|
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
|
|
|
bool VisitCastExpr(CastExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
bool VisitBinAssign(BinaryOperator *E) { return true; }
|
2009-10-30 07:34:20 +08:00
|
|
|
bool VisitCompoundAssignOperator(BinaryOperator *E) { return true; }
|
2009-10-30 04:48:09 +08:00
|
|
|
bool VisitBinaryOperator(BinaryOperator *E)
|
|
|
|
{ return Visit(E->getLHS()) || Visit(E->getRHS()); }
|
2009-10-28 06:09:17 +08:00
|
|
|
bool VisitUnaryPreInc(UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPostInc(UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPreDec(UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPostDec(UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryDeref(UnaryOperator *E) {
|
2009-11-04 07:25:48 +08:00
|
|
|
if (Info.Ctx.getCanonicalType(E->getType()).isVolatileQualified())
|
2009-10-28 06:09:17 +08:00
|
|
|
return true;
|
2009-10-30 04:48:09 +08:00
|
|
|
return Visit(E->getSubExpr());
|
2009-10-28 06:09:17 +08:00
|
|
|
}
|
|
|
|
bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
|
2010-04-14 01:34:23 +08:00
|
|
|
|
|
|
|
// Has side effects if any element does.
|
|
|
|
bool VisitInitListExpr(InitListExpr *E) {
|
|
|
|
for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
|
|
|
|
if (Visit(E->getInit(i))) return true;
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-28 06:09:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LValue Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class LValueExprEvaluator
|
2010-05-08 05:00:08 +08:00
|
|
|
: public StmtVisitor<LValueExprEvaluator, bool> {
|
2008-11-12 17:44:48 +08:00
|
|
|
EvalInfo &Info;
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue &Result;
|
|
|
|
|
|
|
|
bool Success(Expr *E) {
|
|
|
|
Result.Base = E;
|
|
|
|
Result.Offset = CharUnits::Zero();
|
|
|
|
return true;
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LValueExprEvaluator(EvalInfo &info, LValue &Result) :
|
|
|
|
Info(info), Result(Result) {}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
bool VisitDeclRefExpr(DeclRefExpr *E);
|
|
|
|
bool VisitPredefinedExpr(PredefinedExpr *E) { return Success(E); }
|
|
|
|
bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
|
|
|
|
bool VisitMemberExpr(MemberExpr *E);
|
|
|
|
bool VisitStringLiteral(StringLiteral *E) { return Success(E); }
|
|
|
|
bool VisitObjCEncodeExpr(ObjCEncodeExpr *E) { return Success(E); }
|
|
|
|
bool VisitArraySubscriptExpr(ArraySubscriptExpr *E);
|
|
|
|
bool VisitUnaryDeref(UnaryOperator *E);
|
|
|
|
bool VisitUnaryExtension(const UnaryOperator *E)
|
2009-03-23 12:56:01 +08:00
|
|
|
{ return Visit(E->getSubExpr()); }
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitChooseExpr(const ChooseExpr *E)
|
2009-03-23 12:56:01 +08:00
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
2009-10-04 00:30:22 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitCastExpr(CastExpr *E) {
|
2009-10-04 00:30:22 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-10-04 00:30:22 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_NoOp:
|
2009-10-04 00:30:22 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
}
|
2009-03-23 12:56:01 +08:00
|
|
|
// FIXME: Missing: __real__, __imag__
|
2008-11-12 17:44:48 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
|
|
|
|
return LValueExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool LValueExprEvaluator::VisitDeclRefExpr(DeclRefExpr *E) {
|
2009-05-27 14:04:58 +08:00
|
|
|
if (isa<FunctionDecl>(E->getDecl())) {
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
2009-05-27 14:04:58 +08:00
|
|
|
} else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
|
|
|
|
if (!VD->getType()->isReferenceType())
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
2010-05-16 17:32:51 +08:00
|
|
|
// Reference parameters can refer to anything even if they have an
|
|
|
|
// "initializer" in the form of a default argument.
|
|
|
|
if (isa<ParmVarDecl>(VD))
|
|
|
|
return false;
|
2009-08-30 03:09:59 +08:00
|
|
|
// FIXME: Check whether VD might be overridden!
|
2010-02-02 04:16:42 +08:00
|
|
|
if (const Expr *Init = VD->getAnyInitializer())
|
2009-11-02 04:32:48 +08:00
|
|
|
return Visit(const_cast<Expr *>(Init));
|
2009-05-27 14:04:58 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2008-11-24 12:41:22 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
|
|
|
|
return Success(E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
|
2008-11-12 17:44:48 +08:00
|
|
|
QualType Ty;
|
|
|
|
if (E->isArrow()) {
|
2010-05-08 05:00:08 +08:00
|
|
|
if (!EvaluatePointer(E->getBase(), Result, Info))
|
|
|
|
return false;
|
2009-07-30 05:53:49 +08:00
|
|
|
Ty = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
|
2008-11-12 17:44:48 +08:00
|
|
|
} else {
|
2010-05-08 05:00:08 +08:00
|
|
|
if (!Visit(E->getBase()))
|
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
Ty = E->getBase()->getType();
|
|
|
|
}
|
|
|
|
|
2009-07-30 05:53:49 +08:00
|
|
|
RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
|
2008-11-12 17:44:48 +08:00
|
|
|
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
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-05-31 05:09:44 +08:00
|
|
|
|
|
|
|
if (FD->getType()->isReferenceType())
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-05-31 05:09:44 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
// FIXME: This is linear time.
|
2008-12-12 00:49:14 +08:00
|
|
|
unsigned i = 0;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
2008-12-12 00:49:14 +08:00
|
|
|
Field != FieldEnd; (void)++Field, ++i) {
|
|
|
|
if (*Field == FD)
|
2008-11-12 17:44:48 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
Result.Offset += CharUnits::fromQuantity(RL.getFieldOffset(i) / 8);
|
|
|
|
return true;
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool LValueExprEvaluator::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
|
2008-11-17 03:01:22 +08:00
|
|
|
if (!EvaluatePointer(E->getBase(), Result, Info))
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-17 03:01:22 +08:00
|
|
|
APSInt Index;
|
|
|
|
if (!EvaluateInteger(E->getIdx(), Index, Info))
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2008-11-17 03:01:22 +08:00
|
|
|
|
2010-01-12 01:06:35 +08:00
|
|
|
CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(E->getType());
|
2010-05-08 05:00:08 +08:00
|
|
|
Result.Offset += Index.getSExtValue() * ElementSize;
|
|
|
|
return true;
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool LValueExprEvaluator::VisitUnaryDeref(UnaryOperator *E) {
|
|
|
|
return EvaluatePointer(E->getSubExpr(), Result, Info);
|
2009-02-20 09:57:15 +08:00
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pointer Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-07-08 22:30:00 +08:00
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class PointerExprEvaluator
|
2010-05-08 05:00:08 +08:00
|
|
|
: public StmtVisitor<PointerExprEvaluator, bool> {
|
2008-07-12 06:52:41 +08:00
|
|
|
EvalInfo &Info;
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue &Result;
|
|
|
|
|
|
|
|
bool Success(Expr *E) {
|
|
|
|
Result.Base = E;
|
|
|
|
Result.Offset = CharUnits::Zero();
|
|
|
|
return true;
|
|
|
|
}
|
2008-07-08 22:30:00 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
PointerExprEvaluator(EvalInfo &info, LValue &Result)
|
|
|
|
: Info(info), Result(Result) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
return false;
|
2008-07-08 22:30:00 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
2008-07-08 22:30:00 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
bool VisitCastExpr(CastExpr* E);
|
|
|
|
bool VisitUnaryExtension(const UnaryOperator *E)
|
2009-02-22 19:46:18 +08:00
|
|
|
{ return Visit(E->getSubExpr()); }
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitUnaryAddrOf(const UnaryOperator *E);
|
|
|
|
bool VisitObjCStringLiteral(ObjCStringLiteral *E)
|
|
|
|
{ return Success(E); }
|
|
|
|
bool VisitAddrLabelExpr(AddrLabelExpr *E)
|
|
|
|
{ return Success(E); }
|
|
|
|
bool VisitCallExpr(CallExpr *E);
|
|
|
|
bool VisitBlockExpr(BlockExpr *E) {
|
2009-02-20 06:01:56 +08:00
|
|
|
if (!E->hasBlockDeclRefExprs())
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
|
|
|
return false;
|
2009-02-20 06:01:56 +08:00
|
|
|
}
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitImplicitValueInitExpr(ImplicitValueInitExpr *E)
|
|
|
|
{ return Success((Expr*)0); }
|
|
|
|
bool VisitConditionalOperator(ConditionalOperator *E);
|
|
|
|
bool VisitChooseExpr(ChooseExpr *E)
|
2009-05-11 02:38:11 +08:00
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E)
|
|
|
|
{ return Success((Expr*)0); }
|
2009-03-23 12:56:01 +08:00
|
|
|
// FIXME: Missing: @protocol, @selector
|
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
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info) {
|
2010-05-07 13:46:35 +08:00
|
|
|
assert(E->getType()->hasPointerRepresentation());
|
2010-05-08 05:00:08 +08:00
|
|
|
return PointerExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
2008-07-12 02:11:29 +08:00
|
|
|
}
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() != BO_Add &&
|
|
|
|
E->getOpcode() != BO_Sub)
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
if (!EvaluatePointer(PExp, Result, Info))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
llvm::APSInt Offset;
|
|
|
|
if (!EvaluateInteger(IExp, Offset, Info))
|
|
|
|
return false;
|
|
|
|
int64_t AdditionalOffset
|
|
|
|
= Offset.isSigned() ? Offset.getSExtValue()
|
|
|
|
: static_cast<int64_t>(Offset.getZExtValue());
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2010-03-20 13:53:45 +08:00
|
|
|
// Compute the new offset in the appropriate width.
|
|
|
|
|
|
|
|
QualType PointeeType =
|
|
|
|
PExp->getType()->getAs<PointerType>()->getPointeeType();
|
2010-05-08 05:00:08 +08:00
|
|
|
CharUnits SizeOfPointee;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-19 12:55:58 +08:00
|
|
|
// Explicitly handle GNU void* and function pointer arithmetic extensions.
|
|
|
|
if (PointeeType->isVoidType() || PointeeType->isFunctionType())
|
2010-05-08 05:00:08 +08:00
|
|
|
SizeOfPointee = CharUnits::One();
|
2009-02-19 12:55:58 +08:00
|
|
|
else
|
2010-05-08 05:00:08 +08:00
|
|
|
SizeOfPointee = Info.Ctx.getTypeSizeInChars(PointeeType);
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Add)
|
2010-05-08 05:00:08 +08:00
|
|
|
Result.Offset += AdditionalOffset * SizeOfPointee;
|
2008-07-08 23:34:11 +08:00
|
|
|
else
|
2010-05-08 05:00:08 +08:00
|
|
|
Result.Offset -= AdditionalOffset * SizeOfPointee;
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
return true;
|
2008-07-08 23:34:11 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
|
|
|
|
return EvaluateLValue(E->getSubExpr(), Result, Info);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool PointerExprEvaluator::VisitCastExpr(CastExpr* E) {
|
2009-12-27 13:43:15 +08:00
|
|
|
Expr* SubExpr = E->getSubExpr();
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2009-12-27 13:43:15 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_Unknown: {
|
2009-12-27 13:43:15 +08:00
|
|
|
// FIXME: The handling for CK_Unknown is ugly/shouldn't be necessary!
|
|
|
|
|
|
|
|
// Check for pointer->pointer cast
|
|
|
|
if (SubExpr->getType()->isPointerType() ||
|
|
|
|
SubExpr->getType()->isObjCObjectPointerType() ||
|
|
|
|
SubExpr->getType()->isNullPtrType() ||
|
|
|
|
SubExpr->getType()->isBlockPointerType())
|
|
|
|
return Visit(SubExpr);
|
|
|
|
|
2010-06-16 08:17:44 +08:00
|
|
|
if (SubExpr->getType()->isIntegralOrEnumerationType()) {
|
2010-05-08 05:00:08 +08:00
|
|
|
APValue Value;
|
|
|
|
if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
|
2009-12-27 13:43:15 +08:00
|
|
|
break;
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
if (Value.isInt()) {
|
|
|
|
Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
|
|
|
|
Result.Base = 0;
|
|
|
|
Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Result.Base = Value.getLValueBase();
|
|
|
|
Result.Offset = Value.getLValueOffset();
|
|
|
|
return true;
|
2009-12-27 13:43:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-07-08 23:34:11 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_NoOp:
|
|
|
|
case CK_BitCast:
|
|
|
|
case CK_LValueBitCast:
|
|
|
|
case CK_AnyPointerToObjCPointerCast:
|
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
2009-12-27 13:43:15 +08:00
|
|
|
return Visit(SubExpr);
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralToPointer: {
|
2010-05-08 05:00:08 +08:00
|
|
|
APValue Value;
|
|
|
|
if (!EvaluateIntegerOrLValue(SubExpr, Value, Info))
|
2009-12-27 13:43:15 +08:00
|
|
|
break;
|
2009-02-21 02:22:23 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
if (Value.isInt()) {
|
|
|
|
Value.getInt().extOrTrunc((unsigned)Info.Ctx.getTypeSize(E->getType()));
|
|
|
|
Result.Base = 0;
|
|
|
|
Result.Offset = CharUnits::fromQuantity(Value.getInt().getZExtValue());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Cast is of an lvalue, no need to change value.
|
|
|
|
Result.Base = Value.getLValueBase();
|
|
|
|
Result.Offset = Value.getLValueOffset();
|
|
|
|
return true;
|
2008-07-08 23:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
2010-05-08 05:00:08 +08:00
|
|
|
return EvaluateLValue(SubExpr, Result, Info);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool PointerExprEvaluator::VisitCallExpr(CallExpr *E) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (E->isBuiltinCall(Info.Ctx) ==
|
2010-01-23 10:40:42 +08:00
|
|
|
Builtin::BI__builtin___CFStringMakeConstantString ||
|
|
|
|
E->isBuiltinCall(Info.Ctx) ==
|
|
|
|
Builtin::BI__builtin___NSStringMakeConstantString)
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
|
|
|
return false;
|
2009-01-25 09:54:01 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool PointerExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
|
2008-11-12 17:44:48 +08:00
|
|
|
bool BoolResult;
|
|
|
|
if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
|
|
|
|
Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
|
2010-05-08 05:00:08 +08:00
|
|
|
return Visit(EvalExpr);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Vector Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class VectorExprEvaluator
|
2009-01-18 11:20:47 +08:00
|
|
|
: public StmtVisitor<VectorExprEvaluator, APValue> {
|
|
|
|
EvalInfo &Info;
|
2009-02-23 12:23:56 +08:00
|
|
|
APValue GetZeroVector(QualType VecType);
|
2009-01-18 11:20:47 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
VectorExprEvaluator(EvalInfo &info) : Info(info) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
APValue VisitStmt(Stmt *S) {
|
|
|
|
return APValue();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-23 12:23:56 +08:00
|
|
|
APValue VisitParenExpr(ParenExpr *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
|
|
|
APValue VisitUnaryExtension(const UnaryOperator *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
|
|
|
APValue VisitUnaryPlus(const UnaryOperator *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
|
|
|
APValue VisitUnaryReal(const UnaryOperator *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
|
|
|
APValue VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
|
|
|
|
{ return GetZeroVector(E->getType()); }
|
2009-01-18 11:20:47 +08:00
|
|
|
APValue VisitCastExpr(const CastExpr* E);
|
|
|
|
APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
|
|
|
|
APValue VisitInitListExpr(const InitListExpr *E);
|
2009-02-23 12:23:56 +08:00
|
|
|
APValue VisitConditionalOperator(const ConditionalOperator *E);
|
2009-03-23 12:56:01 +08:00
|
|
|
APValue VisitChooseExpr(const ChooseExpr *E)
|
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
2009-02-23 12:23:56 +08:00
|
|
|
APValue VisitUnaryImag(const UnaryOperator *E);
|
|
|
|
// FIXME: Missing: unary -, unary ~, binary add/sub/mul/div,
|
2009-02-22 19:46:18 +08:00
|
|
|
// binary comparisons, binary and/or/xor,
|
2009-02-23 12:23:56 +08:00
|
|
|
// shufflevector, ExtVectorElementExpr
|
|
|
|
// (Note that these require implementing conversions
|
|
|
|
// between vector types.)
|
2009-01-18 11:20:47 +08:00
|
|
|
};
|
|
|
|
} // 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) {
|
2009-09-22 07:43:11 +08:00
|
|
|
const VectorType *VTy = E->getType()->getAs<VectorType>();
|
2009-07-01 15:50:47 +08:00
|
|
|
QualType EltTy = VTy->getElementType();
|
|
|
|
unsigned NElts = VTy->getNumElements();
|
|
|
|
unsigned EltWidth = Info.Ctx.getTypeSize(EltTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
const Expr* SE = E->getSubExpr();
|
2009-06-27 02:22:18 +08:00
|
|
|
QualType SETy = SE->getType();
|
|
|
|
APValue Result = APValue();
|
2009-01-18 11:20:47 +08:00
|
|
|
|
2009-06-27 02:22:18 +08:00
|
|
|
// Check for vector->vector bitcast and scalar->vector splat.
|
|
|
|
if (SETy->isVectorType()) {
|
2009-01-18 11:20:47 +08:00
|
|
|
return this->Visit(const_cast<Expr*>(SE));
|
2009-06-27 02:22:18 +08:00
|
|
|
} else if (SETy->isIntegerType()) {
|
|
|
|
APSInt IntResult;
|
2009-07-02 04:37:45 +08:00
|
|
|
if (!EvaluateInteger(SE, IntResult, Info))
|
|
|
|
return APValue();
|
|
|
|
Result = APValue(IntResult);
|
2009-06-27 02:22:18 +08:00
|
|
|
} else if (SETy->isRealFloatingType()) {
|
|
|
|
APFloat F(0.0);
|
2009-07-02 04:37:45 +08:00
|
|
|
if (!EvaluateFloat(SE, F, Info))
|
|
|
|
return APValue();
|
|
|
|
Result = APValue(F);
|
|
|
|
} else
|
2009-07-01 15:50:47 +08:00
|
|
|
return APValue();
|
2009-01-18 11:20:47 +08:00
|
|
|
|
2009-07-01 15:50:47 +08:00
|
|
|
// For casts of a scalar to ExtVector, convert the scalar to the element type
|
|
|
|
// and splat it to all elements.
|
|
|
|
if (E->getType()->isExtVectorType()) {
|
|
|
|
if (EltTy->isIntegerType() && Result.isInt())
|
|
|
|
Result = APValue(HandleIntToIntCast(EltTy, SETy, Result.getInt(),
|
|
|
|
Info.Ctx));
|
|
|
|
else if (EltTy->isIntegerType())
|
|
|
|
Result = APValue(HandleFloatToIntCast(EltTy, SETy, Result.getFloat(),
|
|
|
|
Info.Ctx));
|
|
|
|
else if (EltTy->isRealFloatingType() && Result.isInt())
|
|
|
|
Result = APValue(HandleIntToFloatCast(EltTy, SETy, Result.getInt(),
|
|
|
|
Info.Ctx));
|
|
|
|
else if (EltTy->isRealFloatingType())
|
|
|
|
Result = APValue(HandleFloatToFloatCast(EltTy, SETy, Result.getFloat(),
|
|
|
|
Info.Ctx));
|
|
|
|
else
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
// Splat and create vector APValue.
|
|
|
|
llvm::SmallVector<APValue, 4> Elts(NElts, Result);
|
|
|
|
return APValue(&Elts[0], Elts.size());
|
2009-06-27 02:22:18 +08:00
|
|
|
}
|
2009-07-01 15:50:47 +08:00
|
|
|
|
|
|
|
// For casts of a scalar to regular gcc-style vector type, bitcast the scalar
|
|
|
|
// to the vector. To construct the APValue vector initializer, bitcast the
|
|
|
|
// initializing value to an APInt, and shift out the bits pertaining to each
|
|
|
|
// element.
|
|
|
|
APSInt Init;
|
|
|
|
Init = Result.isInt() ? Result.getInt() : Result.getFloat().bitcastToAPInt();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-01 15:50:47 +08:00
|
|
|
llvm::SmallVector<APValue, 4> Elts;
|
|
|
|
for (unsigned i = 0; i != NElts; ++i) {
|
|
|
|
APSInt Tmp = Init;
|
|
|
|
Tmp.extOrTrunc(EltWidth);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-01 15:50:47 +08:00
|
|
|
if (EltTy->isIntegerType())
|
|
|
|
Elts.push_back(APValue(Tmp));
|
|
|
|
else if (EltTy->isRealFloatingType())
|
|
|
|
Elts.push_back(APValue(APFloat(Tmp)));
|
|
|
|
else
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
Init >>= EltWidth;
|
|
|
|
}
|
|
|
|
return APValue(&Elts[0], Elts.size());
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
APValue
|
2009-01-18 11:20:47 +08:00
|
|
|
VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
|
|
|
return this->Visit(const_cast<Expr*>(E->getInitializer()));
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
APValue
|
2009-01-18 11:20:47 +08:00
|
|
|
VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
2009-09-22 07:43:11 +08:00
|
|
|
const VectorType *VT = E->getType()->getAs<VectorType>();
|
2009-01-18 11:20:47 +08:00
|
|
|
unsigned NumInits = E->getNumInits();
|
2009-02-23 12:23:56 +08:00
|
|
|
unsigned NumElements = VT->getNumElements();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
QualType EltTy = VT->getElementType();
|
|
|
|
llvm::SmallVector<APValue, 4> Elements;
|
|
|
|
|
2010-06-12 01:54:15 +08:00
|
|
|
// If a vector is initialized with a single element, that value
|
|
|
|
// becomes every element of the vector, not just the first.
|
|
|
|
// This is the behavior described in the IBM AltiVec documentation.
|
|
|
|
if (NumInits == 1) {
|
|
|
|
APValue InitValue;
|
2009-01-18 11:20:47 +08:00
|
|
|
if (EltTy->isIntegerType()) {
|
|
|
|
llvm::APSInt sInt(32);
|
2010-06-12 01:54:15 +08:00
|
|
|
if (!EvaluateInteger(E->getInit(0), sInt, Info))
|
|
|
|
return APValue();
|
|
|
|
InitValue = APValue(sInt);
|
2009-01-18 11:20:47 +08:00
|
|
|
} else {
|
|
|
|
llvm::APFloat f(0.0);
|
2010-06-12 01:54:15 +08:00
|
|
|
if (!EvaluateFloat(E->getInit(0), f, Info))
|
|
|
|
return APValue();
|
|
|
|
InitValue = APValue(f);
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < NumElements; i++) {
|
|
|
|
Elements.push_back(InitValue);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < NumElements; i++) {
|
|
|
|
if (EltTy->isIntegerType()) {
|
|
|
|
llvm::APSInt sInt(32);
|
|
|
|
if (i < NumInits) {
|
|
|
|
if (!EvaluateInteger(E->getInit(i), sInt, Info))
|
|
|
|
return APValue();
|
|
|
|
} else {
|
|
|
|
sInt = Info.Ctx.MakeIntValue(0, EltTy);
|
|
|
|
}
|
|
|
|
Elements.push_back(APValue(sInt));
|
2009-02-23 12:23:56 +08:00
|
|
|
} else {
|
2010-06-12 01:54:15 +08:00
|
|
|
llvm::APFloat f(0.0);
|
|
|
|
if (i < NumInits) {
|
|
|
|
if (!EvaluateFloat(E->getInit(i), f, Info))
|
|
|
|
return APValue();
|
|
|
|
} else {
|
|
|
|
f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
|
|
|
|
}
|
|
|
|
Elements.push_back(APValue(f));
|
2009-02-23 12:23:56 +08:00
|
|
|
}
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return APValue(&Elements[0], Elements.size());
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
APValue
|
2009-02-23 12:23:56 +08:00
|
|
|
VectorExprEvaluator::GetZeroVector(QualType T) {
|
2009-09-22 07:43:11 +08:00
|
|
|
const VectorType *VT = T->getAs<VectorType>();
|
2009-02-23 12:23:56 +08:00
|
|
|
QualType EltTy = VT->getElementType();
|
|
|
|
APValue ZeroElement;
|
|
|
|
if (EltTy->isIntegerType())
|
|
|
|
ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy));
|
|
|
|
else
|
|
|
|
ZeroElement =
|
|
|
|
APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)));
|
|
|
|
|
|
|
|
llvm::SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
|
|
|
|
return APValue(&Elements[0], Elements.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VectorExprEvaluator::VisitConditionalOperator(const ConditionalOperator *E) {
|
|
|
|
bool BoolResult;
|
|
|
|
if (!HandleConversionToBool(E->getCond(), BoolResult, Info))
|
|
|
|
return APValue();
|
|
|
|
|
|
|
|
Expr* EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
|
|
|
|
|
|
|
|
APValue Result;
|
|
|
|
if (EvaluateVector(EvalExpr, Result, Info))
|
|
|
|
return Result;
|
|
|
|
return APValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
APValue VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
|
|
|
|
if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
return GetZeroVector(E->getType());
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Integer Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class IntExprEvaluator
|
2008-07-12 03:10:17 +08:00
|
|
|
: public StmtVisitor<IntExprEvaluator, bool> {
|
2008-07-12 06:52:41 +08:00
|
|
|
EvalInfo &Info;
|
2009-02-20 04:17:33 +08:00
|
|
|
APValue &Result;
|
2008-07-12 02:11:29 +08:00
|
|
|
public:
|
2009-02-20 04:17:33 +08:00
|
|
|
IntExprEvaluator(EvalInfo &info, APValue &result)
|
2008-07-12 06:52:41 +08:00
|
|
|
: Info(info), Result(result) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2009-02-20 02:37:50 +08:00
|
|
|
bool Success(const llvm::APSInt &SI, const Expr *E) {
|
2010-06-16 08:17:44 +08:00
|
|
|
assert(E->getType()->isIntegralOrEnumerationType() &&
|
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
assert(SI.isSigned() == E->getType()->isSignedIntegerType() &&
|
2009-02-20 02:37:50 +08:00
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
|
2009-02-20 02:37:50 +08:00
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
Result = APValue(SI);
|
2009-02-20 02:37:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-19 17:06:44 +08:00
|
|
|
bool Success(const llvm::APInt &I, const Expr *E) {
|
2010-06-16 08:17:44 +08:00
|
|
|
assert(E->getType()->isIntegralOrEnumerationType() &&
|
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
|
2009-02-20 02:37:50 +08:00
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
Result = APValue(APSInt(I));
|
|
|
|
Result.getInt().setIsUnsigned(E->getType()->isUnsignedIntegerType());
|
2009-02-19 17:06:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Success(uint64_t Value, const Expr *E) {
|
2010-06-16 08:17:44 +08:00
|
|
|
assert(E->getType()->isIntegralOrEnumerationType() &&
|
|
|
|
"Invalid evaluation result.");
|
2009-02-20 04:17:33 +08:00
|
|
|
Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
|
2009-02-19 17:06:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-01 02:14:57 +08:00
|
|
|
bool Error(SourceLocation L, diag::kind D, const Expr *E) {
|
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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
|
|
|
}
|
2009-09-09 23:08:12 +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
|
|
|
}
|
2009-09-09 23:08:12 +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) {
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(E->getValue(), E);
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
|
|
|
bool VisitCharacterLiteral(const CharacterLiteral *E) {
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(E->getValue(), E);
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
|
|
|
bool VisitTypesCompatibleExpr(const TypesCompatibleExpr *E) {
|
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());
|
2009-09-09 23:08:12 +08:00
|
|
|
return Success(Info.Ctx.typesAreCompatible(T0.getUnqualifiedType(),
|
2009-02-19 17:06:44 +08:00
|
|
|
T1.getUnqualifiedType()),
|
|
|
|
E);
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
2009-11-24 13:28:59 +08:00
|
|
|
|
|
|
|
bool CheckReferencedDecl(const Expr *E, const Decl *D);
|
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *E) {
|
|
|
|
return CheckReferencedDecl(E, E->getDecl());
|
|
|
|
}
|
|
|
|
bool VisitMemberExpr(const MemberExpr *E) {
|
|
|
|
if (CheckReferencedDecl(E, E->getMemberDecl())) {
|
|
|
|
// Conservatively assume a MemberExpr will have side-effects
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-13 08:10:10 +08:00
|
|
|
bool VisitCallExpr(CallExpr *E);
|
2008-07-12 03:10:17 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
bool VisitOffsetOfExpr(const OffsetOfExpr *E);
|
2008-07-12 03:10:17 +08:00
|
|
|
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) {
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(E->getValue(), E);
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-22 06:39:40 +08:00
|
|
|
bool VisitGNUNullExpr(const GNUNullExpr *E) {
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(0, E);
|
2008-12-22 06:39:40 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-07-08 14:14:04 +08:00
|
|
|
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(0, E);
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
|
|
|
|
2009-02-27 12:45:43 +08:00
|
|
|
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
|
|
|
|
return Success(0, E);
|
|
|
|
}
|
|
|
|
|
2009-01-06 04:52:13 +08:00
|
|
|
bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
|
2009-07-24 07:49:00 +08:00
|
|
|
return Success(E->EvaluateTrait(Info.Ctx), E);
|
2009-01-06 04:52:13 +08:00
|
|
|
}
|
|
|
|
|
2009-03-23 12:56:01 +08:00
|
|
|
bool VisitChooseExpr(const ChooseExpr *E) {
|
|
|
|
return Visit(E->getChosenSubExpr(Info.Ctx));
|
|
|
|
}
|
|
|
|
|
2009-02-28 11:59:05 +08:00
|
|
|
bool VisitUnaryReal(const UnaryOperator *E);
|
2009-02-27 12:45:43 +08:00
|
|
|
bool VisitUnaryImag(const UnaryOperator *E);
|
|
|
|
|
2008-07-12 05:24:13 +08:00
|
|
|
private:
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits GetAlignOfExpr(const Expr *E);
|
|
|
|
CharUnits GetAlignOfType(QualType T);
|
2010-05-11 07:27:23 +08:00
|
|
|
static QualType GetObjectType(const Expr *E);
|
|
|
|
bool TryEvaluateBuiltinObjectSize(CallExpr *E);
|
2009-02-27 12:45:43 +08:00
|
|
|
// FIXME: Missing: array subscript of vector, member of vector
|
2008-07-12 02:11:29 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2009-02-21 02:22:23 +08:00
|
|
|
static bool EvaluateIntegerOrLValue(const Expr* E, APValue &Result, EvalInfo &Info) {
|
2010-06-16 08:17:44 +08:00
|
|
|
assert(E->getType()->isIntegralOrEnumerationType());
|
2009-02-21 02:22:23 +08:00
|
|
|
return IntExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool EvaluateInteger(const Expr* E, APSInt &Result, EvalInfo &Info) {
|
2010-06-16 08:17:44 +08:00
|
|
|
assert(E->getType()->isIntegralOrEnumerationType());
|
2010-05-07 13:46:35 +08:00
|
|
|
|
2009-02-20 04:17:33 +08:00
|
|
|
APValue Val;
|
2009-02-21 02:22:23 +08:00
|
|
|
if (!EvaluateIntegerOrLValue(E, Val, Info) || !Val.isInt())
|
2009-02-20 04:17:33 +08:00
|
|
|
return false;
|
|
|
|
Result = Val.getInt();
|
|
|
|
return true;
|
2008-07-12 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2009-11-24 13:28:59 +08:00
|
|
|
bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
|
2008-07-12 08:38:25 +08:00
|
|
|
// Enums are integer constant exprs.
|
2009-12-11 06:29:29 +08:00
|
|
|
if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D))
|
|
|
|
return Success(ECD->getInitVal(), E);
|
2009-02-08 23:51:17 +08:00
|
|
|
|
|
|
|
// In C++, const, non-volatile integers initialized with ICEs are ICEs.
|
2009-03-31 07:39:01 +08:00
|
|
|
// In C, they can also be folded, although they are not ICEs.
|
2009-11-02 04:32:48 +08:00
|
|
|
if (Info.Ctx.getCanonicalType(E->getType()).getCVRQualifiers()
|
|
|
|
== Qualifiers::Const) {
|
2010-02-04 05:58:41 +08:00
|
|
|
|
|
|
|
if (isa<ParmVarDecl>(D))
|
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
|
|
|
|
2009-11-24 13:28:59 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
|
2010-02-02 04:16:42 +08:00
|
|
|
if (const Expr *Init = VD->getAnyInitializer()) {
|
2009-12-04 04:31:57 +08:00
|
|
|
if (APValue *V = VD->getEvaluatedValue()) {
|
|
|
|
if (V->isInt())
|
|
|
|
return Success(V->getInt(), E);
|
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VD->isEvaluatingValue())
|
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
|
|
|
|
|
|
|
VD->setEvaluatingValue();
|
|
|
|
|
2010-09-06 08:10:32 +08:00
|
|
|
Expr::EvalResult EResult;
|
|
|
|
if (Init->Evaluate(EResult, Info.Ctx) && !EResult.HasSideEffects &&
|
|
|
|
EResult.Val.isInt()) {
|
2009-05-27 02:54:04 +08:00
|
|
|
// Cache the evaluated value in the variable declaration.
|
2010-09-06 08:10:32 +08:00
|
|
|
Result = EResult.Val;
|
2009-12-04 04:31:57 +08:00
|
|
|
VD->setEvaluatedValue(Result);
|
2009-05-27 02:54:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-04 04:31:57 +08:00
|
|
|
VD->setEvaluatedValue(APValue());
|
2009-05-27 02:54:04 +08:00
|
|
|
}
|
2009-02-08 23:51:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 08:38:25 +08:00
|
|
|
// 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.
|
2009-03-17 07:22:08 +08:00
|
|
|
// FIXME: Does GCC differ between lvalue and rvalue references here?
|
2008-10-06 14:40:35 +08:00
|
|
|
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
|
|
|
|
};
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// If no argument was supplied, default to "no_type_class". This isn't
|
2008-10-06 14:40:35 +08:00
|
|
|
// ideal, however it is what gcc does.
|
|
|
|
if (E->getNumArgs() == 0)
|
|
|
|
return no_type_class;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-06 14:40:35 +08:00
|
|
|
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;
|
2010-04-27 05:31:17 +08:00
|
|
|
else if (ArgTy->isStructureOrClassType())
|
2008-10-06 14:40:35 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-11 07:27:23 +08:00
|
|
|
/// Retrieves the "underlying object type" of the given expression,
|
|
|
|
/// as used by __builtin_object_size.
|
|
|
|
QualType IntExprEvaluator::GetObjectType(const Expr *E) {
|
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl()))
|
|
|
|
return VD->getType();
|
|
|
|
} else if (isa<CompoundLiteralExpr>(E)) {
|
|
|
|
return E->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(CallExpr *E) {
|
|
|
|
// TODO: Perhaps we should let LLVM lower this?
|
|
|
|
LValue Base;
|
|
|
|
if (!EvaluatePointer(E->getArg(0), Base, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we can prove the base is null, lower to zero now.
|
|
|
|
const Expr *LVBase = Base.getLValueBase();
|
|
|
|
if (!LVBase) return Success(0, E);
|
|
|
|
|
|
|
|
QualType T = GetObjectType(LVBase);
|
|
|
|
if (T.isNull() ||
|
|
|
|
T->isIncompleteType() ||
|
2010-08-05 10:49:48 +08:00
|
|
|
T->isFunctionType() ||
|
2010-05-11 07:27:23 +08:00
|
|
|
T->isVariablyModifiedType() ||
|
|
|
|
T->isDependentType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
|
|
|
|
CharUnits Offset = Base.getLValueOffset();
|
|
|
|
|
|
|
|
if (!Offset.isNegative() && Offset <= Size)
|
|
|
|
Size -= Offset;
|
|
|
|
else
|
|
|
|
Size = CharUnits::Zero();
|
|
|
|
return Success(Size.getQuantity(), E);
|
|
|
|
}
|
|
|
|
|
2010-02-13 08:10:10 +08:00
|
|
|
bool IntExprEvaluator::VisitCallExpr(CallExpr *E) {
|
2009-02-15 02:57:46 +08:00
|
|
|
switch (E->isBuiltinCall(Info.Ctx)) {
|
2008-10-06 13:28:25 +08:00
|
|
|
default:
|
2008-12-01 02:37:00 +08:00
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
2009-10-27 02:35:08 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_object_size: {
|
2010-05-11 07:27:23 +08:00
|
|
|
if (TryEvaluateBuiltinObjectSize(E))
|
|
|
|
return true;
|
2009-10-27 02:35:08 +08:00
|
|
|
|
2010-01-20 06:58:35 +08:00
|
|
|
// If evaluating the argument has side-effects we can't determine
|
|
|
|
// the size of the object and lower it to unknown now.
|
2009-11-06 02:03:03 +08:00
|
|
|
if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
|
2010-01-04 02:18:37 +08:00
|
|
|
if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() <= 1)
|
2009-11-04 03:48:51 +08:00
|
|
|
return Success(-1ULL, E);
|
2009-10-27 02:35:08 +08:00
|
|
|
return Success(0, E);
|
|
|
|
}
|
2009-10-28 06:09:17 +08:00
|
|
|
|
2009-10-27 02:35:08 +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:
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(EvaluateBuiltinClassifyType(E), E);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(E->getArg(0)->isEvaluatable(Info.Ctx), E);
|
2009-09-23 14:06:36 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_eh_return_data_regno: {
|
|
|
|
int Operand = E->getArg(0)->EvaluateAsInt(Info.Ctx).getZExtValue();
|
|
|
|
Operand = Info.Ctx.Target.getEHDataRegisterNumber(Operand);
|
|
|
|
return Success(Operand, E);
|
|
|
|
}
|
2010-02-13 08:10:10 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_expect:
|
|
|
|
return Visit(E->getArg(0));
|
2010-09-10 14:27:15 +08:00
|
|
|
|
|
|
|
case Builtin::BIstrlen:
|
|
|
|
case Builtin::BI__builtin_strlen:
|
|
|
|
// As an extension, we support strlen() and __builtin_strlen() as constant
|
|
|
|
// expressions when the argument is a string literal.
|
|
|
|
if (StringLiteral *S
|
|
|
|
= dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
|
|
|
|
// The string literal may have embedded null characters. Find the first
|
|
|
|
// one and truncate there.
|
|
|
|
llvm::StringRef Str = S->getString();
|
|
|
|
llvm::StringRef::size_type Pos = Str.find(0);
|
|
|
|
if (Pos != llvm::StringRef::npos)
|
|
|
|
Str = Str.substr(0, Pos);
|
|
|
|
|
|
|
|
return Success(Str.size(), E);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
|
2008-10-06 13:28:25 +08:00
|
|
|
}
|
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) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Comma) {
|
2008-12-01 10:07:06 +08:00
|
|
|
if (!Visit(E->getRHS()))
|
|
|
|
return false;
|
2008-12-01 14:44:05 +08:00
|
|
|
|
2009-02-26 18:19:36 +08:00
|
|
|
// If we can't evaluate the LHS, it might have side effects;
|
|
|
|
// conservatively mark it.
|
|
|
|
if (!E->getLHS()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
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;
|
2009-09-09 23:08:12 +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
|
2010-08-25 19:45:40 +08:00
|
|
|
if (lhsResult == (E->getOpcode() == BO_LOr))
|
2009-02-20 02:37:50 +08:00
|
|
|
return Success(lhsResult, E);
|
2008-11-13 14:09:17 +08:00
|
|
|
|
2008-12-01 00:51:17 +08:00
|
|
|
if (HandleConversionToBool(E->getRHS(), rhsResult, Info)) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_LOr)
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(lhsResult || rhsResult, E);
|
2008-11-24 12:21:33 +08:00
|
|
|
else
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(lhsResult && rhsResult, E);
|
2008-11-24 12:21:33 +08:00
|
|
|
}
|
|
|
|
} 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.
|
2010-08-25 19:45:40 +08:00
|
|
|
if (rhsResult == (E->getOpcode() == BO_LOr) ||
|
|
|
|
!rhsResult == (E->getOpcode() == BO_LAnd)) {
|
2009-02-19 17:06:44 +08:00
|
|
|
// Since we weren't able to evaluate the left hand side, it
|
2008-12-01 00:51:17 +08:00
|
|
|
// must have had side effects.
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
2009-02-19 17:06:44 +08:00
|
|
|
|
|
|
|
return Success(rhsResult, E);
|
2008-11-24 12:21:33 +08:00
|
|
|
}
|
|
|
|
}
|
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");
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue LHS, RHS;
|
2009-01-29 14:43:41 +08:00
|
|
|
|
|
|
|
if (!EvaluateComplex(E->getLHS(), LHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (LHS.isComplexFloat()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
APFloat::cmpResult CR_r =
|
2009-01-29 14:43:41 +08:00
|
|
|
LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal());
|
2009-09-09 23:08:12 +08:00
|
|
|
APFloat::cmpResult CR_i =
|
2009-01-29 14:43:41 +08:00
|
|
|
LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag());
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_EQ)
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success((CR_r == APFloat::cmpEqual &&
|
|
|
|
CR_i == APFloat::cmpEqual), E);
|
|
|
|
else {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getOpcode() == BO_NE &&
|
2009-02-19 17:06:44 +08:00
|
|
|
"Invalid complex comparison.");
|
2009-09-09 23:08:12 +08:00
|
|
|
return Success(((CR_r == APFloat::cmpGreaterThan ||
|
2010-04-29 13:53:29 +08:00
|
|
|
CR_r == APFloat::cmpLessThan ||
|
|
|
|
CR_r == APFloat::cmpUnordered) ||
|
2009-09-09 23:08:12 +08:00
|
|
|
(CR_i == APFloat::cmpGreaterThan ||
|
2010-04-29 13:53:29 +08:00
|
|
|
CR_i == APFloat::cmpLessThan ||
|
|
|
|
CR_i == APFloat::cmpUnordered)), E);
|
2009-02-19 17:06:44 +08:00
|
|
|
}
|
2009-01-29 14:43:41 +08:00
|
|
|
} else {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_EQ)
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success((LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
|
|
|
|
LHS.getComplexIntImag() == RHS.getComplexIntImag()), E);
|
|
|
|
else {
|
2010-08-25 19:45:40 +08:00
|
|
|
assert(E->getOpcode() == BO_NE &&
|
2009-02-19 17:06:44 +08:00
|
|
|
"Invalid compex comparison.");
|
|
|
|
return Success((LHS.getComplexIntReal() != RHS.getComplexIntReal() ||
|
|
|
|
LHS.getComplexIntImag() != RHS.getComplexIntImag()), E);
|
|
|
|
}
|
2009-01-29 14:43:41 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
if (LHSTy->isRealFloatingType() &&
|
|
|
|
RHSTy->isRealFloatingType()) {
|
|
|
|
APFloat RHS(0.0), LHS(0.0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
if (!EvaluateFloat(E->getRHS(), RHS, Info))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
if (!EvaluateFloat(E->getLHS(), LHS, Info))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
APFloat::cmpResult CR = LHS.compare(RHS);
|
2008-11-17 06:46:56 +08:00
|
|
|
|
2008-11-16 15:17:21 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid binary operator!");
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT:
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(CR == APFloat::cmpLessThan, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GT:
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(CR == APFloat::cmpGreaterThan, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LE:
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(CR == APFloat::cmpLessThan || CR == APFloat::cmpEqual, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_GE:
|
2009-09-09 23:08:12 +08:00
|
|
|
return Success(CR == APFloat::cmpGreaterThan || CR == APFloat::cmpEqual,
|
2009-02-19 17:06:44 +08:00
|
|
|
E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_EQ:
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(CR == APFloat::cmpEqual, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_NE:
|
2009-09-09 23:08:12 +08:00
|
|
|
return Success(CR == APFloat::cmpGreaterThan
|
2010-04-29 13:53:29 +08:00
|
|
|
|| CR == APFloat::cmpLessThan
|
|
|
|
|| CR == APFloat::cmpUnordered, E);
|
2008-11-16 15:17:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-29 03:17:36 +08:00
|
|
|
if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Sub || E->isEqualityOp()) {
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue LHSValue;
|
2008-11-17 03:01:22 +08:00
|
|
|
if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
|
|
|
|
return false;
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue RHSValue;
|
2008-11-17 03:01:22 +08:00
|
|
|
if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
|
|
|
|
return false;
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2009-06-14 10:17:33 +08:00
|
|
|
// Reject any bases from the normal codepath; we special-case comparisons
|
|
|
|
// to null.
|
|
|
|
if (LHSValue.getLValueBase()) {
|
|
|
|
if (!E->isEqualityOp())
|
|
|
|
return false;
|
2010-01-15 20:37:54 +08:00
|
|
|
if (RHSValue.getLValueBase() || !RHSValue.getLValueOffset().isZero())
|
2009-06-14 10:17:33 +08:00
|
|
|
return false;
|
|
|
|
bool bres;
|
|
|
|
if (!EvalPointerValueAsBool(LHSValue, bres))
|
|
|
|
return false;
|
2010-08-25 19:45:40 +08:00
|
|
|
return Success(bres ^ (E->getOpcode() == BO_EQ), E);
|
2009-06-14 10:17:33 +08:00
|
|
|
} else if (RHSValue.getLValueBase()) {
|
|
|
|
if (!E->isEqualityOp())
|
|
|
|
return false;
|
2010-01-15 20:37:54 +08:00
|
|
|
if (LHSValue.getLValueBase() || !LHSValue.getLValueOffset().isZero())
|
2009-06-14 10:17:33 +08:00
|
|
|
return false;
|
|
|
|
bool bres;
|
|
|
|
if (!EvalPointerValueAsBool(RHSValue, bres))
|
|
|
|
return false;
|
2010-08-25 19:45:40 +08:00
|
|
|
return Success(bres ^ (E->getOpcode() == BO_EQ), E);
|
2009-06-14 10:17:33 +08:00
|
|
|
}
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Sub) {
|
2010-04-21 01:13:14 +08:00
|
|
|
QualType Type = E->getLHS()->getType();
|
|
|
|
QualType ElementType = Type->getAs<PointerType>()->getPointeeType();
|
2008-11-17 03:01:22 +08:00
|
|
|
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits ElementSize = CharUnits::One();
|
2009-06-05 04:23:20 +08:00
|
|
|
if (!ElementType->isVoidType() && !ElementType->isFunctionType())
|
2010-01-15 20:37:54 +08:00
|
|
|
ElementSize = Info.Ctx.getTypeSizeInChars(ElementType);
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits Diff = LHSValue.getLValueOffset() -
|
|
|
|
RHSValue.getLValueOffset();
|
|
|
|
return Success(Diff / ElementSize, E);
|
2009-04-29 03:17:36 +08:00
|
|
|
}
|
|
|
|
bool Result;
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_EQ) {
|
2009-04-29 03:17:36 +08:00
|
|
|
Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
|
2009-04-30 04:29:43 +08:00
|
|
|
} else {
|
2009-04-29 03:17:36 +08:00
|
|
|
Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
|
|
|
|
}
|
|
|
|
return Success(Result, E);
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-16 08:17:44 +08:00
|
|
|
if (!LHSTy->isIntegralOrEnumerationType() ||
|
|
|
|
!RHSTy->isIntegralOrEnumerationType()) {
|
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.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The LHS of a constant expr is always evaluated and needed.
|
2009-02-20 04:17:33 +08:00
|
|
|
if (!Visit(E->getLHS()))
|
2008-11-13 14:09:17 +08:00
|
|
|
return false; // error in subexpression.
|
2008-07-27 13:46:18 +08:00
|
|
|
|
2009-03-24 09:14:50 +08:00
|
|
|
APValue RHSVal;
|
|
|
|
if (!EvaluateIntegerOrLValue(E->getRHS(), RHSVal, Info))
|
2009-02-20 04:17:33 +08:00
|
|
|
return false;
|
2009-03-24 09:14:50 +08:00
|
|
|
|
|
|
|
// Handle cases like (unsigned long)&a + 4.
|
|
|
|
if (E->isAdditiveOp() && Result.isLValue() && RHSVal.isInt()) {
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits Offset = Result.getLValueOffset();
|
|
|
|
CharUnits AdditionalOffset = CharUnits::fromQuantity(
|
|
|
|
RHSVal.getInt().getZExtValue());
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Add)
|
2010-01-15 20:37:54 +08:00
|
|
|
Offset += AdditionalOffset;
|
2009-03-24 09:14:50 +08:00
|
|
|
else
|
2010-01-15 20:37:54 +08:00
|
|
|
Offset -= AdditionalOffset;
|
|
|
|
Result = APValue(Result.getLValueBase(), Offset);
|
2009-03-24 09:14:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle cases like 4 + (unsigned long)&a
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Add &&
|
2009-03-24 09:14:50 +08:00
|
|
|
RHSVal.isLValue() && Result.isInt()) {
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits Offset = RHSVal.getLValueOffset();
|
|
|
|
Offset += CharUnits::fromQuantity(Result.getInt().getZExtValue());
|
|
|
|
Result = APValue(RHSVal.getLValueBase(), Offset);
|
2009-03-24 09:14:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the following cases expect both operands to be an integer
|
|
|
|
if (!Result.isInt() || !RHSVal.isInt())
|
2008-07-12 03:10:17 +08:00
|
|
|
return false;
|
2008-11-13 14:09:17 +08:00
|
|
|
|
2009-03-24 09:14:50 +08:00
|
|
|
APSInt& RHS = RHSVal.getInt();
|
|
|
|
|
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);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul: return Success(Result.getInt() * RHS, E);
|
|
|
|
case BO_Add: return Success(Result.getInt() + RHS, E);
|
|
|
|
case BO_Sub: return Success(Result.getInt() - RHS, E);
|
|
|
|
case BO_And: return Success(Result.getInt() & RHS, E);
|
|
|
|
case BO_Xor: return Success(Result.getInt() ^ RHS, E);
|
|
|
|
case BO_Or: return Success(Result.getInt() | RHS, E);
|
|
|
|
case BO_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);
|
2009-02-20 04:17:33 +08:00
|
|
|
return Success(Result.getInt() / RHS, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_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);
|
2009-02-20 04:17:33 +08:00
|
|
|
return Success(Result.getInt() % RHS, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shl: {
|
2008-07-12 08:14:42 +08:00
|
|
|
// FIXME: Warn about out of range shift amounts!
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned SA =
|
2009-02-20 04:17:33 +08:00
|
|
|
(unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
|
|
|
|
return Success(Result.getInt() << SA, E);
|
2009-02-20 02:37:50 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shr: {
|
2009-09-09 23:08:12 +08:00
|
|
|
unsigned SA =
|
2009-02-20 04:17:33 +08:00
|
|
|
(unsigned) RHS.getLimitedValue(Result.getInt().getBitWidth()-1);
|
|
|
|
return Success(Result.getInt() >> SA, E);
|
2009-02-20 02:37:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LT: return Success(Result.getInt() < RHS, E);
|
|
|
|
case BO_GT: return Success(Result.getInt() > RHS, E);
|
|
|
|
case BO_LE: return Success(Result.getInt() <= RHS, E);
|
|
|
|
case BO_GE: return Success(Result.getInt() >= RHS, E);
|
|
|
|
case BO_EQ: return Success(Result.getInt() == RHS, E);
|
|
|
|
case BO_NE: return Success(Result.getInt() != RHS, E);
|
2008-11-13 10:13:11 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits IntExprEvaluator::GetAlignOfType(QualType T) {
|
2009-11-24 01:18:46 +08:00
|
|
|
// C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
|
|
|
|
// the result is the size of the referenced type."
|
|
|
|
// C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
|
|
|
|
// result shall be the alignment of the referenced type."
|
|
|
|
if (const ReferenceType *Ref = T->getAs<ReferenceType>())
|
|
|
|
T = Ref->getPointeeType();
|
|
|
|
|
2009-01-25 05:09:06 +08:00
|
|
|
// Get information about the alignment.
|
|
|
|
unsigned CharSize = Info.Ctx.Target.getCharWidth();
|
2009-05-01 01:32:17 +08:00
|
|
|
|
2009-05-31 05:09:44 +08:00
|
|
|
// __alignof is defined to return the preferred alignment.
|
2010-01-28 01:10:57 +08:00
|
|
|
return CharUnits::fromQuantity(
|
|
|
|
Info.Ctx.getPreferredTypeAlign(T.getTypePtr()) / CharSize);
|
2009-01-25 05:09:06 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 01:10:57 +08:00
|
|
|
CharUnits IntExprEvaluator::GetAlignOfExpr(const Expr *E) {
|
2009-01-25 05:53:27 +08:00
|
|
|
E = E->IgnoreParens();
|
|
|
|
|
|
|
|
// alignof decl is always accepted, even if it doesn't make sense: we default
|
2009-09-09 23:08:12 +08:00
|
|
|
// to 1 in those cases.
|
2009-01-25 05:53:27 +08:00
|
|
|
if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
|
2010-01-28 01:10:57 +08:00
|
|
|
return Info.Ctx.getDeclAlign(DRE->getDecl(),
|
|
|
|
/*RefAsPointee*/true);
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2009-01-25 05:53:27 +08:00
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(E))
|
2010-01-28 01:10:57 +08:00
|
|
|
return Info.Ctx.getDeclAlign(ME->getMemberDecl(),
|
|
|
|
/*RefAsPointee*/true);
|
2009-01-25 05:53:27 +08:00
|
|
|
|
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) {
|
2009-01-25 05:09:06 +08:00
|
|
|
// Handle alignof separately.
|
|
|
|
if (!E->isSizeOf()) {
|
|
|
|
if (E->isArgumentType())
|
2010-01-28 01:10:57 +08:00
|
|
|
return Success(GetAlignOfType(E->getArgumentType()).getQuantity(), E);
|
2009-01-25 05:09:06 +08:00
|
|
|
else
|
2010-01-28 01:10:57 +08:00
|
|
|
return Success(GetAlignOfExpr(E->getArgumentExpr()).getQuantity(), E);
|
2009-01-25 05:09:06 +08:00
|
|
|
}
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2008-11-12 01:56:53 +08:00
|
|
|
QualType SrcTy = E->getTypeOfArgument();
|
2009-11-24 01:18:46 +08:00
|
|
|
// C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
|
|
|
|
// the result is the size of the referenced type."
|
|
|
|
// C++ [expr.alignof]p3: "When alignof is applied to a reference type, the
|
|
|
|
// result shall be the alignment of the referenced type."
|
|
|
|
if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
|
|
|
|
SrcTy = Ref->getPointeeType();
|
2008-11-12 01:56:53 +08:00
|
|
|
|
2009-02-19 17:06:44 +08:00
|
|
|
// sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
|
|
|
|
// extension.
|
|
|
|
if (SrcTy->isVoidType() || SrcTy->isFunctionType())
|
|
|
|
return Success(1, E);
|
2009-03-23 12:38:34 +08:00
|
|
|
|
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-25 06:19:05 +08:00
|
|
|
|
2009-01-25 05:09:06 +08:00
|
|
|
// Get information about the size.
|
2010-01-12 01:06:35 +08:00
|
|
|
return Success(Info.Ctx.getTypeSizeInChars(SrcTy).getQuantity(), E);
|
2008-07-12 05:24:13 +08:00
|
|
|
}
|
|
|
|
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *E) {
|
|
|
|
CharUnits Result;
|
|
|
|
unsigned n = E->getNumComponents();
|
|
|
|
OffsetOfExpr* OOE = const_cast<OffsetOfExpr*>(E);
|
|
|
|
if (n == 0)
|
|
|
|
return false;
|
|
|
|
QualType CurrentType = E->getTypeSourceInfo()->getType();
|
|
|
|
for (unsigned i = 0; i != n; ++i) {
|
|
|
|
OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
|
|
|
|
switch (ON.getKind()) {
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Array: {
|
|
|
|
Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
|
|
|
|
APSInt IdxResult;
|
|
|
|
if (!EvaluateInteger(Idx, IdxResult, Info))
|
|
|
|
return false;
|
|
|
|
const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
|
|
|
|
if (!AT)
|
|
|
|
return false;
|
|
|
|
CurrentType = AT->getElementType();
|
|
|
|
CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
|
|
|
|
Result += IdxResult.getSExtValue() * ElementSize;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Field: {
|
|
|
|
FieldDecl *MemberDecl = ON.getField();
|
|
|
|
const RecordType *RT = CurrentType->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return false;
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
unsigned i = 0;
|
|
|
|
// FIXME: It would be nice if we didn't have to loop here!
|
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end();
|
|
|
|
Field != FieldEnd; (void)++Field, ++i) {
|
|
|
|
if (*Field == MemberDecl)
|
|
|
|
break;
|
|
|
|
}
|
2010-04-29 08:18:15 +08:00
|
|
|
assert(i < RL.getFieldCount() && "offsetof field in wrong type");
|
|
|
|
Result += CharUnits::fromQuantity(
|
|
|
|
RL.getFieldOffset(i) / Info.Ctx.getCharWidth());
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
CurrentType = MemberDecl->getType().getNonReferenceType();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Identifier:
|
|
|
|
llvm_unreachable("dependent __builtin_offsetof");
|
2010-04-29 08:18:15 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Base: {
|
|
|
|
CXXBaseSpecifier *BaseSpec = ON.getBase();
|
|
|
|
if (BaseSpec->isVirtual())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the layout of the class whose base we are looking into.
|
|
|
|
const RecordType *RT = CurrentType->getAs<RecordType>();
|
|
|
|
if (!RT)
|
|
|
|
return false;
|
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Find the base class itself.
|
|
|
|
CurrentType = BaseSpec->getType();
|
|
|
|
const RecordType *BaseRT = CurrentType->getAs<RecordType>();
|
|
|
|
if (!BaseRT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Add the offset to the base.
|
|
|
|
Result += CharUnits::fromQuantity(
|
|
|
|
RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()))
|
|
|
|
/ Info.Ctx.getCharWidth());
|
|
|
|
break;
|
|
|
|
}
|
Completely reimplement __builtin_offsetof, based on a patch by Roberto
Amadini.
This change introduces a new expression node type, OffsetOfExpr, that
describes __builtin_offsetof. Previously, __builtin_offsetof was
implemented using a unary operator whose subexpression involved
various synthesized array-subscript and member-reference expressions,
which was ugly and made it very hard to instantiate as a
template. OffsetOfExpr represents the AST more faithfully, with proper
type source information and a more compact representation.
OffsetOfExpr also has support for dependent __builtin_offsetof
expressions; it can be value-dependent, but will never be
type-dependent (like sizeof or alignof). This commit introduces
template instantiation for __builtin_offsetof as well.
There are two major caveats to this patch:
1) CodeGen cannot handle the case where __builtin_offsetof is not a
constant expression, so it produces an error. So, to avoid
regressing in C, we retain the old UnaryOperator-based
__builtin_offsetof implementation in C while using the shiny new
OffsetOfExpr implementation in C++. The old implementation can go
away once we have proper CodeGen support for this case, which we
expect won't cause much trouble in C++.
2) __builtin_offsetof doesn't work well with non-POD class types,
particularly when the designated field is found within a base
class. I will address this in a subsequent patch.
Fixes PR5880 and a bunch of assertions when building Boost.Python
tests.
llvm-svn: 102542
2010-04-29 06:16:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Success(Result.getQuantity(), E);
|
|
|
|
}
|
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == UO_LNot) {
|
2008-11-13 14:09:17 +08:00
|
|
|
// LNot's operand isn't necessarily an integer, so we handle it specially.
|
|
|
|
bool bres;
|
|
|
|
if (!HandleConversionToBool(E->getSubExpr(), bres, Info))
|
|
|
|
return false;
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(!bres, E);
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
2009-02-22 02:14:20 +08:00
|
|
|
// Only handle integral operations...
|
2010-06-16 08:17:44 +08:00
|
|
|
if (!E->getSubExpr()->getType()->isIntegralOrEnumerationType())
|
2009-02-22 02:14:20 +08:00
|
|
|
return false;
|
|
|
|
|
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);
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_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.
|
2009-02-20 02:37:50 +08:00
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Plus:
|
2009-09-09 23:08:12 +08:00
|
|
|
// The result is always just the subexpr.
|
2009-02-20 02:37:50 +08:00
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Minus:
|
2009-02-20 04:17:33 +08:00
|
|
|
if (!Result.isInt()) return false;
|
|
|
|
return Success(-Result.getInt(), E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Not:
|
2009-02-20 04:17:33 +08:00
|
|
|
if (!Result.isInt()) return false;
|
|
|
|
return Success(~Result.getInt(), E);
|
2008-07-08 13:49:43 +08:00
|
|
|
}
|
2008-07-08 22:35:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +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();
|
2009-02-20 06:16:29 +08:00
|
|
|
QualType SrcType = SubExpr->getType();
|
2008-12-01 02:14:57 +08:00
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
if (DestType->isBooleanType()) {
|
|
|
|
bool BoolResult;
|
|
|
|
if (!HandleConversionToBool(SubExpr, BoolResult, Info))
|
|
|
|
return false;
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(BoolResult, E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
// Handle simple integer->integer casts.
|
2010-06-16 08:17:44 +08:00
|
|
|
if (SrcType->isIntegralOrEnumerationType()) {
|
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
|
|
|
|
2009-02-20 09:15:07 +08:00
|
|
|
if (!Result.isInt()) {
|
|
|
|
// Only allow casts of lvalues if they are lossless.
|
|
|
|
return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType);
|
|
|
|
}
|
2009-02-20 04:17:33 +08:00
|
|
|
|
2009-02-20 06:24:01 +08:00
|
|
|
return Success(HandleIntToIntCast(DestType, SrcType,
|
2009-02-20 04:17:33 +08:00
|
|
|
Result.getInt(), Info.Ctx), E);
|
2008-07-12 09:15:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-12 09:15:53 +08:00
|
|
|
// FIXME: Clean this up!
|
2009-02-20 06:16:29 +08:00
|
|
|
if (SrcType->isPointerType()) {
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue 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
|
|
|
|
2009-02-20 06:24:01 +08:00
|
|
|
if (LV.getLValueBase()) {
|
|
|
|
// Only allow based lvalue casts if they are lossless.
|
|
|
|
if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
|
|
|
|
return false;
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LV.moveInto(Result);
|
2009-02-20 06:24:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2010-01-15 20:37:54 +08:00
|
|
|
APSInt AsInt = Info.Ctx.MakeIntValue(LV.getLValueOffset().getQuantity(),
|
|
|
|
SrcType);
|
2009-02-20 06:24:01 +08:00
|
|
|
return Success(HandleIntToIntCast(DestType, SrcType, AsInt, Info.Ctx), E);
|
2008-07-08 22:30:00 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2009-02-20 09:15:07 +08:00
|
|
|
if (SrcType->isArrayType() || SrcType->isFunctionType()) {
|
|
|
|
// This handles double-conversion cases, where there's both
|
|
|
|
// an l-value promotion and an implicit conversion to int.
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue LV;
|
2009-02-20 09:15:07 +08:00
|
|
|
if (!EvaluateLValue(SubExpr, LV, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(Info.Ctx.VoidPtrTy))
|
|
|
|
return false;
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LV.moveInto(Result);
|
2009-02-20 09:15:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-23 03:23:09 +08:00
|
|
|
if (SrcType->isAnyComplexType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue C;
|
2009-04-23 03:23:09 +08:00
|
|
|
if (!EvaluateComplex(SubExpr, C, Info))
|
|
|
|
return false;
|
|
|
|
if (C.isComplexFloat())
|
|
|
|
return Success(HandleFloatToIntCast(DestType, SrcType,
|
|
|
|
C.getComplexFloatReal(), Info.Ctx),
|
|
|
|
E);
|
|
|
|
else
|
|
|
|
return Success(HandleIntToIntCast(DestType, SrcType,
|
|
|
|
C.getComplexIntReal(), Info.Ctx), E);
|
|
|
|
}
|
2009-02-22 19:46:18 +08:00
|
|
|
// FIXME: Handle vectors
|
|
|
|
|
2009-02-20 06:16:29 +08:00
|
|
|
if (!SrcType->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);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-20 06:16:29 +08:00
|
|
|
return Success(HandleFloatToIntCast(DestType, SrcType, F, Info.Ctx), E);
|
2008-07-08 22:35:21 +08:00
|
|
|
}
|
2008-07-08 22:30:00 +08:00
|
|
|
|
2009-02-28 11:59:05 +08:00
|
|
|
bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
|
|
|
|
if (E->getSubExpr()->getType()->isAnyComplexType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue LV;
|
2009-02-28 11:59:05 +08:00
|
|
|
if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
|
|
|
|
return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
|
|
|
|
return Success(LV.getComplexIntReal(), E);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
}
|
|
|
|
|
2009-02-27 12:45:43 +08:00
|
|
|
bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
|
2009-02-28 11:59:05 +08:00
|
|
|
if (E->getSubExpr()->getType()->isComplexIntegerType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue LV;
|
2009-02-28 11:59:05 +08:00
|
|
|
if (!EvaluateComplex(E->getSubExpr(), LV, Info) || !LV.isComplexInt())
|
|
|
|
return Error(E->getExprLoc(), diag::note_invalid_subexpr_in_ice, E);
|
|
|
|
return Success(LV.getComplexIntImag(), E);
|
|
|
|
}
|
|
|
|
|
2009-02-27 12:45:43 +08:00
|
|
|
if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
return Success(0, E);
|
|
|
|
}
|
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Float Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class FloatExprEvaluator
|
2008-08-22 08:06:13 +08:00
|
|
|
: 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);
|
2010-07-08 14:14:04 +08:00
|
|
|
bool VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
|
2009-12-04 10:12:53 +08:00
|
|
|
bool VisitConditionalOperator(ConditionalOperator *E);
|
2009-02-22 19:46:18 +08:00
|
|
|
|
2009-03-23 12:56:01 +08:00
|
|
|
bool VisitChooseExpr(const ChooseExpr *E)
|
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
|
|
|
bool VisitUnaryExtension(const UnaryOperator *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
2010-05-08 06:08:54 +08:00
|
|
|
bool VisitUnaryReal(const UnaryOperator *E);
|
|
|
|
bool VisitUnaryImag(const UnaryOperator *E);
|
2009-03-23 12:56:01 +08:00
|
|
|
|
2010-05-08 06:08:54 +08:00
|
|
|
// FIXME: Missing: array subscript of vector, member of vector,
|
|
|
|
// ImplicitValueInitExpr
|
2008-08-22 08:06:13 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
|
2010-05-07 13:46:35 +08:00
|
|
|
assert(E->getType()->isRealFloatingType());
|
2008-08-22 08:06:13 +08:00
|
|
|
return FloatExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
|
|
|
}
|
|
|
|
|
2010-02-28 21:00:19 +08:00
|
|
|
static bool TryEvaluateBuiltinNaN(ASTContext &Context,
|
|
|
|
QualType ResultTy,
|
|
|
|
const Expr *Arg,
|
|
|
|
bool SNaN,
|
|
|
|
llvm::APFloat &Result) {
|
|
|
|
const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
|
|
|
|
if (!S) return false;
|
|
|
|
|
|
|
|
const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
|
|
|
|
|
|
|
|
llvm::APInt fill;
|
|
|
|
|
|
|
|
// Treat empty strings as if they were zero.
|
|
|
|
if (S->getString().empty())
|
|
|
|
fill = llvm::APInt(32, 0);
|
|
|
|
else if (S->getString().getAsInteger(0, fill))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (SNaN)
|
|
|
|
Result = llvm::APFloat::getSNaN(Sem, false, &fill);
|
|
|
|
else
|
|
|
|
Result = llvm::APFloat::getQNaN(Sem, false, &fill);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-06 13:28:25 +08:00
|
|
|
bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
2009-02-15 02:57:46 +08:00
|
|
|
switch (E->isBuiltinCall(Info.Ctx)) {
|
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
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-28 21:00:19 +08:00
|
|
|
case Builtin::BI__builtin_nans:
|
|
|
|
case Builtin::BI__builtin_nansf:
|
|
|
|
case Builtin::BI__builtin_nansl:
|
|
|
|
return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
|
|
|
|
true, Result);
|
|
|
|
|
2008-10-06 14:31:58 +08:00
|
|
|
case Builtin::BI__builtin_nan:
|
|
|
|
case Builtin::BI__builtin_nanf:
|
|
|
|
case Builtin::BI__builtin_nanl:
|
2009-05-30 11:56:50 +08:00
|
|
|
// If this is __builtin_nan() turn this into a nan, otherwise we
|
2008-10-06 14:31:58 +08:00
|
|
|
// can't constant fold it.
|
2010-02-28 21:00:19 +08:00
|
|
|
return TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
|
|
|
|
false, Result);
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
if (Result.isNegative())
|
|
|
|
Result.changeSign();
|
|
|
|
return true;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
case Builtin::BI__builtin_copysign:
|
|
|
|
case Builtin::BI__builtin_copysignf:
|
2008-10-16 11:51:50 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 06:08:54 +08:00
|
|
|
bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
|
2010-08-15 04:52:13 +08:00
|
|
|
if (E->getSubExpr()->getType()->isAnyComplexType()) {
|
|
|
|
ComplexValue CV;
|
|
|
|
if (!EvaluateComplex(E->getSubExpr(), CV, Info))
|
|
|
|
return false;
|
|
|
|
Result = CV.FloatReal;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Visit(E->getSubExpr());
|
2010-05-08 06:08:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
|
2010-08-15 04:52:13 +08:00
|
|
|
if (E->getSubExpr()->getType()->isAnyComplexType()) {
|
|
|
|
ComplexValue CV;
|
|
|
|
if (!EvaluateComplex(E->getSubExpr(), CV, Info))
|
|
|
|
return false;
|
|
|
|
Result = CV.FloatImag;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!E->getSubExpr()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType());
|
|
|
|
Result = llvm::APFloat::getZero(Sem);
|
2010-05-08 06:08:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == UO_Deref)
|
2008-11-20 01:44:31 +08:00
|
|
|
return false;
|
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
if (!EvaluateFloat(E->getSubExpr(), Result, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: return false;
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Plus:
|
2008-10-16 11:51:50 +08:00
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Minus:
|
2008-10-16 11:51:50 +08:00
|
|
|
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) {
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Comma) {
|
2009-11-16 12:25:37 +08:00
|
|
|
if (!EvaluateFloat(E->getRHS(), Result, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we can't evaluate the LHS, it might have side effects;
|
|
|
|
// conservatively mark it.
|
|
|
|
if (!E->getLHS()->isEvaluatable(Info.Ctx))
|
|
|
|
Info.EvalResult.HasSideEffects = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
// 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;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.multiply(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.add(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.subtract(RHS, APFloat::rmNearestTiesToEven);
|
|
|
|
return true;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.divide(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-09-09 23:08:12 +08:00
|
|
|
|
2010-06-16 08:17:44 +08:00
|
|
|
if (SubExpr->getType()->isIntegralOrEnumerationType()) {
|
2008-11-12 17:44:48 +08:00
|
|
|
APSInt IntResult;
|
2009-02-20 02:37:50 +08:00
|
|
|
if (!EvaluateInteger(SubExpr, IntResult, Info))
|
2008-11-12 17:44:48 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
Result = HandleIntToFloatCast(E->getType(), SubExpr->getType(),
|
2009-01-29 14:16:07 +08:00
|
|
|
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;
|
|
|
|
}
|
2009-02-22 19:46:18 +08:00
|
|
|
// FIXME: Handle complex types
|
2008-11-12 17:44:48 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:14:04 +08:00
|
|
|
bool FloatExprEvaluator::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
|
2008-11-12 17:44:48 +08:00
|
|
|
Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-04 10:12:53 +08:00
|
|
|
bool FloatExprEvaluator::VisitConditionalOperator(ConditionalOperator *E) {
|
|
|
|
bool Cond;
|
|
|
|
if (!HandleConversionToBool(E->getCond(), Cond, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Visit(Cond ? E->getTrueExpr() : E->getFalseExpr());
|
|
|
|
}
|
|
|
|
|
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-11-29 03:03:38 +08:00
|
|
|
class ComplexExprEvaluator
|
2010-05-08 01:22:02 +08:00
|
|
|
: public StmtVisitor<ComplexExprEvaluator, bool> {
|
2008-11-17 04:27:53 +08:00
|
|
|
EvalInfo &Info;
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue &Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-17 04:27:53 +08:00
|
|
|
public:
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
|
|
|
|
: Info(info), Result(Result) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-17 04:27:53 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
bool VisitStmt(Stmt *S) {
|
|
|
|
return false;
|
2008-11-17 04:27:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
bool VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
|
2008-11-17 04:27:53 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
bool VisitImaginaryLiteral(ImaginaryLiteral *E);
|
2009-01-29 06:24:07 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
bool VisitCastExpr(CastExpr *E);
|
|
|
|
|
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
|
|
|
bool VisitChooseExpr(const ChooseExpr *E)
|
|
|
|
{ return Visit(E->getChosenSubExpr(Info.Ctx)); }
|
|
|
|
bool VisitUnaryExtension(const UnaryOperator *E)
|
|
|
|
{ return Visit(E->getSubExpr()); }
|
|
|
|
// FIXME Missing: unary +/-/~, binary div, ImplicitValueInitExpr,
|
|
|
|
// conditional ?:, comma
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
|
|
|
|
EvalInfo &Info) {
|
|
|
|
assert(E->getType()->isAnyComplexType());
|
|
|
|
return ComplexExprEvaluator(Info, Result).Visit(const_cast<Expr*>(E));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComplexExprEvaluator::VisitImaginaryLiteral(ImaginaryLiteral *E) {
|
|
|
|
Expr* SubExpr = E->getSubExpr();
|
|
|
|
|
|
|
|
if (SubExpr->getType()->isRealFloatingType()) {
|
|
|
|
Result.makeComplexFloat();
|
|
|
|
APFloat &Imag = Result.FloatImag;
|
|
|
|
if (!EvaluateFloat(SubExpr, Imag, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Result.FloatReal = APFloat(Imag.getSemantics());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
assert(SubExpr->getType()->isIntegerType() &&
|
|
|
|
"Unexpected imaginary literal.");
|
|
|
|
|
|
|
|
Result.makeComplexInt();
|
|
|
|
APSInt &Imag = Result.IntImag;
|
|
|
|
if (!EvaluateInteger(SubExpr, Imag, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) {
|
|
|
|
Expr* SubExpr = E->getSubExpr();
|
|
|
|
QualType EltType = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
QualType SubType = SubExpr->getType();
|
|
|
|
|
|
|
|
if (SubType->isRealFloatingType()) {
|
|
|
|
APFloat &Real = Result.FloatReal;
|
|
|
|
if (!EvaluateFloat(SubExpr, Real, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (EltType->isRealFloatingType()) {
|
|
|
|
Result.makeComplexFloat();
|
|
|
|
Real = HandleFloatToFloatCast(EltType, SubType, Real, Info.Ctx);
|
|
|
|
Result.FloatImag = APFloat(Real.getSemantics());
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-01-29 06:24:07 +08:00
|
|
|
} else {
|
2010-05-08 01:22:02 +08:00
|
|
|
Result.makeComplexInt();
|
2010-08-17 07:27:44 +08:00
|
|
|
Result.IntReal = HandleFloatToIntCast(EltType, SubType, Real, Info.Ctx);
|
|
|
|
Result.IntImag = APSInt(Result.IntReal.getBitWidth(),
|
|
|
|
!Result.IntReal.isSigned());
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-01-29 06:24:07 +08:00
|
|
|
}
|
2010-08-17 07:27:44 +08:00
|
|
|
} else if (SubType->isIntegerType()) {
|
|
|
|
APSInt &Real = Result.IntReal;
|
|
|
|
if (!EvaluateInteger(SubExpr, Real, Info))
|
|
|
|
return false;
|
2008-11-17 04:27:53 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
if (EltType->isRealFloatingType()) {
|
|
|
|
Result.makeComplexFloat();
|
|
|
|
Result.FloatReal
|
|
|
|
= HandleIntToFloatCast(EltType, SubType, Real, Info.Ctx);
|
|
|
|
Result.FloatImag = APFloat(Result.FloatReal.getSemantics());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
Result.makeComplexInt();
|
|
|
|
Real = HandleIntToIntCast(EltType, SubType, Real, Info.Ctx);
|
|
|
|
Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (const ComplexType *CT = SubType->getAs<ComplexType>()) {
|
|
|
|
if (!Visit(SubExpr))
|
|
|
|
return false;
|
2008-11-17 05:51:21 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
QualType SrcType = CT->getElementType();
|
2009-04-23 03:23:09 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
if (Result.isComplexFloat()) {
|
2009-04-23 03:23:09 +08:00
|
|
|
if (EltType->isRealFloatingType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
Result.makeComplexFloat();
|
2010-08-17 07:27:44 +08:00
|
|
|
Result.FloatReal = HandleFloatToFloatCast(EltType, SrcType,
|
|
|
|
Result.FloatReal,
|
|
|
|
Info.Ctx);
|
|
|
|
Result.FloatImag = HandleFloatToFloatCast(EltType, SrcType,
|
|
|
|
Result.FloatImag,
|
|
|
|
Info.Ctx);
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-04-23 03:23:09 +08:00
|
|
|
} else {
|
2010-05-08 01:22:02 +08:00
|
|
|
Result.makeComplexInt();
|
2010-08-17 07:27:44 +08:00
|
|
|
Result.IntReal = HandleFloatToIntCast(EltType, SrcType,
|
|
|
|
Result.FloatReal,
|
|
|
|
Info.Ctx);
|
|
|
|
Result.IntImag = HandleFloatToIntCast(EltType, SrcType,
|
|
|
|
Result.FloatImag,
|
|
|
|
Info.Ctx);
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-04-23 03:23:09 +08:00
|
|
|
}
|
2010-08-17 07:27:44 +08:00
|
|
|
} else {
|
|
|
|
assert(Result.isComplexInt() && "Invalid evaluate result.");
|
2009-04-23 03:23:09 +08:00
|
|
|
if (EltType->isRealFloatingType()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
Result.makeComplexFloat();
|
2010-08-17 07:27:44 +08:00
|
|
|
Result.FloatReal = HandleIntToFloatCast(EltType, SrcType,
|
|
|
|
Result.IntReal,
|
|
|
|
Info.Ctx);
|
|
|
|
Result.FloatImag = HandleIntToFloatCast(EltType, SrcType,
|
|
|
|
Result.IntImag,
|
|
|
|
Info.Ctx);
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-04-23 03:23:09 +08:00
|
|
|
} else {
|
2010-05-08 01:22:02 +08:00
|
|
|
Result.makeComplexInt();
|
2010-08-17 07:27:44 +08:00
|
|
|
Result.IntReal = HandleIntToIntCast(EltType, SrcType,
|
|
|
|
Result.IntReal,
|
|
|
|
Info.Ctx);
|
|
|
|
Result.IntImag = HandleIntToIntCast(EltType, SrcType,
|
|
|
|
Result.IntImag,
|
|
|
|
Info.Ctx);
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2009-04-23 03:23:09 +08:00
|
|
|
}
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-17 07:27:44 +08:00
|
|
|
// FIXME: Handle more casts.
|
|
|
|
return false;
|
2008-11-17 04:27:53 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
|
|
|
if (!Visit(E->getLHS()))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue RHS;
|
2009-01-29 06:24:07 +08:00
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info))
|
2010-05-08 01:22:02 +08:00
|
|
|
return false;
|
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()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
default: return false;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_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;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_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;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2009-01-29 09:32:56 +08:00
|
|
|
if (Result.isComplexFloat()) {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue LHS = Result;
|
2009-01-29 09:32:56 +08:00
|
|
|
APFloat &LHS_r = LHS.getComplexFloatReal();
|
|
|
|
APFloat &LHS_i = LHS.getComplexFloatImag();
|
|
|
|
APFloat &RHS_r = RHS.getComplexFloatReal();
|
|
|
|
APFloat &RHS_i = RHS.getComplexFloatImag();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-01-29 09:32:56 +08:00
|
|
|
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 {
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue LHS = Result;
|
2009-09-09 23:08:12 +08:00
|
|
|
Result.getComplexIntReal() =
|
2009-01-29 09:32:56 +08:00
|
|
|
(LHS.getComplexIntReal() * RHS.getComplexIntReal() -
|
|
|
|
LHS.getComplexIntImag() * RHS.getComplexIntImag());
|
2009-09-09 23:08:12 +08:00
|
|
|
Result.getComplexIntImag() =
|
2009-01-29 09:32:56 +08:00
|
|
|
(LHS.getComplexIntReal() * RHS.getComplexIntImag() +
|
|
|
|
LHS.getComplexIntImag() * RHS.getComplexIntReal());
|
|
|
|
}
|
|
|
|
break;
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
return true;
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-05-11 07:27:23 +08:00
|
|
|
/// Evaluate - Return true if this is a constant which we can fold using
|
|
|
|
/// 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.
|
|
|
|
bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
|
|
|
|
const Expr *E = this;
|
|
|
|
EvalInfo Info(Ctx, Result);
|
2010-05-08 05:00:08 +08:00
|
|
|
if (E->getType()->isVectorType()) {
|
|
|
|
if (!EvaluateVector(E, Info.EvalResult.Val, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2010-05-08 05:00:08 +08:00
|
|
|
} else if (E->getType()->isIntegerType()) {
|
|
|
|
if (!IntExprEvaluator(Info, Info.EvalResult.Val).Visit(const_cast<Expr*>(E)))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2010-07-07 13:08:32 +08:00
|
|
|
if (Result.Val.isLValue() && !IsGlobalLValue(Result.Val.getLValueBase()))
|
|
|
|
return false;
|
2010-05-08 05:00:08 +08:00
|
|
|
} else if (E->getType()->hasPointerRepresentation()) {
|
|
|
|
LValue LV;
|
|
|
|
if (!EvaluatePointer(E, LV, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2010-05-15 01:07:14 +08:00
|
|
|
if (!IsGlobalLValue(LV.Base))
|
2010-05-11 07:27:23 +08:00
|
|
|
return false;
|
2010-05-08 05:00:08 +08:00
|
|
|
LV.moveInto(Info.EvalResult.Val);
|
|
|
|
} else if (E->getType()->isRealFloatingType()) {
|
|
|
|
llvm::APFloat F(0.0);
|
|
|
|
if (!EvaluateFloat(E, F, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
Info.EvalResult.Val = APValue(F);
|
|
|
|
} else if (E->getType()->isAnyComplexType()) {
|
|
|
|
ComplexValue C;
|
|
|
|
if (!EvaluateComplex(E, C, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2010-05-08 05:00:08 +08:00
|
|
|
C.moveInto(Info.EvalResult.Val);
|
2009-10-27 07:05:19 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-06 07:42:56 +08:00
|
|
|
bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
|
|
|
|
EvalResult Scratch;
|
|
|
|
EvalInfo Info(Ctx, Scratch);
|
|
|
|
|
|
|
|
return HandleConversionToBool(this, Result, Info);
|
|
|
|
}
|
|
|
|
|
2009-04-10 12:54:13 +08:00
|
|
|
bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
|
|
|
|
EvalInfo Info(Ctx, Result);
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue LV;
|
2010-05-11 07:27:23 +08:00
|
|
|
if (EvaluateLValue(this, LV, Info) &&
|
|
|
|
!Result.HasSideEffects &&
|
2010-05-15 01:07:14 +08:00
|
|
|
IsGlobalLValue(LV.Base)) {
|
|
|
|
LV.moveInto(Result.Val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
|
|
|
|
EvalInfo Info(Ctx, Result);
|
|
|
|
|
|
|
|
LValue LV;
|
|
|
|
if (EvaluateLValue(this, LV, Info)) {
|
2010-05-08 05:00:08 +08:00
|
|
|
LV.moveInto(Result.Val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-09-13 18:17:44 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2009-11-06 02:03:03 +08:00
|
|
|
bool Expr::HasSideEffects(ASTContext &Ctx) const {
|
|
|
|
Expr::EvalResult Result;
|
|
|
|
EvalInfo Info(Ctx, Result);
|
|
|
|
return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2010-05-07 13:32:02 +08:00
|
|
|
|
2010-05-15 01:07:14 +08:00
|
|
|
bool Expr::EvalResult::isGlobalLValue() const {
|
|
|
|
assert(Val.isLValue());
|
|
|
|
return IsGlobalLValue(Val.getLValueBase());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 13:32:02 +08:00
|
|
|
/// isIntegerConstantExpr - this recursive routine will test if an expression is
|
|
|
|
/// an integer constant expression.
|
|
|
|
|
|
|
|
/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
|
|
|
|
/// comma, etc
|
|
|
|
///
|
|
|
|
/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
|
|
|
|
/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
|
|
|
|
/// cast+dereference.
|
|
|
|
|
|
|
|
// CheckICE - This function does the fundamental ICE checking: the returned
|
|
|
|
// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
|
|
|
|
// Note that to reduce code duplication, this helper does no evaluation
|
|
|
|
// itself; the caller checks whether the expression is evaluatable, and
|
|
|
|
// in the rare cases where CheckICE actually cares about the evaluated
|
|
|
|
// value, it calls into Evalute.
|
|
|
|
//
|
|
|
|
// Meanings of Val:
|
|
|
|
// 0: This expression is an ICE if it can be evaluated by Evaluate.
|
|
|
|
// 1: This expression is not an ICE, but if it isn't evaluated, it's
|
|
|
|
// a legal subexpression for an ICE. This return value is used to handle
|
|
|
|
// the comma operator in C99 mode.
|
|
|
|
// 2: This expression is not an ICE, and is not a legal subexpression for one.
|
|
|
|
|
2010-07-27 05:25:24 +08:00
|
|
|
namespace {
|
|
|
|
|
2010-05-07 13:32:02 +08:00
|
|
|
struct ICEDiag {
|
|
|
|
unsigned Val;
|
|
|
|
SourceLocation Loc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
|
|
|
|
ICEDiag() : Val(0) {}
|
|
|
|
};
|
|
|
|
|
2010-07-27 05:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static ICEDiag NoDiag() { return ICEDiag(); }
|
2010-05-07 13:32:02 +08:00
|
|
|
|
|
|
|
static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
|
|
|
|
Expr::EvalResult EVResult;
|
|
|
|
if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
|
|
|
|
!EVResult.Val.isInt()) {
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
return NoDiag();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
|
|
|
|
assert(!E->isValueDependent() && "Should not see value dependent exprs!");
|
2010-06-16 08:17:44 +08:00
|
|
|
if (!E->getType()->isIntegralOrEnumerationType()) {
|
2010-05-07 13:32:02 +08:00
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
#define STMT(Node, Base) case Expr::Node##Class:
|
|
|
|
#define EXPR(Node, Base)
|
|
|
|
#include "clang/AST/StmtNodes.inc"
|
|
|
|
case Expr::PredefinedExprClass:
|
|
|
|
case Expr::FloatingLiteralClass:
|
|
|
|
case Expr::ImaginaryLiteralClass:
|
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
case Expr::ArraySubscriptExprClass:
|
|
|
|
case Expr::MemberExprClass:
|
|
|
|
case Expr::CompoundAssignOperatorClass:
|
|
|
|
case Expr::CompoundLiteralExprClass:
|
|
|
|
case Expr::ExtVectorElementExprClass:
|
|
|
|
case Expr::InitListExprClass:
|
|
|
|
case Expr::DesignatedInitExprClass:
|
|
|
|
case Expr::ImplicitValueInitExprClass:
|
|
|
|
case Expr::ParenListExprClass:
|
|
|
|
case Expr::VAArgExprClass:
|
|
|
|
case Expr::AddrLabelExprClass:
|
|
|
|
case Expr::StmtExprClass:
|
|
|
|
case Expr::CXXMemberCallExprClass:
|
|
|
|
case Expr::CXXDynamicCastExprClass:
|
|
|
|
case Expr::CXXTypeidExprClass:
|
2010-09-09 07:47:05 +08:00
|
|
|
case Expr::CXXUuidofExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::CXXNullPtrLiteralExprClass:
|
|
|
|
case Expr::CXXThisExprClass:
|
|
|
|
case Expr::CXXThrowExprClass:
|
|
|
|
case Expr::CXXNewExprClass:
|
|
|
|
case Expr::CXXDeleteExprClass:
|
|
|
|
case Expr::CXXPseudoDestructorExprClass:
|
|
|
|
case Expr::UnresolvedLookupExprClass:
|
|
|
|
case Expr::DependentScopeDeclRefExprClass:
|
|
|
|
case Expr::CXXConstructExprClass:
|
|
|
|
case Expr::CXXBindTemporaryExprClass:
|
|
|
|
case Expr::CXXExprWithTemporariesClass:
|
|
|
|
case Expr::CXXTemporaryObjectExprClass:
|
|
|
|
case Expr::CXXUnresolvedConstructExprClass:
|
|
|
|
case Expr::CXXDependentScopeMemberExprClass:
|
|
|
|
case Expr::UnresolvedMemberExprClass:
|
|
|
|
case Expr::ObjCStringLiteralClass:
|
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
case Expr::ObjCMessageExprClass:
|
|
|
|
case Expr::ObjCSelectorExprClass:
|
|
|
|
case Expr::ObjCProtocolExprClass:
|
|
|
|
case Expr::ObjCIvarRefExprClass:
|
|
|
|
case Expr::ObjCPropertyRefExprClass:
|
|
|
|
case Expr::ObjCImplicitSetterGetterRefExprClass:
|
|
|
|
case Expr::ObjCSuperExprClass:
|
|
|
|
case Expr::ObjCIsaExprClass:
|
|
|
|
case Expr::ShuffleVectorExprClass:
|
|
|
|
case Expr::BlockExprClass:
|
|
|
|
case Expr::BlockDeclRefExprClass:
|
|
|
|
case Expr::NoStmtClass:
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
|
|
|
|
case Expr::GNUNullExprClass:
|
|
|
|
// GCC considers the GNU __null value to be an integral constant expression.
|
|
|
|
return NoDiag();
|
|
|
|
|
|
|
|
case Expr::ParenExprClass:
|
|
|
|
return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
|
|
|
|
case Expr::IntegerLiteralClass:
|
|
|
|
case Expr::CharacterLiteralClass:
|
|
|
|
case Expr::CXXBoolLiteralExprClass:
|
2010-07-08 14:14:04 +08:00
|
|
|
case Expr::CXXScalarValueInitExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::TypesCompatibleExprClass:
|
|
|
|
case Expr::UnaryTypeTraitExprClass:
|
|
|
|
return NoDiag();
|
|
|
|
case Expr::CallExprClass:
|
2010-08-31 01:47:05 +08:00
|
|
|
case Expr::CXXOperatorCallExprClass: {
|
2010-05-07 13:32:02 +08:00
|
|
|
const CallExpr *CE = cast<CallExpr>(E);
|
|
|
|
if (CE->isBuiltinCall(Ctx))
|
|
|
|
return CheckEvalInICE(E, Ctx);
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
case Expr::DeclRefExprClass:
|
|
|
|
if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
|
|
|
|
return NoDiag();
|
|
|
|
if (Ctx.getLangOptions().CPlusPlus &&
|
|
|
|
E->getType().getCVRQualifiers() == Qualifiers::Const) {
|
|
|
|
const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
|
|
|
|
|
|
|
|
// Parameter variables are never constants. Without this check,
|
|
|
|
// getAnyInitializer() can find a default argument, which leads
|
|
|
|
// to chaos.
|
|
|
|
if (isa<ParmVarDecl>(D))
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
|
|
|
|
|
|
|
// C++ 7.1.5.1p2
|
|
|
|
// A variable of non-volatile const-qualified integral or enumeration
|
|
|
|
// type initialized by an ICE can be used in ICEs.
|
|
|
|
if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
|
|
|
|
Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
|
|
|
|
if (Quals.hasVolatile() || !Quals.hasConst())
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
|
|
|
|
|
|
|
// Look for a declaration of this variable that has an initializer.
|
|
|
|
const VarDecl *ID = 0;
|
|
|
|
const Expr *Init = Dcl->getAnyInitializer(ID);
|
|
|
|
if (Init) {
|
|
|
|
if (ID->isInitKnownICE()) {
|
|
|
|
// We have already checked whether this subexpression is an
|
|
|
|
// integral constant expression.
|
|
|
|
if (ID->isInitICE())
|
|
|
|
return NoDiag();
|
|
|
|
else
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's an ICE whether or not the definition we found is
|
|
|
|
// out-of-line. See DR 721 and the discussion in Clang PR
|
|
|
|
// 6206 for details.
|
|
|
|
|
|
|
|
if (Dcl->isCheckingICE()) {
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
|
|
|
}
|
|
|
|
|
|
|
|
Dcl->setCheckingICE();
|
|
|
|
ICEDiag Result = CheckICE(Init, Ctx);
|
|
|
|
// Cache the result of the ICE test.
|
|
|
|
Dcl->setInitKnownICE(Result.Val == 0);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
case Expr::UnaryOperatorClass: {
|
|
|
|
const UnaryOperator *Exp = cast<UnaryOperator>(E);
|
|
|
|
switch (Exp->getOpcode()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_PostInc:
|
|
|
|
case UO_PostDec:
|
|
|
|
case UO_PreInc:
|
|
|
|
case UO_PreDec:
|
|
|
|
case UO_AddrOf:
|
|
|
|
case UO_Deref:
|
2010-05-07 13:32:02 +08:00
|
|
|
return ICEDiag(2, E->getLocStart());
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Extension:
|
|
|
|
case UO_LNot:
|
|
|
|
case UO_Plus:
|
|
|
|
case UO_Minus:
|
|
|
|
case UO_Not:
|
|
|
|
case UO_Real:
|
|
|
|
case UO_Imag:
|
2010-05-07 13:32:02 +08:00
|
|
|
return CheckICE(Exp->getSubExpr(), Ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// OffsetOf falls through here.
|
|
|
|
}
|
|
|
|
case Expr::OffsetOfExprClass: {
|
|
|
|
// Note that per C99, offsetof must be an ICE. And AFAIK, using
|
|
|
|
// Evaluate matches the proposed gcc behavior for cases like
|
|
|
|
// "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
|
|
|
|
// compliance: we should warn earlier for offsetof expressions with
|
|
|
|
// array subscripts that aren't ICEs, and if the array subscripts
|
|
|
|
// are ICEs, the value of the offsetof must be an integer constant.
|
|
|
|
return CheckEvalInICE(E, Ctx);
|
|
|
|
}
|
|
|
|
case Expr::SizeOfAlignOfExprClass: {
|
|
|
|
const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
|
|
|
|
if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
return NoDiag();
|
|
|
|
}
|
|
|
|
case Expr::BinaryOperatorClass: {
|
|
|
|
const BinaryOperator *Exp = cast<BinaryOperator>(E);
|
|
|
|
switch (Exp->getOpcode()) {
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
case BO_Assign:
|
|
|
|
case BO_MulAssign:
|
|
|
|
case BO_DivAssign:
|
|
|
|
case BO_RemAssign:
|
|
|
|
case BO_AddAssign:
|
|
|
|
case BO_SubAssign:
|
|
|
|
case BO_ShlAssign:
|
|
|
|
case BO_ShrAssign:
|
|
|
|
case BO_AndAssign:
|
|
|
|
case BO_XorAssign:
|
|
|
|
case BO_OrAssign:
|
2010-05-07 13:32:02 +08:00
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
|
|
|
case BO_Div:
|
|
|
|
case BO_Rem:
|
|
|
|
case BO_Add:
|
|
|
|
case BO_Sub:
|
|
|
|
case BO_Shl:
|
|
|
|
case BO_Shr:
|
|
|
|
case BO_LT:
|
|
|
|
case BO_GT:
|
|
|
|
case BO_LE:
|
|
|
|
case BO_GE:
|
|
|
|
case BO_EQ:
|
|
|
|
case BO_NE:
|
|
|
|
case BO_And:
|
|
|
|
case BO_Xor:
|
|
|
|
case BO_Or:
|
|
|
|
case BO_Comma: {
|
2010-05-07 13:32:02 +08:00
|
|
|
ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
|
|
|
|
ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
|
2010-08-25 19:45:40 +08:00
|
|
|
if (Exp->getOpcode() == BO_Div ||
|
|
|
|
Exp->getOpcode() == BO_Rem) {
|
2010-05-07 13:32:02 +08:00
|
|
|
// Evaluate gives an error for undefined Div/Rem, so make sure
|
|
|
|
// we don't evaluate one.
|
|
|
|
if (LHSResult.Val != 2 && RHSResult.Val != 2) {
|
|
|
|
llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
|
|
|
|
if (REval == 0)
|
|
|
|
return ICEDiag(1, E->getLocStart());
|
|
|
|
if (REval.isSigned() && REval.isAllOnesValue()) {
|
|
|
|
llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
|
|
|
|
if (LEval.isMinSignedValue())
|
|
|
|
return ICEDiag(1, E->getLocStart());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
if (Exp->getOpcode() == BO_Comma) {
|
2010-05-07 13:32:02 +08:00
|
|
|
if (Ctx.getLangOptions().C99) {
|
|
|
|
// C99 6.6p3 introduces a strange edge case: comma can be in an ICE
|
|
|
|
// if it isn't evaluated.
|
|
|
|
if (LHSResult.Val == 0 && RHSResult.Val == 0)
|
|
|
|
return ICEDiag(1, E->getLocStart());
|
|
|
|
} else {
|
|
|
|
// In both C89 and C++, commas in ICEs are illegal.
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (LHSResult.Val >= RHSResult.Val)
|
|
|
|
return LHSResult;
|
|
|
|
return RHSResult;
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_LAnd:
|
|
|
|
case BO_LOr: {
|
2010-05-07 13:32:02 +08:00
|
|
|
ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
|
|
|
|
ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
|
|
|
|
if (LHSResult.Val == 0 && RHSResult.Val == 1) {
|
|
|
|
// Rare case where the RHS has a comma "side-effect"; we need
|
|
|
|
// to actually check the condition to see whether the side
|
|
|
|
// with the comma is evaluated.
|
2010-08-25 19:45:40 +08:00
|
|
|
if ((Exp->getOpcode() == BO_LAnd) !=
|
2010-05-07 13:32:02 +08:00
|
|
|
(Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
|
|
|
|
return RHSResult;
|
|
|
|
return NoDiag();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LHSResult.Val >= RHSResult.Val)
|
|
|
|
return LHSResult;
|
|
|
|
return RHSResult;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Expr::ImplicitCastExprClass:
|
|
|
|
case Expr::CStyleCastExprClass:
|
|
|
|
case Expr::CXXFunctionalCastExprClass:
|
|
|
|
case Expr::CXXStaticCastExprClass:
|
|
|
|
case Expr::CXXReinterpretCastExprClass:
|
|
|
|
case Expr::CXXConstCastExprClass: {
|
|
|
|
const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
|
2010-06-16 08:17:44 +08:00
|
|
|
if (SubExpr->getType()->isIntegralOrEnumerationType())
|
2010-05-07 13:32:02 +08:00
|
|
|
return CheckICE(SubExpr, Ctx);
|
|
|
|
if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
|
|
|
|
return NoDiag();
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
case Expr::ConditionalOperatorClass: {
|
|
|
|
const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
|
|
|
|
// If the condition (ignoring parens) is a __builtin_constant_p call,
|
|
|
|
// then only the true side is actually considered in an integer constant
|
|
|
|
// expression, and it is fully evaluated. This is an important GNU
|
|
|
|
// extension. See GCC PR38377 for discussion.
|
|
|
|
if (const CallExpr *CallCE
|
|
|
|
= dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
|
|
|
|
if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
|
|
|
|
Expr::EvalResult EVResult;
|
|
|
|
if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
|
|
|
|
!EVResult.Val.isInt()) {
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
return NoDiag();
|
|
|
|
}
|
|
|
|
ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
|
|
|
|
ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
|
|
|
|
ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
|
|
|
|
if (CondResult.Val == 2)
|
|
|
|
return CondResult;
|
|
|
|
if (TrueResult.Val == 2)
|
|
|
|
return TrueResult;
|
|
|
|
if (FalseResult.Val == 2)
|
|
|
|
return FalseResult;
|
|
|
|
if (CondResult.Val == 1)
|
|
|
|
return CondResult;
|
|
|
|
if (TrueResult.Val == 0 && FalseResult.Val == 0)
|
|
|
|
return NoDiag();
|
|
|
|
// Rare case where the diagnostics depend on which side is evaluated
|
|
|
|
// Note that if we get here, CondResult is 0, and at least one of
|
|
|
|
// TrueResult and FalseResult is non-zero.
|
|
|
|
if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
|
|
|
|
return FalseResult;
|
|
|
|
}
|
|
|
|
return TrueResult;
|
|
|
|
}
|
|
|
|
case Expr::CXXDefaultArgExprClass:
|
|
|
|
return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
|
|
|
|
case Expr::ChooseExprClass: {
|
|
|
|
return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silence a GCC warning
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
|
|
|
|
SourceLocation *Loc, bool isEvaluated) const {
|
|
|
|
ICEDiag d = CheckICE(this, Ctx);
|
|
|
|
if (d.Val != 0) {
|
|
|
|
if (Loc) *Loc = d.Loc;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EvalResult EvalResult;
|
|
|
|
if (!Evaluate(EvalResult, Ctx))
|
|
|
|
llvm_unreachable("ICE cannot be evaluated!");
|
|
|
|
assert(!EvalResult.HasSideEffects && "ICE with side effects!");
|
|
|
|
assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
|
|
|
|
Result = EvalResult.Val.getInt();
|
|
|
|
return true;
|
|
|
|
}
|