blob: 6524a312dc8b8a1ba7344c759a05ad9ca54c0915 [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
Douglas Gregore26a2852009-08-07 06:08:38 +0000393void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000394 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000395 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000396}
397
Daniel Dunbar36217882009-09-22 03:27:33 +0000398void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000399 if (StrData)
400 C.Deallocate(const_cast<char*>(StrData));
401
Daniel Dunbar36217882009-09-22 03:27:33 +0000402 char *AStrData = new (C, 1) char[Str.size()];
403 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000404 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000405 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000406}
407
Chris Lattner1b926492006-08-23 06:42:10 +0000408/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
409/// corresponds to, e.g. "sizeof" or "[pre]++".
410const char *UnaryOperator::getOpcodeStr(Opcode Op) {
411 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000412 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000413 case PostInc: return "++";
414 case PostDec: return "--";
415 case PreInc: return "++";
416 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000417 case AddrOf: return "&";
418 case Deref: return "*";
419 case Plus: return "+";
420 case Minus: return "-";
421 case Not: return "~";
422 case LNot: return "!";
423 case Real: return "__real";
424 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000425 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000426 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000427 }
428}
429
Mike Stump11289f42009-09-09 15:08:12 +0000430UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000431UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
432 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000433 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000434 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
435 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
436 case OO_Amp: return AddrOf;
437 case OO_Star: return Deref;
438 case OO_Plus: return Plus;
439 case OO_Minus: return Minus;
440 case OO_Tilde: return Not;
441 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000442 }
443}
444
445OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
446 switch (Opc) {
447 case PostInc: case PreInc: return OO_PlusPlus;
448 case PostDec: case PreDec: return OO_MinusMinus;
449 case AddrOf: return OO_Amp;
450 case Deref: return OO_Star;
451 case Plus: return OO_Plus;
452 case Minus: return OO_Minus;
453 case Not: return OO_Tilde;
454 case LNot: return OO_Exclaim;
455 default: return OO_None;
456 }
457}
458
459
Chris Lattner0eedafe2006-08-24 04:56:27 +0000460//===----------------------------------------------------------------------===//
461// Postfix Operators.
462//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000463
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000464CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000465 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000466 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000467 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000468 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000469 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000470
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000471 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000472 SubExprs[FN] = fn;
473 for (unsigned i = 0; i != numargs; ++i)
474 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000475
Douglas Gregor993603d2008-11-14 16:09:21 +0000476 RParenLoc = rparenloc;
477}
Nate Begeman1e36a852008-01-17 17:46:27 +0000478
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000479CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
480 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000481 : Expr(CallExprClass, t,
482 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000483 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000484 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000485
486 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000487 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000488 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000489 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000490
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000491 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000492}
493
Mike Stump11289f42009-09-09 15:08:12 +0000494CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
495 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000496 SubExprs = new (C) Stmt*[1];
497}
498
Douglas Gregore26a2852009-08-07 06:08:38 +0000499void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000500 DestroyChildren(C);
501 if (SubExprs) C.Deallocate(SubExprs);
502 this->~CallExpr();
503 C.Deallocate(this);
504}
505
Nuno Lopes518e3702009-12-20 23:11:08 +0000506Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000507 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000508 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000509 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000510 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
511 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000512
513 return 0;
514}
515
Nuno Lopes518e3702009-12-20 23:11:08 +0000516FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000517 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000518}
519
Chris Lattnere4407ed2007-12-28 05:25:02 +0000520/// setNumArgs - This changes the number of arguments present in this call.
521/// Any orphaned expressions are deleted by this, and any new operands are set
522/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000523void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000524 // No change, just return.
525 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattnere4407ed2007-12-28 05:25:02 +0000527 // If shrinking # arguments, just delete the extras and forgot them.
528 if (NumArgs < getNumArgs()) {
529 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000530 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000531 this->NumArgs = NumArgs;
532 return;
533 }
534
535 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000536 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000537 // Copy over args.
538 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
539 NewSubExprs[i] = SubExprs[i];
540 // Null out new args.
541 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
542 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Douglas Gregorba6e5572009-04-17 21:46:47 +0000544 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000545 SubExprs = NewSubExprs;
546 this->NumArgs = NumArgs;
547}
548
Chris Lattner01ff98a2008-10-06 05:00:53 +0000549/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
550/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000551unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000552 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000553 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000554 // ImplicitCastExpr.
555 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
556 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000557 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Steve Narofff6e3b3292008-01-31 01:07:12 +0000559 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
560 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000561 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000562
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000563 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
564 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000565 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000566
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000567 if (!FDecl->getIdentifier())
568 return 0;
569
Douglas Gregor15fc9562009-09-12 00:22:50 +0000570 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000571}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000572
Anders Carlsson00a27592009-05-26 04:57:27 +0000573QualType CallExpr::getCallReturnType() const {
574 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000575 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000576 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000577 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000578 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000579 else if (const MemberPointerType *MPT
580 = CalleeType->getAs<MemberPointerType>())
581 CalleeType = MPT->getPointeeType();
582
John McCall9dd450b2009-09-21 23:43:11 +0000583 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000584 return FnType->getResultType();
585}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000586
Alexis Hunta8136cc2010-05-05 15:23:54 +0000587OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000588 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000589 TypeSourceInfo *tsi,
590 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000591 Expr** exprsPtr, unsigned numExprs,
592 SourceLocation RParenLoc) {
593 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000594 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000595 sizeof(Expr*) * numExprs);
596
597 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
598 exprsPtr, numExprs, RParenLoc);
599}
600
601OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
602 unsigned numComps, unsigned numExprs) {
603 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
604 sizeof(OffsetOfNode) * numComps +
605 sizeof(Expr*) * numExprs);
606 return new (Mem) OffsetOfExpr(numComps, numExprs);
607}
608
Alexis Hunta8136cc2010-05-05 15:23:54 +0000609OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000610 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000611 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000612 Expr** exprsPtr, unsigned numExprs,
613 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000614 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000615 /*ValueDependent=*/tsi->getType()->isDependentType() ||
616 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
617 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000618 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
619 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000620{
621 for(unsigned i = 0; i < numComps; ++i) {
622 setComponent(i, compsPtr[i]);
623 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000624
Douglas Gregor882211c2010-04-28 22:16:22 +0000625 for(unsigned i = 0; i < numExprs; ++i) {
626 setIndexExpr(i, exprsPtr[i]);
627 }
628}
629
630IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
631 assert(getKind() == Field || getKind() == Identifier);
632 if (getKind() == Field)
633 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000634
Douglas Gregor882211c2010-04-28 22:16:22 +0000635 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
636}
637
Mike Stump11289f42009-09-09 15:08:12 +0000638MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
639 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000640 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000641 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000642 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000643 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000644 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000645 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000646 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000647
John McCalla8ae2222010-04-06 21:38:20 +0000648 bool hasQualOrFound = (qual != 0 ||
649 founddecl.getDecl() != memberdecl ||
650 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000651 if (hasQualOrFound)
652 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000653
John McCall6b51f282009-11-23 01:53:49 +0000654 if (targs)
655 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000657 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000658 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
659
660 if (hasQualOrFound) {
661 if (qual && qual->isDependent()) {
662 E->setValueDependent(true);
663 E->setTypeDependent(true);
664 }
665 E->HasQualifierOrFoundDecl = true;
666
667 MemberNameQualifier *NQ = E->getMemberQualifier();
668 NQ->NNS = qual;
669 NQ->Range = qualrange;
670 NQ->FoundDecl = founddecl;
671 }
672
673 if (targs) {
674 E->HasExplicitTemplateArgumentList = true;
675 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
676 }
677
678 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000679}
680
Anders Carlsson496335e2009-09-03 00:59:21 +0000681const char *CastExpr::getCastKindName() const {
682 switch (getCastKind()) {
683 case CastExpr::CK_Unknown:
684 return "Unknown";
685 case CastExpr::CK_BitCast:
686 return "BitCast";
Douglas Gregor51954272010-07-13 23:17:26 +0000687 case CastExpr::CK_LValueBitCast:
688 return "LValueBitCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000689 case CastExpr::CK_NoOp:
690 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000691 case CastExpr::CK_BaseToDerived:
692 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000693 case CastExpr::CK_DerivedToBase:
694 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000695 case CastExpr::CK_UncheckedDerivedToBase:
696 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000697 case CastExpr::CK_Dynamic:
698 return "Dynamic";
699 case CastExpr::CK_ToUnion:
700 return "ToUnion";
701 case CastExpr::CK_ArrayToPointerDecay:
702 return "ArrayToPointerDecay";
703 case CastExpr::CK_FunctionToPointerDecay:
704 return "FunctionToPointerDecay";
705 case CastExpr::CK_NullToMemberPointer:
706 return "NullToMemberPointer";
707 case CastExpr::CK_BaseToDerivedMemberPointer:
708 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000709 case CastExpr::CK_DerivedToBaseMemberPointer:
710 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000711 case CastExpr::CK_UserDefinedConversion:
712 return "UserDefinedConversion";
713 case CastExpr::CK_ConstructorConversion:
714 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000715 case CastExpr::CK_IntegralToPointer:
716 return "IntegralToPointer";
717 case CastExpr::CK_PointerToIntegral:
718 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000719 case CastExpr::CK_ToVoid:
720 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000721 case CastExpr::CK_VectorSplat:
722 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000723 case CastExpr::CK_IntegralCast:
724 return "IntegralCast";
725 case CastExpr::CK_IntegralToFloating:
726 return "IntegralToFloating";
727 case CastExpr::CK_FloatingToIntegral:
728 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000729 case CastExpr::CK_FloatingCast:
730 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000731 case CastExpr::CK_MemberPointerToBoolean:
732 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000733 case CastExpr::CK_AnyPointerToObjCPointerCast:
734 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000735 case CastExpr::CK_AnyPointerToBlockPointerCast:
736 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Anders Carlsson496335e2009-09-03 00:59:21 +0000739 assert(0 && "Unhandled cast kind!");
740 return 0;
741}
742
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000743void CastExpr::DoDestroy(ASTContext &C)
744{
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000745 BasePath.Destroy();
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000746 Expr::DoDestroy(C);
747}
748
Douglas Gregord196a582009-12-14 19:27:10 +0000749Expr *CastExpr::getSubExprAsWritten() {
750 Expr *SubExpr = 0;
751 CastExpr *E = this;
752 do {
753 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000754
Douglas Gregord196a582009-12-14 19:27:10 +0000755 // Skip any temporary bindings; they're implicit.
756 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
757 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000758
Douglas Gregord196a582009-12-14 19:27:10 +0000759 // Conversions by constructor and conversion functions have a
760 // subexpression describing the call; strip it off.
761 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
762 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
763 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
764 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000765
Douglas Gregord196a582009-12-14 19:27:10 +0000766 // If the subexpression we're left with is an implicit cast, look
767 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000768 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
769
Douglas Gregord196a582009-12-14 19:27:10 +0000770 return SubExpr;
771}
772
Chris Lattner1b926492006-08-23 06:42:10 +0000773/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
774/// corresponds to, e.g. "<<=".
775const char *BinaryOperator::getOpcodeStr(Opcode Op) {
776 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000777 case PtrMemD: return ".*";
778 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000779 case Mul: return "*";
780 case Div: return "/";
781 case Rem: return "%";
782 case Add: return "+";
783 case Sub: return "-";
784 case Shl: return "<<";
785 case Shr: return ">>";
786 case LT: return "<";
787 case GT: return ">";
788 case LE: return "<=";
789 case GE: return ">=";
790 case EQ: return "==";
791 case NE: return "!=";
792 case And: return "&";
793 case Xor: return "^";
794 case Or: return "|";
795 case LAnd: return "&&";
796 case LOr: return "||";
797 case Assign: return "=";
798 case MulAssign: return "*=";
799 case DivAssign: return "/=";
800 case RemAssign: return "%=";
801 case AddAssign: return "+=";
802 case SubAssign: return "-=";
803 case ShlAssign: return "<<=";
804 case ShrAssign: return ">>=";
805 case AndAssign: return "&=";
806 case XorAssign: return "^=";
807 case OrAssign: return "|=";
808 case Comma: return ",";
809 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000810
811 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000812}
Steve Naroff47500512007-04-19 23:00:49 +0000813
Mike Stump11289f42009-09-09 15:08:12 +0000814BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000815BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
816 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000817 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000818 case OO_Plus: return Add;
819 case OO_Minus: return Sub;
820 case OO_Star: return Mul;
821 case OO_Slash: return Div;
822 case OO_Percent: return Rem;
823 case OO_Caret: return Xor;
824 case OO_Amp: return And;
825 case OO_Pipe: return Or;
826 case OO_Equal: return Assign;
827 case OO_Less: return LT;
828 case OO_Greater: return GT;
829 case OO_PlusEqual: return AddAssign;
830 case OO_MinusEqual: return SubAssign;
831 case OO_StarEqual: return MulAssign;
832 case OO_SlashEqual: return DivAssign;
833 case OO_PercentEqual: return RemAssign;
834 case OO_CaretEqual: return XorAssign;
835 case OO_AmpEqual: return AndAssign;
836 case OO_PipeEqual: return OrAssign;
837 case OO_LessLess: return Shl;
838 case OO_GreaterGreater: return Shr;
839 case OO_LessLessEqual: return ShlAssign;
840 case OO_GreaterGreaterEqual: return ShrAssign;
841 case OO_EqualEqual: return EQ;
842 case OO_ExclaimEqual: return NE;
843 case OO_LessEqual: return LE;
844 case OO_GreaterEqual: return GE;
845 case OO_AmpAmp: return LAnd;
846 case OO_PipePipe: return LOr;
847 case OO_Comma: return Comma;
848 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000849 }
850}
851
852OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
853 static const OverloadedOperatorKind OverOps[] = {
854 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
855 OO_Star, OO_Slash, OO_Percent,
856 OO_Plus, OO_Minus,
857 OO_LessLess, OO_GreaterGreater,
858 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
859 OO_EqualEqual, OO_ExclaimEqual,
860 OO_Amp,
861 OO_Caret,
862 OO_Pipe,
863 OO_AmpAmp,
864 OO_PipePipe,
865 OO_Equal, OO_StarEqual,
866 OO_SlashEqual, OO_PercentEqual,
867 OO_PlusEqual, OO_MinusEqual,
868 OO_LessLessEqual, OO_GreaterGreaterEqual,
869 OO_AmpEqual, OO_CaretEqual,
870 OO_PipeEqual,
871 OO_Comma
872 };
873 return OverOps[Opc];
874}
875
Ted Kremenekac034612010-04-13 23:39:13 +0000876InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000877 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000878 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000879 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000880 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000881 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000882 UnionFieldInit(0), HadArrayRangeDesignator(false)
883{
Ted Kremenek013041e2010-02-19 01:50:18 +0000884 for (unsigned I = 0; I != numInits; ++I) {
885 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000886 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000887 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000888 ValueDependent = true;
889 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000890
Ted Kremenekac034612010-04-13 23:39:13 +0000891 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000892}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000893
Ted Kremenekac034612010-04-13 23:39:13 +0000894void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000895 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000896 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000897}
898
Ted Kremenekac034612010-04-13 23:39:13 +0000899void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000900 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
901 Idx < LastIdx; ++Idx)
Ted Kremenekac034612010-04-13 23:39:13 +0000902 InitExprs[Idx]->Destroy(C);
903 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000904}
905
Ted Kremenekac034612010-04-13 23:39:13 +0000906Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000907 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000908 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000909 InitExprs.back() = expr;
910 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000911 }
Mike Stump11289f42009-09-09 15:08:12 +0000912
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000913 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
914 InitExprs[Init] = expr;
915 return Result;
916}
917
Steve Naroff991e99d2008-09-04 15:31:07 +0000918/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000919///
920const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000921 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000922 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000923}
924
Mike Stump11289f42009-09-09 15:08:12 +0000925SourceLocation BlockExpr::getCaretLocation() const {
926 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000927}
Mike Stump11289f42009-09-09 15:08:12 +0000928const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000929 return TheBlock->getBody();
930}
Mike Stump11289f42009-09-09 15:08:12 +0000931Stmt *BlockExpr::getBody() {
932 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000933}
Steve Naroff415d3d52008-10-08 17:01:13 +0000934
935
Chris Lattner1ec5f562007-06-27 05:38:08 +0000936//===----------------------------------------------------------------------===//
937// Generic Expression Routines
938//===----------------------------------------------------------------------===//
939
Chris Lattner237f2752009-02-14 07:37:35 +0000940/// isUnusedResultAWarning - Return true if this immediate expression should
941/// be warned about if the result is unused. If so, fill in Loc and Ranges
942/// with location to warn on and the source range[s] to report with the
943/// warning.
944bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000945 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000946 // Don't warn if the expr is type dependent. The type could end up
947 // instantiating to void.
948 if (isTypeDependent())
949 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner1ec5f562007-06-27 05:38:08 +0000951 switch (getStmtClass()) {
952 default:
John McCallc493a732010-03-12 07:11:26 +0000953 if (getType()->isVoidType())
954 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000955 Loc = getExprLoc();
956 R1 = getSourceRange();
957 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000958 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000959 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000960 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000961 case UnaryOperatorClass: {
962 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattner1ec5f562007-06-27 05:38:08 +0000964 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000965 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000966 case UnaryOperator::PostInc:
967 case UnaryOperator::PostDec:
968 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000969 case UnaryOperator::PreDec: // ++/--
970 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000971 case UnaryOperator::Deref:
972 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000973 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000974 return false;
975 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000976 case UnaryOperator::Real:
977 case UnaryOperator::Imag:
978 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000979 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
980 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000981 return false;
982 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000983 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000984 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000985 }
Chris Lattner237f2752009-02-14 07:37:35 +0000986 Loc = UO->getOperatorLoc();
987 R1 = UO->getSubExpr()->getSourceRange();
988 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000989 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000990 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000991 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000992 switch (BO->getOpcode()) {
993 default:
994 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +0000995 // Consider the RHS of comma for side effects. LHS was checked by
996 // Sema::CheckCommaOperands.
Ted Kremenek43a9c962010-04-07 18:49:21 +0000997 case BinaryOperator::Comma:
998 // ((foo = <blah>), 0) is an idiom for hiding the result (and
999 // lvalue-ness) of an assignment written in a macro.
1000 if (IntegerLiteral *IE =
1001 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1002 if (IE->getValue() == 0)
1003 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001004 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1005 // Consider '||', '&&' to have side effects if the LHS or RHS does.
Ted Kremenek43a9c962010-04-07 18:49:21 +00001006 case BinaryOperator::LAnd:
1007 case BinaryOperator::LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001008 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1009 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1010 return false;
1011 break;
John McCall1e3715a2010-02-16 04:10:53 +00001012 }
Chris Lattner237f2752009-02-14 07:37:35 +00001013 if (BO->isAssignmentOp())
1014 return false;
1015 Loc = BO->getOperatorLoc();
1016 R1 = BO->getLHS()->getSourceRange();
1017 R2 = BO->getRHS()->getSourceRange();
1018 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001019 }
Chris Lattner86928112007-08-25 02:00:02 +00001020 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001021 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001022 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001023
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001024 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001025 // The condition must be evaluated, but if either the LHS or RHS is a
1026 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001027 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001028 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001029 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001030 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001031 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001032 }
1033
Chris Lattnera44d1162007-06-27 05:58:59 +00001034 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001035 // If the base pointer or element is to a volatile pointer/field, accessing
1036 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001037 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001038 return false;
1039 Loc = cast<MemberExpr>(this)->getMemberLoc();
1040 R1 = SourceRange(Loc, Loc);
1041 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1042 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattner1ec5f562007-06-27 05:38:08 +00001044 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001045 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001046 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001047 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001048 return false;
1049 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1050 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1051 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1052 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001053
Chris Lattner1ec5f562007-06-27 05:38:08 +00001054 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001055 case CXXOperatorCallExprClass:
1056 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001057 // If this is a direct call, get the callee.
1058 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001059 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001060 // If the callee has attribute pure, const, or warn_unused_result, warn
1061 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001062 //
1063 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1064 // updated to match for QoI.
1065 if (FD->getAttr<WarnUnusedResultAttr>() ||
1066 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1067 Loc = CE->getCallee()->getLocStart();
1068 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001069
Chris Lattner1a6babf2009-10-13 04:53:48 +00001070 if (unsigned NumArgs = CE->getNumArgs())
1071 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1072 CE->getArg(NumArgs-1)->getLocEnd());
1073 return true;
1074 }
Chris Lattner237f2752009-02-14 07:37:35 +00001075 }
1076 return false;
1077 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001078
1079 case CXXTemporaryObjectExprClass:
1080 case CXXConstructExprClass:
1081 return false;
1082
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001083 case ObjCMessageExprClass: {
1084 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1085 const ObjCMethodDecl *MD = ME->getMethodDecl();
1086 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1087 Loc = getExprLoc();
1088 return true;
1089 }
Chris Lattner237f2752009-02-14 07:37:35 +00001090 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001091 }
Mike Stump11289f42009-09-09 15:08:12 +00001092
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001093 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001094#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001095 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001096 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001097 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001098 Loc = Ref->getLocation();
1099 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1100 if (Ref->getBase())
1101 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001102#else
1103 Loc = getExprLoc();
1104 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001105#endif
1106 return true;
1107 }
Chris Lattner944d3062008-07-26 19:51:01 +00001108 case StmtExprClass: {
1109 // Statement exprs don't logically have side effects themselves, but are
1110 // sometimes used in macros in ways that give them a type that is unused.
1111 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1112 // however, if the result of the stmt expr is dead, we don't want to emit a
1113 // warning.
1114 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1115 if (!CS->body_empty())
1116 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001117 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001118
John McCallc493a732010-03-12 07:11:26 +00001119 if (getType()->isVoidType())
1120 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001121 Loc = cast<StmtExpr>(this)->getLParenLoc();
1122 R1 = getSourceRange();
1123 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001124 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001125 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001126 // If this is an explicit cast to void, allow it. People do this when they
1127 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001128 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001129 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001130 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1131 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1132 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001133 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001134 if (getType()->isVoidType())
1135 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001136 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001137
Anders Carlsson6aa50392009-11-17 17:11:23 +00001138 // If this is a cast to void or a constructor conversion, check the operand.
1139 // Otherwise, the result of the cast is unused.
1140 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1141 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001142 return (cast<CastExpr>(this)->getSubExpr()
1143 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001144 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1145 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1146 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001149 case ImplicitCastExprClass:
1150 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001151 return (cast<ImplicitCastExpr>(this)
1152 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001153
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001154 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001155 return (cast<CXXDefaultArgExpr>(this)
1156 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001157
1158 case CXXNewExprClass:
1159 // FIXME: In theory, there might be new expressions that don't have side
1160 // effects (e.g. a placement new with an uninitialized POD).
1161 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001162 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001163 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001164 return (cast<CXXBindTemporaryExpr>(this)
1165 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001166 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001167 return (cast<CXXExprWithTemporaries>(this)
1168 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001169 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001170}
1171
Fariborz Jahanian07735332009-02-22 18:40:18 +00001172/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001173/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001174bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001175 switch (getStmtClass()) {
1176 default:
1177 return false;
1178 case ObjCIvarRefExprClass:
1179 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001180 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001181 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001182 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001183 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001184 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001185 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001186 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001187 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001188 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001189 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001190 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1191 if (VD->hasGlobalStorage())
1192 return true;
1193 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001194 // dereferencing to a pointer is always a gc'able candidate,
1195 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001196 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001197 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001198 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001199 return false;
1200 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001201 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001202 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001203 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001204 }
1205 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001206 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001207 }
1208}
Ted Kremenekfff70962008-01-17 16:57:34 +00001209Expr* Expr::IgnoreParens() {
1210 Expr* E = this;
1211 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1212 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001213
Ted Kremenekfff70962008-01-17 16:57:34 +00001214 return E;
1215}
1216
Chris Lattnerf2660962008-02-13 01:02:39 +00001217/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1218/// or CastExprs or ImplicitCastExprs, returning their operand.
1219Expr *Expr::IgnoreParenCasts() {
1220 Expr *E = this;
1221 while (true) {
1222 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1223 E = P->getSubExpr();
1224 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1225 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001226 else
1227 return E;
1228 }
1229}
1230
John McCalleebc8322010-05-05 22:59:52 +00001231Expr *Expr::IgnoreParenImpCasts() {
1232 Expr *E = this;
1233 while (true) {
1234 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1235 E = P->getSubExpr();
1236 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1237 E = P->getSubExpr();
1238 else
1239 return E;
1240 }
1241}
1242
Chris Lattneref26c772009-03-13 17:28:01 +00001243/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1244/// value (including ptr->int casts of the same size). Strip off any
1245/// ParenExpr or CastExprs, returning their operand.
1246Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1247 Expr *E = this;
1248 while (true) {
1249 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1250 E = P->getSubExpr();
1251 continue;
1252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Chris Lattneref26c772009-03-13 17:28:01 +00001254 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1255 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001256 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001257 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001258
Chris Lattneref26c772009-03-13 17:28:01 +00001259 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1260 E = SE;
1261 continue;
1262 }
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregor6972a622010-06-16 00:35:25 +00001264 if ((E->getType()->isPointerType() ||
1265 E->getType()->isIntegralType(Ctx)) &&
1266 (SE->getType()->isPointerType() ||
1267 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001268 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1269 E = SE;
1270 continue;
1271 }
1272 }
Mike Stump11289f42009-09-09 15:08:12 +00001273
Chris Lattneref26c772009-03-13 17:28:01 +00001274 return E;
1275 }
1276}
1277
Douglas Gregord196a582009-12-14 19:27:10 +00001278bool Expr::isDefaultArgument() const {
1279 const Expr *E = this;
1280 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1281 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001282
Douglas Gregord196a582009-12-14 19:27:10 +00001283 return isa<CXXDefaultArgExpr>(E);
1284}
Chris Lattneref26c772009-03-13 17:28:01 +00001285
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001286/// \brief Skip over any no-op casts and any temporary-binding
1287/// expressions.
1288static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1289 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1290 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1291 E = ICE->getSubExpr();
1292 else
1293 break;
1294 }
1295
1296 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1297 E = BE->getSubExpr();
1298
1299 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1300 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1301 E = ICE->getSubExpr();
1302 else
1303 break;
1304 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001305
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001306 return E;
1307}
1308
1309const Expr *Expr::getTemporaryObject() const {
1310 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1311
1312 // A cast can produce a temporary object. The object's construction
1313 // is represented as a CXXConstructExpr.
1314 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1315 // Only user-defined and constructor conversions can produce
1316 // temporary objects.
1317 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1318 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1319 return 0;
1320
1321 // Strip off temporary bindings and no-op casts.
1322 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1323
1324 // If this is a constructor conversion, see if we have an object
1325 // construction.
1326 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1327 return dyn_cast<CXXConstructExpr>(Sub);
1328
1329 // If this is a user-defined conversion, see if we have a call to
1330 // a function that itself returns a temporary object.
1331 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1332 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1333 if (CE->getCallReturnType()->isRecordType())
1334 return CE;
1335
1336 return 0;
1337 }
1338
1339 // A call returning a class type returns a temporary.
1340 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1341 if (CE->getCallReturnType()->isRecordType())
1342 return CE;
1343
1344 return 0;
1345 }
1346
1347 // Explicit temporary object constructors create temporaries.
1348 return dyn_cast<CXXTemporaryObjectExpr>(E);
1349}
1350
Douglas Gregor4619e432008-12-05 23:32:09 +00001351/// hasAnyTypeDependentArguments - Determines if any of the expressions
1352/// in Exprs is type-dependent.
1353bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1354 for (unsigned I = 0; I < NumExprs; ++I)
1355 if (Exprs[I]->isTypeDependent())
1356 return true;
1357
1358 return false;
1359}
1360
1361/// hasAnyValueDependentArguments - Determines if any of the expressions
1362/// in Exprs is value-dependent.
1363bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1364 for (unsigned I = 0; I < NumExprs; ++I)
1365 if (Exprs[I]->isValueDependent())
1366 return true;
1367
1368 return false;
1369}
1370
Eli Friedman7139af42009-01-25 02:32:41 +00001371bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001372 // This function is attempting whether an expression is an initializer
1373 // which can be evaluated at compile-time. isEvaluatable handles most
1374 // of the cases, but it can't deal with some initializer-specific
1375 // expressions, and it can't deal with aggregates; we deal with those here,
1376 // and fall back to isEvaluatable for the other cases.
1377
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001378 // FIXME: This function assumes the variable being assigned to
1379 // isn't a reference type!
1380
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001381 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001382 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001383 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001384 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001385 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001386 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001387 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001388 // This handles gcc's extension that allows global initializers like
1389 // "struct x {int x;} x = (struct x) {};".
1390 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001391 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001392 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001393 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001394 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001395 // FIXME: This doesn't deal with fields with reference types correctly.
1396 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1397 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001398 const InitListExpr *Exp = cast<InitListExpr>(this);
1399 unsigned numInits = Exp->getNumInits();
1400 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001401 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001402 return false;
1403 }
Eli Friedman384da272009-01-25 03:12:18 +00001404 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001405 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001406 case ImplicitValueInitExprClass:
1407 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001408 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001409 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001410 case UnaryOperatorClass: {
1411 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1412 if (Exp->getOpcode() == UnaryOperator::Extension)
1413 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1414 break;
1415 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001416 case BinaryOperatorClass: {
1417 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1418 // but this handles the common case.
1419 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1420 if (Exp->getOpcode() == BinaryOperator::Sub &&
1421 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1422 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1423 return true;
1424 break;
1425 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001426 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001427 case CStyleCastExprClass:
1428 // Handle casts with a destination that's a struct or union; this
1429 // deals with both the gcc no-op struct cast extension and the
1430 // cast-to-union extension.
1431 if (getType()->isRecordType())
1432 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001433
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001434 // Integer->integer casts can be handled here, which is important for
1435 // things like (int)(&&x-&&y). Scary but true.
1436 if (getType()->isIntegerType() &&
1437 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1438 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001439
Eli Friedman384da272009-01-25 03:12:18 +00001440 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001441 }
Eli Friedman384da272009-01-25 03:12:18 +00001442 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001443}
1444
Chris Lattner7eef9192007-05-24 01:23:49 +00001445/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1446/// integer constant expression with the value zero, or if this is one that is
1447/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001448bool Expr::isNullPointerConstant(ASTContext &Ctx,
1449 NullPointerConstantValueDependence NPC) const {
1450 if (isValueDependent()) {
1451 switch (NPC) {
1452 case NPC_NeverValueDependent:
1453 assert(false && "Unexpected value dependent expression!");
1454 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001455
Douglas Gregor56751b52009-09-25 04:25:58 +00001456 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001457 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001458
Douglas Gregor56751b52009-09-25 04:25:58 +00001459 case NPC_ValueDependentIsNotNull:
1460 return false;
1461 }
1462 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001463
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001464 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001465 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001466 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001467 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001468 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001469 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001470 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001471 Pointee->isVoidType() && // to void*
1472 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001473 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001474 }
Steve Naroffada7d422007-05-20 17:54:12 +00001475 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001476 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1477 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001478 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001479 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1480 // Accept ((void*)0) as a null pointer constant, as many other
1481 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001482 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001483 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001484 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001485 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001486 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001487 } else if (isa<GNUNullExpr>(this)) {
1488 // The GNU __null extension is always a null pointer constant.
1489 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001490 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001491
Sebastian Redl576fd422009-05-10 18:38:11 +00001492 // C++0x nullptr_t is always a null pointer constant.
1493 if (getType()->isNullPtrType())
1494 return true;
1495
Steve Naroff4871fe02008-01-14 16:10:57 +00001496 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001497 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001498 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001499 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001500
Chris Lattner1abbd412007-06-08 17:58:43 +00001501 // If we have an integer constant expression, we need to *evaluate* it and
1502 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001503 llvm::APSInt Result;
1504 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001505}
Steve Narofff7a5da12007-07-28 23:10:27 +00001506
Douglas Gregor71235ec2009-05-02 02:18:30 +00001507FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001508 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001509
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001510 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1511 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1512 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)) {
1533 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1534 E = ICE->getSubExpr()->IgnoreParens();
1535 else
1536 break;
1537 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001538
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001539 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1540 return ASE->getBase()->getType()->isVectorType();
1541
1542 if (isa<ExtVectorElementExpr>(E))
1543 return true;
1544
1545 return false;
1546}
1547
Chris Lattnerb8211f62009-02-16 22:14:05 +00001548/// isArrow - Return true if the base expression is a pointer to vector,
1549/// return false if the base expression is a vector.
1550bool ExtVectorElementExpr::isArrow() const {
1551 return getBase()->getType()->isPointerType();
1552}
1553
Nate Begemance4d7fc2008-04-18 23:10:10 +00001554unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001555 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001556 return VT->getNumElements();
1557 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001558}
1559
Nate Begemanf322eab2008-05-09 06:41:27 +00001560/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001561bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001562 // FIXME: Refactor this code to an accessor on the AST node which returns the
1563 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001564 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001565
1566 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001567 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001568 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001569
Nate Begeman7e5185b2009-01-18 02:01:21 +00001570 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001571 if (Comp[0] == 's' || Comp[0] == 'S')
1572 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001573
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001574 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1575 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001576 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001577
Steve Naroff0d595ca2007-07-30 03:29:09 +00001578 return false;
1579}
Chris Lattner885b4952007-08-02 23:36:59 +00001580
Nate Begemanf322eab2008-05-09 06:41:27 +00001581/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001582void ExtVectorElementExpr::getEncodedElementAccess(
1583 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001584 llvm::StringRef Comp = Accessor->getName();
1585 if (Comp[0] == 's' || Comp[0] == 'S')
1586 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001587
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001588 bool isHi = Comp == "hi";
1589 bool isLo = Comp == "lo";
1590 bool isEven = Comp == "even";
1591 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001592
Nate Begemanf322eab2008-05-09 06:41:27 +00001593 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1594 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Nate Begemanf322eab2008-05-09 06:41:27 +00001596 if (isHi)
1597 Index = e + i;
1598 else if (isLo)
1599 Index = i;
1600 else if (isEven)
1601 Index = 2 * i;
1602 else if (isOdd)
1603 Index = 2 * i + 1;
1604 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001605 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001606
Nate Begemand3862152008-05-13 21:03:02 +00001607 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001608 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001609}
1610
Douglas Gregor9a129192010-04-21 00:45:42 +00001611ObjCMessageExpr::ObjCMessageExpr(QualType T,
1612 SourceLocation LBracLoc,
1613 SourceLocation SuperLoc,
1614 bool IsInstanceSuper,
1615 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001616 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001617 ObjCMethodDecl *Method,
1618 Expr **Args, unsigned NumArgs,
1619 SourceLocation RBracLoc)
1620 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001621 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001622 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1623 HasMethod(Method != 0), SuperLoc(SuperLoc),
1624 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1625 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001626 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001627{
Douglas Gregor9a129192010-04-21 00:45:42 +00001628 setReceiverPointer(SuperType.getAsOpaquePtr());
1629 if (NumArgs)
1630 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001631}
1632
Douglas Gregor9a129192010-04-21 00:45:42 +00001633ObjCMessageExpr::ObjCMessageExpr(QualType T,
1634 SourceLocation LBracLoc,
1635 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001636 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001637 ObjCMethodDecl *Method,
1638 Expr **Args, unsigned NumArgs,
1639 SourceLocation RBracLoc)
1640 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001641 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001642 hasAnyValueDependentArguments(Args, NumArgs))),
1643 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1644 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1645 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001646 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001647{
1648 setReceiverPointer(Receiver);
1649 if (NumArgs)
1650 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001651}
1652
Douglas Gregor9a129192010-04-21 00:45:42 +00001653ObjCMessageExpr::ObjCMessageExpr(QualType T,
1654 SourceLocation LBracLoc,
1655 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001656 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001657 ObjCMethodDecl *Method,
1658 Expr **Args, unsigned NumArgs,
1659 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001660 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001661 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001662 hasAnyValueDependentArguments(Args, NumArgs))),
1663 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1664 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1665 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001666 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001667{
1668 setReceiverPointer(Receiver);
1669 if (NumArgs)
1670 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001671}
1672
Douglas Gregor9a129192010-04-21 00:45:42 +00001673ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1674 SourceLocation LBracLoc,
1675 SourceLocation SuperLoc,
1676 bool IsInstanceSuper,
1677 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001678 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001679 ObjCMethodDecl *Method,
1680 Expr **Args, unsigned NumArgs,
1681 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001682 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001683 NumArgs * sizeof(Expr *);
1684 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1685 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001686 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00001687 RBracLoc);
1688}
1689
1690ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1691 SourceLocation LBracLoc,
1692 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001693 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001694 ObjCMethodDecl *Method,
1695 Expr **Args, unsigned NumArgs,
1696 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001697 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001698 NumArgs * sizeof(Expr *);
1699 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001700 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001701 NumArgs, RBracLoc);
1702}
1703
1704ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1705 SourceLocation LBracLoc,
1706 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001707 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001708 ObjCMethodDecl *Method,
1709 Expr **Args, unsigned NumArgs,
1710 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001711 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001712 NumArgs * sizeof(Expr *);
1713 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001714 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001715 NumArgs, RBracLoc);
1716}
1717
Alexis Hunta8136cc2010-05-05 15:23:54 +00001718ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00001719 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001720 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001721 NumArgs * sizeof(Expr *);
1722 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1723 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1724}
Alexis Hunta8136cc2010-05-05 15:23:54 +00001725
Douglas Gregor9a129192010-04-21 00:45:42 +00001726Selector ObjCMessageExpr::getSelector() const {
1727 if (HasMethod)
1728 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1729 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001730 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00001731}
1732
1733ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1734 switch (getReceiverKind()) {
1735 case Instance:
1736 if (const ObjCObjectPointerType *Ptr
1737 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1738 return Ptr->getInterfaceDecl();
1739 break;
1740
1741 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00001742 if (const ObjCObjectType *Ty
1743 = getClassReceiver()->getAs<ObjCObjectType>())
1744 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00001745 break;
1746
1747 case SuperInstance:
1748 if (const ObjCObjectPointerType *Ptr
1749 = getSuperType()->getAs<ObjCObjectPointerType>())
1750 return Ptr->getInterfaceDecl();
1751 break;
1752
1753 case SuperClass:
1754 if (const ObjCObjectPointerType *Iface
1755 = getSuperType()->getAs<ObjCObjectPointerType>())
1756 return Iface->getInterfaceDecl();
1757 break;
1758 }
1759
1760 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00001761}
Chris Lattner7ec71da2009-04-26 00:44:05 +00001762
Chris Lattner35e564e2007-10-25 00:29:32 +00001763bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001764 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001765}
1766
Nate Begeman48745922009-08-12 02:28:50 +00001767void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1768 unsigned NumExprs) {
1769 if (SubExprs) C.Deallocate(SubExprs);
1770
1771 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001772 this->NumExprs = NumExprs;
1773 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001774}
Nate Begeman48745922009-08-12 02:28:50 +00001775
1776void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1777 DestroyChildren(C);
1778 if (SubExprs) C.Deallocate(SubExprs);
1779 this->~ShuffleVectorExpr();
1780 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00001781}
1782
Douglas Gregore26a2852009-08-07 06:08:38 +00001783void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00001784 // Override default behavior of traversing children. If this has a type
1785 // operand and the type is a variable-length array, the child iteration
1786 // will iterate over the size expression. However, this expression belongs
1787 // to the type, not to this, so we don't want to delete it.
1788 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00001789 if (isArgumentType()) {
1790 this->~SizeOfAlignOfExpr();
1791 C.Deallocate(this);
1792 }
Sebastian Redl6f282892008-11-11 17:56:53 +00001793 else
Douglas Gregore26a2852009-08-07 06:08:38 +00001794 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00001795}
1796
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001797//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001798// DesignatedInitExpr
1799//===----------------------------------------------------------------------===//
1800
1801IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1802 assert(Kind == FieldDesignator && "Only valid on a field designator");
1803 if (Field.NameOrField & 0x01)
1804 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1805 else
1806 return getField()->getIdentifier();
1807}
1808
Alexis Hunta8136cc2010-05-05 15:23:54 +00001809DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001810 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001811 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001812 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001813 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001814 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001815 unsigned NumIndexExprs,
1816 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001817 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001818 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001819 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1820 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001821 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001822
1823 // Record the initializer itself.
1824 child_iterator Child = child_begin();
1825 *Child++ = Init;
1826
1827 // Copy the designators and their subexpressions, computing
1828 // value-dependence along the way.
1829 unsigned IndexIdx = 0;
1830 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001831 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001832
1833 if (this->Designators[I].isArrayDesignator()) {
1834 // Compute type- and value-dependence.
1835 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001836 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001837 Index->isTypeDependent() || Index->isValueDependent();
1838
1839 // Copy the index expressions into permanent storage.
1840 *Child++ = IndexExprs[IndexIdx++];
1841 } else if (this->Designators[I].isArrayRangeDesignator()) {
1842 // Compute type- and value-dependence.
1843 Expr *Start = IndexExprs[IndexIdx];
1844 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001845 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001846 Start->isTypeDependent() || Start->isValueDependent() ||
1847 End->isTypeDependent() || End->isValueDependent();
1848
1849 // Copy the start/end expressions into permanent storage.
1850 *Child++ = IndexExprs[IndexIdx++];
1851 *Child++ = IndexExprs[IndexIdx++];
1852 }
1853 }
1854
1855 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001856}
1857
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001858DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001859DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001860 unsigned NumDesignators,
1861 Expr **IndexExprs, unsigned NumIndexExprs,
1862 SourceLocation ColonOrEqualLoc,
1863 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001864 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001865 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001866 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001867 ColonOrEqualLoc, UsesColonSyntax,
1868 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001869}
1870
Mike Stump11289f42009-09-09 15:08:12 +00001871DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001872 unsigned NumIndexExprs) {
1873 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1874 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1875 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1876}
1877
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001878void DesignatedInitExpr::setDesignators(ASTContext &C,
1879 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001880 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001881 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00001882
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001883 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00001884 NumDesignators = NumDesigs;
1885 for (unsigned I = 0; I != NumDesigs; ++I)
1886 Designators[I] = Desigs[I];
1887}
1888
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001889SourceRange DesignatedInitExpr::getSourceRange() const {
1890 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00001891 Designator &First =
1892 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001893 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001894 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001895 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1896 else
1897 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1898 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00001899 StartLoc =
1900 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001901 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1902}
1903
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001904Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1905 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1906 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1907 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001908 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1909 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1910}
1911
1912Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001913 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001914 "Requires array range designator");
1915 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1916 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001917 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1918 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1919}
1920
1921Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001922 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001923 "Requires array range designator");
1924 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1925 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001926 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1927 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1928}
1929
Douglas Gregord5846a12009-04-15 06:41:24 +00001930/// \brief Replaces the designator at index @p Idx with the series
1931/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001932void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00001933 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00001934 const Designator *Last) {
1935 unsigned NumNewDesignators = Last - First;
1936 if (NumNewDesignators == 0) {
1937 std::copy_backward(Designators + Idx + 1,
1938 Designators + NumDesignators,
1939 Designators + Idx);
1940 --NumNewDesignators;
1941 return;
1942 } else if (NumNewDesignators == 1) {
1943 Designators[Idx] = *First;
1944 return;
1945 }
1946
Mike Stump11289f42009-09-09 15:08:12 +00001947 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001948 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00001949 std::copy(Designators, Designators + Idx, NewDesignators);
1950 std::copy(First, Last, NewDesignators + Idx);
1951 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1952 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001953 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00001954 Designators = NewDesignators;
1955 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1956}
1957
Douglas Gregore26a2852009-08-07 06:08:38 +00001958void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001959 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00001960 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00001961}
1962
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001963void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
1964 for (unsigned I = 0; I != NumDesignators; ++I)
1965 Designators[I].~Designator();
1966 C.Deallocate(Designators);
1967 Designators = 0;
1968}
1969
Mike Stump11289f42009-09-09 15:08:12 +00001970ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001971 Expr **exprs, unsigned nexprs,
1972 SourceLocation rparenloc)
1973: Expr(ParenListExprClass, QualType(),
1974 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00001975 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00001976 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00001977
Nate Begeman5ec4b312009-08-10 23:49:36 +00001978 Exprs = new (C) Stmt*[nexprs];
1979 for (unsigned i = 0; i != nexprs; ++i)
1980 Exprs[i] = exprs[i];
1981}
1982
1983void ParenListExpr::DoDestroy(ASTContext& C) {
1984 DestroyChildren(C);
1985 if (Exprs) C.Deallocate(Exprs);
1986 this->~ParenListExpr();
1987 C.Deallocate(this);
1988}
1989
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001990//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00001991// ExprIterator.
1992//===----------------------------------------------------------------------===//
1993
1994Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1995Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1996Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1997const Expr* ConstExprIterator::operator[](size_t idx) const {
1998 return cast<Expr>(I[idx]);
1999}
2000const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2001const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2002
2003//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002004// Child Iterators for iterating over subexpressions/substatements
2005//===----------------------------------------------------------------------===//
2006
2007// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002008Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2009Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002010
Steve Naroffe46504b2007-11-12 14:29:37 +00002011// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002012Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2013Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002014
Steve Naroffebf4cb42008-06-02 23:03:37 +00002015// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002016Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2017Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002018
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002019// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002020Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00002021 // If this is accessing a class member, skip that entry.
2022 if (Base) return &Base;
2023 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002024}
Mike Stump11289f42009-09-09 15:08:12 +00002025Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2026 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002027}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002028
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002029// ObjCSuperExpr
2030Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2031Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2032
Steve Naroffe87026a2009-07-24 17:54:45 +00002033// ObjCIsaExpr
2034Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2035Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2036
Chris Lattner6307f192008-08-10 01:53:14 +00002037// PredefinedExpr
2038Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2039Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002040
2041// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002042Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2043Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002044
2045// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002046Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002047Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002048
2049// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002050Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2051Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002052
Chris Lattner1c20a172007-08-26 03:42:43 +00002053// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002054Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2055Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002056
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002057// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002058Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2059Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002060
2061// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002062Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2063Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002064
2065// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002066Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2067Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002068
Douglas Gregor882211c2010-04-28 22:16:22 +00002069// OffsetOfExpr
2070Stmt::child_iterator OffsetOfExpr::child_begin() {
2071 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2072 + NumComps);
2073}
2074Stmt::child_iterator OffsetOfExpr::child_end() {
2075 return child_iterator(&*child_begin() + NumExprs);
2076}
2077
Sebastian Redl6f282892008-11-11 17:56:53 +00002078// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002079Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002080 // If this is of a type and the type is a VLA type (and not a typedef), the
2081 // size expression of the VLA needs to be treated as an executable expression.
2082 // Why isn't this weirdness documented better in StmtIterator?
2083 if (isArgumentType()) {
2084 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2085 getArgumentType().getTypePtr()))
2086 return child_iterator(T);
2087 return child_iterator();
2088 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002089 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002090}
Sebastian Redl6f282892008-11-11 17:56:53 +00002091Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2092 if (isArgumentType())
2093 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002094 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002095}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002096
2097// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002098Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002099 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002100}
Ted Kremenek23702b62007-08-24 20:06:47 +00002101Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002102 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002103}
2104
2105// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002106Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002107 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002108}
Ted Kremenek23702b62007-08-24 20:06:47 +00002109Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002110 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002111}
Ted Kremenek23702b62007-08-24 20:06:47 +00002112
2113// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002114Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2115Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002116
Nate Begemance4d7fc2008-04-18 23:10:10 +00002117// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002118Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2119Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002120
2121// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002122Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2123Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002124
Ted Kremenek23702b62007-08-24 20:06:47 +00002125// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002126Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2127Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002128
2129// BinaryOperator
2130Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002131 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002132}
Ted Kremenek23702b62007-08-24 20:06:47 +00002133Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002134 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002135}
2136
2137// ConditionalOperator
2138Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002139 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002140}
Ted Kremenek23702b62007-08-24 20:06:47 +00002141Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002142 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002143}
2144
2145// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002146Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2147Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002148
Ted Kremenek23702b62007-08-24 20:06:47 +00002149// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002150Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2151Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002152
2153// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002154Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2155 return child_iterator();
2156}
2157
2158Stmt::child_iterator TypesCompatibleExpr::child_end() {
2159 return child_iterator();
2160}
Ted Kremenek23702b62007-08-24 20:06:47 +00002161
2162// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002163Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2164Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002165
Douglas Gregor3be4b122008-11-29 04:51:27 +00002166// GNUNullExpr
2167Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2168Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2169
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002170// ShuffleVectorExpr
2171Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002172 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002173}
2174Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002175 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002176}
2177
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002178// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002179Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2180Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002181
Anders Carlsson4692db02007-08-31 04:56:16 +00002182// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002183Stmt::child_iterator InitListExpr::child_begin() {
2184 return InitExprs.size() ? &InitExprs[0] : 0;
2185}
2186Stmt::child_iterator InitListExpr::child_end() {
2187 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2188}
Anders Carlsson4692db02007-08-31 04:56:16 +00002189
Douglas Gregor0202cb42009-01-29 17:44:32 +00002190// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002191Stmt::child_iterator DesignatedInitExpr::child_begin() {
2192 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2193 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002194 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2195}
2196Stmt::child_iterator DesignatedInitExpr::child_end() {
2197 return child_iterator(&*child_begin() + NumSubExprs);
2198}
2199
Douglas Gregor0202cb42009-01-29 17:44:32 +00002200// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002201Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2202 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002203}
2204
Mike Stump11289f42009-09-09 15:08:12 +00002205Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2206 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002207}
2208
Nate Begeman5ec4b312009-08-10 23:49:36 +00002209// ParenListExpr
2210Stmt::child_iterator ParenListExpr::child_begin() {
2211 return &Exprs[0];
2212}
2213Stmt::child_iterator ParenListExpr::child_end() {
2214 return &Exprs[0]+NumExprs;
2215}
2216
Ted Kremenek23702b62007-08-24 20:06:47 +00002217// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002218Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002219 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002220}
2221Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002222 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002223}
Ted Kremenek23702b62007-08-24 20:06:47 +00002224
2225// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002226Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2227Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002228
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002229// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002230Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002231 return child_iterator();
2232}
2233Stmt::child_iterator ObjCSelectorExpr::child_end() {
2234 return child_iterator();
2235}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002236
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002237// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002238Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2239 return child_iterator();
2240}
2241Stmt::child_iterator ObjCProtocolExpr::child_end() {
2242 return child_iterator();
2243}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002244
Steve Naroffd54978b2007-09-18 23:55:05 +00002245// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002246Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002247 if (getReceiverKind() == Instance)
2248 return reinterpret_cast<Stmt **>(this + 1);
2249 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002250}
2251Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002252 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002253}
2254
Steve Naroffc540d662008-09-03 18:15:37 +00002255// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002256Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2257Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002258
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002259Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2260Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }