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.
|
|
|
|
//
|
2012-01-27 09:14:48 +08:00
|
|
|
// Constant expression evaluation produces four main results:
|
|
|
|
//
|
|
|
|
// * A success/failure flag indicating whether constant folding was successful.
|
|
|
|
// This is the 'bool' return value used by most of the code in this file. A
|
|
|
|
// 'false' return value indicates that constant folding has failed, and any
|
|
|
|
// appropriate diagnostic has already been produced.
|
|
|
|
//
|
|
|
|
// * An evaluated result, valid only if constant folding has not failed.
|
|
|
|
//
|
|
|
|
// * A flag indicating if evaluation encountered (unevaluated) side-effects.
|
|
|
|
// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
|
|
|
|
// where it is possible to determine the evaluated result regardless.
|
|
|
|
//
|
|
|
|
// * A set of notes indicating why the evaluation was not a constant expression
|
|
|
|
// (under the C++11 rules only, at the moment), or, if folding failed too,
|
|
|
|
// why the expression could not be folded.
|
|
|
|
//
|
|
|
|
// If we are checking for a potential constant expression, failure to constant
|
|
|
|
// fold a potential constant sub-expression will be indicated by a 'false'
|
|
|
|
// return value (the expression could not be folded) and no diagnostic (the
|
|
|
|
// expression is not necessarily non-constant).
|
|
|
|
//
|
2008-07-03 12:20:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
2012-03-02 03:45:56 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2009-05-30 11:56:50 +08:00
|
|
|
#include <cstring>
|
2012-02-01 13:53:12 +08:00
|
|
|
#include <functional>
|
2009-05-30 11:56:50 +08:00
|
|
|
|
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
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
static bool IsGlobalLValue(APValue::LValueBase B);
|
|
|
|
|
2011-03-04 21:12:48 +08:00
|
|
|
namespace {
|
2011-11-10 14:34:14 +08:00
|
|
|
struct LValue;
|
2011-10-29 06:34:42 +08:00
|
|
|
struct CallStackFrame;
|
2011-10-31 13:52:43 +08:00
|
|
|
struct EvalInfo;
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
static QualType getType(APValue::LValueBase B) {
|
2011-11-13 06:28:03 +08:00
|
|
|
if (!B) return QualType();
|
|
|
|
if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>())
|
|
|
|
return D->getType();
|
|
|
|
return B.get<const Expr*>()->getType();
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// Get an LValue path entry, which is known to not be an array index, as a
|
2012-02-02 09:16:57 +08:00
|
|
|
/// field or base class.
|
2012-02-15 10:18:13 +08:00
|
|
|
static
|
2012-02-02 09:16:57 +08:00
|
|
|
APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) {
|
2011-11-10 14:34:14 +08:00
|
|
|
APValue::BaseOrMemberType Value;
|
|
|
|
Value.setFromOpaqueValue(E.BaseOrMember);
|
2012-02-02 09:16:57 +08:00
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an LValue path entry, which is known to not be an array index, as a
|
|
|
|
/// field declaration.
|
2012-02-15 10:18:13 +08:00
|
|
|
static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
|
2012-02-02 09:16:57 +08:00
|
|
|
return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer());
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
/// Get an LValue path entry, which is known to not be an array index, as a
|
|
|
|
/// base class declaration.
|
2012-02-15 10:18:13 +08:00
|
|
|
static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
|
2012-02-02 09:16:57 +08:00
|
|
|
return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer());
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
/// Determine whether this LValue path entry for a base class names a virtual
|
|
|
|
/// base class.
|
2012-02-15 10:18:13 +08:00
|
|
|
static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
|
2012-02-02 09:16:57 +08:00
|
|
|
return getAsBaseOrMember(E).getInt();
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
/// Find the path length and type of the most-derived subobject in the given
|
|
|
|
/// path, and find the size of the containing array, if any.
|
|
|
|
static
|
|
|
|
unsigned findMostDerivedSubobject(ASTContext &Ctx, QualType Base,
|
|
|
|
ArrayRef<APValue::LValuePathEntry> Path,
|
|
|
|
uint64_t &ArraySize, QualType &Type) {
|
|
|
|
unsigned MostDerivedLength = 0;
|
|
|
|
Type = Base;
|
2011-11-07 13:07:52 +08:00
|
|
|
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
|
2012-01-07 00:39:00 +08:00
|
|
|
if (Type->isArrayType()) {
|
|
|
|
const ConstantArrayType *CAT =
|
|
|
|
cast<ConstantArrayType>(Ctx.getAsArrayType(Type));
|
|
|
|
Type = CAT->getElementType();
|
|
|
|
ArraySize = CAT->getSize().getZExtValue();
|
|
|
|
MostDerivedLength = I + 1;
|
2012-02-19 06:04:06 +08:00
|
|
|
} else if (Type->isAnyComplexType()) {
|
|
|
|
const ComplexType *CT = Type->castAs<ComplexType>();
|
|
|
|
Type = CT->getElementType();
|
|
|
|
ArraySize = 2;
|
|
|
|
MostDerivedLength = I + 1;
|
2012-01-07 00:39:00 +08:00
|
|
|
} else if (const FieldDecl *FD = getAsField(Path[I])) {
|
|
|
|
Type = FD->getType();
|
|
|
|
ArraySize = 0;
|
|
|
|
MostDerivedLength = I + 1;
|
|
|
|
} else {
|
2011-11-07 13:07:52 +08:00
|
|
|
// Path[I] describes a base class.
|
2012-01-07 00:39:00 +08:00
|
|
|
ArraySize = 0;
|
|
|
|
}
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
2012-01-07 00:39:00 +08:00
|
|
|
return MostDerivedLength;
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
// The order of this enum is important for diagnostics.
|
|
|
|
enum CheckSubobjectKind {
|
2012-02-01 10:39:43 +08:00
|
|
|
CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex,
|
2012-02-19 06:04:06 +08:00
|
|
|
CSK_This, CSK_Real, CSK_Imag
|
2012-01-07 00:39:00 +08:00
|
|
|
};
|
|
|
|
|
2011-11-04 10:25:55 +08:00
|
|
|
/// A path from a glvalue to a subobject of that glvalue.
|
|
|
|
struct SubobjectDesignator {
|
|
|
|
/// True if the subobject was named in a manner not supported by C++11. Such
|
|
|
|
/// lvalues can still be folded, but they are not core constant expressions
|
|
|
|
/// and we cannot perform lvalue-to-rvalue conversions on them.
|
|
|
|
bool Invalid : 1;
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
/// Is this a pointer one past the end of an object?
|
|
|
|
bool IsOnePastTheEnd : 1;
|
|
|
|
|
|
|
|
/// The length of the path to the most-derived object of which this is a
|
|
|
|
/// subobject.
|
|
|
|
unsigned MostDerivedPathLength : 30;
|
|
|
|
|
|
|
|
/// The size of the array of which the most-derived object is an element, or
|
|
|
|
/// 0 if the most-derived object is not an array element.
|
|
|
|
uint64_t MostDerivedArraySize;
|
2011-11-04 10:25:55 +08:00
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
/// The type of the most derived object referred to by this address.
|
|
|
|
QualType MostDerivedType;
|
2011-11-04 10:25:55 +08:00
|
|
|
|
2011-11-07 13:07:52 +08:00
|
|
|
typedef APValue::LValuePathEntry PathEntry;
|
|
|
|
|
2011-11-04 10:25:55 +08:00
|
|
|
/// The entries on the path from the glvalue to the designated subobject.
|
|
|
|
SmallVector<PathEntry, 8> Entries;
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
SubobjectDesignator() : Invalid(true) {}
|
2011-11-04 10:25:55 +08:00
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
explicit SubobjectDesignator(QualType T)
|
|
|
|
: Invalid(false), IsOnePastTheEnd(false), MostDerivedPathLength(0),
|
|
|
|
MostDerivedArraySize(0), MostDerivedType(T) {}
|
|
|
|
|
|
|
|
SubobjectDesignator(ASTContext &Ctx, const APValue &V)
|
|
|
|
: Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
|
|
|
|
MostDerivedPathLength(0), MostDerivedArraySize(0) {
|
2011-11-07 13:07:52 +08:00
|
|
|
if (!Invalid) {
|
2012-01-07 00:39:00 +08:00
|
|
|
IsOnePastTheEnd = V.isLValueOnePastTheEnd();
|
2011-11-07 13:07:52 +08:00
|
|
|
ArrayRef<PathEntry> VEntries = V.getLValuePath();
|
|
|
|
Entries.insert(Entries.end(), VEntries.begin(), VEntries.end());
|
|
|
|
if (V.getLValueBase())
|
2012-01-07 00:39:00 +08:00
|
|
|
MostDerivedPathLength =
|
|
|
|
findMostDerivedSubobject(Ctx, getType(V.getLValueBase()),
|
|
|
|
V.getLValuePath(), MostDerivedArraySize,
|
|
|
|
MostDerivedType);
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-04 10:25:55 +08:00
|
|
|
void setInvalid() {
|
|
|
|
Invalid = true;
|
|
|
|
Entries.clear();
|
|
|
|
}
|
2012-01-07 00:39:00 +08:00
|
|
|
|
|
|
|
/// Determine whether this is a one-past-the-end pointer.
|
|
|
|
bool isOnePastTheEnd() const {
|
|
|
|
if (IsOnePastTheEnd)
|
|
|
|
return true;
|
|
|
|
if (MostDerivedArraySize &&
|
|
|
|
Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check that this refers to a valid subobject.
|
|
|
|
bool isValidSubobject() const {
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
return !isOnePastTheEnd();
|
|
|
|
}
|
|
|
|
/// Check that this refers to a valid subobject, and if not, produce a
|
|
|
|
/// relevant diagnostic and set the designator as invalid.
|
|
|
|
bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
|
|
|
|
|
|
|
|
/// Update this designator to refer to the first element within this array.
|
|
|
|
void addArrayUnchecked(const ConstantArrayType *CAT) {
|
2011-11-04 10:25:55 +08:00
|
|
|
PathEntry Entry;
|
2012-01-07 00:39:00 +08:00
|
|
|
Entry.ArrayIndex = 0;
|
2011-11-04 10:25:55 +08:00
|
|
|
Entries.push_back(Entry);
|
2012-01-07 00:39:00 +08:00
|
|
|
|
|
|
|
// This is a most-derived object.
|
|
|
|
MostDerivedType = CAT->getElementType();
|
|
|
|
MostDerivedArraySize = CAT->getSize().getZExtValue();
|
|
|
|
MostDerivedPathLength = Entries.size();
|
2011-11-04 10:25:55 +08:00
|
|
|
}
|
|
|
|
/// Update this designator to refer to the given base or member of this
|
|
|
|
/// object.
|
2012-01-07 00:39:00 +08:00
|
|
|
void addDeclUnchecked(const Decl *D, bool Virtual = false) {
|
2011-11-04 10:25:55 +08:00
|
|
|
PathEntry Entry;
|
2011-11-10 14:34:14 +08:00
|
|
|
APValue::BaseOrMemberType Value(D, Virtual);
|
|
|
|
Entry.BaseOrMember = Value.getOpaqueValue();
|
2011-11-04 10:25:55 +08:00
|
|
|
Entries.push_back(Entry);
|
2012-01-07 00:39:00 +08:00
|
|
|
|
|
|
|
// If this isn't a base class, it's a new most-derived object.
|
|
|
|
if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
|
|
|
|
MostDerivedType = FD->getType();
|
|
|
|
MostDerivedArraySize = 0;
|
|
|
|
MostDerivedPathLength = Entries.size();
|
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
}
|
2012-02-19 06:04:06 +08:00
|
|
|
/// Update this designator to refer to the given complex component.
|
|
|
|
void addComplexUnchecked(QualType EltTy, bool Imag) {
|
|
|
|
PathEntry Entry;
|
|
|
|
Entry.ArrayIndex = Imag;
|
|
|
|
Entries.push_back(Entry);
|
|
|
|
|
|
|
|
// This is technically a most-derived object, though in practice this
|
|
|
|
// is unlikely to matter.
|
|
|
|
MostDerivedType = EltTy;
|
|
|
|
MostDerivedArraySize = 2;
|
|
|
|
MostDerivedPathLength = Entries.size();
|
|
|
|
}
|
2012-01-07 00:39:00 +08:00
|
|
|
void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, uint64_t N);
|
2011-11-04 10:25:55 +08:00
|
|
|
/// Add N to the address of this subobject.
|
2012-01-07 00:39:00 +08:00
|
|
|
void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
|
2011-11-04 10:25:55 +08:00
|
|
|
if (Invalid) return;
|
2012-01-07 00:39:00 +08:00
|
|
|
if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize) {
|
2011-11-07 13:07:52 +08:00
|
|
|
Entries.back().ArrayIndex += N;
|
2012-01-07 00:39:00 +08:00
|
|
|
if (Entries.back().ArrayIndex > MostDerivedArraySize) {
|
|
|
|
diagnosePointerArithmetic(Info, E, Entries.back().ArrayIndex);
|
|
|
|
setInvalid();
|
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-01-07 00:39:00 +08:00
|
|
|
// [expr.add]p4: For the purposes of these operators, a pointer to a
|
|
|
|
// nonarray object behaves the same as a pointer to the first element of
|
|
|
|
// an array of length one with the type of the object as its element type.
|
|
|
|
if (IsOnePastTheEnd && N == (uint64_t)-1)
|
|
|
|
IsOnePastTheEnd = false;
|
|
|
|
else if (!IsOnePastTheEnd && N == 1)
|
|
|
|
IsOnePastTheEnd = true;
|
|
|
|
else if (N != 0) {
|
|
|
|
diagnosePointerArithmetic(Info, E, uint64_t(IsOnePastTheEnd) + N);
|
2011-11-04 10:25:55 +08:00
|
|
|
setInvalid();
|
2012-01-07 00:39:00 +08:00
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-10-31 13:52:43 +08:00
|
|
|
/// A stack frame in the constexpr call stack.
|
|
|
|
struct CallStackFrame {
|
|
|
|
EvalInfo &Info;
|
|
|
|
|
|
|
|
/// Parent - The caller of this stack frame.
|
|
|
|
CallStackFrame *Caller;
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
/// CallLoc - The location of the call expression for this call.
|
|
|
|
SourceLocation CallLoc;
|
|
|
|
|
|
|
|
/// Callee - The function which was called.
|
|
|
|
const FunctionDecl *Callee;
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
/// Index - The call index of this call.
|
|
|
|
unsigned Index;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// This - The binding for the this pointer in this call, if any.
|
|
|
|
const LValue *This;
|
|
|
|
|
2011-10-31 13:52:43 +08:00
|
|
|
/// ParmBindings - Parameter bindings for this function call, indexed by
|
|
|
|
/// parameters' function scope indices.
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue *Arguments;
|
2011-10-31 13:52:43 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
typedef llvm::DenseMap<const Expr*, APValue> MapTy;
|
2011-10-31 13:52:43 +08:00
|
|
|
typedef MapTy::const_iterator temp_iterator;
|
|
|
|
/// Temporaries - Temporary lvalues materialized within this stack frame.
|
|
|
|
MapTy Temporaries;
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
|
|
|
|
const FunctionDecl *Callee, const LValue *This,
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue *Arguments);
|
2011-10-31 13:52:43 +08:00
|
|
|
~CallStackFrame();
|
|
|
|
};
|
|
|
|
|
2011-12-12 17:28:41 +08:00
|
|
|
/// A partial diagnostic which we might know in advance that we are not going
|
|
|
|
/// to emit.
|
|
|
|
class OptionalDiagnostic {
|
|
|
|
PartialDiagnostic *Diag;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit OptionalDiagnostic(PartialDiagnostic *Diag = 0) : Diag(Diag) {}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
OptionalDiagnostic &operator<<(const T &v) {
|
|
|
|
if (Diag)
|
|
|
|
*Diag << v;
|
|
|
|
return *this;
|
|
|
|
}
|
2012-01-31 12:08:20 +08:00
|
|
|
|
|
|
|
OptionalDiagnostic &operator<<(const APSInt &I) {
|
|
|
|
if (Diag) {
|
|
|
|
llvm::SmallVector<char, 32> Buffer;
|
|
|
|
I.toString(Buffer);
|
|
|
|
*Diag << StringRef(Buffer.data(), Buffer.size());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalDiagnostic &operator<<(const APFloat &F) {
|
|
|
|
if (Diag) {
|
|
|
|
llvm::SmallVector<char, 32> Buffer;
|
|
|
|
F.toString(Buffer);
|
|
|
|
*Diag << StringRef(Buffer.data(), Buffer.size());
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2011-12-12 17:28:41 +08:00
|
|
|
};
|
|
|
|
|
2012-02-15 10:18:13 +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.
|
2011-03-04 21:12:48 +08:00
|
|
|
struct EvalInfo {
|
2011-12-12 17:28:41 +08:00
|
|
|
ASTContext &Ctx;
|
2012-02-28 04:21:34 +08:00
|
|
|
|
2011-10-17 05:26:27 +08:00
|
|
|
/// EvalStatus - Contains information about the evaluation.
|
|
|
|
Expr::EvalStatus &EvalStatus;
|
2008-12-01 02:26:25 +08:00
|
|
|
|
2011-10-29 06:34:42 +08:00
|
|
|
/// CurrentCall - The top of the constexpr call stack.
|
2011-10-31 13:52:43 +08:00
|
|
|
CallStackFrame *CurrentCall;
|
2011-10-29 06:34:42 +08:00
|
|
|
|
|
|
|
/// CallStackDepth - The number of calls in the call stack right now.
|
|
|
|
unsigned CallStackDepth;
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
/// NextCallIndex - The next call index to assign.
|
|
|
|
unsigned NextCallIndex;
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
typedef llvm::DenseMap<const OpaqueValueExpr*, APValue> MapTy;
|
2011-10-31 13:52:43 +08:00
|
|
|
/// OpaqueValues - Values used as the common expression in a
|
|
|
|
/// BinaryConditionalOperator.
|
2011-03-04 21:12:48 +08:00
|
|
|
MapTy OpaqueValues;
|
2011-10-31 13:52:43 +08:00
|
|
|
|
|
|
|
/// BottomFrame - The frame in which evaluation started. This must be
|
2012-01-27 09:14:48 +08:00
|
|
|
/// initialized after CurrentCall and CallStackDepth.
|
2011-10-31 13:52:43 +08:00
|
|
|
CallStackFrame BottomFrame;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// EvaluatingDecl - This is the declaration whose initializer is being
|
|
|
|
/// evaluated, if any.
|
|
|
|
const VarDecl *EvaluatingDecl;
|
|
|
|
|
|
|
|
/// EvaluatingDeclValue - This is the value being constructed for the
|
|
|
|
/// declaration whose initializer is being evaluated, if any.
|
|
|
|
APValue *EvaluatingDeclValue;
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
/// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further
|
|
|
|
/// notes attached to it will also be stored, otherwise they will not be.
|
|
|
|
bool HasActiveDiagnostic;
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
/// CheckingPotentialConstantExpression - Are we checking whether the
|
|
|
|
/// expression is a potential constant expression? If so, some diagnostics
|
|
|
|
/// are suppressed.
|
|
|
|
bool CheckingPotentialConstantExpression;
|
|
|
|
|
2012-02-28 07:18:37 +08:00
|
|
|
/// \brief Stack depth of IntExprEvaluator.
|
|
|
|
/// We check this against a maximum value to avoid stack overflow, see
|
|
|
|
/// test case in test/Sema/many-logical-ops.c.
|
|
|
|
// FIXME: This is a hack; handle properly unlimited logical ops.
|
|
|
|
unsigned IntExprEvaluatorDepth;
|
2011-10-31 13:52:43 +08:00
|
|
|
|
|
|
|
EvalInfo(const ASTContext &C, Expr::EvalStatus &S)
|
2011-12-12 17:28:41 +08:00
|
|
|
: Ctx(const_cast<ASTContext&>(C)), EvalStatus(S), CurrentCall(0),
|
2012-02-15 10:18:13 +08:00
|
|
|
CallStackDepth(0), NextCallIndex(1),
|
|
|
|
BottomFrame(*this, SourceLocation(), 0, 0, 0),
|
2012-01-27 09:14:48 +08:00
|
|
|
EvaluatingDecl(0), EvaluatingDeclValue(0), HasActiveDiagnostic(false),
|
2012-02-28 07:18:37 +08:00
|
|
|
CheckingPotentialConstantExpression(false), IntExprEvaluatorDepth(0) {}
|
2011-10-31 13:52:43 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue *getOpaqueValue(const OpaqueValueExpr *e) const {
|
2011-03-04 21:12:48 +08:00
|
|
|
MapTy::const_iterator i = OpaqueValues.find(e);
|
|
|
|
if (i == OpaqueValues.end()) return 0;
|
|
|
|
return &i->second;
|
|
|
|
}
|
2011-02-17 18:25:35 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
void setEvaluatingDecl(const VarDecl *VD, APValue &Value) {
|
|
|
|
EvaluatingDecl = VD;
|
|
|
|
EvaluatingDeclValue = &Value;
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); }
|
2011-11-22 03:36:32 +08:00
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
bool CheckCallLimit(SourceLocation Loc) {
|
2012-01-27 09:14:48 +08:00
|
|
|
// Don't perform any constexpr calls (other than the call we're checking)
|
|
|
|
// when checking a potential constant expression.
|
|
|
|
if (CheckingPotentialConstantExpression && CallStackDepth > 1)
|
|
|
|
return false;
|
2012-02-15 10:18:13 +08:00
|
|
|
if (NextCallIndex == 0) {
|
|
|
|
// NextCallIndex has wrapped around.
|
|
|
|
Diag(Loc, diag::note_constexpr_call_limit_exceeded);
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-13 14:39:58 +08:00
|
|
|
if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
|
|
|
|
return true;
|
|
|
|
Diag(Loc, diag::note_constexpr_depth_limit_exceeded)
|
|
|
|
<< getLangOpts().ConstexprCallDepth;
|
|
|
|
return false;
|
2011-11-22 03:36:32 +08:00
|
|
|
}
|
2011-12-10 06:58:01 +08:00
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
CallStackFrame *getCallFrame(unsigned CallIndex) {
|
|
|
|
assert(CallIndex && "no call index in getCallFrame");
|
|
|
|
// We will eventually hit BottomFrame, which has Index 1, so Frame can't
|
|
|
|
// be null in this loop.
|
|
|
|
CallStackFrame *Frame = CurrentCall;
|
|
|
|
while (Frame->Index > CallIndex)
|
|
|
|
Frame = Frame->Caller;
|
|
|
|
return (Frame->Index == CallIndex) ? Frame : 0;
|
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
private:
|
|
|
|
/// Add a diagnostic to the diagnostics list.
|
|
|
|
PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) {
|
|
|
|
PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator());
|
|
|
|
EvalStatus.Diag->push_back(std::make_pair(Loc, PD));
|
|
|
|
return EvalStatus.Diag->back().second;
|
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
/// Add notes containing a call stack to the current point of evaluation.
|
|
|
|
void addCallStack(unsigned Limit);
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
public:
|
2011-12-10 06:58:01 +08:00
|
|
|
/// Diagnose that the evaluation cannot be folded.
|
2011-12-21 13:04:46 +08:00
|
|
|
OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId
|
|
|
|
= diag::note_invalid_subexpr_in_const_expr,
|
2011-12-13 14:39:58 +08:00
|
|
|
unsigned ExtraNotes = 0) {
|
2011-12-10 06:58:01 +08:00
|
|
|
// If we have a prior diagnostic, it will be noting that the expression
|
|
|
|
// isn't a constant expression. This diagnostic is more important.
|
|
|
|
// FIXME: We might want to show both diagnostics to the user.
|
2011-12-12 17:28:41 +08:00
|
|
|
if (EvalStatus.Diag) {
|
2011-12-17 03:06:07 +08:00
|
|
|
unsigned CallStackNotes = CallStackDepth - 1;
|
|
|
|
unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit();
|
|
|
|
if (Limit)
|
|
|
|
CallStackNotes = std::min(CallStackNotes, Limit + 1);
|
2012-01-27 09:14:48 +08:00
|
|
|
if (CheckingPotentialConstantExpression)
|
|
|
|
CallStackNotes = 0;
|
2011-12-17 03:06:07 +08:00
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
HasActiveDiagnostic = true;
|
2011-12-12 17:28:41 +08:00
|
|
|
EvalStatus.Diag->clear();
|
2011-12-17 03:06:07 +08:00
|
|
|
EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes);
|
|
|
|
addDiag(Loc, DiagId);
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!CheckingPotentialConstantExpression)
|
|
|
|
addCallStack(Limit);
|
2011-12-17 03:06:07 +08:00
|
|
|
return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second);
|
2011-12-12 17:28:41 +08:00
|
|
|
}
|
2011-12-13 14:39:58 +08:00
|
|
|
HasActiveDiagnostic = false;
|
2011-12-12 17:28:41 +08:00
|
|
|
return OptionalDiagnostic();
|
|
|
|
}
|
|
|
|
|
2012-03-15 12:53:45 +08:00
|
|
|
OptionalDiagnostic Diag(const Expr *E, diag::kind DiagId
|
|
|
|
= diag::note_invalid_subexpr_in_const_expr,
|
|
|
|
unsigned ExtraNotes = 0) {
|
|
|
|
if (EvalStatus.Diag)
|
|
|
|
return Diag(E->getExprLoc(), DiagId, ExtraNotes);
|
|
|
|
HasActiveDiagnostic = false;
|
|
|
|
return OptionalDiagnostic();
|
|
|
|
}
|
|
|
|
|
2011-12-12 17:28:41 +08:00
|
|
|
/// Diagnose that the evaluation does not produce a C++11 core constant
|
|
|
|
/// expression.
|
2012-03-15 12:53:45 +08:00
|
|
|
template<typename LocArg>
|
|
|
|
OptionalDiagnostic CCEDiag(LocArg Loc, diag::kind DiagId
|
2011-12-21 13:04:46 +08:00
|
|
|
= diag::note_invalid_subexpr_in_const_expr,
|
2011-12-13 14:39:58 +08:00
|
|
|
unsigned ExtraNotes = 0) {
|
2011-12-12 17:28:41 +08:00
|
|
|
// Don't override a previous diagnostic.
|
2012-02-22 06:41:33 +08:00
|
|
|
if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) {
|
|
|
|
HasActiveDiagnostic = false;
|
2011-12-12 17:28:41 +08:00
|
|
|
return OptionalDiagnostic();
|
2012-02-22 06:41:33 +08:00
|
|
|
}
|
2011-12-13 14:39:58 +08:00
|
|
|
return Diag(Loc, DiagId, ExtraNotes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a note to a prior diagnostic.
|
|
|
|
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) {
|
|
|
|
if (!HasActiveDiagnostic)
|
|
|
|
return OptionalDiagnostic();
|
|
|
|
return OptionalDiagnostic(&addDiag(Loc, DiagId));
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-12-19 14:19:21 +08:00
|
|
|
|
|
|
|
/// Add a stack of notes to a prior diagnostic.
|
|
|
|
void addNotes(ArrayRef<PartialDiagnosticAt> Diags) {
|
|
|
|
if (HasActiveDiagnostic) {
|
|
|
|
EvalStatus.Diag->insert(EvalStatus.Diag->end(),
|
|
|
|
Diags.begin(), Diags.end());
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 09:14:48 +08:00
|
|
|
|
|
|
|
/// Should we continue evaluation as much as possible after encountering a
|
|
|
|
/// construct which can't be folded?
|
|
|
|
bool keepEvaluatingAfterFailure() {
|
2012-02-16 10:46:34 +08:00
|
|
|
return CheckingPotentialConstantExpression &&
|
|
|
|
EvalStatus.Diag && EvalStatus.Diag->empty();
|
2012-01-27 09:14:48 +08:00
|
|
|
}
|
2011-03-04 21:12:48 +08:00
|
|
|
};
|
2012-02-02 09:16:57 +08:00
|
|
|
|
|
|
|
/// Object used to treat all foldable expressions as constant expressions.
|
|
|
|
struct FoldConstant {
|
|
|
|
bool Enabled;
|
|
|
|
|
|
|
|
explicit FoldConstant(EvalInfo &Info)
|
|
|
|
: Enabled(Info.EvalStatus.Diag && Info.EvalStatus.Diag->empty() &&
|
|
|
|
!Info.EvalStatus.HasSideEffects) {
|
|
|
|
}
|
|
|
|
// Treat the value we've computed since this object was created as constant.
|
|
|
|
void Fold(EvalInfo &Info) {
|
|
|
|
if (Enabled && !Info.EvalStatus.Diag->empty() &&
|
|
|
|
!Info.EvalStatus.HasSideEffects)
|
|
|
|
Info.EvalStatus.Diag->clear();
|
|
|
|
}
|
|
|
|
};
|
2012-02-16 10:46:34 +08:00
|
|
|
|
|
|
|
/// RAII object used to suppress diagnostics and side-effects from a
|
|
|
|
/// speculative evaluation.
|
|
|
|
class SpeculativeEvaluationRAII {
|
|
|
|
EvalInfo &Info;
|
|
|
|
Expr::EvalStatus Old;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SpeculativeEvaluationRAII(EvalInfo &Info,
|
|
|
|
llvm::SmallVectorImpl<PartialDiagnosticAt>
|
|
|
|
*NewDiag = 0)
|
|
|
|
: Info(Info), Old(Info.EvalStatus) {
|
|
|
|
Info.EvalStatus.Diag = NewDiag;
|
|
|
|
}
|
|
|
|
~SpeculativeEvaluationRAII() {
|
|
|
|
Info.EvalStatus = Old;
|
|
|
|
}
|
|
|
|
};
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
|
|
|
|
CheckSubobjectKind CSK) {
|
|
|
|
if (Invalid)
|
|
|
|
return false;
|
|
|
|
if (isOnePastTheEnd()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_past_end_subobject)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< CSK;
|
|
|
|
setInvalid();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
|
|
|
|
const Expr *E, uint64_t N) {
|
|
|
|
if (MostDerivedPathLength == Entries.size() && MostDerivedArraySize)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_array_index)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< static_cast<int>(N) << /*array*/ 0
|
|
|
|
<< static_cast<unsigned>(MostDerivedArraySize);
|
|
|
|
else
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_array_index)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< static_cast<int>(N) << /*non-array*/ 1;
|
|
|
|
setInvalid();
|
|
|
|
}
|
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc,
|
|
|
|
const FunctionDecl *Callee, const LValue *This,
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue *Arguments)
|
2011-12-17 03:06:07 +08:00
|
|
|
: Info(Info), Caller(Info.CurrentCall), CallLoc(CallLoc), Callee(Callee),
|
2012-02-15 10:18:13 +08:00
|
|
|
Index(Info.NextCallIndex++), This(This), Arguments(Arguments) {
|
2011-12-17 03:06:07 +08:00
|
|
|
Info.CurrentCall = this;
|
|
|
|
++Info.CallStackDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
CallStackFrame::~CallStackFrame() {
|
|
|
|
assert(Info.CurrentCall == this && "calls retired out of order");
|
|
|
|
--Info.CallStackDepth;
|
|
|
|
Info.CurrentCall = Caller;
|
|
|
|
}
|
2008-07-12 06:52:41 +08:00
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
/// Produce a string describing the given constexpr call.
|
|
|
|
static void describeCall(CallStackFrame *Frame, llvm::raw_ostream &Out) {
|
|
|
|
unsigned ArgIndex = 0;
|
|
|
|
bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) &&
|
2012-02-04 08:33:54 +08:00
|
|
|
!isa<CXXConstructorDecl>(Frame->Callee) &&
|
|
|
|
cast<CXXMethodDecl>(Frame->Callee)->isInstance();
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
if (!IsMemberCall)
|
|
|
|
Out << *Frame->Callee << '(';
|
|
|
|
|
|
|
|
for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(),
|
|
|
|
E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) {
|
2012-01-26 17:37:36 +08:00
|
|
|
if (ArgIndex > (unsigned)IsMemberCall)
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << ", ";
|
|
|
|
|
|
|
|
const ParmVarDecl *Param = *I;
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue &Arg = Frame->Arguments[ArgIndex];
|
|
|
|
Arg.printPretty(Out, Frame->Info.Ctx, Param->getType());
|
2011-12-17 03:06:07 +08:00
|
|
|
|
|
|
|
if (ArgIndex == 0 && IsMemberCall)
|
|
|
|
Out << "->" << *Frame->Callee << '(';
|
2011-10-31 13:52:43 +08:00
|
|
|
}
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
Out << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
void EvalInfo::addCallStack(unsigned Limit) {
|
|
|
|
// Determine which calls to skip, if any.
|
|
|
|
unsigned ActiveCalls = CallStackDepth - 1;
|
|
|
|
unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart;
|
|
|
|
if (Limit && Limit < ActiveCalls) {
|
|
|
|
SkipStart = Limit / 2 + Limit % 2;
|
|
|
|
SkipEnd = ActiveCalls - Limit / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the call stack and add the diagnostics.
|
|
|
|
unsigned CallIdx = 0;
|
|
|
|
for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame;
|
|
|
|
Frame = Frame->Caller, ++CallIdx) {
|
|
|
|
// Skip this call?
|
|
|
|
if (CallIdx >= SkipStart && CallIdx < SkipEnd) {
|
|
|
|
if (CallIdx == SkipStart) {
|
|
|
|
// Note that we're skipping calls.
|
|
|
|
addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed)
|
|
|
|
<< unsigned(ActiveCalls - Limit);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallVector<char, 128> Buffer;
|
|
|
|
llvm::raw_svector_ostream Out(Buffer);
|
|
|
|
describeCall(Frame, Out);
|
|
|
|
addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str();
|
2011-10-31 13:52:43 +08:00
|
|
|
}
|
2011-12-17 03:06:07 +08:00
|
|
|
}
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2011-12-17 03:06:07 +08:00
|
|
|
namespace {
|
2010-05-08 01:22:02 +08:00
|
|
|
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; }
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
void moveInto(APValue &v) const {
|
2010-05-08 01:22:02 +08:00
|
|
|
if (isComplexFloat())
|
2012-03-04 06:46:17 +08:00
|
|
|
v = APValue(FloatReal, FloatImag);
|
2010-05-08 01:22:02 +08:00
|
|
|
else
|
2012-03-04 06:46:17 +08:00
|
|
|
v = APValue(IntReal, IntImag);
|
2010-05-08 01:22:02 +08:00
|
|
|
}
|
2012-03-04 06:46:17 +08:00
|
|
|
void setFrom(const APValue &v) {
|
2011-02-17 18:25:35 +08:00
|
|
|
assert(v.isComplexFloat() || v.isComplexInt());
|
|
|
|
if (v.isComplexFloat()) {
|
|
|
|
makeComplexFloat();
|
|
|
|
FloatReal = v.getComplexFloatReal();
|
|
|
|
FloatImag = v.getComplexFloatImag();
|
|
|
|
} else {
|
|
|
|
makeComplexInt();
|
|
|
|
IntReal = v.getComplexIntReal();
|
|
|
|
IntImag = v.getComplexIntImag();
|
|
|
|
}
|
|
|
|
}
|
2010-05-08 01:22:02 +08:00
|
|
|
};
|
2010-05-08 05:00:08 +08:00
|
|
|
|
|
|
|
struct LValue {
|
2011-11-13 06:28:03 +08:00
|
|
|
APValue::LValueBase Base;
|
2010-05-08 05:00:08 +08:00
|
|
|
CharUnits Offset;
|
2012-02-15 10:18:13 +08:00
|
|
|
unsigned CallIndex;
|
2011-11-04 10:25:55 +08:00
|
|
|
SubobjectDesignator Designator;
|
2010-05-08 05:00:08 +08:00
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
const APValue::LValueBase getLValueBase() const { return Base; }
|
2011-10-30 04:57:55 +08:00
|
|
|
CharUnits &getLValueOffset() { return Offset; }
|
2011-10-31 09:37:14 +08:00
|
|
|
const CharUnits &getLValueOffset() const { return Offset; }
|
2012-02-15 10:18:13 +08:00
|
|
|
unsigned getLValueCallIndex() const { return CallIndex; }
|
2011-11-04 10:25:55 +08:00
|
|
|
SubobjectDesignator &getLValueDesignator() { return Designator; }
|
|
|
|
const SubobjectDesignator &getLValueDesignator() const { return Designator;}
|
2010-05-08 05:00:08 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
void moveInto(APValue &V) const {
|
|
|
|
if (Designator.Invalid)
|
|
|
|
V = APValue(Base, Offset, APValue::NoLValuePath(), CallIndex);
|
|
|
|
else
|
|
|
|
V = APValue(Base, Offset, Designator.Entries,
|
|
|
|
Designator.IsOnePastTheEnd, CallIndex);
|
2010-05-08 05:00:08 +08:00
|
|
|
}
|
2012-03-04 06:46:17 +08:00
|
|
|
void setFrom(ASTContext &Ctx, const APValue &V) {
|
2011-10-30 04:57:55 +08:00
|
|
|
assert(V.isLValue());
|
|
|
|
Base = V.getLValueBase();
|
|
|
|
Offset = V.getLValueOffset();
|
2012-02-15 10:18:13 +08:00
|
|
|
CallIndex = V.getLValueCallIndex();
|
2012-03-04 06:46:17 +08:00
|
|
|
Designator = SubobjectDesignator(Ctx, V);
|
2011-11-04 10:25:55 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
void set(APValue::LValueBase B, unsigned I = 0) {
|
2011-11-13 06:28:03 +08:00
|
|
|
Base = B;
|
2011-11-04 10:25:55 +08:00
|
|
|
Offset = CharUnits::Zero();
|
2012-02-15 10:18:13 +08:00
|
|
|
CallIndex = I;
|
2012-01-07 00:39:00 +08:00
|
|
|
Designator = SubobjectDesignator(getType(B));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that this LValue is not based on a null pointer. If it is, produce
|
|
|
|
// a diagnostic and mark the designator as invalid.
|
|
|
|
bool checkNullPointer(EvalInfo &Info, const Expr *E,
|
|
|
|
CheckSubobjectKind CSK) {
|
|
|
|
if (Designator.Invalid)
|
|
|
|
return false;
|
|
|
|
if (!Base) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_null_subobject)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< CSK;
|
|
|
|
Designator.setInvalid();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check this LValue refers to an object. If not, set the designator to be
|
|
|
|
// invalid and emit a diagnostic.
|
|
|
|
bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
|
2012-03-15 12:53:45 +08:00
|
|
|
// Outside C++11, do not build a designator referring to a subobject of
|
|
|
|
// any object: we won't use such a designator for anything.
|
|
|
|
if (!Info.getLangOpts().CPlusPlus0x)
|
|
|
|
Designator.setInvalid();
|
2012-01-07 00:39:00 +08:00
|
|
|
return checkNullPointer(Info, E, CSK) &&
|
|
|
|
Designator.checkSubobject(Info, E, CSK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addDecl(EvalInfo &Info, const Expr *E,
|
|
|
|
const Decl *D, bool Virtual = false) {
|
2012-03-15 12:53:45 +08:00
|
|
|
if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base))
|
|
|
|
Designator.addDeclUnchecked(D, Virtual);
|
2012-01-07 00:39:00 +08:00
|
|
|
}
|
|
|
|
void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
|
2012-03-15 12:53:45 +08:00
|
|
|
if (checkSubobject(Info, E, CSK_ArrayToPointer))
|
|
|
|
Designator.addArrayUnchecked(CAT);
|
2012-01-07 00:39:00 +08:00
|
|
|
}
|
2012-02-19 06:04:06 +08:00
|
|
|
void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
|
2012-03-15 12:53:45 +08:00
|
|
|
if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real))
|
|
|
|
Designator.addComplexUnchecked(EltTy, Imag);
|
2012-02-19 06:04:06 +08:00
|
|
|
}
|
2012-01-07 00:39:00 +08:00
|
|
|
void adjustIndex(EvalInfo &Info, const Expr *E, uint64_t N) {
|
2012-03-15 12:53:45 +08:00
|
|
|
if (checkNullPointer(Info, E, CSK_ArrayIndex))
|
|
|
|
Designator.adjustIndex(Info, E, N);
|
2011-02-17 18:25:35 +08:00
|
|
|
}
|
2010-05-08 05:00:08 +08:00
|
|
|
};
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
struct MemberPtr {
|
|
|
|
MemberPtr() {}
|
|
|
|
explicit MemberPtr(const ValueDecl *Decl) :
|
|
|
|
DeclAndIsDerivedMember(Decl, false), Path() {}
|
|
|
|
|
|
|
|
/// The member or (direct or indirect) field referred to by this member
|
|
|
|
/// pointer, or 0 if this is a null member pointer.
|
|
|
|
const ValueDecl *getDecl() const {
|
|
|
|
return DeclAndIsDerivedMember.getPointer();
|
|
|
|
}
|
|
|
|
/// Is this actually a member of some type derived from the relevant class?
|
|
|
|
bool isDerivedMember() const {
|
|
|
|
return DeclAndIsDerivedMember.getInt();
|
|
|
|
}
|
|
|
|
/// Get the class which the declaration actually lives in.
|
|
|
|
const CXXRecordDecl *getContainingRecord() const {
|
|
|
|
return cast<CXXRecordDecl>(
|
|
|
|
DeclAndIsDerivedMember.getPointer()->getDeclContext());
|
|
|
|
}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
void moveInto(APValue &V) const {
|
|
|
|
V = APValue(getDecl(), isDerivedMember(), Path);
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
2012-03-04 06:46:17 +08:00
|
|
|
void setFrom(const APValue &V) {
|
2011-11-18 06:56:20 +08:00
|
|
|
assert(V.isMemberPointer());
|
|
|
|
DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
|
|
|
|
DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
|
|
|
|
Path.clear();
|
|
|
|
ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath();
|
|
|
|
Path.insert(Path.end(), P.begin(), P.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeclAndIsDerivedMember - The member declaration, and a flag indicating
|
|
|
|
/// whether the member is a member of some class derived from the class type
|
|
|
|
/// of the member pointer.
|
|
|
|
llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
|
|
|
|
/// Path - The path of base/derived classes from the member declaration's
|
|
|
|
/// class (exclusive) to the class type of the member pointer (inclusive).
|
|
|
|
SmallVector<const CXXRecordDecl*, 4> Path;
|
|
|
|
|
|
|
|
/// Perform a cast towards the class of the Decl (either up or down the
|
|
|
|
/// hierarchy).
|
|
|
|
bool castBack(const CXXRecordDecl *Class) {
|
|
|
|
assert(!Path.empty());
|
|
|
|
const CXXRecordDecl *Expected;
|
|
|
|
if (Path.size() >= 2)
|
|
|
|
Expected = Path[Path.size() - 2];
|
|
|
|
else
|
|
|
|
Expected = getContainingRecord();
|
|
|
|
if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
|
|
|
|
// C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
|
|
|
|
// if B does not contain the original member and is not a base or
|
|
|
|
// derived class of the class containing the original member, the result
|
|
|
|
// of the cast is undefined.
|
|
|
|
// C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
|
|
|
|
// (D::*). We consider that to be a language defect.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Path.pop_back();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/// Perform a base-to-derived member pointer cast.
|
|
|
|
bool castToDerived(const CXXRecordDecl *Derived) {
|
|
|
|
if (!getDecl())
|
|
|
|
return true;
|
|
|
|
if (!isDerivedMember()) {
|
|
|
|
Path.push_back(Derived);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!castBack(Derived))
|
|
|
|
return false;
|
|
|
|
if (Path.empty())
|
|
|
|
DeclAndIsDerivedMember.setInt(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/// Perform a derived-to-base member pointer cast.
|
|
|
|
bool castToBase(const CXXRecordDecl *Base) {
|
|
|
|
if (!getDecl())
|
|
|
|
return true;
|
|
|
|
if (Path.empty())
|
|
|
|
DeclAndIsDerivedMember.setInt(true);
|
|
|
|
if (isDerivedMember()) {
|
|
|
|
Path.push_back(Base);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return castBack(Base);
|
|
|
|
}
|
|
|
|
};
|
2011-12-13 14:39:58 +08:00
|
|
|
|
2012-02-01 09:42:44 +08:00
|
|
|
/// Compare two member pointers, which are assumed to be of the same type.
|
|
|
|
static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
|
|
|
|
if (!LHS.getDecl() || !RHS.getDecl())
|
|
|
|
return !LHS.getDecl() && !RHS.getDecl();
|
|
|
|
if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
|
|
|
|
return false;
|
|
|
|
return LHS.Path == RHS.Path;
|
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
/// Kinds of constant expression checking, for diagnostics.
|
|
|
|
enum CheckConstantExpressionKind {
|
|
|
|
CCEK_Constant, ///< A normal constant.
|
|
|
|
CCEK_ReturnValue, ///< A constexpr function return value.
|
|
|
|
CCEK_MemberInit ///< A constexpr constructor mem-initializer.
|
|
|
|
};
|
2010-05-08 01:22:02 +08:00
|
|
|
}
|
2008-07-12 06:52:41 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
|
2012-02-15 10:18:13 +08:00
|
|
|
static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
|
|
|
|
const LValue &This, const Expr *E,
|
|
|
|
CheckConstantExpressionKind CCEK = CCEK_Constant,
|
|
|
|
bool AllowNonLiteralTypes = false);
|
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);
|
2011-11-18 06:56:20 +08:00
|
|
|
static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
|
|
|
|
EvalInfo &Info);
|
|
|
|
static bool EvaluateTemporary(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);
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
|
2009-10-29 07:59:40 +08:00
|
|
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// Should this call expression be treated as a string literal?
|
|
|
|
static bool IsStringLiteralCall(const CallExpr *E) {
|
|
|
|
unsigned Builtin = E->isBuiltinCall();
|
|
|
|
return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
|
|
|
|
Builtin == Builtin::BI__builtin___NSStringMakeConstantString);
|
|
|
|
}
|
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
static bool IsGlobalLValue(APValue::LValueBase B) {
|
2011-11-10 14:34:14 +08:00
|
|
|
// C++11 [expr.const]p3 An address constant expression is a prvalue core
|
|
|
|
// constant expression of pointer type that evaluates to...
|
|
|
|
|
|
|
|
// ... a null pointer value, or a prvalue core constant expression of type
|
|
|
|
// std::nullptr_t.
|
2011-11-13 06:28:03 +08:00
|
|
|
if (!B) return true;
|
2010-05-11 07:27:23 +08:00
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
|
2011-11-10 14:34:14 +08:00
|
|
|
// ... the address of an object with static storage duration,
|
2011-11-13 06:28:03 +08:00
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
2010-05-11 07:27:23 +08:00
|
|
|
return VD->hasGlobalStorage();
|
2011-11-13 06:28:03 +08:00
|
|
|
// ... the address of a function,
|
|
|
|
return isa<FunctionDecl>(D);
|
2010-05-11 07:27:23 +08:00
|
|
|
}
|
2011-11-13 06:28:03 +08:00
|
|
|
|
|
|
|
const Expr *E = B.get<const Expr*>();
|
|
|
|
switch (E->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return false;
|
2012-02-18 12:58:18 +08:00
|
|
|
case Expr::CompoundLiteralExprClass: {
|
|
|
|
const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
|
|
|
|
return CLE->isFileScope() && CLE->isLValue();
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
// A string literal has static storage duration.
|
|
|
|
case Expr::StringLiteralClass:
|
|
|
|
case Expr::PredefinedExprClass:
|
|
|
|
case Expr::ObjCStringLiteralClass:
|
|
|
|
case Expr::ObjCEncodeExprClass:
|
2011-12-27 20:18:28 +08:00
|
|
|
case Expr::CXXTypeidExprClass:
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
|
|
|
case Expr::CallExprClass:
|
|
|
|
return IsStringLiteralCall(cast<CallExpr>(E));
|
|
|
|
// For GCC compatibility, &&label has static storage duration.
|
|
|
|
case Expr::AddrLabelExprClass:
|
|
|
|
return true;
|
|
|
|
// A Block literal expression may be used as the initialization value for
|
|
|
|
// Block variables at global or local static scope.
|
|
|
|
case Expr::BlockExprClass:
|
|
|
|
return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures();
|
2012-01-27 09:14:48 +08:00
|
|
|
case Expr::ImplicitValueInitExprClass:
|
|
|
|
// FIXME:
|
|
|
|
// We can never form an lvalue with an implicit value initialization as its
|
|
|
|
// base through expression evaluation, so these only appear in one case: the
|
|
|
|
// implicit variable declaration we invent when checking whether a constexpr
|
|
|
|
// constructor can produce a constant expression. We must assume that such
|
|
|
|
// an expression might be a global lvalue.
|
|
|
|
return true;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
2010-05-11 07:27:23 +08:00
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
|
|
|
|
assert(Base && "no location for a null lvalue");
|
|
|
|
const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
|
|
|
|
if (VD)
|
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
else
|
|
|
|
Info.Note(Base.dyn_cast<const Expr*>()->getExprLoc(),
|
|
|
|
diag::note_constexpr_temporary_here);
|
|
|
|
}
|
|
|
|
|
2011-11-07 13:07:52 +08:00
|
|
|
/// Check that this reference or pointer core constant expression is a valid
|
2012-03-04 06:46:17 +08:00
|
|
|
/// value for an address or reference constant expression. Return true if we
|
|
|
|
/// can fold this expression, whether or not it's a constant expression.
|
2012-02-15 10:18:13 +08:00
|
|
|
static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
|
|
|
|
QualType Type, const LValue &LVal) {
|
|
|
|
bool IsReferenceType = Type->isReferenceType();
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
APValue::LValueBase Base = LVal.getLValueBase();
|
|
|
|
const SubobjectDesignator &Designator = LVal.getLValueDesignator();
|
|
|
|
|
2012-02-18 12:58:18 +08:00
|
|
|
// Check that the object is a global. Note that the fake 'this' object we
|
|
|
|
// manufacture when checking potential constant expressions is conservatively
|
|
|
|
// assumed to be global here.
|
2011-12-13 14:39:58 +08:00
|
|
|
if (!IsGlobalLValue(Base)) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x) {
|
|
|
|
const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
|
2012-02-15 10:18:13 +08:00
|
|
|
Info.Diag(Loc, diag::note_constexpr_non_global, 1)
|
|
|
|
<< IsReferenceType << !Designator.Entries.empty()
|
|
|
|
<< !!VD << VD;
|
|
|
|
NoteLValueLocation(Info, Base);
|
2011-12-13 14:39:58 +08:00
|
|
|
} else {
|
2012-02-15 10:18:13 +08:00
|
|
|
Info.Diag(Loc);
|
2011-12-13 14:39:58 +08:00
|
|
|
}
|
2012-01-12 14:08:57 +08:00
|
|
|
// Don't allow references to temporaries to escape.
|
2011-11-04 13:33:44 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2012-02-15 10:18:13 +08:00
|
|
|
assert((Info.CheckingPotentialConstantExpression ||
|
|
|
|
LVal.getLValueCallIndex() == 0) &&
|
|
|
|
"have call index for global lvalue");
|
2012-01-07 00:39:00 +08:00
|
|
|
|
|
|
|
// Allow address constant expressions to be past-the-end pointers. This is
|
|
|
|
// an extension: the standard requires them to point to an object.
|
|
|
|
if (!IsReferenceType)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// A reference constant expression must refer to an object.
|
|
|
|
if (!Base) {
|
|
|
|
// FIXME: diagnostic
|
2012-02-15 10:18:13 +08:00
|
|
|
Info.CCEDiag(Loc);
|
2012-01-12 14:08:57 +08:00
|
|
|
return true;
|
2012-01-07 00:39:00 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
// Does this refer one past the end of some object?
|
2012-01-07 00:39:00 +08:00
|
|
|
if (Designator.isOnePastTheEnd()) {
|
2011-12-13 14:39:58 +08:00
|
|
|
const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
|
2012-02-15 10:18:13 +08:00
|
|
|
Info.Diag(Loc, diag::note_constexpr_past_end, 1)
|
2011-12-13 14:39:58 +08:00
|
|
|
<< !Designator.Entries.empty() << !!VD << VD;
|
2012-02-15 10:18:13 +08:00
|
|
|
NoteLValueLocation(Info, Base);
|
2011-12-13 14:39:58 +08:00
|
|
|
}
|
|
|
|
|
2011-11-04 13:33:44 +08:00
|
|
|
return true;
|
2011-10-30 04:57:55 +08:00
|
|
|
}
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
/// Check that this core constant expression is of literal type, and if not,
|
|
|
|
/// produce an appropriate diagnostic.
|
|
|
|
static bool CheckLiteralType(EvalInfo &Info, const Expr *E) {
|
|
|
|
if (!E->isRValue() || E->getType()->isLiteralType())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Prvalue constant expressions must be of literal types.
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_nonliteral)
|
2011-12-31 05:15:51 +08:00
|
|
|
<< E->getType();
|
|
|
|
else
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-31 05:15:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-07 13:07:52 +08:00
|
|
|
/// Check that this core constant expression value is a valid value for a
|
2012-02-15 10:18:13 +08:00
|
|
|
/// constant expression. If not, report an appropriate diagnostic. Does not
|
|
|
|
/// check that the expression is of literal type.
|
|
|
|
static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
|
|
|
|
QualType Type, const APValue &Value) {
|
|
|
|
// Core issue 1454: For a literal constant expression of array or class type,
|
|
|
|
// each subobject of its value shall have been initialized by a constant
|
|
|
|
// expression.
|
|
|
|
if (Value.isArray()) {
|
|
|
|
QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
|
|
|
|
for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
|
|
|
|
if (!CheckConstantExpression(Info, DiagLoc, EltTy,
|
|
|
|
Value.getArrayInitializedElt(I)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Value.hasArrayFiller())
|
|
|
|
return true;
|
|
|
|
return CheckConstantExpression(Info, DiagLoc, EltTy,
|
|
|
|
Value.getArrayFiller());
|
|
|
|
}
|
|
|
|
if (Value.isUnion() && Value.getUnionField()) {
|
|
|
|
return CheckConstantExpression(Info, DiagLoc,
|
|
|
|
Value.getUnionField()->getType(),
|
|
|
|
Value.getUnionValue());
|
|
|
|
}
|
|
|
|
if (Value.isStruct()) {
|
|
|
|
RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
|
|
|
|
if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
|
|
|
|
unsigned BaseIndex = 0;
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
|
|
|
|
End = CD->bases_end(); I != End; ++I, ++BaseIndex) {
|
|
|
|
if (!CheckConstantExpression(Info, DiagLoc, I->getType(),
|
|
|
|
Value.getStructBase(BaseIndex)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!CheckConstantExpression(Info, DiagLoc, (*I)->getType(),
|
|
|
|
Value.getStructField((*I)->getFieldIndex())))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Value.isLValue()) {
|
|
|
|
LValue LVal;
|
2012-03-04 06:46:17 +08:00
|
|
|
LVal.setFrom(Info.Ctx, Value);
|
2012-02-15 10:18:13 +08:00
|
|
|
return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal);
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
2012-02-15 10:18:13 +08:00
|
|
|
|
|
|
|
// Everything else is fine.
|
|
|
|
return true;
|
2011-11-07 13:07:52 +08:00
|
|
|
}
|
|
|
|
|
2011-10-31 13:11:32 +08:00
|
|
|
const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
|
2011-11-13 06:28:03 +08:00
|
|
|
return LVal.Base.dyn_cast<const ValueDecl*>();
|
2011-10-31 13:11:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsLiteralLValue(const LValue &Value) {
|
2012-02-15 10:18:13 +08:00
|
|
|
return Value.Base.dyn_cast<const Expr*>() && !Value.CallIndex;
|
2011-10-31 13:11:32 +08:00
|
|
|
}
|
|
|
|
|
2011-11-02 05:06:14 +08:00
|
|
|
static bool IsWeakLValue(const LValue &Value) {
|
|
|
|
const ValueDecl *Decl = GetLValueBaseDecl(Value);
|
2011-12-06 04:16:26 +08:00
|
|
|
return Decl && Decl->isWeak();
|
2011-11-02 05:06:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
|
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.
|
2011-11-18 06:56:20 +08:00
|
|
|
if (!Value.getLValueBase()) {
|
|
|
|
Result = !Value.getLValueOffset().isZero();
|
2010-05-08 05:34:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
// We have a non-null base. These are generally known to be true, but if it's
|
|
|
|
// a weak declaration it can be null at runtime.
|
2010-05-08 05:34:32 +08:00
|
|
|
Result = true;
|
2011-11-18 06:56:20 +08:00
|
|
|
const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
|
2011-12-06 04:16:26 +08:00
|
|
|
return !Decl || !Decl->isWeak();
|
2009-06-14 10:17:33 +08:00
|
|
|
}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool HandleConversionToBool(const APValue &Val, bool &Result) {
|
2011-10-29 01:51:58 +08:00
|
|
|
switch (Val.getKind()) {
|
|
|
|
case APValue::Uninitialized:
|
|
|
|
return false;
|
|
|
|
case APValue::Int:
|
|
|
|
Result = Val.getInt().getBoolValue();
|
2011-10-25 05:07:08 +08:00
|
|
|
return true;
|
2011-10-29 01:51:58 +08:00
|
|
|
case APValue::Float:
|
|
|
|
Result = !Val.getFloat().isZero();
|
2009-03-23 12:38:34 +08:00
|
|
|
return true;
|
2011-10-29 01:51:58 +08:00
|
|
|
case APValue::ComplexInt:
|
|
|
|
Result = Val.getComplexIntReal().getBoolValue() ||
|
|
|
|
Val.getComplexIntImag().getBoolValue();
|
|
|
|
return true;
|
|
|
|
case APValue::ComplexFloat:
|
|
|
|
Result = !Val.getComplexFloatReal().isZero() ||
|
|
|
|
!Val.getComplexFloatImag().isZero();
|
2011-10-25 07:14:33 +08:00
|
|
|
return true;
|
2011-11-18 06:56:20 +08:00
|
|
|
case APValue::LValue:
|
|
|
|
return EvalPointerValueAsBool(Val, Result);
|
|
|
|
case APValue::MemberPointer:
|
|
|
|
Result = Val.getMemberPointerDecl();
|
|
|
|
return true;
|
2011-10-29 01:51:58 +08:00
|
|
|
case APValue::Vector:
|
2011-11-07 17:22:26 +08:00
|
|
|
case APValue::Array:
|
2011-11-10 14:34:14 +08:00
|
|
|
case APValue::Struct:
|
|
|
|
case APValue::Union:
|
2012-01-05 07:13:47 +08:00
|
|
|
case APValue::AddrLabelDiff:
|
2011-10-29 01:51:58 +08:00
|
|
|
return false;
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
llvm_unreachable("unknown APValue kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
|
|
|
|
EvalInfo &Info) {
|
|
|
|
assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition");
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Val;
|
2012-02-28 04:21:34 +08:00
|
|
|
if (!Evaluate(Val, Info, E))
|
2011-10-29 01:51:58 +08:00
|
|
|
return false;
|
2012-02-28 04:21:34 +08:00
|
|
|
return HandleConversionToBool(Val, Result);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
template<typename T>
|
|
|
|
static bool HandleOverflow(EvalInfo &Info, const Expr *E,
|
|
|
|
const T &SrcValue, QualType DestType) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_overflow)
|
2012-01-31 12:08:20 +08:00
|
|
|
<< SrcValue << DestType;
|
2011-12-13 14:39:58 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
|
|
|
|
QualType SrcType, const APFloat &Value,
|
|
|
|
QualType DestType, APSInt &Result) {
|
|
|
|
unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
|
2009-01-29 14:16:07 +08:00
|
|
|
// Determine whether we are converting to unsigned or signed.
|
2011-05-21 00:38:50 +08:00
|
|
|
bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
Result = APSInt(DestWidth, !DestSigned);
|
2009-01-29 14:16:07 +08:00
|
|
|
bool ignored;
|
2011-12-13 14:39:58 +08:00
|
|
|
if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored)
|
|
|
|
& APFloat::opInvalidOp)
|
|
|
|
return HandleOverflow(Info, E, Value, DestType);
|
|
|
|
return true;
|
2009-01-29 14:16:07 +08:00
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
|
|
|
|
QualType SrcType, QualType DestType,
|
|
|
|
APFloat &Result) {
|
|
|
|
APFloat Value = Result;
|
2009-01-29 14:16:07 +08:00
|
|
|
bool ignored;
|
2011-12-13 14:39:58 +08:00
|
|
|
if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType),
|
|
|
|
APFloat::rmNearestTiesToEven, &ignored)
|
|
|
|
& APFloat::opOverflow)
|
|
|
|
return HandleOverflow(Info, E, Value, DestType);
|
|
|
|
return true;
|
2009-01-29 14:16:07 +08:00
|
|
|
}
|
|
|
|
|
2012-01-31 06:27:01 +08:00
|
|
|
static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
|
|
|
|
QualType DestType, QualType SrcType,
|
|
|
|
APSInt &Value) {
|
|
|
|
unsigned DestWidth = Info.Ctx.getIntWidth(DestType);
|
2009-01-29 14:16:07 +08:00
|
|
|
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.
|
2010-12-07 16:25:34 +08:00
|
|
|
Result = Result.extOrTrunc(DestWidth);
|
2011-05-21 00:38:50 +08:00
|
|
|
Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
|
2009-01-29 14:16:07 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
|
|
|
|
QualType SrcType, const APSInt &Value,
|
|
|
|
QualType DestType, APFloat &Result) {
|
|
|
|
Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1);
|
|
|
|
if (Result.convertFromAPInt(Value, Value.isSigned(),
|
|
|
|
APFloat::rmNearestTiesToEven)
|
|
|
|
& APFloat::opOverflow)
|
|
|
|
return HandleOverflow(Info, E, Value, DestType);
|
|
|
|
return true;
|
2009-01-29 14:16:07 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 11:51:45 +08:00
|
|
|
static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
|
|
|
|
llvm::APInt &Res) {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue SVal;
|
2011-12-22 11:51:45 +08:00
|
|
|
if (!Evaluate(SVal, Info, E))
|
|
|
|
return false;
|
|
|
|
if (SVal.isInt()) {
|
|
|
|
Res = SVal.getInt();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (SVal.isFloat()) {
|
|
|
|
Res = SVal.getFloat().bitcastToAPInt();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (SVal.isVector()) {
|
|
|
|
QualType VecTy = E->getType();
|
|
|
|
unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
|
|
|
|
QualType EltTy = VecTy->castAs<VectorType>()->getElementType();
|
|
|
|
unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
|
|
|
|
bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
|
|
|
|
Res = llvm::APInt::getNullValue(VecSize);
|
|
|
|
for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
|
|
|
|
APValue &Elt = SVal.getVectorElt(i);
|
|
|
|
llvm::APInt EltAsInt;
|
|
|
|
if (Elt.isInt()) {
|
|
|
|
EltAsInt = Elt.getInt();
|
|
|
|
} else if (Elt.isFloat()) {
|
|
|
|
EltAsInt = Elt.getFloat().bitcastToAPInt();
|
|
|
|
} else {
|
|
|
|
// Don't try to handle vectors of anything other than int or float
|
|
|
|
// (not sure if it's possible to hit this case).
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-22 11:51:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unsigned BaseEltSize = EltAsInt.getBitWidth();
|
|
|
|
if (BigEndian)
|
|
|
|
Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize);
|
|
|
|
else
|
|
|
|
Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Give up if the input isn't an int, float, or vector. For example, we
|
|
|
|
// reject "(v4i16)(intptr_t)&a".
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-22 11:51:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
/// Cast an lvalue referring to a base subobject to a derived class, by
|
|
|
|
/// truncating the lvalue's path to the given length.
|
|
|
|
static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
|
|
|
|
const RecordDecl *TruncatedType,
|
|
|
|
unsigned TruncatedElements) {
|
|
|
|
SubobjectDesignator &D = Result.Designator;
|
2011-11-10 14:34:14 +08:00
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
// Check we actually point to a derived class object.
|
|
|
|
if (TruncatedElements == D.Entries.size())
|
|
|
|
return true;
|
|
|
|
assert(TruncatedElements >= D.MostDerivedPathLength &&
|
|
|
|
"not casting to a derived class");
|
|
|
|
if (!Result.checkSubobject(Info, E, CSK_Derived))
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
// Truncate the path to the subobject, and remove any derived-to-base offsets.
|
2011-11-18 06:56:20 +08:00
|
|
|
const RecordDecl *RD = TruncatedType;
|
|
|
|
for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
|
2011-11-10 14:34:14 +08:00
|
|
|
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]);
|
2011-11-18 06:56:20 +08:00
|
|
|
if (isVirtualBaseClass(D.Entries[I]))
|
2011-11-10 14:34:14 +08:00
|
|
|
Result.Offset -= Layout.getVBaseClassOffset(Base);
|
2011-11-18 06:56:20 +08:00
|
|
|
else
|
2011-11-10 14:34:14 +08:00
|
|
|
Result.Offset -= Layout.getBaseClassOffset(Base);
|
|
|
|
RD = Base;
|
|
|
|
}
|
2011-11-18 06:56:20 +08:00
|
|
|
D.Entries.resize(TruncatedElements);
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
static void HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
|
2011-11-10 14:34:14 +08:00
|
|
|
const CXXRecordDecl *Derived,
|
|
|
|
const CXXRecordDecl *Base,
|
|
|
|
const ASTRecordLayout *RL = 0) {
|
|
|
|
if (!RL) RL = &Info.Ctx.getASTRecordLayout(Derived);
|
|
|
|
Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
|
2012-01-07 00:39:00 +08:00
|
|
|
Obj.addDecl(Info, E, Base, /*Virtual*/ false);
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
|
2011-11-10 14:34:14 +08:00
|
|
|
const CXXRecordDecl *DerivedDecl,
|
|
|
|
const CXXBaseSpecifier *Base) {
|
|
|
|
const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
|
|
|
|
|
|
|
|
if (!Base->isVirtual()) {
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl);
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
SubobjectDesignator &D = Obj.Designator;
|
|
|
|
if (D.Invalid)
|
|
|
|
return false;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
// Extract most-derived object and corresponding type.
|
2012-01-07 00:39:00 +08:00
|
|
|
DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl();
|
|
|
|
if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength))
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
// Find the virtual base class.
|
2011-11-10 14:34:14 +08:00
|
|
|
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl);
|
|
|
|
Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl);
|
2012-01-07 00:39:00 +08:00
|
|
|
Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true);
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update LVal to refer to the given field, which must be a member of the type
|
|
|
|
/// currently described by LVal.
|
2012-01-07 00:39:00 +08:00
|
|
|
static void HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
|
2011-11-10 14:34:14 +08:00
|
|
|
const FieldDecl *FD,
|
|
|
|
const ASTRecordLayout *RL = 0) {
|
|
|
|
if (!RL)
|
|
|
|
RL = &Info.Ctx.getASTRecordLayout(FD->getParent());
|
|
|
|
|
|
|
|
unsigned I = FD->getFieldIndex();
|
|
|
|
LVal.Offset += Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I));
|
2012-01-07 00:39:00 +08:00
|
|
|
LVal.addDecl(Info, E, FD);
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2012-01-26 06:15:11 +08:00
|
|
|
/// Update LVal to refer to the given indirect field.
|
|
|
|
static void HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
|
|
|
|
LValue &LVal,
|
|
|
|
const IndirectFieldDecl *IFD) {
|
|
|
|
for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
|
|
|
|
CE = IFD->chain_end(); C != CE; ++C)
|
|
|
|
HandleLValueMember(Info, E, LVal, cast<FieldDecl>(*C));
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// Get the size of the given type in char units.
|
2012-02-16 10:46:34 +08:00
|
|
|
static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc,
|
|
|
|
QualType Type, CharUnits &Size) {
|
2011-11-10 14:34:14 +08:00
|
|
|
// sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
|
|
|
|
// extension.
|
|
|
|
if (Type->isVoidType() || Type->isFunctionType()) {
|
|
|
|
Size = CharUnits::One();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Type->isConstantSizeType()) {
|
|
|
|
// sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
|
2012-02-16 10:46:34 +08:00
|
|
|
// FIXME: Better diagnostic.
|
|
|
|
Info.Diag(Loc);
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size = Info.Ctx.getTypeSizeInChars(Type);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update a pointer value to model pointer arithmetic.
|
|
|
|
/// \param Info - Information about the ongoing evaluation.
|
2012-01-07 00:39:00 +08:00
|
|
|
/// \param E - The expression being evaluated, for diagnostic purposes.
|
2011-11-10 14:34:14 +08:00
|
|
|
/// \param LVal - The pointer value to be updated.
|
|
|
|
/// \param EltTy - The pointee type represented by LVal.
|
|
|
|
/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
|
2012-01-07 00:39:00 +08:00
|
|
|
static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
|
|
|
|
LValue &LVal, QualType EltTy,
|
|
|
|
int64_t Adjustment) {
|
2011-11-10 14:34:14 +08:00
|
|
|
CharUnits SizeOfPointee;
|
2012-02-16 10:46:34 +08:00
|
|
|
if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee))
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Compute the new offset in the appropriate width.
|
|
|
|
LVal.Offset += Adjustment * SizeOfPointee;
|
2012-01-07 00:39:00 +08:00
|
|
|
LVal.adjustIndex(Info, E, Adjustment);
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-19 06:04:06 +08:00
|
|
|
/// Update an lvalue to refer to a component of a complex number.
|
|
|
|
/// \param Info - Information about the ongoing evaluation.
|
|
|
|
/// \param LVal - The lvalue to be updated.
|
|
|
|
/// \param EltTy - The complex number's component type.
|
|
|
|
/// \param Imag - False for the real component, true for the imaginary.
|
|
|
|
static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
|
|
|
|
LValue &LVal, QualType EltTy,
|
|
|
|
bool Imag) {
|
|
|
|
if (Imag) {
|
|
|
|
CharUnits SizeOfComponent;
|
|
|
|
if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent))
|
|
|
|
return false;
|
|
|
|
LVal.Offset += SizeOfComponent;
|
|
|
|
}
|
|
|
|
LVal.addComplex(Info, E, EltTy, Imag);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-25 01:54:18 +08:00
|
|
|
/// Try to evaluate the initializer for a variable declaration.
|
2011-12-10 06:58:01 +08:00
|
|
|
static bool EvaluateVarDeclInit(EvalInfo &Info, const Expr *E,
|
|
|
|
const VarDecl *VD,
|
2012-03-04 06:46:17 +08:00
|
|
|
CallStackFrame *Frame, APValue &Result) {
|
2011-10-29 06:34:42 +08:00
|
|
|
// If this is a parameter to an active constexpr function call, perform
|
|
|
|
// argument substitution.
|
|
|
|
if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) {
|
2012-01-27 09:14:48 +08:00
|
|
|
// Assume arguments of a potential constant expression are unknown
|
|
|
|
// constant expressions.
|
|
|
|
if (Info.CheckingPotentialConstantExpression)
|
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!Frame || !Frame->Arguments) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-11-02 00:57:24 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-11-02 00:57:24 +08:00
|
|
|
Result = Frame->Arguments[PVD->getFunctionScopeIndex()];
|
|
|
|
return true;
|
2011-10-29 06:34:42 +08:00
|
|
|
}
|
2011-10-25 01:54:18 +08:00
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
// Dig out the initializer, and use the declaration which it's attached to.
|
|
|
|
const Expr *Init = VD->getAnyInitializer(VD);
|
|
|
|
if (!Init || Init->isValueDependent()) {
|
2012-01-27 09:14:48 +08:00
|
|
|
// If we're checking a potential constant expression, the variable could be
|
|
|
|
// initialized later.
|
|
|
|
if (!Info.CheckingPotentialConstantExpression)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-19 14:19:21 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
// If we're currently evaluating the initializer of this declaration, use that
|
|
|
|
// in-flight value.
|
|
|
|
if (Info.EvaluatingDecl == VD) {
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = *Info.EvaluatingDeclValue;
|
2011-11-10 14:34:14 +08:00
|
|
|
return !Result.isUninit();
|
|
|
|
}
|
|
|
|
|
2011-11-02 05:06:14 +08:00
|
|
|
// Never evaluate the initializer of a weak variable. We can't be sure that
|
|
|
|
// this is the definition which will be used.
|
2011-12-10 06:58:01 +08:00
|
|
|
if (VD->isWeak()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-11-02 05:06:14 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-11-02 05:06:14 +08:00
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
// Check that we can fold the initializer. In C++, we will have already done
|
|
|
|
// this in the cases where it matters for conformance.
|
|
|
|
llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
|
|
|
|
if (!VD->evaluateValue(Notes)) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_var_init_non_constant,
|
2011-12-19 14:19:21 +08:00
|
|
|
Notes.size() + 1) << VD;
|
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
Info.addNotes(Notes);
|
2011-10-30 04:57:55 +08:00
|
|
|
return false;
|
2011-12-19 14:19:21 +08:00
|
|
|
} else if (!VD->checkInitIsICE()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant,
|
2011-12-19 14:19:21 +08:00
|
|
|
Notes.size() + 1) << VD;
|
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
Info.addNotes(Notes);
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-10-25 01:54:18 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = *VD->getEvaluatedValue();
|
2011-10-30 04:57:55 +08:00
|
|
|
return true;
|
2011-10-25 01:54:18 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
static bool IsConstNonVolatile(QualType T) {
|
2011-10-25 01:54:18 +08:00
|
|
|
Qualifiers Quals = T.getQualifiers();
|
|
|
|
return Quals.hasConst() && !Quals.hasVolatile();
|
|
|
|
}
|
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
/// Get the base index of the given base class within an APValue representing
|
|
|
|
/// the given derived class.
|
|
|
|
static unsigned getBaseIndex(const CXXRecordDecl *Derived,
|
|
|
|
const CXXRecordDecl *Base) {
|
|
|
|
Base = Base->getCanonicalDecl();
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),
|
|
|
|
E = Derived->bases_end(); I != E; ++I, ++Index) {
|
|
|
|
if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("base class missing from derived class's bases list");
|
|
|
|
}
|
|
|
|
|
2012-02-17 11:35:37 +08:00
|
|
|
/// Extract the value of a character from a string literal.
|
|
|
|
static APSInt ExtractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
|
|
|
|
uint64_t Index) {
|
|
|
|
// FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
|
|
|
|
const StringLiteral *S = dyn_cast<StringLiteral>(Lit);
|
|
|
|
assert(S && "unexpected string literal expression kind");
|
|
|
|
|
|
|
|
APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(),
|
|
|
|
Lit->getType()->getArrayElementTypeNoTypeQual()->isUnsignedIntegerType());
|
|
|
|
if (Index < S->getLength())
|
|
|
|
Value = S->getCodeUnit(Index);
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
|
2011-11-07 17:22:26 +08:00
|
|
|
/// Extract the designated sub-object of an rvalue.
|
2011-12-10 06:58:01 +08:00
|
|
|
static bool ExtractSubobject(EvalInfo &Info, const Expr *E,
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue &Obj, QualType ObjType,
|
2011-11-07 17:22:26 +08:00
|
|
|
const SubobjectDesignator &Sub, QualType SubType) {
|
2012-01-07 00:39:00 +08:00
|
|
|
if (Sub.Invalid)
|
|
|
|
// A diagnostic will have already been produced.
|
2011-11-07 17:22:26 +08:00
|
|
|
return false;
|
2012-01-07 00:39:00 +08:00
|
|
|
if (Sub.isOnePastTheEnd()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
|
2011-12-22 03:36:37 +08:00
|
|
|
(unsigned)diag::note_constexpr_read_past_end :
|
|
|
|
(unsigned)diag::note_invalid_subexpr_in_const_expr);
|
2011-12-21 13:04:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-11 16:28:03 +08:00
|
|
|
if (Sub.Entries.empty())
|
2011-11-07 17:22:26 +08:00
|
|
|
return true;
|
2012-01-27 09:14:48 +08:00
|
|
|
if (Info.CheckingPotentialConstantExpression && Obj.isUninit())
|
|
|
|
// This object might be initialized later.
|
|
|
|
return false;
|
2011-11-07 17:22:26 +08:00
|
|
|
|
2012-03-10 08:28:11 +08:00
|
|
|
APValue *O = &Obj;
|
2011-11-10 14:34:14 +08:00
|
|
|
// Walk the designator's path to find the subobject.
|
2011-11-07 17:22:26 +08:00
|
|
|
for (unsigned I = 0, N = Sub.Entries.size(); I != N; ++I) {
|
|
|
|
if (ObjType->isArrayType()) {
|
2011-11-10 14:34:14 +08:00
|
|
|
// Next subobject is an array element.
|
2011-11-07 17:22:26 +08:00
|
|
|
const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType);
|
2011-12-10 06:58:01 +08:00
|
|
|
assert(CAT && "vla in literal type?");
|
2011-11-07 17:22:26 +08:00
|
|
|
uint64_t Index = Sub.Entries[I].ArrayIndex;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (CAT->getSize().ule(Index)) {
|
2011-12-21 13:04:46 +08:00
|
|
|
// Note, it should not be possible to form a pointer with a valid
|
|
|
|
// designator which points more than one past the end of the array.
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
|
2011-12-22 03:36:37 +08:00
|
|
|
(unsigned)diag::note_constexpr_read_past_end :
|
|
|
|
(unsigned)diag::note_invalid_subexpr_in_const_expr);
|
2011-11-07 17:22:26 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2012-02-17 11:35:37 +08:00
|
|
|
// An array object is represented as either an Array APValue or as an
|
|
|
|
// LValue which refers to a string literal.
|
|
|
|
if (O->isLValue()) {
|
|
|
|
assert(I == N - 1 && "extracting subobject of character?");
|
|
|
|
assert(!O->hasLValuePath() || O->getLValuePath().empty());
|
2012-03-04 06:46:17 +08:00
|
|
|
Obj = APValue(ExtractStringLiteralCharacter(
|
2012-02-17 11:35:37 +08:00
|
|
|
Info, O->getLValueBase().get<const Expr*>(), Index));
|
|
|
|
return true;
|
|
|
|
} else if (O->getArrayInitializedElts() > Index)
|
2011-11-07 17:22:26 +08:00
|
|
|
O = &O->getArrayInitializedElt(Index);
|
|
|
|
else
|
|
|
|
O = &O->getArrayFiller();
|
|
|
|
ObjType = CAT->getElementType();
|
2012-02-19 06:04:06 +08:00
|
|
|
} else if (ObjType->isAnyComplexType()) {
|
|
|
|
// Next subobject is a complex number.
|
|
|
|
uint64_t Index = Sub.Entries[I].ArrayIndex;
|
|
|
|
if (Index > 1) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, Info.getLangOpts().CPlusPlus0x ?
|
2012-02-19 06:04:06 +08:00
|
|
|
(unsigned)diag::note_constexpr_read_past_end :
|
|
|
|
(unsigned)diag::note_invalid_subexpr_in_const_expr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assert(I == N - 1 && "extracting subobject of scalar?");
|
|
|
|
if (O->isComplexInt()) {
|
2012-03-04 06:46:17 +08:00
|
|
|
Obj = APValue(Index ? O->getComplexIntImag()
|
2012-02-19 06:04:06 +08:00
|
|
|
: O->getComplexIntReal());
|
|
|
|
} else {
|
|
|
|
assert(O->isComplexFloat());
|
2012-03-04 06:46:17 +08:00
|
|
|
Obj = APValue(Index ? O->getComplexFloatImag()
|
2012-02-19 06:04:06 +08:00
|
|
|
: O->getComplexFloatReal());
|
|
|
|
}
|
|
|
|
return true;
|
2011-11-10 14:34:14 +08:00
|
|
|
} else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) {
|
2012-02-09 11:29:58 +08:00
|
|
|
if (Field->isMutable()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_ltor_mutable, 1)
|
2012-02-09 11:29:58 +08:00
|
|
|
<< Field;
|
|
|
|
Info.Note(Field->getLocation(), diag::note_declared_at);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
// Next subobject is a class, struct or union field.
|
|
|
|
RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl();
|
|
|
|
if (RD->isUnion()) {
|
|
|
|
const FieldDecl *UnionField = O->getUnionField();
|
|
|
|
if (!UnionField ||
|
2011-12-10 06:58:01 +08:00
|
|
|
UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_read_inactive_union_member)
|
2011-12-21 13:04:46 +08:00
|
|
|
<< Field << !UnionField << UnionField;
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
O = &O->getUnionValue();
|
|
|
|
} else
|
|
|
|
O = &O->getStructField(Field->getFieldIndex());
|
|
|
|
ObjType = Field->getType();
|
2011-12-21 13:04:46 +08:00
|
|
|
|
|
|
|
if (ObjType.isVolatileQualified()) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus) {
|
|
|
|
// FIXME: Include a description of the path to the volatile subobject.
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_ltor_volatile_obj, 1)
|
2011-12-21 13:04:46 +08:00
|
|
|
<< 2 << Field;
|
|
|
|
Info.Note(Field->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-21 13:04:46 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
} else {
|
2011-11-10 14:34:14 +08:00
|
|
|
// Next subobject is a base class.
|
2011-11-11 12:05:33 +08:00
|
|
|
const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
|
|
|
|
const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]);
|
|
|
|
O = &O->getStructBase(getBaseIndex(Derived, Base));
|
|
|
|
ObjType = Info.Ctx.getRecordType(Base);
|
2011-11-07 17:22:26 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
if (O->isUninit()) {
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!Info.CheckingPotentialConstantExpression)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_read_uninit);
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 08:28:11 +08:00
|
|
|
// This may look super-stupid, but it serves an important purpose: if we just
|
|
|
|
// swapped Obj and *O, we'd create an object which had itself as a subobject.
|
|
|
|
// To avoid the leak, we ensure that Tmp ends up owning the original complete
|
|
|
|
// object, which is destroyed by Tmp's destructor.
|
|
|
|
APValue Tmp;
|
|
|
|
O->swap(Tmp);
|
|
|
|
Obj.swap(Tmp);
|
2011-11-07 17:22:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-02 09:16:57 +08:00
|
|
|
/// Find the position where two subobject designators diverge, or equivalently
|
|
|
|
/// the length of the common initial subsequence.
|
|
|
|
static unsigned FindDesignatorMismatch(QualType ObjType,
|
|
|
|
const SubobjectDesignator &A,
|
|
|
|
const SubobjectDesignator &B,
|
|
|
|
bool &WasArrayIndex) {
|
|
|
|
unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size());
|
|
|
|
for (/**/; I != N; ++I) {
|
2012-02-19 06:04:06 +08:00
|
|
|
if (!ObjType.isNull() &&
|
|
|
|
(ObjType->isArrayType() || ObjType->isAnyComplexType())) {
|
2012-02-02 09:16:57 +08:00
|
|
|
// Next subobject is an array element.
|
|
|
|
if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) {
|
|
|
|
WasArrayIndex = true;
|
|
|
|
return I;
|
|
|
|
}
|
2012-02-19 06:04:06 +08:00
|
|
|
if (ObjType->isAnyComplexType())
|
|
|
|
ObjType = ObjType->castAs<ComplexType>()->getElementType();
|
|
|
|
else
|
|
|
|
ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
|
2012-02-02 09:16:57 +08:00
|
|
|
} else {
|
|
|
|
if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) {
|
|
|
|
WasArrayIndex = false;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
if (const FieldDecl *FD = getAsField(A.Entries[I]))
|
|
|
|
// Next subobject is a field.
|
|
|
|
ObjType = FD->getType();
|
|
|
|
else
|
|
|
|
// Next subobject is a base class.
|
|
|
|
ObjType = QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
WasArrayIndex = false;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine whether the given subobject designators refer to elements of the
|
|
|
|
/// same array object.
|
|
|
|
static bool AreElementsOfSameArray(QualType ObjType,
|
|
|
|
const SubobjectDesignator &A,
|
|
|
|
const SubobjectDesignator &B) {
|
|
|
|
if (A.Entries.size() != B.Entries.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool IsArray = A.MostDerivedArraySize != 0;
|
|
|
|
if (IsArray && A.MostDerivedPathLength != A.Entries.size())
|
|
|
|
// A is a subobject of the array element.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If A (and B) designates an array element, the last entry will be the array
|
|
|
|
// index. That doesn't have to match. Otherwise, we're in the 'implicit array
|
|
|
|
// of length 1' case, and the entire path must match.
|
|
|
|
bool WasArrayIndex;
|
|
|
|
unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
|
|
|
|
return CommonLength >= A.Entries.size() - IsArray;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// HandleLValueToRValueConversion - Perform an lvalue-to-rvalue conversion on
|
|
|
|
/// the given lvalue. This can also be used for 'lvalue-to-lvalue' conversions
|
|
|
|
/// for looking up the glvalue referred to by an entity of reference type.
|
|
|
|
///
|
|
|
|
/// \param Info - Information about the ongoing evaluation.
|
2011-12-10 06:58:01 +08:00
|
|
|
/// \param Conv - The expression for which we are performing the conversion.
|
|
|
|
/// Used for diagnostics.
|
2012-02-05 09:23:16 +08:00
|
|
|
/// \param Type - The type we expect this conversion to produce, before
|
|
|
|
/// stripping cv-qualifiers in the case of a non-clas type.
|
2011-11-10 14:34:14 +08:00
|
|
|
/// \param LVal - The glvalue on which we are attempting to perform this action.
|
|
|
|
/// \param RVal - The produced value will be placed here.
|
2011-12-10 06:58:01 +08:00
|
|
|
static bool HandleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv,
|
|
|
|
QualType Type,
|
2012-03-04 06:46:17 +08:00
|
|
|
const LValue &LVal, APValue &RVal) {
|
2012-01-07 00:39:00 +08:00
|
|
|
if (LVal.Designator.Invalid)
|
|
|
|
// A diagnostic will have already been produced.
|
|
|
|
return false;
|
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
|
2011-10-29 01:51:58 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!LVal.Base) {
|
|
|
|
// FIXME: Indirection through a null pointer deserves a specific diagnostic.
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-21 13:04:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
CallStackFrame *Frame = 0;
|
|
|
|
if (LVal.CallIndex) {
|
|
|
|
Frame = Info.getCallFrame(LVal.CallIndex);
|
|
|
|
if (!Frame) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
|
2012-02-15 10:18:13 +08:00
|
|
|
NoteLValueLocation(Info, LVal.Base);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-21 13:04:46 +08:00
|
|
|
// C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
|
|
|
|
// is not a constant expression (even if the object is non-volatile). We also
|
|
|
|
// apply this rule to C++98, in order to conform to the expected 'volatile'
|
|
|
|
// semantics.
|
|
|
|
if (Type.isVolatileQualified()) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_ltor_volatile_type) << Type;
|
2011-12-21 13:04:46 +08:00
|
|
|
else
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-10-29 01:51:58 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-10-29 01:51:58 +08:00
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) {
|
2011-10-29 01:51:58 +08:00
|
|
|
// In C++98, const, non-volatile integers initialized with ICEs are ICEs.
|
|
|
|
// In C++11, constexpr, non-volatile variables initialized with constant
|
2011-10-29 06:34:42 +08:00
|
|
|
// expressions are constant expressions too. Inside constexpr functions,
|
|
|
|
// parameters are constant expressions even if they're non-const.
|
2011-10-29 01:51:58 +08:00
|
|
|
// In C, such things can also be folded, although they are not ICEs.
|
|
|
|
const VarDecl *VD = dyn_cast<VarDecl>(D);
|
2012-03-09 09:51:51 +08:00
|
|
|
if (const VarDecl *VDef = VD->getDefinition(Info.Ctx))
|
2012-02-02 09:16:57 +08:00
|
|
|
VD = VDef;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!VD || VD->isInvalidDecl()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-11-04 10:25:55 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
2011-12-21 13:04:46 +08:00
|
|
|
// DR1313: If the object is volatile-qualified but the glvalue was not,
|
|
|
|
// behavior is undefined so the result is not a constant expression.
|
2011-11-13 06:28:03 +08:00
|
|
|
QualType VT = VD->getType();
|
2011-12-21 13:04:46 +08:00
|
|
|
if (VT.isVolatileQualified()) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 1 << VD;
|
2011-12-21 13:04:46 +08:00
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-12-21 13:04:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isa<ParmVarDecl>(VD)) {
|
|
|
|
if (VD->isConstexpr()) {
|
|
|
|
// OK, we can read this variable.
|
|
|
|
} else if (VT->isIntegralOrEnumerationType()) {
|
|
|
|
if (!VT.isConstQualified()) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_ltor_non_const_int, 1) << VD;
|
2011-12-21 13:04:46 +08:00
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-12-21 13:04:46 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (VT->isFloatingType() && VT.isConstQualified()) {
|
|
|
|
// We support folding of const floating-point types, in order to make
|
|
|
|
// static const data members of such types (supported as an extension)
|
|
|
|
// more useful.
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
|
2011-12-21 13:04:46 +08:00
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(Conv);
|
2011-12-21 13:04:46 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FIXME: Allow folding of values of any literal type in all languages.
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_ltor_non_constexpr, 1) << VD;
|
2011-12-21 13:04:46 +08:00
|
|
|
Info.Note(VD->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-12-21 13:04:46 +08:00
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
}
|
2011-12-21 13:04:46 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateVarDeclInit(Info, Conv, VD, Frame, RVal))
|
2011-10-29 01:51:58 +08:00
|
|
|
return false;
|
|
|
|
|
2011-10-30 04:57:55 +08:00
|
|
|
if (isa<ParmVarDecl>(VD) || !VD->getAnyInitializer()->isLValue())
|
2011-12-10 06:58:01 +08:00
|
|
|
return ExtractSubobject(Info, Conv, RVal, VT, LVal.Designator, Type);
|
2011-10-29 01:51:58 +08:00
|
|
|
|
|
|
|
// The declaration was initialized by an lvalue, with no lvalue-to-rvalue
|
|
|
|
// conversion. This happens when the declaration and the lvalue should be
|
|
|
|
// considered synonymous, for instance when initializing an array of char
|
|
|
|
// from a string literal. Continue as if the initializer lvalue was the
|
|
|
|
// value we were originally given.
|
2011-11-04 10:25:55 +08:00
|
|
|
assert(RVal.getLValueOffset().isZero() &&
|
|
|
|
"offset for lvalue init of non-reference");
|
2011-11-13 06:28:03 +08:00
|
|
|
Base = RVal.getLValueBase().get<const Expr*>();
|
2012-02-15 10:18:13 +08:00
|
|
|
|
|
|
|
if (unsigned CallIndex = RVal.getLValueCallIndex()) {
|
|
|
|
Frame = Info.getCallFrame(CallIndex);
|
|
|
|
if (!Frame) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_lifetime_ended, 1) << !Base;
|
2012-02-15 10:18:13 +08:00
|
|
|
NoteLValueLocation(Info, RVal.getLValueBase());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Frame = 0;
|
|
|
|
}
|
2011-10-29 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
2011-12-21 13:04:46 +08:00
|
|
|
// Volatile temporary objects cannot be read in constant expressions.
|
|
|
|
if (Base->getType().isVolatileQualified()) {
|
|
|
|
if (Info.getLangOpts().CPlusPlus) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_constexpr_ltor_volatile_obj, 1) << 0;
|
2011-12-21 13:04:46 +08:00
|
|
|
Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here);
|
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv);
|
2011-12-21 13:04:46 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-02 00:57:24 +08:00
|
|
|
if (Frame) {
|
2011-11-07 17:22:26 +08:00
|
|
|
// If this is a temporary expression with a nontrivial initializer, grab the
|
|
|
|
// value from the relevant stack frame.
|
2011-11-02 00:57:24 +08:00
|
|
|
RVal = Frame->Temporaries[Base];
|
2011-11-07 17:22:26 +08:00
|
|
|
} else if (const CompoundLiteralExpr *CLE
|
|
|
|
= dyn_cast<CompoundLiteralExpr>(Base)) {
|
|
|
|
// In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the
|
|
|
|
// initializer until now for such expressions. Such an expression can't be
|
|
|
|
// an ICE in C, so this only matters for fold.
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
|
2011-11-07 17:22:26 +08:00
|
|
|
if (!Evaluate(RVal, Info, CLE->getInitializer()))
|
|
|
|
return false;
|
2012-02-17 11:35:37 +08:00
|
|
|
} else if (isa<StringLiteral>(Base)) {
|
|
|
|
// We represent a string literal array as an lvalue pointing at the
|
|
|
|
// corresponding expression, rather than building an array of chars.
|
|
|
|
// FIXME: Support PredefinedExpr, ObjCEncodeExpr, MakeStringConstant
|
2012-03-04 06:46:17 +08:00
|
|
|
RVal = APValue(Base, CharUnits::Zero(), APValue::NoLValuePath(), 0);
|
2011-12-10 06:58:01 +08:00
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(Conv, diag::note_invalid_subexpr_in_const_expr);
|
2011-11-07 17:22:26 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-10-29 01:51:58 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return ExtractSubobject(Info, Conv, RVal, Base->getType(), LVal.Designator,
|
|
|
|
Type);
|
2011-10-29 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
/// Build an lvalue for the object argument of a member function call.
|
|
|
|
static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
|
|
|
|
LValue &This) {
|
|
|
|
if (Object->getType()->isPointerType())
|
|
|
|
return EvaluatePointer(Object, This, Info);
|
|
|
|
|
|
|
|
if (Object->isGLValue())
|
|
|
|
return EvaluateLValue(Object, This, Info);
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
if (Object->getType()->isLiteralType())
|
|
|
|
return EvaluateTemporary(Object, This, Info);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleMemberPointerAccess - Evaluate a member access operation and build an
|
|
|
|
/// lvalue referring to the result.
|
|
|
|
///
|
|
|
|
/// \param Info - Information about the ongoing evaluation.
|
|
|
|
/// \param BO - The member pointer access operation.
|
|
|
|
/// \param LV - Filled in with a reference to the resulting object.
|
|
|
|
/// \param IncludeMember - Specifies whether the member itself is included in
|
|
|
|
/// the resulting LValue subobject designator. This is not possible when
|
|
|
|
/// creating a bound member function.
|
|
|
|
/// \return The field or method declaration to which the member pointer refers,
|
|
|
|
/// or 0 if evaluation fails.
|
|
|
|
static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
|
|
|
|
const BinaryOperator *BO,
|
|
|
|
LValue &LV,
|
|
|
|
bool IncludeMember = true) {
|
|
|
|
assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool EvalObjOK = EvaluateObjectArgument(Info, BO->getLHS(), LV);
|
|
|
|
if (!EvalObjOK && !Info.keepEvaluatingAfterFailure())
|
2011-11-18 06:56:20 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
MemberPtr MemPtr;
|
|
|
|
if (!EvaluateMemberPointer(BO->getRHS(), MemPtr, Info))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
|
|
|
|
// member value, the behavior is undefined.
|
|
|
|
if (!MemPtr.getDecl())
|
|
|
|
return 0;
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvalObjOK)
|
|
|
|
return 0;
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
if (MemPtr.isDerivedMember()) {
|
|
|
|
// This is a member of some derived class. Truncate LV appropriately.
|
|
|
|
// The end of the derived-to-base path for the base object must match the
|
|
|
|
// derived-to-base path for the member pointer.
|
2012-01-07 00:39:00 +08:00
|
|
|
if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
|
2011-11-18 06:56:20 +08:00
|
|
|
LV.Designator.Entries.size())
|
|
|
|
return 0;
|
|
|
|
unsigned PathLengthToMember =
|
|
|
|
LV.Designator.Entries.size() - MemPtr.Path.size();
|
|
|
|
for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
|
|
|
|
const CXXRecordDecl *LVDecl = getAsBaseClass(
|
|
|
|
LV.Designator.Entries[PathLengthToMember + I]);
|
|
|
|
const CXXRecordDecl *MPDecl = MemPtr.Path[I];
|
|
|
|
if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the lvalue to the appropriate derived class.
|
2012-01-07 00:39:00 +08:00
|
|
|
if (!CastToDerivedClass(Info, BO, LV, MemPtr.getContainingRecord(),
|
|
|
|
PathLengthToMember))
|
|
|
|
return 0;
|
2011-11-18 06:56:20 +08:00
|
|
|
} else if (!MemPtr.Path.empty()) {
|
|
|
|
// Extend the LValue path with the member pointer's path.
|
|
|
|
LV.Designator.Entries.reserve(LV.Designator.Entries.size() +
|
|
|
|
MemPtr.Path.size() + IncludeMember);
|
|
|
|
|
|
|
|
// Walk down to the appropriate base class.
|
|
|
|
QualType LVType = BO->getLHS()->getType();
|
|
|
|
if (const PointerType *PT = LVType->getAs<PointerType>())
|
|
|
|
LVType = PT->getPointeeType();
|
|
|
|
const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
|
|
|
|
assert(RD && "member pointer access on non-class-type expression");
|
|
|
|
// The first class in the path is that of the lvalue.
|
|
|
|
for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
|
|
|
|
const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueDirectBase(Info, BO, LV, RD, Base);
|
2011-11-18 06:56:20 +08:00
|
|
|
RD = Base;
|
|
|
|
}
|
|
|
|
// Finally cast to the class containing the member.
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueDirectBase(Info, BO, LV, RD, MemPtr.getContainingRecord());
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the member. Note that we cannot build bound member functions here.
|
|
|
|
if (IncludeMember) {
|
2012-01-26 06:15:11 +08:00
|
|
|
if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl()))
|
|
|
|
HandleLValueMember(Info, BO, LV, FD);
|
|
|
|
else if (const IndirectFieldDecl *IFD =
|
|
|
|
dyn_cast<IndirectFieldDecl>(MemPtr.getDecl()))
|
|
|
|
HandleLValueIndirectMember(Info, BO, LV, IFD);
|
|
|
|
else
|
|
|
|
llvm_unreachable("can't construct reference to bound member function");
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return MemPtr.getDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
|
|
|
|
/// the provided lvalue, which currently refers to the base object.
|
|
|
|
static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
|
|
|
|
LValue &Result) {
|
|
|
|
SubobjectDesignator &D = Result.Designator;
|
2012-01-07 00:39:00 +08:00
|
|
|
if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived))
|
2011-11-18 06:56:20 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType TargetQT = E->getType();
|
|
|
|
if (const PointerType *PT = TargetQT->getAs<PointerType>())
|
|
|
|
TargetQT = PT->getPointeeType();
|
2012-01-07 00:39:00 +08:00
|
|
|
|
|
|
|
// Check this cast lands within the final derived-to-base subobject path.
|
|
|
|
if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< D.MostDerivedType << TargetQT;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the type of the final cast. We don't need to check the path,
|
|
|
|
// since a cast can only be formed if the path is unique.
|
|
|
|
unsigned NewEntriesSize = D.Entries.size() - E->path_size();
|
2011-11-18 06:56:20 +08:00
|
|
|
const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
|
|
|
|
const CXXRecordDecl *FinalType;
|
2012-01-07 00:39:00 +08:00
|
|
|
if (NewEntriesSize == D.MostDerivedPathLength)
|
|
|
|
FinalType = D.MostDerivedType->getAsCXXRecordDecl();
|
|
|
|
else
|
2011-11-18 06:56:20 +08:00
|
|
|
FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]);
|
2012-01-07 00:39:00 +08:00
|
|
|
if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_invalid_downcast)
|
2012-01-07 00:39:00 +08:00
|
|
|
<< D.MostDerivedType << TargetQT;
|
2011-11-18 06:56:20 +08:00
|
|
|
return false;
|
2012-01-07 00:39:00 +08:00
|
|
|
}
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
// Truncate the lvalue to the appropriate derived class.
|
2012-01-07 00:39:00 +08:00
|
|
|
return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize);
|
2011-11-11 12:05:33 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 06:34:42 +08:00
|
|
|
namespace {
|
|
|
|
enum EvalStmtResult {
|
|
|
|
/// Evaluation failed.
|
|
|
|
ESR_Failed,
|
|
|
|
/// Hit a 'return' statement.
|
|
|
|
ESR_Returned,
|
|
|
|
/// Evaluation succeeded.
|
|
|
|
ESR_Succeeded
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate a statement.
|
2012-03-04 06:46:17 +08:00
|
|
|
static EvalStmtResult EvaluateStmt(APValue &Result, EvalInfo &Info,
|
2011-10-29 06:34:42 +08:00
|
|
|
const Stmt *S) {
|
|
|
|
switch (S->getStmtClass()) {
|
|
|
|
default:
|
|
|
|
return ESR_Failed;
|
|
|
|
|
|
|
|
case Stmt::NullStmtClass:
|
|
|
|
case Stmt::DeclStmtClass:
|
|
|
|
return ESR_Succeeded;
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
case Stmt::ReturnStmtClass: {
|
|
|
|
const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue();
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!Evaluate(Result, Info, RetExpr))
|
2011-12-13 14:39:58 +08:00
|
|
|
return ESR_Failed;
|
|
|
|
return ESR_Returned;
|
|
|
|
}
|
2011-10-29 06:34:42 +08:00
|
|
|
|
|
|
|
case Stmt::CompoundStmtClass: {
|
|
|
|
const CompoundStmt *CS = cast<CompoundStmt>(S);
|
|
|
|
for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
|
|
|
|
BE = CS->body_end(); BI != BE; ++BI) {
|
|
|
|
EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI);
|
|
|
|
if (ESR != ESR_Succeeded)
|
|
|
|
return ESR;
|
|
|
|
}
|
|
|
|
return ESR_Succeeded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 10:22:31 +08:00
|
|
|
/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
|
|
|
|
/// default constructor. If so, we'll fold it whether or not it's marked as
|
|
|
|
/// constexpr. If it is marked as constexpr, we will never implicitly define it,
|
|
|
|
/// so we need special handling.
|
|
|
|
static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
|
2011-12-31 05:15:51 +08:00
|
|
|
const CXXConstructorDecl *CD,
|
|
|
|
bool IsValueInitialization) {
|
2011-12-22 10:22:31 +08:00
|
|
|
if (!CD->isTrivial() || !CD->isDefaultConstructor())
|
|
|
|
return false;
|
|
|
|
|
2012-01-18 13:21:49 +08:00
|
|
|
// Value-initialization does not call a trivial default constructor, so such a
|
|
|
|
// call is a core constant expression whether or not the constructor is
|
|
|
|
// constexpr.
|
|
|
|
if (!CD->isConstexpr() && !IsValueInitialization) {
|
2011-12-22 10:22:31 +08:00
|
|
|
if (Info.getLangOpts().CPlusPlus0x) {
|
2012-01-18 13:21:49 +08:00
|
|
|
// FIXME: If DiagDecl is an implicitly-declared special member function,
|
|
|
|
// we should be much more explicit about why it's not constexpr.
|
|
|
|
Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1)
|
|
|
|
<< /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
|
|
|
|
Info.Note(CD->getLocation(), diag::note_declared_at);
|
2011-12-22 10:22:31 +08:00
|
|
|
} else {
|
|
|
|
Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
/// CheckConstexprFunction - Check that a function can be called in a constant
|
|
|
|
/// expression.
|
|
|
|
static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
|
|
|
|
const FunctionDecl *Declaration,
|
|
|
|
const FunctionDecl *Definition) {
|
2012-01-27 09:14:48 +08:00
|
|
|
// Potential constant expressions can contain calls to declared, but not yet
|
|
|
|
// defined, constexpr functions.
|
|
|
|
if (Info.CheckingPotentialConstantExpression && !Definition &&
|
|
|
|
Declaration->isConstexpr())
|
|
|
|
return false;
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
// Can we evaluate this function call?
|
|
|
|
if (Definition && Definition->isConstexpr() && !Definition->isInvalidDecl())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x) {
|
|
|
|
const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
|
2011-12-19 14:19:21 +08:00
|
|
|
// FIXME: If DiagDecl is an implicitly-declared special member function, we
|
|
|
|
// should be much more explicit about why it's not constexpr.
|
2011-12-13 14:39:58 +08:00
|
|
|
Info.Diag(CallLoc, diag::note_constexpr_invalid_function, 1)
|
|
|
|
<< DiagDecl->isConstexpr() << isa<CXXConstructorDecl>(DiagDecl)
|
|
|
|
<< DiagDecl;
|
|
|
|
Info.Note(DiagDecl->getLocation(), diag::note_declared_at);
|
|
|
|
} else {
|
|
|
|
Info.Diag(CallLoc, diag::note_invalid_subexpr_in_const_expr);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
namespace {
|
2012-03-04 06:46:17 +08:00
|
|
|
typedef SmallVector<APValue, 8> ArgVector;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateArgs - Evaluate the arguments to a function call.
|
|
|
|
static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues,
|
|
|
|
EvalInfo &Info) {
|
2012-01-27 09:14:48 +08:00
|
|
|
bool Success = true;
|
2011-11-10 14:34:14 +08:00
|
|
|
for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
|
2012-01-27 09:14:48 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) {
|
|
|
|
// If we're checking for a potential constant expression, evaluate all
|
|
|
|
// initializers even if some of them fail.
|
|
|
|
if (!Info.keepEvaluatingAfterFailure())
|
|
|
|
return false;
|
|
|
|
Success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Success;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 06:34:42 +08:00
|
|
|
/// Evaluate a function call.
|
2012-01-27 09:14:48 +08:00
|
|
|
static bool HandleFunctionCall(SourceLocation CallLoc,
|
|
|
|
const FunctionDecl *Callee, const LValue *This,
|
2011-12-10 06:58:01 +08:00
|
|
|
ArrayRef<const Expr*> Args, const Stmt *Body,
|
2012-03-04 06:46:17 +08:00
|
|
|
EvalInfo &Info, APValue &Result) {
|
2011-11-10 14:34:14 +08:00
|
|
|
ArgVector ArgValues(Args.size());
|
|
|
|
if (!EvaluateArgs(Args, ArgValues, Info))
|
|
|
|
return false;
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!Info.CheckCallLimit(CallLoc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
|
2011-10-29 06:34:42 +08:00
|
|
|
return EvaluateStmt(Result, Info, Body) == ESR_Returned;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// Evaluate a constructor call.
|
2012-01-27 09:14:48 +08:00
|
|
|
static bool HandleConstructorCall(SourceLocation CallLoc, const LValue &This,
|
2011-11-11 12:05:33 +08:00
|
|
|
ArrayRef<const Expr*> Args,
|
2011-11-10 14:34:14 +08:00
|
|
|
const CXXConstructorDecl *Definition,
|
2011-12-31 05:15:51 +08:00
|
|
|
EvalInfo &Info, APValue &Result) {
|
2011-11-10 14:34:14 +08:00
|
|
|
ArgVector ArgValues(Args.size());
|
|
|
|
if (!EvaluateArgs(Args, ArgValues, Info))
|
|
|
|
return false;
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!Info.CheckCallLimit(CallLoc))
|
|
|
|
return false;
|
|
|
|
|
2012-02-13 11:54:03 +08:00
|
|
|
const CXXRecordDecl *RD = Definition->getParent();
|
|
|
|
if (RD->getNumVBases()) {
|
|
|
|
Info.Diag(CallLoc, diag::note_constexpr_virtual_base) << RD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues.data());
|
2011-11-10 14:34:14 +08:00
|
|
|
|
|
|
|
// If it's a delegating constructor, just delegate.
|
|
|
|
if (Definition->isDelegatingConstructor()) {
|
|
|
|
CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result, Info, This, (*I)->getInit());
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2012-01-10 12:32:03 +08:00
|
|
|
// For a trivial copy or move constructor, perform an APValue copy. This is
|
|
|
|
// essential for unions, where the operations performed by the constructor
|
|
|
|
// cannot be represented by ctor-initializers.
|
|
|
|
if (Definition->isDefaulted() &&
|
2012-02-24 15:55:51 +08:00
|
|
|
((Definition->isCopyConstructor() && Definition->isTrivial()) ||
|
|
|
|
(Definition->isMoveConstructor() && Definition->isTrivial()))) {
|
2012-01-10 12:32:03 +08:00
|
|
|
LValue RHS;
|
2012-03-04 06:46:17 +08:00
|
|
|
RHS.setFrom(Info.Ctx, ArgValues[0]);
|
|
|
|
return HandleLValueToRValueConversion(Info, Args[0], Args[0]->getType(),
|
|
|
|
RHS, Result);
|
2012-01-10 12:32:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reserve space for the struct members.
|
2011-12-31 05:15:51 +08:00
|
|
|
if (!RD->isUnion() && Result.isUninit())
|
2011-11-10 14:34:14 +08:00
|
|
|
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
|
|
|
|
std::distance(RD->field_begin(), RD->field_end()));
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool Success = true;
|
2011-11-10 14:34:14 +08:00
|
|
|
unsigned BasesSeen = 0;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
|
|
|
|
#endif
|
|
|
|
for (CXXConstructorDecl::init_const_iterator I = Definition->init_begin(),
|
|
|
|
E = Definition->init_end(); I != E; ++I) {
|
2012-01-27 09:14:48 +08:00
|
|
|
LValue Subobject = This;
|
|
|
|
APValue *Value = &Result;
|
|
|
|
|
|
|
|
// Determine the subobject to initialize.
|
2011-11-10 14:34:14 +08:00
|
|
|
if ((*I)->isBaseInitializer()) {
|
|
|
|
QualType BaseType((*I)->getBaseClass(), 0);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Non-virtual base classes are initialized in the order in the class
|
2012-02-13 11:54:03 +08:00
|
|
|
// definition. We have already checked for virtual base classes.
|
2011-11-10 14:34:14 +08:00
|
|
|
assert(!BaseIt->isVirtual() && "virtual base for literal type");
|
|
|
|
assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) &&
|
|
|
|
"base class initializers not in expected order");
|
|
|
|
++BaseIt;
|
|
|
|
#endif
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueDirectBase(Info, (*I)->getInit(), Subobject, RD,
|
2011-11-10 14:34:14 +08:00
|
|
|
BaseType->getAsCXXRecordDecl(), &Layout);
|
2012-01-27 09:14:48 +08:00
|
|
|
Value = &Result.getStructBase(BasesSeen++);
|
2011-11-10 14:34:14 +08:00
|
|
|
} else if (FieldDecl *FD = (*I)->getMember()) {
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueMember(Info, (*I)->getInit(), Subobject, FD, &Layout);
|
2011-11-10 14:34:14 +08:00
|
|
|
if (RD->isUnion()) {
|
|
|
|
Result = APValue(FD);
|
2012-01-27 09:14:48 +08:00
|
|
|
Value = &Result.getUnionValue();
|
|
|
|
} else {
|
|
|
|
Value = &Result.getStructField(FD->getFieldIndex());
|
|
|
|
}
|
2012-01-26 06:15:11 +08:00
|
|
|
} else if (IndirectFieldDecl *IFD = (*I)->getIndirectMember()) {
|
|
|
|
// Walk the indirect field decl's chain to find the object to initialize,
|
|
|
|
// and make sure we've initialized every step along it.
|
|
|
|
for (IndirectFieldDecl::chain_iterator C = IFD->chain_begin(),
|
|
|
|
CE = IFD->chain_end();
|
|
|
|
C != CE; ++C) {
|
|
|
|
FieldDecl *FD = cast<FieldDecl>(*C);
|
|
|
|
CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent());
|
|
|
|
// Switch the union field if it differs. This happens if we had
|
|
|
|
// preceding zero-initialization, and we're now initializing a union
|
|
|
|
// subobject other than the first.
|
|
|
|
// FIXME: In this case, the values of the other subobjects are
|
|
|
|
// specified, since zero-initialization sets all padding bits to zero.
|
|
|
|
if (Value->isUninit() ||
|
|
|
|
(Value->isUnion() && Value->getUnionField() != FD)) {
|
|
|
|
if (CD->isUnion())
|
|
|
|
*Value = APValue(FD);
|
|
|
|
else
|
|
|
|
*Value = APValue(APValue::UninitStruct(), CD->getNumBases(),
|
|
|
|
std::distance(CD->field_begin(), CD->field_end()));
|
|
|
|
}
|
2012-01-27 09:14:48 +08:00
|
|
|
HandleLValueMember(Info, (*I)->getInit(), Subobject, FD);
|
2012-01-26 06:15:11 +08:00
|
|
|
if (CD->isUnion())
|
|
|
|
Value = &Value->getUnionValue();
|
|
|
|
else
|
|
|
|
Value = &Value->getStructField(FD->getFieldIndex());
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
} else {
|
2012-01-26 06:15:11 +08:00
|
|
|
llvm_unreachable("unknown base initializer kind");
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
2012-01-27 09:14:48 +08:00
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(*Value, Info, Subobject, (*I)->getInit(),
|
|
|
|
(*I)->isBaseInitializer()
|
2012-01-27 09:14:48 +08:00
|
|
|
? CCEK_Constant : CCEK_MemberInit)) {
|
|
|
|
// If we're checking for a potential constant expression, evaluate all
|
|
|
|
// initializers even if some of them fail.
|
|
|
|
if (!Info.keepEvaluatingAfterFailure())
|
|
|
|
return false;
|
|
|
|
Success = false;
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
return Success;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2009-10-28 06:09:17 +08:00
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class HasSideEffect
|
2011-05-13 11:29:01 +08:00
|
|
|
: public ConstStmtVisitor<HasSideEffect, bool> {
|
2011-10-17 05:26:27 +08:00
|
|
|
const ASTContext &Ctx;
|
2009-10-28 06:09:17 +08:00
|
|
|
public:
|
|
|
|
|
2011-10-17 05:26:27 +08:00
|
|
|
HasSideEffect(const ASTContext &C) : Ctx(C) {}
|
2009-10-28 06:09:17 +08:00
|
|
|
|
|
|
|
// Unhandled nodes conservatively default to having side effects.
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitStmt(const Stmt *S) {
|
2009-10-28 06:09:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitParenExpr(const ParenExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) {
|
2011-04-15 08:35:48 +08:00
|
|
|
return Visit(E->getResultExpr());
|
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *E) {
|
2011-10-17 05:26:27 +08:00
|
|
|
if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
|
2009-10-28 06:09:17 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
bool VisitObjCIvarRefExpr(const ObjCIvarRefExpr *E) {
|
2011-10-17 05:26:27 +08:00
|
|
|
if (Ctx.getCanonicalType(E->getType()).isVolatileQualified())
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-28 06:09:17 +08:00
|
|
|
// We don't want to evaluate BlockExprs multiple times, as they generate
|
|
|
|
// a ton of code.
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitBlockExpr(const BlockExpr *E) { return true; }
|
|
|
|
bool VisitPredefinedExpr(const PredefinedExpr *E) { return false; }
|
|
|
|
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
|
2009-10-28 06:09:17 +08:00
|
|
|
{ return Visit(E->getInitializer()); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitMemberExpr(const MemberExpr *E) { return Visit(E->getBase()); }
|
|
|
|
bool VisitIntegerLiteral(const IntegerLiteral *E) { return false; }
|
|
|
|
bool VisitFloatingLiteral(const FloatingLiteral *E) { return false; }
|
|
|
|
bool VisitStringLiteral(const StringLiteral *E) { return false; }
|
|
|
|
bool VisitCharacterLiteral(const CharacterLiteral *E) { return false; }
|
|
|
|
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
|
2011-03-12 03:24:49 +08:00
|
|
|
{ return false; }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
|
2009-10-30 04:48:09 +08:00
|
|
|
{ return Visit(E->getLHS()) || Visit(E->getRHS()); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitChooseExpr(const ChooseExpr *E)
|
2011-10-17 05:26:27 +08:00
|
|
|
{ return Visit(E->getChosenSubExpr(Ctx)); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCastExpr(const CastExpr *E) { return Visit(E->getSubExpr()); }
|
|
|
|
bool VisitBinAssign(const BinaryOperator *E) { return true; }
|
|
|
|
bool VisitCompoundAssignOperator(const BinaryOperator *E) { return true; }
|
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E)
|
2009-10-30 04:48:09 +08:00
|
|
|
{ return Visit(E->getLHS()) || Visit(E->getRHS()); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitUnaryPreInc(const UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPostInc(const UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPreDec(const UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryPostDec(const UnaryOperator *E) { return true; }
|
|
|
|
bool VisitUnaryDeref(const UnaryOperator *E) {
|
2011-10-17 05:26:27 +08:00
|
|
|
if (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
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitUnaryOperator(const UnaryOperator *E) { return Visit(E->getSubExpr()); }
|
2010-04-14 01:34:23 +08:00
|
|
|
|
|
|
|
// Has side effects if any element does.
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitInitListExpr(const InitListExpr *E) {
|
2010-04-14 01:34:23 +08:00
|
|
|
for (unsigned i = 0, e = E->getNumInits(); i != e; ++i)
|
|
|
|
if (Visit(E->getInit(i))) return true;
|
2011-05-13 11:29:01 +08:00
|
|
|
if (const Expr *filler = E->getArrayFiller())
|
2011-04-21 08:27:41 +08:00
|
|
|
return Visit(filler);
|
2010-04-14 01:34:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-05 01:33:58 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitSizeOfPackExpr(const SizeOfPackExpr *) { return false; }
|
2009-10-28 06:09:17 +08:00
|
|
|
};
|
|
|
|
|
2011-02-17 18:25:35 +08:00
|
|
|
class OpaqueValueEvaluation {
|
|
|
|
EvalInfo &info;
|
|
|
|
OpaqueValueExpr *opaqueValue;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OpaqueValueEvaluation(EvalInfo &info, OpaqueValueExpr *opaqueValue,
|
|
|
|
Expr *value)
|
|
|
|
: info(info), opaqueValue(opaqueValue) {
|
|
|
|
|
|
|
|
// If evaluation fails, fail immediately.
|
2011-10-17 05:26:27 +08:00
|
|
|
if (!Evaluate(info.OpaqueValues[opaqueValue], info, value)) {
|
2011-02-17 18:25:35 +08:00
|
|
|
this->opaqueValue = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasError() const { return opaqueValue == 0; }
|
|
|
|
|
|
|
|
~OpaqueValueEvaluation() {
|
2012-02-16 10:46:34 +08:00
|
|
|
// FIXME: For a recursive constexpr call, an outer stack frame might have
|
|
|
|
// been using this opaque value too, and will now have to re-evaluate the
|
|
|
|
// source expression.
|
2011-02-17 18:25:35 +08:00
|
|
|
if (opaqueValue) info.OpaqueValues.erase(opaqueValue);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-10-28 06:09:17 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Generic Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
// FIXME: RetTy is always bool. Remove it.
|
|
|
|
template <class Derived, typename RetTy=bool>
|
2011-05-13 11:29:01 +08:00
|
|
|
class ExprEvaluatorBase
|
|
|
|
: public ConstStmtVisitor<Derived, RetTy> {
|
|
|
|
private:
|
2012-03-04 06:46:17 +08:00
|
|
|
RetTy DerivedSuccess(const APValue &V, const Expr *E) {
|
2011-05-13 11:29:01 +08:00
|
|
|
return static_cast<Derived*>(this)->Success(V, E);
|
|
|
|
}
|
2011-12-31 05:15:51 +08:00
|
|
|
RetTy DerivedZeroInitialization(const Expr *E) {
|
|
|
|
return static_cast<Derived*>(this)->ZeroInitialization(E);
|
2011-10-12 05:43:33 +08:00
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
|
2012-02-16 10:46:34 +08:00
|
|
|
// Check whether a conditional operator with a non-constant condition is a
|
|
|
|
// potential constant expression. If neither arm is a potential constant
|
|
|
|
// expression, then the conditional operator is not either.
|
|
|
|
template<typename ConditionalOperator>
|
|
|
|
void CheckPotentialConstantConditional(const ConditionalOperator *E) {
|
|
|
|
assert(Info.CheckingPotentialConstantExpression);
|
|
|
|
|
|
|
|
// Speculatively evaluate both arms.
|
|
|
|
{
|
|
|
|
llvm::SmallVector<PartialDiagnosticAt, 8> Diag;
|
|
|
|
SpeculativeEvaluationRAII Speculate(Info, &Diag);
|
|
|
|
|
|
|
|
StmtVisitorTy::Visit(E->getFalseExpr());
|
|
|
|
if (Diag.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Diag.clear();
|
|
|
|
StmtVisitorTy::Visit(E->getTrueExpr());
|
|
|
|
if (Diag.empty())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error(E, diag::note_constexpr_conditional_never_const);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename ConditionalOperator>
|
|
|
|
bool HandleConditionalOperator(const ConditionalOperator *E) {
|
|
|
|
bool BoolResult;
|
|
|
|
if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
|
|
|
|
if (Info.CheckingPotentialConstantExpression)
|
|
|
|
CheckPotentialConstantConditional(E);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
|
|
|
|
return StmtVisitorTy::Visit(EvalExpr);
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
protected:
|
|
|
|
EvalInfo &Info;
|
|
|
|
typedef ConstStmtVisitor<Derived, RetTy> StmtVisitorTy;
|
|
|
|
typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
|
|
|
|
|
2011-12-12 17:28:41 +08:00
|
|
|
OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
|
2012-03-15 12:53:45 +08:00
|
|
|
return Info.CCEDiag(E, D);
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Report an evaluation error. This should only be called when an error is
|
|
|
|
/// first discovered. When propagating an error, just return false.
|
|
|
|
bool Error(const Expr *E, diag::kind D) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, D);
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool Error(const Expr *E) {
|
|
|
|
return Error(E, diag::note_invalid_subexpr_in_const_expr);
|
|
|
|
}
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
RetTy ZeroInitialization(const Expr *E) { return Error(E); }
|
2011-10-12 05:43:33 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
public:
|
|
|
|
ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
|
|
|
|
|
|
|
|
RetTy VisitStmt(const Stmt *) {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Expression evaluator should not be called on stmts");
|
2011-05-13 11:29:01 +08:00
|
|
|
}
|
|
|
|
RetTy VisitExpr(const Expr *E) {
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-05-13 11:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RetTy VisitParenExpr(const ParenExpr *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
|
|
|
|
RetTy VisitUnaryExtension(const UnaryOperator *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
|
|
|
|
RetTy VisitUnaryPlus(const UnaryOperator *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
|
|
|
|
RetTy VisitChooseExpr(const ChooseExpr *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getChosenSubExpr(Info.Ctx)); }
|
|
|
|
RetTy VisitGenericSelectionExpr(const GenericSelectionExpr *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getResultExpr()); }
|
2011-07-15 13:09:51 +08:00
|
|
|
RetTy VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getReplacement()); }
|
2011-11-09 10:12:41 +08:00
|
|
|
RetTy VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getExpr()); }
|
2011-12-20 06:12:41 +08:00
|
|
|
// We cannot create any objects for which cleanups are required, so there is
|
|
|
|
// nothing to do here; all cleanups must come from unevaluated subexpressions.
|
|
|
|
RetTy VisitExprWithCleanups(const ExprWithCleanups *E)
|
|
|
|
{ return StmtVisitorTy::Visit(E->getSubExpr()); }
|
2011-05-13 11:29:01 +08:00
|
|
|
|
2011-12-12 20:46:16 +08:00
|
|
|
RetTy VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
|
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast) << 0;
|
|
|
|
return static_cast<Derived*>(this)->VisitCastExpr(E);
|
|
|
|
}
|
|
|
|
RetTy VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
|
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast) << 1;
|
|
|
|
return static_cast<Derived*>(this)->VisitCastExpr(E);
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
RetTy VisitBinaryOperator(const BinaryOperator *E) {
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
case BO_Comma:
|
|
|
|
VisitIgnoredValue(E->getLHS());
|
|
|
|
return StmtVisitorTy::Visit(E->getRHS());
|
|
|
|
|
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI: {
|
|
|
|
LValue Obj;
|
|
|
|
if (!HandleMemberPointerAccess(Info, E, Obj))
|
|
|
|
return false;
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Result;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!HandleLValueToRValueConversion(Info, E, E->getType(), Obj, Result))
|
2011-11-18 06:56:20 +08:00
|
|
|
return false;
|
|
|
|
return DerivedSuccess(Result, E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
RetTy VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
|
2012-02-16 10:46:34 +08:00
|
|
|
// Cache the value of the common expression.
|
2011-05-13 11:29:01 +08:00
|
|
|
OpaqueValueEvaluation opaque(Info, E->getOpaqueValue(), E->getCommon());
|
|
|
|
if (opaque.hasError())
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2011-05-13 11:29:01 +08:00
|
|
|
|
2012-02-16 10:46:34 +08:00
|
|
|
return HandleConditionalOperator(E);
|
2011-05-13 11:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RetTy VisitConditionalOperator(const ConditionalOperator *E) {
|
2012-02-02 09:16:57 +08:00
|
|
|
bool IsBcpCall = false;
|
|
|
|
// If the condition (ignoring parens) is a __builtin_constant_p call,
|
|
|
|
// the result is a constant expression if it can be folded without
|
|
|
|
// side-effects. This is an important GNU extension. See GCC PR38377
|
|
|
|
// for discussion.
|
|
|
|
if (const CallExpr *CallCE =
|
|
|
|
dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts()))
|
|
|
|
if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
|
|
|
|
IsBcpCall = true;
|
|
|
|
|
|
|
|
// Always assume __builtin_constant_p(...) ? ... : ... is a potential
|
|
|
|
// constant expression; we can't check whether it's potentially foldable.
|
|
|
|
if (Info.CheckingPotentialConstantExpression && IsBcpCall)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FoldConstant Fold(Info);
|
|
|
|
|
2012-02-16 10:46:34 +08:00
|
|
|
if (!HandleConditionalOperator(E))
|
2012-02-02 09:16:57 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (IsBcpCall)
|
|
|
|
Fold.Fold(Info);
|
|
|
|
|
|
|
|
return true;
|
2011-05-13 11:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RetTy VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
|
2012-03-04 06:46:17 +08:00
|
|
|
const APValue *Value = Info.getOpaqueValue(E);
|
2011-12-09 10:44:48 +08:00
|
|
|
if (!Value) {
|
|
|
|
const Expr *Source = E->getSourceExpr();
|
|
|
|
if (!Source)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-12-09 10:44:48 +08:00
|
|
|
if (Source == E) { // sanity checking.
|
|
|
|
assert(0 && "OpaqueValueExpr recursively refers to itself");
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-12-09 10:44:48 +08:00
|
|
|
}
|
|
|
|
return StmtVisitorTy::Visit(Source);
|
|
|
|
}
|
2011-10-30 04:57:55 +08:00
|
|
|
return DerivedSuccess(*Value, E);
|
2011-05-13 11:29:01 +08:00
|
|
|
}
|
2011-10-12 05:43:33 +08:00
|
|
|
|
2011-10-29 06:34:42 +08:00
|
|
|
RetTy VisitCallExpr(const CallExpr *E) {
|
2011-11-18 06:56:20 +08:00
|
|
|
const Expr *Callee = E->getCallee()->IgnoreParens();
|
2011-10-29 06:34:42 +08:00
|
|
|
QualType CalleeType = Callee->getType();
|
|
|
|
|
2011-11-11 01:47:39 +08:00
|
|
|
const FunctionDecl *FD = 0;
|
2011-11-11 12:05:33 +08:00
|
|
|
LValue *This = 0, ThisVal;
|
|
|
|
llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
|
2012-02-13 11:54:03 +08:00
|
|
|
bool HasQualifier = false;
|
2011-11-11 12:05:33 +08:00
|
|
|
|
|
|
|
// Extract function decl and 'this' pointer from the callee.
|
|
|
|
if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
|
2011-12-10 06:58:01 +08:00
|
|
|
const ValueDecl *Member = 0;
|
2011-11-18 06:56:20 +08:00
|
|
|
if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) {
|
|
|
|
// Explicit bound member calls, such as x.f() or p->g();
|
|
|
|
if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
|
|
|
Member = ME->getMemberDecl();
|
2011-11-18 06:56:20 +08:00
|
|
|
This = &ThisVal;
|
2012-02-13 11:54:03 +08:00
|
|
|
HasQualifier = ME->hasQualifier();
|
2011-11-18 06:56:20 +08:00
|
|
|
} else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) {
|
|
|
|
// Indirect bound member calls ('.*' or '->*').
|
2011-12-10 06:58:01 +08:00
|
|
|
Member = HandleMemberPointerAccess(Info, BE, ThisVal, false);
|
|
|
|
if (!Member) return false;
|
2011-11-18 06:56:20 +08:00
|
|
|
This = &ThisVal;
|
|
|
|
} else
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(Callee);
|
|
|
|
|
|
|
|
FD = dyn_cast<FunctionDecl>(Member);
|
|
|
|
if (!FD)
|
|
|
|
return Error(Callee);
|
2011-11-11 12:05:33 +08:00
|
|
|
} else if (CalleeType->isFunctionPointerType()) {
|
2012-01-07 00:39:00 +08:00
|
|
|
LValue Call;
|
|
|
|
if (!EvaluatePointer(Callee, Call, Info))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2011-11-11 12:05:33 +08:00
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
if (!Call.getLValueOffset().isZero())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(Callee);
|
2011-11-13 06:28:03 +08:00
|
|
|
FD = dyn_cast_or_null<FunctionDecl>(
|
|
|
|
Call.getLValueBase().dyn_cast<const ValueDecl*>());
|
2011-11-11 12:05:33 +08:00
|
|
|
if (!FD)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(Callee);
|
2011-11-11 12:05:33 +08:00
|
|
|
|
|
|
|
// Overloaded operator calls to member functions are represented as normal
|
|
|
|
// calls with '*this' as the first argument.
|
|
|
|
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
|
|
|
|
if (MD && !MD->isStatic()) {
|
2011-12-10 06:58:01 +08:00
|
|
|
// FIXME: When selecting an implicit conversion for an overloaded
|
|
|
|
// operator delete, we sometimes try to evaluate calls to conversion
|
|
|
|
// operators without a 'this' parameter!
|
|
|
|
if (Args.empty())
|
|
|
|
return Error(E);
|
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
if (!EvaluateObjectArgument(Info, Args[0], ThisVal))
|
|
|
|
return false;
|
|
|
|
This = &ThisVal;
|
|
|
|
Args = Args.slice(1);
|
|
|
|
}
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
// Don't call function pointers which have been cast to some other type.
|
|
|
|
if (!Info.Ctx.hasSameType(CalleeType->getPointeeType(), FD->getType()))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-11 12:05:33 +08:00
|
|
|
} else
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2012-02-01 10:39:43 +08:00
|
|
|
if (This && !This->checkSubobject(Info, E, CSK_This))
|
|
|
|
return false;
|
|
|
|
|
2012-02-13 11:54:03 +08:00
|
|
|
// DR1358 allows virtual constexpr functions in some cases. Don't allow
|
|
|
|
// calls to such functions in constant expressions.
|
|
|
|
if (This && !HasQualifier &&
|
|
|
|
isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual())
|
|
|
|
return Error(E, diag::note_constexpr_virtual_call);
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
const FunctionDecl *Definition = 0;
|
2011-10-29 06:34:42 +08:00
|
|
|
Stmt *Body = FD->getBody(Definition);
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Result;
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition) ||
|
2012-01-27 09:14:48 +08:00
|
|
|
!HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body,
|
|
|
|
Info, Result))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2011-10-29 06:34:42 +08:00
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
return DerivedSuccess(Result, E);
|
2011-10-29 06:34:42 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
RetTy VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
|
|
|
return StmtVisitorTy::Visit(E->getInitializer());
|
|
|
|
}
|
2011-10-12 05:43:33 +08:00
|
|
|
RetTy VisitInitListExpr(const InitListExpr *E) {
|
2012-01-04 07:54:05 +08:00
|
|
|
if (E->getNumInits() == 0)
|
|
|
|
return DerivedZeroInitialization(E);
|
|
|
|
if (E->getNumInits() == 1)
|
|
|
|
return StmtVisitorTy::Visit(E->getInit(0));
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-12 05:43:33 +08:00
|
|
|
}
|
|
|
|
RetTy VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
|
2011-12-31 05:15:51 +08:00
|
|
|
return DerivedZeroInitialization(E);
|
2011-10-12 05:43:33 +08:00
|
|
|
}
|
|
|
|
RetTy VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
|
2011-12-31 05:15:51 +08:00
|
|
|
return DerivedZeroInitialization(E);
|
2011-10-12 05:43:33 +08:00
|
|
|
}
|
2011-11-18 06:56:20 +08:00
|
|
|
RetTy VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
|
2011-12-31 05:15:51 +08:00
|
|
|
return DerivedZeroInitialization(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
2011-10-12 05:43:33 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
/// A member expression where the object is a prvalue is itself a prvalue.
|
|
|
|
RetTy VisitMemberExpr(const MemberExpr *E) {
|
|
|
|
assert(!E->isArrow() && "missing call to bound member function?");
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Val;
|
2011-11-10 14:34:14 +08:00
|
|
|
if (!Evaluate(Val, Info, E->getBase()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType BaseTy = E->getBase()->getType();
|
|
|
|
|
|
|
|
const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!FD) return Error(E);
|
2011-11-10 14:34:14 +08:00
|
|
|
assert(!FD->getType()->isReferenceType() && "prvalue reference?");
|
|
|
|
assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
|
|
|
|
FD->getParent()->getCanonicalDecl() && "record / field mismatch");
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
SubobjectDesignator Designator(BaseTy);
|
|
|
|
Designator.addDeclUnchecked(FD);
|
2011-11-10 14:34:14 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return ExtractSubobject(Info, E, Val, BaseTy, Designator, E->getType()) &&
|
2011-11-10 14:34:14 +08:00
|
|
|
DerivedSuccess(Val, E);
|
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
RetTy VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2011-10-29 01:51:58 +08:00
|
|
|
case CK_NoOp:
|
2012-01-18 05:17:26 +08:00
|
|
|
case CK_UserDefinedConversion:
|
2011-10-29 01:51:58 +08:00
|
|
|
return StmtVisitorTy::Visit(E->getSubExpr());
|
|
|
|
|
|
|
|
case CK_LValueToRValue: {
|
|
|
|
LValue LVal;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateLValue(E->getSubExpr(), LVal, Info))
|
|
|
|
return false;
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue RVal;
|
2012-02-05 09:23:16 +08:00
|
|
|
// Note, we use the subexpression's type in order to retain cv-qualifiers.
|
|
|
|
if (!HandleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(),
|
|
|
|
LVal, RVal))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
|
|
|
return DerivedSuccess(RVal, E);
|
2011-10-29 01:51:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-29 01:51:58 +08:00
|
|
|
}
|
|
|
|
|
2011-10-25 02:44:57 +08:00
|
|
|
/// Visit a value which is evaluated, but whose value is ignored.
|
|
|
|
void VisitIgnoredValue(const Expr *E) {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Scratch;
|
2011-10-25 02:44:57 +08:00
|
|
|
if (!Evaluate(Scratch, Info, E))
|
|
|
|
Info.EvalStatus.HasSideEffects = true;
|
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Common base class for lvalue and temporary evaluation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
template<class Derived>
|
|
|
|
class LValueExprEvaluatorBase
|
|
|
|
: public ExprEvaluatorBase<Derived, bool> {
|
|
|
|
protected:
|
|
|
|
LValue &Result;
|
|
|
|
typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
|
|
|
|
typedef ExprEvaluatorBase<Derived, bool> ExprEvaluatorBaseTy;
|
|
|
|
|
|
|
|
bool Success(APValue::LValueBase B) {
|
|
|
|
Result.set(B);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result) :
|
|
|
|
ExprEvaluatorBaseTy(Info), Result(Result) {}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
|
|
|
Result.setFrom(this->Info.Ctx, V);
|
2011-11-18 06:56:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitMemberExpr(const MemberExpr *E) {
|
|
|
|
// Handle non-static data members.
|
|
|
|
QualType BaseTy;
|
|
|
|
if (E->isArrow()) {
|
|
|
|
if (!EvaluatePointer(E->getBase(), Result, this->Info))
|
|
|
|
return false;
|
|
|
|
BaseTy = E->getBase()->getType()->getAs<PointerType>()->getPointeeType();
|
2011-12-13 14:39:58 +08:00
|
|
|
} else if (E->getBase()->isRValue()) {
|
2011-12-20 06:01:37 +08:00
|
|
|
assert(E->getBase()->getType()->isRecordType());
|
2011-12-13 14:39:58 +08:00
|
|
|
if (!EvaluateTemporary(E->getBase(), Result, this->Info))
|
|
|
|
return false;
|
|
|
|
BaseTy = E->getBase()->getType();
|
2011-11-18 06:56:20 +08:00
|
|
|
} else {
|
|
|
|
if (!this->Visit(E->getBase()))
|
|
|
|
return false;
|
|
|
|
BaseTy = E->getBase()->getType();
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:15:11 +08:00
|
|
|
const ValueDecl *MD = E->getMemberDecl();
|
|
|
|
if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) {
|
|
|
|
assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() ==
|
|
|
|
FD->getParent()->getCanonicalDecl() && "record / field mismatch");
|
|
|
|
(void)BaseTy;
|
|
|
|
HandleLValueMember(this->Info, E, Result, FD);
|
|
|
|
} else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) {
|
|
|
|
HandleLValueIndirectMember(this->Info, E, Result, IFD);
|
|
|
|
} else
|
|
|
|
return this->Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
|
2012-01-26 06:15:11 +08:00
|
|
|
if (MD->getType()->isReferenceType()) {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue RefValue;
|
2012-01-26 06:15:11 +08:00
|
|
|
if (!HandleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
|
2011-11-18 06:56:20 +08:00
|
|
|
RefValue))
|
|
|
|
return false;
|
|
|
|
return Success(RefValue, E);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E) {
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
|
|
|
|
|
|
|
|
case BO_PtrMemD:
|
|
|
|
case BO_PtrMemI:
|
|
|
|
return HandleMemberPointerAccess(this->Info, E, Result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
|
|
|
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase: {
|
|
|
|
if (!this->Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Now figure out the necessary offset to add to the base LV to get from
|
|
|
|
// the derived class to the base class.
|
|
|
|
QualType Type = E->getSubExpr()->getType();
|
|
|
|
|
|
|
|
for (CastExpr::path_const_iterator PathI = E->path_begin(),
|
|
|
|
PathE = E->path_end(); PathI != PathE; ++PathI) {
|
2012-01-07 00:39:00 +08:00
|
|
|
if (!HandleLValueBase(this->Info, E, Result, Type->getAsCXXRecordDecl(),
|
2011-11-18 06:56:20 +08:00
|
|
|
*PathI))
|
|
|
|
return false;
|
|
|
|
Type = (*PathI)->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LValue Evaluation
|
2011-10-29 01:51:58 +08:00
|
|
|
//
|
|
|
|
// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
|
|
|
|
// function designators (in C), decl references to void objects (in C), and
|
|
|
|
// temporaries (if building with -Wno-address-of-temporary).
|
|
|
|
//
|
|
|
|
// LValue evaluation produces values comprising a base expression of one of the
|
|
|
|
// following types:
|
2011-11-13 06:28:03 +08:00
|
|
|
// - Declarations
|
|
|
|
// * VarDecl
|
|
|
|
// * FunctionDecl
|
|
|
|
// - Literals
|
2011-10-29 01:51:58 +08:00
|
|
|
// * CompoundLiteralExpr in C
|
|
|
|
// * StringLiteral
|
2011-12-27 20:18:28 +08:00
|
|
|
// * CXXTypeidExpr
|
2011-10-29 01:51:58 +08:00
|
|
|
// * PredefinedExpr
|
2011-11-10 14:34:14 +08:00
|
|
|
// * ObjCStringLiteralExpr
|
2011-10-29 01:51:58 +08:00
|
|
|
// * ObjCEncodeExpr
|
|
|
|
// * AddrLabelExpr
|
|
|
|
// * BlockExpr
|
|
|
|
// * CallExpr for a MakeStringConstant builtin
|
2011-11-13 06:28:03 +08:00
|
|
|
// - Locals and temporaries
|
2012-02-15 10:18:13 +08:00
|
|
|
// * Any Expr, with a CallIndex indicating the function in which the temporary
|
|
|
|
// was evaluated.
|
2011-11-13 06:28:03 +08:00
|
|
|
// plus an offset in bytes.
|
2008-11-12 17:44:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class LValueExprEvaluator
|
2011-11-18 06:56:20 +08:00
|
|
|
: public LValueExprEvaluatorBase<LValueExprEvaluator> {
|
2008-11-12 17:44:48 +08:00
|
|
|
public:
|
2011-11-18 06:56:20 +08:00
|
|
|
LValueExprEvaluator(EvalInfo &Info, LValue &Result) :
|
|
|
|
LValueExprEvaluatorBaseTy(Info, Result) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
bool VisitVarDecl(const Expr *E, const VarDecl *VD);
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *E);
|
|
|
|
bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); }
|
2011-10-31 13:52:43 +08:00
|
|
|
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
|
|
|
|
bool VisitMemberExpr(const MemberExpr *E);
|
|
|
|
bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
|
|
|
|
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
|
2011-12-27 20:18:28 +08:00
|
|
|
bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
|
|
|
|
bool VisitUnaryDeref(const UnaryOperator *E);
|
2012-02-19 06:04:06 +08:00
|
|
|
bool VisitUnaryReal(const UnaryOperator *E);
|
|
|
|
bool VisitUnaryImag(const UnaryOperator *E);
|
2011-05-13 11:29:01 +08:00
|
|
|
|
|
|
|
bool VisitCastExpr(const CastExpr *E) {
|
2009-10-04 00:30:22 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
2011-11-18 06:56:20 +08:00
|
|
|
return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
|
2009-10-04 00:30:22 +08:00
|
|
|
|
2011-10-11 08:13:24 +08:00
|
|
|
case CK_LValueBitCast:
|
2011-12-12 20:46:16 +08:00
|
|
|
this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
|
2011-11-04 10:25:55 +08:00
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
Result.Designator.setInvalid();
|
|
|
|
return true;
|
2011-10-11 08:13:24 +08:00
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
case CK_BaseToDerived:
|
2011-11-10 14:34:14 +08:00
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
2011-11-18 06:56:20 +08:00
|
|
|
return HandleBaseToDerivedCast(Info, E, Result);
|
2009-10-04 00:30:22 +08:00
|
|
|
}
|
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
/// Evaluate an expression as an lvalue. This can be legitimately called on
|
|
|
|
/// expressions which are not glvalues, in a few cases:
|
|
|
|
/// * function designators in C,
|
|
|
|
/// * "extern void" objects,
|
|
|
|
/// * temporaries, if building with -Wno-address-of-temporary.
|
2010-05-08 05:00:08 +08:00
|
|
|
static bool EvaluateLValue(const Expr* E, LValue& Result, EvalInfo &Info) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert((E->isGLValue() || E->getType()->isFunctionType() ||
|
|
|
|
E->getType()->isVoidType() || isa<CXXTemporaryObjectExpr>(E)) &&
|
|
|
|
"can't evaluate expression as an lvalue");
|
2011-05-13 11:29:01 +08:00
|
|
|
return LValueExprEvaluator(Info, Result).Visit(E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
|
2011-11-13 06:28:03 +08:00
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl()))
|
|
|
|
return Success(FD);
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
|
2011-10-29 01:51:58 +08:00
|
|
|
return VisitVarDecl(E, VD);
|
|
|
|
return Error(E);
|
|
|
|
}
|
2011-10-25 07:14:33 +08:00
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
|
2011-11-02 00:57:24 +08:00
|
|
|
if (!VD->getType()->isReferenceType()) {
|
|
|
|
if (isa<ParmVarDecl>(VD)) {
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.set(VD, Info.CurrentCall->Index);
|
2011-11-02 00:57:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2011-11-13 06:28:03 +08:00
|
|
|
return Success(VD);
|
2011-11-02 00:57:24 +08:00
|
|
|
}
|
2009-05-27 14:04:58 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue V;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateVarDeclInit(Info, E, VD, Info.CurrentCall, V))
|
|
|
|
return false;
|
|
|
|
return Success(V, E);
|
2008-11-24 12:41:22 +08:00
|
|
|
}
|
|
|
|
|
2011-10-31 13:52:43 +08:00
|
|
|
bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
|
|
|
|
const MaterializeTemporaryExpr *E) {
|
2011-11-18 06:56:20 +08:00
|
|
|
if (E->GetTemporaryExpr()->isRValue()) {
|
2011-12-20 06:01:37 +08:00
|
|
|
if (E->getType()->isRecordType())
|
2011-11-18 06:56:20 +08:00
|
|
|
return EvaluateTemporary(E->GetTemporaryExpr(), Result, Info);
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.set(E, Info.CurrentCall->Index);
|
|
|
|
return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info,
|
|
|
|
Result, E->GetTemporaryExpr());
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Materialization of an lvalue temporary occurs when we need to force a copy
|
|
|
|
// (for instance, if it's a bitfield).
|
|
|
|
// FIXME: The AST should contain an lvalue-to-rvalue node for such cases.
|
|
|
|
if (!Visit(E->GetTemporaryExpr()))
|
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!HandleLValueToRValueConversion(Info, E, E->getType(), Result,
|
2011-11-18 06:56:20 +08:00
|
|
|
Info.CurrentCall->Temporaries[E]))
|
|
|
|
return false;
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.set(E, Info.CurrentCall->Index);
|
2011-11-18 06:56:20 +08:00
|
|
|
return true;
|
2011-10-31 13:52:43 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool
|
|
|
|
LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(!Info.getLangOpts().CPlusPlus && "lvalue compound literal in c++?");
|
|
|
|
// Defer visiting the literal until the lvalue-to-rvalue conversion. We can
|
|
|
|
// only see this when folding in C, so there's no standard to follow here.
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 20:18:28 +08:00
|
|
|
bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
|
|
|
|
if (E->isTypeOperand())
|
|
|
|
return Success(E);
|
|
|
|
CXXRecordDecl *RD = E->getExprOperand()->getType()->getAsCXXRecordDecl();
|
|
|
|
if (RD && RD->isPolymorphic()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_typeid_polymorphic)
|
2011-12-27 20:18:28 +08:00
|
|
|
<< E->getExprOperand()->getType()
|
|
|
|
<< E->getExprOperand()->getSourceRange();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Success(E);
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
|
2011-10-29 01:51:58 +08:00
|
|
|
// Handle static data members.
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) {
|
|
|
|
VisitIgnoredValue(E->getBase());
|
|
|
|
return VisitVarDecl(E, VD);
|
|
|
|
}
|
|
|
|
|
2011-10-29 06:34:42 +08:00
|
|
|
// Handle static member functions.
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
|
|
|
|
if (MD->isStatic()) {
|
|
|
|
VisitIgnoredValue(E->getBase());
|
2011-11-13 06:28:03 +08:00
|
|
|
return Success(MD);
|
2011-10-29 06:34:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
// Handle non-static data members.
|
2011-11-18 06:56:20 +08:00
|
|
|
return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
|
2011-10-29 01:51:58 +08:00
|
|
|
// FIXME: Deal with vectors as array subscript bases.
|
|
|
|
if (E->getBase()->getType()->isVectorType())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-29 01:51:58 +08:00
|
|
|
|
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;
|
2011-11-10 14:34:14 +08:00
|
|
|
int64_t IndexValue
|
|
|
|
= Index.isSigned() ? Index.getSExtValue()
|
|
|
|
: static_cast<int64_t>(Index.getZExtValue());
|
2008-11-17 03:01:22 +08:00
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
return HandleLValueArrayAdjustment(Info, E, Result, E->getType(), IndexValue);
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
|
2010-05-08 05:00:08 +08:00
|
|
|
return EvaluatePointer(E->getSubExpr(), Result, Info);
|
2009-02-20 09:57:15 +08:00
|
|
|
}
|
|
|
|
|
2012-02-19 06:04:06 +08:00
|
|
|
bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
// __real is a no-op on scalar lvalues.
|
|
|
|
if (E->getSubExpr()->getType()->isAnyComplexType())
|
|
|
|
HandleLValueComplexElement(Info, E, Result, E->getType(), false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
|
|
|
|
assert(E->getSubExpr()->getType()->isAnyComplexType() &&
|
|
|
|
"lvalue __imag__ on scalar?");
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
HandleLValueComplexElement(Info, E, Result, E->getType(), true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
2011-05-13 11:29:01 +08:00
|
|
|
: public ExprEvaluatorBase<PointerExprEvaluator, bool> {
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue &Result;
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool Success(const Expr *E) {
|
2011-11-13 06:28:03 +08:00
|
|
|
Result.set(E);
|
2010-05-08 05:00:08 +08:00
|
|
|
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)
|
2011-05-13 11:29:01 +08:00
|
|
|
: ExprEvaluatorBaseTy(info), Result(Result) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
|
|
|
Result.setFrom(Info.Ctx, V);
|
2011-05-13 11:29:01 +08:00
|
|
|
return true;
|
2008-07-08 22:30:00 +08:00
|
|
|
}
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E) {
|
2011-10-12 05:43:33 +08:00
|
|
|
return Success((Expr*)0);
|
|
|
|
}
|
2008-07-08 22:30:00 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCastExpr(const CastExpr* E);
|
2010-05-08 05:00:08 +08:00
|
|
|
bool VisitUnaryAddrOf(const UnaryOperator *E);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
|
2010-05-08 05:00:08 +08:00
|
|
|
{ return Success(E); }
|
2012-03-07 04:05:56 +08:00
|
|
|
bool VisitObjCNumericLiteral(const ObjCNumericLiteral *E)
|
|
|
|
{ return Success(E); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
|
2010-05-08 05:00:08 +08:00
|
|
|
{ return Success(E); }
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCallExpr(const CallExpr *E);
|
|
|
|
bool VisitBlockExpr(const BlockExpr *E) {
|
2011-02-02 21:00:07 +08:00
|
|
|
if (!E->getBlockDecl()->hasCaptures())
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2009-02-20 06:01:56 +08:00
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
bool VisitCXXThisExpr(const CXXThisExpr *E) {
|
|
|
|
if (!Info.CurrentCall->This)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-10 14:34:14 +08:00
|
|
|
Result = *Info.CurrentCall->This;
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-17 18:25:35 +08:00
|
|
|
|
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) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(E->isRValue() && E->getType()->hasPointerRepresentation());
|
2011-05-13 11:29:01 +08:00
|
|
|
return PointerExprEvaluator(Info, Result).Visit(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)
|
2011-11-18 06:56:20 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
|
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
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool EvalPtrOK = EvaluatePointer(PExp, Result, Info);
|
|
|
|
if (!EvalPtrOK && !Info.keepEvaluatingAfterFailure())
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
llvm::APSInt Offset;
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK)
|
2010-05-08 05:00:08 +08:00
|
|
|
return false;
|
|
|
|
int64_t AdditionalOffset
|
|
|
|
= Offset.isSigned() ? Offset.getSExtValue()
|
|
|
|
: static_cast<int64_t>(Offset.getZExtValue());
|
2011-11-04 10:25:55 +08:00
|
|
|
if (E->getOpcode() == BO_Sub)
|
|
|
|
AdditionalOffset = -AdditionalOffset;
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
QualType Pointee = PExp->getType()->getAs<PointerType>()->getPointeeType();
|
2012-01-07 00:39:00 +08:00
|
|
|
return HandleLValueArrayAdjustment(Info, E, Result, Pointee,
|
|
|
|
AdditionalOffset);
|
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
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
|
|
|
|
const 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_BitCast:
|
2011-09-09 13:25:32 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
2012-01-15 11:25:41 +08:00
|
|
|
if (!Visit(SubExpr))
|
|
|
|
return false;
|
2011-12-12 20:46:16 +08:00
|
|
|
// Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
|
|
|
|
// permitted in constant expressions in C++11. Bitcasts from cv void* are
|
|
|
|
// also static_casts, but we disallow them as a resolution to DR1312.
|
2011-12-13 03:10:03 +08:00
|
|
|
if (!E->getType()->isVoidPointerType()) {
|
2012-01-15 11:25:41 +08:00
|
|
|
Result.Designator.setInvalid();
|
2011-12-13 03:10:03 +08:00
|
|
|
if (SubExpr->getType()->isVoidPointerType())
|
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast)
|
|
|
|
<< 3 << SubExpr->getType();
|
|
|
|
else
|
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
|
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
return true;
|
2009-12-27 13:43:15 +08:00
|
|
|
|
2010-11-01 04:41:46 +08:00
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase: {
|
2011-10-30 04:57:55 +08:00
|
|
|
if (!EvaluatePointer(E->getSubExpr(), Result, Info))
|
2010-11-01 04:41:46 +08:00
|
|
|
return false;
|
2011-11-18 06:56:20 +08:00
|
|
|
if (!Result.Base && Result.Offset.isZero())
|
|
|
|
return true;
|
2010-11-01 04:41:46 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
// Now figure out the necessary offset to add to the base LV to get from
|
2010-11-01 04:41:46 +08:00
|
|
|
// the derived class to the base class.
|
2011-11-10 14:34:14 +08:00
|
|
|
QualType Type =
|
|
|
|
E->getSubExpr()->getType()->castAs<PointerType>()->getPointeeType();
|
2010-11-01 04:41:46 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
for (CastExpr::path_const_iterator PathI = E->path_begin(),
|
2010-11-01 04:41:46 +08:00
|
|
|
PathE = E->path_end(); PathI != PathE; ++PathI) {
|
2012-01-07 00:39:00 +08:00
|
|
|
if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(),
|
|
|
|
*PathI))
|
2010-11-01 04:41:46 +08:00
|
|
|
return false;
|
2011-11-10 14:34:14 +08:00
|
|
|
Type = (*PathI)->getType();
|
2010-11-01 04:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
case CK_BaseToDerived:
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
if (!Result.Base && Result.Offset.isZero())
|
|
|
|
return true;
|
|
|
|
return HandleBaseToDerivedCast(Info, E, Result);
|
|
|
|
|
2011-10-30 04:57:55 +08:00
|
|
|
case CK_NullToPointer:
|
2011-12-31 05:15:51 +08:00
|
|
|
return ZeroInitialization(E);
|
2010-11-13 09:35:44 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_IntegralToPointer: {
|
2011-12-12 20:46:16 +08:00
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Value;
|
2010-05-08 05:00:08 +08:00
|
|
|
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()) {
|
2011-10-30 04:57:55 +08:00
|
|
|
unsigned Size = Info.Ctx.getTypeSize(E->getType());
|
|
|
|
uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue();
|
2011-11-13 06:28:03 +08:00
|
|
|
Result.Base = (Expr*)0;
|
2011-10-30 04:57:55 +08:00
|
|
|
Result.Offset = CharUnits::fromQuantity(N);
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.CallIndex = 0;
|
2011-11-04 10:25:55 +08:00
|
|
|
Result.Designator.setInvalid();
|
2010-05-08 05:00:08 +08:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Cast is of an lvalue, no need to change value.
|
2012-03-04 06:46:17 +08:00
|
|
|
Result.setFrom(Info.Ctx, Value);
|
2010-05-08 05:00:08 +08:00
|
|
|
return true;
|
2008-07-08 23:34:11 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_ArrayToPointerDecay:
|
2011-11-18 06:56:20 +08:00
|
|
|
if (SubExpr->isGLValue()) {
|
|
|
|
if (!EvaluateLValue(SubExpr, Result, Info))
|
|
|
|
return false;
|
|
|
|
} else {
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.set(SubExpr, Info.CurrentCall->Index);
|
|
|
|
if (!EvaluateInPlace(Info.CurrentCall->Temporaries[SubExpr],
|
|
|
|
Info, Result, SubExpr))
|
2011-11-18 06:56:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-11-04 10:25:55 +08:00
|
|
|
// The result is a pointer to the first element of the array.
|
2012-01-07 00:39:00 +08:00
|
|
|
if (const ConstantArrayType *CAT
|
|
|
|
= Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
|
|
|
|
Result.addArray(Info, E, CAT);
|
|
|
|
else
|
|
|
|
Result.Designator.setInvalid();
|
2011-11-04 10:25:55 +08:00
|
|
|
return true;
|
2011-11-01 04:57:44 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
case CK_FunctionToPointerDecay:
|
2011-11-01 04:57:44 +08:00
|
|
|
return EvaluateLValue(SubExpr, Result, Info);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-07-08 23:34:11 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
2011-11-10 14:34:14 +08:00
|
|
|
if (IsStringLiteralCall(E))
|
2010-05-08 05:00:08 +08:00
|
|
|
return Success(E);
|
2011-02-17 18:25:35 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCallExpr(E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Member Pointer Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MemberPointerExprEvaluator
|
|
|
|
: public ExprEvaluatorBase<MemberPointerExprEvaluator, bool> {
|
|
|
|
MemberPtr &Result;
|
|
|
|
|
|
|
|
bool Success(const ValueDecl *D) {
|
|
|
|
Result = MemberPtr(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
|
|
|
|
MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
|
|
|
|
: ExprEvaluatorBaseTy(Info), Result(Result) {}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
2011-11-18 06:56:20 +08:00
|
|
|
Result.setFrom(V);
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E) {
|
2011-11-18 06:56:20 +08:00
|
|
|
return Success((const ValueDecl*)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCastExpr(const CastExpr *E);
|
|
|
|
bool VisitUnaryAddrOf(const UnaryOperator *E);
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
|
|
|
|
EvalInfo &Info) {
|
|
|
|
assert(E->isRValue() && E->getType()->isMemberPointerType());
|
|
|
|
return MemberPointerExprEvaluator(Info, Result).Visit(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
|
|
|
|
|
|
|
case CK_NullToMemberPointer:
|
2011-12-31 05:15:51 +08:00
|
|
|
return ZeroInitialization(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
case CK_BaseToDerivedMemberPointer: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
if (E->path_empty())
|
|
|
|
return true;
|
|
|
|
// Base-to-derived member pointer casts store the path in derived-to-base
|
|
|
|
// order, so iterate backwards. The CXXBaseSpecifier also provides us with
|
|
|
|
// the wrong end of the derived->base arc, so stagger the path by one class.
|
|
|
|
typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
|
|
|
|
for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
|
|
|
|
PathI != PathE; ++PathI) {
|
|
|
|
assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
|
|
|
|
const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
|
|
|
|
if (!Result.castToDerived(Derived))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass();
|
|
|
|
if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl()))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
for (CastExpr::path_const_iterator PathI = E->path_begin(),
|
|
|
|
PathE = E->path_end(); PathI != PathE; ++PathI) {
|
|
|
|
assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
|
|
|
|
const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
|
|
|
|
if (!Result.castToBase(Base))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
|
|
|
|
// C++11 [expr.unary.op]p3 has very strict rules on how the address of a
|
|
|
|
// member can be formed.
|
|
|
|
return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl());
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Record Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RecordExprEvaluator
|
|
|
|
: public ExprEvaluatorBase<RecordExprEvaluator, bool> {
|
|
|
|
const LValue &This;
|
|
|
|
APValue &Result;
|
|
|
|
public:
|
|
|
|
|
|
|
|
RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
|
|
|
|
: ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
2012-02-15 10:18:13 +08:00
|
|
|
Result = V;
|
|
|
|
return true;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E);
|
2011-11-10 14:34:14 +08:00
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
bool VisitCastExpr(const CastExpr *E);
|
2011-11-10 14:34:14 +08:00
|
|
|
bool VisitInitListExpr(const InitListExpr *E);
|
|
|
|
bool VisitCXXConstructExpr(const CXXConstructExpr *E);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
/// Perform zero-initialization on an object of non-union class type.
|
|
|
|
/// C++11 [dcl.init]p5:
|
|
|
|
/// To zero-initialize an object or reference of type T means:
|
|
|
|
/// [...]
|
|
|
|
/// -- if T is a (possibly cv-qualified) non-union class type,
|
|
|
|
/// each non-static data member and each base-class subobject is
|
|
|
|
/// zero-initialized
|
2012-01-07 00:39:00 +08:00
|
|
|
static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
|
|
|
|
const RecordDecl *RD,
|
2011-12-31 05:15:51 +08:00
|
|
|
const LValue &This, APValue &Result) {
|
|
|
|
assert(!RD->isUnion() && "Expected non-union class type");
|
|
|
|
const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
|
|
|
|
Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
|
|
|
|
std::distance(RD->field_begin(), RD->field_end()));
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (CD) {
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
|
2012-01-07 00:39:00 +08:00
|
|
|
End = CD->bases_end(); I != End; ++I, ++Index) {
|
2011-12-31 05:15:51 +08:00
|
|
|
const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
|
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout);
|
|
|
|
if (!HandleClassZeroInitialization(Info, E, Base, Subobject,
|
2011-12-31 05:15:51 +08:00
|
|
|
Result.getStructBase(Index)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
for (RecordDecl::field_iterator I = RD->field_begin(), End = RD->field_end();
|
|
|
|
I != End; ++I) {
|
2011-12-31 05:15:51 +08:00
|
|
|
// -- if T is a reference type, no initialization is performed.
|
|
|
|
if ((*I)->getType()->isReferenceType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueMember(Info, E, Subobject, *I, &Layout);
|
2011-12-31 05:15:51 +08:00
|
|
|
|
|
|
|
ImplicitValueInitExpr VIE((*I)->getType());
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(
|
2011-12-31 05:15:51 +08:00
|
|
|
Result.getStructField((*I)->getFieldIndex()), Info, Subobject, &VIE))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RecordExprEvaluator::ZeroInitialization(const Expr *E) {
|
|
|
|
const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
|
|
|
|
if (RD->isUnion()) {
|
|
|
|
// C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
|
|
|
|
// object's first non-static named data member is zero-initialized
|
|
|
|
RecordDecl::field_iterator I = RD->field_begin();
|
|
|
|
if (I == RD->field_end()) {
|
|
|
|
Result = APValue((const FieldDecl*)0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
HandleLValueMember(Info, E, Subobject, *I);
|
2011-12-31 05:15:51 +08:00
|
|
|
Result = APValue(*I);
|
|
|
|
ImplicitValueInitExpr VIE((*I)->getType());
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE);
|
2011-12-31 05:15:51 +08:00
|
|
|
}
|
|
|
|
|
2012-02-17 08:44:16 +08:00
|
|
|
if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_virtual_base) << RD;
|
2012-02-17 08:44:16 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-07 00:39:00 +08:00
|
|
|
return HandleClassZeroInitialization(Info, E, RD, This, Result);
|
2011-12-31 05:15:51 +08:00
|
|
|
}
|
|
|
|
|
2011-11-11 12:05:33 +08:00
|
|
|
bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
|
|
|
|
|
|
|
case CK_ConstructorConversion:
|
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase: {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue DerivedObject;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!Evaluate(DerivedObject, Info, E->getSubExpr()))
|
2011-11-11 12:05:33 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!DerivedObject.isStruct())
|
|
|
|
return Error(E->getSubExpr());
|
2011-11-11 12:05:33 +08:00
|
|
|
|
|
|
|
// Derived-to-base rvalue conversion: just slice off the derived part.
|
|
|
|
APValue *Value = &DerivedObject;
|
|
|
|
const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
|
|
|
|
for (CastExpr::path_const_iterator PathI = E->path_begin(),
|
|
|
|
PathE = E->path_end(); PathI != PathE; ++PathI) {
|
|
|
|
assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
|
|
|
|
const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
|
|
|
|
Value = &Value->getStructBase(getBaseIndex(RD, Base));
|
|
|
|
RD = Base;
|
|
|
|
}
|
|
|
|
Result = *Value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
2012-02-19 22:53:49 +08:00
|
|
|
// Cannot constant-evaluate std::initializer_list inits.
|
|
|
|
if (E->initializesStdInitializerList())
|
|
|
|
return false;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl();
|
|
|
|
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (RD->isUnion()) {
|
2012-01-13 02:54:33 +08:00
|
|
|
const FieldDecl *Field = E->getInitializedFieldInUnion();
|
|
|
|
Result = APValue(Field);
|
|
|
|
if (!Field)
|
2011-11-10 14:34:14 +08:00
|
|
|
return true;
|
2012-01-13 02:54:33 +08:00
|
|
|
|
|
|
|
// If the initializer list for a union does not contain any elements, the
|
|
|
|
// first element of the union is value-initialized.
|
|
|
|
ImplicitValueInitExpr VIE(Field->getType());
|
|
|
|
const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
LValue Subobject = This;
|
2012-01-13 02:54:33 +08:00
|
|
|
HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout);
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr);
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
|
|
|
|
"initializer list for class with base classes");
|
|
|
|
Result = APValue(APValue::UninitStruct(), 0,
|
|
|
|
std::distance(RD->field_begin(), RD->field_end()));
|
|
|
|
unsigned ElementNo = 0;
|
2012-01-27 09:14:48 +08:00
|
|
|
bool Success = true;
|
2011-11-10 14:34:14 +08:00
|
|
|
for (RecordDecl::field_iterator Field = RD->field_begin(),
|
|
|
|
FieldEnd = RD->field_end(); Field != FieldEnd; ++Field) {
|
|
|
|
// Anonymous bit-fields are not considered members of the class for
|
|
|
|
// purposes of aggregate initialization.
|
|
|
|
if (Field->isUnnamedBitfield())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LValue Subobject = This;
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool HaveInit = ElementNo < E->getNumInits();
|
|
|
|
|
|
|
|
// FIXME: Diagnostics here should point to the end of the initializer
|
|
|
|
// list, not the start.
|
|
|
|
HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, Subobject,
|
|
|
|
*Field, &Layout);
|
|
|
|
|
|
|
|
// Perform an implicit value-initialization for members beyond the end of
|
|
|
|
// the initializer list.
|
|
|
|
ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(
|
2012-01-27 09:14:48 +08:00
|
|
|
Result.getStructField((*Field)->getFieldIndex()),
|
|
|
|
Info, Subobject, HaveInit ? E->getInit(ElementNo++) : &VIE)) {
|
|
|
|
if (!Info.keepEvaluatingAfterFailure())
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
2012-01-27 09:14:48 +08:00
|
|
|
Success = false;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
return Success;
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
|
|
|
|
const CXXConstructorDecl *FD = E->getConstructor();
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInit = E->requiresZeroInitialization();
|
|
|
|
if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
|
2012-01-13 02:54:33 +08:00
|
|
|
// If we've already performed zero-initialization, we're already done.
|
|
|
|
if (!Result.isUninit())
|
|
|
|
return true;
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
if (ZeroInit)
|
|
|
|
return ZeroInitialization(E);
|
|
|
|
|
2011-12-22 10:22:31 +08:00
|
|
|
const CXXRecordDecl *RD = FD->getParent();
|
|
|
|
if (RD->isUnion())
|
|
|
|
Result = APValue((FieldDecl*)0);
|
|
|
|
else
|
|
|
|
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
|
|
|
|
std::distance(RD->field_begin(), RD->field_end()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
const FunctionDecl *Definition = 0;
|
|
|
|
FD->getBody(Definition);
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
|
|
|
|
return false;
|
2011-11-10 14:34:14 +08:00
|
|
|
|
2012-01-10 12:32:03 +08:00
|
|
|
// Avoid materializing a temporary for an elidable copy/move constructor.
|
2011-12-31 05:15:51 +08:00
|
|
|
if (E->isElidable() && !ZeroInit)
|
2011-11-10 14:34:14 +08:00
|
|
|
if (const MaterializeTemporaryExpr *ME
|
|
|
|
= dyn_cast<MaterializeTemporaryExpr>(E->getArg(0)))
|
|
|
|
return Visit(ME->GetTemporaryExpr());
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
if (ZeroInit && !ZeroInitialization(E))
|
|
|
|
return false;
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
|
2012-01-27 09:14:48 +08:00
|
|
|
return HandleConstructorCall(E->getExprLoc(), This, Args,
|
2011-12-10 06:58:01 +08:00
|
|
|
cast<CXXConstructorDecl>(Definition), Info,
|
|
|
|
Result);
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool EvaluateRecord(const Expr *E, const LValue &This,
|
|
|
|
APValue &Result, EvalInfo &Info) {
|
|
|
|
assert(E->isRValue() && E->getType()->isRecordType() &&
|
|
|
|
"can't evaluate expression as a record rvalue");
|
|
|
|
return RecordExprEvaluator(Info, This, Result).Visit(E);
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Temporary Evaluation
|
|
|
|
//
|
|
|
|
// Temporaries are represented in the AST as rvalues, but generally behave like
|
|
|
|
// lvalues. The full-object of which the temporary is a subobject is implicitly
|
|
|
|
// materialized so that a reference can bind to it.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
namespace {
|
|
|
|
class TemporaryExprEvaluator
|
|
|
|
: public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
|
|
|
|
public:
|
|
|
|
TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
|
|
|
|
LValueExprEvaluatorBaseTy(Info, Result) {}
|
|
|
|
|
|
|
|
/// Visit an expression which constructs the value of this temporary.
|
|
|
|
bool VisitConstructExpr(const Expr *E) {
|
2012-02-15 10:18:13 +08:00
|
|
|
Result.set(E, Info.CurrentCall->Index);
|
|
|
|
return EvaluateInPlace(Info.CurrentCall->Temporaries[E], Info, Result, E);
|
2011-11-18 06:56:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
|
|
|
|
|
|
|
|
case CK_ConstructorConversion:
|
|
|
|
return VisitConstructExpr(E->getSubExpr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool VisitInitListExpr(const InitListExpr *E) {
|
|
|
|
return VisitConstructExpr(E);
|
|
|
|
}
|
|
|
|
bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
|
|
|
|
return VisitConstructExpr(E);
|
|
|
|
}
|
|
|
|
bool VisitCallExpr(const CallExpr *E) {
|
|
|
|
return VisitConstructExpr(E);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// Evaluate an expression of record type as a temporary.
|
|
|
|
static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
|
2011-12-20 06:01:37 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isRecordType());
|
2011-11-18 06:56:20 +08:00
|
|
|
return TemporaryExprEvaluator(Info, Result).Visit(E);
|
|
|
|
}
|
|
|
|
|
2009-01-18 11:20:47 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Vector Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class VectorExprEvaluator
|
2011-10-23 05:10:00 +08:00
|
|
|
: public ExprEvaluatorBase<VectorExprEvaluator, bool> {
|
|
|
|
APValue &Result;
|
2009-01-18 11:20:47 +08:00
|
|
|
public:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
VectorExprEvaluator(EvalInfo &info, APValue &Result)
|
|
|
|
: ExprEvaluatorBaseTy(info), Result(Result) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool Success(const ArrayRef<APValue> &V, const Expr *E) {
|
|
|
|
assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
|
|
|
|
// FIXME: remove this APValue copy.
|
|
|
|
Result = APValue(V.data(), V.size());
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
2011-11-04 13:33:44 +08:00
|
|
|
assert(V.isVector());
|
2011-10-23 05:10:00 +08:00
|
|
|
Result = V;
|
|
|
|
return true;
|
|
|
|
}
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool VisitUnaryReal(const UnaryOperator *E)
|
2009-02-23 12:23:56 +08:00
|
|
|
{ return Visit(E->getSubExpr()); }
|
2011-10-23 05:10:00 +08:00
|
|
|
bool VisitCastExpr(const CastExpr* E);
|
|
|
|
bool VisitInitListExpr(const InitListExpr *E);
|
|
|
|
bool VisitUnaryImag(const UnaryOperator *E);
|
2009-02-23 12:23:56 +08:00
|
|
|
// 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
|
2009-01-18 11:20:47 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue");
|
2011-10-23 05:10:00 +08:00
|
|
|
return VectorExprEvaluator(Info, Result).Visit(E);
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
|
|
|
|
const VectorType *VTy = E->getType()->castAs<VectorType>();
|
2009-07-01 15:50:47 +08:00
|
|
|
unsigned NElts = VTy->getNumElements();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 06:44:34 +08:00
|
|
|
const Expr *SE = E->getSubExpr();
|
2009-06-27 02:22:18 +08:00
|
|
|
QualType SETy = SE->getType();
|
2009-01-18 11:20:47 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
case CK_VectorSplat: {
|
2011-10-23 05:10:00 +08:00
|
|
|
APValue Val = APValue();
|
2011-03-25 08:43:55 +08:00
|
|
|
if (SETy->isIntegerType()) {
|
|
|
|
APSInt IntResult;
|
|
|
|
if (!EvaluateInteger(SE, IntResult, Info))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2011-10-23 05:10:00 +08:00
|
|
|
Val = APValue(IntResult);
|
2011-03-25 08:43:55 +08:00
|
|
|
} else if (SETy->isRealFloatingType()) {
|
|
|
|
APFloat F(0.0);
|
|
|
|
if (!EvaluateFloat(SE, F, Info))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2011-10-23 05:10:00 +08:00
|
|
|
Val = APValue(F);
|
2011-03-25 08:43:55 +08:00
|
|
|
} else {
|
2011-10-23 05:10:00 +08:00
|
|
|
return Error(E);
|
2011-03-25 08:43:55 +08:00
|
|
|
}
|
2009-07-01 15:50:47 +08:00
|
|
|
|
|
|
|
// Splat and create vector APValue.
|
2011-10-23 05:10:00 +08:00
|
|
|
SmallVector<APValue, 4> Elts(NElts, Val);
|
|
|
|
return Success(Elts, E);
|
2009-06-27 02:22:18 +08:00
|
|
|
}
|
2011-12-22 11:51:45 +08:00
|
|
|
case CK_BitCast: {
|
|
|
|
// Evaluate the operand into an APInt we can extract from.
|
|
|
|
llvm::APInt SValInt;
|
|
|
|
if (!EvalAndBitcastToAPInt(Info, SE, SValInt))
|
|
|
|
return false;
|
|
|
|
// Extract the elements
|
|
|
|
QualType EltTy = VTy->getElementType();
|
|
|
|
unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
|
|
|
|
bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
|
|
|
|
SmallVector<APValue, 4> Elts;
|
|
|
|
if (EltTy->isRealFloatingType()) {
|
|
|
|
const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
|
|
|
|
bool isIEESem = &Sem != &APFloat::PPCDoubleDouble;
|
|
|
|
unsigned FloatEltSize = EltSize;
|
|
|
|
if (&Sem == &APFloat::x87DoubleExtended)
|
|
|
|
FloatEltSize = 80;
|
|
|
|
for (unsigned i = 0; i < NElts; i++) {
|
|
|
|
llvm::APInt Elt;
|
|
|
|
if (BigEndian)
|
|
|
|
Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize);
|
|
|
|
else
|
|
|
|
Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize);
|
|
|
|
Elts.push_back(APValue(APFloat(Elt, isIEESem)));
|
|
|
|
}
|
|
|
|
} else if (EltTy->isIntegerType()) {
|
|
|
|
for (unsigned i = 0; i < NElts; i++) {
|
|
|
|
llvm::APInt Elt;
|
|
|
|
if (BigEndian)
|
|
|
|
Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize);
|
|
|
|
else
|
|
|
|
Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize);
|
|
|
|
Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType())));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Error(E);
|
|
|
|
}
|
|
|
|
return Success(Elts, E);
|
|
|
|
}
|
2011-03-25 08:43:55 +08:00
|
|
|
default:
|
2011-10-29 01:51:58 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
2009-07-01 15:50:47 +08:00
|
|
|
}
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool
|
2009-01-18 11:20:47 +08:00
|
|
|
VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
2011-10-23 05:10:00 +08:00
|
|
|
const VectorType *VT = E->getType()->castAs<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();
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<APValue, 4> Elements;
|
2009-01-18 11:20:47 +08:00
|
|
|
|
2012-01-04 07:24:20 +08:00
|
|
|
// The number of initializers can be less than the number of
|
|
|
|
// vector elements. For OpenCL, this can be due to nested vector
|
|
|
|
// initialization. For GCC compatibility, missing trailing elements
|
|
|
|
// should be initialized with zeroes.
|
|
|
|
unsigned CountInits = 0, CountElts = 0;
|
|
|
|
while (CountElts < NumElements) {
|
|
|
|
// Handle nested vector initialization.
|
|
|
|
if (CountInits < NumInits
|
|
|
|
&& E->getInit(CountInits)->getType()->isExtVectorType()) {
|
|
|
|
APValue v;
|
|
|
|
if (!EvaluateVector(E->getInit(CountInits), v, Info))
|
|
|
|
return Error(E);
|
|
|
|
unsigned vlen = v.getVectorLength();
|
|
|
|
for (unsigned j = 0; j < vlen; j++)
|
|
|
|
Elements.push_back(v.getVectorElt(j));
|
|
|
|
CountElts += vlen;
|
|
|
|
} else if (EltTy->isIntegerType()) {
|
2009-01-18 11:20:47 +08:00
|
|
|
llvm::APSInt sInt(32);
|
2012-01-04 07:24:20 +08:00
|
|
|
if (CountInits < NumInits) {
|
|
|
|
if (!EvaluateInteger(E->getInit(CountInits), sInt, Info))
|
2012-03-14 04:58:32 +08:00
|
|
|
return false;
|
2012-01-04 07:24:20 +08:00
|
|
|
} else // trailing integer zero.
|
|
|
|
sInt = Info.Ctx.MakeIntValue(0, EltTy);
|
|
|
|
Elements.push_back(APValue(sInt));
|
|
|
|
CountElts++;
|
2009-01-18 11:20:47 +08:00
|
|
|
} else {
|
|
|
|
llvm::APFloat f(0.0);
|
2012-01-04 07:24:20 +08:00
|
|
|
if (CountInits < NumInits) {
|
|
|
|
if (!EvaluateFloat(E->getInit(CountInits), f, Info))
|
2012-03-14 04:58:32 +08:00
|
|
|
return false;
|
2012-01-04 07:24:20 +08:00
|
|
|
} else // trailing float zero.
|
|
|
|
f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
|
|
|
|
Elements.push_back(APValue(f));
|
|
|
|
CountElts++;
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
2012-01-04 07:24:20 +08:00
|
|
|
CountInits++;
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
2011-10-23 05:10:00 +08:00
|
|
|
return Success(Elements, E);
|
2009-01-18 11:20:47 +08:00
|
|
|
}
|
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool
|
2011-12-31 05:15:51 +08:00
|
|
|
VectorExprEvaluator::ZeroInitialization(const Expr *E) {
|
2011-10-23 05:10:00 +08:00
|
|
|
const VectorType *VT = E->getType()->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)));
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
|
2011-10-23 05:10:00 +08:00
|
|
|
return Success(Elements, E);
|
2009-02-23 12:23:56 +08:00
|
|
|
}
|
|
|
|
|
2011-10-23 05:10:00 +08:00
|
|
|
bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
|
2011-10-25 02:44:57 +08:00
|
|
|
VisitIgnoredValue(E->getSubExpr());
|
2011-12-31 05:15:51 +08:00
|
|
|
return ZeroInitialization(E);
|
2009-02-23 12:23:56 +08:00
|
|
|
}
|
|
|
|
|
2011-11-07 17:22:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Array Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ArrayExprEvaluator
|
|
|
|
: public ExprEvaluatorBase<ArrayExprEvaluator, bool> {
|
2011-11-10 14:34:14 +08:00
|
|
|
const LValue &This;
|
2011-11-07 17:22:26 +08:00
|
|
|
APValue &Result;
|
|
|
|
public:
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
|
|
|
|
: ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
|
2011-11-07 17:22:26 +08:00
|
|
|
|
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
2012-02-17 11:35:37 +08:00
|
|
|
assert((V.isArray() || V.isLValue()) &&
|
|
|
|
"expected array or string literal");
|
2011-11-07 17:22:26 +08:00
|
|
|
Result = V;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E) {
|
2011-11-10 14:34:14 +08:00
|
|
|
const ConstantArrayType *CAT =
|
|
|
|
Info.Ctx.getAsConstantArrayType(E->getType());
|
|
|
|
if (!CAT)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-10 14:34:14 +08:00
|
|
|
|
|
|
|
Result = APValue(APValue::UninitArray(), 0,
|
|
|
|
CAT->getSize().getZExtValue());
|
|
|
|
if (!Result.hasArrayFiller()) return true;
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
// Zero-initialize all elements.
|
2011-11-10 14:34:14 +08:00
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
Subobject.addArray(Info, E, CAT);
|
2011-11-10 14:34:14 +08:00
|
|
|
ImplicitValueInitExpr VIE(CAT->getElementType());
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
|
|
|
|
2011-11-07 17:22:26 +08:00
|
|
|
bool VisitInitListExpr(const InitListExpr *E);
|
2011-11-18 06:56:20 +08:00
|
|
|
bool VisitCXXConstructExpr(const CXXConstructExpr *E);
|
2011-11-07 17:22:26 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
static bool EvaluateArray(const Expr *E, const LValue &This,
|
|
|
|
APValue &Result, EvalInfo &Info) {
|
2011-12-31 05:15:51 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue");
|
2011-11-10 14:34:14 +08:00
|
|
|
return ArrayExprEvaluator(Info, This, Result).Visit(E);
|
2011-11-07 17:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
|
|
|
const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
|
|
|
|
if (!CAT)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-07 17:22:26 +08:00
|
|
|
|
2011-12-22 09:07:19 +08:00
|
|
|
// C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
|
|
|
|
// an appropriately-typed string literal enclosed in braces.
|
2012-01-13 02:54:33 +08:00
|
|
|
if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() &&
|
2011-12-22 09:07:19 +08:00
|
|
|
Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) {
|
|
|
|
LValue LV;
|
|
|
|
if (!EvaluateLValue(E->getInit(0), LV, Info))
|
|
|
|
return false;
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Val;
|
2012-02-17 11:35:37 +08:00
|
|
|
LV.moveInto(Val);
|
|
|
|
return Success(Val, E);
|
2011-12-22 09:07:19 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool Success = true;
|
|
|
|
|
2011-11-07 17:22:26 +08:00
|
|
|
Result = APValue(APValue::UninitArray(), E->getNumInits(),
|
|
|
|
CAT->getSize().getZExtValue());
|
2011-11-10 14:34:14 +08:00
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
Subobject.addArray(Info, E, CAT);
|
2011-11-10 14:34:14 +08:00
|
|
|
unsigned Index = 0;
|
2011-11-07 17:22:26 +08:00
|
|
|
for (InitListExpr::const_iterator I = E->begin(), End = E->end();
|
2011-11-10 14:34:14 +08:00
|
|
|
I != End; ++I, ++Index) {
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(Result.getArrayInitializedElt(Index),
|
|
|
|
Info, Subobject, cast<Expr>(*I)) ||
|
2012-01-27 09:14:48 +08:00
|
|
|
!HandleLValueArrayAdjustment(Info, cast<Expr>(*I), Subobject,
|
|
|
|
CAT->getElementType(), 1)) {
|
|
|
|
if (!Info.keepEvaluatingAfterFailure())
|
|
|
|
return false;
|
|
|
|
Success = false;
|
|
|
|
}
|
2011-11-10 14:34:14 +08:00
|
|
|
}
|
2011-11-07 17:22:26 +08:00
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!Result.hasArrayFiller()) return Success;
|
2011-11-07 17:22:26 +08:00
|
|
|
assert(E->hasArrayFiller() && "no array filler for incomplete init list");
|
2011-11-10 14:34:14 +08:00
|
|
|
// FIXME: The Subobject here isn't necessarily right. This rarely matters,
|
|
|
|
// but sometimes does:
|
|
|
|
// struct S { constexpr S() : p(&p) {} void *p; };
|
|
|
|
// S s[10] = {};
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result.getArrayFiller(), Info,
|
|
|
|
Subobject, E->getArrayFiller()) && Success;
|
2011-11-07 17:22:26 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
|
|
|
|
const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType());
|
|
|
|
if (!CAT)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-18 06:56:20 +08:00
|
|
|
|
2012-01-13 02:54:33 +08:00
|
|
|
bool HadZeroInit = !Result.isUninit();
|
|
|
|
if (!HadZeroInit)
|
|
|
|
Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
|
2011-11-18 06:56:20 +08:00
|
|
|
if (!Result.hasArrayFiller())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const CXXConstructorDecl *FD = E->getConstructor();
|
2011-12-22 10:22:31 +08:00
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInit = E->requiresZeroInitialization();
|
|
|
|
if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
|
2012-01-13 02:54:33 +08:00
|
|
|
if (HadZeroInit)
|
|
|
|
return true;
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
if (ZeroInit) {
|
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
Subobject.addArray(Info, E, CAT);
|
2011-12-31 05:15:51 +08:00
|
|
|
ImplicitValueInitExpr VIE(CAT->getElementType());
|
2012-02-15 10:18:13 +08:00
|
|
|
return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE);
|
2011-12-31 05:15:51 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 10:22:31 +08:00
|
|
|
const CXXRecordDecl *RD = FD->getParent();
|
|
|
|
if (RD->isUnion())
|
|
|
|
Result.getArrayFiller() = APValue((FieldDecl*)0);
|
|
|
|
else
|
|
|
|
Result.getArrayFiller() =
|
|
|
|
APValue(APValue::UninitStruct(), RD->getNumBases(),
|
|
|
|
std::distance(RD->field_begin(), RD->field_end()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
const FunctionDecl *Definition = 0;
|
|
|
|
FD->getBody(Definition);
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition))
|
|
|
|
return false;
|
2011-11-18 06:56:20 +08:00
|
|
|
|
|
|
|
// FIXME: The Subobject here isn't necessarily right. This rarely matters,
|
|
|
|
// but sometimes does:
|
|
|
|
// struct S { constexpr S() : p(&p) {} void *p; };
|
|
|
|
// S s[10];
|
|
|
|
LValue Subobject = This;
|
2012-01-07 00:39:00 +08:00
|
|
|
Subobject.addArray(Info, E, CAT);
|
2011-12-31 05:15:51 +08:00
|
|
|
|
2012-01-13 02:54:33 +08:00
|
|
|
if (ZeroInit && !HadZeroInit) {
|
2011-12-31 05:15:51 +08:00
|
|
|
ImplicitValueInitExpr VIE(CAT->getElementType());
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE))
|
2011-12-31 05:15:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-18 06:56:20 +08:00
|
|
|
llvm::ArrayRef<const Expr*> Args(E->getArgs(), E->getNumArgs());
|
2012-01-27 09:14:48 +08:00
|
|
|
return HandleConstructorCall(E->getExprLoc(), Subobject, Args,
|
2011-11-18 06:56:20 +08:00
|
|
|
cast<CXXConstructorDecl>(Definition),
|
|
|
|
Info, Result.getArrayFiller());
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Integer Evaluation
|
2011-10-29 01:51:58 +08:00
|
|
|
//
|
|
|
|
// As a GNU extension, we support casting pointers to sufficiently-wide integer
|
|
|
|
// types and back in constant folding. Integer values are thus represented
|
|
|
|
// either as an integer-valued APValue, or as an lvalue-valued APValue.
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class IntExprEvaluator
|
2011-05-13 11:29:01 +08:00
|
|
|
: public ExprEvaluatorBase<IntExprEvaluator, bool> {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue &Result;
|
2008-07-12 02:11:29 +08:00
|
|
|
public:
|
2012-03-04 06:46:17 +08:00
|
|
|
IntExprEvaluator(EvalInfo &info, APValue &result)
|
2011-05-13 11:29:01 +08:00
|
|
|
: ExprEvaluatorBaseTy(info), Result(result) {}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2011-07-02 21:13:53 +08:00
|
|
|
bool Success(const llvm::APSInt &SI, const Expr *E) {
|
|
|
|
assert(E->getType()->isIntegralOrEnumerationType() &&
|
2010-06-16 08:17:44 +08:00
|
|
|
"Invalid evaluation result.");
|
2011-07-02 21:13:53 +08:00
|
|
|
assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
|
2009-02-20 02:37:50 +08:00
|
|
|
"Invalid evaluation result.");
|
2011-07-02 21:13:53 +08:00
|
|
|
assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
|
2009-02-20 02:37:50 +08:00
|
|
|
"Invalid evaluation result.");
|
2012-03-04 06:46:17 +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.");
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = APValue(APSInt(I));
|
2011-05-21 00:38:50 +08:00
|
|
|
Result.getInt().setIsUnsigned(
|
|
|
|
E->getType()->isUnsignedIntegerOrEnumerationType());
|
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.");
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType()));
|
2009-02-19 17:06:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-11 10:13:43 +08:00
|
|
|
bool Success(CharUnits Size, const Expr *E) {
|
|
|
|
return Success(Size.getQuantity(), E);
|
|
|
|
}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *E) {
|
2012-01-06 07:59:40 +08:00
|
|
|
if (V.isLValue() || V.isAddrLabelDiff()) {
|
2011-10-30 06:55:55 +08:00
|
|
|
Result = V;
|
|
|
|
return true;
|
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
return Success(V.getInt(), E);
|
2008-11-12 15:43:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E) { return Success(0, E); }
|
2011-10-12 05:43:33 +08:00
|
|
|
|
2012-02-28 07:18:37 +08:00
|
|
|
// FIXME: See EvalInfo::IntExprEvaluatorDepth.
|
|
|
|
bool Visit(const Expr *E) {
|
|
|
|
SaveAndRestore<unsigned> Depth(Info.IntExprEvaluatorDepth,
|
|
|
|
Info.IntExprEvaluatorDepth+1);
|
|
|
|
const unsigned MaxDepth = 512;
|
|
|
|
if (Depth.get() > MaxDepth) {
|
|
|
|
Info.Ctx.getDiagnostics().Report(E->getExprLoc(),
|
|
|
|
diag::err_intexpr_depth_limit_exceeded);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ExprEvaluatorBaseTy::Visit(E);
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
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
|
|
|
}
|
2009-11-24 13:28:59 +08:00
|
|
|
|
|
|
|
bool CheckReferencedDecl(const Expr *E, const Decl *D);
|
|
|
|
bool VisitDeclRefExpr(const DeclRefExpr *E) {
|
2011-05-13 11:29:01 +08:00
|
|
|
if (CheckReferencedDecl(E, E->getDecl()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return ExprEvaluatorBaseTy::VisitDeclRefExpr(E);
|
2009-11-24 13:28:59 +08:00
|
|
|
}
|
|
|
|
bool VisitMemberExpr(const MemberExpr *E) {
|
|
|
|
if (CheckReferencedDecl(E, E->getMemberDecl())) {
|
2011-10-29 01:51:58 +08:00
|
|
|
VisitIgnoredValue(E->getBase());
|
2009-11-24 13:28:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
|
|
|
|
return ExprEvaluatorBaseTy::VisitMemberExpr(E);
|
2009-11-24 13:28:59 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCallExpr(const 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-07-12 02:11:29 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCastExpr(const CastExpr* E);
|
2011-03-12 03:24:49 +08:00
|
|
|
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
|
2008-11-12 01:56:53 +08:00
|
|
|
|
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
|
|
|
|
2012-03-07 04:05:56 +08:00
|
|
|
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
2011-10-12 05:43:33 +08:00
|
|
|
// Note, GNU defines __null as an integer, not a pointer.
|
2008-12-22 06:39:40 +08:00
|
|
|
bool VisitGNUNullExpr(const GNUNullExpr *E) {
|
2011-12-31 05:15:51 +08:00
|
|
|
return ZeroInitialization(E);
|
2009-02-27 12:45:43 +08:00
|
|
|
}
|
|
|
|
|
2009-01-06 04:52:13 +08:00
|
|
|
bool VisitUnaryTypeTraitExpr(const UnaryTypeTraitExpr *E) {
|
2010-09-14 04:56:31 +08:00
|
|
|
return Success(E->getValue(), E);
|
2009-01-06 04:52:13 +08:00
|
|
|
}
|
|
|
|
|
2010-12-07 08:08:36 +08:00
|
|
|
bool VisitBinaryTypeTraitExpr(const BinaryTypeTraitExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
2012-02-24 15:38:34 +08:00
|
|
|
bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
2011-04-28 08:16:57 +08:00
|
|
|
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
2011-04-25 14:54:41 +08:00
|
|
|
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2010-09-11 04:55:47 +08:00
|
|
|
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
|
2011-01-05 01:33:58 +08:00
|
|
|
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
|
2011-09-25 01:48:14 +08:00
|
|
|
|
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);
|
2011-11-13 06:28:03 +08:00
|
|
|
static QualType GetObjectType(APValue::LValueBase B);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool TryEvaluateBuiltinObjectSize(const 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
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
|
|
|
|
/// produce either the integer value or a pointer.
|
|
|
|
///
|
|
|
|
/// GCC has a heinous extension which folds casts between pointer types and
|
|
|
|
/// pointer-sized integral types. We support this by allowing the evaluation of
|
|
|
|
/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
|
|
|
|
/// Some simple arithmetic on such values is supported (they are treated much
|
|
|
|
/// like char*).
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
|
2011-10-30 04:57:55 +08:00
|
|
|
EvalInfo &Info) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType());
|
2011-05-13 11:29:01 +08:00
|
|
|
return IntExprEvaluator(Info, Result).Visit(E);
|
2009-02-21 02:22:23 +08:00
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Val;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateIntegerOrLValue(E, Val, Info))
|
|
|
|
return false;
|
|
|
|
if (!Val.isInt()) {
|
|
|
|
// FIXME: It would be better to produce the diagnostic for casting
|
|
|
|
// a pointer to an integer.
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2009-02-20 04:17:33 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2009-02-20 04:17:33 +08:00
|
|
|
Result = Val.getInt();
|
|
|
|
return true;
|
2008-07-12 02:11:29 +08:00
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
/// Check whether the given declaration can be directly converted to an integral
|
|
|
|
/// rvalue. If not, no diagnostic is produced; there are other things we can
|
|
|
|
/// try.
|
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.
|
2011-06-30 17:36:05 +08:00
|
|
|
if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) {
|
2011-07-02 21:13:53 +08:00
|
|
|
// Check for signedness/width mismatches between E type and ECD value.
|
|
|
|
bool SameSign = (ECD->getInitVal().isSigned()
|
|
|
|
== E->getType()->isSignedIntegerOrEnumerationType());
|
|
|
|
bool SameWidth = (ECD->getInitVal().getBitWidth()
|
|
|
|
== Info.Ctx.getIntWidth(E->getType()));
|
|
|
|
if (SameSign && SameWidth)
|
|
|
|
return Success(ECD->getInitVal(), E);
|
|
|
|
else {
|
|
|
|
// Get rid of mismatch (otherwise Success assertions will fail)
|
|
|
|
// by computing a new value matching the type of E.
|
|
|
|
llvm::APSInt Val = ECD->getInitVal();
|
|
|
|
if (!SameSign)
|
|
|
|
Val.setIsSigned(!ECD->getInitVal().isSigned());
|
|
|
|
if (!SameWidth)
|
|
|
|
Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType()));
|
|
|
|
return Success(Val, E);
|
|
|
|
}
|
2011-06-30 17:36:05 +08:00
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
return false;
|
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?
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("CallExpr::isBuiltinClassifyType(): unimplemented type");
|
2008-10-06 14:40:35 +08:00
|
|
|
}
|
|
|
|
|
2011-12-29 03:48:30 +08:00
|
|
|
/// EvaluateBuiltinConstantPForLValue - Determine the result of
|
|
|
|
/// __builtin_constant_p when applied to the given lvalue.
|
|
|
|
///
|
|
|
|
/// An lvalue is only "constant" if it is a pointer or reference to the first
|
|
|
|
/// character of a string literal.
|
|
|
|
template<typename LValue>
|
|
|
|
static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) {
|
2012-03-11 10:23:56 +08:00
|
|
|
const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>();
|
2011-12-29 03:48:30 +08:00
|
|
|
return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
|
|
|
|
/// GCC as we can manage.
|
|
|
|
static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) {
|
|
|
|
QualType ArgType = Arg->getType();
|
|
|
|
|
|
|
|
// __builtin_constant_p always has one operand. The rules which gcc follows
|
|
|
|
// are not precisely documented, but are as follows:
|
|
|
|
//
|
|
|
|
// - If the operand is of integral, floating, complex or enumeration type,
|
|
|
|
// and can be folded to a known value of that type, it returns 1.
|
|
|
|
// - If the operand and can be folded to a pointer to the first character
|
|
|
|
// of a string literal (or such a pointer cast to an integral type), it
|
|
|
|
// returns 1.
|
|
|
|
//
|
|
|
|
// Otherwise, it returns 0.
|
|
|
|
//
|
|
|
|
// FIXME: GCC also intends to return 1 for literals of aggregate types, but
|
|
|
|
// its support for this does not currently work.
|
|
|
|
if (ArgType->isIntegralOrEnumerationType()) {
|
|
|
|
Expr::EvalResult Result;
|
|
|
|
if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
APValue &V = Result.Val;
|
|
|
|
if (V.getKind() == APValue::Int)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return EvaluateBuiltinConstantPForLValue(V);
|
|
|
|
} else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) {
|
|
|
|
return Arg->isEvaluatable(Ctx);
|
|
|
|
} else if (ArgType->isPointerType() || Arg->isGLValue()) {
|
|
|
|
LValue LV;
|
|
|
|
Expr::EvalStatus Status;
|
|
|
|
EvalInfo Info(Ctx, Status);
|
|
|
|
if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info)
|
|
|
|
: EvaluatePointer(Arg, LV, Info)) &&
|
|
|
|
!Status.HasSideEffects)
|
|
|
|
return EvaluateBuiltinConstantPForLValue(LV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anything else isn't considered to be sufficiently constant.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-11 07:27:23 +08:00
|
|
|
/// Retrieves the "underlying object type" of the given expression,
|
|
|
|
/// as used by __builtin_object_size.
|
2011-11-13 06:28:03 +08:00
|
|
|
QualType IntExprEvaluator::GetObjectType(APValue::LValueBase B) {
|
|
|
|
if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
|
|
|
|
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
|
2010-05-11 07:27:23 +08:00
|
|
|
return VD->getType();
|
2011-11-13 06:28:03 +08:00
|
|
|
} else if (const Expr *E = B.get<const Expr*>()) {
|
|
|
|
if (isa<CompoundLiteralExpr>(E))
|
|
|
|
return E->getType();
|
2010-05-11 07:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return QualType();
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool IntExprEvaluator::TryEvaluateBuiltinObjectSize(const CallExpr *E) {
|
2010-05-11 07:27:23 +08:00
|
|
|
// 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.
|
2011-11-13 06:28:03 +08:00
|
|
|
if (!Base.getLValueBase()) return Success(0, E);
|
2010-05-11 07:27:23 +08:00
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
QualType T = GetObjectType(Base.getLValueBase());
|
2010-05-11 07:27:23 +08:00
|
|
|
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())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2010-05-11 07:27:23 +08:00
|
|
|
|
|
|
|
CharUnits Size = Info.Ctx.getTypeSizeInChars(T);
|
|
|
|
CharUnits Offset = Base.getLValueOffset();
|
|
|
|
|
|
|
|
if (!Offset.isNegative() && Offset <= Size)
|
|
|
|
Size -= Offset;
|
|
|
|
else
|
|
|
|
Size = CharUnits::Zero();
|
2011-03-11 10:13:43 +08:00
|
|
|
return Success(Size, E);
|
2010-05-11 07:27:23 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
|
2011-11-10 14:34:14 +08:00
|
|
|
switch (E->isBuiltinCall()) {
|
2008-10-06 13:28:25 +08:00
|
|
|
default:
|
2011-05-13 11:29:01 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCallExpr(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)) {
|
2011-10-11 02:28:20 +08:00
|
|
|
if (E->getArg(1)->EvaluateKnownConstInt(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
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2009-10-27 02:35:08 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-12-29 03:48:30 +08:00
|
|
|
case Builtin::BI__builtin_constant_p:
|
|
|
|
return Success(EvaluateBuiltinConstantP(Info.Ctx, E->getArg(0)), E);
|
2011-12-09 10:04:48 +08:00
|
|
|
|
2009-09-23 14:06:36 +08:00
|
|
|
case Builtin::BI__builtin_eh_return_data_regno: {
|
2011-10-11 02:28:20 +08:00
|
|
|
int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue();
|
2011-09-02 08:18:52 +08:00
|
|
|
Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand);
|
2009-09-23 14:06:36 +08:00
|
|
|
return Success(Operand, E);
|
|
|
|
}
|
2010-02-13 08:10:10 +08:00
|
|
|
|
|
|
|
case Builtin::BI__builtin_expect:
|
|
|
|
return Visit(E->getArg(0));
|
2012-01-18 11:06:12 +08:00
|
|
|
|
2010-09-10 14:27:15 +08:00
|
|
|
case Builtin::BIstrlen:
|
2012-01-18 11:06:12 +08:00
|
|
|
// A call to strlen is not a constant expression.
|
|
|
|
if (Info.getLangOpts().CPlusPlus0x)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
|
2012-01-18 11:06:12 +08:00
|
|
|
<< /*isConstexpr*/0 << /*isConstructor*/0 << "'strlen'";
|
|
|
|
else
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
|
2012-01-18 11:06:12 +08:00
|
|
|
// Fall through.
|
2010-09-10 14:27:15 +08:00
|
|
|
case Builtin::BI__builtin_strlen:
|
|
|
|
// As an extension, we support strlen() and __builtin_strlen() as constant
|
|
|
|
// expressions when the argument is a string literal.
|
2011-05-13 11:29:01 +08:00
|
|
|
if (const StringLiteral *S
|
2010-09-10 14:27:15 +08:00
|
|
|
= dyn_cast<StringLiteral>(E->getArg(0)->IgnoreParenImpCasts())) {
|
|
|
|
// The string literal may have embedded null characters. Find the first
|
|
|
|
// one and truncate there.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Str = S->getString();
|
|
|
|
StringRef::size_type Pos = Str.find(0);
|
|
|
|
if (Pos != StringRef::npos)
|
2010-09-10 14:27:15 +08:00
|
|
|
Str = Str.substr(0, Pos);
|
|
|
|
|
|
|
|
return Success(Str.size(), E);
|
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-18 05:44:23 +08:00
|
|
|
|
|
|
|
case Builtin::BI__atomic_is_lock_free: {
|
|
|
|
APSInt SizeVal;
|
|
|
|
if (!EvaluateInteger(E->getArg(0), SizeVal, Info))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
|
|
|
|
// of two less than the maximum inline atomic width, we know it is
|
|
|
|
// lock-free. If the size isn't a power of two, or greater than the
|
|
|
|
// maximum alignment where we promote atomics, we know it is not lock-free
|
|
|
|
// (at least not in the sense of atomic_is_lock_free). Otherwise,
|
|
|
|
// the answer can only be determined at runtime; for example, 16-byte
|
|
|
|
// atomics have lock-free implementations on some, but not all,
|
|
|
|
// x86-64 processors.
|
|
|
|
|
|
|
|
// Check power-of-two.
|
|
|
|
CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue());
|
|
|
|
if (!Size.isPowerOfTwo())
|
|
|
|
#if 0
|
|
|
|
// FIXME: Suppress this folding until the ABI for the promotion width
|
|
|
|
// settles.
|
|
|
|
return Success(0, E);
|
|
|
|
#else
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-18 05:44:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// Check against promotion width.
|
|
|
|
// FIXME: Suppress this folding until the ABI for the promotion width
|
|
|
|
// settles.
|
|
|
|
unsigned PromoteWidthBits =
|
|
|
|
Info.Ctx.getTargetInfo().getMaxAtomicPromoteWidth();
|
|
|
|
if (Size > Info.Ctx.toCharUnitsFromBits(PromoteWidthBits))
|
|
|
|
return Success(0, E);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Check against inlining width.
|
|
|
|
unsigned InlineWidthBits =
|
|
|
|
Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
|
|
|
|
if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits))
|
|
|
|
return Success(1, E);
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-18 05:44:23 +08:00
|
|
|
}
|
2008-10-06 13:28:25 +08:00
|
|
|
}
|
2008-07-12 08:38:25 +08:00
|
|
|
}
|
2008-07-12 02:11:29 +08:00
|
|
|
|
2011-10-31 09:37:14 +08:00
|
|
|
static bool HasSameBase(const LValue &A, const LValue &B) {
|
|
|
|
if (!A.getLValueBase())
|
|
|
|
return !B.getLValueBase();
|
|
|
|
if (!B.getLValueBase())
|
|
|
|
return false;
|
|
|
|
|
2011-11-13 06:28:03 +08:00
|
|
|
if (A.getLValueBase().getOpaqueValue() !=
|
|
|
|
B.getLValueBase().getOpaqueValue()) {
|
2011-10-31 09:37:14 +08:00
|
|
|
const Decl *ADecl = GetLValueBaseDecl(A);
|
|
|
|
if (!ADecl)
|
|
|
|
return false;
|
|
|
|
const Decl *BDecl = GetLValueBaseDecl(B);
|
2011-11-07 13:07:52 +08:00
|
|
|
if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl())
|
2011-10-31 09:37:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsGlobalLValue(A.getLValueBase()) ||
|
2012-02-15 10:18:13 +08:00
|
|
|
A.getLValueCallIndex() == B.getLValueCallIndex();
|
2011-10-31 09:37:14 +08:00
|
|
|
}
|
|
|
|
|
2012-02-01 13:53:12 +08:00
|
|
|
/// Perform the given integer operation, which is known to need at most BitWidth
|
|
|
|
/// bits, and check for overflow in the original type (if that type was not an
|
|
|
|
/// unsigned type).
|
|
|
|
template<typename Operation>
|
|
|
|
static APSInt CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
|
|
|
|
const APSInt &LHS, const APSInt &RHS,
|
|
|
|
unsigned BitWidth, Operation Op) {
|
|
|
|
if (LHS.isUnsigned())
|
|
|
|
return Op(LHS, RHS);
|
|
|
|
|
|
|
|
APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false);
|
|
|
|
APSInt Result = Value.trunc(LHS.getBitWidth());
|
|
|
|
if (Result.extend(BitWidth) != Value)
|
|
|
|
HandleOverflow(Info, E, Value, E->getType());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
2011-10-29 01:51:58 +08:00
|
|
|
if (E->isAssignmentOp())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-29 01:51:58 +08:00
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Comma) {
|
2011-10-25 02:44:57 +08:00
|
|
|
VisitIgnoredValue(E->getLHS());
|
|
|
|
return Visit(E->getRHS());
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
2012-02-26 07:21:37 +08:00
|
|
|
if (E->isLogicalOp()) {
|
|
|
|
// These need to be handled specially because the operands aren't
|
|
|
|
// necessarily integral nor evaluated.
|
|
|
|
bool lhsResult, rhsResult;
|
|
|
|
|
|
|
|
if (EvaluateAsBooleanCondition(E->getLHS(), lhsResult, Info)) {
|
|
|
|
// We were able to evaluate the LHS, see if we can get away with not
|
|
|
|
// evaluating the RHS: 0 && X -> 0, 1 || X -> 1
|
|
|
|
if (lhsResult == (E->getOpcode() == BO_LOr))
|
|
|
|
return Success(lhsResult, E);
|
|
|
|
|
|
|
|
if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) {
|
|
|
|
if (E->getOpcode() == BO_LOr)
|
|
|
|
return Success(lhsResult || rhsResult, E);
|
|
|
|
else
|
|
|
|
return Success(lhsResult && rhsResult, E);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Since we weren't able to evaluate the left hand side, it
|
|
|
|
// must have had side effects.
|
|
|
|
Info.EvalStatus.HasSideEffects = true;
|
|
|
|
|
|
|
|
// Suppress diagnostics from this arm.
|
|
|
|
SpeculativeEvaluationRAII Speculative(Info);
|
|
|
|
if (EvaluateAsBooleanCondition(E->getRHS(), rhsResult, Info)) {
|
|
|
|
// We can't evaluate the LHS; however, sometimes the result
|
|
|
|
// is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
|
|
|
|
if (rhsResult == (E->getOpcode() == BO_LOr))
|
|
|
|
return Success(rhsResult, E);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool LHSOK = EvaluateComplex(E->getLHS(), LHS, Info);
|
|
|
|
if (!LHSOK && !Info.keepEvaluatingAfterFailure())
|
2009-01-29 14:43:41 +08:00
|
|
|
return false;
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
|
2009-01-29 14:43:41 +08:00
|
|
|
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
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info);
|
|
|
|
if (!LHSOK && !Info.keepEvaluatingAfterFailure())
|
2008-11-16 15:17:21 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK)
|
2008-11-16 15:17:21 +08:00
|
|
|
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:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("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()) {
|
2011-10-31 09:37:14 +08:00
|
|
|
if (E->getOpcode() == BO_Sub || E->isComparisonOp()) {
|
2012-01-27 09:14:48 +08:00
|
|
|
LValue LHSValue, RHSValue;
|
|
|
|
|
|
|
|
bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info);
|
|
|
|
if (!LHSOK && Info.keepEvaluatingAfterFailure())
|
2008-11-17 03:01:22 +08:00
|
|
|
return false;
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
|
2008-11-17 03:01:22 +08:00
|
|
|
return false;
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2011-10-31 09:37:14 +08:00
|
|
|
// Reject differing bases from the normal codepath; we special-case
|
|
|
|
// comparisons to null.
|
|
|
|
if (!HasSameBase(LHSValue, RHSValue)) {
|
2012-01-05 07:13:47 +08:00
|
|
|
if (E->getOpcode() == BO_Sub) {
|
|
|
|
// Handle &&A - &&B.
|
|
|
|
if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
|
|
|
|
return false;
|
|
|
|
const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
|
|
|
|
const Expr *RHSExpr = LHSValue.Base.dyn_cast<const Expr*>();
|
|
|
|
if (!LHSExpr || !RHSExpr)
|
|
|
|
return false;
|
|
|
|
const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
|
|
|
|
const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
|
|
|
|
if (!LHSAddrExpr || !RHSAddrExpr)
|
|
|
|
return false;
|
2012-01-06 07:59:40 +08:00
|
|
|
// Make sure both labels come from the same function.
|
|
|
|
if (LHSAddrExpr->getLabel()->getDeclContext() !=
|
|
|
|
RHSAddrExpr->getLabel()->getDeclContext())
|
|
|
|
return false;
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = APValue(LHSAddrExpr, RHSAddrExpr);
|
2012-01-05 07:13:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
2011-10-31 13:11:32 +08:00
|
|
|
// Inequalities and subtractions between unrelated pointers have
|
|
|
|
// unspecified or undefined behavior.
|
2009-06-14 10:17:33 +08:00
|
|
|
if (!E->isEqualityOp())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-11-01 06:28:05 +08:00
|
|
|
// A constant address may compare equal to the address of a symbol.
|
|
|
|
// The one exception is that address of an object cannot compare equal
|
2011-11-01 06:54:30 +08:00
|
|
|
// to a null pointer constant.
|
2011-11-01 06:28:05 +08:00
|
|
|
if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
|
|
|
|
(!RHSValue.Base && !RHSValue.Offset.isZero()))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-31 13:11:32 +08:00
|
|
|
// It's implementation-defined whether distinct literals will have
|
2012-02-01 09:42:44 +08:00
|
|
|
// distinct addresses. In clang, the result of such a comparison is
|
|
|
|
// unspecified, so it is not a constant expression. However, we do know
|
|
|
|
// that the address of a literal will be non-null.
|
2011-11-04 09:10:57 +08:00
|
|
|
if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
|
|
|
|
LHSValue.Base && RHSValue.Base)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-31 13:11:32 +08:00
|
|
|
// We can't tell whether weak symbols will end up pointing to the same
|
|
|
|
// object.
|
|
|
|
if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-10-31 13:11:32 +08:00
|
|
|
// Pointers with different bases cannot represent the same object.
|
2011-11-01 06:54:30 +08:00
|
|
|
// (Note that clang defaults to -fmerge-all-constants, which can
|
|
|
|
// lead to inconsistent results for comparisons involving the address
|
|
|
|
// of a constant; this generally doesn't matter in practice.)
|
2011-10-31 13:11:32 +08:00
|
|
|
return Success(E->getOpcode() == BO_NE, E);
|
2009-06-14 10:17:33 +08:00
|
|
|
}
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2012-02-01 16:10:20 +08:00
|
|
|
const CharUnits &LHSOffset = LHSValue.getLValueOffset();
|
|
|
|
const CharUnits &RHSOffset = RHSValue.getLValueOffset();
|
|
|
|
|
2012-02-02 09:16:57 +08:00
|
|
|
SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
|
|
|
|
SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
|
|
|
|
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Sub) {
|
2012-02-02 09:16:57 +08:00
|
|
|
// C++11 [expr.add]p6:
|
|
|
|
// Unless both pointers point to elements of the same array object, or
|
|
|
|
// one past the last element of the array object, the behavior is
|
|
|
|
// undefined.
|
|
|
|
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
|
|
|
|
!AreElementsOfSameArray(getType(LHSValue.Base),
|
|
|
|
LHSDesignator, RHSDesignator))
|
|
|
|
CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array);
|
|
|
|
|
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
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
CharUnits ElementSize;
|
2012-02-16 10:46:34 +08:00
|
|
|
if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize))
|
2011-11-10 14:34:14 +08:00
|
|
|
return false;
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2012-02-01 16:10:20 +08:00
|
|
|
// FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
|
|
|
|
// and produce incorrect results when it overflows. Such behavior
|
|
|
|
// appears to be non-conforming, but is common, so perhaps we should
|
|
|
|
// assume the standard intended for such cases to be undefined behavior
|
|
|
|
// and check for them.
|
|
|
|
|
|
|
|
// Compute (LHSOffset - RHSOffset) / Size carefully, checking for
|
|
|
|
// overflow in the final conversion to ptrdiff_t.
|
|
|
|
APSInt LHS(
|
|
|
|
llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
|
|
|
|
APSInt RHS(
|
|
|
|
llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
|
|
|
|
APSInt ElemSize(
|
|
|
|
llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), false);
|
|
|
|
APSInt TrueResult = (LHS - RHS) / ElemSize;
|
|
|
|
APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType()));
|
|
|
|
|
|
|
|
if (Result.extend(65) != TrueResult)
|
|
|
|
HandleOverflow(Info, E, TrueResult, E->getType());
|
|
|
|
return Success(Result, E);
|
2009-04-29 03:17:36 +08:00
|
|
|
}
|
2011-10-31 09:37:14 +08:00
|
|
|
|
2012-01-31 14:41:30 +08:00
|
|
|
// C++11 [expr.rel]p3:
|
|
|
|
// Pointers to void (after pointer conversions) can be compared, with a
|
|
|
|
// result defined as follows: If both pointers represent the same
|
|
|
|
// address or are both the null pointer value, the result is true if the
|
|
|
|
// operator is <= or >= and false otherwise; otherwise the result is
|
|
|
|
// unspecified.
|
|
|
|
// We interpret this as applying to pointers to *cv* void.
|
|
|
|
if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset &&
|
2012-02-02 09:16:57 +08:00
|
|
|
E->isRelationalOp())
|
2012-01-31 14:41:30 +08:00
|
|
|
CCEDiag(E, diag::note_constexpr_void_comparison);
|
|
|
|
|
2012-02-02 09:16:57 +08:00
|
|
|
// C++11 [expr.rel]p2:
|
|
|
|
// - If two pointers point to non-static data members of the same object,
|
|
|
|
// or to subobjects or array elements fo such members, recursively, the
|
|
|
|
// pointer to the later declared member compares greater provided the
|
|
|
|
// two members have the same access control and provided their class is
|
|
|
|
// not a union.
|
|
|
|
// [...]
|
|
|
|
// - Otherwise pointer comparisons are unspecified.
|
|
|
|
if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
|
|
|
|
E->isRelationalOp()) {
|
|
|
|
bool WasArrayIndex;
|
|
|
|
unsigned Mismatch =
|
|
|
|
FindDesignatorMismatch(getType(LHSValue.Base), LHSDesignator,
|
|
|
|
RHSDesignator, WasArrayIndex);
|
|
|
|
// At the point where the designators diverge, the comparison has a
|
|
|
|
// specified value if:
|
|
|
|
// - we are comparing array indices
|
|
|
|
// - we are comparing fields of a union, or fields with the same access
|
|
|
|
// Otherwise, the result is unspecified and thus the comparison is not a
|
|
|
|
// constant expression.
|
|
|
|
if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
|
|
|
|
Mismatch < RHSDesignator.Entries.size()) {
|
|
|
|
const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]);
|
|
|
|
const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]);
|
|
|
|
if (!LF && !RF)
|
|
|
|
CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes);
|
|
|
|
else if (!LF)
|
|
|
|
CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
|
|
|
|
<< getAsBaseClass(LHSDesignator.Entries[Mismatch])
|
|
|
|
<< RF->getParent() << RF;
|
|
|
|
else if (!RF)
|
|
|
|
CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field)
|
|
|
|
<< getAsBaseClass(RHSDesignator.Entries[Mismatch])
|
|
|
|
<< LF->getParent() << LF;
|
|
|
|
else if (!LF->getParent()->isUnion() &&
|
|
|
|
LF->getAccess() != RF->getAccess())
|
|
|
|
CCEDiag(E, diag::note_constexpr_pointer_comparison_differing_access)
|
|
|
|
<< LF << LF->getAccess() << RF << RF->getAccess()
|
|
|
|
<< LF->getParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 09:37:14 +08:00
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default: llvm_unreachable("missing comparison operator");
|
|
|
|
case BO_LT: return Success(LHSOffset < RHSOffset, E);
|
|
|
|
case BO_GT: return Success(LHSOffset > RHSOffset, E);
|
|
|
|
case BO_LE: return Success(LHSOffset <= RHSOffset, E);
|
|
|
|
case BO_GE: return Success(LHSOffset >= RHSOffset, E);
|
|
|
|
case BO_EQ: return Success(LHSOffset == RHSOffset, E);
|
|
|
|
case BO_NE: return Success(LHSOffset != RHSOffset, E);
|
2009-04-29 03:17:36 +08:00
|
|
|
}
|
2008-11-17 03:01:22 +08:00
|
|
|
}
|
|
|
|
}
|
2012-02-01 09:42:44 +08:00
|
|
|
|
|
|
|
if (LHSTy->isMemberPointerType()) {
|
|
|
|
assert(E->isEqualityOp() && "unexpected member pointer operation");
|
|
|
|
assert(RHSTy->isMemberPointerType() && "invalid comparison");
|
|
|
|
|
|
|
|
MemberPtr LHSValue, RHSValue;
|
|
|
|
|
|
|
|
bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info);
|
|
|
|
if (!LHSOK && Info.keepEvaluatingAfterFailure())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// C++11 [expr.eq]p2:
|
|
|
|
// If both operands are null, they compare equal. Otherwise if only one is
|
|
|
|
// null, they compare unequal.
|
|
|
|
if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
|
|
|
|
bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
|
|
|
|
return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise if either is a pointer to a virtual member function, the
|
|
|
|
// result is unspecified.
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl()))
|
|
|
|
if (MD->isVirtual())
|
|
|
|
CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
|
|
|
|
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl()))
|
|
|
|
if (MD->isVirtual())
|
|
|
|
CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD;
|
|
|
|
|
|
|
|
// Otherwise they compare equal if and only if they would refer to the
|
|
|
|
// same member of the same most derived object or the same subobject if
|
|
|
|
// they were dereferenced with a hypothetical object of the associated
|
|
|
|
// class type.
|
|
|
|
bool Equal = LHSValue == RHSValue;
|
|
|
|
return Success(E->getOpcode() == BO_EQ ? Equal : !Equal, E);
|
|
|
|
}
|
|
|
|
|
2012-02-15 06:35:28 +08:00
|
|
|
if (LHSTy->isNullPtrType()) {
|
|
|
|
assert(E->isComparisonOp() && "unexpected nullptr operation");
|
|
|
|
assert(RHSTy->isNullPtrType() && "missing pointer conversion");
|
|
|
|
// C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
|
|
|
|
// are compared, the result is true of the operator is <=, >= or ==, and
|
|
|
|
// false otherwise.
|
|
|
|
BinaryOperator::Opcode Opcode = E->getOpcode();
|
|
|
|
return Success(Opcode == BO_EQ || Opcode == BO_LE || Opcode == BO_GE, E);
|
|
|
|
}
|
|
|
|
|
2010-06-16 08:17:44 +08:00
|
|
|
if (!LHSTy->isIntegralOrEnumerationType() ||
|
|
|
|
!RHSTy->isIntegralOrEnumerationType()) {
|
2011-11-18 06:56:20 +08:00
|
|
|
// We can't continue from here for non-integral types.
|
|
|
|
return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
|
2008-11-13 14:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The LHS of a constant expr is always evaluated and needed.
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue LHSVal;
|
2012-01-27 09:14:48 +08:00
|
|
|
|
|
|
|
bool LHSOK = EvaluateIntegerOrLValue(E->getLHS(), LHSVal, Info);
|
|
|
|
if (!LHSOK && !Info.keepEvaluatingAfterFailure())
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
2008-07-27 13:46:18 +08:00
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!Visit(E->getRHS()) || !LHSOK)
|
2009-02-20 04:17:33 +08:00
|
|
|
return false;
|
2012-01-27 09:14:48 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue &RHSVal = Result;
|
2009-03-24 09:14:50 +08:00
|
|
|
|
|
|
|
// Handle cases like (unsigned long)&a + 4.
|
2011-10-29 01:51:58 +08:00
|
|
|
if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
|
2010-01-15 20:37:54 +08:00
|
|
|
CharUnits AdditionalOffset = CharUnits::fromQuantity(
|
|
|
|
RHSVal.getInt().getZExtValue());
|
2010-08-25 19:45:40 +08:00
|
|
|
if (E->getOpcode() == BO_Add)
|
2011-10-30 04:57:55 +08:00
|
|
|
LHSVal.getLValueOffset() += AdditionalOffset;
|
2009-03-24 09:14:50 +08:00
|
|
|
else
|
2011-10-30 04:57:55 +08:00
|
|
|
LHSVal.getLValueOffset() -= AdditionalOffset;
|
|
|
|
Result = LHSVal;
|
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 &&
|
2011-10-29 01:51:58 +08:00
|
|
|
RHSVal.isLValue() && LHSVal.isInt()) {
|
2011-10-30 04:57:55 +08:00
|
|
|
RHSVal.getLValueOffset() += CharUnits::fromQuantity(
|
|
|
|
LHSVal.getInt().getZExtValue());
|
|
|
|
// Note that RHSVal is Result.
|
2009-03-24 09:14:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-05 07:13:47 +08:00
|
|
|
if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
|
|
|
|
// Handle (intptr_t)&&A - (intptr_t)&&B.
|
|
|
|
if (!LHSVal.getLValueOffset().isZero() ||
|
|
|
|
!RHSVal.getLValueOffset().isZero())
|
|
|
|
return false;
|
|
|
|
const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
|
|
|
|
const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
|
|
|
|
if (!LHSExpr || !RHSExpr)
|
|
|
|
return false;
|
|
|
|
const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr);
|
|
|
|
const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr);
|
|
|
|
if (!LHSAddrExpr || !RHSAddrExpr)
|
|
|
|
return false;
|
2012-01-06 07:59:40 +08:00
|
|
|
// Make sure both labels come from the same function.
|
|
|
|
if (LHSAddrExpr->getLabel()->getDeclContext() !=
|
|
|
|
RHSAddrExpr->getLabel()->getDeclContext())
|
|
|
|
return false;
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = APValue(LHSAddrExpr, RHSAddrExpr);
|
2012-01-05 07:13:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-24 09:14:50 +08:00
|
|
|
// All the following cases expect both operands to be an integer
|
2011-10-29 01:51:58 +08:00
|
|
|
if (!LHSVal.isInt() || !RHSVal.isInt())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2008-11-13 14:09:17 +08:00
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
APSInt &LHS = LHSVal.getInt();
|
|
|
|
APSInt &RHS = RHSVal.getInt();
|
2009-03-24 09:14:50 +08:00
|
|
|
|
2008-07-08 22:35:21 +08:00
|
|
|
switch (E->getOpcode()) {
|
2008-11-12 15:43:42 +08:00
|
|
|
default:
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2012-02-01 13:53:12 +08:00
|
|
|
case BO_Mul:
|
|
|
|
return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
|
|
|
|
LHS.getBitWidth() * 2,
|
|
|
|
std::multiplies<APSInt>()), E);
|
|
|
|
case BO_Add:
|
|
|
|
return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
|
|
|
|
LHS.getBitWidth() + 1,
|
|
|
|
std::plus<APSInt>()), E);
|
|
|
|
case BO_Sub:
|
|
|
|
return Success(CheckedIntArithmetic(Info, E, LHS, RHS,
|
|
|
|
LHS.getBitWidth() + 1,
|
|
|
|
std::minus<APSInt>()), E);
|
2011-10-29 01:51:58 +08:00
|
|
|
case BO_And: return Success(LHS & RHS, E);
|
|
|
|
case BO_Xor: return Success(LHS ^ RHS, E);
|
|
|
|
case BO_Or: return Success(LHS | RHS, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2012-02-01 07:24:19 +08:00
|
|
|
case BO_Rem:
|
2008-07-12 08:14:42 +08:00
|
|
|
if (RHS == 0)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E, diag::note_expr_divide_by_zero);
|
2012-02-01 07:24:19 +08:00
|
|
|
// Check for overflow case: INT_MIN / -1 or INT_MIN % -1. The latter is not
|
|
|
|
// actually undefined behavior in C++11 due to a language defect.
|
2012-01-31 12:08:20 +08:00
|
|
|
if (RHS.isNegative() && RHS.isAllOnesValue() &&
|
|
|
|
LHS.isSigned() && LHS.isMinSignedValue())
|
|
|
|
HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), E->getType());
|
2012-02-01 07:24:19 +08:00
|
|
|
return Success(E->getOpcode() == BO_Rem ? LHS % RHS : LHS / RHS, E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shl: {
|
2012-01-31 12:08:20 +08:00
|
|
|
// During constant-folding, a negative shift is an opposite shift. Such a
|
|
|
|
// shift is not a constant expression.
|
2010-11-10 06:22:12 +08:00
|
|
|
if (RHS.isSigned() && RHS.isNegative()) {
|
2012-01-31 12:08:20 +08:00
|
|
|
CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
|
2010-11-10 06:22:12 +08:00
|
|
|
RHS = -RHS;
|
|
|
|
goto shift_right;
|
|
|
|
}
|
|
|
|
|
|
|
|
shift_left:
|
2012-01-31 12:08:20 +08:00
|
|
|
// C++11 [expr.shift]p1: Shift width must be less than the bit width of the
|
|
|
|
// shifted type.
|
|
|
|
unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
|
|
|
|
if (SA != RHS) {
|
|
|
|
CCEDiag(E, diag::note_constexpr_large_shift)
|
|
|
|
<< RHS << E->getType() << LHS.getBitWidth();
|
|
|
|
} else if (LHS.isSigned()) {
|
|
|
|
// C++11 [expr.shift]p2: A signed left shift must have a non-negative
|
2012-02-08 14:14:53 +08:00
|
|
|
// operand, and must not overflow the corresponding unsigned type.
|
2012-01-31 12:08:20 +08:00
|
|
|
if (LHS.isNegative())
|
|
|
|
CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS;
|
2012-02-08 14:14:53 +08:00
|
|
|
else if (LHS.countLeadingZeros() < SA)
|
|
|
|
CCEDiag(E, diag::note_constexpr_lshift_discards);
|
2012-01-31 12:08:20 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
return Success(LHS << SA, E);
|
2009-02-20 02:37:50 +08:00
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Shr: {
|
2012-01-31 12:08:20 +08:00
|
|
|
// During constant-folding, a negative shift is an opposite shift. Such a
|
|
|
|
// shift is not a constant expression.
|
2010-11-10 06:22:12 +08:00
|
|
|
if (RHS.isSigned() && RHS.isNegative()) {
|
2012-01-31 12:08:20 +08:00
|
|
|
CCEDiag(E, diag::note_constexpr_negative_shift) << RHS;
|
2010-11-10 06:22:12 +08:00
|
|
|
RHS = -RHS;
|
|
|
|
goto shift_left;
|
|
|
|
}
|
|
|
|
|
|
|
|
shift_right:
|
2012-01-31 12:08:20 +08:00
|
|
|
// C++11 [expr.shift]p1: Shift width must be less than the bit width of the
|
|
|
|
// shifted type.
|
|
|
|
unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1);
|
|
|
|
if (SA != RHS)
|
|
|
|
CCEDiag(E, diag::note_constexpr_large_shift)
|
|
|
|
<< RHS << E->getType() << LHS.getBitWidth();
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
return Success(LHS >> SA, E);
|
2009-02-20 02:37:50 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
case BO_LT: return Success(LHS < RHS, E);
|
|
|
|
case BO_GT: return Success(LHS > RHS, E);
|
|
|
|
case BO_LE: return Success(LHS <= RHS, E);
|
|
|
|
case BO_GE: return Success(LHS >= RHS, E);
|
|
|
|
case BO_EQ: return Success(LHS == RHS, E);
|
|
|
|
case BO_NE: return Success(LHS != RHS, E);
|
2008-11-13 10:13:11 +08:00
|
|
|
}
|
2008-07-08 22:35:21 +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.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();
|
2011-07-26 15:03:04 +08:00
|
|
|
|
|
|
|
// __alignof is defined to return the preferred alignment.
|
|
|
|
return Info.Ctx.toCharUnitsFromBits(
|
|
|
|
Info.Ctx.getPreferredTypeAlign(T.getTypePtr()));
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-12 03:24:49 +08:00
|
|
|
/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
|
|
|
|
/// a result as the expression's type.
|
|
|
|
bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
|
|
|
|
const UnaryExprOrTypeTraitExpr *E) {
|
|
|
|
switch(E->getKind()) {
|
|
|
|
case UETT_AlignOf: {
|
2009-01-25 05:09:06 +08:00
|
|
|
if (E->isArgumentType())
|
2011-03-11 10:13:43 +08:00
|
|
|
return Success(GetAlignOfType(E->getArgumentType()), E);
|
2009-01-25 05:09:06 +08:00
|
|
|
else
|
2011-03-11 10:13:43 +08:00
|
|
|
return Success(GetAlignOfExpr(E->getArgumentExpr()), E);
|
2009-01-25 05:09:06 +08:00
|
|
|
}
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2011-03-12 03:24:49 +08:00
|
|
|
case UETT_VecStep: {
|
|
|
|
QualType Ty = E->getTypeOfArgument();
|
2008-11-12 01:56:53 +08:00
|
|
|
|
2011-03-12 03:24:49 +08:00
|
|
|
if (Ty->isVectorType()) {
|
|
|
|
unsigned n = Ty->getAs<VectorType>()->getNumElements();
|
2009-03-23 12:38:34 +08:00
|
|
|
|
2011-03-12 03:24:49 +08:00
|
|
|
// The vec_step built-in functions that take a 3-component
|
|
|
|
// vector return 4. (OpenCL 1.1 spec 6.11.12)
|
|
|
|
if (n == 3)
|
|
|
|
n = 4;
|
|
|
|
|
|
|
|
return Success(n, E);
|
|
|
|
} else
|
|
|
|
return Success(1, E);
|
|
|
|
}
|
|
|
|
|
|
|
|
case UETT_SizeOf: {
|
|
|
|
QualType SrcTy = E->getTypeOfArgument();
|
|
|
|
// C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
|
|
|
|
// the result is the size of the referenced type."
|
|
|
|
if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
|
|
|
|
SrcTy = Ref->getPointeeType();
|
2009-01-25 06:19:05 +08:00
|
|
|
|
2011-11-10 14:34:14 +08:00
|
|
|
CharUnits Sizeof;
|
2012-02-16 10:46:34 +08:00
|
|
|
if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof))
|
2011-03-12 03:24:49 +08:00
|
|
|
return false;
|
2011-11-10 14:34:14 +08:00
|
|
|
return Success(Sizeof, E);
|
2011-03-12 03:24:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unknown expr/type trait");
|
2008-07-12 05:24:13 +08:00
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
|
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
|
|
|
CharUnits Result;
|
2011-05-13 11:29:01 +08:00
|
|
|
unsigned n = OOE->getNumComponents();
|
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
|
|
|
if (n == 0)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(OOE);
|
2011-05-13 11:29:01 +08:00
|
|
|
QualType CurrentType = OOE->getTypeSourceInfo()->getType();
|
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
|
|
|
for (unsigned i = 0; i != n; ++i) {
|
|
|
|
OffsetOfExpr::OffsetOfNode ON = OOE->getComponent(i);
|
|
|
|
switch (ON.getKind()) {
|
|
|
|
case OffsetOfExpr::OffsetOfNode::Array: {
|
2011-05-13 11:29:01 +08:00
|
|
|
const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex());
|
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
|
|
|
APSInt IdxResult;
|
|
|
|
if (!EvaluateInteger(Idx, IdxResult, Info))
|
|
|
|
return false;
|
|
|
|
const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType);
|
|
|
|
if (!AT)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(OOE);
|
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 = AT->getElementType();
|
|
|
|
CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType);
|
|
|
|
Result += IdxResult.getSExtValue() * ElementSize;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-10 06:58:01 +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
|
|
|
case OffsetOfExpr::OffsetOfNode::Field: {
|
|
|
|
FieldDecl *MemberDecl = ON.getField();
|
|
|
|
const RecordType *RT = CurrentType->getAs<RecordType>();
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!RT)
|
|
|
|
return Error(OOE);
|
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
|
|
|
RecordDecl *RD = RT->getDecl();
|
|
|
|
const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD);
|
2011-01-20 15:57:12 +08:00
|
|
|
unsigned i = MemberDecl->getFieldIndex();
|
2010-04-29 08:18:15 +08:00
|
|
|
assert(i < RL.getFieldCount() && "offsetof field in wrong type");
|
2011-01-18 09:56:16 +08:00
|
|
|
Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i));
|
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;
|
|
|
|
}
|
2011-12-10 06:58:01 +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
|
|
|
case OffsetOfExpr::OffsetOfNode::Identifier:
|
|
|
|
llvm_unreachable("dependent __builtin_offsetof");
|
2011-12-10 06:58:01 +08:00
|
|
|
|
2010-04-29 08:18:15 +08:00
|
|
|
case OffsetOfExpr::OffsetOfNode::Base: {
|
|
|
|
CXXBaseSpecifier *BaseSpec = ON.getBase();
|
|
|
|
if (BaseSpec->isVirtual())
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(OOE);
|
2010-04-29 08:18:15 +08:00
|
|
|
|
|
|
|
// Find the layout of the class whose base we are looking into.
|
|
|
|
const RecordType *RT = CurrentType->getAs<RecordType>();
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!RT)
|
|
|
|
return Error(OOE);
|
2010-04-29 08:18:15 +08:00
|
|
|
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)
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(OOE);
|
2010-04-29 08:18:15 +08:00
|
|
|
|
|
|
|
// Add the offset to the base.
|
2011-01-26 10:17:08 +08:00
|
|
|
Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl()));
|
2010-04-29 08:18:15 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2011-05-13 11:29:01 +08:00
|
|
|
return Success(Result, OOE);
|
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
|
|
|
}
|
|
|
|
|
2008-07-12 03:10:17 +08:00
|
|
|
bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
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.
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(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.
|
2011-12-10 06:58:01 +08:00
|
|
|
return Visit(E->getSubExpr());
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Plus:
|
2011-10-29 01:51:58 +08:00
|
|
|
// The result is just the value.
|
2011-12-10 06:58:01 +08:00
|
|
|
return Visit(E->getSubExpr());
|
|
|
|
case UO_Minus: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
if (!Result.isInt()) return Error(E);
|
2012-01-31 12:08:20 +08:00
|
|
|
const APSInt &Value = Result.getInt();
|
|
|
|
if (Value.isSigned() && Value.isMinSignedValue())
|
|
|
|
HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1),
|
|
|
|
E->getType());
|
|
|
|
return Success(-Value, E);
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
|
|
|
case UO_Not: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
if (!Result.isInt()) return Error(E);
|
|
|
|
return Success(~Result.getInt(), E);
|
|
|
|
}
|
|
|
|
case UO_LNot: {
|
|
|
|
bool bres;
|
|
|
|
if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info))
|
|
|
|
return false;
|
|
|
|
return Success(!bres, 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.
|
2011-05-13 11:29:01 +08:00
|
|
|
bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
|
|
|
|
const Expr *SubExpr = E->getSubExpr();
|
2008-12-01 02:14:57 +08:00
|
|
|
QualType DestType = E->getType();
|
2009-02-20 06:16:29 +08:00
|
|
|
QualType SrcType = SubExpr->getType();
|
2008-12-01 02:14:57 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
case CK_BaseToDerived:
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_Dynamic:
|
|
|
|
case CK_ToUnion:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_NullToPointer:
|
|
|
|
case CK_NullToMemberPointer:
|
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_ConstructorConversion:
|
|
|
|
case CK_IntegralToPointer:
|
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingCast:
|
2011-09-09 13:25:32 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
|
|
|
case CK_ObjCObjectLValueCast:
|
|
|
|
case CK_FloatingRealToComplex:
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexCast:
|
|
|
|
case CK_FloatingComplexToIntegralComplex:
|
|
|
|
case CK_IntegralRealToComplex:
|
|
|
|
case CK_IntegralComplexCast:
|
|
|
|
case CK_IntegralComplexToFloatingComplex:
|
|
|
|
llvm_unreachable("invalid cast kind for integral value");
|
|
|
|
|
2011-03-26 03:07:11 +08:00
|
|
|
case CK_BitCast:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_Dependent:
|
|
|
|
case CK_LValueBitCast:
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
|
|
|
case CK_ARCConsumeObject:
|
|
|
|
case CK_ARCReclaimReturnedObject:
|
|
|
|
case CK_ARCExtendBlockObject:
|
2012-02-22 13:02:47 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2011-03-25 08:43:55 +08:00
|
|
|
|
2012-01-18 05:17:26 +08:00
|
|
|
case CK_UserDefinedConversion:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_LValueToRValue:
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_NoOp:
|
2011-10-29 01:51:58 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
2011-03-25 08:43:55 +08:00
|
|
|
|
|
|
|
case CK_MemberPointerToBoolean:
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_IntegralComplexToBoolean: {
|
2008-11-12 17:44:48 +08:00
|
|
|
bool BoolResult;
|
2011-10-29 01:51:58 +08:00
|
|
|
if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info))
|
2008-11-12 17:44:48 +08:00
|
|
|
return false;
|
2009-02-19 17:06:44 +08:00
|
|
|
return Success(BoolResult, E);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_IntegralCast: {
|
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()) {
|
2012-01-05 07:13:47 +08:00
|
|
|
// Allow casts of address-of-label differences if they are no-ops
|
|
|
|
// or narrowing. (The narrowing case isn't actually guaranteed to
|
|
|
|
// be constant-evaluatable except in some narrow cases which are hard
|
|
|
|
// to detect here. We let it through on the assumption the user knows
|
|
|
|
// what they are doing.)
|
|
|
|
if (Result.isAddrLabelDiff())
|
|
|
|
return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType);
|
2009-02-20 09:15:07 +08:00
|
|
|
// 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
|
|
|
|
2012-01-31 06:27:01 +08:00
|
|
|
return Success(HandleIntToIntCast(Info, E, DestType, SrcType,
|
|
|
|
Result.getInt()), E);
|
2008-07-12 09:15:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_PointerToIntegral: {
|
2011-12-12 20:46:16 +08:00
|
|
|
CCEDiag(E, diag::note_constexpr_invalid_cast) << 2;
|
|
|
|
|
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.
|
2012-01-31 06:27:01 +08:00
|
|
|
// FIXME: Allow a larger integer size than the pointer size, and allow
|
|
|
|
// narrowing back down to pointer width in subsequent integral casts.
|
|
|
|
// FIXME: Check integer type's active bits, not its type size.
|
2009-02-20 06:24:01 +08:00
|
|
|
if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType))
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2009-02-20 06:24:01 +08:00
|
|
|
|
2011-11-16 15:18:12 +08:00
|
|
|
LV.Designator.setInvalid();
|
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);
|
2012-01-31 06:27:01 +08:00
|
|
|
return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E);
|
2008-07-08 22:30:00 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_IntegralComplexToReal: {
|
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;
|
2011-03-25 08:43:55 +08:00
|
|
|
return Success(C.getComplexIntReal(), E);
|
2009-04-23 03:23:09 +08:00
|
|
|
}
|
2009-02-22 19:46:18 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_FloatingToIntegral: {
|
|
|
|
APFloat F(0.0);
|
|
|
|
if (!EvaluateFloat(SubExpr, F, Info))
|
|
|
|
return false;
|
2008-07-12 09:15:53 +08:00
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
APSInt Value;
|
|
|
|
if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value))
|
|
|
|
return false;
|
|
|
|
return Success(Value, E);
|
2011-03-25 08:43:55 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-25 08:43:55 +08:00
|
|
|
llvm_unreachable("unknown cast resulting in integral value");
|
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;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateComplex(E->getSubExpr(), LV, Info))
|
|
|
|
return false;
|
|
|
|
if (!LV.isComplexInt())
|
|
|
|
return Error(E);
|
2009-02-28 11:59:05 +08:00
|
|
|
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;
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!EvaluateComplex(E->getSubExpr(), LV, Info))
|
|
|
|
return false;
|
|
|
|
if (!LV.isComplexInt())
|
|
|
|
return Error(E);
|
2009-02-28 11:59:05 +08:00
|
|
|
return Success(LV.getComplexIntImag(), E);
|
|
|
|
}
|
|
|
|
|
2011-10-25 02:44:57 +08:00
|
|
|
VisitIgnoredValue(E->getSubExpr());
|
2009-02-27 12:45:43 +08:00
|
|
|
return Success(0, E);
|
|
|
|
}
|
|
|
|
|
2011-01-05 01:33:58 +08:00
|
|
|
bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
|
|
|
|
return Success(E->getPackLength(), E);
|
|
|
|
}
|
|
|
|
|
2010-09-11 04:55:47 +08:00
|
|
|
bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
|
|
|
|
return Success(E->getValue(), E);
|
|
|
|
}
|
|
|
|
|
2008-08-22 08:06:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Float Evaluation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2009-11-29 03:03:38 +08:00
|
|
|
class FloatExprEvaluator
|
2011-05-13 11:29:01 +08:00
|
|
|
: public ExprEvaluatorBase<FloatExprEvaluator, bool> {
|
2008-08-22 08:06:13 +08:00
|
|
|
APFloat &Result;
|
|
|
|
public:
|
|
|
|
FloatExprEvaluator(EvalInfo &info, APFloat &result)
|
2011-05-13 11:29:01 +08:00
|
|
|
: ExprEvaluatorBaseTy(info), Result(result) {}
|
2008-08-22 08:06:13 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *e) {
|
2011-05-13 11:29:01 +08:00
|
|
|
Result = V.getFloat();
|
|
|
|
return true;
|
|
|
|
}
|
2008-08-22 08:06:13 +08:00
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
bool ZeroInitialization(const Expr *E) {
|
2011-10-12 05:43:33 +08:00
|
|
|
Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitCastExpr(const CastExpr *E);
|
2009-02-22 19:46:18 +08:00
|
|
|
|
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
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
// FIXME: Missing: array subscript of vector, member of vector
|
2008-08-22 08:06:13 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isRealFloatingType());
|
2011-05-13 11:29:01 +08:00
|
|
|
return FloatExprEvaluator(Info, Result).Visit(E);
|
2008-08-22 08:06:13 +08:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
|
2010-02-28 21:00:19 +08:00
|
|
|
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) {
|
2011-11-10 14:34:14 +08:00
|
|
|
switch (E->isBuiltinCall()) {
|
2011-05-13 11:29:01 +08:00
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitCallExpr(E);
|
|
|
|
|
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:
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
|
|
|
|
true, Result))
|
|
|
|
return Error(E);
|
|
|
|
return true;
|
2010-02-28 21:00:19 +08:00
|
|
|
|
2008-10-06 14:31:58 +08:00
|
|
|
case Builtin::BI__builtin_nan:
|
|
|
|
case Builtin::BI__builtin_nanf:
|
|
|
|
case Builtin::BI__builtin_nanl:
|
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.
|
2011-12-10 06:58:01 +08:00
|
|
|
if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0),
|
|
|
|
false, Result))
|
|
|
|
return Error(E);
|
|
|
|
return true;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-25 02:44:57 +08:00
|
|
|
VisitIgnoredValue(E->getSubExpr());
|
2010-08-15 04:52:13 +08:00
|
|
|
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) {
|
|
|
|
switch (E->getOpcode()) {
|
2011-12-10 06:58:01 +08:00
|
|
|
default: return Error(E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Plus:
|
2011-10-31 07:17:09 +08:00
|
|
|
return EvaluateFloat(E->getSubExpr(), Result, Info);
|
2010-08-25 19:45:40 +08:00
|
|
|
case UO_Minus:
|
2011-10-31 07:17:09 +08:00
|
|
|
if (!EvaluateFloat(E->getSubExpr(), Result, Info))
|
|
|
|
return false;
|
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) {
|
2011-11-18 06:56:20 +08:00
|
|
|
if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
|
|
|
|
return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
|
2010-10-31 09:21:47 +08:00
|
|
|
|
2008-10-16 11:51:50 +08:00
|
|
|
APFloat RHS(0.0);
|
2012-01-27 09:14:48 +08:00
|
|
|
bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info);
|
|
|
|
if (!LHSOK && !Info.keepEvaluatingAfterFailure())
|
2008-08-22 08:06:13 +08:00
|
|
|
return false;
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluateFloat(E->getRHS(), RHS, Info) || !LHSOK)
|
2008-08-22 08:06:13 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E->getOpcode()) {
|
2011-12-10 06:58:01 +08:00
|
|
|
default: return Error(E);
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Mul:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.multiply(RHS, APFloat::rmNearestTiesToEven);
|
2012-02-01 13:53:12 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Add:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.add(RHS, APFloat::rmNearestTiesToEven);
|
2012-02-01 13:53:12 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Sub:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.subtract(RHS, APFloat::rmNearestTiesToEven);
|
2012-02-01 13:53:12 +08:00
|
|
|
break;
|
2010-08-25 19:45:40 +08:00
|
|
|
case BO_Div:
|
2008-08-22 08:06:13 +08:00
|
|
|
Result.divide(RHS, APFloat::rmNearestTiesToEven);
|
2012-02-01 13:53:12 +08:00
|
|
|
break;
|
2008-08-22 08:06:13 +08:00
|
|
|
}
|
2012-02-01 13:53:12 +08:00
|
|
|
|
|
|
|
if (Result.isInfinity() || Result.isNaN())
|
|
|
|
CCEDiag(E, diag::note_constexpr_float_arithmetic) << Result.isNaN();
|
|
|
|
return true;
|
2008-08-22 08:06:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
|
|
|
|
Result = E->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
|
|
|
|
const Expr* SubExpr = E->getSubExpr();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-25 08:54:52 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
2011-10-29 01:51:58 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
2011-03-25 08:54:52 +08:00
|
|
|
|
|
|
|
case CK_IntegralToFloating: {
|
2008-11-12 17:44:48 +08:00
|
|
|
APSInt IntResult;
|
2011-12-13 14:39:58 +08:00
|
|
|
return EvaluateInteger(SubExpr, IntResult, Info) &&
|
|
|
|
HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult,
|
|
|
|
E->getType(), Result);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
2011-03-25 08:54:52 +08:00
|
|
|
|
|
|
|
case CK_FloatingCast: {
|
2008-11-12 17:44:48 +08:00
|
|
|
if (!Visit(SubExpr))
|
|
|
|
return false;
|
2011-12-13 14:39:58 +08:00
|
|
|
return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(),
|
|
|
|
Result);
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
2010-11-14 16:17:51 +08:00
|
|
|
|
2011-03-25 08:54:52 +08:00
|
|
|
case CK_FloatingComplexToReal: {
|
2010-11-14 16:17:51 +08:00
|
|
|
ComplexValue V;
|
|
|
|
if (!EvaluateComplex(SubExpr, V, Info))
|
|
|
|
return false;
|
|
|
|
Result = V.getComplexFloatReal();
|
|
|
|
return true;
|
|
|
|
}
|
2011-03-25 08:54:52 +08:00
|
|
|
}
|
2008-11-12 17:44:48 +08:00
|
|
|
}
|
|
|
|
|
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
|
2011-05-13 11:29:01 +08:00
|
|
|
: public ExprEvaluatorBase<ComplexExprEvaluator, bool> {
|
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)
|
2011-05-13 11:29:01 +08:00
|
|
|
: ExprEvaluatorBaseTy(info), Result(Result) {}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *e) {
|
2011-05-13 11:29:01 +08:00
|
|
|
Result.setFrom(V);
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-10 12:58:17 +08:00
|
|
|
bool ZeroInitialization(const Expr *E);
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Visitor Methods
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-11-17 04:27:53 +08:00
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
|
|
|
|
bool VisitCastExpr(const CastExpr *E);
|
2010-08-17 07:27:44 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *E);
|
2010-12-12 00:05:48 +08:00
|
|
|
bool VisitUnaryOperator(const UnaryOperator *E);
|
2012-01-10 12:58:17 +08:00
|
|
|
bool VisitInitListExpr(const InitListExpr *E);
|
2010-08-17 07:27:44 +08:00
|
|
|
};
|
|
|
|
} // 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) {
|
2011-10-29 01:51:58 +08:00
|
|
|
assert(E->isRValue() && E->getType()->isAnyComplexType());
|
2011-05-13 11:29:01 +08:00
|
|
|
return ComplexExprEvaluator(Info, Result).Visit(E);
|
2010-08-17 07:27:44 +08:00
|
|
|
}
|
|
|
|
|
2012-01-10 12:58:17 +08:00
|
|
|
bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
|
2012-01-14 07:34:56 +08:00
|
|
|
QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
|
2012-01-10 12:58:17 +08:00
|
|
|
if (ElemTy->isRealFloatingType()) {
|
|
|
|
Result.makeComplexFloat();
|
|
|
|
APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy));
|
|
|
|
Result.FloatReal = Zero;
|
|
|
|
Result.FloatImag = Zero;
|
|
|
|
} else {
|
|
|
|
Result.makeComplexInt();
|
|
|
|
APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy);
|
|
|
|
Result.IntReal = Zero;
|
|
|
|
Result.IntImag = Zero;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
|
|
|
|
const Expr* SubExpr = E->getSubExpr();
|
2010-08-17 07:27:44 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-13 11:29:01 +08:00
|
|
|
bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
|
2010-08-17 07:27:44 +08:00
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
switch (E->getCastKind()) {
|
|
|
|
case CK_BitCast:
|
|
|
|
case CK_BaseToDerived:
|
|
|
|
case CK_DerivedToBase:
|
|
|
|
case CK_UncheckedDerivedToBase:
|
|
|
|
case CK_Dynamic:
|
|
|
|
case CK_ToUnion:
|
|
|
|
case CK_ArrayToPointerDecay:
|
|
|
|
case CK_FunctionToPointerDecay:
|
|
|
|
case CK_NullToPointer:
|
|
|
|
case CK_NullToMemberPointer:
|
|
|
|
case CK_BaseToDerivedMemberPointer:
|
|
|
|
case CK_DerivedToBaseMemberPointer:
|
|
|
|
case CK_MemberPointerToBoolean:
|
2012-02-15 09:22:51 +08:00
|
|
|
case CK_ReinterpretMemberPointer:
|
2010-12-15 01:51:41 +08:00
|
|
|
case CK_ConstructorConversion:
|
|
|
|
case CK_IntegralToPointer:
|
|
|
|
case CK_PointerToIntegral:
|
|
|
|
case CK_PointerToBoolean:
|
|
|
|
case CK_ToVoid:
|
|
|
|
case CK_VectorSplat:
|
|
|
|
case CK_IntegralCast:
|
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_IntegralToFloating:
|
|
|
|
case CK_FloatingToIntegral:
|
|
|
|
case CK_FloatingToBoolean:
|
|
|
|
case CK_FloatingCast:
|
2011-09-09 13:25:32 +08:00
|
|
|
case CK_CPointerToObjCPointerCast:
|
|
|
|
case CK_BlockPointerToObjCPointerCast:
|
2010-12-15 01:51:41 +08:00
|
|
|
case CK_AnyPointerToBlockPointerCast:
|
|
|
|
case CK_ObjCObjectLValueCast:
|
|
|
|
case CK_FloatingComplexToReal:
|
|
|
|
case CK_FloatingComplexToBoolean:
|
|
|
|
case CK_IntegralComplexToReal:
|
|
|
|
case CK_IntegralComplexToBoolean:
|
2011-09-10 14:18:15 +08:00
|
|
|
case CK_ARCProduceObject:
|
|
|
|
case CK_ARCConsumeObject:
|
|
|
|
case CK_ARCReclaimReturnedObject:
|
|
|
|
case CK_ARCExtendBlockObject:
|
2012-02-22 13:02:47 +08:00
|
|
|
case CK_CopyAndAutoreleaseBlockObject:
|
2010-12-15 01:51:41 +08:00
|
|
|
llvm_unreachable("invalid cast kind for complex value");
|
|
|
|
|
|
|
|
case CK_LValueToRValue:
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2010-12-15 01:51:41 +08:00
|
|
|
case CK_NoOp:
|
2011-10-29 01:51:58 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
2010-11-13 17:02:35 +08:00
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
case CK_Dependent:
|
2011-03-25 08:43:55 +08:00
|
|
|
case CK_LValueBitCast:
|
2010-12-15 01:51:41 +08:00
|
|
|
case CK_UserDefinedConversion:
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2010-12-15 01:51:41 +08:00
|
|
|
|
|
|
|
case CK_FloatingRealToComplex: {
|
2010-08-17 07:27:44 +08:00
|
|
|
APFloat &Real = Result.FloatReal;
|
2010-12-15 01:51:41 +08:00
|
|
|
if (!EvaluateFloat(E->getSubExpr(), Real, Info))
|
2010-08-17 07:27:44 +08:00
|
|
|
return false;
|
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
Result.makeComplexFloat();
|
|
|
|
Result.FloatImag = APFloat(Real.getSemantics());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CK_FloatingComplexCast: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType To = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
QualType From
|
|
|
|
= E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
|
2011-12-13 14:39:58 +08:00
|
|
|
return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) &&
|
|
|
|
HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag);
|
2010-12-15 01:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CK_FloatingComplexToIntegralComplex: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType To = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
QualType From
|
|
|
|
= E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
Result.makeComplexInt();
|
2011-12-13 14:39:58 +08:00
|
|
|
return HandleFloatToIntCast(Info, E, From, Result.FloatReal,
|
|
|
|
To, Result.IntReal) &&
|
|
|
|
HandleFloatToIntCast(Info, E, From, Result.FloatImag,
|
|
|
|
To, Result.IntImag);
|
2010-12-15 01:51:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case CK_IntegralRealToComplex: {
|
2010-08-17 07:27:44 +08:00
|
|
|
APSInt &Real = Result.IntReal;
|
2010-12-15 01:51:41 +08:00
|
|
|
if (!EvaluateInteger(E->getSubExpr(), Real, Info))
|
2010-08-17 07:27:44 +08:00
|
|
|
return false;
|
2008-11-17 04:27:53 +08:00
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
Result.makeComplexInt();
|
|
|
|
Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CK_IntegralComplexCast: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
2010-08-17 07:27:44 +08:00
|
|
|
return false;
|
2008-11-17 05:51:21 +08:00
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
QualType To = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
QualType From
|
|
|
|
= E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
|
2009-04-23 03:23:09 +08:00
|
|
|
|
2012-01-31 06:27:01 +08:00
|
|
|
Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal);
|
|
|
|
Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag);
|
2010-12-15 01:51:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CK_IntegralComplexToFloatingComplex: {
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType To = E->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
QualType From
|
|
|
|
= E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType();
|
|
|
|
Result.makeComplexFloat();
|
2011-12-13 14:39:58 +08:00
|
|
|
return HandleIntToFloatCast(Info, E, From, Result.IntReal,
|
|
|
|
To, Result.FloatReal) &&
|
|
|
|
HandleIntToFloatCast(Info, E, From, Result.IntImag,
|
|
|
|
To, Result.FloatImag);
|
2010-12-15 01:51:41 +08:00
|
|
|
}
|
2008-11-17 05:51:21 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-15 01:51:41 +08:00
|
|
|
llvm_unreachable("unknown cast resulting in complex value");
|
2008-11-17 04:27:53 +08:00
|
|
|
}
|
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
|
2011-11-18 06:56:20 +08:00
|
|
|
if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
|
2011-11-17 01:22:48 +08:00
|
|
|
return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
|
|
|
|
|
2012-01-27 09:14:48 +08:00
|
|
|
bool LHSOK = Visit(E->getLHS());
|
|
|
|
if (!LHSOK && !Info.keepEvaluatingAfterFailure())
|
2010-05-08 01:22:02 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-08 01:22:02 +08:00
|
|
|
ComplexValue RHS;
|
2012-01-27 09:14:48 +08:00
|
|
|
if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK)
|
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()) {
|
2011-12-10 06:58:01 +08:00
|
|
|
default: return Error(E);
|
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;
|
2010-12-12 00:05:48 +08:00
|
|
|
case BO_Div:
|
|
|
|
if (Result.isComplexFloat()) {
|
|
|
|
ComplexValue LHS = Result;
|
|
|
|
APFloat &LHS_r = LHS.getComplexFloatReal();
|
|
|
|
APFloat &LHS_i = LHS.getComplexFloatImag();
|
|
|
|
APFloat &RHS_r = RHS.getComplexFloatReal();
|
|
|
|
APFloat &RHS_i = RHS.getComplexFloatImag();
|
|
|
|
APFloat &Res_r = Result.getComplexFloatReal();
|
|
|
|
APFloat &Res_i = Result.getComplexFloatImag();
|
|
|
|
|
|
|
|
APFloat Den = RHS_r;
|
|
|
|
Den.multiply(RHS_r, APFloat::rmNearestTiesToEven);
|
|
|
|
APFloat Tmp = RHS_i;
|
|
|
|
Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
|
|
|
|
Den.add(Tmp, APFloat::rmNearestTiesToEven);
|
|
|
|
|
|
|
|
Res_r = LHS_r;
|
|
|
|
Res_r.multiply(RHS_r, APFloat::rmNearestTiesToEven);
|
|
|
|
Tmp = LHS_i;
|
|
|
|
Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
|
|
|
|
Res_r.add(Tmp, APFloat::rmNearestTiesToEven);
|
|
|
|
Res_r.divide(Den, APFloat::rmNearestTiesToEven);
|
|
|
|
|
|
|
|
Res_i = LHS_i;
|
|
|
|
Res_i.multiply(RHS_r, APFloat::rmNearestTiesToEven);
|
|
|
|
Tmp = LHS_r;
|
|
|
|
Tmp.multiply(RHS_i, APFloat::rmNearestTiesToEven);
|
|
|
|
Res_i.subtract(Tmp, APFloat::rmNearestTiesToEven);
|
|
|
|
Res_i.divide(Den, APFloat::rmNearestTiesToEven);
|
|
|
|
} else {
|
2011-12-10 06:58:01 +08:00
|
|
|
if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0)
|
|
|
|
return Error(E, diag::note_expr_divide_by_zero);
|
|
|
|
|
2010-12-12 00:05:48 +08:00
|
|
|
ComplexValue LHS = Result;
|
|
|
|
APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
|
|
|
|
RHS.getComplexIntImag() * RHS.getComplexIntImag();
|
|
|
|
Result.getComplexIntReal() =
|
|
|
|
(LHS.getComplexIntReal() * RHS.getComplexIntReal() +
|
|
|
|
LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
|
|
|
|
Result.getComplexIntImag() =
|
|
|
|
(LHS.getComplexIntImag() * RHS.getComplexIntReal() -
|
|
|
|
LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-12 00:05:48 +08:00
|
|
|
bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
|
|
|
|
// Get the operand value into 'Result'.
|
|
|
|
if (!Visit(E->getSubExpr()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (E->getOpcode()) {
|
|
|
|
default:
|
2011-12-10 06:58:01 +08:00
|
|
|
return Error(E);
|
2010-12-12 00:05:48 +08:00
|
|
|
case UO_Extension:
|
|
|
|
return true;
|
|
|
|
case UO_Plus:
|
|
|
|
// The result is always just the subexpr.
|
|
|
|
return true;
|
|
|
|
case UO_Minus:
|
|
|
|
if (Result.isComplexFloat()) {
|
|
|
|
Result.getComplexFloatReal().changeSign();
|
|
|
|
Result.getComplexFloatImag().changeSign();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Result.getComplexIntReal() = -Result.getComplexIntReal();
|
|
|
|
Result.getComplexIntImag() = -Result.getComplexIntImag();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case UO_Not:
|
|
|
|
if (Result.isComplexFloat())
|
|
|
|
Result.getComplexFloatImag().changeSign();
|
|
|
|
else
|
|
|
|
Result.getComplexIntImag() = -Result.getComplexIntImag();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 12:58:17 +08:00
|
|
|
bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
|
|
|
|
if (E->getNumInits() == 2) {
|
|
|
|
if (E->getType()->isComplexType()) {
|
|
|
|
Result.makeComplexFloat();
|
|
|
|
if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info))
|
|
|
|
return false;
|
|
|
|
if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
Result.makeComplexInt();
|
|
|
|
if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info))
|
|
|
|
return false;
|
|
|
|
if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return ExprEvaluatorBaseTy::VisitInitListExpr(E);
|
|
|
|
}
|
|
|
|
|
2011-12-07 08:43:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Void expression evaluation, primarily for a cast to void on the LHS of a
|
|
|
|
// comma operator
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VoidExprEvaluator
|
|
|
|
: public ExprEvaluatorBase<VoidExprEvaluator, bool> {
|
|
|
|
public:
|
|
|
|
VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
bool Success(const APValue &V, const Expr *e) { return true; }
|
2011-12-07 08:43:50 +08:00
|
|
|
|
|
|
|
bool VisitCastExpr(const CastExpr *E) {
|
|
|
|
switch (E->getCastKind()) {
|
|
|
|
default:
|
|
|
|
return ExprEvaluatorBaseTy::VisitCastExpr(E);
|
|
|
|
case CK_ToVoid:
|
|
|
|
VisitIgnoredValue(E->getSubExpr());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
|
|
|
|
assert(E->isRValue() && E->getType()->isVoidType());
|
|
|
|
return VoidExprEvaluator(Info).Visit(E);
|
|
|
|
}
|
|
|
|
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-10-29 08:50:52 +08:00
|
|
|
// Top level Expr::EvaluateAsRValue method.
|
2008-07-12 02:11:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
|
2011-10-29 01:51:58 +08:00
|
|
|
// In C, function designators are not lvalues, but we evaluate them as if they
|
|
|
|
// are.
|
|
|
|
if (E->isGLValue() || E->getType()->isFunctionType()) {
|
|
|
|
LValue LV;
|
|
|
|
if (!EvaluateLValue(E, LV, Info))
|
|
|
|
return false;
|
|
|
|
LV.moveInto(Result);
|
|
|
|
} else if (E->getType()->isVectorType()) {
|
2011-10-17 05:26:27 +08:00
|
|
|
if (!EvaluateVector(E, Result, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2011-05-21 00:38:50 +08:00
|
|
|
} else if (E->getType()->isIntegralOrEnumerationType()) {
|
2011-10-17 05:26:27 +08:00
|
|
|
if (!IntExprEvaluator(Info, Result).Visit(E))
|
2009-10-27 07:05:19 +08:00
|
|
|
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;
|
2011-10-17 05:26:27 +08:00
|
|
|
LV.moveInto(Result);
|
2010-05-08 05:00:08 +08:00
|
|
|
} 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;
|
2012-03-04 06:46:17 +08:00
|
|
|
Result = APValue(F);
|
2010-05-08 05:00:08 +08:00
|
|
|
} else if (E->getType()->isAnyComplexType()) {
|
|
|
|
ComplexValue C;
|
|
|
|
if (!EvaluateComplex(E, C, Info))
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2011-10-17 05:26:27 +08:00
|
|
|
C.moveInto(Result);
|
2011-11-04 13:33:44 +08:00
|
|
|
} else if (E->getType()->isMemberPointerType()) {
|
2011-11-18 06:56:20 +08:00
|
|
|
MemberPtr P;
|
|
|
|
if (!EvaluateMemberPointer(E, P, Info))
|
|
|
|
return false;
|
|
|
|
P.moveInto(Result);
|
|
|
|
return true;
|
2011-12-31 05:15:51 +08:00
|
|
|
} else if (E->getType()->isArrayType()) {
|
2011-11-10 14:34:14 +08:00
|
|
|
LValue LV;
|
2012-02-15 10:18:13 +08:00
|
|
|
LV.set(E, Info.CurrentCall->Index);
|
2011-11-10 14:34:14 +08:00
|
|
|
if (!EvaluateArray(E, LV, Info.CurrentCall->Temporaries[E], Info))
|
2011-11-07 17:22:26 +08:00
|
|
|
return false;
|
2011-11-10 14:34:14 +08:00
|
|
|
Result = Info.CurrentCall->Temporaries[E];
|
2011-12-31 05:15:51 +08:00
|
|
|
} else if (E->getType()->isRecordType()) {
|
2011-11-10 14:34:14 +08:00
|
|
|
LValue LV;
|
2012-02-15 10:18:13 +08:00
|
|
|
LV.set(E, Info.CurrentCall->Index);
|
2011-11-10 14:34:14 +08:00
|
|
|
if (!EvaluateRecord(E, LV, Info.CurrentCall->Temporaries[E], Info))
|
|
|
|
return false;
|
|
|
|
Result = Info.CurrentCall->Temporaries[E];
|
2011-12-07 08:43:50 +08:00
|
|
|
} else if (E->getType()->isVoidType()) {
|
2011-12-13 14:39:58 +08:00
|
|
|
if (Info.getLangOpts().CPlusPlus0x)
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_constexpr_nonliteral)
|
2011-12-13 14:39:58 +08:00
|
|
|
<< E->getType();
|
|
|
|
else
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
|
2011-12-07 08:43:50 +08:00
|
|
|
if (!EvaluateVoid(E, Info))
|
|
|
|
return false;
|
2011-12-13 14:39:58 +08:00
|
|
|
} else if (Info.getLangOpts().CPlusPlus0x) {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_constexpr_nonliteral) << E->getType();
|
2011-12-13 14:39:58 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
} else {
|
2012-03-15 12:53:45 +08:00
|
|
|
Info.Diag(E, diag::note_invalid_subexpr_in_const_expr);
|
2009-10-27 07:05:19 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2009-10-27 07:05:19 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
|
|
|
|
/// cases, the in-place evaluation is essential, since later initializers for
|
|
|
|
/// an object can indirectly refer to subobjects which were initialized earlier.
|
|
|
|
static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
|
|
|
|
const Expr *E, CheckConstantExpressionKind CCEK,
|
|
|
|
bool AllowNonLiteralTypes) {
|
2012-02-14 06:16:19 +08:00
|
|
|
if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E))
|
2011-12-31 05:15:51 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (E->isRValue()) {
|
2011-11-04 13:33:44 +08:00
|
|
|
// Evaluate arrays and record types in-place, so that later initializers can
|
|
|
|
// refer to earlier-initialized members of the object.
|
2011-11-10 14:34:14 +08:00
|
|
|
if (E->getType()->isArrayType())
|
|
|
|
return EvaluateArray(E, This, Result, Info);
|
|
|
|
else if (E->getType()->isRecordType())
|
|
|
|
return EvaluateRecord(E, This, Result, Info);
|
2011-11-04 13:33:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For any other type, in-place evaluation is unimportant.
|
2012-03-04 06:46:17 +08:00
|
|
|
return Evaluate(Result, Info, E);
|
2011-11-04 13:33:44 +08:00
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
|
|
|
|
/// lvalue-to-rvalue cast if it is an lvalue.
|
|
|
|
static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
|
2011-12-31 05:15:51 +08:00
|
|
|
if (!CheckLiteralType(Info, E))
|
|
|
|
return false;
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
if (!::Evaluate(Result, Info, E))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (E->isGLValue()) {
|
|
|
|
LValue LV;
|
2012-03-04 06:46:17 +08:00
|
|
|
LV.setFrom(Info.Ctx, Result);
|
|
|
|
if (!HandleLValueToRValueConversion(Info, E, E->getType(), LV, Result))
|
2011-12-10 06:58:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
// Check this core constant expression is a constant expression.
|
2012-02-15 10:18:13 +08:00
|
|
|
return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result);
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
2011-10-29 01:51:58 +08:00
|
|
|
|
2011-10-29 08:50:52 +08:00
|
|
|
/// EvaluateAsRValue - Return true if this is a constant which we can fold using
|
2011-02-17 18:25:35 +08:00
|
|
|
/// any crazy technique (that has nothing to do with language standards) that
|
|
|
|
/// we want to. If this function returns true, it returns the folded constant
|
2011-10-29 01:51:58 +08:00
|
|
|
/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
|
|
|
|
/// will be applied to the result.
|
2011-10-29 08:50:52 +08:00
|
|
|
bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx) const {
|
2011-12-10 09:10:13 +08:00
|
|
|
// Fast-path evaluations of integer literals, since we sometimes see files
|
|
|
|
// containing vast quantities of these.
|
|
|
|
if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(this)) {
|
|
|
|
Result.Val = APValue(APSInt(L->getValue(),
|
|
|
|
L->getType()->isUnsignedIntegerType()));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-14 12:30:29 +08:00
|
|
|
// FIXME: Evaluating values of large array and record types can cause
|
|
|
|
// performance problems. Only do so in C++11 for now.
|
2011-11-18 06:56:20 +08:00
|
|
|
if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
|
2012-03-11 15:00:24 +08:00
|
|
|
!Ctx.getLangOpts().CPlusPlus0x)
|
2011-11-10 11:30:42 +08:00
|
|
|
return false;
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
EvalInfo Info(Ctx, Result);
|
|
|
|
return ::EvaluateAsRValue(Info, this, Result.Val);
|
2011-02-17 18:25:35 +08:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
bool Expr::EvaluateAsBooleanCondition(bool &Result,
|
|
|
|
const ASTContext &Ctx) const {
|
2011-10-29 01:51:58 +08:00
|
|
|
EvalResult Scratch;
|
2011-10-29 08:50:52 +08:00
|
|
|
return EvaluateAsRValue(Scratch, Ctx) &&
|
2012-03-04 06:46:17 +08:00
|
|
|
HandleConversionToBool(Scratch.Val, Result);
|
2010-01-06 07:42:56 +08:00
|
|
|
}
|
|
|
|
|
2011-12-29 03:48:30 +08:00
|
|
|
bool Expr::EvaluateAsInt(APSInt &Result, const ASTContext &Ctx,
|
|
|
|
SideEffectsKind AllowSideEffects) const {
|
|
|
|
if (!getType()->isIntegralOrEnumerationType())
|
|
|
|
return false;
|
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
EvalResult ExprResult;
|
2011-12-29 03:48:30 +08:00
|
|
|
if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isInt() ||
|
|
|
|
(!AllowSideEffects && ExprResult.HasSideEffects))
|
2011-10-29 01:51:58 +08:00
|
|
|
return false;
|
2011-12-10 06:58:01 +08:00
|
|
|
|
2011-10-29 01:51:58 +08:00
|
|
|
Result = ExprResult.Val.getInt();
|
|
|
|
return true;
|
2011-10-11 02:28:20 +08:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const {
|
2009-04-10 12:54:13 +08:00
|
|
|
EvalInfo Info(Ctx, Result);
|
|
|
|
|
2010-05-08 05:00:08 +08:00
|
|
|
LValue LV;
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects ||
|
|
|
|
!CheckLValueConstantExpression(Info, getExprLoc(),
|
|
|
|
Ctx.getLValueReferenceType(getType()), LV))
|
|
|
|
return false;
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
LV.moveInto(Result.Val);
|
2012-02-15 10:18:13 +08:00
|
|
|
return true;
|
2009-09-13 18:17:44 +08:00
|
|
|
}
|
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx,
|
|
|
|
const VarDecl *VD,
|
|
|
|
llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
|
2012-01-14 12:30:29 +08:00
|
|
|
// FIXME: Evaluating initializers for large array and record types can cause
|
|
|
|
// performance problems. Only do so in C++11 for now.
|
|
|
|
if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) &&
|
2012-03-11 15:00:24 +08:00
|
|
|
!Ctx.getLangOpts().CPlusPlus0x)
|
2012-01-14 12:30:29 +08:00
|
|
|
return false;
|
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
Expr::EvalStatus EStatus;
|
|
|
|
EStatus.Diag = &Notes;
|
|
|
|
|
|
|
|
EvalInfo InitInfo(Ctx, EStatus);
|
|
|
|
InitInfo.setEvaluatingDecl(VD, Value);
|
|
|
|
|
|
|
|
LValue LVal;
|
|
|
|
LVal.set(VD);
|
|
|
|
|
2011-12-31 05:15:51 +08:00
|
|
|
// C++11 [basic.start.init]p2:
|
|
|
|
// Variables with static storage duration or thread storage duration shall be
|
|
|
|
// zero-initialized before any other initialization takes place.
|
|
|
|
// This behavior is not present in C.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() &&
|
2011-12-31 05:15:51 +08:00
|
|
|
!VD->getType()->isReferenceType()) {
|
|
|
|
ImplicitValueInitExpr VIE(VD->getType());
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, CCEK_Constant,
|
|
|
|
/*AllowNonLiteralTypes=*/true))
|
2011-12-31 05:15:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-15 10:18:13 +08:00
|
|
|
if (!EvaluateInPlace(Value, InitInfo, LVal, this, CCEK_Constant,
|
|
|
|
/*AllowNonLiteralTypes=*/true) ||
|
|
|
|
EStatus.HasSideEffects)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(),
|
|
|
|
Value);
|
2011-12-19 14:19:21 +08:00
|
|
|
}
|
|
|
|
|
2011-10-29 08:50:52 +08:00
|
|
|
/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
|
|
|
|
/// constant folded, but discard the result.
|
2011-01-12 17:06:06 +08:00
|
|
|
bool Expr::isEvaluatable(const ASTContext &Ctx) const {
|
2008-12-01 14:44:05 +08:00
|
|
|
EvalResult Result;
|
2011-10-29 08:50:52 +08:00
|
|
|
return EvaluateAsRValue(Result, Ctx) && !Result.HasSideEffects;
|
2008-10-06 14:49:02 +08:00
|
|
|
}
|
2008-11-23 05:04:56 +08:00
|
|
|
|
2011-01-12 17:06:06 +08:00
|
|
|
bool Expr::HasSideEffects(const ASTContext &Ctx) const {
|
2011-10-17 05:26:27 +08:00
|
|
|
return HasSideEffect(Ctx).Visit(this);
|
2009-11-06 02:03:03 +08:00
|
|
|
}
|
|
|
|
|
2011-10-11 02:28:20 +08:00
|
|
|
APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
|
2008-12-20 04:58:05 +08:00
|
|
|
EvalResult EvalResult;
|
2011-10-29 08:50:52 +08:00
|
|
|
bool Result = EvaluateAsRValue(EvalResult, Ctx);
|
2010-12-23 09:01:28 +08:00
|
|
|
(void)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:
|
2011-10-29 08:50:52 +08:00
|
|
|
// 0: This expression is an ICE.
|
2010-05-07 13:32:02 +08:00
|
|
|
// 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;
|
2011-10-29 08:50:52 +08:00
|
|
|
if (!E->EvaluateAsRValue(EVResult, Ctx) || EVResult.HasSideEffects ||
|
2010-05-07 13:32:02 +08:00
|
|
|
!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()) {
|
2011-02-09 16:16:59 +08:00
|
|
|
#define ABSTRACT_STMT(Node)
|
2010-05-07 13:32:02 +08:00
|
|
|
#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::DesignatedInitExprClass:
|
|
|
|
case Expr::ImplicitValueInitExprClass:
|
|
|
|
case Expr::ParenListExprClass:
|
|
|
|
case Expr::VAArgExprClass:
|
|
|
|
case Expr::AddrLabelExprClass:
|
|
|
|
case Expr::StmtExprClass:
|
|
|
|
case Expr::CXXMemberCallExprClass:
|
2011-02-10 05:07:24 +08:00
|
|
|
case Expr::CUDAKernelCallExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
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:
|
2012-03-07 16:35:16 +08:00
|
|
|
case Expr::UserDefinedLiteralClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
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:
|
2010-12-06 16:20:24 +08:00
|
|
|
case Expr::ExprWithCleanupsClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::CXXTemporaryObjectExprClass:
|
|
|
|
case Expr::CXXUnresolvedConstructExprClass:
|
|
|
|
case Expr::CXXDependentScopeMemberExprClass:
|
|
|
|
case Expr::UnresolvedMemberExprClass:
|
|
|
|
case Expr::ObjCStringLiteralClass:
|
2012-03-07 04:05:56 +08:00
|
|
|
case Expr::ObjCNumericLiteralClass:
|
|
|
|
case Expr::ObjCArrayLiteralClass:
|
|
|
|
case Expr::ObjCDictionaryLiteralClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::ObjCEncodeExprClass:
|
|
|
|
case Expr::ObjCMessageExprClass:
|
|
|
|
case Expr::ObjCSelectorExprClass:
|
|
|
|
case Expr::ObjCProtocolExprClass:
|
|
|
|
case Expr::ObjCIvarRefExprClass:
|
|
|
|
case Expr::ObjCPropertyRefExprClass:
|
2012-03-07 04:05:56 +08:00
|
|
|
case Expr::ObjCSubscriptRefExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::ObjCIsaExprClass:
|
|
|
|
case Expr::ShuffleVectorExprClass:
|
|
|
|
case Expr::BlockExprClass:
|
|
|
|
case Expr::NoStmtClass:
|
2010-11-16 07:31:06 +08:00
|
|
|
case Expr::OpaqueValueExprClass:
|
2011-01-04 01:17:50 +08:00
|
|
|
case Expr::PackExpansionExprClass:
|
2011-01-15 09:15:58 +08:00
|
|
|
case Expr::SubstNonTypeTemplateParmPackExprClass:
|
2011-06-04 08:47:47 +08:00
|
|
|
case Expr::AsTypeExprClass:
|
2011-06-16 07:02:42 +08:00
|
|
|
case Expr::ObjCIndirectCopyRestoreExprClass:
|
2011-06-22 01:03:29 +08:00
|
|
|
case Expr::MaterializeTemporaryExprClass:
|
2011-11-06 17:01:30 +08:00
|
|
|
case Expr::PseudoObjectExprClass:
|
2011-10-11 10:20:01 +08:00
|
|
|
case Expr::AtomicExprClass:
|
2011-09-25 01:48:14 +08:00
|
|
|
case Expr::InitListExprClass:
|
2012-02-07 18:09:13 +08:00
|
|
|
case Expr::LambdaExprClass:
|
2011-09-25 01:48:14 +08:00
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
|
2011-01-05 01:33:58 +08:00
|
|
|
case Expr::SizeOfPackExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::GNUNullExprClass:
|
|
|
|
// GCC considers the GNU __null value to be an integral constant expression.
|
|
|
|
return NoDiag();
|
|
|
|
|
2011-07-15 13:09:51 +08:00
|
|
|
case Expr::SubstNonTypeTemplateParmExprClass:
|
|
|
|
return
|
|
|
|
CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx);
|
|
|
|
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::ParenExprClass:
|
|
|
|
return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
|
2011-04-15 08:35:48 +08:00
|
|
|
case Expr::GenericSelectionExprClass:
|
|
|
|
return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx);
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::IntegerLiteralClass:
|
|
|
|
case Expr::CharacterLiteralClass:
|
2012-03-07 04:05:56 +08:00
|
|
|
case Expr::ObjCBoolLiteralExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::CXXBoolLiteralExprClass:
|
2010-07-08 14:14:04 +08:00
|
|
|
case Expr::CXXScalarValueInitExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
case Expr::UnaryTypeTraitExprClass:
|
2010-12-07 08:08:36 +08:00
|
|
|
case Expr::BinaryTypeTraitExprClass:
|
2012-02-24 15:38:34 +08:00
|
|
|
case Expr::TypeTraitExprClass:
|
2011-04-28 08:16:57 +08:00
|
|
|
case Expr::ArrayTypeTraitExprClass:
|
2011-04-25 14:54:41 +08:00
|
|
|
case Expr::ExpressionTraitExprClass:
|
2010-09-11 04:55:43 +08:00
|
|
|
case Expr::CXXNoexceptExprClass:
|
2010-05-07 13:32:02 +08:00
|
|
|
return NoDiag();
|
|
|
|
case Expr::CallExprClass:
|
2010-08-31 01:47:05 +08:00
|
|
|
case Expr::CXXOperatorCallExprClass: {
|
2011-10-25 06:35:48 +08:00
|
|
|
// C99 6.6/3 allows function calls within unevaluated subexpressions of
|
|
|
|
// constant expressions, but they can never be ICEs because an ICE cannot
|
|
|
|
// contain an operand of (pointer to) function type.
|
2010-05-07 13:32:02 +08:00
|
|
|
const CallExpr *CE = cast<CallExpr>(E);
|
2011-11-10 14:34:14 +08:00
|
|
|
if (CE->isBuiltinCall())
|
2010-05-07 13:32:02 +08:00
|
|
|
return CheckEvalInICE(E, Ctx);
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
2012-02-25 06:12:32 +08:00
|
|
|
case Expr::DeclRefExprClass: {
|
2010-05-07 13:32:02 +08:00
|
|
|
if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
|
|
|
|
return NoDiag();
|
2012-02-25 06:12:32 +08:00
|
|
|
const ValueDecl *D = dyn_cast<ValueDecl>(cast<DeclRefExpr>(E)->getDecl());
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus &&
|
2012-02-25 06:12:32 +08:00
|
|
|
D && IsConstNonVolatile(D->getType())) {
|
2010-05-07 13:32:02 +08:00
|
|
|
// 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)) {
|
2011-11-08 09:31:09 +08:00
|
|
|
if (!Dcl->getType()->isIntegralOrEnumerationType())
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
|
|
|
|
2011-12-19 14:19:21 +08:00
|
|
|
const VarDecl *VD;
|
|
|
|
// Look for a declaration of this variable that has an initializer, and
|
|
|
|
// check whether it is an ICE.
|
|
|
|
if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE())
|
|
|
|
return NoDiag();
|
|
|
|
else
|
|
|
|
return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
|
2010-05-07 13:32:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
2012-02-25 06:12:32 +08:00
|
|
|
}
|
2010-05-07 13:32:02 +08:00
|
|
|
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:
|
2011-10-25 06:35:48 +08:00
|
|
|
// C99 6.6/3 allows increment and decrement within unevaluated
|
|
|
|
// subexpressions of constant expressions, but they can never be ICEs
|
|
|
|
// because an ICE cannot contain an lvalue operand.
|
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
|
2011-10-29 08:50:52 +08:00
|
|
|
// EvaluateAsRValue matches the proposed gcc behavior for cases like
|
2011-10-25 06:35:48 +08:00
|
|
|
// "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
|
2010-05-07 13:32:02 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2011-03-12 03:24:49 +08:00
|
|
|
case Expr::UnaryExprOrTypeTraitExprClass: {
|
|
|
|
const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E);
|
|
|
|
if ((Exp->getKind() == UETT_SizeOf) &&
|
|
|
|
Exp->getTypeOfArgument()->isVariableArrayType())
|
2010-05-07 13:32:02 +08:00
|
|
|
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:
|
2011-10-25 06:35:48 +08:00
|
|
|
// C99 6.6/3 allows assignments within unevaluated subexpressions of
|
|
|
|
// constant expressions, but they can never be ICEs because an ICE cannot
|
|
|
|
// contain an lvalue operand.
|
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) {
|
2011-10-29 08:50:52 +08:00
|
|
|
// EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
|
2010-05-07 13:32:02 +08:00
|
|
|
// we don't evaluate one.
|
2011-02-26 16:27:17 +08:00
|
|
|
if (LHSResult.Val == 0 && RHSResult.Val == 0) {
|
2011-10-11 02:28:20 +08:00
|
|
|
llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
|
2010-05-07 13:32:02 +08:00
|
|
|
if (REval == 0)
|
|
|
|
return ICEDiag(1, E->getLocStart());
|
|
|
|
if (REval.isSigned() && REval.isAllOnesValue()) {
|
2011-10-11 02:28:20 +08:00
|
|
|
llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
|
2010-05-07 13:32:02 +08:00
|
|
|
if (LEval.isMinSignedValue())
|
|
|
|
return ICEDiag(1, E->getLocStart());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 19:45:40 +08:00
|
|
|
if (Exp->getOpcode() == BO_Comma) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().C99) {
|
2010-05-07 13:32:02 +08:00
|
|
|
// 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) !=
|
2011-10-11 02:28:20 +08:00
|
|
|
(Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
|
2010-05-07 13:32:02 +08:00
|
|
|
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:
|
2011-10-25 02:26:35 +08:00
|
|
|
case Expr::CXXConstCastExprClass:
|
2011-06-16 07:02:42 +08:00
|
|
|
case Expr::ObjCBridgedCastExprClass: {
|
2010-05-07 13:32:02 +08:00
|
|
|
const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
|
2011-12-18 10:33:09 +08:00
|
|
|
if (isa<ExplicitCastExpr>(E)) {
|
|
|
|
if (const FloatingLiteral *FL
|
|
|
|
= dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) {
|
|
|
|
unsigned DestWidth = Ctx.getIntWidth(E->getType());
|
|
|
|
bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
|
|
|
|
APSInt IgnoredVal(DestWidth, !DestSigned);
|
|
|
|
bool Ignored;
|
|
|
|
// If the value does not fit in the destination type, the behavior is
|
|
|
|
// undefined, so we are not required to treat it as a constant
|
|
|
|
// expression.
|
|
|
|
if (FL->getValue().convertToInteger(IgnoredVal,
|
|
|
|
llvm::APFloat::rmTowardZero,
|
|
|
|
&Ignored) & APFloat::opInvalidOp)
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
return NoDiag();
|
|
|
|
}
|
|
|
|
}
|
2011-09-30 05:49:34 +08:00
|
|
|
switch (cast<CastExpr>(E)->getCastKind()) {
|
|
|
|
case CK_LValueToRValue:
|
2012-01-17 01:27:18 +08:00
|
|
|
case CK_AtomicToNonAtomic:
|
|
|
|
case CK_NonAtomicToAtomic:
|
2011-09-30 05:49:34 +08:00
|
|
|
case CK_NoOp:
|
|
|
|
case CK_IntegralToBoolean:
|
|
|
|
case CK_IntegralCast:
|
2010-05-07 13:32:02 +08:00
|
|
|
return CheckICE(SubExpr, Ctx);
|
2011-09-30 05:49:34 +08:00
|
|
|
default:
|
|
|
|
return ICEDiag(2, E->getLocStart());
|
|
|
|
}
|
2010-05-07 13:32:02 +08:00
|
|
|
}
|
2011-02-17 18:25:35 +08:00
|
|
|
case Expr::BinaryConditionalOperatorClass: {
|
|
|
|
const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E);
|
|
|
|
ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx);
|
|
|
|
if (CommonResult.Val == 2) return CommonResult;
|
|
|
|
ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
|
|
|
|
if (FalseResult.Val == 2) return FalseResult;
|
|
|
|
if (CommonResult.Val == 1) return CommonResult;
|
|
|
|
if (FalseResult.Val == 1 &&
|
2011-10-11 02:28:20 +08:00
|
|
|
Exp->getCommon()->EvaluateKnownConstInt(Ctx) == 0) return NoDiag();
|
2011-02-17 18:25:35 +08:00
|
|
|
return FalseResult;
|
|
|
|
}
|
2010-05-07 13:32:02 +08:00
|
|
|
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()))
|
2011-12-29 03:48:30 +08:00
|
|
|
if (CallCE->isBuiltinCall() == Builtin::BI__builtin_constant_p)
|
|
|
|
return CheckEvalInICE(E, Ctx);
|
2010-05-07 13:32:02 +08:00
|
|
|
ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
|
|
|
|
if (CondResult.Val == 2)
|
|
|
|
return CondResult;
|
2011-05-25 00:02:01 +08:00
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
|
|
|
|
ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
|
2011-05-25 00:02:01 +08:00
|
|
|
|
2010-05-07 13:32:02 +08:00
|
|
|
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.
|
2011-10-11 02:28:20 +08:00
|
|
|
if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) {
|
2010-05-07 13:32:02 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid StmtClass!");
|
2010-05-07 13:32:02 +08:00
|
|
|
}
|
|
|
|
|
2011-12-10 06:58:01 +08:00
|
|
|
/// Evaluate an expression as a C++11 integral constant expression.
|
|
|
|
static bool EvaluateCPlusPlus11IntegralConstantExpr(ASTContext &Ctx,
|
|
|
|
const Expr *E,
|
|
|
|
llvm::APSInt *Value,
|
|
|
|
SourceLocation *Loc) {
|
|
|
|
if (!E->getType()->isIntegralOrEnumerationType()) {
|
|
|
|
if (Loc) *Loc = E->getExprLoc();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-18 13:21:49 +08:00
|
|
|
APValue Result;
|
|
|
|
if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc))
|
2011-12-12 17:28:41 +08:00
|
|
|
return false;
|
|
|
|
|
2012-01-18 13:21:49 +08:00
|
|
|
assert(Result.isInt() && "pointer cast to int is not an ICE");
|
|
|
|
if (Value) *Value = Result.getInt();
|
2011-12-12 17:28:41 +08:00
|
|
|
return true;
|
2011-12-10 06:58:01 +08:00
|
|
|
}
|
|
|
|
|
2011-12-12 17:28:41 +08:00
|
|
|
bool Expr::isIntegerConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus0x)
|
2011-12-10 06:58:01 +08:00
|
|
|
return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, 0, Loc);
|
|
|
|
|
2010-05-07 13:32:02 +08:00
|
|
|
ICEDiag d = CheckICE(this, Ctx);
|
|
|
|
if (d.Val != 0) {
|
|
|
|
if (Loc) *Loc = d.Loc;
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-10 06:58:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, ASTContext &Ctx,
|
|
|
|
SourceLocation *Loc, bool isEvaluated) const {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (Ctx.getLangOpts().CPlusPlus0x)
|
2011-12-10 06:58:01 +08:00
|
|
|
return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc);
|
|
|
|
|
|
|
|
if (!isIntegerConstantExpr(Ctx, Loc))
|
|
|
|
return false;
|
|
|
|
if (!EvaluateAsInt(Value, Ctx))
|
2010-05-07 13:32:02 +08:00
|
|
|
llvm_unreachable("ICE cannot be evaluated!");
|
|
|
|
return true;
|
|
|
|
}
|
2012-01-18 13:21:49 +08:00
|
|
|
|
2012-02-15 05:38:30 +08:00
|
|
|
bool Expr::isCXX98IntegralConstantExpr(ASTContext &Ctx) const {
|
|
|
|
return CheckICE(this, Ctx).Val == 0;
|
|
|
|
}
|
|
|
|
|
2012-01-18 13:21:49 +08:00
|
|
|
bool Expr::isCXX11ConstantExpr(ASTContext &Ctx, APValue *Result,
|
|
|
|
SourceLocation *Loc) const {
|
|
|
|
// We support this checking in C++98 mode in order to diagnose compatibility
|
|
|
|
// issues.
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(Ctx.getLangOpts().CPlusPlus);
|
2012-01-18 13:21:49 +08:00
|
|
|
|
2012-02-15 05:38:30 +08:00
|
|
|
// Build evaluation settings.
|
2012-01-18 13:21:49 +08:00
|
|
|
Expr::EvalStatus Status;
|
|
|
|
llvm::SmallVector<PartialDiagnosticAt, 8> Diags;
|
|
|
|
Status.Diag = &Diags;
|
|
|
|
EvalInfo Info(Ctx, Status);
|
|
|
|
|
|
|
|
APValue Scratch;
|
|
|
|
bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch);
|
|
|
|
|
|
|
|
if (!Diags.empty()) {
|
|
|
|
IsConstExpr = false;
|
|
|
|
if (Loc) *Loc = Diags[0].first;
|
|
|
|
} else if (!IsConstExpr) {
|
|
|
|
// FIXME: This shouldn't happen.
|
|
|
|
if (Loc) *Loc = getExprLoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsConstExpr;
|
|
|
|
}
|
2012-01-27 09:14:48 +08:00
|
|
|
|
|
|
|
bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
|
|
|
|
llvm::SmallVectorImpl<
|
|
|
|
PartialDiagnosticAt> &Diags) {
|
|
|
|
// FIXME: It would be useful to check constexpr function templates, but at the
|
|
|
|
// moment the constant expression evaluator cannot cope with the non-rigorous
|
|
|
|
// ASTs which we build for dependent expressions.
|
|
|
|
if (FD->isDependentContext())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Expr::EvalStatus Status;
|
|
|
|
Status.Diag = &Diags;
|
|
|
|
|
|
|
|
EvalInfo Info(FD->getASTContext(), Status);
|
|
|
|
Info.CheckingPotentialConstantExpression = true;
|
|
|
|
|
|
|
|
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
|
|
|
|
const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : 0;
|
|
|
|
|
|
|
|
// FIXME: Fabricate an arbitrary expression on the stack and pretend that it
|
|
|
|
// is a temporary being used as the 'this' pointer.
|
|
|
|
LValue This;
|
|
|
|
ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy);
|
2012-02-15 10:18:13 +08:00
|
|
|
This.set(&VIE, Info.CurrentCall->Index);
|
2012-01-27 09:14:48 +08:00
|
|
|
|
|
|
|
ArrayRef<const Expr*> Args;
|
|
|
|
|
|
|
|
SourceLocation Loc = FD->getLocation();
|
|
|
|
|
2012-03-04 06:46:17 +08:00
|
|
|
APValue Scratch;
|
|
|
|
if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD))
|
2012-01-27 09:14:48 +08:00
|
|
|
HandleConstructorCall(Loc, This, Args, CD, Info, Scratch);
|
2012-03-04 06:46:17 +08:00
|
|
|
else
|
2012-01-27 09:14:48 +08:00
|
|
|
HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : 0,
|
|
|
|
Args, FD->getBody(), Info, Scratch);
|
|
|
|
|
|
|
|
return Diags.empty();
|
|
|
|
}
|