blob: 3e3792e21fe727119ebc07558c0ea911bb75f767 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000030#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattnerbef0efd2010-05-13 01:02:19 +000033void Expr::ANCHOR() {} // key function for Expr class.
34
Chris Lattner2b334bb2010-04-16 23:34:13 +000035/// isKnownToHaveBooleanValue - Return true if this is an integer expression
36/// that is known to return 0 or 1. This happens for _Bool/bool expressions
37/// but also int expressions which are produced by things like comparisons in
38/// C.
39bool Expr::isKnownToHaveBooleanValue() const {
40 // If this value has _Bool type, it is obvious 0/1.
41 if (getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregor2ade35e2010-06-16 00:17:44 +000043 if (!getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000044
Chris Lattner2b334bb2010-04-16 23:34:13 +000045 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
46 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000047
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
49 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
51 case UO_Extension:
Chris Lattner2b334bb2010-04-16 23:34:13 +000052 return UO->getSubExpr()->isKnownToHaveBooleanValue();
53 default:
54 return false;
55 }
56 }
Sean Huntc3021132010-05-05 15:23:54 +000057
John McCall6907fbe2010-06-12 01:56:02 +000058 // Only look through implicit casts. If the user writes
59 // '(int) (a && b)' treat it as an arbitrary int.
60 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000062
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
64 switch (BO->getOpcode()) {
65 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000066 case BO_LT: // Relational operators.
67 case BO_GT:
68 case BO_LE:
69 case BO_GE:
70 case BO_EQ: // Equality operators.
71 case BO_NE:
72 case BO_LAnd: // AND operator.
73 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000074 return true;
Sean Huntc3021132010-05-05 15:23:54 +000075
John McCall2de56d12010-08-25 11:45:40 +000076 case BO_And: // Bitwise AND operator.
77 case BO_Xor: // Bitwise XOR operator.
78 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000079 // Handle things like (x==2)|(y==12).
80 return BO->getLHS()->isKnownToHaveBooleanValue() &&
81 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000082
John McCall2de56d12010-08-25 11:45:40 +000083 case BO_Comma:
84 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000085 return BO->getRHS()->isKnownToHaveBooleanValue();
86 }
87 }
Sean Huntc3021132010-05-05 15:23:54 +000088
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
90 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
91 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000092
Chris Lattner2b334bb2010-04-16 23:34:13 +000093 return false;
94}
95
Reid Spencer5f016e22007-07-11 17:01:13 +000096//===----------------------------------------------------------------------===//
97// Primary Expressions.
98//===----------------------------------------------------------------------===//
99
John McCalld5532b62009-11-23 01:53:49 +0000100void ExplicitTemplateArgumentList::initializeFrom(
101 const TemplateArgumentListInfo &Info) {
102 LAngleLoc = Info.getLAngleLoc();
103 RAngleLoc = Info.getRAngleLoc();
104 NumTemplateArgs = Info.size();
105
106 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
107 for (unsigned i = 0; i != NumTemplateArgs; ++i)
108 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
109}
110
111void ExplicitTemplateArgumentList::copyInto(
112 TemplateArgumentListInfo &Info) const {
113 Info.setLAngleLoc(LAngleLoc);
114 Info.setRAngleLoc(RAngleLoc);
115 for (unsigned I = 0; I != NumTemplateArgs; ++I)
116 Info.addArgument(getTemplateArgs()[I]);
117}
118
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000119std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
120 return sizeof(ExplicitTemplateArgumentList) +
121 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
122}
123
John McCalld5532b62009-11-23 01:53:49 +0000124std::size_t ExplicitTemplateArgumentList::sizeFor(
125 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000126 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000127}
128
Douglas Gregor0da76df2009-11-23 11:41:28 +0000129void DeclRefExpr::computeDependence() {
John McCall8e6285a2010-10-26 08:39:16 +0000130 ExprBits.TypeDependent = false;
131 ExprBits.ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000132
Douglas Gregor0da76df2009-11-23 11:41:28 +0000133 NamedDecl *D = getDecl();
134
135 // (TD) C++ [temp.dep.expr]p3:
136 // An id-expression is type-dependent if it contains:
137 //
Sean Huntc3021132010-05-05 15:23:54 +0000138 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000139 //
140 // (VD) C++ [temp.dep.constexpr]p2:
141 // An identifier is value-dependent if it is:
142
143 // (TD) - an identifier that was declared with dependent type
144 // (VD) - a name declared with a dependent type,
145 if (getType()->isDependentType()) {
John McCall8e6285a2010-10-26 08:39:16 +0000146 ExprBits.TypeDependent = true;
147 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000148 }
149 // (TD) - a conversion-function-id that specifies a dependent type
Sean Huntc3021132010-05-05 15:23:54 +0000150 else if (D->getDeclName().getNameKind()
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 == DeclarationName::CXXConversionFunctionName &&
152 D->getDeclName().getCXXNameType()->isDependentType()) {
John McCall8e6285a2010-10-26 08:39:16 +0000153 ExprBits.TypeDependent = true;
154 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 }
156 // (TD) - a template-id that is dependent,
John McCall096832c2010-08-19 23:49:38 +0000157 else if (hasExplicitTemplateArgs() &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000158 TemplateSpecializationType::anyDependentTemplateArguments(
Sean Huntc3021132010-05-05 15:23:54 +0000159 getTemplateArgs(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000160 getNumTemplateArgs())) {
John McCall8e6285a2010-10-26 08:39:16 +0000161 ExprBits.TypeDependent = true;
162 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000163 }
164 // (VD) - the name of a non-type template parameter,
165 else if (isa<NonTypeTemplateParmDecl>(D))
John McCall8e6285a2010-10-26 08:39:16 +0000166 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000167 // (VD) - a constant with integral or enumeration type and is
168 // initialized with an expression that is value-dependent.
169 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000170 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000171 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000172 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000173 if (Init->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +0000174 ExprBits.ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000175 }
176 // (VD) - FIXME: Missing from the standard:
177 // - a member function or a static data member of the current
178 // instantiation
179 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000180 Var->getDeclContext()->isDependentContext())
John McCall8e6285a2010-10-26 08:39:16 +0000181 ExprBits.ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000182 }
183 // (VD) - FIXME: Missing from the standard:
184 // - a member function or a static data member of the current
185 // instantiation
186 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
John McCall8e6285a2010-10-26 08:39:16 +0000187 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188 // (TD) - a nested-name-specifier or a qualified-id that names a
189 // member of an unknown specialization.
190 // (handled by DependentScopeDeclRefExpr)
191}
192
Sean Huntc3021132010-05-05 15:23:54 +0000193DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000194 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000195 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000196 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000197 QualType T, ExprValueKind VK)
198 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000199 DecoratedD(D,
200 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000201 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000202 Loc(NameLoc) {
203 if (Qualifier) {
204 NameQualifier *NQ = getNameQualifier();
205 NQ->NNS = Qualifier;
206 NQ->Range = QualifierRange;
207 }
Sean Huntc3021132010-05-05 15:23:54 +0000208
John McCalld5532b62009-11-23 01:53:49 +0000209 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000210 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000211
212 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000213}
214
Abramo Bagnara25777432010-08-11 22:01:17 +0000215DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
216 SourceRange QualifierRange,
217 ValueDecl *D, const DeclarationNameInfo &NameInfo,
218 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000219 QualType T, ExprValueKind VK)
220 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000221 DecoratedD(D,
222 (Qualifier? HasQualifierFlag : 0) |
223 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
224 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
225 if (Qualifier) {
226 NameQualifier *NQ = getNameQualifier();
227 NQ->NNS = Qualifier;
228 NQ->Range = QualifierRange;
229 }
230
231 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000232 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000233
234 computeDependence();
235}
236
Douglas Gregora2813ce2009-10-23 18:54:35 +0000237DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
238 NestedNameSpecifier *Qualifier,
239 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000240 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000241 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000242 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000243 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000244 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000245 return Create(Context, Qualifier, QualifierRange, D,
246 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000247 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000248}
249
250DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
251 NestedNameSpecifier *Qualifier,
252 SourceRange QualifierRange,
253 ValueDecl *D,
254 const DeclarationNameInfo &NameInfo,
255 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000256 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000257 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000258 std::size_t Size = sizeof(DeclRefExpr);
259 if (Qualifier != 0)
260 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000261
John McCalld5532b62009-11-23 01:53:49 +0000262 if (TemplateArgs)
263 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000264
Chris Lattner32488542010-10-30 05:14:06 +0000265 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara25777432010-08-11 22:01:17 +0000266 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCallf89e55a2010-11-18 06:31:45 +0000267 TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000268}
269
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000270DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
271 unsigned NumTemplateArgs) {
272 std::size_t Size = sizeof(DeclRefExpr);
273 if (HasQualifier)
274 Size += sizeof(NameQualifier);
275
276 if (NumTemplateArgs)
277 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
278
Chris Lattner32488542010-10-30 05:14:06 +0000279 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000280 return new (Mem) DeclRefExpr(EmptyShell());
281}
282
Douglas Gregora2813ce2009-10-23 18:54:35 +0000283SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000284 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000285 if (hasQualifier())
286 R.setBegin(getQualifierRange().getBegin());
John McCall096832c2010-08-19 23:49:38 +0000287 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000288 R.setEnd(getRAngleLoc());
289 return R;
290}
291
Anders Carlsson3a082d82009-09-08 18:24:21 +0000292// FIXME: Maybe this should use DeclPrinter with a special "print predefined
293// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000294std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
295 ASTContext &Context = CurrentDecl->getASTContext();
296
Anders Carlsson3a082d82009-09-08 18:24:21 +0000297 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000298 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000299 return FD->getNameAsString();
300
301 llvm::SmallString<256> Name;
302 llvm::raw_svector_ostream Out(Name);
303
304 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000305 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000306 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000307 if (MD->isStatic())
308 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000309 }
310
311 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000312
313 std::string Proto = FD->getQualifiedNameAsString(Policy);
314
John McCall183700f2009-09-21 23:43:11 +0000315 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000316 const FunctionProtoType *FT = 0;
317 if (FD->hasWrittenPrototype())
318 FT = dyn_cast<FunctionProtoType>(AFT);
319
320 Proto += "(";
321 if (FT) {
322 llvm::raw_string_ostream POut(Proto);
323 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
324 if (i) POut << ", ";
325 std::string Param;
326 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
327 POut << Param;
328 }
329
330 if (FT->isVariadic()) {
331 if (FD->getNumParams()) POut << ", ";
332 POut << "...";
333 }
334 }
335 Proto += ")";
336
Sam Weinig4eadcc52009-12-27 01:38:20 +0000337 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
338 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
339 if (ThisQuals.hasConst())
340 Proto += " const";
341 if (ThisQuals.hasVolatile())
342 Proto += " volatile";
343 }
344
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000345 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
346 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000347
348 Out << Proto;
349
350 Out.flush();
351 return Name.str().str();
352 }
353 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
354 llvm::SmallString<256> Name;
355 llvm::raw_svector_ostream Out(Name);
356 Out << (MD->isInstanceMethod() ? '-' : '+');
357 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000358
359 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
360 // a null check to avoid a crash.
361 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000362 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000363
Anders Carlsson3a082d82009-09-08 18:24:21 +0000364 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000365 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
366 Out << '(' << CID << ')';
367
Anders Carlsson3a082d82009-09-08 18:24:21 +0000368 Out << ' ';
369 Out << MD->getSelector().getAsString();
370 Out << ']';
371
372 Out.flush();
373 return Name.str().str();
374 }
375 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
376 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
377 return "top level";
378 }
379 return "";
380}
381
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000382void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
383 if (hasAllocation())
384 C.Deallocate(pVal);
385
386 BitWidth = Val.getBitWidth();
387 unsigned NumWords = Val.getNumWords();
388 const uint64_t* Words = Val.getRawData();
389 if (NumWords > 1) {
390 pVal = new (C) uint64_t[NumWords];
391 std::copy(Words, Words + NumWords, pVal);
392 } else if (NumWords == 1)
393 VAL = Words[0];
394 else
395 VAL = 0;
396}
397
398IntegerLiteral *
399IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
400 QualType type, SourceLocation l) {
401 return new (C) IntegerLiteral(C, V, type, l);
402}
403
404IntegerLiteral *
405IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
406 return new (C) IntegerLiteral(Empty);
407}
408
409FloatingLiteral *
410FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
411 bool isexact, QualType Type, SourceLocation L) {
412 return new (C) FloatingLiteral(C, V, isexact, Type, L);
413}
414
415FloatingLiteral *
416FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
417 return new (C) FloatingLiteral(Empty);
418}
419
Chris Lattnerda8249e2008-06-07 22:13:43 +0000420/// getValueAsApproximateDouble - This returns the value as an inaccurate
421/// double. Note that this may cause loss of precision, but is useful for
422/// debugging dumps, etc.
423double FloatingLiteral::getValueAsApproximateDouble() const {
424 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000425 bool ignored;
426 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
427 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000428 return V.convertToDouble();
429}
430
Chris Lattner2085fd62009-02-18 06:40:38 +0000431StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
432 unsigned ByteLength, bool Wide,
433 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000434 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000435 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000436 // Allocate enough space for the StringLiteral plus an array of locations for
437 // any concatenated string tokens.
438 void *Mem = C.Allocate(sizeof(StringLiteral)+
439 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000440 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000441 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000444 char *AStrData = new (C, 1) char[ByteLength];
445 memcpy(AStrData, StrData, ByteLength);
446 SL->StrData = AStrData;
447 SL->ByteLength = ByteLength;
448 SL->IsWide = Wide;
449 SL->TokLocs[0] = Loc[0];
450 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000451
Chris Lattner726e1682009-02-18 05:49:11 +0000452 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000453 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
454 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000455}
456
Douglas Gregor673ecd62009-04-15 16:35:07 +0000457StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
458 void *Mem = C.Allocate(sizeof(StringLiteral)+
459 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000460 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000461 StringLiteral *SL = new (Mem) StringLiteral(QualType());
462 SL->StrData = 0;
463 SL->ByteLength = 0;
464 SL->NumConcatenated = NumStrs;
465 return SL;
466}
467
Daniel Dunbarb6480232009-09-22 03:27:33 +0000468void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000469 char *AStrData = new (C, 1) char[Str.size()];
470 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000471 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000472 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000473}
474
Chris Lattner08f92e32010-11-17 07:37:15 +0000475/// getLocationOfByte - Return a source location that points to the specified
476/// byte of this string literal.
477///
478/// Strings are amazingly complex. They can be formed from multiple tokens and
479/// can have escape sequences in them in addition to the usual trigraph and
480/// escaped newline business. This routine handles this complexity.
481///
482SourceLocation StringLiteral::
483getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
484 const LangOptions &Features, const TargetInfo &Target) const {
485 assert(!isWide() && "This doesn't work for wide strings yet");
486
487 // Loop over all of the tokens in this string until we find the one that
488 // contains the byte we're looking for.
489 unsigned TokNo = 0;
490 while (1) {
491 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
492 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
493
494 // Get the spelling of the string so that we can get the data that makes up
495 // the string literal, not the identifier for the macro it is potentially
496 // expanded through.
497 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
498
499 // Re-lex the token to get its length and original spelling.
500 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
501 bool Invalid = false;
502 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
503 if (Invalid)
504 return StrTokSpellingLoc;
505
506 const char *StrData = Buffer.data()+LocInfo.second;
507
508 // Create a langops struct and enable trigraphs. This is sufficient for
509 // relexing tokens.
510 LangOptions LangOpts;
511 LangOpts.Trigraphs = true;
512
513 // Create a lexer starting at the beginning of this token.
514 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
515 Buffer.end());
516 Token TheTok;
517 TheLexer.LexFromRawLexer(TheTok);
518
519 // Use the StringLiteralParser to compute the length of the string in bytes.
520 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
521 unsigned TokNumBytes = SLP.GetStringLength();
522
523 // If the byte is in this token, return the location of the byte.
524 if (ByteNo < TokNumBytes ||
525 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
526 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
527
528 // Now that we know the offset of the token in the spelling, use the
529 // preprocessor to get the offset in the original source.
530 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
531 }
532
533 // Move to the next string token.
534 ++TokNo;
535 ByteNo -= TokNumBytes;
536 }
537}
538
539
540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
542/// corresponds to, e.g. "sizeof" or "[pre]++".
543const char *UnaryOperator::getOpcodeStr(Opcode Op) {
544 switch (Op) {
545 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000546 case UO_PostInc: return "++";
547 case UO_PostDec: return "--";
548 case UO_PreInc: return "++";
549 case UO_PreDec: return "--";
550 case UO_AddrOf: return "&";
551 case UO_Deref: return "*";
552 case UO_Plus: return "+";
553 case UO_Minus: return "-";
554 case UO_Not: return "~";
555 case UO_LNot: return "!";
556 case UO_Real: return "__real";
557 case UO_Imag: return "__imag";
558 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 }
560}
561
John McCall2de56d12010-08-25 11:45:40 +0000562UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000563UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
564 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000565 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000566 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
567 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
568 case OO_Amp: return UO_AddrOf;
569 case OO_Star: return UO_Deref;
570 case OO_Plus: return UO_Plus;
571 case OO_Minus: return UO_Minus;
572 case OO_Tilde: return UO_Not;
573 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000574 }
575}
576
577OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
578 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000579 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
580 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
581 case UO_AddrOf: return OO_Amp;
582 case UO_Deref: return OO_Star;
583 case UO_Plus: return OO_Plus;
584 case UO_Minus: return OO_Minus;
585 case UO_Not: return OO_Tilde;
586 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000587 default: return OO_None;
588 }
589}
590
591
Reid Spencer5f016e22007-07-11 17:01:13 +0000592//===----------------------------------------------------------------------===//
593// Postfix Operators.
594//===----------------------------------------------------------------------===//
595
Ted Kremenek668bf912009-02-09 20:51:47 +0000596CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
John McCallf89e55a2010-11-18 06:31:45 +0000597 unsigned numargs, QualType t, ExprValueKind VK,
598 SourceLocation rparenloc)
599 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregor898574e2008-12-05 23:32:09 +0000600 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000601 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000602 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Ted Kremenek668bf912009-02-09 20:51:47 +0000604 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000605 SubExprs[FN] = fn;
606 for (unsigned i = 0; i != numargs; ++i)
607 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000608
Douglas Gregorb4609802008-11-14 16:09:21 +0000609 RParenLoc = rparenloc;
610}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000611
Ted Kremenek668bf912009-02-09 20:51:47 +0000612CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000613 QualType t, ExprValueKind VK, SourceLocation rparenloc)
614 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregor898574e2008-12-05 23:32:09 +0000615 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000616 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000617 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000618
619 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000620 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000622 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 RParenLoc = rparenloc;
625}
626
Mike Stump1eb44332009-09-09 15:08:12 +0000627CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
628 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000629 SubExprs = new (C) Stmt*[1];
630}
631
Nuno Lopesd20254f2009-12-20 23:11:08 +0000632Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000633 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000634 // If we're calling a dereference, look at the pointer instead.
635 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
636 if (BO->isPtrMemOp())
637 CEE = BO->getRHS()->IgnoreParenCasts();
638 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
639 if (UO->getOpcode() == UO_Deref)
640 CEE = UO->getSubExpr()->IgnoreParenCasts();
641 }
Chris Lattner6346f962009-07-17 15:46:27 +0000642 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000643 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000644 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
645 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000646
647 return 0;
648}
649
Nuno Lopesd20254f2009-12-20 23:11:08 +0000650FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000651 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000652}
653
Chris Lattnerd18b3292007-12-28 05:25:02 +0000654/// setNumArgs - This changes the number of arguments present in this call.
655/// Any orphaned expressions are deleted by this, and any new operands are set
656/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000657void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000658 // No change, just return.
659 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Chris Lattnerd18b3292007-12-28 05:25:02 +0000661 // If shrinking # arguments, just delete the extras and forgot them.
662 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000663 this->NumArgs = NumArgs;
664 return;
665 }
666
667 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000668 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000669 // Copy over args.
670 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
671 NewSubExprs[i] = SubExprs[i];
672 // Null out new args.
673 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
674 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor88c9a462009-04-17 21:46:47 +0000676 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000677 SubExprs = NewSubExprs;
678 this->NumArgs = NumArgs;
679}
680
Chris Lattnercb888962008-10-06 05:00:53 +0000681/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
682/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000683unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000684 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000685 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000686 // ImplicitCastExpr.
687 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
688 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000689 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000691 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
692 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000693 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Anders Carlssonbcba2012008-01-31 02:13:57 +0000695 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
696 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000697 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000699 if (!FDecl->getIdentifier())
700 return 0;
701
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000702 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000703}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000704
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000705QualType CallExpr::getCallReturnType() const {
706 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000707 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000708 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000709 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000710 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000711 else if (const MemberPointerType *MPT
712 = CalleeType->getAs<MemberPointerType>())
713 CalleeType = MPT->getPointeeType();
714
John McCall183700f2009-09-21 23:43:11 +0000715 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000716 return FnType->getResultType();
717}
Chris Lattnercb888962008-10-06 05:00:53 +0000718
Sean Huntc3021132010-05-05 15:23:54 +0000719OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000720 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000721 TypeSourceInfo *tsi,
722 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000723 Expr** exprsPtr, unsigned numExprs,
724 SourceLocation RParenLoc) {
725 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000726 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000727 sizeof(Expr*) * numExprs);
728
729 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
730 exprsPtr, numExprs, RParenLoc);
731}
732
733OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
734 unsigned numComps, unsigned numExprs) {
735 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
736 sizeof(OffsetOfNode) * numComps +
737 sizeof(Expr*) * numExprs);
738 return new (Mem) OffsetOfExpr(numComps, numExprs);
739}
740
Sean Huntc3021132010-05-05 15:23:54 +0000741OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000742 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000743 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000744 Expr** exprsPtr, unsigned numExprs,
745 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000746 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
747 /*TypeDependent=*/false,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000748 /*ValueDependent=*/tsi->getType()->isDependentType() ||
749 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
750 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Sean Huntc3021132010-05-05 15:23:54 +0000751 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
752 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000753{
754 for(unsigned i = 0; i < numComps; ++i) {
755 setComponent(i, compsPtr[i]);
756 }
Sean Huntc3021132010-05-05 15:23:54 +0000757
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000758 for(unsigned i = 0; i < numExprs; ++i) {
759 setIndexExpr(i, exprsPtr[i]);
760 }
761}
762
763IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
764 assert(getKind() == Field || getKind() == Identifier);
765 if (getKind() == Field)
766 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000767
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000768 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
769}
770
Mike Stump1eb44332009-09-09 15:08:12 +0000771MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
772 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000773 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000774 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000775 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000776 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000777 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000778 QualType ty,
779 ExprValueKind vk,
780 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000781 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000782
John McCall161755a2010-04-06 21:38:20 +0000783 bool hasQualOrFound = (qual != 0 ||
784 founddecl.getDecl() != memberdecl ||
785 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000786 if (hasQualOrFound)
787 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000788
John McCalld5532b62009-11-23 01:53:49 +0000789 if (targs)
790 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattner32488542010-10-30 05:14:06 +0000792 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000793 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
794 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000795
796 if (hasQualOrFound) {
797 if (qual && qual->isDependent()) {
798 E->setValueDependent(true);
799 E->setTypeDependent(true);
800 }
801 E->HasQualifierOrFoundDecl = true;
802
803 MemberNameQualifier *NQ = E->getMemberQualifier();
804 NQ->NNS = qual;
805 NQ->Range = qualrange;
806 NQ->FoundDecl = founddecl;
807 }
808
809 if (targs) {
810 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000811 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000812 }
813
814 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000815}
816
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000817const char *CastExpr::getCastKindName() const {
818 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000819 case CK_Dependent:
820 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000821 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000822 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000823 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000824 return "LValueBitCast";
John McCall2de56d12010-08-25 11:45:40 +0000825 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000826 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000827 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000828 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000829 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000830 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000831 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000832 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000833 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000834 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000835 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000836 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000837 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000838 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000839 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000840 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000841 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000842 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000843 case CK_NullToPointer:
844 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000845 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000846 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000847 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000848 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000849 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000850 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000851 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000852 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000853 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000854 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000855 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000856 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000857 case CK_PointerToBoolean:
858 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000859 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000860 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000861 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000862 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000863 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000864 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000865 case CK_IntegralToBoolean:
866 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000867 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000868 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000869 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000870 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000871 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000872 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000873 case CK_FloatingToBoolean:
874 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000875 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000876 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000877 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000878 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000879 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000880 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000881 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000882 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000883 case CK_FloatingRealToComplex:
884 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000885 case CK_FloatingComplexToReal:
886 return "FloatingComplexToReal";
887 case CK_FloatingComplexToBoolean:
888 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000889 case CK_FloatingComplexCast:
890 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000891 case CK_FloatingComplexToIntegralComplex:
892 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000893 case CK_IntegralRealToComplex:
894 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000895 case CK_IntegralComplexToReal:
896 return "IntegralComplexToReal";
897 case CK_IntegralComplexToBoolean:
898 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000899 case CK_IntegralComplexCast:
900 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000901 case CK_IntegralComplexToFloatingComplex:
902 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
John McCall2bb5d002010-11-13 09:02:35 +0000905 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000906 return 0;
907}
908
Douglas Gregor6eef5192009-12-14 19:27:10 +0000909Expr *CastExpr::getSubExprAsWritten() {
910 Expr *SubExpr = 0;
911 CastExpr *E = this;
912 do {
913 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000914
Douglas Gregor6eef5192009-12-14 19:27:10 +0000915 // Skip any temporary bindings; they're implicit.
916 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
917 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000918
Douglas Gregor6eef5192009-12-14 19:27:10 +0000919 // Conversions by constructor and conversion functions have a
920 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +0000921 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000922 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +0000923 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000924 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000925
Douglas Gregor6eef5192009-12-14 19:27:10 +0000926 // If the subexpression we're left with is an implicit cast, look
927 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000928 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
929
Douglas Gregor6eef5192009-12-14 19:27:10 +0000930 return SubExpr;
931}
932
John McCallf871d0c2010-08-07 06:22:56 +0000933CXXBaseSpecifier **CastExpr::path_buffer() {
934 switch (getStmtClass()) {
935#define ABSTRACT_STMT(x)
936#define CASTEXPR(Type, Base) \
937 case Stmt::Type##Class: \
938 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
939#define STMT(Type, Base)
940#include "clang/AST/StmtNodes.inc"
941 default:
942 llvm_unreachable("non-cast expressions not possible here");
943 return 0;
944 }
945}
946
947void CastExpr::setCastPath(const CXXCastPath &Path) {
948 assert(Path.size() == path_size());
949 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
950}
951
952ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
953 CastKind Kind, Expr *Operand,
954 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +0000955 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +0000956 unsigned PathSize = (BasePath ? BasePath->size() : 0);
957 void *Buffer =
958 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
959 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +0000960 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +0000961 if (PathSize) E->setCastPath(*BasePath);
962 return E;
963}
964
965ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
966 unsigned PathSize) {
967 void *Buffer =
968 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
969 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
970}
971
972
973CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000974 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000975 const CXXCastPath *BasePath,
976 TypeSourceInfo *WrittenTy,
977 SourceLocation L, SourceLocation R) {
978 unsigned PathSize = (BasePath ? BasePath->size() : 0);
979 void *Buffer =
980 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
981 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000982 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +0000983 if (PathSize) E->setCastPath(*BasePath);
984 return E;
985}
986
987CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
988 void *Buffer =
989 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
990 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
991}
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
994/// corresponds to, e.g. "<<=".
995const char *BinaryOperator::getOpcodeStr(Opcode Op) {
996 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000997 case BO_PtrMemD: return ".*";
998 case BO_PtrMemI: return "->*";
999 case BO_Mul: return "*";
1000 case BO_Div: return "/";
1001 case BO_Rem: return "%";
1002 case BO_Add: return "+";
1003 case BO_Sub: return "-";
1004 case BO_Shl: return "<<";
1005 case BO_Shr: return ">>";
1006 case BO_LT: return "<";
1007 case BO_GT: return ">";
1008 case BO_LE: return "<=";
1009 case BO_GE: return ">=";
1010 case BO_EQ: return "==";
1011 case BO_NE: return "!=";
1012 case BO_And: return "&";
1013 case BO_Xor: return "^";
1014 case BO_Or: return "|";
1015 case BO_LAnd: return "&&";
1016 case BO_LOr: return "||";
1017 case BO_Assign: return "=";
1018 case BO_MulAssign: return "*=";
1019 case BO_DivAssign: return "/=";
1020 case BO_RemAssign: return "%=";
1021 case BO_AddAssign: return "+=";
1022 case BO_SubAssign: return "-=";
1023 case BO_ShlAssign: return "<<=";
1024 case BO_ShrAssign: return ">>=";
1025 case BO_AndAssign: return "&=";
1026 case BO_XorAssign: return "^=";
1027 case BO_OrAssign: return "|=";
1028 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001030
1031 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001032}
1033
John McCall2de56d12010-08-25 11:45:40 +00001034BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001035BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1036 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001037 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001038 case OO_Plus: return BO_Add;
1039 case OO_Minus: return BO_Sub;
1040 case OO_Star: return BO_Mul;
1041 case OO_Slash: return BO_Div;
1042 case OO_Percent: return BO_Rem;
1043 case OO_Caret: return BO_Xor;
1044 case OO_Amp: return BO_And;
1045 case OO_Pipe: return BO_Or;
1046 case OO_Equal: return BO_Assign;
1047 case OO_Less: return BO_LT;
1048 case OO_Greater: return BO_GT;
1049 case OO_PlusEqual: return BO_AddAssign;
1050 case OO_MinusEqual: return BO_SubAssign;
1051 case OO_StarEqual: return BO_MulAssign;
1052 case OO_SlashEqual: return BO_DivAssign;
1053 case OO_PercentEqual: return BO_RemAssign;
1054 case OO_CaretEqual: return BO_XorAssign;
1055 case OO_AmpEqual: return BO_AndAssign;
1056 case OO_PipeEqual: return BO_OrAssign;
1057 case OO_LessLess: return BO_Shl;
1058 case OO_GreaterGreater: return BO_Shr;
1059 case OO_LessLessEqual: return BO_ShlAssign;
1060 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1061 case OO_EqualEqual: return BO_EQ;
1062 case OO_ExclaimEqual: return BO_NE;
1063 case OO_LessEqual: return BO_LE;
1064 case OO_GreaterEqual: return BO_GE;
1065 case OO_AmpAmp: return BO_LAnd;
1066 case OO_PipePipe: return BO_LOr;
1067 case OO_Comma: return BO_Comma;
1068 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001069 }
1070}
1071
1072OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1073 static const OverloadedOperatorKind OverOps[] = {
1074 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1075 OO_Star, OO_Slash, OO_Percent,
1076 OO_Plus, OO_Minus,
1077 OO_LessLess, OO_GreaterGreater,
1078 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1079 OO_EqualEqual, OO_ExclaimEqual,
1080 OO_Amp,
1081 OO_Caret,
1082 OO_Pipe,
1083 OO_AmpAmp,
1084 OO_PipePipe,
1085 OO_Equal, OO_StarEqual,
1086 OO_SlashEqual, OO_PercentEqual,
1087 OO_PlusEqual, OO_MinusEqual,
1088 OO_LessLessEqual, OO_GreaterGreaterEqual,
1089 OO_AmpEqual, OO_CaretEqual,
1090 OO_PipeEqual,
1091 OO_Comma
1092 };
1093 return OverOps[Opc];
1094}
1095
Ted Kremenek709210f2010-04-13 23:39:13 +00001096InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001097 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001098 SourceLocation rbraceloc)
John McCallf89e55a2010-11-18 06:31:45 +00001099 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001100 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001101 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001102 UnionFieldInit(0), HadArrayRangeDesignator(false)
1103{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001104 for (unsigned I = 0; I != numInits; ++I) {
1105 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001106 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001107 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001108 ExprBits.ValueDependent = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001109 }
Sean Huntc3021132010-05-05 15:23:54 +00001110
Ted Kremenek709210f2010-04-13 23:39:13 +00001111 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001112}
Reid Spencer5f016e22007-07-11 17:01:13 +00001113
Ted Kremenek709210f2010-04-13 23:39:13 +00001114void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001115 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001116 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001117}
1118
Ted Kremenek709210f2010-04-13 23:39:13 +00001119void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001120 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001121}
1122
Ted Kremenek709210f2010-04-13 23:39:13 +00001123Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001124 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001125 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001126 InitExprs.back() = expr;
1127 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor4c678342009-01-28 21:54:33 +00001130 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1131 InitExprs[Init] = expr;
1132 return Result;
1133}
1134
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001135SourceRange InitListExpr::getSourceRange() const {
1136 if (SyntacticForm)
1137 return SyntacticForm->getSourceRange();
1138 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1139 if (Beg.isInvalid()) {
1140 // Find the first non-null initializer.
1141 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1142 E = InitExprs.end();
1143 I != E; ++I) {
1144 if (Stmt *S = *I) {
1145 Beg = S->getLocStart();
1146 break;
1147 }
1148 }
1149 }
1150 if (End.isInvalid()) {
1151 // Find the first non-null initializer from the end.
1152 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1153 E = InitExprs.rend();
1154 I != E; ++I) {
1155 if (Stmt *S = *I) {
1156 End = S->getSourceRange().getEnd();
1157 break;
1158 }
1159 }
1160 }
1161 return SourceRange(Beg, End);
1162}
1163
Steve Naroffbfdcae62008-09-04 15:31:07 +00001164/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001165///
1166const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001167 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001168 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001169}
1170
Mike Stump1eb44332009-09-09 15:08:12 +00001171SourceLocation BlockExpr::getCaretLocation() const {
1172 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001173}
Mike Stump1eb44332009-09-09 15:08:12 +00001174const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001175 return TheBlock->getBody();
1176}
Mike Stump1eb44332009-09-09 15:08:12 +00001177Stmt *BlockExpr::getBody() {
1178 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001179}
Steve Naroff56ee6892008-10-08 17:01:13 +00001180
1181
Reid Spencer5f016e22007-07-11 17:01:13 +00001182//===----------------------------------------------------------------------===//
1183// Generic Expression Routines
1184//===----------------------------------------------------------------------===//
1185
Chris Lattner026dc962009-02-14 07:37:35 +00001186/// isUnusedResultAWarning - Return true if this immediate expression should
1187/// be warned about if the result is unused. If so, fill in Loc and Ranges
1188/// with location to warn on and the source range[s] to report with the
1189/// warning.
1190bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001191 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001192 // Don't warn if the expr is type dependent. The type could end up
1193 // instantiating to void.
1194 if (isTypeDependent())
1195 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 switch (getStmtClass()) {
1198 default:
John McCall0faede62010-03-12 07:11:26 +00001199 if (getType()->isVoidType())
1200 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001201 Loc = getExprLoc();
1202 R1 = getSourceRange();
1203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001205 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001206 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 case UnaryOperatorClass: {
1208 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001211 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001212 case UO_PostInc:
1213 case UO_PostDec:
1214 case UO_PreInc:
1215 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001216 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001217 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001219 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001220 return false;
1221 break;
John McCall2de56d12010-08-25 11:45:40 +00001222 case UO_Real:
1223 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001225 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1226 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001227 return false;
1228 break;
John McCall2de56d12010-08-25 11:45:40 +00001229 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001230 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 }
Chris Lattner026dc962009-02-14 07:37:35 +00001232 Loc = UO->getOperatorLoc();
1233 R1 = UO->getSubExpr()->getSourceRange();
1234 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001236 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001237 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001238 switch (BO->getOpcode()) {
1239 default:
1240 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001241 // Consider the RHS of comma for side effects. LHS was checked by
1242 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001243 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001244 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1245 // lvalue-ness) of an assignment written in a macro.
1246 if (IntegerLiteral *IE =
1247 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1248 if (IE->getValue() == 0)
1249 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001250 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1251 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001252 case BO_LAnd:
1253 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001254 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1255 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1256 return false;
1257 break;
John McCallbf0ee352010-02-16 04:10:53 +00001258 }
Chris Lattner026dc962009-02-14 07:37:35 +00001259 if (BO->isAssignmentOp())
1260 return false;
1261 Loc = BO->getOperatorLoc();
1262 R1 = BO->getLHS()->getSourceRange();
1263 R2 = BO->getRHS()->getSourceRange();
1264 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001265 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001266 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001267 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001268 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001269
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001270 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001271 // The condition must be evaluated, but if either the LHS or RHS is a
1272 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001273 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001274 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001275 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001276 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001277 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001278 }
1279
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001281 // If the base pointer or element is to a volatile pointer/field, accessing
1282 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001283 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001284 return false;
1285 Loc = cast<MemberExpr>(this)->getMemberLoc();
1286 R1 = SourceRange(Loc, Loc);
1287 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1288 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 case ArraySubscriptExprClass:
1291 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001292 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001293 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001294 return false;
1295 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1296 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1297 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1298 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001299
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001301 case CXXOperatorCallExprClass:
1302 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001303 // If this is a direct call, get the callee.
1304 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001305 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001306 // If the callee has attribute pure, const, or warn_unused_result, warn
1307 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001308 //
1309 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1310 // updated to match for QoI.
1311 if (FD->getAttr<WarnUnusedResultAttr>() ||
1312 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1313 Loc = CE->getCallee()->getLocStart();
1314 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001316 if (unsigned NumArgs = CE->getNumArgs())
1317 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1318 CE->getArg(NumArgs-1)->getLocEnd());
1319 return true;
1320 }
Chris Lattner026dc962009-02-14 07:37:35 +00001321 }
1322 return false;
1323 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001324
1325 case CXXTemporaryObjectExprClass:
1326 case CXXConstructExprClass:
1327 return false;
1328
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001329 case ObjCMessageExprClass: {
1330 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1331 const ObjCMethodDecl *MD = ME->getMethodDecl();
1332 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1333 Loc = getExprLoc();
1334 return true;
1335 }
Chris Lattner026dc962009-02-14 07:37:35 +00001336 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001339 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001340#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001341 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001342 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001343 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001344 Loc = Ref->getLocation();
1345 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1346 if (Ref->getBase())
1347 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001348#else
1349 Loc = getExprLoc();
1350 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001351#endif
1352 return true;
1353 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001354 case StmtExprClass: {
1355 // Statement exprs don't logically have side effects themselves, but are
1356 // sometimes used in macros in ways that give them a type that is unused.
1357 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1358 // however, if the result of the stmt expr is dead, we don't want to emit a
1359 // warning.
1360 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001361 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001362 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001363 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001364 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1365 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1366 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1367 }
Mike Stump1eb44332009-09-09 15:08:12 +00001368
John McCall0faede62010-03-12 07:11:26 +00001369 if (getType()->isVoidType())
1370 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001371 Loc = cast<StmtExpr>(this)->getLParenLoc();
1372 R1 = getSourceRange();
1373 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001374 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001375 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001376 // If this is an explicit cast to void, allow it. People do this when they
1377 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001378 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001379 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001380 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1381 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1382 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001383 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001384 if (getType()->isVoidType())
1385 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001386 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001387
Anders Carlsson58beed92009-11-17 17:11:23 +00001388 // If this is a cast to void or a constructor conversion, check the operand.
1389 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001390 if (CE->getCastKind() == CK_ToVoid ||
1391 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001392 return (cast<CastExpr>(this)->getSubExpr()
1393 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001394 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1395 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1396 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001397 }
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Eli Friedman4be1f472008-05-19 21:24:43 +00001399 case ImplicitCastExprClass:
1400 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001401 return (cast<ImplicitCastExpr>(this)
1402 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001403
Chris Lattner04421082008-04-08 04:40:51 +00001404 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001405 return (cast<CXXDefaultArgExpr>(this)
1406 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001407
1408 case CXXNewExprClass:
1409 // FIXME: In theory, there might be new expressions that don't have side
1410 // effects (e.g. a placement new with an uninitialized POD).
1411 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001412 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001413 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001414 return (cast<CXXBindTemporaryExpr>(this)
1415 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001416 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001417 return (cast<CXXExprWithTemporaries>(this)
1418 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001419 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001420}
1421
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001422/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001423/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001424bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001425 switch (getStmtClass()) {
1426 default:
1427 return false;
1428 case ObjCIvarRefExprClass:
1429 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001430 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001431 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001432 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001433 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001434 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001435 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001436 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001437 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001438 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001439 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001440 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1441 if (VD->hasGlobalStorage())
1442 return true;
1443 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001444 // dereferencing to a pointer is always a gc'able candidate,
1445 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001446 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001447 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001448 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001449 return false;
1450 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001451 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001452 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001453 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001454 }
1455 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001456 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001457 }
1458}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001459
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001460bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1461 if (isTypeDependent())
1462 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001463 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001464}
1465
Sebastian Redl369e51f2010-09-10 20:55:33 +00001466static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1467 Expr::CanThrowResult CT2) {
1468 // CanThrowResult constants are ordered so that the maximum is the correct
1469 // merge result.
1470 return CT1 > CT2 ? CT1 : CT2;
1471}
1472
1473static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1474 Expr *E = const_cast<Expr*>(CE);
1475 Expr::CanThrowResult R = Expr::CT_Cannot;
1476 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1477 I != IE && R != Expr::CT_Can; ++I) {
1478 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1479 }
1480 return R;
1481}
1482
1483static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1484 bool NullThrows = true) {
1485 if (!D)
1486 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1487
1488 // See if we can get a function type from the decl somehow.
1489 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1490 if (!VD) // If we have no clue what we're calling, assume the worst.
1491 return Expr::CT_Can;
1492
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001493 // As an extension, we assume that __attribute__((nothrow)) functions don't
1494 // throw.
1495 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1496 return Expr::CT_Cannot;
1497
Sebastian Redl369e51f2010-09-10 20:55:33 +00001498 QualType T = VD->getType();
1499 const FunctionProtoType *FT;
1500 if ((FT = T->getAs<FunctionProtoType>())) {
1501 } else if (const PointerType *PT = T->getAs<PointerType>())
1502 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1503 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1504 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1505 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1506 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1507 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1508 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1509
1510 if (!FT)
1511 return Expr::CT_Can;
1512
1513 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1514}
1515
1516static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1517 if (DC->isTypeDependent())
1518 return Expr::CT_Dependent;
1519
Sebastian Redl295995c2010-09-10 20:55:47 +00001520 if (!DC->getTypeAsWritten()->isReferenceType())
1521 return Expr::CT_Cannot;
1522
Sebastian Redl369e51f2010-09-10 20:55:33 +00001523 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1524}
1525
1526static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1527 const CXXTypeidExpr *DC) {
1528 if (DC->isTypeOperand())
1529 return Expr::CT_Cannot;
1530
1531 Expr *Op = DC->getExprOperand();
1532 if (Op->isTypeDependent())
1533 return Expr::CT_Dependent;
1534
1535 const RecordType *RT = Op->getType()->getAs<RecordType>();
1536 if (!RT)
1537 return Expr::CT_Cannot;
1538
1539 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1540 return Expr::CT_Cannot;
1541
1542 if (Op->Classify(C).isPRValue())
1543 return Expr::CT_Cannot;
1544
1545 return Expr::CT_Can;
1546}
1547
1548Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1549 // C++ [expr.unary.noexcept]p3:
1550 // [Can throw] if in a potentially-evaluated context the expression would
1551 // contain:
1552 switch (getStmtClass()) {
1553 case CXXThrowExprClass:
1554 // - a potentially evaluated throw-expression
1555 return CT_Can;
1556
1557 case CXXDynamicCastExprClass: {
1558 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1559 // where T is a reference type, that requires a run-time check
1560 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1561 if (CT == CT_Can)
1562 return CT;
1563 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1564 }
1565
1566 case CXXTypeidExprClass:
1567 // - a potentially evaluated typeid expression applied to a glvalue
1568 // expression whose type is a polymorphic class type
1569 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1570
1571 // - a potentially evaluated call to a function, member function, function
1572 // pointer, or member function pointer that does not have a non-throwing
1573 // exception-specification
1574 case CallExprClass:
1575 case CXXOperatorCallExprClass:
1576 case CXXMemberCallExprClass: {
1577 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1578 if (CT == CT_Can)
1579 return CT;
1580 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1581 }
1582
Sebastian Redl295995c2010-09-10 20:55:47 +00001583 case CXXConstructExprClass:
1584 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001585 CanThrowResult CT = CanCalleeThrow(
1586 cast<CXXConstructExpr>(this)->getConstructor());
1587 if (CT == CT_Can)
1588 return CT;
1589 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1590 }
1591
1592 case CXXNewExprClass: {
1593 CanThrowResult CT = MergeCanThrow(
1594 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1595 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1596 /*NullThrows*/false));
1597 if (CT == CT_Can)
1598 return CT;
1599 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1600 }
1601
1602 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001603 CanThrowResult CT = CanCalleeThrow(
1604 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1605 if (CT == CT_Can)
1606 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001607 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1608 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1609 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1610 Arg = Cast->getSubExpr();
1611 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1612 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1613 CanThrowResult CT2 = CanCalleeThrow(
1614 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1615 if (CT2 == CT_Can)
1616 return CT2;
1617 CT = MergeCanThrow(CT, CT2);
1618 }
1619 }
1620 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1621 }
1622
1623 case CXXBindTemporaryExprClass: {
1624 // The bound temporary has to be destroyed again, which might throw.
1625 CanThrowResult CT = CanCalleeThrow(
1626 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1627 if (CT == CT_Can)
1628 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001629 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1630 }
1631
1632 // ObjC message sends are like function calls, but never have exception
1633 // specs.
1634 case ObjCMessageExprClass:
1635 case ObjCPropertyRefExprClass:
1636 case ObjCImplicitSetterGetterRefExprClass:
1637 return CT_Can;
1638
1639 // Many other things have subexpressions, so we have to test those.
1640 // Some are simple:
1641 case ParenExprClass:
1642 case MemberExprClass:
1643 case CXXReinterpretCastExprClass:
1644 case CXXConstCastExprClass:
1645 case ConditionalOperatorClass:
1646 case CompoundLiteralExprClass:
1647 case ExtVectorElementExprClass:
1648 case InitListExprClass:
1649 case DesignatedInitExprClass:
1650 case ParenListExprClass:
1651 case VAArgExprClass:
1652 case CXXDefaultArgExprClass:
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001653 case CXXExprWithTemporariesClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001654 case ObjCIvarRefExprClass:
1655 case ObjCIsaExprClass:
1656 case ShuffleVectorExprClass:
1657 return CanSubExprsThrow(C, this);
1658
1659 // Some might be dependent for other reasons.
1660 case UnaryOperatorClass:
1661 case ArraySubscriptExprClass:
1662 case ImplicitCastExprClass:
1663 case CStyleCastExprClass:
1664 case CXXStaticCastExprClass:
1665 case CXXFunctionalCastExprClass:
1666 case BinaryOperatorClass:
1667 case CompoundAssignOperatorClass: {
1668 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1669 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1670 }
1671
1672 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1673 case StmtExprClass:
1674 return CT_Can;
1675
1676 case ChooseExprClass:
1677 if (isTypeDependent() || isValueDependent())
1678 return CT_Dependent;
1679 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1680
1681 // Some expressions are always dependent.
1682 case DependentScopeDeclRefExprClass:
1683 case CXXUnresolvedConstructExprClass:
1684 case CXXDependentScopeMemberExprClass:
1685 return CT_Dependent;
1686
1687 default:
1688 // All other expressions don't have subexpressions, or else they are
1689 // unevaluated.
1690 return CT_Cannot;
1691 }
1692}
1693
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001694Expr* Expr::IgnoreParens() {
1695 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001696 while (true) {
1697 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1698 E = P->getSubExpr();
1699 continue;
1700 }
1701 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1702 if (P->getOpcode() == UO_Extension) {
1703 E = P->getSubExpr();
1704 continue;
1705 }
1706 }
1707 return E;
1708 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001709}
1710
Chris Lattner56f34942008-02-13 01:02:39 +00001711/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1712/// or CastExprs or ImplicitCastExprs, returning their operand.
1713Expr *Expr::IgnoreParenCasts() {
1714 Expr *E = this;
1715 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001716 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001717 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001718 continue;
1719 }
1720 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001721 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001722 continue;
1723 }
1724 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1725 if (P->getOpcode() == UO_Extension) {
1726 E = P->getSubExpr();
1727 continue;
1728 }
1729 }
1730 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001731 }
1732}
1733
John McCall2fc46bf2010-05-05 22:59:52 +00001734Expr *Expr::IgnoreParenImpCasts() {
1735 Expr *E = this;
1736 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001737 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001738 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001739 continue;
1740 }
1741 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001742 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001743 continue;
1744 }
1745 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1746 if (P->getOpcode() == UO_Extension) {
1747 E = P->getSubExpr();
1748 continue;
1749 }
1750 }
1751 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001752 }
1753}
1754
Chris Lattnerecdd8412009-03-13 17:28:01 +00001755/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1756/// value (including ptr->int casts of the same size). Strip off any
1757/// ParenExpr or CastExprs, returning their operand.
1758Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1759 Expr *E = this;
1760 while (true) {
1761 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1762 E = P->getSubExpr();
1763 continue;
1764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Chris Lattnerecdd8412009-03-13 17:28:01 +00001766 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1767 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001768 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001769 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Chris Lattnerecdd8412009-03-13 17:28:01 +00001771 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1772 E = SE;
1773 continue;
1774 }
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001776 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001777 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001778 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001779 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001780 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1781 E = SE;
1782 continue;
1783 }
1784 }
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001786 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1787 if (P->getOpcode() == UO_Extension) {
1788 E = P->getSubExpr();
1789 continue;
1790 }
1791 }
1792
Chris Lattnerecdd8412009-03-13 17:28:01 +00001793 return E;
1794 }
1795}
1796
Douglas Gregor6eef5192009-12-14 19:27:10 +00001797bool Expr::isDefaultArgument() const {
1798 const Expr *E = this;
1799 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1800 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001801
Douglas Gregor6eef5192009-12-14 19:27:10 +00001802 return isa<CXXDefaultArgExpr>(E);
1803}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001804
Douglas Gregor2f599792010-04-02 18:24:57 +00001805/// \brief Skip over any no-op casts and any temporary-binding
1806/// expressions.
1807static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1808 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001809 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001810 E = ICE->getSubExpr();
1811 else
1812 break;
1813 }
1814
1815 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1816 E = BE->getSubExpr();
1817
1818 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001819 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001820 E = ICE->getSubExpr();
1821 else
1822 break;
1823 }
Sean Huntc3021132010-05-05 15:23:54 +00001824
Douglas Gregor2f599792010-04-02 18:24:57 +00001825 return E;
1826}
1827
John McCall558d2ab2010-09-15 10:14:12 +00001828/// isTemporaryObject - Determines if this expression produces a
1829/// temporary of the given class type.
1830bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1831 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1832 return false;
1833
Douglas Gregor2f599792010-04-02 18:24:57 +00001834 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1835
John McCall58277b52010-09-15 20:59:13 +00001836 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001837 if (!E->Classify(C).isPRValue()) {
1838 // In this context, property reference is a message call and is pr-value.
1839 if (!isa<ObjCPropertyRefExpr>(E) &&
1840 !isa<ObjCImplicitSetterGetterRefExpr>(E))
1841 return false;
1842 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001843
John McCall19e60ad2010-09-16 06:57:56 +00001844 // Black-list a few cases which yield pr-values of class type that don't
1845 // refer to temporaries of that type:
1846
1847 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001848 if (isa<ImplicitCastExpr>(E)) {
1849 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1850 case CK_DerivedToBase:
1851 case CK_UncheckedDerivedToBase:
1852 return false;
1853 default:
1854 break;
1855 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001856 }
1857
John McCall19e60ad2010-09-16 06:57:56 +00001858 // - member expressions (all)
1859 if (isa<MemberExpr>(E))
1860 return false;
1861
John McCall558d2ab2010-09-15 10:14:12 +00001862 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001863}
1864
Douglas Gregor898574e2008-12-05 23:32:09 +00001865/// hasAnyTypeDependentArguments - Determines if any of the expressions
1866/// in Exprs is type-dependent.
1867bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1868 for (unsigned I = 0; I < NumExprs; ++I)
1869 if (Exprs[I]->isTypeDependent())
1870 return true;
1871
1872 return false;
1873}
1874
1875/// hasAnyValueDependentArguments - Determines if any of the expressions
1876/// in Exprs is value-dependent.
1877bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1878 for (unsigned I = 0; I < NumExprs; ++I)
1879 if (Exprs[I]->isValueDependent())
1880 return true;
1881
1882 return false;
1883}
1884
John McCall4204f072010-08-02 21:13:48 +00001885bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001886 // This function is attempting whether an expression is an initializer
1887 // which can be evaluated at compile-time. isEvaluatable handles most
1888 // of the cases, but it can't deal with some initializer-specific
1889 // expressions, and it can't deal with aggregates; we deal with those here,
1890 // and fall back to isEvaluatable for the other cases.
1891
John McCall4204f072010-08-02 21:13:48 +00001892 // If we ever capture reference-binding directly in the AST, we can
1893 // kill the second parameter.
1894
1895 if (IsForRef) {
1896 EvalResult Result;
1897 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1898 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001899
Anders Carlssone8a32b82008-11-24 05:23:59 +00001900 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001901 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001902 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001903 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001904 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001905 return true;
John McCallb4b9b152010-08-01 21:51:45 +00001906 case CXXTemporaryObjectExprClass:
1907 case CXXConstructExprClass: {
1908 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00001909
1910 // Only if it's
1911 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00001912 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00001913 if (!CE->getNumArgs()) return true;
1914
1915 // 2) an elidable trivial copy construction of an operand which is
1916 // itself a constant initializer. Note that we consider the
1917 // operand on its own, *not* as a reference binding.
1918 return CE->isElidable() &&
1919 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00001920 }
Nate Begeman59b5da62009-01-18 03:20:47 +00001921 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001922 // This handles gcc's extension that allows global initializers like
1923 // "struct x {int x;} x = (struct x) {};".
1924 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001925 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00001926 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00001927 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001928 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001929 // FIXME: This doesn't deal with fields with reference types correctly.
1930 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1931 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001932 const InitListExpr *Exp = cast<InitListExpr>(this);
1933 unsigned numInits = Exp->getNumInits();
1934 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00001935 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001936 return false;
1937 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001938 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001939 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001940 case ImplicitValueInitExprClass:
1941 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001942 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00001943 return cast<ParenExpr>(this)->getSubExpr()
1944 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00001945 case ChooseExprClass:
1946 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
1947 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001948 case UnaryOperatorClass: {
1949 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001950 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00001951 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001952 break;
1953 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001954 case BinaryOperatorClass: {
1955 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1956 // but this handles the common case.
1957 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001958 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00001959 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1960 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1961 return true;
1962 break;
1963 }
John McCall4204f072010-08-02 21:13:48 +00001964 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00001965 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00001966 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001967 case CStyleCastExprClass:
1968 // Handle casts with a destination that's a struct or union; this
1969 // deals with both the gcc no-op struct cast extension and the
1970 // cast-to-union extension.
1971 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00001972 return cast<CastExpr>(this)->getSubExpr()
1973 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001974
Chris Lattner430656e2009-10-13 22:12:09 +00001975 // Integer->integer casts can be handled here, which is important for
1976 // things like (int)(&&x-&&y). Scary but true.
1977 if (getType()->isIntegerType() &&
1978 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00001979 return cast<CastExpr>(this)->getSubExpr()
1980 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001981
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001982 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001983 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001984 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001985}
1986
Reid Spencer5f016e22007-07-11 17:01:13 +00001987/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1988/// integer constant expression with the value zero, or if this is one that is
1989/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001990bool Expr::isNullPointerConstant(ASTContext &Ctx,
1991 NullPointerConstantValueDependence NPC) const {
1992 if (isValueDependent()) {
1993 switch (NPC) {
1994 case NPC_NeverValueDependent:
1995 assert(false && "Unexpected value dependent expression!");
1996 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00001997
Douglas Gregorce940492009-09-25 04:25:58 +00001998 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001999 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002000
Douglas Gregorce940492009-09-25 04:25:58 +00002001 case NPC_ValueDependentIsNotNull:
2002 return false;
2003 }
2004 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002005
Sebastian Redl07779722008-10-31 14:43:28 +00002006 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002007 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002008 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002009 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002010 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002011 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002012 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002013 Pointee->isVoidType() && // to void*
2014 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002015 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002016 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002018 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2019 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002020 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002021 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2022 // Accept ((void*)0) as a null pointer constant, as many other
2023 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002024 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002025 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002026 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002027 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002028 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002029 } else if (isa<GNUNullExpr>(this)) {
2030 // The GNU __null extension is always a null pointer constant.
2031 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002032 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002033
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002034 // C++0x nullptr_t is always a null pointer constant.
2035 if (getType()->isNullPtrType())
2036 return true;
2037
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002038 if (const RecordType *UT = getType()->getAsUnionType())
2039 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2040 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2041 const Expr *InitExpr = CLE->getInitializer();
2042 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2043 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2044 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002045 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002046 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002047 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002048 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 // If we have an integer constant expression, we need to *evaluate* it and
2051 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002052 llvm::APSInt Result;
2053 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002054}
Steve Naroff31a45842007-07-28 23:10:27 +00002055
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002056FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002057 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002058
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002059 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002060 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002061 ICE->getCastKind() == CK_NoOp)
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002062 E = ICE->getSubExpr()->IgnoreParens();
2063 else
2064 break;
2065 }
2066
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002067 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002068 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002069 if (Field->isBitField())
2070 return Field;
2071
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002072 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2073 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2074 if (Field->isBitField())
2075 return Field;
2076
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002077 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2078 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2079 return BinOp->getLHS()->getBitField();
2080
2081 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002082}
2083
Anders Carlsson09380262010-01-31 17:18:49 +00002084bool Expr::refersToVectorElement() const {
2085 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002086
Anders Carlsson09380262010-01-31 17:18:49 +00002087 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002088 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002089 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002090 E = ICE->getSubExpr()->IgnoreParens();
2091 else
2092 break;
2093 }
Sean Huntc3021132010-05-05 15:23:54 +00002094
Anders Carlsson09380262010-01-31 17:18:49 +00002095 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2096 return ASE->getBase()->getType()->isVectorType();
2097
2098 if (isa<ExtVectorElementExpr>(E))
2099 return true;
2100
2101 return false;
2102}
2103
Chris Lattner2140e902009-02-16 22:14:05 +00002104/// isArrow - Return true if the base expression is a pointer to vector,
2105/// return false if the base expression is a vector.
2106bool ExtVectorElementExpr::isArrow() const {
2107 return getBase()->getType()->isPointerType();
2108}
2109
Nate Begeman213541a2008-04-18 23:10:10 +00002110unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002111 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002112 return VT->getNumElements();
2113 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002114}
2115
Nate Begeman8a997642008-05-09 06:41:27 +00002116/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002117bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002118 // FIXME: Refactor this code to an accessor on the AST node which returns the
2119 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002120 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002121
2122 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002123 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002124 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002125
Nate Begeman190d6a22009-01-18 02:01:21 +00002126 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002127 if (Comp[0] == 's' || Comp[0] == 'S')
2128 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Daniel Dunbar15027422009-10-17 23:53:04 +00002130 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2131 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002132 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002133
Steve Narofffec0b492007-07-30 03:29:09 +00002134 return false;
2135}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002136
Nate Begeman8a997642008-05-09 06:41:27 +00002137/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002138void ExtVectorElementExpr::getEncodedElementAccess(
2139 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002140 llvm::StringRef Comp = Accessor->getName();
2141 if (Comp[0] == 's' || Comp[0] == 'S')
2142 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002144 bool isHi = Comp == "hi";
2145 bool isLo = Comp == "lo";
2146 bool isEven = Comp == "even";
2147 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Nate Begeman8a997642008-05-09 06:41:27 +00002149 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2150 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Nate Begeman8a997642008-05-09 06:41:27 +00002152 if (isHi)
2153 Index = e + i;
2154 else if (isLo)
2155 Index = i;
2156 else if (isEven)
2157 Index = 2 * i;
2158 else if (isOdd)
2159 Index = 2 * i + 1;
2160 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002161 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002162
Nate Begeman3b8d1162008-05-13 21:03:02 +00002163 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002164 }
Nate Begeman8a997642008-05-09 06:41:27 +00002165}
2166
Douglas Gregor04badcf2010-04-21 00:45:42 +00002167ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002168 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002169 SourceLocation LBracLoc,
2170 SourceLocation SuperLoc,
2171 bool IsInstanceSuper,
2172 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002173 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002174 ObjCMethodDecl *Method,
2175 Expr **Args, unsigned NumArgs,
2176 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002177 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2178 /*TypeDependent=*/false, /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002179 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2180 HasMethod(Method != 0), SuperLoc(SuperLoc),
2181 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2182 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002183 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002184{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002185 setReceiverPointer(SuperType.getAsOpaquePtr());
2186 if (NumArgs)
2187 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002188}
2189
Douglas Gregor04badcf2010-04-21 00:45:42 +00002190ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002191 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002192 SourceLocation LBracLoc,
2193 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002194 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002195 ObjCMethodDecl *Method,
2196 Expr **Args, unsigned NumArgs,
2197 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002198 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00002199 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002200 hasAnyValueDependentArguments(Args, NumArgs))),
2201 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2202 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2203 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002204 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002205{
2206 setReceiverPointer(Receiver);
2207 if (NumArgs)
2208 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002209}
2210
Douglas Gregor04badcf2010-04-21 00:45:42 +00002211ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002212 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002213 SourceLocation LBracLoc,
2214 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002215 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002216 ObjCMethodDecl *Method,
2217 Expr **Args, unsigned NumArgs,
2218 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002219 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00002220 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002221 hasAnyValueDependentArguments(Args, NumArgs))),
2222 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2223 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2224 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002225 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002226{
2227 setReceiverPointer(Receiver);
2228 if (NumArgs)
2229 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002230}
2231
Douglas Gregor04badcf2010-04-21 00:45:42 +00002232ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002233 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002234 SourceLocation LBracLoc,
2235 SourceLocation SuperLoc,
2236 bool IsInstanceSuper,
2237 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002238 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002239 ObjCMethodDecl *Method,
2240 Expr **Args, unsigned NumArgs,
2241 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002242 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002243 NumArgs * sizeof(Expr *);
2244 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002245 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00002246 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002247 RBracLoc);
2248}
2249
2250ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002251 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002252 SourceLocation LBracLoc,
2253 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002254 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002255 ObjCMethodDecl *Method,
2256 Expr **Args, unsigned NumArgs,
2257 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002258 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002259 NumArgs * sizeof(Expr *);
2260 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002261 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002262 NumArgs, RBracLoc);
2263}
2264
2265ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002266 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002267 SourceLocation LBracLoc,
2268 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002269 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002270 ObjCMethodDecl *Method,
2271 Expr **Args, unsigned NumArgs,
2272 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002273 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002274 NumArgs * sizeof(Expr *);
2275 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002276 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002277 NumArgs, RBracLoc);
2278}
2279
Sean Huntc3021132010-05-05 15:23:54 +00002280ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002281 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002282 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002283 NumArgs * sizeof(Expr *);
2284 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2285 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2286}
Sean Huntc3021132010-05-05 15:23:54 +00002287
Douglas Gregor04badcf2010-04-21 00:45:42 +00002288Selector ObjCMessageExpr::getSelector() const {
2289 if (HasMethod)
2290 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2291 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002292 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002293}
2294
2295ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2296 switch (getReceiverKind()) {
2297 case Instance:
2298 if (const ObjCObjectPointerType *Ptr
2299 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2300 return Ptr->getInterfaceDecl();
2301 break;
2302
2303 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002304 if (const ObjCObjectType *Ty
2305 = getClassReceiver()->getAs<ObjCObjectType>())
2306 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002307 break;
2308
2309 case SuperInstance:
2310 if (const ObjCObjectPointerType *Ptr
2311 = getSuperType()->getAs<ObjCObjectPointerType>())
2312 return Ptr->getInterfaceDecl();
2313 break;
2314
2315 case SuperClass:
2316 if (const ObjCObjectPointerType *Iface
2317 = getSuperType()->getAs<ObjCObjectPointerType>())
2318 return Iface->getInterfaceDecl();
2319 break;
2320 }
2321
2322 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002323}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002324
Chris Lattner27437ca2007-10-25 00:29:32 +00002325bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002326 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002327}
2328
Nate Begeman888376a2009-08-12 02:28:50 +00002329void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2330 unsigned NumExprs) {
2331 if (SubExprs) C.Deallocate(SubExprs);
2332
2333 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002334 this->NumExprs = NumExprs;
2335 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002336}
Nate Begeman888376a2009-08-12 02:28:50 +00002337
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002338//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002339// DesignatedInitExpr
2340//===----------------------------------------------------------------------===//
2341
2342IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2343 assert(Kind == FieldDesignator && "Only valid on a field designator");
2344 if (Field.NameOrField & 0x01)
2345 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2346 else
2347 return getField()->getIdentifier();
2348}
2349
Sean Huntc3021132010-05-05 15:23:54 +00002350DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002351 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002352 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002353 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002354 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002355 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002356 unsigned NumIndexExprs,
2357 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002358 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002359 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregor9ea62762009-05-21 23:17:49 +00002360 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002361 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2362 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002363 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002364
2365 // Record the initializer itself.
2366 child_iterator Child = child_begin();
2367 *Child++ = Init;
2368
2369 // Copy the designators and their subexpressions, computing
2370 // value-dependence along the way.
2371 unsigned IndexIdx = 0;
2372 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002373 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002374
2375 if (this->Designators[I].isArrayDesignator()) {
2376 // Compute type- and value-dependence.
2377 Expr *Index = IndexExprs[IndexIdx];
John McCall8e6285a2010-10-26 08:39:16 +00002378 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002379 Index->isTypeDependent() || Index->isValueDependent();
2380
2381 // Copy the index expressions into permanent storage.
2382 *Child++ = IndexExprs[IndexIdx++];
2383 } else if (this->Designators[I].isArrayRangeDesignator()) {
2384 // Compute type- and value-dependence.
2385 Expr *Start = IndexExprs[IndexIdx];
2386 Expr *End = IndexExprs[IndexIdx + 1];
John McCall8e6285a2010-10-26 08:39:16 +00002387 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002388 Start->isTypeDependent() || Start->isValueDependent() ||
2389 End->isTypeDependent() || End->isValueDependent();
2390
2391 // Copy the start/end expressions into permanent storage.
2392 *Child++ = IndexExprs[IndexIdx++];
2393 *Child++ = IndexExprs[IndexIdx++];
2394 }
2395 }
2396
2397 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002398}
2399
Douglas Gregor05c13a32009-01-22 00:58:24 +00002400DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002401DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002402 unsigned NumDesignators,
2403 Expr **IndexExprs, unsigned NumIndexExprs,
2404 SourceLocation ColonOrEqualLoc,
2405 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002406 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002407 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002408 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002409 ColonOrEqualLoc, UsesColonSyntax,
2410 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002411}
2412
Mike Stump1eb44332009-09-09 15:08:12 +00002413DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002414 unsigned NumIndexExprs) {
2415 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2416 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2417 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2418}
2419
Douglas Gregor319d57f2010-01-06 23:17:19 +00002420void DesignatedInitExpr::setDesignators(ASTContext &C,
2421 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002422 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002423 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002424 NumDesignators = NumDesigs;
2425 for (unsigned I = 0; I != NumDesigs; ++I)
2426 Designators[I] = Desigs[I];
2427}
2428
Douglas Gregor05c13a32009-01-22 00:58:24 +00002429SourceRange DesignatedInitExpr::getSourceRange() const {
2430 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002431 Designator &First =
2432 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002433 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002434 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002435 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2436 else
2437 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2438 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002439 StartLoc =
2440 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002441 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2442}
2443
Douglas Gregor05c13a32009-01-22 00:58:24 +00002444Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2445 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2446 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2447 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002448 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2449 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2450}
2451
2452Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002453 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002454 "Requires array range designator");
2455 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2456 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002457 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2458 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2459}
2460
2461Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002462 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002463 "Requires array range designator");
2464 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2465 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002466 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2467 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2468}
2469
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002470/// \brief Replaces the designator at index @p Idx with the series
2471/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002472void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002473 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002474 const Designator *Last) {
2475 unsigned NumNewDesignators = Last - First;
2476 if (NumNewDesignators == 0) {
2477 std::copy_backward(Designators + Idx + 1,
2478 Designators + NumDesignators,
2479 Designators + Idx);
2480 --NumNewDesignators;
2481 return;
2482 } else if (NumNewDesignators == 1) {
2483 Designators[Idx] = *First;
2484 return;
2485 }
2486
Mike Stump1eb44332009-09-09 15:08:12 +00002487 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002488 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002489 std::copy(Designators, Designators + Idx, NewDesignators);
2490 std::copy(First, Last, NewDesignators + Idx);
2491 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2492 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002493 Designators = NewDesignators;
2494 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2495}
2496
Mike Stump1eb44332009-09-09 15:08:12 +00002497ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002498 Expr **exprs, unsigned nexprs,
2499 SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00002500: Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002501 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002502 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002503 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002504
Nate Begeman2ef13e52009-08-10 23:49:36 +00002505 Exprs = new (C) Stmt*[nexprs];
2506 for (unsigned i = 0; i != nexprs; ++i)
2507 Exprs[i] = exprs[i];
2508}
2509
Douglas Gregor05c13a32009-01-22 00:58:24 +00002510//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002511// ExprIterator.
2512//===----------------------------------------------------------------------===//
2513
2514Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2515Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2516Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2517const Expr* ConstExprIterator::operator[](size_t idx) const {
2518 return cast<Expr>(I[idx]);
2519}
2520const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2521const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2522
2523//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002524// Child Iterators for iterating over subexpressions/substatements
2525//===----------------------------------------------------------------------===//
2526
2527// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002528Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2529Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002530
Steve Naroff7779db42007-11-12 14:29:37 +00002531// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002532Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2533Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002534
Steve Naroffe3e9add2008-06-02 23:03:37 +00002535// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002536Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2537{
2538 if (BaseExprOrSuperType.is<Stmt*>()) {
2539 // Hack alert!
2540 return reinterpret_cast<Stmt**> (&BaseExprOrSuperType);
2541 }
2542 return child_iterator();
2543}
2544
2545Stmt::child_iterator ObjCPropertyRefExpr::child_end()
2546{ return BaseExprOrSuperType.is<Stmt*>() ?
2547 reinterpret_cast<Stmt**> (&BaseExprOrSuperType)+1 :
2548 child_iterator();
2549}
Steve Naroffae784072008-05-30 00:40:33 +00002550
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002551// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002552Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002553 // If this is accessing a class member or super, skip that entry.
2554 // Technically, 2nd condition is sufficient. But I want to be verbose
2555 if (isSuperReceiver() || !Base)
2556 return child_iterator();
2557 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002558}
Mike Stump1eb44332009-09-09 15:08:12 +00002559Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002560 if (isSuperReceiver() || !Base)
2561 return child_iterator();
Mike Stump1eb44332009-09-09 15:08:12 +00002562 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002563}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002564
Steve Narofff242b1b2009-07-24 17:54:45 +00002565// ObjCIsaExpr
2566Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2567Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2568
Chris Lattnerd9f69102008-08-10 01:53:14 +00002569// PredefinedExpr
2570Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2571Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002572
2573// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002574Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2575Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002576
2577// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002578Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002579Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002580
2581// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002582Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2583Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002584
Chris Lattner5d661452007-08-26 03:42:43 +00002585// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002586Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2587Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002588
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002589// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002590Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2591Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002592
2593// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002594Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2595Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002596
2597// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002598Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2599Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002600
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002601// OffsetOfExpr
2602Stmt::child_iterator OffsetOfExpr::child_begin() {
2603 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2604 + NumComps);
2605}
2606Stmt::child_iterator OffsetOfExpr::child_end() {
2607 return child_iterator(&*child_begin() + NumExprs);
2608}
2609
Sebastian Redl05189992008-11-11 17:56:53 +00002610// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002611Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002612 // If this is of a type and the type is a VLA type (and not a typedef), the
2613 // size expression of the VLA needs to be treated as an executable expression.
2614 // Why isn't this weirdness documented better in StmtIterator?
2615 if (isArgumentType()) {
2616 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2617 getArgumentType().getTypePtr()))
2618 return child_iterator(T);
2619 return child_iterator();
2620 }
Sebastian Redld4575892008-12-03 23:17:54 +00002621 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002622}
Sebastian Redl05189992008-11-11 17:56:53 +00002623Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2624 if (isArgumentType())
2625 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002626 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002627}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002628
2629// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002630Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002631 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002632}
Ted Kremenek1237c672007-08-24 20:06:47 +00002633Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002634 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002635}
2636
2637// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002638Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002639 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002640}
Ted Kremenek1237c672007-08-24 20:06:47 +00002641Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002642 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002643}
Ted Kremenek1237c672007-08-24 20:06:47 +00002644
2645// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002646Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2647Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002648
Nate Begeman213541a2008-04-18 23:10:10 +00002649// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002650Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2651Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002652
2653// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002654Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2655Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002656
Ted Kremenek1237c672007-08-24 20:06:47 +00002657// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002658Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2659Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002660
2661// BinaryOperator
2662Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002663 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002664}
Ted Kremenek1237c672007-08-24 20:06:47 +00002665Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002666 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002667}
2668
2669// ConditionalOperator
2670Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002671 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002672}
Ted Kremenek1237c672007-08-24 20:06:47 +00002673Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002674 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002675}
2676
2677// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002678Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2679Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002680
Ted Kremenek1237c672007-08-24 20:06:47 +00002681// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002682Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2683Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002684
2685// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002686Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2687 return child_iterator();
2688}
2689
2690Stmt::child_iterator TypesCompatibleExpr::child_end() {
2691 return child_iterator();
2692}
Ted Kremenek1237c672007-08-24 20:06:47 +00002693
2694// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002695Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2696Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002697
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002698// GNUNullExpr
2699Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2700Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2701
Eli Friedmand38617c2008-05-14 19:38:39 +00002702// ShuffleVectorExpr
2703Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002704 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002705}
2706Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002707 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002708}
2709
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002710// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002711Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2712Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002713
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002714// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002715Stmt::child_iterator InitListExpr::child_begin() {
2716 return InitExprs.size() ? &InitExprs[0] : 0;
2717}
2718Stmt::child_iterator InitListExpr::child_end() {
2719 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2720}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002721
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002722// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002723Stmt::child_iterator DesignatedInitExpr::child_begin() {
2724 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2725 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002726 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2727}
2728Stmt::child_iterator DesignatedInitExpr::child_end() {
2729 return child_iterator(&*child_begin() + NumSubExprs);
2730}
2731
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002732// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002733Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2734 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002735}
2736
Mike Stump1eb44332009-09-09 15:08:12 +00002737Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2738 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002739}
2740
Nate Begeman2ef13e52009-08-10 23:49:36 +00002741// ParenListExpr
2742Stmt::child_iterator ParenListExpr::child_begin() {
2743 return &Exprs[0];
2744}
2745Stmt::child_iterator ParenListExpr::child_end() {
2746 return &Exprs[0]+NumExprs;
2747}
2748
Ted Kremenek1237c672007-08-24 20:06:47 +00002749// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002750Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002751 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002752}
2753Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002754 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002755}
Ted Kremenek1237c672007-08-24 20:06:47 +00002756
2757// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002758Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2759Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002760
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002761// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002762Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002763 return child_iterator();
2764}
2765Stmt::child_iterator ObjCSelectorExpr::child_end() {
2766 return child_iterator();
2767}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002768
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002769// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002770Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2771 return child_iterator();
2772}
2773Stmt::child_iterator ObjCProtocolExpr::child_end() {
2774 return child_iterator();
2775}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002776
Steve Naroff563477d2007-09-18 23:55:05 +00002777// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002778Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002779 if (getReceiverKind() == Instance)
2780 return reinterpret_cast<Stmt **>(this + 1);
2781 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002782}
2783Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002784 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002785}
2786
Steve Naroff4eb206b2008-09-03 18:15:37 +00002787// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002788Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2789Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002790
Ted Kremenek9da13f92008-09-26 23:24:14 +00002791Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2792Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002793
2794// OpaqueValueExpr
2795SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2796Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2797Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2798