blob: 724c65aa312f15619e2b2ce92a24ca8ee098e240 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Chris Lattnerbef0efd2010-05-13 01:02:19 +000030void Expr::ANCHOR() {} // key function for Expr class.
31
Chris Lattner2b334bb2010-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;
Sean Huntc3021132010-05-05 15:23:54 +000039 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregor2ade35e2010-06-16 00:17:44 +000040 if (!getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000041
Chris Lattner2b334bb2010-04-16 23:34:13 +000042 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
43 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000044
Chris Lattner2b334bb2010-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 }
Sean Huntc3021132010-05-05 15:23:54 +000054
John McCall6907fbe2010-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 Lattner2b334bb2010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000059
Chris Lattner2b334bb2010-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;
Sean Huntc3021132010-05-05 15:23:54 +000072
Chris Lattner2b334bb2010-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();
Sean Huntc3021132010-05-05 15:23:54 +000079
Chris Lattner2b334bb2010-04-16 23:34:13 +000080 case BinaryOperator::Comma:
81 case BinaryOperator::Assign:
82 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Sean Huntc3021132010-05-05 15:23:54 +000085
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
87 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000089
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 return false;
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093//===----------------------------------------------------------------------===//
94// Primary Expressions.
95//===----------------------------------------------------------------------===//
96
John McCalld5532b62009-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 Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000116std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
117 return sizeof(ExplicitTemplateArgumentList) +
118 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
119}
120
John McCalld5532b62009-11-23 01:53:49 +0000121std::size_t ExplicitTemplateArgumentList::sizeFor(
122 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000123 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000124}
125
Douglas Gregor0da76df2009-11-23 11:41:28 +0000126void DeclRefExpr::computeDependence() {
127 TypeDependent = false;
128 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000129
Douglas Gregor0da76df2009-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 //
Sean Huntc3021132010-05-05 15:23:54 +0000135 // and
Douglas Gregor0da76df2009-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
Sean Huntc3021132010-05-05 15:23:54 +0000147 else if (D->getDeclName().getNameKind()
Douglas Gregor0da76df2009-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,
Sean Huntc3021132010-05-05 15:23:54 +0000154 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 TemplateSpecializationType::anyDependentTemplateArguments(
Sean Huntc3021132010-05-05 15:23:54 +0000156 getTemplateArgs(),
Douglas Gregor0da76df2009-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 Gregor2ade35e2010-06-16 00:17:44 +0000167 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000168 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000169 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000170 if (Init->isValueDependent())
171 ValueDependent = true;
Douglas Gregorbb6e73f2010-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 Gregor7ed5bd32010-05-11 08:44:04 +0000177 Var->getDeclContext()->isDependentContext())
Douglas Gregorbb6e73f2010-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 Gregor0da76df2009-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
Sean Huntc3021132010-05-05 15:23:54 +0000190DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000191 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000192 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000193 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000194 QualType T)
195 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000196 DecoratedD(D,
197 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000198 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000199 Loc(NameLoc) {
200 if (Qualifier) {
201 NameQualifier *NQ = getNameQualifier();
202 NQ->NNS = Qualifier;
203 NQ->Range = QualifierRange;
204 }
Sean Huntc3021132010-05-05 15:23:54 +0000205
John McCalld5532b62009-11-23 01:53:49 +0000206 if (TemplateArgs)
207 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000208
209 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000210}
211
212DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
213 NestedNameSpecifier *Qualifier,
214 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000215 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000216 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000217 QualType T,
218 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000219 std::size_t Size = sizeof(DeclRefExpr);
220 if (Qualifier != 0)
221 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000222
John McCalld5532b62009-11-23 01:53:49 +0000223 if (TemplateArgs)
224 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000225
Douglas Gregora2813ce2009-10-23 18:54:35 +0000226 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
227 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000228 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000229}
230
Argyrios Kyrtzidis663e3802010-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 Gregora2813ce2009-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);
Sean Huntc3021132010-05-05 15:23:54 +0000247
Douglas Gregora2813ce2009-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 Carlsson3a082d82009-09-08 18:24:21 +0000255// FIXME: Maybe this should use DeclPrinter with a special "print predefined
256// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000257std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
258 ASTContext &Context = CurrentDecl->getASTContext();
259
Anders Carlsson3a082d82009-09-08 18:24:21 +0000260 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000261 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-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 Carlsson848fa642010-02-11 18:20:28 +0000268 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000269 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000270 if (MD->isStatic())
271 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000272 }
273
274 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000275
276 std::string Proto = FD->getQualifiedNameAsString(Policy);
277
John McCall183700f2009-09-21 23:43:11 +0000278 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-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 Weinig4eadcc52009-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 Weinig3a1ce1e2009-12-06 23:55:13 +0000308 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
309 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-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 Kremenekb03d33e2010-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 Kramer900fc632010-04-17 09:33:03 +0000325 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000326
Anders Carlsson3a082d82009-09-08 18:24:21 +0000327 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000328 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
329 Out << '(' << CID << ')';
330
Anders Carlsson3a082d82009-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 Lattnerda8249e2008-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 Johannesenee5a7002008-10-09 23:02:32 +0000350 bool ignored;
351 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
352 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000353 return V.convertToDouble();
354}
355
Chris Lattner2085fd62009-02-18 06:40:38 +0000356StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
357 unsigned ByteLength, bool Wide,
358 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000359 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000360 unsigned NumStrs) {
Chris Lattner2085fd62009-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 Stump1eb44332009-09-09 15:08:12 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376
Chris Lattner726e1682009-02-18 05:49:11 +0000377 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000378 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
379 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000380}
381
Douglas Gregor673ecd62009-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 Gregor42602bb2009-08-07 06:08:38 +0000393void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000394 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000395 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000396}
397
Daniel Dunbarb6480232009-09-22 03:27:33 +0000398void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000399 if (StrData)
400 C.Deallocate(const_cast<char*>(StrData));
401
Daniel Dunbarb6480232009-09-22 03:27:33 +0000402 char *AStrData = new (C, 1) char[Str.size()];
403 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000404 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000405 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000406}
407
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
412 default: assert(0 && "Unknown unary operator");
413 case PostInc: return "++";
414 case PostDec: return "--";
415 case PreInc: return "++";
416 case PreDec: return "--";
417 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";
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000426 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 }
428}
429
Mike Stump1eb44332009-09-09 15:08:12 +0000430UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000431UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
432 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000433 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-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 Gregorbc736fc2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000460//===----------------------------------------------------------------------===//
461// Postfix Operators.
462//===----------------------------------------------------------------------===//
463
Ted Kremenek668bf912009-02-09 20:51:47 +0000464CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000465 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000466 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000467 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000468 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000469 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek668bf912009-02-09 20:51:47 +0000471 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000475
Douglas Gregorb4609802008-11-14 16:09:21 +0000476 RParenLoc = rparenloc;
477}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000478
Ted Kremenek668bf912009-02-09 20:51:47 +0000479CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
480 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000481 : Expr(CallExprClass, t,
482 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000483 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000484 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000485
486 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000487 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000489 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000490
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 RParenLoc = rparenloc;
492}
493
Mike Stump1eb44332009-09-09 15:08:12 +0000494CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
495 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000496 SubExprs = new (C) Stmt*[1];
497}
498
Douglas Gregor42602bb2009-08-07 06:08:38 +0000499void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000500 DestroyChildren(C);
501 if (SubExprs) C.Deallocate(SubExprs);
502 this->~CallExpr();
503 C.Deallocate(this);
504}
505
Nuno Lopesd20254f2009-12-20 23:11:08 +0000506Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000507 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000508 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000509 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000510 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
511 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000512
513 return 0;
514}
515
Nuno Lopesd20254f2009-12-20 23:11:08 +0000516FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000517 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000518}
519
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000523void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000524 // No change, just return.
525 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000530 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Dunbar68a049c2009-07-28 06:29:46 +0000536 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregor88c9a462009-04-17 21:46:47 +0000544 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000545 SubExprs = NewSubExprs;
546 this->NumArgs = NumArgs;
547}
548
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000551unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000552 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000553 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000557 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000559 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
560 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000561 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Anders Carlssonbcba2012008-01-31 02:13:57 +0000563 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
564 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000565 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000567 if (!FDecl->getIdentifier())
568 return 0;
569
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000570 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000571}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000572
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000573QualType CallExpr::getCallReturnType() const {
574 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000575 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000576 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000577 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000578 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000579 else if (const MemberPointerType *MPT
580 = CalleeType->getAs<MemberPointerType>())
581 CalleeType = MPT->getPointeeType();
582
John McCall183700f2009-09-21 23:43:11 +0000583 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000584 return FnType->getResultType();
585}
Chris Lattnercb888962008-10-06 05:00:53 +0000586
Sean Huntc3021132010-05-05 15:23:54 +0000587OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000588 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000589 TypeSourceInfo *tsi,
590 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000591 Expr** exprsPtr, unsigned numExprs,
592 SourceLocation RParenLoc) {
593 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000594 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-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
Sean Huntc3021132010-05-05 15:23:54 +0000609OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000610 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000611 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000612 Expr** exprsPtr, unsigned numExprs,
613 SourceLocation RParenLoc)
Sean Huntc3021132010-05-05 15:23:54 +0000614 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000615 /*ValueDependent=*/tsi->getType()->isDependentType() ||
616 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
617 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Sean Huntc3021132010-05-05 15:23:54 +0000618 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
619 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000620{
621 for(unsigned i = 0; i < numComps; ++i) {
622 setComponent(i, compsPtr[i]);
623 }
Sean Huntc3021132010-05-05 15:23:54 +0000624
Douglas Gregor8ecdb652010-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();
Sean Huntc3021132010-05-05 15:23:54 +0000634
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000635 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
636}
637
Mike Stump1eb44332009-09-09 15:08:12 +0000638MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
639 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000640 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000641 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000642 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000643 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000644 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000645 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000646 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000647
John McCall161755a2010-04-06 21:38:20 +0000648 bool hasQualOrFound = (qual != 0 ||
649 founddecl.getDecl() != memberdecl ||
650 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000651 if (hasQualOrFound)
652 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
John McCalld5532b62009-11-23 01:53:49 +0000654 if (targs)
655 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000657 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-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 Gregor83f6faf2009-08-31 23:41:50 +0000679}
680
Anders Carlssonf8ec55a2009-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 Gregore39a3892010-07-13 23:17:26 +0000687 case CastExpr::CK_LValueBitCast:
688 return "LValueBitCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000689 case CastExpr::CK_NoOp:
690 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000691 case CastExpr::CK_BaseToDerived:
692 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000693 case CastExpr::CK_DerivedToBase:
694 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000695 case CastExpr::CK_UncheckedDerivedToBase:
696 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-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 Carlsson1a31a182009-10-30 00:46:35 +0000709 case CastExpr::CK_DerivedToBaseMemberPointer:
710 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000711 case CastExpr::CK_UserDefinedConversion:
712 return "UserDefinedConversion";
713 case CastExpr::CK_ConstructorConversion:
714 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000715 case CastExpr::CK_IntegralToPointer:
716 return "IntegralToPointer";
717 case CastExpr::CK_PointerToIntegral:
718 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000719 case CastExpr::CK_ToVoid:
720 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000721 case CastExpr::CK_VectorSplat:
722 return "VectorSplat";
Anders Carlsson82debc72009-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 Kramerc6b29162009-10-18 19:02:15 +0000729 case CastExpr::CK_FloatingCast:
730 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000731 case CastExpr::CK_MemberPointerToBoolean:
732 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000733 case CastExpr::CK_AnyPointerToObjCPointerCast:
734 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000735 case CastExpr::CK_AnyPointerToBlockPointerCast:
736 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000739 assert(0 && "Unhandled cast kind!");
740 return 0;
741}
742
Anders Carlssona3bdded2010-04-23 21:02:34 +0000743void CastExpr::DoDestroy(ASTContext &C)
744{
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000745 BasePath.Destroy();
Anders Carlssona3bdded2010-04-23 21:02:34 +0000746 Expr::DoDestroy(C);
747}
748
Douglas Gregor6eef5192009-12-14 19:27:10 +0000749Expr *CastExpr::getSubExprAsWritten() {
750 Expr *SubExpr = 0;
751 CastExpr *E = this;
752 do {
753 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000754
Douglas Gregor6eef5192009-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();
Sean Huntc3021132010-05-05 15:23:54 +0000758
Douglas Gregor6eef5192009-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();
Sean Huntc3021132010-05-05 15:23:54 +0000765
Douglas Gregor6eef5192009-12-14 19:27:10 +0000766 // If the subexpression we're left with is an implicit cast, look
767 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000768 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
769
Douglas Gregor6eef5192009-12-14 19:27:10 +0000770 return SubExpr;
771}
772
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorbaf53482009-03-12 22:51:37 +0000777 case PtrMemD: return ".*";
778 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorbaf53482009-03-12 22:51:37 +0000810
811 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000812}
813
Mike Stump1eb44332009-09-09 15:08:12 +0000814BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000815BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
816 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000817 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-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 Gregor063daf62009-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 Kremenek709210f2010-04-13 23:39:13 +0000876InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000877 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000878 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000879 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000880 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000881 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +0000882 UnionFieldInit(0), HadArrayRangeDesignator(false)
883{
Ted Kremenekba7bc552010-02-19 01:50:18 +0000884 for (unsigned I = 0; I != numInits; ++I) {
885 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000886 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000887 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000888 ValueDependent = true;
889 }
Sean Huntc3021132010-05-05 15:23:54 +0000890
Ted Kremenek709210f2010-04-13 23:39:13 +0000891 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000892}
Reid Spencer5f016e22007-07-11 17:01:13 +0000893
Ted Kremenek709210f2010-04-13 23:39:13 +0000894void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000895 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000896 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000897}
898
Ted Kremenek709210f2010-04-13 23:39:13 +0000899void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000900 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
901 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000902 InitExprs[Idx]->Destroy(C);
903 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000904}
905
Ted Kremenek709210f2010-04-13 23:39:13 +0000906Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000907 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000908 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000909 InitExprs.back() = expr;
910 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Douglas Gregor4c678342009-01-28 21:54:33 +0000913 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
914 InitExprs[Init] = expr;
915 return Result;
916}
917
Steve Naroffbfdcae62008-09-04 15:31:07 +0000918/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000919///
920const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000921 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000922 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000923}
924
Mike Stump1eb44332009-09-09 15:08:12 +0000925SourceLocation BlockExpr::getCaretLocation() const {
926 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000927}
Mike Stump1eb44332009-09-09 15:08:12 +0000928const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000929 return TheBlock->getBody();
930}
Mike Stump1eb44332009-09-09 15:08:12 +0000931Stmt *BlockExpr::getBody() {
932 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000933}
Steve Naroff56ee6892008-10-08 17:01:13 +0000934
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936//===----------------------------------------------------------------------===//
937// Generic Expression Routines
938//===----------------------------------------------------------------------===//
939
Chris Lattner026dc962009-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 Stumpdf317bf2009-11-03 23:25:48 +0000945 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-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 Stump1eb44332009-09-09 15:08:12 +0000950
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 switch (getStmtClass()) {
952 default:
John McCall0faede62010-03-12 07:11:26 +0000953 if (getType()->isVoidType())
954 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000955 Loc = getExprLoc();
956 R1 = getSourceRange();
957 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000959 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000960 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 case UnaryOperatorClass: {
962 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Reid Spencer5f016e22007-07-11 17:01:13 +0000964 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000965 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 case UnaryOperator::PostInc:
967 case UnaryOperator::PostDec:
968 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000969 case UnaryOperator::PreDec: // ++/--
970 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 case UnaryOperator::Deref:
972 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000973 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000974 return false;
975 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 case UnaryOperator::Real:
977 case UnaryOperator::Imag:
978 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000979 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
980 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000981 return false;
982 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000984 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 }
Chris Lattner026dc962009-02-14 07:37:35 +0000986 Loc = UO->getOperatorLoc();
987 R1 = UO->getSubExpr()->getSourceRange();
988 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000990 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000991 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000992 switch (BO->getOpcode()) {
993 default:
994 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +0000995 // Consider the RHS of comma for side effects. LHS was checked by
996 // Sema::CheckCommaOperands.
Ted Kremenekc46a2462010-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 Kyrtzidis25973452010-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 Kremenekc46a2462010-04-07 18:49:21 +00001006 case BinaryOperator::LAnd:
1007 case BinaryOperator::LOr:
Argyrios Kyrtzidis25973452010-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 McCallbf0ee352010-02-16 04:10:53 +00001012 }
Chris Lattner026dc962009-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 Lattnere7716e62007-12-01 06:07:34 +00001019 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001020 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001021 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001022 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001023
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001024 case ConditionalOperatorClass: {
Chris Lattner026dc962009-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 Jahanianab38e4b2007-12-01 19:58:28 +00001027 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001028 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001029 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001030 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001031 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001032 }
1033
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 case MemberExprClass:
Chris Lattner026dc962009-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 Stumpdf317bf2009-11-03 23:25:48 +00001037 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-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 Stump1eb44332009-09-09 15:08:12 +00001043
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 case ArraySubscriptExprClass:
1045 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001046 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001047 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-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 Friedman211f6ad2008-05-27 15:24:04 +00001053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001055 case CXXOperatorCallExprClass:
1056 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001057 // If this is a direct call, get the callee.
1058 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001059 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-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 Lattnerbc8d42c2009-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 Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattnerbc8d42c2009-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 Lattner026dc962009-02-14 07:37:35 +00001075 }
1076 return false;
1077 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001078
1079 case CXXTemporaryObjectExprClass:
1080 case CXXConstructExprClass:
1081 return false;
1082
Fariborz Jahanianf0317742010-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 Lattner026dc962009-02-14 07:37:35 +00001090 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001093 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001094#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001095 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001096 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001097 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-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 Lattner5e94a0d2009-08-16 16:51:50 +00001102#else
1103 Loc = getExprLoc();
1104 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001105#endif
1106 return true;
1107 }
Chris Lattner611b2ec2008-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 Stumpdf317bf2009-11-03 23:25:48 +00001117 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001118
John McCall0faede62010-03-12 07:11:26 +00001119 if (getType()->isVoidType())
1120 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001121 Loc = cast<StmtExpr>(this)->getLParenLoc();
1122 R1 = getSourceRange();
1123 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001124 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001125 case CStyleCastExprClass:
Chris Lattnerfb846642009-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 Lattner026dc962009-02-14 07:37:35 +00001128 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001129 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001130 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1131 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1132 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001133 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001134 if (getType()->isVoidType())
1135 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001136 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001137
Anders Carlsson58beed92009-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 Stumpdf317bf2009-11-03 23:25:48 +00001142 return (cast<CastExpr>(this)->getSubExpr()
1143 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001144 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1145 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1146 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Eli Friedman4be1f472008-05-19 21:24:43 +00001149 case ImplicitCastExprClass:
1150 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001151 return (cast<ImplicitCastExpr>(this)
1152 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001153
Chris Lattner04421082008-04-08 04:40:51 +00001154 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001155 return (cast<CXXDefaultArgExpr>(this)
1156 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-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 Lattner026dc962009-02-14 07:37:35 +00001162 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001163 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001164 return (cast<CXXBindTemporaryExpr>(this)
1165 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001166 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001167 return (cast<CXXExprWithTemporaries>(this)
1168 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001169 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001170}
1171
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001172/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001173/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001174bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001175 switch (getStmtClass()) {
1176 default:
1177 return false;
1178 case ObjCIvarRefExprClass:
1179 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001180 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001181 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001182 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001183 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001184 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001185 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001186 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001187 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001188 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001189 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-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 Jahanian59a53fa2009-09-16 18:09:18 +00001194 // dereferencing to a pointer is always a gc'able candidate,
1195 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001196 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001197 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001198 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001199 return false;
1200 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001201 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001202 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001203 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001204 }
1205 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001206 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001207 }
1208}
Ted Kremenek4e99a5f2008-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 Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001214 return E;
1215}
1216
Chris Lattner56f34942008-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 Lattner56f34942008-02-13 01:02:39 +00001226 else
1227 return E;
1228 }
1229}
1230
John McCall2fc46bf2010-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 Lattnerecdd8412009-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 Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattnerecdd8412009-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 Gregor2ade35e2010-06-16 00:17:44 +00001256 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001257 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Chris Lattnerecdd8412009-03-13 17:28:01 +00001259 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1260 E = SE;
1261 continue;
1262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Douglas Gregor9d3347a2010-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 Lattnerecdd8412009-03-13 17:28:01 +00001268 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1269 E = SE;
1270 continue;
1271 }
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattnerecdd8412009-03-13 17:28:01 +00001274 return E;
1275 }
1276}
1277
Douglas Gregor6eef5192009-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();
Sean Huntc3021132010-05-05 15:23:54 +00001282
Douglas Gregor6eef5192009-12-14 19:27:10 +00001283 return isa<CXXDefaultArgExpr>(E);
1284}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001285
Douglas Gregor2f599792010-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 }
Sean Huntc3021132010-05-05 15:23:54 +00001305
Douglas Gregor2f599792010-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 Gregor898574e2008-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 Friedmanc9e8f602009-01-25 02:32:41 +00001371bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-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 Friedman1f4a6db2009-02-20 02:36:22 +00001378 // FIXME: This function assumes the variable being assigned to
1379 // isn't a reference type!
1380
Anders Carlssone8a32b82008-11-24 05:23:59 +00001381 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001382 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001383 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001384 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001385 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001386 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001387 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-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 Begeman59b5da62009-01-18 03:20:47 +00001391 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001392 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001393 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001394 case InitListExprClass: {
Eli Friedman1f4a6db2009-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 Carlssone8a32b82008-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 Stump1eb44332009-09-09 15:08:12 +00001401 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001402 return false;
1403 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001404 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001405 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001406 case ImplicitValueInitExprClass:
1407 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001408 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001409 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-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 Lattner3ae9f482009-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 Lattner81045d82009-04-21 05:19:11 +00001426 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-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);
Sean Huntc3021132010-05-05 15:23:54 +00001433
Chris Lattner430656e2009-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);
Sean Huntc3021132010-05-05 15:23:54 +00001439
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001440 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001441 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001442 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001443}
1444
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregorce940492009-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.
Sean Huntc3021132010-05-05 15:23:54 +00001455
Douglas Gregorce940492009-09-25 04:25:58 +00001456 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001457 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001458
Douglas Gregorce940492009-09-25 04:25:58 +00001459 case NPC_ValueDependentIsNotNull:
1460 return false;
1461 }
1462 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001463
Sebastian Redl07779722008-10-31 14:43:28 +00001464 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001465 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001466 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001467 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001468 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001469 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001470 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001471 Pointee->isVoidType() && // to void*
1472 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001473 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001474 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001476 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1477 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001478 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-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 Gregorce940492009-09-25 04:25:58 +00001482 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001483 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001484 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001485 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001486 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-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 Naroffaaffbf72008-01-14 02:53:34 +00001490 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001491
Sebastian Redl6e8ed162009-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 Naroffaa58f002008-01-14 16:10:57 +00001496 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00001497 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001498 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001499 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 // If we have an integer constant expression, we need to *evaluate* it and
1502 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001503 llvm::APSInt Result;
1504 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001505}
Steve Naroff31a45842007-07-28 23:10:27 +00001506
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001507FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001508 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001509
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001510 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redl906082e2010-07-20 04:20:21 +00001511 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1512 ICE->getCastKind() == CastExpr::CK_NoOp)
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001513 E = ICE->getSubExpr()->IgnoreParens();
1514 else
1515 break;
1516 }
1517
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001518 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001519 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001520 if (Field->isBitField())
1521 return Field;
1522
1523 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1524 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1525 return BinOp->getLHS()->getBitField();
1526
1527 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001528}
1529
Anders Carlsson09380262010-01-31 17:18:49 +00001530bool Expr::refersToVectorElement() const {
1531 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00001532
Anders Carlsson09380262010-01-31 17:18:49 +00001533 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redl906082e2010-07-20 04:20:21 +00001534 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1535 ICE->getCastKind() == CastExpr::CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00001536 E = ICE->getSubExpr()->IgnoreParens();
1537 else
1538 break;
1539 }
Sean Huntc3021132010-05-05 15:23:54 +00001540
Anders Carlsson09380262010-01-31 17:18:49 +00001541 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1542 return ASE->getBase()->getType()->isVectorType();
1543
1544 if (isa<ExtVectorElementExpr>(E))
1545 return true;
1546
1547 return false;
1548}
1549
Chris Lattner2140e902009-02-16 22:14:05 +00001550/// isArrow - Return true if the base expression is a pointer to vector,
1551/// return false if the base expression is a vector.
1552bool ExtVectorElementExpr::isArrow() const {
1553 return getBase()->getType()->isPointerType();
1554}
1555
Nate Begeman213541a2008-04-18 23:10:10 +00001556unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001557 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001558 return VT->getNumElements();
1559 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001560}
1561
Nate Begeman8a997642008-05-09 06:41:27 +00001562/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001563bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001564 // FIXME: Refactor this code to an accessor on the AST node which returns the
1565 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001566 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001567
1568 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001569 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001570 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Nate Begeman190d6a22009-01-18 02:01:21 +00001572 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001573 if (Comp[0] == 's' || Comp[0] == 'S')
1574 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Daniel Dunbar15027422009-10-17 23:53:04 +00001576 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1577 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001578 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001579
Steve Narofffec0b492007-07-30 03:29:09 +00001580 return false;
1581}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001582
Nate Begeman8a997642008-05-09 06:41:27 +00001583/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001584void ExtVectorElementExpr::getEncodedElementAccess(
1585 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001586 llvm::StringRef Comp = Accessor->getName();
1587 if (Comp[0] == 's' || Comp[0] == 'S')
1588 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001590 bool isHi = Comp == "hi";
1591 bool isLo = Comp == "lo";
1592 bool isEven = Comp == "even";
1593 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Nate Begeman8a997642008-05-09 06:41:27 +00001595 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1596 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Nate Begeman8a997642008-05-09 06:41:27 +00001598 if (isHi)
1599 Index = e + i;
1600 else if (isLo)
1601 Index = i;
1602 else if (isEven)
1603 Index = 2 * i;
1604 else if (isOdd)
1605 Index = 2 * i + 1;
1606 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001607 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001608
Nate Begeman3b8d1162008-05-13 21:03:02 +00001609 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001610 }
Nate Begeman8a997642008-05-09 06:41:27 +00001611}
1612
Douglas Gregor04badcf2010-04-21 00:45:42 +00001613ObjCMessageExpr::ObjCMessageExpr(QualType T,
1614 SourceLocation LBracLoc,
1615 SourceLocation SuperLoc,
1616 bool IsInstanceSuper,
1617 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00001618 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001619 ObjCMethodDecl *Method,
1620 Expr **Args, unsigned NumArgs,
1621 SourceLocation RBracLoc)
1622 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregor92e986e2010-04-22 16:44:27 +00001623 /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00001624 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1625 HasMethod(Method != 0), SuperLoc(SuperLoc),
1626 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1627 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00001628 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00001629{
Douglas Gregor04badcf2010-04-21 00:45:42 +00001630 setReceiverPointer(SuperType.getAsOpaquePtr());
1631 if (NumArgs)
1632 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00001633}
1634
Douglas Gregor04badcf2010-04-21 00:45:42 +00001635ObjCMessageExpr::ObjCMessageExpr(QualType T,
1636 SourceLocation LBracLoc,
1637 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00001638 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001639 ObjCMethodDecl *Method,
1640 Expr **Args, unsigned NumArgs,
1641 SourceLocation RBracLoc)
1642 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00001643 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00001644 hasAnyValueDependentArguments(Args, NumArgs))),
1645 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1646 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1647 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00001648 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00001649{
1650 setReceiverPointer(Receiver);
1651 if (NumArgs)
1652 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00001653}
1654
Douglas Gregor04badcf2010-04-21 00:45:42 +00001655ObjCMessageExpr::ObjCMessageExpr(QualType T,
1656 SourceLocation LBracLoc,
1657 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00001658 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001659 ObjCMethodDecl *Method,
1660 Expr **Args, unsigned NumArgs,
1661 SourceLocation RBracLoc)
Douglas Gregor92e986e2010-04-22 16:44:27 +00001662 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00001663 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00001664 hasAnyValueDependentArguments(Args, NumArgs))),
1665 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1666 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1667 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00001668 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00001669{
1670 setReceiverPointer(Receiver);
1671 if (NumArgs)
1672 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00001673}
1674
Douglas Gregor04badcf2010-04-21 00:45:42 +00001675ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1676 SourceLocation LBracLoc,
1677 SourceLocation SuperLoc,
1678 bool IsInstanceSuper,
1679 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00001680 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001681 ObjCMethodDecl *Method,
1682 Expr **Args, unsigned NumArgs,
1683 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00001684 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00001685 NumArgs * sizeof(Expr *);
1686 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1687 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00001688 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001689 RBracLoc);
1690}
1691
1692ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1693 SourceLocation LBracLoc,
1694 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00001695 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001696 ObjCMethodDecl *Method,
1697 Expr **Args, unsigned NumArgs,
1698 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00001699 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00001700 NumArgs * sizeof(Expr *);
1701 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00001702 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001703 NumArgs, RBracLoc);
1704}
1705
1706ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1707 SourceLocation LBracLoc,
1708 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00001709 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001710 ObjCMethodDecl *Method,
1711 Expr **Args, unsigned NumArgs,
1712 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00001713 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00001714 NumArgs * sizeof(Expr *);
1715 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00001716 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001717 NumArgs, RBracLoc);
1718}
1719
Sean Huntc3021132010-05-05 15:23:54 +00001720ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001721 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00001722 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00001723 NumArgs * sizeof(Expr *);
1724 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1725 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1726}
Sean Huntc3021132010-05-05 15:23:54 +00001727
Douglas Gregor04badcf2010-04-21 00:45:42 +00001728Selector ObjCMessageExpr::getSelector() const {
1729 if (HasMethod)
1730 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1731 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00001732 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00001733}
1734
1735ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1736 switch (getReceiverKind()) {
1737 case Instance:
1738 if (const ObjCObjectPointerType *Ptr
1739 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1740 return Ptr->getInterfaceDecl();
1741 break;
1742
1743 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00001744 if (const ObjCObjectType *Ty
1745 = getClassReceiver()->getAs<ObjCObjectType>())
1746 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00001747 break;
1748
1749 case SuperInstance:
1750 if (const ObjCObjectPointerType *Ptr
1751 = getSuperType()->getAs<ObjCObjectPointerType>())
1752 return Ptr->getInterfaceDecl();
1753 break;
1754
1755 case SuperClass:
1756 if (const ObjCObjectPointerType *Iface
1757 = getSuperType()->getAs<ObjCObjectPointerType>())
1758 return Iface->getInterfaceDecl();
1759 break;
1760 }
1761
1762 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00001763}
Chris Lattner0389e6b2009-04-26 00:44:05 +00001764
Chris Lattner27437ca2007-10-25 00:29:32 +00001765bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00001766 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00001767}
1768
Nate Begeman888376a2009-08-12 02:28:50 +00001769void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1770 unsigned NumExprs) {
1771 if (SubExprs) C.Deallocate(SubExprs);
1772
1773 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001774 this->NumExprs = NumExprs;
1775 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00001776}
Nate Begeman888376a2009-08-12 02:28:50 +00001777
1778void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
1779 DestroyChildren(C);
1780 if (SubExprs) C.Deallocate(SubExprs);
1781 this->~ShuffleVectorExpr();
1782 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00001783}
1784
Douglas Gregor42602bb2009-08-07 06:08:38 +00001785void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00001786 // Override default behavior of traversing children. If this has a type
1787 // operand and the type is a variable-length array, the child iteration
1788 // will iterate over the size expression. However, this expression belongs
1789 // to the type, not to this, so we don't want to delete it.
1790 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001791 if (isArgumentType()) {
1792 this->~SizeOfAlignOfExpr();
1793 C.Deallocate(this);
1794 }
Sebastian Redl05189992008-11-11 17:56:53 +00001795 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00001796 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00001797}
1798
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001799//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00001800// DesignatedInitExpr
1801//===----------------------------------------------------------------------===//
1802
1803IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1804 assert(Kind == FieldDesignator && "Only valid on a field designator");
1805 if (Field.NameOrField & 0x01)
1806 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1807 else
1808 return getField()->getIdentifier();
1809}
1810
Sean Huntc3021132010-05-05 15:23:54 +00001811DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00001812 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001813 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00001814 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001815 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00001816 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001817 unsigned NumIndexExprs,
1818 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00001819 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001820 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00001821 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1822 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00001823 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001824
1825 // Record the initializer itself.
1826 child_iterator Child = child_begin();
1827 *Child++ = Init;
1828
1829 // Copy the designators and their subexpressions, computing
1830 // value-dependence along the way.
1831 unsigned IndexIdx = 0;
1832 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001833 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00001834
1835 if (this->Designators[I].isArrayDesignator()) {
1836 // Compute type- and value-dependence.
1837 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00001838 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00001839 Index->isTypeDependent() || Index->isValueDependent();
1840
1841 // Copy the index expressions into permanent storage.
1842 *Child++ = IndexExprs[IndexIdx++];
1843 } else if (this->Designators[I].isArrayRangeDesignator()) {
1844 // Compute type- and value-dependence.
1845 Expr *Start = IndexExprs[IndexIdx];
1846 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00001847 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00001848 Start->isTypeDependent() || Start->isValueDependent() ||
1849 End->isTypeDependent() || End->isValueDependent();
1850
1851 // Copy the start/end expressions into permanent storage.
1852 *Child++ = IndexExprs[IndexIdx++];
1853 *Child++ = IndexExprs[IndexIdx++];
1854 }
1855 }
1856
1857 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001858}
1859
Douglas Gregor05c13a32009-01-22 00:58:24 +00001860DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00001861DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00001862 unsigned NumDesignators,
1863 Expr **IndexExprs, unsigned NumIndexExprs,
1864 SourceLocation ColonOrEqualLoc,
1865 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00001866 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001867 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00001868 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00001869 ColonOrEqualLoc, UsesColonSyntax,
1870 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001871}
1872
Mike Stump1eb44332009-09-09 15:08:12 +00001873DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00001874 unsigned NumIndexExprs) {
1875 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1876 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1877 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1878}
1879
Douglas Gregor319d57f2010-01-06 23:17:19 +00001880void DesignatedInitExpr::setDesignators(ASTContext &C,
1881 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00001882 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00001883 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00001884
Douglas Gregor319d57f2010-01-06 23:17:19 +00001885 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00001886 NumDesignators = NumDesigs;
1887 for (unsigned I = 0; I != NumDesigs; ++I)
1888 Designators[I] = Desigs[I];
1889}
1890
Douglas Gregor05c13a32009-01-22 00:58:24 +00001891SourceRange DesignatedInitExpr::getSourceRange() const {
1892 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001893 Designator &First =
1894 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00001895 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00001896 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00001897 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1898 else
1899 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1900 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00001901 StartLoc =
1902 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001903 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1904}
1905
Douglas Gregor05c13a32009-01-22 00:58:24 +00001906Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1907 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1908 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1909 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001910 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1911 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1912}
1913
1914Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001915 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00001916 "Requires array range designator");
1917 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1918 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001919 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1920 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1921}
1922
1923Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00001924 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00001925 "Requires array range designator");
1926 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1927 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00001928 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1929 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1930}
1931
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001932/// \brief Replaces the designator at index @p Idx with the series
1933/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00001934void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00001935 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001936 const Designator *Last) {
1937 unsigned NumNewDesignators = Last - First;
1938 if (NumNewDesignators == 0) {
1939 std::copy_backward(Designators + Idx + 1,
1940 Designators + NumDesignators,
1941 Designators + Idx);
1942 --NumNewDesignators;
1943 return;
1944 } else if (NumNewDesignators == 1) {
1945 Designators[Idx] = *First;
1946 return;
1947 }
1948
Mike Stump1eb44332009-09-09 15:08:12 +00001949 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00001950 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001951 std::copy(Designators, Designators + Idx, NewDesignators);
1952 std::copy(First, Last, NewDesignators + Idx);
1953 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1954 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00001955 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001956 Designators = NewDesignators;
1957 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1958}
1959
Douglas Gregor42602bb2009-08-07 06:08:38 +00001960void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00001961 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00001962 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00001963}
1964
Douglas Gregor319d57f2010-01-06 23:17:19 +00001965void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
1966 for (unsigned I = 0; I != NumDesignators; ++I)
1967 Designators[I].~Designator();
1968 C.Deallocate(Designators);
1969 Designators = 0;
1970}
1971
Mike Stump1eb44332009-09-09 15:08:12 +00001972ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00001973 Expr **exprs, unsigned nexprs,
1974 SourceLocation rparenloc)
1975: Expr(ParenListExprClass, QualType(),
1976 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00001977 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00001978 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Nate Begeman2ef13e52009-08-10 23:49:36 +00001980 Exprs = new (C) Stmt*[nexprs];
1981 for (unsigned i = 0; i != nexprs; ++i)
1982 Exprs[i] = exprs[i];
1983}
1984
1985void ParenListExpr::DoDestroy(ASTContext& C) {
1986 DestroyChildren(C);
1987 if (Exprs) C.Deallocate(Exprs);
1988 this->~ParenListExpr();
1989 C.Deallocate(this);
1990}
1991
Douglas Gregor05c13a32009-01-22 00:58:24 +00001992//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00001993// ExprIterator.
1994//===----------------------------------------------------------------------===//
1995
1996Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
1997Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
1998Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
1999const Expr* ConstExprIterator::operator[](size_t idx) const {
2000 return cast<Expr>(I[idx]);
2001}
2002const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2003const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2004
2005//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002006// Child Iterators for iterating over subexpressions/substatements
2007//===----------------------------------------------------------------------===//
2008
2009// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002010Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2011Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002012
Steve Naroff7779db42007-11-12 14:29:37 +00002013// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002014Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2015Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002016
Steve Naroffe3e9add2008-06-02 23:03:37 +00002017// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002018Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2019Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002020
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002021// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002022Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCall2fc46bf2010-05-05 22:59:52 +00002023 // If this is accessing a class member, skip that entry.
2024 if (Base) return &Base;
2025 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002026}
Mike Stump1eb44332009-09-09 15:08:12 +00002027Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2028 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002029}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002030
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002031// ObjCSuperExpr
2032Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2033Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2034
Steve Narofff242b1b2009-07-24 17:54:45 +00002035// ObjCIsaExpr
2036Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2037Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2038
Chris Lattnerd9f69102008-08-10 01:53:14 +00002039// PredefinedExpr
2040Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2041Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002042
2043// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002044Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2045Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002046
2047// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002048Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002049Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002050
2051// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002052Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2053Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002054
Chris Lattner5d661452007-08-26 03:42:43 +00002055// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002056Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2057Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002058
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002059// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002060Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2061Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002062
2063// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002064Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2065Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002066
2067// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002068Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2069Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002070
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002071// OffsetOfExpr
2072Stmt::child_iterator OffsetOfExpr::child_begin() {
2073 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2074 + NumComps);
2075}
2076Stmt::child_iterator OffsetOfExpr::child_end() {
2077 return child_iterator(&*child_begin() + NumExprs);
2078}
2079
Sebastian Redl05189992008-11-11 17:56:53 +00002080// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002081Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002082 // If this is of a type and the type is a VLA type (and not a typedef), the
2083 // size expression of the VLA needs to be treated as an executable expression.
2084 // Why isn't this weirdness documented better in StmtIterator?
2085 if (isArgumentType()) {
2086 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2087 getArgumentType().getTypePtr()))
2088 return child_iterator(T);
2089 return child_iterator();
2090 }
Sebastian Redld4575892008-12-03 23:17:54 +00002091 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002092}
Sebastian Redl05189992008-11-11 17:56:53 +00002093Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2094 if (isArgumentType())
2095 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002096 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002097}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002098
2099// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002100Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002101 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002102}
Ted Kremenek1237c672007-08-24 20:06:47 +00002103Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002104 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002105}
2106
2107// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002108Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002109 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002110}
Ted Kremenek1237c672007-08-24 20:06:47 +00002111Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002112 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002113}
Ted Kremenek1237c672007-08-24 20:06:47 +00002114
2115// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002116Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2117Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002118
Nate Begeman213541a2008-04-18 23:10:10 +00002119// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002120Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2121Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002122
2123// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002124Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2125Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002126
Ted Kremenek1237c672007-08-24 20:06:47 +00002127// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002128Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2129Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002130
2131// BinaryOperator
2132Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002133 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002134}
Ted Kremenek1237c672007-08-24 20:06:47 +00002135Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002136 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002137}
2138
2139// ConditionalOperator
2140Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002141 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002142}
Ted Kremenek1237c672007-08-24 20:06:47 +00002143Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002144 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002145}
2146
2147// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002148Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2149Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002150
Ted Kremenek1237c672007-08-24 20:06:47 +00002151// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002152Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2153Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002154
2155// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002156Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2157 return child_iterator();
2158}
2159
2160Stmt::child_iterator TypesCompatibleExpr::child_end() {
2161 return child_iterator();
2162}
Ted Kremenek1237c672007-08-24 20:06:47 +00002163
2164// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002165Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2166Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002167
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002168// GNUNullExpr
2169Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2170Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2171
Eli Friedmand38617c2008-05-14 19:38:39 +00002172// ShuffleVectorExpr
2173Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002174 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002175}
2176Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002177 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002178}
2179
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002180// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002181Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2182Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002183
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002184// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002185Stmt::child_iterator InitListExpr::child_begin() {
2186 return InitExprs.size() ? &InitExprs[0] : 0;
2187}
2188Stmt::child_iterator InitListExpr::child_end() {
2189 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2190}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002191
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002192// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002193Stmt::child_iterator DesignatedInitExpr::child_begin() {
2194 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2195 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002196 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2197}
2198Stmt::child_iterator DesignatedInitExpr::child_end() {
2199 return child_iterator(&*child_begin() + NumSubExprs);
2200}
2201
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002202// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002203Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2204 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002205}
2206
Mike Stump1eb44332009-09-09 15:08:12 +00002207Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2208 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002209}
2210
Nate Begeman2ef13e52009-08-10 23:49:36 +00002211// ParenListExpr
2212Stmt::child_iterator ParenListExpr::child_begin() {
2213 return &Exprs[0];
2214}
2215Stmt::child_iterator ParenListExpr::child_end() {
2216 return &Exprs[0]+NumExprs;
2217}
2218
Ted Kremenek1237c672007-08-24 20:06:47 +00002219// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002220Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002221 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002222}
2223Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002224 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002225}
Ted Kremenek1237c672007-08-24 20:06:47 +00002226
2227// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002228Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2229Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002230
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002231// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002232Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002233 return child_iterator();
2234}
2235Stmt::child_iterator ObjCSelectorExpr::child_end() {
2236 return child_iterator();
2237}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002238
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002239// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002240Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2241 return child_iterator();
2242}
2243Stmt::child_iterator ObjCProtocolExpr::child_end() {
2244 return child_iterator();
2245}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002246
Steve Naroff563477d2007-09-18 23:55:05 +00002247// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002248Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002249 if (getReceiverKind() == Instance)
2250 return reinterpret_cast<Stmt **>(this + 1);
2251 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002252}
2253Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002254 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002255}
2256
Steve Naroff4eb206b2008-09-03 18:15:37 +00002257// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002258Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2259Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002260
Ted Kremenek9da13f92008-09-26 23:24:14 +00002261Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2262Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }