blob: 00662a53afedde38113b2a829fe1661f096b3b14 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Chris Lattner2b334bb2010-04-16 23:34:13 +000030/// isKnownToHaveBooleanValue - Return true if this is an integer expression
31/// that is known to return 0 or 1. This happens for _Bool/bool expressions
32/// but also int expressions which are produced by things like comparisons in
33/// C.
34bool Expr::isKnownToHaveBooleanValue() const {
35 // If this value has _Bool type, it is obvious 0/1.
36 if (getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000037 // If this is a non-scalar-integer type, we don't care enough to try.
Chris Lattner2b334bb2010-04-16 23:34:13 +000038 if (!getType()->isIntegralType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000039
Chris Lattner2b334bb2010-04-16 23:34:13 +000040 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
41 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000042
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
44 switch (UO->getOpcode()) {
45 case UnaryOperator::Plus:
46 case UnaryOperator::Extension:
47 return UO->getSubExpr()->isKnownToHaveBooleanValue();
48 default:
49 return false;
50 }
51 }
Sean Huntc3021132010-05-05 15:23:54 +000052
Chris Lattner2b334bb2010-04-16 23:34:13 +000053 if (const CastExpr *CE = dyn_cast<CastExpr>(this))
54 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000055
Chris Lattner2b334bb2010-04-16 23:34:13 +000056 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
57 switch (BO->getOpcode()) {
58 default: return false;
59 case BinaryOperator::LT: // Relational operators.
60 case BinaryOperator::GT:
61 case BinaryOperator::LE:
62 case BinaryOperator::GE:
63 case BinaryOperator::EQ: // Equality operators.
64 case BinaryOperator::NE:
65 case BinaryOperator::LAnd: // AND operator.
66 case BinaryOperator::LOr: // Logical OR operator.
67 return true;
Sean Huntc3021132010-05-05 15:23:54 +000068
Chris Lattner2b334bb2010-04-16 23:34:13 +000069 case BinaryOperator::And: // Bitwise AND operator.
70 case BinaryOperator::Xor: // Bitwise XOR operator.
71 case BinaryOperator::Or: // Bitwise OR operator.
72 // Handle things like (x==2)|(y==12).
73 return BO->getLHS()->isKnownToHaveBooleanValue() &&
74 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000075
Chris Lattner2b334bb2010-04-16 23:34:13 +000076 case BinaryOperator::Comma:
77 case BinaryOperator::Assign:
78 return BO->getRHS()->isKnownToHaveBooleanValue();
79 }
80 }
Sean Huntc3021132010-05-05 15:23:54 +000081
Chris Lattner2b334bb2010-04-16 23:34:13 +000082 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
83 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
84 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000085
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 return false;
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCalld5532b62009-11-23 01:53:49 +000093void ExplicitTemplateArgumentList::initializeFrom(
94 const TemplateArgumentListInfo &Info) {
95 LAngleLoc = Info.getLAngleLoc();
96 RAngleLoc = Info.getRAngleLoc();
97 NumTemplateArgs = Info.size();
98
99 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
100 for (unsigned i = 0; i != NumTemplateArgs; ++i)
101 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
102}
103
104void ExplicitTemplateArgumentList::copyInto(
105 TemplateArgumentListInfo &Info) const {
106 Info.setLAngleLoc(LAngleLoc);
107 Info.setRAngleLoc(RAngleLoc);
108 for (unsigned I = 0; I != NumTemplateArgs; ++I)
109 Info.addArgument(getTemplateArgs()[I]);
110}
111
112std::size_t ExplicitTemplateArgumentList::sizeFor(
113 const TemplateArgumentListInfo &Info) {
114 return sizeof(ExplicitTemplateArgumentList) +
115 sizeof(TemplateArgumentLoc) * Info.size();
116}
117
Douglas Gregor0da76df2009-11-23 11:41:28 +0000118void DeclRefExpr::computeDependence() {
119 TypeDependent = false;
120 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000121
Douglas Gregor0da76df2009-11-23 11:41:28 +0000122 NamedDecl *D = getDecl();
123
124 // (TD) C++ [temp.dep.expr]p3:
125 // An id-expression is type-dependent if it contains:
126 //
Sean Huntc3021132010-05-05 15:23:54 +0000127 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000128 //
129 // (VD) C++ [temp.dep.constexpr]p2:
130 // An identifier is value-dependent if it is:
131
132 // (TD) - an identifier that was declared with dependent type
133 // (VD) - a name declared with a dependent type,
134 if (getType()->isDependentType()) {
135 TypeDependent = true;
136 ValueDependent = true;
137 }
138 // (TD) - a conversion-function-id that specifies a dependent type
Sean Huntc3021132010-05-05 15:23:54 +0000139 else if (D->getDeclName().getNameKind()
Douglas Gregor0da76df2009-11-23 11:41:28 +0000140 == DeclarationName::CXXConversionFunctionName &&
141 D->getDeclName().getCXXNameType()->isDependentType()) {
142 TypeDependent = true;
143 ValueDependent = true;
144 }
145 // (TD) - a template-id that is dependent,
Sean Huntc3021132010-05-05 15:23:54 +0000146 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000147 TemplateSpecializationType::anyDependentTemplateArguments(
Sean Huntc3021132010-05-05 15:23:54 +0000148 getTemplateArgs(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 getNumTemplateArgs())) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (VD) - the name of a non-type template parameter,
154 else if (isa<NonTypeTemplateParmDecl>(D))
155 ValueDependent = true;
156 // (VD) - a constant with integral or enumeration type and is
157 // initialized with an expression that is value-dependent.
158 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
159 if (Var->getType()->isIntegralType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
164 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000165 }
166 // (TD) - a nested-name-specifier or a qualified-id that names a
167 // member of an unknown specialization.
168 // (handled by DependentScopeDeclRefExpr)
169}
170
Sean Huntc3021132010-05-05 15:23:54 +0000171DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000172 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000173 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000174 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000175 QualType T)
176 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000177 DecoratedD(D,
178 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000179 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000180 Loc(NameLoc) {
181 if (Qualifier) {
182 NameQualifier *NQ = getNameQualifier();
183 NQ->NNS = Qualifier;
184 NQ->Range = QualifierRange;
185 }
Sean Huntc3021132010-05-05 15:23:54 +0000186
John McCalld5532b62009-11-23 01:53:49 +0000187 if (TemplateArgs)
188 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000189
190 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000191}
192
193DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
194 NestedNameSpecifier *Qualifier,
195 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000196 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000197 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000198 QualType T,
199 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000200 std::size_t Size = sizeof(DeclRefExpr);
201 if (Qualifier != 0)
202 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000203
John McCalld5532b62009-11-23 01:53:49 +0000204 if (TemplateArgs)
205 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000206
Douglas Gregora2813ce2009-10-23 18:54:35 +0000207 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
208 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000209 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000210}
211
212SourceRange DeclRefExpr::getSourceRange() const {
213 // FIXME: Does not handle multi-token names well, e.g., operator[].
214 SourceRange R(Loc);
Sean Huntc3021132010-05-05 15:23:54 +0000215
Douglas Gregora2813ce2009-10-23 18:54:35 +0000216 if (hasQualifier())
217 R.setBegin(getQualifierRange().getBegin());
218 if (hasExplicitTemplateArgumentList())
219 R.setEnd(getRAngleLoc());
220 return R;
221}
222
Anders Carlsson3a082d82009-09-08 18:24:21 +0000223// FIXME: Maybe this should use DeclPrinter with a special "print predefined
224// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000225std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
226 ASTContext &Context = CurrentDecl->getASTContext();
227
Anders Carlsson3a082d82009-09-08 18:24:21 +0000228 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000229 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000230 return FD->getNameAsString();
231
232 llvm::SmallString<256> Name;
233 llvm::raw_svector_ostream Out(Name);
234
235 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000236 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000237 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000238 if (MD->isStatic())
239 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000240 }
241
242 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000243
244 std::string Proto = FD->getQualifiedNameAsString(Policy);
245
John McCall183700f2009-09-21 23:43:11 +0000246 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000247 const FunctionProtoType *FT = 0;
248 if (FD->hasWrittenPrototype())
249 FT = dyn_cast<FunctionProtoType>(AFT);
250
251 Proto += "(";
252 if (FT) {
253 llvm::raw_string_ostream POut(Proto);
254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
255 if (i) POut << ", ";
256 std::string Param;
257 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
258 POut << Param;
259 }
260
261 if (FT->isVariadic()) {
262 if (FD->getNumParams()) POut << ", ";
263 POut << "...";
264 }
265 }
266 Proto += ")";
267
Sam Weinig4eadcc52009-12-27 01:38:20 +0000268 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
269 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
270 if (ThisQuals.hasConst())
271 Proto += " const";
272 if (ThisQuals.hasVolatile())
273 Proto += " volatile";
274 }
275
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000276 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
277 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000278
279 Out << Proto;
280
281 Out.flush();
282 return Name.str().str();
283 }
284 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
285 llvm::SmallString<256> Name;
286 llvm::raw_svector_ostream Out(Name);
287 Out << (MD->isInstanceMethod() ? '-' : '+');
288 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000289
290 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
291 // a null check to avoid a crash.
292 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000293 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000294
Anders Carlsson3a082d82009-09-08 18:24:21 +0000295 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000296 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
297 Out << '(' << CID << ')';
298
Anders Carlsson3a082d82009-09-08 18:24:21 +0000299 Out << ' ';
300 Out << MD->getSelector().getAsString();
301 Out << ']';
302
303 Out.flush();
304 return Name.str().str();
305 }
306 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
307 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
308 return "top level";
309 }
310 return "";
311}
312
Chris Lattnerda8249e2008-06-07 22:13:43 +0000313/// getValueAsApproximateDouble - This returns the value as an inaccurate
314/// double. Note that this may cause loss of precision, but is useful for
315/// debugging dumps, etc.
316double FloatingLiteral::getValueAsApproximateDouble() const {
317 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000318 bool ignored;
319 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
320 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000321 return V.convertToDouble();
322}
323
Chris Lattner2085fd62009-02-18 06:40:38 +0000324StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
325 unsigned ByteLength, bool Wide,
326 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000327 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000328 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000329 // Allocate enough space for the StringLiteral plus an array of locations for
330 // any concatenated string tokens.
331 void *Mem = C.Allocate(sizeof(StringLiteral)+
332 sizeof(SourceLocation)*(NumStrs-1),
333 llvm::alignof<StringLiteral>());
334 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000337 char *AStrData = new (C, 1) char[ByteLength];
338 memcpy(AStrData, StrData, ByteLength);
339 SL->StrData = AStrData;
340 SL->ByteLength = ByteLength;
341 SL->IsWide = Wide;
342 SL->TokLocs[0] = Loc[0];
343 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
Chris Lattner726e1682009-02-18 05:49:11 +0000345 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000346 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
347 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000348}
349
Douglas Gregor673ecd62009-04-15 16:35:07 +0000350StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
351 void *Mem = C.Allocate(sizeof(StringLiteral)+
352 sizeof(SourceLocation)*(NumStrs-1),
353 llvm::alignof<StringLiteral>());
354 StringLiteral *SL = new (Mem) StringLiteral(QualType());
355 SL->StrData = 0;
356 SL->ByteLength = 0;
357 SL->NumConcatenated = NumStrs;
358 return SL;
359}
360
Douglas Gregor42602bb2009-08-07 06:08:38 +0000361void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000362 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000363 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
Daniel Dunbarb6480232009-09-22 03:27:33 +0000366void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000367 if (StrData)
368 C.Deallocate(const_cast<char*>(StrData));
369
Daniel Dunbarb6480232009-09-22 03:27:33 +0000370 char *AStrData = new (C, 1) char[Str.size()];
371 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000372 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000373 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
377/// corresponds to, e.g. "sizeof" or "[pre]++".
378const char *UnaryOperator::getOpcodeStr(Opcode Op) {
379 switch (Op) {
380 default: assert(0 && "Unknown unary operator");
381 case PostInc: return "++";
382 case PostDec: return "--";
383 case PreInc: return "++";
384 case PreDec: return "--";
385 case AddrOf: return "&";
386 case Deref: return "*";
387 case Plus: return "+";
388 case Minus: return "-";
389 case Not: return "~";
390 case LNot: return "!";
391 case Real: return "__real";
392 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000394 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
396}
397
Mike Stump1eb44332009-09-09 15:08:12 +0000398UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000399UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
400 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000401 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000402 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
403 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
404 case OO_Amp: return AddrOf;
405 case OO_Star: return Deref;
406 case OO_Plus: return Plus;
407 case OO_Minus: return Minus;
408 case OO_Tilde: return Not;
409 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000410 }
411}
412
413OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
414 switch (Opc) {
415 case PostInc: case PreInc: return OO_PlusPlus;
416 case PostDec: case PreDec: return OO_MinusMinus;
417 case AddrOf: return OO_Amp;
418 case Deref: return OO_Star;
419 case Plus: return OO_Plus;
420 case Minus: return OO_Minus;
421 case Not: return OO_Tilde;
422 case LNot: return OO_Exclaim;
423 default: return OO_None;
424 }
425}
426
427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428//===----------------------------------------------------------------------===//
429// Postfix Operators.
430//===----------------------------------------------------------------------===//
431
Ted Kremenek668bf912009-02-09 20:51:47 +0000432CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000433 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000434 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000435 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000436 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000437 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek668bf912009-02-09 20:51:47 +0000439 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000440 SubExprs[FN] = fn;
441 for (unsigned i = 0; i != numargs; ++i)
442 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000443
Douglas Gregorb4609802008-11-14 16:09:21 +0000444 RParenLoc = rparenloc;
445}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000446
Ted Kremenek668bf912009-02-09 20:51:47 +0000447CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
448 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000449 : Expr(CallExprClass, t,
450 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000451 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000452 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000453
454 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000455 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000457 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 RParenLoc = rparenloc;
460}
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
463 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000464 SubExprs = new (C) Stmt*[1];
465}
466
Douglas Gregor42602bb2009-08-07 06:08:38 +0000467void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000468 DestroyChildren(C);
469 if (SubExprs) C.Deallocate(SubExprs);
470 this->~CallExpr();
471 C.Deallocate(this);
472}
473
Nuno Lopesd20254f2009-12-20 23:11:08 +0000474Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000475 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000476 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000477 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000478 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
479 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000480
481 return 0;
482}
483
Nuno Lopesd20254f2009-12-20 23:11:08 +0000484FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000485 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000486}
487
Chris Lattnerd18b3292007-12-28 05:25:02 +0000488/// setNumArgs - This changes the number of arguments present in this call.
489/// Any orphaned expressions are deleted by this, and any new operands are set
490/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000491void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000492 // No change, just return.
493 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerd18b3292007-12-28 05:25:02 +0000495 // If shrinking # arguments, just delete the extras and forgot them.
496 if (NumArgs < getNumArgs()) {
497 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000498 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000499 this->NumArgs = NumArgs;
500 return;
501 }
502
503 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000504 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000505 // Copy over args.
506 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
507 NewSubExprs[i] = SubExprs[i];
508 // Null out new args.
509 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
510 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor88c9a462009-04-17 21:46:47 +0000512 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000513 SubExprs = NewSubExprs;
514 this->NumArgs = NumArgs;
515}
516
Chris Lattnercb888962008-10-06 05:00:53 +0000517/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
518/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000519unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000520 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000521 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000522 // ImplicitCastExpr.
523 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
524 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000525 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000527 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
528 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000529 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlssonbcba2012008-01-31 02:13:57 +0000531 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
532 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000533 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000535 if (!FDecl->getIdentifier())
536 return 0;
537
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000538 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000539}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000540
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000541QualType CallExpr::getCallReturnType() const {
542 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000543 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000544 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000545 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000546 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
John McCall183700f2009-09-21 23:43:11 +0000548 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000549 return FnType->getResultType();
550}
Chris Lattnercb888962008-10-06 05:00:53 +0000551
Sean Huntc3021132010-05-05 15:23:54 +0000552OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000553 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000554 TypeSourceInfo *tsi,
555 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000556 Expr** exprsPtr, unsigned numExprs,
557 SourceLocation RParenLoc) {
558 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000559 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000560 sizeof(Expr*) * numExprs);
561
562 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
563 exprsPtr, numExprs, RParenLoc);
564}
565
566OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
567 unsigned numComps, unsigned numExprs) {
568 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
569 sizeof(OffsetOfNode) * numComps +
570 sizeof(Expr*) * numExprs);
571 return new (Mem) OffsetOfExpr(numComps, numExprs);
572}
573
Sean Huntc3021132010-05-05 15:23:54 +0000574OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000575 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000576 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000577 Expr** exprsPtr, unsigned numExprs,
578 SourceLocation RParenLoc)
Sean Huntc3021132010-05-05 15:23:54 +0000579 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000580 /*ValueDependent=*/tsi->getType()->isDependentType() ||
581 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
582 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Sean Huntc3021132010-05-05 15:23:54 +0000583 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
584 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000585{
586 for(unsigned i = 0; i < numComps; ++i) {
587 setComponent(i, compsPtr[i]);
588 }
Sean Huntc3021132010-05-05 15:23:54 +0000589
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000590 for(unsigned i = 0; i < numExprs; ++i) {
591 setIndexExpr(i, exprsPtr[i]);
592 }
593}
594
595IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
596 assert(getKind() == Field || getKind() == Identifier);
597 if (getKind() == Field)
598 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000599
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000600 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
601}
602
Mike Stump1eb44332009-09-09 15:08:12 +0000603MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
604 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000605 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000606 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000607 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000608 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000609 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000610 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000611 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000612
John McCall161755a2010-04-06 21:38:20 +0000613 bool hasQualOrFound = (qual != 0 ||
614 founddecl.getDecl() != memberdecl ||
615 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000616 if (hasQualOrFound)
617 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
John McCalld5532b62009-11-23 01:53:49 +0000619 if (targs)
620 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000622 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000623 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
624
625 if (hasQualOrFound) {
626 if (qual && qual->isDependent()) {
627 E->setValueDependent(true);
628 E->setTypeDependent(true);
629 }
630 E->HasQualifierOrFoundDecl = true;
631
632 MemberNameQualifier *NQ = E->getMemberQualifier();
633 NQ->NNS = qual;
634 NQ->Range = qualrange;
635 NQ->FoundDecl = founddecl;
636 }
637
638 if (targs) {
639 E->HasExplicitTemplateArgumentList = true;
640 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
641 }
642
643 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000644}
645
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000646const char *CastExpr::getCastKindName() const {
647 switch (getCastKind()) {
648 case CastExpr::CK_Unknown:
649 return "Unknown";
650 case CastExpr::CK_BitCast:
651 return "BitCast";
652 case CastExpr::CK_NoOp:
653 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000654 case CastExpr::CK_BaseToDerived:
655 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000656 case CastExpr::CK_DerivedToBase:
657 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000658 case CastExpr::CK_UncheckedDerivedToBase:
659 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000660 case CastExpr::CK_Dynamic:
661 return "Dynamic";
662 case CastExpr::CK_ToUnion:
663 return "ToUnion";
664 case CastExpr::CK_ArrayToPointerDecay:
665 return "ArrayToPointerDecay";
666 case CastExpr::CK_FunctionToPointerDecay:
667 return "FunctionToPointerDecay";
668 case CastExpr::CK_NullToMemberPointer:
669 return "NullToMemberPointer";
670 case CastExpr::CK_BaseToDerivedMemberPointer:
671 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000672 case CastExpr::CK_DerivedToBaseMemberPointer:
673 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000674 case CastExpr::CK_UserDefinedConversion:
675 return "UserDefinedConversion";
676 case CastExpr::CK_ConstructorConversion:
677 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000678 case CastExpr::CK_IntegralToPointer:
679 return "IntegralToPointer";
680 case CastExpr::CK_PointerToIntegral:
681 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000682 case CastExpr::CK_ToVoid:
683 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000684 case CastExpr::CK_VectorSplat:
685 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000686 case CastExpr::CK_IntegralCast:
687 return "IntegralCast";
688 case CastExpr::CK_IntegralToFloating:
689 return "IntegralToFloating";
690 case CastExpr::CK_FloatingToIntegral:
691 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000692 case CastExpr::CK_FloatingCast:
693 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000694 case CastExpr::CK_MemberPointerToBoolean:
695 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000696 case CastExpr::CK_AnyPointerToObjCPointerCast:
697 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000698 case CastExpr::CK_AnyPointerToBlockPointerCast:
699 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000702 assert(0 && "Unhandled cast kind!");
703 return 0;
704}
705
Anders Carlssona3bdded2010-04-23 21:02:34 +0000706void CastExpr::DoDestroy(ASTContext &C)
707{
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000708 BasePath.Destroy();
Anders Carlssona3bdded2010-04-23 21:02:34 +0000709 Expr::DoDestroy(C);
710}
711
Douglas Gregor6eef5192009-12-14 19:27:10 +0000712Expr *CastExpr::getSubExprAsWritten() {
713 Expr *SubExpr = 0;
714 CastExpr *E = this;
715 do {
716 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000717
Douglas Gregor6eef5192009-12-14 19:27:10 +0000718 // Skip any temporary bindings; they're implicit.
719 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
720 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000721
Douglas Gregor6eef5192009-12-14 19:27:10 +0000722 // Conversions by constructor and conversion functions have a
723 // subexpression describing the call; strip it off.
724 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
725 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
726 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
727 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000728
Douglas Gregor6eef5192009-12-14 19:27:10 +0000729 // If the subexpression we're left with is an implicit cast, look
730 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000731 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
732
Douglas Gregor6eef5192009-12-14 19:27:10 +0000733 return SubExpr;
734}
735
Reid Spencer5f016e22007-07-11 17:01:13 +0000736/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
737/// corresponds to, e.g. "<<=".
738const char *BinaryOperator::getOpcodeStr(Opcode Op) {
739 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000740 case PtrMemD: return ".*";
741 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 case Mul: return "*";
743 case Div: return "/";
744 case Rem: return "%";
745 case Add: return "+";
746 case Sub: return "-";
747 case Shl: return "<<";
748 case Shr: return ">>";
749 case LT: return "<";
750 case GT: return ">";
751 case LE: return "<=";
752 case GE: return ">=";
753 case EQ: return "==";
754 case NE: return "!=";
755 case And: return "&";
756 case Xor: return "^";
757 case Or: return "|";
758 case LAnd: return "&&";
759 case LOr: return "||";
760 case Assign: return "=";
761 case MulAssign: return "*=";
762 case DivAssign: return "/=";
763 case RemAssign: return "%=";
764 case AddAssign: return "+=";
765 case SubAssign: return "-=";
766 case ShlAssign: return "<<=";
767 case ShrAssign: return ">>=";
768 case AndAssign: return "&=";
769 case XorAssign: return "^=";
770 case OrAssign: return "|=";
771 case Comma: return ",";
772 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000773
774 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000775}
776
Mike Stump1eb44332009-09-09 15:08:12 +0000777BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000778BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
779 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000780 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000781 case OO_Plus: return Add;
782 case OO_Minus: return Sub;
783 case OO_Star: return Mul;
784 case OO_Slash: return Div;
785 case OO_Percent: return Rem;
786 case OO_Caret: return Xor;
787 case OO_Amp: return And;
788 case OO_Pipe: return Or;
789 case OO_Equal: return Assign;
790 case OO_Less: return LT;
791 case OO_Greater: return GT;
792 case OO_PlusEqual: return AddAssign;
793 case OO_MinusEqual: return SubAssign;
794 case OO_StarEqual: return MulAssign;
795 case OO_SlashEqual: return DivAssign;
796 case OO_PercentEqual: return RemAssign;
797 case OO_CaretEqual: return XorAssign;
798 case OO_AmpEqual: return AndAssign;
799 case OO_PipeEqual: return OrAssign;
800 case OO_LessLess: return Shl;
801 case OO_GreaterGreater: return Shr;
802 case OO_LessLessEqual: return ShlAssign;
803 case OO_GreaterGreaterEqual: return ShrAssign;
804 case OO_EqualEqual: return EQ;
805 case OO_ExclaimEqual: return NE;
806 case OO_LessEqual: return LE;
807 case OO_GreaterEqual: return GE;
808 case OO_AmpAmp: return LAnd;
809 case OO_PipePipe: return LOr;
810 case OO_Comma: return Comma;
811 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000812 }
813}
814
815OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
816 static const OverloadedOperatorKind OverOps[] = {
817 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
818 OO_Star, OO_Slash, OO_Percent,
819 OO_Plus, OO_Minus,
820 OO_LessLess, OO_GreaterGreater,
821 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
822 OO_EqualEqual, OO_ExclaimEqual,
823 OO_Amp,
824 OO_Caret,
825 OO_Pipe,
826 OO_AmpAmp,
827 OO_PipePipe,
828 OO_Equal, OO_StarEqual,
829 OO_SlashEqual, OO_PercentEqual,
830 OO_PlusEqual, OO_MinusEqual,
831 OO_LessLessEqual, OO_GreaterGreaterEqual,
832 OO_AmpEqual, OO_CaretEqual,
833 OO_PipeEqual,
834 OO_Comma
835 };
836 return OverOps[Opc];
837}
838
Ted Kremenek709210f2010-04-13 23:39:13 +0000839InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000840 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000841 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000842 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000843 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000844 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +0000845 UnionFieldInit(0), HadArrayRangeDesignator(false)
846{
Ted Kremenekba7bc552010-02-19 01:50:18 +0000847 for (unsigned I = 0; I != numInits; ++I) {
848 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000849 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000850 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000851 ValueDependent = true;
852 }
Sean Huntc3021132010-05-05 15:23:54 +0000853
Ted Kremenek709210f2010-04-13 23:39:13 +0000854 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000855}
Reid Spencer5f016e22007-07-11 17:01:13 +0000856
Ted Kremenek709210f2010-04-13 23:39:13 +0000857void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000858 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000859 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000860}
861
Ted Kremenek709210f2010-04-13 23:39:13 +0000862void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000863 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
864 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000865 InitExprs[Idx]->Destroy(C);
866 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000867}
868
Ted Kremenek709210f2010-04-13 23:39:13 +0000869Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000870 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000871 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000872 InitExprs.back() = expr;
873 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Douglas Gregor4c678342009-01-28 21:54:33 +0000876 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
877 InitExprs[Init] = expr;
878 return Result;
879}
880
Steve Naroffbfdcae62008-09-04 15:31:07 +0000881/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000882///
883const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000884 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000885 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000886}
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888SourceLocation BlockExpr::getCaretLocation() const {
889 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000890}
Mike Stump1eb44332009-09-09 15:08:12 +0000891const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000892 return TheBlock->getBody();
893}
Mike Stump1eb44332009-09-09 15:08:12 +0000894Stmt *BlockExpr::getBody() {
895 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000896}
Steve Naroff56ee6892008-10-08 17:01:13 +0000897
898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899//===----------------------------------------------------------------------===//
900// Generic Expression Routines
901//===----------------------------------------------------------------------===//
902
Chris Lattner026dc962009-02-14 07:37:35 +0000903/// isUnusedResultAWarning - Return true if this immediate expression should
904/// be warned about if the result is unused. If so, fill in Loc and Ranges
905/// with location to warn on and the source range[s] to report with the
906/// warning.
907bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000908 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000909 // Don't warn if the expr is type dependent. The type could end up
910 // instantiating to void.
911 if (isTypeDependent())
912 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 switch (getStmtClass()) {
915 default:
John McCall0faede62010-03-12 07:11:26 +0000916 if (getType()->isVoidType())
917 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000918 Loc = getExprLoc();
919 R1 = getSourceRange();
920 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000922 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000923 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000924 case UnaryOperatorClass: {
925 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000928 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 case UnaryOperator::PostInc:
930 case UnaryOperator::PostDec:
931 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000932 case UnaryOperator::PreDec: // ++/--
933 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 case UnaryOperator::Deref:
935 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000936 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000937 return false;
938 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 case UnaryOperator::Real:
940 case UnaryOperator::Imag:
941 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000942 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
943 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000944 return false;
945 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000947 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 }
Chris Lattner026dc962009-02-14 07:37:35 +0000949 Loc = UO->getOperatorLoc();
950 R1 = UO->getSubExpr()->getSourceRange();
951 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000953 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000954 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000955 switch (BO->getOpcode()) {
956 default:
957 break;
958 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
959 case BinaryOperator::Comma:
960 // ((foo = <blah>), 0) is an idiom for hiding the result (and
961 // lvalue-ness) of an assignment written in a macro.
962 if (IntegerLiteral *IE =
963 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
964 if (IE->getValue() == 0)
965 return false;
966 case BinaryOperator::LAnd:
967 case BinaryOperator::LOr:
968 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
969 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000970 }
Chris Lattner026dc962009-02-14 07:37:35 +0000971 if (BO->isAssignmentOp())
972 return false;
973 Loc = BO->getOperatorLoc();
974 R1 = BO->getLHS()->getSourceRange();
975 R2 = BO->getRHS()->getSourceRange();
976 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000977 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000978 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000979 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000981 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000982 // The condition must be evaluated, but if either the LHS or RHS is a
983 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000984 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000985 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000986 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000987 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000988 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000989 }
990
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000992 // If the base pointer or element is to a volatile pointer/field, accessing
993 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000994 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000995 return false;
996 Loc = cast<MemberExpr>(this)->getMemberLoc();
997 R1 = SourceRange(Loc, Loc);
998 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
999 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 case ArraySubscriptExprClass:
1002 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001003 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001004 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001005 return false;
1006 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1007 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1008 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1009 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001012 case CXXOperatorCallExprClass:
1013 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001014 // If this is a direct call, get the callee.
1015 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001016 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001017 // If the callee has attribute pure, const, or warn_unused_result, warn
1018 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001019 //
1020 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1021 // updated to match for QoI.
1022 if (FD->getAttr<WarnUnusedResultAttr>() ||
1023 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1024 Loc = CE->getCallee()->getLocStart();
1025 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001027 if (unsigned NumArgs = CE->getNumArgs())
1028 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1029 CE->getArg(NumArgs-1)->getLocEnd());
1030 return true;
1031 }
Chris Lattner026dc962009-02-14 07:37:35 +00001032 }
1033 return false;
1034 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001035
1036 case CXXTemporaryObjectExprClass:
1037 case CXXConstructExprClass:
1038 return false;
1039
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001040 case ObjCMessageExprClass: {
1041 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1042 const ObjCMethodDecl *MD = ME->getMethodDecl();
1043 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1044 Loc = getExprLoc();
1045 return true;
1046 }
Chris Lattner026dc962009-02-14 07:37:35 +00001047 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001050 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001051#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001052 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001053 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001054 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001055 Loc = Ref->getLocation();
1056 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1057 if (Ref->getBase())
1058 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001059#else
1060 Loc = getExprLoc();
1061 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001062#endif
1063 return true;
1064 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001065 case StmtExprClass: {
1066 // Statement exprs don't logically have side effects themselves, but are
1067 // sometimes used in macros in ways that give them a type that is unused.
1068 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1069 // however, if the result of the stmt expr is dead, we don't want to emit a
1070 // warning.
1071 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1072 if (!CS->body_empty())
1073 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001074 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
John McCall0faede62010-03-12 07:11:26 +00001076 if (getType()->isVoidType())
1077 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001078 Loc = cast<StmtExpr>(this)->getLParenLoc();
1079 R1 = getSourceRange();
1080 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001081 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001082 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001083 // If this is an explicit cast to void, allow it. People do this when they
1084 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001085 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001086 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001087 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1088 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1089 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001090 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001091 if (getType()->isVoidType())
1092 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001093 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001094
Anders Carlsson58beed92009-11-17 17:11:23 +00001095 // If this is a cast to void or a constructor conversion, check the operand.
1096 // Otherwise, the result of the cast is unused.
1097 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1098 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001099 return (cast<CastExpr>(this)->getSubExpr()
1100 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001101 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1102 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1103 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Eli Friedman4be1f472008-05-19 21:24:43 +00001106 case ImplicitCastExprClass:
1107 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001108 return (cast<ImplicitCastExpr>(this)
1109 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001110
Chris Lattner04421082008-04-08 04:40:51 +00001111 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001112 return (cast<CXXDefaultArgExpr>(this)
1113 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001114
1115 case CXXNewExprClass:
1116 // FIXME: In theory, there might be new expressions that don't have side
1117 // effects (e.g. a placement new with an uninitialized POD).
1118 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001119 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001120 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001121 return (cast<CXXBindTemporaryExpr>(this)
1122 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001123 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001124 return (cast<CXXExprWithTemporaries>(this)
1125 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001126 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001127}
1128
Douglas Gregorba7e2102008-10-22 15:04:37 +00001129/// DeclCanBeLvalue - Determine whether the given declaration can be
1130/// an lvalue. This is a helper routine for isLvalue.
1131static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001132 // C++ [temp.param]p6:
1133 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001134 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001135 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1136 return NTTParm->getType()->isReferenceType();
1137
Douglas Gregor44b43212008-12-11 16:49:14 +00001138 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001139 // C++ 3.10p2: An lvalue refers to an object or function.
1140 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001141 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001142}
1143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1145/// incomplete type other than void. Nonarray expressions that can be lvalues:
1146/// - name, where name must be a variable
1147/// - e[i]
1148/// - (e), where e must be an lvalue
1149/// - e.name, where e must be an lvalue
1150/// - e->name
1151/// - *e, the type of e cannot be a function type
1152/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001153/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001154/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001155///
Chris Lattner28be73f2008-07-26 21:30:36 +00001156Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001157 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1158
1159 isLvalueResult Res = isLvalueInternal(Ctx);
1160 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1161 return Res;
1162
Douglas Gregor98cd5992008-10-21 23:43:52 +00001163 // first, check the type (C99 6.3.2.1). Expressions with function
1164 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001165 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 return LV_NotObjectType;
1167
Steve Naroffacb818a2008-02-10 01:39:04 +00001168 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001169 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001170 return LV_IncompleteVoidType;
1171
Eli Friedman53202852009-05-03 22:36:05 +00001172 return LV_Valid;
1173}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001174
Eli Friedman53202852009-05-03 22:36:05 +00001175// Check whether the expression can be sanely treated like an l-value
1176Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001178 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001179 case StringLiteralClass: // C99 6.5.1p4
1180 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001181 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1183 // For vectors, make sure base is an lvalue (i.e. not a function call).
1184 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001185 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001187 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001188 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1189 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 return LV_Valid;
1191 break;
Chris Lattner41110242008-06-17 18:05:57 +00001192 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001193 case BlockDeclRefExprClass: {
1194 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001195 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001196 return LV_Valid;
1197 break;
1198 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001199 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001201 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1202 NamedDecl *Member = m->getMemberDecl();
1203 // C++ [expr.ref]p4:
1204 // If E2 is declared to have type "reference to T", then E1.E2
1205 // is an lvalue.
1206 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1207 if (Value->getType()->isReferenceType())
1208 return LV_Valid;
1209
1210 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001211 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001212 return LV_Valid;
1213
1214 // -- If E2 is a non-static data member [...]. If E1 is an
1215 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001216 if (isa<FieldDecl>(Member)) {
1217 if (m->isArrow())
1218 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001219 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001220 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001221
1222 // -- If it refers to a static member function [...], then
1223 // E1.E2 is an lvalue.
1224 // -- Otherwise, if E1.E2 refers to a non-static member
1225 // function [...], then E1.E2 is not an lvalue.
1226 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1227 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1228
1229 // -- If E2 is a member enumerator [...], the expression E1.E2
1230 // is not an lvalue.
1231 if (isa<EnumConstantDecl>(Member))
1232 return LV_InvalidExpression;
1233
1234 // Not an lvalue.
1235 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001236 }
Sean Huntc3021132010-05-05 15:23:54 +00001237
Douglas Gregor86f19402008-12-20 23:49:58 +00001238 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001239 if (m->isArrow())
1240 return LV_Valid;
1241 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001242 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1243 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001244 return LV_SubObjCPropertySetting;
Sean Huntc3021132010-05-05 15:23:54 +00001245 return
1246 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001247 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001248 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001250 return LV_Valid; // C99 6.5.3p4
1251
1252 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001253 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1254 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001255 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001256
1257 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1258 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1259 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1260 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001262 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001263 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1264 return LV_Valid;
1265
1266 // If this is a conversion to a class temporary, make a note of
1267 // that.
1268 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1269 return LV_ClassTemporary;
1270
1271 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001273 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001274 case BinaryOperatorClass:
1275 case CompoundAssignOperatorClass: {
1276 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001277
1278 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1279 BinOp->getOpcode() == BinaryOperator::Comma)
1280 return BinOp->getRHS()->isLvalue(Ctx);
1281
Sebastian Redl22460502009-02-07 00:15:38 +00001282 // C++ [expr.mptr.oper]p6
Sean Huntc3021132010-05-05 15:23:54 +00001283 // The result of a .* expression is an lvalue only if its first operand is
1284 // an lvalue and its second operand is a pointer to data member.
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001285 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001286 !BinOp->getType()->isFunctionType())
1287 return BinOp->getLHS()->isLvalue(Ctx);
1288
Sean Huntc3021132010-05-05 15:23:54 +00001289 // The result of an ->* expression is an lvalue only if its second operand
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001290 // is a pointer to data member.
1291 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1292 !BinOp->getType()->isFunctionType()) {
1293 QualType Ty = BinOp->getRHS()->getType();
1294 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1295 return LV_Valid;
1296 }
Sean Huntc3021132010-05-05 15:23:54 +00001297
Douglas Gregorbf3af052008-11-13 20:12:29 +00001298 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001299 return LV_InvalidExpression;
1300
Douglas Gregorbf3af052008-11-13 20:12:29 +00001301 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001302 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001303 // The result of an assignment operation [...] is an lvalue.
1304 return LV_Valid;
1305
1306
1307 // C99 6.5.16:
1308 // An assignment expression [...] is not an lvalue.
1309 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001310 }
Mike Stump1eb44332009-09-09 15:08:12 +00001311 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001312 case CXXOperatorCallExprClass:
1313 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001314 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001315 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001316 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001317 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1318 if (ReturnType->isLValueReferenceType())
1319 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001320
Douglas Gregore873fb72010-02-16 21:39:57 +00001321 // If the function is returning a class temporary, make a note of
1322 // that.
1323 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1324 return LV_ClassTemporary;
1325
Douglas Gregor9d293df2008-10-28 00:22:11 +00001326 break;
1327 }
Steve Naroffe6386392007-12-05 04:00:10 +00001328 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001329 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001330 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001331 case ChooseExprClass:
1332 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001333 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001334 case ExtVectorElementExprClass:
1335 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001336 return LV_DuplicateVectorComponents;
1337 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001338 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1339 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001340 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1341 return LV_Valid;
Enea Zaffanella049c51e2010-04-27 07:38:32 +00001342 case ObjCImplicitSetterGetterRefExprClass:
1343 // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001344 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001345 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001346 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001347 case UnresolvedLookupExprClass:
Douglas Gregorad4e02f2010-04-29 18:24:40 +00001348 case UnresolvedMemberExprClass:
John McCallba135432009-11-21 08:51:07 +00001349 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001350 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001351 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001352 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001353 case CXXFunctionalCastExprClass:
1354 case CXXStaticCastExprClass:
1355 case CXXDynamicCastExprClass:
1356 case CXXReinterpretCastExprClass:
1357 case CXXConstCastExprClass:
1358 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001359 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001360 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1361 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001362 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1363 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001364 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001365
1366 // If this is a conversion to a class temporary, make a note of
1367 // that.
Sean Huntc3021132010-05-05 15:23:54 +00001368 if (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregore873fb72010-02-16 21:39:57 +00001369 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1370 return LV_ClassTemporary;
1371
Douglas Gregor9d293df2008-10-28 00:22:11 +00001372 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001373 case CXXTypeidExprClass:
1374 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1375 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001376 case CXXBindTemporaryExprClass:
1377 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1378 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001379 case CXXBindReferenceExprClass:
1380 // Something that's bound to a reference is always an lvalue.
1381 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001382 case ConditionalOperatorClass: {
1383 // Complicated handling is only for C++.
1384 if (!Ctx.getLangOptions().CPlusPlus)
1385 return LV_InvalidExpression;
1386
1387 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1388 // everywhere there's an object converted to an rvalue. Also, any other
1389 // casts should be wrapped by ImplicitCastExprs. There's just the special
1390 // case involving throws to work out.
1391 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001392 Expr *True = Cond->getTrueExpr();
1393 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001394 // C++0x 5.16p2
1395 // If either the second or the third operand has type (cv) void, [...]
1396 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001397 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001398 return LV_InvalidExpression;
1399
1400 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001401 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001402 return LV_InvalidExpression;
1403
1404 // That's it.
1405 return LV_Valid;
1406 }
1407
Douglas Gregor2d48e782009-12-19 07:07:47 +00001408 case Expr::CXXExprWithTemporariesClass:
1409 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1410
1411 case Expr::ObjCMessageExprClass:
1412 if (const ObjCMethodDecl *Method
1413 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1414 if (Method->getResultType()->isLValueReferenceType())
1415 return LV_Valid;
1416 break;
1417
Douglas Gregore873fb72010-02-16 21:39:57 +00001418 case Expr::CXXConstructExprClass:
1419 case Expr::CXXTemporaryObjectExprClass:
1420 case Expr::CXXZeroInitValueExprClass:
1421 return LV_ClassTemporary;
1422
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 default:
1424 break;
1425 }
1426 return LV_InvalidExpression;
1427}
1428
1429/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1430/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001431/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001432/// recursively, any member or element of all contained aggregates or unions)
1433/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001434Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001435Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001436 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001439 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001440 // C++ 3.10p11: Functions cannot be modified, but pointers to
1441 // functions can be modifiable.
1442 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1443 return MLV_NotObjectType;
1444 break;
1445
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 case LV_NotObjectType: return MLV_NotObjectType;
1447 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001448 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001449 case LV_InvalidExpression:
1450 // If the top level is a C-style cast, and the subexpression is a valid
1451 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1452 // GCC extension. We don't support it, but we want to produce good
1453 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001454 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1455 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1456 if (Loc)
1457 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001458 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001459 }
1460 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001461 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001462 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001463 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001464 case LV_ClassTemporary:
1465 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001467
1468 // The following is illegal:
1469 // void takeclosure(void (^C)(void));
1470 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1471 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001472 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001473 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1474 return MLV_NotBlockQualified;
1475 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001476
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001477 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001478 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001479 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1480 if (Expr->getSetterMethod() == 0)
1481 return MLV_NoSetterProperty;
1482 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001483
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001484 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001486 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001488 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001490 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Ted Kremenek6217b802009-07-29 21:53:49 +00001493 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001494 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 return MLV_ConstQualified;
1496 }
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Mike Stump1eb44332009-09-09 15:08:12 +00001498 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001499}
1500
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001501/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001502/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001503bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001504 switch (getStmtClass()) {
1505 default:
1506 return false;
1507 case ObjCIvarRefExprClass:
1508 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001509 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001510 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001511 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001512 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001513 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001514 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001515 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001516 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001517 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001518 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001519 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1520 if (VD->hasGlobalStorage())
1521 return true;
1522 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001523 // dereferencing to a pointer is always a gc'able candidate,
1524 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001525 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001526 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001527 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001528 return false;
1529 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001530 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001531 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001532 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001533 }
1534 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001535 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001536 }
1537}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001538Expr* Expr::IgnoreParens() {
1539 Expr* E = this;
1540 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1541 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001543 return E;
1544}
1545
Chris Lattner56f34942008-02-13 01:02:39 +00001546/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1547/// or CastExprs or ImplicitCastExprs, returning their operand.
1548Expr *Expr::IgnoreParenCasts() {
1549 Expr *E = this;
1550 while (true) {
1551 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1552 E = P->getSubExpr();
1553 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1554 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001555 else
1556 return E;
1557 }
1558}
1559
Chris Lattnerecdd8412009-03-13 17:28:01 +00001560/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1561/// value (including ptr->int casts of the same size). Strip off any
1562/// ParenExpr or CastExprs, returning their operand.
1563Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1564 Expr *E = this;
1565 while (true) {
1566 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1567 E = P->getSubExpr();
1568 continue;
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Chris Lattnerecdd8412009-03-13 17:28:01 +00001571 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1572 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1573 // ptr<->int casts of the same width. We also ignore all identify casts.
1574 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Chris Lattnerecdd8412009-03-13 17:28:01 +00001576 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1577 E = SE;
1578 continue;
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Chris Lattnerecdd8412009-03-13 17:28:01 +00001581 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1582 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1583 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1584 E = SE;
1585 continue;
1586 }
1587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Chris Lattnerecdd8412009-03-13 17:28:01 +00001589 return E;
1590 }
1591}
1592
Douglas Gregor6eef5192009-12-14 19:27:10 +00001593bool Expr::isDefaultArgument() const {
1594 const Expr *E = this;
1595 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1596 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001597
Douglas Gregor6eef5192009-12-14 19:27:10 +00001598 return isa<CXXDefaultArgExpr>(E);
1599}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001600
Douglas Gregor2f599792010-04-02 18:24:57 +00001601/// \brief Skip over any no-op casts and any temporary-binding
1602/// expressions.
1603static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1604 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1605 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1606 E = ICE->getSubExpr();
1607 else
1608 break;
1609 }
1610
1611 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1612 E = BE->getSubExpr();
1613
1614 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1615 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1616 E = ICE->getSubExpr();
1617 else
1618 break;
1619 }
Sean Huntc3021132010-05-05 15:23:54 +00001620
Douglas Gregor2f599792010-04-02 18:24:57 +00001621 return E;
1622}
1623
1624const Expr *Expr::getTemporaryObject() const {
1625 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1626
1627 // A cast can produce a temporary object. The object's construction
1628 // is represented as a CXXConstructExpr.
1629 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1630 // Only user-defined and constructor conversions can produce
1631 // temporary objects.
1632 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1633 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1634 return 0;
1635
1636 // Strip off temporary bindings and no-op casts.
1637 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1638
1639 // If this is a constructor conversion, see if we have an object
1640 // construction.
1641 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1642 return dyn_cast<CXXConstructExpr>(Sub);
1643
1644 // If this is a user-defined conversion, see if we have a call to
1645 // a function that itself returns a temporary object.
1646 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1647 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1648 if (CE->getCallReturnType()->isRecordType())
1649 return CE;
1650
1651 return 0;
1652 }
1653
1654 // A call returning a class type returns a temporary.
1655 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1656 if (CE->getCallReturnType()->isRecordType())
1657 return CE;
1658
1659 return 0;
1660 }
1661
1662 // Explicit temporary object constructors create temporaries.
1663 return dyn_cast<CXXTemporaryObjectExpr>(E);
1664}
1665
Douglas Gregor898574e2008-12-05 23:32:09 +00001666/// hasAnyTypeDependentArguments - Determines if any of the expressions
1667/// in Exprs is type-dependent.
1668bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1669 for (unsigned I = 0; I < NumExprs; ++I)
1670 if (Exprs[I]->isTypeDependent())
1671 return true;
1672
1673 return false;
1674}
1675
1676/// hasAnyValueDependentArguments - Determines if any of the expressions
1677/// in Exprs is value-dependent.
1678bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1679 for (unsigned I = 0; I < NumExprs; ++I)
1680 if (Exprs[I]->isValueDependent())
1681 return true;
1682
1683 return false;
1684}
1685
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001686bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001687 // This function is attempting whether an expression is an initializer
1688 // which can be evaluated at compile-time. isEvaluatable handles most
1689 // of the cases, but it can't deal with some initializer-specific
1690 // expressions, and it can't deal with aggregates; we deal with those here,
1691 // and fall back to isEvaluatable for the other cases.
1692
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001693 // FIXME: This function assumes the variable being assigned to
1694 // isn't a reference type!
1695
Anders Carlssone8a32b82008-11-24 05:23:59 +00001696 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001697 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001698 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001699 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001700 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001701 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001702 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001703 // This handles gcc's extension that allows global initializers like
1704 // "struct x {int x;} x = (struct x) {};".
1705 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001706 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001707 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001708 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001709 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001710 // FIXME: This doesn't deal with fields with reference types correctly.
1711 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1712 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001713 const InitListExpr *Exp = cast<InitListExpr>(this);
1714 unsigned numInits = Exp->getNumInits();
1715 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001716 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001717 return false;
1718 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001719 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001720 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001721 case ImplicitValueInitExprClass:
1722 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001723 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001724 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001725 case UnaryOperatorClass: {
1726 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1727 if (Exp->getOpcode() == UnaryOperator::Extension)
1728 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1729 break;
1730 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001731 case BinaryOperatorClass: {
1732 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1733 // but this handles the common case.
1734 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1735 if (Exp->getOpcode() == BinaryOperator::Sub &&
1736 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1737 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1738 return true;
1739 break;
1740 }
Chris Lattner81045d82009-04-21 05:19:11 +00001741 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001742 case CStyleCastExprClass:
1743 // Handle casts with a destination that's a struct or union; this
1744 // deals with both the gcc no-op struct cast extension and the
1745 // cast-to-union extension.
1746 if (getType()->isRecordType())
1747 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001748
Chris Lattner430656e2009-10-13 22:12:09 +00001749 // Integer->integer casts can be handled here, which is important for
1750 // things like (int)(&&x-&&y). Scary but true.
1751 if (getType()->isIntegerType() &&
1752 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1753 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001754
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001755 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001756 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001757 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001758}
1759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001761/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001762
1763/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1764/// comma, etc
1765///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001766/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1767/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1768/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001769
Eli Friedmane28d7192009-02-26 09:29:13 +00001770// CheckICE - This function does the fundamental ICE checking: the returned
1771// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1772// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001773// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001774// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001775// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001776//
1777// Meanings of Val:
1778// 0: This expression is an ICE if it can be evaluated by Evaluate.
1779// 1: This expression is not an ICE, but if it isn't evaluated, it's
1780// a legal subexpression for an ICE. This return value is used to handle
1781// the comma operator in C99 mode.
1782// 2: This expression is not an ICE, and is not a legal subexpression for one.
1783
1784struct ICEDiag {
1785 unsigned Val;
1786 SourceLocation Loc;
1787
1788 public:
1789 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1790 ICEDiag() : Val(0) {}
1791};
1792
1793ICEDiag NoDiag() { return ICEDiag(); }
1794
Eli Friedman60ce9632009-02-27 04:07:58 +00001795static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1796 Expr::EvalResult EVResult;
1797 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1798 !EVResult.Val.isInt()) {
1799 return ICEDiag(2, E->getLocStart());
1800 }
1801 return NoDiag();
1802}
1803
Eli Friedmane28d7192009-02-26 09:29:13 +00001804static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001805 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001806 if (!E->getType()->isIntegralType()) {
1807 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001808 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001809
1810 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001811#define STMT(Node, Base) case Expr::Node##Class:
1812#define EXPR(Node, Base)
Sean Huntc3021132010-05-05 15:23:54 +00001813#include "clang/AST/StmtNodes.def"
Douglas Gregorf2991242009-09-10 23:31:45 +00001814 case Expr::PredefinedExprClass:
1815 case Expr::FloatingLiteralClass:
1816 case Expr::ImaginaryLiteralClass:
1817 case Expr::StringLiteralClass:
1818 case Expr::ArraySubscriptExprClass:
1819 case Expr::MemberExprClass:
1820 case Expr::CompoundAssignOperatorClass:
1821 case Expr::CompoundLiteralExprClass:
1822 case Expr::ExtVectorElementExprClass:
1823 case Expr::InitListExprClass:
1824 case Expr::DesignatedInitExprClass:
1825 case Expr::ImplicitValueInitExprClass:
1826 case Expr::ParenListExprClass:
1827 case Expr::VAArgExprClass:
1828 case Expr::AddrLabelExprClass:
1829 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001830 case Expr::CXXMemberCallExprClass:
1831 case Expr::CXXDynamicCastExprClass:
1832 case Expr::CXXTypeidExprClass:
1833 case Expr::CXXNullPtrLiteralExprClass:
1834 case Expr::CXXThisExprClass:
1835 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001836 case Expr::CXXNewExprClass:
1837 case Expr::CXXDeleteExprClass:
1838 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001839 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001840 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001841 case Expr::CXXConstructExprClass:
1842 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001843 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001844 case Expr::CXXExprWithTemporariesClass:
1845 case Expr::CXXTemporaryObjectExprClass:
1846 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001847 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001848 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001849 case Expr::ObjCStringLiteralClass:
1850 case Expr::ObjCEncodeExprClass:
1851 case Expr::ObjCMessageExprClass:
1852 case Expr::ObjCSelectorExprClass:
1853 case Expr::ObjCProtocolExprClass:
1854 case Expr::ObjCIvarRefExprClass:
1855 case Expr::ObjCPropertyRefExprClass:
1856 case Expr::ObjCImplicitSetterGetterRefExprClass:
1857 case Expr::ObjCSuperExprClass:
1858 case Expr::ObjCIsaExprClass:
1859 case Expr::ShuffleVectorExprClass:
1860 case Expr::BlockExprClass:
1861 case Expr::BlockDeclRefExprClass:
1862 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001863 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001864
Douglas Gregor043cad22009-09-11 00:18:58 +00001865 case Expr::GNUNullExprClass:
1866 // GCC considers the GNU __null value to be an integral constant expression.
1867 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001868
Eli Friedmane28d7192009-02-26 09:29:13 +00001869 case Expr::ParenExprClass:
1870 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1871 case Expr::IntegerLiteralClass:
1872 case Expr::CharacterLiteralClass:
1873 case Expr::CXXBoolLiteralExprClass:
1874 case Expr::CXXZeroInitValueExprClass:
1875 case Expr::TypesCompatibleExprClass:
1876 case Expr::UnaryTypeTraitExprClass:
1877 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001878 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001879 case Expr::CXXOperatorCallExprClass: {
1880 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001881 if (CE->isBuiltinCall(Ctx))
1882 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001883 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001884 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001885 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001886 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1887 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001888 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001889 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001890 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1891
1892 // Parameter variables are never constants. Without this check,
1893 // getAnyInitializer() can find a default argument, which leads
1894 // to chaos.
1895 if (isa<ParmVarDecl>(D))
1896 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1897
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001898 // C++ 7.1.5.1p2
1899 // A variable of non-volatile const-qualified integral or enumeration
1900 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001901 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001902 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1903 if (Quals.hasVolatile() || !Quals.hasConst())
1904 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
Sean Huntc3021132010-05-05 15:23:54 +00001905
Sebastian Redl31310a22010-02-01 20:16:42 +00001906 // Look for a declaration of this variable that has an initializer.
1907 const VarDecl *ID = 0;
1908 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001909 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001910 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001911 // We have already checked whether this subexpression is an
1912 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001913 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001914 return NoDiag();
1915 else
1916 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1917 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001918
John McCall1f1b3b32010-02-06 01:07:37 +00001919 // It's an ICE whether or not the definition we found is
1920 // out-of-line. See DR 721 and the discussion in Clang PR
1921 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001922
1923 if (Dcl->isCheckingICE()) {
1924 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1925 }
1926
1927 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001928 ICEDiag Result = CheckICE(Init, Ctx);
1929 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001930 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001931 return Result;
1932 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001933 }
1934 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001935 return ICEDiag(2, E->getLocStart());
1936 case Expr::UnaryOperatorClass: {
1937 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001939 case UnaryOperator::PostInc:
1940 case UnaryOperator::PostDec:
1941 case UnaryOperator::PreInc:
1942 case UnaryOperator::PreDec:
1943 case UnaryOperator::AddrOf:
1944 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001945 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001947 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001951 case UnaryOperator::Real:
1952 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001953 return CheckICE(Exp->getSubExpr(), Ctx);
Douglas Gregor72be24f2010-04-30 20:35:01 +00001954 case UnaryOperator::OffsetOf:
1955 break;
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001956 }
Douglas Gregor72be24f2010-04-30 20:35:01 +00001957
1958 // OffsetOf falls through here.
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001959 }
1960 case Expr::OffsetOfExprClass: {
Eli Friedman60ce9632009-02-27 04:07:58 +00001961 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1962 // Evaluate matches the proposed gcc behavior for cases like
1963 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1964 // compliance: we should warn earlier for offsetof expressions with
1965 // array subscripts that aren't ICEs, and if the array subscripts
1966 // are ICEs, the value of the offsetof must be an integer constant.
1967 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001969 case Expr::SizeOfAlignOfExprClass: {
1970 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1971 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1972 return ICEDiag(2, E->getLocStart());
1973 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001975 case Expr::BinaryOperatorClass: {
1976 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001978 case BinaryOperator::PtrMemD:
1979 case BinaryOperator::PtrMemI:
1980 case BinaryOperator::Assign:
1981 case BinaryOperator::MulAssign:
1982 case BinaryOperator::DivAssign:
1983 case BinaryOperator::RemAssign:
1984 case BinaryOperator::AddAssign:
1985 case BinaryOperator::SubAssign:
1986 case BinaryOperator::ShlAssign:
1987 case BinaryOperator::ShrAssign:
1988 case BinaryOperator::AndAssign:
1989 case BinaryOperator::XorAssign:
1990 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001991 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001996 case BinaryOperator::Add:
1997 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001999 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00002000 case BinaryOperator::LT:
2001 case BinaryOperator::GT:
2002 case BinaryOperator::LE:
2003 case BinaryOperator::GE:
2004 case BinaryOperator::EQ:
2005 case BinaryOperator::NE:
2006 case BinaryOperator::And:
2007 case BinaryOperator::Xor:
2008 case BinaryOperator::Or:
2009 case BinaryOperator::Comma: {
2010 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2011 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002012 if (Exp->getOpcode() == BinaryOperator::Div ||
2013 Exp->getOpcode() == BinaryOperator::Rem) {
2014 // Evaluate gives an error for undefined Div/Rem, so make sure
2015 // we don't evaluate one.
2016 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
2017 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
2018 if (REval == 0)
2019 return ICEDiag(1, E->getLocStart());
2020 if (REval.isSigned() && REval.isAllOnesValue()) {
2021 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
2022 if (LEval.isMinSignedValue())
2023 return ICEDiag(1, E->getLocStart());
2024 }
2025 }
2026 }
2027 if (Exp->getOpcode() == BinaryOperator::Comma) {
2028 if (Ctx.getLangOptions().C99) {
2029 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
2030 // if it isn't evaluated.
2031 if (LHSResult.Val == 0 && RHSResult.Val == 0)
2032 return ICEDiag(1, E->getLocStart());
2033 } else {
2034 // In both C89 and C++, commas in ICEs are illegal.
2035 return ICEDiag(2, E->getLocStart());
2036 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002037 }
2038 if (LHSResult.Val >= RHSResult.Val)
2039 return LHSResult;
2040 return RHSResult;
2041 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00002043 case BinaryOperator::LOr: {
2044 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
2045 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
2046 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
2047 // Rare case where the RHS has a comma "side-effect"; we need
2048 // to actually check the condition to see whether the side
2049 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00002050 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00002051 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00002052 return RHSResult;
2053 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00002054 }
Eli Friedman60ce9632009-02-27 04:07:58 +00002055
Eli Friedmane28d7192009-02-26 09:29:13 +00002056 if (LHSResult.Val >= RHSResult.Val)
2057 return LHSResult;
2058 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002062 case Expr::ImplicitCastExprClass:
2063 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00002064 case Expr::CXXFunctionalCastExprClass:
2065 case Expr::CXXStaticCastExprClass:
2066 case Expr::CXXReinterpretCastExprClass:
2067 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00002068 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2069 if (SubExpr->getType()->isIntegralType())
2070 return CheckICE(SubExpr, Ctx);
2071 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2072 return NoDiag();
2073 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00002074 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002075 case Expr::ConditionalOperatorClass: {
2076 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002077 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00002078 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00002079 // expression, and it is fully evaluated. This is an important GNU
2080 // extension. See GCC PR38377 for discussion.
Enea Zaffanella049c51e2010-04-27 07:38:32 +00002081 if (const CallExpr *CallCE
2082 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00002083 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002084 Expr::EvalResult EVResult;
2085 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2086 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00002087 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002088 }
2089 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00002090 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002091 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2092 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2093 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2094 if (CondResult.Val == 2)
2095 return CondResult;
2096 if (TrueResult.Val == 2)
2097 return TrueResult;
2098 if (FalseResult.Val == 2)
2099 return FalseResult;
2100 if (CondResult.Val == 1)
2101 return CondResult;
2102 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2103 return NoDiag();
2104 // Rare case where the diagnostics depend on which side is evaluated
2105 // Note that if we get here, CondResult is 0, and at least one of
2106 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00002107 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002108 return FalseResult;
2109 }
2110 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00002111 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002112 case Expr::CXXDefaultArgExprClass:
2113 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002114 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00002115 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002116 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002118
Douglas Gregorf2991242009-09-10 23:31:45 +00002119 // Silence a GCC warning
2120 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002121}
Reid Spencer5f016e22007-07-11 17:01:13 +00002122
Eli Friedmane28d7192009-02-26 09:29:13 +00002123bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2124 SourceLocation *Loc, bool isEvaluated) const {
2125 ICEDiag d = CheckICE(this, Ctx);
2126 if (d.Val != 0) {
2127 if (Loc) *Loc = d.Loc;
2128 return false;
2129 }
2130 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00002131 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002132 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00002133 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2134 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00002135 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00002136 return true;
2137}
2138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2140/// integer constant expression with the value zero, or if this is one that is
2141/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002142bool Expr::isNullPointerConstant(ASTContext &Ctx,
2143 NullPointerConstantValueDependence NPC) const {
2144 if (isValueDependent()) {
2145 switch (NPC) {
2146 case NPC_NeverValueDependent:
2147 assert(false && "Unexpected value dependent expression!");
2148 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002149
Douglas Gregorce940492009-09-25 04:25:58 +00002150 case NPC_ValueDependentIsNull:
2151 return isTypeDependent() || getType()->isIntegralType();
Sean Huntc3021132010-05-05 15:23:54 +00002152
Douglas Gregorce940492009-09-25 04:25:58 +00002153 case NPC_ValueDependentIsNotNull:
2154 return false;
2155 }
2156 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002157
Sebastian Redl07779722008-10-31 14:43:28 +00002158 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002159 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002160 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002161 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002162 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002163 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002164 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002165 Pointee->isVoidType() && // to void*
2166 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002167 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002168 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002169 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002170 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2171 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002172 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002173 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2174 // Accept ((void*)0) as a null pointer constant, as many other
2175 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002176 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002177 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002178 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002179 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002180 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002181 } else if (isa<GNUNullExpr>(this)) {
2182 // The GNU __null extension is always a null pointer constant.
2183 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002184 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002185
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002186 // C++0x nullptr_t is always a null pointer constant.
2187 if (getType()->isNullPtrType())
2188 return true;
2189
Steve Naroffaa58f002008-01-14 16:10:57 +00002190 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002191 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002192 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002193 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 // If we have an integer constant expression, we need to *evaluate* it and
2196 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002197 llvm::APSInt Result;
2198 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002199}
Steve Naroff31a45842007-07-28 23:10:27 +00002200
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002201FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002202 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002203
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002204 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2205 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2206 E = ICE->getSubExpr()->IgnoreParens();
2207 else
2208 break;
2209 }
2210
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002211 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002212 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002213 if (Field->isBitField())
2214 return Field;
2215
2216 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2217 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2218 return BinOp->getLHS()->getBitField();
2219
2220 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002221}
2222
Anders Carlsson09380262010-01-31 17:18:49 +00002223bool Expr::refersToVectorElement() const {
2224 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002225
Anders Carlsson09380262010-01-31 17:18:49 +00002226 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2227 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2228 E = ICE->getSubExpr()->IgnoreParens();
2229 else
2230 break;
2231 }
Sean Huntc3021132010-05-05 15:23:54 +00002232
Anders Carlsson09380262010-01-31 17:18:49 +00002233 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2234 return ASE->getBase()->getType()->isVectorType();
2235
2236 if (isa<ExtVectorElementExpr>(E))
2237 return true;
2238
2239 return false;
2240}
2241
Chris Lattner2140e902009-02-16 22:14:05 +00002242/// isArrow - Return true if the base expression is a pointer to vector,
2243/// return false if the base expression is a vector.
2244bool ExtVectorElementExpr::isArrow() const {
2245 return getBase()->getType()->isPointerType();
2246}
2247
Nate Begeman213541a2008-04-18 23:10:10 +00002248unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002249 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002250 return VT->getNumElements();
2251 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002252}
2253
Nate Begeman8a997642008-05-09 06:41:27 +00002254/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002255bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002256 // FIXME: Refactor this code to an accessor on the AST node which returns the
2257 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002258 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002259
2260 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002261 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002262 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002263
Nate Begeman190d6a22009-01-18 02:01:21 +00002264 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002265 if (Comp[0] == 's' || Comp[0] == 'S')
2266 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Daniel Dunbar15027422009-10-17 23:53:04 +00002268 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2269 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002270 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002271
Steve Narofffec0b492007-07-30 03:29:09 +00002272 return false;
2273}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002274
Nate Begeman8a997642008-05-09 06:41:27 +00002275/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002276void ExtVectorElementExpr::getEncodedElementAccess(
2277 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002278 llvm::StringRef Comp = Accessor->getName();
2279 if (Comp[0] == 's' || Comp[0] == 'S')
2280 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002282 bool isHi = Comp == "hi";
2283 bool isLo = Comp == "lo";
2284 bool isEven = Comp == "even";
2285 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002286
Nate Begeman8a997642008-05-09 06:41:27 +00002287 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2288 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Nate Begeman8a997642008-05-09 06:41:27 +00002290 if (isHi)
2291 Index = e + i;
2292 else if (isLo)
2293 Index = i;
2294 else if (isEven)
2295 Index = 2 * i;
2296 else if (isOdd)
2297 Index = 2 * i + 1;
2298 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002299 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002300
Nate Begeman3b8d1162008-05-13 21:03:02 +00002301 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002302 }
Nate Begeman8a997642008-05-09 06:41:27 +00002303}
2304
Douglas Gregor04badcf2010-04-21 00:45:42 +00002305ObjCMessageExpr::ObjCMessageExpr(QualType T,
2306 SourceLocation LBracLoc,
2307 SourceLocation SuperLoc,
2308 bool IsInstanceSuper,
2309 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002310 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002311 ObjCMethodDecl *Method,
2312 Expr **Args, unsigned NumArgs,
2313 SourceLocation RBracLoc)
2314 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002315 /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002316 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2317 HasMethod(Method != 0), SuperLoc(SuperLoc),
2318 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2319 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002320 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002321{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002322 setReceiverPointer(SuperType.getAsOpaquePtr());
2323 if (NumArgs)
2324 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002325}
2326
Douglas Gregor04badcf2010-04-21 00:45:42 +00002327ObjCMessageExpr::ObjCMessageExpr(QualType T,
2328 SourceLocation LBracLoc,
2329 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002330 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002331 ObjCMethodDecl *Method,
2332 Expr **Args, unsigned NumArgs,
2333 SourceLocation RBracLoc)
2334 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00002335 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002336 hasAnyValueDependentArguments(Args, NumArgs))),
2337 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2338 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2339 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002340 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002341{
2342 setReceiverPointer(Receiver);
2343 if (NumArgs)
2344 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002345}
2346
Douglas Gregor04badcf2010-04-21 00:45:42 +00002347ObjCMessageExpr::ObjCMessageExpr(QualType T,
2348 SourceLocation LBracLoc,
2349 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002350 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002351 ObjCMethodDecl *Method,
2352 Expr **Args, unsigned NumArgs,
2353 SourceLocation RBracLoc)
Douglas Gregor92e986e2010-04-22 16:44:27 +00002354 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00002355 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002356 hasAnyValueDependentArguments(Args, NumArgs))),
2357 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2358 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2359 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002360 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002361{
2362 setReceiverPointer(Receiver);
2363 if (NumArgs)
2364 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002365}
2366
Douglas Gregor04badcf2010-04-21 00:45:42 +00002367ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2368 SourceLocation LBracLoc,
2369 SourceLocation SuperLoc,
2370 bool IsInstanceSuper,
2371 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002372 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002373 ObjCMethodDecl *Method,
2374 Expr **Args, unsigned NumArgs,
2375 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002376 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002377 NumArgs * sizeof(Expr *);
2378 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2379 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00002380 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002381 RBracLoc);
2382}
2383
2384ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2385 SourceLocation LBracLoc,
2386 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002387 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002388 ObjCMethodDecl *Method,
2389 Expr **Args, unsigned NumArgs,
2390 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002391 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002392 NumArgs * sizeof(Expr *);
2393 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00002394 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002395 NumArgs, RBracLoc);
2396}
2397
2398ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2399 SourceLocation LBracLoc,
2400 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002401 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002402 ObjCMethodDecl *Method,
2403 Expr **Args, unsigned NumArgs,
2404 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002405 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002406 NumArgs * sizeof(Expr *);
2407 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00002408 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002409 NumArgs, RBracLoc);
2410}
2411
Sean Huntc3021132010-05-05 15:23:54 +00002412ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002413 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002414 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002415 NumArgs * sizeof(Expr *);
2416 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2417 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2418}
Sean Huntc3021132010-05-05 15:23:54 +00002419
Douglas Gregor04badcf2010-04-21 00:45:42 +00002420Selector ObjCMessageExpr::getSelector() const {
2421 if (HasMethod)
2422 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2423 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002424 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002425}
2426
2427ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2428 switch (getReceiverKind()) {
2429 case Instance:
2430 if (const ObjCObjectPointerType *Ptr
2431 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2432 return Ptr->getInterfaceDecl();
2433 break;
2434
2435 case Class:
2436 if (const ObjCInterfaceType *Iface
2437 = getClassReceiver()->getAs<ObjCInterfaceType>())
2438 return Iface->getDecl();
2439 break;
2440
2441 case SuperInstance:
2442 if (const ObjCObjectPointerType *Ptr
2443 = getSuperType()->getAs<ObjCObjectPointerType>())
2444 return Ptr->getInterfaceDecl();
2445 break;
2446
2447 case SuperClass:
2448 if (const ObjCObjectPointerType *Iface
2449 = getSuperType()->getAs<ObjCObjectPointerType>())
2450 return Iface->getInterfaceDecl();
2451 break;
2452 }
2453
2454 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002455}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002456
Chris Lattner27437ca2007-10-25 00:29:32 +00002457bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002458 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002459}
2460
Nate Begeman888376a2009-08-12 02:28:50 +00002461void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2462 unsigned NumExprs) {
2463 if (SubExprs) C.Deallocate(SubExprs);
2464
2465 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002466 this->NumExprs = NumExprs;
2467 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002468}
Nate Begeman888376a2009-08-12 02:28:50 +00002469
2470void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2471 DestroyChildren(C);
2472 if (SubExprs) C.Deallocate(SubExprs);
2473 this->~ShuffleVectorExpr();
2474 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002475}
2476
Douglas Gregor42602bb2009-08-07 06:08:38 +00002477void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002478 // Override default behavior of traversing children. If this has a type
2479 // operand and the type is a variable-length array, the child iteration
2480 // will iterate over the size expression. However, this expression belongs
2481 // to the type, not to this, so we don't want to delete it.
2482 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002483 if (isArgumentType()) {
2484 this->~SizeOfAlignOfExpr();
2485 C.Deallocate(this);
2486 }
Sebastian Redl05189992008-11-11 17:56:53 +00002487 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002488 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002489}
2490
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002491//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002492// DesignatedInitExpr
2493//===----------------------------------------------------------------------===//
2494
2495IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2496 assert(Kind == FieldDesignator && "Only valid on a field designator");
2497 if (Field.NameOrField & 0x01)
2498 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2499 else
2500 return getField()->getIdentifier();
2501}
2502
Sean Huntc3021132010-05-05 15:23:54 +00002503DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002504 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002505 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002506 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002507 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002508 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002509 unsigned NumIndexExprs,
2510 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002511 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002512 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002513 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2514 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002515 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002516
2517 // Record the initializer itself.
2518 child_iterator Child = child_begin();
2519 *Child++ = Init;
2520
2521 // Copy the designators and their subexpressions, computing
2522 // value-dependence along the way.
2523 unsigned IndexIdx = 0;
2524 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002525 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002526
2527 if (this->Designators[I].isArrayDesignator()) {
2528 // Compute type- and value-dependence.
2529 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002530 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002531 Index->isTypeDependent() || Index->isValueDependent();
2532
2533 // Copy the index expressions into permanent storage.
2534 *Child++ = IndexExprs[IndexIdx++];
2535 } else if (this->Designators[I].isArrayRangeDesignator()) {
2536 // Compute type- and value-dependence.
2537 Expr *Start = IndexExprs[IndexIdx];
2538 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002539 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002540 Start->isTypeDependent() || Start->isValueDependent() ||
2541 End->isTypeDependent() || End->isValueDependent();
2542
2543 // Copy the start/end expressions into permanent storage.
2544 *Child++ = IndexExprs[IndexIdx++];
2545 *Child++ = IndexExprs[IndexIdx++];
2546 }
2547 }
2548
2549 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002550}
2551
Douglas Gregor05c13a32009-01-22 00:58:24 +00002552DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002553DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002554 unsigned NumDesignators,
2555 Expr **IndexExprs, unsigned NumIndexExprs,
2556 SourceLocation ColonOrEqualLoc,
2557 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002558 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002559 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002560 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002561 ColonOrEqualLoc, UsesColonSyntax,
2562 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002563}
2564
Mike Stump1eb44332009-09-09 15:08:12 +00002565DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002566 unsigned NumIndexExprs) {
2567 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2568 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2569 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2570}
2571
Douglas Gregor319d57f2010-01-06 23:17:19 +00002572void DesignatedInitExpr::setDesignators(ASTContext &C,
2573 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002574 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002575 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002576
Douglas Gregor319d57f2010-01-06 23:17:19 +00002577 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002578 NumDesignators = NumDesigs;
2579 for (unsigned I = 0; I != NumDesigs; ++I)
2580 Designators[I] = Desigs[I];
2581}
2582
Douglas Gregor05c13a32009-01-22 00:58:24 +00002583SourceRange DesignatedInitExpr::getSourceRange() const {
2584 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002585 Designator &First =
2586 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002587 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002588 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002589 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2590 else
2591 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2592 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002593 StartLoc =
2594 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002595 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2596}
2597
Douglas Gregor05c13a32009-01-22 00:58:24 +00002598Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2599 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2600 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2601 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002602 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2603 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2604}
2605
2606Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002607 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002608 "Requires array range designator");
2609 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2610 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002611 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2612 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2613}
2614
2615Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002616 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002617 "Requires array range designator");
2618 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2619 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002620 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2621 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2622}
2623
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002624/// \brief Replaces the designator at index @p Idx with the series
2625/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002626void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002627 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002628 const Designator *Last) {
2629 unsigned NumNewDesignators = Last - First;
2630 if (NumNewDesignators == 0) {
2631 std::copy_backward(Designators + Idx + 1,
2632 Designators + NumDesignators,
2633 Designators + Idx);
2634 --NumNewDesignators;
2635 return;
2636 } else if (NumNewDesignators == 1) {
2637 Designators[Idx] = *First;
2638 return;
2639 }
2640
Mike Stump1eb44332009-09-09 15:08:12 +00002641 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002642 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002643 std::copy(Designators, Designators + Idx, NewDesignators);
2644 std::copy(First, Last, NewDesignators + Idx);
2645 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2646 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002647 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002648 Designators = NewDesignators;
2649 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2650}
2651
Douglas Gregor42602bb2009-08-07 06:08:38 +00002652void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002653 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002654 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002655}
2656
Douglas Gregor319d57f2010-01-06 23:17:19 +00002657void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2658 for (unsigned I = 0; I != NumDesignators; ++I)
2659 Designators[I].~Designator();
2660 C.Deallocate(Designators);
2661 Designators = 0;
2662}
2663
Mike Stump1eb44332009-09-09 15:08:12 +00002664ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002665 Expr **exprs, unsigned nexprs,
2666 SourceLocation rparenloc)
2667: Expr(ParenListExprClass, QualType(),
2668 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002669 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002670 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002671
Nate Begeman2ef13e52009-08-10 23:49:36 +00002672 Exprs = new (C) Stmt*[nexprs];
2673 for (unsigned i = 0; i != nexprs; ++i)
2674 Exprs[i] = exprs[i];
2675}
2676
2677void ParenListExpr::DoDestroy(ASTContext& C) {
2678 DestroyChildren(C);
2679 if (Exprs) C.Deallocate(Exprs);
2680 this->~ParenListExpr();
2681 C.Deallocate(this);
2682}
2683
Douglas Gregor05c13a32009-01-22 00:58:24 +00002684//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002685// ExprIterator.
2686//===----------------------------------------------------------------------===//
2687
2688Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2689Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2690Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2691const Expr* ConstExprIterator::operator[](size_t idx) const {
2692 return cast<Expr>(I[idx]);
2693}
2694const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2695const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2696
2697//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002698// Child Iterators for iterating over subexpressions/substatements
2699//===----------------------------------------------------------------------===//
2700
2701// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002702Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2703Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002704
Steve Naroff7779db42007-11-12 14:29:37 +00002705// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002706Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2707Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002708
Steve Naroffe3e9add2008-06-02 23:03:37 +00002709// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002710Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2711Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002712
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002713// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002714Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2715 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002716}
Mike Stump1eb44332009-09-09 15:08:12 +00002717Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2718 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002719}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002720
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002721// ObjCSuperExpr
2722Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2723Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2724
Steve Narofff242b1b2009-07-24 17:54:45 +00002725// ObjCIsaExpr
2726Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2727Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2728
Chris Lattnerd9f69102008-08-10 01:53:14 +00002729// PredefinedExpr
2730Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2731Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002732
2733// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002734Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2735Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002736
2737// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002738Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002739Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002740
2741// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002742Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2743Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002744
Chris Lattner5d661452007-08-26 03:42:43 +00002745// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002746Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2747Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002748
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002749// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002750Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2751Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002752
2753// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002754Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2755Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002756
2757// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002758Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2759Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002760
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002761// OffsetOfExpr
2762Stmt::child_iterator OffsetOfExpr::child_begin() {
2763 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2764 + NumComps);
2765}
2766Stmt::child_iterator OffsetOfExpr::child_end() {
2767 return child_iterator(&*child_begin() + NumExprs);
2768}
2769
Sebastian Redl05189992008-11-11 17:56:53 +00002770// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002771Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002772 // If this is of a type and the type is a VLA type (and not a typedef), the
2773 // size expression of the VLA needs to be treated as an executable expression.
2774 // Why isn't this weirdness documented better in StmtIterator?
2775 if (isArgumentType()) {
2776 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2777 getArgumentType().getTypePtr()))
2778 return child_iterator(T);
2779 return child_iterator();
2780 }
Sebastian Redld4575892008-12-03 23:17:54 +00002781 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002782}
Sebastian Redl05189992008-11-11 17:56:53 +00002783Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2784 if (isArgumentType())
2785 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002786 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002787}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002788
2789// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002790Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002791 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002792}
Ted Kremenek1237c672007-08-24 20:06:47 +00002793Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002794 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002795}
2796
2797// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002798Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002799 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002800}
Ted Kremenek1237c672007-08-24 20:06:47 +00002801Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002802 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002803}
Ted Kremenek1237c672007-08-24 20:06:47 +00002804
2805// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002806Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2807Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002808
Nate Begeman213541a2008-04-18 23:10:10 +00002809// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002810Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2811Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002812
2813// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002814Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2815Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002816
Ted Kremenek1237c672007-08-24 20:06:47 +00002817// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002818Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2819Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002820
2821// BinaryOperator
2822Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002823 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002824}
Ted Kremenek1237c672007-08-24 20:06:47 +00002825Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002826 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002827}
2828
2829// ConditionalOperator
2830Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002831 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002832}
Ted Kremenek1237c672007-08-24 20:06:47 +00002833Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002834 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002835}
2836
2837// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002838Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2839Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002840
Ted Kremenek1237c672007-08-24 20:06:47 +00002841// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002842Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2843Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002844
2845// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002846Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2847 return child_iterator();
2848}
2849
2850Stmt::child_iterator TypesCompatibleExpr::child_end() {
2851 return child_iterator();
2852}
Ted Kremenek1237c672007-08-24 20:06:47 +00002853
2854// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002855Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2856Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002857
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002858// GNUNullExpr
2859Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2860Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2861
Eli Friedmand38617c2008-05-14 19:38:39 +00002862// ShuffleVectorExpr
2863Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002864 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002865}
2866Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002867 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002868}
2869
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002870// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002871Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2872Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002873
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002874// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002875Stmt::child_iterator InitListExpr::child_begin() {
2876 return InitExprs.size() ? &InitExprs[0] : 0;
2877}
2878Stmt::child_iterator InitListExpr::child_end() {
2879 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2880}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002881
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002882// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002883Stmt::child_iterator DesignatedInitExpr::child_begin() {
2884 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2885 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002886 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2887}
2888Stmt::child_iterator DesignatedInitExpr::child_end() {
2889 return child_iterator(&*child_begin() + NumSubExprs);
2890}
2891
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002892// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002893Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2894 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002895}
2896
Mike Stump1eb44332009-09-09 15:08:12 +00002897Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2898 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002899}
2900
Nate Begeman2ef13e52009-08-10 23:49:36 +00002901// ParenListExpr
2902Stmt::child_iterator ParenListExpr::child_begin() {
2903 return &Exprs[0];
2904}
2905Stmt::child_iterator ParenListExpr::child_end() {
2906 return &Exprs[0]+NumExprs;
2907}
2908
Ted Kremenek1237c672007-08-24 20:06:47 +00002909// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002910Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002911 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002912}
2913Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002914 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002915}
Ted Kremenek1237c672007-08-24 20:06:47 +00002916
2917// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002918Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2919Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002920
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002921// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002922Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002923 return child_iterator();
2924}
2925Stmt::child_iterator ObjCSelectorExpr::child_end() {
2926 return child_iterator();
2927}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002928
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002929// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002930Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2931 return child_iterator();
2932}
2933Stmt::child_iterator ObjCProtocolExpr::child_end() {
2934 return child_iterator();
2935}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002936
Steve Naroff563477d2007-09-18 23:55:05 +00002937// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002938Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002939 if (getReceiverKind() == Instance)
2940 return reinterpret_cast<Stmt **>(this + 1);
2941 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002942}
2943Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002944 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002945}
2946
Steve Naroff4eb206b2008-09-03 18:15:37 +00002947// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002948Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2949Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002950
Ted Kremenek9da13f92008-09-26 23:24:14 +00002951Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2952Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }