blob: 6e56603c53e63b6fdc4906823e5cdc8211c1804d [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 McCall0ae287a2010-12-01 04:43:34 +0000825 case CK_LValueToRValue:
826 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +0000827 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000828 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000829 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000830 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000831 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000832 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000833 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000834 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000835 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000836 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000837 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000838 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000839 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000840 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000841 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000842 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000843 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000844 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000845 case CK_NullToPointer:
846 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000847 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000848 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000849 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000850 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000851 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000852 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000853 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000854 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000855 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000856 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000857 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000858 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000859 case CK_PointerToBoolean:
860 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000861 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000862 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000863 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000864 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000865 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000866 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000867 case CK_IntegralToBoolean:
868 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000869 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000870 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000871 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000872 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000873 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000874 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000875 case CK_FloatingToBoolean:
876 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000877 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000878 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000879 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000880 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000881 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000882 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000883 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000884 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000885 case CK_FloatingRealToComplex:
886 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000887 case CK_FloatingComplexToReal:
888 return "FloatingComplexToReal";
889 case CK_FloatingComplexToBoolean:
890 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000891 case CK_FloatingComplexCast:
892 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000893 case CK_FloatingComplexToIntegralComplex:
894 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000895 case CK_IntegralRealToComplex:
896 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000897 case CK_IntegralComplexToReal:
898 return "IntegralComplexToReal";
899 case CK_IntegralComplexToBoolean:
900 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000901 case CK_IntegralComplexCast:
902 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000903 case CK_IntegralComplexToFloatingComplex:
904 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
John McCall2bb5d002010-11-13 09:02:35 +0000907 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000908 return 0;
909}
910
Douglas Gregor6eef5192009-12-14 19:27:10 +0000911Expr *CastExpr::getSubExprAsWritten() {
912 Expr *SubExpr = 0;
913 CastExpr *E = this;
914 do {
915 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000916
Douglas Gregor6eef5192009-12-14 19:27:10 +0000917 // Skip any temporary bindings; they're implicit.
918 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
919 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000920
Douglas Gregor6eef5192009-12-14 19:27:10 +0000921 // Conversions by constructor and conversion functions have a
922 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +0000923 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000924 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +0000925 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000926 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000927
Douglas Gregor6eef5192009-12-14 19:27:10 +0000928 // If the subexpression we're left with is an implicit cast, look
929 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000930 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
931
Douglas Gregor6eef5192009-12-14 19:27:10 +0000932 return SubExpr;
933}
934
John McCallf871d0c2010-08-07 06:22:56 +0000935CXXBaseSpecifier **CastExpr::path_buffer() {
936 switch (getStmtClass()) {
937#define ABSTRACT_STMT(x)
938#define CASTEXPR(Type, Base) \
939 case Stmt::Type##Class: \
940 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
941#define STMT(Type, Base)
942#include "clang/AST/StmtNodes.inc"
943 default:
944 llvm_unreachable("non-cast expressions not possible here");
945 return 0;
946 }
947}
948
949void CastExpr::setCastPath(const CXXCastPath &Path) {
950 assert(Path.size() == path_size());
951 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
952}
953
954ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
955 CastKind Kind, Expr *Operand,
956 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +0000957 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +0000958 unsigned PathSize = (BasePath ? BasePath->size() : 0);
959 void *Buffer =
960 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
961 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +0000962 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +0000963 if (PathSize) E->setCastPath(*BasePath);
964 return E;
965}
966
967ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
968 unsigned PathSize) {
969 void *Buffer =
970 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
971 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
972}
973
974
975CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000976 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000977 const CXXCastPath *BasePath,
978 TypeSourceInfo *WrittenTy,
979 SourceLocation L, SourceLocation R) {
980 unsigned PathSize = (BasePath ? BasePath->size() : 0);
981 void *Buffer =
982 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
983 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000984 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +0000985 if (PathSize) E->setCastPath(*BasePath);
986 return E;
987}
988
989CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
990 void *Buffer =
991 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
992 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
993}
994
Reid Spencer5f016e22007-07-11 17:01:13 +0000995/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
996/// corresponds to, e.g. "<<=".
997const char *BinaryOperator::getOpcodeStr(Opcode Op) {
998 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000999 case BO_PtrMemD: return ".*";
1000 case BO_PtrMemI: return "->*";
1001 case BO_Mul: return "*";
1002 case BO_Div: return "/";
1003 case BO_Rem: return "%";
1004 case BO_Add: return "+";
1005 case BO_Sub: return "-";
1006 case BO_Shl: return "<<";
1007 case BO_Shr: return ">>";
1008 case BO_LT: return "<";
1009 case BO_GT: return ">";
1010 case BO_LE: return "<=";
1011 case BO_GE: return ">=";
1012 case BO_EQ: return "==";
1013 case BO_NE: return "!=";
1014 case BO_And: return "&";
1015 case BO_Xor: return "^";
1016 case BO_Or: return "|";
1017 case BO_LAnd: return "&&";
1018 case BO_LOr: return "||";
1019 case BO_Assign: return "=";
1020 case BO_MulAssign: return "*=";
1021 case BO_DivAssign: return "/=";
1022 case BO_RemAssign: return "%=";
1023 case BO_AddAssign: return "+=";
1024 case BO_SubAssign: return "-=";
1025 case BO_ShlAssign: return "<<=";
1026 case BO_ShrAssign: return ">>=";
1027 case BO_AndAssign: return "&=";
1028 case BO_XorAssign: return "^=";
1029 case BO_OrAssign: return "|=";
1030 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001032
1033 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001034}
1035
John McCall2de56d12010-08-25 11:45:40 +00001036BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001037BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1038 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001039 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001040 case OO_Plus: return BO_Add;
1041 case OO_Minus: return BO_Sub;
1042 case OO_Star: return BO_Mul;
1043 case OO_Slash: return BO_Div;
1044 case OO_Percent: return BO_Rem;
1045 case OO_Caret: return BO_Xor;
1046 case OO_Amp: return BO_And;
1047 case OO_Pipe: return BO_Or;
1048 case OO_Equal: return BO_Assign;
1049 case OO_Less: return BO_LT;
1050 case OO_Greater: return BO_GT;
1051 case OO_PlusEqual: return BO_AddAssign;
1052 case OO_MinusEqual: return BO_SubAssign;
1053 case OO_StarEqual: return BO_MulAssign;
1054 case OO_SlashEqual: return BO_DivAssign;
1055 case OO_PercentEqual: return BO_RemAssign;
1056 case OO_CaretEqual: return BO_XorAssign;
1057 case OO_AmpEqual: return BO_AndAssign;
1058 case OO_PipeEqual: return BO_OrAssign;
1059 case OO_LessLess: return BO_Shl;
1060 case OO_GreaterGreater: return BO_Shr;
1061 case OO_LessLessEqual: return BO_ShlAssign;
1062 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1063 case OO_EqualEqual: return BO_EQ;
1064 case OO_ExclaimEqual: return BO_NE;
1065 case OO_LessEqual: return BO_LE;
1066 case OO_GreaterEqual: return BO_GE;
1067 case OO_AmpAmp: return BO_LAnd;
1068 case OO_PipePipe: return BO_LOr;
1069 case OO_Comma: return BO_Comma;
1070 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001071 }
1072}
1073
1074OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1075 static const OverloadedOperatorKind OverOps[] = {
1076 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1077 OO_Star, OO_Slash, OO_Percent,
1078 OO_Plus, OO_Minus,
1079 OO_LessLess, OO_GreaterGreater,
1080 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1081 OO_EqualEqual, OO_ExclaimEqual,
1082 OO_Amp,
1083 OO_Caret,
1084 OO_Pipe,
1085 OO_AmpAmp,
1086 OO_PipePipe,
1087 OO_Equal, OO_StarEqual,
1088 OO_SlashEqual, OO_PercentEqual,
1089 OO_PlusEqual, OO_MinusEqual,
1090 OO_LessLessEqual, OO_GreaterGreaterEqual,
1091 OO_AmpEqual, OO_CaretEqual,
1092 OO_PipeEqual,
1093 OO_Comma
1094 };
1095 return OverOps[Opc];
1096}
1097
Ted Kremenek709210f2010-04-13 23:39:13 +00001098InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001099 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001100 SourceLocation rbraceloc)
John McCallf89e55a2010-11-18 06:31:45 +00001101 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001102 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001103 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001104 UnionFieldInit(0), HadArrayRangeDesignator(false)
1105{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001106 for (unsigned I = 0; I != numInits; ++I) {
1107 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001108 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001109 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001110 ExprBits.ValueDependent = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001111 }
Sean Huntc3021132010-05-05 15:23:54 +00001112
Ted Kremenek709210f2010-04-13 23:39:13 +00001113 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001114}
Reid Spencer5f016e22007-07-11 17:01:13 +00001115
Ted Kremenek709210f2010-04-13 23:39:13 +00001116void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001117 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001118 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001119}
1120
Ted Kremenek709210f2010-04-13 23:39:13 +00001121void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001122 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001123}
1124
Ted Kremenek709210f2010-04-13 23:39:13 +00001125Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001126 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001127 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001128 InitExprs.back() = expr;
1129 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor4c678342009-01-28 21:54:33 +00001132 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1133 InitExprs[Init] = expr;
1134 return Result;
1135}
1136
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001137SourceRange InitListExpr::getSourceRange() const {
1138 if (SyntacticForm)
1139 return SyntacticForm->getSourceRange();
1140 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1141 if (Beg.isInvalid()) {
1142 // Find the first non-null initializer.
1143 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1144 E = InitExprs.end();
1145 I != E; ++I) {
1146 if (Stmt *S = *I) {
1147 Beg = S->getLocStart();
1148 break;
1149 }
1150 }
1151 }
1152 if (End.isInvalid()) {
1153 // Find the first non-null initializer from the end.
1154 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1155 E = InitExprs.rend();
1156 I != E; ++I) {
1157 if (Stmt *S = *I) {
1158 End = S->getSourceRange().getEnd();
1159 break;
1160 }
1161 }
1162 }
1163 return SourceRange(Beg, End);
1164}
1165
Steve Naroffbfdcae62008-09-04 15:31:07 +00001166/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001167///
1168const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001169 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001170 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001171}
1172
Mike Stump1eb44332009-09-09 15:08:12 +00001173SourceLocation BlockExpr::getCaretLocation() const {
1174 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001175}
Mike Stump1eb44332009-09-09 15:08:12 +00001176const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001177 return TheBlock->getBody();
1178}
Mike Stump1eb44332009-09-09 15:08:12 +00001179Stmt *BlockExpr::getBody() {
1180 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001181}
Steve Naroff56ee6892008-10-08 17:01:13 +00001182
1183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184//===----------------------------------------------------------------------===//
1185// Generic Expression Routines
1186//===----------------------------------------------------------------------===//
1187
Chris Lattner026dc962009-02-14 07:37:35 +00001188/// isUnusedResultAWarning - Return true if this immediate expression should
1189/// be warned about if the result is unused. If so, fill in Loc and Ranges
1190/// with location to warn on and the source range[s] to report with the
1191/// warning.
1192bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001193 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001194 // Don't warn if the expr is type dependent. The type could end up
1195 // instantiating to void.
1196 if (isTypeDependent())
1197 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 switch (getStmtClass()) {
1200 default:
John McCall0faede62010-03-12 07:11:26 +00001201 if (getType()->isVoidType())
1202 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001203 Loc = getExprLoc();
1204 R1 = getSourceRange();
1205 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001207 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001208 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 case UnaryOperatorClass: {
1210 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001213 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001214 case UO_PostInc:
1215 case UO_PostDec:
1216 case UO_PreInc:
1217 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001218 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001219 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001221 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001222 return false;
1223 break;
John McCall2de56d12010-08-25 11:45:40 +00001224 case UO_Real:
1225 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001227 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1228 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001229 return false;
1230 break;
John McCall2de56d12010-08-25 11:45:40 +00001231 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001232 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 }
Chris Lattner026dc962009-02-14 07:37:35 +00001234 Loc = UO->getOperatorLoc();
1235 R1 = UO->getSubExpr()->getSourceRange();
1236 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001238 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001239 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001240 switch (BO->getOpcode()) {
1241 default:
1242 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001243 // Consider the RHS of comma for side effects. LHS was checked by
1244 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001245 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001246 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1247 // lvalue-ness) of an assignment written in a macro.
1248 if (IntegerLiteral *IE =
1249 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1250 if (IE->getValue() == 0)
1251 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001252 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1253 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001254 case BO_LAnd:
1255 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001256 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1257 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1258 return false;
1259 break;
John McCallbf0ee352010-02-16 04:10:53 +00001260 }
Chris Lattner026dc962009-02-14 07:37:35 +00001261 if (BO->isAssignmentOp())
1262 return false;
1263 Loc = BO->getOperatorLoc();
1264 R1 = BO->getLHS()->getSourceRange();
1265 R2 = BO->getRHS()->getSourceRange();
1266 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001267 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001268 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001269 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001270 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001272 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001273 // The condition must be evaluated, but if either the LHS or RHS is a
1274 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001275 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001276 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001277 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001278 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001279 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001280 }
1281
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001283 // If the base pointer or element is to a volatile pointer/field, accessing
1284 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001285 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001286 return false;
1287 Loc = cast<MemberExpr>(this)->getMemberLoc();
1288 R1 = SourceRange(Loc, Loc);
1289 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1290 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 case ArraySubscriptExprClass:
1293 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001294 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001295 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001296 return false;
1297 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1298 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1299 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1300 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001301
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001303 case CXXOperatorCallExprClass:
1304 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001305 // If this is a direct call, get the callee.
1306 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001307 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001308 // If the callee has attribute pure, const, or warn_unused_result, warn
1309 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001310 //
1311 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1312 // updated to match for QoI.
1313 if (FD->getAttr<WarnUnusedResultAttr>() ||
1314 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1315 Loc = CE->getCallee()->getLocStart();
1316 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001318 if (unsigned NumArgs = CE->getNumArgs())
1319 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1320 CE->getArg(NumArgs-1)->getLocEnd());
1321 return true;
1322 }
Chris Lattner026dc962009-02-14 07:37:35 +00001323 }
1324 return false;
1325 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001326
1327 case CXXTemporaryObjectExprClass:
1328 case CXXConstructExprClass:
1329 return false;
1330
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001331 case ObjCMessageExprClass: {
1332 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1333 const ObjCMethodDecl *MD = ME->getMethodDecl();
1334 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1335 Loc = getExprLoc();
1336 return true;
1337 }
Chris Lattner026dc962009-02-14 07:37:35 +00001338 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001339 }
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001341 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001342#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001343 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001344 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001345 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001346 Loc = Ref->getLocation();
1347 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1348 if (Ref->getBase())
1349 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001350#else
1351 Loc = getExprLoc();
1352 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001353#endif
1354 return true;
1355 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001356 case StmtExprClass: {
1357 // Statement exprs don't logically have side effects themselves, but are
1358 // sometimes used in macros in ways that give them a type that is unused.
1359 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1360 // however, if the result of the stmt expr is dead, we don't want to emit a
1361 // warning.
1362 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001363 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001364 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001365 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001366 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1367 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1368 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
John McCall0faede62010-03-12 07:11:26 +00001371 if (getType()->isVoidType())
1372 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001373 Loc = cast<StmtExpr>(this)->getLParenLoc();
1374 R1 = getSourceRange();
1375 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001376 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001377 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001378 // If this is an explicit cast to void, allow it. People do this when they
1379 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001380 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001381 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001382 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1383 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1384 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001385 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001386 if (getType()->isVoidType())
1387 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001388 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001389
Anders Carlsson58beed92009-11-17 17:11:23 +00001390 // If this is a cast to void or a constructor conversion, check the operand.
1391 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001392 if (CE->getCastKind() == CK_ToVoid ||
1393 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001394 return (cast<CastExpr>(this)->getSubExpr()
1395 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001396 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1397 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1398 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001399 }
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Eli Friedman4be1f472008-05-19 21:24:43 +00001401 case ImplicitCastExprClass:
1402 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001403 return (cast<ImplicitCastExpr>(this)
1404 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001405
Chris Lattner04421082008-04-08 04:40:51 +00001406 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001407 return (cast<CXXDefaultArgExpr>(this)
1408 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001409
1410 case CXXNewExprClass:
1411 // FIXME: In theory, there might be new expressions that don't have side
1412 // effects (e.g. a placement new with an uninitialized POD).
1413 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001414 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001415 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001416 return (cast<CXXBindTemporaryExpr>(this)
1417 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001418 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001419 return (cast<CXXExprWithTemporaries>(this)
1420 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001421 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001422}
1423
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001424/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001425/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001426bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001427 switch (getStmtClass()) {
1428 default:
1429 return false;
1430 case ObjCIvarRefExprClass:
1431 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001432 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001433 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001434 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001435 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001436 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001437 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001438 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001439 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001440 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001441 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001442 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1443 if (VD->hasGlobalStorage())
1444 return true;
1445 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001446 // dereferencing to a pointer is always a gc'able candidate,
1447 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001448 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001449 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001450 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001451 return false;
1452 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001453 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001454 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001455 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001456 }
1457 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001458 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001459 }
1460}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001461
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001462bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1463 if (isTypeDependent())
1464 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001465 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001466}
1467
Sebastian Redl369e51f2010-09-10 20:55:33 +00001468static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1469 Expr::CanThrowResult CT2) {
1470 // CanThrowResult constants are ordered so that the maximum is the correct
1471 // merge result.
1472 return CT1 > CT2 ? CT1 : CT2;
1473}
1474
1475static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1476 Expr *E = const_cast<Expr*>(CE);
1477 Expr::CanThrowResult R = Expr::CT_Cannot;
1478 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1479 I != IE && R != Expr::CT_Can; ++I) {
1480 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1481 }
1482 return R;
1483}
1484
1485static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1486 bool NullThrows = true) {
1487 if (!D)
1488 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1489
1490 // See if we can get a function type from the decl somehow.
1491 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1492 if (!VD) // If we have no clue what we're calling, assume the worst.
1493 return Expr::CT_Can;
1494
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001495 // As an extension, we assume that __attribute__((nothrow)) functions don't
1496 // throw.
1497 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1498 return Expr::CT_Cannot;
1499
Sebastian Redl369e51f2010-09-10 20:55:33 +00001500 QualType T = VD->getType();
1501 const FunctionProtoType *FT;
1502 if ((FT = T->getAs<FunctionProtoType>())) {
1503 } else if (const PointerType *PT = T->getAs<PointerType>())
1504 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1505 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1506 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1507 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1508 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1509 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1510 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1511
1512 if (!FT)
1513 return Expr::CT_Can;
1514
1515 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1516}
1517
1518static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1519 if (DC->isTypeDependent())
1520 return Expr::CT_Dependent;
1521
Sebastian Redl295995c2010-09-10 20:55:47 +00001522 if (!DC->getTypeAsWritten()->isReferenceType())
1523 return Expr::CT_Cannot;
1524
Sebastian Redl369e51f2010-09-10 20:55:33 +00001525 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1526}
1527
1528static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1529 const CXXTypeidExpr *DC) {
1530 if (DC->isTypeOperand())
1531 return Expr::CT_Cannot;
1532
1533 Expr *Op = DC->getExprOperand();
1534 if (Op->isTypeDependent())
1535 return Expr::CT_Dependent;
1536
1537 const RecordType *RT = Op->getType()->getAs<RecordType>();
1538 if (!RT)
1539 return Expr::CT_Cannot;
1540
1541 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1542 return Expr::CT_Cannot;
1543
1544 if (Op->Classify(C).isPRValue())
1545 return Expr::CT_Cannot;
1546
1547 return Expr::CT_Can;
1548}
1549
1550Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1551 // C++ [expr.unary.noexcept]p3:
1552 // [Can throw] if in a potentially-evaluated context the expression would
1553 // contain:
1554 switch (getStmtClass()) {
1555 case CXXThrowExprClass:
1556 // - a potentially evaluated throw-expression
1557 return CT_Can;
1558
1559 case CXXDynamicCastExprClass: {
1560 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1561 // where T is a reference type, that requires a run-time check
1562 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1563 if (CT == CT_Can)
1564 return CT;
1565 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1566 }
1567
1568 case CXXTypeidExprClass:
1569 // - a potentially evaluated typeid expression applied to a glvalue
1570 // expression whose type is a polymorphic class type
1571 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1572
1573 // - a potentially evaluated call to a function, member function, function
1574 // pointer, or member function pointer that does not have a non-throwing
1575 // exception-specification
1576 case CallExprClass:
1577 case CXXOperatorCallExprClass:
1578 case CXXMemberCallExprClass: {
1579 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1580 if (CT == CT_Can)
1581 return CT;
1582 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1583 }
1584
Sebastian Redl295995c2010-09-10 20:55:47 +00001585 case CXXConstructExprClass:
1586 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001587 CanThrowResult CT = CanCalleeThrow(
1588 cast<CXXConstructExpr>(this)->getConstructor());
1589 if (CT == CT_Can)
1590 return CT;
1591 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1592 }
1593
1594 case CXXNewExprClass: {
1595 CanThrowResult CT = MergeCanThrow(
1596 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1597 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1598 /*NullThrows*/false));
1599 if (CT == CT_Can)
1600 return CT;
1601 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1602 }
1603
1604 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001605 CanThrowResult CT = CanCalleeThrow(
1606 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1607 if (CT == CT_Can)
1608 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001609 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1610 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1611 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1612 Arg = Cast->getSubExpr();
1613 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1614 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1615 CanThrowResult CT2 = CanCalleeThrow(
1616 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1617 if (CT2 == CT_Can)
1618 return CT2;
1619 CT = MergeCanThrow(CT, CT2);
1620 }
1621 }
1622 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1623 }
1624
1625 case CXXBindTemporaryExprClass: {
1626 // The bound temporary has to be destroyed again, which might throw.
1627 CanThrowResult CT = CanCalleeThrow(
1628 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1629 if (CT == CT_Can)
1630 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001631 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1632 }
1633
1634 // ObjC message sends are like function calls, but never have exception
1635 // specs.
1636 case ObjCMessageExprClass:
1637 case ObjCPropertyRefExprClass:
1638 case ObjCImplicitSetterGetterRefExprClass:
1639 return CT_Can;
1640
1641 // Many other things have subexpressions, so we have to test those.
1642 // Some are simple:
1643 case ParenExprClass:
1644 case MemberExprClass:
1645 case CXXReinterpretCastExprClass:
1646 case CXXConstCastExprClass:
1647 case ConditionalOperatorClass:
1648 case CompoundLiteralExprClass:
1649 case ExtVectorElementExprClass:
1650 case InitListExprClass:
1651 case DesignatedInitExprClass:
1652 case ParenListExprClass:
1653 case VAArgExprClass:
1654 case CXXDefaultArgExprClass:
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001655 case CXXExprWithTemporariesClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001656 case ObjCIvarRefExprClass:
1657 case ObjCIsaExprClass:
1658 case ShuffleVectorExprClass:
1659 return CanSubExprsThrow(C, this);
1660
1661 // Some might be dependent for other reasons.
1662 case UnaryOperatorClass:
1663 case ArraySubscriptExprClass:
1664 case ImplicitCastExprClass:
1665 case CStyleCastExprClass:
1666 case CXXStaticCastExprClass:
1667 case CXXFunctionalCastExprClass:
1668 case BinaryOperatorClass:
1669 case CompoundAssignOperatorClass: {
1670 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1671 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1672 }
1673
1674 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1675 case StmtExprClass:
1676 return CT_Can;
1677
1678 case ChooseExprClass:
1679 if (isTypeDependent() || isValueDependent())
1680 return CT_Dependent;
1681 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1682
1683 // Some expressions are always dependent.
1684 case DependentScopeDeclRefExprClass:
1685 case CXXUnresolvedConstructExprClass:
1686 case CXXDependentScopeMemberExprClass:
1687 return CT_Dependent;
1688
1689 default:
1690 // All other expressions don't have subexpressions, or else they are
1691 // unevaluated.
1692 return CT_Cannot;
1693 }
1694}
1695
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001696Expr* Expr::IgnoreParens() {
1697 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001698 while (true) {
1699 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1700 E = P->getSubExpr();
1701 continue;
1702 }
1703 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1704 if (P->getOpcode() == UO_Extension) {
1705 E = P->getSubExpr();
1706 continue;
1707 }
1708 }
1709 return E;
1710 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001711}
1712
Chris Lattner56f34942008-02-13 01:02:39 +00001713/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1714/// or CastExprs or ImplicitCastExprs, returning their operand.
1715Expr *Expr::IgnoreParenCasts() {
1716 Expr *E = this;
1717 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001718 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001719 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001720 continue;
1721 }
1722 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001723 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001724 continue;
1725 }
1726 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1727 if (P->getOpcode() == UO_Extension) {
1728 E = P->getSubExpr();
1729 continue;
1730 }
1731 }
1732 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001733 }
1734}
1735
John McCall2fc46bf2010-05-05 22:59:52 +00001736Expr *Expr::IgnoreParenImpCasts() {
1737 Expr *E = this;
1738 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001739 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001740 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001741 continue;
1742 }
1743 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001744 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001745 continue;
1746 }
1747 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1748 if (P->getOpcode() == UO_Extension) {
1749 E = P->getSubExpr();
1750 continue;
1751 }
1752 }
1753 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001754 }
1755}
1756
Chris Lattnerecdd8412009-03-13 17:28:01 +00001757/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1758/// value (including ptr->int casts of the same size). Strip off any
1759/// ParenExpr or CastExprs, returning their operand.
1760Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1761 Expr *E = this;
1762 while (true) {
1763 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1764 E = P->getSubExpr();
1765 continue;
1766 }
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Chris Lattnerecdd8412009-03-13 17:28:01 +00001768 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1769 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001770 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001771 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Chris Lattnerecdd8412009-03-13 17:28:01 +00001773 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1774 E = SE;
1775 continue;
1776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001778 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001779 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001780 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001781 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001782 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1783 E = SE;
1784 continue;
1785 }
1786 }
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001788 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1789 if (P->getOpcode() == UO_Extension) {
1790 E = P->getSubExpr();
1791 continue;
1792 }
1793 }
1794
Chris Lattnerecdd8412009-03-13 17:28:01 +00001795 return E;
1796 }
1797}
1798
Douglas Gregor6eef5192009-12-14 19:27:10 +00001799bool Expr::isDefaultArgument() const {
1800 const Expr *E = this;
1801 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1802 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001803
Douglas Gregor6eef5192009-12-14 19:27:10 +00001804 return isa<CXXDefaultArgExpr>(E);
1805}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001806
Douglas Gregor2f599792010-04-02 18:24:57 +00001807/// \brief Skip over any no-op casts and any temporary-binding
1808/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001809static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001810 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001811 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001812 E = ICE->getSubExpr();
1813 else
1814 break;
1815 }
1816
1817 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1818 E = BE->getSubExpr();
1819
1820 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001821 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001822 E = ICE->getSubExpr();
1823 else
1824 break;
1825 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001826
1827 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001828}
1829
John McCall558d2ab2010-09-15 10:14:12 +00001830/// isTemporaryObject - Determines if this expression produces a
1831/// temporary of the given class type.
1832bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1833 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1834 return false;
1835
Anders Carlssonf8b30152010-11-28 16:40:49 +00001836 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001837
John McCall58277b52010-09-15 20:59:13 +00001838 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001839 if (!E->Classify(C).isPRValue()) {
1840 // In this context, property reference is a message call and is pr-value.
1841 if (!isa<ObjCPropertyRefExpr>(E) &&
1842 !isa<ObjCImplicitSetterGetterRefExpr>(E))
1843 return false;
1844 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001845
John McCall19e60ad2010-09-16 06:57:56 +00001846 // Black-list a few cases which yield pr-values of class type that don't
1847 // refer to temporaries of that type:
1848
1849 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001850 if (isa<ImplicitCastExpr>(E)) {
1851 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1852 case CK_DerivedToBase:
1853 case CK_UncheckedDerivedToBase:
1854 return false;
1855 default:
1856 break;
1857 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001858 }
1859
John McCall19e60ad2010-09-16 06:57:56 +00001860 // - member expressions (all)
1861 if (isa<MemberExpr>(E))
1862 return false;
1863
John McCall558d2ab2010-09-15 10:14:12 +00001864 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001865}
1866
Douglas Gregor898574e2008-12-05 23:32:09 +00001867/// hasAnyTypeDependentArguments - Determines if any of the expressions
1868/// in Exprs is type-dependent.
1869bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1870 for (unsigned I = 0; I < NumExprs; ++I)
1871 if (Exprs[I]->isTypeDependent())
1872 return true;
1873
1874 return false;
1875}
1876
1877/// hasAnyValueDependentArguments - Determines if any of the expressions
1878/// in Exprs is value-dependent.
1879bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1880 for (unsigned I = 0; I < NumExprs; ++I)
1881 if (Exprs[I]->isValueDependent())
1882 return true;
1883
1884 return false;
1885}
1886
John McCall4204f072010-08-02 21:13:48 +00001887bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001888 // This function is attempting whether an expression is an initializer
1889 // which can be evaluated at compile-time. isEvaluatable handles most
1890 // of the cases, but it can't deal with some initializer-specific
1891 // expressions, and it can't deal with aggregates; we deal with those here,
1892 // and fall back to isEvaluatable for the other cases.
1893
John McCall4204f072010-08-02 21:13:48 +00001894 // If we ever capture reference-binding directly in the AST, we can
1895 // kill the second parameter.
1896
1897 if (IsForRef) {
1898 EvalResult Result;
1899 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1900 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001901
Anders Carlssone8a32b82008-11-24 05:23:59 +00001902 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001903 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001904 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001905 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001906 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001907 return true;
John McCallb4b9b152010-08-01 21:51:45 +00001908 case CXXTemporaryObjectExprClass:
1909 case CXXConstructExprClass: {
1910 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00001911
1912 // Only if it's
1913 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00001914 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00001915 if (!CE->getNumArgs()) return true;
1916
1917 // 2) an elidable trivial copy construction of an operand which is
1918 // itself a constant initializer. Note that we consider the
1919 // operand on its own, *not* as a reference binding.
1920 return CE->isElidable() &&
1921 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00001922 }
Nate Begeman59b5da62009-01-18 03:20:47 +00001923 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001924 // This handles gcc's extension that allows global initializers like
1925 // "struct x {int x;} x = (struct x) {};".
1926 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001927 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00001928 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00001929 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001930 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001931 // FIXME: This doesn't deal with fields with reference types correctly.
1932 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1933 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001934 const InitListExpr *Exp = cast<InitListExpr>(this);
1935 unsigned numInits = Exp->getNumInits();
1936 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00001937 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001938 return false;
1939 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001940 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001941 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001942 case ImplicitValueInitExprClass:
1943 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001944 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00001945 return cast<ParenExpr>(this)->getSubExpr()
1946 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00001947 case ChooseExprClass:
1948 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
1949 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001950 case UnaryOperatorClass: {
1951 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001952 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00001953 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001954 break;
1955 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001956 case BinaryOperatorClass: {
1957 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1958 // but this handles the common case.
1959 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001960 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00001961 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1962 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1963 return true;
1964 break;
1965 }
John McCall4204f072010-08-02 21:13:48 +00001966 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00001967 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00001968 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001969 case CStyleCastExprClass:
1970 // Handle casts with a destination that's a struct or union; this
1971 // deals with both the gcc no-op struct cast extension and the
1972 // cast-to-union extension.
1973 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00001974 return cast<CastExpr>(this)->getSubExpr()
1975 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001976
Chris Lattner430656e2009-10-13 22:12:09 +00001977 // Integer->integer casts can be handled here, which is important for
1978 // things like (int)(&&x-&&y). Scary but true.
1979 if (getType()->isIntegerType() &&
1980 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00001981 return cast<CastExpr>(this)->getSubExpr()
1982 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001983
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001984 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001985 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001986 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001987}
1988
Reid Spencer5f016e22007-07-11 17:01:13 +00001989/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1990/// integer constant expression with the value zero, or if this is one that is
1991/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001992bool Expr::isNullPointerConstant(ASTContext &Ctx,
1993 NullPointerConstantValueDependence NPC) const {
1994 if (isValueDependent()) {
1995 switch (NPC) {
1996 case NPC_NeverValueDependent:
1997 assert(false && "Unexpected value dependent expression!");
1998 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00001999
Douglas Gregorce940492009-09-25 04:25:58 +00002000 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002001 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002002
Douglas Gregorce940492009-09-25 04:25:58 +00002003 case NPC_ValueDependentIsNotNull:
2004 return false;
2005 }
2006 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002007
Sebastian Redl07779722008-10-31 14:43:28 +00002008 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002009 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002010 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002011 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002012 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002013 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002014 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002015 Pointee->isVoidType() && // to void*
2016 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002017 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002018 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002020 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2021 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002022 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002023 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2024 // Accept ((void*)0) as a null pointer constant, as many other
2025 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002026 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002027 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002028 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002029 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002030 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002031 } else if (isa<GNUNullExpr>(this)) {
2032 // The GNU __null extension is always a null pointer constant.
2033 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002034 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002035
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002036 // C++0x nullptr_t is always a null pointer constant.
2037 if (getType()->isNullPtrType())
2038 return true;
2039
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002040 if (const RecordType *UT = getType()->getAsUnionType())
2041 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2042 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2043 const Expr *InitExpr = CLE->getInitializer();
2044 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2045 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2046 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002047 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002048 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002049 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002050 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 // If we have an integer constant expression, we need to *evaluate* it and
2053 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002054 llvm::APSInt Result;
2055 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002056}
Steve Naroff31a45842007-07-28 23:10:27 +00002057
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002058FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002059 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002060
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002061 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002062 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002063 ICE->getCastKind() == CK_NoOp)
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002064 E = ICE->getSubExpr()->IgnoreParens();
2065 else
2066 break;
2067 }
2068
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002069 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002070 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002071 if (Field->isBitField())
2072 return Field;
2073
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002074 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2075 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2076 if (Field->isBitField())
2077 return Field;
2078
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002079 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2080 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2081 return BinOp->getLHS()->getBitField();
2082
2083 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002084}
2085
Anders Carlsson09380262010-01-31 17:18:49 +00002086bool Expr::refersToVectorElement() const {
2087 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002088
Anders Carlsson09380262010-01-31 17:18:49 +00002089 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002090 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002091 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002092 E = ICE->getSubExpr()->IgnoreParens();
2093 else
2094 break;
2095 }
Sean Huntc3021132010-05-05 15:23:54 +00002096
Anders Carlsson09380262010-01-31 17:18:49 +00002097 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2098 return ASE->getBase()->getType()->isVectorType();
2099
2100 if (isa<ExtVectorElementExpr>(E))
2101 return true;
2102
2103 return false;
2104}
2105
Chris Lattner2140e902009-02-16 22:14:05 +00002106/// isArrow - Return true if the base expression is a pointer to vector,
2107/// return false if the base expression is a vector.
2108bool ExtVectorElementExpr::isArrow() const {
2109 return getBase()->getType()->isPointerType();
2110}
2111
Nate Begeman213541a2008-04-18 23:10:10 +00002112unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002113 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002114 return VT->getNumElements();
2115 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002116}
2117
Nate Begeman8a997642008-05-09 06:41:27 +00002118/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002119bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002120 // FIXME: Refactor this code to an accessor on the AST node which returns the
2121 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002122 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002123
2124 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002125 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002126 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Nate Begeman190d6a22009-01-18 02:01:21 +00002128 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002129 if (Comp[0] == 's' || Comp[0] == 'S')
2130 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Daniel Dunbar15027422009-10-17 23:53:04 +00002132 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2133 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002134 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002135
Steve Narofffec0b492007-07-30 03:29:09 +00002136 return false;
2137}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002138
Nate Begeman8a997642008-05-09 06:41:27 +00002139/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002140void ExtVectorElementExpr::getEncodedElementAccess(
2141 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002142 llvm::StringRef Comp = Accessor->getName();
2143 if (Comp[0] == 's' || Comp[0] == 'S')
2144 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002146 bool isHi = Comp == "hi";
2147 bool isLo = Comp == "lo";
2148 bool isEven = Comp == "even";
2149 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002150
Nate Begeman8a997642008-05-09 06:41:27 +00002151 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2152 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002153
Nate Begeman8a997642008-05-09 06:41:27 +00002154 if (isHi)
2155 Index = e + i;
2156 else if (isLo)
2157 Index = i;
2158 else if (isEven)
2159 Index = 2 * i;
2160 else if (isOdd)
2161 Index = 2 * i + 1;
2162 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002163 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002164
Nate Begeman3b8d1162008-05-13 21:03:02 +00002165 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002166 }
Nate Begeman8a997642008-05-09 06:41:27 +00002167}
2168
Douglas Gregor04badcf2010-04-21 00:45:42 +00002169ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002170 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002171 SourceLocation LBracLoc,
2172 SourceLocation SuperLoc,
2173 bool IsInstanceSuper,
2174 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002175 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002176 ObjCMethodDecl *Method,
2177 Expr **Args, unsigned NumArgs,
2178 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002179 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2180 /*TypeDependent=*/false, /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002181 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2182 HasMethod(Method != 0), SuperLoc(SuperLoc),
2183 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2184 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002185 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002186{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002187 setReceiverPointer(SuperType.getAsOpaquePtr());
2188 if (NumArgs)
2189 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002190}
2191
Douglas Gregor04badcf2010-04-21 00:45:42 +00002192ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002193 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002194 SourceLocation LBracLoc,
2195 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002196 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002197 ObjCMethodDecl *Method,
2198 Expr **Args, unsigned NumArgs,
2199 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002200 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00002201 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002202 hasAnyValueDependentArguments(Args, NumArgs))),
2203 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2204 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2205 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002206 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002207{
2208 setReceiverPointer(Receiver);
2209 if (NumArgs)
2210 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002211}
2212
Douglas Gregor04badcf2010-04-21 00:45:42 +00002213ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002214 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002215 SourceLocation LBracLoc,
2216 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002217 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002218 ObjCMethodDecl *Method,
2219 Expr **Args, unsigned NumArgs,
2220 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002221 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00002222 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002223 hasAnyValueDependentArguments(Args, NumArgs))),
2224 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2225 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2226 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002227 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002228{
2229 setReceiverPointer(Receiver);
2230 if (NumArgs)
2231 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002232}
2233
Douglas Gregor04badcf2010-04-21 00:45:42 +00002234ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002235 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002236 SourceLocation LBracLoc,
2237 SourceLocation SuperLoc,
2238 bool IsInstanceSuper,
2239 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002240 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002241 ObjCMethodDecl *Method,
2242 Expr **Args, unsigned NumArgs,
2243 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002244 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002245 NumArgs * sizeof(Expr *);
2246 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002247 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00002248 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002249 RBracLoc);
2250}
2251
2252ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002253 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002254 SourceLocation LBracLoc,
2255 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002256 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002257 ObjCMethodDecl *Method,
2258 Expr **Args, unsigned NumArgs,
2259 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002260 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002261 NumArgs * sizeof(Expr *);
2262 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002263 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002264 NumArgs, RBracLoc);
2265}
2266
2267ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002268 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002269 SourceLocation LBracLoc,
2270 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002271 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002272 ObjCMethodDecl *Method,
2273 Expr **Args, unsigned NumArgs,
2274 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002275 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002276 NumArgs * sizeof(Expr *);
2277 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002278 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002279 NumArgs, RBracLoc);
2280}
2281
Sean Huntc3021132010-05-05 15:23:54 +00002282ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002283 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002284 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002285 NumArgs * sizeof(Expr *);
2286 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2287 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2288}
Sean Huntc3021132010-05-05 15:23:54 +00002289
Douglas Gregor04badcf2010-04-21 00:45:42 +00002290Selector ObjCMessageExpr::getSelector() const {
2291 if (HasMethod)
2292 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2293 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002294 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002295}
2296
2297ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2298 switch (getReceiverKind()) {
2299 case Instance:
2300 if (const ObjCObjectPointerType *Ptr
2301 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2302 return Ptr->getInterfaceDecl();
2303 break;
2304
2305 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002306 if (const ObjCObjectType *Ty
2307 = getClassReceiver()->getAs<ObjCObjectType>())
2308 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002309 break;
2310
2311 case SuperInstance:
2312 if (const ObjCObjectPointerType *Ptr
2313 = getSuperType()->getAs<ObjCObjectPointerType>())
2314 return Ptr->getInterfaceDecl();
2315 break;
2316
2317 case SuperClass:
2318 if (const ObjCObjectPointerType *Iface
2319 = getSuperType()->getAs<ObjCObjectPointerType>())
2320 return Iface->getInterfaceDecl();
2321 break;
2322 }
2323
2324 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002325}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002326
Chris Lattner27437ca2007-10-25 00:29:32 +00002327bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002328 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002329}
2330
Nate Begeman888376a2009-08-12 02:28:50 +00002331void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2332 unsigned NumExprs) {
2333 if (SubExprs) C.Deallocate(SubExprs);
2334
2335 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002336 this->NumExprs = NumExprs;
2337 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002338}
Nate Begeman888376a2009-08-12 02:28:50 +00002339
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002340//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002341// DesignatedInitExpr
2342//===----------------------------------------------------------------------===//
2343
2344IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2345 assert(Kind == FieldDesignator && "Only valid on a field designator");
2346 if (Field.NameOrField & 0x01)
2347 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2348 else
2349 return getField()->getIdentifier();
2350}
2351
Sean Huntc3021132010-05-05 15:23:54 +00002352DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002353 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002354 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002355 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002356 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002357 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002358 unsigned NumIndexExprs,
2359 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002360 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002361 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregor9ea62762009-05-21 23:17:49 +00002362 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002363 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2364 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002365 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002366
2367 // Record the initializer itself.
2368 child_iterator Child = child_begin();
2369 *Child++ = Init;
2370
2371 // Copy the designators and their subexpressions, computing
2372 // value-dependence along the way.
2373 unsigned IndexIdx = 0;
2374 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002375 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002376
2377 if (this->Designators[I].isArrayDesignator()) {
2378 // Compute type- and value-dependence.
2379 Expr *Index = IndexExprs[IndexIdx];
John McCall8e6285a2010-10-26 08:39:16 +00002380 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002381 Index->isTypeDependent() || Index->isValueDependent();
2382
2383 // Copy the index expressions into permanent storage.
2384 *Child++ = IndexExprs[IndexIdx++];
2385 } else if (this->Designators[I].isArrayRangeDesignator()) {
2386 // Compute type- and value-dependence.
2387 Expr *Start = IndexExprs[IndexIdx];
2388 Expr *End = IndexExprs[IndexIdx + 1];
John McCall8e6285a2010-10-26 08:39:16 +00002389 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002390 Start->isTypeDependent() || Start->isValueDependent() ||
2391 End->isTypeDependent() || End->isValueDependent();
2392
2393 // Copy the start/end expressions into permanent storage.
2394 *Child++ = IndexExprs[IndexIdx++];
2395 *Child++ = IndexExprs[IndexIdx++];
2396 }
2397 }
2398
2399 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002400}
2401
Douglas Gregor05c13a32009-01-22 00:58:24 +00002402DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002403DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002404 unsigned NumDesignators,
2405 Expr **IndexExprs, unsigned NumIndexExprs,
2406 SourceLocation ColonOrEqualLoc,
2407 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002408 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002409 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002410 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002411 ColonOrEqualLoc, UsesColonSyntax,
2412 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002413}
2414
Mike Stump1eb44332009-09-09 15:08:12 +00002415DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002416 unsigned NumIndexExprs) {
2417 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2418 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2419 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2420}
2421
Douglas Gregor319d57f2010-01-06 23:17:19 +00002422void DesignatedInitExpr::setDesignators(ASTContext &C,
2423 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002424 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002425 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002426 NumDesignators = NumDesigs;
2427 for (unsigned I = 0; I != NumDesigs; ++I)
2428 Designators[I] = Desigs[I];
2429}
2430
Douglas Gregor05c13a32009-01-22 00:58:24 +00002431SourceRange DesignatedInitExpr::getSourceRange() const {
2432 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002433 Designator &First =
2434 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002435 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002436 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002437 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2438 else
2439 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2440 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002441 StartLoc =
2442 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002443 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2444}
2445
Douglas Gregor05c13a32009-01-22 00:58:24 +00002446Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2447 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2448 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2449 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002450 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2451 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2452}
2453
2454Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002455 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002456 "Requires array range designator");
2457 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2458 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002459 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2460 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2461}
2462
2463Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002464 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002465 "Requires array range designator");
2466 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2467 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002468 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2469 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2470}
2471
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002472/// \brief Replaces the designator at index @p Idx with the series
2473/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002474void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002475 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002476 const Designator *Last) {
2477 unsigned NumNewDesignators = Last - First;
2478 if (NumNewDesignators == 0) {
2479 std::copy_backward(Designators + Idx + 1,
2480 Designators + NumDesignators,
2481 Designators + Idx);
2482 --NumNewDesignators;
2483 return;
2484 } else if (NumNewDesignators == 1) {
2485 Designators[Idx] = *First;
2486 return;
2487 }
2488
Mike Stump1eb44332009-09-09 15:08:12 +00002489 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002490 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002491 std::copy(Designators, Designators + Idx, NewDesignators);
2492 std::copy(First, Last, NewDesignators + Idx);
2493 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2494 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002495 Designators = NewDesignators;
2496 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2497}
2498
Mike Stump1eb44332009-09-09 15:08:12 +00002499ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002500 Expr **exprs, unsigned nexprs,
2501 SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00002502: Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002503 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002504 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002505 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002506
Nate Begeman2ef13e52009-08-10 23:49:36 +00002507 Exprs = new (C) Stmt*[nexprs];
2508 for (unsigned i = 0; i != nexprs; ++i)
2509 Exprs[i] = exprs[i];
2510}
2511
Douglas Gregor05c13a32009-01-22 00:58:24 +00002512//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002513// ExprIterator.
2514//===----------------------------------------------------------------------===//
2515
2516Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2517Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2518Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2519const Expr* ConstExprIterator::operator[](size_t idx) const {
2520 return cast<Expr>(I[idx]);
2521}
2522const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2523const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2524
2525//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002526// Child Iterators for iterating over subexpressions/substatements
2527//===----------------------------------------------------------------------===//
2528
2529// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002530Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2531Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002532
Steve Naroff7779db42007-11-12 14:29:37 +00002533// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002534Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2535Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002536
Steve Naroffe3e9add2008-06-02 23:03:37 +00002537// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002538Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2539{
2540 if (BaseExprOrSuperType.is<Stmt*>()) {
2541 // Hack alert!
2542 return reinterpret_cast<Stmt**> (&BaseExprOrSuperType);
2543 }
2544 return child_iterator();
2545}
2546
2547Stmt::child_iterator ObjCPropertyRefExpr::child_end()
2548{ return BaseExprOrSuperType.is<Stmt*>() ?
2549 reinterpret_cast<Stmt**> (&BaseExprOrSuperType)+1 :
2550 child_iterator();
2551}
Steve Naroffae784072008-05-30 00:40:33 +00002552
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002553// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002554Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002555 // If this is accessing a class member or super, skip that entry.
2556 // Technically, 2nd condition is sufficient. But I want to be verbose
2557 if (isSuperReceiver() || !Base)
2558 return child_iterator();
2559 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002560}
Mike Stump1eb44332009-09-09 15:08:12 +00002561Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002562 if (isSuperReceiver() || !Base)
2563 return child_iterator();
Mike Stump1eb44332009-09-09 15:08:12 +00002564 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002565}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002566
Steve Narofff242b1b2009-07-24 17:54:45 +00002567// ObjCIsaExpr
2568Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2569Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2570
Chris Lattnerd9f69102008-08-10 01:53:14 +00002571// PredefinedExpr
2572Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2573Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002574
2575// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002576Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2577Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002578
2579// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002580Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002581Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002582
2583// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002584Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2585Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002586
Chris Lattner5d661452007-08-26 03:42:43 +00002587// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002588Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2589Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002590
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002591// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002592Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2593Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002594
2595// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002596Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2597Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002598
2599// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002600Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2601Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002602
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002603// OffsetOfExpr
2604Stmt::child_iterator OffsetOfExpr::child_begin() {
2605 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2606 + NumComps);
2607}
2608Stmt::child_iterator OffsetOfExpr::child_end() {
2609 return child_iterator(&*child_begin() + NumExprs);
2610}
2611
Sebastian Redl05189992008-11-11 17:56:53 +00002612// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002613Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002614 // If this is of a type and the type is a VLA type (and not a typedef), the
2615 // size expression of the VLA needs to be treated as an executable expression.
2616 // Why isn't this weirdness documented better in StmtIterator?
2617 if (isArgumentType()) {
2618 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2619 getArgumentType().getTypePtr()))
2620 return child_iterator(T);
2621 return child_iterator();
2622 }
Sebastian Redld4575892008-12-03 23:17:54 +00002623 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002624}
Sebastian Redl05189992008-11-11 17:56:53 +00002625Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2626 if (isArgumentType())
2627 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002628 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002629}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002630
2631// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002632Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002633 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002634}
Ted Kremenek1237c672007-08-24 20:06:47 +00002635Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002636 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002637}
2638
2639// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002640Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002641 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002642}
Ted Kremenek1237c672007-08-24 20:06:47 +00002643Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002644 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002645}
Ted Kremenek1237c672007-08-24 20:06:47 +00002646
2647// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002648Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2649Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002650
Nate Begeman213541a2008-04-18 23:10:10 +00002651// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002652Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2653Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002654
2655// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002656Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2657Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002658
Ted Kremenek1237c672007-08-24 20:06:47 +00002659// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002660Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2661Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002662
2663// BinaryOperator
2664Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002665 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002666}
Ted Kremenek1237c672007-08-24 20:06:47 +00002667Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002668 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002669}
2670
2671// ConditionalOperator
2672Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002673 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002674}
Ted Kremenek1237c672007-08-24 20:06:47 +00002675Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002676 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002677}
2678
2679// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002680Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2681Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002682
Ted Kremenek1237c672007-08-24 20:06:47 +00002683// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002684Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2685Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002686
2687// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002688Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2689 return child_iterator();
2690}
2691
2692Stmt::child_iterator TypesCompatibleExpr::child_end() {
2693 return child_iterator();
2694}
Ted Kremenek1237c672007-08-24 20:06:47 +00002695
2696// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002697Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2698Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002699
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002700// GNUNullExpr
2701Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2702Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2703
Eli Friedmand38617c2008-05-14 19:38:39 +00002704// ShuffleVectorExpr
2705Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002706 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002707}
2708Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002709 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002710}
2711
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002712// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002713Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2714Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002715
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002716// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002717Stmt::child_iterator InitListExpr::child_begin() {
2718 return InitExprs.size() ? &InitExprs[0] : 0;
2719}
2720Stmt::child_iterator InitListExpr::child_end() {
2721 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2722}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002723
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002724// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002725Stmt::child_iterator DesignatedInitExpr::child_begin() {
2726 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2727 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002728 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2729}
2730Stmt::child_iterator DesignatedInitExpr::child_end() {
2731 return child_iterator(&*child_begin() + NumSubExprs);
2732}
2733
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002734// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002735Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2736 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002737}
2738
Mike Stump1eb44332009-09-09 15:08:12 +00002739Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2740 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002741}
2742
Nate Begeman2ef13e52009-08-10 23:49:36 +00002743// ParenListExpr
2744Stmt::child_iterator ParenListExpr::child_begin() {
2745 return &Exprs[0];
2746}
2747Stmt::child_iterator ParenListExpr::child_end() {
2748 return &Exprs[0]+NumExprs;
2749}
2750
Ted Kremenek1237c672007-08-24 20:06:47 +00002751// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002752Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002753 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002754}
2755Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002756 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002757}
Ted Kremenek1237c672007-08-24 20:06:47 +00002758
2759// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002760Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2761Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002762
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002763// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002764Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002765 return child_iterator();
2766}
2767Stmt::child_iterator ObjCSelectorExpr::child_end() {
2768 return child_iterator();
2769}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002770
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002771// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002772Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2773 return child_iterator();
2774}
2775Stmt::child_iterator ObjCProtocolExpr::child_end() {
2776 return child_iterator();
2777}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002778
Steve Naroff563477d2007-09-18 23:55:05 +00002779// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002780Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002781 if (getReceiverKind() == Instance)
2782 return reinterpret_cast<Stmt **>(this + 1);
2783 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002784}
2785Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002786 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002787}
2788
Steve Naroff4eb206b2008-09-03 18:15:37 +00002789// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002790Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2791Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002792
Ted Kremenek9da13f92008-09-26 23:24:14 +00002793Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2794Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002795
2796// OpaqueValueExpr
2797SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2798Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2799Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2800