blob: cfe89a8fb0f985deedc5710ba8da77eccecd4068 [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
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000111void ExplicitTemplateArgumentList::initializeFrom(
112 const TemplateArgumentListInfo &Info,
113 bool &Dependent,
114 bool &ContainsUnexpandedParameterPack) {
115 LAngleLoc = Info.getLAngleLoc();
116 RAngleLoc = Info.getRAngleLoc();
117 NumTemplateArgs = Info.size();
118
119 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
120 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
121 Dependent = Dependent || Info[i].getArgument().isDependent();
122 ContainsUnexpandedParameterPack
123 = ContainsUnexpandedParameterPack ||
124 Info[i].getArgument().containsUnexpandedParameterPack();
125
126 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
127 }
128}
129
John McCalld5532b62009-11-23 01:53:49 +0000130void ExplicitTemplateArgumentList::copyInto(
131 TemplateArgumentListInfo &Info) const {
132 Info.setLAngleLoc(LAngleLoc);
133 Info.setRAngleLoc(RAngleLoc);
134 for (unsigned I = 0; I != NumTemplateArgs; ++I)
135 Info.addArgument(getTemplateArgs()[I]);
136}
137
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000138std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
139 return sizeof(ExplicitTemplateArgumentList) +
140 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
141}
142
John McCalld5532b62009-11-23 01:53:49 +0000143std::size_t ExplicitTemplateArgumentList::sizeFor(
144 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000145 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000146}
147
Douglas Gregord967e312011-01-19 21:52:31 +0000148/// \brief Compute the type- and value-dependence of a declaration reference
149/// based on the declaration being referenced.
150static void computeDeclRefDependence(NamedDecl *D, QualType T,
151 bool &TypeDependent,
152 bool &ValueDependent) {
153 TypeDependent = false;
154 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000155
Douglas Gregor0da76df2009-11-23 11:41:28 +0000156
157 // (TD) C++ [temp.dep.expr]p3:
158 // An id-expression is type-dependent if it contains:
159 //
Sean Huntc3021132010-05-05 15:23:54 +0000160 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000161 //
162 // (VD) C++ [temp.dep.constexpr]p2:
163 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000164
Douglas Gregor0da76df2009-11-23 11:41:28 +0000165 // (TD) - an identifier that was declared with dependent type
166 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (T->isDependentType()) {
168 TypeDependent = true;
169 ValueDependent = true;
170 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000171 }
Douglas Gregord967e312011-01-19 21:52:31 +0000172
Douglas Gregor0da76df2009-11-23 11:41:28 +0000173 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000174 if (D->getDeclName().getNameKind()
175 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000176 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000177 TypeDependent = true;
178 ValueDependent = true;
179 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000180 }
181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
184 return;
185 }
186
Douglas Gregor0da76df2009-11-23 11:41:28 +0000187 // (VD) - a constant with integral or enumeration type and is
188 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000189 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000190 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000191 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000192 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000193 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000194 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000195 }
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000197 // (VD) - FIXME: Missing from the standard:
198 // - a member function or a static data member of the current
199 // instantiation
200 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000201 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000202 ValueDependent = true;
203
204 return;
205 }
206
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000207 // (VD) - FIXME: Missing from the standard:
208 // - a member function or a static data member of the current
209 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000210 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
211 ValueDependent = true;
212 return;
213 }
214}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000215
Douglas Gregord967e312011-01-19 21:52:31 +0000216void DeclRefExpr::computeDependence() {
217 bool TypeDependent = false;
218 bool ValueDependent = false;
219 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
224 // and
225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
228 if (!TypeDependent && !ValueDependent &&
229 hasExplicitTemplateArgs() &&
230 TemplateSpecializationType::anyDependentTemplateArguments(
231 getTemplateArgs(),
232 getNumTemplateArgs())) {
233 TypeDependent = true;
234 ValueDependent = true;
235 }
236
237 ExprBits.TypeDependent = TypeDependent;
238 ExprBits.ValueDependent = ValueDependent;
239
Douglas Gregor10738d32010-12-23 23:51:58 +0000240 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000241 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000242 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000243}
244
Sean Huntc3021132010-05-05 15:23:54 +0000245DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000246 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000247 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000248 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000249 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000250 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000251 DecoratedD(D,
252 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000253 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000254 Loc(NameLoc) {
255 if (Qualifier) {
256 NameQualifier *NQ = getNameQualifier();
257 NQ->NNS = Qualifier;
258 NQ->Range = QualifierRange;
259 }
Sean Huntc3021132010-05-05 15:23:54 +0000260
John McCalld5532b62009-11-23 01:53:49 +0000261 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000262 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000263
264 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000265}
266
Abramo Bagnara25777432010-08-11 22:01:17 +0000267DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
268 SourceRange QualifierRange,
269 ValueDecl *D, const DeclarationNameInfo &NameInfo,
270 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000271 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000272 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000273 DecoratedD(D,
274 (Qualifier? HasQualifierFlag : 0) |
275 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
276 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
277 if (Qualifier) {
278 NameQualifier *NQ = getNameQualifier();
279 NQ->NNS = Qualifier;
280 NQ->Range = QualifierRange;
281 }
282
283 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000284 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000285
286 computeDependence();
287}
288
Douglas Gregora2813ce2009-10-23 18:54:35 +0000289DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
290 NestedNameSpecifier *Qualifier,
291 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000292 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000293 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000294 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000295 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000296 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000297 return Create(Context, Qualifier, QualifierRange, D,
298 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000299 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000300}
301
302DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
303 NestedNameSpecifier *Qualifier,
304 SourceRange QualifierRange,
305 ValueDecl *D,
306 const DeclarationNameInfo &NameInfo,
307 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000308 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000310 std::size_t Size = sizeof(DeclRefExpr);
311 if (Qualifier != 0)
312 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000313
John McCalld5532b62009-11-23 01:53:49 +0000314 if (TemplateArgs)
315 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000316
Chris Lattner32488542010-10-30 05:14:06 +0000317 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara25777432010-08-11 22:01:17 +0000318 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCallf89e55a2010-11-18 06:31:45 +0000319 TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000320}
321
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000322DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
323 unsigned NumTemplateArgs) {
324 std::size_t Size = sizeof(DeclRefExpr);
325 if (HasQualifier)
326 Size += sizeof(NameQualifier);
327
328 if (NumTemplateArgs)
329 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
330
Chris Lattner32488542010-10-30 05:14:06 +0000331 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000332 return new (Mem) DeclRefExpr(EmptyShell());
333}
334
Douglas Gregora2813ce2009-10-23 18:54:35 +0000335SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000336 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000337 if (hasQualifier())
338 R.setBegin(getQualifierRange().getBegin());
John McCall096832c2010-08-19 23:49:38 +0000339 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000340 R.setEnd(getRAngleLoc());
341 return R;
342}
343
Anders Carlsson3a082d82009-09-08 18:24:21 +0000344// FIXME: Maybe this should use DeclPrinter with a special "print predefined
345// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000346std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
347 ASTContext &Context = CurrentDecl->getASTContext();
348
Anders Carlsson3a082d82009-09-08 18:24:21 +0000349 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000350 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000351 return FD->getNameAsString();
352
353 llvm::SmallString<256> Name;
354 llvm::raw_svector_ostream Out(Name);
355
356 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000357 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000358 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000359 if (MD->isStatic())
360 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000361 }
362
363 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000364
365 std::string Proto = FD->getQualifiedNameAsString(Policy);
366
John McCall183700f2009-09-21 23:43:11 +0000367 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000368 const FunctionProtoType *FT = 0;
369 if (FD->hasWrittenPrototype())
370 FT = dyn_cast<FunctionProtoType>(AFT);
371
372 Proto += "(";
373 if (FT) {
374 llvm::raw_string_ostream POut(Proto);
375 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
376 if (i) POut << ", ";
377 std::string Param;
378 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
379 POut << Param;
380 }
381
382 if (FT->isVariadic()) {
383 if (FD->getNumParams()) POut << ", ";
384 POut << "...";
385 }
386 }
387 Proto += ")";
388
Sam Weinig4eadcc52009-12-27 01:38:20 +0000389 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
390 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
391 if (ThisQuals.hasConst())
392 Proto += " const";
393 if (ThisQuals.hasVolatile())
394 Proto += " volatile";
395 }
396
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000397 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
398 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399
400 Out << Proto;
401
402 Out.flush();
403 return Name.str().str();
404 }
405 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
406 llvm::SmallString<256> Name;
407 llvm::raw_svector_ostream Out(Name);
408 Out << (MD->isInstanceMethod() ? '-' : '+');
409 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000410
411 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
412 // a null check to avoid a crash.
413 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000414 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000415
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000417 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
418 Out << '(' << CID << ')';
419
Anders Carlsson3a082d82009-09-08 18:24:21 +0000420 Out << ' ';
421 Out << MD->getSelector().getAsString();
422 Out << ']';
423
424 Out.flush();
425 return Name.str().str();
426 }
427 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
428 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
429 return "top level";
430 }
431 return "";
432}
433
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000434void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
435 if (hasAllocation())
436 C.Deallocate(pVal);
437
438 BitWidth = Val.getBitWidth();
439 unsigned NumWords = Val.getNumWords();
440 const uint64_t* Words = Val.getRawData();
441 if (NumWords > 1) {
442 pVal = new (C) uint64_t[NumWords];
443 std::copy(Words, Words + NumWords, pVal);
444 } else if (NumWords == 1)
445 VAL = Words[0];
446 else
447 VAL = 0;
448}
449
450IntegerLiteral *
451IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
452 QualType type, SourceLocation l) {
453 return new (C) IntegerLiteral(C, V, type, l);
454}
455
456IntegerLiteral *
457IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
458 return new (C) IntegerLiteral(Empty);
459}
460
461FloatingLiteral *
462FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
463 bool isexact, QualType Type, SourceLocation L) {
464 return new (C) FloatingLiteral(C, V, isexact, Type, L);
465}
466
467FloatingLiteral *
468FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
469 return new (C) FloatingLiteral(Empty);
470}
471
Chris Lattnerda8249e2008-06-07 22:13:43 +0000472/// getValueAsApproximateDouble - This returns the value as an inaccurate
473/// double. Note that this may cause loss of precision, but is useful for
474/// debugging dumps, etc.
475double FloatingLiteral::getValueAsApproximateDouble() const {
476 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000477 bool ignored;
478 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
479 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000480 return V.convertToDouble();
481}
482
Chris Lattner2085fd62009-02-18 06:40:38 +0000483StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
484 unsigned ByteLength, bool Wide,
485 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000486 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000487 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000488 // Allocate enough space for the StringLiteral plus an array of locations for
489 // any concatenated string tokens.
490 void *Mem = C.Allocate(sizeof(StringLiteral)+
491 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000492 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000493 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000496 char *AStrData = new (C, 1) char[ByteLength];
497 memcpy(AStrData, StrData, ByteLength);
498 SL->StrData = AStrData;
499 SL->ByteLength = ByteLength;
500 SL->IsWide = Wide;
501 SL->TokLocs[0] = Loc[0];
502 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000503
Chris Lattner726e1682009-02-18 05:49:11 +0000504 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000505 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
506 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000507}
508
Douglas Gregor673ecd62009-04-15 16:35:07 +0000509StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
510 void *Mem = C.Allocate(sizeof(StringLiteral)+
511 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000512 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000513 StringLiteral *SL = new (Mem) StringLiteral(QualType());
514 SL->StrData = 0;
515 SL->ByteLength = 0;
516 SL->NumConcatenated = NumStrs;
517 return SL;
518}
519
Daniel Dunbarb6480232009-09-22 03:27:33 +0000520void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000521 char *AStrData = new (C, 1) char[Str.size()];
522 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000523 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000524 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000525}
526
Chris Lattner08f92e32010-11-17 07:37:15 +0000527/// getLocationOfByte - Return a source location that points to the specified
528/// byte of this string literal.
529///
530/// Strings are amazingly complex. They can be formed from multiple tokens and
531/// can have escape sequences in them in addition to the usual trigraph and
532/// escaped newline business. This routine handles this complexity.
533///
534SourceLocation StringLiteral::
535getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
536 const LangOptions &Features, const TargetInfo &Target) const {
537 assert(!isWide() && "This doesn't work for wide strings yet");
538
539 // Loop over all of the tokens in this string until we find the one that
540 // contains the byte we're looking for.
541 unsigned TokNo = 0;
542 while (1) {
543 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
544 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
545
546 // Get the spelling of the string so that we can get the data that makes up
547 // the string literal, not the identifier for the macro it is potentially
548 // expanded through.
549 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
550
551 // Re-lex the token to get its length and original spelling.
552 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
553 bool Invalid = false;
554 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
555 if (Invalid)
556 return StrTokSpellingLoc;
557
558 const char *StrData = Buffer.data()+LocInfo.second;
559
560 // Create a langops struct and enable trigraphs. This is sufficient for
561 // relexing tokens.
562 LangOptions LangOpts;
563 LangOpts.Trigraphs = true;
564
565 // Create a lexer starting at the beginning of this token.
566 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
567 Buffer.end());
568 Token TheTok;
569 TheLexer.LexFromRawLexer(TheTok);
570
571 // Use the StringLiteralParser to compute the length of the string in bytes.
572 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
573 unsigned TokNumBytes = SLP.GetStringLength();
574
575 // If the byte is in this token, return the location of the byte.
576 if (ByteNo < TokNumBytes ||
577 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
578 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
579
580 // Now that we know the offset of the token in the spelling, use the
581 // preprocessor to get the offset in the original source.
582 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
583 }
584
585 // Move to the next string token.
586 ++TokNo;
587 ByteNo -= TokNumBytes;
588 }
589}
590
591
592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
594/// corresponds to, e.g. "sizeof" or "[pre]++".
595const char *UnaryOperator::getOpcodeStr(Opcode Op) {
596 switch (Op) {
597 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000598 case UO_PostInc: return "++";
599 case UO_PostDec: return "--";
600 case UO_PreInc: return "++";
601 case UO_PreDec: return "--";
602 case UO_AddrOf: return "&";
603 case UO_Deref: return "*";
604 case UO_Plus: return "+";
605 case UO_Minus: return "-";
606 case UO_Not: return "~";
607 case UO_LNot: return "!";
608 case UO_Real: return "__real";
609 case UO_Imag: return "__imag";
610 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 }
612}
613
John McCall2de56d12010-08-25 11:45:40 +0000614UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000615UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
616 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000617 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000618 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
619 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
620 case OO_Amp: return UO_AddrOf;
621 case OO_Star: return UO_Deref;
622 case OO_Plus: return UO_Plus;
623 case OO_Minus: return UO_Minus;
624 case OO_Tilde: return UO_Not;
625 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000626 }
627}
628
629OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
630 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000631 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
632 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
633 case UO_AddrOf: return OO_Amp;
634 case UO_Deref: return OO_Star;
635 case UO_Plus: return OO_Plus;
636 case UO_Minus: return OO_Minus;
637 case UO_Not: return OO_Tilde;
638 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000639 default: return OO_None;
640 }
641}
642
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644//===----------------------------------------------------------------------===//
645// Postfix Operators.
646//===----------------------------------------------------------------------===//
647
Ted Kremenek668bf912009-02-09 20:51:47 +0000648CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
John McCallf89e55a2010-11-18 06:31:45 +0000649 unsigned numargs, QualType t, ExprValueKind VK,
650 SourceLocation rparenloc)
651 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000652 fn->isTypeDependent(),
653 fn->isValueDependent(),
654 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000655 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremenek668bf912009-02-09 20:51:47 +0000657 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000658 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000659 for (unsigned i = 0; i != numargs; ++i) {
660 if (args[i]->isTypeDependent())
661 ExprBits.TypeDependent = true;
662 if (args[i]->isValueDependent())
663 ExprBits.ValueDependent = true;
664 if (args[i]->containsUnexpandedParameterPack())
665 ExprBits.ContainsUnexpandedParameterPack = true;
666
Douglas Gregorb4609802008-11-14 16:09:21 +0000667 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000668 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000669
Douglas Gregorb4609802008-11-14 16:09:21 +0000670 RParenLoc = rparenloc;
671}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000672
Ted Kremenek668bf912009-02-09 20:51:47 +0000673CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000674 QualType t, ExprValueKind VK, SourceLocation rparenloc)
675 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000676 fn->isTypeDependent(),
677 fn->isValueDependent(),
678 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000679 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000680
681 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000682 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000683 for (unsigned i = 0; i != numargs; ++i) {
684 if (args[i]->isTypeDependent())
685 ExprBits.TypeDependent = true;
686 if (args[i]->isValueDependent())
687 ExprBits.ValueDependent = true;
688 if (args[i]->containsUnexpandedParameterPack())
689 ExprBits.ContainsUnexpandedParameterPack = true;
690
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000691 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000692 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 RParenLoc = rparenloc;
695}
696
Mike Stump1eb44332009-09-09 15:08:12 +0000697CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
698 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000699 // FIXME: Why do we allocate this?
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000700 SubExprs = new (C) Stmt*[1];
701}
702
Nuno Lopesd20254f2009-12-20 23:11:08 +0000703Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000704 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000705 // If we're calling a dereference, look at the pointer instead.
706 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
707 if (BO->isPtrMemOp())
708 CEE = BO->getRHS()->IgnoreParenCasts();
709 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
710 if (UO->getOpcode() == UO_Deref)
711 CEE = UO->getSubExpr()->IgnoreParenCasts();
712 }
Chris Lattner6346f962009-07-17 15:46:27 +0000713 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000714 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000715 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
716 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000717
718 return 0;
719}
720
Nuno Lopesd20254f2009-12-20 23:11:08 +0000721FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000722 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000723}
724
Chris Lattnerd18b3292007-12-28 05:25:02 +0000725/// setNumArgs - This changes the number of arguments present in this call.
726/// Any orphaned expressions are deleted by this, and any new operands are set
727/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000728void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000729 // No change, just return.
730 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Chris Lattnerd18b3292007-12-28 05:25:02 +0000732 // If shrinking # arguments, just delete the extras and forgot them.
733 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000734 this->NumArgs = NumArgs;
735 return;
736 }
737
738 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000739 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000740 // Copy over args.
741 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
742 NewSubExprs[i] = SubExprs[i];
743 // Null out new args.
744 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
745 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregor88c9a462009-04-17 21:46:47 +0000747 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000748 SubExprs = NewSubExprs;
749 this->NumArgs = NumArgs;
750}
751
Chris Lattnercb888962008-10-06 05:00:53 +0000752/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
753/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000754unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000755 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000756 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000757 // ImplicitCastExpr.
758 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
759 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000760 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000762 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
763 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000764 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Anders Carlssonbcba2012008-01-31 02:13:57 +0000766 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
767 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000768 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000770 if (!FDecl->getIdentifier())
771 return 0;
772
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000773 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000774}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000775
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000776QualType CallExpr::getCallReturnType() const {
777 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000778 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000779 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000780 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000781 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000782 else if (const MemberPointerType *MPT
783 = CalleeType->getAs<MemberPointerType>())
784 CalleeType = MPT->getPointeeType();
785
John McCall183700f2009-09-21 23:43:11 +0000786 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000787 return FnType->getResultType();
788}
Chris Lattnercb888962008-10-06 05:00:53 +0000789
Sean Huntc3021132010-05-05 15:23:54 +0000790OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000791 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000792 TypeSourceInfo *tsi,
793 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000794 Expr** exprsPtr, unsigned numExprs,
795 SourceLocation RParenLoc) {
796 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000797 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000798 sizeof(Expr*) * numExprs);
799
800 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
801 exprsPtr, numExprs, RParenLoc);
802}
803
804OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
805 unsigned numComps, unsigned numExprs) {
806 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
807 sizeof(OffsetOfNode) * numComps +
808 sizeof(Expr*) * numExprs);
809 return new (Mem) OffsetOfExpr(numComps, numExprs);
810}
811
Sean Huntc3021132010-05-05 15:23:54 +0000812OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000813 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000814 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000815 Expr** exprsPtr, unsigned numExprs,
816 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000817 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
818 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000819 /*ValueDependent=*/tsi->getType()->isDependentType(),
820 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000821 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
822 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000823{
824 for(unsigned i = 0; i < numComps; ++i) {
825 setComponent(i, compsPtr[i]);
826 }
Sean Huntc3021132010-05-05 15:23:54 +0000827
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000828 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000829 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
830 ExprBits.ValueDependent = true;
831 if (exprsPtr[i]->containsUnexpandedParameterPack())
832 ExprBits.ContainsUnexpandedParameterPack = true;
833
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000834 setIndexExpr(i, exprsPtr[i]);
835 }
836}
837
838IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
839 assert(getKind() == Field || getKind() == Identifier);
840 if (getKind() == Field)
841 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000842
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000843 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
844}
845
Mike Stump1eb44332009-09-09 15:08:12 +0000846MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
847 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000848 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000849 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000850 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000851 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000852 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000853 QualType ty,
854 ExprValueKind vk,
855 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000856 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000857
John McCall161755a2010-04-06 21:38:20 +0000858 bool hasQualOrFound = (qual != 0 ||
859 founddecl.getDecl() != memberdecl ||
860 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000861 if (hasQualOrFound)
862 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000863
John McCalld5532b62009-11-23 01:53:49 +0000864 if (targs)
865 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner32488542010-10-30 05:14:06 +0000867 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000868 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
869 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000870
871 if (hasQualOrFound) {
872 if (qual && qual->isDependent()) {
873 E->setValueDependent(true);
874 E->setTypeDependent(true);
875 }
876 E->HasQualifierOrFoundDecl = true;
877
878 MemberNameQualifier *NQ = E->getMemberQualifier();
879 NQ->NNS = qual;
880 NQ->Range = qualrange;
881 NQ->FoundDecl = founddecl;
882 }
883
884 if (targs) {
885 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000886 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000887 }
888
889 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000890}
891
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000892const char *CastExpr::getCastKindName() const {
893 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000894 case CK_Dependent:
895 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000896 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000897 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000898 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000899 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000900 case CK_LValueToRValue:
901 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000902 case CK_GetObjCProperty:
903 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000904 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000905 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000906 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000907 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000908 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000909 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000910 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000911 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000912 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000913 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000914 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000915 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000916 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000917 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000918 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000919 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000920 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000921 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000922 case CK_NullToPointer:
923 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000924 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000925 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000926 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000927 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000928 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000929 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000930 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000931 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000932 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000933 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000934 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000935 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000936 case CK_PointerToBoolean:
937 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000938 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000939 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000940 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000941 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000942 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000943 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000944 case CK_IntegralToBoolean:
945 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000946 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000947 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000948 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000949 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000950 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000951 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000952 case CK_FloatingToBoolean:
953 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000954 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000955 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000956 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000957 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000958 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000959 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000960 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000961 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000962 case CK_FloatingRealToComplex:
963 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000964 case CK_FloatingComplexToReal:
965 return "FloatingComplexToReal";
966 case CK_FloatingComplexToBoolean:
967 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000968 case CK_FloatingComplexCast:
969 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000970 case CK_FloatingComplexToIntegralComplex:
971 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000972 case CK_IntegralRealToComplex:
973 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000974 case CK_IntegralComplexToReal:
975 return "IntegralComplexToReal";
976 case CK_IntegralComplexToBoolean:
977 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000978 case CK_IntegralComplexCast:
979 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000980 case CK_IntegralComplexToFloatingComplex:
981 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
John McCall2bb5d002010-11-13 09:02:35 +0000984 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000985 return 0;
986}
987
Douglas Gregor6eef5192009-12-14 19:27:10 +0000988Expr *CastExpr::getSubExprAsWritten() {
989 Expr *SubExpr = 0;
990 CastExpr *E = this;
991 do {
992 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000993
Douglas Gregor6eef5192009-12-14 19:27:10 +0000994 // Skip any temporary bindings; they're implicit.
995 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
996 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000997
Douglas Gregor6eef5192009-12-14 19:27:10 +0000998 // Conversions by constructor and conversion functions have a
999 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001000 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001001 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001002 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001003 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001004
Douglas Gregor6eef5192009-12-14 19:27:10 +00001005 // If the subexpression we're left with is an implicit cast, look
1006 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001007 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1008
Douglas Gregor6eef5192009-12-14 19:27:10 +00001009 return SubExpr;
1010}
1011
John McCallf871d0c2010-08-07 06:22:56 +00001012CXXBaseSpecifier **CastExpr::path_buffer() {
1013 switch (getStmtClass()) {
1014#define ABSTRACT_STMT(x)
1015#define CASTEXPR(Type, Base) \
1016 case Stmt::Type##Class: \
1017 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1018#define STMT(Type, Base)
1019#include "clang/AST/StmtNodes.inc"
1020 default:
1021 llvm_unreachable("non-cast expressions not possible here");
1022 return 0;
1023 }
1024}
1025
1026void CastExpr::setCastPath(const CXXCastPath &Path) {
1027 assert(Path.size() == path_size());
1028 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1029}
1030
1031ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1032 CastKind Kind, Expr *Operand,
1033 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001034 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001035 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1036 void *Buffer =
1037 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1038 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001039 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001040 if (PathSize) E->setCastPath(*BasePath);
1041 return E;
1042}
1043
1044ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1045 unsigned PathSize) {
1046 void *Buffer =
1047 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1048 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1049}
1050
1051
1052CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001053 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001054 const CXXCastPath *BasePath,
1055 TypeSourceInfo *WrittenTy,
1056 SourceLocation L, SourceLocation R) {
1057 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1058 void *Buffer =
1059 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1060 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001061 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001062 if (PathSize) E->setCastPath(*BasePath);
1063 return E;
1064}
1065
1066CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1067 void *Buffer =
1068 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1069 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1070}
1071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1073/// corresponds to, e.g. "<<=".
1074const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1075 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001076 case BO_PtrMemD: return ".*";
1077 case BO_PtrMemI: return "->*";
1078 case BO_Mul: return "*";
1079 case BO_Div: return "/";
1080 case BO_Rem: return "%";
1081 case BO_Add: return "+";
1082 case BO_Sub: return "-";
1083 case BO_Shl: return "<<";
1084 case BO_Shr: return ">>";
1085 case BO_LT: return "<";
1086 case BO_GT: return ">";
1087 case BO_LE: return "<=";
1088 case BO_GE: return ">=";
1089 case BO_EQ: return "==";
1090 case BO_NE: return "!=";
1091 case BO_And: return "&";
1092 case BO_Xor: return "^";
1093 case BO_Or: return "|";
1094 case BO_LAnd: return "&&";
1095 case BO_LOr: return "||";
1096 case BO_Assign: return "=";
1097 case BO_MulAssign: return "*=";
1098 case BO_DivAssign: return "/=";
1099 case BO_RemAssign: return "%=";
1100 case BO_AddAssign: return "+=";
1101 case BO_SubAssign: return "-=";
1102 case BO_ShlAssign: return "<<=";
1103 case BO_ShrAssign: return ">>=";
1104 case BO_AndAssign: return "&=";
1105 case BO_XorAssign: return "^=";
1106 case BO_OrAssign: return "|=";
1107 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001109
1110 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001111}
1112
John McCall2de56d12010-08-25 11:45:40 +00001113BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001114BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1115 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001116 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001117 case OO_Plus: return BO_Add;
1118 case OO_Minus: return BO_Sub;
1119 case OO_Star: return BO_Mul;
1120 case OO_Slash: return BO_Div;
1121 case OO_Percent: return BO_Rem;
1122 case OO_Caret: return BO_Xor;
1123 case OO_Amp: return BO_And;
1124 case OO_Pipe: return BO_Or;
1125 case OO_Equal: return BO_Assign;
1126 case OO_Less: return BO_LT;
1127 case OO_Greater: return BO_GT;
1128 case OO_PlusEqual: return BO_AddAssign;
1129 case OO_MinusEqual: return BO_SubAssign;
1130 case OO_StarEqual: return BO_MulAssign;
1131 case OO_SlashEqual: return BO_DivAssign;
1132 case OO_PercentEqual: return BO_RemAssign;
1133 case OO_CaretEqual: return BO_XorAssign;
1134 case OO_AmpEqual: return BO_AndAssign;
1135 case OO_PipeEqual: return BO_OrAssign;
1136 case OO_LessLess: return BO_Shl;
1137 case OO_GreaterGreater: return BO_Shr;
1138 case OO_LessLessEqual: return BO_ShlAssign;
1139 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1140 case OO_EqualEqual: return BO_EQ;
1141 case OO_ExclaimEqual: return BO_NE;
1142 case OO_LessEqual: return BO_LE;
1143 case OO_GreaterEqual: return BO_GE;
1144 case OO_AmpAmp: return BO_LAnd;
1145 case OO_PipePipe: return BO_LOr;
1146 case OO_Comma: return BO_Comma;
1147 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001148 }
1149}
1150
1151OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1152 static const OverloadedOperatorKind OverOps[] = {
1153 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1154 OO_Star, OO_Slash, OO_Percent,
1155 OO_Plus, OO_Minus,
1156 OO_LessLess, OO_GreaterGreater,
1157 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1158 OO_EqualEqual, OO_ExclaimEqual,
1159 OO_Amp,
1160 OO_Caret,
1161 OO_Pipe,
1162 OO_AmpAmp,
1163 OO_PipePipe,
1164 OO_Equal, OO_StarEqual,
1165 OO_SlashEqual, OO_PercentEqual,
1166 OO_PlusEqual, OO_MinusEqual,
1167 OO_LessLessEqual, OO_GreaterGreaterEqual,
1168 OO_AmpEqual, OO_CaretEqual,
1169 OO_PipeEqual,
1170 OO_Comma
1171 };
1172 return OverOps[Opc];
1173}
1174
Ted Kremenek709210f2010-04-13 23:39:13 +00001175InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001176 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001177 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001178 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1179 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001180 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001181 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001182 UnionFieldInit(0), HadArrayRangeDesignator(false)
1183{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001184 for (unsigned I = 0; I != numInits; ++I) {
1185 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001186 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001187 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001188 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001189 if (initExprs[I]->containsUnexpandedParameterPack())
1190 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001191 }
Sean Huntc3021132010-05-05 15:23:54 +00001192
Ted Kremenek709210f2010-04-13 23:39:13 +00001193 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001194}
Reid Spencer5f016e22007-07-11 17:01:13 +00001195
Ted Kremenek709210f2010-04-13 23:39:13 +00001196void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001197 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001198 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001199}
1200
Ted Kremenek709210f2010-04-13 23:39:13 +00001201void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001202 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001203}
1204
Ted Kremenek709210f2010-04-13 23:39:13 +00001205Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001206 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001207 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001208 InitExprs.back() = expr;
1209 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Douglas Gregor4c678342009-01-28 21:54:33 +00001212 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1213 InitExprs[Init] = expr;
1214 return Result;
1215}
1216
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001217SourceRange InitListExpr::getSourceRange() const {
1218 if (SyntacticForm)
1219 return SyntacticForm->getSourceRange();
1220 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1221 if (Beg.isInvalid()) {
1222 // Find the first non-null initializer.
1223 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1224 E = InitExprs.end();
1225 I != E; ++I) {
1226 if (Stmt *S = *I) {
1227 Beg = S->getLocStart();
1228 break;
1229 }
1230 }
1231 }
1232 if (End.isInvalid()) {
1233 // Find the first non-null initializer from the end.
1234 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1235 E = InitExprs.rend();
1236 I != E; ++I) {
1237 if (Stmt *S = *I) {
1238 End = S->getSourceRange().getEnd();
1239 break;
1240 }
1241 }
1242 }
1243 return SourceRange(Beg, End);
1244}
1245
Steve Naroffbfdcae62008-09-04 15:31:07 +00001246/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001247///
1248const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001249 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001250 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001251}
1252
Mike Stump1eb44332009-09-09 15:08:12 +00001253SourceLocation BlockExpr::getCaretLocation() const {
1254 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001255}
Mike Stump1eb44332009-09-09 15:08:12 +00001256const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001257 return TheBlock->getBody();
1258}
Mike Stump1eb44332009-09-09 15:08:12 +00001259Stmt *BlockExpr::getBody() {
1260 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001261}
Steve Naroff56ee6892008-10-08 17:01:13 +00001262
1263
Reid Spencer5f016e22007-07-11 17:01:13 +00001264//===----------------------------------------------------------------------===//
1265// Generic Expression Routines
1266//===----------------------------------------------------------------------===//
1267
Chris Lattner026dc962009-02-14 07:37:35 +00001268/// isUnusedResultAWarning - Return true if this immediate expression should
1269/// be warned about if the result is unused. If so, fill in Loc and Ranges
1270/// with location to warn on and the source range[s] to report with the
1271/// warning.
1272bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001273 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001274 // Don't warn if the expr is type dependent. The type could end up
1275 // instantiating to void.
1276 if (isTypeDependent())
1277 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 switch (getStmtClass()) {
1280 default:
John McCall0faede62010-03-12 07:11:26 +00001281 if (getType()->isVoidType())
1282 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001283 Loc = getExprLoc();
1284 R1 = getSourceRange();
1285 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001287 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001288 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 case UnaryOperatorClass: {
1290 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001293 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001294 case UO_PostInc:
1295 case UO_PostDec:
1296 case UO_PreInc:
1297 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001298 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001299 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001301 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001302 return false;
1303 break;
John McCall2de56d12010-08-25 11:45:40 +00001304 case UO_Real:
1305 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001307 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1308 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001309 return false;
1310 break;
John McCall2de56d12010-08-25 11:45:40 +00001311 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001312 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 }
Chris Lattner026dc962009-02-14 07:37:35 +00001314 Loc = UO->getOperatorLoc();
1315 R1 = UO->getSubExpr()->getSourceRange();
1316 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001318 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001319 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001320 switch (BO->getOpcode()) {
1321 default:
1322 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001323 // Consider the RHS of comma for side effects. LHS was checked by
1324 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001325 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001326 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1327 // lvalue-ness) of an assignment written in a macro.
1328 if (IntegerLiteral *IE =
1329 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1330 if (IE->getValue() == 0)
1331 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001332 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1333 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001334 case BO_LAnd:
1335 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001336 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1337 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1338 return false;
1339 break;
John McCallbf0ee352010-02-16 04:10:53 +00001340 }
Chris Lattner026dc962009-02-14 07:37:35 +00001341 if (BO->isAssignmentOp())
1342 return false;
1343 Loc = BO->getOperatorLoc();
1344 R1 = BO->getLHS()->getSourceRange();
1345 R2 = BO->getRHS()->getSourceRange();
1346 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001347 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001348 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001349 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001350 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001352 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001353 // The condition must be evaluated, but if either the LHS or RHS is a
1354 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001355 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001356 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001357 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001358 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001359 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001360 }
1361
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001363 // If the base pointer or element is to a volatile pointer/field, accessing
1364 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001365 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001366 return false;
1367 Loc = cast<MemberExpr>(this)->getMemberLoc();
1368 R1 = SourceRange(Loc, Loc);
1369 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1370 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 case ArraySubscriptExprClass:
1373 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001374 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001375 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001376 return false;
1377 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1378 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1379 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1380 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001381
Reid Spencer5f016e22007-07-11 17:01:13 +00001382 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001383 case CXXOperatorCallExprClass:
1384 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001385 // If this is a direct call, get the callee.
1386 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001387 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001388 // If the callee has attribute pure, const, or warn_unused_result, warn
1389 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001390 //
1391 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1392 // updated to match for QoI.
1393 if (FD->getAttr<WarnUnusedResultAttr>() ||
1394 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1395 Loc = CE->getCallee()->getLocStart();
1396 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001398 if (unsigned NumArgs = CE->getNumArgs())
1399 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1400 CE->getArg(NumArgs-1)->getLocEnd());
1401 return true;
1402 }
Chris Lattner026dc962009-02-14 07:37:35 +00001403 }
1404 return false;
1405 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001406
1407 case CXXTemporaryObjectExprClass:
1408 case CXXConstructExprClass:
1409 return false;
1410
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001411 case ObjCMessageExprClass: {
1412 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1413 const ObjCMethodDecl *MD = ME->getMethodDecl();
1414 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1415 Loc = getExprLoc();
1416 return true;
1417 }
Chris Lattner026dc962009-02-14 07:37:35 +00001418 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001419 }
Mike Stump1eb44332009-09-09 15:08:12 +00001420
John McCall12f78a62010-12-02 01:19:52 +00001421 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001422 Loc = getExprLoc();
1423 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001424 return true;
John McCall12f78a62010-12-02 01:19:52 +00001425
Chris Lattner611b2ec2008-07-26 19:51:01 +00001426 case StmtExprClass: {
1427 // Statement exprs don't logically have side effects themselves, but are
1428 // sometimes used in macros in ways that give them a type that is unused.
1429 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1430 // however, if the result of the stmt expr is dead, we don't want to emit a
1431 // warning.
1432 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001433 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001434 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001435 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001436 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1437 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1438 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
John McCall0faede62010-03-12 07:11:26 +00001441 if (getType()->isVoidType())
1442 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001443 Loc = cast<StmtExpr>(this)->getLParenLoc();
1444 R1 = getSourceRange();
1445 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001446 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001447 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001448 // If this is an explicit cast to void, allow it. People do this when they
1449 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001450 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001451 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001452 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1453 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1454 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001455 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001456 if (getType()->isVoidType())
1457 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001458 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001459
Anders Carlsson58beed92009-11-17 17:11:23 +00001460 // If this is a cast to void or a constructor conversion, check the operand.
1461 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001462 if (CE->getCastKind() == CK_ToVoid ||
1463 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001464 return (cast<CastExpr>(this)->getSubExpr()
1465 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001466 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1467 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1468 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001469 }
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Eli Friedman4be1f472008-05-19 21:24:43 +00001471 case ImplicitCastExprClass:
1472 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001473 return (cast<ImplicitCastExpr>(this)
1474 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001475
Chris Lattner04421082008-04-08 04:40:51 +00001476 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001477 return (cast<CXXDefaultArgExpr>(this)
1478 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001479
1480 case CXXNewExprClass:
1481 // FIXME: In theory, there might be new expressions that don't have side
1482 // effects (e.g. a placement new with an uninitialized POD).
1483 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001484 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001485 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001486 return (cast<CXXBindTemporaryExpr>(this)
1487 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001488 case ExprWithCleanupsClass:
1489 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001490 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001491 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001492}
1493
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001494/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001495/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001496bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001497 switch (getStmtClass()) {
1498 default:
1499 return false;
1500 case ObjCIvarRefExprClass:
1501 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001502 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001503 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001504 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001505 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001506 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001507 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001508 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001509 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001510 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001511 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001512 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1513 if (VD->hasGlobalStorage())
1514 return true;
1515 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001516 // dereferencing to a pointer is always a gc'able candidate,
1517 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001518 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001519 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001520 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001521 return false;
1522 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001523 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001524 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001525 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001526 }
1527 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001528 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001529 }
1530}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001531
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001532bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1533 if (isTypeDependent())
1534 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001535 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001536}
1537
Sebastian Redl369e51f2010-09-10 20:55:33 +00001538static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1539 Expr::CanThrowResult CT2) {
1540 // CanThrowResult constants are ordered so that the maximum is the correct
1541 // merge result.
1542 return CT1 > CT2 ? CT1 : CT2;
1543}
1544
1545static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1546 Expr *E = const_cast<Expr*>(CE);
1547 Expr::CanThrowResult R = Expr::CT_Cannot;
1548 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1549 I != IE && R != Expr::CT_Can; ++I) {
1550 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1551 }
1552 return R;
1553}
1554
1555static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1556 bool NullThrows = true) {
1557 if (!D)
1558 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1559
1560 // See if we can get a function type from the decl somehow.
1561 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1562 if (!VD) // If we have no clue what we're calling, assume the worst.
1563 return Expr::CT_Can;
1564
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001565 // As an extension, we assume that __attribute__((nothrow)) functions don't
1566 // throw.
1567 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1568 return Expr::CT_Cannot;
1569
Sebastian Redl369e51f2010-09-10 20:55:33 +00001570 QualType T = VD->getType();
1571 const FunctionProtoType *FT;
1572 if ((FT = T->getAs<FunctionProtoType>())) {
1573 } else if (const PointerType *PT = T->getAs<PointerType>())
1574 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1575 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1576 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1577 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1578 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1579 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1580 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1581
1582 if (!FT)
1583 return Expr::CT_Can;
1584
1585 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1586}
1587
1588static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1589 if (DC->isTypeDependent())
1590 return Expr::CT_Dependent;
1591
Sebastian Redl295995c2010-09-10 20:55:47 +00001592 if (!DC->getTypeAsWritten()->isReferenceType())
1593 return Expr::CT_Cannot;
1594
Sebastian Redl369e51f2010-09-10 20:55:33 +00001595 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1596}
1597
1598static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1599 const CXXTypeidExpr *DC) {
1600 if (DC->isTypeOperand())
1601 return Expr::CT_Cannot;
1602
1603 Expr *Op = DC->getExprOperand();
1604 if (Op->isTypeDependent())
1605 return Expr::CT_Dependent;
1606
1607 const RecordType *RT = Op->getType()->getAs<RecordType>();
1608 if (!RT)
1609 return Expr::CT_Cannot;
1610
1611 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1612 return Expr::CT_Cannot;
1613
1614 if (Op->Classify(C).isPRValue())
1615 return Expr::CT_Cannot;
1616
1617 return Expr::CT_Can;
1618}
1619
1620Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1621 // C++ [expr.unary.noexcept]p3:
1622 // [Can throw] if in a potentially-evaluated context the expression would
1623 // contain:
1624 switch (getStmtClass()) {
1625 case CXXThrowExprClass:
1626 // - a potentially evaluated throw-expression
1627 return CT_Can;
1628
1629 case CXXDynamicCastExprClass: {
1630 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1631 // where T is a reference type, that requires a run-time check
1632 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1633 if (CT == CT_Can)
1634 return CT;
1635 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1636 }
1637
1638 case CXXTypeidExprClass:
1639 // - a potentially evaluated typeid expression applied to a glvalue
1640 // expression whose type is a polymorphic class type
1641 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1642
1643 // - a potentially evaluated call to a function, member function, function
1644 // pointer, or member function pointer that does not have a non-throwing
1645 // exception-specification
1646 case CallExprClass:
1647 case CXXOperatorCallExprClass:
1648 case CXXMemberCallExprClass: {
1649 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1650 if (CT == CT_Can)
1651 return CT;
1652 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1653 }
1654
Sebastian Redl295995c2010-09-10 20:55:47 +00001655 case CXXConstructExprClass:
1656 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001657 CanThrowResult CT = CanCalleeThrow(
1658 cast<CXXConstructExpr>(this)->getConstructor());
1659 if (CT == CT_Can)
1660 return CT;
1661 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1662 }
1663
1664 case CXXNewExprClass: {
1665 CanThrowResult CT = MergeCanThrow(
1666 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1667 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1668 /*NullThrows*/false));
1669 if (CT == CT_Can)
1670 return CT;
1671 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1672 }
1673
1674 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001675 CanThrowResult CT = CanCalleeThrow(
1676 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1677 if (CT == CT_Can)
1678 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001679 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1680 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1681 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1682 Arg = Cast->getSubExpr();
1683 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1684 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1685 CanThrowResult CT2 = CanCalleeThrow(
1686 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1687 if (CT2 == CT_Can)
1688 return CT2;
1689 CT = MergeCanThrow(CT, CT2);
1690 }
1691 }
1692 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1693 }
1694
1695 case CXXBindTemporaryExprClass: {
1696 // The bound temporary has to be destroyed again, which might throw.
1697 CanThrowResult CT = CanCalleeThrow(
1698 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1699 if (CT == CT_Can)
1700 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001701 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1702 }
1703
1704 // ObjC message sends are like function calls, but never have exception
1705 // specs.
1706 case ObjCMessageExprClass:
1707 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001708 return CT_Can;
1709
1710 // Many other things have subexpressions, so we have to test those.
1711 // Some are simple:
1712 case ParenExprClass:
1713 case MemberExprClass:
1714 case CXXReinterpretCastExprClass:
1715 case CXXConstCastExprClass:
1716 case ConditionalOperatorClass:
1717 case CompoundLiteralExprClass:
1718 case ExtVectorElementExprClass:
1719 case InitListExprClass:
1720 case DesignatedInitExprClass:
1721 case ParenListExprClass:
1722 case VAArgExprClass:
1723 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001724 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001725 case ObjCIvarRefExprClass:
1726 case ObjCIsaExprClass:
1727 case ShuffleVectorExprClass:
1728 return CanSubExprsThrow(C, this);
1729
1730 // Some might be dependent for other reasons.
1731 case UnaryOperatorClass:
1732 case ArraySubscriptExprClass:
1733 case ImplicitCastExprClass:
1734 case CStyleCastExprClass:
1735 case CXXStaticCastExprClass:
1736 case CXXFunctionalCastExprClass:
1737 case BinaryOperatorClass:
1738 case CompoundAssignOperatorClass: {
1739 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1740 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1741 }
1742
1743 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1744 case StmtExprClass:
1745 return CT_Can;
1746
1747 case ChooseExprClass:
1748 if (isTypeDependent() || isValueDependent())
1749 return CT_Dependent;
1750 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1751
1752 // Some expressions are always dependent.
1753 case DependentScopeDeclRefExprClass:
1754 case CXXUnresolvedConstructExprClass:
1755 case CXXDependentScopeMemberExprClass:
1756 return CT_Dependent;
1757
1758 default:
1759 // All other expressions don't have subexpressions, or else they are
1760 // unevaluated.
1761 return CT_Cannot;
1762 }
1763}
1764
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001765Expr* Expr::IgnoreParens() {
1766 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001767 while (true) {
1768 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1769 E = P->getSubExpr();
1770 continue;
1771 }
1772 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1773 if (P->getOpcode() == UO_Extension) {
1774 E = P->getSubExpr();
1775 continue;
1776 }
1777 }
1778 return E;
1779 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001780}
1781
Chris Lattner56f34942008-02-13 01:02:39 +00001782/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1783/// or CastExprs or ImplicitCastExprs, returning their operand.
1784Expr *Expr::IgnoreParenCasts() {
1785 Expr *E = this;
1786 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001787 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001788 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001789 continue;
1790 }
1791 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001792 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001793 continue;
1794 }
1795 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1796 if (P->getOpcode() == UO_Extension) {
1797 E = P->getSubExpr();
1798 continue;
1799 }
1800 }
1801 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001802 }
1803}
1804
John McCall9c5d70c2010-12-04 08:24:19 +00001805/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1806/// casts. This is intended purely as a temporary workaround for code
1807/// that hasn't yet been rewritten to do the right thing about those
1808/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001809Expr *Expr::IgnoreParenLValueCasts() {
1810 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001811 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001812 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1813 E = P->getSubExpr();
1814 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001815 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001816 if (P->getCastKind() == CK_LValueToRValue) {
1817 E = P->getSubExpr();
1818 continue;
1819 }
John McCall9c5d70c2010-12-04 08:24:19 +00001820 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1821 if (P->getOpcode() == UO_Extension) {
1822 E = P->getSubExpr();
1823 continue;
1824 }
John McCallf6a16482010-12-04 03:47:34 +00001825 }
1826 break;
1827 }
1828 return E;
1829}
1830
John McCall2fc46bf2010-05-05 22:59:52 +00001831Expr *Expr::IgnoreParenImpCasts() {
1832 Expr *E = this;
1833 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001834 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001835 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001836 continue;
1837 }
1838 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001839 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001840 continue;
1841 }
1842 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1843 if (P->getOpcode() == UO_Extension) {
1844 E = P->getSubExpr();
1845 continue;
1846 }
1847 }
1848 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001849 }
1850}
1851
Chris Lattnerecdd8412009-03-13 17:28:01 +00001852/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1853/// value (including ptr->int casts of the same size). Strip off any
1854/// ParenExpr or CastExprs, returning their operand.
1855Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1856 Expr *E = this;
1857 while (true) {
1858 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1859 E = P->getSubExpr();
1860 continue;
1861 }
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Chris Lattnerecdd8412009-03-13 17:28:01 +00001863 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1864 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001865 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001866 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Chris Lattnerecdd8412009-03-13 17:28:01 +00001868 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1869 E = SE;
1870 continue;
1871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001873 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001874 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001875 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001876 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001877 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1878 E = SE;
1879 continue;
1880 }
1881 }
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001883 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1884 if (P->getOpcode() == UO_Extension) {
1885 E = P->getSubExpr();
1886 continue;
1887 }
1888 }
1889
Chris Lattnerecdd8412009-03-13 17:28:01 +00001890 return E;
1891 }
1892}
1893
Douglas Gregor6eef5192009-12-14 19:27:10 +00001894bool Expr::isDefaultArgument() const {
1895 const Expr *E = this;
1896 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1897 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001898
Douglas Gregor6eef5192009-12-14 19:27:10 +00001899 return isa<CXXDefaultArgExpr>(E);
1900}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001901
Douglas Gregor2f599792010-04-02 18:24:57 +00001902/// \brief Skip over any no-op casts and any temporary-binding
1903/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001904static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001905 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001906 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001907 E = ICE->getSubExpr();
1908 else
1909 break;
1910 }
1911
1912 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1913 E = BE->getSubExpr();
1914
1915 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001916 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001917 E = ICE->getSubExpr();
1918 else
1919 break;
1920 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001921
1922 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001923}
1924
John McCall558d2ab2010-09-15 10:14:12 +00001925/// isTemporaryObject - Determines if this expression produces a
1926/// temporary of the given class type.
1927bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1928 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1929 return false;
1930
Anders Carlssonf8b30152010-11-28 16:40:49 +00001931 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001932
John McCall58277b52010-09-15 20:59:13 +00001933 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001934 if (!E->Classify(C).isPRValue()) {
1935 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001936 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001937 return false;
1938 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001939
John McCall19e60ad2010-09-16 06:57:56 +00001940 // Black-list a few cases which yield pr-values of class type that don't
1941 // refer to temporaries of that type:
1942
1943 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001944 if (isa<ImplicitCastExpr>(E)) {
1945 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1946 case CK_DerivedToBase:
1947 case CK_UncheckedDerivedToBase:
1948 return false;
1949 default:
1950 break;
1951 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001952 }
1953
John McCall19e60ad2010-09-16 06:57:56 +00001954 // - member expressions (all)
1955 if (isa<MemberExpr>(E))
1956 return false;
1957
John McCall558d2ab2010-09-15 10:14:12 +00001958 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001959}
1960
Douglas Gregor898574e2008-12-05 23:32:09 +00001961/// hasAnyTypeDependentArguments - Determines if any of the expressions
1962/// in Exprs is type-dependent.
1963bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1964 for (unsigned I = 0; I < NumExprs; ++I)
1965 if (Exprs[I]->isTypeDependent())
1966 return true;
1967
1968 return false;
1969}
1970
1971/// hasAnyValueDependentArguments - Determines if any of the expressions
1972/// in Exprs is value-dependent.
1973bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1974 for (unsigned I = 0; I < NumExprs; ++I)
1975 if (Exprs[I]->isValueDependent())
1976 return true;
1977
1978 return false;
1979}
1980
John McCall4204f072010-08-02 21:13:48 +00001981bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001982 // This function is attempting whether an expression is an initializer
1983 // which can be evaluated at compile-time. isEvaluatable handles most
1984 // of the cases, but it can't deal with some initializer-specific
1985 // expressions, and it can't deal with aggregates; we deal with those here,
1986 // and fall back to isEvaluatable for the other cases.
1987
John McCall4204f072010-08-02 21:13:48 +00001988 // If we ever capture reference-binding directly in the AST, we can
1989 // kill the second parameter.
1990
1991 if (IsForRef) {
1992 EvalResult Result;
1993 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1994 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001995
Anders Carlssone8a32b82008-11-24 05:23:59 +00001996 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001997 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001998 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001999 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002000 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002001 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002002 case CXXTemporaryObjectExprClass:
2003 case CXXConstructExprClass: {
2004 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002005
2006 // Only if it's
2007 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002008 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002009 if (!CE->getNumArgs()) return true;
2010
2011 // 2) an elidable trivial copy construction of an operand which is
2012 // itself a constant initializer. Note that we consider the
2013 // operand on its own, *not* as a reference binding.
2014 return CE->isElidable() &&
2015 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002016 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002017 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002018 // This handles gcc's extension that allows global initializers like
2019 // "struct x {int x;} x = (struct x) {};".
2020 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002021 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002022 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002023 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002024 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002025 // FIXME: This doesn't deal with fields with reference types correctly.
2026 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2027 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002028 const InitListExpr *Exp = cast<InitListExpr>(this);
2029 unsigned numInits = Exp->getNumInits();
2030 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002031 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002032 return false;
2033 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002034 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002035 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002036 case ImplicitValueInitExprClass:
2037 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002038 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002039 return cast<ParenExpr>(this)->getSubExpr()
2040 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002041 case ChooseExprClass:
2042 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2043 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002044 case UnaryOperatorClass: {
2045 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002046 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002047 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002048 break;
2049 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002050 case BinaryOperatorClass: {
2051 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2052 // but this handles the common case.
2053 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002054 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002055 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2056 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2057 return true;
2058 break;
2059 }
John McCall4204f072010-08-02 21:13:48 +00002060 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002061 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002062 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002063 case CStyleCastExprClass:
2064 // Handle casts with a destination that's a struct or union; this
2065 // deals with both the gcc no-op struct cast extension and the
2066 // cast-to-union extension.
2067 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002068 return cast<CastExpr>(this)->getSubExpr()
2069 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002070
Chris Lattner430656e2009-10-13 22:12:09 +00002071 // Integer->integer casts can be handled here, which is important for
2072 // things like (int)(&&x-&&y). Scary but true.
2073 if (getType()->isIntegerType() &&
2074 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002075 return cast<CastExpr>(this)->getSubExpr()
2076 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002077
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002078 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002079 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002080 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002081}
2082
Reid Spencer5f016e22007-07-11 17:01:13 +00002083/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2084/// integer constant expression with the value zero, or if this is one that is
2085/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002086bool Expr::isNullPointerConstant(ASTContext &Ctx,
2087 NullPointerConstantValueDependence NPC) const {
2088 if (isValueDependent()) {
2089 switch (NPC) {
2090 case NPC_NeverValueDependent:
2091 assert(false && "Unexpected value dependent expression!");
2092 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002093
Douglas Gregorce940492009-09-25 04:25:58 +00002094 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002095 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002096
Douglas Gregorce940492009-09-25 04:25:58 +00002097 case NPC_ValueDependentIsNotNull:
2098 return false;
2099 }
2100 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002101
Sebastian Redl07779722008-10-31 14:43:28 +00002102 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002103 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002104 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002105 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002106 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002107 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002108 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002109 Pointee->isVoidType() && // to void*
2110 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002111 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002112 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002114 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2115 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002116 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002117 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2118 // Accept ((void*)0) as a null pointer constant, as many other
2119 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002120 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002121 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002122 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002123 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002124 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002125 } else if (isa<GNUNullExpr>(this)) {
2126 // The GNU __null extension is always a null pointer constant.
2127 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002128 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002129
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002130 // C++0x nullptr_t is always a null pointer constant.
2131 if (getType()->isNullPtrType())
2132 return true;
2133
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002134 if (const RecordType *UT = getType()->getAsUnionType())
2135 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2136 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2137 const Expr *InitExpr = CLE->getInitializer();
2138 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2139 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2140 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002141 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002142 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002143 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002144 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Reid Spencer5f016e22007-07-11 17:01:13 +00002146 // If we have an integer constant expression, we need to *evaluate* it and
2147 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002148 llvm::APSInt Result;
2149 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002150}
Steve Naroff31a45842007-07-28 23:10:27 +00002151
John McCallf6a16482010-12-04 03:47:34 +00002152/// \brief If this expression is an l-value for an Objective C
2153/// property, find the underlying property reference expression.
2154const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2155 const Expr *E = this;
2156 while (true) {
2157 assert((E->getValueKind() == VK_LValue &&
2158 E->getObjectKind() == OK_ObjCProperty) &&
2159 "expression is not a property reference");
2160 E = E->IgnoreParenCasts();
2161 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2162 if (BO->getOpcode() == BO_Comma) {
2163 E = BO->getRHS();
2164 continue;
2165 }
2166 }
2167
2168 break;
2169 }
2170
2171 return cast<ObjCPropertyRefExpr>(E);
2172}
2173
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002174FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002175 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002176
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002177 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002178 if (ICE->getCastKind() == CK_LValueToRValue ||
2179 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002180 E = ICE->getSubExpr()->IgnoreParens();
2181 else
2182 break;
2183 }
2184
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002185 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002186 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002187 if (Field->isBitField())
2188 return Field;
2189
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002190 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2191 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2192 if (Field->isBitField())
2193 return Field;
2194
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002195 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2196 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2197 return BinOp->getLHS()->getBitField();
2198
2199 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002200}
2201
Anders Carlsson09380262010-01-31 17:18:49 +00002202bool Expr::refersToVectorElement() const {
2203 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002204
Anders Carlsson09380262010-01-31 17:18:49 +00002205 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002206 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002207 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002208 E = ICE->getSubExpr()->IgnoreParens();
2209 else
2210 break;
2211 }
Sean Huntc3021132010-05-05 15:23:54 +00002212
Anders Carlsson09380262010-01-31 17:18:49 +00002213 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2214 return ASE->getBase()->getType()->isVectorType();
2215
2216 if (isa<ExtVectorElementExpr>(E))
2217 return true;
2218
2219 return false;
2220}
2221
Chris Lattner2140e902009-02-16 22:14:05 +00002222/// isArrow - Return true if the base expression is a pointer to vector,
2223/// return false if the base expression is a vector.
2224bool ExtVectorElementExpr::isArrow() const {
2225 return getBase()->getType()->isPointerType();
2226}
2227
Nate Begeman213541a2008-04-18 23:10:10 +00002228unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002229 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002230 return VT->getNumElements();
2231 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002232}
2233
Nate Begeman8a997642008-05-09 06:41:27 +00002234/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002235bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002236 // FIXME: Refactor this code to an accessor on the AST node which returns the
2237 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002238 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002239
2240 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002241 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002242 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Nate Begeman190d6a22009-01-18 02:01:21 +00002244 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002245 if (Comp[0] == 's' || Comp[0] == 'S')
2246 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Daniel Dunbar15027422009-10-17 23:53:04 +00002248 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2249 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002250 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002251
Steve Narofffec0b492007-07-30 03:29:09 +00002252 return false;
2253}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002254
Nate Begeman8a997642008-05-09 06:41:27 +00002255/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002256void ExtVectorElementExpr::getEncodedElementAccess(
2257 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002258 llvm::StringRef Comp = Accessor->getName();
2259 if (Comp[0] == 's' || Comp[0] == 'S')
2260 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002262 bool isHi = Comp == "hi";
2263 bool isLo = Comp == "lo";
2264 bool isEven = Comp == "even";
2265 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Nate Begeman8a997642008-05-09 06:41:27 +00002267 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2268 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Nate Begeman8a997642008-05-09 06:41:27 +00002270 if (isHi)
2271 Index = e + i;
2272 else if (isLo)
2273 Index = i;
2274 else if (isEven)
2275 Index = 2 * i;
2276 else if (isOdd)
2277 Index = 2 * i + 1;
2278 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002279 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002280
Nate Begeman3b8d1162008-05-13 21:03:02 +00002281 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002282 }
Nate Begeman8a997642008-05-09 06:41:27 +00002283}
2284
Douglas Gregor04badcf2010-04-21 00:45:42 +00002285ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002286 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002287 SourceLocation LBracLoc,
2288 SourceLocation SuperLoc,
2289 bool IsInstanceSuper,
2290 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002291 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002292 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002293 ObjCMethodDecl *Method,
2294 Expr **Args, unsigned NumArgs,
2295 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002296 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002297 /*TypeDependent=*/false, /*ValueDependent=*/false,
2298 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002299 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2300 HasMethod(Method != 0), SuperLoc(SuperLoc),
2301 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2302 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002303 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002304{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002305 setReceiverPointer(SuperType.getAsOpaquePtr());
2306 if (NumArgs)
2307 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002308}
2309
Douglas Gregor04badcf2010-04-21 00:45:42 +00002310ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002311 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002312 SourceLocation LBracLoc,
2313 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002314 Selector Sel,
2315 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002316 ObjCMethodDecl *Method,
2317 Expr **Args, unsigned NumArgs,
2318 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002319 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002320 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002321 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2322 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2323 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002324 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002325{
2326 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002327 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002328 for (unsigned I = 0; I != NumArgs; ++I) {
2329 if (Args[I]->isTypeDependent())
2330 ExprBits.TypeDependent = true;
2331 if (Args[I]->isValueDependent())
2332 ExprBits.ValueDependent = true;
2333 if (Args[I]->containsUnexpandedParameterPack())
2334 ExprBits.ContainsUnexpandedParameterPack = true;
2335
2336 MyArgs[I] = Args[I];
2337 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002338}
2339
Douglas Gregor04badcf2010-04-21 00:45:42 +00002340ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002341 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002342 SourceLocation LBracLoc,
2343 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002344 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002345 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002346 ObjCMethodDecl *Method,
2347 Expr **Args, unsigned NumArgs,
2348 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002349 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002350 Receiver->isTypeDependent(),
2351 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002352 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2353 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2354 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002355 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002356{
2357 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002358 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002359 for (unsigned I = 0; I != NumArgs; ++I) {
2360 if (Args[I]->isTypeDependent())
2361 ExprBits.TypeDependent = true;
2362 if (Args[I]->isValueDependent())
2363 ExprBits.ValueDependent = true;
2364 if (Args[I]->containsUnexpandedParameterPack())
2365 ExprBits.ContainsUnexpandedParameterPack = true;
2366
2367 MyArgs[I] = Args[I];
2368 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002369}
2370
Douglas Gregor04badcf2010-04-21 00:45:42 +00002371ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002372 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002373 SourceLocation LBracLoc,
2374 SourceLocation SuperLoc,
2375 bool IsInstanceSuper,
2376 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002377 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002378 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002379 ObjCMethodDecl *Method,
2380 Expr **Args, unsigned NumArgs,
2381 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002382 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002383 NumArgs * sizeof(Expr *);
2384 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002385 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002386 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002387 RBracLoc);
2388}
2389
2390ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002391 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002392 SourceLocation LBracLoc,
2393 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002394 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002395 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002396 ObjCMethodDecl *Method,
2397 Expr **Args, unsigned NumArgs,
2398 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002399 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002400 NumArgs * sizeof(Expr *);
2401 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002402 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2403 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002404}
2405
2406ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002407 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002408 SourceLocation LBracLoc,
2409 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002410 Selector Sel,
2411 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002412 ObjCMethodDecl *Method,
2413 Expr **Args, unsigned NumArgs,
2414 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002415 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002416 NumArgs * sizeof(Expr *);
2417 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002418 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2419 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002420}
2421
Sean Huntc3021132010-05-05 15:23:54 +00002422ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002423 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002424 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002425 NumArgs * sizeof(Expr *);
2426 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2427 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2428}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002429
2430SourceRange ObjCMessageExpr::getReceiverRange() const {
2431 switch (getReceiverKind()) {
2432 case Instance:
2433 return getInstanceReceiver()->getSourceRange();
2434
2435 case Class:
2436 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2437
2438 case SuperInstance:
2439 case SuperClass:
2440 return getSuperLoc();
2441 }
2442
2443 return SourceLocation();
2444}
2445
Douglas Gregor04badcf2010-04-21 00:45:42 +00002446Selector ObjCMessageExpr::getSelector() const {
2447 if (HasMethod)
2448 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2449 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002450 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002451}
2452
2453ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2454 switch (getReceiverKind()) {
2455 case Instance:
2456 if (const ObjCObjectPointerType *Ptr
2457 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2458 return Ptr->getInterfaceDecl();
2459 break;
2460
2461 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002462 if (const ObjCObjectType *Ty
2463 = getClassReceiver()->getAs<ObjCObjectType>())
2464 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002465 break;
2466
2467 case SuperInstance:
2468 if (const ObjCObjectPointerType *Ptr
2469 = getSuperType()->getAs<ObjCObjectPointerType>())
2470 return Ptr->getInterfaceDecl();
2471 break;
2472
2473 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002474 if (const ObjCObjectType *Iface
2475 = getSuperType()->getAs<ObjCObjectType>())
2476 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002477 break;
2478 }
2479
2480 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002481}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002482
Jay Foad4ba2a172011-01-12 09:06:06 +00002483bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002484 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002485}
2486
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002487ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2488 QualType Type, SourceLocation BLoc,
2489 SourceLocation RP)
2490 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2491 Type->isDependentType(), Type->isDependentType(),
2492 Type->containsUnexpandedParameterPack()),
2493 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2494{
2495 SubExprs = new (C) Stmt*[nexpr];
2496 for (unsigned i = 0; i < nexpr; i++) {
2497 if (args[i]->isTypeDependent())
2498 ExprBits.TypeDependent = true;
2499 if (args[i]->isValueDependent())
2500 ExprBits.ValueDependent = true;
2501 if (args[i]->containsUnexpandedParameterPack())
2502 ExprBits.ContainsUnexpandedParameterPack = true;
2503
2504 SubExprs[i] = args[i];
2505 }
2506}
2507
Nate Begeman888376a2009-08-12 02:28:50 +00002508void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2509 unsigned NumExprs) {
2510 if (SubExprs) C.Deallocate(SubExprs);
2511
2512 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002513 this->NumExprs = NumExprs;
2514 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002515}
Nate Begeman888376a2009-08-12 02:28:50 +00002516
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002517//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002518// DesignatedInitExpr
2519//===----------------------------------------------------------------------===//
2520
2521IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2522 assert(Kind == FieldDesignator && "Only valid on a field designator");
2523 if (Field.NameOrField & 0x01)
2524 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2525 else
2526 return getField()->getIdentifier();
2527}
2528
Sean Huntc3021132010-05-05 15:23:54 +00002529DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002530 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002531 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002532 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002533 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002534 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002535 unsigned NumIndexExprs,
2536 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002537 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002538 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002539 Init->isTypeDependent(), Init->isValueDependent(),
2540 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002541 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2542 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002543 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002544
2545 // Record the initializer itself.
2546 child_iterator Child = child_begin();
2547 *Child++ = Init;
2548
2549 // Copy the designators and their subexpressions, computing
2550 // value-dependence along the way.
2551 unsigned IndexIdx = 0;
2552 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002553 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002554
2555 if (this->Designators[I].isArrayDesignator()) {
2556 // Compute type- and value-dependence.
2557 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002558 if (Index->isTypeDependent() || Index->isValueDependent())
2559 ExprBits.ValueDependent = true;
2560
2561 // Propagate unexpanded parameter packs.
2562 if (Index->containsUnexpandedParameterPack())
2563 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002564
2565 // Copy the index expressions into permanent storage.
2566 *Child++ = IndexExprs[IndexIdx++];
2567 } else if (this->Designators[I].isArrayRangeDesignator()) {
2568 // Compute type- and value-dependence.
2569 Expr *Start = IndexExprs[IndexIdx];
2570 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002571 if (Start->isTypeDependent() || Start->isValueDependent() ||
2572 End->isTypeDependent() || End->isValueDependent())
2573 ExprBits.ValueDependent = true;
2574
2575 // Propagate unexpanded parameter packs.
2576 if (Start->containsUnexpandedParameterPack() ||
2577 End->containsUnexpandedParameterPack())
2578 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002579
2580 // Copy the start/end expressions into permanent storage.
2581 *Child++ = IndexExprs[IndexIdx++];
2582 *Child++ = IndexExprs[IndexIdx++];
2583 }
2584 }
2585
2586 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002587}
2588
Douglas Gregor05c13a32009-01-22 00:58:24 +00002589DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002590DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002591 unsigned NumDesignators,
2592 Expr **IndexExprs, unsigned NumIndexExprs,
2593 SourceLocation ColonOrEqualLoc,
2594 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002595 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002596 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002597 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002598 ColonOrEqualLoc, UsesColonSyntax,
2599 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002600}
2601
Mike Stump1eb44332009-09-09 15:08:12 +00002602DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002603 unsigned NumIndexExprs) {
2604 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2605 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2606 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2607}
2608
Douglas Gregor319d57f2010-01-06 23:17:19 +00002609void DesignatedInitExpr::setDesignators(ASTContext &C,
2610 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002611 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002612 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002613 NumDesignators = NumDesigs;
2614 for (unsigned I = 0; I != NumDesigs; ++I)
2615 Designators[I] = Desigs[I];
2616}
2617
Douglas Gregor05c13a32009-01-22 00:58:24 +00002618SourceRange DesignatedInitExpr::getSourceRange() const {
2619 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002620 Designator &First =
2621 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002622 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002623 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002624 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2625 else
2626 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2627 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002628 StartLoc =
2629 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002630 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2631}
2632
Douglas Gregor05c13a32009-01-22 00:58:24 +00002633Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2634 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2635 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2636 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002637 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2638 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2639}
2640
2641Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002642 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002643 "Requires array range designator");
2644 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2645 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002646 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2647 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2648}
2649
2650Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002651 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002652 "Requires array range designator");
2653 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2654 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002655 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2656 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2657}
2658
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002659/// \brief Replaces the designator at index @p Idx with the series
2660/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002661void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002662 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002663 const Designator *Last) {
2664 unsigned NumNewDesignators = Last - First;
2665 if (NumNewDesignators == 0) {
2666 std::copy_backward(Designators + Idx + 1,
2667 Designators + NumDesignators,
2668 Designators + Idx);
2669 --NumNewDesignators;
2670 return;
2671 } else if (NumNewDesignators == 1) {
2672 Designators[Idx] = *First;
2673 return;
2674 }
2675
Mike Stump1eb44332009-09-09 15:08:12 +00002676 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002677 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002678 std::copy(Designators, Designators + Idx, NewDesignators);
2679 std::copy(First, Last, NewDesignators + Idx);
2680 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2681 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002682 Designators = NewDesignators;
2683 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2684}
2685
Mike Stump1eb44332009-09-09 15:08:12 +00002686ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002687 Expr **exprs, unsigned nexprs,
2688 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002689 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2690 false, false, false),
2691 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Nate Begeman2ef13e52009-08-10 23:49:36 +00002693 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002694 for (unsigned i = 0; i != nexprs; ++i) {
2695 if (exprs[i]->isTypeDependent())
2696 ExprBits.TypeDependent = true;
2697 if (exprs[i]->isValueDependent())
2698 ExprBits.ValueDependent = true;
2699 if (exprs[i]->containsUnexpandedParameterPack())
2700 ExprBits.ContainsUnexpandedParameterPack = true;
2701
Nate Begeman2ef13e52009-08-10 23:49:36 +00002702 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002703 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002704}
2705
Douglas Gregor05c13a32009-01-22 00:58:24 +00002706//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002707// ExprIterator.
2708//===----------------------------------------------------------------------===//
2709
2710Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2711Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2712Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2713const Expr* ConstExprIterator::operator[](size_t idx) const {
2714 return cast<Expr>(I[idx]);
2715}
2716const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2717const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2718
2719//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002720// Child Iterators for iterating over subexpressions/substatements
2721//===----------------------------------------------------------------------===//
2722
2723// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002724Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2725Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002726
Steve Naroff7779db42007-11-12 14:29:37 +00002727// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002728Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2729Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002730
Steve Naroffe3e9add2008-06-02 23:03:37 +00002731// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002732Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2733{
John McCall12f78a62010-12-02 01:19:52 +00002734 if (Receiver.is<Stmt*>()) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002735 // Hack alert!
John McCall12f78a62010-12-02 01:19:52 +00002736 return reinterpret_cast<Stmt**> (&Receiver);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002737 }
2738 return child_iterator();
2739}
2740
2741Stmt::child_iterator ObjCPropertyRefExpr::child_end()
John McCall12f78a62010-12-02 01:19:52 +00002742{ return Receiver.is<Stmt*>() ?
2743 reinterpret_cast<Stmt**> (&Receiver)+1 :
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002744 child_iterator();
2745}
Steve Naroffae784072008-05-30 00:40:33 +00002746
Steve Narofff242b1b2009-07-24 17:54:45 +00002747// ObjCIsaExpr
2748Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2749Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2750
Chris Lattnerd9f69102008-08-10 01:53:14 +00002751// PredefinedExpr
2752Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2753Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002754
2755// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002756Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2757Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002758
2759// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002760Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002761Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002762
2763// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002764Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2765Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002766
Chris Lattner5d661452007-08-26 03:42:43 +00002767// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002768Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2769Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002770
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002771// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002772Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2773Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002774
2775// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002776Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2777Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002778
2779// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002780Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2781Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002782
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002783// OffsetOfExpr
2784Stmt::child_iterator OffsetOfExpr::child_begin() {
2785 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2786 + NumComps);
2787}
2788Stmt::child_iterator OffsetOfExpr::child_end() {
2789 return child_iterator(&*child_begin() + NumExprs);
2790}
2791
Sebastian Redl05189992008-11-11 17:56:53 +00002792// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002793Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002794 // If this is of a type and the type is a VLA type (and not a typedef), the
2795 // size expression of the VLA needs to be treated as an executable expression.
2796 // Why isn't this weirdness documented better in StmtIterator?
2797 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002798 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002799 getArgumentType().getTypePtr()))
2800 return child_iterator(T);
2801 return child_iterator();
2802 }
Sebastian Redld4575892008-12-03 23:17:54 +00002803 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002804}
Sebastian Redl05189992008-11-11 17:56:53 +00002805Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2806 if (isArgumentType())
2807 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002808 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002809}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002810
2811// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002812Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002813 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002814}
Ted Kremenek1237c672007-08-24 20:06:47 +00002815Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002816 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002817}
2818
2819// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002820Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002821 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002822}
Ted Kremenek1237c672007-08-24 20:06:47 +00002823Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002824 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002825}
Ted Kremenek1237c672007-08-24 20:06:47 +00002826
2827// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002828Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2829Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002830
Nate Begeman213541a2008-04-18 23:10:10 +00002831// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002832Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2833Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002834
2835// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002836Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2837Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002838
Ted Kremenek1237c672007-08-24 20:06:47 +00002839// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002840Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2841Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002842
2843// BinaryOperator
2844Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002845 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002846}
Ted Kremenek1237c672007-08-24 20:06:47 +00002847Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002848 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002849}
2850
2851// ConditionalOperator
2852Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002853 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002854}
Ted Kremenek1237c672007-08-24 20:06:47 +00002855Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002856 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002857}
2858
2859// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002860Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2861Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002862
Ted Kremenek1237c672007-08-24 20:06:47 +00002863// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002864Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2865Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002866
Ted Kremenek1237c672007-08-24 20:06:47 +00002867
2868// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002869Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2870Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002871
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002872// GNUNullExpr
2873Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2874Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2875
Eli Friedmand38617c2008-05-14 19:38:39 +00002876// ShuffleVectorExpr
2877Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002878 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002879}
2880Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002881 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002882}
2883
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002884// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002885Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2886Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002887
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002888// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002889Stmt::child_iterator InitListExpr::child_begin() {
2890 return InitExprs.size() ? &InitExprs[0] : 0;
2891}
2892Stmt::child_iterator InitListExpr::child_end() {
2893 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2894}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002895
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002896// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002897Stmt::child_iterator DesignatedInitExpr::child_begin() {
2898 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2899 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002900 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2901}
2902Stmt::child_iterator DesignatedInitExpr::child_end() {
2903 return child_iterator(&*child_begin() + NumSubExprs);
2904}
2905
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002906// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002907Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2908 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002909}
2910
Mike Stump1eb44332009-09-09 15:08:12 +00002911Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2912 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002913}
2914
Nate Begeman2ef13e52009-08-10 23:49:36 +00002915// ParenListExpr
2916Stmt::child_iterator ParenListExpr::child_begin() {
2917 return &Exprs[0];
2918}
2919Stmt::child_iterator ParenListExpr::child_end() {
2920 return &Exprs[0]+NumExprs;
2921}
2922
Ted Kremenek1237c672007-08-24 20:06:47 +00002923// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002924Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002925 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002926}
2927Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002928 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002929}
Ted Kremenek1237c672007-08-24 20:06:47 +00002930
2931// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002932Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2933Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002934
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002935// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002936Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002937 return child_iterator();
2938}
2939Stmt::child_iterator ObjCSelectorExpr::child_end() {
2940 return child_iterator();
2941}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002942
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002943// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002944Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2945 return child_iterator();
2946}
2947Stmt::child_iterator ObjCProtocolExpr::child_end() {
2948 return child_iterator();
2949}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002950
Steve Naroff563477d2007-09-18 23:55:05 +00002951// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002952Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002953 if (getReceiverKind() == Instance)
2954 return reinterpret_cast<Stmt **>(this + 1);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002955 return reinterpret_cast<Stmt **>(getArgs());
Steve Naroff563477d2007-09-18 23:55:05 +00002956}
2957Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregoraa165f82011-01-03 19:04:46 +00002958 return reinterpret_cast<Stmt **>(getArgs() + getNumArgs());
Steve Naroff563477d2007-09-18 23:55:05 +00002959}
2960
Steve Naroff4eb206b2008-09-03 18:15:37 +00002961// Blocks
Douglas Gregora779d9c2011-01-19 21:32:01 +00002962BlockDeclRefExpr::BlockDeclRefExpr(ValueDecl *d, QualType t, ExprValueKind VK,
2963 SourceLocation l, bool ByRef,
2964 bool constAdded, Stmt *copyConstructorVal)
Douglas Gregord967e312011-01-19 21:52:31 +00002965 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002966 d->isParameterPack()),
2967 D(d), Loc(l), IsByRef(ByRef),
2968 ConstQualAdded(constAdded), CopyConstructorVal(copyConstructorVal)
2969{
Douglas Gregord967e312011-01-19 21:52:31 +00002970 bool TypeDependent = false;
2971 bool ValueDependent = false;
2972 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2973 ExprBits.TypeDependent = TypeDependent;
2974 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00002975}
2976
Steve Naroff56ee6892008-10-08 17:01:13 +00002977Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2978Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002979
Ted Kremenek9da13f92008-09-26 23:24:14 +00002980Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2981Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002982
2983// OpaqueValueExpr
2984SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2985Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2986Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2987