blob: 4a8068629e9f97271530611d5de906ae97aceafb [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattnerc96f1fb2010-05-13 01:02:19 +000030void Expr::ANCHOR() {} // key function for Expr class.
31
Chris Lattner4ebae652010-04-16 23:34:13 +000032/// isKnownToHaveBooleanValue - Return true if this is an integer expression
33/// that is known to return 0 or 1. This happens for _Bool/bool expressions
34/// but also int expressions which are produced by things like comparisons in
35/// C.
36bool Expr::isKnownToHaveBooleanValue() const {
37 // If this value has _Bool type, it is obvious 0/1.
38 if (getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000039 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregorb90df602010-06-16 00:17:44 +000040 if (!getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000041
Chris Lattner4ebae652010-04-16 23:34:13 +000042 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
43 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000044
Chris Lattner4ebae652010-04-16 23:34:13 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
46 switch (UO->getOpcode()) {
47 case UnaryOperator::Plus:
48 case UnaryOperator::Extension:
49 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000054
John McCall45d30c32010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
57 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner4ebae652010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000059
Chris Lattner4ebae652010-04-16 23:34:13 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
61 switch (BO->getOpcode()) {
62 default: return false;
63 case BinaryOperator::LT: // Relational operators.
64 case BinaryOperator::GT:
65 case BinaryOperator::LE:
66 case BinaryOperator::GE:
67 case BinaryOperator::EQ: // Equality operators.
68 case BinaryOperator::NE:
69 case BinaryOperator::LAnd: // AND operator.
70 case BinaryOperator::LOr: // Logical OR operator.
71 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000072
Chris Lattner4ebae652010-04-16 23:34:13 +000073 case BinaryOperator::And: // Bitwise AND operator.
74 case BinaryOperator::Xor: // Bitwise XOR operator.
75 case BinaryOperator::Or: // Bitwise OR operator.
76 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000079
Chris Lattner4ebae652010-04-16 23:34:13 +000080 case BinaryOperator::Comma:
81 case BinaryOperator::Assign:
82 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Chris Lattner4ebae652010-04-16 23:34:13 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
87 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000089
Chris Lattner4ebae652010-04-16 23:34:13 +000090 return false;
91}
92
Chris Lattner0eedafe2006-08-24 04:56:27 +000093//===----------------------------------------------------------------------===//
94// Primary Expressions.
95//===----------------------------------------------------------------------===//
96
John McCall6b51f282009-11-23 01:53:49 +000097void ExplicitTemplateArgumentList::initializeFrom(
98 const TemplateArgumentListInfo &Info) {
99 LAngleLoc = Info.getLAngleLoc();
100 RAngleLoc = Info.getRAngleLoc();
101 NumTemplateArgs = Info.size();
102
103 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
104 for (unsigned i = 0; i != NumTemplateArgs; ++i)
105 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
106}
107
108void ExplicitTemplateArgumentList::copyInto(
109 TemplateArgumentListInfo &Info) const {
110 Info.setLAngleLoc(LAngleLoc);
111 Info.setRAngleLoc(RAngleLoc);
112 for (unsigned I = 0; I != NumTemplateArgs; ++I)
113 Info.addArgument(getTemplateArgs()[I]);
114}
115
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000116std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
117 return sizeof(ExplicitTemplateArgumentList) +
118 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
119}
120
John McCall6b51f282009-11-23 01:53:49 +0000121std::size_t ExplicitTemplateArgumentList::sizeFor(
122 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000123 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000124}
125
Douglas Gregored6c7442009-11-23 11:41:28 +0000126void DeclRefExpr::computeDependence() {
127 TypeDependent = false;
128 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000129
Douglas Gregored6c7442009-11-23 11:41:28 +0000130 NamedDecl *D = getDecl();
131
132 // (TD) C++ [temp.dep.expr]p3:
133 // An id-expression is type-dependent if it contains:
134 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000135 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000136 //
137 // (VD) C++ [temp.dep.constexpr]p2:
138 // An identifier is value-dependent if it is:
139
140 // (TD) - an identifier that was declared with dependent type
141 // (VD) - a name declared with a dependent type,
142 if (getType()->isDependentType()) {
143 TypeDependent = true;
144 ValueDependent = true;
145 }
146 // (TD) - a conversion-function-id that specifies a dependent type
Alexis Hunta8136cc2010-05-05 15:23:54 +0000147 else if (D->getDeclName().getNameKind()
Douglas Gregored6c7442009-11-23 11:41:28 +0000148 == DeclarationName::CXXConversionFunctionName &&
149 D->getDeclName().getCXXNameType()->isDependentType()) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (TD) - a template-id that is dependent,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000154 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000155 TemplateSpecializationType::anyDependentTemplateArguments(
Alexis Hunta8136cc2010-05-05 15:23:54 +0000156 getTemplateArgs(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000157 getNumTemplateArgs())) {
158 TypeDependent = true;
159 ValueDependent = true;
160 }
161 // (VD) - the name of a non-type template parameter,
162 else if (isa<NonTypeTemplateParmDecl>(D))
163 ValueDependent = true;
164 // (VD) - a constant with integral or enumeration type and is
165 // initialized with an expression that is value-dependent.
166 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000167 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000168 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000169 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000170 if (Init->isValueDependent())
171 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000172 }
173 // (VD) - FIXME: Missing from the standard:
174 // - a member function or a static data member of the current
175 // instantiation
176 else if (Var->isStaticDataMember() &&
Douglas Gregorbe49fc52010-05-11 08:44:04 +0000177 Var->getDeclContext()->isDependentContext())
Douglas Gregor0e4de762010-05-11 08:41:30 +0000178 ValueDependent = true;
179 }
180 // (VD) - FIXME: Missing from the standard:
181 // - a member function or a static data member of the current
182 // instantiation
183 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
184 ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000185 // (TD) - a nested-name-specifier or a qualified-id that names a
186 // member of an unknown specialization.
187 // (handled by DependentScopeDeclRefExpr)
188}
189
Alexis Hunta8136cc2010-05-05 15:23:54 +0000190DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000191 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000192 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000193 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000194 QualType T)
195 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000196 DecoratedD(D,
197 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000198 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000199 Loc(NameLoc) {
200 if (Qualifier) {
201 NameQualifier *NQ = getNameQualifier();
202 NQ->NNS = Qualifier;
203 NQ->Range = QualifierRange;
204 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000205
John McCall6b51f282009-11-23 01:53:49 +0000206 if (TemplateArgs)
207 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000208
209 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000210}
211
212DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
213 NestedNameSpecifier *Qualifier,
214 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000215 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000216 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000217 QualType T,
218 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000219 std::size_t Size = sizeof(DeclRefExpr);
220 if (Qualifier != 0)
221 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000222
John McCall6b51f282009-11-23 01:53:49 +0000223 if (TemplateArgs)
224 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000225
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000226 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
227 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000228 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000229}
230
231SourceRange DeclRefExpr::getSourceRange() const {
232 // FIXME: Does not handle multi-token names well, e.g., operator[].
233 SourceRange R(Loc);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000234
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000235 if (hasQualifier())
236 R.setBegin(getQualifierRange().getBegin());
237 if (hasExplicitTemplateArgumentList())
238 R.setEnd(getRAngleLoc());
239 return R;
240}
241
Anders Carlsson2fb08242009-09-08 18:24:21 +0000242// FIXME: Maybe this should use DeclPrinter with a special "print predefined
243// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000244std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
245 ASTContext &Context = CurrentDecl->getASTContext();
246
Anders Carlsson2fb08242009-09-08 18:24:21 +0000247 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000248 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000249 return FD->getNameAsString();
250
251 llvm::SmallString<256> Name;
252 llvm::raw_svector_ostream Out(Name);
253
254 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000255 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000256 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000257 if (MD->isStatic())
258 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000259 }
260
261 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000262
263 std::string Proto = FD->getQualifiedNameAsString(Policy);
264
John McCall9dd450b2009-09-21 23:43:11 +0000265 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000266 const FunctionProtoType *FT = 0;
267 if (FD->hasWrittenPrototype())
268 FT = dyn_cast<FunctionProtoType>(AFT);
269
270 Proto += "(";
271 if (FT) {
272 llvm::raw_string_ostream POut(Proto);
273 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
274 if (i) POut << ", ";
275 std::string Param;
276 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
277 POut << Param;
278 }
279
280 if (FT->isVariadic()) {
281 if (FD->getNumParams()) POut << ", ";
282 POut << "...";
283 }
284 }
285 Proto += ")";
286
Sam Weinig4e83bd22009-12-27 01:38:20 +0000287 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
288 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
289 if (ThisQuals.hasConst())
290 Proto += " const";
291 if (ThisQuals.hasVolatile())
292 Proto += " volatile";
293 }
294
Sam Weinigd060ed42009-12-06 23:55:13 +0000295 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
296 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000297
298 Out << Proto;
299
300 Out.flush();
301 return Name.str().str();
302 }
303 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
304 llvm::SmallString<256> Name;
305 llvm::raw_svector_ostream Out(Name);
306 Out << (MD->isInstanceMethod() ? '-' : '+');
307 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000308
309 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
310 // a null check to avoid a crash.
311 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000312 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000313
Anders Carlsson2fb08242009-09-08 18:24:21 +0000314 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000315 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
316 Out << '(' << CID << ')';
317
Anders Carlsson2fb08242009-09-08 18:24:21 +0000318 Out << ' ';
319 Out << MD->getSelector().getAsString();
320 Out << ']';
321
322 Out.flush();
323 return Name.str().str();
324 }
325 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
326 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
327 return "top level";
328 }
329 return "";
330}
331
Chris Lattnera0173132008-06-07 22:13:43 +0000332/// getValueAsApproximateDouble - This returns the value as an inaccurate
333/// double. Note that this may cause loss of precision, but is useful for
334/// debugging dumps, etc.
335double FloatingLiteral::getValueAsApproximateDouble() const {
336 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000337 bool ignored;
338 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
339 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000340 return V.convertToDouble();
341}
342
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000343StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
344 unsigned ByteLength, bool Wide,
345 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000346 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000347 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000348 // Allocate enough space for the StringLiteral plus an array of locations for
349 // any concatenated string tokens.
350 void *Mem = C.Allocate(sizeof(StringLiteral)+
351 sizeof(SourceLocation)*(NumStrs-1),
352 llvm::alignof<StringLiteral>());
353 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Steve Naroffdf7855b2007-02-21 23:46:25 +0000355 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000356 char *AStrData = new (C, 1) char[ByteLength];
357 memcpy(AStrData, StrData, ByteLength);
358 SL->StrData = AStrData;
359 SL->ByteLength = ByteLength;
360 SL->IsWide = Wide;
361 SL->TokLocs[0] = Loc[0];
362 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000363
Chris Lattner630970d2009-02-18 05:49:11 +0000364 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000365 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
366 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000367}
368
Douglas Gregor958dfc92009-04-15 16:35:07 +0000369StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
370 void *Mem = C.Allocate(sizeof(StringLiteral)+
371 sizeof(SourceLocation)*(NumStrs-1),
372 llvm::alignof<StringLiteral>());
373 StringLiteral *SL = new (Mem) StringLiteral(QualType());
374 SL->StrData = 0;
375 SL->ByteLength = 0;
376 SL->NumConcatenated = NumStrs;
377 return SL;
378}
379
Douglas Gregore26a2852009-08-07 06:08:38 +0000380void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000381 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000382 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000383}
384
Daniel Dunbar36217882009-09-22 03:27:33 +0000385void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000386 if (StrData)
387 C.Deallocate(const_cast<char*>(StrData));
388
Daniel Dunbar36217882009-09-22 03:27:33 +0000389 char *AStrData = new (C, 1) char[Str.size()];
390 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000391 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000392 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000393}
394
Chris Lattner1b926492006-08-23 06:42:10 +0000395/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
396/// corresponds to, e.g. "sizeof" or "[pre]++".
397const char *UnaryOperator::getOpcodeStr(Opcode Op) {
398 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000399 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000400 case PostInc: return "++";
401 case PostDec: return "--";
402 case PreInc: return "++";
403 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000404 case AddrOf: return "&";
405 case Deref: return "*";
406 case Plus: return "+";
407 case Minus: return "-";
408 case Not: return "~";
409 case LNot: return "!";
410 case Real: return "__real";
411 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000412 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000413 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000414 }
415}
416
Mike Stump11289f42009-09-09 15:08:12 +0000417UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000418UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
419 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000420 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000421 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
422 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
423 case OO_Amp: return AddrOf;
424 case OO_Star: return Deref;
425 case OO_Plus: return Plus;
426 case OO_Minus: return Minus;
427 case OO_Tilde: return Not;
428 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000429 }
430}
431
432OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
433 switch (Opc) {
434 case PostInc: case PreInc: return OO_PlusPlus;
435 case PostDec: case PreDec: return OO_MinusMinus;
436 case AddrOf: return OO_Amp;
437 case Deref: return OO_Star;
438 case Plus: return OO_Plus;
439 case Minus: return OO_Minus;
440 case Not: return OO_Tilde;
441 case LNot: return OO_Exclaim;
442 default: return OO_None;
443 }
444}
445
446
Chris Lattner0eedafe2006-08-24 04:56:27 +0000447//===----------------------------------------------------------------------===//
448// Postfix Operators.
449//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000450
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000451CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000452 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000453 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000454 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000455 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000456 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000457
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000458 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000459 SubExprs[FN] = fn;
460 for (unsigned i = 0; i != numargs; ++i)
461 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000462
Douglas Gregor993603d2008-11-14 16:09:21 +0000463 RParenLoc = rparenloc;
464}
Nate Begeman1e36a852008-01-17 17:46:27 +0000465
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000466CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
467 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000468 : Expr(CallExprClass, t,
469 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000470 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000471 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000472
473 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000474 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000475 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000476 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000477
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000478 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000479}
480
Mike Stump11289f42009-09-09 15:08:12 +0000481CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
482 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000483 SubExprs = new (C) Stmt*[1];
484}
485
Douglas Gregore26a2852009-08-07 06:08:38 +0000486void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000487 DestroyChildren(C);
488 if (SubExprs) C.Deallocate(SubExprs);
489 this->~CallExpr();
490 C.Deallocate(this);
491}
492
Nuno Lopes518e3702009-12-20 23:11:08 +0000493Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000494 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000495 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000496 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000497 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
498 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000499
500 return 0;
501}
502
Nuno Lopes518e3702009-12-20 23:11:08 +0000503FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000504 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000505}
506
Chris Lattnere4407ed2007-12-28 05:25:02 +0000507/// setNumArgs - This changes the number of arguments present in this call.
508/// Any orphaned expressions are deleted by this, and any new operands are set
509/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000510void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000511 // No change, just return.
512 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattnere4407ed2007-12-28 05:25:02 +0000514 // If shrinking # arguments, just delete the extras and forgot them.
515 if (NumArgs < getNumArgs()) {
516 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000517 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000518 this->NumArgs = NumArgs;
519 return;
520 }
521
522 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000523 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000524 // Copy over args.
525 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
526 NewSubExprs[i] = SubExprs[i];
527 // Null out new args.
528 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
529 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Douglas Gregorba6e5572009-04-17 21:46:47 +0000531 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000532 SubExprs = NewSubExprs;
533 this->NumArgs = NumArgs;
534}
535
Chris Lattner01ff98a2008-10-06 05:00:53 +0000536/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
537/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000538unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000539 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000540 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000541 // ImplicitCastExpr.
542 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
543 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000544 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Steve Narofff6e3b3292008-01-31 01:07:12 +0000546 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
547 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000548 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000550 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
551 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000552 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000554 if (!FDecl->getIdentifier())
555 return 0;
556
Douglas Gregor15fc9562009-09-12 00:22:50 +0000557 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000558}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000559
Anders Carlsson00a27592009-05-26 04:57:27 +0000560QualType CallExpr::getCallReturnType() const {
561 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000562 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000563 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000564 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000565 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000566
John McCall9dd450b2009-09-21 23:43:11 +0000567 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000568 return FnType->getResultType();
569}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000570
Alexis Hunta8136cc2010-05-05 15:23:54 +0000571OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000572 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000573 TypeSourceInfo *tsi,
574 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000575 Expr** exprsPtr, unsigned numExprs,
576 SourceLocation RParenLoc) {
577 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000578 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000579 sizeof(Expr*) * numExprs);
580
581 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
582 exprsPtr, numExprs, RParenLoc);
583}
584
585OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
586 unsigned numComps, unsigned numExprs) {
587 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
588 sizeof(OffsetOfNode) * numComps +
589 sizeof(Expr*) * numExprs);
590 return new (Mem) OffsetOfExpr(numComps, numExprs);
591}
592
Alexis Hunta8136cc2010-05-05 15:23:54 +0000593OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000594 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000595 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000596 Expr** exprsPtr, unsigned numExprs,
597 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000598 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000599 /*ValueDependent=*/tsi->getType()->isDependentType() ||
600 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
601 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000602 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
603 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000604{
605 for(unsigned i = 0; i < numComps; ++i) {
606 setComponent(i, compsPtr[i]);
607 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000608
Douglas Gregor882211c2010-04-28 22:16:22 +0000609 for(unsigned i = 0; i < numExprs; ++i) {
610 setIndexExpr(i, exprsPtr[i]);
611 }
612}
613
614IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
615 assert(getKind() == Field || getKind() == Identifier);
616 if (getKind() == Field)
617 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000618
Douglas Gregor882211c2010-04-28 22:16:22 +0000619 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
620}
621
Mike Stump11289f42009-09-09 15:08:12 +0000622MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
623 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000624 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000625 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000626 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000627 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000628 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000629 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000630 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000631
John McCalla8ae2222010-04-06 21:38:20 +0000632 bool hasQualOrFound = (qual != 0 ||
633 founddecl.getDecl() != memberdecl ||
634 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000635 if (hasQualOrFound)
636 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000637
John McCall6b51f282009-11-23 01:53:49 +0000638 if (targs)
639 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000641 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000642 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
643
644 if (hasQualOrFound) {
645 if (qual && qual->isDependent()) {
646 E->setValueDependent(true);
647 E->setTypeDependent(true);
648 }
649 E->HasQualifierOrFoundDecl = true;
650
651 MemberNameQualifier *NQ = E->getMemberQualifier();
652 NQ->NNS = qual;
653 NQ->Range = qualrange;
654 NQ->FoundDecl = founddecl;
655 }
656
657 if (targs) {
658 E->HasExplicitTemplateArgumentList = true;
659 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
660 }
661
662 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000663}
664
Anders Carlsson496335e2009-09-03 00:59:21 +0000665const char *CastExpr::getCastKindName() const {
666 switch (getCastKind()) {
667 case CastExpr::CK_Unknown:
668 return "Unknown";
669 case CastExpr::CK_BitCast:
670 return "BitCast";
671 case CastExpr::CK_NoOp:
672 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000673 case CastExpr::CK_BaseToDerived:
674 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000675 case CastExpr::CK_DerivedToBase:
676 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000677 case CastExpr::CK_UncheckedDerivedToBase:
678 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000679 case CastExpr::CK_Dynamic:
680 return "Dynamic";
681 case CastExpr::CK_ToUnion:
682 return "ToUnion";
683 case CastExpr::CK_ArrayToPointerDecay:
684 return "ArrayToPointerDecay";
685 case CastExpr::CK_FunctionToPointerDecay:
686 return "FunctionToPointerDecay";
687 case CastExpr::CK_NullToMemberPointer:
688 return "NullToMemberPointer";
689 case CastExpr::CK_BaseToDerivedMemberPointer:
690 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000691 case CastExpr::CK_DerivedToBaseMemberPointer:
692 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000693 case CastExpr::CK_UserDefinedConversion:
694 return "UserDefinedConversion";
695 case CastExpr::CK_ConstructorConversion:
696 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000697 case CastExpr::CK_IntegralToPointer:
698 return "IntegralToPointer";
699 case CastExpr::CK_PointerToIntegral:
700 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000701 case CastExpr::CK_ToVoid:
702 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000703 case CastExpr::CK_VectorSplat:
704 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000705 case CastExpr::CK_IntegralCast:
706 return "IntegralCast";
707 case CastExpr::CK_IntegralToFloating:
708 return "IntegralToFloating";
709 case CastExpr::CK_FloatingToIntegral:
710 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000711 case CastExpr::CK_FloatingCast:
712 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000713 case CastExpr::CK_MemberPointerToBoolean:
714 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000715 case CastExpr::CK_AnyPointerToObjCPointerCast:
716 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000717 case CastExpr::CK_AnyPointerToBlockPointerCast:
718 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000719 }
Mike Stump11289f42009-09-09 15:08:12 +0000720
Anders Carlsson496335e2009-09-03 00:59:21 +0000721 assert(0 && "Unhandled cast kind!");
722 return 0;
723}
724
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000725void CastExpr::DoDestroy(ASTContext &C)
726{
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000727 BasePath.Destroy();
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000728 Expr::DoDestroy(C);
729}
730
Douglas Gregord196a582009-12-14 19:27:10 +0000731Expr *CastExpr::getSubExprAsWritten() {
732 Expr *SubExpr = 0;
733 CastExpr *E = this;
734 do {
735 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000736
Douglas Gregord196a582009-12-14 19:27:10 +0000737 // Skip any temporary bindings; they're implicit.
738 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
739 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000740
Douglas Gregord196a582009-12-14 19:27:10 +0000741 // Conversions by constructor and conversion functions have a
742 // subexpression describing the call; strip it off.
743 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
744 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
745 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
746 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000747
Douglas Gregord196a582009-12-14 19:27:10 +0000748 // If the subexpression we're left with is an implicit cast, look
749 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000750 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
751
Douglas Gregord196a582009-12-14 19:27:10 +0000752 return SubExpr;
753}
754
Chris Lattner1b926492006-08-23 06:42:10 +0000755/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
756/// corresponds to, e.g. "<<=".
757const char *BinaryOperator::getOpcodeStr(Opcode Op) {
758 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000759 case PtrMemD: return ".*";
760 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000761 case Mul: return "*";
762 case Div: return "/";
763 case Rem: return "%";
764 case Add: return "+";
765 case Sub: return "-";
766 case Shl: return "<<";
767 case Shr: return ">>";
768 case LT: return "<";
769 case GT: return ">";
770 case LE: return "<=";
771 case GE: return ">=";
772 case EQ: return "==";
773 case NE: return "!=";
774 case And: return "&";
775 case Xor: return "^";
776 case Or: return "|";
777 case LAnd: return "&&";
778 case LOr: return "||";
779 case Assign: return "=";
780 case MulAssign: return "*=";
781 case DivAssign: return "/=";
782 case RemAssign: return "%=";
783 case AddAssign: return "+=";
784 case SubAssign: return "-=";
785 case ShlAssign: return "<<=";
786 case ShrAssign: return ">>=";
787 case AndAssign: return "&=";
788 case XorAssign: return "^=";
789 case OrAssign: return "|=";
790 case Comma: return ",";
791 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000792
793 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000794}
Steve Naroff47500512007-04-19 23:00:49 +0000795
Mike Stump11289f42009-09-09 15:08:12 +0000796BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000797BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
798 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000799 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000800 case OO_Plus: return Add;
801 case OO_Minus: return Sub;
802 case OO_Star: return Mul;
803 case OO_Slash: return Div;
804 case OO_Percent: return Rem;
805 case OO_Caret: return Xor;
806 case OO_Amp: return And;
807 case OO_Pipe: return Or;
808 case OO_Equal: return Assign;
809 case OO_Less: return LT;
810 case OO_Greater: return GT;
811 case OO_PlusEqual: return AddAssign;
812 case OO_MinusEqual: return SubAssign;
813 case OO_StarEqual: return MulAssign;
814 case OO_SlashEqual: return DivAssign;
815 case OO_PercentEqual: return RemAssign;
816 case OO_CaretEqual: return XorAssign;
817 case OO_AmpEqual: return AndAssign;
818 case OO_PipeEqual: return OrAssign;
819 case OO_LessLess: return Shl;
820 case OO_GreaterGreater: return Shr;
821 case OO_LessLessEqual: return ShlAssign;
822 case OO_GreaterGreaterEqual: return ShrAssign;
823 case OO_EqualEqual: return EQ;
824 case OO_ExclaimEqual: return NE;
825 case OO_LessEqual: return LE;
826 case OO_GreaterEqual: return GE;
827 case OO_AmpAmp: return LAnd;
828 case OO_PipePipe: return LOr;
829 case OO_Comma: return Comma;
830 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000831 }
832}
833
834OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
835 static const OverloadedOperatorKind OverOps[] = {
836 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
837 OO_Star, OO_Slash, OO_Percent,
838 OO_Plus, OO_Minus,
839 OO_LessLess, OO_GreaterGreater,
840 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
841 OO_EqualEqual, OO_ExclaimEqual,
842 OO_Amp,
843 OO_Caret,
844 OO_Pipe,
845 OO_AmpAmp,
846 OO_PipePipe,
847 OO_Equal, OO_StarEqual,
848 OO_SlashEqual, OO_PercentEqual,
849 OO_PlusEqual, OO_MinusEqual,
850 OO_LessLessEqual, OO_GreaterGreaterEqual,
851 OO_AmpEqual, OO_CaretEqual,
852 OO_PipeEqual,
853 OO_Comma
854 };
855 return OverOps[Opc];
856}
857
Ted Kremenekac034612010-04-13 23:39:13 +0000858InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000859 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000860 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000861 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000862 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000863 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000864 UnionFieldInit(0), HadArrayRangeDesignator(false)
865{
Ted Kremenek013041e2010-02-19 01:50:18 +0000866 for (unsigned I = 0; I != numInits; ++I) {
867 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000868 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000869 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000870 ValueDependent = true;
871 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000872
Ted Kremenekac034612010-04-13 23:39:13 +0000873 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000874}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000875
Ted Kremenekac034612010-04-13 23:39:13 +0000876void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000877 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000878 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000879}
880
Ted Kremenekac034612010-04-13 23:39:13 +0000881void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000882 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
883 Idx < LastIdx; ++Idx)
Ted Kremenekac034612010-04-13 23:39:13 +0000884 InitExprs[Idx]->Destroy(C);
885 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000886}
887
Ted Kremenekac034612010-04-13 23:39:13 +0000888Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000889 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000890 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000891 InitExprs.back() = expr;
892 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000895 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
896 InitExprs[Init] = expr;
897 return Result;
898}
899
Steve Naroff991e99d2008-09-04 15:31:07 +0000900/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000901///
902const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000903 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000904 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000905}
906
Mike Stump11289f42009-09-09 15:08:12 +0000907SourceLocation BlockExpr::getCaretLocation() const {
908 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000909}
Mike Stump11289f42009-09-09 15:08:12 +0000910const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000911 return TheBlock->getBody();
912}
Mike Stump11289f42009-09-09 15:08:12 +0000913Stmt *BlockExpr::getBody() {
914 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000915}
Steve Naroff415d3d52008-10-08 17:01:13 +0000916
917
Chris Lattner1ec5f562007-06-27 05:38:08 +0000918//===----------------------------------------------------------------------===//
919// Generic Expression Routines
920//===----------------------------------------------------------------------===//
921
Chris Lattner237f2752009-02-14 07:37:35 +0000922/// isUnusedResultAWarning - Return true if this immediate expression should
923/// be warned about if the result is unused. If so, fill in Loc and Ranges
924/// with location to warn on and the source range[s] to report with the
925/// warning.
926bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000927 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000928 // Don't warn if the expr is type dependent. The type could end up
929 // instantiating to void.
930 if (isTypeDependent())
931 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner1ec5f562007-06-27 05:38:08 +0000933 switch (getStmtClass()) {
934 default:
John McCallc493a732010-03-12 07:11:26 +0000935 if (getType()->isVoidType())
936 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000937 Loc = getExprLoc();
938 R1 = getSourceRange();
939 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000940 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000941 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000942 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000943 case UnaryOperatorClass: {
944 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner1ec5f562007-06-27 05:38:08 +0000946 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000947 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000948 case UnaryOperator::PostInc:
949 case UnaryOperator::PostDec:
950 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000951 case UnaryOperator::PreDec: // ++/--
952 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000953 case UnaryOperator::Deref:
954 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000955 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000956 return false;
957 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000958 case UnaryOperator::Real:
959 case UnaryOperator::Imag:
960 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000961 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
962 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000963 return false;
964 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000965 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000966 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000967 }
Chris Lattner237f2752009-02-14 07:37:35 +0000968 Loc = UO->getOperatorLoc();
969 R1 = UO->getSubExpr()->getSourceRange();
970 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000971 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000972 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000973 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000974 switch (BO->getOpcode()) {
975 default:
976 break;
977 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
978 case BinaryOperator::Comma:
979 // ((foo = <blah>), 0) is an idiom for hiding the result (and
980 // lvalue-ness) of an assignment written in a macro.
981 if (IntegerLiteral *IE =
982 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
983 if (IE->getValue() == 0)
984 return false;
985 case BinaryOperator::LAnd:
986 case BinaryOperator::LOr:
987 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
988 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000989 }
Chris Lattner237f2752009-02-14 07:37:35 +0000990 if (BO->isAssignmentOp())
991 return false;
992 Loc = BO->getOperatorLoc();
993 R1 = BO->getLHS()->getSourceRange();
994 R2 = BO->getRHS()->getSourceRange();
995 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000996 }
Chris Lattner86928112007-08-25 02:00:02 +0000997 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +0000998 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000999 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001000
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001001 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001002 // The condition must be evaluated, but if either the LHS or RHS is a
1003 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001004 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001005 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001006 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001007 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001008 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001009 }
1010
Chris Lattnera44d1162007-06-27 05:58:59 +00001011 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001012 // If the base pointer or element is to a volatile pointer/field, accessing
1013 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001014 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001015 return false;
1016 Loc = cast<MemberExpr>(this)->getMemberLoc();
1017 R1 = SourceRange(Loc, Loc);
1018 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1019 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Chris Lattner1ec5f562007-06-27 05:38:08 +00001021 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001022 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001023 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001024 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001025 return false;
1026 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1027 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1028 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1029 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001030
Chris Lattner1ec5f562007-06-27 05:38:08 +00001031 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001032 case CXXOperatorCallExprClass:
1033 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001034 // If this is a direct call, get the callee.
1035 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001036 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001037 // If the callee has attribute pure, const, or warn_unused_result, warn
1038 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001039 //
1040 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1041 // updated to match for QoI.
1042 if (FD->getAttr<WarnUnusedResultAttr>() ||
1043 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1044 Loc = CE->getCallee()->getLocStart();
1045 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001046
Chris Lattner1a6babf2009-10-13 04:53:48 +00001047 if (unsigned NumArgs = CE->getNumArgs())
1048 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1049 CE->getArg(NumArgs-1)->getLocEnd());
1050 return true;
1051 }
Chris Lattner237f2752009-02-14 07:37:35 +00001052 }
1053 return false;
1054 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001055
1056 case CXXTemporaryObjectExprClass:
1057 case CXXConstructExprClass:
1058 return false;
1059
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001060 case ObjCMessageExprClass: {
1061 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1062 const ObjCMethodDecl *MD = ME->getMethodDecl();
1063 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1064 Loc = getExprLoc();
1065 return true;
1066 }
Chris Lattner237f2752009-02-14 07:37:35 +00001067 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001070 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001071#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001072 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001073 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001074 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001075 Loc = Ref->getLocation();
1076 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1077 if (Ref->getBase())
1078 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001079#else
1080 Loc = getExprLoc();
1081 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001082#endif
1083 return true;
1084 }
Chris Lattner944d3062008-07-26 19:51:01 +00001085 case StmtExprClass: {
1086 // Statement exprs don't logically have side effects themselves, but are
1087 // sometimes used in macros in ways that give them a type that is unused.
1088 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1089 // however, if the result of the stmt expr is dead, we don't want to emit a
1090 // warning.
1091 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1092 if (!CS->body_empty())
1093 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001094 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001095
John McCallc493a732010-03-12 07:11:26 +00001096 if (getType()->isVoidType())
1097 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001098 Loc = cast<StmtExpr>(this)->getLParenLoc();
1099 R1 = getSourceRange();
1100 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001101 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001102 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001103 // If this is an explicit cast to void, allow it. People do this when they
1104 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001105 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001106 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001107 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1108 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1109 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001110 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001111 if (getType()->isVoidType())
1112 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001113 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001114
Anders Carlsson6aa50392009-11-17 17:11:23 +00001115 // If this is a cast to void or a constructor conversion, check the operand.
1116 // Otherwise, the result of the cast is unused.
1117 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1118 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001119 return (cast<CastExpr>(this)->getSubExpr()
1120 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001121 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1122 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1123 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001126 case ImplicitCastExprClass:
1127 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001128 return (cast<ImplicitCastExpr>(this)
1129 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001130
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001131 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001132 return (cast<CXXDefaultArgExpr>(this)
1133 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001134
1135 case CXXNewExprClass:
1136 // FIXME: In theory, there might be new expressions that don't have side
1137 // effects (e.g. a placement new with an uninitialized POD).
1138 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001139 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001140 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001141 return (cast<CXXBindTemporaryExpr>(this)
1142 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001143 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001144 return (cast<CXXExprWithTemporaries>(this)
1145 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001146 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001147}
1148
Fariborz Jahanian07735332009-02-22 18:40:18 +00001149/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001150/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001151bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001152 switch (getStmtClass()) {
1153 default:
1154 return false;
1155 case ObjCIvarRefExprClass:
1156 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001157 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001158 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001159 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001160 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001161 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001162 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001163 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001164 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001165 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001166 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001167 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1168 if (VD->hasGlobalStorage())
1169 return true;
1170 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001171 // dereferencing to a pointer is always a gc'able candidate,
1172 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001173 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001174 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001175 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001176 return false;
1177 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001178 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001179 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001180 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001181 }
1182 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001183 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001184 }
1185}
Ted Kremenekfff70962008-01-17 16:57:34 +00001186Expr* Expr::IgnoreParens() {
1187 Expr* E = this;
1188 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1189 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001190
Ted Kremenekfff70962008-01-17 16:57:34 +00001191 return E;
1192}
1193
Chris Lattnerf2660962008-02-13 01:02:39 +00001194/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1195/// or CastExprs or ImplicitCastExprs, returning their operand.
1196Expr *Expr::IgnoreParenCasts() {
1197 Expr *E = this;
1198 while (true) {
1199 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1200 E = P->getSubExpr();
1201 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1202 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001203 else
1204 return E;
1205 }
1206}
1207
John McCalleebc8322010-05-05 22:59:52 +00001208Expr *Expr::IgnoreParenImpCasts() {
1209 Expr *E = this;
1210 while (true) {
1211 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1212 E = P->getSubExpr();
1213 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1214 E = P->getSubExpr();
1215 else
1216 return E;
1217 }
1218}
1219
Chris Lattneref26c772009-03-13 17:28:01 +00001220/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1221/// value (including ptr->int casts of the same size). Strip off any
1222/// ParenExpr or CastExprs, returning their operand.
1223Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1224 Expr *E = this;
1225 while (true) {
1226 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1227 E = P->getSubExpr();
1228 continue;
1229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Chris Lattneref26c772009-03-13 17:28:01 +00001231 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1232 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001233 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001234 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattneref26c772009-03-13 17:28:01 +00001236 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1237 E = SE;
1238 continue;
1239 }
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregor6972a622010-06-16 00:35:25 +00001241 if ((E->getType()->isPointerType() ||
1242 E->getType()->isIntegralType(Ctx)) &&
1243 (SE->getType()->isPointerType() ||
1244 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001245 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1246 E = SE;
1247 continue;
1248 }
1249 }
Mike Stump11289f42009-09-09 15:08:12 +00001250
Chris Lattneref26c772009-03-13 17:28:01 +00001251 return E;
1252 }
1253}
1254
Douglas Gregord196a582009-12-14 19:27:10 +00001255bool Expr::isDefaultArgument() const {
1256 const Expr *E = this;
1257 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1258 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001259
Douglas Gregord196a582009-12-14 19:27:10 +00001260 return isa<CXXDefaultArgExpr>(E);
1261}
Chris Lattneref26c772009-03-13 17:28:01 +00001262
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001263/// \brief Skip over any no-op casts and any temporary-binding
1264/// expressions.
1265static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1266 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1267 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1268 E = ICE->getSubExpr();
1269 else
1270 break;
1271 }
1272
1273 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1274 E = BE->getSubExpr();
1275
1276 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1277 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1278 E = ICE->getSubExpr();
1279 else
1280 break;
1281 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001282
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001283 return E;
1284}
1285
1286const Expr *Expr::getTemporaryObject() const {
1287 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1288
1289 // A cast can produce a temporary object. The object's construction
1290 // is represented as a CXXConstructExpr.
1291 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1292 // Only user-defined and constructor conversions can produce
1293 // temporary objects.
1294 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1295 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1296 return 0;
1297
1298 // Strip off temporary bindings and no-op casts.
1299 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1300
1301 // If this is a constructor conversion, see if we have an object
1302 // construction.
1303 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1304 return dyn_cast<CXXConstructExpr>(Sub);
1305
1306 // If this is a user-defined conversion, see if we have a call to
1307 // a function that itself returns a temporary object.
1308 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1309 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1310 if (CE->getCallReturnType()->isRecordType())
1311 return CE;
1312
1313 return 0;
1314 }
1315
1316 // A call returning a class type returns a temporary.
1317 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1318 if (CE->getCallReturnType()->isRecordType())
1319 return CE;
1320
1321 return 0;
1322 }
1323
1324 // Explicit temporary object constructors create temporaries.
1325 return dyn_cast<CXXTemporaryObjectExpr>(E);
1326}
1327
Douglas Gregor4619e432008-12-05 23:32:09 +00001328/// hasAnyTypeDependentArguments - Determines if any of the expressions
1329/// in Exprs is type-dependent.
1330bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1331 for (unsigned I = 0; I < NumExprs; ++I)
1332 if (Exprs[I]->isTypeDependent())
1333 return true;
1334
1335 return false;
1336}
1337
1338/// hasAnyValueDependentArguments - Determines if any of the expressions
1339/// in Exprs is value-dependent.
1340bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1341 for (unsigned I = 0; I < NumExprs; ++I)
1342 if (Exprs[I]->isValueDependent())
1343 return true;
1344
1345 return false;
1346}
1347
Eli Friedman7139af42009-01-25 02:32:41 +00001348bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001349 // This function is attempting whether an expression is an initializer
1350 // which can be evaluated at compile-time. isEvaluatable handles most
1351 // of the cases, but it can't deal with some initializer-specific
1352 // expressions, and it can't deal with aggregates; we deal with those here,
1353 // and fall back to isEvaluatable for the other cases.
1354
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001355 // FIXME: This function assumes the variable being assigned to
1356 // isn't a reference type!
1357
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001358 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001359 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001360 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001361 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001362 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001363 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001364 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001365 // This handles gcc's extension that allows global initializers like
1366 // "struct x {int x;} x = (struct x) {};".
1367 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001368 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001369 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001370 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001371 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001372 // FIXME: This doesn't deal with fields with reference types correctly.
1373 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1374 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001375 const InitListExpr *Exp = cast<InitListExpr>(this);
1376 unsigned numInits = Exp->getNumInits();
1377 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001378 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001379 return false;
1380 }
Eli Friedman384da272009-01-25 03:12:18 +00001381 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001382 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001383 case ImplicitValueInitExprClass:
1384 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001385 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001386 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001387 case UnaryOperatorClass: {
1388 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1389 if (Exp->getOpcode() == UnaryOperator::Extension)
1390 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1391 break;
1392 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001393 case BinaryOperatorClass: {
1394 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1395 // but this handles the common case.
1396 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1397 if (Exp->getOpcode() == BinaryOperator::Sub &&
1398 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1399 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1400 return true;
1401 break;
1402 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001403 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001404 case CStyleCastExprClass:
1405 // Handle casts with a destination that's a struct or union; this
1406 // deals with both the gcc no-op struct cast extension and the
1407 // cast-to-union extension.
1408 if (getType()->isRecordType())
1409 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001410
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001411 // Integer->integer casts can be handled here, which is important for
1412 // things like (int)(&&x-&&y). Scary but true.
1413 if (getType()->isIntegerType() &&
1414 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1415 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001416
Eli Friedman384da272009-01-25 03:12:18 +00001417 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001418 }
Eli Friedman384da272009-01-25 03:12:18 +00001419 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001420}
1421
Chris Lattner7eef9192007-05-24 01:23:49 +00001422/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1423/// integer constant expression with the value zero, or if this is one that is
1424/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001425bool Expr::isNullPointerConstant(ASTContext &Ctx,
1426 NullPointerConstantValueDependence NPC) const {
1427 if (isValueDependent()) {
1428 switch (NPC) {
1429 case NPC_NeverValueDependent:
1430 assert(false && "Unexpected value dependent expression!");
1431 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001432
Douglas Gregor56751b52009-09-25 04:25:58 +00001433 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001434 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001435
Douglas Gregor56751b52009-09-25 04:25:58 +00001436 case NPC_ValueDependentIsNotNull:
1437 return false;
1438 }
1439 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001440
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001441 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001442 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001443 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001444 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001445 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001446 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001447 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001448 Pointee->isVoidType() && // to void*
1449 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001450 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001451 }
Steve Naroffada7d422007-05-20 17:54:12 +00001452 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001453 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1454 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001455 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001456 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1457 // Accept ((void*)0) as a null pointer constant, as many other
1458 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001459 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001460 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001461 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001462 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001463 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001464 } else if (isa<GNUNullExpr>(this)) {
1465 // The GNU __null extension is always a null pointer constant.
1466 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001467 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001468
Sebastian Redl576fd422009-05-10 18:38:11 +00001469 // C++0x nullptr_t is always a null pointer constant.
1470 if (getType()->isNullPtrType())
1471 return true;
1472
Steve Naroff4871fe02008-01-14 16:10:57 +00001473 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001474 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001475 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001476 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001477
Chris Lattner1abbd412007-06-08 17:58:43 +00001478 // If we have an integer constant expression, we need to *evaluate* it and
1479 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001480 llvm::APSInt Result;
1481 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001482}
Steve Narofff7a5da12007-07-28 23:10:27 +00001483
Douglas Gregor71235ec2009-05-02 02:18:30 +00001484FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001485 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001486
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001487 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1488 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1489 E = ICE->getSubExpr()->IgnoreParens();
1490 else
1491 break;
1492 }
1493
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001494 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001495 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001496 if (Field->isBitField())
1497 return Field;
1498
1499 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1500 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1501 return BinOp->getLHS()->getBitField();
1502
1503 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001504}
1505
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001506bool Expr::refersToVectorElement() const {
1507 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001508
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001509 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1510 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1511 E = ICE->getSubExpr()->IgnoreParens();
1512 else
1513 break;
1514 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001515
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001516 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1517 return ASE->getBase()->getType()->isVectorType();
1518
1519 if (isa<ExtVectorElementExpr>(E))
1520 return true;
1521
1522 return false;
1523}
1524
Chris Lattnerb8211f62009-02-16 22:14:05 +00001525/// isArrow - Return true if the base expression is a pointer to vector,
1526/// return false if the base expression is a vector.
1527bool ExtVectorElementExpr::isArrow() const {
1528 return getBase()->getType()->isPointerType();
1529}
1530
Nate Begemance4d7fc2008-04-18 23:10:10 +00001531unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001532 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001533 return VT->getNumElements();
1534 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001535}
1536
Nate Begemanf322eab2008-05-09 06:41:27 +00001537/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001538bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001539 // FIXME: Refactor this code to an accessor on the AST node which returns the
1540 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001541 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001542
1543 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001544 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001545 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Nate Begeman7e5185b2009-01-18 02:01:21 +00001547 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001548 if (Comp[0] == 's' || Comp[0] == 'S')
1549 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001550
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001551 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1552 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001553 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001554
Steve Naroff0d595ca2007-07-30 03:29:09 +00001555 return false;
1556}
Chris Lattner885b4952007-08-02 23:36:59 +00001557
Nate Begemanf322eab2008-05-09 06:41:27 +00001558/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001559void ExtVectorElementExpr::getEncodedElementAccess(
1560 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001561 llvm::StringRef Comp = Accessor->getName();
1562 if (Comp[0] == 's' || Comp[0] == 'S')
1563 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001564
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001565 bool isHi = Comp == "hi";
1566 bool isLo = Comp == "lo";
1567 bool isEven = Comp == "even";
1568 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001569
Nate Begemanf322eab2008-05-09 06:41:27 +00001570 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1571 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001572
Nate Begemanf322eab2008-05-09 06:41:27 +00001573 if (isHi)
1574 Index = e + i;
1575 else if (isLo)
1576 Index = i;
1577 else if (isEven)
1578 Index = 2 * i;
1579 else if (isOdd)
1580 Index = 2 * i + 1;
1581 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001582 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001583
Nate Begemand3862152008-05-13 21:03:02 +00001584 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001585 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001586}
1587
Douglas Gregor9a129192010-04-21 00:45:42 +00001588ObjCMessageExpr::ObjCMessageExpr(QualType T,
1589 SourceLocation LBracLoc,
1590 SourceLocation SuperLoc,
1591 bool IsInstanceSuper,
1592 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001593 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001594 ObjCMethodDecl *Method,
1595 Expr **Args, unsigned NumArgs,
1596 SourceLocation RBracLoc)
1597 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001598 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001599 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1600 HasMethod(Method != 0), SuperLoc(SuperLoc),
1601 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1602 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001603 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001604{
Douglas Gregor9a129192010-04-21 00:45:42 +00001605 setReceiverPointer(SuperType.getAsOpaquePtr());
1606 if (NumArgs)
1607 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001608}
1609
Douglas Gregor9a129192010-04-21 00:45:42 +00001610ObjCMessageExpr::ObjCMessageExpr(QualType T,
1611 SourceLocation LBracLoc,
1612 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001613 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001614 ObjCMethodDecl *Method,
1615 Expr **Args, unsigned NumArgs,
1616 SourceLocation RBracLoc)
1617 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001618 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001619 hasAnyValueDependentArguments(Args, NumArgs))),
1620 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1621 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1622 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001623 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001624{
1625 setReceiverPointer(Receiver);
1626 if (NumArgs)
1627 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001628}
1629
Douglas Gregor9a129192010-04-21 00:45:42 +00001630ObjCMessageExpr::ObjCMessageExpr(QualType T,
1631 SourceLocation LBracLoc,
1632 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001633 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001634 ObjCMethodDecl *Method,
1635 Expr **Args, unsigned NumArgs,
1636 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001637 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001638 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001639 hasAnyValueDependentArguments(Args, NumArgs))),
1640 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1641 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1642 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001643 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001644{
1645 setReceiverPointer(Receiver);
1646 if (NumArgs)
1647 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001648}
1649
Douglas Gregor9a129192010-04-21 00:45:42 +00001650ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1651 SourceLocation LBracLoc,
1652 SourceLocation SuperLoc,
1653 bool IsInstanceSuper,
1654 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001655 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001656 ObjCMethodDecl *Method,
1657 Expr **Args, unsigned NumArgs,
1658 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001659 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001660 NumArgs * sizeof(Expr *);
1661 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1662 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001663 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00001664 RBracLoc);
1665}
1666
1667ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1668 SourceLocation LBracLoc,
1669 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001670 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001671 ObjCMethodDecl *Method,
1672 Expr **Args, unsigned NumArgs,
1673 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001674 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001675 NumArgs * sizeof(Expr *);
1676 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001677 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001678 NumArgs, RBracLoc);
1679}
1680
1681ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1682 SourceLocation LBracLoc,
1683 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001684 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001685 ObjCMethodDecl *Method,
1686 Expr **Args, unsigned NumArgs,
1687 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001688 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001689 NumArgs * sizeof(Expr *);
1690 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001691 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001692 NumArgs, RBracLoc);
1693}
1694
Alexis Hunta8136cc2010-05-05 15:23:54 +00001695ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00001696 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001697 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001698 NumArgs * sizeof(Expr *);
1699 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1700 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1701}
Alexis Hunta8136cc2010-05-05 15:23:54 +00001702
Douglas Gregor9a129192010-04-21 00:45:42 +00001703Selector ObjCMessageExpr::getSelector() const {
1704 if (HasMethod)
1705 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1706 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001707 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00001708}
1709
1710ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1711 switch (getReceiverKind()) {
1712 case Instance:
1713 if (const ObjCObjectPointerType *Ptr
1714 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1715 return Ptr->getInterfaceDecl();
1716 break;
1717
1718 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00001719 if (const ObjCObjectType *Ty
1720 = getClassReceiver()->getAs<ObjCObjectType>())
1721 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00001722 break;
1723
1724 case SuperInstance:
1725 if (const ObjCObjectPointerType *Ptr
1726 = getSuperType()->getAs<ObjCObjectPointerType>())
1727 return Ptr->getInterfaceDecl();
1728 break;
1729
1730 case SuperClass:
1731 if (const ObjCObjectPointerType *Iface
1732 = getSuperType()->getAs<ObjCObjectPointerType>())
1733 return Iface->getInterfaceDecl();
1734 break;
1735 }
1736
1737 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00001738}
Chris Lattner7ec71da2009-04-26 00:44:05 +00001739
Chris Lattner35e564e2007-10-25 00:29:32 +00001740bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001741 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001742}
1743
Nate Begeman48745922009-08-12 02:28:50 +00001744void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1745 unsigned NumExprs) {
1746 if (SubExprs) C.Deallocate(SubExprs);
1747
1748 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001749 this->NumExprs = NumExprs;
1750 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001751}
Nate Begeman48745922009-08-12 02:28:50 +00001752
1753void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1754 DestroyChildren(C);
1755 if (SubExprs) C.Deallocate(SubExprs);
1756 this->~ShuffleVectorExpr();
1757 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00001758}
1759
Douglas Gregore26a2852009-08-07 06:08:38 +00001760void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00001761 // Override default behavior of traversing children. If this has a type
1762 // operand and the type is a variable-length array, the child iteration
1763 // will iterate over the size expression. However, this expression belongs
1764 // to the type, not to this, so we don't want to delete it.
1765 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00001766 if (isArgumentType()) {
1767 this->~SizeOfAlignOfExpr();
1768 C.Deallocate(this);
1769 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001770 else
Douglas Gregore26a2852009-08-07 06:08:38 +00001771 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00001772}
1773
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001774//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001775// DesignatedInitExpr
1776//===----------------------------------------------------------------------===//
1777
1778IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1779 assert(Kind == FieldDesignator && "Only valid on a field designator");
1780 if (Field.NameOrField & 0x01)
1781 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1782 else
1783 return getField()->getIdentifier();
1784}
1785
Alexis Hunta8136cc2010-05-05 15:23:54 +00001786DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001787 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001788 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001789 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001790 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001791 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001792 unsigned NumIndexExprs,
1793 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001794 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001795 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001796 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1797 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001798 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001799
1800 // Record the initializer itself.
1801 child_iterator Child = child_begin();
1802 *Child++ = Init;
1803
1804 // Copy the designators and their subexpressions, computing
1805 // value-dependence along the way.
1806 unsigned IndexIdx = 0;
1807 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001808 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001809
1810 if (this->Designators[I].isArrayDesignator()) {
1811 // Compute type- and value-dependence.
1812 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001813 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001814 Index->isTypeDependent() || Index->isValueDependent();
1815
1816 // Copy the index expressions into permanent storage.
1817 *Child++ = IndexExprs[IndexIdx++];
1818 } else if (this->Designators[I].isArrayRangeDesignator()) {
1819 // Compute type- and value-dependence.
1820 Expr *Start = IndexExprs[IndexIdx];
1821 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001822 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001823 Start->isTypeDependent() || Start->isValueDependent() ||
1824 End->isTypeDependent() || End->isValueDependent();
1825
1826 // Copy the start/end expressions into permanent storage.
1827 *Child++ = IndexExprs[IndexIdx++];
1828 *Child++ = IndexExprs[IndexIdx++];
1829 }
1830 }
1831
1832 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001833}
1834
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001835DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001836DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001837 unsigned NumDesignators,
1838 Expr **IndexExprs, unsigned NumIndexExprs,
1839 SourceLocation ColonOrEqualLoc,
1840 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001841 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001842 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001843 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001844 ColonOrEqualLoc, UsesColonSyntax,
1845 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001846}
1847
Mike Stump11289f42009-09-09 15:08:12 +00001848DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001849 unsigned NumIndexExprs) {
1850 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1851 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1852 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1853}
1854
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001855void DesignatedInitExpr::setDesignators(ASTContext &C,
1856 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001857 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001858 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00001859
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001860 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00001861 NumDesignators = NumDesigs;
1862 for (unsigned I = 0; I != NumDesigs; ++I)
1863 Designators[I] = Desigs[I];
1864}
1865
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001866SourceRange DesignatedInitExpr::getSourceRange() const {
1867 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00001868 Designator &First =
1869 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001870 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001871 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001872 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1873 else
1874 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1875 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00001876 StartLoc =
1877 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001878 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1879}
1880
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001881Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1882 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1883 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1884 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001885 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1886 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1887}
1888
1889Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001890 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001891 "Requires array range designator");
1892 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1893 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001894 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1895 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1896}
1897
1898Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001899 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001900 "Requires array range designator");
1901 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1902 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001903 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1904 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1905}
1906
Douglas Gregord5846a12009-04-15 06:41:24 +00001907/// \brief Replaces the designator at index @p Idx with the series
1908/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001909void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00001910 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00001911 const Designator *Last) {
1912 unsigned NumNewDesignators = Last - First;
1913 if (NumNewDesignators == 0) {
1914 std::copy_backward(Designators + Idx + 1,
1915 Designators + NumDesignators,
1916 Designators + Idx);
1917 --NumNewDesignators;
1918 return;
1919 } else if (NumNewDesignators == 1) {
1920 Designators[Idx] = *First;
1921 return;
1922 }
1923
Mike Stump11289f42009-09-09 15:08:12 +00001924 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001925 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00001926 std::copy(Designators, Designators + Idx, NewDesignators);
1927 std::copy(First, Last, NewDesignators + Idx);
1928 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1929 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001930 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00001931 Designators = NewDesignators;
1932 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1933}
1934
Douglas Gregore26a2852009-08-07 06:08:38 +00001935void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001936 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00001937 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00001938}
1939
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001940void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
1941 for (unsigned I = 0; I != NumDesignators; ++I)
1942 Designators[I].~Designator();
1943 C.Deallocate(Designators);
1944 Designators = 0;
1945}
1946
Mike Stump11289f42009-09-09 15:08:12 +00001947ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001948 Expr **exprs, unsigned nexprs,
1949 SourceLocation rparenloc)
1950: Expr(ParenListExprClass, QualType(),
1951 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00001952 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00001953 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00001954
Nate Begeman5ec4b312009-08-10 23:49:36 +00001955 Exprs = new (C) Stmt*[nexprs];
1956 for (unsigned i = 0; i != nexprs; ++i)
1957 Exprs[i] = exprs[i];
1958}
1959
1960void ParenListExpr::DoDestroy(ASTContext& C) {
1961 DestroyChildren(C);
1962 if (Exprs) C.Deallocate(Exprs);
1963 this->~ParenListExpr();
1964 C.Deallocate(this);
1965}
1966
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001967//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00001968// ExprIterator.
1969//===----------------------------------------------------------------------===//
1970
1971Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1972Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1973Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1974const Expr* ConstExprIterator::operator[](size_t idx) const {
1975 return cast<Expr>(I[idx]);
1976}
1977const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1978const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1979
1980//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001981// Child Iterators for iterating over subexpressions/substatements
1982//===----------------------------------------------------------------------===//
1983
1984// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001985Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1986Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001987
Steve Naroffe46504b2007-11-12 14:29:37 +00001988// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001989Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1990Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00001991
Steve Naroffebf4cb42008-06-02 23:03:37 +00001992// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001993Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1994Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00001995
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001996// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00001997Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00001998 // If this is accessing a class member, skip that entry.
1999 if (Base) return &Base;
2000 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002001}
Mike Stump11289f42009-09-09 15:08:12 +00002002Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2003 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002004}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002005
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002006// ObjCSuperExpr
2007Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2008Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2009
Steve Naroffe87026a2009-07-24 17:54:45 +00002010// ObjCIsaExpr
2011Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2012Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2013
Chris Lattner6307f192008-08-10 01:53:14 +00002014// PredefinedExpr
2015Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2016Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002017
2018// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002019Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2020Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002021
2022// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002023Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002024Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002025
2026// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002027Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2028Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002029
Chris Lattner1c20a172007-08-26 03:42:43 +00002030// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002031Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2032Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002033
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002034// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002035Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2036Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002037
2038// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002039Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2040Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002041
2042// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002043Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2044Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002045
Douglas Gregor882211c2010-04-28 22:16:22 +00002046// OffsetOfExpr
2047Stmt::child_iterator OffsetOfExpr::child_begin() {
2048 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2049 + NumComps);
2050}
2051Stmt::child_iterator OffsetOfExpr::child_end() {
2052 return child_iterator(&*child_begin() + NumExprs);
2053}
2054
Sebastian Redl6f282892008-11-11 17:56:53 +00002055// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002056Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002057 // If this is of a type and the type is a VLA type (and not a typedef), the
2058 // size expression of the VLA needs to be treated as an executable expression.
2059 // Why isn't this weirdness documented better in StmtIterator?
2060 if (isArgumentType()) {
2061 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2062 getArgumentType().getTypePtr()))
2063 return child_iterator(T);
2064 return child_iterator();
2065 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002066 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002067}
Sebastian Redl6f282892008-11-11 17:56:53 +00002068Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2069 if (isArgumentType())
2070 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002071 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002072}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002073
2074// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002075Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002076 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002077}
Ted Kremenek23702b62007-08-24 20:06:47 +00002078Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002079 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002080}
2081
2082// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002083Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002084 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002085}
Ted Kremenek23702b62007-08-24 20:06:47 +00002086Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002087 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002088}
Ted Kremenek23702b62007-08-24 20:06:47 +00002089
2090// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002091Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2092Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002093
Nate Begemance4d7fc2008-04-18 23:10:10 +00002094// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002095Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2096Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002097
2098// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002099Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2100Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002101
Ted Kremenek23702b62007-08-24 20:06:47 +00002102// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002103Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2104Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002105
2106// BinaryOperator
2107Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002108 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002109}
Ted Kremenek23702b62007-08-24 20:06:47 +00002110Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002111 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002112}
2113
2114// ConditionalOperator
2115Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002116 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002117}
Ted Kremenek23702b62007-08-24 20:06:47 +00002118Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002119 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002120}
2121
2122// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002123Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2124Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002125
Ted Kremenek23702b62007-08-24 20:06:47 +00002126// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002127Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2128Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002129
2130// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002131Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2132 return child_iterator();
2133}
2134
2135Stmt::child_iterator TypesCompatibleExpr::child_end() {
2136 return child_iterator();
2137}
Ted Kremenek23702b62007-08-24 20:06:47 +00002138
2139// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002140Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2141Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002142
Douglas Gregor3be4b122008-11-29 04:51:27 +00002143// GNUNullExpr
2144Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2145Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2146
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002147// ShuffleVectorExpr
2148Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002149 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002150}
2151Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002152 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002153}
2154
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002155// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002156Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2157Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002158
Anders Carlsson4692db02007-08-31 04:56:16 +00002159// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002160Stmt::child_iterator InitListExpr::child_begin() {
2161 return InitExprs.size() ? &InitExprs[0] : 0;
2162}
2163Stmt::child_iterator InitListExpr::child_end() {
2164 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2165}
Anders Carlsson4692db02007-08-31 04:56:16 +00002166
Douglas Gregor0202cb42009-01-29 17:44:32 +00002167// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002168Stmt::child_iterator DesignatedInitExpr::child_begin() {
2169 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2170 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002171 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2172}
2173Stmt::child_iterator DesignatedInitExpr::child_end() {
2174 return child_iterator(&*child_begin() + NumSubExprs);
2175}
2176
Douglas Gregor0202cb42009-01-29 17:44:32 +00002177// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002178Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2179 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002180}
2181
Mike Stump11289f42009-09-09 15:08:12 +00002182Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2183 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002184}
2185
Nate Begeman5ec4b312009-08-10 23:49:36 +00002186// ParenListExpr
2187Stmt::child_iterator ParenListExpr::child_begin() {
2188 return &Exprs[0];
2189}
2190Stmt::child_iterator ParenListExpr::child_end() {
2191 return &Exprs[0]+NumExprs;
2192}
2193
Ted Kremenek23702b62007-08-24 20:06:47 +00002194// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002195Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002196 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002197}
2198Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002199 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002200}
Ted Kremenek23702b62007-08-24 20:06:47 +00002201
2202// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002203Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2204Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002205
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002206// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002207Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002208 return child_iterator();
2209}
2210Stmt::child_iterator ObjCSelectorExpr::child_end() {
2211 return child_iterator();
2212}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002213
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002214// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002215Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2216 return child_iterator();
2217}
2218Stmt::child_iterator ObjCProtocolExpr::child_end() {
2219 return child_iterator();
2220}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002221
Steve Naroffd54978b2007-09-18 23:55:05 +00002222// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002223Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002224 if (getReceiverKind() == Instance)
2225 return reinterpret_cast<Stmt **>(this + 1);
2226 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002227}
2228Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002229 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002230}
2231
Steve Naroffc540d662008-09-03 18:15:37 +00002232// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002233Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2234Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002235
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002236Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2237Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }