blob: d8bb0af01838b238a0ec485ac410c9e7607a9523 [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 Gregor0da76df2009-11-23 11:41:28 +0000148void DeclRefExpr::computeDependence() {
John McCall8e6285a2010-10-26 08:39:16 +0000149 ExprBits.TypeDependent = false;
150 ExprBits.ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000151
Douglas Gregor0da76df2009-11-23 11:41:28 +0000152 NamedDecl *D = getDecl();
153
154 // (TD) C++ [temp.dep.expr]p3:
155 // An id-expression is type-dependent if it contains:
156 //
Sean Huntc3021132010-05-05 15:23:54 +0000157 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000158 //
159 // (VD) C++ [temp.dep.constexpr]p2:
160 // An identifier is value-dependent if it is:
161
162 // (TD) - an identifier that was declared with dependent type
163 // (VD) - a name declared with a dependent type,
164 if (getType()->isDependentType()) {
John McCall8e6285a2010-10-26 08:39:16 +0000165 ExprBits.TypeDependent = true;
166 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000167 }
168 // (TD) - a conversion-function-id that specifies a dependent type
Sean Huntc3021132010-05-05 15:23:54 +0000169 else if (D->getDeclName().getNameKind()
Douglas Gregor0da76df2009-11-23 11:41:28 +0000170 == DeclarationName::CXXConversionFunctionName &&
171 D->getDeclName().getCXXNameType()->isDependentType()) {
John McCall8e6285a2010-10-26 08:39:16 +0000172 ExprBits.TypeDependent = true;
173 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000174 }
175 // (TD) - a template-id that is dependent,
John McCall096832c2010-08-19 23:49:38 +0000176 else if (hasExplicitTemplateArgs() &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000177 TemplateSpecializationType::anyDependentTemplateArguments(
Sean Huntc3021132010-05-05 15:23:54 +0000178 getTemplateArgs(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 getNumTemplateArgs())) {
John McCall8e6285a2010-10-26 08:39:16 +0000180 ExprBits.TypeDependent = true;
181 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000182 }
183 // (VD) - the name of a non-type template parameter,
184 else if (isa<NonTypeTemplateParmDecl>(D))
John McCall8e6285a2010-10-26 08:39:16 +0000185 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000186 // (VD) - a constant with integral or enumeration type and is
187 // initialized with an expression that is value-dependent.
188 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000189 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000190 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000191 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000192 if (Init->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +0000193 ExprBits.ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000194 }
195 // (VD) - FIXME: Missing from the standard:
196 // - a member function or a static data member of the current
197 // instantiation
198 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000199 Var->getDeclContext()->isDependentContext())
John McCall8e6285a2010-10-26 08:39:16 +0000200 ExprBits.ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000201 }
202 // (VD) - FIXME: Missing from the standard:
203 // - a member function or a static data member of the current
204 // instantiation
205 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
John McCall8e6285a2010-10-26 08:39:16 +0000206 ExprBits.ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000207 // (TD) - a nested-name-specifier or a qualified-id that names a
208 // member of an unknown specialization.
209 // (handled by DependentScopeDeclRefExpr)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000210
Douglas Gregor10738d32010-12-23 23:51:58 +0000211 // Determine whether this expression contains any unexpanded parameter
212 // packs.
213 // Is the declaration a parameter pack?
214 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) {
215 if (NTTP->isParameterPack())
216 ExprBits.ContainsUnexpandedParameterPack = true;
217 }
218 // FIXME: Variadic templates function parameter packs.
Douglas Gregor0da76df2009-11-23 11:41:28 +0000219}
220
Sean Huntc3021132010-05-05 15:23:54 +0000221DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000222 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000223 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000224 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000225 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000226 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000227 DecoratedD(D,
228 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000229 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000230 Loc(NameLoc) {
231 if (Qualifier) {
232 NameQualifier *NQ = getNameQualifier();
233 NQ->NNS = Qualifier;
234 NQ->Range = QualifierRange;
235 }
Sean Huntc3021132010-05-05 15:23:54 +0000236
John McCalld5532b62009-11-23 01:53:49 +0000237 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000238 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000239
240 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000241}
242
Abramo Bagnara25777432010-08-11 22:01:17 +0000243DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
244 SourceRange QualifierRange,
245 ValueDecl *D, const DeclarationNameInfo &NameInfo,
246 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000247 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000248 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000249 DecoratedD(D,
250 (Qualifier? HasQualifierFlag : 0) |
251 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
252 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
253 if (Qualifier) {
254 NameQualifier *NQ = getNameQualifier();
255 NQ->NNS = Qualifier;
256 NQ->Range = QualifierRange;
257 }
258
259 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000260 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000261
262 computeDependence();
263}
264
Douglas Gregora2813ce2009-10-23 18:54:35 +0000265DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
266 NestedNameSpecifier *Qualifier,
267 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000268 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000269 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000270 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000271 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000272 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000273 return Create(Context, Qualifier, QualifierRange, D,
274 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000275 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000276}
277
278DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
279 NestedNameSpecifier *Qualifier,
280 SourceRange QualifierRange,
281 ValueDecl *D,
282 const DeclarationNameInfo &NameInfo,
283 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000284 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000285 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000286 std::size_t Size = sizeof(DeclRefExpr);
287 if (Qualifier != 0)
288 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000289
John McCalld5532b62009-11-23 01:53:49 +0000290 if (TemplateArgs)
291 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000292
Chris Lattner32488542010-10-30 05:14:06 +0000293 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara25777432010-08-11 22:01:17 +0000294 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCallf89e55a2010-11-18 06:31:45 +0000295 TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000296}
297
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000298DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
299 unsigned NumTemplateArgs) {
300 std::size_t Size = sizeof(DeclRefExpr);
301 if (HasQualifier)
302 Size += sizeof(NameQualifier);
303
304 if (NumTemplateArgs)
305 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
306
Chris Lattner32488542010-10-30 05:14:06 +0000307 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000308 return new (Mem) DeclRefExpr(EmptyShell());
309}
310
Douglas Gregora2813ce2009-10-23 18:54:35 +0000311SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000313 if (hasQualifier())
314 R.setBegin(getQualifierRange().getBegin());
John McCall096832c2010-08-19 23:49:38 +0000315 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000316 R.setEnd(getRAngleLoc());
317 return R;
318}
319
Anders Carlsson3a082d82009-09-08 18:24:21 +0000320// FIXME: Maybe this should use DeclPrinter with a special "print predefined
321// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000322std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
323 ASTContext &Context = CurrentDecl->getASTContext();
324
Anders Carlsson3a082d82009-09-08 18:24:21 +0000325 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000326 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000327 return FD->getNameAsString();
328
329 llvm::SmallString<256> Name;
330 llvm::raw_svector_ostream Out(Name);
331
332 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000333 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000334 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000335 if (MD->isStatic())
336 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000337 }
338
339 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000340
341 std::string Proto = FD->getQualifiedNameAsString(Policy);
342
John McCall183700f2009-09-21 23:43:11 +0000343 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000344 const FunctionProtoType *FT = 0;
345 if (FD->hasWrittenPrototype())
346 FT = dyn_cast<FunctionProtoType>(AFT);
347
348 Proto += "(";
349 if (FT) {
350 llvm::raw_string_ostream POut(Proto);
351 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
352 if (i) POut << ", ";
353 std::string Param;
354 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
355 POut << Param;
356 }
357
358 if (FT->isVariadic()) {
359 if (FD->getNumParams()) POut << ", ";
360 POut << "...";
361 }
362 }
363 Proto += ")";
364
Sam Weinig4eadcc52009-12-27 01:38:20 +0000365 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
366 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
367 if (ThisQuals.hasConst())
368 Proto += " const";
369 if (ThisQuals.hasVolatile())
370 Proto += " volatile";
371 }
372
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000373 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
374 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000375
376 Out << Proto;
377
378 Out.flush();
379 return Name.str().str();
380 }
381 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
382 llvm::SmallString<256> Name;
383 llvm::raw_svector_ostream Out(Name);
384 Out << (MD->isInstanceMethod() ? '-' : '+');
385 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000386
387 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
388 // a null check to avoid a crash.
389 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000390 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000391
Anders Carlsson3a082d82009-09-08 18:24:21 +0000392 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000393 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
394 Out << '(' << CID << ')';
395
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396 Out << ' ';
397 Out << MD->getSelector().getAsString();
398 Out << ']';
399
400 Out.flush();
401 return Name.str().str();
402 }
403 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
404 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
405 return "top level";
406 }
407 return "";
408}
409
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000410void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
411 if (hasAllocation())
412 C.Deallocate(pVal);
413
414 BitWidth = Val.getBitWidth();
415 unsigned NumWords = Val.getNumWords();
416 const uint64_t* Words = Val.getRawData();
417 if (NumWords > 1) {
418 pVal = new (C) uint64_t[NumWords];
419 std::copy(Words, Words + NumWords, pVal);
420 } else if (NumWords == 1)
421 VAL = Words[0];
422 else
423 VAL = 0;
424}
425
426IntegerLiteral *
427IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
428 QualType type, SourceLocation l) {
429 return new (C) IntegerLiteral(C, V, type, l);
430}
431
432IntegerLiteral *
433IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
434 return new (C) IntegerLiteral(Empty);
435}
436
437FloatingLiteral *
438FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
439 bool isexact, QualType Type, SourceLocation L) {
440 return new (C) FloatingLiteral(C, V, isexact, Type, L);
441}
442
443FloatingLiteral *
444FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
445 return new (C) FloatingLiteral(Empty);
446}
447
Chris Lattnerda8249e2008-06-07 22:13:43 +0000448/// getValueAsApproximateDouble - This returns the value as an inaccurate
449/// double. Note that this may cause loss of precision, but is useful for
450/// debugging dumps, etc.
451double FloatingLiteral::getValueAsApproximateDouble() const {
452 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000453 bool ignored;
454 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
455 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000456 return V.convertToDouble();
457}
458
Chris Lattner2085fd62009-02-18 06:40:38 +0000459StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
460 unsigned ByteLength, bool Wide,
461 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000462 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000463 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000464 // Allocate enough space for the StringLiteral plus an array of locations for
465 // any concatenated string tokens.
466 void *Mem = C.Allocate(sizeof(StringLiteral)+
467 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000468 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000469 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000472 char *AStrData = new (C, 1) char[ByteLength];
473 memcpy(AStrData, StrData, ByteLength);
474 SL->StrData = AStrData;
475 SL->ByteLength = ByteLength;
476 SL->IsWide = Wide;
477 SL->TokLocs[0] = Loc[0];
478 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000479
Chris Lattner726e1682009-02-18 05:49:11 +0000480 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000481 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
482 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000483}
484
Douglas Gregor673ecd62009-04-15 16:35:07 +0000485StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
486 void *Mem = C.Allocate(sizeof(StringLiteral)+
487 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000488 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000489 StringLiteral *SL = new (Mem) StringLiteral(QualType());
490 SL->StrData = 0;
491 SL->ByteLength = 0;
492 SL->NumConcatenated = NumStrs;
493 return SL;
494}
495
Daniel Dunbarb6480232009-09-22 03:27:33 +0000496void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000497 char *AStrData = new (C, 1) char[Str.size()];
498 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000499 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000500 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000501}
502
Chris Lattner08f92e32010-11-17 07:37:15 +0000503/// getLocationOfByte - Return a source location that points to the specified
504/// byte of this string literal.
505///
506/// Strings are amazingly complex. They can be formed from multiple tokens and
507/// can have escape sequences in them in addition to the usual trigraph and
508/// escaped newline business. This routine handles this complexity.
509///
510SourceLocation StringLiteral::
511getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
512 const LangOptions &Features, const TargetInfo &Target) const {
513 assert(!isWide() && "This doesn't work for wide strings yet");
514
515 // Loop over all of the tokens in this string until we find the one that
516 // contains the byte we're looking for.
517 unsigned TokNo = 0;
518 while (1) {
519 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
520 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
521
522 // Get the spelling of the string so that we can get the data that makes up
523 // the string literal, not the identifier for the macro it is potentially
524 // expanded through.
525 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
526
527 // Re-lex the token to get its length and original spelling.
528 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
529 bool Invalid = false;
530 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
531 if (Invalid)
532 return StrTokSpellingLoc;
533
534 const char *StrData = Buffer.data()+LocInfo.second;
535
536 // Create a langops struct and enable trigraphs. This is sufficient for
537 // relexing tokens.
538 LangOptions LangOpts;
539 LangOpts.Trigraphs = true;
540
541 // Create a lexer starting at the beginning of this token.
542 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
543 Buffer.end());
544 Token TheTok;
545 TheLexer.LexFromRawLexer(TheTok);
546
547 // Use the StringLiteralParser to compute the length of the string in bytes.
548 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
549 unsigned TokNumBytes = SLP.GetStringLength();
550
551 // If the byte is in this token, return the location of the byte.
552 if (ByteNo < TokNumBytes ||
553 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
554 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
555
556 // Now that we know the offset of the token in the spelling, use the
557 // preprocessor to get the offset in the original source.
558 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
559 }
560
561 // Move to the next string token.
562 ++TokNo;
563 ByteNo -= TokNumBytes;
564 }
565}
566
567
568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
570/// corresponds to, e.g. "sizeof" or "[pre]++".
571const char *UnaryOperator::getOpcodeStr(Opcode Op) {
572 switch (Op) {
573 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000574 case UO_PostInc: return "++";
575 case UO_PostDec: return "--";
576 case UO_PreInc: return "++";
577 case UO_PreDec: return "--";
578 case UO_AddrOf: return "&";
579 case UO_Deref: return "*";
580 case UO_Plus: return "+";
581 case UO_Minus: return "-";
582 case UO_Not: return "~";
583 case UO_LNot: return "!";
584 case UO_Real: return "__real";
585 case UO_Imag: return "__imag";
586 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 }
588}
589
John McCall2de56d12010-08-25 11:45:40 +0000590UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000591UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
592 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000593 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000594 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
595 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
596 case OO_Amp: return UO_AddrOf;
597 case OO_Star: return UO_Deref;
598 case OO_Plus: return UO_Plus;
599 case OO_Minus: return UO_Minus;
600 case OO_Tilde: return UO_Not;
601 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000602 }
603}
604
605OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
606 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000607 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
608 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
609 case UO_AddrOf: return OO_Amp;
610 case UO_Deref: return OO_Star;
611 case UO_Plus: return OO_Plus;
612 case UO_Minus: return OO_Minus;
613 case UO_Not: return OO_Tilde;
614 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000615 default: return OO_None;
616 }
617}
618
619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620//===----------------------------------------------------------------------===//
621// Postfix Operators.
622//===----------------------------------------------------------------------===//
623
Ted Kremenek668bf912009-02-09 20:51:47 +0000624CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
John McCallf89e55a2010-11-18 06:31:45 +0000625 unsigned numargs, QualType t, ExprValueKind VK,
626 SourceLocation rparenloc)
627 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000628 fn->isTypeDependent(),
629 fn->isValueDependent(),
630 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000631 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Ted Kremenek668bf912009-02-09 20:51:47 +0000633 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000634 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000635 for (unsigned i = 0; i != numargs; ++i) {
636 if (args[i]->isTypeDependent())
637 ExprBits.TypeDependent = true;
638 if (args[i]->isValueDependent())
639 ExprBits.ValueDependent = true;
640 if (args[i]->containsUnexpandedParameterPack())
641 ExprBits.ContainsUnexpandedParameterPack = true;
642
Douglas Gregorb4609802008-11-14 16:09:21 +0000643 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000644 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000645
Douglas Gregorb4609802008-11-14 16:09:21 +0000646 RParenLoc = rparenloc;
647}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000648
Ted Kremenek668bf912009-02-09 20:51:47 +0000649CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000650 QualType t, ExprValueKind VK, SourceLocation rparenloc)
651 : Expr(CallExprClass, 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) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000656
657 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +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
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000667 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000668 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 RParenLoc = rparenloc;
671}
672
Mike Stump1eb44332009-09-09 15:08:12 +0000673CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
674 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000675 // FIXME: Why do we allocate this?
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000676 SubExprs = new (C) Stmt*[1];
677}
678
Nuno Lopesd20254f2009-12-20 23:11:08 +0000679Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000680 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000681 // If we're calling a dereference, look at the pointer instead.
682 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
683 if (BO->isPtrMemOp())
684 CEE = BO->getRHS()->IgnoreParenCasts();
685 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
686 if (UO->getOpcode() == UO_Deref)
687 CEE = UO->getSubExpr()->IgnoreParenCasts();
688 }
Chris Lattner6346f962009-07-17 15:46:27 +0000689 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000690 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000691 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
692 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000693
694 return 0;
695}
696
Nuno Lopesd20254f2009-12-20 23:11:08 +0000697FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000698 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000699}
700
Chris Lattnerd18b3292007-12-28 05:25:02 +0000701/// setNumArgs - This changes the number of arguments present in this call.
702/// Any orphaned expressions are deleted by this, and any new operands are set
703/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000704void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000705 // No change, just return.
706 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattnerd18b3292007-12-28 05:25:02 +0000708 // If shrinking # arguments, just delete the extras and forgot them.
709 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000710 this->NumArgs = NumArgs;
711 return;
712 }
713
714 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000715 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000716 // Copy over args.
717 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
718 NewSubExprs[i] = SubExprs[i];
719 // Null out new args.
720 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
721 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregor88c9a462009-04-17 21:46:47 +0000723 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000724 SubExprs = NewSubExprs;
725 this->NumArgs = NumArgs;
726}
727
Chris Lattnercb888962008-10-06 05:00:53 +0000728/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
729/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000730unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000731 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000732 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000733 // ImplicitCastExpr.
734 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
735 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000736 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000738 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
739 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000740 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Anders Carlssonbcba2012008-01-31 02:13:57 +0000742 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
743 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000744 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000746 if (!FDecl->getIdentifier())
747 return 0;
748
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000749 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000750}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000751
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000752QualType CallExpr::getCallReturnType() const {
753 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000754 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000755 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000756 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000757 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000758 else if (const MemberPointerType *MPT
759 = CalleeType->getAs<MemberPointerType>())
760 CalleeType = MPT->getPointeeType();
761
John McCall183700f2009-09-21 23:43:11 +0000762 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000763 return FnType->getResultType();
764}
Chris Lattnercb888962008-10-06 05:00:53 +0000765
Sean Huntc3021132010-05-05 15:23:54 +0000766OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000767 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000768 TypeSourceInfo *tsi,
769 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000770 Expr** exprsPtr, unsigned numExprs,
771 SourceLocation RParenLoc) {
772 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000773 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000774 sizeof(Expr*) * numExprs);
775
776 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
777 exprsPtr, numExprs, RParenLoc);
778}
779
780OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
781 unsigned numComps, unsigned numExprs) {
782 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
783 sizeof(OffsetOfNode) * numComps +
784 sizeof(Expr*) * numExprs);
785 return new (Mem) OffsetOfExpr(numComps, numExprs);
786}
787
Sean Huntc3021132010-05-05 15:23:54 +0000788OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000789 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000790 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000791 Expr** exprsPtr, unsigned numExprs,
792 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000793 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
794 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000795 /*ValueDependent=*/tsi->getType()->isDependentType(),
796 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000797 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
798 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000799{
800 for(unsigned i = 0; i < numComps; ++i) {
801 setComponent(i, compsPtr[i]);
802 }
Sean Huntc3021132010-05-05 15:23:54 +0000803
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000804 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000805 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
806 ExprBits.ValueDependent = true;
807 if (exprsPtr[i]->containsUnexpandedParameterPack())
808 ExprBits.ContainsUnexpandedParameterPack = true;
809
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000810 setIndexExpr(i, exprsPtr[i]);
811 }
812}
813
814IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
815 assert(getKind() == Field || getKind() == Identifier);
816 if (getKind() == Field)
817 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000818
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000819 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
820}
821
Mike Stump1eb44332009-09-09 15:08:12 +0000822MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
823 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000824 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000825 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000826 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000827 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000828 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000829 QualType ty,
830 ExprValueKind vk,
831 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000832 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000833
John McCall161755a2010-04-06 21:38:20 +0000834 bool hasQualOrFound = (qual != 0 ||
835 founddecl.getDecl() != memberdecl ||
836 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000837 if (hasQualOrFound)
838 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000839
John McCalld5532b62009-11-23 01:53:49 +0000840 if (targs)
841 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner32488542010-10-30 05:14:06 +0000843 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000844 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
845 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000846
847 if (hasQualOrFound) {
848 if (qual && qual->isDependent()) {
849 E->setValueDependent(true);
850 E->setTypeDependent(true);
851 }
852 E->HasQualifierOrFoundDecl = true;
853
854 MemberNameQualifier *NQ = E->getMemberQualifier();
855 NQ->NNS = qual;
856 NQ->Range = qualrange;
857 NQ->FoundDecl = founddecl;
858 }
859
860 if (targs) {
861 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000862 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000863 }
864
865 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000866}
867
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000868const char *CastExpr::getCastKindName() const {
869 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000870 case CK_Dependent:
871 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000872 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000873 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000874 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000875 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000876 case CK_LValueToRValue:
877 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000878 case CK_GetObjCProperty:
879 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000880 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000881 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000882 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000883 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000884 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000885 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000886 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000887 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000888 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000889 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000890 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000891 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000892 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000893 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000894 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000895 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000896 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000897 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000898 case CK_NullToPointer:
899 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000900 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000901 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000902 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000903 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000904 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000905 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000906 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000907 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000908 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000909 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000910 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000911 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000912 case CK_PointerToBoolean:
913 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000914 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000915 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000916 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000917 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000918 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000919 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000920 case CK_IntegralToBoolean:
921 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000922 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000923 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000924 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000925 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000926 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000927 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000928 case CK_FloatingToBoolean:
929 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000930 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000931 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000932 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000933 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000934 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000935 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000936 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000937 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000938 case CK_FloatingRealToComplex:
939 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000940 case CK_FloatingComplexToReal:
941 return "FloatingComplexToReal";
942 case CK_FloatingComplexToBoolean:
943 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000944 case CK_FloatingComplexCast:
945 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000946 case CK_FloatingComplexToIntegralComplex:
947 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000948 case CK_IntegralRealToComplex:
949 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000950 case CK_IntegralComplexToReal:
951 return "IntegralComplexToReal";
952 case CK_IntegralComplexToBoolean:
953 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000954 case CK_IntegralComplexCast:
955 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000956 case CK_IntegralComplexToFloatingComplex:
957 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
John McCall2bb5d002010-11-13 09:02:35 +0000960 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000961 return 0;
962}
963
Douglas Gregor6eef5192009-12-14 19:27:10 +0000964Expr *CastExpr::getSubExprAsWritten() {
965 Expr *SubExpr = 0;
966 CastExpr *E = this;
967 do {
968 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000969
Douglas Gregor6eef5192009-12-14 19:27:10 +0000970 // Skip any temporary bindings; they're implicit.
971 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
972 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000973
Douglas Gregor6eef5192009-12-14 19:27:10 +0000974 // Conversions by constructor and conversion functions have a
975 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +0000976 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000977 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +0000978 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +0000979 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000980
Douglas Gregor6eef5192009-12-14 19:27:10 +0000981 // If the subexpression we're left with is an implicit cast, look
982 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000983 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
984
Douglas Gregor6eef5192009-12-14 19:27:10 +0000985 return SubExpr;
986}
987
John McCallf871d0c2010-08-07 06:22:56 +0000988CXXBaseSpecifier **CastExpr::path_buffer() {
989 switch (getStmtClass()) {
990#define ABSTRACT_STMT(x)
991#define CASTEXPR(Type, Base) \
992 case Stmt::Type##Class: \
993 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
994#define STMT(Type, Base)
995#include "clang/AST/StmtNodes.inc"
996 default:
997 llvm_unreachable("non-cast expressions not possible here");
998 return 0;
999 }
1000}
1001
1002void CastExpr::setCastPath(const CXXCastPath &Path) {
1003 assert(Path.size() == path_size());
1004 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1005}
1006
1007ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1008 CastKind Kind, Expr *Operand,
1009 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001010 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001011 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1012 void *Buffer =
1013 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1014 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001015 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001016 if (PathSize) E->setCastPath(*BasePath);
1017 return E;
1018}
1019
1020ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1021 unsigned PathSize) {
1022 void *Buffer =
1023 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1024 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1025}
1026
1027
1028CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001029 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001030 const CXXCastPath *BasePath,
1031 TypeSourceInfo *WrittenTy,
1032 SourceLocation L, SourceLocation R) {
1033 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1034 void *Buffer =
1035 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1036 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001037 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001038 if (PathSize) E->setCastPath(*BasePath);
1039 return E;
1040}
1041
1042CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1043 void *Buffer =
1044 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1045 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1046}
1047
Reid Spencer5f016e22007-07-11 17:01:13 +00001048/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1049/// corresponds to, e.g. "<<=".
1050const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1051 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001052 case BO_PtrMemD: return ".*";
1053 case BO_PtrMemI: return "->*";
1054 case BO_Mul: return "*";
1055 case BO_Div: return "/";
1056 case BO_Rem: return "%";
1057 case BO_Add: return "+";
1058 case BO_Sub: return "-";
1059 case BO_Shl: return "<<";
1060 case BO_Shr: return ">>";
1061 case BO_LT: return "<";
1062 case BO_GT: return ">";
1063 case BO_LE: return "<=";
1064 case BO_GE: return ">=";
1065 case BO_EQ: return "==";
1066 case BO_NE: return "!=";
1067 case BO_And: return "&";
1068 case BO_Xor: return "^";
1069 case BO_Or: return "|";
1070 case BO_LAnd: return "&&";
1071 case BO_LOr: return "||";
1072 case BO_Assign: return "=";
1073 case BO_MulAssign: return "*=";
1074 case BO_DivAssign: return "/=";
1075 case BO_RemAssign: return "%=";
1076 case BO_AddAssign: return "+=";
1077 case BO_SubAssign: return "-=";
1078 case BO_ShlAssign: return "<<=";
1079 case BO_ShrAssign: return ">>=";
1080 case BO_AndAssign: return "&=";
1081 case BO_XorAssign: return "^=";
1082 case BO_OrAssign: return "|=";
1083 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001085
1086 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001087}
1088
John McCall2de56d12010-08-25 11:45:40 +00001089BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001090BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1091 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001092 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001093 case OO_Plus: return BO_Add;
1094 case OO_Minus: return BO_Sub;
1095 case OO_Star: return BO_Mul;
1096 case OO_Slash: return BO_Div;
1097 case OO_Percent: return BO_Rem;
1098 case OO_Caret: return BO_Xor;
1099 case OO_Amp: return BO_And;
1100 case OO_Pipe: return BO_Or;
1101 case OO_Equal: return BO_Assign;
1102 case OO_Less: return BO_LT;
1103 case OO_Greater: return BO_GT;
1104 case OO_PlusEqual: return BO_AddAssign;
1105 case OO_MinusEqual: return BO_SubAssign;
1106 case OO_StarEqual: return BO_MulAssign;
1107 case OO_SlashEqual: return BO_DivAssign;
1108 case OO_PercentEqual: return BO_RemAssign;
1109 case OO_CaretEqual: return BO_XorAssign;
1110 case OO_AmpEqual: return BO_AndAssign;
1111 case OO_PipeEqual: return BO_OrAssign;
1112 case OO_LessLess: return BO_Shl;
1113 case OO_GreaterGreater: return BO_Shr;
1114 case OO_LessLessEqual: return BO_ShlAssign;
1115 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1116 case OO_EqualEqual: return BO_EQ;
1117 case OO_ExclaimEqual: return BO_NE;
1118 case OO_LessEqual: return BO_LE;
1119 case OO_GreaterEqual: return BO_GE;
1120 case OO_AmpAmp: return BO_LAnd;
1121 case OO_PipePipe: return BO_LOr;
1122 case OO_Comma: return BO_Comma;
1123 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001124 }
1125}
1126
1127OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1128 static const OverloadedOperatorKind OverOps[] = {
1129 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1130 OO_Star, OO_Slash, OO_Percent,
1131 OO_Plus, OO_Minus,
1132 OO_LessLess, OO_GreaterGreater,
1133 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1134 OO_EqualEqual, OO_ExclaimEqual,
1135 OO_Amp,
1136 OO_Caret,
1137 OO_Pipe,
1138 OO_AmpAmp,
1139 OO_PipePipe,
1140 OO_Equal, OO_StarEqual,
1141 OO_SlashEqual, OO_PercentEqual,
1142 OO_PlusEqual, OO_MinusEqual,
1143 OO_LessLessEqual, OO_GreaterGreaterEqual,
1144 OO_AmpEqual, OO_CaretEqual,
1145 OO_PipeEqual,
1146 OO_Comma
1147 };
1148 return OverOps[Opc];
1149}
1150
Ted Kremenek709210f2010-04-13 23:39:13 +00001151InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001152 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001153 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001154 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1155 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001156 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001157 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001158 UnionFieldInit(0), HadArrayRangeDesignator(false)
1159{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001160 for (unsigned I = 0; I != numInits; ++I) {
1161 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001162 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001163 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001164 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001165 if (initExprs[I]->containsUnexpandedParameterPack())
1166 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001167 }
Sean Huntc3021132010-05-05 15:23:54 +00001168
Ted Kremenek709210f2010-04-13 23:39:13 +00001169 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001170}
Reid Spencer5f016e22007-07-11 17:01:13 +00001171
Ted Kremenek709210f2010-04-13 23:39:13 +00001172void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001173 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001174 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001175}
1176
Ted Kremenek709210f2010-04-13 23:39:13 +00001177void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001178 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001179}
1180
Ted Kremenek709210f2010-04-13 23:39:13 +00001181Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001182 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001183 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001184 InitExprs.back() = expr;
1185 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Douglas Gregor4c678342009-01-28 21:54:33 +00001188 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1189 InitExprs[Init] = expr;
1190 return Result;
1191}
1192
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001193SourceRange InitListExpr::getSourceRange() const {
1194 if (SyntacticForm)
1195 return SyntacticForm->getSourceRange();
1196 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1197 if (Beg.isInvalid()) {
1198 // Find the first non-null initializer.
1199 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1200 E = InitExprs.end();
1201 I != E; ++I) {
1202 if (Stmt *S = *I) {
1203 Beg = S->getLocStart();
1204 break;
1205 }
1206 }
1207 }
1208 if (End.isInvalid()) {
1209 // Find the first non-null initializer from the end.
1210 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1211 E = InitExprs.rend();
1212 I != E; ++I) {
1213 if (Stmt *S = *I) {
1214 End = S->getSourceRange().getEnd();
1215 break;
1216 }
1217 }
1218 }
1219 return SourceRange(Beg, End);
1220}
1221
Steve Naroffbfdcae62008-09-04 15:31:07 +00001222/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001223///
1224const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001225 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001226 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001227}
1228
Mike Stump1eb44332009-09-09 15:08:12 +00001229SourceLocation BlockExpr::getCaretLocation() const {
1230 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001231}
Mike Stump1eb44332009-09-09 15:08:12 +00001232const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001233 return TheBlock->getBody();
1234}
Mike Stump1eb44332009-09-09 15:08:12 +00001235Stmt *BlockExpr::getBody() {
1236 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001237}
Steve Naroff56ee6892008-10-08 17:01:13 +00001238
1239
Reid Spencer5f016e22007-07-11 17:01:13 +00001240//===----------------------------------------------------------------------===//
1241// Generic Expression Routines
1242//===----------------------------------------------------------------------===//
1243
Chris Lattner026dc962009-02-14 07:37:35 +00001244/// isUnusedResultAWarning - Return true if this immediate expression should
1245/// be warned about if the result is unused. If so, fill in Loc and Ranges
1246/// with location to warn on and the source range[s] to report with the
1247/// warning.
1248bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001249 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001250 // Don't warn if the expr is type dependent. The type could end up
1251 // instantiating to void.
1252 if (isTypeDependent())
1253 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 switch (getStmtClass()) {
1256 default:
John McCall0faede62010-03-12 07:11:26 +00001257 if (getType()->isVoidType())
1258 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001259 Loc = getExprLoc();
1260 R1 = getSourceRange();
1261 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001263 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001264 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 case UnaryOperatorClass: {
1266 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001269 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001270 case UO_PostInc:
1271 case UO_PostDec:
1272 case UO_PreInc:
1273 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001274 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001275 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001277 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001278 return false;
1279 break;
John McCall2de56d12010-08-25 11:45:40 +00001280 case UO_Real:
1281 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001282 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001283 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1284 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001285 return false;
1286 break;
John McCall2de56d12010-08-25 11:45:40 +00001287 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001288 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 }
Chris Lattner026dc962009-02-14 07:37:35 +00001290 Loc = UO->getOperatorLoc();
1291 R1 = UO->getSubExpr()->getSourceRange();
1292 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001294 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001295 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001296 switch (BO->getOpcode()) {
1297 default:
1298 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001299 // Consider the RHS of comma for side effects. LHS was checked by
1300 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001301 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001302 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1303 // lvalue-ness) of an assignment written in a macro.
1304 if (IntegerLiteral *IE =
1305 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1306 if (IE->getValue() == 0)
1307 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001308 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1309 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001310 case BO_LAnd:
1311 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001312 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1313 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1314 return false;
1315 break;
John McCallbf0ee352010-02-16 04:10:53 +00001316 }
Chris Lattner026dc962009-02-14 07:37:35 +00001317 if (BO->isAssignmentOp())
1318 return false;
1319 Loc = BO->getOperatorLoc();
1320 R1 = BO->getLHS()->getSourceRange();
1321 R2 = BO->getRHS()->getSourceRange();
1322 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001323 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001324 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001325 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001326 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001327
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001328 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001329 // The condition must be evaluated, but if either the LHS or RHS is a
1330 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001331 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001332 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001333 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001334 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001335 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001336 }
1337
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001339 // If the base pointer or element is to a volatile pointer/field, accessing
1340 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001341 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001342 return false;
1343 Loc = cast<MemberExpr>(this)->getMemberLoc();
1344 R1 = SourceRange(Loc, Loc);
1345 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1346 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 case ArraySubscriptExprClass:
1349 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001350 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001351 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001352 return false;
1353 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1354 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1355 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1356 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001357
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001359 case CXXOperatorCallExprClass:
1360 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001361 // If this is a direct call, get the callee.
1362 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001363 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001364 // If the callee has attribute pure, const, or warn_unused_result, warn
1365 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001366 //
1367 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1368 // updated to match for QoI.
1369 if (FD->getAttr<WarnUnusedResultAttr>() ||
1370 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1371 Loc = CE->getCallee()->getLocStart();
1372 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001374 if (unsigned NumArgs = CE->getNumArgs())
1375 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1376 CE->getArg(NumArgs-1)->getLocEnd());
1377 return true;
1378 }
Chris Lattner026dc962009-02-14 07:37:35 +00001379 }
1380 return false;
1381 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001382
1383 case CXXTemporaryObjectExprClass:
1384 case CXXConstructExprClass:
1385 return false;
1386
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001387 case ObjCMessageExprClass: {
1388 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1389 const ObjCMethodDecl *MD = ME->getMethodDecl();
1390 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1391 Loc = getExprLoc();
1392 return true;
1393 }
Chris Lattner026dc962009-02-14 07:37:35 +00001394 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001395 }
Mike Stump1eb44332009-09-09 15:08:12 +00001396
John McCall12f78a62010-12-02 01:19:52 +00001397 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001398 Loc = getExprLoc();
1399 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001400 return true;
John McCall12f78a62010-12-02 01:19:52 +00001401
Chris Lattner611b2ec2008-07-26 19:51:01 +00001402 case StmtExprClass: {
1403 // Statement exprs don't logically have side effects themselves, but are
1404 // sometimes used in macros in ways that give them a type that is unused.
1405 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1406 // however, if the result of the stmt expr is dead, we don't want to emit a
1407 // warning.
1408 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001409 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001410 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001411 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001412 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1413 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1414 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
John McCall0faede62010-03-12 07:11:26 +00001417 if (getType()->isVoidType())
1418 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001419 Loc = cast<StmtExpr>(this)->getLParenLoc();
1420 R1 = getSourceRange();
1421 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001422 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001423 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001424 // If this is an explicit cast to void, allow it. People do this when they
1425 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001426 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001427 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001428 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1429 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1430 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001431 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001432 if (getType()->isVoidType())
1433 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001434 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001435
Anders Carlsson58beed92009-11-17 17:11:23 +00001436 // If this is a cast to void or a constructor conversion, check the operand.
1437 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001438 if (CE->getCastKind() == CK_ToVoid ||
1439 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001440 return (cast<CastExpr>(this)->getSubExpr()
1441 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001442 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1443 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1444 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Eli Friedman4be1f472008-05-19 21:24:43 +00001447 case ImplicitCastExprClass:
1448 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001449 return (cast<ImplicitCastExpr>(this)
1450 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001451
Chris Lattner04421082008-04-08 04:40:51 +00001452 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001453 return (cast<CXXDefaultArgExpr>(this)
1454 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001455
1456 case CXXNewExprClass:
1457 // FIXME: In theory, there might be new expressions that don't have side
1458 // effects (e.g. a placement new with an uninitialized POD).
1459 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001460 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001461 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001462 return (cast<CXXBindTemporaryExpr>(this)
1463 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001464 case ExprWithCleanupsClass:
1465 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001466 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001467 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001468}
1469
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001470/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001471/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001472bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001473 switch (getStmtClass()) {
1474 default:
1475 return false;
1476 case ObjCIvarRefExprClass:
1477 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001478 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001479 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001480 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001481 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001482 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001483 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001484 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001485 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001486 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001487 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001488 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1489 if (VD->hasGlobalStorage())
1490 return true;
1491 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001492 // dereferencing to a pointer is always a gc'able candidate,
1493 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001494 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001495 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001496 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001497 return false;
1498 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001499 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001500 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001501 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001502 }
1503 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001504 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001505 }
1506}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001507
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001508bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1509 if (isTypeDependent())
1510 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001511 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001512}
1513
Sebastian Redl369e51f2010-09-10 20:55:33 +00001514static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1515 Expr::CanThrowResult CT2) {
1516 // CanThrowResult constants are ordered so that the maximum is the correct
1517 // merge result.
1518 return CT1 > CT2 ? CT1 : CT2;
1519}
1520
1521static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1522 Expr *E = const_cast<Expr*>(CE);
1523 Expr::CanThrowResult R = Expr::CT_Cannot;
1524 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1525 I != IE && R != Expr::CT_Can; ++I) {
1526 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1527 }
1528 return R;
1529}
1530
1531static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1532 bool NullThrows = true) {
1533 if (!D)
1534 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1535
1536 // See if we can get a function type from the decl somehow.
1537 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1538 if (!VD) // If we have no clue what we're calling, assume the worst.
1539 return Expr::CT_Can;
1540
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001541 // As an extension, we assume that __attribute__((nothrow)) functions don't
1542 // throw.
1543 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1544 return Expr::CT_Cannot;
1545
Sebastian Redl369e51f2010-09-10 20:55:33 +00001546 QualType T = VD->getType();
1547 const FunctionProtoType *FT;
1548 if ((FT = T->getAs<FunctionProtoType>())) {
1549 } else if (const PointerType *PT = T->getAs<PointerType>())
1550 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1551 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1552 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1553 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1554 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1555 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1556 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1557
1558 if (!FT)
1559 return Expr::CT_Can;
1560
1561 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1562}
1563
1564static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1565 if (DC->isTypeDependent())
1566 return Expr::CT_Dependent;
1567
Sebastian Redl295995c2010-09-10 20:55:47 +00001568 if (!DC->getTypeAsWritten()->isReferenceType())
1569 return Expr::CT_Cannot;
1570
Sebastian Redl369e51f2010-09-10 20:55:33 +00001571 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1572}
1573
1574static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1575 const CXXTypeidExpr *DC) {
1576 if (DC->isTypeOperand())
1577 return Expr::CT_Cannot;
1578
1579 Expr *Op = DC->getExprOperand();
1580 if (Op->isTypeDependent())
1581 return Expr::CT_Dependent;
1582
1583 const RecordType *RT = Op->getType()->getAs<RecordType>();
1584 if (!RT)
1585 return Expr::CT_Cannot;
1586
1587 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1588 return Expr::CT_Cannot;
1589
1590 if (Op->Classify(C).isPRValue())
1591 return Expr::CT_Cannot;
1592
1593 return Expr::CT_Can;
1594}
1595
1596Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1597 // C++ [expr.unary.noexcept]p3:
1598 // [Can throw] if in a potentially-evaluated context the expression would
1599 // contain:
1600 switch (getStmtClass()) {
1601 case CXXThrowExprClass:
1602 // - a potentially evaluated throw-expression
1603 return CT_Can;
1604
1605 case CXXDynamicCastExprClass: {
1606 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1607 // where T is a reference type, that requires a run-time check
1608 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1609 if (CT == CT_Can)
1610 return CT;
1611 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1612 }
1613
1614 case CXXTypeidExprClass:
1615 // - a potentially evaluated typeid expression applied to a glvalue
1616 // expression whose type is a polymorphic class type
1617 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1618
1619 // - a potentially evaluated call to a function, member function, function
1620 // pointer, or member function pointer that does not have a non-throwing
1621 // exception-specification
1622 case CallExprClass:
1623 case CXXOperatorCallExprClass:
1624 case CXXMemberCallExprClass: {
1625 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1626 if (CT == CT_Can)
1627 return CT;
1628 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1629 }
1630
Sebastian Redl295995c2010-09-10 20:55:47 +00001631 case CXXConstructExprClass:
1632 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001633 CanThrowResult CT = CanCalleeThrow(
1634 cast<CXXConstructExpr>(this)->getConstructor());
1635 if (CT == CT_Can)
1636 return CT;
1637 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1638 }
1639
1640 case CXXNewExprClass: {
1641 CanThrowResult CT = MergeCanThrow(
1642 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1643 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1644 /*NullThrows*/false));
1645 if (CT == CT_Can)
1646 return CT;
1647 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1648 }
1649
1650 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001651 CanThrowResult CT = CanCalleeThrow(
1652 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1653 if (CT == CT_Can)
1654 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001655 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1656 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1657 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1658 Arg = Cast->getSubExpr();
1659 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1660 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1661 CanThrowResult CT2 = CanCalleeThrow(
1662 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1663 if (CT2 == CT_Can)
1664 return CT2;
1665 CT = MergeCanThrow(CT, CT2);
1666 }
1667 }
1668 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1669 }
1670
1671 case CXXBindTemporaryExprClass: {
1672 // The bound temporary has to be destroyed again, which might throw.
1673 CanThrowResult CT = CanCalleeThrow(
1674 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1675 if (CT == CT_Can)
1676 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001677 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1678 }
1679
1680 // ObjC message sends are like function calls, but never have exception
1681 // specs.
1682 case ObjCMessageExprClass:
1683 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001684 return CT_Can;
1685
1686 // Many other things have subexpressions, so we have to test those.
1687 // Some are simple:
1688 case ParenExprClass:
1689 case MemberExprClass:
1690 case CXXReinterpretCastExprClass:
1691 case CXXConstCastExprClass:
1692 case ConditionalOperatorClass:
1693 case CompoundLiteralExprClass:
1694 case ExtVectorElementExprClass:
1695 case InitListExprClass:
1696 case DesignatedInitExprClass:
1697 case ParenListExprClass:
1698 case VAArgExprClass:
1699 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001700 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001701 case ObjCIvarRefExprClass:
1702 case ObjCIsaExprClass:
1703 case ShuffleVectorExprClass:
1704 return CanSubExprsThrow(C, this);
1705
1706 // Some might be dependent for other reasons.
1707 case UnaryOperatorClass:
1708 case ArraySubscriptExprClass:
1709 case ImplicitCastExprClass:
1710 case CStyleCastExprClass:
1711 case CXXStaticCastExprClass:
1712 case CXXFunctionalCastExprClass:
1713 case BinaryOperatorClass:
1714 case CompoundAssignOperatorClass: {
1715 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1716 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1717 }
1718
1719 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1720 case StmtExprClass:
1721 return CT_Can;
1722
1723 case ChooseExprClass:
1724 if (isTypeDependent() || isValueDependent())
1725 return CT_Dependent;
1726 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1727
1728 // Some expressions are always dependent.
1729 case DependentScopeDeclRefExprClass:
1730 case CXXUnresolvedConstructExprClass:
1731 case CXXDependentScopeMemberExprClass:
1732 return CT_Dependent;
1733
1734 default:
1735 // All other expressions don't have subexpressions, or else they are
1736 // unevaluated.
1737 return CT_Cannot;
1738 }
1739}
1740
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001741Expr* Expr::IgnoreParens() {
1742 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001743 while (true) {
1744 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1745 E = P->getSubExpr();
1746 continue;
1747 }
1748 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1749 if (P->getOpcode() == UO_Extension) {
1750 E = P->getSubExpr();
1751 continue;
1752 }
1753 }
1754 return E;
1755 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001756}
1757
Chris Lattner56f34942008-02-13 01:02:39 +00001758/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1759/// or CastExprs or ImplicitCastExprs, returning their operand.
1760Expr *Expr::IgnoreParenCasts() {
1761 Expr *E = this;
1762 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001763 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001764 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001765 continue;
1766 }
1767 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001768 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001769 continue;
1770 }
1771 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1772 if (P->getOpcode() == UO_Extension) {
1773 E = P->getSubExpr();
1774 continue;
1775 }
1776 }
1777 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001778 }
1779}
1780
John McCall9c5d70c2010-12-04 08:24:19 +00001781/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1782/// casts. This is intended purely as a temporary workaround for code
1783/// that hasn't yet been rewritten to do the right thing about those
1784/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001785Expr *Expr::IgnoreParenLValueCasts() {
1786 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001787 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001788 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1789 E = P->getSubExpr();
1790 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001791 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001792 if (P->getCastKind() == CK_LValueToRValue) {
1793 E = P->getSubExpr();
1794 continue;
1795 }
John McCall9c5d70c2010-12-04 08:24:19 +00001796 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1797 if (P->getOpcode() == UO_Extension) {
1798 E = P->getSubExpr();
1799 continue;
1800 }
John McCallf6a16482010-12-04 03:47:34 +00001801 }
1802 break;
1803 }
1804 return E;
1805}
1806
John McCall2fc46bf2010-05-05 22:59:52 +00001807Expr *Expr::IgnoreParenImpCasts() {
1808 Expr *E = this;
1809 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001810 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001811 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001812 continue;
1813 }
1814 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001815 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001816 continue;
1817 }
1818 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1819 if (P->getOpcode() == UO_Extension) {
1820 E = P->getSubExpr();
1821 continue;
1822 }
1823 }
1824 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001825 }
1826}
1827
Chris Lattnerecdd8412009-03-13 17:28:01 +00001828/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1829/// value (including ptr->int casts of the same size). Strip off any
1830/// ParenExpr or CastExprs, returning their operand.
1831Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1832 Expr *E = this;
1833 while (true) {
1834 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1835 E = P->getSubExpr();
1836 continue;
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Chris Lattnerecdd8412009-03-13 17:28:01 +00001839 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1840 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001841 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001842 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Chris Lattnerecdd8412009-03-13 17:28:01 +00001844 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1845 E = SE;
1846 continue;
1847 }
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001849 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001850 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001851 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001852 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001853 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1854 E = SE;
1855 continue;
1856 }
1857 }
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001859 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1860 if (P->getOpcode() == UO_Extension) {
1861 E = P->getSubExpr();
1862 continue;
1863 }
1864 }
1865
Chris Lattnerecdd8412009-03-13 17:28:01 +00001866 return E;
1867 }
1868}
1869
Douglas Gregor6eef5192009-12-14 19:27:10 +00001870bool Expr::isDefaultArgument() const {
1871 const Expr *E = this;
1872 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1873 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001874
Douglas Gregor6eef5192009-12-14 19:27:10 +00001875 return isa<CXXDefaultArgExpr>(E);
1876}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001877
Douglas Gregor2f599792010-04-02 18:24:57 +00001878/// \brief Skip over any no-op casts and any temporary-binding
1879/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001880static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001881 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001882 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001883 E = ICE->getSubExpr();
1884 else
1885 break;
1886 }
1887
1888 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1889 E = BE->getSubExpr();
1890
1891 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001892 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001893 E = ICE->getSubExpr();
1894 else
1895 break;
1896 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001897
1898 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001899}
1900
John McCall558d2ab2010-09-15 10:14:12 +00001901/// isTemporaryObject - Determines if this expression produces a
1902/// temporary of the given class type.
1903bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1904 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1905 return false;
1906
Anders Carlssonf8b30152010-11-28 16:40:49 +00001907 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001908
John McCall58277b52010-09-15 20:59:13 +00001909 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001910 if (!E->Classify(C).isPRValue()) {
1911 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001912 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001913 return false;
1914 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001915
John McCall19e60ad2010-09-16 06:57:56 +00001916 // Black-list a few cases which yield pr-values of class type that don't
1917 // refer to temporaries of that type:
1918
1919 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001920 if (isa<ImplicitCastExpr>(E)) {
1921 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1922 case CK_DerivedToBase:
1923 case CK_UncheckedDerivedToBase:
1924 return false;
1925 default:
1926 break;
1927 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001928 }
1929
John McCall19e60ad2010-09-16 06:57:56 +00001930 // - member expressions (all)
1931 if (isa<MemberExpr>(E))
1932 return false;
1933
John McCall558d2ab2010-09-15 10:14:12 +00001934 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001935}
1936
Douglas Gregor898574e2008-12-05 23:32:09 +00001937/// hasAnyTypeDependentArguments - Determines if any of the expressions
1938/// in Exprs is type-dependent.
1939bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1940 for (unsigned I = 0; I < NumExprs; ++I)
1941 if (Exprs[I]->isTypeDependent())
1942 return true;
1943
1944 return false;
1945}
1946
1947/// hasAnyValueDependentArguments - Determines if any of the expressions
1948/// in Exprs is value-dependent.
1949bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1950 for (unsigned I = 0; I < NumExprs; ++I)
1951 if (Exprs[I]->isValueDependent())
1952 return true;
1953
1954 return false;
1955}
1956
John McCall4204f072010-08-02 21:13:48 +00001957bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001958 // This function is attempting whether an expression is an initializer
1959 // which can be evaluated at compile-time. isEvaluatable handles most
1960 // of the cases, but it can't deal with some initializer-specific
1961 // expressions, and it can't deal with aggregates; we deal with those here,
1962 // and fall back to isEvaluatable for the other cases.
1963
John McCall4204f072010-08-02 21:13:48 +00001964 // If we ever capture reference-binding directly in the AST, we can
1965 // kill the second parameter.
1966
1967 if (IsForRef) {
1968 EvalResult Result;
1969 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1970 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001971
Anders Carlssone8a32b82008-11-24 05:23:59 +00001972 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001973 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001974 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001975 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001976 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001977 return true;
John McCallb4b9b152010-08-01 21:51:45 +00001978 case CXXTemporaryObjectExprClass:
1979 case CXXConstructExprClass: {
1980 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00001981
1982 // Only if it's
1983 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00001984 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00001985 if (!CE->getNumArgs()) return true;
1986
1987 // 2) an elidable trivial copy construction of an operand which is
1988 // itself a constant initializer. Note that we consider the
1989 // operand on its own, *not* as a reference binding.
1990 return CE->isElidable() &&
1991 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00001992 }
Nate Begeman59b5da62009-01-18 03:20:47 +00001993 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001994 // This handles gcc's extension that allows global initializers like
1995 // "struct x {int x;} x = (struct x) {};".
1996 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001997 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00001998 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00001999 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002000 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002001 // FIXME: This doesn't deal with fields with reference types correctly.
2002 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2003 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002004 const InitListExpr *Exp = cast<InitListExpr>(this);
2005 unsigned numInits = Exp->getNumInits();
2006 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002007 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002008 return false;
2009 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002010 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002011 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002012 case ImplicitValueInitExprClass:
2013 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002014 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002015 return cast<ParenExpr>(this)->getSubExpr()
2016 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002017 case ChooseExprClass:
2018 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2019 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002020 case UnaryOperatorClass: {
2021 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002022 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002023 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002024 break;
2025 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002026 case BinaryOperatorClass: {
2027 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2028 // but this handles the common case.
2029 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002030 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002031 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2032 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2033 return true;
2034 break;
2035 }
John McCall4204f072010-08-02 21:13:48 +00002036 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002037 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002038 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002039 case CStyleCastExprClass:
2040 // Handle casts with a destination that's a struct or union; this
2041 // deals with both the gcc no-op struct cast extension and the
2042 // cast-to-union extension.
2043 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002044 return cast<CastExpr>(this)->getSubExpr()
2045 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002046
Chris Lattner430656e2009-10-13 22:12:09 +00002047 // Integer->integer casts can be handled here, which is important for
2048 // things like (int)(&&x-&&y). Scary but true.
2049 if (getType()->isIntegerType() &&
2050 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002051 return cast<CastExpr>(this)->getSubExpr()
2052 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002053
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002054 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002055 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002056 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002057}
2058
Reid Spencer5f016e22007-07-11 17:01:13 +00002059/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2060/// integer constant expression with the value zero, or if this is one that is
2061/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002062bool Expr::isNullPointerConstant(ASTContext &Ctx,
2063 NullPointerConstantValueDependence NPC) const {
2064 if (isValueDependent()) {
2065 switch (NPC) {
2066 case NPC_NeverValueDependent:
2067 assert(false && "Unexpected value dependent expression!");
2068 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002069
Douglas Gregorce940492009-09-25 04:25:58 +00002070 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002071 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002072
Douglas Gregorce940492009-09-25 04:25:58 +00002073 case NPC_ValueDependentIsNotNull:
2074 return false;
2075 }
2076 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002077
Sebastian Redl07779722008-10-31 14:43:28 +00002078 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002079 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002080 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002081 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002082 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002083 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002084 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002085 Pointee->isVoidType() && // to void*
2086 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002087 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002088 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002090 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2091 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002092 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002093 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2094 // Accept ((void*)0) as a null pointer constant, as many other
2095 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002096 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002097 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002098 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002099 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002100 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002101 } else if (isa<GNUNullExpr>(this)) {
2102 // The GNU __null extension is always a null pointer constant.
2103 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002104 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002105
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002106 // C++0x nullptr_t is always a null pointer constant.
2107 if (getType()->isNullPtrType())
2108 return true;
2109
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002110 if (const RecordType *UT = getType()->getAsUnionType())
2111 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2112 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2113 const Expr *InitExpr = CLE->getInitializer();
2114 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2115 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2116 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002117 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002118 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002119 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002120 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002121
Reid Spencer5f016e22007-07-11 17:01:13 +00002122 // If we have an integer constant expression, we need to *evaluate* it and
2123 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002124 llvm::APSInt Result;
2125 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002126}
Steve Naroff31a45842007-07-28 23:10:27 +00002127
John McCallf6a16482010-12-04 03:47:34 +00002128/// \brief If this expression is an l-value for an Objective C
2129/// property, find the underlying property reference expression.
2130const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2131 const Expr *E = this;
2132 while (true) {
2133 assert((E->getValueKind() == VK_LValue &&
2134 E->getObjectKind() == OK_ObjCProperty) &&
2135 "expression is not a property reference");
2136 E = E->IgnoreParenCasts();
2137 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2138 if (BO->getOpcode() == BO_Comma) {
2139 E = BO->getRHS();
2140 continue;
2141 }
2142 }
2143
2144 break;
2145 }
2146
2147 return cast<ObjCPropertyRefExpr>(E);
2148}
2149
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002150FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002151 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002152
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002153 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002154 if (ICE->getCastKind() == CK_LValueToRValue ||
2155 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002156 E = ICE->getSubExpr()->IgnoreParens();
2157 else
2158 break;
2159 }
2160
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002161 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002162 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002163 if (Field->isBitField())
2164 return Field;
2165
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002166 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2167 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2168 if (Field->isBitField())
2169 return Field;
2170
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002171 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2172 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2173 return BinOp->getLHS()->getBitField();
2174
2175 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002176}
2177
Anders Carlsson09380262010-01-31 17:18:49 +00002178bool Expr::refersToVectorElement() const {
2179 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002180
Anders Carlsson09380262010-01-31 17:18:49 +00002181 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002182 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002183 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002184 E = ICE->getSubExpr()->IgnoreParens();
2185 else
2186 break;
2187 }
Sean Huntc3021132010-05-05 15:23:54 +00002188
Anders Carlsson09380262010-01-31 17:18:49 +00002189 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2190 return ASE->getBase()->getType()->isVectorType();
2191
2192 if (isa<ExtVectorElementExpr>(E))
2193 return true;
2194
2195 return false;
2196}
2197
Chris Lattner2140e902009-02-16 22:14:05 +00002198/// isArrow - Return true if the base expression is a pointer to vector,
2199/// return false if the base expression is a vector.
2200bool ExtVectorElementExpr::isArrow() const {
2201 return getBase()->getType()->isPointerType();
2202}
2203
Nate Begeman213541a2008-04-18 23:10:10 +00002204unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002205 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002206 return VT->getNumElements();
2207 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002208}
2209
Nate Begeman8a997642008-05-09 06:41:27 +00002210/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002211bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002212 // FIXME: Refactor this code to an accessor on the AST node which returns the
2213 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002214 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002215
2216 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002217 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002218 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002219
Nate Begeman190d6a22009-01-18 02:01:21 +00002220 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002221 if (Comp[0] == 's' || Comp[0] == 'S')
2222 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Daniel Dunbar15027422009-10-17 23:53:04 +00002224 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2225 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002226 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002227
Steve Narofffec0b492007-07-30 03:29:09 +00002228 return false;
2229}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002230
Nate Begeman8a997642008-05-09 06:41:27 +00002231/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002232void ExtVectorElementExpr::getEncodedElementAccess(
2233 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002234 llvm::StringRef Comp = Accessor->getName();
2235 if (Comp[0] == 's' || Comp[0] == 'S')
2236 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002238 bool isHi = Comp == "hi";
2239 bool isLo = Comp == "lo";
2240 bool isEven = Comp == "even";
2241 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002242
Nate Begeman8a997642008-05-09 06:41:27 +00002243 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2244 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Nate Begeman8a997642008-05-09 06:41:27 +00002246 if (isHi)
2247 Index = e + i;
2248 else if (isLo)
2249 Index = i;
2250 else if (isEven)
2251 Index = 2 * i;
2252 else if (isOdd)
2253 Index = 2 * i + 1;
2254 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002255 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002256
Nate Begeman3b8d1162008-05-13 21:03:02 +00002257 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002258 }
Nate Begeman8a997642008-05-09 06:41:27 +00002259}
2260
Douglas Gregor04badcf2010-04-21 00:45:42 +00002261ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002262 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002263 SourceLocation LBracLoc,
2264 SourceLocation SuperLoc,
2265 bool IsInstanceSuper,
2266 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002267 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002268 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002269 ObjCMethodDecl *Method,
2270 Expr **Args, unsigned NumArgs,
2271 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002272 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002273 /*TypeDependent=*/false, /*ValueDependent=*/false,
2274 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002275 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2276 HasMethod(Method != 0), SuperLoc(SuperLoc),
2277 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2278 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002279 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002280{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002281 setReceiverPointer(SuperType.getAsOpaquePtr());
2282 if (NumArgs)
2283 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002284}
2285
Douglas Gregor04badcf2010-04-21 00:45:42 +00002286ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002287 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002288 SourceLocation LBracLoc,
2289 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002290 Selector Sel,
2291 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002292 ObjCMethodDecl *Method,
2293 Expr **Args, unsigned NumArgs,
2294 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002295 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002296 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002297 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2298 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2299 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002300 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002301{
2302 setReceiverPointer(Receiver);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002303 Stmt **MyArgs = getArgs();
2304 for (unsigned I = 0; I != NumArgs; ++I) {
2305 if (Args[I]->isTypeDependent())
2306 ExprBits.TypeDependent = true;
2307 if (Args[I]->isValueDependent())
2308 ExprBits.ValueDependent = true;
2309 if (Args[I]->containsUnexpandedParameterPack())
2310 ExprBits.ContainsUnexpandedParameterPack = true;
2311
2312 MyArgs[I] = Args[I];
2313 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002314}
2315
Douglas Gregor04badcf2010-04-21 00:45:42 +00002316ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002317 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002318 SourceLocation LBracLoc,
2319 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002320 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002321 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002322 ObjCMethodDecl *Method,
2323 Expr **Args, unsigned NumArgs,
2324 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002325 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002326 Receiver->isTypeDependent(),
2327 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002328 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2329 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2330 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002331 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002332{
2333 setReceiverPointer(Receiver);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002334 Stmt **MyArgs = getArgs();
2335 for (unsigned I = 0; I != NumArgs; ++I) {
2336 if (Args[I]->isTypeDependent())
2337 ExprBits.TypeDependent = true;
2338 if (Args[I]->isValueDependent())
2339 ExprBits.ValueDependent = true;
2340 if (Args[I]->containsUnexpandedParameterPack())
2341 ExprBits.ContainsUnexpandedParameterPack = true;
2342
2343 MyArgs[I] = Args[I];
2344 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002345}
2346
Douglas Gregor04badcf2010-04-21 00:45:42 +00002347ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002348 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002349 SourceLocation LBracLoc,
2350 SourceLocation SuperLoc,
2351 bool IsInstanceSuper,
2352 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002353 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002354 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002355 ObjCMethodDecl *Method,
2356 Expr **Args, unsigned NumArgs,
2357 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002358 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002359 NumArgs * sizeof(Expr *);
2360 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002361 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002362 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002363 RBracLoc);
2364}
2365
2366ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002367 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002368 SourceLocation LBracLoc,
2369 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002370 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002371 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002372 ObjCMethodDecl *Method,
2373 Expr **Args, unsigned NumArgs,
2374 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002375 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002376 NumArgs * sizeof(Expr *);
2377 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002378 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2379 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002380}
2381
2382ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002383 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002384 SourceLocation LBracLoc,
2385 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002386 Selector Sel,
2387 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002388 ObjCMethodDecl *Method,
2389 Expr **Args, unsigned NumArgs,
2390 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002391 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002392 NumArgs * sizeof(Expr *);
2393 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002394 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2395 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002396}
2397
Sean Huntc3021132010-05-05 15:23:54 +00002398ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002399 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002400 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002401 NumArgs * sizeof(Expr *);
2402 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2403 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2404}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002405
2406SourceRange ObjCMessageExpr::getReceiverRange() const {
2407 switch (getReceiverKind()) {
2408 case Instance:
2409 return getInstanceReceiver()->getSourceRange();
2410
2411 case Class:
2412 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2413
2414 case SuperInstance:
2415 case SuperClass:
2416 return getSuperLoc();
2417 }
2418
2419 return SourceLocation();
2420}
2421
Douglas Gregor04badcf2010-04-21 00:45:42 +00002422Selector ObjCMessageExpr::getSelector() const {
2423 if (HasMethod)
2424 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2425 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002426 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002427}
2428
2429ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2430 switch (getReceiverKind()) {
2431 case Instance:
2432 if (const ObjCObjectPointerType *Ptr
2433 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2434 return Ptr->getInterfaceDecl();
2435 break;
2436
2437 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002438 if (const ObjCObjectType *Ty
2439 = getClassReceiver()->getAs<ObjCObjectType>())
2440 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002441 break;
2442
2443 case SuperInstance:
2444 if (const ObjCObjectPointerType *Ptr
2445 = getSuperType()->getAs<ObjCObjectPointerType>())
2446 return Ptr->getInterfaceDecl();
2447 break;
2448
2449 case SuperClass:
2450 if (const ObjCObjectPointerType *Iface
2451 = getSuperType()->getAs<ObjCObjectPointerType>())
2452 return Iface->getInterfaceDecl();
2453 break;
2454 }
2455
2456 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002457}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002458
Chris Lattner27437ca2007-10-25 00:29:32 +00002459bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002460 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002461}
2462
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002463ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2464 QualType Type, SourceLocation BLoc,
2465 SourceLocation RP)
2466 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2467 Type->isDependentType(), Type->isDependentType(),
2468 Type->containsUnexpandedParameterPack()),
2469 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2470{
2471 SubExprs = new (C) Stmt*[nexpr];
2472 for (unsigned i = 0; i < nexpr; i++) {
2473 if (args[i]->isTypeDependent())
2474 ExprBits.TypeDependent = true;
2475 if (args[i]->isValueDependent())
2476 ExprBits.ValueDependent = true;
2477 if (args[i]->containsUnexpandedParameterPack())
2478 ExprBits.ContainsUnexpandedParameterPack = true;
2479
2480 SubExprs[i] = args[i];
2481 }
2482}
2483
Nate Begeman888376a2009-08-12 02:28:50 +00002484void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2485 unsigned NumExprs) {
2486 if (SubExprs) C.Deallocate(SubExprs);
2487
2488 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002489 this->NumExprs = NumExprs;
2490 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002491}
Nate Begeman888376a2009-08-12 02:28:50 +00002492
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002493//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002494// DesignatedInitExpr
2495//===----------------------------------------------------------------------===//
2496
2497IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2498 assert(Kind == FieldDesignator && "Only valid on a field designator");
2499 if (Field.NameOrField & 0x01)
2500 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2501 else
2502 return getField()->getIdentifier();
2503}
2504
Sean Huntc3021132010-05-05 15:23:54 +00002505DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002506 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002507 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002508 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002509 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002510 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002511 unsigned NumIndexExprs,
2512 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002513 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002514 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002515 Init->isTypeDependent(), Init->isValueDependent(),
2516 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002517 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2518 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002519 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002520
2521 // Record the initializer itself.
2522 child_iterator Child = child_begin();
2523 *Child++ = Init;
2524
2525 // Copy the designators and their subexpressions, computing
2526 // value-dependence along the way.
2527 unsigned IndexIdx = 0;
2528 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002529 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002530
2531 if (this->Designators[I].isArrayDesignator()) {
2532 // Compute type- and value-dependence.
2533 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002534 if (Index->isTypeDependent() || Index->isValueDependent())
2535 ExprBits.ValueDependent = true;
2536
2537 // Propagate unexpanded parameter packs.
2538 if (Index->containsUnexpandedParameterPack())
2539 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002540
2541 // Copy the index expressions into permanent storage.
2542 *Child++ = IndexExprs[IndexIdx++];
2543 } else if (this->Designators[I].isArrayRangeDesignator()) {
2544 // Compute type- and value-dependence.
2545 Expr *Start = IndexExprs[IndexIdx];
2546 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002547 if (Start->isTypeDependent() || Start->isValueDependent() ||
2548 End->isTypeDependent() || End->isValueDependent())
2549 ExprBits.ValueDependent = true;
2550
2551 // Propagate unexpanded parameter packs.
2552 if (Start->containsUnexpandedParameterPack() ||
2553 End->containsUnexpandedParameterPack())
2554 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002555
2556 // Copy the start/end expressions into permanent storage.
2557 *Child++ = IndexExprs[IndexIdx++];
2558 *Child++ = IndexExprs[IndexIdx++];
2559 }
2560 }
2561
2562 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002563}
2564
Douglas Gregor05c13a32009-01-22 00:58:24 +00002565DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002566DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002567 unsigned NumDesignators,
2568 Expr **IndexExprs, unsigned NumIndexExprs,
2569 SourceLocation ColonOrEqualLoc,
2570 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002571 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002572 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002573 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002574 ColonOrEqualLoc, UsesColonSyntax,
2575 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002576}
2577
Mike Stump1eb44332009-09-09 15:08:12 +00002578DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002579 unsigned NumIndexExprs) {
2580 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2581 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2582 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2583}
2584
Douglas Gregor319d57f2010-01-06 23:17:19 +00002585void DesignatedInitExpr::setDesignators(ASTContext &C,
2586 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002587 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002588 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002589 NumDesignators = NumDesigs;
2590 for (unsigned I = 0; I != NumDesigs; ++I)
2591 Designators[I] = Desigs[I];
2592}
2593
Douglas Gregor05c13a32009-01-22 00:58:24 +00002594SourceRange DesignatedInitExpr::getSourceRange() const {
2595 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002596 Designator &First =
2597 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002598 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002599 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002600 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2601 else
2602 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2603 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002604 StartLoc =
2605 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002606 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2607}
2608
Douglas Gregor05c13a32009-01-22 00:58:24 +00002609Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2610 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2611 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2612 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002613 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2614 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2615}
2616
2617Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002618 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002619 "Requires array range designator");
2620 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2621 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002622 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2623 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2624}
2625
2626Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002627 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002628 "Requires array range designator");
2629 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2630 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002631 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2632 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2633}
2634
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002635/// \brief Replaces the designator at index @p Idx with the series
2636/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002637void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002638 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002639 const Designator *Last) {
2640 unsigned NumNewDesignators = Last - First;
2641 if (NumNewDesignators == 0) {
2642 std::copy_backward(Designators + Idx + 1,
2643 Designators + NumDesignators,
2644 Designators + Idx);
2645 --NumNewDesignators;
2646 return;
2647 } else if (NumNewDesignators == 1) {
2648 Designators[Idx] = *First;
2649 return;
2650 }
2651
Mike Stump1eb44332009-09-09 15:08:12 +00002652 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002653 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002654 std::copy(Designators, Designators + Idx, NewDesignators);
2655 std::copy(First, Last, NewDesignators + Idx);
2656 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2657 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002658 Designators = NewDesignators;
2659 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2660}
2661
Mike Stump1eb44332009-09-09 15:08:12 +00002662ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002663 Expr **exprs, unsigned nexprs,
2664 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002665 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2666 false, false, false),
2667 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Nate Begeman2ef13e52009-08-10 23:49:36 +00002669 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002670 for (unsigned i = 0; i != nexprs; ++i) {
2671 if (exprs[i]->isTypeDependent())
2672 ExprBits.TypeDependent = true;
2673 if (exprs[i]->isValueDependent())
2674 ExprBits.ValueDependent = true;
2675 if (exprs[i]->containsUnexpandedParameterPack())
2676 ExprBits.ContainsUnexpandedParameterPack = true;
2677
Nate Begeman2ef13e52009-08-10 23:49:36 +00002678 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002679 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002680}
2681
Douglas Gregor05c13a32009-01-22 00:58:24 +00002682//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002683// ExprIterator.
2684//===----------------------------------------------------------------------===//
2685
2686Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2687Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2688Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2689const Expr* ConstExprIterator::operator[](size_t idx) const {
2690 return cast<Expr>(I[idx]);
2691}
2692const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2693const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2694
2695//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002696// Child Iterators for iterating over subexpressions/substatements
2697//===----------------------------------------------------------------------===//
2698
2699// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002700Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2701Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002702
Steve Naroff7779db42007-11-12 14:29:37 +00002703// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002704Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2705Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002706
Steve Naroffe3e9add2008-06-02 23:03:37 +00002707// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002708Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2709{
John McCall12f78a62010-12-02 01:19:52 +00002710 if (Receiver.is<Stmt*>()) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002711 // Hack alert!
John McCall12f78a62010-12-02 01:19:52 +00002712 return reinterpret_cast<Stmt**> (&Receiver);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002713 }
2714 return child_iterator();
2715}
2716
2717Stmt::child_iterator ObjCPropertyRefExpr::child_end()
John McCall12f78a62010-12-02 01:19:52 +00002718{ return Receiver.is<Stmt*>() ?
2719 reinterpret_cast<Stmt**> (&Receiver)+1 :
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002720 child_iterator();
2721}
Steve Naroffae784072008-05-30 00:40:33 +00002722
Steve Narofff242b1b2009-07-24 17:54:45 +00002723// ObjCIsaExpr
2724Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2725Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2726
Chris Lattnerd9f69102008-08-10 01:53:14 +00002727// PredefinedExpr
2728Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2729Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002730
2731// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002732Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2733Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002734
2735// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002736Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002737Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002738
2739// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002740Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2741Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002742
Chris Lattner5d661452007-08-26 03:42:43 +00002743// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002744Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2745Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002746
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002747// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002748Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2749Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002750
2751// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002752Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2753Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002754
2755// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002756Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2757Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002758
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002759// OffsetOfExpr
2760Stmt::child_iterator OffsetOfExpr::child_begin() {
2761 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2762 + NumComps);
2763}
2764Stmt::child_iterator OffsetOfExpr::child_end() {
2765 return child_iterator(&*child_begin() + NumExprs);
2766}
2767
Sebastian Redl05189992008-11-11 17:56:53 +00002768// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002769Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002770 // If this is of a type and the type is a VLA type (and not a typedef), the
2771 // size expression of the VLA needs to be treated as an executable expression.
2772 // Why isn't this weirdness documented better in StmtIterator?
2773 if (isArgumentType()) {
2774 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2775 getArgumentType().getTypePtr()))
2776 return child_iterator(T);
2777 return child_iterator();
2778 }
Sebastian Redld4575892008-12-03 23:17:54 +00002779 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002780}
Sebastian Redl05189992008-11-11 17:56:53 +00002781Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2782 if (isArgumentType())
2783 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002784 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002785}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002786
2787// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002788Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002789 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002790}
Ted Kremenek1237c672007-08-24 20:06:47 +00002791Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002792 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002793}
2794
2795// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002796Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002797 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002798}
Ted Kremenek1237c672007-08-24 20:06:47 +00002799Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002800 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002801}
Ted Kremenek1237c672007-08-24 20:06:47 +00002802
2803// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002804Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2805Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002806
Nate Begeman213541a2008-04-18 23:10:10 +00002807// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002808Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2809Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002810
2811// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002812Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2813Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002814
Ted Kremenek1237c672007-08-24 20:06:47 +00002815// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002816Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2817Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002818
2819// BinaryOperator
2820Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002821 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002822}
Ted Kremenek1237c672007-08-24 20:06:47 +00002823Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002824 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002825}
2826
2827// ConditionalOperator
2828Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002829 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002830}
Ted Kremenek1237c672007-08-24 20:06:47 +00002831Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002832 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002833}
2834
2835// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002836Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2837Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002838
Ted Kremenek1237c672007-08-24 20:06:47 +00002839// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002840Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2841Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002842
Ted Kremenek1237c672007-08-24 20:06:47 +00002843
2844// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002845Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2846Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002847
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002848// GNUNullExpr
2849Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2850Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2851
Eli Friedmand38617c2008-05-14 19:38:39 +00002852// ShuffleVectorExpr
2853Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002854 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002855}
2856Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002857 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002858}
2859
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002860// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002861Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2862Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002863
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002864// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002865Stmt::child_iterator InitListExpr::child_begin() {
2866 return InitExprs.size() ? &InitExprs[0] : 0;
2867}
2868Stmt::child_iterator InitListExpr::child_end() {
2869 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2870}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002871
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002872// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002873Stmt::child_iterator DesignatedInitExpr::child_begin() {
2874 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2875 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002876 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2877}
2878Stmt::child_iterator DesignatedInitExpr::child_end() {
2879 return child_iterator(&*child_begin() + NumSubExprs);
2880}
2881
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002882// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002883Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2884 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002885}
2886
Mike Stump1eb44332009-09-09 15:08:12 +00002887Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2888 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002889}
2890
Nate Begeman2ef13e52009-08-10 23:49:36 +00002891// ParenListExpr
2892Stmt::child_iterator ParenListExpr::child_begin() {
2893 return &Exprs[0];
2894}
2895Stmt::child_iterator ParenListExpr::child_end() {
2896 return &Exprs[0]+NumExprs;
2897}
2898
Ted Kremenek1237c672007-08-24 20:06:47 +00002899// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002900Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002901 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002902}
2903Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002904 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002905}
Ted Kremenek1237c672007-08-24 20:06:47 +00002906
2907// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002908Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2909Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002910
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002911// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002912Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002913 return child_iterator();
2914}
2915Stmt::child_iterator ObjCSelectorExpr::child_end() {
2916 return child_iterator();
2917}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002918
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002919// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002920Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2921 return child_iterator();
2922}
2923Stmt::child_iterator ObjCProtocolExpr::child_end() {
2924 return child_iterator();
2925}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002926
Steve Naroff563477d2007-09-18 23:55:05 +00002927// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002928Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002929 if (getReceiverKind() == Instance)
2930 return reinterpret_cast<Stmt **>(this + 1);
2931 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002932}
2933Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002934 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002935}
2936
Steve Naroff4eb206b2008-09-03 18:15:37 +00002937// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002938Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2939Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002940
Ted Kremenek9da13f92008-09-26 23:24:14 +00002941Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2942Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002943
2944// OpaqueValueExpr
2945SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2946Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2947Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2948