freebsd-dev/lib/AST/Expr.cpp

2832 lines
98 KiB
C++
Raw Normal View History

2009-06-02 17:58:47 +00:00
//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
//
// 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 class and subclasses.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/Expr.h"
2009-10-14 18:03:49 +00:00
#include "clang/AST/ExprCXX.h"
2009-06-02 17:58:47 +00:00
#include "clang/AST/APValue.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Lex/LiteralSupport.h"
#include "clang/Lex/Lexer.h"
2009-06-14 09:24:02 +00:00
#include "clang/Basic/Builtins.h"
#include "clang/Basic/SourceManager.h"
2009-06-02 17:58:47 +00:00
#include "clang/Basic/TargetInfo.h"
2009-11-04 15:04:32 +00:00
#include "llvm/Support/ErrorHandling.h"
2009-10-14 18:03:49 +00:00
#include "llvm/Support/raw_ostream.h"
2009-06-02 17:58:47 +00:00
#include <algorithm>
using namespace clang;
2010-05-04 16:12:48 +00:00
/// isKnownToHaveBooleanValue - Return true if this is an integer expression
/// that is known to return 0 or 1. This happens for _Bool/bool expressions
/// but also int expressions which are produced by things like comparisons in
/// C.
bool Expr::isKnownToHaveBooleanValue() const {
// If this value has _Bool type, it is obvious 0/1.
if (getType()->isBooleanType()) return true;
// If this is a non-scalar-integer type, we don't care enough to try.
2010-07-13 17:21:42 +00:00
if (!getType()->isIntegralOrEnumerationType()) return false;
2010-05-04 16:12:48 +00:00
if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
return PE->getSubExpr()->isKnownToHaveBooleanValue();
if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
switch (UO->getOpcode()) {
case UO_Plus:
case UO_Extension:
2010-05-04 16:12:48 +00:00
return UO->getSubExpr()->isKnownToHaveBooleanValue();
default:
return false;
}
}
2010-07-13 17:21:42 +00:00
// Only look through implicit casts. If the user writes
// '(int) (a && b)' treat it as an arbitrary int.
if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
2010-05-04 16:12:48 +00:00
return CE->getSubExpr()->isKnownToHaveBooleanValue();
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
switch (BO->getOpcode()) {
default: return false;
case BO_LT: // Relational operators.
case BO_GT:
case BO_LE:
case BO_GE:
case BO_EQ: // Equality operators.
case BO_NE:
case BO_LAnd: // AND operator.
case BO_LOr: // Logical OR operator.
2010-05-04 16:12:48 +00:00
return true;
case BO_And: // Bitwise AND operator.
case BO_Xor: // Bitwise XOR operator.
case BO_Or: // Bitwise OR operator.
2010-05-04 16:12:48 +00:00
// Handle things like (x==2)|(y==12).
return BO->getLHS()->isKnownToHaveBooleanValue() &&
BO->getRHS()->isKnownToHaveBooleanValue();
case BO_Comma:
case BO_Assign:
2010-05-04 16:12:48 +00:00
return BO->getRHS()->isKnownToHaveBooleanValue();
}
}
if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
CO->getFalseExpr()->isKnownToHaveBooleanValue();
return false;
}
// Amusing macro metaprogramming hack: check whether a class provides
// a more specific implementation of getExprLoc().
namespace {
/// This implementation is used when a class provides a custom
/// implementation of getExprLoc.
template <class E, class T>
SourceLocation getExprLocImpl(const Expr *expr,
SourceLocation (T::*v)() const) {
return static_cast<const E*>(expr)->getExprLoc();
}
/// This implementation is used when a class doesn't provide
/// a custom implementation of getExprLoc. Overload resolution
/// should pick it over the implementation above because it's
/// more specialized according to function template partial ordering.
template <class E>
SourceLocation getExprLocImpl(const Expr *expr,
SourceLocation (Expr::*v)() const) {
return static_cast<const E*>(expr)->getSourceRange().getBegin();
}
}
SourceLocation Expr::getExprLoc() const {
switch (getStmtClass()) {
case Stmt::NoStmtClass: llvm_unreachable("statement without class");
#define ABSTRACT_STMT(type)
#define STMT(type, base) \
case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
#define EXPR(type, base) \
case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
#include "clang/AST/StmtNodes.inc"
}
llvm_unreachable("unknown statement kind");
return SourceLocation();
}
2009-06-02 17:58:47 +00:00
//===----------------------------------------------------------------------===//
// Primary Expressions.
//===----------------------------------------------------------------------===//
2009-12-01 11:08:04 +00:00
void ExplicitTemplateArgumentList::initializeFrom(
const TemplateArgumentListInfo &Info) {
LAngleLoc = Info.getLAngleLoc();
RAngleLoc = Info.getRAngleLoc();
NumTemplateArgs = Info.size();
TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
for (unsigned i = 0; i != NumTemplateArgs; ++i)
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
}
void ExplicitTemplateArgumentList::initializeFrom(
const TemplateArgumentListInfo &Info,
bool &Dependent,
bool &ContainsUnexpandedParameterPack) {
LAngleLoc = Info.getLAngleLoc();
RAngleLoc = Info.getRAngleLoc();
NumTemplateArgs = Info.size();
TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
for (unsigned i = 0; i != NumTemplateArgs; ++i) {
Dependent = Dependent || Info[i].getArgument().isDependent();
ContainsUnexpandedParameterPack
= ContainsUnexpandedParameterPack ||
Info[i].getArgument().containsUnexpandedParameterPack();
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
}
}
2009-12-01 11:08:04 +00:00
void ExplicitTemplateArgumentList::copyInto(
TemplateArgumentListInfo &Info) const {
Info.setLAngleLoc(LAngleLoc);
Info.setRAngleLoc(RAngleLoc);
for (unsigned I = 0; I != NumTemplateArgs; ++I)
Info.addArgument(getTemplateArgs()[I]);
}
2010-07-13 17:21:42 +00:00
std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
return sizeof(ExplicitTemplateArgumentList) +
sizeof(TemplateArgumentLoc) * NumTemplateArgs;
}
2009-12-01 11:08:04 +00:00
std::size_t ExplicitTemplateArgumentList::sizeFor(
const TemplateArgumentListInfo &Info) {
2010-07-13 17:21:42 +00:00
return sizeFor(Info.size());
2009-12-01 11:08:04 +00:00
}
/// \brief Compute the type- and value-dependence of a declaration reference
/// based on the declaration being referenced.
static void computeDeclRefDependence(NamedDecl *D, QualType T,
bool &TypeDependent,
bool &ValueDependent) {
2009-12-01 11:08:04 +00:00
TypeDependent = false;
ValueDependent = false;
// (TD) C++ [temp.dep.expr]p3:
// An id-expression is type-dependent if it contains:
//
// and
//
// (VD) C++ [temp.dep.constexpr]p2:
// An identifier is value-dependent if it is:
2009-12-01 11:08:04 +00:00
// (TD) - an identifier that was declared with dependent type
// (VD) - a name declared with a dependent type,
if (T->isDependentType()) {
2009-12-01 11:08:04 +00:00
TypeDependent = true;
ValueDependent = true;
return;
2009-12-01 11:08:04 +00:00
}
2009-12-01 11:08:04 +00:00
// (TD) - a conversion-function-id that specifies a dependent type
if (D->getDeclName().getNameKind()
== DeclarationName::CXXConversionFunctionName &&
2009-12-01 11:08:04 +00:00
D->getDeclName().getCXXNameType()->isDependentType()) {
TypeDependent = true;
ValueDependent = true;
return;
2009-12-01 11:08:04 +00:00
}
// (VD) - the name of a non-type template parameter,
if (isa<NonTypeTemplateParmDecl>(D)) {
2009-12-01 11:08:04 +00:00
ValueDependent = true;
return;
}
2009-12-01 11:08:04 +00:00
// (VD) - a constant with integral or enumeration type and is
// initialized with an expression that is value-dependent.
if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
2010-07-13 17:21:42 +00:00
if (Var->getType()->isIntegralOrEnumerationType() &&
2010-01-23 11:10:26 +00:00
Var->getType().getCVRQualifiers() == Qualifiers::Const) {
2010-02-16 09:31:36 +00:00
if (const Expr *Init = Var->getAnyInitializer())
2010-01-23 11:10:26 +00:00
if (Init->isValueDependent())
ValueDependent = true;
2010-05-27 15:17:06 +00:00
}
2010-05-27 15:17:06 +00:00
// (VD) - FIXME: Missing from the standard:
// - a member function or a static data member of the current
// instantiation
else if (Var->isStaticDataMember() &&
Var->getDeclContext()->isDependentContext())
ValueDependent = true;
return;
}
2010-05-27 15:17:06 +00:00
// (VD) - FIXME: Missing from the standard:
// - a member function or a static data member of the current
// instantiation
if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
2010-05-27 15:17:06 +00:00
ValueDependent = true;
return;
}
}
void DeclRefExpr::computeDependence() {
bool TypeDependent = false;
bool ValueDependent = false;
computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
// (TD) C++ [temp.dep.expr]p3:
// An id-expression is type-dependent if it contains:
//
// and
//
// (VD) C++ [temp.dep.constexpr]p2:
// An identifier is value-dependent if it is:
if (!TypeDependent && !ValueDependent &&
hasExplicitTemplateArgs() &&
TemplateSpecializationType::anyDependentTemplateArguments(
getTemplateArgs(),
getNumTemplateArgs())) {
TypeDependent = true;
ValueDependent = true;
}
ExprBits.TypeDependent = TypeDependent;
ExprBits.ValueDependent = ValueDependent;
// Is the declaration a parameter pack?
if (getDecl()->isParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2009-12-01 11:08:04 +00:00
}
2009-11-04 15:04:32 +00:00
DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
SourceRange QualifierRange,
2009-12-15 18:49:47 +00:00
ValueDecl *D, SourceLocation NameLoc,
2009-12-01 11:08:04 +00:00
const TemplateArgumentListInfo *TemplateArgs,
QualType T, ExprValueKind VK)
: Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
2009-11-04 15:04:32 +00:00
DecoratedD(D,
(Qualifier? HasQualifierFlag : 0) |
2009-12-01 11:08:04 +00:00
(TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
2009-11-04 15:04:32 +00:00
Loc(NameLoc) {
if (Qualifier) {
NameQualifier *NQ = getNameQualifier();
NQ->NNS = Qualifier;
NQ->Range = QualifierRange;
}
2009-12-01 11:08:04 +00:00
if (TemplateArgs)
getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
computeDependence();
}
DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
SourceRange QualifierRange,
ValueDecl *D, const DeclarationNameInfo &NameInfo,
const TemplateArgumentListInfo *TemplateArgs,
QualType T, ExprValueKind VK)
: Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
DecoratedD(D,
(Qualifier? HasQualifierFlag : 0) |
(TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
if (Qualifier) {
NameQualifier *NQ = getNameQualifier();
NQ->NNS = Qualifier;
NQ->Range = QualifierRange;
}
if (TemplateArgs)
getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
2009-11-04 15:04:32 +00:00
2009-12-01 11:08:04 +00:00
computeDependence();
2009-11-04 15:04:32 +00:00
}
DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
NestedNameSpecifier *Qualifier,
SourceRange QualifierRange,
2009-12-15 18:49:47 +00:00
ValueDecl *D,
2009-11-04 15:04:32 +00:00
SourceLocation NameLoc,
2009-12-01 11:08:04 +00:00
QualType T,
ExprValueKind VK,
2009-12-01 11:08:04 +00:00
const TemplateArgumentListInfo *TemplateArgs) {
return Create(Context, Qualifier, QualifierRange, D,
DeclarationNameInfo(D->getDeclName(), NameLoc),
T, VK, TemplateArgs);
}
DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
NestedNameSpecifier *Qualifier,
SourceRange QualifierRange,
ValueDecl *D,
const DeclarationNameInfo &NameInfo,
QualType T,
ExprValueKind VK,
const TemplateArgumentListInfo *TemplateArgs) {
2009-11-04 15:04:32 +00:00
std::size_t Size = sizeof(DeclRefExpr);
if (Qualifier != 0)
Size += sizeof(NameQualifier);
2009-12-01 11:08:04 +00:00
if (TemplateArgs)
Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
2009-11-04 15:04:32 +00:00
void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
TemplateArgs, T, VK);
2009-11-04 15:04:32 +00:00
}
DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
bool HasQualifier,
bool HasExplicitTemplateArgs,
2010-07-13 17:21:42 +00:00
unsigned NumTemplateArgs) {
std::size_t Size = sizeof(DeclRefExpr);
if (HasQualifier)
Size += sizeof(NameQualifier);
if (HasExplicitTemplateArgs)
2010-07-13 17:21:42 +00:00
Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
2010-07-13 17:21:42 +00:00
return new (Mem) DeclRefExpr(EmptyShell());
}
2009-11-04 15:04:32 +00:00
SourceRange DeclRefExpr::getSourceRange() const {
SourceRange R = getNameInfo().getSourceRange();
2009-11-04 15:04:32 +00:00
if (hasQualifier())
R.setBegin(getQualifierRange().getBegin());
if (hasExplicitTemplateArgs())
2009-11-04 15:04:32 +00:00
R.setEnd(getRAngleLoc());
return R;
}
2009-10-14 18:03:49 +00:00
// FIXME: Maybe this should use DeclPrinter with a special "print predefined
// expr" policy instead.
2010-02-16 09:31:36 +00:00
std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
ASTContext &Context = CurrentDecl->getASTContext();
2009-10-14 18:03:49 +00:00
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
2010-02-16 09:31:36 +00:00
if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
2009-10-14 18:03:49 +00:00
return FD->getNameAsString();
2009-06-02 17:58:47 +00:00
2009-10-14 18:03:49 +00:00
llvm::SmallString<256> Name;
llvm::raw_svector_ostream Out(Name);
2009-06-02 17:58:47 +00:00
2009-10-14 18:03:49 +00:00
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
2010-02-16 09:31:36 +00:00
if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
2009-10-14 18:03:49 +00:00
Out << "virtual ";
2010-01-01 10:34:51 +00:00
if (MD->isStatic())
Out << "static ";
2009-10-14 18:03:49 +00:00
}
2009-06-02 17:58:47 +00:00
2009-10-14 18:03:49 +00:00
PrintingPolicy Policy(Context.getLangOptions());
2009-06-02 17:58:47 +00:00
2009-10-14 18:03:49 +00:00
std::string Proto = FD->getQualifiedNameAsString(Policy);
const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
const FunctionProtoType *FT = 0;
if (FD->hasWrittenPrototype())
FT = dyn_cast<FunctionProtoType>(AFT);
Proto += "(";
if (FT) {
llvm::raw_string_ostream POut(Proto);
for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
if (i) POut << ", ";
std::string Param;
FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
POut << Param;
}
if (FT->isVariadic()) {
if (FD->getNumParams()) POut << ", ";
POut << "...";
}
}
Proto += ")";
2010-01-01 10:34:51 +00:00
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
if (ThisQuals.hasConst())
Proto += " const";
if (ThisQuals.hasVolatile())
Proto += " volatile";
}
2009-12-15 18:49:47 +00:00
if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
AFT->getResultType().getAsStringInternal(Proto, Policy);
2009-10-14 18:03:49 +00:00
Out << Proto;
Out.flush();
return Name.str().str();
}
if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
llvm::SmallString<256> Name;
llvm::raw_svector_ostream Out(Name);
Out << (MD->isInstanceMethod() ? '-' : '+');
Out << '[';
2010-03-21 10:50:08 +00:00
// For incorrect code, there might not be an ObjCInterfaceDecl. Do
// a null check to avoid a crash.
if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
2010-05-04 16:12:48 +00:00
Out << ID;
2010-03-21 10:50:08 +00:00
2009-10-14 18:03:49 +00:00
if (const ObjCCategoryImplDecl *CID =
2010-05-04 16:12:48 +00:00
dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Out << '(' << CID << ')';
2009-10-14 18:03:49 +00:00
Out << ' ';
Out << MD->getSelector().getAsString();
Out << ']';
2009-06-02 17:58:47 +00:00
2009-10-14 18:03:49 +00:00
Out.flush();
return Name.str().str();
}
if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
// __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
return "top level";
}
return "";
2009-06-02 17:58:47 +00:00
}
void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
if (hasAllocation())
C.Deallocate(pVal);
BitWidth = Val.getBitWidth();
unsigned NumWords = Val.getNumWords();
const uint64_t* Words = Val.getRawData();
if (NumWords > 1) {
pVal = new (C) uint64_t[NumWords];
std::copy(Words, Words + NumWords, pVal);
} else if (NumWords == 1)
VAL = Words[0];
else
VAL = 0;
}
IntegerLiteral *
IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
QualType type, SourceLocation l) {
return new (C) IntegerLiteral(C, V, type, l);
}
IntegerLiteral *
IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
return new (C) IntegerLiteral(Empty);
}
FloatingLiteral *
FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
bool isexact, QualType Type, SourceLocation L) {
return new (C) FloatingLiteral(C, V, isexact, Type, L);
}
FloatingLiteral *
FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
return new (C) FloatingLiteral(Empty);
}
2009-06-02 17:58:47 +00:00
/// getValueAsApproximateDouble - This returns the value as an inaccurate
/// double. Note that this may cause loss of precision, but is useful for
/// debugging dumps, etc.
double FloatingLiteral::getValueAsApproximateDouble() const {
llvm::APFloat V = getValue();
bool ignored;
V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
&ignored);
return V.convertToDouble();
}
StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
unsigned ByteLength, bool Wide,
QualType Ty,
2009-10-14 18:03:49 +00:00
const SourceLocation *Loc,
2009-06-02 17:58:47 +00:00
unsigned NumStrs) {
// Allocate enough space for the StringLiteral plus an array of locations for
// any concatenated string tokens.
void *Mem = C.Allocate(sizeof(StringLiteral)+
sizeof(SourceLocation)*(NumStrs-1),
llvm::alignOf<StringLiteral>());
2009-06-02 17:58:47 +00:00
StringLiteral *SL = new (Mem) StringLiteral(Ty);
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
// OPTIMIZE: could allocate this appended to the StringLiteral.
char *AStrData = new (C, 1) char[ByteLength];
memcpy(AStrData, StrData, ByteLength);
SL->StrData = AStrData;
SL->ByteLength = ByteLength;
SL->IsWide = Wide;
SL->TokLocs[0] = Loc[0];
SL->NumConcatenated = NumStrs;
if (NumStrs != 1)
memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
return SL;
}
StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
void *Mem = C.Allocate(sizeof(StringLiteral)+
sizeof(SourceLocation)*(NumStrs-1),
llvm::alignOf<StringLiteral>());
2009-06-02 17:58:47 +00:00
StringLiteral *SL = new (Mem) StringLiteral(QualType());
SL->StrData = 0;
SL->ByteLength = 0;
SL->NumConcatenated = NumStrs;
return SL;
}
2009-10-14 18:03:49 +00:00
void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
char *AStrData = new (C, 1) char[Str.size()];
memcpy(AStrData, Str.data(), Str.size());
2009-06-02 17:58:47 +00:00
StrData = AStrData;
2009-10-14 18:03:49 +00:00
ByteLength = Str.size();
2009-06-02 17:58:47 +00:00
}
/// getLocationOfByte - Return a source location that points to the specified
/// byte of this string literal.
///
/// Strings are amazingly complex. They can be formed from multiple tokens and
/// can have escape sequences in them in addition to the usual trigraph and
/// escaped newline business. This routine handles this complexity.
///
SourceLocation StringLiteral::
getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
const LangOptions &Features, const TargetInfo &Target) const {
assert(!isWide() && "This doesn't work for wide strings yet");
// Loop over all of the tokens in this string until we find the one that
// contains the byte we're looking for.
unsigned TokNo = 0;
while (1) {
assert(TokNo < getNumConcatenated() && "Invalid byte number!");
SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
// Get the spelling of the string so that we can get the data that makes up
// the string literal, not the identifier for the macro it is potentially
// expanded through.
SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
// Re-lex the token to get its length and original spelling.
std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
bool Invalid = false;
llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
if (Invalid)
return StrTokSpellingLoc;
const char *StrData = Buffer.data()+LocInfo.second;
// Create a langops struct and enable trigraphs. This is sufficient for
// relexing tokens.
LangOptions LangOpts;
LangOpts.Trigraphs = true;
// Create a lexer starting at the beginning of this token.
Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
Buffer.end());
Token TheTok;
TheLexer.LexFromRawLexer(TheTok);
// Use the StringLiteralParser to compute the length of the string in bytes.
StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
unsigned TokNumBytes = SLP.GetStringLength();
// If the byte is in this token, return the location of the byte.
if (ByteNo < TokNumBytes ||
(ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
// Now that we know the offset of the token in the spelling, use the
// preprocessor to get the offset in the original source.
return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
}
// Move to the next string token.
++TokNo;
ByteNo -= TokNumBytes;
}
}
2009-06-02 17:58:47 +00:00
/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
/// corresponds to, e.g. "sizeof" or "[pre]++".
const char *UnaryOperator::getOpcodeStr(Opcode Op) {
switch (Op) {
default: assert(0 && "Unknown unary operator");
case UO_PostInc: return "++";
case UO_PostDec: return "--";
case UO_PreInc: return "++";
case UO_PreDec: return "--";
case UO_AddrOf: return "&";
case UO_Deref: return "*";
case UO_Plus: return "+";
case UO_Minus: return "-";
case UO_Not: return "~";
case UO_LNot: return "!";
case UO_Real: return "__real";
case UO_Imag: return "__imag";
case UO_Extension: return "__extension__";
2009-06-02 17:58:47 +00:00
}
}
UnaryOperatorKind
2009-06-02 17:58:47 +00:00
UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
switch (OO) {
default: assert(false && "No unary operator for overloaded function");
case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
case OO_Amp: return UO_AddrOf;
case OO_Star: return UO_Deref;
case OO_Plus: return UO_Plus;
case OO_Minus: return UO_Minus;
case OO_Tilde: return UO_Not;
case OO_Exclaim: return UO_LNot;
2009-06-02 17:58:47 +00:00
}
}
OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
switch (Opc) {
case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
case UO_AddrOf: return OO_Amp;
case UO_Deref: return OO_Star;
case UO_Plus: return OO_Plus;
case UO_Minus: return OO_Minus;
case UO_Not: return OO_Tilde;
case UO_LNot: return OO_Exclaim;
2009-06-02 17:58:47 +00:00
default: return OO_None;
}
}
//===----------------------------------------------------------------------===//
// Postfix Operators.
//===----------------------------------------------------------------------===//
CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
SourceLocation rparenloc)
: Expr(SC, t, VK, OK_Ordinary,
fn->isTypeDependent(),
fn->isValueDependent(),
fn->containsUnexpandedParameterPack()),
2009-06-02 17:58:47 +00:00
NumArgs(numargs) {
2009-10-14 18:03:49 +00:00
SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
2009-06-02 17:58:47 +00:00
SubExprs[FN] = fn;
for (unsigned i = 0; i != numargs; ++i) {
if (args[i]->isTypeDependent())
ExprBits.TypeDependent = true;
if (args[i]->isValueDependent())
ExprBits.ValueDependent = true;
if (args[i]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
}
2009-06-02 17:58:47 +00:00
CallExprBits.NumPreArgs = NumPreArgs;
2009-06-02 17:58:47 +00:00
RParenLoc = rparenloc;
}
CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
QualType t, ExprValueKind VK, SourceLocation rparenloc)
: Expr(CallExprClass, t, VK, OK_Ordinary,
fn->isTypeDependent(),
fn->isValueDependent(),
fn->containsUnexpandedParameterPack()),
2009-06-02 17:58:47 +00:00
NumArgs(numargs) {
SubExprs = new (C) Stmt*[numargs+PREARGS_START];
2009-06-02 17:58:47 +00:00
SubExprs[FN] = fn;
for (unsigned i = 0; i != numargs; ++i) {
if (args[i]->isTypeDependent())
ExprBits.TypeDependent = true;
if (args[i]->isValueDependent())
ExprBits.ValueDependent = true;
if (args[i]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
SubExprs[i+PREARGS_START] = args[i];
}
2009-06-02 17:58:47 +00:00
CallExprBits.NumPreArgs = 0;
2009-06-02 17:58:47 +00:00
RParenLoc = rparenloc;
}
2009-10-14 18:03:49 +00:00
CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
: Expr(SC, Empty), SubExprs(0), NumArgs(0) {
// FIXME: Why do we allocate this?
SubExprs = new (C) Stmt*[PREARGS_START];
CallExprBits.NumPreArgs = 0;
}
CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
EmptyShell Empty)
: Expr(SC, Empty), SubExprs(0), NumArgs(0) {
// FIXME: Why do we allocate this?
SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
CallExprBits.NumPreArgs = NumPreArgs;
2009-06-02 17:58:47 +00:00
}
2010-01-01 10:34:51 +00:00
Decl *CallExpr::getCalleeDecl() {
2009-10-14 18:03:49 +00:00
Expr *CEE = getCallee()->IgnoreParenCasts();
// If we're calling a dereference, look at the pointer instead.
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
if (BO->isPtrMemOp())
CEE = BO->getRHS()->IgnoreParenCasts();
} else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
if (UO->getOpcode() == UO_Deref)
CEE = UO->getSubExpr()->IgnoreParenCasts();
}
2009-10-14 18:03:49 +00:00
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
2010-01-01 10:34:51 +00:00
return DRE->getDecl();
if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
return ME->getMemberDecl();
2009-10-14 18:03:49 +00:00
return 0;
}
2010-01-01 10:34:51 +00:00
FunctionDecl *CallExpr::getDirectCallee() {
return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
}
2009-06-02 17:58:47 +00:00
/// setNumArgs - This changes the number of arguments present in this call.
/// Any orphaned expressions are deleted by this, and any new operands are set
/// to null.
void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
// No change, just return.
if (NumArgs == getNumArgs()) return;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
// If shrinking # arguments, just delete the extras and forgot them.
if (NumArgs < getNumArgs()) {
this->NumArgs = NumArgs;
return;
}
// Otherwise, we are growing the # arguments. New an bigger argument array.
unsigned NumPreArgs = getNumPreArgs();
Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
2009-06-02 17:58:47 +00:00
// Copy over args.
for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
2009-06-02 17:58:47 +00:00
NewSubExprs[i] = SubExprs[i];
// Null out new args.
for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
i != NumArgs+PREARGS_START+NumPreArgs; ++i)
2009-06-02 17:58:47 +00:00
NewSubExprs[i] = 0;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
if (SubExprs) C.Deallocate(SubExprs);
SubExprs = NewSubExprs;
this->NumArgs = NumArgs;
}
/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
/// not, return 0.
unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
2009-06-02 17:58:47 +00:00
// All simple function calls (e.g. func()) are implicitly cast to pointer to
2009-10-14 18:03:49 +00:00
// function. As a result, we try and obtain the DeclRefExpr from the
2009-06-02 17:58:47 +00:00
// ImplicitCastExpr.
const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
return 0;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
if (!DRE)
return 0;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
if (!FDecl)
return 0;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
if (!FDecl->getIdentifier())
return 0;
2009-10-14 18:03:49 +00:00
return FDecl->getBuiltinID();
2009-06-02 17:58:47 +00:00
}
QualType CallExpr::getCallReturnType() const {
QualType CalleeType = getCallee()->getType();
2009-10-14 18:03:49 +00:00
if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
2009-06-02 17:58:47 +00:00
CalleeType = FnTypePtr->getPointeeType();
2009-10-14 18:03:49 +00:00
else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
2009-06-02 17:58:47 +00:00
CalleeType = BPT->getPointeeType();
2010-07-13 17:21:42 +00:00
else if (const MemberPointerType *MPT
= CalleeType->getAs<MemberPointerType>())
CalleeType = MPT->getPointeeType();
2009-10-14 18:03:49 +00:00
const FunctionType *FnType = CalleeType->getAs<FunctionType>();
2009-06-02 17:58:47 +00:00
return FnType->getResultType();
}
2010-05-04 16:12:48 +00:00
OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
SourceLocation OperatorLoc,
TypeSourceInfo *tsi,
OffsetOfNode* compsPtr, unsigned numComps,
Expr** exprsPtr, unsigned numExprs,
SourceLocation RParenLoc) {
void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
sizeof(OffsetOfNode) * numComps +
sizeof(Expr*) * numExprs);
return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
exprsPtr, numExprs, RParenLoc);
}
OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
unsigned numComps, unsigned numExprs) {
void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
sizeof(OffsetOfNode) * numComps +
sizeof(Expr*) * numExprs);
return new (Mem) OffsetOfExpr(numComps, numExprs);
}
OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
SourceLocation OperatorLoc, TypeSourceInfo *tsi,
OffsetOfNode* compsPtr, unsigned numComps,
Expr** exprsPtr, unsigned numExprs,
SourceLocation RParenLoc)
: Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
/*TypeDependent=*/false,
/*ValueDependent=*/tsi->getType()->isDependentType(),
tsi->getType()->containsUnexpandedParameterPack()),
2010-05-04 16:12:48 +00:00
OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
NumComps(numComps), NumExprs(numExprs)
{
for(unsigned i = 0; i < numComps; ++i) {
setComponent(i, compsPtr[i]);
}
for(unsigned i = 0; i < numExprs; ++i) {
if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
ExprBits.ValueDependent = true;
if (exprsPtr[i]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2010-05-04 16:12:48 +00:00
setIndexExpr(i, exprsPtr[i]);
}
}
IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
assert(getKind() == Field || getKind() == Identifier);
if (getKind() == Field)
return getField()->getIdentifier();
return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
}
2009-10-14 18:03:49 +00:00
MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
NestedNameSpecifier *qual,
SourceRange qualrange,
2009-12-15 18:49:47 +00:00
ValueDecl *memberdecl,
2010-05-04 16:12:48 +00:00
DeclAccessPair founddecl,
DeclarationNameInfo nameinfo,
2009-12-01 11:08:04 +00:00
const TemplateArgumentListInfo *targs,
QualType ty,
ExprValueKind vk,
ExprObjectKind ok) {
2009-10-14 18:03:49 +00:00
std::size_t Size = sizeof(MemberExpr);
2010-04-02 08:55:10 +00:00
2010-05-04 16:12:48 +00:00
bool hasQualOrFound = (qual != 0 ||
founddecl.getDecl() != memberdecl ||
founddecl.getAccess() != memberdecl->getAccess());
2010-04-02 08:55:10 +00:00
if (hasQualOrFound)
Size += sizeof(MemberNameQualifier);
2009-10-14 18:03:49 +00:00
2009-12-01 11:08:04 +00:00
if (targs)
Size += ExplicitTemplateArgumentList::sizeFor(*targs);
2009-10-14 18:03:49 +00:00
void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
ty, vk, ok);
2010-04-02 08:55:10 +00:00
if (hasQualOrFound) {
if (qual && qual->isDependent()) {
E->setValueDependent(true);
E->setTypeDependent(true);
}
E->HasQualifierOrFoundDecl = true;
MemberNameQualifier *NQ = E->getMemberQualifier();
NQ->NNS = qual;
NQ->Range = qualrange;
NQ->FoundDecl = founddecl;
}
if (targs) {
E->HasExplicitTemplateArgumentList = true;
E->getExplicitTemplateArgs().initializeFrom(*targs);
2010-04-02 08:55:10 +00:00
}
return E;
2009-10-14 18:03:49 +00:00
}
const char *CastExpr::getCastKindName() const {
switch (getCastKind()) {
case CK_Dependent:
return "Dependent";
case CK_BitCast:
2009-10-14 18:03:49 +00:00
return "BitCast";
case CK_LValueBitCast:
2010-07-15 17:07:12 +00:00
return "LValueBitCast";
case CK_LValueToRValue:
return "LValueToRValue";
case CK_GetObjCProperty:
return "GetObjCProperty";
case CK_NoOp:
2009-10-14 18:03:49 +00:00
return "NoOp";
case CK_BaseToDerived:
2009-11-18 14:59:57 +00:00
return "BaseToDerived";
case CK_DerivedToBase:
2009-10-14 18:03:49 +00:00
return "DerivedToBase";
case CK_UncheckedDerivedToBase:
2010-04-02 08:55:10 +00:00
return "UncheckedDerivedToBase";
case CK_Dynamic:
2009-10-14 18:03:49 +00:00
return "Dynamic";
case CK_ToUnion:
2009-10-14 18:03:49 +00:00
return "ToUnion";
case CK_ArrayToPointerDecay:
2009-10-14 18:03:49 +00:00
return "ArrayToPointerDecay";
case CK_FunctionToPointerDecay:
2009-10-14 18:03:49 +00:00
return "FunctionToPointerDecay";
case CK_NullToMemberPointer:
2009-10-14 18:03:49 +00:00
return "NullToMemberPointer";
case CK_NullToPointer:
return "NullToPointer";
case CK_BaseToDerivedMemberPointer:
2009-10-14 18:03:49 +00:00
return "BaseToDerivedMemberPointer";
case CK_DerivedToBaseMemberPointer:
2009-11-04 15:04:32 +00:00
return "DerivedToBaseMemberPointer";
case CK_UserDefinedConversion:
2009-10-14 18:03:49 +00:00
return "UserDefinedConversion";
case CK_ConstructorConversion:
2009-10-14 18:03:49 +00:00
return "ConstructorConversion";
case CK_IntegralToPointer:
2009-10-14 18:03:49 +00:00
return "IntegralToPointer";
case CK_PointerToIntegral:
2009-10-14 18:03:49 +00:00
return "PointerToIntegral";
case CK_PointerToBoolean:
return "PointerToBoolean";
case CK_ToVoid:
2009-10-23 14:22:18 +00:00
return "ToVoid";
case CK_VectorSplat:
2009-10-23 14:22:18 +00:00
return "VectorSplat";
case CK_IntegralCast:
2009-10-23 14:22:18 +00:00
return "IntegralCast";
case CK_IntegralToBoolean:
return "IntegralToBoolean";
case CK_IntegralToFloating:
2009-10-23 14:22:18 +00:00
return "IntegralToFloating";
case CK_FloatingToIntegral:
2009-10-23 14:22:18 +00:00
return "FloatingToIntegral";
case CK_FloatingCast:
2009-10-23 14:22:18 +00:00
return "FloatingCast";
case CK_FloatingToBoolean:
return "FloatingToBoolean";
case CK_MemberPointerToBoolean:
2009-12-01 11:08:04 +00:00
return "MemberPointerToBoolean";
case CK_AnyPointerToObjCPointerCast:
2009-12-15 18:49:47 +00:00
return "AnyPointerToObjCPointerCast";
case CK_AnyPointerToBlockPointerCast:
2009-12-15 18:49:47 +00:00
return "AnyPointerToBlockPointerCast";
case CK_ObjCObjectLValueCast:
return "ObjCObjectLValueCast";
case CK_FloatingRealToComplex:
return "FloatingRealToComplex";
case CK_FloatingComplexToReal:
return "FloatingComplexToReal";
case CK_FloatingComplexToBoolean:
return "FloatingComplexToBoolean";
case CK_FloatingComplexCast:
return "FloatingComplexCast";
case CK_FloatingComplexToIntegralComplex:
return "FloatingComplexToIntegralComplex";
case CK_IntegralRealToComplex:
return "IntegralRealToComplex";
case CK_IntegralComplexToReal:
return "IntegralComplexToReal";
case CK_IntegralComplexToBoolean:
return "IntegralComplexToBoolean";
case CK_IntegralComplexCast:
return "IntegralComplexCast";
case CK_IntegralComplexToFloatingComplex:
return "IntegralComplexToFloatingComplex";
2009-10-14 18:03:49 +00:00
}
llvm_unreachable("Unhandled cast kind!");
2009-10-14 18:03:49 +00:00
return 0;
}
2009-12-15 18:49:47 +00:00
Expr *CastExpr::getSubExprAsWritten() {
Expr *SubExpr = 0;
CastExpr *E = this;
do {
SubExpr = E->getSubExpr();
// Skip any temporary bindings; they're implicit.
if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
SubExpr = Binder->getSubExpr();
// Conversions by constructor and conversion functions have a
// subexpression describing the call; strip it off.
if (E->getCastKind() == CK_ConstructorConversion)
2009-12-15 18:49:47 +00:00
SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
else if (E->getCastKind() == CK_UserDefinedConversion)
2009-12-15 18:49:47 +00:00
SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
// If the subexpression we're left with is an implicit cast, look
// through that, too.
} while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
return SubExpr;
}
CXXBaseSpecifier **CastExpr::path_buffer() {
switch (getStmtClass()) {
#define ABSTRACT_STMT(x)
#define CASTEXPR(Type, Base) \
case Stmt::Type##Class: \
return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
#define STMT(Type, Base)
#include "clang/AST/StmtNodes.inc"
default:
llvm_unreachable("non-cast expressions not possible here");
return 0;
}
}
void CastExpr::setCastPath(const CXXCastPath &Path) {
assert(Path.size() == path_size());
memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
}
ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
CastKind Kind, Expr *Operand,
const CXXCastPath *BasePath,
ExprValueKind VK) {
unsigned PathSize = (BasePath ? BasePath->size() : 0);
void *Buffer =
C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
ImplicitCastExpr *E =
new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
if (PathSize) E->setCastPath(*BasePath);
return E;
}
ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
unsigned PathSize) {
void *Buffer =
C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
}
CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
ExprValueKind VK, CastKind K, Expr *Op,
const CXXCastPath *BasePath,
TypeSourceInfo *WrittenTy,
SourceLocation L, SourceLocation R) {
unsigned PathSize = (BasePath ? BasePath->size() : 0);
void *Buffer =
C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
CStyleCastExpr *E =
new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
if (PathSize) E->setCastPath(*BasePath);
return E;
}
CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
void *Buffer =
C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
}
2009-06-02 17:58:47 +00:00
/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
/// corresponds to, e.g. "<<=".
const char *BinaryOperator::getOpcodeStr(Opcode Op) {
switch (Op) {
case BO_PtrMemD: return ".*";
case BO_PtrMemI: return "->*";
case BO_Mul: return "*";
case BO_Div: return "/";
case BO_Rem: return "%";
case BO_Add: return "+";
case BO_Sub: return "-";
case BO_Shl: return "<<";
case BO_Shr: return ">>";
case BO_LT: return "<";
case BO_GT: return ">";
case BO_LE: return "<=";
case BO_GE: return ">=";
case BO_EQ: return "==";
case BO_NE: return "!=";
case BO_And: return "&";
case BO_Xor: return "^";
case BO_Or: return "|";
case BO_LAnd: return "&&";
case BO_LOr: return "||";
case BO_Assign: return "=";
case BO_MulAssign: return "*=";
case BO_DivAssign: return "/=";
case BO_RemAssign: return "%=";
case BO_AddAssign: return "+=";
case BO_SubAssign: return "-=";
case BO_ShlAssign: return "<<=";
case BO_ShrAssign: return ">>=";
case BO_AndAssign: return "&=";
case BO_XorAssign: return "^=";
case BO_OrAssign: return "|=";
case BO_Comma: return ",";
2009-06-02 17:58:47 +00:00
}
return "";
}
BinaryOperatorKind
2009-06-02 17:58:47 +00:00
BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
switch (OO) {
default: assert(false && "Not an overloadable binary operator");
case OO_Plus: return BO_Add;
case OO_Minus: return BO_Sub;
case OO_Star: return BO_Mul;
case OO_Slash: return BO_Div;
case OO_Percent: return BO_Rem;
case OO_Caret: return BO_Xor;
case OO_Amp: return BO_And;
case OO_Pipe: return BO_Or;
case OO_Equal: return BO_Assign;
case OO_Less: return BO_LT;
case OO_Greater: return BO_GT;
case OO_PlusEqual: return BO_AddAssign;
case OO_MinusEqual: return BO_SubAssign;
case OO_StarEqual: return BO_MulAssign;
case OO_SlashEqual: return BO_DivAssign;
case OO_PercentEqual: return BO_RemAssign;
case OO_CaretEqual: return BO_XorAssign;
case OO_AmpEqual: return BO_AndAssign;
case OO_PipeEqual: return BO_OrAssign;
case OO_LessLess: return BO_Shl;
case OO_GreaterGreater: return BO_Shr;
case OO_LessLessEqual: return BO_ShlAssign;
case OO_GreaterGreaterEqual: return BO_ShrAssign;
case OO_EqualEqual: return BO_EQ;
case OO_ExclaimEqual: return BO_NE;
case OO_LessEqual: return BO_LE;
case OO_GreaterEqual: return BO_GE;
case OO_AmpAmp: return BO_LAnd;
case OO_PipePipe: return BO_LOr;
case OO_Comma: return BO_Comma;
case OO_ArrowStar: return BO_PtrMemI;
2009-06-02 17:58:47 +00:00
}
}
OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
static const OverloadedOperatorKind OverOps[] = {
/* .* Cannot be overloaded */OO_None, OO_ArrowStar,
OO_Star, OO_Slash, OO_Percent,
OO_Plus, OO_Minus,
OO_LessLess, OO_GreaterGreater,
OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
OO_EqualEqual, OO_ExclaimEqual,
OO_Amp,
OO_Caret,
OO_Pipe,
OO_AmpAmp,
OO_PipePipe,
OO_Equal, OO_StarEqual,
OO_SlashEqual, OO_PercentEqual,
OO_PlusEqual, OO_MinusEqual,
OO_LessLessEqual, OO_GreaterGreaterEqual,
OO_AmpEqual, OO_CaretEqual,
OO_PipeEqual,
OO_Comma
};
return OverOps[Opc];
}
2010-05-04 16:12:48 +00:00
InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
2009-06-02 17:58:47 +00:00
Expr **initExprs, unsigned numInits,
SourceLocation rbraceloc)
: Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
false),
2010-05-04 16:12:48 +00:00
InitExprs(C, numInits),
2009-10-14 18:03:49 +00:00
LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
2009-12-01 11:08:04 +00:00
UnionFieldInit(0), HadArrayRangeDesignator(false)
{
for (unsigned I = 0; I != numInits; ++I) {
if (initExprs[I]->isTypeDependent())
ExprBits.TypeDependent = true;
2009-12-01 11:08:04 +00:00
if (initExprs[I]->isValueDependent())
ExprBits.ValueDependent = true;
if (initExprs[I]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2009-12-01 11:08:04 +00:00
}
2010-05-04 16:12:48 +00:00
InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
2009-06-02 17:58:47 +00:00
}
2010-05-04 16:12:48 +00:00
void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
2009-06-02 17:58:47 +00:00
if (NumInits > InitExprs.size())
2010-05-04 16:12:48 +00:00
InitExprs.reserve(C, NumInits);
2009-06-02 17:58:47 +00:00
}
2010-05-04 16:12:48 +00:00
void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
InitExprs.resize(C, NumInits, 0);
2009-06-02 17:58:47 +00:00
}
2010-05-04 16:12:48 +00:00
Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
2009-06-02 17:58:47 +00:00
if (Init >= InitExprs.size()) {
2010-05-04 16:12:48 +00:00
InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
2009-06-02 17:58:47 +00:00
InitExprs.back() = expr;
return 0;
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
InitExprs[Init] = expr;
return Result;
}
SourceRange InitListExpr::getSourceRange() const {
if (SyntacticForm)
return SyntacticForm->getSourceRange();
SourceLocation Beg = LBraceLoc, End = RBraceLoc;
if (Beg.isInvalid()) {
// Find the first non-null initializer.
for (InitExprsTy::const_iterator I = InitExprs.begin(),
E = InitExprs.end();
I != E; ++I) {
if (Stmt *S = *I) {
Beg = S->getLocStart();
break;
}
}
}
if (End.isInvalid()) {
// Find the first non-null initializer from the end.
for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
E = InitExprs.rend();
I != E; ++I) {
if (Stmt *S = *I) {
End = S->getSourceRange().getEnd();
break;
}
}
}
return SourceRange(Beg, End);
}
2009-06-02 17:58:47 +00:00
/// getFunctionType - Return the underlying function type for this block.
///
const FunctionType *BlockExpr::getFunctionType() const {
2009-10-14 18:03:49 +00:00
return getType()->getAs<BlockPointerType>()->
getPointeeType()->getAs<FunctionType>();
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
SourceLocation BlockExpr::getCaretLocation() const {
return TheBlock->getCaretLocation();
2009-06-02 17:58:47 +00:00
}
2009-10-14 18:03:49 +00:00
const Stmt *BlockExpr::getBody() const {
2009-06-02 17:58:47 +00:00
return TheBlock->getBody();
}
2009-10-14 18:03:49 +00:00
Stmt *BlockExpr::getBody() {
return TheBlock->getBody();
2009-06-02 17:58:47 +00:00
}
//===----------------------------------------------------------------------===//
// Generic Expression Routines
//===----------------------------------------------------------------------===//
/// isUnusedResultAWarning - Return true if this immediate expression should
/// be warned about if the result is unused. If so, fill in Loc and Ranges
/// with location to warn on and the source range[s] to report with the
/// warning.
bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
2009-11-04 15:04:32 +00:00
SourceRange &R2, ASTContext &Ctx) const {
2009-06-02 17:58:47 +00:00
// Don't warn if the expr is type dependent. The type could end up
// instantiating to void.
if (isTypeDependent())
return false;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
switch (getStmtClass()) {
default:
2010-03-16 16:52:15 +00:00
if (getType()->isVoidType())
return false;
2009-06-02 17:58:47 +00:00
Loc = getExprLoc();
R1 = getSourceRange();
return true;
case ParenExprClass:
return cast<ParenExpr>(this)->getSubExpr()->
2009-11-04 15:04:32 +00:00
isUnusedResultAWarning(Loc, R1, R2, Ctx);
2009-06-02 17:58:47 +00:00
case UnaryOperatorClass: {
const UnaryOperator *UO = cast<UnaryOperator>(this);
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
switch (UO->getOpcode()) {
default: break;
case UO_PostInc:
case UO_PostDec:
case UO_PreInc:
case UO_PreDec: // ++/--
2009-06-02 17:58:47 +00:00
return false; // Not a warning.
case UO_Deref:
2009-06-02 17:58:47 +00:00
// Dereferencing a volatile pointer is a side-effect.
2009-11-04 15:04:32 +00:00
if (Ctx.getCanonicalType(getType()).isVolatileQualified())
2009-06-02 17:58:47 +00:00
return false;
break;
case UO_Real:
case UO_Imag:
2009-06-02 17:58:47 +00:00
// accessing a piece of a volatile complex is a side-effect.
2009-11-04 15:04:32 +00:00
if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
.isVolatileQualified())
2009-06-02 17:58:47 +00:00
return false;
break;
case UO_Extension:
2009-11-04 15:04:32 +00:00
return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
2009-06-02 17:58:47 +00:00
}
Loc = UO->getOperatorLoc();
R1 = UO->getSubExpr()->getSourceRange();
return true;
}
case BinaryOperatorClass: {
const BinaryOperator *BO = cast<BinaryOperator>(this);
2010-05-04 16:12:48 +00:00
switch (BO->getOpcode()) {
default:
break;
2010-07-13 17:21:42 +00:00
// Consider the RHS of comma for side effects. LHS was checked by
// Sema::CheckCommaOperands.
case BO_Comma:
2010-05-04 16:12:48 +00:00
// ((foo = <blah>), 0) is an idiom for hiding the result (and
// lvalue-ness) of an assignment written in a macro.
if (IntegerLiteral *IE =
dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
if (IE->getValue() == 0)
return false;
2010-07-13 17:21:42 +00:00
return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
// Consider '||', '&&' to have side effects if the LHS or RHS does.
case BO_LAnd:
case BO_LOr:
2010-07-13 17:21:42 +00:00
if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
!BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
return false;
break;
2010-02-16 09:31:36 +00:00
}
2009-06-02 17:58:47 +00:00
if (BO->isAssignmentOp())
return false;
Loc = BO->getOperatorLoc();
R1 = BO->getLHS()->getSourceRange();
R2 = BO->getRHS()->getSourceRange();
return true;
}
case CompoundAssignOperatorClass:
2010-05-27 15:17:06 +00:00
case VAArgExprClass:
2009-06-02 17:58:47 +00:00
return false;
case ConditionalOperatorClass: {
// The condition must be evaluated, but if either the LHS or RHS is a
// warning, warn about them.
const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
2009-10-14 18:03:49 +00:00
if (Exp->getLHS() &&
2009-11-04 15:04:32 +00:00
Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
2009-06-02 17:58:47 +00:00
return true;
2009-11-04 15:04:32 +00:00
return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
2009-06-02 17:58:47 +00:00
}
case MemberExprClass:
// If the base pointer or element is to a volatile pointer/field, accessing
// it is a side effect.
2009-11-04 15:04:32 +00:00
if (Ctx.getCanonicalType(getType()).isVolatileQualified())
2009-06-02 17:58:47 +00:00
return false;
Loc = cast<MemberExpr>(this)->getMemberLoc();
R1 = SourceRange(Loc, Loc);
R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
return true;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
case ArraySubscriptExprClass:
// If the base pointer or element is to a volatile pointer/field, accessing
// it is a side effect.
2009-11-04 15:04:32 +00:00
if (Ctx.getCanonicalType(getType()).isVolatileQualified())
2009-06-02 17:58:47 +00:00
return false;
Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
return true;
case CallExprClass:
case CXXOperatorCallExprClass:
case CXXMemberCallExprClass: {
// If this is a direct call, get the callee.
const CallExpr *CE = cast<CallExpr>(this);
2010-01-01 10:34:51 +00:00
if (const Decl *FD = CE->getCalleeDecl()) {
2009-06-02 17:58:47 +00:00
// If the callee has attribute pure, const, or warn_unused_result, warn
// about it. void foo() { strlen("bar"); } should warn.
2009-10-14 18:03:49 +00:00
//
// Note: If new cases are added here, DiagnoseUnusedExprResult should be
// updated to match for QoI.
if (FD->getAttr<WarnUnusedResultAttr>() ||
FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Loc = CE->getCallee()->getLocStart();
R1 = CE->getCallee()->getSourceRange();
if (unsigned NumArgs = CE->getNumArgs())
R2 = SourceRange(CE->getArg(0)->getLocStart(),
CE->getArg(NumArgs-1)->getLocEnd());
return true;
}
2009-06-02 17:58:47 +00:00
}
return false;
}
2009-11-18 14:59:57 +00:00
case CXXTemporaryObjectExprClass:
case CXXConstructExprClass:
return false;
2010-04-02 08:55:10 +00:00
case ObjCMessageExprClass: {
const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
const ObjCMethodDecl *MD = ME->getMethodDecl();
if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Loc = getExprLoc();
return true;
}
2009-06-02 17:58:47 +00:00
return false;
2010-04-02 08:55:10 +00:00
}
2009-10-14 18:03:49 +00:00
case ObjCPropertyRefExprClass:
2009-10-14 18:03:49 +00:00
Loc = getExprLoc();
R1 = getSourceRange();
return true;
2009-06-02 17:58:47 +00:00
case StmtExprClass: {
// Statement exprs don't logically have side effects themselves, but are
// sometimes used in macros in ways that give them a type that is unused.
// For example ({ blah; foo(); }) will end up with a type if foo has a type.
// however, if the result of the stmt expr is dead, we don't want to emit a
// warning.
const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
if (!CS->body_empty()) {
2009-06-02 17:58:47 +00:00
if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
2009-11-04 15:04:32 +00:00
return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
}
2009-10-14 18:03:49 +00:00
2010-03-16 16:52:15 +00:00
if (getType()->isVoidType())
return false;
2009-06-02 17:58:47 +00:00
Loc = cast<StmtExpr>(this)->getLParenLoc();
R1 = getSourceRange();
return true;
}
case CStyleCastExprClass:
2009-10-14 18:03:49 +00:00
// If this is an explicit cast to void, allow it. People do this when they
// think they know what they're doing :).
2009-06-02 17:58:47 +00:00
if (getType()->isVoidType())
2009-10-14 18:03:49 +00:00
return false;
2009-06-02 17:58:47 +00:00
Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
return true;
2009-11-18 14:59:57 +00:00
case CXXFunctionalCastExprClass: {
2010-03-16 16:52:15 +00:00
if (getType()->isVoidType())
return false;
2009-11-18 14:59:57 +00:00
const CastExpr *CE = cast<CastExpr>(this);
// If this is a cast to void or a constructor conversion, check the operand.
// Otherwise, the result of the cast is unused.
if (CE->getCastKind() == CK_ToVoid ||
CE->getCastKind() == CK_ConstructorConversion)
2009-11-04 15:04:32 +00:00
return (cast<CastExpr>(this)->getSubExpr()
->isUnusedResultAWarning(Loc, R1, R2, Ctx));
2009-06-02 17:58:47 +00:00
Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
return true;
2009-11-18 14:59:57 +00:00
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
case ImplicitCastExprClass:
// Check the operand, since implicit casts are inserted by Sema
2009-11-04 15:04:32 +00:00
return (cast<ImplicitCastExpr>(this)
->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
2009-06-02 17:58:47 +00:00
case CXXDefaultArgExprClass:
2009-11-04 15:04:32 +00:00
return (cast<CXXDefaultArgExpr>(this)
->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
2009-06-02 17:58:47 +00:00
case CXXNewExprClass:
// FIXME: In theory, there might be new expressions that don't have side
// effects (e.g. a placement new with an uninitialized POD).
case CXXDeleteExprClass:
return false;
2009-10-14 18:03:49 +00:00
case CXXBindTemporaryExprClass:
2009-11-04 15:04:32 +00:00
return (cast<CXXBindTemporaryExpr>(this)
->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
case ExprWithCleanupsClass:
return (cast<ExprWithCleanups>(this)
2009-11-04 15:04:32 +00:00
->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
2009-06-02 17:58:47 +00:00
}
}
/// isOBJCGCCandidate - Check if an expression is objc gc'able.
2009-10-14 18:03:49 +00:00
/// returns true, if it is; false otherwise.
2009-06-02 17:58:47 +00:00
bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
switch (getStmtClass()) {
default:
return false;
case ObjCIvarRefExprClass:
return true;
case Expr::UnaryOperatorClass:
return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
case ParenExprClass:
return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
case ImplicitCastExprClass:
return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
case CStyleCastExprClass:
return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
2009-11-04 15:04:32 +00:00
case DeclRefExprClass: {
2009-06-02 17:58:47 +00:00
const Decl *D = cast<DeclRefExpr>(this)->getDecl();
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
if (VD->hasGlobalStorage())
return true;
QualType T = VD->getType();
2009-10-14 18:03:49 +00:00
// dereferencing to a pointer is always a gc'able candidate,
// unless it is __weak.
return T->isPointerType() &&
(Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
2009-06-02 17:58:47 +00:00
}
return false;
}
case MemberExprClass: {
const MemberExpr *M = cast<MemberExpr>(this);
return M->getBase()->isOBJCGCCandidate(Ctx);
}
case ArraySubscriptExprClass:
return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
}
}
bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
if (isTypeDependent())
return false;
return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
}
static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
Expr::CanThrowResult CT2) {
// CanThrowResult constants are ordered so that the maximum is the correct
// merge result.
return CT1 > CT2 ? CT1 : CT2;
}
static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
Expr *E = const_cast<Expr*>(CE);
Expr::CanThrowResult R = Expr::CT_Cannot;
for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
}
return R;
}
static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
bool NullThrows = true) {
if (!D)
return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
// See if we can get a function type from the decl somehow.
const ValueDecl *VD = dyn_cast<ValueDecl>(D);
if (!VD) // If we have no clue what we're calling, assume the worst.
return Expr::CT_Can;
// As an extension, we assume that __attribute__((nothrow)) functions don't
// throw.
if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
return Expr::CT_Cannot;
QualType T = VD->getType();
const FunctionProtoType *FT;
if ((FT = T->getAs<FunctionProtoType>())) {
} else if (const PointerType *PT = T->getAs<PointerType>())
FT = PT->getPointeeType()->getAs<FunctionProtoType>();
else if (const ReferenceType *RT = T->getAs<ReferenceType>())
FT = RT->getPointeeType()->getAs<FunctionProtoType>();
else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
FT = MT->getPointeeType()->getAs<FunctionProtoType>();
else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
FT = BT->getPointeeType()->getAs<FunctionProtoType>();
if (!FT)
return Expr::CT_Can;
return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
}
static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
if (DC->isTypeDependent())
return Expr::CT_Dependent;
if (!DC->getTypeAsWritten()->isReferenceType())
return Expr::CT_Cannot;
return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
}
static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
const CXXTypeidExpr *DC) {
if (DC->isTypeOperand())
return Expr::CT_Cannot;
Expr *Op = DC->getExprOperand();
if (Op->isTypeDependent())
return Expr::CT_Dependent;
const RecordType *RT = Op->getType()->getAs<RecordType>();
if (!RT)
return Expr::CT_Cannot;
if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
return Expr::CT_Cannot;
if (Op->Classify(C).isPRValue())
return Expr::CT_Cannot;
return Expr::CT_Can;
}
Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
// C++ [expr.unary.noexcept]p3:
// [Can throw] if in a potentially-evaluated context the expression would
// contain:
switch (getStmtClass()) {
case CXXThrowExprClass:
// - a potentially evaluated throw-expression
return CT_Can;
case CXXDynamicCastExprClass: {
// - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
// where T is a reference type, that requires a run-time check
CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
if (CT == CT_Can)
return CT;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
case CXXTypeidExprClass:
// - a potentially evaluated typeid expression applied to a glvalue
// expression whose type is a polymorphic class type
return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
// - a potentially evaluated call to a function, member function, function
// pointer, or member function pointer that does not have a non-throwing
// exception-specification
case CallExprClass:
case CXXOperatorCallExprClass:
case CXXMemberCallExprClass: {
CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
if (CT == CT_Can)
return CT;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
case CXXConstructExprClass:
case CXXTemporaryObjectExprClass: {
CanThrowResult CT = CanCalleeThrow(
cast<CXXConstructExpr>(this)->getConstructor());
if (CT == CT_Can)
return CT;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
case CXXNewExprClass: {
CanThrowResult CT = MergeCanThrow(
CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
/*NullThrows*/false));
if (CT == CT_Can)
return CT;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
case CXXDeleteExprClass: {
CanThrowResult CT = CanCalleeThrow(
cast<CXXDeleteExpr>(this)->getOperatorDelete());
if (CT == CT_Can)
return CT;
const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
// Unwrap exactly one implicit cast, which converts all pointers to void*.
if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Arg = Cast->getSubExpr();
if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
CanThrowResult CT2 = CanCalleeThrow(
cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
if (CT2 == CT_Can)
return CT2;
CT = MergeCanThrow(CT, CT2);
}
}
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
case CXXBindTemporaryExprClass: {
// The bound temporary has to be destroyed again, which might throw.
CanThrowResult CT = CanCalleeThrow(
cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
if (CT == CT_Can)
return CT;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
// ObjC message sends are like function calls, but never have exception
// specs.
case ObjCMessageExprClass:
case ObjCPropertyRefExprClass:
return CT_Can;
// Many other things have subexpressions, so we have to test those.
// Some are simple:
case ParenExprClass:
case MemberExprClass:
case CXXReinterpretCastExprClass:
case CXXConstCastExprClass:
case ConditionalOperatorClass:
case CompoundLiteralExprClass:
case ExtVectorElementExprClass:
case InitListExprClass:
case DesignatedInitExprClass:
case ParenListExprClass:
case VAArgExprClass:
case CXXDefaultArgExprClass:
case ExprWithCleanupsClass:
case ObjCIvarRefExprClass:
case ObjCIsaExprClass:
case ShuffleVectorExprClass:
return CanSubExprsThrow(C, this);
// Some might be dependent for other reasons.
case UnaryOperatorClass:
case ArraySubscriptExprClass:
case ImplicitCastExprClass:
case CStyleCastExprClass:
case CXXStaticCastExprClass:
case CXXFunctionalCastExprClass:
case BinaryOperatorClass:
case CompoundAssignOperatorClass: {
CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
return MergeCanThrow(CT, CanSubExprsThrow(C, this));
}
// FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
case StmtExprClass:
return CT_Can;
case ChooseExprClass:
if (isTypeDependent() || isValueDependent())
return CT_Dependent;
return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
// Some expressions are always dependent.
case DependentScopeDeclRefExprClass:
case CXXUnresolvedConstructExprClass:
case CXXDependentScopeMemberExprClass:
return CT_Dependent;
default:
// All other expressions don't have subexpressions, or else they are
// unevaluated.
return CT_Cannot;
}
}
2009-06-02 17:58:47 +00:00
Expr* Expr::IgnoreParens() {
Expr* E = this;
while (true) {
if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
E = P->getSubExpr();
continue;
}
if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
if (P->getOpcode() == UO_Extension) {
E = P->getSubExpr();
continue;
}
}
return E;
}
2009-06-02 17:58:47 +00:00
}
/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
/// or CastExprs or ImplicitCastExprs, returning their operand.
Expr *Expr::IgnoreParenCasts() {
Expr *E = this;
while (true) {
if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2009-06-02 17:58:47 +00:00
E = P->getSubExpr();
continue;
}
if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2009-06-02 17:58:47 +00:00
E = P->getSubExpr();
continue;
}
if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
if (P->getOpcode() == UO_Extension) {
E = P->getSubExpr();
continue;
}
}
return E;
2009-06-02 17:58:47 +00:00
}
}
/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
/// casts. This is intended purely as a temporary workaround for code
/// that hasn't yet been rewritten to do the right thing about those
/// casts, and may disappear along with the last internal use.
Expr *Expr::IgnoreParenLValueCasts() {
Expr *E = this;
while (true) {
if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
E = P->getSubExpr();
continue;
} else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
if (P->getCastKind() == CK_LValueToRValue) {
E = P->getSubExpr();
continue;
}
} else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
if (P->getOpcode() == UO_Extension) {
E = P->getSubExpr();
continue;
}
}
break;
}
return E;
}
2010-05-27 15:17:06 +00:00
Expr *Expr::IgnoreParenImpCasts() {
Expr *E = this;
while (true) {
if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2010-05-27 15:17:06 +00:00
E = P->getSubExpr();
continue;
}
if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
2010-05-27 15:17:06 +00:00
E = P->getSubExpr();
continue;
}
if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
if (P->getOpcode() == UO_Extension) {
E = P->getSubExpr();
continue;
}
}
return E;
2010-05-27 15:17:06 +00:00
}
}
2009-06-02 17:58:47 +00:00
/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
/// value (including ptr->int casts of the same size). Strip off any
/// ParenExpr or CastExprs, returning their operand.
Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
Expr *E = this;
while (true) {
if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
E = P->getSubExpr();
continue;
}
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
if (CastExpr *P = dyn_cast<CastExpr>(E)) {
// We ignore integer <-> casts that are of the same width, ptr<->ptr and
2010-07-13 17:21:42 +00:00
// ptr<->int casts of the same width. We also ignore all identity casts.
2009-06-02 17:58:47 +00:00
Expr *SE = P->getSubExpr();
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
E = SE;
continue;
}
2009-10-14 18:03:49 +00:00
if ((E->getType()->isPointerType() ||
2010-07-13 17:21:42 +00:00
E->getType()->isIntegralType(Ctx)) &&
(SE->getType()->isPointerType() ||
2010-07-13 17:21:42 +00:00
SE->getType()->isIntegralType(Ctx)) &&
2009-06-02 17:58:47 +00:00
Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
E = SE;
continue;
}
}
2009-10-14 18:03:49 +00:00
if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
if (P->getOpcode() == UO_Extension) {
E = P->getSubExpr();
continue;
}
}
2009-06-02 17:58:47 +00:00
return E;
}
}
2009-12-15 18:49:47 +00:00
bool Expr::isDefaultArgument() const {
const Expr *E = this;
while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
E = ICE->getSubExprAsWritten();
return isa<CXXDefaultArgExpr>(E);
}
2009-06-02 17:58:47 +00:00
2010-04-03 07:51:34 +00:00
/// \brief Skip over any no-op casts and any temporary-binding
/// expressions.
static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
2010-04-03 07:51:34 +00:00
while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
if (ICE->getCastKind() == CK_NoOp)
2010-04-03 07:51:34 +00:00
E = ICE->getSubExpr();
else
break;
}
while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
E = BE->getSubExpr();
while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
if (ICE->getCastKind() == CK_NoOp)
2010-04-03 07:51:34 +00:00
E = ICE->getSubExpr();
else
break;
}
return E->IgnoreParens();
}
2010-04-03 07:51:34 +00:00
/// isTemporaryObject - Determines if this expression produces a
/// temporary of the given class type.
bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
return false;
2010-04-03 07:51:34 +00:00
const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
2010-04-03 07:51:34 +00:00
// Temporaries are by definition pr-values of class type.
if (!E->Classify(C).isPRValue()) {
// In this context, property reference is a message call and is pr-value.
if (!isa<ObjCPropertyRefExpr>(E))
return false;
}
2010-04-03 07:51:34 +00:00
// Black-list a few cases which yield pr-values of class type that don't
// refer to temporaries of that type:
2010-04-03 07:51:34 +00:00
// - implicit derived-to-base conversions
if (isa<ImplicitCastExpr>(E)) {
switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
case CK_DerivedToBase:
case CK_UncheckedDerivedToBase:
return false;
default:
break;
}
2010-04-03 07:51:34 +00:00
}
// - member expressions (all)
if (isa<MemberExpr>(E))
return false;
2010-04-03 07:51:34 +00:00
// - opaque values (all)
if (isa<OpaqueValueExpr>(E))
return false;
2010-04-03 07:51:34 +00:00
return true;
2010-04-03 07:51:34 +00:00
}
2009-06-02 17:58:47 +00:00
/// hasAnyTypeDependentArguments - Determines if any of the expressions
/// in Exprs is type-dependent.
bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
for (unsigned I = 0; I < NumExprs; ++I)
if (Exprs[I]->isTypeDependent())
return true;
return false;
}
/// hasAnyValueDependentArguments - Determines if any of the expressions
/// in Exprs is value-dependent.
bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
for (unsigned I = 0; I < NumExprs; ++I)
if (Exprs[I]->isValueDependent())
return true;
return false;
}
bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
2009-06-02 17:58:47 +00:00
// This function is attempting whether an expression is an initializer
// which can be evaluated at compile-time. isEvaluatable handles most
// of the cases, but it can't deal with some initializer-specific
// expressions, and it can't deal with aggregates; we deal with those here,
// and fall back to isEvaluatable for the other cases.
// If we ever capture reference-binding directly in the AST, we can
// kill the second parameter.
if (IsForRef) {
EvalResult Result;
return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
}
2009-06-02 17:58:47 +00:00
switch (getStmtClass()) {
default: break;
case StringLiteralClass:
2009-10-14 18:03:49 +00:00
case ObjCStringLiteralClass:
2009-06-02 17:58:47 +00:00
case ObjCEncodeExprClass:
return true;
case CXXTemporaryObjectExprClass:
case CXXConstructExprClass: {
const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
// Only if it's
// 1) an application of the trivial default constructor or
if (!CE->getConstructor()->isTrivial()) return false;
if (!CE->getNumArgs()) return true;
// 2) an elidable trivial copy construction of an operand which is
// itself a constant initializer. Note that we consider the
// operand on its own, *not* as a reference binding.
return CE->isElidable() &&
CE->getArg(0)->isConstantInitializer(Ctx, false);
}
2009-06-02 17:58:47 +00:00
case CompoundLiteralExprClass: {
// This handles gcc's extension that allows global initializers like
// "struct x {int x;} x = (struct x) {};".
// FIXME: This accepts other cases it shouldn't!
const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
return Exp->isConstantInitializer(Ctx, false);
2009-06-02 17:58:47 +00:00
}
case InitListExprClass: {
// FIXME: This doesn't deal with fields with reference types correctly.
// FIXME: This incorrectly allows pointers cast to integers to be assigned
// to bitfields.
const InitListExpr *Exp = cast<InitListExpr>(this);
unsigned numInits = Exp->getNumInits();
for (unsigned i = 0; i < numInits; i++) {
if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
2009-06-02 17:58:47 +00:00
return false;
}
return true;
}
case ImplicitValueInitExprClass:
return true;
2009-10-14 18:03:49 +00:00
case ParenExprClass:
return cast<ParenExpr>(this)->getSubExpr()
->isConstantInitializer(Ctx, IsForRef);
case ChooseExprClass:
return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
->isConstantInitializer(Ctx, IsForRef);
2009-06-02 17:58:47 +00:00
case UnaryOperatorClass: {
const UnaryOperator* Exp = cast<UnaryOperator>(this);
if (Exp->getOpcode() == UO_Extension)
return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
2009-06-02 17:58:47 +00:00
break;
}
2009-10-14 18:03:49 +00:00
case BinaryOperatorClass: {
// Special case &&foo - &&bar. It would be nice to generalize this somehow
// but this handles the common case.
const BinaryOperator *Exp = cast<BinaryOperator>(this);
if (Exp->getOpcode() == BO_Sub &&
2009-10-14 18:03:49 +00:00
isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
return true;
break;
}
case CXXFunctionalCastExprClass:
case CXXStaticCastExprClass:
2009-06-02 17:58:47 +00:00
case ImplicitCastExprClass:
case CStyleCastExprClass:
// Handle casts with a destination that's a struct or union; this
// deals with both the gcc no-op struct cast extension and the
// cast-to-union extension.
if (getType()->isRecordType())
return cast<CastExpr>(this)->getSubExpr()
->isConstantInitializer(Ctx, false);
2009-10-14 18:03:49 +00:00
// Integer->integer casts can be handled here, which is important for
// things like (int)(&&x-&&y). Scary but true.
if (getType()->isIntegerType() &&
cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
return cast<CastExpr>(this)->getSubExpr()
->isConstantInitializer(Ctx, false);
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
break;
}
return isEvaluatable(Ctx);
}
/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
/// pointer constant or not, as well as the specific kind of constant detected.
/// Null pointer constants can be integer constant expressions with the
/// value zero, casts of zero to void*, nullptr (C++0X), or __null
/// (a GNU extension).
Expr::NullPointerConstantKind
Expr::isNullPointerConstant(ASTContext &Ctx,
NullPointerConstantValueDependence NPC) const {
2009-10-14 18:03:49 +00:00
if (isValueDependent()) {
switch (NPC) {
case NPC_NeverValueDependent:
assert(false && "Unexpected value dependent expression!");
// If the unthinkable happens, fall through to the safest alternative.
case NPC_ValueDependentIsNull:
if (isTypeDependent() || getType()->isIntegralType(Ctx))
return NPCK_ZeroInteger;
else
return NPCK_NotNull;
2009-10-14 18:03:49 +00:00
case NPC_ValueDependentIsNotNull:
return NPCK_NotNull;
2009-10-14 18:03:49 +00:00
}
}
2009-06-02 17:58:47 +00:00
// Strip off a cast to void*, if it exists. Except in C++.
if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
if (!Ctx.getLangOptions().CPlusPlus) {
// Check that it is a cast to void*.
2009-10-14 18:03:49 +00:00
if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
2009-06-02 17:58:47 +00:00
QualType Pointee = PT->getPointeeType();
2009-10-14 18:03:49 +00:00
if (!Pointee.hasQualifiers() &&
2009-06-02 17:58:47 +00:00
Pointee->isVoidType() && // to void*
CE->getSubExpr()->getType()->isIntegerType()) // from int.
2009-10-14 18:03:49 +00:00
return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2009-06-02 17:58:47 +00:00
}
}
} else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
// Ignore the ImplicitCastExpr type entirely.
2009-10-14 18:03:49 +00:00
return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
2009-06-02 17:58:47 +00:00
} else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
// Accept ((void*)0) as a null pointer constant, as many other
// implementations do.
2009-10-14 18:03:49 +00:00
return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
} else if (const CXXDefaultArgExpr *DefaultArg
2009-06-02 17:58:47 +00:00
= dyn_cast<CXXDefaultArgExpr>(this)) {
// See through default argument expressions
2009-10-14 18:03:49 +00:00
return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
2009-06-02 17:58:47 +00:00
} else if (isa<GNUNullExpr>(this)) {
// The GNU __null extension is always a null pointer constant.
return NPCK_GNUNull;
2009-06-02 17:58:47 +00:00
}
// C++0x nullptr_t is always a null pointer constant.
if (getType()->isNullPtrType())
return NPCK_CXX0X_nullptr;
if (const RecordType *UT = getType()->getAsUnionType())
if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
const Expr *InitExpr = CLE->getInitializer();
if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
}
2009-06-02 17:58:47 +00:00
// This expression must be an integer type.
2009-10-14 18:03:49 +00:00
if (!getType()->isIntegerType() ||
(Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
return NPCK_NotNull;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
// If we have an integer constant expression, we need to *evaluate* it and
// test for the value 0.
llvm::APSInt Result;
bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
}
/// \brief If this expression is an l-value for an Objective C
/// property, find the underlying property reference expression.
const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
const Expr *E = this;
while (true) {
assert((E->getValueKind() == VK_LValue &&
E->getObjectKind() == OK_ObjCProperty) &&
"expression is not a property reference");
E = E->IgnoreParenCasts();
if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
if (BO->getOpcode() == BO_Comma) {
E = BO->getRHS();
continue;
}
}
break;
}
return cast<ObjCPropertyRefExpr>(E);
2009-06-02 17:58:47 +00:00
}
FieldDecl *Expr::getBitField() {
2009-10-14 18:03:49 +00:00
Expr *E = this->IgnoreParens();
2009-06-02 17:58:47 +00:00
2010-02-16 09:31:36 +00:00
while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
if (ICE->getCastKind() == CK_LValueToRValue ||
(ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
2010-02-16 09:31:36 +00:00
E = ICE->getSubExpr()->IgnoreParens();
else
break;
}
2009-06-02 17:58:47 +00:00
if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
if (Field->isBitField())
return Field;
if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
if (Field->isBitField())
return Field;
2009-06-02 17:58:47 +00:00
if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
if (BinOp->isAssignmentOp() && BinOp->getLHS())
return BinOp->getLHS()->getBitField();
return 0;
}
2010-02-16 09:31:36 +00:00
bool Expr::refersToVectorElement() const {
const Expr *E = this->IgnoreParens();
while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
if (ICE->getValueKind() != VK_RValue &&
ICE->getCastKind() == CK_NoOp)
2010-02-16 09:31:36 +00:00
E = ICE->getSubExpr()->IgnoreParens();
else
break;
}
if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
return ASE->getBase()->getType()->isVectorType();
if (isa<ExtVectorElementExpr>(E))
return true;
return false;
}
2009-06-02 17:58:47 +00:00
/// isArrow - Return true if the base expression is a pointer to vector,
/// return false if the base expression is a vector.
bool ExtVectorElementExpr::isArrow() const {
return getBase()->getType()->isPointerType();
}
unsigned ExtVectorElementExpr::getNumElements() const {
2009-10-14 18:03:49 +00:00
if (const VectorType *VT = getType()->getAs<VectorType>())
2009-06-02 17:58:47 +00:00
return VT->getNumElements();
return 1;
}
/// containsDuplicateElements - Return true if any element access is repeated.
bool ExtVectorElementExpr::containsDuplicateElements() const {
2009-10-23 14:22:18 +00:00
// FIXME: Refactor this code to an accessor on the AST node which returns the
// "type" of component access, and share with code below and in Sema.
llvm::StringRef Comp = Accessor->getName();
2009-06-02 17:58:47 +00:00
// Halving swizzles do not contain duplicate elements.
2009-10-23 14:22:18 +00:00
if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
2009-06-02 17:58:47 +00:00
return false;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
// Advance past s-char prefix on hex swizzles.
2009-10-23 14:22:18 +00:00
if (Comp[0] == 's' || Comp[0] == 'S')
Comp = Comp.substr(1);
2009-10-14 18:03:49 +00:00
2009-10-23 14:22:18 +00:00
for (unsigned i = 0, e = Comp.size(); i != e; ++i)
if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
2009-06-02 17:58:47 +00:00
return true;
2009-10-23 14:22:18 +00:00
2009-06-02 17:58:47 +00:00
return false;
}
/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
void ExtVectorElementExpr::getEncodedElementAccess(
llvm::SmallVectorImpl<unsigned> &Elts) const {
2009-10-23 14:22:18 +00:00
llvm::StringRef Comp = Accessor->getName();
if (Comp[0] == 's' || Comp[0] == 'S')
Comp = Comp.substr(1);
2009-10-14 18:03:49 +00:00
2009-10-23 14:22:18 +00:00
bool isHi = Comp == "hi";
bool isLo = Comp == "lo";
bool isEven = Comp == "even";
bool isOdd = Comp == "odd";
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
uint64_t Index;
2009-10-14 18:03:49 +00:00
2009-06-02 17:58:47 +00:00
if (isHi)
Index = e + i;
else if (isLo)
Index = i;
else if (isEven)
Index = 2 * i;
else if (isOdd)
Index = 2 * i + 1;
else
2009-10-23 14:22:18 +00:00
Index = ExtVectorType::getAccessorIdx(Comp[i]);
2009-06-02 17:58:47 +00:00
Elts.push_back(Index);
}
}
2010-05-04 16:12:48 +00:00
ObjCMessageExpr::ObjCMessageExpr(QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
SourceLocation SuperLoc,
bool IsInstanceSuper,
QualType SuperType,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc)
: Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
/*TypeDependent=*/false, /*ValueDependent=*/false,
/*ContainsUnexpandedParameterPack=*/false),
2010-05-04 16:12:48 +00:00
NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
HasMethod(Method != 0), SuperLoc(SuperLoc),
SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
: Sel.getAsOpaquePtr())),
SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2010-03-10 17:45:58 +00:00
{
2010-05-04 16:12:48 +00:00
setReceiverPointer(SuperType.getAsOpaquePtr());
if (NumArgs)
memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
}
ObjCMessageExpr::ObjCMessageExpr(QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
TypeSourceInfo *Receiver,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc)
: Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
T->isDependentType(), T->containsUnexpandedParameterPack()),
2010-05-04 16:12:48 +00:00
NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
: Sel.getAsOpaquePtr())),
SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2010-05-04 16:12:48 +00:00
{
setReceiverPointer(Receiver);
Expr **MyArgs = getArgs();
for (unsigned I = 0; I != NumArgs; ++I) {
if (Args[I]->isTypeDependent())
ExprBits.TypeDependent = true;
if (Args[I]->isValueDependent())
ExprBits.ValueDependent = true;
if (Args[I]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
MyArgs[I] = Args[I];
}
2010-05-04 16:12:48 +00:00
}
ObjCMessageExpr::ObjCMessageExpr(QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
Expr *Receiver,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc)
: Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Receiver->isTypeDependent(),
Receiver->containsUnexpandedParameterPack()),
2010-05-04 16:12:48 +00:00
NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
: Sel.getAsOpaquePtr())),
SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2010-05-04 16:12:48 +00:00
{
setReceiverPointer(Receiver);
Expr **MyArgs = getArgs();
for (unsigned I = 0; I != NumArgs; ++I) {
if (Args[I]->isTypeDependent())
ExprBits.TypeDependent = true;
if (Args[I]->isValueDependent())
ExprBits.ValueDependent = true;
if (Args[I]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
MyArgs[I] = Args[I];
}
2010-05-04 16:12:48 +00:00
}
ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
SourceLocation SuperLoc,
bool IsInstanceSuper,
QualType SuperType,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc) {
unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
NumArgs * sizeof(Expr *);
void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
SuperType, Sel, SelLoc, Method, Args,NumArgs,
2010-05-04 16:12:48 +00:00
RBracLoc);
}
ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
TypeSourceInfo *Receiver,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc) {
unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
NumArgs * sizeof(Expr *);
void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
Method, Args, NumArgs, RBracLoc);
2010-05-04 16:12:48 +00:00
}
ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
ExprValueKind VK,
2010-05-04 16:12:48 +00:00
SourceLocation LBracLoc,
Expr *Receiver,
Selector Sel,
SourceLocation SelLoc,
2010-05-04 16:12:48 +00:00
ObjCMethodDecl *Method,
Expr **Args, unsigned NumArgs,
SourceLocation RBracLoc) {
unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
NumArgs * sizeof(Expr *);
void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
Method, Args, NumArgs, RBracLoc);
2010-05-04 16:12:48 +00:00
}
ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
unsigned NumArgs) {
unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
NumArgs * sizeof(Expr *);
void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
}
SourceRange ObjCMessageExpr::getReceiverRange() const {
switch (getReceiverKind()) {
case Instance:
return getInstanceReceiver()->getSourceRange();
case Class:
return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
case SuperInstance:
case SuperClass:
return getSuperLoc();
}
return SourceLocation();
}
2010-05-04 16:12:48 +00:00
Selector ObjCMessageExpr::getSelector() const {
if (HasMethod)
return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
->getSelector();
return Selector(SelectorOrMethod);
}
ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
switch (getReceiverKind()) {
case Instance:
if (const ObjCObjectPointerType *Ptr
= getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
return Ptr->getInterfaceDecl();
break;
2009-06-02 17:58:47 +00:00
2010-05-04 16:12:48 +00:00
case Class:
2010-05-27 15:17:06 +00:00
if (const ObjCObjectType *Ty
= getClassReceiver()->getAs<ObjCObjectType>())
return Ty->getInterface();
2010-05-04 16:12:48 +00:00
break;
2009-06-02 17:58:47 +00:00
2010-05-04 16:12:48 +00:00
case SuperInstance:
if (const ObjCObjectPointerType *Ptr
= getSuperType()->getAs<ObjCObjectPointerType>())
return Ptr->getInterfaceDecl();
break;
2010-03-10 17:45:58 +00:00
2010-05-04 16:12:48 +00:00
case SuperClass:
if (const ObjCObjectType *Iface
= getSuperType()->getAs<ObjCObjectType>())
return Iface->getInterface();
2010-05-04 16:12:48 +00:00
break;
}
2009-06-02 17:58:47 +00:00
2010-05-04 16:12:48 +00:00
return 0;
2010-02-16 09:31:36 +00:00
}
2009-06-02 17:58:47 +00:00
bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
2009-06-02 17:58:47 +00:00
return getCond()->EvaluateAsInt(C) != 0;
}
ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
QualType Type, SourceLocation BLoc,
SourceLocation RP)
: Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
Type->isDependentType(), Type->isDependentType(),
Type->containsUnexpandedParameterPack()),
BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
{
SubExprs = new (C) Stmt*[nexpr];
for (unsigned i = 0; i < nexpr; i++) {
if (args[i]->isTypeDependent())
ExprBits.TypeDependent = true;
if (args[i]->isValueDependent())
ExprBits.ValueDependent = true;
if (args[i]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
SubExprs[i] = args[i];
}
}
2009-10-14 18:03:49 +00:00
void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
unsigned NumExprs) {
if (SubExprs) C.Deallocate(SubExprs);
SubExprs = new (C) Stmt* [NumExprs];
2009-06-02 17:58:47 +00:00
this->NumExprs = NumExprs;
memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
}
//===----------------------------------------------------------------------===//
// DesignatedInitExpr
//===----------------------------------------------------------------------===//
IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
assert(Kind == FieldDesignator && "Only valid on a field designator");
if (Field.NameOrField & 0x01)
return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
else
return getField()->getIdentifier();
}
2010-01-15 15:39:40 +00:00
DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
unsigned NumDesignators,
2009-06-02 17:58:47 +00:00
const Designator *Designators,
2009-10-14 18:03:49 +00:00
SourceLocation EqualOrColonLoc,
2009-06-02 17:58:47 +00:00
bool GNUSyntax,
2009-10-14 18:03:49 +00:00
Expr **IndexExprs,
2009-06-02 17:58:47 +00:00
unsigned NumIndexExprs,
Expr *Init)
2009-10-14 18:03:49 +00:00
: Expr(DesignatedInitExprClass, Ty,
Init->getValueKind(), Init->getObjectKind(),
Init->isTypeDependent(), Init->isValueDependent(),
Init->containsUnexpandedParameterPack()),
2009-10-14 18:03:49 +00:00
EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
2010-01-15 15:39:40 +00:00
this->Designators = new (C) Designator[NumDesignators];
2009-06-02 17:58:47 +00:00
// Record the initializer itself.
child_range Child = children();
2009-06-02 17:58:47 +00:00
*Child++ = Init;
// Copy the designators and their subexpressions, computing
// value-dependence along the way.
unsigned IndexIdx = 0;
for (unsigned I = 0; I != NumDesignators; ++I) {
this->Designators[I] = Designators[I];
if (this->Designators[I].isArrayDesignator()) {
// Compute type- and value-dependence.
Expr *Index = IndexExprs[IndexIdx];
if (Index->isTypeDependent() || Index->isValueDependent())
ExprBits.ValueDependent = true;
// Propagate unexpanded parameter packs.
if (Index->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2009-06-02 17:58:47 +00:00
// Copy the index expressions into permanent storage.
*Child++ = IndexExprs[IndexIdx++];
} else if (this->Designators[I].isArrayRangeDesignator()) {
// Compute type- and value-dependence.
Expr *Start = IndexExprs[IndexIdx];
Expr *End = IndexExprs[IndexIdx + 1];
if (Start->isTypeDependent() || Start->isValueDependent() ||
End->isTypeDependent() || End->isValueDependent())
ExprBits.ValueDependent = true;
// Propagate unexpanded parameter packs.
if (Start->containsUnexpandedParameterPack() ||
End->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2009-06-02 17:58:47 +00:00
// Copy the start/end expressions into permanent storage.
*Child++ = IndexExprs[IndexIdx++];
*Child++ = IndexExprs[IndexIdx++];
}
}
assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
}
DesignatedInitExpr *
2009-10-14 18:03:49 +00:00
DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
2009-06-02 17:58:47 +00:00
unsigned NumDesignators,
Expr **IndexExprs, unsigned NumIndexExprs,
SourceLocation ColonOrEqualLoc,
bool UsesColonSyntax, Expr *Init) {
void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2010-01-15 15:39:40 +00:00
return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
2009-06-02 17:58:47 +00:00
ColonOrEqualLoc, UsesColonSyntax,
IndexExprs, NumIndexExprs, Init);
}
2009-10-14 18:03:49 +00:00
DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
2009-06-02 17:58:47 +00:00
unsigned NumIndexExprs) {
void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
sizeof(Stmt *) * (NumIndexExprs + 1), 8);
return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
}
2010-01-15 15:39:40 +00:00
void DesignatedInitExpr::setDesignators(ASTContext &C,
const Designator *Desigs,
2009-06-02 17:58:47 +00:00
unsigned NumDesigs) {
2010-01-15 15:39:40 +00:00
Designators = new (C) Designator[NumDesigs];
2009-06-02 17:58:47 +00:00
NumDesignators = NumDesigs;
for (unsigned I = 0; I != NumDesigs; ++I)
Designators[I] = Desigs[I];
}
SourceRange DesignatedInitExpr::getSourceRange() const {
SourceLocation StartLoc;
Designator &First =
*const_cast<DesignatedInitExpr*>(this)->designators_begin();
if (First.isFieldDesignator()) {
if (GNUSyntax)
StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
else
StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
} else
StartLoc =
SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
}
Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
char* Ptr = static_cast<char*>(static_cast<void *>(this));
Ptr += sizeof(DesignatedInitExpr);
Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
}
Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
2009-10-14 18:03:49 +00:00
assert(D.Kind == Designator::ArrayRangeDesignator &&
2009-06-02 17:58:47 +00:00
"Requires array range designator");
char* Ptr = static_cast<char*>(static_cast<void *>(this));
Ptr += sizeof(DesignatedInitExpr);
Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
}
Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
2009-10-14 18:03:49 +00:00
assert(D.Kind == Designator::ArrayRangeDesignator &&
2009-06-02 17:58:47 +00:00
"Requires array range designator");
char* Ptr = static_cast<char*>(static_cast<void *>(this));
Ptr += sizeof(DesignatedInitExpr);
Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
}
/// \brief Replaces the designator at index @p Idx with the series
/// of designators in [First, Last).
2010-01-15 15:39:40 +00:00
void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
2009-10-14 18:03:49 +00:00
const Designator *First,
2009-06-02 17:58:47 +00:00
const Designator *Last) {
unsigned NumNewDesignators = Last - First;
if (NumNewDesignators == 0) {
std::copy_backward(Designators + Idx + 1,
Designators + NumDesignators,
Designators + Idx);
--NumNewDesignators;
return;
} else if (NumNewDesignators == 1) {
Designators[Idx] = *First;
return;
}
2009-10-14 18:03:49 +00:00
Designator *NewDesignators
2010-01-15 15:39:40 +00:00
= new (C) Designator[NumDesignators - 1 + NumNewDesignators];
2009-06-02 17:58:47 +00:00
std::copy(Designators, Designators + Idx, NewDesignators);
std::copy(First, Last, NewDesignators + Idx);
std::copy(Designators + Idx + 1, Designators + NumDesignators,
NewDesignators + Idx + NumNewDesignators);
Designators = NewDesignators;
NumDesignators = NumDesignators - 1 + NumNewDesignators;
}
2009-10-14 18:03:49 +00:00
ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Expr **exprs, unsigned nexprs,
SourceLocation rparenloc)
: Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
false, false, false),
NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
2009-10-14 18:03:49 +00:00
Exprs = new (C) Stmt*[nexprs];
for (unsigned i = 0; i != nexprs; ++i) {
if (exprs[i]->isTypeDependent())
ExprBits.TypeDependent = true;
if (exprs[i]->isValueDependent())
ExprBits.ValueDependent = true;
if (exprs[i]->containsUnexpandedParameterPack())
ExprBits.ContainsUnexpandedParameterPack = true;
2009-10-14 18:03:49 +00:00
Exprs[i] = exprs[i];
}
}
const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
e = ewc->getSubExpr();
e = cast<CXXConstructExpr>(e)->getArg(0);
while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
e = ice->getSubExpr();
return cast<OpaqueValueExpr>(e);
2009-10-14 18:03:49 +00:00
}
2009-06-02 17:58:47 +00:00
//===----------------------------------------------------------------------===//
// ExprIterator.
//===----------------------------------------------------------------------===//
Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
const Expr* ConstExprIterator::operator[](size_t idx) const {
return cast<Expr>(I[idx]);
}
const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
//===----------------------------------------------------------------------===//
// Child Iterators for iterating over subexpressions/substatements
//===----------------------------------------------------------------------===//
// SizeOfAlignOfExpr
Stmt::child_range SizeOfAlignOfExpr::children() {
2009-06-02 17:58:47 +00:00
// If this is of a type and the type is a VLA type (and not a typedef), the
// size expression of the VLA needs to be treated as an executable expression.
// Why isn't this weirdness documented better in StmtIterator?
if (isArgumentType()) {
if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
2009-06-02 17:58:47 +00:00
getArgumentType().getTypePtr()))
return child_range(child_iterator(T), child_iterator());
return child_range();
2009-06-02 17:58:47 +00:00
}
return child_range(&Argument.Ex, &Argument.Ex + 1);
2009-06-02 17:58:47 +00:00
}
// ObjCMessageExpr
Stmt::child_range ObjCMessageExpr::children() {
Stmt **begin;
2010-05-04 16:12:48 +00:00
if (getReceiverKind() == Instance)
begin = reinterpret_cast<Stmt **>(this + 1);
else
begin = reinterpret_cast<Stmt **>(getArgs());
return child_range(begin,
reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
2009-06-02 17:58:47 +00:00
}
// Blocks
BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
SourceLocation l, bool ByRef,
bool constAdded)
: Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
d->isParameterPack()),
D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
{
bool TypeDependent = false;
bool ValueDependent = false;
computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
ExprBits.TypeDependent = TypeDependent;
ExprBits.ValueDependent = ValueDependent;
}
2009-06-02 17:58:47 +00:00