blob: cc209a458a121daa6c0817b1b1ced1474d160b48 [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() {
John McCall925b16622010-10-26 08:39:16 +0000127 ExprBits.TypeDependent = false;
128 ExprBits.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()) {
John McCall925b16622010-10-26 08:39:16 +0000143 ExprBits.TypeDependent = true;
144 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000145 }
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()) {
John McCall925b16622010-10-26 08:39:16 +0000150 ExprBits.TypeDependent = true;
151 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000152 }
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())) {
John McCall925b16622010-10-26 08:39:16 +0000158 ExprBits.TypeDependent = true;
159 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000160 }
161 // (VD) - the name of a non-type template parameter,
162 else if (isa<NonTypeTemplateParmDecl>(D))
John McCall925b16622010-10-26 08:39:16 +0000163 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000164 // (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())
John McCall925b16622010-10-26 08:39:16 +0000171 ExprBits.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())
John McCall925b16622010-10-26 08:39:16 +0000178 ExprBits.ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000179 }
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())
John McCall925b16622010-10-26 08:39:16 +0000184 ExprBits.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
Chris Lattner5c0b4052010-10-30 05:14:06 +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
Chris Lattner5c0b4052010-10-30 05:14:06 +0000274 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000275 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),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000435 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000436 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),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000455 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000456 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();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000562 // If we're calling a dereference, look at the pointer instead.
563 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
564 if (BO->isPtrMemOp())
565 CEE = BO->getRHS()->IgnoreParenCasts();
566 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
567 if (UO->getOpcode() == UO_Deref)
568 CEE = UO->getSubExpr()->IgnoreParenCasts();
569 }
Chris Lattner52301912009-07-17 15:46:27 +0000570 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000571 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000572 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
573 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000574
575 return 0;
576}
577
Nuno Lopes518e3702009-12-20 23:11:08 +0000578FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000579 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000580}
581
Chris Lattnere4407ed2007-12-28 05:25:02 +0000582/// setNumArgs - This changes the number of arguments present in this call.
583/// Any orphaned expressions are deleted by this, and any new operands are set
584/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000585void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000586 // No change, just return.
587 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattnere4407ed2007-12-28 05:25:02 +0000589 // If shrinking # arguments, just delete the extras and forgot them.
590 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000591 this->NumArgs = NumArgs;
592 return;
593 }
594
595 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000596 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000597 // Copy over args.
598 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
599 NewSubExprs[i] = SubExprs[i];
600 // Null out new args.
601 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
602 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000603
Douglas Gregorba6e5572009-04-17 21:46:47 +0000604 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000605 SubExprs = NewSubExprs;
606 this->NumArgs = NumArgs;
607}
608
Chris Lattner01ff98a2008-10-06 05:00:53 +0000609/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
610/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000611unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000612 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000613 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000614 // ImplicitCastExpr.
615 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
616 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000617 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Steve Narofff6e3b3292008-01-31 01:07:12 +0000619 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
620 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000621 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000623 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
624 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000625 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000627 if (!FDecl->getIdentifier())
628 return 0;
629
Douglas Gregor15fc9562009-09-12 00:22:50 +0000630 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000631}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000632
Anders Carlsson00a27592009-05-26 04:57:27 +0000633QualType CallExpr::getCallReturnType() const {
634 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000635 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000636 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000637 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000638 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000639 else if (const MemberPointerType *MPT
640 = CalleeType->getAs<MemberPointerType>())
641 CalleeType = MPT->getPointeeType();
642
John McCall9dd450b2009-09-21 23:43:11 +0000643 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000644 return FnType->getResultType();
645}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000646
Alexis Hunta8136cc2010-05-05 15:23:54 +0000647OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000648 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000649 TypeSourceInfo *tsi,
650 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000651 Expr** exprsPtr, unsigned numExprs,
652 SourceLocation RParenLoc) {
653 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000654 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000655 sizeof(Expr*) * numExprs);
656
657 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
658 exprsPtr, numExprs, RParenLoc);
659}
660
661OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
662 unsigned numComps, unsigned numExprs) {
663 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
664 sizeof(OffsetOfNode) * numComps +
665 sizeof(Expr*) * numExprs);
666 return new (Mem) OffsetOfExpr(numComps, numExprs);
667}
668
Alexis Hunta8136cc2010-05-05 15:23:54 +0000669OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000670 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000671 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000672 Expr** exprsPtr, unsigned numExprs,
673 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000674 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000675 /*ValueDependent=*/tsi->getType()->isDependentType() ||
676 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
677 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000678 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
679 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000680{
681 for(unsigned i = 0; i < numComps; ++i) {
682 setComponent(i, compsPtr[i]);
683 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000684
Douglas Gregor882211c2010-04-28 22:16:22 +0000685 for(unsigned i = 0; i < numExprs; ++i) {
686 setIndexExpr(i, exprsPtr[i]);
687 }
688}
689
690IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
691 assert(getKind() == Field || getKind() == Identifier);
692 if (getKind() == Field)
693 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000694
Douglas Gregor882211c2010-04-28 22:16:22 +0000695 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
696}
697
Mike Stump11289f42009-09-09 15:08:12 +0000698MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
699 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000700 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000701 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000702 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000703 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000704 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000705 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000706 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000707
John McCalla8ae2222010-04-06 21:38:20 +0000708 bool hasQualOrFound = (qual != 0 ||
709 founddecl.getDecl() != memberdecl ||
710 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000711 if (hasQualOrFound)
712 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000713
John McCall6b51f282009-11-23 01:53:49 +0000714 if (targs)
715 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattner5c0b4052010-10-30 05:14:06 +0000717 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000718 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo, ty);
John McCall16df1e52010-03-30 21:47:33 +0000719
720 if (hasQualOrFound) {
721 if (qual && qual->isDependent()) {
722 E->setValueDependent(true);
723 E->setTypeDependent(true);
724 }
725 E->HasQualifierOrFoundDecl = true;
726
727 MemberNameQualifier *NQ = E->getMemberQualifier();
728 NQ->NNS = qual;
729 NQ->Range = qualrange;
730 NQ->FoundDecl = founddecl;
731 }
732
733 if (targs) {
734 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000735 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000736 }
737
738 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000739}
740
Anders Carlsson496335e2009-09-03 00:59:21 +0000741const char *CastExpr::getCastKindName() const {
742 switch (getCastKind()) {
John McCalle3027922010-08-25 11:45:40 +0000743 case CK_Unknown:
Anders Carlsson496335e2009-09-03 00:59:21 +0000744 return "Unknown";
John McCalle3027922010-08-25 11:45:40 +0000745 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000746 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000747 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000748 return "LValueBitCast";
John McCalle3027922010-08-25 11:45:40 +0000749 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000750 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000751 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000752 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000753 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000754 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000755 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000756 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000757 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000758 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000759 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000760 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000761 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000762 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000763 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000764 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000765 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000766 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000767 case CK_NullToPointer:
768 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000769 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000770 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000771 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000772 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000773 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000774 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000775 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000776 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000777 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000778 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000779 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000780 return "PointerToIntegral";
John McCalle3027922010-08-25 11:45:40 +0000781 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +0000782 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +0000783 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +0000784 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +0000785 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +0000786 return "IntegralCast";
John McCalle3027922010-08-25 11:45:40 +0000787 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +0000788 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +0000789 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +0000790 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +0000791 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000792 return "FloatingCast";
John McCalle3027922010-08-25 11:45:40 +0000793 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000794 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000795 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000796 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000797 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000798 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000799 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +0000800 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +0000801 case CK_FloatingRealToComplex:
802 return "FloatingRealToComplex";
803 case CK_FloatingComplexCast:
804 return "FloatingComplexCast";
805 case CK_IntegralRealToComplex:
806 return "IntegralRealToComplex";
807 case CK_IntegralComplexCast:
808 return "IntegralComplexCast";
809 case CK_IntegralToFloatingComplex:
810 return "IntegralToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +0000811 }
Mike Stump11289f42009-09-09 15:08:12 +0000812
John McCallc5e62b42010-11-13 09:02:35 +0000813 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +0000814 return 0;
815}
816
Douglas Gregord196a582009-12-14 19:27:10 +0000817Expr *CastExpr::getSubExprAsWritten() {
818 Expr *SubExpr = 0;
819 CastExpr *E = this;
820 do {
821 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000822
Douglas Gregord196a582009-12-14 19:27:10 +0000823 // Skip any temporary bindings; they're implicit.
824 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
825 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000826
Douglas Gregord196a582009-12-14 19:27:10 +0000827 // Conversions by constructor and conversion functions have a
828 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +0000829 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000830 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +0000831 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000832 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000833
Douglas Gregord196a582009-12-14 19:27:10 +0000834 // If the subexpression we're left with is an implicit cast, look
835 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000836 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
837
Douglas Gregord196a582009-12-14 19:27:10 +0000838 return SubExpr;
839}
840
John McCallcf142162010-08-07 06:22:56 +0000841CXXBaseSpecifier **CastExpr::path_buffer() {
842 switch (getStmtClass()) {
843#define ABSTRACT_STMT(x)
844#define CASTEXPR(Type, Base) \
845 case Stmt::Type##Class: \
846 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
847#define STMT(Type, Base)
848#include "clang/AST/StmtNodes.inc"
849 default:
850 llvm_unreachable("non-cast expressions not possible here");
851 return 0;
852 }
853}
854
855void CastExpr::setCastPath(const CXXCastPath &Path) {
856 assert(Path.size() == path_size());
857 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
858}
859
860ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
861 CastKind Kind, Expr *Operand,
862 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +0000863 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +0000864 unsigned PathSize = (BasePath ? BasePath->size() : 0);
865 void *Buffer =
866 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
867 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +0000868 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +0000869 if (PathSize) E->setCastPath(*BasePath);
870 return E;
871}
872
873ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
874 unsigned PathSize) {
875 void *Buffer =
876 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
877 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
878}
879
880
881CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
882 CastKind K, Expr *Op,
883 const CXXCastPath *BasePath,
884 TypeSourceInfo *WrittenTy,
885 SourceLocation L, SourceLocation R) {
886 unsigned PathSize = (BasePath ? BasePath->size() : 0);
887 void *Buffer =
888 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
889 CStyleCastExpr *E =
890 new (Buffer) CStyleCastExpr(T, K, Op, PathSize, WrittenTy, L, R);
891 if (PathSize) E->setCastPath(*BasePath);
892 return E;
893}
894
895CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
896 void *Buffer =
897 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
898 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
899}
900
Chris Lattner1b926492006-08-23 06:42:10 +0000901/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
902/// corresponds to, e.g. "<<=".
903const char *BinaryOperator::getOpcodeStr(Opcode Op) {
904 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000905 case BO_PtrMemD: return ".*";
906 case BO_PtrMemI: return "->*";
907 case BO_Mul: return "*";
908 case BO_Div: return "/";
909 case BO_Rem: return "%";
910 case BO_Add: return "+";
911 case BO_Sub: return "-";
912 case BO_Shl: return "<<";
913 case BO_Shr: return ">>";
914 case BO_LT: return "<";
915 case BO_GT: return ">";
916 case BO_LE: return "<=";
917 case BO_GE: return ">=";
918 case BO_EQ: return "==";
919 case BO_NE: return "!=";
920 case BO_And: return "&";
921 case BO_Xor: return "^";
922 case BO_Or: return "|";
923 case BO_LAnd: return "&&";
924 case BO_LOr: return "||";
925 case BO_Assign: return "=";
926 case BO_MulAssign: return "*=";
927 case BO_DivAssign: return "/=";
928 case BO_RemAssign: return "%=";
929 case BO_AddAssign: return "+=";
930 case BO_SubAssign: return "-=";
931 case BO_ShlAssign: return "<<=";
932 case BO_ShrAssign: return ">>=";
933 case BO_AndAssign: return "&=";
934 case BO_XorAssign: return "^=";
935 case BO_OrAssign: return "|=";
936 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +0000937 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000938
939 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000940}
Steve Naroff47500512007-04-19 23:00:49 +0000941
John McCalle3027922010-08-25 11:45:40 +0000942BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000943BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
944 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000945 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +0000946 case OO_Plus: return BO_Add;
947 case OO_Minus: return BO_Sub;
948 case OO_Star: return BO_Mul;
949 case OO_Slash: return BO_Div;
950 case OO_Percent: return BO_Rem;
951 case OO_Caret: return BO_Xor;
952 case OO_Amp: return BO_And;
953 case OO_Pipe: return BO_Or;
954 case OO_Equal: return BO_Assign;
955 case OO_Less: return BO_LT;
956 case OO_Greater: return BO_GT;
957 case OO_PlusEqual: return BO_AddAssign;
958 case OO_MinusEqual: return BO_SubAssign;
959 case OO_StarEqual: return BO_MulAssign;
960 case OO_SlashEqual: return BO_DivAssign;
961 case OO_PercentEqual: return BO_RemAssign;
962 case OO_CaretEqual: return BO_XorAssign;
963 case OO_AmpEqual: return BO_AndAssign;
964 case OO_PipeEqual: return BO_OrAssign;
965 case OO_LessLess: return BO_Shl;
966 case OO_GreaterGreater: return BO_Shr;
967 case OO_LessLessEqual: return BO_ShlAssign;
968 case OO_GreaterGreaterEqual: return BO_ShrAssign;
969 case OO_EqualEqual: return BO_EQ;
970 case OO_ExclaimEqual: return BO_NE;
971 case OO_LessEqual: return BO_LE;
972 case OO_GreaterEqual: return BO_GE;
973 case OO_AmpAmp: return BO_LAnd;
974 case OO_PipePipe: return BO_LOr;
975 case OO_Comma: return BO_Comma;
976 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000977 }
978}
979
980OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
981 static const OverloadedOperatorKind OverOps[] = {
982 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
983 OO_Star, OO_Slash, OO_Percent,
984 OO_Plus, OO_Minus,
985 OO_LessLess, OO_GreaterGreater,
986 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
987 OO_EqualEqual, OO_ExclaimEqual,
988 OO_Amp,
989 OO_Caret,
990 OO_Pipe,
991 OO_AmpAmp,
992 OO_PipePipe,
993 OO_Equal, OO_StarEqual,
994 OO_SlashEqual, OO_PercentEqual,
995 OO_PlusEqual, OO_MinusEqual,
996 OO_LessLessEqual, OO_GreaterGreaterEqual,
997 OO_AmpEqual, OO_CaretEqual,
998 OO_PipeEqual,
999 OO_Comma
1000 };
1001 return OverOps[Opc];
1002}
1003
Ted Kremenekac034612010-04-13 23:39:13 +00001004InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001005 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001006 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001007 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001008 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001009 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001010 UnionFieldInit(0), HadArrayRangeDesignator(false)
1011{
Ted Kremenek013041e2010-02-19 01:50:18 +00001012 for (unsigned I = 0; I != numInits; ++I) {
1013 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001014 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001015 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001016 ExprBits.ValueDependent = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001017 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001018
Ted Kremenekac034612010-04-13 23:39:13 +00001019 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001020}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001021
Ted Kremenekac034612010-04-13 23:39:13 +00001022void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001023 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001024 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001025}
1026
Ted Kremenekac034612010-04-13 23:39:13 +00001027void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001028 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001029}
1030
Ted Kremenekac034612010-04-13 23:39:13 +00001031Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001032 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001033 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001034 InitExprs.back() = expr;
1035 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001036 }
Mike Stump11289f42009-09-09 15:08:12 +00001037
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001038 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1039 InitExprs[Init] = expr;
1040 return Result;
1041}
1042
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001043SourceRange InitListExpr::getSourceRange() const {
1044 if (SyntacticForm)
1045 return SyntacticForm->getSourceRange();
1046 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1047 if (Beg.isInvalid()) {
1048 // Find the first non-null initializer.
1049 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1050 E = InitExprs.end();
1051 I != E; ++I) {
1052 if (Stmt *S = *I) {
1053 Beg = S->getLocStart();
1054 break;
1055 }
1056 }
1057 }
1058 if (End.isInvalid()) {
1059 // Find the first non-null initializer from the end.
1060 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1061 E = InitExprs.rend();
1062 I != E; ++I) {
1063 if (Stmt *S = *I) {
1064 End = S->getSourceRange().getEnd();
1065 break;
1066 }
1067 }
1068 }
1069 return SourceRange(Beg, End);
1070}
1071
Steve Naroff991e99d2008-09-04 15:31:07 +00001072/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001073///
1074const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001075 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001076 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001077}
1078
Mike Stump11289f42009-09-09 15:08:12 +00001079SourceLocation BlockExpr::getCaretLocation() const {
1080 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001081}
Mike Stump11289f42009-09-09 15:08:12 +00001082const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001083 return TheBlock->getBody();
1084}
Mike Stump11289f42009-09-09 15:08:12 +00001085Stmt *BlockExpr::getBody() {
1086 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001087}
Steve Naroff415d3d52008-10-08 17:01:13 +00001088
1089
Chris Lattner1ec5f562007-06-27 05:38:08 +00001090//===----------------------------------------------------------------------===//
1091// Generic Expression Routines
1092//===----------------------------------------------------------------------===//
1093
Chris Lattner237f2752009-02-14 07:37:35 +00001094/// isUnusedResultAWarning - Return true if this immediate expression should
1095/// be warned about if the result is unused. If so, fill in Loc and Ranges
1096/// with location to warn on and the source range[s] to report with the
1097/// warning.
1098bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001099 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001100 // Don't warn if the expr is type dependent. The type could end up
1101 // instantiating to void.
1102 if (isTypeDependent())
1103 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner1ec5f562007-06-27 05:38:08 +00001105 switch (getStmtClass()) {
1106 default:
John McCallc493a732010-03-12 07:11:26 +00001107 if (getType()->isVoidType())
1108 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001109 Loc = getExprLoc();
1110 R1 = getSourceRange();
1111 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001112 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001113 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001114 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001115 case UnaryOperatorClass: {
1116 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner1ec5f562007-06-27 05:38:08 +00001118 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001119 default: break;
John McCalle3027922010-08-25 11:45:40 +00001120 case UO_PostInc:
1121 case UO_PostDec:
1122 case UO_PreInc:
1123 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001124 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001125 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001126 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001127 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001128 return false;
1129 break;
John McCalle3027922010-08-25 11:45:40 +00001130 case UO_Real:
1131 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001132 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001133 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1134 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001135 return false;
1136 break;
John McCalle3027922010-08-25 11:45:40 +00001137 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001138 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001139 }
Chris Lattner237f2752009-02-14 07:37:35 +00001140 Loc = UO->getOperatorLoc();
1141 R1 = UO->getSubExpr()->getSourceRange();
1142 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001143 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001144 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001145 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001146 switch (BO->getOpcode()) {
1147 default:
1148 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001149 // Consider the RHS of comma for side effects. LHS was checked by
1150 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001151 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001152 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1153 // lvalue-ness) of an assignment written in a macro.
1154 if (IntegerLiteral *IE =
1155 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1156 if (IE->getValue() == 0)
1157 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001158 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1159 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001160 case BO_LAnd:
1161 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001162 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1163 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1164 return false;
1165 break;
John McCall1e3715a2010-02-16 04:10:53 +00001166 }
Chris Lattner237f2752009-02-14 07:37:35 +00001167 if (BO->isAssignmentOp())
1168 return false;
1169 Loc = BO->getOperatorLoc();
1170 R1 = BO->getLHS()->getSourceRange();
1171 R2 = BO->getRHS()->getSourceRange();
1172 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001173 }
Chris Lattner86928112007-08-25 02:00:02 +00001174 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001175 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001176 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001177
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001178 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001179 // The condition must be evaluated, but if either the LHS or RHS is a
1180 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001181 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001182 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001183 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001184 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001185 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001186 }
1187
Chris Lattnera44d1162007-06-27 05:58:59 +00001188 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001189 // If the base pointer or element is to a volatile pointer/field, accessing
1190 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001191 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001192 return false;
1193 Loc = cast<MemberExpr>(this)->getMemberLoc();
1194 R1 = SourceRange(Loc, Loc);
1195 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1196 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner1ec5f562007-06-27 05:38:08 +00001198 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001199 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001200 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001201 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001202 return false;
1203 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1204 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1205 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1206 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001207
Chris Lattner1ec5f562007-06-27 05:38:08 +00001208 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001209 case CXXOperatorCallExprClass:
1210 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001211 // If this is a direct call, get the callee.
1212 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001213 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001214 // If the callee has attribute pure, const, or warn_unused_result, warn
1215 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001216 //
1217 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1218 // updated to match for QoI.
1219 if (FD->getAttr<WarnUnusedResultAttr>() ||
1220 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1221 Loc = CE->getCallee()->getLocStart();
1222 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001223
Chris Lattner1a6babf2009-10-13 04:53:48 +00001224 if (unsigned NumArgs = CE->getNumArgs())
1225 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1226 CE->getArg(NumArgs-1)->getLocEnd());
1227 return true;
1228 }
Chris Lattner237f2752009-02-14 07:37:35 +00001229 }
1230 return false;
1231 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001232
1233 case CXXTemporaryObjectExprClass:
1234 case CXXConstructExprClass:
1235 return false;
1236
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001237 case ObjCMessageExprClass: {
1238 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1239 const ObjCMethodDecl *MD = ME->getMethodDecl();
1240 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1241 Loc = getExprLoc();
1242 return true;
1243 }
Chris Lattner237f2752009-02-14 07:37:35 +00001244 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001247 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001248#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001249 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001250 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001251 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001252 Loc = Ref->getLocation();
1253 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1254 if (Ref->getBase())
1255 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001256#else
1257 Loc = getExprLoc();
1258 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001259#endif
1260 return true;
1261 }
Chris Lattner944d3062008-07-26 19:51:01 +00001262 case StmtExprClass: {
1263 // Statement exprs don't logically have side effects themselves, but are
1264 // sometimes used in macros in ways that give them a type that is unused.
1265 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1266 // however, if the result of the stmt expr is dead, we don't want to emit a
1267 // warning.
1268 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001269 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001270 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001271 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001272 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1273 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1274 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
John McCallc493a732010-03-12 07:11:26 +00001277 if (getType()->isVoidType())
1278 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001279 Loc = cast<StmtExpr>(this)->getLParenLoc();
1280 R1 = getSourceRange();
1281 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001282 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001283 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001284 // If this is an explicit cast to void, allow it. People do this when they
1285 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001286 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001287 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001288 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1289 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1290 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001291 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001292 if (getType()->isVoidType())
1293 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001294 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001295
Anders Carlsson6aa50392009-11-17 17:11:23 +00001296 // If this is a cast to void or a constructor conversion, check the operand.
1297 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001298 if (CE->getCastKind() == CK_ToVoid ||
1299 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001300 return (cast<CastExpr>(this)->getSubExpr()
1301 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001302 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1303 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1304 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001305 }
Mike Stump11289f42009-09-09 15:08:12 +00001306
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001307 case ImplicitCastExprClass:
1308 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001309 return (cast<ImplicitCastExpr>(this)
1310 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001311
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001312 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001313 return (cast<CXXDefaultArgExpr>(this)
1314 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001315
1316 case CXXNewExprClass:
1317 // FIXME: In theory, there might be new expressions that don't have side
1318 // effects (e.g. a placement new with an uninitialized POD).
1319 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001320 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001321 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001322 return (cast<CXXBindTemporaryExpr>(this)
1323 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001324 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001325 return (cast<CXXExprWithTemporaries>(this)
1326 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001327 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001328}
1329
Fariborz Jahanian07735332009-02-22 18:40:18 +00001330/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001331/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001332bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001333 switch (getStmtClass()) {
1334 default:
1335 return false;
1336 case ObjCIvarRefExprClass:
1337 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001338 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001339 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001340 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001341 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001342 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001343 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001344 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001345 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001346 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001347 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001348 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1349 if (VD->hasGlobalStorage())
1350 return true;
1351 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001352 // dereferencing to a pointer is always a gc'able candidate,
1353 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001354 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001355 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001356 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001357 return false;
1358 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001359 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001360 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001361 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001362 }
1363 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001364 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001365 }
1366}
Sebastian Redlce354af2010-09-10 20:55:33 +00001367
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001368bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1369 if (isTypeDependent())
1370 return false;
1371 return isLvalue(Ctx) == Expr::LV_MemberFunction;
1372}
1373
Sebastian Redlce354af2010-09-10 20:55:33 +00001374static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1375 Expr::CanThrowResult CT2) {
1376 // CanThrowResult constants are ordered so that the maximum is the correct
1377 // merge result.
1378 return CT1 > CT2 ? CT1 : CT2;
1379}
1380
1381static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1382 Expr *E = const_cast<Expr*>(CE);
1383 Expr::CanThrowResult R = Expr::CT_Cannot;
1384 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1385 I != IE && R != Expr::CT_Can; ++I) {
1386 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1387 }
1388 return R;
1389}
1390
1391static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1392 bool NullThrows = true) {
1393 if (!D)
1394 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1395
1396 // See if we can get a function type from the decl somehow.
1397 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1398 if (!VD) // If we have no clue what we're calling, assume the worst.
1399 return Expr::CT_Can;
1400
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001401 // As an extension, we assume that __attribute__((nothrow)) functions don't
1402 // throw.
1403 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1404 return Expr::CT_Cannot;
1405
Sebastian Redlce354af2010-09-10 20:55:33 +00001406 QualType T = VD->getType();
1407 const FunctionProtoType *FT;
1408 if ((FT = T->getAs<FunctionProtoType>())) {
1409 } else if (const PointerType *PT = T->getAs<PointerType>())
1410 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1411 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1412 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1413 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1414 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1415 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1416 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1417
1418 if (!FT)
1419 return Expr::CT_Can;
1420
1421 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1422}
1423
1424static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1425 if (DC->isTypeDependent())
1426 return Expr::CT_Dependent;
1427
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001428 if (!DC->getTypeAsWritten()->isReferenceType())
1429 return Expr::CT_Cannot;
1430
Sebastian Redlce354af2010-09-10 20:55:33 +00001431 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1432}
1433
1434static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1435 const CXXTypeidExpr *DC) {
1436 if (DC->isTypeOperand())
1437 return Expr::CT_Cannot;
1438
1439 Expr *Op = DC->getExprOperand();
1440 if (Op->isTypeDependent())
1441 return Expr::CT_Dependent;
1442
1443 const RecordType *RT = Op->getType()->getAs<RecordType>();
1444 if (!RT)
1445 return Expr::CT_Cannot;
1446
1447 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1448 return Expr::CT_Cannot;
1449
1450 if (Op->Classify(C).isPRValue())
1451 return Expr::CT_Cannot;
1452
1453 return Expr::CT_Can;
1454}
1455
1456Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1457 // C++ [expr.unary.noexcept]p3:
1458 // [Can throw] if in a potentially-evaluated context the expression would
1459 // contain:
1460 switch (getStmtClass()) {
1461 case CXXThrowExprClass:
1462 // - a potentially evaluated throw-expression
1463 return CT_Can;
1464
1465 case CXXDynamicCastExprClass: {
1466 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1467 // where T is a reference type, that requires a run-time check
1468 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1469 if (CT == CT_Can)
1470 return CT;
1471 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1472 }
1473
1474 case CXXTypeidExprClass:
1475 // - a potentially evaluated typeid expression applied to a glvalue
1476 // expression whose type is a polymorphic class type
1477 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1478
1479 // - a potentially evaluated call to a function, member function, function
1480 // pointer, or member function pointer that does not have a non-throwing
1481 // exception-specification
1482 case CallExprClass:
1483 case CXXOperatorCallExprClass:
1484 case CXXMemberCallExprClass: {
1485 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1486 if (CT == CT_Can)
1487 return CT;
1488 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1489 }
1490
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001491 case CXXConstructExprClass:
1492 case CXXTemporaryObjectExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001493 CanThrowResult CT = CanCalleeThrow(
1494 cast<CXXConstructExpr>(this)->getConstructor());
1495 if (CT == CT_Can)
1496 return CT;
1497 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1498 }
1499
1500 case CXXNewExprClass: {
1501 CanThrowResult CT = MergeCanThrow(
1502 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1503 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1504 /*NullThrows*/false));
1505 if (CT == CT_Can)
1506 return CT;
1507 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1508 }
1509
1510 case CXXDeleteExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001511 CanThrowResult CT = CanCalleeThrow(
1512 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1513 if (CT == CT_Can)
1514 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001515 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1516 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1517 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1518 Arg = Cast->getSubExpr();
1519 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1520 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1521 CanThrowResult CT2 = CanCalleeThrow(
1522 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1523 if (CT2 == CT_Can)
1524 return CT2;
1525 CT = MergeCanThrow(CT, CT2);
1526 }
1527 }
1528 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1529 }
1530
1531 case CXXBindTemporaryExprClass: {
1532 // The bound temporary has to be destroyed again, which might throw.
1533 CanThrowResult CT = CanCalleeThrow(
1534 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1535 if (CT == CT_Can)
1536 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001537 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1538 }
1539
1540 // ObjC message sends are like function calls, but never have exception
1541 // specs.
1542 case ObjCMessageExprClass:
1543 case ObjCPropertyRefExprClass:
1544 case ObjCImplicitSetterGetterRefExprClass:
1545 return CT_Can;
1546
1547 // Many other things have subexpressions, so we have to test those.
1548 // Some are simple:
1549 case ParenExprClass:
1550 case MemberExprClass:
1551 case CXXReinterpretCastExprClass:
1552 case CXXConstCastExprClass:
1553 case ConditionalOperatorClass:
1554 case CompoundLiteralExprClass:
1555 case ExtVectorElementExprClass:
1556 case InitListExprClass:
1557 case DesignatedInitExprClass:
1558 case ParenListExprClass:
1559 case VAArgExprClass:
1560 case CXXDefaultArgExprClass:
Sebastian Redla8bac372010-09-10 23:27:10 +00001561 case CXXExprWithTemporariesClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001562 case ObjCIvarRefExprClass:
1563 case ObjCIsaExprClass:
1564 case ShuffleVectorExprClass:
1565 return CanSubExprsThrow(C, this);
1566
1567 // Some might be dependent for other reasons.
1568 case UnaryOperatorClass:
1569 case ArraySubscriptExprClass:
1570 case ImplicitCastExprClass:
1571 case CStyleCastExprClass:
1572 case CXXStaticCastExprClass:
1573 case CXXFunctionalCastExprClass:
1574 case BinaryOperatorClass:
1575 case CompoundAssignOperatorClass: {
1576 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1577 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1578 }
1579
1580 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1581 case StmtExprClass:
1582 return CT_Can;
1583
1584 case ChooseExprClass:
1585 if (isTypeDependent() || isValueDependent())
1586 return CT_Dependent;
1587 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1588
1589 // Some expressions are always dependent.
1590 case DependentScopeDeclRefExprClass:
1591 case CXXUnresolvedConstructExprClass:
1592 case CXXDependentScopeMemberExprClass:
1593 return CT_Dependent;
1594
1595 default:
1596 // All other expressions don't have subexpressions, or else they are
1597 // unevaluated.
1598 return CT_Cannot;
1599 }
1600}
1601
Ted Kremenekfff70962008-01-17 16:57:34 +00001602Expr* Expr::IgnoreParens() {
1603 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001604 while (true) {
1605 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1606 E = P->getSubExpr();
1607 continue;
1608 }
1609 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1610 if (P->getOpcode() == UO_Extension) {
1611 E = P->getSubExpr();
1612 continue;
1613 }
1614 }
1615 return E;
1616 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001617}
1618
Chris Lattnerf2660962008-02-13 01:02:39 +00001619/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1620/// or CastExprs or ImplicitCastExprs, returning their operand.
1621Expr *Expr::IgnoreParenCasts() {
1622 Expr *E = this;
1623 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001624 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001625 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001626 continue;
1627 }
1628 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001629 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001630 continue;
1631 }
1632 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1633 if (P->getOpcode() == UO_Extension) {
1634 E = P->getSubExpr();
1635 continue;
1636 }
1637 }
1638 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001639 }
1640}
1641
John McCalleebc8322010-05-05 22:59:52 +00001642Expr *Expr::IgnoreParenImpCasts() {
1643 Expr *E = this;
1644 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001645 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001646 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001647 continue;
1648 }
1649 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001650 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001651 continue;
1652 }
1653 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1654 if (P->getOpcode() == UO_Extension) {
1655 E = P->getSubExpr();
1656 continue;
1657 }
1658 }
1659 return E;
John McCalleebc8322010-05-05 22:59:52 +00001660 }
1661}
1662
Chris Lattneref26c772009-03-13 17:28:01 +00001663/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1664/// value (including ptr->int casts of the same size). Strip off any
1665/// ParenExpr or CastExprs, returning their operand.
1666Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1667 Expr *E = this;
1668 while (true) {
1669 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1670 E = P->getSubExpr();
1671 continue;
1672 }
Mike Stump11289f42009-09-09 15:08:12 +00001673
Chris Lattneref26c772009-03-13 17:28:01 +00001674 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1675 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001676 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001677 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001678
Chris Lattneref26c772009-03-13 17:28:01 +00001679 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1680 E = SE;
1681 continue;
1682 }
Mike Stump11289f42009-09-09 15:08:12 +00001683
Abramo Bagnara932e3932010-10-15 07:51:18 +00001684 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001685 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00001686 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001687 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001688 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1689 E = SE;
1690 continue;
1691 }
1692 }
Mike Stump11289f42009-09-09 15:08:12 +00001693
Abramo Bagnara932e3932010-10-15 07:51:18 +00001694 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1695 if (P->getOpcode() == UO_Extension) {
1696 E = P->getSubExpr();
1697 continue;
1698 }
1699 }
1700
Chris Lattneref26c772009-03-13 17:28:01 +00001701 return E;
1702 }
1703}
1704
Douglas Gregord196a582009-12-14 19:27:10 +00001705bool Expr::isDefaultArgument() const {
1706 const Expr *E = this;
1707 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1708 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001709
Douglas Gregord196a582009-12-14 19:27:10 +00001710 return isa<CXXDefaultArgExpr>(E);
1711}
Chris Lattneref26c772009-03-13 17:28:01 +00001712
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001713/// \brief Skip over any no-op casts and any temporary-binding
1714/// expressions.
1715static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1716 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001717 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001718 E = ICE->getSubExpr();
1719 else
1720 break;
1721 }
1722
1723 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1724 E = BE->getSubExpr();
1725
1726 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001727 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001728 E = ICE->getSubExpr();
1729 else
1730 break;
1731 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001732
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001733 return E;
1734}
1735
John McCall7a626f62010-09-15 10:14:12 +00001736/// isTemporaryObject - Determines if this expression produces a
1737/// temporary of the given class type.
1738bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1739 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1740 return false;
1741
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001742 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1743
John McCall02dc8c72010-09-15 20:59:13 +00001744 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001745 if (!E->Classify(C).isPRValue()) {
1746 // In this context, property reference is a message call and is pr-value.
1747 if (!isa<ObjCPropertyRefExpr>(E) &&
1748 !isa<ObjCImplicitSetterGetterRefExpr>(E))
1749 return false;
1750 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001751
John McCallf4ee1dd2010-09-16 06:57:56 +00001752 // Black-list a few cases which yield pr-values of class type that don't
1753 // refer to temporaries of that type:
1754
1755 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00001756 if (isa<ImplicitCastExpr>(E)) {
1757 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1758 case CK_DerivedToBase:
1759 case CK_UncheckedDerivedToBase:
1760 return false;
1761 default:
1762 break;
1763 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001764 }
1765
John McCallf4ee1dd2010-09-16 06:57:56 +00001766 // - member expressions (all)
1767 if (isa<MemberExpr>(E))
1768 return false;
1769
John McCall7a626f62010-09-15 10:14:12 +00001770 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001771}
1772
Douglas Gregor4619e432008-12-05 23:32:09 +00001773/// hasAnyTypeDependentArguments - Determines if any of the expressions
1774/// in Exprs is type-dependent.
1775bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1776 for (unsigned I = 0; I < NumExprs; ++I)
1777 if (Exprs[I]->isTypeDependent())
1778 return true;
1779
1780 return false;
1781}
1782
1783/// hasAnyValueDependentArguments - Determines if any of the expressions
1784/// in Exprs is value-dependent.
1785bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1786 for (unsigned I = 0; I < NumExprs; ++I)
1787 if (Exprs[I]->isValueDependent())
1788 return true;
1789
1790 return false;
1791}
1792
John McCall8b0f4ff2010-08-02 21:13:48 +00001793bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00001794 // This function is attempting whether an expression is an initializer
1795 // which can be evaluated at compile-time. isEvaluatable handles most
1796 // of the cases, but it can't deal with some initializer-specific
1797 // expressions, and it can't deal with aggregates; we deal with those here,
1798 // and fall back to isEvaluatable for the other cases.
1799
John McCall8b0f4ff2010-08-02 21:13:48 +00001800 // If we ever capture reference-binding directly in the AST, we can
1801 // kill the second parameter.
1802
1803 if (IsForRef) {
1804 EvalResult Result;
1805 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1806 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001807
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001808 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001809 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001810 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001811 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001812 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001813 return true;
John McCall81c9cea2010-08-01 21:51:45 +00001814 case CXXTemporaryObjectExprClass:
1815 case CXXConstructExprClass: {
1816 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00001817
1818 // Only if it's
1819 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00001820 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00001821 if (!CE->getNumArgs()) return true;
1822
1823 // 2) an elidable trivial copy construction of an operand which is
1824 // itself a constant initializer. Note that we consider the
1825 // operand on its own, *not* as a reference binding.
1826 return CE->isElidable() &&
1827 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00001828 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001829 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001830 // This handles gcc's extension that allows global initializers like
1831 // "struct x {int x;} x = (struct x) {};".
1832 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001833 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00001834 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001835 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001836 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001837 // FIXME: This doesn't deal with fields with reference types correctly.
1838 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1839 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001840 const InitListExpr *Exp = cast<InitListExpr>(this);
1841 unsigned numInits = Exp->getNumInits();
1842 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00001843 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001844 return false;
1845 }
Eli Friedman384da272009-01-25 03:12:18 +00001846 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001847 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001848 case ImplicitValueInitExprClass:
1849 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001850 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00001851 return cast<ParenExpr>(this)->getSubExpr()
1852 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00001853 case ChooseExprClass:
1854 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
1855 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00001856 case UnaryOperatorClass: {
1857 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001858 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00001859 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00001860 break;
1861 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001862 case BinaryOperatorClass: {
1863 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1864 // but this handles the common case.
1865 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001866 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00001867 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1868 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1869 return true;
1870 break;
1871 }
John McCall8b0f4ff2010-08-02 21:13:48 +00001872 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00001873 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00001874 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001875 case CStyleCastExprClass:
1876 // Handle casts with a destination that's a struct or union; this
1877 // deals with both the gcc no-op struct cast extension and the
1878 // cast-to-union extension.
1879 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001880 return cast<CastExpr>(this)->getSubExpr()
1881 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001882
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001883 // Integer->integer casts can be handled here, which is important for
1884 // things like (int)(&&x-&&y). Scary but true.
1885 if (getType()->isIntegerType() &&
1886 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001887 return cast<CastExpr>(this)->getSubExpr()
1888 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001889
Eli Friedman384da272009-01-25 03:12:18 +00001890 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001891 }
Eli Friedman384da272009-01-25 03:12:18 +00001892 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001893}
1894
Chris Lattner7eef9192007-05-24 01:23:49 +00001895/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1896/// integer constant expression with the value zero, or if this is one that is
1897/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001898bool Expr::isNullPointerConstant(ASTContext &Ctx,
1899 NullPointerConstantValueDependence NPC) const {
1900 if (isValueDependent()) {
1901 switch (NPC) {
1902 case NPC_NeverValueDependent:
1903 assert(false && "Unexpected value dependent expression!");
1904 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001905
Douglas Gregor56751b52009-09-25 04:25:58 +00001906 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001907 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001908
Douglas Gregor56751b52009-09-25 04:25:58 +00001909 case NPC_ValueDependentIsNotNull:
1910 return false;
1911 }
1912 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001913
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001914 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001915 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001916 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001917 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001918 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001919 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001920 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001921 Pointee->isVoidType() && // to void*
1922 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001923 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001924 }
Steve Naroffada7d422007-05-20 17:54:12 +00001925 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001926 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1927 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001928 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001929 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1930 // Accept ((void*)0) as a null pointer constant, as many other
1931 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001932 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001933 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001934 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001935 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001936 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001937 } else if (isa<GNUNullExpr>(this)) {
1938 // The GNU __null extension is always a null pointer constant.
1939 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001940 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001941
Sebastian Redl576fd422009-05-10 18:38:11 +00001942 // C++0x nullptr_t is always a null pointer constant.
1943 if (getType()->isNullPtrType())
1944 return true;
1945
Fariborz Jahanian3567c422010-09-27 22:42:37 +00001946 if (const RecordType *UT = getType()->getAsUnionType())
1947 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
1948 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
1949 const Expr *InitExpr = CLE->getInitializer();
1950 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
1951 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
1952 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001953 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001954 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001955 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001956 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Chris Lattner1abbd412007-06-08 17:58:43 +00001958 // If we have an integer constant expression, we need to *evaluate* it and
1959 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001960 llvm::APSInt Result;
1961 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001962}
Steve Narofff7a5da12007-07-28 23:10:27 +00001963
Douglas Gregor71235ec2009-05-02 02:18:30 +00001964FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001965 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001966
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001967 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00001968 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00001969 ICE->getCastKind() == CK_NoOp)
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001970 E = ICE->getSubExpr()->IgnoreParens();
1971 else
1972 break;
1973 }
1974
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001975 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001976 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001977 if (Field->isBitField())
1978 return Field;
1979
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00001980 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
1981 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
1982 if (Field->isBitField())
1983 return Field;
1984
Douglas Gregor71235ec2009-05-02 02:18:30 +00001985 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1986 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1987 return BinOp->getLHS()->getBitField();
1988
1989 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001990}
1991
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001992bool Expr::refersToVectorElement() const {
1993 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001994
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001995 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00001996 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00001997 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001998 E = ICE->getSubExpr()->IgnoreParens();
1999 else
2000 break;
2001 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002002
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002003 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2004 return ASE->getBase()->getType()->isVectorType();
2005
2006 if (isa<ExtVectorElementExpr>(E))
2007 return true;
2008
2009 return false;
2010}
2011
Chris Lattnerb8211f62009-02-16 22:14:05 +00002012/// isArrow - Return true if the base expression is a pointer to vector,
2013/// return false if the base expression is a vector.
2014bool ExtVectorElementExpr::isArrow() const {
2015 return getBase()->getType()->isPointerType();
2016}
2017
Nate Begemance4d7fc2008-04-18 23:10:10 +00002018unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002019 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002020 return VT->getNumElements();
2021 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002022}
2023
Nate Begemanf322eab2008-05-09 06:41:27 +00002024/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002025bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002026 // FIXME: Refactor this code to an accessor on the AST node which returns the
2027 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002028 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002029
2030 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002031 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002032 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002033
Nate Begeman7e5185b2009-01-18 02:01:21 +00002034 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002035 if (Comp[0] == 's' || Comp[0] == 'S')
2036 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002037
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002038 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2039 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002040 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002041
Steve Naroff0d595ca2007-07-30 03:29:09 +00002042 return false;
2043}
Chris Lattner885b4952007-08-02 23:36:59 +00002044
Nate Begemanf322eab2008-05-09 06:41:27 +00002045/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002046void ExtVectorElementExpr::getEncodedElementAccess(
2047 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002048 llvm::StringRef Comp = Accessor->getName();
2049 if (Comp[0] == 's' || Comp[0] == 'S')
2050 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002051
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002052 bool isHi = Comp == "hi";
2053 bool isLo = Comp == "lo";
2054 bool isEven = Comp == "even";
2055 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002056
Nate Begemanf322eab2008-05-09 06:41:27 +00002057 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2058 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002059
Nate Begemanf322eab2008-05-09 06:41:27 +00002060 if (isHi)
2061 Index = e + i;
2062 else if (isLo)
2063 Index = i;
2064 else if (isEven)
2065 Index = 2 * i;
2066 else if (isOdd)
2067 Index = 2 * i + 1;
2068 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002069 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002070
Nate Begemand3862152008-05-13 21:03:02 +00002071 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002072 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002073}
2074
Douglas Gregor9a129192010-04-21 00:45:42 +00002075ObjCMessageExpr::ObjCMessageExpr(QualType T,
2076 SourceLocation LBracLoc,
2077 SourceLocation SuperLoc,
2078 bool IsInstanceSuper,
2079 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002080 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002081 ObjCMethodDecl *Method,
2082 Expr **Args, unsigned NumArgs,
2083 SourceLocation RBracLoc)
2084 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00002085 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002086 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2087 HasMethod(Method != 0), SuperLoc(SuperLoc),
2088 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2089 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002090 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002091{
Douglas Gregor9a129192010-04-21 00:45:42 +00002092 setReceiverPointer(SuperType.getAsOpaquePtr());
2093 if (NumArgs)
2094 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002095}
2096
Douglas Gregor9a129192010-04-21 00:45:42 +00002097ObjCMessageExpr::ObjCMessageExpr(QualType T,
2098 SourceLocation LBracLoc,
2099 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002100 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002101 ObjCMethodDecl *Method,
2102 Expr **Args, unsigned NumArgs,
2103 SourceLocation RBracLoc)
2104 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002105 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00002106 hasAnyValueDependentArguments(Args, NumArgs))),
2107 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2108 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2109 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002110 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002111{
2112 setReceiverPointer(Receiver);
2113 if (NumArgs)
2114 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002115}
2116
Douglas Gregor9a129192010-04-21 00:45:42 +00002117ObjCMessageExpr::ObjCMessageExpr(QualType T,
2118 SourceLocation LBracLoc,
2119 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002120 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002121 ObjCMethodDecl *Method,
2122 Expr **Args, unsigned NumArgs,
2123 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00002124 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002125 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00002126 hasAnyValueDependentArguments(Args, NumArgs))),
2127 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2128 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2129 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002130 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002131{
2132 setReceiverPointer(Receiver);
2133 if (NumArgs)
2134 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00002135}
2136
Douglas Gregor9a129192010-04-21 00:45:42 +00002137ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2138 SourceLocation LBracLoc,
2139 SourceLocation SuperLoc,
2140 bool IsInstanceSuper,
2141 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002142 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002143 ObjCMethodDecl *Method,
2144 Expr **Args, unsigned NumArgs,
2145 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002146 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002147 NumArgs * sizeof(Expr *);
2148 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2149 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002150 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002151 RBracLoc);
2152}
2153
2154ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2155 SourceLocation LBracLoc,
2156 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002157 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002158 ObjCMethodDecl *Method,
2159 Expr **Args, unsigned NumArgs,
2160 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002161 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002162 NumArgs * sizeof(Expr *);
2163 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002164 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002165 NumArgs, RBracLoc);
2166}
2167
2168ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2169 SourceLocation LBracLoc,
2170 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002171 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002172 ObjCMethodDecl *Method,
2173 Expr **Args, unsigned NumArgs,
2174 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002175 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002176 NumArgs * sizeof(Expr *);
2177 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002178 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002179 NumArgs, RBracLoc);
2180}
2181
Alexis Hunta8136cc2010-05-05 15:23:54 +00002182ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002183 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002184 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002185 NumArgs * sizeof(Expr *);
2186 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2187 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2188}
Alexis Hunta8136cc2010-05-05 15:23:54 +00002189
Douglas Gregor9a129192010-04-21 00:45:42 +00002190Selector ObjCMessageExpr::getSelector() const {
2191 if (HasMethod)
2192 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2193 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002194 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002195}
2196
2197ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2198 switch (getReceiverKind()) {
2199 case Instance:
2200 if (const ObjCObjectPointerType *Ptr
2201 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2202 return Ptr->getInterfaceDecl();
2203 break;
2204
2205 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002206 if (const ObjCObjectType *Ty
2207 = getClassReceiver()->getAs<ObjCObjectType>())
2208 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002209 break;
2210
2211 case SuperInstance:
2212 if (const ObjCObjectPointerType *Ptr
2213 = getSuperType()->getAs<ObjCObjectPointerType>())
2214 return Ptr->getInterfaceDecl();
2215 break;
2216
2217 case SuperClass:
2218 if (const ObjCObjectPointerType *Iface
2219 = getSuperType()->getAs<ObjCObjectPointerType>())
2220 return Iface->getInterfaceDecl();
2221 break;
2222 }
2223
2224 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002225}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002226
Chris Lattner35e564e2007-10-25 00:29:32 +00002227bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002228 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002229}
2230
Nate Begeman48745922009-08-12 02:28:50 +00002231void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2232 unsigned NumExprs) {
2233 if (SubExprs) C.Deallocate(SubExprs);
2234
2235 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002236 this->NumExprs = NumExprs;
2237 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002238}
Nate Begeman48745922009-08-12 02:28:50 +00002239
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002240//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002241// DesignatedInitExpr
2242//===----------------------------------------------------------------------===//
2243
2244IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2245 assert(Kind == FieldDesignator && "Only valid on a field designator");
2246 if (Field.NameOrField & 0x01)
2247 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2248 else
2249 return getField()->getIdentifier();
2250}
2251
Alexis Hunta8136cc2010-05-05 15:23:54 +00002252DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002253 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002254 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002255 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002256 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002257 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002258 unsigned NumIndexExprs,
2259 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002260 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002261 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002262 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2263 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002264 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002265
2266 // Record the initializer itself.
2267 child_iterator Child = child_begin();
2268 *Child++ = Init;
2269
2270 // Copy the designators and their subexpressions, computing
2271 // value-dependence along the way.
2272 unsigned IndexIdx = 0;
2273 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002274 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002275
2276 if (this->Designators[I].isArrayDesignator()) {
2277 // Compute type- and value-dependence.
2278 Expr *Index = IndexExprs[IndexIdx];
John McCall925b16622010-10-26 08:39:16 +00002279 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002280 Index->isTypeDependent() || Index->isValueDependent();
2281
2282 // Copy the index expressions into permanent storage.
2283 *Child++ = IndexExprs[IndexIdx++];
2284 } else if (this->Designators[I].isArrayRangeDesignator()) {
2285 // Compute type- and value-dependence.
2286 Expr *Start = IndexExprs[IndexIdx];
2287 Expr *End = IndexExprs[IndexIdx + 1];
John McCall925b16622010-10-26 08:39:16 +00002288 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002289 Start->isTypeDependent() || Start->isValueDependent() ||
2290 End->isTypeDependent() || End->isValueDependent();
2291
2292 // Copy the start/end expressions into permanent storage.
2293 *Child++ = IndexExprs[IndexIdx++];
2294 *Child++ = IndexExprs[IndexIdx++];
2295 }
2296 }
2297
2298 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002299}
2300
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002301DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002302DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002303 unsigned NumDesignators,
2304 Expr **IndexExprs, unsigned NumIndexExprs,
2305 SourceLocation ColonOrEqualLoc,
2306 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002307 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002308 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002309 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002310 ColonOrEqualLoc, UsesColonSyntax,
2311 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002312}
2313
Mike Stump11289f42009-09-09 15:08:12 +00002314DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002315 unsigned NumIndexExprs) {
2316 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2317 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2318 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2319}
2320
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002321void DesignatedInitExpr::setDesignators(ASTContext &C,
2322 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002323 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002324 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002325 NumDesignators = NumDesigs;
2326 for (unsigned I = 0; I != NumDesigs; ++I)
2327 Designators[I] = Desigs[I];
2328}
2329
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002330SourceRange DesignatedInitExpr::getSourceRange() const {
2331 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002332 Designator &First =
2333 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002334 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002335 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002336 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2337 else
2338 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2339 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002340 StartLoc =
2341 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002342 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2343}
2344
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002345Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2346 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2347 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2348 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002349 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2350 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2351}
2352
2353Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002354 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002355 "Requires array range designator");
2356 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2357 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002358 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2359 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2360}
2361
2362Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002363 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002364 "Requires array range designator");
2365 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2366 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002367 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2368 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2369}
2370
Douglas Gregord5846a12009-04-15 06:41:24 +00002371/// \brief Replaces the designator at index @p Idx with the series
2372/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002373void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002374 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002375 const Designator *Last) {
2376 unsigned NumNewDesignators = Last - First;
2377 if (NumNewDesignators == 0) {
2378 std::copy_backward(Designators + Idx + 1,
2379 Designators + NumDesignators,
2380 Designators + Idx);
2381 --NumNewDesignators;
2382 return;
2383 } else if (NumNewDesignators == 1) {
2384 Designators[Idx] = *First;
2385 return;
2386 }
2387
Mike Stump11289f42009-09-09 15:08:12 +00002388 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002389 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002390 std::copy(Designators, Designators + Idx, NewDesignators);
2391 std::copy(First, Last, NewDesignators + Idx);
2392 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2393 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002394 Designators = NewDesignators;
2395 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2396}
2397
Mike Stump11289f42009-09-09 15:08:12 +00002398ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002399 Expr **exprs, unsigned nexprs,
2400 SourceLocation rparenloc)
2401: Expr(ParenListExprClass, QualType(),
2402 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002403 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002404 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002405
Nate Begeman5ec4b312009-08-10 23:49:36 +00002406 Exprs = new (C) Stmt*[nexprs];
2407 for (unsigned i = 0; i != nexprs; ++i)
2408 Exprs[i] = exprs[i];
2409}
2410
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002411//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002412// ExprIterator.
2413//===----------------------------------------------------------------------===//
2414
2415Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2416Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2417Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2418const Expr* ConstExprIterator::operator[](size_t idx) const {
2419 return cast<Expr>(I[idx]);
2420}
2421const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2422const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2423
2424//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002425// Child Iterators for iterating over subexpressions/substatements
2426//===----------------------------------------------------------------------===//
2427
2428// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002429Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2430Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002431
Steve Naroffe46504b2007-11-12 14:29:37 +00002432// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002433Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2434Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002435
Steve Naroffebf4cb42008-06-02 23:03:37 +00002436// ObjCPropertyRefExpr
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002437Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2438{
2439 if (BaseExprOrSuperType.is<Stmt*>()) {
2440 // Hack alert!
2441 return reinterpret_cast<Stmt**> (&BaseExprOrSuperType);
2442 }
2443 return child_iterator();
2444}
2445
2446Stmt::child_iterator ObjCPropertyRefExpr::child_end()
2447{ return BaseExprOrSuperType.is<Stmt*>() ?
2448 reinterpret_cast<Stmt**> (&BaseExprOrSuperType)+1 :
2449 child_iterator();
2450}
Steve Naroffec944032008-05-30 00:40:33 +00002451
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002452// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002453Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002454 // If this is accessing a class member or super, skip that entry.
2455 // Technically, 2nd condition is sufficient. But I want to be verbose
2456 if (isSuperReceiver() || !Base)
2457 return child_iterator();
2458 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002459}
Mike Stump11289f42009-09-09 15:08:12 +00002460Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002461 if (isSuperReceiver() || !Base)
2462 return child_iterator();
Mike Stump11289f42009-09-09 15:08:12 +00002463 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002464}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002465
Steve Naroffe87026a2009-07-24 17:54:45 +00002466// ObjCIsaExpr
2467Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2468Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2469
Chris Lattner6307f192008-08-10 01:53:14 +00002470// PredefinedExpr
2471Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2472Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002473
2474// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002475Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2476Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002477
2478// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002479Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002480Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002481
2482// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002483Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2484Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002485
Chris Lattner1c20a172007-08-26 03:42:43 +00002486// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002487Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2488Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002489
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002490// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002491Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2492Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002493
2494// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002495Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2496Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002497
2498// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002499Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2500Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002501
Douglas Gregor882211c2010-04-28 22:16:22 +00002502// OffsetOfExpr
2503Stmt::child_iterator OffsetOfExpr::child_begin() {
2504 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2505 + NumComps);
2506}
2507Stmt::child_iterator OffsetOfExpr::child_end() {
2508 return child_iterator(&*child_begin() + NumExprs);
2509}
2510
Sebastian Redl6f282892008-11-11 17:56:53 +00002511// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002512Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002513 // If this is of a type and the type is a VLA type (and not a typedef), the
2514 // size expression of the VLA needs to be treated as an executable expression.
2515 // Why isn't this weirdness documented better in StmtIterator?
2516 if (isArgumentType()) {
2517 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2518 getArgumentType().getTypePtr()))
2519 return child_iterator(T);
2520 return child_iterator();
2521 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002522 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002523}
Sebastian Redl6f282892008-11-11 17:56:53 +00002524Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2525 if (isArgumentType())
2526 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002527 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002528}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002529
2530// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002531Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002532 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002533}
Ted Kremenek23702b62007-08-24 20:06:47 +00002534Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002535 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002536}
2537
2538// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002539Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002540 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002541}
Ted Kremenek23702b62007-08-24 20:06:47 +00002542Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002543 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002544}
Ted Kremenek23702b62007-08-24 20:06:47 +00002545
2546// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002547Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2548Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002549
Nate Begemance4d7fc2008-04-18 23:10:10 +00002550// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002551Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2552Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002553
2554// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002555Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2556Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002557
Ted Kremenek23702b62007-08-24 20:06:47 +00002558// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002559Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2560Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002561
2562// BinaryOperator
2563Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002564 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002565}
Ted Kremenek23702b62007-08-24 20:06:47 +00002566Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002567 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002568}
2569
2570// ConditionalOperator
2571Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002572 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002573}
Ted Kremenek23702b62007-08-24 20:06:47 +00002574Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002575 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002576}
2577
2578// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002579Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2580Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002581
Ted Kremenek23702b62007-08-24 20:06:47 +00002582// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002583Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2584Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002585
2586// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002587Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2588 return child_iterator();
2589}
2590
2591Stmt::child_iterator TypesCompatibleExpr::child_end() {
2592 return child_iterator();
2593}
Ted Kremenek23702b62007-08-24 20:06:47 +00002594
2595// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002596Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2597Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002598
Douglas Gregor3be4b122008-11-29 04:51:27 +00002599// GNUNullExpr
2600Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2601Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2602
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002603// ShuffleVectorExpr
2604Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002605 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002606}
2607Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002608 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002609}
2610
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002611// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002612Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2613Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002614
Anders Carlsson4692db02007-08-31 04:56:16 +00002615// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002616Stmt::child_iterator InitListExpr::child_begin() {
2617 return InitExprs.size() ? &InitExprs[0] : 0;
2618}
2619Stmt::child_iterator InitListExpr::child_end() {
2620 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2621}
Anders Carlsson4692db02007-08-31 04:56:16 +00002622
Douglas Gregor0202cb42009-01-29 17:44:32 +00002623// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002624Stmt::child_iterator DesignatedInitExpr::child_begin() {
2625 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2626 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002627 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2628}
2629Stmt::child_iterator DesignatedInitExpr::child_end() {
2630 return child_iterator(&*child_begin() + NumSubExprs);
2631}
2632
Douglas Gregor0202cb42009-01-29 17:44:32 +00002633// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002634Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2635 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002636}
2637
Mike Stump11289f42009-09-09 15:08:12 +00002638Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2639 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002640}
2641
Nate Begeman5ec4b312009-08-10 23:49:36 +00002642// ParenListExpr
2643Stmt::child_iterator ParenListExpr::child_begin() {
2644 return &Exprs[0];
2645}
2646Stmt::child_iterator ParenListExpr::child_end() {
2647 return &Exprs[0]+NumExprs;
2648}
2649
Ted Kremenek23702b62007-08-24 20:06:47 +00002650// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002651Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002652 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002653}
2654Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002655 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002656}
Ted Kremenek23702b62007-08-24 20:06:47 +00002657
2658// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002659Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2660Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002661
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002662// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002663Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002664 return child_iterator();
2665}
2666Stmt::child_iterator ObjCSelectorExpr::child_end() {
2667 return child_iterator();
2668}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002669
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002670// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002671Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2672 return child_iterator();
2673}
2674Stmt::child_iterator ObjCProtocolExpr::child_end() {
2675 return child_iterator();
2676}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002677
Steve Naroffd54978b2007-09-18 23:55:05 +00002678// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002679Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002680 if (getReceiverKind() == Instance)
2681 return reinterpret_cast<Stmt **>(this + 1);
2682 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002683}
2684Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002685 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002686}
2687
Steve Naroffc540d662008-09-03 18:15:37 +00002688// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002689Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2690Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002691
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002692Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2693Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }