blob: e40fc7988cbb4859e8644ed9dae0f806b88550b5 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattnerc96f1fb2010-05-13 01:02:19 +000030void Expr::ANCHOR() {} // key function for Expr class.
31
Chris Lattner4ebae652010-04-16 23:34:13 +000032/// isKnownToHaveBooleanValue - Return true if this is an integer expression
33/// that is known to return 0 or 1. This happens for _Bool/bool expressions
34/// but also int expressions which are produced by things like comparisons in
35/// C.
36bool Expr::isKnownToHaveBooleanValue() const {
37 // If this value has _Bool type, it is obvious 0/1.
38 if (getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000039 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregorb90df602010-06-16 00:17:44 +000040 if (!getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000041
Chris Lattner4ebae652010-04-16 23:34:13 +000042 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
43 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000044
Chris Lattner4ebae652010-04-16 23:34:13 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
46 switch (UO->getOpcode()) {
47 case UnaryOperator::Plus:
48 case UnaryOperator::Extension:
49 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000054
John McCall45d30c32010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
57 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner4ebae652010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000059
Chris Lattner4ebae652010-04-16 23:34:13 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
61 switch (BO->getOpcode()) {
62 default: return false;
63 case BinaryOperator::LT: // Relational operators.
64 case BinaryOperator::GT:
65 case BinaryOperator::LE:
66 case BinaryOperator::GE:
67 case BinaryOperator::EQ: // Equality operators.
68 case BinaryOperator::NE:
69 case BinaryOperator::LAnd: // AND operator.
70 case BinaryOperator::LOr: // Logical OR operator.
71 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000072
Chris Lattner4ebae652010-04-16 23:34:13 +000073 case BinaryOperator::And: // Bitwise AND operator.
74 case BinaryOperator::Xor: // Bitwise XOR operator.
75 case BinaryOperator::Or: // Bitwise OR operator.
76 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000079
Chris Lattner4ebae652010-04-16 23:34:13 +000080 case BinaryOperator::Comma:
81 case BinaryOperator::Assign:
82 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Chris Lattner4ebae652010-04-16 23:34:13 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
87 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000089
Chris Lattner4ebae652010-04-16 23:34:13 +000090 return false;
91}
92
Chris Lattner0eedafe2006-08-24 04:56:27 +000093//===----------------------------------------------------------------------===//
94// Primary Expressions.
95//===----------------------------------------------------------------------===//
96
John McCall6b51f282009-11-23 01:53:49 +000097void ExplicitTemplateArgumentList::initializeFrom(
98 const TemplateArgumentListInfo &Info) {
99 LAngleLoc = Info.getLAngleLoc();
100 RAngleLoc = Info.getRAngleLoc();
101 NumTemplateArgs = Info.size();
102
103 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
104 for (unsigned i = 0; i != NumTemplateArgs; ++i)
105 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
106}
107
108void ExplicitTemplateArgumentList::copyInto(
109 TemplateArgumentListInfo &Info) const {
110 Info.setLAngleLoc(LAngleLoc);
111 Info.setRAngleLoc(RAngleLoc);
112 for (unsigned I = 0; I != NumTemplateArgs; ++I)
113 Info.addArgument(getTemplateArgs()[I]);
114}
115
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000116std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
117 return sizeof(ExplicitTemplateArgumentList) +
118 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
119}
120
John McCall6b51f282009-11-23 01:53:49 +0000121std::size_t ExplicitTemplateArgumentList::sizeFor(
122 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000123 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000124}
125
Douglas Gregored6c7442009-11-23 11:41:28 +0000126void DeclRefExpr::computeDependence() {
127 TypeDependent = false;
128 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000129
Douglas Gregored6c7442009-11-23 11:41:28 +0000130 NamedDecl *D = getDecl();
131
132 // (TD) C++ [temp.dep.expr]p3:
133 // An id-expression is type-dependent if it contains:
134 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000135 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000136 //
137 // (VD) C++ [temp.dep.constexpr]p2:
138 // An identifier is value-dependent if it is:
139
140 // (TD) - an identifier that was declared with dependent type
141 // (VD) - a name declared with a dependent type,
142 if (getType()->isDependentType()) {
143 TypeDependent = true;
144 ValueDependent = true;
145 }
146 // (TD) - a conversion-function-id that specifies a dependent type
Alexis Hunta8136cc2010-05-05 15:23:54 +0000147 else if (D->getDeclName().getNameKind()
Douglas Gregored6c7442009-11-23 11:41:28 +0000148 == DeclarationName::CXXConversionFunctionName &&
149 D->getDeclName().getCXXNameType()->isDependentType()) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (TD) - a template-id that is dependent,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000154 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000155 TemplateSpecializationType::anyDependentTemplateArguments(
Alexis Hunta8136cc2010-05-05 15:23:54 +0000156 getTemplateArgs(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000157 getNumTemplateArgs())) {
158 TypeDependent = true;
159 ValueDependent = true;
160 }
161 // (VD) - the name of a non-type template parameter,
162 else if (isa<NonTypeTemplateParmDecl>(D))
163 ValueDependent = true;
164 // (VD) - a constant with integral or enumeration type and is
165 // initialized with an expression that is value-dependent.
166 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000167 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000168 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000169 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000170 if (Init->isValueDependent())
171 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000172 }
173 // (VD) - FIXME: Missing from the standard:
174 // - a member function or a static data member of the current
175 // instantiation
176 else if (Var->isStaticDataMember() &&
Douglas Gregorbe49fc52010-05-11 08:44:04 +0000177 Var->getDeclContext()->isDependentContext())
Douglas Gregor0e4de762010-05-11 08:41:30 +0000178 ValueDependent = true;
179 }
180 // (VD) - FIXME: Missing from the standard:
181 // - a member function or a static data member of the current
182 // instantiation
183 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
184 ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000185 // (TD) - a nested-name-specifier or a qualified-id that names a
186 // member of an unknown specialization.
187 // (handled by DependentScopeDeclRefExpr)
188}
189
Alexis Hunta8136cc2010-05-05 15:23:54 +0000190DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000191 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000192 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000193 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000194 QualType T)
195 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000196 DecoratedD(D,
197 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000198 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000199 Loc(NameLoc) {
200 if (Qualifier) {
201 NameQualifier *NQ = getNameQualifier();
202 NQ->NNS = Qualifier;
203 NQ->Range = QualifierRange;
204 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000205
John McCall6b51f282009-11-23 01:53:49 +0000206 if (TemplateArgs)
207 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000208
209 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000210}
211
212DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
213 NestedNameSpecifier *Qualifier,
214 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000215 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000216 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000217 QualType T,
218 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000219 std::size_t Size = sizeof(DeclRefExpr);
220 if (Qualifier != 0)
221 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000222
John McCall6b51f282009-11-23 01:53:49 +0000223 if (TemplateArgs)
224 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000225
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000226 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
227 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000228 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000229}
230
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000231DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
232 unsigned NumTemplateArgs) {
233 std::size_t Size = sizeof(DeclRefExpr);
234 if (HasQualifier)
235 Size += sizeof(NameQualifier);
236
237 if (NumTemplateArgs)
238 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
239
240 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
241 return new (Mem) DeclRefExpr(EmptyShell());
242}
243
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000244SourceRange DeclRefExpr::getSourceRange() const {
245 // FIXME: Does not handle multi-token names well, e.g., operator[].
246 SourceRange R(Loc);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000247
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000248 if (hasQualifier())
249 R.setBegin(getQualifierRange().getBegin());
250 if (hasExplicitTemplateArgumentList())
251 R.setEnd(getRAngleLoc());
252 return R;
253}
254
Anders Carlsson2fb08242009-09-08 18:24:21 +0000255// FIXME: Maybe this should use DeclPrinter with a special "print predefined
256// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000257std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
258 ASTContext &Context = CurrentDecl->getASTContext();
259
Anders Carlsson2fb08242009-09-08 18:24:21 +0000260 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000261 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000262 return FD->getNameAsString();
263
264 llvm::SmallString<256> Name;
265 llvm::raw_svector_ostream Out(Name);
266
267 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000268 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000269 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000270 if (MD->isStatic())
271 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000272 }
273
274 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000275
276 std::string Proto = FD->getQualifiedNameAsString(Policy);
277
John McCall9dd450b2009-09-21 23:43:11 +0000278 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000279 const FunctionProtoType *FT = 0;
280 if (FD->hasWrittenPrototype())
281 FT = dyn_cast<FunctionProtoType>(AFT);
282
283 Proto += "(";
284 if (FT) {
285 llvm::raw_string_ostream POut(Proto);
286 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
287 if (i) POut << ", ";
288 std::string Param;
289 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
290 POut << Param;
291 }
292
293 if (FT->isVariadic()) {
294 if (FD->getNumParams()) POut << ", ";
295 POut << "...";
296 }
297 }
298 Proto += ")";
299
Sam Weinig4e83bd22009-12-27 01:38:20 +0000300 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
301 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
302 if (ThisQuals.hasConst())
303 Proto += " const";
304 if (ThisQuals.hasVolatile())
305 Proto += " volatile";
306 }
307
Sam Weinigd060ed42009-12-06 23:55:13 +0000308 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
309 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000310
311 Out << Proto;
312
313 Out.flush();
314 return Name.str().str();
315 }
316 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
317 llvm::SmallString<256> Name;
318 llvm::raw_svector_ostream Out(Name);
319 Out << (MD->isInstanceMethod() ? '-' : '+');
320 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000321
322 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
323 // a null check to avoid a crash.
324 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000325 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000326
Anders Carlsson2fb08242009-09-08 18:24:21 +0000327 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000328 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
329 Out << '(' << CID << ')';
330
Anders Carlsson2fb08242009-09-08 18:24:21 +0000331 Out << ' ';
332 Out << MD->getSelector().getAsString();
333 Out << ']';
334
335 Out.flush();
336 return Name.str().str();
337 }
338 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
339 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
340 return "top level";
341 }
342 return "";
343}
344
Chris Lattnera0173132008-06-07 22:13:43 +0000345/// getValueAsApproximateDouble - This returns the value as an inaccurate
346/// double. Note that this may cause loss of precision, but is useful for
347/// debugging dumps, etc.
348double FloatingLiteral::getValueAsApproximateDouble() const {
349 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000350 bool ignored;
351 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
352 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000353 return V.convertToDouble();
354}
355
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000356StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
357 unsigned ByteLength, bool Wide,
358 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000359 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000360 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000361 // Allocate enough space for the StringLiteral plus an array of locations for
362 // any concatenated string tokens.
363 void *Mem = C.Allocate(sizeof(StringLiteral)+
364 sizeof(SourceLocation)*(NumStrs-1),
365 llvm::alignof<StringLiteral>());
366 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Steve Naroffdf7855b2007-02-21 23:46:25 +0000368 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000369 char *AStrData = new (C, 1) char[ByteLength];
370 memcpy(AStrData, StrData, ByteLength);
371 SL->StrData = AStrData;
372 SL->ByteLength = ByteLength;
373 SL->IsWide = Wide;
374 SL->TokLocs[0] = Loc[0];
375 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000376
Chris Lattner630970d2009-02-18 05:49:11 +0000377 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000378 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
379 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000380}
381
Douglas Gregor958dfc92009-04-15 16:35:07 +0000382StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
383 void *Mem = C.Allocate(sizeof(StringLiteral)+
384 sizeof(SourceLocation)*(NumStrs-1),
385 llvm::alignof<StringLiteral>());
386 StringLiteral *SL = new (Mem) StringLiteral(QualType());
387 SL->StrData = 0;
388 SL->ByteLength = 0;
389 SL->NumConcatenated = NumStrs;
390 return SL;
391}
392
Daniel Dunbar36217882009-09-22 03:27:33 +0000393void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000394 char *AStrData = new (C, 1) char[Str.size()];
395 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000396 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000397 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000398}
399
Chris Lattner1b926492006-08-23 06:42:10 +0000400/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
401/// corresponds to, e.g. "sizeof" or "[pre]++".
402const char *UnaryOperator::getOpcodeStr(Opcode Op) {
403 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000404 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000405 case PostInc: return "++";
406 case PostDec: return "--";
407 case PreInc: return "++";
408 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000409 case AddrOf: return "&";
410 case Deref: return "*";
411 case Plus: return "+";
412 case Minus: return "-";
413 case Not: return "~";
414 case LNot: return "!";
415 case Real: return "__real";
416 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000417 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000418 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000419 }
420}
421
Mike Stump11289f42009-09-09 15:08:12 +0000422UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000423UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
424 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000425 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000426 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
427 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
428 case OO_Amp: return AddrOf;
429 case OO_Star: return Deref;
430 case OO_Plus: return Plus;
431 case OO_Minus: return Minus;
432 case OO_Tilde: return Not;
433 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000434 }
435}
436
437OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
438 switch (Opc) {
439 case PostInc: case PreInc: return OO_PlusPlus;
440 case PostDec: case PreDec: return OO_MinusMinus;
441 case AddrOf: return OO_Amp;
442 case Deref: return OO_Star;
443 case Plus: return OO_Plus;
444 case Minus: return OO_Minus;
445 case Not: return OO_Tilde;
446 case LNot: return OO_Exclaim;
447 default: return OO_None;
448 }
449}
450
451
Chris Lattner0eedafe2006-08-24 04:56:27 +0000452//===----------------------------------------------------------------------===//
453// Postfix Operators.
454//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000455
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000456CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000457 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000458 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000459 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000460 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000461 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000462
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000463 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000464 SubExprs[FN] = fn;
465 for (unsigned i = 0; i != numargs; ++i)
466 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000467
Douglas Gregor993603d2008-11-14 16:09:21 +0000468 RParenLoc = rparenloc;
469}
Nate Begeman1e36a852008-01-17 17:46:27 +0000470
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000471CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
472 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000473 : Expr(CallExprClass, t,
474 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000475 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000476 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000477
478 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000479 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000480 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000481 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000482
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000483 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000484}
485
Mike Stump11289f42009-09-09 15:08:12 +0000486CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
487 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000488 SubExprs = new (C) Stmt*[1];
489}
490
Nuno Lopes518e3702009-12-20 23:11:08 +0000491Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000492 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000493 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000494 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000495 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
496 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000497
498 return 0;
499}
500
Nuno Lopes518e3702009-12-20 23:11:08 +0000501FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000502 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000503}
504
Chris Lattnere4407ed2007-12-28 05:25:02 +0000505/// setNumArgs - This changes the number of arguments present in this call.
506/// Any orphaned expressions are deleted by this, and any new operands are set
507/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000508void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000509 // No change, just return.
510 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000511
Chris Lattnere4407ed2007-12-28 05:25:02 +0000512 // If shrinking # arguments, just delete the extras and forgot them.
513 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000514 this->NumArgs = NumArgs;
515 return;
516 }
517
518 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000519 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000520 // Copy over args.
521 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
522 NewSubExprs[i] = SubExprs[i];
523 // Null out new args.
524 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
525 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Douglas Gregorba6e5572009-04-17 21:46:47 +0000527 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000528 SubExprs = NewSubExprs;
529 this->NumArgs = NumArgs;
530}
531
Chris Lattner01ff98a2008-10-06 05:00:53 +0000532/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
533/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000534unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000535 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000536 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000537 // ImplicitCastExpr.
538 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
539 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000540 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Steve Narofff6e3b3292008-01-31 01:07:12 +0000542 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
543 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000544 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000546 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
547 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000548 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000550 if (!FDecl->getIdentifier())
551 return 0;
552
Douglas Gregor15fc9562009-09-12 00:22:50 +0000553 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000554}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000555
Anders Carlsson00a27592009-05-26 04:57:27 +0000556QualType CallExpr::getCallReturnType() const {
557 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000558 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000559 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000560 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000561 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000562 else if (const MemberPointerType *MPT
563 = CalleeType->getAs<MemberPointerType>())
564 CalleeType = MPT->getPointeeType();
565
John McCall9dd450b2009-09-21 23:43:11 +0000566 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000567 return FnType->getResultType();
568}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000569
Alexis Hunta8136cc2010-05-05 15:23:54 +0000570OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000571 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000572 TypeSourceInfo *tsi,
573 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000574 Expr** exprsPtr, unsigned numExprs,
575 SourceLocation RParenLoc) {
576 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000577 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000578 sizeof(Expr*) * numExprs);
579
580 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
581 exprsPtr, numExprs, RParenLoc);
582}
583
584OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
585 unsigned numComps, unsigned numExprs) {
586 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
587 sizeof(OffsetOfNode) * numComps +
588 sizeof(Expr*) * numExprs);
589 return new (Mem) OffsetOfExpr(numComps, numExprs);
590}
591
Alexis Hunta8136cc2010-05-05 15:23:54 +0000592OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000593 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000594 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000595 Expr** exprsPtr, unsigned numExprs,
596 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000597 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000598 /*ValueDependent=*/tsi->getType()->isDependentType() ||
599 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
600 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000601 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
602 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000603{
604 for(unsigned i = 0; i < numComps; ++i) {
605 setComponent(i, compsPtr[i]);
606 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000607
Douglas Gregor882211c2010-04-28 22:16:22 +0000608 for(unsigned i = 0; i < numExprs; ++i) {
609 setIndexExpr(i, exprsPtr[i]);
610 }
611}
612
613IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
614 assert(getKind() == Field || getKind() == Identifier);
615 if (getKind() == Field)
616 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000617
Douglas Gregor882211c2010-04-28 22:16:22 +0000618 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
619}
620
Mike Stump11289f42009-09-09 15:08:12 +0000621MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
622 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000623 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000624 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000625 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000626 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000627 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000628 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000629 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000630
John McCalla8ae2222010-04-06 21:38:20 +0000631 bool hasQualOrFound = (qual != 0 ||
632 founddecl.getDecl() != memberdecl ||
633 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000634 if (hasQualOrFound)
635 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000636
John McCall6b51f282009-11-23 01:53:49 +0000637 if (targs)
638 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000639
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000640 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000641 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
642
643 if (hasQualOrFound) {
644 if (qual && qual->isDependent()) {
645 E->setValueDependent(true);
646 E->setTypeDependent(true);
647 }
648 E->HasQualifierOrFoundDecl = true;
649
650 MemberNameQualifier *NQ = E->getMemberQualifier();
651 NQ->NNS = qual;
652 NQ->Range = qualrange;
653 NQ->FoundDecl = founddecl;
654 }
655
656 if (targs) {
657 E->HasExplicitTemplateArgumentList = true;
658 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
659 }
660
661 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000662}
663
Anders Carlsson496335e2009-09-03 00:59:21 +0000664const char *CastExpr::getCastKindName() const {
665 switch (getCastKind()) {
666 case CastExpr::CK_Unknown:
667 return "Unknown";
668 case CastExpr::CK_BitCast:
669 return "BitCast";
Douglas Gregor51954272010-07-13 23:17:26 +0000670 case CastExpr::CK_LValueBitCast:
671 return "LValueBitCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000672 case CastExpr::CK_NoOp:
673 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000674 case CastExpr::CK_BaseToDerived:
675 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000676 case CastExpr::CK_DerivedToBase:
677 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000678 case CastExpr::CK_UncheckedDerivedToBase:
679 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000680 case CastExpr::CK_Dynamic:
681 return "Dynamic";
682 case CastExpr::CK_ToUnion:
683 return "ToUnion";
684 case CastExpr::CK_ArrayToPointerDecay:
685 return "ArrayToPointerDecay";
686 case CastExpr::CK_FunctionToPointerDecay:
687 return "FunctionToPointerDecay";
688 case CastExpr::CK_NullToMemberPointer:
689 return "NullToMemberPointer";
690 case CastExpr::CK_BaseToDerivedMemberPointer:
691 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000692 case CastExpr::CK_DerivedToBaseMemberPointer:
693 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000694 case CastExpr::CK_UserDefinedConversion:
695 return "UserDefinedConversion";
696 case CastExpr::CK_ConstructorConversion:
697 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000698 case CastExpr::CK_IntegralToPointer:
699 return "IntegralToPointer";
700 case CastExpr::CK_PointerToIntegral:
701 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000702 case CastExpr::CK_ToVoid:
703 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000704 case CastExpr::CK_VectorSplat:
705 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000706 case CastExpr::CK_IntegralCast:
707 return "IntegralCast";
708 case CastExpr::CK_IntegralToFloating:
709 return "IntegralToFloating";
710 case CastExpr::CK_FloatingToIntegral:
711 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000712 case CastExpr::CK_FloatingCast:
713 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000714 case CastExpr::CK_MemberPointerToBoolean:
715 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000716 case CastExpr::CK_AnyPointerToObjCPointerCast:
717 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000718 case CastExpr::CK_AnyPointerToBlockPointerCast:
719 return "AnyPointerToBlockPointerCast";
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +0000720 case CastExpr::CK_ObjCObjectLValueCast:
721 return "ObjCObjectLValueCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Anders Carlsson496335e2009-09-03 00:59:21 +0000724 assert(0 && "Unhandled cast kind!");
725 return 0;
726}
727
Douglas Gregord196a582009-12-14 19:27:10 +0000728Expr *CastExpr::getSubExprAsWritten() {
729 Expr *SubExpr = 0;
730 CastExpr *E = this;
731 do {
732 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000733
Douglas Gregord196a582009-12-14 19:27:10 +0000734 // Skip any temporary bindings; they're implicit.
735 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
736 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000737
Douglas Gregord196a582009-12-14 19:27:10 +0000738 // Conversions by constructor and conversion functions have a
739 // subexpression describing the call; strip it off.
740 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
741 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
742 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
743 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000744
Douglas Gregord196a582009-12-14 19:27:10 +0000745 // If the subexpression we're left with is an implicit cast, look
746 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000747 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
748
Douglas Gregord196a582009-12-14 19:27:10 +0000749 return SubExpr;
750}
751
John McCallcf142162010-08-07 06:22:56 +0000752CXXBaseSpecifier **CastExpr::path_buffer() {
753 switch (getStmtClass()) {
754#define ABSTRACT_STMT(x)
755#define CASTEXPR(Type, Base) \
756 case Stmt::Type##Class: \
757 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
758#define STMT(Type, Base)
759#include "clang/AST/StmtNodes.inc"
760 default:
761 llvm_unreachable("non-cast expressions not possible here");
762 return 0;
763 }
764}
765
766void CastExpr::setCastPath(const CXXCastPath &Path) {
767 assert(Path.size() == path_size());
768 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
769}
770
771ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
772 CastKind Kind, Expr *Operand,
773 const CXXCastPath *BasePath,
774 ResultCategory Cat) {
775 unsigned PathSize = (BasePath ? BasePath->size() : 0);
776 void *Buffer =
777 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
778 ImplicitCastExpr *E =
779 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, Cat);
780 if (PathSize) E->setCastPath(*BasePath);
781 return E;
782}
783
784ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
785 unsigned PathSize) {
786 void *Buffer =
787 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
788 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
789}
790
791
792CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
793 CastKind K, Expr *Op,
794 const CXXCastPath *BasePath,
795 TypeSourceInfo *WrittenTy,
796 SourceLocation L, SourceLocation R) {
797 unsigned PathSize = (BasePath ? BasePath->size() : 0);
798 void *Buffer =
799 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
800 CStyleCastExpr *E =
801 new (Buffer) CStyleCastExpr(T, K, Op, PathSize, WrittenTy, L, R);
802 if (PathSize) E->setCastPath(*BasePath);
803 return E;
804}
805
806CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
807 void *Buffer =
808 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
809 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
810}
811
Chris Lattner1b926492006-08-23 06:42:10 +0000812/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
813/// corresponds to, e.g. "<<=".
814const char *BinaryOperator::getOpcodeStr(Opcode Op) {
815 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000816 case PtrMemD: return ".*";
817 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000818 case Mul: return "*";
819 case Div: return "/";
820 case Rem: return "%";
821 case Add: return "+";
822 case Sub: return "-";
823 case Shl: return "<<";
824 case Shr: return ">>";
825 case LT: return "<";
826 case GT: return ">";
827 case LE: return "<=";
828 case GE: return ">=";
829 case EQ: return "==";
830 case NE: return "!=";
831 case And: return "&";
832 case Xor: return "^";
833 case Or: return "|";
834 case LAnd: return "&&";
835 case LOr: return "||";
836 case Assign: return "=";
837 case MulAssign: return "*=";
838 case DivAssign: return "/=";
839 case RemAssign: return "%=";
840 case AddAssign: return "+=";
841 case SubAssign: return "-=";
842 case ShlAssign: return "<<=";
843 case ShrAssign: return ">>=";
844 case AndAssign: return "&=";
845 case XorAssign: return "^=";
846 case OrAssign: return "|=";
847 case Comma: return ",";
848 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000849
850 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000851}
Steve Naroff47500512007-04-19 23:00:49 +0000852
Mike Stump11289f42009-09-09 15:08:12 +0000853BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000854BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
855 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000856 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000857 case OO_Plus: return Add;
858 case OO_Minus: return Sub;
859 case OO_Star: return Mul;
860 case OO_Slash: return Div;
861 case OO_Percent: return Rem;
862 case OO_Caret: return Xor;
863 case OO_Amp: return And;
864 case OO_Pipe: return Or;
865 case OO_Equal: return Assign;
866 case OO_Less: return LT;
867 case OO_Greater: return GT;
868 case OO_PlusEqual: return AddAssign;
869 case OO_MinusEqual: return SubAssign;
870 case OO_StarEqual: return MulAssign;
871 case OO_SlashEqual: return DivAssign;
872 case OO_PercentEqual: return RemAssign;
873 case OO_CaretEqual: return XorAssign;
874 case OO_AmpEqual: return AndAssign;
875 case OO_PipeEqual: return OrAssign;
876 case OO_LessLess: return Shl;
877 case OO_GreaterGreater: return Shr;
878 case OO_LessLessEqual: return ShlAssign;
879 case OO_GreaterGreaterEqual: return ShrAssign;
880 case OO_EqualEqual: return EQ;
881 case OO_ExclaimEqual: return NE;
882 case OO_LessEqual: return LE;
883 case OO_GreaterEqual: return GE;
884 case OO_AmpAmp: return LAnd;
885 case OO_PipePipe: return LOr;
886 case OO_Comma: return Comma;
887 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000888 }
889}
890
891OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
892 static const OverloadedOperatorKind OverOps[] = {
893 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
894 OO_Star, OO_Slash, OO_Percent,
895 OO_Plus, OO_Minus,
896 OO_LessLess, OO_GreaterGreater,
897 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
898 OO_EqualEqual, OO_ExclaimEqual,
899 OO_Amp,
900 OO_Caret,
901 OO_Pipe,
902 OO_AmpAmp,
903 OO_PipePipe,
904 OO_Equal, OO_StarEqual,
905 OO_SlashEqual, OO_PercentEqual,
906 OO_PlusEqual, OO_MinusEqual,
907 OO_LessLessEqual, OO_GreaterGreaterEqual,
908 OO_AmpEqual, OO_CaretEqual,
909 OO_PipeEqual,
910 OO_Comma
911 };
912 return OverOps[Opc];
913}
914
Ted Kremenekac034612010-04-13 23:39:13 +0000915InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000916 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000917 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000918 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000919 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000920 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000921 UnionFieldInit(0), HadArrayRangeDesignator(false)
922{
Ted Kremenek013041e2010-02-19 01:50:18 +0000923 for (unsigned I = 0; I != numInits; ++I) {
924 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000925 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000926 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000927 ValueDependent = true;
928 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000929
Ted Kremenekac034612010-04-13 23:39:13 +0000930 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000931}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000932
Ted Kremenekac034612010-04-13 23:39:13 +0000933void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000934 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000935 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000936}
937
Ted Kremenekac034612010-04-13 23:39:13 +0000938void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +0000939 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000940}
941
Ted Kremenekac034612010-04-13 23:39:13 +0000942Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000943 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000944 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000945 InitExprs.back() = expr;
946 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000949 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
950 InitExprs[Init] = expr;
951 return Result;
952}
953
Steve Naroff991e99d2008-09-04 15:31:07 +0000954/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000955///
956const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000957 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000958 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000959}
960
Mike Stump11289f42009-09-09 15:08:12 +0000961SourceLocation BlockExpr::getCaretLocation() const {
962 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000963}
Mike Stump11289f42009-09-09 15:08:12 +0000964const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000965 return TheBlock->getBody();
966}
Mike Stump11289f42009-09-09 15:08:12 +0000967Stmt *BlockExpr::getBody() {
968 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000969}
Steve Naroff415d3d52008-10-08 17:01:13 +0000970
971
Chris Lattner1ec5f562007-06-27 05:38:08 +0000972//===----------------------------------------------------------------------===//
973// Generic Expression Routines
974//===----------------------------------------------------------------------===//
975
Chris Lattner237f2752009-02-14 07:37:35 +0000976/// isUnusedResultAWarning - Return true if this immediate expression should
977/// be warned about if the result is unused. If so, fill in Loc and Ranges
978/// with location to warn on and the source range[s] to report with the
979/// warning.
980bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000981 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000982 // Don't warn if the expr is type dependent. The type could end up
983 // instantiating to void.
984 if (isTypeDependent())
985 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000986
Chris Lattner1ec5f562007-06-27 05:38:08 +0000987 switch (getStmtClass()) {
988 default:
John McCallc493a732010-03-12 07:11:26 +0000989 if (getType()->isVoidType())
990 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000991 Loc = getExprLoc();
992 R1 = getSourceRange();
993 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000994 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000995 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000996 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000997 case UnaryOperatorClass: {
998 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattner1ec5f562007-06-27 05:38:08 +00001000 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001001 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001002 case UnaryOperator::PostInc:
1003 case UnaryOperator::PostDec:
1004 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +00001005 case UnaryOperator::PreDec: // ++/--
1006 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +00001007 case UnaryOperator::Deref:
1008 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001009 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001010 return false;
1011 break;
Chris Lattnera44d1162007-06-27 05:58:59 +00001012 case UnaryOperator::Real:
1013 case UnaryOperator::Imag:
1014 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001015 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1016 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001017 return false;
1018 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001019 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001020 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001021 }
Chris Lattner237f2752009-02-14 07:37:35 +00001022 Loc = UO->getOperatorLoc();
1023 R1 = UO->getSubExpr()->getSourceRange();
1024 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001025 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001026 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001027 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001028 switch (BO->getOpcode()) {
1029 default:
1030 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001031 // Consider the RHS of comma for side effects. LHS was checked by
1032 // Sema::CheckCommaOperands.
Ted Kremenek43a9c962010-04-07 18:49:21 +00001033 case BinaryOperator::Comma:
1034 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1035 // lvalue-ness) of an assignment written in a macro.
1036 if (IntegerLiteral *IE =
1037 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1038 if (IE->getValue() == 0)
1039 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001040 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1041 // Consider '||', '&&' to have side effects if the LHS or RHS does.
Ted Kremenek43a9c962010-04-07 18:49:21 +00001042 case BinaryOperator::LAnd:
1043 case BinaryOperator::LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001044 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1045 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1046 return false;
1047 break;
John McCall1e3715a2010-02-16 04:10:53 +00001048 }
Chris Lattner237f2752009-02-14 07:37:35 +00001049 if (BO->isAssignmentOp())
1050 return false;
1051 Loc = BO->getOperatorLoc();
1052 R1 = BO->getLHS()->getSourceRange();
1053 R2 = BO->getRHS()->getSourceRange();
1054 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001055 }
Chris Lattner86928112007-08-25 02:00:02 +00001056 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001057 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001058 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001059
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001060 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001061 // The condition must be evaluated, but if either the LHS or RHS is a
1062 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001063 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001064 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001065 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001066 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001067 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001068 }
1069
Chris Lattnera44d1162007-06-27 05:58:59 +00001070 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001071 // If the base pointer or element is to a volatile pointer/field, accessing
1072 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001073 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001074 return false;
1075 Loc = cast<MemberExpr>(this)->getMemberLoc();
1076 R1 = SourceRange(Loc, Loc);
1077 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1078 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattner1ec5f562007-06-27 05:38:08 +00001080 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001081 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001082 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001083 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001084 return false;
1085 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1086 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1087 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1088 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001089
Chris Lattner1ec5f562007-06-27 05:38:08 +00001090 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001091 case CXXOperatorCallExprClass:
1092 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001093 // If this is a direct call, get the callee.
1094 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001095 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001096 // If the callee has attribute pure, const, or warn_unused_result, warn
1097 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001098 //
1099 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1100 // updated to match for QoI.
1101 if (FD->getAttr<WarnUnusedResultAttr>() ||
1102 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1103 Loc = CE->getCallee()->getLocStart();
1104 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner1a6babf2009-10-13 04:53:48 +00001106 if (unsigned NumArgs = CE->getNumArgs())
1107 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1108 CE->getArg(NumArgs-1)->getLocEnd());
1109 return true;
1110 }
Chris Lattner237f2752009-02-14 07:37:35 +00001111 }
1112 return false;
1113 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001114
1115 case CXXTemporaryObjectExprClass:
1116 case CXXConstructExprClass:
1117 return false;
1118
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001119 case ObjCMessageExprClass: {
1120 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1121 const ObjCMethodDecl *MD = ME->getMethodDecl();
1122 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1123 Loc = getExprLoc();
1124 return true;
1125 }
Chris Lattner237f2752009-02-14 07:37:35 +00001126 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001129 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001130#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001131 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001132 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001133 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001134 Loc = Ref->getLocation();
1135 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1136 if (Ref->getBase())
1137 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001138#else
1139 Loc = getExprLoc();
1140 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001141#endif
1142 return true;
1143 }
Chris Lattner944d3062008-07-26 19:51:01 +00001144 case StmtExprClass: {
1145 // Statement exprs don't logically have side effects themselves, but are
1146 // sometimes used in macros in ways that give them a type that is unused.
1147 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1148 // however, if the result of the stmt expr is dead, we don't want to emit a
1149 // warning.
1150 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1151 if (!CS->body_empty())
1152 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001153 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001154
John McCallc493a732010-03-12 07:11:26 +00001155 if (getType()->isVoidType())
1156 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001157 Loc = cast<StmtExpr>(this)->getLParenLoc();
1158 R1 = getSourceRange();
1159 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001160 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001161 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001162 // If this is an explicit cast to void, allow it. People do this when they
1163 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001164 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001165 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001166 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1167 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1168 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001169 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001170 if (getType()->isVoidType())
1171 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001172 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001173
Anders Carlsson6aa50392009-11-17 17:11:23 +00001174 // If this is a cast to void or a constructor conversion, check the operand.
1175 // Otherwise, the result of the cast is unused.
1176 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1177 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001178 return (cast<CastExpr>(this)->getSubExpr()
1179 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001180 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1181 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1182 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001183 }
Mike Stump11289f42009-09-09 15:08:12 +00001184
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001185 case ImplicitCastExprClass:
1186 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001187 return (cast<ImplicitCastExpr>(this)
1188 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001189
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001190 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001191 return (cast<CXXDefaultArgExpr>(this)
1192 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001193
1194 case CXXNewExprClass:
1195 // FIXME: In theory, there might be new expressions that don't have side
1196 // effects (e.g. a placement new with an uninitialized POD).
1197 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001198 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001199 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001200 return (cast<CXXBindTemporaryExpr>(this)
1201 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001202 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001203 return (cast<CXXExprWithTemporaries>(this)
1204 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001205 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001206}
1207
Fariborz Jahanian07735332009-02-22 18:40:18 +00001208/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001209/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001210bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001211 switch (getStmtClass()) {
1212 default:
1213 return false;
1214 case ObjCIvarRefExprClass:
1215 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001216 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001217 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001218 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001219 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001220 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001221 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001222 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001223 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001224 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001225 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001226 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1227 if (VD->hasGlobalStorage())
1228 return true;
1229 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001230 // dereferencing to a pointer is always a gc'able candidate,
1231 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001232 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001233 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001234 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001235 return false;
1236 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001237 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001238 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001239 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001240 }
1241 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001242 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001243 }
1244}
Ted Kremenekfff70962008-01-17 16:57:34 +00001245Expr* Expr::IgnoreParens() {
1246 Expr* E = this;
1247 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1248 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001249
Ted Kremenekfff70962008-01-17 16:57:34 +00001250 return E;
1251}
1252
Chris Lattnerf2660962008-02-13 01:02:39 +00001253/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1254/// or CastExprs or ImplicitCastExprs, returning their operand.
1255Expr *Expr::IgnoreParenCasts() {
1256 Expr *E = this;
1257 while (true) {
1258 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1259 E = P->getSubExpr();
1260 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1261 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001262 else
1263 return E;
1264 }
1265}
1266
John McCalleebc8322010-05-05 22:59:52 +00001267Expr *Expr::IgnoreParenImpCasts() {
1268 Expr *E = this;
1269 while (true) {
1270 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1271 E = P->getSubExpr();
1272 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1273 E = P->getSubExpr();
1274 else
1275 return E;
1276 }
1277}
1278
Chris Lattneref26c772009-03-13 17:28:01 +00001279/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1280/// value (including ptr->int casts of the same size). Strip off any
1281/// ParenExpr or CastExprs, returning their operand.
1282Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1283 Expr *E = this;
1284 while (true) {
1285 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1286 E = P->getSubExpr();
1287 continue;
1288 }
Mike Stump11289f42009-09-09 15:08:12 +00001289
Chris Lattneref26c772009-03-13 17:28:01 +00001290 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1291 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001292 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001293 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001294
Chris Lattneref26c772009-03-13 17:28:01 +00001295 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1296 E = SE;
1297 continue;
1298 }
Mike Stump11289f42009-09-09 15:08:12 +00001299
Douglas Gregor6972a622010-06-16 00:35:25 +00001300 if ((E->getType()->isPointerType() ||
1301 E->getType()->isIntegralType(Ctx)) &&
1302 (SE->getType()->isPointerType() ||
1303 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001304 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1305 E = SE;
1306 continue;
1307 }
1308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Chris Lattneref26c772009-03-13 17:28:01 +00001310 return E;
1311 }
1312}
1313
Douglas Gregord196a582009-12-14 19:27:10 +00001314bool Expr::isDefaultArgument() const {
1315 const Expr *E = this;
1316 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1317 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001318
Douglas Gregord196a582009-12-14 19:27:10 +00001319 return isa<CXXDefaultArgExpr>(E);
1320}
Chris Lattneref26c772009-03-13 17:28:01 +00001321
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001322/// \brief Skip over any no-op casts and any temporary-binding
1323/// expressions.
1324static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1325 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1326 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1327 E = ICE->getSubExpr();
1328 else
1329 break;
1330 }
1331
1332 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1333 E = BE->getSubExpr();
1334
1335 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1336 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1337 E = ICE->getSubExpr();
1338 else
1339 break;
1340 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001341
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001342 return E;
1343}
1344
1345const Expr *Expr::getTemporaryObject() const {
1346 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1347
1348 // A cast can produce a temporary object. The object's construction
1349 // is represented as a CXXConstructExpr.
1350 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1351 // Only user-defined and constructor conversions can produce
1352 // temporary objects.
1353 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1354 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1355 return 0;
1356
1357 // Strip off temporary bindings and no-op casts.
1358 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1359
1360 // If this is a constructor conversion, see if we have an object
1361 // construction.
1362 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1363 return dyn_cast<CXXConstructExpr>(Sub);
1364
1365 // If this is a user-defined conversion, see if we have a call to
1366 // a function that itself returns a temporary object.
1367 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1368 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1369 if (CE->getCallReturnType()->isRecordType())
1370 return CE;
1371
1372 return 0;
1373 }
1374
1375 // A call returning a class type returns a temporary.
1376 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1377 if (CE->getCallReturnType()->isRecordType())
1378 return CE;
1379
1380 return 0;
1381 }
1382
1383 // Explicit temporary object constructors create temporaries.
1384 return dyn_cast<CXXTemporaryObjectExpr>(E);
1385}
1386
Douglas Gregor4619e432008-12-05 23:32:09 +00001387/// hasAnyTypeDependentArguments - Determines if any of the expressions
1388/// in Exprs is type-dependent.
1389bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1390 for (unsigned I = 0; I < NumExprs; ++I)
1391 if (Exprs[I]->isTypeDependent())
1392 return true;
1393
1394 return false;
1395}
1396
1397/// hasAnyValueDependentArguments - Determines if any of the expressions
1398/// in Exprs is value-dependent.
1399bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1400 for (unsigned I = 0; I < NumExprs; ++I)
1401 if (Exprs[I]->isValueDependent())
1402 return true;
1403
1404 return false;
1405}
1406
John McCall8b0f4ff2010-08-02 21:13:48 +00001407bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00001408 // This function is attempting whether an expression is an initializer
1409 // which can be evaluated at compile-time. isEvaluatable handles most
1410 // of the cases, but it can't deal with some initializer-specific
1411 // expressions, and it can't deal with aggregates; we deal with those here,
1412 // and fall back to isEvaluatable for the other cases.
1413
John McCall8b0f4ff2010-08-02 21:13:48 +00001414 // If we ever capture reference-binding directly in the AST, we can
1415 // kill the second parameter.
1416
1417 if (IsForRef) {
1418 EvalResult Result;
1419 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1420 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001421
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001422 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001423 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001424 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001425 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001426 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001427 return true;
John McCall81c9cea2010-08-01 21:51:45 +00001428 case CXXTemporaryObjectExprClass:
1429 case CXXConstructExprClass: {
1430 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00001431
1432 // Only if it's
1433 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00001434 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00001435 if (!CE->getNumArgs()) return true;
1436
1437 // 2) an elidable trivial copy construction of an operand which is
1438 // itself a constant initializer. Note that we consider the
1439 // operand on its own, *not* as a reference binding.
1440 return CE->isElidable() &&
1441 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00001442 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001443 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001444 // This handles gcc's extension that allows global initializers like
1445 // "struct x {int x;} x = (struct x) {};".
1446 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001447 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00001448 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001449 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001450 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001451 // FIXME: This doesn't deal with fields with reference types correctly.
1452 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1453 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001454 const InitListExpr *Exp = cast<InitListExpr>(this);
1455 unsigned numInits = Exp->getNumInits();
1456 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00001457 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001458 return false;
1459 }
Eli Friedman384da272009-01-25 03:12:18 +00001460 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001461 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001462 case ImplicitValueInitExprClass:
1463 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001464 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00001465 return cast<ParenExpr>(this)->getSubExpr()
1466 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00001467 case UnaryOperatorClass: {
1468 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1469 if (Exp->getOpcode() == UnaryOperator::Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00001470 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00001471 break;
1472 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001473 case BinaryOperatorClass: {
1474 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1475 // but this handles the common case.
1476 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1477 if (Exp->getOpcode() == BinaryOperator::Sub &&
1478 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1479 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1480 return true;
1481 break;
1482 }
John McCall8b0f4ff2010-08-02 21:13:48 +00001483 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00001484 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00001485 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001486 case CStyleCastExprClass:
1487 // Handle casts with a destination that's a struct or union; this
1488 // deals with both the gcc no-op struct cast extension and the
1489 // cast-to-union extension.
1490 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001491 return cast<CastExpr>(this)->getSubExpr()
1492 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001493
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001494 // Integer->integer casts can be handled here, which is important for
1495 // things like (int)(&&x-&&y). Scary but true.
1496 if (getType()->isIntegerType() &&
1497 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001498 return cast<CastExpr>(this)->getSubExpr()
1499 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001500
Eli Friedman384da272009-01-25 03:12:18 +00001501 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001502 }
Eli Friedman384da272009-01-25 03:12:18 +00001503 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001504}
1505
Chris Lattner7eef9192007-05-24 01:23:49 +00001506/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1507/// integer constant expression with the value zero, or if this is one that is
1508/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001509bool Expr::isNullPointerConstant(ASTContext &Ctx,
1510 NullPointerConstantValueDependence NPC) const {
1511 if (isValueDependent()) {
1512 switch (NPC) {
1513 case NPC_NeverValueDependent:
1514 assert(false && "Unexpected value dependent expression!");
1515 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001516
Douglas Gregor56751b52009-09-25 04:25:58 +00001517 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001518 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001519
Douglas Gregor56751b52009-09-25 04:25:58 +00001520 case NPC_ValueDependentIsNotNull:
1521 return false;
1522 }
1523 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001524
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001525 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001526 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001527 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001528 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001529 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001530 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001531 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001532 Pointee->isVoidType() && // to void*
1533 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001534 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001535 }
Steve Naroffada7d422007-05-20 17:54:12 +00001536 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001537 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1538 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001539 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001540 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1541 // Accept ((void*)0) as a null pointer constant, as many other
1542 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001543 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001544 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001545 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001546 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001547 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001548 } else if (isa<GNUNullExpr>(this)) {
1549 // The GNU __null extension is always a null pointer constant.
1550 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001551 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001552
Sebastian Redl576fd422009-05-10 18:38:11 +00001553 // C++0x nullptr_t is always a null pointer constant.
1554 if (getType()->isNullPtrType())
1555 return true;
1556
Steve Naroff4871fe02008-01-14 16:10:57 +00001557 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001558 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001559 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001560 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001561
Chris Lattner1abbd412007-06-08 17:58:43 +00001562 // If we have an integer constant expression, we need to *evaluate* it and
1563 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001564 llvm::APSInt Result;
1565 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001566}
Steve Narofff7a5da12007-07-28 23:10:27 +00001567
Douglas Gregor71235ec2009-05-02 02:18:30 +00001568FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001569 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001570
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001571 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redlc57d34b2010-07-20 04:20:21 +00001572 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1573 ICE->getCastKind() == CastExpr::CK_NoOp)
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001574 E = ICE->getSubExpr()->IgnoreParens();
1575 else
1576 break;
1577 }
1578
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001579 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001580 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001581 if (Field->isBitField())
1582 return Field;
1583
1584 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1585 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1586 return BinOp->getLHS()->getBitField();
1587
1588 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001589}
1590
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001591bool Expr::refersToVectorElement() const {
1592 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001593
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001594 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
Sebastian Redlc57d34b2010-07-20 04:20:21 +00001595 if (ICE->getCategory() != ImplicitCastExpr::RValue &&
1596 ICE->getCastKind() == CastExpr::CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001597 E = ICE->getSubExpr()->IgnoreParens();
1598 else
1599 break;
1600 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001601
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001602 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1603 return ASE->getBase()->getType()->isVectorType();
1604
1605 if (isa<ExtVectorElementExpr>(E))
1606 return true;
1607
1608 return false;
1609}
1610
Chris Lattnerb8211f62009-02-16 22:14:05 +00001611/// isArrow - Return true if the base expression is a pointer to vector,
1612/// return false if the base expression is a vector.
1613bool ExtVectorElementExpr::isArrow() const {
1614 return getBase()->getType()->isPointerType();
1615}
1616
Nate Begemance4d7fc2008-04-18 23:10:10 +00001617unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001618 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001619 return VT->getNumElements();
1620 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001621}
1622
Nate Begemanf322eab2008-05-09 06:41:27 +00001623/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001624bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001625 // FIXME: Refactor this code to an accessor on the AST node which returns the
1626 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001627 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001628
1629 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001630 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001631 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001632
Nate Begeman7e5185b2009-01-18 02:01:21 +00001633 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001634 if (Comp[0] == 's' || Comp[0] == 'S')
1635 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001636
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001637 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1638 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001639 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001640
Steve Naroff0d595ca2007-07-30 03:29:09 +00001641 return false;
1642}
Chris Lattner885b4952007-08-02 23:36:59 +00001643
Nate Begemanf322eab2008-05-09 06:41:27 +00001644/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001645void ExtVectorElementExpr::getEncodedElementAccess(
1646 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001647 llvm::StringRef Comp = Accessor->getName();
1648 if (Comp[0] == 's' || Comp[0] == 'S')
1649 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001650
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001651 bool isHi = Comp == "hi";
1652 bool isLo = Comp == "lo";
1653 bool isEven = Comp == "even";
1654 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001655
Nate Begemanf322eab2008-05-09 06:41:27 +00001656 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1657 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001658
Nate Begemanf322eab2008-05-09 06:41:27 +00001659 if (isHi)
1660 Index = e + i;
1661 else if (isLo)
1662 Index = i;
1663 else if (isEven)
1664 Index = 2 * i;
1665 else if (isOdd)
1666 Index = 2 * i + 1;
1667 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001668 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001669
Nate Begemand3862152008-05-13 21:03:02 +00001670 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001671 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001672}
1673
Douglas Gregor9a129192010-04-21 00:45:42 +00001674ObjCMessageExpr::ObjCMessageExpr(QualType T,
1675 SourceLocation LBracLoc,
1676 SourceLocation SuperLoc,
1677 bool IsInstanceSuper,
1678 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001679 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001680 ObjCMethodDecl *Method,
1681 Expr **Args, unsigned NumArgs,
1682 SourceLocation RBracLoc)
1683 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001684 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001685 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1686 HasMethod(Method != 0), SuperLoc(SuperLoc),
1687 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1688 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001689 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001690{
Douglas Gregor9a129192010-04-21 00:45:42 +00001691 setReceiverPointer(SuperType.getAsOpaquePtr());
1692 if (NumArgs)
1693 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001694}
1695
Douglas Gregor9a129192010-04-21 00:45:42 +00001696ObjCMessageExpr::ObjCMessageExpr(QualType T,
1697 SourceLocation LBracLoc,
1698 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001699 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001700 ObjCMethodDecl *Method,
1701 Expr **Args, unsigned NumArgs,
1702 SourceLocation RBracLoc)
1703 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001704 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001705 hasAnyValueDependentArguments(Args, NumArgs))),
1706 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1707 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1708 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001709 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001710{
1711 setReceiverPointer(Receiver);
1712 if (NumArgs)
1713 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001714}
1715
Douglas Gregor9a129192010-04-21 00:45:42 +00001716ObjCMessageExpr::ObjCMessageExpr(QualType T,
1717 SourceLocation LBracLoc,
1718 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001719 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001720 ObjCMethodDecl *Method,
1721 Expr **Args, unsigned NumArgs,
1722 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001723 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001724 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001725 hasAnyValueDependentArguments(Args, NumArgs))),
1726 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1727 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1728 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001729 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001730{
1731 setReceiverPointer(Receiver);
1732 if (NumArgs)
1733 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001734}
1735
Douglas Gregor9a129192010-04-21 00:45:42 +00001736ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1737 SourceLocation LBracLoc,
1738 SourceLocation SuperLoc,
1739 bool IsInstanceSuper,
1740 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001741 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001742 ObjCMethodDecl *Method,
1743 Expr **Args, unsigned NumArgs,
1744 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001745 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001746 NumArgs * sizeof(Expr *);
1747 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1748 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001749 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00001750 RBracLoc);
1751}
1752
1753ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1754 SourceLocation LBracLoc,
1755 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001756 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001757 ObjCMethodDecl *Method,
1758 Expr **Args, unsigned NumArgs,
1759 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001760 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001761 NumArgs * sizeof(Expr *);
1762 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001763 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001764 NumArgs, RBracLoc);
1765}
1766
1767ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1768 SourceLocation LBracLoc,
1769 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001770 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001771 ObjCMethodDecl *Method,
1772 Expr **Args, unsigned NumArgs,
1773 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001774 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001775 NumArgs * sizeof(Expr *);
1776 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001777 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001778 NumArgs, RBracLoc);
1779}
1780
Alexis Hunta8136cc2010-05-05 15:23:54 +00001781ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00001782 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001783 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001784 NumArgs * sizeof(Expr *);
1785 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1786 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1787}
Alexis Hunta8136cc2010-05-05 15:23:54 +00001788
Douglas Gregor9a129192010-04-21 00:45:42 +00001789Selector ObjCMessageExpr::getSelector() const {
1790 if (HasMethod)
1791 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1792 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001793 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00001794}
1795
1796ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1797 switch (getReceiverKind()) {
1798 case Instance:
1799 if (const ObjCObjectPointerType *Ptr
1800 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1801 return Ptr->getInterfaceDecl();
1802 break;
1803
1804 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00001805 if (const ObjCObjectType *Ty
1806 = getClassReceiver()->getAs<ObjCObjectType>())
1807 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00001808 break;
1809
1810 case SuperInstance:
1811 if (const ObjCObjectPointerType *Ptr
1812 = getSuperType()->getAs<ObjCObjectPointerType>())
1813 return Ptr->getInterfaceDecl();
1814 break;
1815
1816 case SuperClass:
1817 if (const ObjCObjectPointerType *Iface
1818 = getSuperType()->getAs<ObjCObjectPointerType>())
1819 return Iface->getInterfaceDecl();
1820 break;
1821 }
1822
1823 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00001824}
Chris Lattner7ec71da2009-04-26 00:44:05 +00001825
Chris Lattner35e564e2007-10-25 00:29:32 +00001826bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001827 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001828}
1829
Nate Begeman48745922009-08-12 02:28:50 +00001830void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1831 unsigned NumExprs) {
1832 if (SubExprs) C.Deallocate(SubExprs);
1833
1834 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001835 this->NumExprs = NumExprs;
1836 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001837}
Nate Begeman48745922009-08-12 02:28:50 +00001838
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001839//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001840// DesignatedInitExpr
1841//===----------------------------------------------------------------------===//
1842
1843IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1844 assert(Kind == FieldDesignator && "Only valid on a field designator");
1845 if (Field.NameOrField & 0x01)
1846 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1847 else
1848 return getField()->getIdentifier();
1849}
1850
Alexis Hunta8136cc2010-05-05 15:23:54 +00001851DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001852 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001853 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001854 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001855 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001856 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001857 unsigned NumIndexExprs,
1858 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001859 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001860 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001861 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1862 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001863 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001864
1865 // Record the initializer itself.
1866 child_iterator Child = child_begin();
1867 *Child++ = Init;
1868
1869 // Copy the designators and their subexpressions, computing
1870 // value-dependence along the way.
1871 unsigned IndexIdx = 0;
1872 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001873 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001874
1875 if (this->Designators[I].isArrayDesignator()) {
1876 // Compute type- and value-dependence.
1877 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001878 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001879 Index->isTypeDependent() || Index->isValueDependent();
1880
1881 // Copy the index expressions into permanent storage.
1882 *Child++ = IndexExprs[IndexIdx++];
1883 } else if (this->Designators[I].isArrayRangeDesignator()) {
1884 // Compute type- and value-dependence.
1885 Expr *Start = IndexExprs[IndexIdx];
1886 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001887 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001888 Start->isTypeDependent() || Start->isValueDependent() ||
1889 End->isTypeDependent() || End->isValueDependent();
1890
1891 // Copy the start/end expressions into permanent storage.
1892 *Child++ = IndexExprs[IndexIdx++];
1893 *Child++ = IndexExprs[IndexIdx++];
1894 }
1895 }
1896
1897 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001898}
1899
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001900DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001901DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001902 unsigned NumDesignators,
1903 Expr **IndexExprs, unsigned NumIndexExprs,
1904 SourceLocation ColonOrEqualLoc,
1905 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001906 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001907 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001908 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001909 ColonOrEqualLoc, UsesColonSyntax,
1910 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001911}
1912
Mike Stump11289f42009-09-09 15:08:12 +00001913DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001914 unsigned NumIndexExprs) {
1915 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1916 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1917 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1918}
1919
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001920void DesignatedInitExpr::setDesignators(ASTContext &C,
1921 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001922 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001923 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00001924 NumDesignators = NumDesigs;
1925 for (unsigned I = 0; I != NumDesigs; ++I)
1926 Designators[I] = Desigs[I];
1927}
1928
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001929SourceRange DesignatedInitExpr::getSourceRange() const {
1930 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00001931 Designator &First =
1932 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001933 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00001934 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001935 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
1936 else
1937 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
1938 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00001939 StartLoc =
1940 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001941 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
1942}
1943
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001944Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
1945 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
1946 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1947 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001948 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1949 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1950}
1951
1952Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001953 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001954 "Requires array range designator");
1955 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1956 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001957 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1958 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
1959}
1960
1961Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00001962 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001963 "Requires array range designator");
1964 char* Ptr = static_cast<char*>(static_cast<void *>(this));
1965 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001966 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
1967 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
1968}
1969
Douglas Gregord5846a12009-04-15 06:41:24 +00001970/// \brief Replaces the designator at index @p Idx with the series
1971/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001972void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00001973 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00001974 const Designator *Last) {
1975 unsigned NumNewDesignators = Last - First;
1976 if (NumNewDesignators == 0) {
1977 std::copy_backward(Designators + Idx + 1,
1978 Designators + NumDesignators,
1979 Designators + Idx);
1980 --NumNewDesignators;
1981 return;
1982 } else if (NumNewDesignators == 1) {
1983 Designators[Idx] = *First;
1984 return;
1985 }
1986
Mike Stump11289f42009-09-09 15:08:12 +00001987 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001988 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00001989 std::copy(Designators, Designators + Idx, NewDesignators);
1990 std::copy(First, Last, NewDesignators + Idx);
1991 std::copy(Designators + Idx + 1, Designators + NumDesignators,
1992 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00001993 Designators = NewDesignators;
1994 NumDesignators = NumDesignators - 1 + NumNewDesignators;
1995}
1996
Mike Stump11289f42009-09-09 15:08:12 +00001997ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00001998 Expr **exprs, unsigned nexprs,
1999 SourceLocation rparenloc)
2000: Expr(ParenListExprClass, QualType(),
2001 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002002 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002003 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002004
Nate Begeman5ec4b312009-08-10 23:49:36 +00002005 Exprs = new (C) Stmt*[nexprs];
2006 for (unsigned i = 0; i != nexprs; ++i)
2007 Exprs[i] = exprs[i];
2008}
2009
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002010//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002011// ExprIterator.
2012//===----------------------------------------------------------------------===//
2013
2014Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2015Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2016Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2017const Expr* ConstExprIterator::operator[](size_t idx) const {
2018 return cast<Expr>(I[idx]);
2019}
2020const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2021const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2022
2023//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002024// Child Iterators for iterating over subexpressions/substatements
2025//===----------------------------------------------------------------------===//
2026
2027// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002028Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2029Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002030
Steve Naroffe46504b2007-11-12 14:29:37 +00002031// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002032Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2033Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002034
Steve Naroffebf4cb42008-06-02 23:03:37 +00002035// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002036Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2037Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002038
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002039// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002040Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00002041 // If this is accessing a class member, skip that entry.
2042 if (Base) return &Base;
2043 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002044}
Mike Stump11289f42009-09-09 15:08:12 +00002045Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2046 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002047}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002048
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002049// ObjCSuperExpr
2050Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2051Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2052
Steve Naroffe87026a2009-07-24 17:54:45 +00002053// ObjCIsaExpr
2054Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2055Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2056
Chris Lattner6307f192008-08-10 01:53:14 +00002057// PredefinedExpr
2058Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2059Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002060
2061// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002062Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2063Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002064
2065// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002066Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002067Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002068
2069// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002070Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2071Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002072
Chris Lattner1c20a172007-08-26 03:42:43 +00002073// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002074Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2075Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002076
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002077// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002078Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2079Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002080
2081// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002082Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2083Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002084
2085// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002086Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2087Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002088
Douglas Gregor882211c2010-04-28 22:16:22 +00002089// OffsetOfExpr
2090Stmt::child_iterator OffsetOfExpr::child_begin() {
2091 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2092 + NumComps);
2093}
2094Stmt::child_iterator OffsetOfExpr::child_end() {
2095 return child_iterator(&*child_begin() + NumExprs);
2096}
2097
Sebastian Redl6f282892008-11-11 17:56:53 +00002098// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002099Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002100 // If this is of a type and the type is a VLA type (and not a typedef), the
2101 // size expression of the VLA needs to be treated as an executable expression.
2102 // Why isn't this weirdness documented better in StmtIterator?
2103 if (isArgumentType()) {
2104 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2105 getArgumentType().getTypePtr()))
2106 return child_iterator(T);
2107 return child_iterator();
2108 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002109 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002110}
Sebastian Redl6f282892008-11-11 17:56:53 +00002111Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2112 if (isArgumentType())
2113 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002114 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002115}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002116
2117// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002118Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002119 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002120}
Ted Kremenek23702b62007-08-24 20:06:47 +00002121Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002122 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002123}
2124
2125// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002126Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002127 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002128}
Ted Kremenek23702b62007-08-24 20:06:47 +00002129Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002130 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002131}
Ted Kremenek23702b62007-08-24 20:06:47 +00002132
2133// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002134Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2135Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002136
Nate Begemance4d7fc2008-04-18 23:10:10 +00002137// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002138Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2139Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002140
2141// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002142Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2143Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002144
Ted Kremenek23702b62007-08-24 20:06:47 +00002145// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002146Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2147Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002148
2149// BinaryOperator
2150Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002151 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002152}
Ted Kremenek23702b62007-08-24 20:06:47 +00002153Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002154 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002155}
2156
2157// ConditionalOperator
2158Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002159 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002160}
Ted Kremenek23702b62007-08-24 20:06:47 +00002161Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002162 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002163}
2164
2165// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002166Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2167Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002168
Ted Kremenek23702b62007-08-24 20:06:47 +00002169// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002170Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2171Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002172
2173// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002174Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2175 return child_iterator();
2176}
2177
2178Stmt::child_iterator TypesCompatibleExpr::child_end() {
2179 return child_iterator();
2180}
Ted Kremenek23702b62007-08-24 20:06:47 +00002181
2182// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002183Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2184Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002185
Douglas Gregor3be4b122008-11-29 04:51:27 +00002186// GNUNullExpr
2187Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2188Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2189
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002190// ShuffleVectorExpr
2191Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002192 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002193}
2194Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002195 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002196}
2197
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002198// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002199Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2200Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002201
Anders Carlsson4692db02007-08-31 04:56:16 +00002202// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002203Stmt::child_iterator InitListExpr::child_begin() {
2204 return InitExprs.size() ? &InitExprs[0] : 0;
2205}
2206Stmt::child_iterator InitListExpr::child_end() {
2207 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2208}
Anders Carlsson4692db02007-08-31 04:56:16 +00002209
Douglas Gregor0202cb42009-01-29 17:44:32 +00002210// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002211Stmt::child_iterator DesignatedInitExpr::child_begin() {
2212 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2213 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002214 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2215}
2216Stmt::child_iterator DesignatedInitExpr::child_end() {
2217 return child_iterator(&*child_begin() + NumSubExprs);
2218}
2219
Douglas Gregor0202cb42009-01-29 17:44:32 +00002220// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002221Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2222 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002223}
2224
Mike Stump11289f42009-09-09 15:08:12 +00002225Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2226 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002227}
2228
Nate Begeman5ec4b312009-08-10 23:49:36 +00002229// ParenListExpr
2230Stmt::child_iterator ParenListExpr::child_begin() {
2231 return &Exprs[0];
2232}
2233Stmt::child_iterator ParenListExpr::child_end() {
2234 return &Exprs[0]+NumExprs;
2235}
2236
Ted Kremenek23702b62007-08-24 20:06:47 +00002237// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002238Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002239 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002240}
2241Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002242 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002243}
Ted Kremenek23702b62007-08-24 20:06:47 +00002244
2245// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002246Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2247Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002248
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002249// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002250Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002251 return child_iterator();
2252}
2253Stmt::child_iterator ObjCSelectorExpr::child_end() {
2254 return child_iterator();
2255}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002256
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002257// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002258Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2259 return child_iterator();
2260}
2261Stmt::child_iterator ObjCProtocolExpr::child_end() {
2262 return child_iterator();
2263}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002264
Steve Naroffd54978b2007-09-18 23:55:05 +00002265// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002266Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002267 if (getReceiverKind() == Instance)
2268 return reinterpret_cast<Stmt **>(this + 1);
2269 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002270}
2271Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002272 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002273}
2274
Steve Naroffc540d662008-09-03 18:15:37 +00002275// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002276Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2277Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002278
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002279Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2280Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }