blob: 5feef1c803320d25e20734a073cfb9c003131945 [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()) {
John McCalle3027922010-08-25 11:45:40 +000047 case UO_Plus:
48 case UO_Extension:
Chris Lattner4ebae652010-04-16 23:34:13 +000049 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;
John McCalle3027922010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000071 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000072
John McCalle3027922010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000079
John McCalle3027922010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000082 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,
John McCallb3774b52010-08-19 23:49:38 +0000154 else if (hasExplicitTemplateArgs() &&
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)
John McCallb3774b52010-08-19 23:49:38 +0000207 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000208
209 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000210}
211
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000212DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
213 SourceRange QualifierRange,
214 ValueDecl *D, const DeclarationNameInfo &NameInfo,
215 const TemplateArgumentListInfo *TemplateArgs,
216 QualType T)
217 : Expr(DeclRefExprClass, T, false, false),
218 DecoratedD(D,
219 (Qualifier? HasQualifierFlag : 0) |
220 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
221 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
222 if (Qualifier) {
223 NameQualifier *NQ = getNameQualifier();
224 NQ->NNS = Qualifier;
225 NQ->Range = QualifierRange;
226 }
227
228 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000229 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000230
231 computeDependence();
232}
233
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000234DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
235 NestedNameSpecifier *Qualifier,
236 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000237 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000238 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000239 QualType T,
240 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000241 return Create(Context, Qualifier, QualifierRange, D,
242 DeclarationNameInfo(D->getDeclName(), NameLoc),
243 T, TemplateArgs);
244}
245
246DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
247 NestedNameSpecifier *Qualifier,
248 SourceRange QualifierRange,
249 ValueDecl *D,
250 const DeclarationNameInfo &NameInfo,
251 QualType T,
252 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000253 std::size_t Size = sizeof(DeclRefExpr);
254 if (Qualifier != 0)
255 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000256
John McCall6b51f282009-11-23 01:53:49 +0000257 if (TemplateArgs)
258 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000259
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000260 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000261 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
Douglas Gregored6c7442009-11-23 11:41:28 +0000262 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000263}
264
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000265DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
266 unsigned NumTemplateArgs) {
267 std::size_t Size = sizeof(DeclRefExpr);
268 if (HasQualifier)
269 Size += sizeof(NameQualifier);
270
271 if (NumTemplateArgs)
272 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
273
274 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
275 return new (Mem) DeclRefExpr(EmptyShell());
276}
277
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000278SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000279 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000280 if (hasQualifier())
281 R.setBegin(getQualifierRange().getBegin());
John McCallb3774b52010-08-19 23:49:38 +0000282 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000283 R.setEnd(getRAngleLoc());
284 return R;
285}
286
Anders Carlsson2fb08242009-09-08 18:24:21 +0000287// FIXME: Maybe this should use DeclPrinter with a special "print predefined
288// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000289std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
290 ASTContext &Context = CurrentDecl->getASTContext();
291
Anders Carlsson2fb08242009-09-08 18:24:21 +0000292 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000293 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000294 return FD->getNameAsString();
295
296 llvm::SmallString<256> Name;
297 llvm::raw_svector_ostream Out(Name);
298
299 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000300 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000301 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000302 if (MD->isStatic())
303 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000304 }
305
306 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000307
308 std::string Proto = FD->getQualifiedNameAsString(Policy);
309
John McCall9dd450b2009-09-21 23:43:11 +0000310 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000311 const FunctionProtoType *FT = 0;
312 if (FD->hasWrittenPrototype())
313 FT = dyn_cast<FunctionProtoType>(AFT);
314
315 Proto += "(";
316 if (FT) {
317 llvm::raw_string_ostream POut(Proto);
318 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
319 if (i) POut << ", ";
320 std::string Param;
321 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
322 POut << Param;
323 }
324
325 if (FT->isVariadic()) {
326 if (FD->getNumParams()) POut << ", ";
327 POut << "...";
328 }
329 }
330 Proto += ")";
331
Sam Weinig4e83bd22009-12-27 01:38:20 +0000332 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
333 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
334 if (ThisQuals.hasConst())
335 Proto += " const";
336 if (ThisQuals.hasVolatile())
337 Proto += " volatile";
338 }
339
Sam Weinigd060ed42009-12-06 23:55:13 +0000340 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
341 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000342
343 Out << Proto;
344
345 Out.flush();
346 return Name.str().str();
347 }
348 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
349 llvm::SmallString<256> Name;
350 llvm::raw_svector_ostream Out(Name);
351 Out << (MD->isInstanceMethod() ? '-' : '+');
352 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000353
354 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
355 // a null check to avoid a crash.
356 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000357 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000358
Anders Carlsson2fb08242009-09-08 18:24:21 +0000359 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000360 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
361 Out << '(' << CID << ')';
362
Anders Carlsson2fb08242009-09-08 18:24:21 +0000363 Out << ' ';
364 Out << MD->getSelector().getAsString();
365 Out << ']';
366
367 Out.flush();
368 return Name.str().str();
369 }
370 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
371 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
372 return "top level";
373 }
374 return "";
375}
376
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000377void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
378 if (hasAllocation())
379 C.Deallocate(pVal);
380
381 BitWidth = Val.getBitWidth();
382 unsigned NumWords = Val.getNumWords();
383 const uint64_t* Words = Val.getRawData();
384 if (NumWords > 1) {
385 pVal = new (C) uint64_t[NumWords];
386 std::copy(Words, Words + NumWords, pVal);
387 } else if (NumWords == 1)
388 VAL = Words[0];
389 else
390 VAL = 0;
391}
392
393IntegerLiteral *
394IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
395 QualType type, SourceLocation l) {
396 return new (C) IntegerLiteral(C, V, type, l);
397}
398
399IntegerLiteral *
400IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
401 return new (C) IntegerLiteral(Empty);
402}
403
404FloatingLiteral *
405FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
406 bool isexact, QualType Type, SourceLocation L) {
407 return new (C) FloatingLiteral(C, V, isexact, Type, L);
408}
409
410FloatingLiteral *
411FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
412 return new (C) FloatingLiteral(Empty);
413}
414
Chris Lattnera0173132008-06-07 22:13:43 +0000415/// getValueAsApproximateDouble - This returns the value as an inaccurate
416/// double. Note that this may cause loss of precision, but is useful for
417/// debugging dumps, etc.
418double FloatingLiteral::getValueAsApproximateDouble() const {
419 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000420 bool ignored;
421 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
422 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000423 return V.convertToDouble();
424}
425
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000426StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
427 unsigned ByteLength, bool Wide,
428 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000429 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000430 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000431 // Allocate enough space for the StringLiteral plus an array of locations for
432 // any concatenated string tokens.
433 void *Mem = C.Allocate(sizeof(StringLiteral)+
434 sizeof(SourceLocation)*(NumStrs-1),
435 llvm::alignof<StringLiteral>());
436 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Steve Naroffdf7855b2007-02-21 23:46:25 +0000438 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000439 char *AStrData = new (C, 1) char[ByteLength];
440 memcpy(AStrData, StrData, ByteLength);
441 SL->StrData = AStrData;
442 SL->ByteLength = ByteLength;
443 SL->IsWide = Wide;
444 SL->TokLocs[0] = Loc[0];
445 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000446
Chris Lattner630970d2009-02-18 05:49:11 +0000447 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000448 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
449 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000450}
451
Douglas Gregor958dfc92009-04-15 16:35:07 +0000452StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
453 void *Mem = C.Allocate(sizeof(StringLiteral)+
454 sizeof(SourceLocation)*(NumStrs-1),
455 llvm::alignof<StringLiteral>());
456 StringLiteral *SL = new (Mem) StringLiteral(QualType());
457 SL->StrData = 0;
458 SL->ByteLength = 0;
459 SL->NumConcatenated = NumStrs;
460 return SL;
461}
462
Daniel Dunbar36217882009-09-22 03:27:33 +0000463void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000464 char *AStrData = new (C, 1) char[Str.size()];
465 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000466 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000467 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000468}
469
Chris Lattner1b926492006-08-23 06:42:10 +0000470/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
471/// corresponds to, e.g. "sizeof" or "[pre]++".
472const char *UnaryOperator::getOpcodeStr(Opcode Op) {
473 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000474 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000475 case UO_PostInc: return "++";
476 case UO_PostDec: return "--";
477 case UO_PreInc: return "++";
478 case UO_PreDec: return "--";
479 case UO_AddrOf: return "&";
480 case UO_Deref: return "*";
481 case UO_Plus: return "+";
482 case UO_Minus: return "-";
483 case UO_Not: return "~";
484 case UO_LNot: return "!";
485 case UO_Real: return "__real";
486 case UO_Imag: return "__imag";
487 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000488 }
489}
490
John McCalle3027922010-08-25 11:45:40 +0000491UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000492UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
493 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000494 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000495 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
496 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
497 case OO_Amp: return UO_AddrOf;
498 case OO_Star: return UO_Deref;
499 case OO_Plus: return UO_Plus;
500 case OO_Minus: return UO_Minus;
501 case OO_Tilde: return UO_Not;
502 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000503 }
504}
505
506OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
507 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000508 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
509 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
510 case UO_AddrOf: return OO_Amp;
511 case UO_Deref: return OO_Star;
512 case UO_Plus: return OO_Plus;
513 case UO_Minus: return OO_Minus;
514 case UO_Not: return OO_Tilde;
515 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000516 default: return OO_None;
517 }
518}
519
520
Chris Lattner0eedafe2006-08-24 04:56:27 +0000521//===----------------------------------------------------------------------===//
522// Postfix Operators.
523//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000524
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000525CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000526 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000527 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000528 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000529 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000530 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000531
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000532 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000533 SubExprs[FN] = fn;
534 for (unsigned i = 0; i != numargs; ++i)
535 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000536
Douglas Gregor993603d2008-11-14 16:09:21 +0000537 RParenLoc = rparenloc;
538}
Nate Begeman1e36a852008-01-17 17:46:27 +0000539
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000540CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
541 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000542 : Expr(CallExprClass, t,
543 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000544 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000545 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000546
547 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000548 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000549 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000550 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000551
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000552 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000553}
554
Mike Stump11289f42009-09-09 15:08:12 +0000555CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
556 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000557 SubExprs = new (C) Stmt*[1];
558}
559
Nuno Lopes518e3702009-12-20 23:11:08 +0000560Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000561 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000562 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000563 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000564 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
565 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000566
567 return 0;
568}
569
Nuno Lopes518e3702009-12-20 23:11:08 +0000570FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000571 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000572}
573
Chris Lattnere4407ed2007-12-28 05:25:02 +0000574/// setNumArgs - This changes the number of arguments present in this call.
575/// Any orphaned expressions are deleted by this, and any new operands are set
576/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000577void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000578 // No change, just return.
579 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattnere4407ed2007-12-28 05:25:02 +0000581 // If shrinking # arguments, just delete the extras and forgot them.
582 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000583 this->NumArgs = NumArgs;
584 return;
585 }
586
587 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000588 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000589 // Copy over args.
590 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
591 NewSubExprs[i] = SubExprs[i];
592 // Null out new args.
593 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
594 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Douglas Gregorba6e5572009-04-17 21:46:47 +0000596 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000597 SubExprs = NewSubExprs;
598 this->NumArgs = NumArgs;
599}
600
Chris Lattner01ff98a2008-10-06 05:00:53 +0000601/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
602/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000603unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000604 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000605 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000606 // ImplicitCastExpr.
607 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
608 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000609 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Steve Narofff6e3b3292008-01-31 01:07:12 +0000611 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
612 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000613 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000614
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000615 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
616 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000617 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000619 if (!FDecl->getIdentifier())
620 return 0;
621
Douglas Gregor15fc9562009-09-12 00:22:50 +0000622 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000623}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000624
Anders Carlsson00a27592009-05-26 04:57:27 +0000625QualType CallExpr::getCallReturnType() const {
626 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000627 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000628 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000629 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000630 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000631 else if (const MemberPointerType *MPT
632 = CalleeType->getAs<MemberPointerType>())
633 CalleeType = MPT->getPointeeType();
634
John McCall9dd450b2009-09-21 23:43:11 +0000635 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000636 return FnType->getResultType();
637}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000638
Alexis Hunta8136cc2010-05-05 15:23:54 +0000639OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000640 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000641 TypeSourceInfo *tsi,
642 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000643 Expr** exprsPtr, unsigned numExprs,
644 SourceLocation RParenLoc) {
645 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000646 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000647 sizeof(Expr*) * numExprs);
648
649 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
650 exprsPtr, numExprs, RParenLoc);
651}
652
653OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
654 unsigned numComps, unsigned numExprs) {
655 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
656 sizeof(OffsetOfNode) * numComps +
657 sizeof(Expr*) * numExprs);
658 return new (Mem) OffsetOfExpr(numComps, numExprs);
659}
660
Alexis Hunta8136cc2010-05-05 15:23:54 +0000661OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000662 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000663 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000664 Expr** exprsPtr, unsigned numExprs,
665 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000666 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000667 /*ValueDependent=*/tsi->getType()->isDependentType() ||
668 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
669 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000670 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
671 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000672{
673 for(unsigned i = 0; i < numComps; ++i) {
674 setComponent(i, compsPtr[i]);
675 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000676
Douglas Gregor882211c2010-04-28 22:16:22 +0000677 for(unsigned i = 0; i < numExprs; ++i) {
678 setIndexExpr(i, exprsPtr[i]);
679 }
680}
681
682IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
683 assert(getKind() == Field || getKind() == Identifier);
684 if (getKind() == Field)
685 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000686
Douglas Gregor882211c2010-04-28 22:16:22 +0000687 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
688}
689
Mike Stump11289f42009-09-09 15:08:12 +0000690MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
691 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000692 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000693 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000694 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000695 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000696 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000697 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000698 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000699
John McCalla8ae2222010-04-06 21:38:20 +0000700 bool hasQualOrFound = (qual != 0 ||
701 founddecl.getDecl() != memberdecl ||
702 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000703 if (hasQualOrFound)
704 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000705
John McCall6b51f282009-11-23 01:53:49 +0000706 if (targs)
707 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000708
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000709 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000710 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo, ty);
John McCall16df1e52010-03-30 21:47:33 +0000711
712 if (hasQualOrFound) {
713 if (qual && qual->isDependent()) {
714 E->setValueDependent(true);
715 E->setTypeDependent(true);
716 }
717 E->HasQualifierOrFoundDecl = true;
718
719 MemberNameQualifier *NQ = E->getMemberQualifier();
720 NQ->NNS = qual;
721 NQ->Range = qualrange;
722 NQ->FoundDecl = founddecl;
723 }
724
725 if (targs) {
726 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000727 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000728 }
729
730 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000731}
732
Anders Carlsson496335e2009-09-03 00:59:21 +0000733const char *CastExpr::getCastKindName() const {
734 switch (getCastKind()) {
John McCalle3027922010-08-25 11:45:40 +0000735 case CK_Unknown:
Anders Carlsson496335e2009-09-03 00:59:21 +0000736 return "Unknown";
John McCalle3027922010-08-25 11:45:40 +0000737 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000738 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000739 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000740 return "LValueBitCast";
John McCalle3027922010-08-25 11:45:40 +0000741 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000742 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000743 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000744 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000745 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000746 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000747 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000748 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000749 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000750 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000751 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000752 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000753 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000754 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000755 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000756 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000757 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000758 return "NullToMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000759 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000760 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000761 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000762 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000763 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000764 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000765 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000766 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000767 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000768 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000769 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000770 return "PointerToIntegral";
John McCalle3027922010-08-25 11:45:40 +0000771 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +0000772 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +0000773 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +0000774 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +0000775 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +0000776 return "IntegralCast";
John McCalle3027922010-08-25 11:45:40 +0000777 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +0000778 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +0000779 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +0000780 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +0000781 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000782 return "FloatingCast";
John McCalle3027922010-08-25 11:45:40 +0000783 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000784 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000785 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000786 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000787 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000788 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000789 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +0000790 return "ObjCObjectLValueCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Anders Carlsson496335e2009-09-03 00:59:21 +0000793 assert(0 && "Unhandled cast kind!");
794 return 0;
795}
796
Douglas Gregord196a582009-12-14 19:27:10 +0000797Expr *CastExpr::getSubExprAsWritten() {
798 Expr *SubExpr = 0;
799 CastExpr *E = this;
800 do {
801 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000802
Douglas Gregord196a582009-12-14 19:27:10 +0000803 // Skip any temporary bindings; they're implicit.
804 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
805 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000806
Douglas Gregord196a582009-12-14 19:27:10 +0000807 // Conversions by constructor and conversion functions have a
808 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +0000809 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000810 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +0000811 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000812 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000813
Douglas Gregord196a582009-12-14 19:27:10 +0000814 // If the subexpression we're left with is an implicit cast, look
815 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000816 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
817
Douglas Gregord196a582009-12-14 19:27:10 +0000818 return SubExpr;
819}
820
John McCallcf142162010-08-07 06:22:56 +0000821CXXBaseSpecifier **CastExpr::path_buffer() {
822 switch (getStmtClass()) {
823#define ABSTRACT_STMT(x)
824#define CASTEXPR(Type, Base) \
825 case Stmt::Type##Class: \
826 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
827#define STMT(Type, Base)
828#include "clang/AST/StmtNodes.inc"
829 default:
830 llvm_unreachable("non-cast expressions not possible here");
831 return 0;
832 }
833}
834
835void CastExpr::setCastPath(const CXXCastPath &Path) {
836 assert(Path.size() == path_size());
837 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
838}
839
840ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
841 CastKind Kind, Expr *Operand,
842 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +0000843 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +0000844 unsigned PathSize = (BasePath ? BasePath->size() : 0);
845 void *Buffer =
846 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
847 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +0000848 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +0000849 if (PathSize) E->setCastPath(*BasePath);
850 return E;
851}
852
853ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
854 unsigned PathSize) {
855 void *Buffer =
856 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
857 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
858}
859
860
861CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
862 CastKind K, Expr *Op,
863 const CXXCastPath *BasePath,
864 TypeSourceInfo *WrittenTy,
865 SourceLocation L, SourceLocation R) {
866 unsigned PathSize = (BasePath ? BasePath->size() : 0);
867 void *Buffer =
868 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
869 CStyleCastExpr *E =
870 new (Buffer) CStyleCastExpr(T, K, Op, PathSize, WrittenTy, L, R);
871 if (PathSize) E->setCastPath(*BasePath);
872 return E;
873}
874
875CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
876 void *Buffer =
877 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
878 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
879}
880
Chris Lattner1b926492006-08-23 06:42:10 +0000881/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
882/// corresponds to, e.g. "<<=".
883const char *BinaryOperator::getOpcodeStr(Opcode Op) {
884 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000885 case BO_PtrMemD: return ".*";
886 case BO_PtrMemI: return "->*";
887 case BO_Mul: return "*";
888 case BO_Div: return "/";
889 case BO_Rem: return "%";
890 case BO_Add: return "+";
891 case BO_Sub: return "-";
892 case BO_Shl: return "<<";
893 case BO_Shr: return ">>";
894 case BO_LT: return "<";
895 case BO_GT: return ">";
896 case BO_LE: return "<=";
897 case BO_GE: return ">=";
898 case BO_EQ: return "==";
899 case BO_NE: return "!=";
900 case BO_And: return "&";
901 case BO_Xor: return "^";
902 case BO_Or: return "|";
903 case BO_LAnd: return "&&";
904 case BO_LOr: return "||";
905 case BO_Assign: return "=";
906 case BO_MulAssign: return "*=";
907 case BO_DivAssign: return "/=";
908 case BO_RemAssign: return "%=";
909 case BO_AddAssign: return "+=";
910 case BO_SubAssign: return "-=";
911 case BO_ShlAssign: return "<<=";
912 case BO_ShrAssign: return ">>=";
913 case BO_AndAssign: return "&=";
914 case BO_XorAssign: return "^=";
915 case BO_OrAssign: return "|=";
916 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +0000917 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000918
919 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000920}
Steve Naroff47500512007-04-19 23:00:49 +0000921
John McCalle3027922010-08-25 11:45:40 +0000922BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000923BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
924 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000925 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +0000926 case OO_Plus: return BO_Add;
927 case OO_Minus: return BO_Sub;
928 case OO_Star: return BO_Mul;
929 case OO_Slash: return BO_Div;
930 case OO_Percent: return BO_Rem;
931 case OO_Caret: return BO_Xor;
932 case OO_Amp: return BO_And;
933 case OO_Pipe: return BO_Or;
934 case OO_Equal: return BO_Assign;
935 case OO_Less: return BO_LT;
936 case OO_Greater: return BO_GT;
937 case OO_PlusEqual: return BO_AddAssign;
938 case OO_MinusEqual: return BO_SubAssign;
939 case OO_StarEqual: return BO_MulAssign;
940 case OO_SlashEqual: return BO_DivAssign;
941 case OO_PercentEqual: return BO_RemAssign;
942 case OO_CaretEqual: return BO_XorAssign;
943 case OO_AmpEqual: return BO_AndAssign;
944 case OO_PipeEqual: return BO_OrAssign;
945 case OO_LessLess: return BO_Shl;
946 case OO_GreaterGreater: return BO_Shr;
947 case OO_LessLessEqual: return BO_ShlAssign;
948 case OO_GreaterGreaterEqual: return BO_ShrAssign;
949 case OO_EqualEqual: return BO_EQ;
950 case OO_ExclaimEqual: return BO_NE;
951 case OO_LessEqual: return BO_LE;
952 case OO_GreaterEqual: return BO_GE;
953 case OO_AmpAmp: return BO_LAnd;
954 case OO_PipePipe: return BO_LOr;
955 case OO_Comma: return BO_Comma;
956 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000957 }
958}
959
960OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
961 static const OverloadedOperatorKind OverOps[] = {
962 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
963 OO_Star, OO_Slash, OO_Percent,
964 OO_Plus, OO_Minus,
965 OO_LessLess, OO_GreaterGreater,
966 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
967 OO_EqualEqual, OO_ExclaimEqual,
968 OO_Amp,
969 OO_Caret,
970 OO_Pipe,
971 OO_AmpAmp,
972 OO_PipePipe,
973 OO_Equal, OO_StarEqual,
974 OO_SlashEqual, OO_PercentEqual,
975 OO_PlusEqual, OO_MinusEqual,
976 OO_LessLessEqual, OO_GreaterGreaterEqual,
977 OO_AmpEqual, OO_CaretEqual,
978 OO_PipeEqual,
979 OO_Comma
980 };
981 return OverOps[Opc];
982}
983
Ted Kremenekac034612010-04-13 23:39:13 +0000984InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000985 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000986 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000987 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000988 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000989 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000990 UnionFieldInit(0), HadArrayRangeDesignator(false)
991{
Ted Kremenek013041e2010-02-19 01:50:18 +0000992 for (unsigned I = 0; I != numInits; ++I) {
993 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000994 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000995 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000996 ValueDependent = true;
997 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000998
Ted Kremenekac034612010-04-13 23:39:13 +0000999 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001000}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001001
Ted Kremenekac034612010-04-13 23:39:13 +00001002void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001003 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001004 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001005}
1006
Ted Kremenekac034612010-04-13 23:39:13 +00001007void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001008 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001009}
1010
Ted Kremenekac034612010-04-13 23:39:13 +00001011Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001012 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001013 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001014 InitExprs.back() = expr;
1015 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001016 }
Mike Stump11289f42009-09-09 15:08:12 +00001017
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001018 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1019 InitExprs[Init] = expr;
1020 return Result;
1021}
1022
Steve Naroff991e99d2008-09-04 15:31:07 +00001023/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001024///
1025const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001026 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001027 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001028}
1029
Mike Stump11289f42009-09-09 15:08:12 +00001030SourceLocation BlockExpr::getCaretLocation() const {
1031 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001032}
Mike Stump11289f42009-09-09 15:08:12 +00001033const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001034 return TheBlock->getBody();
1035}
Mike Stump11289f42009-09-09 15:08:12 +00001036Stmt *BlockExpr::getBody() {
1037 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001038}
Steve Naroff415d3d52008-10-08 17:01:13 +00001039
1040
Chris Lattner1ec5f562007-06-27 05:38:08 +00001041//===----------------------------------------------------------------------===//
1042// Generic Expression Routines
1043//===----------------------------------------------------------------------===//
1044
Chris Lattner237f2752009-02-14 07:37:35 +00001045/// isUnusedResultAWarning - Return true if this immediate expression should
1046/// be warned about if the result is unused. If so, fill in Loc and Ranges
1047/// with location to warn on and the source range[s] to report with the
1048/// warning.
1049bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001050 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001051 // Don't warn if the expr is type dependent. The type could end up
1052 // instantiating to void.
1053 if (isTypeDependent())
1054 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner1ec5f562007-06-27 05:38:08 +00001056 switch (getStmtClass()) {
1057 default:
John McCallc493a732010-03-12 07:11:26 +00001058 if (getType()->isVoidType())
1059 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001060 Loc = getExprLoc();
1061 R1 = getSourceRange();
1062 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001063 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001064 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001065 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001066 case UnaryOperatorClass: {
1067 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001068
Chris Lattner1ec5f562007-06-27 05:38:08 +00001069 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001070 default: break;
John McCalle3027922010-08-25 11:45:40 +00001071 case UO_PostInc:
1072 case UO_PostDec:
1073 case UO_PreInc:
1074 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001075 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001076 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001077 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001078 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001079 return false;
1080 break;
John McCalle3027922010-08-25 11:45:40 +00001081 case UO_Real:
1082 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001083 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001084 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1085 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001086 return false;
1087 break;
John McCalle3027922010-08-25 11:45:40 +00001088 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001089 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001090 }
Chris Lattner237f2752009-02-14 07:37:35 +00001091 Loc = UO->getOperatorLoc();
1092 R1 = UO->getSubExpr()->getSourceRange();
1093 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001094 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001095 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001096 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001097 switch (BO->getOpcode()) {
1098 default:
1099 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001100 // Consider the RHS of comma for side effects. LHS was checked by
1101 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001102 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001103 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1104 // lvalue-ness) of an assignment written in a macro.
1105 if (IntegerLiteral *IE =
1106 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1107 if (IE->getValue() == 0)
1108 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001109 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1110 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001111 case BO_LAnd:
1112 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001113 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1114 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1115 return false;
1116 break;
John McCall1e3715a2010-02-16 04:10:53 +00001117 }
Chris Lattner237f2752009-02-14 07:37:35 +00001118 if (BO->isAssignmentOp())
1119 return false;
1120 Loc = BO->getOperatorLoc();
1121 R1 = BO->getLHS()->getSourceRange();
1122 R2 = BO->getRHS()->getSourceRange();
1123 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001124 }
Chris Lattner86928112007-08-25 02:00:02 +00001125 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001126 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001127 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001128
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001129 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001130 // The condition must be evaluated, but if either the LHS or RHS is a
1131 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001132 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001133 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001134 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001135 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001136 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001137 }
1138
Chris Lattnera44d1162007-06-27 05:58:59 +00001139 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001140 // If the base pointer or element is to a volatile pointer/field, accessing
1141 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001142 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001143 return false;
1144 Loc = cast<MemberExpr>(this)->getMemberLoc();
1145 R1 = SourceRange(Loc, Loc);
1146 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1147 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner1ec5f562007-06-27 05:38:08 +00001149 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001150 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001151 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001152 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001153 return false;
1154 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1155 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1156 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1157 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001158
Chris Lattner1ec5f562007-06-27 05:38:08 +00001159 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001160 case CXXOperatorCallExprClass:
1161 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001162 // If this is a direct call, get the callee.
1163 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001164 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001165 // If the callee has attribute pure, const, or warn_unused_result, warn
1166 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001167 //
1168 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1169 // updated to match for QoI.
1170 if (FD->getAttr<WarnUnusedResultAttr>() ||
1171 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1172 Loc = CE->getCallee()->getLocStart();
1173 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattner1a6babf2009-10-13 04:53:48 +00001175 if (unsigned NumArgs = CE->getNumArgs())
1176 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1177 CE->getArg(NumArgs-1)->getLocEnd());
1178 return true;
1179 }
Chris Lattner237f2752009-02-14 07:37:35 +00001180 }
1181 return false;
1182 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001183
1184 case CXXTemporaryObjectExprClass:
1185 case CXXConstructExprClass:
1186 return false;
1187
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001188 case ObjCMessageExprClass: {
1189 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1190 const ObjCMethodDecl *MD = ME->getMethodDecl();
1191 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1192 Loc = getExprLoc();
1193 return true;
1194 }
Chris Lattner237f2752009-02-14 07:37:35 +00001195 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001198 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001199#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001200 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001201 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001202 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001203 Loc = Ref->getLocation();
1204 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1205 if (Ref->getBase())
1206 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001207#else
1208 Loc = getExprLoc();
1209 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001210#endif
1211 return true;
1212 }
Chris Lattner944d3062008-07-26 19:51:01 +00001213 case StmtExprClass: {
1214 // Statement exprs don't logically have side effects themselves, but are
1215 // sometimes used in macros in ways that give them a type that is unused.
1216 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1217 // however, if the result of the stmt expr is dead, we don't want to emit a
1218 // warning.
1219 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1220 if (!CS->body_empty())
1221 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001222 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001223
John McCallc493a732010-03-12 07:11:26 +00001224 if (getType()->isVoidType())
1225 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001226 Loc = cast<StmtExpr>(this)->getLParenLoc();
1227 R1 = getSourceRange();
1228 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001229 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001230 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001231 // If this is an explicit cast to void, allow it. People do this when they
1232 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001233 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001234 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001235 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1236 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1237 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001238 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001239 if (getType()->isVoidType())
1240 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001241 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001242
Anders Carlsson6aa50392009-11-17 17:11:23 +00001243 // If this is a cast to void or a constructor conversion, check the operand.
1244 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001245 if (CE->getCastKind() == CK_ToVoid ||
1246 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001247 return (cast<CastExpr>(this)->getSubExpr()
1248 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001249 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1250 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1251 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001254 case ImplicitCastExprClass:
1255 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001256 return (cast<ImplicitCastExpr>(this)
1257 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001258
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001259 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001260 return (cast<CXXDefaultArgExpr>(this)
1261 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001262
1263 case CXXNewExprClass:
1264 // FIXME: In theory, there might be new expressions that don't have side
1265 // effects (e.g. a placement new with an uninitialized POD).
1266 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001267 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001268 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001269 return (cast<CXXBindTemporaryExpr>(this)
1270 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001271 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001272 return (cast<CXXExprWithTemporaries>(this)
1273 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001274 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001275}
1276
Fariborz Jahanian07735332009-02-22 18:40:18 +00001277/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001278/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001279bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001280 switch (getStmtClass()) {
1281 default:
1282 return false;
1283 case ObjCIvarRefExprClass:
1284 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001285 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001286 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001287 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001288 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001289 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001290 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001291 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001292 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001293 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001294 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001295 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1296 if (VD->hasGlobalStorage())
1297 return true;
1298 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001299 // dereferencing to a pointer is always a gc'able candidate,
1300 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001301 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001302 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001303 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001304 return false;
1305 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001306 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001307 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001308 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001309 }
1310 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001311 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001312 }
1313}
Ted Kremenekfff70962008-01-17 16:57:34 +00001314Expr* Expr::IgnoreParens() {
1315 Expr* E = this;
1316 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1317 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001318
Ted Kremenekfff70962008-01-17 16:57:34 +00001319 return E;
1320}
1321
Chris Lattnerf2660962008-02-13 01:02:39 +00001322/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1323/// or CastExprs or ImplicitCastExprs, returning their operand.
1324Expr *Expr::IgnoreParenCasts() {
1325 Expr *E = this;
1326 while (true) {
1327 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1328 E = P->getSubExpr();
1329 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1330 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001331 else
1332 return E;
1333 }
1334}
1335
John McCalleebc8322010-05-05 22:59:52 +00001336Expr *Expr::IgnoreParenImpCasts() {
1337 Expr *E = this;
1338 while (true) {
1339 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1340 E = P->getSubExpr();
1341 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1342 E = P->getSubExpr();
1343 else
1344 return E;
1345 }
1346}
1347
Chris Lattneref26c772009-03-13 17:28:01 +00001348/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1349/// value (including ptr->int casts of the same size). Strip off any
1350/// ParenExpr or CastExprs, returning their operand.
1351Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1352 Expr *E = this;
1353 while (true) {
1354 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1355 E = P->getSubExpr();
1356 continue;
1357 }
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattneref26c772009-03-13 17:28:01 +00001359 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1360 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001361 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001362 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001363
Chris Lattneref26c772009-03-13 17:28:01 +00001364 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1365 E = SE;
1366 continue;
1367 }
Mike Stump11289f42009-09-09 15:08:12 +00001368
Douglas Gregor6972a622010-06-16 00:35:25 +00001369 if ((E->getType()->isPointerType() ||
1370 E->getType()->isIntegralType(Ctx)) &&
1371 (SE->getType()->isPointerType() ||
1372 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001373 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1374 E = SE;
1375 continue;
1376 }
1377 }
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattneref26c772009-03-13 17:28:01 +00001379 return E;
1380 }
1381}
1382
Douglas Gregord196a582009-12-14 19:27:10 +00001383bool Expr::isDefaultArgument() const {
1384 const Expr *E = this;
1385 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1386 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001387
Douglas Gregord196a582009-12-14 19:27:10 +00001388 return isa<CXXDefaultArgExpr>(E);
1389}
Chris Lattneref26c772009-03-13 17:28:01 +00001390
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001391/// \brief Skip over any no-op casts and any temporary-binding
1392/// expressions.
1393static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1394 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001395 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001396 E = ICE->getSubExpr();
1397 else
1398 break;
1399 }
1400
1401 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1402 E = BE->getSubExpr();
1403
1404 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001405 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001406 E = ICE->getSubExpr();
1407 else
1408 break;
1409 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001410
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001411 return E;
1412}
1413
1414const Expr *Expr::getTemporaryObject() const {
1415 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1416
1417 // A cast can produce a temporary object. The object's construction
1418 // is represented as a CXXConstructExpr.
1419 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1420 // Only user-defined and constructor conversions can produce
1421 // temporary objects.
John McCalle3027922010-08-25 11:45:40 +00001422 if (Cast->getCastKind() != CK_ConstructorConversion &&
1423 Cast->getCastKind() != CK_UserDefinedConversion)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001424 return 0;
1425
1426 // Strip off temporary bindings and no-op casts.
1427 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1428
1429 // If this is a constructor conversion, see if we have an object
1430 // construction.
John McCalle3027922010-08-25 11:45:40 +00001431 if (Cast->getCastKind() == CK_ConstructorConversion)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001432 return dyn_cast<CXXConstructExpr>(Sub);
1433
1434 // If this is a user-defined conversion, see if we have a call to
1435 // a function that itself returns a temporary object.
John McCalle3027922010-08-25 11:45:40 +00001436 if (Cast->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001437 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1438 if (CE->getCallReturnType()->isRecordType())
1439 return CE;
1440
1441 return 0;
1442 }
1443
1444 // A call returning a class type returns a temporary.
1445 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1446 if (CE->getCallReturnType()->isRecordType())
1447 return CE;
1448
1449 return 0;
1450 }
1451
1452 // Explicit temporary object constructors create temporaries.
1453 return dyn_cast<CXXTemporaryObjectExpr>(E);
1454}
1455
Douglas Gregor4619e432008-12-05 23:32:09 +00001456/// hasAnyTypeDependentArguments - Determines if any of the expressions
1457/// in Exprs is type-dependent.
1458bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1459 for (unsigned I = 0; I < NumExprs; ++I)
1460 if (Exprs[I]->isTypeDependent())
1461 return true;
1462
1463 return false;
1464}
1465
1466/// hasAnyValueDependentArguments - Determines if any of the expressions
1467/// in Exprs is value-dependent.
1468bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1469 for (unsigned I = 0; I < NumExprs; ++I)
1470 if (Exprs[I]->isValueDependent())
1471 return true;
1472
1473 return false;
1474}
1475
John McCall8b0f4ff2010-08-02 21:13:48 +00001476bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00001477 // This function is attempting whether an expression is an initializer
1478 // which can be evaluated at compile-time. isEvaluatable handles most
1479 // of the cases, but it can't deal with some initializer-specific
1480 // expressions, and it can't deal with aggregates; we deal with those here,
1481 // and fall back to isEvaluatable for the other cases.
1482
John McCall8b0f4ff2010-08-02 21:13:48 +00001483 // If we ever capture reference-binding directly in the AST, we can
1484 // kill the second parameter.
1485
1486 if (IsForRef) {
1487 EvalResult Result;
1488 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1489 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001490
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001491 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001492 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001493 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001494 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001495 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001496 return true;
John McCall81c9cea2010-08-01 21:51:45 +00001497 case CXXTemporaryObjectExprClass:
1498 case CXXConstructExprClass: {
1499 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00001500
1501 // Only if it's
1502 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00001503 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00001504 if (!CE->getNumArgs()) return true;
1505
1506 // 2) an elidable trivial copy construction of an operand which is
1507 // itself a constant initializer. Note that we consider the
1508 // operand on its own, *not* as a reference binding.
1509 return CE->isElidable() &&
1510 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00001511 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001512 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001513 // This handles gcc's extension that allows global initializers like
1514 // "struct x {int x;} x = (struct x) {};".
1515 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001516 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00001517 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001518 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001519 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001520 // FIXME: This doesn't deal with fields with reference types correctly.
1521 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1522 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001523 const InitListExpr *Exp = cast<InitListExpr>(this);
1524 unsigned numInits = Exp->getNumInits();
1525 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00001526 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001527 return false;
1528 }
Eli Friedman384da272009-01-25 03:12:18 +00001529 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001530 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001531 case ImplicitValueInitExprClass:
1532 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001533 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00001534 return cast<ParenExpr>(this)->getSubExpr()
1535 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00001536 case UnaryOperatorClass: {
1537 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001538 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00001539 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00001540 break;
1541 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001542 case BinaryOperatorClass: {
1543 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1544 // but this handles the common case.
1545 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001546 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00001547 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1548 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1549 return true;
1550 break;
1551 }
John McCall8b0f4ff2010-08-02 21:13:48 +00001552 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00001553 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00001554 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001555 case CStyleCastExprClass:
1556 // Handle casts with a destination that's a struct or union; this
1557 // deals with both the gcc no-op struct cast extension and the
1558 // cast-to-union extension.
1559 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001560 return cast<CastExpr>(this)->getSubExpr()
1561 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001562
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001563 // Integer->integer casts can be handled here, which is important for
1564 // things like (int)(&&x-&&y). Scary but true.
1565 if (getType()->isIntegerType() &&
1566 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001567 return cast<CastExpr>(this)->getSubExpr()
1568 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001569
Eli Friedman384da272009-01-25 03:12:18 +00001570 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001571 }
Eli Friedman384da272009-01-25 03:12:18 +00001572 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001573}
1574
Chris Lattner7eef9192007-05-24 01:23:49 +00001575/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1576/// integer constant expression with the value zero, or if this is one that is
1577/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001578bool Expr::isNullPointerConstant(ASTContext &Ctx,
1579 NullPointerConstantValueDependence NPC) const {
1580 if (isValueDependent()) {
1581 switch (NPC) {
1582 case NPC_NeverValueDependent:
1583 assert(false && "Unexpected value dependent expression!");
1584 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001585
Douglas Gregor56751b52009-09-25 04:25:58 +00001586 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001587 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001588
Douglas Gregor56751b52009-09-25 04:25:58 +00001589 case NPC_ValueDependentIsNotNull:
1590 return false;
1591 }
1592 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001593
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001594 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001595 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001596 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001597 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001598 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001599 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001600 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001601 Pointee->isVoidType() && // to void*
1602 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001603 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001604 }
Steve Naroffada7d422007-05-20 17:54:12 +00001605 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001606 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1607 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001608 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001609 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1610 // Accept ((void*)0) as a null pointer constant, as many other
1611 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001612 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001613 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001614 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001615 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001616 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001617 } else if (isa<GNUNullExpr>(this)) {
1618 // The GNU __null extension is always a null pointer constant.
1619 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001620 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001621
Sebastian Redl576fd422009-05-10 18:38:11 +00001622 // C++0x nullptr_t is always a null pointer constant.
1623 if (getType()->isNullPtrType())
1624 return true;
1625
Steve Naroff4871fe02008-01-14 16:10:57 +00001626 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001627 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001628 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001629 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001630
Chris Lattner1abbd412007-06-08 17:58:43 +00001631 // If we have an integer constant expression, we need to *evaluate* it and
1632 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001633 llvm::APSInt Result;
1634 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001635}
Steve Narofff7a5da12007-07-28 23:10:27 +00001636
Douglas Gregor71235ec2009-05-02 02:18:30 +00001637FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001638 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001639
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001640 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00001641 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00001642 ICE->getCastKind() == CK_NoOp)
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001643 E = ICE->getSubExpr()->IgnoreParens();
1644 else
1645 break;
1646 }
1647
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001648 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001649 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001650 if (Field->isBitField())
1651 return Field;
1652
1653 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1654 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1655 return BinOp->getLHS()->getBitField();
1656
1657 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001658}
1659
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001660bool Expr::refersToVectorElement() const {
1661 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001662
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001663 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00001664 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00001665 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001666 E = ICE->getSubExpr()->IgnoreParens();
1667 else
1668 break;
1669 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001670
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001671 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1672 return ASE->getBase()->getType()->isVectorType();
1673
1674 if (isa<ExtVectorElementExpr>(E))
1675 return true;
1676
1677 return false;
1678}
1679
Chris Lattnerb8211f62009-02-16 22:14:05 +00001680/// isArrow - Return true if the base expression is a pointer to vector,
1681/// return false if the base expression is a vector.
1682bool ExtVectorElementExpr::isArrow() const {
1683 return getBase()->getType()->isPointerType();
1684}
1685
Nate Begemance4d7fc2008-04-18 23:10:10 +00001686unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001687 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001688 return VT->getNumElements();
1689 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001690}
1691
Nate Begemanf322eab2008-05-09 06:41:27 +00001692/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001693bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001694 // FIXME: Refactor this code to an accessor on the AST node which returns the
1695 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001696 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001697
1698 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001699 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001700 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001701
Nate Begeman7e5185b2009-01-18 02:01:21 +00001702 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001703 if (Comp[0] == 's' || Comp[0] == 'S')
1704 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001705
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001706 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1707 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001708 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001709
Steve Naroff0d595ca2007-07-30 03:29:09 +00001710 return false;
1711}
Chris Lattner885b4952007-08-02 23:36:59 +00001712
Nate Begemanf322eab2008-05-09 06:41:27 +00001713/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001714void ExtVectorElementExpr::getEncodedElementAccess(
1715 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001716 llvm::StringRef Comp = Accessor->getName();
1717 if (Comp[0] == 's' || Comp[0] == 'S')
1718 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001719
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001720 bool isHi = Comp == "hi";
1721 bool isLo = Comp == "lo";
1722 bool isEven = Comp == "even";
1723 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001724
Nate Begemanf322eab2008-05-09 06:41:27 +00001725 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1726 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001727
Nate Begemanf322eab2008-05-09 06:41:27 +00001728 if (isHi)
1729 Index = e + i;
1730 else if (isLo)
1731 Index = i;
1732 else if (isEven)
1733 Index = 2 * i;
1734 else if (isOdd)
1735 Index = 2 * i + 1;
1736 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001737 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001738
Nate Begemand3862152008-05-13 21:03:02 +00001739 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001740 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001741}
1742
Douglas Gregor9a129192010-04-21 00:45:42 +00001743ObjCMessageExpr::ObjCMessageExpr(QualType T,
1744 SourceLocation LBracLoc,
1745 SourceLocation SuperLoc,
1746 bool IsInstanceSuper,
1747 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001748 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001749 ObjCMethodDecl *Method,
1750 Expr **Args, unsigned NumArgs,
1751 SourceLocation RBracLoc)
1752 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001753 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001754 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1755 HasMethod(Method != 0), SuperLoc(SuperLoc),
1756 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1757 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001758 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001759{
Douglas Gregor9a129192010-04-21 00:45:42 +00001760 setReceiverPointer(SuperType.getAsOpaquePtr());
1761 if (NumArgs)
1762 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001763}
1764
Douglas Gregor9a129192010-04-21 00:45:42 +00001765ObjCMessageExpr::ObjCMessageExpr(QualType T,
1766 SourceLocation LBracLoc,
1767 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001768 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001769 ObjCMethodDecl *Method,
1770 Expr **Args, unsigned NumArgs,
1771 SourceLocation RBracLoc)
1772 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001773 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001774 hasAnyValueDependentArguments(Args, NumArgs))),
1775 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1776 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1777 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001778 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001779{
1780 setReceiverPointer(Receiver);
1781 if (NumArgs)
1782 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001783}
1784
Douglas Gregor9a129192010-04-21 00:45:42 +00001785ObjCMessageExpr::ObjCMessageExpr(QualType T,
1786 SourceLocation LBracLoc,
1787 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001788 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001789 ObjCMethodDecl *Method,
1790 Expr **Args, unsigned NumArgs,
1791 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001792 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001793 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001794 hasAnyValueDependentArguments(Args, NumArgs))),
1795 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1796 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1797 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001798 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001799{
1800 setReceiverPointer(Receiver);
1801 if (NumArgs)
1802 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001803}
1804
Douglas Gregor9a129192010-04-21 00:45:42 +00001805ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1806 SourceLocation LBracLoc,
1807 SourceLocation SuperLoc,
1808 bool IsInstanceSuper,
1809 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001810 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001811 ObjCMethodDecl *Method,
1812 Expr **Args, unsigned NumArgs,
1813 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001814 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001815 NumArgs * sizeof(Expr *);
1816 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1817 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001818 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00001819 RBracLoc);
1820}
1821
1822ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1823 SourceLocation LBracLoc,
1824 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001825 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001826 ObjCMethodDecl *Method,
1827 Expr **Args, unsigned NumArgs,
1828 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001829 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001830 NumArgs * sizeof(Expr *);
1831 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001832 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001833 NumArgs, RBracLoc);
1834}
1835
1836ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
1837 SourceLocation LBracLoc,
1838 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001839 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001840 ObjCMethodDecl *Method,
1841 Expr **Args, unsigned NumArgs,
1842 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001843 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001844 NumArgs * sizeof(Expr *);
1845 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001846 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00001847 NumArgs, RBracLoc);
1848}
1849
Alexis Hunta8136cc2010-05-05 15:23:54 +00001850ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00001851 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00001852 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00001853 NumArgs * sizeof(Expr *);
1854 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
1855 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
1856}
Alexis Hunta8136cc2010-05-05 15:23:54 +00001857
Douglas Gregor9a129192010-04-21 00:45:42 +00001858Selector ObjCMessageExpr::getSelector() const {
1859 if (HasMethod)
1860 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
1861 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001862 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00001863}
1864
1865ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
1866 switch (getReceiverKind()) {
1867 case Instance:
1868 if (const ObjCObjectPointerType *Ptr
1869 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
1870 return Ptr->getInterfaceDecl();
1871 break;
1872
1873 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00001874 if (const ObjCObjectType *Ty
1875 = getClassReceiver()->getAs<ObjCObjectType>())
1876 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00001877 break;
1878
1879 case SuperInstance:
1880 if (const ObjCObjectPointerType *Ptr
1881 = getSuperType()->getAs<ObjCObjectPointerType>())
1882 return Ptr->getInterfaceDecl();
1883 break;
1884
1885 case SuperClass:
1886 if (const ObjCObjectPointerType *Iface
1887 = getSuperType()->getAs<ObjCObjectPointerType>())
1888 return Iface->getInterfaceDecl();
1889 break;
1890 }
1891
1892 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00001893}
Chris Lattner7ec71da2009-04-26 00:44:05 +00001894
Chris Lattner35e564e2007-10-25 00:29:32 +00001895bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00001896 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00001897}
1898
Nate Begeman48745922009-08-12 02:28:50 +00001899void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
1900 unsigned NumExprs) {
1901 if (SubExprs) C.Deallocate(SubExprs);
1902
1903 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00001904 this->NumExprs = NumExprs;
1905 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00001906}
Nate Begeman48745922009-08-12 02:28:50 +00001907
Ted Kremenek85e92ec2007-08-24 18:13:47 +00001908//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001909// DesignatedInitExpr
1910//===----------------------------------------------------------------------===//
1911
1912IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
1913 assert(Kind == FieldDesignator && "Only valid on a field designator");
1914 if (Field.NameOrField & 0x01)
1915 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
1916 else
1917 return getField()->getIdentifier();
1918}
1919
Alexis Hunta8136cc2010-05-05 15:23:54 +00001920DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001921 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00001922 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00001923 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00001924 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00001925 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001926 unsigned NumIndexExprs,
1927 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00001928 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001929 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00001930 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
1931 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001932 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001933
1934 // Record the initializer itself.
1935 child_iterator Child = child_begin();
1936 *Child++ = Init;
1937
1938 // Copy the designators and their subexpressions, computing
1939 // value-dependence along the way.
1940 unsigned IndexIdx = 0;
1941 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00001942 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001943
1944 if (this->Designators[I].isArrayDesignator()) {
1945 // Compute type- and value-dependence.
1946 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00001947 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001948 Index->isTypeDependent() || Index->isValueDependent();
1949
1950 // Copy the index expressions into permanent storage.
1951 *Child++ = IndexExprs[IndexIdx++];
1952 } else if (this->Designators[I].isArrayRangeDesignator()) {
1953 // Compute type- and value-dependence.
1954 Expr *Start = IndexExprs[IndexIdx];
1955 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00001956 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001957 Start->isTypeDependent() || Start->isValueDependent() ||
1958 End->isTypeDependent() || End->isValueDependent();
1959
1960 // Copy the start/end expressions into permanent storage.
1961 *Child++ = IndexExprs[IndexIdx++];
1962 *Child++ = IndexExprs[IndexIdx++];
1963 }
1964 }
1965
1966 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00001967}
1968
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001969DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00001970DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001971 unsigned NumDesignators,
1972 Expr **IndexExprs, unsigned NumIndexExprs,
1973 SourceLocation ColonOrEqualLoc,
1974 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001975 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00001976 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001977 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00001978 ColonOrEqualLoc, UsesColonSyntax,
1979 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001980}
1981
Mike Stump11289f42009-09-09 15:08:12 +00001982DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00001983 unsigned NumIndexExprs) {
1984 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
1985 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
1986 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
1987}
1988
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001989void DesignatedInitExpr::setDesignators(ASTContext &C,
1990 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00001991 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00001992 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00001993 NumDesignators = NumDesigs;
1994 for (unsigned I = 0; I != NumDesigs; ++I)
1995 Designators[I] = Desigs[I];
1996}
1997
Douglas Gregore4a0bb72009-01-22 00:58:24 +00001998SourceRange DesignatedInitExpr::getSourceRange() const {
1999 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002000 Designator &First =
2001 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002002 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002003 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002004 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2005 else
2006 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2007 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002008 StartLoc =
2009 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002010 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2011}
2012
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002013Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2014 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2015 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2016 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002017 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2018 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2019}
2020
2021Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002022 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002023 "Requires array range designator");
2024 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2025 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002026 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2027 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2028}
2029
2030Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002031 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002032 "Requires array range designator");
2033 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2034 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002035 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2036 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2037}
2038
Douglas Gregord5846a12009-04-15 06:41:24 +00002039/// \brief Replaces the designator at index @p Idx with the series
2040/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002041void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002042 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002043 const Designator *Last) {
2044 unsigned NumNewDesignators = Last - First;
2045 if (NumNewDesignators == 0) {
2046 std::copy_backward(Designators + Idx + 1,
2047 Designators + NumDesignators,
2048 Designators + Idx);
2049 --NumNewDesignators;
2050 return;
2051 } else if (NumNewDesignators == 1) {
2052 Designators[Idx] = *First;
2053 return;
2054 }
2055
Mike Stump11289f42009-09-09 15:08:12 +00002056 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002057 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002058 std::copy(Designators, Designators + Idx, NewDesignators);
2059 std::copy(First, Last, NewDesignators + Idx);
2060 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2061 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002062 Designators = NewDesignators;
2063 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2064}
2065
Mike Stump11289f42009-09-09 15:08:12 +00002066ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002067 Expr **exprs, unsigned nexprs,
2068 SourceLocation rparenloc)
2069: Expr(ParenListExprClass, QualType(),
2070 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002071 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002072 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002073
Nate Begeman5ec4b312009-08-10 23:49:36 +00002074 Exprs = new (C) Stmt*[nexprs];
2075 for (unsigned i = 0; i != nexprs; ++i)
2076 Exprs[i] = exprs[i];
2077}
2078
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002079//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002080// ExprIterator.
2081//===----------------------------------------------------------------------===//
2082
2083Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2084Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2085Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2086const Expr* ConstExprIterator::operator[](size_t idx) const {
2087 return cast<Expr>(I[idx]);
2088}
2089const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2090const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2091
2092//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002093// Child Iterators for iterating over subexpressions/substatements
2094//===----------------------------------------------------------------------===//
2095
2096// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002097Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2098Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002099
Steve Naroffe46504b2007-11-12 14:29:37 +00002100// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002101Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2102Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002103
Steve Naroffebf4cb42008-06-02 23:03:37 +00002104// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002105Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2106Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002107
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002108// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002109Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00002110 // If this is accessing a class member, skip that entry.
2111 if (Base) return &Base;
2112 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002113}
Mike Stump11289f42009-09-09 15:08:12 +00002114Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2115 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002116}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002117
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002118// ObjCSuperExpr
2119Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2120Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2121
Steve Naroffe87026a2009-07-24 17:54:45 +00002122// ObjCIsaExpr
2123Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2124Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2125
Chris Lattner6307f192008-08-10 01:53:14 +00002126// PredefinedExpr
2127Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2128Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002129
2130// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002131Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2132Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002133
2134// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002135Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002136Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002137
2138// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002139Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2140Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002141
Chris Lattner1c20a172007-08-26 03:42:43 +00002142// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002143Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2144Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002145
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002146// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002147Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2148Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002149
2150// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002151Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2152Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002153
2154// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002155Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2156Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002157
Douglas Gregor882211c2010-04-28 22:16:22 +00002158// OffsetOfExpr
2159Stmt::child_iterator OffsetOfExpr::child_begin() {
2160 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2161 + NumComps);
2162}
2163Stmt::child_iterator OffsetOfExpr::child_end() {
2164 return child_iterator(&*child_begin() + NumExprs);
2165}
2166
Sebastian Redl6f282892008-11-11 17:56:53 +00002167// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002168Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002169 // If this is of a type and the type is a VLA type (and not a typedef), the
2170 // size expression of the VLA needs to be treated as an executable expression.
2171 // Why isn't this weirdness documented better in StmtIterator?
2172 if (isArgumentType()) {
2173 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2174 getArgumentType().getTypePtr()))
2175 return child_iterator(T);
2176 return child_iterator();
2177 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002178 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002179}
Sebastian Redl6f282892008-11-11 17:56:53 +00002180Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2181 if (isArgumentType())
2182 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002183 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002184}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002185
2186// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002187Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002188 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002189}
Ted Kremenek23702b62007-08-24 20:06:47 +00002190Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002191 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002192}
2193
2194// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002195Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002196 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002197}
Ted Kremenek23702b62007-08-24 20:06:47 +00002198Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002199 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002200}
Ted Kremenek23702b62007-08-24 20:06:47 +00002201
2202// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002203Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2204Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002205
Nate Begemance4d7fc2008-04-18 23:10:10 +00002206// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002207Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2208Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002209
2210// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002211Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2212Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002213
Ted Kremenek23702b62007-08-24 20:06:47 +00002214// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002215Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2216Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002217
2218// BinaryOperator
2219Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002220 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002221}
Ted Kremenek23702b62007-08-24 20:06:47 +00002222Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002223 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002224}
2225
2226// ConditionalOperator
2227Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002228 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002229}
Ted Kremenek23702b62007-08-24 20:06:47 +00002230Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002231 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002232}
2233
2234// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002235Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2236Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002237
Ted Kremenek23702b62007-08-24 20:06:47 +00002238// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002239Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2240Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002241
2242// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002243Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2244 return child_iterator();
2245}
2246
2247Stmt::child_iterator TypesCompatibleExpr::child_end() {
2248 return child_iterator();
2249}
Ted Kremenek23702b62007-08-24 20:06:47 +00002250
2251// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002252Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2253Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002254
Douglas Gregor3be4b122008-11-29 04:51:27 +00002255// GNUNullExpr
2256Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2257Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2258
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002259// ShuffleVectorExpr
2260Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002261 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002262}
2263Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002264 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002265}
2266
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002267// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002268Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2269Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002270
Anders Carlsson4692db02007-08-31 04:56:16 +00002271// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002272Stmt::child_iterator InitListExpr::child_begin() {
2273 return InitExprs.size() ? &InitExprs[0] : 0;
2274}
2275Stmt::child_iterator InitListExpr::child_end() {
2276 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2277}
Anders Carlsson4692db02007-08-31 04:56:16 +00002278
Douglas Gregor0202cb42009-01-29 17:44:32 +00002279// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002280Stmt::child_iterator DesignatedInitExpr::child_begin() {
2281 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2282 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002283 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2284}
2285Stmt::child_iterator DesignatedInitExpr::child_end() {
2286 return child_iterator(&*child_begin() + NumSubExprs);
2287}
2288
Douglas Gregor0202cb42009-01-29 17:44:32 +00002289// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002290Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2291 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002292}
2293
Mike Stump11289f42009-09-09 15:08:12 +00002294Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2295 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002296}
2297
Nate Begeman5ec4b312009-08-10 23:49:36 +00002298// ParenListExpr
2299Stmt::child_iterator ParenListExpr::child_begin() {
2300 return &Exprs[0];
2301}
2302Stmt::child_iterator ParenListExpr::child_end() {
2303 return &Exprs[0]+NumExprs;
2304}
2305
Ted Kremenek23702b62007-08-24 20:06:47 +00002306// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002307Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002308 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002309}
2310Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002311 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002312}
Ted Kremenek23702b62007-08-24 20:06:47 +00002313
2314// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002315Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2316Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002317
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002318// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002319Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002320 return child_iterator();
2321}
2322Stmt::child_iterator ObjCSelectorExpr::child_end() {
2323 return child_iterator();
2324}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002325
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002326// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002327Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2328 return child_iterator();
2329}
2330Stmt::child_iterator ObjCProtocolExpr::child_end() {
2331 return child_iterator();
2332}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002333
Steve Naroffd54978b2007-09-18 23:55:05 +00002334// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002335Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002336 if (getReceiverKind() == Instance)
2337 return reinterpret_cast<Stmt **>(this + 1);
2338 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002339}
2340Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002341 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002342}
2343
Steve Naroffc540d662008-09-03 18:15:37 +00002344// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002345Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2346Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002347
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002348Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2349Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }