blob: eef45e388a9bcf395a807b9f4c195ee5e2fe889c [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 McCallf6a16482010-12-04 03:47:34 +0000827 case CK_GetObjCProperty:
828 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000829 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000830 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000831 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000832 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000833 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000834 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000835 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000836 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000837 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000838 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000839 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000840 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000841 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000842 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000843 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000844 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000845 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000846 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000847 case CK_NullToPointer:
848 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000849 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000850 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000851 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000852 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000853 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000854 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000855 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000856 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000857 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000858 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000859 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000860 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000861 case CK_PointerToBoolean:
862 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000863 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000864 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000865 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000866 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000867 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000868 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000869 case CK_IntegralToBoolean:
870 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000871 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000872 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000873 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000874 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000875 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000876 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000877 case CK_FloatingToBoolean:
878 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000879 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000880 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000881 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000882 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000883 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000884 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000885 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000886 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000887 case CK_FloatingRealToComplex:
888 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000889 case CK_FloatingComplexToReal:
890 return "FloatingComplexToReal";
891 case CK_FloatingComplexToBoolean:
892 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000893 case CK_FloatingComplexCast:
894 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000895 case CK_FloatingComplexToIntegralComplex:
896 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000897 case CK_IntegralRealToComplex:
898 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000899 case CK_IntegralComplexToReal:
900 return "IntegralComplexToReal";
901 case CK_IntegralComplexToBoolean:
902 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000903 case CK_IntegralComplexCast:
904 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000905 case CK_IntegralComplexToFloatingComplex:
906 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
John McCall2bb5d002010-11-13 09:02:35 +0000909 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000910 return 0;
911}
912
Douglas Gregor6eef5192009-12-14 19:27:10 +0000913Expr *CastExpr::getSubExprAsWritten() {
914 Expr *SubExpr = 0;
915 CastExpr *E = this;
916 do {
917 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000918
Douglas Gregor6eef5192009-12-14 19:27:10 +0000919 // Skip any temporary bindings; they're implicit.
920 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
921 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000922
Douglas Gregor6eef5192009-12-14 19:27:10 +0000923 // Conversions by constructor and conversion functions have a
924 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +0000925 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000926 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +0000927 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000928 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000929
Douglas Gregor6eef5192009-12-14 19:27:10 +0000930 // If the subexpression we're left with is an implicit cast, look
931 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000932 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
933
Douglas Gregor6eef5192009-12-14 19:27:10 +0000934 return SubExpr;
935}
936
John McCallf871d0c2010-08-07 06:22:56 +0000937CXXBaseSpecifier **CastExpr::path_buffer() {
938 switch (getStmtClass()) {
939#define ABSTRACT_STMT(x)
940#define CASTEXPR(Type, Base) \
941 case Stmt::Type##Class: \
942 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
943#define STMT(Type, Base)
944#include "clang/AST/StmtNodes.inc"
945 default:
946 llvm_unreachable("non-cast expressions not possible here");
947 return 0;
948 }
949}
950
951void CastExpr::setCastPath(const CXXCastPath &Path) {
952 assert(Path.size() == path_size());
953 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
954}
955
956ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
957 CastKind Kind, Expr *Operand,
958 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +0000959 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +0000960 unsigned PathSize = (BasePath ? BasePath->size() : 0);
961 void *Buffer =
962 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
963 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +0000964 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +0000965 if (PathSize) E->setCastPath(*BasePath);
966 return E;
967}
968
969ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
970 unsigned PathSize) {
971 void *Buffer =
972 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
973 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
974}
975
976
977CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000978 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000979 const CXXCastPath *BasePath,
980 TypeSourceInfo *WrittenTy,
981 SourceLocation L, SourceLocation R) {
982 unsigned PathSize = (BasePath ? BasePath->size() : 0);
983 void *Buffer =
984 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
985 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000986 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +0000987 if (PathSize) E->setCastPath(*BasePath);
988 return E;
989}
990
991CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
992 void *Buffer =
993 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
994 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
995}
996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
998/// corresponds to, e.g. "<<=".
999const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1000 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001001 case BO_PtrMemD: return ".*";
1002 case BO_PtrMemI: return "->*";
1003 case BO_Mul: return "*";
1004 case BO_Div: return "/";
1005 case BO_Rem: return "%";
1006 case BO_Add: return "+";
1007 case BO_Sub: return "-";
1008 case BO_Shl: return "<<";
1009 case BO_Shr: return ">>";
1010 case BO_LT: return "<";
1011 case BO_GT: return ">";
1012 case BO_LE: return "<=";
1013 case BO_GE: return ">=";
1014 case BO_EQ: return "==";
1015 case BO_NE: return "!=";
1016 case BO_And: return "&";
1017 case BO_Xor: return "^";
1018 case BO_Or: return "|";
1019 case BO_LAnd: return "&&";
1020 case BO_LOr: return "||";
1021 case BO_Assign: return "=";
1022 case BO_MulAssign: return "*=";
1023 case BO_DivAssign: return "/=";
1024 case BO_RemAssign: return "%=";
1025 case BO_AddAssign: return "+=";
1026 case BO_SubAssign: return "-=";
1027 case BO_ShlAssign: return "<<=";
1028 case BO_ShrAssign: return ">>=";
1029 case BO_AndAssign: return "&=";
1030 case BO_XorAssign: return "^=";
1031 case BO_OrAssign: return "|=";
1032 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001034
1035 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001036}
1037
John McCall2de56d12010-08-25 11:45:40 +00001038BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001039BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1040 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001041 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001042 case OO_Plus: return BO_Add;
1043 case OO_Minus: return BO_Sub;
1044 case OO_Star: return BO_Mul;
1045 case OO_Slash: return BO_Div;
1046 case OO_Percent: return BO_Rem;
1047 case OO_Caret: return BO_Xor;
1048 case OO_Amp: return BO_And;
1049 case OO_Pipe: return BO_Or;
1050 case OO_Equal: return BO_Assign;
1051 case OO_Less: return BO_LT;
1052 case OO_Greater: return BO_GT;
1053 case OO_PlusEqual: return BO_AddAssign;
1054 case OO_MinusEqual: return BO_SubAssign;
1055 case OO_StarEqual: return BO_MulAssign;
1056 case OO_SlashEqual: return BO_DivAssign;
1057 case OO_PercentEqual: return BO_RemAssign;
1058 case OO_CaretEqual: return BO_XorAssign;
1059 case OO_AmpEqual: return BO_AndAssign;
1060 case OO_PipeEqual: return BO_OrAssign;
1061 case OO_LessLess: return BO_Shl;
1062 case OO_GreaterGreater: return BO_Shr;
1063 case OO_LessLessEqual: return BO_ShlAssign;
1064 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1065 case OO_EqualEqual: return BO_EQ;
1066 case OO_ExclaimEqual: return BO_NE;
1067 case OO_LessEqual: return BO_LE;
1068 case OO_GreaterEqual: return BO_GE;
1069 case OO_AmpAmp: return BO_LAnd;
1070 case OO_PipePipe: return BO_LOr;
1071 case OO_Comma: return BO_Comma;
1072 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001073 }
1074}
1075
1076OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1077 static const OverloadedOperatorKind OverOps[] = {
1078 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1079 OO_Star, OO_Slash, OO_Percent,
1080 OO_Plus, OO_Minus,
1081 OO_LessLess, OO_GreaterGreater,
1082 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1083 OO_EqualEqual, OO_ExclaimEqual,
1084 OO_Amp,
1085 OO_Caret,
1086 OO_Pipe,
1087 OO_AmpAmp,
1088 OO_PipePipe,
1089 OO_Equal, OO_StarEqual,
1090 OO_SlashEqual, OO_PercentEqual,
1091 OO_PlusEqual, OO_MinusEqual,
1092 OO_LessLessEqual, OO_GreaterGreaterEqual,
1093 OO_AmpEqual, OO_CaretEqual,
1094 OO_PipeEqual,
1095 OO_Comma
1096 };
1097 return OverOps[Opc];
1098}
1099
Ted Kremenek709210f2010-04-13 23:39:13 +00001100InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001101 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001102 SourceLocation rbraceloc)
John McCallf89e55a2010-11-18 06:31:45 +00001103 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001104 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001105 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001106 UnionFieldInit(0), HadArrayRangeDesignator(false)
1107{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001108 for (unsigned I = 0; I != numInits; ++I) {
1109 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001110 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001111 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001112 ExprBits.ValueDependent = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001113 }
Sean Huntc3021132010-05-05 15:23:54 +00001114
Ted Kremenek709210f2010-04-13 23:39:13 +00001115 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001116}
Reid Spencer5f016e22007-07-11 17:01:13 +00001117
Ted Kremenek709210f2010-04-13 23:39:13 +00001118void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001119 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001120 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001121}
1122
Ted Kremenek709210f2010-04-13 23:39:13 +00001123void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001124 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001125}
1126
Ted Kremenek709210f2010-04-13 23:39:13 +00001127Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001128 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001129 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001130 InitExprs.back() = expr;
1131 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Douglas Gregor4c678342009-01-28 21:54:33 +00001134 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1135 InitExprs[Init] = expr;
1136 return Result;
1137}
1138
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001139SourceRange InitListExpr::getSourceRange() const {
1140 if (SyntacticForm)
1141 return SyntacticForm->getSourceRange();
1142 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1143 if (Beg.isInvalid()) {
1144 // Find the first non-null initializer.
1145 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1146 E = InitExprs.end();
1147 I != E; ++I) {
1148 if (Stmt *S = *I) {
1149 Beg = S->getLocStart();
1150 break;
1151 }
1152 }
1153 }
1154 if (End.isInvalid()) {
1155 // Find the first non-null initializer from the end.
1156 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1157 E = InitExprs.rend();
1158 I != E; ++I) {
1159 if (Stmt *S = *I) {
1160 End = S->getSourceRange().getEnd();
1161 break;
1162 }
1163 }
1164 }
1165 return SourceRange(Beg, End);
1166}
1167
Steve Naroffbfdcae62008-09-04 15:31:07 +00001168/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001169///
1170const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001171 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001172 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001173}
1174
Mike Stump1eb44332009-09-09 15:08:12 +00001175SourceLocation BlockExpr::getCaretLocation() const {
1176 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001177}
Mike Stump1eb44332009-09-09 15:08:12 +00001178const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001179 return TheBlock->getBody();
1180}
Mike Stump1eb44332009-09-09 15:08:12 +00001181Stmt *BlockExpr::getBody() {
1182 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001183}
Steve Naroff56ee6892008-10-08 17:01:13 +00001184
1185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186//===----------------------------------------------------------------------===//
1187// Generic Expression Routines
1188//===----------------------------------------------------------------------===//
1189
Chris Lattner026dc962009-02-14 07:37:35 +00001190/// isUnusedResultAWarning - Return true if this immediate expression should
1191/// be warned about if the result is unused. If so, fill in Loc and Ranges
1192/// with location to warn on and the source range[s] to report with the
1193/// warning.
1194bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001195 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001196 // Don't warn if the expr is type dependent. The type could end up
1197 // instantiating to void.
1198 if (isTypeDependent())
1199 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 switch (getStmtClass()) {
1202 default:
John McCall0faede62010-03-12 07:11:26 +00001203 if (getType()->isVoidType())
1204 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001205 Loc = getExprLoc();
1206 R1 = getSourceRange();
1207 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001209 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001210 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 case UnaryOperatorClass: {
1212 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Reid Spencer5f016e22007-07-11 17:01:13 +00001214 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001215 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001216 case UO_PostInc:
1217 case UO_PostDec:
1218 case UO_PreInc:
1219 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001220 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001221 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001223 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001224 return false;
1225 break;
John McCall2de56d12010-08-25 11:45:40 +00001226 case UO_Real:
1227 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001229 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1230 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001231 return false;
1232 break;
John McCall2de56d12010-08-25 11:45:40 +00001233 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001234 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 }
Chris Lattner026dc962009-02-14 07:37:35 +00001236 Loc = UO->getOperatorLoc();
1237 R1 = UO->getSubExpr()->getSourceRange();
1238 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001240 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001241 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001242 switch (BO->getOpcode()) {
1243 default:
1244 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001245 // Consider the RHS of comma for side effects. LHS was checked by
1246 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001247 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001248 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1249 // lvalue-ness) of an assignment written in a macro.
1250 if (IntegerLiteral *IE =
1251 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1252 if (IE->getValue() == 0)
1253 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001254 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1255 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001256 case BO_LAnd:
1257 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001258 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1259 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1260 return false;
1261 break;
John McCallbf0ee352010-02-16 04:10:53 +00001262 }
Chris Lattner026dc962009-02-14 07:37:35 +00001263 if (BO->isAssignmentOp())
1264 return false;
1265 Loc = BO->getOperatorLoc();
1266 R1 = BO->getLHS()->getSourceRange();
1267 R2 = BO->getRHS()->getSourceRange();
1268 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001269 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001270 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001271 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001272 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001273
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001274 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001275 // The condition must be evaluated, but if either the LHS or RHS is a
1276 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001277 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001278 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001279 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001280 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001281 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001282 }
1283
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001285 // If the base pointer or element is to a volatile pointer/field, accessing
1286 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001287 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001288 return false;
1289 Loc = cast<MemberExpr>(this)->getMemberLoc();
1290 R1 = SourceRange(Loc, Loc);
1291 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1292 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 case ArraySubscriptExprClass:
1295 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001296 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001297 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001298 return false;
1299 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1300 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1301 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1302 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001305 case CXXOperatorCallExprClass:
1306 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001307 // If this is a direct call, get the callee.
1308 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001309 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001310 // If the callee has attribute pure, const, or warn_unused_result, warn
1311 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001312 //
1313 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1314 // updated to match for QoI.
1315 if (FD->getAttr<WarnUnusedResultAttr>() ||
1316 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1317 Loc = CE->getCallee()->getLocStart();
1318 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001320 if (unsigned NumArgs = CE->getNumArgs())
1321 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1322 CE->getArg(NumArgs-1)->getLocEnd());
1323 return true;
1324 }
Chris Lattner026dc962009-02-14 07:37:35 +00001325 }
1326 return false;
1327 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001328
1329 case CXXTemporaryObjectExprClass:
1330 case CXXConstructExprClass:
1331 return false;
1332
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001333 case ObjCMessageExprClass: {
1334 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1335 const ObjCMethodDecl *MD = ME->getMethodDecl();
1336 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1337 Loc = getExprLoc();
1338 return true;
1339 }
Chris Lattner026dc962009-02-14 07:37:35 +00001340 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
John McCall12f78a62010-12-02 01:19:52 +00001343 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001344 Loc = getExprLoc();
1345 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001346 return true;
John McCall12f78a62010-12-02 01:19:52 +00001347
Chris Lattner611b2ec2008-07-26 19:51:01 +00001348 case StmtExprClass: {
1349 // Statement exprs don't logically have side effects themselves, but are
1350 // sometimes used in macros in ways that give them a type that is unused.
1351 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1352 // however, if the result of the stmt expr is dead, we don't want to emit a
1353 // warning.
1354 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001355 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001356 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001357 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001358 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1359 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1360 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1361 }
Mike Stump1eb44332009-09-09 15:08:12 +00001362
John McCall0faede62010-03-12 07:11:26 +00001363 if (getType()->isVoidType())
1364 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001365 Loc = cast<StmtExpr>(this)->getLParenLoc();
1366 R1 = getSourceRange();
1367 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001368 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001369 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001370 // If this is an explicit cast to void, allow it. People do this when they
1371 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001372 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001373 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001374 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1375 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1376 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001377 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001378 if (getType()->isVoidType())
1379 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001380 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001381
Anders Carlsson58beed92009-11-17 17:11:23 +00001382 // If this is a cast to void or a constructor conversion, check the operand.
1383 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001384 if (CE->getCastKind() == CK_ToVoid ||
1385 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001386 return (cast<CastExpr>(this)->getSubExpr()
1387 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001388 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1389 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1390 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Eli Friedman4be1f472008-05-19 21:24:43 +00001393 case ImplicitCastExprClass:
1394 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001395 return (cast<ImplicitCastExpr>(this)
1396 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001397
Chris Lattner04421082008-04-08 04:40:51 +00001398 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001399 return (cast<CXXDefaultArgExpr>(this)
1400 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001401
1402 case CXXNewExprClass:
1403 // FIXME: In theory, there might be new expressions that don't have side
1404 // effects (e.g. a placement new with an uninitialized POD).
1405 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001406 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001407 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001408 return (cast<CXXBindTemporaryExpr>(this)
1409 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001410 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001411 return (cast<CXXExprWithTemporaries>(this)
1412 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001413 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001414}
1415
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001416/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001417/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001418bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001419 switch (getStmtClass()) {
1420 default:
1421 return false;
1422 case ObjCIvarRefExprClass:
1423 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001424 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001425 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001426 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001427 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001428 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001429 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001430 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001431 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001432 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001433 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001434 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1435 if (VD->hasGlobalStorage())
1436 return true;
1437 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001438 // dereferencing to a pointer is always a gc'able candidate,
1439 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001440 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001441 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001442 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001443 return false;
1444 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001445 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001446 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001447 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001448 }
1449 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001450 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001451 }
1452}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001453
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001454bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1455 if (isTypeDependent())
1456 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001457 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001458}
1459
Sebastian Redl369e51f2010-09-10 20:55:33 +00001460static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1461 Expr::CanThrowResult CT2) {
1462 // CanThrowResult constants are ordered so that the maximum is the correct
1463 // merge result.
1464 return CT1 > CT2 ? CT1 : CT2;
1465}
1466
1467static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1468 Expr *E = const_cast<Expr*>(CE);
1469 Expr::CanThrowResult R = Expr::CT_Cannot;
1470 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1471 I != IE && R != Expr::CT_Can; ++I) {
1472 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1473 }
1474 return R;
1475}
1476
1477static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1478 bool NullThrows = true) {
1479 if (!D)
1480 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1481
1482 // See if we can get a function type from the decl somehow.
1483 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1484 if (!VD) // If we have no clue what we're calling, assume the worst.
1485 return Expr::CT_Can;
1486
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001487 // As an extension, we assume that __attribute__((nothrow)) functions don't
1488 // throw.
1489 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1490 return Expr::CT_Cannot;
1491
Sebastian Redl369e51f2010-09-10 20:55:33 +00001492 QualType T = VD->getType();
1493 const FunctionProtoType *FT;
1494 if ((FT = T->getAs<FunctionProtoType>())) {
1495 } else if (const PointerType *PT = T->getAs<PointerType>())
1496 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1497 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1498 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1499 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1500 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1501 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1502 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1503
1504 if (!FT)
1505 return Expr::CT_Can;
1506
1507 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1508}
1509
1510static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1511 if (DC->isTypeDependent())
1512 return Expr::CT_Dependent;
1513
Sebastian Redl295995c2010-09-10 20:55:47 +00001514 if (!DC->getTypeAsWritten()->isReferenceType())
1515 return Expr::CT_Cannot;
1516
Sebastian Redl369e51f2010-09-10 20:55:33 +00001517 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1518}
1519
1520static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1521 const CXXTypeidExpr *DC) {
1522 if (DC->isTypeOperand())
1523 return Expr::CT_Cannot;
1524
1525 Expr *Op = DC->getExprOperand();
1526 if (Op->isTypeDependent())
1527 return Expr::CT_Dependent;
1528
1529 const RecordType *RT = Op->getType()->getAs<RecordType>();
1530 if (!RT)
1531 return Expr::CT_Cannot;
1532
1533 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1534 return Expr::CT_Cannot;
1535
1536 if (Op->Classify(C).isPRValue())
1537 return Expr::CT_Cannot;
1538
1539 return Expr::CT_Can;
1540}
1541
1542Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1543 // C++ [expr.unary.noexcept]p3:
1544 // [Can throw] if in a potentially-evaluated context the expression would
1545 // contain:
1546 switch (getStmtClass()) {
1547 case CXXThrowExprClass:
1548 // - a potentially evaluated throw-expression
1549 return CT_Can;
1550
1551 case CXXDynamicCastExprClass: {
1552 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1553 // where T is a reference type, that requires a run-time check
1554 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1555 if (CT == CT_Can)
1556 return CT;
1557 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1558 }
1559
1560 case CXXTypeidExprClass:
1561 // - a potentially evaluated typeid expression applied to a glvalue
1562 // expression whose type is a polymorphic class type
1563 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1564
1565 // - a potentially evaluated call to a function, member function, function
1566 // pointer, or member function pointer that does not have a non-throwing
1567 // exception-specification
1568 case CallExprClass:
1569 case CXXOperatorCallExprClass:
1570 case CXXMemberCallExprClass: {
1571 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1572 if (CT == CT_Can)
1573 return CT;
1574 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1575 }
1576
Sebastian Redl295995c2010-09-10 20:55:47 +00001577 case CXXConstructExprClass:
1578 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001579 CanThrowResult CT = CanCalleeThrow(
1580 cast<CXXConstructExpr>(this)->getConstructor());
1581 if (CT == CT_Can)
1582 return CT;
1583 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1584 }
1585
1586 case CXXNewExprClass: {
1587 CanThrowResult CT = MergeCanThrow(
1588 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1589 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1590 /*NullThrows*/false));
1591 if (CT == CT_Can)
1592 return CT;
1593 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1594 }
1595
1596 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001597 CanThrowResult CT = CanCalleeThrow(
1598 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1599 if (CT == CT_Can)
1600 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001601 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1602 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1603 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1604 Arg = Cast->getSubExpr();
1605 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1606 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1607 CanThrowResult CT2 = CanCalleeThrow(
1608 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1609 if (CT2 == CT_Can)
1610 return CT2;
1611 CT = MergeCanThrow(CT, CT2);
1612 }
1613 }
1614 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1615 }
1616
1617 case CXXBindTemporaryExprClass: {
1618 // The bound temporary has to be destroyed again, which might throw.
1619 CanThrowResult CT = CanCalleeThrow(
1620 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1621 if (CT == CT_Can)
1622 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001623 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1624 }
1625
1626 // ObjC message sends are like function calls, but never have exception
1627 // specs.
1628 case ObjCMessageExprClass:
1629 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001630 return CT_Can;
1631
1632 // Many other things have subexpressions, so we have to test those.
1633 // Some are simple:
1634 case ParenExprClass:
1635 case MemberExprClass:
1636 case CXXReinterpretCastExprClass:
1637 case CXXConstCastExprClass:
1638 case ConditionalOperatorClass:
1639 case CompoundLiteralExprClass:
1640 case ExtVectorElementExprClass:
1641 case InitListExprClass:
1642 case DesignatedInitExprClass:
1643 case ParenListExprClass:
1644 case VAArgExprClass:
1645 case CXXDefaultArgExprClass:
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001646 case CXXExprWithTemporariesClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001647 case ObjCIvarRefExprClass:
1648 case ObjCIsaExprClass:
1649 case ShuffleVectorExprClass:
1650 return CanSubExprsThrow(C, this);
1651
1652 // Some might be dependent for other reasons.
1653 case UnaryOperatorClass:
1654 case ArraySubscriptExprClass:
1655 case ImplicitCastExprClass:
1656 case CStyleCastExprClass:
1657 case CXXStaticCastExprClass:
1658 case CXXFunctionalCastExprClass:
1659 case BinaryOperatorClass:
1660 case CompoundAssignOperatorClass: {
1661 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1662 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1663 }
1664
1665 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1666 case StmtExprClass:
1667 return CT_Can;
1668
1669 case ChooseExprClass:
1670 if (isTypeDependent() || isValueDependent())
1671 return CT_Dependent;
1672 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1673
1674 // Some expressions are always dependent.
1675 case DependentScopeDeclRefExprClass:
1676 case CXXUnresolvedConstructExprClass:
1677 case CXXDependentScopeMemberExprClass:
1678 return CT_Dependent;
1679
1680 default:
1681 // All other expressions don't have subexpressions, or else they are
1682 // unevaluated.
1683 return CT_Cannot;
1684 }
1685}
1686
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001687Expr* Expr::IgnoreParens() {
1688 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001689 while (true) {
1690 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1691 E = P->getSubExpr();
1692 continue;
1693 }
1694 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1695 if (P->getOpcode() == UO_Extension) {
1696 E = P->getSubExpr();
1697 continue;
1698 }
1699 }
1700 return E;
1701 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001702}
1703
Chris Lattner56f34942008-02-13 01:02:39 +00001704/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1705/// or CastExprs or ImplicitCastExprs, returning their operand.
1706Expr *Expr::IgnoreParenCasts() {
1707 Expr *E = this;
1708 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001709 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001710 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001711 continue;
1712 }
1713 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001714 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001715 continue;
1716 }
1717 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1718 if (P->getOpcode() == UO_Extension) {
1719 E = P->getSubExpr();
1720 continue;
1721 }
1722 }
1723 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001724 }
1725}
1726
John McCall9c5d70c2010-12-04 08:24:19 +00001727/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1728/// casts. This is intended purely as a temporary workaround for code
1729/// that hasn't yet been rewritten to do the right thing about those
1730/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001731Expr *Expr::IgnoreParenLValueCasts() {
1732 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001733 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001734 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1735 E = P->getSubExpr();
1736 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001737 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001738 if (P->getCastKind() == CK_LValueToRValue) {
1739 E = P->getSubExpr();
1740 continue;
1741 }
John McCall9c5d70c2010-12-04 08:24:19 +00001742 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1743 if (P->getOpcode() == UO_Extension) {
1744 E = P->getSubExpr();
1745 continue;
1746 }
John McCallf6a16482010-12-04 03:47:34 +00001747 }
1748 break;
1749 }
1750 return E;
1751}
1752
John McCall2fc46bf2010-05-05 22:59:52 +00001753Expr *Expr::IgnoreParenImpCasts() {
1754 Expr *E = this;
1755 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001756 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001757 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001758 continue;
1759 }
1760 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001761 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001762 continue;
1763 }
1764 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1765 if (P->getOpcode() == UO_Extension) {
1766 E = P->getSubExpr();
1767 continue;
1768 }
1769 }
1770 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001771 }
1772}
1773
Chris Lattnerecdd8412009-03-13 17:28:01 +00001774/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1775/// value (including ptr->int casts of the same size). Strip off any
1776/// ParenExpr or CastExprs, returning their operand.
1777Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1778 Expr *E = this;
1779 while (true) {
1780 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1781 E = P->getSubExpr();
1782 continue;
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Chris Lattnerecdd8412009-03-13 17:28:01 +00001785 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1786 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001787 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001788 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnerecdd8412009-03-13 17:28:01 +00001790 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1791 E = SE;
1792 continue;
1793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001795 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001796 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001797 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001798 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001799 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1800 E = SE;
1801 continue;
1802 }
1803 }
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001805 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1806 if (P->getOpcode() == UO_Extension) {
1807 E = P->getSubExpr();
1808 continue;
1809 }
1810 }
1811
Chris Lattnerecdd8412009-03-13 17:28:01 +00001812 return E;
1813 }
1814}
1815
Douglas Gregor6eef5192009-12-14 19:27:10 +00001816bool Expr::isDefaultArgument() const {
1817 const Expr *E = this;
1818 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1819 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001820
Douglas Gregor6eef5192009-12-14 19:27:10 +00001821 return isa<CXXDefaultArgExpr>(E);
1822}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001823
Douglas Gregor2f599792010-04-02 18:24:57 +00001824/// \brief Skip over any no-op casts and any temporary-binding
1825/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001826static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001827 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001828 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001829 E = ICE->getSubExpr();
1830 else
1831 break;
1832 }
1833
1834 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1835 E = BE->getSubExpr();
1836
1837 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001838 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001839 E = ICE->getSubExpr();
1840 else
1841 break;
1842 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001843
1844 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001845}
1846
John McCall558d2ab2010-09-15 10:14:12 +00001847/// isTemporaryObject - Determines if this expression produces a
1848/// temporary of the given class type.
1849bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1850 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1851 return false;
1852
Anders Carlssonf8b30152010-11-28 16:40:49 +00001853 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001854
John McCall58277b52010-09-15 20:59:13 +00001855 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001856 if (!E->Classify(C).isPRValue()) {
1857 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001858 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001859 return false;
1860 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001861
John McCall19e60ad2010-09-16 06:57:56 +00001862 // Black-list a few cases which yield pr-values of class type that don't
1863 // refer to temporaries of that type:
1864
1865 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001866 if (isa<ImplicitCastExpr>(E)) {
1867 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1868 case CK_DerivedToBase:
1869 case CK_UncheckedDerivedToBase:
1870 return false;
1871 default:
1872 break;
1873 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001874 }
1875
John McCall19e60ad2010-09-16 06:57:56 +00001876 // - member expressions (all)
1877 if (isa<MemberExpr>(E))
1878 return false;
1879
John McCall558d2ab2010-09-15 10:14:12 +00001880 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001881}
1882
Douglas Gregor898574e2008-12-05 23:32:09 +00001883/// hasAnyTypeDependentArguments - Determines if any of the expressions
1884/// in Exprs is type-dependent.
1885bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1886 for (unsigned I = 0; I < NumExprs; ++I)
1887 if (Exprs[I]->isTypeDependent())
1888 return true;
1889
1890 return false;
1891}
1892
1893/// hasAnyValueDependentArguments - Determines if any of the expressions
1894/// in Exprs is value-dependent.
1895bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1896 for (unsigned I = 0; I < NumExprs; ++I)
1897 if (Exprs[I]->isValueDependent())
1898 return true;
1899
1900 return false;
1901}
1902
John McCall4204f072010-08-02 21:13:48 +00001903bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001904 // This function is attempting whether an expression is an initializer
1905 // which can be evaluated at compile-time. isEvaluatable handles most
1906 // of the cases, but it can't deal with some initializer-specific
1907 // expressions, and it can't deal with aggregates; we deal with those here,
1908 // and fall back to isEvaluatable for the other cases.
1909
John McCall4204f072010-08-02 21:13:48 +00001910 // If we ever capture reference-binding directly in the AST, we can
1911 // kill the second parameter.
1912
1913 if (IsForRef) {
1914 EvalResult Result;
1915 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1916 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001917
Anders Carlssone8a32b82008-11-24 05:23:59 +00001918 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001919 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001920 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001921 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001922 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001923 return true;
John McCallb4b9b152010-08-01 21:51:45 +00001924 case CXXTemporaryObjectExprClass:
1925 case CXXConstructExprClass: {
1926 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00001927
1928 // Only if it's
1929 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00001930 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00001931 if (!CE->getNumArgs()) return true;
1932
1933 // 2) an elidable trivial copy construction of an operand which is
1934 // itself a constant initializer. Note that we consider the
1935 // operand on its own, *not* as a reference binding.
1936 return CE->isElidable() &&
1937 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00001938 }
Nate Begeman59b5da62009-01-18 03:20:47 +00001939 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001940 // This handles gcc's extension that allows global initializers like
1941 // "struct x {int x;} x = (struct x) {};".
1942 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001943 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00001944 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00001945 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001946 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001947 // FIXME: This doesn't deal with fields with reference types correctly.
1948 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1949 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001950 const InitListExpr *Exp = cast<InitListExpr>(this);
1951 unsigned numInits = Exp->getNumInits();
1952 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00001953 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001954 return false;
1955 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001956 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001957 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001958 case ImplicitValueInitExprClass:
1959 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001960 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00001961 return cast<ParenExpr>(this)->getSubExpr()
1962 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00001963 case ChooseExprClass:
1964 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
1965 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001966 case UnaryOperatorClass: {
1967 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001968 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00001969 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001970 break;
1971 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001972 case BinaryOperatorClass: {
1973 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1974 // but this handles the common case.
1975 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00001976 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00001977 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1978 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1979 return true;
1980 break;
1981 }
John McCall4204f072010-08-02 21:13:48 +00001982 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00001983 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00001984 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001985 case CStyleCastExprClass:
1986 // Handle casts with a destination that's a struct or union; this
1987 // deals with both the gcc no-op struct cast extension and the
1988 // cast-to-union extension.
1989 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00001990 return cast<CastExpr>(this)->getSubExpr()
1991 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001992
Chris Lattner430656e2009-10-13 22:12:09 +00001993 // Integer->integer casts can be handled here, which is important for
1994 // things like (int)(&&x-&&y). Scary but true.
1995 if (getType()->isIntegerType() &&
1996 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00001997 return cast<CastExpr>(this)->getSubExpr()
1998 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00001999
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002000 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002001 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002002 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002003}
2004
Reid Spencer5f016e22007-07-11 17:01:13 +00002005/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2006/// integer constant expression with the value zero, or if this is one that is
2007/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002008bool Expr::isNullPointerConstant(ASTContext &Ctx,
2009 NullPointerConstantValueDependence NPC) const {
2010 if (isValueDependent()) {
2011 switch (NPC) {
2012 case NPC_NeverValueDependent:
2013 assert(false && "Unexpected value dependent expression!");
2014 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002015
Douglas Gregorce940492009-09-25 04:25:58 +00002016 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002017 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002018
Douglas Gregorce940492009-09-25 04:25:58 +00002019 case NPC_ValueDependentIsNotNull:
2020 return false;
2021 }
2022 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002023
Sebastian Redl07779722008-10-31 14:43:28 +00002024 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002025 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002026 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002027 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002028 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002029 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002030 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002031 Pointee->isVoidType() && // to void*
2032 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002033 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002034 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002036 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2037 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002038 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002039 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2040 // Accept ((void*)0) as a null pointer constant, as many other
2041 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002042 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002043 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002044 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002045 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002046 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002047 } else if (isa<GNUNullExpr>(this)) {
2048 // The GNU __null extension is always a null pointer constant.
2049 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002050 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002051
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002052 // C++0x nullptr_t is always a null pointer constant.
2053 if (getType()->isNullPtrType())
2054 return true;
2055
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002056 if (const RecordType *UT = getType()->getAsUnionType())
2057 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2058 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2059 const Expr *InitExpr = CLE->getInitializer();
2060 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2061 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2062 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002063 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002064 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002065 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002066 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 // If we have an integer constant expression, we need to *evaluate* it and
2069 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002070 llvm::APSInt Result;
2071 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002072}
Steve Naroff31a45842007-07-28 23:10:27 +00002073
John McCallf6a16482010-12-04 03:47:34 +00002074/// \brief If this expression is an l-value for an Objective C
2075/// property, find the underlying property reference expression.
2076const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2077 const Expr *E = this;
2078 while (true) {
2079 assert((E->getValueKind() == VK_LValue &&
2080 E->getObjectKind() == OK_ObjCProperty) &&
2081 "expression is not a property reference");
2082 E = E->IgnoreParenCasts();
2083 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2084 if (BO->getOpcode() == BO_Comma) {
2085 E = BO->getRHS();
2086 continue;
2087 }
2088 }
2089
2090 break;
2091 }
2092
2093 return cast<ObjCPropertyRefExpr>(E);
2094}
2095
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002096FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002097 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002098
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002099 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002100 if (ICE->getCastKind() == CK_LValueToRValue ||
2101 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002102 E = ICE->getSubExpr()->IgnoreParens();
2103 else
2104 break;
2105 }
2106
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002107 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002108 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002109 if (Field->isBitField())
2110 return Field;
2111
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002112 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2113 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2114 if (Field->isBitField())
2115 return Field;
2116
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002117 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2118 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2119 return BinOp->getLHS()->getBitField();
2120
2121 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002122}
2123
Anders Carlsson09380262010-01-31 17:18:49 +00002124bool Expr::refersToVectorElement() const {
2125 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002126
Anders Carlsson09380262010-01-31 17:18:49 +00002127 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002128 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002129 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002130 E = ICE->getSubExpr()->IgnoreParens();
2131 else
2132 break;
2133 }
Sean Huntc3021132010-05-05 15:23:54 +00002134
Anders Carlsson09380262010-01-31 17:18:49 +00002135 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2136 return ASE->getBase()->getType()->isVectorType();
2137
2138 if (isa<ExtVectorElementExpr>(E))
2139 return true;
2140
2141 return false;
2142}
2143
Chris Lattner2140e902009-02-16 22:14:05 +00002144/// isArrow - Return true if the base expression is a pointer to vector,
2145/// return false if the base expression is a vector.
2146bool ExtVectorElementExpr::isArrow() const {
2147 return getBase()->getType()->isPointerType();
2148}
2149
Nate Begeman213541a2008-04-18 23:10:10 +00002150unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002151 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002152 return VT->getNumElements();
2153 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002154}
2155
Nate Begeman8a997642008-05-09 06:41:27 +00002156/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002157bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002158 // FIXME: Refactor this code to an accessor on the AST node which returns the
2159 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002160 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002161
2162 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002163 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002164 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002165
Nate Begeman190d6a22009-01-18 02:01:21 +00002166 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002167 if (Comp[0] == 's' || Comp[0] == 'S')
2168 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Daniel Dunbar15027422009-10-17 23:53:04 +00002170 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2171 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002172 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002173
Steve Narofffec0b492007-07-30 03:29:09 +00002174 return false;
2175}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002176
Nate Begeman8a997642008-05-09 06:41:27 +00002177/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002178void ExtVectorElementExpr::getEncodedElementAccess(
2179 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002180 llvm::StringRef Comp = Accessor->getName();
2181 if (Comp[0] == 's' || Comp[0] == 'S')
2182 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002183
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002184 bool isHi = Comp == "hi";
2185 bool isLo = Comp == "lo";
2186 bool isEven = Comp == "even";
2187 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002188
Nate Begeman8a997642008-05-09 06:41:27 +00002189 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2190 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002191
Nate Begeman8a997642008-05-09 06:41:27 +00002192 if (isHi)
2193 Index = e + i;
2194 else if (isLo)
2195 Index = i;
2196 else if (isEven)
2197 Index = 2 * i;
2198 else if (isOdd)
2199 Index = 2 * i + 1;
2200 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002201 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002202
Nate Begeman3b8d1162008-05-13 21:03:02 +00002203 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002204 }
Nate Begeman8a997642008-05-09 06:41:27 +00002205}
2206
Douglas Gregor04badcf2010-04-21 00:45:42 +00002207ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002208 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002209 SourceLocation LBracLoc,
2210 SourceLocation SuperLoc,
2211 bool IsInstanceSuper,
2212 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002213 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002214 ObjCMethodDecl *Method,
2215 Expr **Args, unsigned NumArgs,
2216 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002217 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2218 /*TypeDependent=*/false, /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002219 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2220 HasMethod(Method != 0), SuperLoc(SuperLoc),
2221 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2222 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002223 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002224{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002225 setReceiverPointer(SuperType.getAsOpaquePtr());
2226 if (NumArgs)
2227 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002228}
2229
Douglas Gregor04badcf2010-04-21 00:45:42 +00002230ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002231 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002232 SourceLocation LBracLoc,
2233 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002234 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002235 ObjCMethodDecl *Method,
2236 Expr **Args, unsigned NumArgs,
2237 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002238 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00002239 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002240 hasAnyValueDependentArguments(Args, NumArgs))),
2241 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2242 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2243 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002244 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002245{
2246 setReceiverPointer(Receiver);
2247 if (NumArgs)
2248 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002249}
2250
Douglas Gregor04badcf2010-04-21 00:45:42 +00002251ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002252 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002253 SourceLocation LBracLoc,
2254 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002255 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002256 ObjCMethodDecl *Method,
2257 Expr **Args, unsigned NumArgs,
2258 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002259 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00002260 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002261 hasAnyValueDependentArguments(Args, NumArgs))),
2262 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2263 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2264 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002265 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002266{
2267 setReceiverPointer(Receiver);
2268 if (NumArgs)
2269 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002270}
2271
Douglas Gregor04badcf2010-04-21 00:45:42 +00002272ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002273 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002274 SourceLocation LBracLoc,
2275 SourceLocation SuperLoc,
2276 bool IsInstanceSuper,
2277 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002278 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002279 ObjCMethodDecl *Method,
2280 Expr **Args, unsigned NumArgs,
2281 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002282 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002283 NumArgs * sizeof(Expr *);
2284 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002285 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00002286 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002287 RBracLoc);
2288}
2289
2290ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002291 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002292 SourceLocation LBracLoc,
2293 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002294 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002295 ObjCMethodDecl *Method,
2296 Expr **Args, unsigned NumArgs,
2297 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002298 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002299 NumArgs * sizeof(Expr *);
2300 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002301 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002302 NumArgs, RBracLoc);
2303}
2304
2305ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002306 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002307 SourceLocation LBracLoc,
2308 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002309 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002310 ObjCMethodDecl *Method,
2311 Expr **Args, unsigned NumArgs,
2312 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002313 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002314 NumArgs * sizeof(Expr *);
2315 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002316 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002317 NumArgs, RBracLoc);
2318}
2319
Sean Huntc3021132010-05-05 15:23:54 +00002320ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002321 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002322 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002323 NumArgs * sizeof(Expr *);
2324 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2325 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2326}
Sean Huntc3021132010-05-05 15:23:54 +00002327
Douglas Gregor04badcf2010-04-21 00:45:42 +00002328Selector ObjCMessageExpr::getSelector() const {
2329 if (HasMethod)
2330 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2331 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002332 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002333}
2334
2335ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2336 switch (getReceiverKind()) {
2337 case Instance:
2338 if (const ObjCObjectPointerType *Ptr
2339 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2340 return Ptr->getInterfaceDecl();
2341 break;
2342
2343 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002344 if (const ObjCObjectType *Ty
2345 = getClassReceiver()->getAs<ObjCObjectType>())
2346 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002347 break;
2348
2349 case SuperInstance:
2350 if (const ObjCObjectPointerType *Ptr
2351 = getSuperType()->getAs<ObjCObjectPointerType>())
2352 return Ptr->getInterfaceDecl();
2353 break;
2354
2355 case SuperClass:
2356 if (const ObjCObjectPointerType *Iface
2357 = getSuperType()->getAs<ObjCObjectPointerType>())
2358 return Iface->getInterfaceDecl();
2359 break;
2360 }
2361
2362 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002363}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002364
Chris Lattner27437ca2007-10-25 00:29:32 +00002365bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002366 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002367}
2368
Nate Begeman888376a2009-08-12 02:28:50 +00002369void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2370 unsigned NumExprs) {
2371 if (SubExprs) C.Deallocate(SubExprs);
2372
2373 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002374 this->NumExprs = NumExprs;
2375 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002376}
Nate Begeman888376a2009-08-12 02:28:50 +00002377
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002378//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002379// DesignatedInitExpr
2380//===----------------------------------------------------------------------===//
2381
2382IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2383 assert(Kind == FieldDesignator && "Only valid on a field designator");
2384 if (Field.NameOrField & 0x01)
2385 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2386 else
2387 return getField()->getIdentifier();
2388}
2389
Sean Huntc3021132010-05-05 15:23:54 +00002390DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002391 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002392 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002393 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002394 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002395 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002396 unsigned NumIndexExprs,
2397 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002398 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002399 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregor9ea62762009-05-21 23:17:49 +00002400 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002401 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2402 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002403 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002404
2405 // Record the initializer itself.
2406 child_iterator Child = child_begin();
2407 *Child++ = Init;
2408
2409 // Copy the designators and their subexpressions, computing
2410 // value-dependence along the way.
2411 unsigned IndexIdx = 0;
2412 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002413 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002414
2415 if (this->Designators[I].isArrayDesignator()) {
2416 // Compute type- and value-dependence.
2417 Expr *Index = IndexExprs[IndexIdx];
John McCall8e6285a2010-10-26 08:39:16 +00002418 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002419 Index->isTypeDependent() || Index->isValueDependent();
2420
2421 // Copy the index expressions into permanent storage.
2422 *Child++ = IndexExprs[IndexIdx++];
2423 } else if (this->Designators[I].isArrayRangeDesignator()) {
2424 // Compute type- and value-dependence.
2425 Expr *Start = IndexExprs[IndexIdx];
2426 Expr *End = IndexExprs[IndexIdx + 1];
John McCall8e6285a2010-10-26 08:39:16 +00002427 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002428 Start->isTypeDependent() || Start->isValueDependent() ||
2429 End->isTypeDependent() || End->isValueDependent();
2430
2431 // Copy the start/end expressions into permanent storage.
2432 *Child++ = IndexExprs[IndexIdx++];
2433 *Child++ = IndexExprs[IndexIdx++];
2434 }
2435 }
2436
2437 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002438}
2439
Douglas Gregor05c13a32009-01-22 00:58:24 +00002440DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002441DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002442 unsigned NumDesignators,
2443 Expr **IndexExprs, unsigned NumIndexExprs,
2444 SourceLocation ColonOrEqualLoc,
2445 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002446 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002447 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002448 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002449 ColonOrEqualLoc, UsesColonSyntax,
2450 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002451}
2452
Mike Stump1eb44332009-09-09 15:08:12 +00002453DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002454 unsigned NumIndexExprs) {
2455 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2456 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2457 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2458}
2459
Douglas Gregor319d57f2010-01-06 23:17:19 +00002460void DesignatedInitExpr::setDesignators(ASTContext &C,
2461 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002462 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002463 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002464 NumDesignators = NumDesigs;
2465 for (unsigned I = 0; I != NumDesigs; ++I)
2466 Designators[I] = Desigs[I];
2467}
2468
Douglas Gregor05c13a32009-01-22 00:58:24 +00002469SourceRange DesignatedInitExpr::getSourceRange() const {
2470 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002471 Designator &First =
2472 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002473 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002474 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002475 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2476 else
2477 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2478 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002479 StartLoc =
2480 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002481 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2482}
2483
Douglas Gregor05c13a32009-01-22 00:58:24 +00002484Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2485 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2486 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2487 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002488 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2489 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2490}
2491
2492Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002493 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002494 "Requires array range designator");
2495 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2496 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002497 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2498 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2499}
2500
2501Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002502 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002503 "Requires array range designator");
2504 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2505 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002506 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2507 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2508}
2509
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002510/// \brief Replaces the designator at index @p Idx with the series
2511/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002512void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002513 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002514 const Designator *Last) {
2515 unsigned NumNewDesignators = Last - First;
2516 if (NumNewDesignators == 0) {
2517 std::copy_backward(Designators + Idx + 1,
2518 Designators + NumDesignators,
2519 Designators + Idx);
2520 --NumNewDesignators;
2521 return;
2522 } else if (NumNewDesignators == 1) {
2523 Designators[Idx] = *First;
2524 return;
2525 }
2526
Mike Stump1eb44332009-09-09 15:08:12 +00002527 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002528 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002529 std::copy(Designators, Designators + Idx, NewDesignators);
2530 std::copy(First, Last, NewDesignators + Idx);
2531 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2532 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002533 Designators = NewDesignators;
2534 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2535}
2536
Mike Stump1eb44332009-09-09 15:08:12 +00002537ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002538 Expr **exprs, unsigned nexprs,
2539 SourceLocation rparenloc)
John McCallf89e55a2010-11-18 06:31:45 +00002540: Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002541 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002542 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002543 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Nate Begeman2ef13e52009-08-10 23:49:36 +00002545 Exprs = new (C) Stmt*[nexprs];
2546 for (unsigned i = 0; i != nexprs; ++i)
2547 Exprs[i] = exprs[i];
2548}
2549
Douglas Gregor05c13a32009-01-22 00:58:24 +00002550//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002551// ExprIterator.
2552//===----------------------------------------------------------------------===//
2553
2554Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2555Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2556Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2557const Expr* ConstExprIterator::operator[](size_t idx) const {
2558 return cast<Expr>(I[idx]);
2559}
2560const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2561const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2562
2563//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002564// Child Iterators for iterating over subexpressions/substatements
2565//===----------------------------------------------------------------------===//
2566
2567// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002568Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2569Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002570
Steve Naroff7779db42007-11-12 14:29:37 +00002571// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002572Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2573Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002574
Steve Naroffe3e9add2008-06-02 23:03:37 +00002575// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002576Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2577{
John McCall12f78a62010-12-02 01:19:52 +00002578 if (Receiver.is<Stmt*>()) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002579 // Hack alert!
John McCall12f78a62010-12-02 01:19:52 +00002580 return reinterpret_cast<Stmt**> (&Receiver);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002581 }
2582 return child_iterator();
2583}
2584
2585Stmt::child_iterator ObjCPropertyRefExpr::child_end()
John McCall12f78a62010-12-02 01:19:52 +00002586{ return Receiver.is<Stmt*>() ?
2587 reinterpret_cast<Stmt**> (&Receiver)+1 :
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002588 child_iterator();
2589}
Steve Naroffae784072008-05-30 00:40:33 +00002590
Steve Narofff242b1b2009-07-24 17:54:45 +00002591// ObjCIsaExpr
2592Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2593Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2594
Chris Lattnerd9f69102008-08-10 01:53:14 +00002595// PredefinedExpr
2596Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2597Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002598
2599// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002600Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2601Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002602
2603// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002604Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002605Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002606
2607// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002608Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2609Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002610
Chris Lattner5d661452007-08-26 03:42:43 +00002611// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002612Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2613Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002614
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002615// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002616Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2617Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002618
2619// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002620Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2621Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002622
2623// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002624Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2625Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002626
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002627// OffsetOfExpr
2628Stmt::child_iterator OffsetOfExpr::child_begin() {
2629 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2630 + NumComps);
2631}
2632Stmt::child_iterator OffsetOfExpr::child_end() {
2633 return child_iterator(&*child_begin() + NumExprs);
2634}
2635
Sebastian Redl05189992008-11-11 17:56:53 +00002636// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002637Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002638 // If this is of a type and the type is a VLA type (and not a typedef), the
2639 // size expression of the VLA needs to be treated as an executable expression.
2640 // Why isn't this weirdness documented better in StmtIterator?
2641 if (isArgumentType()) {
2642 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2643 getArgumentType().getTypePtr()))
2644 return child_iterator(T);
2645 return child_iterator();
2646 }
Sebastian Redld4575892008-12-03 23:17:54 +00002647 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002648}
Sebastian Redl05189992008-11-11 17:56:53 +00002649Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2650 if (isArgumentType())
2651 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002652 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002653}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002654
2655// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002656Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002657 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002658}
Ted Kremenek1237c672007-08-24 20:06:47 +00002659Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002660 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002661}
2662
2663// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002664Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002665 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002666}
Ted Kremenek1237c672007-08-24 20:06:47 +00002667Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002668 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002669}
Ted Kremenek1237c672007-08-24 20:06:47 +00002670
2671// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002672Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2673Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002674
Nate Begeman213541a2008-04-18 23:10:10 +00002675// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002676Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2677Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002678
2679// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002680Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2681Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002682
Ted Kremenek1237c672007-08-24 20:06:47 +00002683// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002684Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2685Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002686
2687// BinaryOperator
2688Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002689 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002690}
Ted Kremenek1237c672007-08-24 20:06:47 +00002691Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002692 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002693}
2694
2695// ConditionalOperator
2696Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002697 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002698}
Ted Kremenek1237c672007-08-24 20:06:47 +00002699Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002700 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002701}
2702
2703// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002704Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2705Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002706
Ted Kremenek1237c672007-08-24 20:06:47 +00002707// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002708Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2709Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002710
2711// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002712Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2713 return child_iterator();
2714}
2715
2716Stmt::child_iterator TypesCompatibleExpr::child_end() {
2717 return child_iterator();
2718}
Ted Kremenek1237c672007-08-24 20:06:47 +00002719
2720// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002721Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2722Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002723
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002724// GNUNullExpr
2725Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2726Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2727
Eli Friedmand38617c2008-05-14 19:38:39 +00002728// ShuffleVectorExpr
2729Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002730 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002731}
2732Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002733 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002734}
2735
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002736// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002737Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2738Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002739
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002740// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002741Stmt::child_iterator InitListExpr::child_begin() {
2742 return InitExprs.size() ? &InitExprs[0] : 0;
2743}
2744Stmt::child_iterator InitListExpr::child_end() {
2745 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2746}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002747
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002748// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002749Stmt::child_iterator DesignatedInitExpr::child_begin() {
2750 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2751 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002752 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2753}
2754Stmt::child_iterator DesignatedInitExpr::child_end() {
2755 return child_iterator(&*child_begin() + NumSubExprs);
2756}
2757
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002758// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002759Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2760 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002761}
2762
Mike Stump1eb44332009-09-09 15:08:12 +00002763Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2764 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002765}
2766
Nate Begeman2ef13e52009-08-10 23:49:36 +00002767// ParenListExpr
2768Stmt::child_iterator ParenListExpr::child_begin() {
2769 return &Exprs[0];
2770}
2771Stmt::child_iterator ParenListExpr::child_end() {
2772 return &Exprs[0]+NumExprs;
2773}
2774
Ted Kremenek1237c672007-08-24 20:06:47 +00002775// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002776Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002777 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002778}
2779Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002780 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002781}
Ted Kremenek1237c672007-08-24 20:06:47 +00002782
2783// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002784Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2785Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002786
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002787// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002788Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002789 return child_iterator();
2790}
2791Stmt::child_iterator ObjCSelectorExpr::child_end() {
2792 return child_iterator();
2793}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002794
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002795// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002796Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2797 return child_iterator();
2798}
2799Stmt::child_iterator ObjCProtocolExpr::child_end() {
2800 return child_iterator();
2801}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002802
Steve Naroff563477d2007-09-18 23:55:05 +00002803// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002804Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002805 if (getReceiverKind() == Instance)
2806 return reinterpret_cast<Stmt **>(this + 1);
2807 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002808}
2809Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002810 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002811}
2812
Steve Naroff4eb206b2008-09-03 18:15:37 +00002813// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002814Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2815Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002816
Ted Kremenek9da13f92008-09-26 23:24:14 +00002817Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2818Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002819
2820// OpaqueValueExpr
2821SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2822Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2823Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2824