blob: 7642c0de2cb92762c65a2dd2b5d4cd60c898e266 [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
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000231DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
232 unsigned NumTemplateArgs) {
233 std::size_t Size = sizeof(DeclRefExpr);
234 if (HasQualifier)
235 Size += sizeof(NameQualifier);
236
237 if (NumTemplateArgs)
238 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
239
240 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
241 return new (Mem) DeclRefExpr(EmptyShell());
242}
243
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000244SourceRange DeclRefExpr::getSourceRange() const {
245 // FIXME: Does not handle multi-token names well, e.g., operator[].
246 SourceRange R(Loc);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000247
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000248 if (hasQualifier())
249 R.setBegin(getQualifierRange().getBegin());
250 if (hasExplicitTemplateArgumentList())
251 R.setEnd(getRAngleLoc());
252 return R;
253}
254
Anders Carlsson2fb08242009-09-08 18:24:21 +0000255// FIXME: Maybe this should use DeclPrinter with a special "print predefined
256// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000257std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
258 ASTContext &Context = CurrentDecl->getASTContext();
259
Anders Carlsson2fb08242009-09-08 18:24:21 +0000260 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000261 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000262 return FD->getNameAsString();
263
264 llvm::SmallString<256> Name;
265 llvm::raw_svector_ostream Out(Name);
266
267 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000268 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000269 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000270 if (MD->isStatic())
271 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000272 }
273
274 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000275
276 std::string Proto = FD->getQualifiedNameAsString(Policy);
277
John McCall9dd450b2009-09-21 23:43:11 +0000278 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000279 const FunctionProtoType *FT = 0;
280 if (FD->hasWrittenPrototype())
281 FT = dyn_cast<FunctionProtoType>(AFT);
282
283 Proto += "(";
284 if (FT) {
285 llvm::raw_string_ostream POut(Proto);
286 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
287 if (i) POut << ", ";
288 std::string Param;
289 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
290 POut << Param;
291 }
292
293 if (FT->isVariadic()) {
294 if (FD->getNumParams()) POut << ", ";
295 POut << "...";
296 }
297 }
298 Proto += ")";
299
Sam Weinig4e83bd22009-12-27 01:38:20 +0000300 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
301 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
302 if (ThisQuals.hasConst())
303 Proto += " const";
304 if (ThisQuals.hasVolatile())
305 Proto += " volatile";
306 }
307
Sam Weinigd060ed42009-12-06 23:55:13 +0000308 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
309 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000310
311 Out << Proto;
312
313 Out.flush();
314 return Name.str().str();
315 }
316 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
317 llvm::SmallString<256> Name;
318 llvm::raw_svector_ostream Out(Name);
319 Out << (MD->isInstanceMethod() ? '-' : '+');
320 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000321
322 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
323 // a null check to avoid a crash.
324 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000325 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000326
Anders Carlsson2fb08242009-09-08 18:24:21 +0000327 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000328 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
329 Out << '(' << CID << ')';
330
Anders Carlsson2fb08242009-09-08 18:24:21 +0000331 Out << ' ';
332 Out << MD->getSelector().getAsString();
333 Out << ']';
334
335 Out.flush();
336 return Name.str().str();
337 }
338 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
339 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
340 return "top level";
341 }
342 return "";
343}
344
Chris Lattnera0173132008-06-07 22:13:43 +0000345/// getValueAsApproximateDouble - This returns the value as an inaccurate
346/// double. Note that this may cause loss of precision, but is useful for
347/// debugging dumps, etc.
348double FloatingLiteral::getValueAsApproximateDouble() const {
349 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000350 bool ignored;
351 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
352 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000353 return V.convertToDouble();
354}
355
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000356StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
357 unsigned ByteLength, bool Wide,
358 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000359 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000360 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000361 // Allocate enough space for the StringLiteral plus an array of locations for
362 // any concatenated string tokens.
363 void *Mem = C.Allocate(sizeof(StringLiteral)+
364 sizeof(SourceLocation)*(NumStrs-1),
365 llvm::alignof<StringLiteral>());
366 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Steve Naroffdf7855b2007-02-21 23:46:25 +0000368 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000369 char *AStrData = new (C, 1) char[ByteLength];
370 memcpy(AStrData, StrData, ByteLength);
371 SL->StrData = AStrData;
372 SL->ByteLength = ByteLength;
373 SL->IsWide = Wide;
374 SL->TokLocs[0] = Loc[0];
375 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000376
Chris Lattner630970d2009-02-18 05:49:11 +0000377 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000378 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
379 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000380}
381
Douglas Gregor958dfc92009-04-15 16:35:07 +0000382StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
383 void *Mem = C.Allocate(sizeof(StringLiteral)+
384 sizeof(SourceLocation)*(NumStrs-1),
385 llvm::alignof<StringLiteral>());
386 StringLiteral *SL = new (Mem) StringLiteral(QualType());
387 SL->StrData = 0;
388 SL->ByteLength = 0;
389 SL->NumConcatenated = NumStrs;
390 return SL;
391}
392
Daniel Dunbar36217882009-09-22 03:27:33 +0000393void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000394 char *AStrData = new (C, 1) char[Str.size()];
395 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000396 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000397 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000398}
399
Chris Lattner1b926492006-08-23 06:42:10 +0000400/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
401/// corresponds to, e.g. "sizeof" or "[pre]++".
402const char *UnaryOperator::getOpcodeStr(Opcode Op) {
403 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000404 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000405 case PostInc: return "++";
406 case PostDec: return "--";
407 case PreInc: return "++";
408 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000409 case AddrOf: return "&";
410 case Deref: return "*";
411 case Plus: return "+";
412 case Minus: return "-";
413 case Not: return "~";
414 case LNot: return "!";
415 case Real: return "__real";
416 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000417 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000418 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000419 }
420}
421
Mike Stump11289f42009-09-09 15:08:12 +0000422UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000423UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
424 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000425 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000426 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
427 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
428 case OO_Amp: return AddrOf;
429 case OO_Star: return Deref;
430 case OO_Plus: return Plus;
431 case OO_Minus: return Minus;
432 case OO_Tilde: return Not;
433 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000434 }
435}
436
437OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
438 switch (Opc) {
439 case PostInc: case PreInc: return OO_PlusPlus;
440 case PostDec: case PreDec: return OO_MinusMinus;
441 case AddrOf: return OO_Amp;
442 case Deref: return OO_Star;
443 case Plus: return OO_Plus;
444 case Minus: return OO_Minus;
445 case Not: return OO_Tilde;
446 case LNot: return OO_Exclaim;
447 default: return OO_None;
448 }
449}
450
451
Chris Lattner0eedafe2006-08-24 04:56:27 +0000452//===----------------------------------------------------------------------===//
453// Postfix Operators.
454//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000455
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000456CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000457 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000458 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000459 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000460 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000461 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000463 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000464 SubExprs[FN] = fn;
465 for (unsigned i = 0; i != numargs; ++i)
466 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000467
Douglas Gregor993603d2008-11-14 16:09:21 +0000468 RParenLoc = rparenloc;
469}
Nate Begeman1e36a852008-01-17 17:46:27 +0000470
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000471CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
472 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000473 : Expr(CallExprClass, t,
474 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000475 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000476 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000477
478 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000479 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000480 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000481 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000482
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000483 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000484}
485
Mike Stump11289f42009-09-09 15:08:12 +0000486CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
487 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000488 SubExprs = new (C) Stmt*[1];
489}
490
Nuno Lopes518e3702009-12-20 23:11:08 +0000491Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000492 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000493 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000494 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000495 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
496 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000497
498 return 0;
499}
500
Nuno Lopes518e3702009-12-20 23:11:08 +0000501FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000502 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000503}
504
Chris Lattnere4407ed2007-12-28 05:25:02 +0000505/// setNumArgs - This changes the number of arguments present in this call.
506/// Any orphaned expressions are deleted by this, and any new operands are set
507/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000508void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000509 // No change, just return.
510 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattnere4407ed2007-12-28 05:25:02 +0000512 // If shrinking # arguments, just delete the extras and forgot them.
513 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000514 this->NumArgs = NumArgs;
515 return;
516 }
517
518 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000519 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000520 // Copy over args.
521 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
522 NewSubExprs[i] = SubExprs[i];
523 // Null out new args.
524 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
525 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Douglas Gregorba6e5572009-04-17 21:46:47 +0000527 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000528 SubExprs = NewSubExprs;
529 this->NumArgs = NumArgs;
530}
531
Chris Lattner01ff98a2008-10-06 05:00:53 +0000532/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
533/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000534unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000535 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000536 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000537 // ImplicitCastExpr.
538 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
539 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000540 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Steve Narofff6e3b3292008-01-31 01:07:12 +0000542 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
543 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000544 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000546 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
547 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000548 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000550 if (!FDecl->getIdentifier())
551 return 0;
552
Douglas Gregor15fc9562009-09-12 00:22:50 +0000553 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000554}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000555
Anders Carlsson00a27592009-05-26 04:57:27 +0000556QualType CallExpr::getCallReturnType() const {
557 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000558 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000559 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000560 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000561 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000562 else if (const MemberPointerType *MPT
563 = CalleeType->getAs<MemberPointerType>())
564 CalleeType = MPT->getPointeeType();
565
John McCall9dd450b2009-09-21 23:43:11 +0000566 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000567 return FnType->getResultType();
568}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000569
Alexis Hunta8136cc2010-05-05 15:23:54 +0000570OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000571 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000572 TypeSourceInfo *tsi,
573 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000574 Expr** exprsPtr, unsigned numExprs,
575 SourceLocation RParenLoc) {
576 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000577 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000578 sizeof(Expr*) * numExprs);
579
580 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
581 exprsPtr, numExprs, RParenLoc);
582}
583
584OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
585 unsigned numComps, unsigned numExprs) {
586 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
587 sizeof(OffsetOfNode) * numComps +
588 sizeof(Expr*) * numExprs);
589 return new (Mem) OffsetOfExpr(numComps, numExprs);
590}
591
Alexis Hunta8136cc2010-05-05 15:23:54 +0000592OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000593 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000594 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000595 Expr** exprsPtr, unsigned numExprs,
596 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000597 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000598 /*ValueDependent=*/tsi->getType()->isDependentType() ||
599 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
600 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000601 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
602 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000603{
604 for(unsigned i = 0; i < numComps; ++i) {
605 setComponent(i, compsPtr[i]);
606 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000607
Douglas Gregor882211c2010-04-28 22:16:22 +0000608 for(unsigned i = 0; i < numExprs; ++i) {
609 setIndexExpr(i, exprsPtr[i]);
610 }
611}
612
613IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
614 assert(getKind() == Field || getKind() == Identifier);
615 if (getKind() == Field)
616 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000617
Douglas Gregor882211c2010-04-28 22:16:22 +0000618 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
619}
620
Mike Stump11289f42009-09-09 15:08:12 +0000621MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
622 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000623 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000624 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000625 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000626 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000627 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000628 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000629 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000630
John McCalla8ae2222010-04-06 21:38:20 +0000631 bool hasQualOrFound = (qual != 0 ||
632 founddecl.getDecl() != memberdecl ||
633 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000634 if (hasQualOrFound)
635 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000636
John McCall6b51f282009-11-23 01:53:49 +0000637 if (targs)
638 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000640 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000641 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
642
643 if (hasQualOrFound) {
644 if (qual && qual->isDependent()) {
645 E->setValueDependent(true);
646 E->setTypeDependent(true);
647 }
648 E->HasQualifierOrFoundDecl = true;
649
650 MemberNameQualifier *NQ = E->getMemberQualifier();
651 NQ->NNS = qual;
652 NQ->Range = qualrange;
653 NQ->FoundDecl = founddecl;
654 }
655
656 if (targs) {
657 E->HasExplicitTemplateArgumentList = true;
658 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
659 }
660
661 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000662}
663
Anders Carlsson496335e2009-09-03 00:59:21 +0000664const char *CastExpr::getCastKindName() const {
665 switch (getCastKind()) {
666 case CastExpr::CK_Unknown:
667 return "Unknown";
668 case CastExpr::CK_BitCast:
669 return "BitCast";
Douglas Gregor51954272010-07-13 23:17:26 +0000670 case CastExpr::CK_LValueBitCast:
671 return "LValueBitCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000672 case CastExpr::CK_NoOp:
673 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000674 case CastExpr::CK_BaseToDerived:
675 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000676 case CastExpr::CK_DerivedToBase:
677 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000678 case CastExpr::CK_UncheckedDerivedToBase:
679 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000680 case CastExpr::CK_Dynamic:
681 return "Dynamic";
682 case CastExpr::CK_ToUnion:
683 return "ToUnion";
684 case CastExpr::CK_ArrayToPointerDecay:
685 return "ArrayToPointerDecay";
686 case CastExpr::CK_FunctionToPointerDecay:
687 return "FunctionToPointerDecay";
688 case CastExpr::CK_NullToMemberPointer:
689 return "NullToMemberPointer";
690 case CastExpr::CK_BaseToDerivedMemberPointer:
691 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000692 case CastExpr::CK_DerivedToBaseMemberPointer:
693 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000694 case CastExpr::CK_UserDefinedConversion:
695 return "UserDefinedConversion";
696 case CastExpr::CK_ConstructorConversion:
697 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000698 case CastExpr::CK_IntegralToPointer:
699 return "IntegralToPointer";
700 case CastExpr::CK_PointerToIntegral:
701 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000702 case CastExpr::CK_ToVoid:
703 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000704 case CastExpr::CK_VectorSplat:
705 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000706 case CastExpr::CK_IntegralCast:
707 return "IntegralCast";
708 case CastExpr::CK_IntegralToFloating:
709 return "IntegralToFloating";
710 case CastExpr::CK_FloatingToIntegral:
711 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000712 case CastExpr::CK_FloatingCast:
713 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000714 case CastExpr::CK_MemberPointerToBoolean:
715 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000716 case CastExpr::CK_AnyPointerToObjCPointerCast:
717 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000718 case CastExpr::CK_AnyPointerToBlockPointerCast:
719 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000720 }
Mike Stump11289f42009-09-09 15:08:12 +0000721
Anders Carlsson496335e2009-09-03 00:59:21 +0000722 assert(0 && "Unhandled cast kind!");
723 return 0;
724}
725
Douglas Gregord196a582009-12-14 19:27:10 +0000726Expr *CastExpr::getSubExprAsWritten() {
727 Expr *SubExpr = 0;
728 CastExpr *E = this;
729 do {
730 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000731
Douglas Gregord196a582009-12-14 19:27:10 +0000732 // Skip any temporary bindings; they're implicit.
733 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
734 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000735
Douglas Gregord196a582009-12-14 19:27:10 +0000736 // Conversions by constructor and conversion functions have a
737 // subexpression describing the call; strip it off.
738 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
739 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
740 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
741 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000742
Douglas Gregord196a582009-12-14 19:27:10 +0000743 // If the subexpression we're left with is an implicit cast, look
744 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000745 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
746
Douglas Gregord196a582009-12-14 19:27:10 +0000747 return SubExpr;
748}
749
Chris Lattner1b926492006-08-23 06:42:10 +0000750/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
751/// corresponds to, e.g. "<<=".
752const char *BinaryOperator::getOpcodeStr(Opcode Op) {
753 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000754 case PtrMemD: return ".*";
755 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000756 case Mul: return "*";
757 case Div: return "/";
758 case Rem: return "%";
759 case Add: return "+";
760 case Sub: return "-";
761 case Shl: return "<<";
762 case Shr: return ">>";
763 case LT: return "<";
764 case GT: return ">";
765 case LE: return "<=";
766 case GE: return ">=";
767 case EQ: return "==";
768 case NE: return "!=";
769 case And: return "&";
770 case Xor: return "^";
771 case Or: return "|";
772 case LAnd: return "&&";
773 case LOr: return "||";
774 case Assign: return "=";
775 case MulAssign: return "*=";
776 case DivAssign: return "/=";
777 case RemAssign: return "%=";
778 case AddAssign: return "+=";
779 case SubAssign: return "-=";
780 case ShlAssign: return "<<=";
781 case ShrAssign: return ">>=";
782 case AndAssign: return "&=";
783 case XorAssign: return "^=";
784 case OrAssign: return "|=";
785 case Comma: return ",";
786 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000787
788 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000789}
Steve Naroff47500512007-04-19 23:00:49 +0000790
Mike Stump11289f42009-09-09 15:08:12 +0000791BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000792BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
793 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000794 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000795 case OO_Plus: return Add;
796 case OO_Minus: return Sub;
797 case OO_Star: return Mul;
798 case OO_Slash: return Div;
799 case OO_Percent: return Rem;
800 case OO_Caret: return Xor;
801 case OO_Amp: return And;
802 case OO_Pipe: return Or;
803 case OO_Equal: return Assign;
804 case OO_Less: return LT;
805 case OO_Greater: return GT;
806 case OO_PlusEqual: return AddAssign;
807 case OO_MinusEqual: return SubAssign;
808 case OO_StarEqual: return MulAssign;
809 case OO_SlashEqual: return DivAssign;
810 case OO_PercentEqual: return RemAssign;
811 case OO_CaretEqual: return XorAssign;
812 case OO_AmpEqual: return AndAssign;
813 case OO_PipeEqual: return OrAssign;
814 case OO_LessLess: return Shl;
815 case OO_GreaterGreater: return Shr;
816 case OO_LessLessEqual: return ShlAssign;
817 case OO_GreaterGreaterEqual: return ShrAssign;
818 case OO_EqualEqual: return EQ;
819 case OO_ExclaimEqual: return NE;
820 case OO_LessEqual: return LE;
821 case OO_GreaterEqual: return GE;
822 case OO_AmpAmp: return LAnd;
823 case OO_PipePipe: return LOr;
824 case OO_Comma: return Comma;
825 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000826 }
827}
828
829OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
830 static const OverloadedOperatorKind OverOps[] = {
831 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
832 OO_Star, OO_Slash, OO_Percent,
833 OO_Plus, OO_Minus,
834 OO_LessLess, OO_GreaterGreater,
835 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
836 OO_EqualEqual, OO_ExclaimEqual,
837 OO_Amp,
838 OO_Caret,
839 OO_Pipe,
840 OO_AmpAmp,
841 OO_PipePipe,
842 OO_Equal, OO_StarEqual,
843 OO_SlashEqual, OO_PercentEqual,
844 OO_PlusEqual, OO_MinusEqual,
845 OO_LessLessEqual, OO_GreaterGreaterEqual,
846 OO_AmpEqual, OO_CaretEqual,
847 OO_PipeEqual,
848 OO_Comma
849 };
850 return OverOps[Opc];
851}
852
Ted Kremenekac034612010-04-13 23:39:13 +0000853InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000854 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000855 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000856 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000857 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000858 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000859 UnionFieldInit(0), HadArrayRangeDesignator(false)
860{
Ted Kremenek013041e2010-02-19 01:50:18 +0000861 for (unsigned I = 0; I != numInits; ++I) {
862 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000863 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000864 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000865 ValueDependent = true;
866 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000867
Ted Kremenekac034612010-04-13 23:39:13 +0000868 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000869}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000870
Ted Kremenekac034612010-04-13 23:39:13 +0000871void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000872 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000873 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000874}
875
Ted Kremenekac034612010-04-13 23:39:13 +0000876void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +0000877 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000878}
879
Ted Kremenekac034612010-04-13 23:39:13 +0000880Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000881 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000882 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000883 InitExprs.back() = expr;
884 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000887 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
888 InitExprs[Init] = expr;
889 return Result;
890}
891
Steve Naroff991e99d2008-09-04 15:31:07 +0000892/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000893///
894const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000895 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000896 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000897}
898
Mike Stump11289f42009-09-09 15:08:12 +0000899SourceLocation BlockExpr::getCaretLocation() const {
900 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000901}
Mike Stump11289f42009-09-09 15:08:12 +0000902const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000903 return TheBlock->getBody();
904}
Mike Stump11289f42009-09-09 15:08:12 +0000905Stmt *BlockExpr::getBody() {
906 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000907}
Steve Naroff415d3d52008-10-08 17:01:13 +0000908
909
Chris Lattner1ec5f562007-06-27 05:38:08 +0000910//===----------------------------------------------------------------------===//
911// Generic Expression Routines
912//===----------------------------------------------------------------------===//
913
Chris Lattner237f2752009-02-14 07:37:35 +0000914/// isUnusedResultAWarning - Return true if this immediate expression should
915/// be warned about if the result is unused. If so, fill in Loc and Ranges
916/// with location to warn on and the source range[s] to report with the
917/// warning.
918bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000919 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000920 // Don't warn if the expr is type dependent. The type could end up
921 // instantiating to void.
922 if (isTypeDependent())
923 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner1ec5f562007-06-27 05:38:08 +0000925 switch (getStmtClass()) {
926 default:
John McCallc493a732010-03-12 07:11:26 +0000927 if (getType()->isVoidType())
928 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000929 Loc = getExprLoc();
930 R1 = getSourceRange();
931 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000932 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000933 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000934 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000935 case UnaryOperatorClass: {
936 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattner1ec5f562007-06-27 05:38:08 +0000938 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000939 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000940 case UnaryOperator::PostInc:
941 case UnaryOperator::PostDec:
942 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000943 case UnaryOperator::PreDec: // ++/--
944 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000945 case UnaryOperator::Deref:
946 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000947 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000948 return false;
949 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000950 case UnaryOperator::Real:
951 case UnaryOperator::Imag:
952 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000953 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
954 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000955 return false;
956 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000957 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000958 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000959 }
Chris Lattner237f2752009-02-14 07:37:35 +0000960 Loc = UO->getOperatorLoc();
961 R1 = UO->getSubExpr()->getSourceRange();
962 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000963 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000964 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000965 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000966 switch (BO->getOpcode()) {
967 default:
968 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +0000969 // Consider the RHS of comma for side effects. LHS was checked by
970 // Sema::CheckCommaOperands.
Ted Kremenek43a9c962010-04-07 18:49:21 +0000971 case BinaryOperator::Comma:
972 // ((foo = <blah>), 0) is an idiom for hiding the result (and
973 // lvalue-ness) of an assignment written in a macro.
974 if (IntegerLiteral *IE =
975 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
976 if (IE->getValue() == 0)
977 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +0000978 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
979 // Consider '||', '&&' to have side effects if the LHS or RHS does.
Ted Kremenek43a9c962010-04-07 18:49:21 +0000980 case BinaryOperator::LAnd:
981 case BinaryOperator::LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +0000982 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
983 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
984 return false;
985 break;
John McCall1e3715a2010-02-16 04:10:53 +0000986 }
Chris Lattner237f2752009-02-14 07:37:35 +0000987 if (BO->isAssignmentOp())
988 return false;
989 Loc = BO->getOperatorLoc();
990 R1 = BO->getLHS()->getSourceRange();
991 R2 = BO->getRHS()->getSourceRange();
992 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000993 }
Chris Lattner86928112007-08-25 02:00:02 +0000994 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +0000995 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000996 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000997
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000998 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000999 // The condition must be evaluated, but if either the LHS or RHS is a
1000 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001001 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001002 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001003 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001004 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001005 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001006 }
1007
Chris Lattnera44d1162007-06-27 05:58:59 +00001008 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001009 // If the base pointer or element is to a volatile pointer/field, accessing
1010 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001011 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001012 return false;
1013 Loc = cast<MemberExpr>(this)->getMemberLoc();
1014 R1 = SourceRange(Loc, Loc);
1015 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1016 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001017
Chris Lattner1ec5f562007-06-27 05:38:08 +00001018 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001019 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001020 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001021 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001022 return false;
1023 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1024 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1025 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1026 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001027
Chris Lattner1ec5f562007-06-27 05:38:08 +00001028 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001029 case CXXOperatorCallExprClass:
1030 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001031 // If this is a direct call, get the callee.
1032 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001033 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001034 // If the callee has attribute pure, const, or warn_unused_result, warn
1035 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001036 //
1037 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1038 // updated to match for QoI.
1039 if (FD->getAttr<WarnUnusedResultAttr>() ||
1040 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1041 Loc = CE->getCallee()->getLocStart();
1042 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattner1a6babf2009-10-13 04:53:48 +00001044 if (unsigned NumArgs = CE->getNumArgs())
1045 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1046 CE->getArg(NumArgs-1)->getLocEnd());
1047 return true;
1048 }
Chris Lattner237f2752009-02-14 07:37:35 +00001049 }
1050 return false;
1051 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001052
1053 case CXXTemporaryObjectExprClass:
1054 case CXXConstructExprClass:
1055 return false;
1056
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001057 case ObjCMessageExprClass: {
1058 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1059 const ObjCMethodDecl *MD = ME->getMethodDecl();
1060 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1061 Loc = getExprLoc();
1062 return true;
1063 }
Chris Lattner237f2752009-02-14 07:37:35 +00001064 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001065 }
Mike Stump11289f42009-09-09 15:08:12 +00001066
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001067 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001068#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001069 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001070 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001071 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001072 Loc = Ref->getLocation();
1073 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1074 if (Ref->getBase())
1075 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001076#else
1077 Loc = getExprLoc();
1078 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001079#endif
1080 return true;
1081 }
Chris Lattner944d3062008-07-26 19:51:01 +00001082 case StmtExprClass: {
1083 // Statement exprs don't logically have side effects themselves, but are
1084 // sometimes used in macros in ways that give them a type that is unused.
1085 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1086 // however, if the result of the stmt expr is dead, we don't want to emit a
1087 // warning.
1088 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1089 if (!CS->body_empty())
1090 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001091 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001092
John McCallc493a732010-03-12 07:11:26 +00001093 if (getType()->isVoidType())
1094 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001095 Loc = cast<StmtExpr>(this)->getLParenLoc();
1096 R1 = getSourceRange();
1097 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001098 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001099 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001100 // If this is an explicit cast to void, allow it. People do this when they
1101 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001102 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001103 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001104 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1105 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1106 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001107 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001108 if (getType()->isVoidType())
1109 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001110 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001111
Anders Carlsson6aa50392009-11-17 17:11:23 +00001112 // If this is a cast to void or a constructor conversion, check the operand.
1113 // Otherwise, the result of the cast is unused.
1114 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1115 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001116 return (cast<CastExpr>(this)->getSubExpr()
1117 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001118 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1119 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1120 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001123 case ImplicitCastExprClass:
1124 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001125 return (cast<ImplicitCastExpr>(this)
1126 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001127
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001128 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001129 return (cast<CXXDefaultArgExpr>(this)
1130 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001131
1132 case CXXNewExprClass:
1133 // FIXME: In theory, there might be new expressions that don't have side
1134 // effects (e.g. a placement new with an uninitialized POD).
1135 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001136 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001137 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001138 return (cast<CXXBindTemporaryExpr>(this)
1139 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001140 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001141 return (cast<CXXExprWithTemporaries>(this)
1142 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001143 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001144}
1145
Fariborz Jahanian07735332009-02-22 18:40:18 +00001146/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001147/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001148bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001149 switch (getStmtClass()) {
1150 default:
1151 return false;
1152 case ObjCIvarRefExprClass:
1153 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001154 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001155 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001156 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001157 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001158 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001159 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001160 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001161 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001162 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001163 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001164 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1165 if (VD->hasGlobalStorage())
1166 return true;
1167 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001168 // dereferencing to a pointer is always a gc'able candidate,
1169 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001170 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001171 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001172 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001173 return false;
1174 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001175 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001176 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001177 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001178 }
1179 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001180 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001181 }
1182}
Ted Kremenekfff70962008-01-17 16:57:34 +00001183Expr* Expr::IgnoreParens() {
1184 Expr* E = this;
1185 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1186 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001187
Ted Kremenekfff70962008-01-17 16:57:34 +00001188 return E;
1189}
1190
Chris Lattnerf2660962008-02-13 01:02:39 +00001191/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1192/// or CastExprs or ImplicitCastExprs, returning their operand.
1193Expr *Expr::IgnoreParenCasts() {
1194 Expr *E = this;
1195 while (true) {
1196 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1197 E = P->getSubExpr();
1198 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1199 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001200 else
1201 return E;
1202 }
1203}
1204
John McCalleebc8322010-05-05 22:59:52 +00001205Expr *Expr::IgnoreParenImpCasts() {
1206 Expr *E = this;
1207 while (true) {
1208 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1209 E = P->getSubExpr();
1210 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1211 E = P->getSubExpr();
1212 else
1213 return E;
1214 }
1215}
1216
Chris Lattneref26c772009-03-13 17:28:01 +00001217/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1218/// value (including ptr->int casts of the same size). Strip off any
1219/// ParenExpr or CastExprs, returning their operand.
1220Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1221 Expr *E = this;
1222 while (true) {
1223 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1224 E = P->getSubExpr();
1225 continue;
1226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
Chris Lattneref26c772009-03-13 17:28:01 +00001228 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1229 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001230 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001231 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001232
Chris Lattneref26c772009-03-13 17:28:01 +00001233 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1234 E = SE;
1235 continue;
1236 }
Mike Stump11289f42009-09-09 15:08:12 +00001237
Douglas Gregor6972a622010-06-16 00:35:25 +00001238 if ((E->getType()->isPointerType() ||
1239 E->getType()->isIntegralType(Ctx)) &&
1240 (SE->getType()->isPointerType() ||
1241 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001242 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1243 E = SE;
1244 continue;
1245 }
1246 }
Mike Stump11289f42009-09-09 15:08:12 +00001247
Chris Lattneref26c772009-03-13 17:28:01 +00001248 return E;
1249 }
1250}
1251
Douglas Gregord196a582009-12-14 19:27:10 +00001252bool Expr::isDefaultArgument() const {
1253 const Expr *E = this;
1254 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1255 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001256
Douglas Gregord196a582009-12-14 19:27:10 +00001257 return isa<CXXDefaultArgExpr>(E);
1258}
Chris Lattneref26c772009-03-13 17:28:01 +00001259
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001260/// \brief Skip over any no-op casts and any temporary-binding
1261/// expressions.
1262static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1263 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1264 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1265 E = ICE->getSubExpr();
1266 else
1267 break;
1268 }
1269
1270 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1271 E = BE->getSubExpr();
1272
1273 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1274 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1275 E = ICE->getSubExpr();
1276 else
1277 break;
1278 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001279
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001280 return E;
1281}
1282
1283const Expr *Expr::getTemporaryObject() const {
1284 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1285
1286 // A cast can produce a temporary object. The object's construction
1287 // is represented as a CXXConstructExpr.
1288 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1289 // Only user-defined and constructor conversions can produce
1290 // temporary objects.
1291 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1292 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1293 return 0;
1294
1295 // Strip off temporary bindings and no-op casts.
1296 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1297
1298 // If this is a constructor conversion, see if we have an object
1299 // construction.
1300 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1301 return dyn_cast<CXXConstructExpr>(Sub);
1302
1303 // If this is a user-defined conversion, see if we have a call to
1304 // a function that itself returns a temporary object.
1305 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1306 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1307 if (CE->getCallReturnType()->isRecordType())
1308 return CE;
1309
1310 return 0;
1311 }
1312
1313 // A call returning a class type returns a temporary.
1314 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1315 if (CE->getCallReturnType()->isRecordType())
1316 return CE;
1317
1318 return 0;
1319 }
1320
1321 // Explicit temporary object constructors create temporaries.
1322 return dyn_cast<CXXTemporaryObjectExpr>(E);
1323}
1324
Douglas Gregor4619e432008-12-05 23:32:09 +00001325/// hasAnyTypeDependentArguments - Determines if any of the expressions
1326/// in Exprs is type-dependent.
1327bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1328 for (unsigned I = 0; I < NumExprs; ++I)
1329 if (Exprs[I]->isTypeDependent())
1330 return true;
1331
1332 return false;
1333}
1334
1335/// hasAnyValueDependentArguments - Determines if any of the expressions
1336/// in Exprs is value-dependent.
1337bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1338 for (unsigned I = 0; I < NumExprs; ++I)
1339 if (Exprs[I]->isValueDependent())
1340 return true;
1341
1342 return false;
1343}
1344
John McCall8b0f4ff2010-08-02 21:13:48 +00001345bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00001346 // This function is attempting whether an expression is an initializer
1347 // which can be evaluated at compile-time. isEvaluatable handles most
1348 // of the cases, but it can't deal with some initializer-specific
1349 // expressions, and it can't deal with aggregates; we deal with those here,
1350 // and fall back to isEvaluatable for the other cases.
1351
John McCall8b0f4ff2010-08-02 21:13:48 +00001352 // If we ever capture reference-binding directly in the AST, we can
1353 // kill the second parameter.
1354
1355 if (IsForRef) {
1356 EvalResult Result;
1357 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1358 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001359
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001360 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001361 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001362 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001363 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001364 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001365 return true;
John McCall81c9cea2010-08-01 21:51:45 +00001366 case CXXTemporaryObjectExprClass:
1367 case CXXConstructExprClass: {
1368 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00001369
1370 // Only if it's
1371 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00001372 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00001373 if (!CE->getNumArgs()) return true;
1374
1375 // 2) an elidable trivial copy construction of an operand which is
1376 // itself a constant initializer. Note that we consider the
1377 // operand on its own, *not* as a reference binding.
1378 return CE->isElidable() &&
1379 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00001380 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001381 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001382 // This handles gcc's extension that allows global initializers like
1383 // "struct x {int x;} x = (struct x) {};".
1384 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001385 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00001386 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001387 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001388 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001389 // FIXME: This doesn't deal with fields with reference types correctly.
1390 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1391 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001392 const InitListExpr *Exp = cast<InitListExpr>(this);
1393 unsigned numInits = Exp->getNumInits();
1394 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00001395 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001396 return false;
1397 }
Eli Friedman384da272009-01-25 03:12:18 +00001398 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001399 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001400 case ImplicitValueInitExprClass:
1401 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001402 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00001403 return cast<ParenExpr>(this)->getSubExpr()
1404 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00001405 case UnaryOperatorClass: {
1406 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1407 if (Exp->getOpcode() == UnaryOperator::Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00001408 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00001409 break;
1410 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001411 case BinaryOperatorClass: {
1412 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1413 // but this handles the common case.
1414 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1415 if (Exp->getOpcode() == BinaryOperator::Sub &&
1416 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1417 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1418 return true;
1419 break;
1420 }
John McCall8b0f4ff2010-08-02 21:13:48 +00001421 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00001422 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00001423 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001424 case CStyleCastExprClass:
1425 // Handle casts with a destination that's a struct or union; this
1426 // deals with both the gcc no-op struct cast extension and the
1427 // cast-to-union extension.
1428 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001429 return cast<CastExpr>(this)->getSubExpr()
1430 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001431
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001432 // Integer->integer casts can be handled here, which is important for
1433 // things like (int)(&&x-&&y). Scary but true.
1434 if (getType()->isIntegerType() &&
1435 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001436 return cast<CastExpr>(this)->getSubExpr()
1437 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001438
Eli Friedman384da272009-01-25 03:12:18 +00001439 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001440 }
Eli Friedman384da272009-01-25 03:12:18 +00001441 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001442}
1443
Chris Lattner7eef9192007-05-24 01:23:49 +00001444/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1445/// integer constant expression with the value zero, or if this is one that is
1446/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001447bool Expr::isNullPointerConstant(ASTContext &Ctx,
1448 NullPointerConstantValueDependence NPC) const {
1449 if (isValueDependent()) {
1450 switch (NPC) {
1451 case NPC_NeverValueDependent:
1452 assert(false && "Unexpected value dependent expression!");
1453 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001454
Douglas Gregor56751b52009-09-25 04:25:58 +00001455 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001456 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001457
Douglas Gregor56751b52009-09-25 04:25:58 +00001458 case NPC_ValueDependentIsNotNull:
1459 return false;
1460 }
1461 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001462
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001463 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001464 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001465 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001466 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001467 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001468 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001469 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001470 Pointee->isVoidType() && // to void*
1471 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001472 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001473 }
Steve Naroffada7d422007-05-20 17:54:12 +00001474 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001475 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1476 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001477 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001478 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1479 // Accept ((void*)0) as a null pointer constant, as many other
1480 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001481 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001482 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001483 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001484 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001485 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001486 } else if (isa<GNUNullExpr>(this)) {
1487 // The GNU __null extension is always a null pointer constant.
1488 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001489 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001490
Sebastian Redl576fd422009-05-10 18:38:11 +00001491 // C++0x nullptr_t is always a null pointer constant.
1492 if (getType()->isNullPtrType())
1493 return true;
1494
Steve Naroff4871fe02008-01-14 16:10:57 +00001495 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001496 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001497 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001498 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001499
Chris Lattner1abbd412007-06-08 17:58:43 +00001500 // If we have an integer constant expression, we need to *evaluate* it and
1501 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001502 llvm::APSInt Result;
1503 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001504}
Steve Narofff7a5da12007-07-28 23:10:27 +00001505
Douglas Gregor71235ec2009-05-02 02:18:30 +00001506FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001507 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001508
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001509 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redlc57d34b2010-07-20 04:20:21 +00001510 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1511 ICE->getCastKind() == CastExpr::CK_NoOp)
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001512 E = ICE->getSubExpr()->IgnoreParens();
1513 else
1514 break;
1515 }
1516
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001517 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001518 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001519 if (Field->isBitField())
1520 return Field;
1521
1522 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1523 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1524 return BinOp->getLHS()->getBitField();
1525
1526 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001527}
1528
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001529bool Expr::refersToVectorElement() const {
1530 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001531
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001532 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redlc57d34b2010-07-20 04:20:21 +00001533 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1534 ICE->getCastKind() == CastExpr::CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001535 E = ICE->getSubExpr()->IgnoreParens();
1536 else
1537 break;
1538 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001539
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001540 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1541 return ASE->getBase()->getType()->isVectorType();
1542
1543 if (isa<ExtVectorElementExpr>(E))
1544 return true;
1545
1546 return false;
1547}
1548
Chris Lattnerb8211f62009-02-16 22:14:05 +00001549/// isArrow - Return true if the base expression is a pointer to vector,
1550/// return false if the base expression is a vector.
1551bool ExtVectorElementExpr::isArrow() const {
1552 return getBase()->getType()->isPointerType();
1553}
1554
Nate Begemance4d7fc2008-04-18 23:10:10 +00001555unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001556 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001557 return VT->getNumElements();
1558 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001559}
1560
Nate Begemanf322eab2008-05-09 06:41:27 +00001561/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001562bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001563 // FIXME: Refactor this code to an accessor on the AST node which returns the
1564 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001565 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001566
1567 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001568 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001569 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001570
Nate Begeman7e5185b2009-01-18 02:01:21 +00001571 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001572 if (Comp[0] == 's' || Comp[0] == 'S')
1573 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001574
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001575 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1576 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001577 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001578
Steve Naroff0d595ca2007-07-30 03:29:09 +00001579 return false;
1580}
Chris Lattner885b4952007-08-02 23:36:59 +00001581
Nate Begemanf322eab2008-05-09 06:41:27 +00001582/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001583void ExtVectorElementExpr::getEncodedElementAccess(
1584 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001585 llvm::StringRef Comp = Accessor->getName();
1586 if (Comp[0] == 's' || Comp[0] == 'S')
1587 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001588
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001589 bool isHi = Comp == "hi";
1590 bool isLo = Comp == "lo";
1591 bool isEven = Comp == "even";
1592 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001593
Nate Begemanf322eab2008-05-09 06:41:27 +00001594 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1595 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001596
Nate Begemanf322eab2008-05-09 06:41:27 +00001597 if (isHi)
1598 Index = e + i;
1599 else if (isLo)
1600 Index = i;
1601 else if (isEven)
1602 Index = 2 * i;
1603 else if (isOdd)
1604 Index = 2 * i + 1;
1605 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001606 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001607
Nate Begemand3862152008-05-13 21:03:02 +00001608 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001609 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001610}
1611
Douglas Gregor9a129192010-04-21 00:45:42 +00001612ObjCMessageExpr::ObjCMessageExpr(QualType T,
1613 SourceLocation LBracLoc,
1614 SourceLocation SuperLoc,
1615 bool IsInstanceSuper,
1616 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001617 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001618 ObjCMethodDecl *Method,
1619 Expr **Args, unsigned NumArgs,
1620 SourceLocation RBracLoc)
1621 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001622 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001623 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1624 HasMethod(Method != 0), SuperLoc(SuperLoc),
1625 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1626 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001627 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001628{
Douglas Gregor9a129192010-04-21 00:45:42 +00001629 setReceiverPointer(SuperType.getAsOpaquePtr());
1630 if (NumArgs)
1631 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001632}
1633
Douglas Gregor9a129192010-04-21 00:45:42 +00001634ObjCMessageExpr::ObjCMessageExpr(QualType T,
1635 SourceLocation LBracLoc,
1636 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001637 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001638 ObjCMethodDecl *Method,
1639 Expr **Args, unsigned NumArgs,
1640 SourceLocation RBracLoc)
1641 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001642 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001643 hasAnyValueDependentArguments(Args, NumArgs))),
1644 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1645 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1646 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001647 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001648{
1649 setReceiverPointer(Receiver);
1650 if (NumArgs)
1651 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001652}
1653
Douglas Gregor9a129192010-04-21 00:45:42 +00001654ObjCMessageExpr::ObjCMessageExpr(QualType T,
1655 SourceLocation LBracLoc,
1656 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001657 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001658 ObjCMethodDecl *Method,
1659 Expr **Args, unsigned NumArgs,
1660 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001661 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001662 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001663 hasAnyValueDependentArguments(Args, NumArgs))),
1664 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1665 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1666 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001667 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001668{
1669 setReceiverPointer(Receiver);
1670 if (NumArgs)
1671 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001672}
1673
Douglas Gregor9a129192010-04-21 00:45:42 +00001674ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1675 SourceLocation LBracLoc,
1676 SourceLocation SuperLoc,
1677 bool IsInstanceSuper,
1678 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001679 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001680 ObjCMethodDecl *Method,
1681 Expr **Args, unsigned NumArgs,
1682 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001683 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001684 NumArgs * sizeof(Expr *);
1685 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1686 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001687 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00001688 RBracLoc);
1689}
1690
1691ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1692 SourceLocation LBracLoc,
1693 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001694 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001695 ObjCMethodDecl *Method,
1696 Expr **Args, unsigned NumArgs,
1697 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001698 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001699 NumArgs * sizeof(Expr *);
1700 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001701 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001702 NumArgs, RBracLoc);
1703}
1704
1705ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1706 SourceLocation LBracLoc,
1707 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001708 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001709 ObjCMethodDecl *Method,
1710 Expr **Args, unsigned NumArgs,
1711 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001712 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001713 NumArgs * sizeof(Expr *);
1714 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001715 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001716 NumArgs, RBracLoc);
1717}
1718
Alexis Hunta8136cc2010-05-05 15:23:54 +00001719ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00001720 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001721 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001722 NumArgs * sizeof(Expr *);
1723 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1724 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1725}
Alexis Hunta8136cc2010-05-05 15:23:54 +00001726
Douglas Gregor9a129192010-04-21 00:45:42 +00001727Selector ObjCMessageExpr::getSelector() const {
1728 if (HasMethod)
1729 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1730 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001731 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00001732}
1733
1734ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1735 switch (getReceiverKind()) {
1736 case Instance:
1737 if (const ObjCObjectPointerType *Ptr
1738 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1739 return Ptr->getInterfaceDecl();
1740 break;
1741
1742 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00001743 if (const ObjCObjectType *Ty
1744 = getClassReceiver()->getAs<ObjCObjectType>())
1745 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00001746 break;
1747
1748 case SuperInstance:
1749 if (const ObjCObjectPointerType *Ptr
1750 = getSuperType()->getAs<ObjCObjectPointerType>())
1751 return Ptr->getInterfaceDecl();
1752 break;
1753
1754 case SuperClass:
1755 if (const ObjCObjectPointerType *Iface
1756 = getSuperType()->getAs<ObjCObjectPointerType>())
1757 return Iface->getInterfaceDecl();
1758 break;
1759 }
1760
1761 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00001762}
Chris Lattner7ec71da2009-04-26 00:44:05 +00001763
Chris Lattner35e564e2007-10-25 00:29:32 +00001764bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001765 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001766}
1767
Nate Begeman48745922009-08-12 02:28:50 +00001768void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1769 unsigned NumExprs) {
1770 if (SubExprs) C.Deallocate(SubExprs);
1771
1772 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001773 this->NumExprs = NumExprs;
1774 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001775}
Nate Begeman48745922009-08-12 02:28:50 +00001776
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001777//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001778// DesignatedInitExpr
1779//===----------------------------------------------------------------------===//
1780
1781IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1782 assert(Kind == FieldDesignator && "Only valid on a field designator");
1783 if (Field.NameOrField & 0x01)
1784 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1785 else
1786 return getField()->getIdentifier();
1787}
1788
Alexis Hunta8136cc2010-05-05 15:23:54 +00001789DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001790 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001791 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001792 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001793 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001794 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001795 unsigned NumIndexExprs,
1796 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001797 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001798 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001799 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1800 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001801 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001802
1803 // Record the initializer itself.
1804 child_iterator Child = child_begin();
1805 *Child++ = Init;
1806
1807 // Copy the designators and their subexpressions, computing
1808 // value-dependence along the way.
1809 unsigned IndexIdx = 0;
1810 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001811 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001812
1813 if (this->Designators[I].isArrayDesignator()) {
1814 // Compute type- and value-dependence.
1815 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001816 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001817 Index->isTypeDependent() || Index->isValueDependent();
1818
1819 // Copy the index expressions into permanent storage.
1820 *Child++ = IndexExprs[IndexIdx++];
1821 } else if (this->Designators[I].isArrayRangeDesignator()) {
1822 // Compute type- and value-dependence.
1823 Expr *Start = IndexExprs[IndexIdx];
1824 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001825 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001826 Start->isTypeDependent() || Start->isValueDependent() ||
1827 End->isTypeDependent() || End->isValueDependent();
1828
1829 // Copy the start/end expressions into permanent storage.
1830 *Child++ = IndexExprs[IndexIdx++];
1831 *Child++ = IndexExprs[IndexIdx++];
1832 }
1833 }
1834
1835 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001836}
1837
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001838DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001839DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001840 unsigned NumDesignators,
1841 Expr **IndexExprs, unsigned NumIndexExprs,
1842 SourceLocation ColonOrEqualLoc,
1843 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001844 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001845 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001846 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001847 ColonOrEqualLoc, UsesColonSyntax,
1848 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001849}
1850
Mike Stump11289f42009-09-09 15:08:12 +00001851DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001852 unsigned NumIndexExprs) {
1853 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1854 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1855 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1856}
1857
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001858void DesignatedInitExpr::setDesignators(ASTContext &C,
1859 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001860 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001861 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00001862 NumDesignators = NumDesigs;
1863 for (unsigned I = 0; I != NumDesigs; ++I)
1864 Designators[I] = Desigs[I];
1865}
1866
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001867SourceRange DesignatedInitExpr::getSourceRange() const {
1868 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00001869 Designator &First =
1870 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001871 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001872 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001873 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1874 else
1875 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1876 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00001877 StartLoc =
1878 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001879 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1880}
1881
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001882Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1883 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1884 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1885 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001886 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1887 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1888}
1889
1890Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001891 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001892 "Requires array range designator");
1893 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1894 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001895 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1896 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1897}
1898
1899Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001900 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001901 "Requires array range designator");
1902 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1903 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001904 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1905 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1906}
1907
Douglas Gregord5846a12009-04-15 06:41:24 +00001908/// \brief Replaces the designator at index @p Idx with the series
1909/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001910void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00001911 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00001912 const Designator *Last) {
1913 unsigned NumNewDesignators = Last - First;
1914 if (NumNewDesignators == 0) {
1915 std::copy_backward(Designators + Idx + 1,
1916 Designators + NumDesignators,
1917 Designators + Idx);
1918 --NumNewDesignators;
1919 return;
1920 } else if (NumNewDesignators == 1) {
1921 Designators[Idx] = *First;
1922 return;
1923 }
1924
Mike Stump11289f42009-09-09 15:08:12 +00001925 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001926 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00001927 std::copy(Designators, Designators + Idx, NewDesignators);
1928 std::copy(First, Last, NewDesignators + Idx);
1929 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1930 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00001931 Designators = NewDesignators;
1932 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1933}
1934
Mike Stump11289f42009-09-09 15:08:12 +00001935ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001936 Expr **exprs, unsigned nexprs,
1937 SourceLocation rparenloc)
1938: Expr(ParenListExprClass, QualType(),
1939 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00001940 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00001941 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00001942
Nate Begeman5ec4b312009-08-10 23:49:36 +00001943 Exprs = new (C) Stmt*[nexprs];
1944 for (unsigned i = 0; i != nexprs; ++i)
1945 Exprs[i] = exprs[i];
1946}
1947
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001948//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00001949// ExprIterator.
1950//===----------------------------------------------------------------------===//
1951
1952Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1953Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1954Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1955const Expr* ConstExprIterator::operator[](size_t idx) const {
1956 return cast<Expr>(I[idx]);
1957}
1958const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
1959const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
1960
1961//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001962// Child Iterators for iterating over subexpressions/substatements
1963//===----------------------------------------------------------------------===//
1964
1965// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00001966Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1967Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001968
Steve Naroffe46504b2007-11-12 14:29:37 +00001969// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001970Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1971Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00001972
Steve Naroffebf4cb42008-06-02 23:03:37 +00001973// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00001974Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1975Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00001976
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001977// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00001978Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00001979 // If this is accessing a class member, skip that entry.
1980 if (Base) return &Base;
1981 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001982}
Mike Stump11289f42009-09-09 15:08:12 +00001983Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
1984 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001985}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00001986
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001987// ObjCSuperExpr
1988Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
1989Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
1990
Steve Naroffe87026a2009-07-24 17:54:45 +00001991// ObjCIsaExpr
1992Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
1993Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
1994
Chris Lattner6307f192008-08-10 01:53:14 +00001995// PredefinedExpr
1996Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1997Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001998
1999// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002000Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2001Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002002
2003// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002004Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002005Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002006
2007// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002008Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2009Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002010
Chris Lattner1c20a172007-08-26 03:42:43 +00002011// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002012Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2013Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002014
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002015// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002016Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2017Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002018
2019// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002020Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2021Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002022
2023// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002024Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2025Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002026
Douglas Gregor882211c2010-04-28 22:16:22 +00002027// OffsetOfExpr
2028Stmt::child_iterator OffsetOfExpr::child_begin() {
2029 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2030 + NumComps);
2031}
2032Stmt::child_iterator OffsetOfExpr::child_end() {
2033 return child_iterator(&*child_begin() + NumExprs);
2034}
2035
Sebastian Redl6f282892008-11-11 17:56:53 +00002036// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002037Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002038 // If this is of a type and the type is a VLA type (and not a typedef), the
2039 // size expression of the VLA needs to be treated as an executable expression.
2040 // Why isn't this weirdness documented better in StmtIterator?
2041 if (isArgumentType()) {
2042 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2043 getArgumentType().getTypePtr()))
2044 return child_iterator(T);
2045 return child_iterator();
2046 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002047 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002048}
Sebastian Redl6f282892008-11-11 17:56:53 +00002049Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2050 if (isArgumentType())
2051 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002052 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002053}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002054
2055// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002056Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002057 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002058}
Ted Kremenek23702b62007-08-24 20:06:47 +00002059Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002060 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002061}
2062
2063// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002064Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002065 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002066}
Ted Kremenek23702b62007-08-24 20:06:47 +00002067Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002068 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002069}
Ted Kremenek23702b62007-08-24 20:06:47 +00002070
2071// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002072Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2073Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002074
Nate Begemance4d7fc2008-04-18 23:10:10 +00002075// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002076Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2077Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002078
2079// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002080Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2081Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002082
Ted Kremenek23702b62007-08-24 20:06:47 +00002083// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002084Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2085Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002086
2087// BinaryOperator
2088Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002089 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002090}
Ted Kremenek23702b62007-08-24 20:06:47 +00002091Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002092 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002093}
2094
2095// ConditionalOperator
2096Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002097 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002098}
Ted Kremenek23702b62007-08-24 20:06:47 +00002099Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002100 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002101}
2102
2103// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002104Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2105Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002106
Ted Kremenek23702b62007-08-24 20:06:47 +00002107// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002108Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2109Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002110
2111// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002112Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2113 return child_iterator();
2114}
2115
2116Stmt::child_iterator TypesCompatibleExpr::child_end() {
2117 return child_iterator();
2118}
Ted Kremenek23702b62007-08-24 20:06:47 +00002119
2120// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002121Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2122Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002123
Douglas Gregor3be4b122008-11-29 04:51:27 +00002124// GNUNullExpr
2125Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2126Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2127
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002128// ShuffleVectorExpr
2129Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002130 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002131}
2132Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002133 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002134}
2135
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002136// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002137Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2138Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002139
Anders Carlsson4692db02007-08-31 04:56:16 +00002140// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002141Stmt::child_iterator InitListExpr::child_begin() {
2142 return InitExprs.size() ? &InitExprs[0] : 0;
2143}
2144Stmt::child_iterator InitListExpr::child_end() {
2145 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2146}
Anders Carlsson4692db02007-08-31 04:56:16 +00002147
Douglas Gregor0202cb42009-01-29 17:44:32 +00002148// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002149Stmt::child_iterator DesignatedInitExpr::child_begin() {
2150 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2151 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002152 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2153}
2154Stmt::child_iterator DesignatedInitExpr::child_end() {
2155 return child_iterator(&*child_begin() + NumSubExprs);
2156}
2157
Douglas Gregor0202cb42009-01-29 17:44:32 +00002158// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002159Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2160 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002161}
2162
Mike Stump11289f42009-09-09 15:08:12 +00002163Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2164 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002165}
2166
Nate Begeman5ec4b312009-08-10 23:49:36 +00002167// ParenListExpr
2168Stmt::child_iterator ParenListExpr::child_begin() {
2169 return &Exprs[0];
2170}
2171Stmt::child_iterator ParenListExpr::child_end() {
2172 return &Exprs[0]+NumExprs;
2173}
2174
Ted Kremenek23702b62007-08-24 20:06:47 +00002175// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002176Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002177 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002178}
2179Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002180 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002181}
Ted Kremenek23702b62007-08-24 20:06:47 +00002182
2183// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002184Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2185Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002186
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002187// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002188Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002189 return child_iterator();
2190}
2191Stmt::child_iterator ObjCSelectorExpr::child_end() {
2192 return child_iterator();
2193}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002194
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002195// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002196Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2197 return child_iterator();
2198}
2199Stmt::child_iterator ObjCProtocolExpr::child_end() {
2200 return child_iterator();
2201}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002202
Steve Naroffd54978b2007-09-18 23:55:05 +00002203// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002204Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002205 if (getReceiverKind() == Instance)
2206 return reinterpret_cast<Stmt **>(this + 1);
2207 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002208}
2209Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002210 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002211}
2212
Steve Naroffc540d662008-09-03 18:15:37 +00002213// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002214Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2215Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002216
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002217Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2218Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }