blob: 04498f7b9a9448e2d376134bbbe5f8b44f22a368 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000030#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattnerbef0efd2010-05-13 01:02:19 +000033void Expr::ANCHOR() {} // key function for Expr class.
34
Chris Lattner2b334bb2010-04-16 23:34:13 +000035/// isKnownToHaveBooleanValue - Return true if this is an integer expression
36/// that is known to return 0 or 1. This happens for _Bool/bool expressions
37/// but also int expressions which are produced by things like comparisons in
38/// C.
39bool Expr::isKnownToHaveBooleanValue() const {
40 // If this value has _Bool type, it is obvious 0/1.
41 if (getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregor2ade35e2010-06-16 00:17:44 +000043 if (!getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000044
Chris Lattner2b334bb2010-04-16 23:34:13 +000045 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
46 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000047
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
49 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
51 case UO_Extension:
Chris Lattner2b334bb2010-04-16 23:34:13 +000052 return UO->getSubExpr()->isKnownToHaveBooleanValue();
53 default:
54 return false;
55 }
56 }
Sean Huntc3021132010-05-05 15:23:54 +000057
John McCall6907fbe2010-06-12 01:56:02 +000058 // Only look through implicit casts. If the user writes
59 // '(int) (a && b)' treat it as an arbitrary int.
60 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000062
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
64 switch (BO->getOpcode()) {
65 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000066 case BO_LT: // Relational operators.
67 case BO_GT:
68 case BO_LE:
69 case BO_GE:
70 case BO_EQ: // Equality operators.
71 case BO_NE:
72 case BO_LAnd: // AND operator.
73 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000074 return true;
Sean Huntc3021132010-05-05 15:23:54 +000075
John McCall2de56d12010-08-25 11:45:40 +000076 case BO_And: // Bitwise AND operator.
77 case BO_Xor: // Bitwise XOR operator.
78 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000079 // Handle things like (x==2)|(y==12).
80 return BO->getLHS()->isKnownToHaveBooleanValue() &&
81 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000082
John McCall2de56d12010-08-25 11:45:40 +000083 case BO_Comma:
84 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000085 return BO->getRHS()->isKnownToHaveBooleanValue();
86 }
87 }
Sean Huntc3021132010-05-05 15:23:54 +000088
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
90 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
91 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000092
Chris Lattner2b334bb2010-04-16 23:34:13 +000093 return false;
94}
95
Reid Spencer5f016e22007-07-11 17:01:13 +000096//===----------------------------------------------------------------------===//
97// Primary Expressions.
98//===----------------------------------------------------------------------===//
99
John McCalld5532b62009-11-23 01:53:49 +0000100void ExplicitTemplateArgumentList::initializeFrom(
101 const TemplateArgumentListInfo &Info) {
102 LAngleLoc = Info.getLAngleLoc();
103 RAngleLoc = Info.getRAngleLoc();
104 NumTemplateArgs = Info.size();
105
106 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
107 for (unsigned i = 0; i != NumTemplateArgs; ++i)
108 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
109}
110
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000111void ExplicitTemplateArgumentList::initializeFrom(
112 const TemplateArgumentListInfo &Info,
113 bool &Dependent,
114 bool &ContainsUnexpandedParameterPack) {
115 LAngleLoc = Info.getLAngleLoc();
116 RAngleLoc = Info.getRAngleLoc();
117 NumTemplateArgs = Info.size();
118
119 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
120 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
121 Dependent = Dependent || Info[i].getArgument().isDependent();
122 ContainsUnexpandedParameterPack
123 = ContainsUnexpandedParameterPack ||
124 Info[i].getArgument().containsUnexpandedParameterPack();
125
126 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
127 }
128}
129
John McCalld5532b62009-11-23 01:53:49 +0000130void ExplicitTemplateArgumentList::copyInto(
131 TemplateArgumentListInfo &Info) const {
132 Info.setLAngleLoc(LAngleLoc);
133 Info.setRAngleLoc(RAngleLoc);
134 for (unsigned I = 0; I != NumTemplateArgs; ++I)
135 Info.addArgument(getTemplateArgs()[I]);
136}
137
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000138std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
139 return sizeof(ExplicitTemplateArgumentList) +
140 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
141}
142
John McCalld5532b62009-11-23 01:53:49 +0000143std::size_t ExplicitTemplateArgumentList::sizeFor(
144 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000145 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000146}
147
Douglas Gregord967e312011-01-19 21:52:31 +0000148/// \brief Compute the type- and value-dependence of a declaration reference
149/// based on the declaration being referenced.
150static void computeDeclRefDependence(NamedDecl *D, QualType T,
151 bool &TypeDependent,
152 bool &ValueDependent) {
153 TypeDependent = false;
154 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000155
Douglas Gregor0da76df2009-11-23 11:41:28 +0000156
157 // (TD) C++ [temp.dep.expr]p3:
158 // An id-expression is type-dependent if it contains:
159 //
Sean Huntc3021132010-05-05 15:23:54 +0000160 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000161 //
162 // (VD) C++ [temp.dep.constexpr]p2:
163 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000164
Douglas Gregor0da76df2009-11-23 11:41:28 +0000165 // (TD) - an identifier that was declared with dependent type
166 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (T->isDependentType()) {
168 TypeDependent = true;
169 ValueDependent = true;
170 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000171 }
Douglas Gregord967e312011-01-19 21:52:31 +0000172
Douglas Gregor0da76df2009-11-23 11:41:28 +0000173 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000174 if (D->getDeclName().getNameKind()
175 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000176 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000177 TypeDependent = true;
178 ValueDependent = true;
179 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000180 }
181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
184 return;
185 }
186
Douglas Gregor0da76df2009-11-23 11:41:28 +0000187 // (VD) - a constant with integral or enumeration type and is
188 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000189 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000190 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000191 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000192 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000193 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000194 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000195 }
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000197 // (VD) - FIXME: Missing from the standard:
198 // - a member function or a static data member of the current
199 // instantiation
200 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000201 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000202 ValueDependent = true;
203
204 return;
205 }
206
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000207 // (VD) - FIXME: Missing from the standard:
208 // - a member function or a static data member of the current
209 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000210 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
211 ValueDependent = true;
212 return;
213 }
214}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000215
Douglas Gregord967e312011-01-19 21:52:31 +0000216void DeclRefExpr::computeDependence() {
217 bool TypeDependent = false;
218 bool ValueDependent = false;
219 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
220
221 // (TD) C++ [temp.dep.expr]p3:
222 // An id-expression is type-dependent if it contains:
223 //
224 // and
225 //
226 // (VD) C++ [temp.dep.constexpr]p2:
227 // An identifier is value-dependent if it is:
228 if (!TypeDependent && !ValueDependent &&
229 hasExplicitTemplateArgs() &&
230 TemplateSpecializationType::anyDependentTemplateArguments(
231 getTemplateArgs(),
232 getNumTemplateArgs())) {
233 TypeDependent = true;
234 ValueDependent = true;
235 }
236
237 ExprBits.TypeDependent = TypeDependent;
238 ExprBits.ValueDependent = ValueDependent;
239
Douglas Gregor10738d32010-12-23 23:51:58 +0000240 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000241 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000242 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000243}
244
Sean Huntc3021132010-05-05 15:23:54 +0000245DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000246 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000247 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000248 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000249 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000250 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000251 DecoratedD(D,
252 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000253 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000254 Loc(NameLoc) {
255 if (Qualifier) {
256 NameQualifier *NQ = getNameQualifier();
257 NQ->NNS = Qualifier;
258 NQ->Range = QualifierRange;
259 }
Sean Huntc3021132010-05-05 15:23:54 +0000260
John McCalld5532b62009-11-23 01:53:49 +0000261 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000262 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000263
264 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000265}
266
Abramo Bagnara25777432010-08-11 22:01:17 +0000267DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
268 SourceRange QualifierRange,
269 ValueDecl *D, const DeclarationNameInfo &NameInfo,
270 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000271 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000272 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000273 DecoratedD(D,
274 (Qualifier? HasQualifierFlag : 0) |
275 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
276 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
277 if (Qualifier) {
278 NameQualifier *NQ = getNameQualifier();
279 NQ->NNS = Qualifier;
280 NQ->Range = QualifierRange;
281 }
282
283 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000284 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000285
286 computeDependence();
287}
288
Douglas Gregora2813ce2009-10-23 18:54:35 +0000289DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
290 NestedNameSpecifier *Qualifier,
291 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000292 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000293 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000294 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000295 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000296 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000297 return Create(Context, Qualifier, QualifierRange, D,
298 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000299 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000300}
301
302DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
303 NestedNameSpecifier *Qualifier,
304 SourceRange QualifierRange,
305 ValueDecl *D,
306 const DeclarationNameInfo &NameInfo,
307 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000308 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000310 std::size_t Size = sizeof(DeclRefExpr);
311 if (Qualifier != 0)
312 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000313
John McCalld5532b62009-11-23 01:53:49 +0000314 if (TemplateArgs)
315 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000316
Chris Lattner32488542010-10-30 05:14:06 +0000317 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara25777432010-08-11 22:01:17 +0000318 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCallf89e55a2010-11-18 06:31:45 +0000319 TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000320}
321
Douglas Gregordef03542011-02-04 12:01:24 +0000322DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
323 bool HasQualifier,
324 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000325 unsigned NumTemplateArgs) {
326 std::size_t Size = sizeof(DeclRefExpr);
327 if (HasQualifier)
328 Size += sizeof(NameQualifier);
329
Douglas Gregordef03542011-02-04 12:01:24 +0000330 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000331 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
332
Chris Lattner32488542010-10-30 05:14:06 +0000333 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000334 return new (Mem) DeclRefExpr(EmptyShell());
335}
336
Douglas Gregora2813ce2009-10-23 18:54:35 +0000337SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000338 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000339 if (hasQualifier())
340 R.setBegin(getQualifierRange().getBegin());
John McCall096832c2010-08-19 23:49:38 +0000341 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000342 R.setEnd(getRAngleLoc());
343 return R;
344}
345
Anders Carlsson3a082d82009-09-08 18:24:21 +0000346// FIXME: Maybe this should use DeclPrinter with a special "print predefined
347// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000348std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
349 ASTContext &Context = CurrentDecl->getASTContext();
350
Anders Carlsson3a082d82009-09-08 18:24:21 +0000351 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000352 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000353 return FD->getNameAsString();
354
355 llvm::SmallString<256> Name;
356 llvm::raw_svector_ostream Out(Name);
357
358 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000359 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000360 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000361 if (MD->isStatic())
362 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000363 }
364
365 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000366
367 std::string Proto = FD->getQualifiedNameAsString(Policy);
368
John McCall183700f2009-09-21 23:43:11 +0000369 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000370 const FunctionProtoType *FT = 0;
371 if (FD->hasWrittenPrototype())
372 FT = dyn_cast<FunctionProtoType>(AFT);
373
374 Proto += "(";
375 if (FT) {
376 llvm::raw_string_ostream POut(Proto);
377 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
378 if (i) POut << ", ";
379 std::string Param;
380 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
381 POut << Param;
382 }
383
384 if (FT->isVariadic()) {
385 if (FD->getNumParams()) POut << ", ";
386 POut << "...";
387 }
388 }
389 Proto += ")";
390
Sam Weinig4eadcc52009-12-27 01:38:20 +0000391 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
392 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
393 if (ThisQuals.hasConst())
394 Proto += " const";
395 if (ThisQuals.hasVolatile())
396 Proto += " volatile";
397 }
398
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000399 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
400 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401
402 Out << Proto;
403
404 Out.flush();
405 return Name.str().str();
406 }
407 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
408 llvm::SmallString<256> Name;
409 llvm::raw_svector_ostream Out(Name);
410 Out << (MD->isInstanceMethod() ? '-' : '+');
411 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000412
413 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
414 // a null check to avoid a crash.
415 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000416 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000417
Anders Carlsson3a082d82009-09-08 18:24:21 +0000418 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000419 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
420 Out << '(' << CID << ')';
421
Anders Carlsson3a082d82009-09-08 18:24:21 +0000422 Out << ' ';
423 Out << MD->getSelector().getAsString();
424 Out << ']';
425
426 Out.flush();
427 return Name.str().str();
428 }
429 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
430 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
431 return "top level";
432 }
433 return "";
434}
435
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000436void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
437 if (hasAllocation())
438 C.Deallocate(pVal);
439
440 BitWidth = Val.getBitWidth();
441 unsigned NumWords = Val.getNumWords();
442 const uint64_t* Words = Val.getRawData();
443 if (NumWords > 1) {
444 pVal = new (C) uint64_t[NumWords];
445 std::copy(Words, Words + NumWords, pVal);
446 } else if (NumWords == 1)
447 VAL = Words[0];
448 else
449 VAL = 0;
450}
451
452IntegerLiteral *
453IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
454 QualType type, SourceLocation l) {
455 return new (C) IntegerLiteral(C, V, type, l);
456}
457
458IntegerLiteral *
459IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
460 return new (C) IntegerLiteral(Empty);
461}
462
463FloatingLiteral *
464FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
465 bool isexact, QualType Type, SourceLocation L) {
466 return new (C) FloatingLiteral(C, V, isexact, Type, L);
467}
468
469FloatingLiteral *
470FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
471 return new (C) FloatingLiteral(Empty);
472}
473
Chris Lattnerda8249e2008-06-07 22:13:43 +0000474/// getValueAsApproximateDouble - This returns the value as an inaccurate
475/// double. Note that this may cause loss of precision, but is useful for
476/// debugging dumps, etc.
477double FloatingLiteral::getValueAsApproximateDouble() const {
478 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000479 bool ignored;
480 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
481 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000482 return V.convertToDouble();
483}
484
Chris Lattner2085fd62009-02-18 06:40:38 +0000485StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
486 unsigned ByteLength, bool Wide,
487 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000488 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000489 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000490 // Allocate enough space for the StringLiteral plus an array of locations for
491 // any concatenated string tokens.
492 void *Mem = C.Allocate(sizeof(StringLiteral)+
493 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000494 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000495 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000498 char *AStrData = new (C, 1) char[ByteLength];
499 memcpy(AStrData, StrData, ByteLength);
500 SL->StrData = AStrData;
501 SL->ByteLength = ByteLength;
502 SL->IsWide = Wide;
503 SL->TokLocs[0] = Loc[0];
504 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000505
Chris Lattner726e1682009-02-18 05:49:11 +0000506 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000507 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
508 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000509}
510
Douglas Gregor673ecd62009-04-15 16:35:07 +0000511StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
512 void *Mem = C.Allocate(sizeof(StringLiteral)+
513 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000514 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000515 StringLiteral *SL = new (Mem) StringLiteral(QualType());
516 SL->StrData = 0;
517 SL->ByteLength = 0;
518 SL->NumConcatenated = NumStrs;
519 return SL;
520}
521
Daniel Dunbarb6480232009-09-22 03:27:33 +0000522void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000523 char *AStrData = new (C, 1) char[Str.size()];
524 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000525 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000526 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000527}
528
Chris Lattner08f92e32010-11-17 07:37:15 +0000529/// getLocationOfByte - Return a source location that points to the specified
530/// byte of this string literal.
531///
532/// Strings are amazingly complex. They can be formed from multiple tokens and
533/// can have escape sequences in them in addition to the usual trigraph and
534/// escaped newline business. This routine handles this complexity.
535///
536SourceLocation StringLiteral::
537getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
538 const LangOptions &Features, const TargetInfo &Target) const {
539 assert(!isWide() && "This doesn't work for wide strings yet");
540
541 // Loop over all of the tokens in this string until we find the one that
542 // contains the byte we're looking for.
543 unsigned TokNo = 0;
544 while (1) {
545 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
546 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
547
548 // Get the spelling of the string so that we can get the data that makes up
549 // the string literal, not the identifier for the macro it is potentially
550 // expanded through.
551 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
552
553 // Re-lex the token to get its length and original spelling.
554 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
555 bool Invalid = false;
556 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
557 if (Invalid)
558 return StrTokSpellingLoc;
559
560 const char *StrData = Buffer.data()+LocInfo.second;
561
562 // Create a langops struct and enable trigraphs. This is sufficient for
563 // relexing tokens.
564 LangOptions LangOpts;
565 LangOpts.Trigraphs = true;
566
567 // Create a lexer starting at the beginning of this token.
568 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
569 Buffer.end());
570 Token TheTok;
571 TheLexer.LexFromRawLexer(TheTok);
572
573 // Use the StringLiteralParser to compute the length of the string in bytes.
574 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
575 unsigned TokNumBytes = SLP.GetStringLength();
576
577 // If the byte is in this token, return the location of the byte.
578 if (ByteNo < TokNumBytes ||
579 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
580 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
581
582 // Now that we know the offset of the token in the spelling, use the
583 // preprocessor to get the offset in the original source.
584 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
585 }
586
587 // Move to the next string token.
588 ++TokNo;
589 ByteNo -= TokNumBytes;
590 }
591}
592
593
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
596/// corresponds to, e.g. "sizeof" or "[pre]++".
597const char *UnaryOperator::getOpcodeStr(Opcode Op) {
598 switch (Op) {
599 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000600 case UO_PostInc: return "++";
601 case UO_PostDec: return "--";
602 case UO_PreInc: return "++";
603 case UO_PreDec: return "--";
604 case UO_AddrOf: return "&";
605 case UO_Deref: return "*";
606 case UO_Plus: return "+";
607 case UO_Minus: return "-";
608 case UO_Not: return "~";
609 case UO_LNot: return "!";
610 case UO_Real: return "__real";
611 case UO_Imag: return "__imag";
612 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 }
614}
615
John McCall2de56d12010-08-25 11:45:40 +0000616UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000617UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
618 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000619 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000620 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
621 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
622 case OO_Amp: return UO_AddrOf;
623 case OO_Star: return UO_Deref;
624 case OO_Plus: return UO_Plus;
625 case OO_Minus: return UO_Minus;
626 case OO_Tilde: return UO_Not;
627 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000628 }
629}
630
631OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
632 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000633 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
634 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
635 case UO_AddrOf: return OO_Amp;
636 case UO_Deref: return OO_Star;
637 case UO_Plus: return OO_Plus;
638 case UO_Minus: return OO_Minus;
639 case UO_Not: return OO_Tilde;
640 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000641 default: return OO_None;
642 }
643}
644
645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646//===----------------------------------------------------------------------===//
647// Postfix Operators.
648//===----------------------------------------------------------------------===//
649
Ted Kremenek668bf912009-02-09 20:51:47 +0000650CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
John McCallf89e55a2010-11-18 06:31:45 +0000651 unsigned numargs, QualType t, ExprValueKind VK,
652 SourceLocation rparenloc)
653 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000654 fn->isTypeDependent(),
655 fn->isValueDependent(),
656 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000657 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Ted Kremenek668bf912009-02-09 20:51:47 +0000659 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000660 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000661 for (unsigned i = 0; i != numargs; ++i) {
662 if (args[i]->isTypeDependent())
663 ExprBits.TypeDependent = true;
664 if (args[i]->isValueDependent())
665 ExprBits.ValueDependent = true;
666 if (args[i]->containsUnexpandedParameterPack())
667 ExprBits.ContainsUnexpandedParameterPack = true;
668
Douglas Gregorb4609802008-11-14 16:09:21 +0000669 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000670 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000671
Douglas Gregorb4609802008-11-14 16:09:21 +0000672 RParenLoc = rparenloc;
673}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000674
Ted Kremenek668bf912009-02-09 20:51:47 +0000675CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000676 QualType t, ExprValueKind VK, SourceLocation rparenloc)
677 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000678 fn->isTypeDependent(),
679 fn->isValueDependent(),
680 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000681 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000682
683 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000684 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000685 for (unsigned i = 0; i != numargs; ++i) {
686 if (args[i]->isTypeDependent())
687 ExprBits.TypeDependent = true;
688 if (args[i]->isValueDependent())
689 ExprBits.ValueDependent = true;
690 if (args[i]->containsUnexpandedParameterPack())
691 ExprBits.ContainsUnexpandedParameterPack = true;
692
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000693 SubExprs[i+ARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000694 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 RParenLoc = rparenloc;
697}
698
Mike Stump1eb44332009-09-09 15:08:12 +0000699CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
700 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000701 // FIXME: Why do we allocate this?
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000702 SubExprs = new (C) Stmt*[1];
703}
704
Nuno Lopesd20254f2009-12-20 23:11:08 +0000705Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000706 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000707 // If we're calling a dereference, look at the pointer instead.
708 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
709 if (BO->isPtrMemOp())
710 CEE = BO->getRHS()->IgnoreParenCasts();
711 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
712 if (UO->getOpcode() == UO_Deref)
713 CEE = UO->getSubExpr()->IgnoreParenCasts();
714 }
Chris Lattner6346f962009-07-17 15:46:27 +0000715 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000716 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000717 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
718 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000719
720 return 0;
721}
722
Nuno Lopesd20254f2009-12-20 23:11:08 +0000723FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000724 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000725}
726
Chris Lattnerd18b3292007-12-28 05:25:02 +0000727/// setNumArgs - This changes the number of arguments present in this call.
728/// Any orphaned expressions are deleted by this, and any new operands are set
729/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000730void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000731 // No change, just return.
732 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattnerd18b3292007-12-28 05:25:02 +0000734 // If shrinking # arguments, just delete the extras and forgot them.
735 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000736 this->NumArgs = NumArgs;
737 return;
738 }
739
740 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000741 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000742 // Copy over args.
743 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
744 NewSubExprs[i] = SubExprs[i];
745 // Null out new args.
746 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
747 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregor88c9a462009-04-17 21:46:47 +0000749 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000750 SubExprs = NewSubExprs;
751 this->NumArgs = NumArgs;
752}
753
Chris Lattnercb888962008-10-06 05:00:53 +0000754/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
755/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000756unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000757 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000758 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000759 // ImplicitCastExpr.
760 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
761 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000762 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000764 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
765 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000766 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Anders Carlssonbcba2012008-01-31 02:13:57 +0000768 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
769 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000770 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000772 if (!FDecl->getIdentifier())
773 return 0;
774
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000775 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000776}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000777
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000778QualType CallExpr::getCallReturnType() const {
779 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000780 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000781 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000782 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000783 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000784 else if (const MemberPointerType *MPT
785 = CalleeType->getAs<MemberPointerType>())
786 CalleeType = MPT->getPointeeType();
787
John McCall183700f2009-09-21 23:43:11 +0000788 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000789 return FnType->getResultType();
790}
Chris Lattnercb888962008-10-06 05:00:53 +0000791
Sean Huntc3021132010-05-05 15:23:54 +0000792OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000793 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000794 TypeSourceInfo *tsi,
795 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000796 Expr** exprsPtr, unsigned numExprs,
797 SourceLocation RParenLoc) {
798 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000799 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000800 sizeof(Expr*) * numExprs);
801
802 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
803 exprsPtr, numExprs, RParenLoc);
804}
805
806OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
807 unsigned numComps, unsigned numExprs) {
808 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
809 sizeof(OffsetOfNode) * numComps +
810 sizeof(Expr*) * numExprs);
811 return new (Mem) OffsetOfExpr(numComps, numExprs);
812}
813
Sean Huntc3021132010-05-05 15:23:54 +0000814OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000815 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000816 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000817 Expr** exprsPtr, unsigned numExprs,
818 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000819 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
820 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000821 /*ValueDependent=*/tsi->getType()->isDependentType(),
822 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000823 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
824 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000825{
826 for(unsigned i = 0; i < numComps; ++i) {
827 setComponent(i, compsPtr[i]);
828 }
Sean Huntc3021132010-05-05 15:23:54 +0000829
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000830 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000831 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
832 ExprBits.ValueDependent = true;
833 if (exprsPtr[i]->containsUnexpandedParameterPack())
834 ExprBits.ContainsUnexpandedParameterPack = true;
835
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000836 setIndexExpr(i, exprsPtr[i]);
837 }
838}
839
840IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
841 assert(getKind() == Field || getKind() == Identifier);
842 if (getKind() == Field)
843 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000844
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000845 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
846}
847
Mike Stump1eb44332009-09-09 15:08:12 +0000848MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
849 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000850 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000851 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000852 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000853 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000854 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000855 QualType ty,
856 ExprValueKind vk,
857 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000858 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000859
John McCall161755a2010-04-06 21:38:20 +0000860 bool hasQualOrFound = (qual != 0 ||
861 founddecl.getDecl() != memberdecl ||
862 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000863 if (hasQualOrFound)
864 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000865
John McCalld5532b62009-11-23 01:53:49 +0000866 if (targs)
867 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Chris Lattner32488542010-10-30 05:14:06 +0000869 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000870 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
871 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000872
873 if (hasQualOrFound) {
874 if (qual && qual->isDependent()) {
875 E->setValueDependent(true);
876 E->setTypeDependent(true);
877 }
878 E->HasQualifierOrFoundDecl = true;
879
880 MemberNameQualifier *NQ = E->getMemberQualifier();
881 NQ->NNS = qual;
882 NQ->Range = qualrange;
883 NQ->FoundDecl = founddecl;
884 }
885
886 if (targs) {
887 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000888 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000889 }
890
891 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000892}
893
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000894const char *CastExpr::getCastKindName() const {
895 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000896 case CK_Dependent:
897 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000898 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000899 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000900 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000901 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000902 case CK_LValueToRValue:
903 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000904 case CK_GetObjCProperty:
905 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000906 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000907 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000908 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000909 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000910 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000911 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000912 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000913 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000914 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000915 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000916 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000917 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000918 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000919 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000920 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000921 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000922 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000923 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000924 case CK_NullToPointer:
925 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000926 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000927 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000928 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000929 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000930 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000931 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000932 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000933 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000934 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000935 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000936 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000937 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000938 case CK_PointerToBoolean:
939 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000940 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000941 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000942 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000943 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000944 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000945 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000946 case CK_IntegralToBoolean:
947 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000948 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000949 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000950 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000951 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000952 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000953 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000954 case CK_FloatingToBoolean:
955 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000956 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000957 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000958 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000959 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000960 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000961 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +0000962 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +0000963 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +0000964 case CK_FloatingRealToComplex:
965 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000966 case CK_FloatingComplexToReal:
967 return "FloatingComplexToReal";
968 case CK_FloatingComplexToBoolean:
969 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000970 case CK_FloatingComplexCast:
971 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000972 case CK_FloatingComplexToIntegralComplex:
973 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +0000974 case CK_IntegralRealToComplex:
975 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +0000976 case CK_IntegralComplexToReal:
977 return "IntegralComplexToReal";
978 case CK_IntegralComplexToBoolean:
979 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +0000980 case CK_IntegralComplexCast:
981 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +0000982 case CK_IntegralComplexToFloatingComplex:
983 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000984 }
Mike Stump1eb44332009-09-09 15:08:12 +0000985
John McCall2bb5d002010-11-13 09:02:35 +0000986 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000987 return 0;
988}
989
Douglas Gregor6eef5192009-12-14 19:27:10 +0000990Expr *CastExpr::getSubExprAsWritten() {
991 Expr *SubExpr = 0;
992 CastExpr *E = this;
993 do {
994 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000995
Douglas Gregor6eef5192009-12-14 19:27:10 +0000996 // Skip any temporary bindings; they're implicit.
997 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
998 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000999
Douglas Gregor6eef5192009-12-14 19:27:10 +00001000 // Conversions by constructor and conversion functions have a
1001 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001002 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001003 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001004 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001005 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001006
Douglas Gregor6eef5192009-12-14 19:27:10 +00001007 // If the subexpression we're left with is an implicit cast, look
1008 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001009 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1010
Douglas Gregor6eef5192009-12-14 19:27:10 +00001011 return SubExpr;
1012}
1013
John McCallf871d0c2010-08-07 06:22:56 +00001014CXXBaseSpecifier **CastExpr::path_buffer() {
1015 switch (getStmtClass()) {
1016#define ABSTRACT_STMT(x)
1017#define CASTEXPR(Type, Base) \
1018 case Stmt::Type##Class: \
1019 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1020#define STMT(Type, Base)
1021#include "clang/AST/StmtNodes.inc"
1022 default:
1023 llvm_unreachable("non-cast expressions not possible here");
1024 return 0;
1025 }
1026}
1027
1028void CastExpr::setCastPath(const CXXCastPath &Path) {
1029 assert(Path.size() == path_size());
1030 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1031}
1032
1033ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1034 CastKind Kind, Expr *Operand,
1035 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001036 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001037 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1038 void *Buffer =
1039 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1040 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001041 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001042 if (PathSize) E->setCastPath(*BasePath);
1043 return E;
1044}
1045
1046ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1047 unsigned PathSize) {
1048 void *Buffer =
1049 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1050 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1051}
1052
1053
1054CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001055 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001056 const CXXCastPath *BasePath,
1057 TypeSourceInfo *WrittenTy,
1058 SourceLocation L, SourceLocation R) {
1059 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1060 void *Buffer =
1061 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1062 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001063 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001064 if (PathSize) E->setCastPath(*BasePath);
1065 return E;
1066}
1067
1068CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1069 void *Buffer =
1070 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1071 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1072}
1073
Reid Spencer5f016e22007-07-11 17:01:13 +00001074/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1075/// corresponds to, e.g. "<<=".
1076const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1077 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001078 case BO_PtrMemD: return ".*";
1079 case BO_PtrMemI: return "->*";
1080 case BO_Mul: return "*";
1081 case BO_Div: return "/";
1082 case BO_Rem: return "%";
1083 case BO_Add: return "+";
1084 case BO_Sub: return "-";
1085 case BO_Shl: return "<<";
1086 case BO_Shr: return ">>";
1087 case BO_LT: return "<";
1088 case BO_GT: return ">";
1089 case BO_LE: return "<=";
1090 case BO_GE: return ">=";
1091 case BO_EQ: return "==";
1092 case BO_NE: return "!=";
1093 case BO_And: return "&";
1094 case BO_Xor: return "^";
1095 case BO_Or: return "|";
1096 case BO_LAnd: return "&&";
1097 case BO_LOr: return "||";
1098 case BO_Assign: return "=";
1099 case BO_MulAssign: return "*=";
1100 case BO_DivAssign: return "/=";
1101 case BO_RemAssign: return "%=";
1102 case BO_AddAssign: return "+=";
1103 case BO_SubAssign: return "-=";
1104 case BO_ShlAssign: return "<<=";
1105 case BO_ShrAssign: return ">>=";
1106 case BO_AndAssign: return "&=";
1107 case BO_XorAssign: return "^=";
1108 case BO_OrAssign: return "|=";
1109 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001111
1112 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001113}
1114
John McCall2de56d12010-08-25 11:45:40 +00001115BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001116BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1117 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001118 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001119 case OO_Plus: return BO_Add;
1120 case OO_Minus: return BO_Sub;
1121 case OO_Star: return BO_Mul;
1122 case OO_Slash: return BO_Div;
1123 case OO_Percent: return BO_Rem;
1124 case OO_Caret: return BO_Xor;
1125 case OO_Amp: return BO_And;
1126 case OO_Pipe: return BO_Or;
1127 case OO_Equal: return BO_Assign;
1128 case OO_Less: return BO_LT;
1129 case OO_Greater: return BO_GT;
1130 case OO_PlusEqual: return BO_AddAssign;
1131 case OO_MinusEqual: return BO_SubAssign;
1132 case OO_StarEqual: return BO_MulAssign;
1133 case OO_SlashEqual: return BO_DivAssign;
1134 case OO_PercentEqual: return BO_RemAssign;
1135 case OO_CaretEqual: return BO_XorAssign;
1136 case OO_AmpEqual: return BO_AndAssign;
1137 case OO_PipeEqual: return BO_OrAssign;
1138 case OO_LessLess: return BO_Shl;
1139 case OO_GreaterGreater: return BO_Shr;
1140 case OO_LessLessEqual: return BO_ShlAssign;
1141 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1142 case OO_EqualEqual: return BO_EQ;
1143 case OO_ExclaimEqual: return BO_NE;
1144 case OO_LessEqual: return BO_LE;
1145 case OO_GreaterEqual: return BO_GE;
1146 case OO_AmpAmp: return BO_LAnd;
1147 case OO_PipePipe: return BO_LOr;
1148 case OO_Comma: return BO_Comma;
1149 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001150 }
1151}
1152
1153OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1154 static const OverloadedOperatorKind OverOps[] = {
1155 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1156 OO_Star, OO_Slash, OO_Percent,
1157 OO_Plus, OO_Minus,
1158 OO_LessLess, OO_GreaterGreater,
1159 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1160 OO_EqualEqual, OO_ExclaimEqual,
1161 OO_Amp,
1162 OO_Caret,
1163 OO_Pipe,
1164 OO_AmpAmp,
1165 OO_PipePipe,
1166 OO_Equal, OO_StarEqual,
1167 OO_SlashEqual, OO_PercentEqual,
1168 OO_PlusEqual, OO_MinusEqual,
1169 OO_LessLessEqual, OO_GreaterGreaterEqual,
1170 OO_AmpEqual, OO_CaretEqual,
1171 OO_PipeEqual,
1172 OO_Comma
1173 };
1174 return OverOps[Opc];
1175}
1176
Ted Kremenek709210f2010-04-13 23:39:13 +00001177InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001178 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001179 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001180 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1181 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001182 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001183 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001184 UnionFieldInit(0), HadArrayRangeDesignator(false)
1185{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001186 for (unsigned I = 0; I != numInits; ++I) {
1187 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001188 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001189 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001190 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001191 if (initExprs[I]->containsUnexpandedParameterPack())
1192 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001193 }
Sean Huntc3021132010-05-05 15:23:54 +00001194
Ted Kremenek709210f2010-04-13 23:39:13 +00001195 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001196}
Reid Spencer5f016e22007-07-11 17:01:13 +00001197
Ted Kremenek709210f2010-04-13 23:39:13 +00001198void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001199 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001200 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001201}
1202
Ted Kremenek709210f2010-04-13 23:39:13 +00001203void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001204 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001205}
1206
Ted Kremenek709210f2010-04-13 23:39:13 +00001207Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001208 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001209 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001210 InitExprs.back() = expr;
1211 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregor4c678342009-01-28 21:54:33 +00001214 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1215 InitExprs[Init] = expr;
1216 return Result;
1217}
1218
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001219SourceRange InitListExpr::getSourceRange() const {
1220 if (SyntacticForm)
1221 return SyntacticForm->getSourceRange();
1222 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1223 if (Beg.isInvalid()) {
1224 // Find the first non-null initializer.
1225 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1226 E = InitExprs.end();
1227 I != E; ++I) {
1228 if (Stmt *S = *I) {
1229 Beg = S->getLocStart();
1230 break;
1231 }
1232 }
1233 }
1234 if (End.isInvalid()) {
1235 // Find the first non-null initializer from the end.
1236 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1237 E = InitExprs.rend();
1238 I != E; ++I) {
1239 if (Stmt *S = *I) {
1240 End = S->getSourceRange().getEnd();
1241 break;
1242 }
1243 }
1244 }
1245 return SourceRange(Beg, End);
1246}
1247
Steve Naroffbfdcae62008-09-04 15:31:07 +00001248/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001249///
1250const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001251 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001252 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001253}
1254
Mike Stump1eb44332009-09-09 15:08:12 +00001255SourceLocation BlockExpr::getCaretLocation() const {
1256 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001257}
Mike Stump1eb44332009-09-09 15:08:12 +00001258const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001259 return TheBlock->getBody();
1260}
Mike Stump1eb44332009-09-09 15:08:12 +00001261Stmt *BlockExpr::getBody() {
1262 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001263}
Steve Naroff56ee6892008-10-08 17:01:13 +00001264
1265
Reid Spencer5f016e22007-07-11 17:01:13 +00001266//===----------------------------------------------------------------------===//
1267// Generic Expression Routines
1268//===----------------------------------------------------------------------===//
1269
Chris Lattner026dc962009-02-14 07:37:35 +00001270/// isUnusedResultAWarning - Return true if this immediate expression should
1271/// be warned about if the result is unused. If so, fill in Loc and Ranges
1272/// with location to warn on and the source range[s] to report with the
1273/// warning.
1274bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001275 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001276 // Don't warn if the expr is type dependent. The type could end up
1277 // instantiating to void.
1278 if (isTypeDependent())
1279 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 switch (getStmtClass()) {
1282 default:
John McCall0faede62010-03-12 07:11:26 +00001283 if (getType()->isVoidType())
1284 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001285 Loc = getExprLoc();
1286 R1 = getSourceRange();
1287 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001289 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001290 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 case UnaryOperatorClass: {
1292 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001295 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001296 case UO_PostInc:
1297 case UO_PostDec:
1298 case UO_PreInc:
1299 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001300 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001301 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001302 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001303 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001304 return false;
1305 break;
John McCall2de56d12010-08-25 11:45:40 +00001306 case UO_Real:
1307 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001309 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1310 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001311 return false;
1312 break;
John McCall2de56d12010-08-25 11:45:40 +00001313 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001314 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 }
Chris Lattner026dc962009-02-14 07:37:35 +00001316 Loc = UO->getOperatorLoc();
1317 R1 = UO->getSubExpr()->getSourceRange();
1318 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001320 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001321 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001322 switch (BO->getOpcode()) {
1323 default:
1324 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001325 // Consider the RHS of comma for side effects. LHS was checked by
1326 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001327 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001328 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1329 // lvalue-ness) of an assignment written in a macro.
1330 if (IntegerLiteral *IE =
1331 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1332 if (IE->getValue() == 0)
1333 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001334 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1335 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001336 case BO_LAnd:
1337 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001338 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1339 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1340 return false;
1341 break;
John McCallbf0ee352010-02-16 04:10:53 +00001342 }
Chris Lattner026dc962009-02-14 07:37:35 +00001343 if (BO->isAssignmentOp())
1344 return false;
1345 Loc = BO->getOperatorLoc();
1346 R1 = BO->getLHS()->getSourceRange();
1347 R2 = BO->getRHS()->getSourceRange();
1348 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001349 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001350 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001351 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001352 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001353
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001354 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001355 // The condition must be evaluated, but if either the LHS or RHS is a
1356 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001357 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001358 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001359 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001360 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001361 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001362 }
1363
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001365 // If the base pointer or element is to a volatile pointer/field, accessing
1366 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001367 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001368 return false;
1369 Loc = cast<MemberExpr>(this)->getMemberLoc();
1370 R1 = SourceRange(Loc, Loc);
1371 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1372 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 case ArraySubscriptExprClass:
1375 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001376 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001377 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001378 return false;
1379 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1380 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1381 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1382 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001383
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001385 case CXXOperatorCallExprClass:
1386 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001387 // If this is a direct call, get the callee.
1388 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001389 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001390 // If the callee has attribute pure, const, or warn_unused_result, warn
1391 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001392 //
1393 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1394 // updated to match for QoI.
1395 if (FD->getAttr<WarnUnusedResultAttr>() ||
1396 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1397 Loc = CE->getCallee()->getLocStart();
1398 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001400 if (unsigned NumArgs = CE->getNumArgs())
1401 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1402 CE->getArg(NumArgs-1)->getLocEnd());
1403 return true;
1404 }
Chris Lattner026dc962009-02-14 07:37:35 +00001405 }
1406 return false;
1407 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001408
1409 case CXXTemporaryObjectExprClass:
1410 case CXXConstructExprClass:
1411 return false;
1412
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001413 case ObjCMessageExprClass: {
1414 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1415 const ObjCMethodDecl *MD = ME->getMethodDecl();
1416 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1417 Loc = getExprLoc();
1418 return true;
1419 }
Chris Lattner026dc962009-02-14 07:37:35 +00001420 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001421 }
Mike Stump1eb44332009-09-09 15:08:12 +00001422
John McCall12f78a62010-12-02 01:19:52 +00001423 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001424 Loc = getExprLoc();
1425 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001426 return true;
John McCall12f78a62010-12-02 01:19:52 +00001427
Chris Lattner611b2ec2008-07-26 19:51:01 +00001428 case StmtExprClass: {
1429 // Statement exprs don't logically have side effects themselves, but are
1430 // sometimes used in macros in ways that give them a type that is unused.
1431 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1432 // however, if the result of the stmt expr is dead, we don't want to emit a
1433 // warning.
1434 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001435 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001436 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001437 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001438 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1439 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1440 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1441 }
Mike Stump1eb44332009-09-09 15:08:12 +00001442
John McCall0faede62010-03-12 07:11:26 +00001443 if (getType()->isVoidType())
1444 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001445 Loc = cast<StmtExpr>(this)->getLParenLoc();
1446 R1 = getSourceRange();
1447 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001448 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001449 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001450 // If this is an explicit cast to void, allow it. People do this when they
1451 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001452 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001453 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001454 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1455 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1456 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001457 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001458 if (getType()->isVoidType())
1459 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001460 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001461
Anders Carlsson58beed92009-11-17 17:11:23 +00001462 // If this is a cast to void or a constructor conversion, check the operand.
1463 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001464 if (CE->getCastKind() == CK_ToVoid ||
1465 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001466 return (cast<CastExpr>(this)->getSubExpr()
1467 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001468 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1469 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1470 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Eli Friedman4be1f472008-05-19 21:24:43 +00001473 case ImplicitCastExprClass:
1474 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001475 return (cast<ImplicitCastExpr>(this)
1476 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001477
Chris Lattner04421082008-04-08 04:40:51 +00001478 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001479 return (cast<CXXDefaultArgExpr>(this)
1480 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001481
1482 case CXXNewExprClass:
1483 // FIXME: In theory, there might be new expressions that don't have side
1484 // effects (e.g. a placement new with an uninitialized POD).
1485 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001486 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001487 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001488 return (cast<CXXBindTemporaryExpr>(this)
1489 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001490 case ExprWithCleanupsClass:
1491 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001492 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001493 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001494}
1495
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001496/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001497/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001498bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001499 switch (getStmtClass()) {
1500 default:
1501 return false;
1502 case ObjCIvarRefExprClass:
1503 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001504 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001505 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001506 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001507 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001508 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001509 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001510 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001511 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001512 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001513 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001514 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1515 if (VD->hasGlobalStorage())
1516 return true;
1517 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001518 // dereferencing to a pointer is always a gc'able candidate,
1519 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001520 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001521 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001522 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001523 return false;
1524 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001525 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001526 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001527 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001528 }
1529 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001530 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001531 }
1532}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001533
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001534bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1535 if (isTypeDependent())
1536 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001537 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001538}
1539
Sebastian Redl369e51f2010-09-10 20:55:33 +00001540static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1541 Expr::CanThrowResult CT2) {
1542 // CanThrowResult constants are ordered so that the maximum is the correct
1543 // merge result.
1544 return CT1 > CT2 ? CT1 : CT2;
1545}
1546
1547static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1548 Expr *E = const_cast<Expr*>(CE);
1549 Expr::CanThrowResult R = Expr::CT_Cannot;
1550 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1551 I != IE && R != Expr::CT_Can; ++I) {
1552 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1553 }
1554 return R;
1555}
1556
1557static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1558 bool NullThrows = true) {
1559 if (!D)
1560 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1561
1562 // See if we can get a function type from the decl somehow.
1563 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1564 if (!VD) // If we have no clue what we're calling, assume the worst.
1565 return Expr::CT_Can;
1566
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001567 // As an extension, we assume that __attribute__((nothrow)) functions don't
1568 // throw.
1569 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1570 return Expr::CT_Cannot;
1571
Sebastian Redl369e51f2010-09-10 20:55:33 +00001572 QualType T = VD->getType();
1573 const FunctionProtoType *FT;
1574 if ((FT = T->getAs<FunctionProtoType>())) {
1575 } else if (const PointerType *PT = T->getAs<PointerType>())
1576 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1577 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1578 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1579 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1580 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1581 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1582 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1583
1584 if (!FT)
1585 return Expr::CT_Can;
1586
1587 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1588}
1589
1590static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1591 if (DC->isTypeDependent())
1592 return Expr::CT_Dependent;
1593
Sebastian Redl295995c2010-09-10 20:55:47 +00001594 if (!DC->getTypeAsWritten()->isReferenceType())
1595 return Expr::CT_Cannot;
1596
Sebastian Redl369e51f2010-09-10 20:55:33 +00001597 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1598}
1599
1600static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1601 const CXXTypeidExpr *DC) {
1602 if (DC->isTypeOperand())
1603 return Expr::CT_Cannot;
1604
1605 Expr *Op = DC->getExprOperand();
1606 if (Op->isTypeDependent())
1607 return Expr::CT_Dependent;
1608
1609 const RecordType *RT = Op->getType()->getAs<RecordType>();
1610 if (!RT)
1611 return Expr::CT_Cannot;
1612
1613 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1614 return Expr::CT_Cannot;
1615
1616 if (Op->Classify(C).isPRValue())
1617 return Expr::CT_Cannot;
1618
1619 return Expr::CT_Can;
1620}
1621
1622Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1623 // C++ [expr.unary.noexcept]p3:
1624 // [Can throw] if in a potentially-evaluated context the expression would
1625 // contain:
1626 switch (getStmtClass()) {
1627 case CXXThrowExprClass:
1628 // - a potentially evaluated throw-expression
1629 return CT_Can;
1630
1631 case CXXDynamicCastExprClass: {
1632 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1633 // where T is a reference type, that requires a run-time check
1634 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1635 if (CT == CT_Can)
1636 return CT;
1637 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1638 }
1639
1640 case CXXTypeidExprClass:
1641 // - a potentially evaluated typeid expression applied to a glvalue
1642 // expression whose type is a polymorphic class type
1643 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1644
1645 // - a potentially evaluated call to a function, member function, function
1646 // pointer, or member function pointer that does not have a non-throwing
1647 // exception-specification
1648 case CallExprClass:
1649 case CXXOperatorCallExprClass:
1650 case CXXMemberCallExprClass: {
1651 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1652 if (CT == CT_Can)
1653 return CT;
1654 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1655 }
1656
Sebastian Redl295995c2010-09-10 20:55:47 +00001657 case CXXConstructExprClass:
1658 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001659 CanThrowResult CT = CanCalleeThrow(
1660 cast<CXXConstructExpr>(this)->getConstructor());
1661 if (CT == CT_Can)
1662 return CT;
1663 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1664 }
1665
1666 case CXXNewExprClass: {
1667 CanThrowResult CT = MergeCanThrow(
1668 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1669 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1670 /*NullThrows*/false));
1671 if (CT == CT_Can)
1672 return CT;
1673 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1674 }
1675
1676 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001677 CanThrowResult CT = CanCalleeThrow(
1678 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1679 if (CT == CT_Can)
1680 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001681 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1682 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1683 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1684 Arg = Cast->getSubExpr();
1685 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1686 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1687 CanThrowResult CT2 = CanCalleeThrow(
1688 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1689 if (CT2 == CT_Can)
1690 return CT2;
1691 CT = MergeCanThrow(CT, CT2);
1692 }
1693 }
1694 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1695 }
1696
1697 case CXXBindTemporaryExprClass: {
1698 // The bound temporary has to be destroyed again, which might throw.
1699 CanThrowResult CT = CanCalleeThrow(
1700 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1701 if (CT == CT_Can)
1702 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001703 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1704 }
1705
1706 // ObjC message sends are like function calls, but never have exception
1707 // specs.
1708 case ObjCMessageExprClass:
1709 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001710 return CT_Can;
1711
1712 // Many other things have subexpressions, so we have to test those.
1713 // Some are simple:
1714 case ParenExprClass:
1715 case MemberExprClass:
1716 case CXXReinterpretCastExprClass:
1717 case CXXConstCastExprClass:
1718 case ConditionalOperatorClass:
1719 case CompoundLiteralExprClass:
1720 case ExtVectorElementExprClass:
1721 case InitListExprClass:
1722 case DesignatedInitExprClass:
1723 case ParenListExprClass:
1724 case VAArgExprClass:
1725 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001726 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001727 case ObjCIvarRefExprClass:
1728 case ObjCIsaExprClass:
1729 case ShuffleVectorExprClass:
1730 return CanSubExprsThrow(C, this);
1731
1732 // Some might be dependent for other reasons.
1733 case UnaryOperatorClass:
1734 case ArraySubscriptExprClass:
1735 case ImplicitCastExprClass:
1736 case CStyleCastExprClass:
1737 case CXXStaticCastExprClass:
1738 case CXXFunctionalCastExprClass:
1739 case BinaryOperatorClass:
1740 case CompoundAssignOperatorClass: {
1741 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1742 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1743 }
1744
1745 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1746 case StmtExprClass:
1747 return CT_Can;
1748
1749 case ChooseExprClass:
1750 if (isTypeDependent() || isValueDependent())
1751 return CT_Dependent;
1752 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1753
1754 // Some expressions are always dependent.
1755 case DependentScopeDeclRefExprClass:
1756 case CXXUnresolvedConstructExprClass:
1757 case CXXDependentScopeMemberExprClass:
1758 return CT_Dependent;
1759
1760 default:
1761 // All other expressions don't have subexpressions, or else they are
1762 // unevaluated.
1763 return CT_Cannot;
1764 }
1765}
1766
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001767Expr* Expr::IgnoreParens() {
1768 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001769 while (true) {
1770 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1771 E = P->getSubExpr();
1772 continue;
1773 }
1774 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1775 if (P->getOpcode() == UO_Extension) {
1776 E = P->getSubExpr();
1777 continue;
1778 }
1779 }
1780 return E;
1781 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001782}
1783
Chris Lattner56f34942008-02-13 01:02:39 +00001784/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1785/// or CastExprs or ImplicitCastExprs, returning their operand.
1786Expr *Expr::IgnoreParenCasts() {
1787 Expr *E = this;
1788 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001789 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001790 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001791 continue;
1792 }
1793 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001794 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001795 continue;
1796 }
1797 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1798 if (P->getOpcode() == UO_Extension) {
1799 E = P->getSubExpr();
1800 continue;
1801 }
1802 }
1803 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001804 }
1805}
1806
John McCall9c5d70c2010-12-04 08:24:19 +00001807/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1808/// casts. This is intended purely as a temporary workaround for code
1809/// that hasn't yet been rewritten to do the right thing about those
1810/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001811Expr *Expr::IgnoreParenLValueCasts() {
1812 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001813 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001814 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1815 E = P->getSubExpr();
1816 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001817 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001818 if (P->getCastKind() == CK_LValueToRValue) {
1819 E = P->getSubExpr();
1820 continue;
1821 }
John McCall9c5d70c2010-12-04 08:24:19 +00001822 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1823 if (P->getOpcode() == UO_Extension) {
1824 E = P->getSubExpr();
1825 continue;
1826 }
John McCallf6a16482010-12-04 03:47:34 +00001827 }
1828 break;
1829 }
1830 return E;
1831}
1832
John McCall2fc46bf2010-05-05 22:59:52 +00001833Expr *Expr::IgnoreParenImpCasts() {
1834 Expr *E = this;
1835 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001836 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001837 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001838 continue;
1839 }
1840 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001841 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001842 continue;
1843 }
1844 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1845 if (P->getOpcode() == UO_Extension) {
1846 E = P->getSubExpr();
1847 continue;
1848 }
1849 }
1850 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001851 }
1852}
1853
Chris Lattnerecdd8412009-03-13 17:28:01 +00001854/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1855/// value (including ptr->int casts of the same size). Strip off any
1856/// ParenExpr or CastExprs, returning their operand.
1857Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1858 Expr *E = this;
1859 while (true) {
1860 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1861 E = P->getSubExpr();
1862 continue;
1863 }
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Chris Lattnerecdd8412009-03-13 17:28:01 +00001865 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1866 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001867 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001868 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Chris Lattnerecdd8412009-03-13 17:28:01 +00001870 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1871 E = SE;
1872 continue;
1873 }
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001875 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001876 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001877 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001878 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001879 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1880 E = SE;
1881 continue;
1882 }
1883 }
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001885 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1886 if (P->getOpcode() == UO_Extension) {
1887 E = P->getSubExpr();
1888 continue;
1889 }
1890 }
1891
Chris Lattnerecdd8412009-03-13 17:28:01 +00001892 return E;
1893 }
1894}
1895
Douglas Gregor6eef5192009-12-14 19:27:10 +00001896bool Expr::isDefaultArgument() const {
1897 const Expr *E = this;
1898 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1899 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001900
Douglas Gregor6eef5192009-12-14 19:27:10 +00001901 return isa<CXXDefaultArgExpr>(E);
1902}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001903
Douglas Gregor2f599792010-04-02 18:24:57 +00001904/// \brief Skip over any no-op casts and any temporary-binding
1905/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001906static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001907 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001908 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001909 E = ICE->getSubExpr();
1910 else
1911 break;
1912 }
1913
1914 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1915 E = BE->getSubExpr();
1916
1917 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001918 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001919 E = ICE->getSubExpr();
1920 else
1921 break;
1922 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001923
1924 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001925}
1926
John McCall558d2ab2010-09-15 10:14:12 +00001927/// isTemporaryObject - Determines if this expression produces a
1928/// temporary of the given class type.
1929bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1930 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1931 return false;
1932
Anders Carlssonf8b30152010-11-28 16:40:49 +00001933 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001934
John McCall58277b52010-09-15 20:59:13 +00001935 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001936 if (!E->Classify(C).isPRValue()) {
1937 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001938 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001939 return false;
1940 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001941
John McCall19e60ad2010-09-16 06:57:56 +00001942 // Black-list a few cases which yield pr-values of class type that don't
1943 // refer to temporaries of that type:
1944
1945 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001946 if (isa<ImplicitCastExpr>(E)) {
1947 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1948 case CK_DerivedToBase:
1949 case CK_UncheckedDerivedToBase:
1950 return false;
1951 default:
1952 break;
1953 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001954 }
1955
John McCall19e60ad2010-09-16 06:57:56 +00001956 // - member expressions (all)
1957 if (isa<MemberExpr>(E))
1958 return false;
1959
John McCall558d2ab2010-09-15 10:14:12 +00001960 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00001961}
1962
Douglas Gregor898574e2008-12-05 23:32:09 +00001963/// hasAnyTypeDependentArguments - Determines if any of the expressions
1964/// in Exprs is type-dependent.
1965bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1966 for (unsigned I = 0; I < NumExprs; ++I)
1967 if (Exprs[I]->isTypeDependent())
1968 return true;
1969
1970 return false;
1971}
1972
1973/// hasAnyValueDependentArguments - Determines if any of the expressions
1974/// in Exprs is value-dependent.
1975bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1976 for (unsigned I = 0; I < NumExprs; ++I)
1977 if (Exprs[I]->isValueDependent())
1978 return true;
1979
1980 return false;
1981}
1982
John McCall4204f072010-08-02 21:13:48 +00001983bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001984 // This function is attempting whether an expression is an initializer
1985 // which can be evaluated at compile-time. isEvaluatable handles most
1986 // of the cases, but it can't deal with some initializer-specific
1987 // expressions, and it can't deal with aggregates; we deal with those here,
1988 // and fall back to isEvaluatable for the other cases.
1989
John McCall4204f072010-08-02 21:13:48 +00001990 // If we ever capture reference-binding directly in the AST, we can
1991 // kill the second parameter.
1992
1993 if (IsForRef) {
1994 EvalResult Result;
1995 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1996 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001997
Anders Carlssone8a32b82008-11-24 05:23:59 +00001998 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001999 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002000 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002001 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002002 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002003 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002004 case CXXTemporaryObjectExprClass:
2005 case CXXConstructExprClass: {
2006 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002007
2008 // Only if it's
2009 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002010 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002011 if (!CE->getNumArgs()) return true;
2012
2013 // 2) an elidable trivial copy construction of an operand which is
2014 // itself a constant initializer. Note that we consider the
2015 // operand on its own, *not* as a reference binding.
2016 return CE->isElidable() &&
2017 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002018 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002019 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002020 // This handles gcc's extension that allows global initializers like
2021 // "struct x {int x;} x = (struct x) {};".
2022 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002023 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002024 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002025 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002026 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002027 // FIXME: This doesn't deal with fields with reference types correctly.
2028 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2029 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002030 const InitListExpr *Exp = cast<InitListExpr>(this);
2031 unsigned numInits = Exp->getNumInits();
2032 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002033 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002034 return false;
2035 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002036 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002037 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002038 case ImplicitValueInitExprClass:
2039 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002040 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002041 return cast<ParenExpr>(this)->getSubExpr()
2042 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002043 case ChooseExprClass:
2044 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2045 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002046 case UnaryOperatorClass: {
2047 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002048 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002049 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002050 break;
2051 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002052 case BinaryOperatorClass: {
2053 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2054 // but this handles the common case.
2055 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002056 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002057 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2058 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2059 return true;
2060 break;
2061 }
John McCall4204f072010-08-02 21:13:48 +00002062 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002063 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002064 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002065 case CStyleCastExprClass:
2066 // Handle casts with a destination that's a struct or union; this
2067 // deals with both the gcc no-op struct cast extension and the
2068 // cast-to-union extension.
2069 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002070 return cast<CastExpr>(this)->getSubExpr()
2071 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002072
Chris Lattner430656e2009-10-13 22:12:09 +00002073 // Integer->integer casts can be handled here, which is important for
2074 // things like (int)(&&x-&&y). Scary but true.
2075 if (getType()->isIntegerType() &&
2076 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002077 return cast<CastExpr>(this)->getSubExpr()
2078 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002079
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002080 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002081 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002082 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002083}
2084
Reid Spencer5f016e22007-07-11 17:01:13 +00002085/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2086/// integer constant expression with the value zero, or if this is one that is
2087/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002088bool Expr::isNullPointerConstant(ASTContext &Ctx,
2089 NullPointerConstantValueDependence NPC) const {
2090 if (isValueDependent()) {
2091 switch (NPC) {
2092 case NPC_NeverValueDependent:
2093 assert(false && "Unexpected value dependent expression!");
2094 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002095
Douglas Gregorce940492009-09-25 04:25:58 +00002096 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002097 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002098
Douglas Gregorce940492009-09-25 04:25:58 +00002099 case NPC_ValueDependentIsNotNull:
2100 return false;
2101 }
2102 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002103
Sebastian Redl07779722008-10-31 14:43:28 +00002104 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002105 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002106 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002107 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002108 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002109 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002110 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002111 Pointee->isVoidType() && // to void*
2112 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002113 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002114 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002116 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2117 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002118 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002119 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2120 // Accept ((void*)0) as a null pointer constant, as many other
2121 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002122 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002123 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002124 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002125 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002126 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002127 } else if (isa<GNUNullExpr>(this)) {
2128 // The GNU __null extension is always a null pointer constant.
2129 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002130 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002131
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002132 // C++0x nullptr_t is always a null pointer constant.
2133 if (getType()->isNullPtrType())
2134 return true;
2135
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002136 if (const RecordType *UT = getType()->getAsUnionType())
2137 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2138 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2139 const Expr *InitExpr = CLE->getInitializer();
2140 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2141 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2142 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002143 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002144 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002145 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002146 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 // If we have an integer constant expression, we need to *evaluate* it and
2149 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002150 llvm::APSInt Result;
2151 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002152}
Steve Naroff31a45842007-07-28 23:10:27 +00002153
John McCallf6a16482010-12-04 03:47:34 +00002154/// \brief If this expression is an l-value for an Objective C
2155/// property, find the underlying property reference expression.
2156const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2157 const Expr *E = this;
2158 while (true) {
2159 assert((E->getValueKind() == VK_LValue &&
2160 E->getObjectKind() == OK_ObjCProperty) &&
2161 "expression is not a property reference");
2162 E = E->IgnoreParenCasts();
2163 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2164 if (BO->getOpcode() == BO_Comma) {
2165 E = BO->getRHS();
2166 continue;
2167 }
2168 }
2169
2170 break;
2171 }
2172
2173 return cast<ObjCPropertyRefExpr>(E);
2174}
2175
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002176FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002177 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002178
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002179 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002180 if (ICE->getCastKind() == CK_LValueToRValue ||
2181 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002182 E = ICE->getSubExpr()->IgnoreParens();
2183 else
2184 break;
2185 }
2186
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002187 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002188 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002189 if (Field->isBitField())
2190 return Field;
2191
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002192 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2193 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2194 if (Field->isBitField())
2195 return Field;
2196
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002197 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2198 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2199 return BinOp->getLHS()->getBitField();
2200
2201 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002202}
2203
Anders Carlsson09380262010-01-31 17:18:49 +00002204bool Expr::refersToVectorElement() const {
2205 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002206
Anders Carlsson09380262010-01-31 17:18:49 +00002207 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002208 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002209 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002210 E = ICE->getSubExpr()->IgnoreParens();
2211 else
2212 break;
2213 }
Sean Huntc3021132010-05-05 15:23:54 +00002214
Anders Carlsson09380262010-01-31 17:18:49 +00002215 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2216 return ASE->getBase()->getType()->isVectorType();
2217
2218 if (isa<ExtVectorElementExpr>(E))
2219 return true;
2220
2221 return false;
2222}
2223
Chris Lattner2140e902009-02-16 22:14:05 +00002224/// isArrow - Return true if the base expression is a pointer to vector,
2225/// return false if the base expression is a vector.
2226bool ExtVectorElementExpr::isArrow() const {
2227 return getBase()->getType()->isPointerType();
2228}
2229
Nate Begeman213541a2008-04-18 23:10:10 +00002230unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002231 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002232 return VT->getNumElements();
2233 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002234}
2235
Nate Begeman8a997642008-05-09 06:41:27 +00002236/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002237bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002238 // FIXME: Refactor this code to an accessor on the AST node which returns the
2239 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002240 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002241
2242 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002243 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002244 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002245
Nate Begeman190d6a22009-01-18 02:01:21 +00002246 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002247 if (Comp[0] == 's' || Comp[0] == 'S')
2248 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002249
Daniel Dunbar15027422009-10-17 23:53:04 +00002250 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2251 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002252 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002253
Steve Narofffec0b492007-07-30 03:29:09 +00002254 return false;
2255}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002256
Nate Begeman8a997642008-05-09 06:41:27 +00002257/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002258void ExtVectorElementExpr::getEncodedElementAccess(
2259 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002260 llvm::StringRef Comp = Accessor->getName();
2261 if (Comp[0] == 's' || Comp[0] == 'S')
2262 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002263
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002264 bool isHi = Comp == "hi";
2265 bool isLo = Comp == "lo";
2266 bool isEven = Comp == "even";
2267 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Nate Begeman8a997642008-05-09 06:41:27 +00002269 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2270 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Nate Begeman8a997642008-05-09 06:41:27 +00002272 if (isHi)
2273 Index = e + i;
2274 else if (isLo)
2275 Index = i;
2276 else if (isEven)
2277 Index = 2 * i;
2278 else if (isOdd)
2279 Index = 2 * i + 1;
2280 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002281 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002282
Nate Begeman3b8d1162008-05-13 21:03:02 +00002283 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002284 }
Nate Begeman8a997642008-05-09 06:41:27 +00002285}
2286
Douglas Gregor04badcf2010-04-21 00:45:42 +00002287ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002288 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002289 SourceLocation LBracLoc,
2290 SourceLocation SuperLoc,
2291 bool IsInstanceSuper,
2292 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002293 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002294 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002295 ObjCMethodDecl *Method,
2296 Expr **Args, unsigned NumArgs,
2297 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002298 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002299 /*TypeDependent=*/false, /*ValueDependent=*/false,
2300 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002301 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2302 HasMethod(Method != 0), SuperLoc(SuperLoc),
2303 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2304 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002305 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002306{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002307 setReceiverPointer(SuperType.getAsOpaquePtr());
2308 if (NumArgs)
2309 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002310}
2311
Douglas Gregor04badcf2010-04-21 00:45:42 +00002312ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002313 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002314 SourceLocation LBracLoc,
2315 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002316 Selector Sel,
2317 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002318 ObjCMethodDecl *Method,
2319 Expr **Args, unsigned NumArgs,
2320 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002321 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002322 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002323 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2324 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2325 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002326 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002327{
2328 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002329 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002330 for (unsigned I = 0; I != NumArgs; ++I) {
2331 if (Args[I]->isTypeDependent())
2332 ExprBits.TypeDependent = true;
2333 if (Args[I]->isValueDependent())
2334 ExprBits.ValueDependent = true;
2335 if (Args[I]->containsUnexpandedParameterPack())
2336 ExprBits.ContainsUnexpandedParameterPack = true;
2337
2338 MyArgs[I] = Args[I];
2339 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002340}
2341
Douglas Gregor04badcf2010-04-21 00:45:42 +00002342ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002343 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002344 SourceLocation LBracLoc,
2345 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002346 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002347 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002348 ObjCMethodDecl *Method,
2349 Expr **Args, unsigned NumArgs,
2350 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002351 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002352 Receiver->isTypeDependent(),
2353 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002354 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2355 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2356 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002357 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002358{
2359 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002360 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002361 for (unsigned I = 0; I != NumArgs; ++I) {
2362 if (Args[I]->isTypeDependent())
2363 ExprBits.TypeDependent = true;
2364 if (Args[I]->isValueDependent())
2365 ExprBits.ValueDependent = true;
2366 if (Args[I]->containsUnexpandedParameterPack())
2367 ExprBits.ContainsUnexpandedParameterPack = true;
2368
2369 MyArgs[I] = Args[I];
2370 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002371}
2372
Douglas Gregor04badcf2010-04-21 00:45:42 +00002373ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002374 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002375 SourceLocation LBracLoc,
2376 SourceLocation SuperLoc,
2377 bool IsInstanceSuper,
2378 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002379 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002380 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002381 ObjCMethodDecl *Method,
2382 Expr **Args, unsigned NumArgs,
2383 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002384 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002385 NumArgs * sizeof(Expr *);
2386 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002387 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002388 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002389 RBracLoc);
2390}
2391
2392ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002393 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002394 SourceLocation LBracLoc,
2395 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002396 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002397 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002398 ObjCMethodDecl *Method,
2399 Expr **Args, unsigned NumArgs,
2400 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002401 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002402 NumArgs * sizeof(Expr *);
2403 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002404 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2405 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002406}
2407
2408ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002409 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002410 SourceLocation LBracLoc,
2411 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002412 Selector Sel,
2413 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002414 ObjCMethodDecl *Method,
2415 Expr **Args, unsigned NumArgs,
2416 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002417 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002418 NumArgs * sizeof(Expr *);
2419 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002420 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2421 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002422}
2423
Sean Huntc3021132010-05-05 15:23:54 +00002424ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002425 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002426 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002427 NumArgs * sizeof(Expr *);
2428 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2429 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2430}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002431
2432SourceRange ObjCMessageExpr::getReceiverRange() const {
2433 switch (getReceiverKind()) {
2434 case Instance:
2435 return getInstanceReceiver()->getSourceRange();
2436
2437 case Class:
2438 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2439
2440 case SuperInstance:
2441 case SuperClass:
2442 return getSuperLoc();
2443 }
2444
2445 return SourceLocation();
2446}
2447
Douglas Gregor04badcf2010-04-21 00:45:42 +00002448Selector ObjCMessageExpr::getSelector() const {
2449 if (HasMethod)
2450 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2451 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002452 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002453}
2454
2455ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2456 switch (getReceiverKind()) {
2457 case Instance:
2458 if (const ObjCObjectPointerType *Ptr
2459 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2460 return Ptr->getInterfaceDecl();
2461 break;
2462
2463 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002464 if (const ObjCObjectType *Ty
2465 = getClassReceiver()->getAs<ObjCObjectType>())
2466 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002467 break;
2468
2469 case SuperInstance:
2470 if (const ObjCObjectPointerType *Ptr
2471 = getSuperType()->getAs<ObjCObjectPointerType>())
2472 return Ptr->getInterfaceDecl();
2473 break;
2474
2475 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002476 if (const ObjCObjectType *Iface
2477 = getSuperType()->getAs<ObjCObjectType>())
2478 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002479 break;
2480 }
2481
2482 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002483}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002484
Jay Foad4ba2a172011-01-12 09:06:06 +00002485bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002486 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002487}
2488
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002489ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2490 QualType Type, SourceLocation BLoc,
2491 SourceLocation RP)
2492 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2493 Type->isDependentType(), Type->isDependentType(),
2494 Type->containsUnexpandedParameterPack()),
2495 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2496{
2497 SubExprs = new (C) Stmt*[nexpr];
2498 for (unsigned i = 0; i < nexpr; i++) {
2499 if (args[i]->isTypeDependent())
2500 ExprBits.TypeDependent = true;
2501 if (args[i]->isValueDependent())
2502 ExprBits.ValueDependent = true;
2503 if (args[i]->containsUnexpandedParameterPack())
2504 ExprBits.ContainsUnexpandedParameterPack = true;
2505
2506 SubExprs[i] = args[i];
2507 }
2508}
2509
Nate Begeman888376a2009-08-12 02:28:50 +00002510void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2511 unsigned NumExprs) {
2512 if (SubExprs) C.Deallocate(SubExprs);
2513
2514 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002515 this->NumExprs = NumExprs;
2516 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002517}
Nate Begeman888376a2009-08-12 02:28:50 +00002518
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002519//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002520// DesignatedInitExpr
2521//===----------------------------------------------------------------------===//
2522
2523IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2524 assert(Kind == FieldDesignator && "Only valid on a field designator");
2525 if (Field.NameOrField & 0x01)
2526 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2527 else
2528 return getField()->getIdentifier();
2529}
2530
Sean Huntc3021132010-05-05 15:23:54 +00002531DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002532 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002533 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002534 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002535 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002536 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002537 unsigned NumIndexExprs,
2538 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002539 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002540 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002541 Init->isTypeDependent(), Init->isValueDependent(),
2542 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002543 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2544 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002545 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002546
2547 // Record the initializer itself.
2548 child_iterator Child = child_begin();
2549 *Child++ = Init;
2550
2551 // Copy the designators and their subexpressions, computing
2552 // value-dependence along the way.
2553 unsigned IndexIdx = 0;
2554 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002555 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002556
2557 if (this->Designators[I].isArrayDesignator()) {
2558 // Compute type- and value-dependence.
2559 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002560 if (Index->isTypeDependent() || Index->isValueDependent())
2561 ExprBits.ValueDependent = true;
2562
2563 // Propagate unexpanded parameter packs.
2564 if (Index->containsUnexpandedParameterPack())
2565 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002566
2567 // Copy the index expressions into permanent storage.
2568 *Child++ = IndexExprs[IndexIdx++];
2569 } else if (this->Designators[I].isArrayRangeDesignator()) {
2570 // Compute type- and value-dependence.
2571 Expr *Start = IndexExprs[IndexIdx];
2572 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002573 if (Start->isTypeDependent() || Start->isValueDependent() ||
2574 End->isTypeDependent() || End->isValueDependent())
2575 ExprBits.ValueDependent = true;
2576
2577 // Propagate unexpanded parameter packs.
2578 if (Start->containsUnexpandedParameterPack() ||
2579 End->containsUnexpandedParameterPack())
2580 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002581
2582 // Copy the start/end expressions into permanent storage.
2583 *Child++ = IndexExprs[IndexIdx++];
2584 *Child++ = IndexExprs[IndexIdx++];
2585 }
2586 }
2587
2588 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002589}
2590
Douglas Gregor05c13a32009-01-22 00:58:24 +00002591DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002592DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002593 unsigned NumDesignators,
2594 Expr **IndexExprs, unsigned NumIndexExprs,
2595 SourceLocation ColonOrEqualLoc,
2596 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002597 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002598 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002599 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002600 ColonOrEqualLoc, UsesColonSyntax,
2601 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002602}
2603
Mike Stump1eb44332009-09-09 15:08:12 +00002604DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002605 unsigned NumIndexExprs) {
2606 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2607 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2608 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2609}
2610
Douglas Gregor319d57f2010-01-06 23:17:19 +00002611void DesignatedInitExpr::setDesignators(ASTContext &C,
2612 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002613 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002614 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002615 NumDesignators = NumDesigs;
2616 for (unsigned I = 0; I != NumDesigs; ++I)
2617 Designators[I] = Desigs[I];
2618}
2619
Douglas Gregor05c13a32009-01-22 00:58:24 +00002620SourceRange DesignatedInitExpr::getSourceRange() const {
2621 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002622 Designator &First =
2623 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002624 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002625 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002626 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2627 else
2628 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2629 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002630 StartLoc =
2631 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002632 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2633}
2634
Douglas Gregor05c13a32009-01-22 00:58:24 +00002635Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2636 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2637 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2638 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002639 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2640 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2641}
2642
2643Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002644 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002645 "Requires array range designator");
2646 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2647 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002648 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2649 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2650}
2651
2652Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002653 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002654 "Requires array range designator");
2655 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2656 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002657 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2658 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2659}
2660
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002661/// \brief Replaces the designator at index @p Idx with the series
2662/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002663void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002664 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002665 const Designator *Last) {
2666 unsigned NumNewDesignators = Last - First;
2667 if (NumNewDesignators == 0) {
2668 std::copy_backward(Designators + Idx + 1,
2669 Designators + NumDesignators,
2670 Designators + Idx);
2671 --NumNewDesignators;
2672 return;
2673 } else if (NumNewDesignators == 1) {
2674 Designators[Idx] = *First;
2675 return;
2676 }
2677
Mike Stump1eb44332009-09-09 15:08:12 +00002678 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002679 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002680 std::copy(Designators, Designators + Idx, NewDesignators);
2681 std::copy(First, Last, NewDesignators + Idx);
2682 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2683 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002684 Designators = NewDesignators;
2685 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2686}
2687
Mike Stump1eb44332009-09-09 15:08:12 +00002688ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002689 Expr **exprs, unsigned nexprs,
2690 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002691 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2692 false, false, false),
2693 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002694
Nate Begeman2ef13e52009-08-10 23:49:36 +00002695 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002696 for (unsigned i = 0; i != nexprs; ++i) {
2697 if (exprs[i]->isTypeDependent())
2698 ExprBits.TypeDependent = true;
2699 if (exprs[i]->isValueDependent())
2700 ExprBits.ValueDependent = true;
2701 if (exprs[i]->containsUnexpandedParameterPack())
2702 ExprBits.ContainsUnexpandedParameterPack = true;
2703
Nate Begeman2ef13e52009-08-10 23:49:36 +00002704 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002705 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002706}
2707
Douglas Gregor05c13a32009-01-22 00:58:24 +00002708//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002709// ExprIterator.
2710//===----------------------------------------------------------------------===//
2711
2712Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2713Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2714Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2715const Expr* ConstExprIterator::operator[](size_t idx) const {
2716 return cast<Expr>(I[idx]);
2717}
2718const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2719const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2720
2721//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002722// Child Iterators for iterating over subexpressions/substatements
2723//===----------------------------------------------------------------------===//
2724
2725// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002726Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2727Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002728
Steve Naroff7779db42007-11-12 14:29:37 +00002729// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002730Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2731Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002732
Steve Naroffe3e9add2008-06-02 23:03:37 +00002733// ObjCPropertyRefExpr
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002734Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2735{
John McCall12f78a62010-12-02 01:19:52 +00002736 if (Receiver.is<Stmt*>()) {
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002737 // Hack alert!
John McCall12f78a62010-12-02 01:19:52 +00002738 return reinterpret_cast<Stmt**> (&Receiver);
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002739 }
2740 return child_iterator();
2741}
2742
2743Stmt::child_iterator ObjCPropertyRefExpr::child_end()
John McCall12f78a62010-12-02 01:19:52 +00002744{ return Receiver.is<Stmt*>() ?
2745 reinterpret_cast<Stmt**> (&Receiver)+1 :
Fariborz Jahanian8ac2d442010-10-14 16:04:05 +00002746 child_iterator();
2747}
Steve Naroffae784072008-05-30 00:40:33 +00002748
Steve Narofff242b1b2009-07-24 17:54:45 +00002749// ObjCIsaExpr
2750Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2751Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2752
Chris Lattnerd9f69102008-08-10 01:53:14 +00002753// PredefinedExpr
2754Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2755Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002756
2757// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002758Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2759Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002760
2761// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002762Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002763Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002764
2765// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002766Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2767Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002768
Chris Lattner5d661452007-08-26 03:42:43 +00002769// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002770Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2771Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002772
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002773// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002774Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2775Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002776
2777// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002778Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2779Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002780
2781// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002782Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2783Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002784
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002785// OffsetOfExpr
2786Stmt::child_iterator OffsetOfExpr::child_begin() {
2787 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2788 + NumComps);
2789}
2790Stmt::child_iterator OffsetOfExpr::child_end() {
2791 return child_iterator(&*child_begin() + NumExprs);
2792}
2793
Sebastian Redl05189992008-11-11 17:56:53 +00002794// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002795Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002796 // If this is of a type and the type is a VLA type (and not a typedef), the
2797 // size expression of the VLA needs to be treated as an executable expression.
2798 // Why isn't this weirdness documented better in StmtIterator?
2799 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002800 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002801 getArgumentType().getTypePtr()))
2802 return child_iterator(T);
2803 return child_iterator();
2804 }
Sebastian Redld4575892008-12-03 23:17:54 +00002805 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002806}
Sebastian Redl05189992008-11-11 17:56:53 +00002807Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2808 if (isArgumentType())
2809 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002810 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002811}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002812
2813// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002814Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002815 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002816}
Ted Kremenek1237c672007-08-24 20:06:47 +00002817Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002818 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002819}
2820
2821// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002822Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002823 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002824}
Ted Kremenek1237c672007-08-24 20:06:47 +00002825Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002826 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002827}
Ted Kremenek1237c672007-08-24 20:06:47 +00002828
2829// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002830Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2831Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002832
Nate Begeman213541a2008-04-18 23:10:10 +00002833// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002834Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2835Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002836
2837// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002838Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2839Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002840
Ted Kremenek1237c672007-08-24 20:06:47 +00002841// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002842Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2843Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002844
2845// BinaryOperator
2846Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002847 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002848}
Ted Kremenek1237c672007-08-24 20:06:47 +00002849Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002850 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002851}
2852
2853// ConditionalOperator
2854Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002855 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002856}
Ted Kremenek1237c672007-08-24 20:06:47 +00002857Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002858 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002859}
2860
2861// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002862Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2863Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002864
Ted Kremenek1237c672007-08-24 20:06:47 +00002865// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002866Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2867Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002868
Ted Kremenek1237c672007-08-24 20:06:47 +00002869
2870// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002871Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2872Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002873
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002874// GNUNullExpr
2875Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2876Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2877
Eli Friedmand38617c2008-05-14 19:38:39 +00002878// ShuffleVectorExpr
2879Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002880 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002881}
2882Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002883 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002884}
2885
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002886// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002887Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2888Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002889
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002890// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002891Stmt::child_iterator InitListExpr::child_begin() {
2892 return InitExprs.size() ? &InitExprs[0] : 0;
2893}
2894Stmt::child_iterator InitListExpr::child_end() {
2895 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2896}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002897
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002898// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002899Stmt::child_iterator DesignatedInitExpr::child_begin() {
2900 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2901 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002902 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2903}
2904Stmt::child_iterator DesignatedInitExpr::child_end() {
2905 return child_iterator(&*child_begin() + NumSubExprs);
2906}
2907
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002908// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002909Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2910 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002911}
2912
Mike Stump1eb44332009-09-09 15:08:12 +00002913Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2914 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002915}
2916
Nate Begeman2ef13e52009-08-10 23:49:36 +00002917// ParenListExpr
2918Stmt::child_iterator ParenListExpr::child_begin() {
2919 return &Exprs[0];
2920}
2921Stmt::child_iterator ParenListExpr::child_end() {
2922 return &Exprs[0]+NumExprs;
2923}
2924
Ted Kremenek1237c672007-08-24 20:06:47 +00002925// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002926Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002927 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002928}
2929Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002930 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002931}
Ted Kremenek1237c672007-08-24 20:06:47 +00002932
2933// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002934Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2935Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002936
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002937// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002938Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002939 return child_iterator();
2940}
2941Stmt::child_iterator ObjCSelectorExpr::child_end() {
2942 return child_iterator();
2943}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002944
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002945// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002946Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2947 return child_iterator();
2948}
2949Stmt::child_iterator ObjCProtocolExpr::child_end() {
2950 return child_iterator();
2951}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002952
Steve Naroff563477d2007-09-18 23:55:05 +00002953// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002954Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002955 if (getReceiverKind() == Instance)
2956 return reinterpret_cast<Stmt **>(this + 1);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002957 return reinterpret_cast<Stmt **>(getArgs());
Steve Naroff563477d2007-09-18 23:55:05 +00002958}
2959Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregoraa165f82011-01-03 19:04:46 +00002960 return reinterpret_cast<Stmt **>(getArgs() + getNumArgs());
Steve Naroff563477d2007-09-18 23:55:05 +00002961}
2962
Steve Naroff4eb206b2008-09-03 18:15:37 +00002963// Blocks
Douglas Gregora779d9c2011-01-19 21:32:01 +00002964BlockDeclRefExpr::BlockDeclRefExpr(ValueDecl *d, QualType t, ExprValueKind VK,
2965 SourceLocation l, bool ByRef,
2966 bool constAdded, Stmt *copyConstructorVal)
Douglas Gregord967e312011-01-19 21:52:31 +00002967 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002968 d->isParameterPack()),
2969 D(d), Loc(l), IsByRef(ByRef),
2970 ConstQualAdded(constAdded), CopyConstructorVal(copyConstructorVal)
2971{
Douglas Gregord967e312011-01-19 21:52:31 +00002972 bool TypeDependent = false;
2973 bool ValueDependent = false;
2974 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2975 ExprBits.TypeDependent = TypeDependent;
2976 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00002977}
2978
Steve Naroff56ee6892008-10-08 17:01:13 +00002979Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2980Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002981
Ted Kremenek9da13f92008-09-26 23:24:14 +00002982Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2983Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall7cd7d1a2010-11-15 23:31:06 +00002984
2985// OpaqueValueExpr
Douglas Gregorb608b982011-01-28 02:26:04 +00002986SourceRange OpaqueValueExpr::getSourceRange() const { return Loc; }
John McCall7cd7d1a2010-11-15 23:31:06 +00002987Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2988Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2989