blob: 265788a0817fec4f3540a8c25b5de37e68989cf4 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattnere925d612010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000030#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000031using namespace clang;
32
Chris Lattnerc96f1fb2010-05-13 01:02:19 +000033void Expr::ANCHOR() {} // key function for Expr class.
34
Chris Lattner4ebae652010-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;
Alexis Hunta8136cc2010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregorb90df602010-06-16 00:17:44 +000043 if (!getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000044
Chris Lattner4ebae652010-04-16 23:34:13 +000045 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
46 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000047
Chris Lattner4ebae652010-04-16 23:34:13 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
49 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000050 case UO_Plus:
51 case UO_Extension:
Chris Lattner4ebae652010-04-16 23:34:13 +000052 return UO->getSubExpr()->isKnownToHaveBooleanValue();
53 default:
54 return false;
55 }
56 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000057
John McCall45d30c32010-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 Lattner4ebae652010-04-16 23:34:13 +000061 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000062
Chris Lattner4ebae652010-04-16 23:34:13 +000063 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
64 switch (BO->getOpcode()) {
65 default: return false;
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000074 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000075
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000079 // Handle things like (x==2)|(y==12).
80 return BO->getLHS()->isKnownToHaveBooleanValue() &&
81 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000082
John McCalle3027922010-08-25 11:45:40 +000083 case BO_Comma:
84 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000085 return BO->getRHS()->isKnownToHaveBooleanValue();
86 }
87 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000088
Chris Lattner4ebae652010-04-16 23:34:13 +000089 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
90 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
91 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000092
Chris Lattner4ebae652010-04-16 23:34:13 +000093 return false;
94}
95
Chris Lattner0eedafe2006-08-24 04:56:27 +000096//===----------------------------------------------------------------------===//
97// Primary Expressions.
98//===----------------------------------------------------------------------===//
99
John McCall6b51f282009-11-23 01:53:49 +0000100void ExplicitTemplateArgumentList::initializeFrom(
101 const TemplateArgumentListInfo &Info) {
102 LAngleLoc = Info.getLAngleLoc();
103 RAngleLoc = Info.getRAngleLoc();
104 NumTemplateArgs = Info.size();
105
106 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
107 for (unsigned i = 0; i != NumTemplateArgs; ++i)
108 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
109}
110
111void ExplicitTemplateArgumentList::copyInto(
112 TemplateArgumentListInfo &Info) const {
113 Info.setLAngleLoc(LAngleLoc);
114 Info.setRAngleLoc(RAngleLoc);
115 for (unsigned I = 0; I != NumTemplateArgs; ++I)
116 Info.addArgument(getTemplateArgs()[I]);
117}
118
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000119std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
120 return sizeof(ExplicitTemplateArgumentList) +
121 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
122}
123
John McCall6b51f282009-11-23 01:53:49 +0000124std::size_t ExplicitTemplateArgumentList::sizeFor(
125 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000126 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000127}
128
Douglas Gregored6c7442009-11-23 11:41:28 +0000129void DeclRefExpr::computeDependence() {
John McCall925b16622010-10-26 08:39:16 +0000130 ExprBits.TypeDependent = false;
131 ExprBits.ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000132
Douglas Gregored6c7442009-11-23 11:41:28 +0000133 NamedDecl *D = getDecl();
134
135 // (TD) C++ [temp.dep.expr]p3:
136 // An id-expression is type-dependent if it contains:
137 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000138 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000139 //
140 // (VD) C++ [temp.dep.constexpr]p2:
141 // An identifier is value-dependent if it is:
142
143 // (TD) - an identifier that was declared with dependent type
144 // (VD) - a name declared with a dependent type,
145 if (getType()->isDependentType()) {
John McCall925b16622010-10-26 08:39:16 +0000146 ExprBits.TypeDependent = true;
147 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000148 }
149 // (TD) - a conversion-function-id that specifies a dependent type
Alexis Hunta8136cc2010-05-05 15:23:54 +0000150 else if (D->getDeclName().getNameKind()
Douglas Gregored6c7442009-11-23 11:41:28 +0000151 == DeclarationName::CXXConversionFunctionName &&
152 D->getDeclName().getCXXNameType()->isDependentType()) {
John McCall925b16622010-10-26 08:39:16 +0000153 ExprBits.TypeDependent = true;
154 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000155 }
156 // (TD) - a template-id that is dependent,
John McCallb3774b52010-08-19 23:49:38 +0000157 else if (hasExplicitTemplateArgs() &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000158 TemplateSpecializationType::anyDependentTemplateArguments(
Alexis Hunta8136cc2010-05-05 15:23:54 +0000159 getTemplateArgs(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000160 getNumTemplateArgs())) {
John McCall925b16622010-10-26 08:39:16 +0000161 ExprBits.TypeDependent = true;
162 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000163 }
164 // (VD) - the name of a non-type template parameter,
165 else if (isa<NonTypeTemplateParmDecl>(D))
John McCall925b16622010-10-26 08:39:16 +0000166 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000167 // (VD) - a constant with integral or enumeration type and is
168 // initialized with an expression that is value-dependent.
169 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000170 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000171 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000172 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000173 if (Init->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +0000174 ExprBits.ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000175 }
176 // (VD) - FIXME: Missing from the standard:
177 // - a member function or a static data member of the current
178 // instantiation
179 else if (Var->isStaticDataMember() &&
Douglas Gregorbe49fc52010-05-11 08:44:04 +0000180 Var->getDeclContext()->isDependentContext())
John McCall925b16622010-10-26 08:39:16 +0000181 ExprBits.ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000182 }
183 // (VD) - FIXME: Missing from the standard:
184 // - a member function or a static data member of the current
185 // instantiation
186 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
John McCall925b16622010-10-26 08:39:16 +0000187 ExprBits.ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000188 // (TD) - a nested-name-specifier or a qualified-id that names a
189 // member of an unknown specialization.
190 // (handled by DependentScopeDeclRefExpr)
191}
192
Alexis Hunta8136cc2010-05-05 15:23:54 +0000193DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000194 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000195 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000196 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000197 QualType T, ExprValueKind VK)
198 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000199 DecoratedD(D,
200 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000201 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000202 Loc(NameLoc) {
203 if (Qualifier) {
204 NameQualifier *NQ = getNameQualifier();
205 NQ->NNS = Qualifier;
206 NQ->Range = QualifierRange;
207 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000208
John McCall6b51f282009-11-23 01:53:49 +0000209 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000210 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000211
212 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000213}
214
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000215DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
216 SourceRange QualifierRange,
217 ValueDecl *D, const DeclarationNameInfo &NameInfo,
218 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000219 QualType T, ExprValueKind VK)
220 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000221 DecoratedD(D,
222 (Qualifier? HasQualifierFlag : 0) |
223 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
224 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
225 if (Qualifier) {
226 NameQualifier *NQ = getNameQualifier();
227 NQ->NNS = Qualifier;
228 NQ->Range = QualifierRange;
229 }
230
231 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000232 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000233
234 computeDependence();
235}
236
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000237DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
238 NestedNameSpecifier *Qualifier,
239 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000240 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000241 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000242 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000243 ExprValueKind VK,
Douglas Gregored6c7442009-11-23 11:41:28 +0000244 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000245 return Create(Context, Qualifier, QualifierRange, D,
246 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCall7decc9e2010-11-18 06:31:45 +0000247 T, VK, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000248}
249
250DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
251 NestedNameSpecifier *Qualifier,
252 SourceRange QualifierRange,
253 ValueDecl *D,
254 const DeclarationNameInfo &NameInfo,
255 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000256 ExprValueKind VK,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000257 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000258 std::size_t Size = sizeof(DeclRefExpr);
259 if (Qualifier != 0)
260 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000261
John McCall6b51f282009-11-23 01:53:49 +0000262 if (TemplateArgs)
263 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000264
Chris Lattner5c0b4052010-10-30 05:14:06 +0000265 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000266 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCall7decc9e2010-11-18 06:31:45 +0000267 TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000268}
269
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000270DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, bool HasQualifier,
271 unsigned NumTemplateArgs) {
272 std::size_t Size = sizeof(DeclRefExpr);
273 if (HasQualifier)
274 Size += sizeof(NameQualifier);
275
276 if (NumTemplateArgs)
277 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
278
Chris Lattner5c0b4052010-10-30 05:14:06 +0000279 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000280 return new (Mem) DeclRefExpr(EmptyShell());
281}
282
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000283SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000284 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000285 if (hasQualifier())
286 R.setBegin(getQualifierRange().getBegin());
John McCallb3774b52010-08-19 23:49:38 +0000287 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000288 R.setEnd(getRAngleLoc());
289 return R;
290}
291
Anders Carlsson2fb08242009-09-08 18:24:21 +0000292// FIXME: Maybe this should use DeclPrinter with a special "print predefined
293// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000294std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
295 ASTContext &Context = CurrentDecl->getASTContext();
296
Anders Carlsson2fb08242009-09-08 18:24:21 +0000297 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000298 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000299 return FD->getNameAsString();
300
301 llvm::SmallString<256> Name;
302 llvm::raw_svector_ostream Out(Name);
303
304 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000305 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000306 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000307 if (MD->isStatic())
308 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000309 }
310
311 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000312
313 std::string Proto = FD->getQualifiedNameAsString(Policy);
314
John McCall9dd450b2009-09-21 23:43:11 +0000315 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000316 const FunctionProtoType *FT = 0;
317 if (FD->hasWrittenPrototype())
318 FT = dyn_cast<FunctionProtoType>(AFT);
319
320 Proto += "(";
321 if (FT) {
322 llvm::raw_string_ostream POut(Proto);
323 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
324 if (i) POut << ", ";
325 std::string Param;
326 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
327 POut << Param;
328 }
329
330 if (FT->isVariadic()) {
331 if (FD->getNumParams()) POut << ", ";
332 POut << "...";
333 }
334 }
335 Proto += ")";
336
Sam Weinig4e83bd22009-12-27 01:38:20 +0000337 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
338 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
339 if (ThisQuals.hasConst())
340 Proto += " const";
341 if (ThisQuals.hasVolatile())
342 Proto += " volatile";
343 }
344
Sam Weinigd060ed42009-12-06 23:55:13 +0000345 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
346 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000347
348 Out << Proto;
349
350 Out.flush();
351 return Name.str().str();
352 }
353 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
354 llvm::SmallString<256> Name;
355 llvm::raw_svector_ostream Out(Name);
356 Out << (MD->isInstanceMethod() ? '-' : '+');
357 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000358
359 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
360 // a null check to avoid a crash.
361 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000362 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000363
Anders Carlsson2fb08242009-09-08 18:24:21 +0000364 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000365 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
366 Out << '(' << CID << ')';
367
Anders Carlsson2fb08242009-09-08 18:24:21 +0000368 Out << ' ';
369 Out << MD->getSelector().getAsString();
370 Out << ']';
371
372 Out.flush();
373 return Name.str().str();
374 }
375 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
376 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
377 return "top level";
378 }
379 return "";
380}
381
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000382void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
383 if (hasAllocation())
384 C.Deallocate(pVal);
385
386 BitWidth = Val.getBitWidth();
387 unsigned NumWords = Val.getNumWords();
388 const uint64_t* Words = Val.getRawData();
389 if (NumWords > 1) {
390 pVal = new (C) uint64_t[NumWords];
391 std::copy(Words, Words + NumWords, pVal);
392 } else if (NumWords == 1)
393 VAL = Words[0];
394 else
395 VAL = 0;
396}
397
398IntegerLiteral *
399IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
400 QualType type, SourceLocation l) {
401 return new (C) IntegerLiteral(C, V, type, l);
402}
403
404IntegerLiteral *
405IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
406 return new (C) IntegerLiteral(Empty);
407}
408
409FloatingLiteral *
410FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
411 bool isexact, QualType Type, SourceLocation L) {
412 return new (C) FloatingLiteral(C, V, isexact, Type, L);
413}
414
415FloatingLiteral *
416FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
417 return new (C) FloatingLiteral(Empty);
418}
419
Chris Lattnera0173132008-06-07 22:13:43 +0000420/// getValueAsApproximateDouble - This returns the value as an inaccurate
421/// double. Note that this may cause loss of precision, but is useful for
422/// debugging dumps, etc.
423double FloatingLiteral::getValueAsApproximateDouble() const {
424 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000425 bool ignored;
426 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
427 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000428 return V.convertToDouble();
429}
430
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000431StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
432 unsigned ByteLength, bool Wide,
433 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000434 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000435 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000436 // Allocate enough space for the StringLiteral plus an array of locations for
437 // any concatenated string tokens.
438 void *Mem = C.Allocate(sizeof(StringLiteral)+
439 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000440 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000441 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000442
Steve Naroffdf7855b2007-02-21 23:46:25 +0000443 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000444 char *AStrData = new (C, 1) char[ByteLength];
445 memcpy(AStrData, StrData, ByteLength);
446 SL->StrData = AStrData;
447 SL->ByteLength = ByteLength;
448 SL->IsWide = Wide;
449 SL->TokLocs[0] = Loc[0];
450 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000451
Chris Lattner630970d2009-02-18 05:49:11 +0000452 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000453 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
454 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000455}
456
Douglas Gregor958dfc92009-04-15 16:35:07 +0000457StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
458 void *Mem = C.Allocate(sizeof(StringLiteral)+
459 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000460 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000461 StringLiteral *SL = new (Mem) StringLiteral(QualType());
462 SL->StrData = 0;
463 SL->ByteLength = 0;
464 SL->NumConcatenated = NumStrs;
465 return SL;
466}
467
Daniel Dunbar36217882009-09-22 03:27:33 +0000468void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000469 char *AStrData = new (C, 1) char[Str.size()];
470 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000471 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000472 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000473}
474
Chris Lattnere925d612010-11-17 07:37:15 +0000475/// getLocationOfByte - Return a source location that points to the specified
476/// byte of this string literal.
477///
478/// Strings are amazingly complex. They can be formed from multiple tokens and
479/// can have escape sequences in them in addition to the usual trigraph and
480/// escaped newline business. This routine handles this complexity.
481///
482SourceLocation StringLiteral::
483getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
484 const LangOptions &Features, const TargetInfo &Target) const {
485 assert(!isWide() && "This doesn't work for wide strings yet");
486
487 // Loop over all of the tokens in this string until we find the one that
488 // contains the byte we're looking for.
489 unsigned TokNo = 0;
490 while (1) {
491 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
492 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
493
494 // Get the spelling of the string so that we can get the data that makes up
495 // the string literal, not the identifier for the macro it is potentially
496 // expanded through.
497 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
498
499 // Re-lex the token to get its length and original spelling.
500 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
501 bool Invalid = false;
502 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
503 if (Invalid)
504 return StrTokSpellingLoc;
505
506 const char *StrData = Buffer.data()+LocInfo.second;
507
508 // Create a langops struct and enable trigraphs. This is sufficient for
509 // relexing tokens.
510 LangOptions LangOpts;
511 LangOpts.Trigraphs = true;
512
513 // Create a lexer starting at the beginning of this token.
514 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
515 Buffer.end());
516 Token TheTok;
517 TheLexer.LexFromRawLexer(TheTok);
518
519 // Use the StringLiteralParser to compute the length of the string in bytes.
520 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
521 unsigned TokNumBytes = SLP.GetStringLength();
522
523 // If the byte is in this token, return the location of the byte.
524 if (ByteNo < TokNumBytes ||
525 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
526 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
527
528 // Now that we know the offset of the token in the spelling, use the
529 // preprocessor to get the offset in the original source.
530 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
531 }
532
533 // Move to the next string token.
534 ++TokNo;
535 ByteNo -= TokNumBytes;
536 }
537}
538
539
540
Chris Lattner1b926492006-08-23 06:42:10 +0000541/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
542/// corresponds to, e.g. "sizeof" or "[pre]++".
543const char *UnaryOperator::getOpcodeStr(Opcode Op) {
544 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000545 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000546 case UO_PostInc: return "++";
547 case UO_PostDec: return "--";
548 case UO_PreInc: return "++";
549 case UO_PreDec: return "--";
550 case UO_AddrOf: return "&";
551 case UO_Deref: return "*";
552 case UO_Plus: return "+";
553 case UO_Minus: return "-";
554 case UO_Not: return "~";
555 case UO_LNot: return "!";
556 case UO_Real: return "__real";
557 case UO_Imag: return "__imag";
558 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000559 }
560}
561
John McCalle3027922010-08-25 11:45:40 +0000562UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000563UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
564 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000565 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000566 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
567 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
568 case OO_Amp: return UO_AddrOf;
569 case OO_Star: return UO_Deref;
570 case OO_Plus: return UO_Plus;
571 case OO_Minus: return UO_Minus;
572 case OO_Tilde: return UO_Not;
573 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000574 }
575}
576
577OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
578 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000579 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
580 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
581 case UO_AddrOf: return OO_Amp;
582 case UO_Deref: return OO_Star;
583 case UO_Plus: return OO_Plus;
584 case UO_Minus: return OO_Minus;
585 case UO_Not: return OO_Tilde;
586 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000587 default: return OO_None;
588 }
589}
590
591
Chris Lattner0eedafe2006-08-24 04:56:27 +0000592//===----------------------------------------------------------------------===//
593// Postfix Operators.
594//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000595
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000596CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
John McCall7decc9e2010-11-18 06:31:45 +0000597 unsigned numargs, QualType t, ExprValueKind VK,
598 SourceLocation rparenloc)
599 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregor4619e432008-12-05 23:32:09 +0000600 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000601 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000602 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000603
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000604 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000605 SubExprs[FN] = fn;
606 for (unsigned i = 0; i != numargs; ++i)
607 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000608
Douglas Gregor993603d2008-11-14 16:09:21 +0000609 RParenLoc = rparenloc;
610}
Nate Begeman1e36a852008-01-17 17:46:27 +0000611
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000612CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000613 QualType t, ExprValueKind VK, SourceLocation rparenloc)
614 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregor4619e432008-12-05 23:32:09 +0000615 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000616 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000617 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000618
619 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000620 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000621 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000622 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000623
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000624 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000625}
626
Mike Stump11289f42009-09-09 15:08:12 +0000627CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
628 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000629 SubExprs = new (C) Stmt*[1];
630}
631
Nuno Lopes518e3702009-12-20 23:11:08 +0000632Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000633 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000634 // If we're calling a dereference, look at the pointer instead.
635 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
636 if (BO->isPtrMemOp())
637 CEE = BO->getRHS()->IgnoreParenCasts();
638 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
639 if (UO->getOpcode() == UO_Deref)
640 CEE = UO->getSubExpr()->IgnoreParenCasts();
641 }
Chris Lattner52301912009-07-17 15:46:27 +0000642 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000643 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000644 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
645 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000646
647 return 0;
648}
649
Nuno Lopes518e3702009-12-20 23:11:08 +0000650FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000651 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000652}
653
Chris Lattnere4407ed2007-12-28 05:25:02 +0000654/// setNumArgs - This changes the number of arguments present in this call.
655/// Any orphaned expressions are deleted by this, and any new operands are set
656/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000657void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000658 // No change, just return.
659 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattnere4407ed2007-12-28 05:25:02 +0000661 // If shrinking # arguments, just delete the extras and forgot them.
662 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000663 this->NumArgs = NumArgs;
664 return;
665 }
666
667 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000668 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000669 // Copy over args.
670 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
671 NewSubExprs[i] = SubExprs[i];
672 // Null out new args.
673 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
674 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000675
Douglas Gregorba6e5572009-04-17 21:46:47 +0000676 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000677 SubExprs = NewSubExprs;
678 this->NumArgs = NumArgs;
679}
680
Chris Lattner01ff98a2008-10-06 05:00:53 +0000681/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
682/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000683unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000684 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000685 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000686 // ImplicitCastExpr.
687 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
688 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000689 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000690
Steve Narofff6e3b3292008-01-31 01:07:12 +0000691 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
692 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000693 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000694
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000695 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
696 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000697 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000698
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000699 if (!FDecl->getIdentifier())
700 return 0;
701
Douglas Gregor15fc9562009-09-12 00:22:50 +0000702 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000703}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000704
Anders Carlsson00a27592009-05-26 04:57:27 +0000705QualType CallExpr::getCallReturnType() const {
706 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000707 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000708 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000709 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000710 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000711 else if (const MemberPointerType *MPT
712 = CalleeType->getAs<MemberPointerType>())
713 CalleeType = MPT->getPointeeType();
714
John McCall9dd450b2009-09-21 23:43:11 +0000715 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000716 return FnType->getResultType();
717}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000718
Alexis Hunta8136cc2010-05-05 15:23:54 +0000719OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000720 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000721 TypeSourceInfo *tsi,
722 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000723 Expr** exprsPtr, unsigned numExprs,
724 SourceLocation RParenLoc) {
725 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000726 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000727 sizeof(Expr*) * numExprs);
728
729 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
730 exprsPtr, numExprs, RParenLoc);
731}
732
733OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
734 unsigned numComps, unsigned numExprs) {
735 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
736 sizeof(OffsetOfNode) * numComps +
737 sizeof(Expr*) * numExprs);
738 return new (Mem) OffsetOfExpr(numComps, numExprs);
739}
740
Alexis Hunta8136cc2010-05-05 15:23:54 +0000741OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000742 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000743 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000744 Expr** exprsPtr, unsigned numExprs,
745 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000746 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
747 /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000748 /*ValueDependent=*/tsi->getType()->isDependentType() ||
749 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
750 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000751 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
752 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000753{
754 for(unsigned i = 0; i < numComps; ++i) {
755 setComponent(i, compsPtr[i]);
756 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000757
Douglas Gregor882211c2010-04-28 22:16:22 +0000758 for(unsigned i = 0; i < numExprs; ++i) {
759 setIndexExpr(i, exprsPtr[i]);
760 }
761}
762
763IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
764 assert(getKind() == Field || getKind() == Identifier);
765 if (getKind() == Field)
766 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000767
Douglas Gregor882211c2010-04-28 22:16:22 +0000768 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
769}
770
Mike Stump11289f42009-09-09 15:08:12 +0000771MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
772 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000773 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000774 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000775 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000776 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000777 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000778 QualType ty,
779 ExprValueKind vk,
780 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000781 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000782
John McCalla8ae2222010-04-06 21:38:20 +0000783 bool hasQualOrFound = (qual != 0 ||
784 founddecl.getDecl() != memberdecl ||
785 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000786 if (hasQualOrFound)
787 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000788
John McCall6b51f282009-11-23 01:53:49 +0000789 if (targs)
790 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattner5c0b4052010-10-30 05:14:06 +0000792 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000793 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
794 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000795
796 if (hasQualOrFound) {
797 if (qual && qual->isDependent()) {
798 E->setValueDependent(true);
799 E->setTypeDependent(true);
800 }
801 E->HasQualifierOrFoundDecl = true;
802
803 MemberNameQualifier *NQ = E->getMemberQualifier();
804 NQ->NNS = qual;
805 NQ->Range = qualrange;
806 NQ->FoundDecl = founddecl;
807 }
808
809 if (targs) {
810 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000811 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000812 }
813
814 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000815}
816
Anders Carlsson496335e2009-09-03 00:59:21 +0000817const char *CastExpr::getCastKindName() const {
818 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000819 case CK_Dependent:
820 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000821 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000822 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000823 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000824 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000825 case CK_LValueToRValue:
826 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +0000827 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000828 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000829 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000830 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000831 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000832 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000833 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000834 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000835 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000836 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000837 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000838 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000839 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000840 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000841 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000842 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000843 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000844 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000845 case CK_NullToPointer:
846 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000847 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000848 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000849 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000850 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000851 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000852 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000853 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000854 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000855 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000856 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000857 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000858 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +0000859 case CK_PointerToBoolean:
860 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000861 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +0000862 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +0000863 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +0000864 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +0000865 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +0000866 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +0000867 case CK_IntegralToBoolean:
868 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000869 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +0000870 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +0000871 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +0000872 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +0000873 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000874 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +0000875 case CK_FloatingToBoolean:
876 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000877 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000878 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000879 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000880 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000881 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000882 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +0000883 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +0000884 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +0000885 case CK_FloatingRealToComplex:
886 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +0000887 case CK_FloatingComplexToReal:
888 return "FloatingComplexToReal";
889 case CK_FloatingComplexToBoolean:
890 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +0000891 case CK_FloatingComplexCast:
892 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +0000893 case CK_FloatingComplexToIntegralComplex:
894 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +0000895 case CK_IntegralRealToComplex:
896 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +0000897 case CK_IntegralComplexToReal:
898 return "IntegralComplexToReal";
899 case CK_IntegralComplexToBoolean:
900 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +0000901 case CK_IntegralComplexCast:
902 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +0000903 case CK_IntegralComplexToFloatingComplex:
904 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +0000905 }
Mike Stump11289f42009-09-09 15:08:12 +0000906
John McCallc5e62b42010-11-13 09:02:35 +0000907 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +0000908 return 0;
909}
910
Douglas Gregord196a582009-12-14 19:27:10 +0000911Expr *CastExpr::getSubExprAsWritten() {
912 Expr *SubExpr = 0;
913 CastExpr *E = this;
914 do {
915 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000916
Douglas Gregord196a582009-12-14 19:27:10 +0000917 // Skip any temporary bindings; they're implicit.
918 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
919 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000920
Douglas Gregord196a582009-12-14 19:27:10 +0000921 // Conversions by constructor and conversion functions have a
922 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +0000923 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000924 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +0000925 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +0000926 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000927
Douglas Gregord196a582009-12-14 19:27:10 +0000928 // If the subexpression we're left with is an implicit cast, look
929 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000930 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
931
Douglas Gregord196a582009-12-14 19:27:10 +0000932 return SubExpr;
933}
934
John McCallcf142162010-08-07 06:22:56 +0000935CXXBaseSpecifier **CastExpr::path_buffer() {
936 switch (getStmtClass()) {
937#define ABSTRACT_STMT(x)
938#define CASTEXPR(Type, Base) \
939 case Stmt::Type##Class: \
940 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
941#define STMT(Type, Base)
942#include "clang/AST/StmtNodes.inc"
943 default:
944 llvm_unreachable("non-cast expressions not possible here");
945 return 0;
946 }
947}
948
949void CastExpr::setCastPath(const CXXCastPath &Path) {
950 assert(Path.size() == path_size());
951 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
952}
953
954ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
955 CastKind Kind, Expr *Operand,
956 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +0000957 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +0000958 unsigned PathSize = (BasePath ? BasePath->size() : 0);
959 void *Buffer =
960 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
961 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +0000962 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +0000963 if (PathSize) E->setCastPath(*BasePath);
964 return E;
965}
966
967ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
968 unsigned PathSize) {
969 void *Buffer =
970 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
971 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
972}
973
974
975CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000976 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000977 const CXXCastPath *BasePath,
978 TypeSourceInfo *WrittenTy,
979 SourceLocation L, SourceLocation R) {
980 unsigned PathSize = (BasePath ? BasePath->size() : 0);
981 void *Buffer =
982 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
983 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000984 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +0000985 if (PathSize) E->setCastPath(*BasePath);
986 return E;
987}
988
989CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
990 void *Buffer =
991 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
992 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
993}
994
Chris Lattner1b926492006-08-23 06:42:10 +0000995/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
996/// corresponds to, e.g. "<<=".
997const char *BinaryOperator::getOpcodeStr(Opcode Op) {
998 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000999 case BO_PtrMemD: return ".*";
1000 case BO_PtrMemI: return "->*";
1001 case BO_Mul: return "*";
1002 case BO_Div: return "/";
1003 case BO_Rem: return "%";
1004 case BO_Add: return "+";
1005 case BO_Sub: return "-";
1006 case BO_Shl: return "<<";
1007 case BO_Shr: return ">>";
1008 case BO_LT: return "<";
1009 case BO_GT: return ">";
1010 case BO_LE: return "<=";
1011 case BO_GE: return ">=";
1012 case BO_EQ: return "==";
1013 case BO_NE: return "!=";
1014 case BO_And: return "&";
1015 case BO_Xor: return "^";
1016 case BO_Or: return "|";
1017 case BO_LAnd: return "&&";
1018 case BO_LOr: return "||";
1019 case BO_Assign: return "=";
1020 case BO_MulAssign: return "*=";
1021 case BO_DivAssign: return "/=";
1022 case BO_RemAssign: return "%=";
1023 case BO_AddAssign: return "+=";
1024 case BO_SubAssign: return "-=";
1025 case BO_ShlAssign: return "<<=";
1026 case BO_ShrAssign: return ">>=";
1027 case BO_AndAssign: return "&=";
1028 case BO_XorAssign: return "^=";
1029 case BO_OrAssign: return "|=";
1030 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001031 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001032
1033 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001034}
Steve Naroff47500512007-04-19 23:00:49 +00001035
John McCalle3027922010-08-25 11:45:40 +00001036BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001037BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1038 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001039 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001040 case OO_Plus: return BO_Add;
1041 case OO_Minus: return BO_Sub;
1042 case OO_Star: return BO_Mul;
1043 case OO_Slash: return BO_Div;
1044 case OO_Percent: return BO_Rem;
1045 case OO_Caret: return BO_Xor;
1046 case OO_Amp: return BO_And;
1047 case OO_Pipe: return BO_Or;
1048 case OO_Equal: return BO_Assign;
1049 case OO_Less: return BO_LT;
1050 case OO_Greater: return BO_GT;
1051 case OO_PlusEqual: return BO_AddAssign;
1052 case OO_MinusEqual: return BO_SubAssign;
1053 case OO_StarEqual: return BO_MulAssign;
1054 case OO_SlashEqual: return BO_DivAssign;
1055 case OO_PercentEqual: return BO_RemAssign;
1056 case OO_CaretEqual: return BO_XorAssign;
1057 case OO_AmpEqual: return BO_AndAssign;
1058 case OO_PipeEqual: return BO_OrAssign;
1059 case OO_LessLess: return BO_Shl;
1060 case OO_GreaterGreater: return BO_Shr;
1061 case OO_LessLessEqual: return BO_ShlAssign;
1062 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1063 case OO_EqualEqual: return BO_EQ;
1064 case OO_ExclaimEqual: return BO_NE;
1065 case OO_LessEqual: return BO_LE;
1066 case OO_GreaterEqual: return BO_GE;
1067 case OO_AmpAmp: return BO_LAnd;
1068 case OO_PipePipe: return BO_LOr;
1069 case OO_Comma: return BO_Comma;
1070 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001071 }
1072}
1073
1074OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1075 static const OverloadedOperatorKind OverOps[] = {
1076 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1077 OO_Star, OO_Slash, OO_Percent,
1078 OO_Plus, OO_Minus,
1079 OO_LessLess, OO_GreaterGreater,
1080 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1081 OO_EqualEqual, OO_ExclaimEqual,
1082 OO_Amp,
1083 OO_Caret,
1084 OO_Pipe,
1085 OO_AmpAmp,
1086 OO_PipePipe,
1087 OO_Equal, OO_StarEqual,
1088 OO_SlashEqual, OO_PercentEqual,
1089 OO_PlusEqual, OO_MinusEqual,
1090 OO_LessLessEqual, OO_GreaterGreaterEqual,
1091 OO_AmpEqual, OO_CaretEqual,
1092 OO_PipeEqual,
1093 OO_Comma
1094 };
1095 return OverOps[Opc];
1096}
1097
Ted Kremenekac034612010-04-13 23:39:13 +00001098InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001099 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001100 SourceLocation rbraceloc)
John McCall7decc9e2010-11-18 06:31:45 +00001101 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001102 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001103 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001104 UnionFieldInit(0), HadArrayRangeDesignator(false)
1105{
Ted Kremenek013041e2010-02-19 01:50:18 +00001106 for (unsigned I = 0; I != numInits; ++I) {
1107 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001108 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001109 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001110 ExprBits.ValueDependent = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001111 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001112
Ted Kremenekac034612010-04-13 23:39:13 +00001113 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001114}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001115
Ted Kremenekac034612010-04-13 23:39:13 +00001116void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001117 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001118 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001119}
1120
Ted Kremenekac034612010-04-13 23:39:13 +00001121void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001122 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001123}
1124
Ted Kremenekac034612010-04-13 23:39:13 +00001125Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001126 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001127 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001128 InitExprs.back() = expr;
1129 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001130 }
Mike Stump11289f42009-09-09 15:08:12 +00001131
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001132 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1133 InitExprs[Init] = expr;
1134 return Result;
1135}
1136
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001137SourceRange InitListExpr::getSourceRange() const {
1138 if (SyntacticForm)
1139 return SyntacticForm->getSourceRange();
1140 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1141 if (Beg.isInvalid()) {
1142 // Find the first non-null initializer.
1143 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1144 E = InitExprs.end();
1145 I != E; ++I) {
1146 if (Stmt *S = *I) {
1147 Beg = S->getLocStart();
1148 break;
1149 }
1150 }
1151 }
1152 if (End.isInvalid()) {
1153 // Find the first non-null initializer from the end.
1154 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1155 E = InitExprs.rend();
1156 I != E; ++I) {
1157 if (Stmt *S = *I) {
1158 End = S->getSourceRange().getEnd();
1159 break;
1160 }
1161 }
1162 }
1163 return SourceRange(Beg, End);
1164}
1165
Steve Naroff991e99d2008-09-04 15:31:07 +00001166/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001167///
1168const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001169 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001170 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001171}
1172
Mike Stump11289f42009-09-09 15:08:12 +00001173SourceLocation BlockExpr::getCaretLocation() const {
1174 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001175}
Mike Stump11289f42009-09-09 15:08:12 +00001176const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001177 return TheBlock->getBody();
1178}
Mike Stump11289f42009-09-09 15:08:12 +00001179Stmt *BlockExpr::getBody() {
1180 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001181}
Steve Naroff415d3d52008-10-08 17:01:13 +00001182
1183
Chris Lattner1ec5f562007-06-27 05:38:08 +00001184//===----------------------------------------------------------------------===//
1185// Generic Expression Routines
1186//===----------------------------------------------------------------------===//
1187
Chris Lattner237f2752009-02-14 07:37:35 +00001188/// isUnusedResultAWarning - Return true if this immediate expression should
1189/// be warned about if the result is unused. If so, fill in Loc and Ranges
1190/// with location to warn on and the source range[s] to report with the
1191/// warning.
1192bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001193 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001194 // Don't warn if the expr is type dependent. The type could end up
1195 // instantiating to void.
1196 if (isTypeDependent())
1197 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner1ec5f562007-06-27 05:38:08 +00001199 switch (getStmtClass()) {
1200 default:
John McCallc493a732010-03-12 07:11:26 +00001201 if (getType()->isVoidType())
1202 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001203 Loc = getExprLoc();
1204 R1 = getSourceRange();
1205 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001206 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001207 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001208 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001209 case UnaryOperatorClass: {
1210 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner1ec5f562007-06-27 05:38:08 +00001212 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001213 default: break;
John McCalle3027922010-08-25 11:45:40 +00001214 case UO_PostInc:
1215 case UO_PostDec:
1216 case UO_PreInc:
1217 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001218 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001219 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001220 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001221 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001222 return false;
1223 break;
John McCalle3027922010-08-25 11:45:40 +00001224 case UO_Real:
1225 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001226 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001227 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1228 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001229 return false;
1230 break;
John McCalle3027922010-08-25 11:45:40 +00001231 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001232 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001233 }
Chris Lattner237f2752009-02-14 07:37:35 +00001234 Loc = UO->getOperatorLoc();
1235 R1 = UO->getSubExpr()->getSourceRange();
1236 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001237 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001238 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001239 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001240 switch (BO->getOpcode()) {
1241 default:
1242 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001243 // Consider the RHS of comma for side effects. LHS was checked by
1244 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001245 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001246 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1247 // lvalue-ness) of an assignment written in a macro.
1248 if (IntegerLiteral *IE =
1249 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1250 if (IE->getValue() == 0)
1251 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001252 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1253 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001254 case BO_LAnd:
1255 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001256 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1257 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1258 return false;
1259 break;
John McCall1e3715a2010-02-16 04:10:53 +00001260 }
Chris Lattner237f2752009-02-14 07:37:35 +00001261 if (BO->isAssignmentOp())
1262 return false;
1263 Loc = BO->getOperatorLoc();
1264 R1 = BO->getLHS()->getSourceRange();
1265 R2 = BO->getRHS()->getSourceRange();
1266 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001267 }
Chris Lattner86928112007-08-25 02:00:02 +00001268 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001269 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001270 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001271
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001272 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001273 // The condition must be evaluated, but if either the LHS or RHS is a
1274 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001275 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001276 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001277 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001278 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001279 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001280 }
1281
Chris Lattnera44d1162007-06-27 05:58:59 +00001282 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001283 // If the base pointer or element is to a volatile pointer/field, accessing
1284 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001285 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001286 return false;
1287 Loc = cast<MemberExpr>(this)->getMemberLoc();
1288 R1 = SourceRange(Loc, Loc);
1289 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1290 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001291
Chris Lattner1ec5f562007-06-27 05:38:08 +00001292 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001293 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001294 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001295 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001296 return false;
1297 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1298 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1299 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1300 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001301
Chris Lattner1ec5f562007-06-27 05:38:08 +00001302 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001303 case CXXOperatorCallExprClass:
1304 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001305 // If this is a direct call, get the callee.
1306 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001307 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001308 // If the callee has attribute pure, const, or warn_unused_result, warn
1309 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001310 //
1311 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1312 // updated to match for QoI.
1313 if (FD->getAttr<WarnUnusedResultAttr>() ||
1314 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1315 Loc = CE->getCallee()->getLocStart();
1316 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001317
Chris Lattner1a6babf2009-10-13 04:53:48 +00001318 if (unsigned NumArgs = CE->getNumArgs())
1319 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1320 CE->getArg(NumArgs-1)->getLocEnd());
1321 return true;
1322 }
Chris Lattner237f2752009-02-14 07:37:35 +00001323 }
1324 return false;
1325 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001326
1327 case CXXTemporaryObjectExprClass:
1328 case CXXConstructExprClass:
1329 return false;
1330
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001331 case ObjCMessageExprClass: {
1332 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1333 const ObjCMethodDecl *MD = ME->getMethodDecl();
1334 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1335 Loc = getExprLoc();
1336 return true;
1337 }
Chris Lattner237f2752009-02-14 07:37:35 +00001338 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001339 }
Mike Stump11289f42009-09-09 15:08:12 +00001340
John McCallb7bd14f2010-12-02 01:19:52 +00001341 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001342 Loc = getExprLoc();
1343 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001344 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001345
Chris Lattner944d3062008-07-26 19:51:01 +00001346 case StmtExprClass: {
1347 // Statement exprs don't logically have side effects themselves, but are
1348 // sometimes used in macros in ways that give them a type that is unused.
1349 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1350 // however, if the result of the stmt expr is dead, we don't want to emit a
1351 // warning.
1352 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001353 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001354 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001355 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001356 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1357 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1358 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1359 }
Mike Stump11289f42009-09-09 15:08:12 +00001360
John McCallc493a732010-03-12 07:11:26 +00001361 if (getType()->isVoidType())
1362 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001363 Loc = cast<StmtExpr>(this)->getLParenLoc();
1364 R1 = getSourceRange();
1365 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001366 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001367 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001368 // If this is an explicit cast to void, allow it. People do this when they
1369 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001370 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001371 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001372 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1373 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1374 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001375 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001376 if (getType()->isVoidType())
1377 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001378 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001379
Anders Carlsson6aa50392009-11-17 17:11:23 +00001380 // If this is a cast to void or a constructor conversion, check the operand.
1381 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001382 if (CE->getCastKind() == CK_ToVoid ||
1383 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001384 return (cast<CastExpr>(this)->getSubExpr()
1385 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001386 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1387 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1388 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001391 case ImplicitCastExprClass:
1392 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001393 return (cast<ImplicitCastExpr>(this)
1394 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001395
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001396 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001397 return (cast<CXXDefaultArgExpr>(this)
1398 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001399
1400 case CXXNewExprClass:
1401 // FIXME: In theory, there might be new expressions that don't have side
1402 // effects (e.g. a placement new with an uninitialized POD).
1403 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001404 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001405 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001406 return (cast<CXXBindTemporaryExpr>(this)
1407 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001408 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001409 return (cast<CXXExprWithTemporaries>(this)
1410 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001411 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001412}
1413
Fariborz Jahanian07735332009-02-22 18:40:18 +00001414/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001415/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001416bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001417 switch (getStmtClass()) {
1418 default:
1419 return false;
1420 case ObjCIvarRefExprClass:
1421 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001422 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001423 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001424 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001425 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001426 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001427 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001428 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001429 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001430 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001431 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001432 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1433 if (VD->hasGlobalStorage())
1434 return true;
1435 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001436 // dereferencing to a pointer is always a gc'able candidate,
1437 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001438 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001439 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001440 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001441 return false;
1442 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001443 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001444 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001445 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001446 }
1447 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001448 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001449 }
1450}
Sebastian Redlce354af2010-09-10 20:55:33 +00001451
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001452bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1453 if (isTypeDependent())
1454 return false;
John McCall086a4642010-11-24 05:12:34 +00001455 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001456}
1457
Sebastian Redlce354af2010-09-10 20:55:33 +00001458static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1459 Expr::CanThrowResult CT2) {
1460 // CanThrowResult constants are ordered so that the maximum is the correct
1461 // merge result.
1462 return CT1 > CT2 ? CT1 : CT2;
1463}
1464
1465static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1466 Expr *E = const_cast<Expr*>(CE);
1467 Expr::CanThrowResult R = Expr::CT_Cannot;
1468 for (Expr::child_iterator I = E->child_begin(), IE = E->child_end();
1469 I != IE && R != Expr::CT_Can; ++I) {
1470 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1471 }
1472 return R;
1473}
1474
1475static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1476 bool NullThrows = true) {
1477 if (!D)
1478 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1479
1480 // See if we can get a function type from the decl somehow.
1481 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1482 if (!VD) // If we have no clue what we're calling, assume the worst.
1483 return Expr::CT_Can;
1484
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001485 // As an extension, we assume that __attribute__((nothrow)) functions don't
1486 // throw.
1487 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1488 return Expr::CT_Cannot;
1489
Sebastian Redlce354af2010-09-10 20:55:33 +00001490 QualType T = VD->getType();
1491 const FunctionProtoType *FT;
1492 if ((FT = T->getAs<FunctionProtoType>())) {
1493 } else if (const PointerType *PT = T->getAs<PointerType>())
1494 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1495 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1496 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1497 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1498 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1499 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1500 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1501
1502 if (!FT)
1503 return Expr::CT_Can;
1504
1505 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1506}
1507
1508static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1509 if (DC->isTypeDependent())
1510 return Expr::CT_Dependent;
1511
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001512 if (!DC->getTypeAsWritten()->isReferenceType())
1513 return Expr::CT_Cannot;
1514
Sebastian Redlce354af2010-09-10 20:55:33 +00001515 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1516}
1517
1518static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1519 const CXXTypeidExpr *DC) {
1520 if (DC->isTypeOperand())
1521 return Expr::CT_Cannot;
1522
1523 Expr *Op = DC->getExprOperand();
1524 if (Op->isTypeDependent())
1525 return Expr::CT_Dependent;
1526
1527 const RecordType *RT = Op->getType()->getAs<RecordType>();
1528 if (!RT)
1529 return Expr::CT_Cannot;
1530
1531 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1532 return Expr::CT_Cannot;
1533
1534 if (Op->Classify(C).isPRValue())
1535 return Expr::CT_Cannot;
1536
1537 return Expr::CT_Can;
1538}
1539
1540Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1541 // C++ [expr.unary.noexcept]p3:
1542 // [Can throw] if in a potentially-evaluated context the expression would
1543 // contain:
1544 switch (getStmtClass()) {
1545 case CXXThrowExprClass:
1546 // - a potentially evaluated throw-expression
1547 return CT_Can;
1548
1549 case CXXDynamicCastExprClass: {
1550 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1551 // where T is a reference type, that requires a run-time check
1552 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1553 if (CT == CT_Can)
1554 return CT;
1555 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1556 }
1557
1558 case CXXTypeidExprClass:
1559 // - a potentially evaluated typeid expression applied to a glvalue
1560 // expression whose type is a polymorphic class type
1561 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1562
1563 // - a potentially evaluated call to a function, member function, function
1564 // pointer, or member function pointer that does not have a non-throwing
1565 // exception-specification
1566 case CallExprClass:
1567 case CXXOperatorCallExprClass:
1568 case CXXMemberCallExprClass: {
1569 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1570 if (CT == CT_Can)
1571 return CT;
1572 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1573 }
1574
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001575 case CXXConstructExprClass:
1576 case CXXTemporaryObjectExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001577 CanThrowResult CT = CanCalleeThrow(
1578 cast<CXXConstructExpr>(this)->getConstructor());
1579 if (CT == CT_Can)
1580 return CT;
1581 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1582 }
1583
1584 case CXXNewExprClass: {
1585 CanThrowResult CT = MergeCanThrow(
1586 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1587 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1588 /*NullThrows*/false));
1589 if (CT == CT_Can)
1590 return CT;
1591 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1592 }
1593
1594 case CXXDeleteExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001595 CanThrowResult CT = CanCalleeThrow(
1596 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1597 if (CT == CT_Can)
1598 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001599 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1600 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1601 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1602 Arg = Cast->getSubExpr();
1603 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1604 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1605 CanThrowResult CT2 = CanCalleeThrow(
1606 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1607 if (CT2 == CT_Can)
1608 return CT2;
1609 CT = MergeCanThrow(CT, CT2);
1610 }
1611 }
1612 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1613 }
1614
1615 case CXXBindTemporaryExprClass: {
1616 // The bound temporary has to be destroyed again, which might throw.
1617 CanThrowResult CT = CanCalleeThrow(
1618 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1619 if (CT == CT_Can)
1620 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001621 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1622 }
1623
1624 // ObjC message sends are like function calls, but never have exception
1625 // specs.
1626 case ObjCMessageExprClass:
1627 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001628 return CT_Can;
1629
1630 // Many other things have subexpressions, so we have to test those.
1631 // Some are simple:
1632 case ParenExprClass:
1633 case MemberExprClass:
1634 case CXXReinterpretCastExprClass:
1635 case CXXConstCastExprClass:
1636 case ConditionalOperatorClass:
1637 case CompoundLiteralExprClass:
1638 case ExtVectorElementExprClass:
1639 case InitListExprClass:
1640 case DesignatedInitExprClass:
1641 case ParenListExprClass:
1642 case VAArgExprClass:
1643 case CXXDefaultArgExprClass:
Sebastian Redla8bac372010-09-10 23:27:10 +00001644 case CXXExprWithTemporariesClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001645 case ObjCIvarRefExprClass:
1646 case ObjCIsaExprClass:
1647 case ShuffleVectorExprClass:
1648 return CanSubExprsThrow(C, this);
1649
1650 // Some might be dependent for other reasons.
1651 case UnaryOperatorClass:
1652 case ArraySubscriptExprClass:
1653 case ImplicitCastExprClass:
1654 case CStyleCastExprClass:
1655 case CXXStaticCastExprClass:
1656 case CXXFunctionalCastExprClass:
1657 case BinaryOperatorClass:
1658 case CompoundAssignOperatorClass: {
1659 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1660 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1661 }
1662
1663 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1664 case StmtExprClass:
1665 return CT_Can;
1666
1667 case ChooseExprClass:
1668 if (isTypeDependent() || isValueDependent())
1669 return CT_Dependent;
1670 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1671
1672 // Some expressions are always dependent.
1673 case DependentScopeDeclRefExprClass:
1674 case CXXUnresolvedConstructExprClass:
1675 case CXXDependentScopeMemberExprClass:
1676 return CT_Dependent;
1677
1678 default:
1679 // All other expressions don't have subexpressions, or else they are
1680 // unevaluated.
1681 return CT_Cannot;
1682 }
1683}
1684
Ted Kremenekfff70962008-01-17 16:57:34 +00001685Expr* Expr::IgnoreParens() {
1686 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001687 while (true) {
1688 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1689 E = P->getSubExpr();
1690 continue;
1691 }
1692 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1693 if (P->getOpcode() == UO_Extension) {
1694 E = P->getSubExpr();
1695 continue;
1696 }
1697 }
1698 return E;
1699 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001700}
1701
Chris Lattnerf2660962008-02-13 01:02:39 +00001702/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1703/// or CastExprs or ImplicitCastExprs, returning their operand.
1704Expr *Expr::IgnoreParenCasts() {
1705 Expr *E = this;
1706 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001707 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001708 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001709 continue;
1710 }
1711 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001712 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001713 continue;
1714 }
1715 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1716 if (P->getOpcode() == UO_Extension) {
1717 E = P->getSubExpr();
1718 continue;
1719 }
1720 }
1721 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001722 }
1723}
1724
John McCalleebc8322010-05-05 22:59:52 +00001725Expr *Expr::IgnoreParenImpCasts() {
1726 Expr *E = this;
1727 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001728 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001729 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001730 continue;
1731 }
1732 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001733 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001734 continue;
1735 }
1736 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1737 if (P->getOpcode() == UO_Extension) {
1738 E = P->getSubExpr();
1739 continue;
1740 }
1741 }
1742 return E;
John McCalleebc8322010-05-05 22:59:52 +00001743 }
1744}
1745
Chris Lattneref26c772009-03-13 17:28:01 +00001746/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1747/// value (including ptr->int casts of the same size). Strip off any
1748/// ParenExpr or CastExprs, returning their operand.
1749Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1750 Expr *E = this;
1751 while (true) {
1752 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1753 E = P->getSubExpr();
1754 continue;
1755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756
Chris Lattneref26c772009-03-13 17:28:01 +00001757 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1758 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001759 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001760 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001761
Chris Lattneref26c772009-03-13 17:28:01 +00001762 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1763 E = SE;
1764 continue;
1765 }
Mike Stump11289f42009-09-09 15:08:12 +00001766
Abramo Bagnara932e3932010-10-15 07:51:18 +00001767 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001768 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00001769 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001770 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001771 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1772 E = SE;
1773 continue;
1774 }
1775 }
Mike Stump11289f42009-09-09 15:08:12 +00001776
Abramo Bagnara932e3932010-10-15 07:51:18 +00001777 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1778 if (P->getOpcode() == UO_Extension) {
1779 E = P->getSubExpr();
1780 continue;
1781 }
1782 }
1783
Chris Lattneref26c772009-03-13 17:28:01 +00001784 return E;
1785 }
1786}
1787
Douglas Gregord196a582009-12-14 19:27:10 +00001788bool Expr::isDefaultArgument() const {
1789 const Expr *E = this;
1790 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1791 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001792
Douglas Gregord196a582009-12-14 19:27:10 +00001793 return isa<CXXDefaultArgExpr>(E);
1794}
Chris Lattneref26c772009-03-13 17:28:01 +00001795
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001796/// \brief Skip over any no-op casts and any temporary-binding
1797/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00001798static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001799 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001800 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001801 E = ICE->getSubExpr();
1802 else
1803 break;
1804 }
1805
1806 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1807 E = BE->getSubExpr();
1808
1809 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001810 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001811 E = ICE->getSubExpr();
1812 else
1813 break;
1814 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00001815
1816 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001817}
1818
John McCall7a626f62010-09-15 10:14:12 +00001819/// isTemporaryObject - Determines if this expression produces a
1820/// temporary of the given class type.
1821bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1822 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1823 return false;
1824
Anders Carlsson66bbf502010-11-28 16:40:49 +00001825 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001826
John McCall02dc8c72010-09-15 20:59:13 +00001827 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001828 if (!E->Classify(C).isPRValue()) {
1829 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00001830 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001831 return false;
1832 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001833
John McCallf4ee1dd2010-09-16 06:57:56 +00001834 // Black-list a few cases which yield pr-values of class type that don't
1835 // refer to temporaries of that type:
1836
1837 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00001838 if (isa<ImplicitCastExpr>(E)) {
1839 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1840 case CK_DerivedToBase:
1841 case CK_UncheckedDerivedToBase:
1842 return false;
1843 default:
1844 break;
1845 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001846 }
1847
John McCallf4ee1dd2010-09-16 06:57:56 +00001848 // - member expressions (all)
1849 if (isa<MemberExpr>(E))
1850 return false;
1851
John McCall7a626f62010-09-15 10:14:12 +00001852 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001853}
1854
Douglas Gregor4619e432008-12-05 23:32:09 +00001855/// hasAnyTypeDependentArguments - Determines if any of the expressions
1856/// in Exprs is type-dependent.
1857bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1858 for (unsigned I = 0; I < NumExprs; ++I)
1859 if (Exprs[I]->isTypeDependent())
1860 return true;
1861
1862 return false;
1863}
1864
1865/// hasAnyValueDependentArguments - Determines if any of the expressions
1866/// in Exprs is value-dependent.
1867bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1868 for (unsigned I = 0; I < NumExprs; ++I)
1869 if (Exprs[I]->isValueDependent())
1870 return true;
1871
1872 return false;
1873}
1874
John McCall8b0f4ff2010-08-02 21:13:48 +00001875bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00001876 // This function is attempting whether an expression is an initializer
1877 // which can be evaluated at compile-time. isEvaluatable handles most
1878 // of the cases, but it can't deal with some initializer-specific
1879 // expressions, and it can't deal with aggregates; we deal with those here,
1880 // and fall back to isEvaluatable for the other cases.
1881
John McCall8b0f4ff2010-08-02 21:13:48 +00001882 // If we ever capture reference-binding directly in the AST, we can
1883 // kill the second parameter.
1884
1885 if (IsForRef) {
1886 EvalResult Result;
1887 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
1888 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001889
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001890 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001891 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001892 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001893 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001894 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001895 return true;
John McCall81c9cea2010-08-01 21:51:45 +00001896 case CXXTemporaryObjectExprClass:
1897 case CXXConstructExprClass: {
1898 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00001899
1900 // Only if it's
1901 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00001902 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00001903 if (!CE->getNumArgs()) return true;
1904
1905 // 2) an elidable trivial copy construction of an operand which is
1906 // itself a constant initializer. Note that we consider the
1907 // operand on its own, *not* as a reference binding.
1908 return CE->isElidable() &&
1909 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00001910 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001911 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001912 // This handles gcc's extension that allows global initializers like
1913 // "struct x {int x;} x = (struct x) {};".
1914 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001915 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00001916 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001917 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001918 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001919 // FIXME: This doesn't deal with fields with reference types correctly.
1920 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1921 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001922 const InitListExpr *Exp = cast<InitListExpr>(this);
1923 unsigned numInits = Exp->getNumInits();
1924 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00001925 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001926 return false;
1927 }
Eli Friedman384da272009-01-25 03:12:18 +00001928 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001929 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001930 case ImplicitValueInitExprClass:
1931 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001932 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00001933 return cast<ParenExpr>(this)->getSubExpr()
1934 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00001935 case ChooseExprClass:
1936 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
1937 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00001938 case UnaryOperatorClass: {
1939 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001940 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00001941 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00001942 break;
1943 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001944 case BinaryOperatorClass: {
1945 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1946 // but this handles the common case.
1947 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00001948 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00001949 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1950 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1951 return true;
1952 break;
1953 }
John McCall8b0f4ff2010-08-02 21:13:48 +00001954 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00001955 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00001956 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001957 case CStyleCastExprClass:
1958 // Handle casts with a destination that's a struct or union; this
1959 // deals with both the gcc no-op struct cast extension and the
1960 // cast-to-union extension.
1961 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001962 return cast<CastExpr>(this)->getSubExpr()
1963 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001964
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001965 // Integer->integer casts can be handled here, which is important for
1966 // things like (int)(&&x-&&y). Scary but true.
1967 if (getType()->isIntegerType() &&
1968 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00001969 return cast<CastExpr>(this)->getSubExpr()
1970 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001971
Eli Friedman384da272009-01-25 03:12:18 +00001972 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001973 }
Eli Friedman384da272009-01-25 03:12:18 +00001974 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001975}
1976
Chris Lattner7eef9192007-05-24 01:23:49 +00001977/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1978/// integer constant expression with the value zero, or if this is one that is
1979/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001980bool Expr::isNullPointerConstant(ASTContext &Ctx,
1981 NullPointerConstantValueDependence NPC) const {
1982 if (isValueDependent()) {
1983 switch (NPC) {
1984 case NPC_NeverValueDependent:
1985 assert(false && "Unexpected value dependent expression!");
1986 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001987
Douglas Gregor56751b52009-09-25 04:25:58 +00001988 case NPC_ValueDependentIsNull:
Douglas Gregor6972a622010-06-16 00:35:25 +00001989 return isTypeDependent() || getType()->isIntegralType(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001990
Douglas Gregor56751b52009-09-25 04:25:58 +00001991 case NPC_ValueDependentIsNotNull:
1992 return false;
1993 }
1994 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001995
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001996 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001997 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001998 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001999 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002000 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002001 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002002 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002003 Pointee->isVoidType() && // to void*
2004 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002005 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002006 }
Steve Naroffada7d422007-05-20 17:54:12 +00002007 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002008 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2009 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002010 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002011 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2012 // Accept ((void*)0) as a null pointer constant, as many other
2013 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002014 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002015 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002016 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002017 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002018 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002019 } else if (isa<GNUNullExpr>(this)) {
2020 // The GNU __null extension is always a null pointer constant.
2021 return true;
Steve Naroff09035312008-01-14 02:53:34 +00002022 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002023
Sebastian Redl576fd422009-05-10 18:38:11 +00002024 // C++0x nullptr_t is always a null pointer constant.
2025 if (getType()->isNullPtrType())
2026 return true;
2027
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002028 if (const RecordType *UT = getType()->getAsUnionType())
2029 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2030 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2031 const Expr *InitExpr = CLE->getInitializer();
2032 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2033 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2034 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002035 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002036 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002037 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00002038 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002039
Chris Lattner1abbd412007-06-08 17:58:43 +00002040 // If we have an integer constant expression, we need to *evaluate* it and
2041 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002042 llvm::APSInt Result;
2043 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002044}
Steve Narofff7a5da12007-07-28 23:10:27 +00002045
Douglas Gregor71235ec2009-05-02 02:18:30 +00002046FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002047 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002048
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002049 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002050 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002051 ICE->getCastKind() == CK_NoOp)
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002052 E = ICE->getSubExpr()->IgnoreParens();
2053 else
2054 break;
2055 }
2056
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002057 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002058 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002059 if (Field->isBitField())
2060 return Field;
2061
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002062 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2063 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2064 if (Field->isBitField())
2065 return Field;
2066
Douglas Gregor71235ec2009-05-02 02:18:30 +00002067 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2068 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2069 return BinOp->getLHS()->getBitField();
2070
2071 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002072}
2073
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002074bool Expr::refersToVectorElement() const {
2075 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002076
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002077 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002078 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002079 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002080 E = ICE->getSubExpr()->IgnoreParens();
2081 else
2082 break;
2083 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002084
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002085 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2086 return ASE->getBase()->getType()->isVectorType();
2087
2088 if (isa<ExtVectorElementExpr>(E))
2089 return true;
2090
2091 return false;
2092}
2093
Chris Lattnerb8211f62009-02-16 22:14:05 +00002094/// isArrow - Return true if the base expression is a pointer to vector,
2095/// return false if the base expression is a vector.
2096bool ExtVectorElementExpr::isArrow() const {
2097 return getBase()->getType()->isPointerType();
2098}
2099
Nate Begemance4d7fc2008-04-18 23:10:10 +00002100unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002101 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002102 return VT->getNumElements();
2103 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002104}
2105
Nate Begemanf322eab2008-05-09 06:41:27 +00002106/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002107bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002108 // FIXME: Refactor this code to an accessor on the AST node which returns the
2109 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002110 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002111
2112 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002113 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002114 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002115
Nate Begeman7e5185b2009-01-18 02:01:21 +00002116 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002117 if (Comp[0] == 's' || Comp[0] == 'S')
2118 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002119
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002120 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2121 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002122 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002123
Steve Naroff0d595ca2007-07-30 03:29:09 +00002124 return false;
2125}
Chris Lattner885b4952007-08-02 23:36:59 +00002126
Nate Begemanf322eab2008-05-09 06:41:27 +00002127/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002128void ExtVectorElementExpr::getEncodedElementAccess(
2129 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002130 llvm::StringRef Comp = Accessor->getName();
2131 if (Comp[0] == 's' || Comp[0] == 'S')
2132 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002133
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002134 bool isHi = Comp == "hi";
2135 bool isLo = Comp == "lo";
2136 bool isEven = Comp == "even";
2137 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002138
Nate Begemanf322eab2008-05-09 06:41:27 +00002139 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2140 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002141
Nate Begemanf322eab2008-05-09 06:41:27 +00002142 if (isHi)
2143 Index = e + i;
2144 else if (isLo)
2145 Index = i;
2146 else if (isEven)
2147 Index = 2 * i;
2148 else if (isOdd)
2149 Index = 2 * i + 1;
2150 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002151 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002152
Nate Begemand3862152008-05-13 21:03:02 +00002153 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002154 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002155}
2156
Douglas Gregor9a129192010-04-21 00:45:42 +00002157ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002158 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002159 SourceLocation LBracLoc,
2160 SourceLocation SuperLoc,
2161 bool IsInstanceSuper,
2162 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002163 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002164 ObjCMethodDecl *Method,
2165 Expr **Args, unsigned NumArgs,
2166 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002167 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
2168 /*TypeDependent=*/false, /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002169 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2170 HasMethod(Method != 0), SuperLoc(SuperLoc),
2171 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2172 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002173 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002174{
Douglas Gregor9a129192010-04-21 00:45:42 +00002175 setReceiverPointer(SuperType.getAsOpaquePtr());
2176 if (NumArgs)
2177 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002178}
2179
Douglas Gregor9a129192010-04-21 00:45:42 +00002180ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002181 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002182 SourceLocation LBracLoc,
2183 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002184 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002185 ObjCMethodDecl *Method,
2186 Expr **Args, unsigned NumArgs,
2187 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002188 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002189 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00002190 hasAnyValueDependentArguments(Args, NumArgs))),
2191 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2192 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2193 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002194 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002195{
2196 setReceiverPointer(Receiver);
2197 if (NumArgs)
2198 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002199}
2200
Douglas Gregor9a129192010-04-21 00:45:42 +00002201ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002202 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002203 SourceLocation LBracLoc,
2204 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002205 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002206 ObjCMethodDecl *Method,
2207 Expr **Args, unsigned NumArgs,
2208 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002209 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002210 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00002211 hasAnyValueDependentArguments(Args, NumArgs))),
2212 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2213 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2214 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002215 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002216{
2217 setReceiverPointer(Receiver);
2218 if (NumArgs)
2219 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00002220}
2221
Douglas Gregor9a129192010-04-21 00:45:42 +00002222ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002223 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002224 SourceLocation LBracLoc,
2225 SourceLocation SuperLoc,
2226 bool IsInstanceSuper,
2227 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002228 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002229 ObjCMethodDecl *Method,
2230 Expr **Args, unsigned NumArgs,
2231 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002232 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002233 NumArgs * sizeof(Expr *);
2234 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002235 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002236 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002237 RBracLoc);
2238}
2239
2240ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002241 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002242 SourceLocation LBracLoc,
2243 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002244 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002245 ObjCMethodDecl *Method,
2246 Expr **Args, unsigned NumArgs,
2247 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002248 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002249 NumArgs * sizeof(Expr *);
2250 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002251 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002252 NumArgs, RBracLoc);
2253}
2254
2255ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002256 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002257 SourceLocation LBracLoc,
2258 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002259 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002260 ObjCMethodDecl *Method,
2261 Expr **Args, unsigned NumArgs,
2262 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002263 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002264 NumArgs * sizeof(Expr *);
2265 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002266 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002267 NumArgs, RBracLoc);
2268}
2269
Alexis Hunta8136cc2010-05-05 15:23:54 +00002270ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002271 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002272 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002273 NumArgs * sizeof(Expr *);
2274 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2275 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2276}
Alexis Hunta8136cc2010-05-05 15:23:54 +00002277
Douglas Gregor9a129192010-04-21 00:45:42 +00002278Selector ObjCMessageExpr::getSelector() const {
2279 if (HasMethod)
2280 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2281 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002282 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002283}
2284
2285ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2286 switch (getReceiverKind()) {
2287 case Instance:
2288 if (const ObjCObjectPointerType *Ptr
2289 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2290 return Ptr->getInterfaceDecl();
2291 break;
2292
2293 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002294 if (const ObjCObjectType *Ty
2295 = getClassReceiver()->getAs<ObjCObjectType>())
2296 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002297 break;
2298
2299 case SuperInstance:
2300 if (const ObjCObjectPointerType *Ptr
2301 = getSuperType()->getAs<ObjCObjectPointerType>())
2302 return Ptr->getInterfaceDecl();
2303 break;
2304
2305 case SuperClass:
2306 if (const ObjCObjectPointerType *Iface
2307 = getSuperType()->getAs<ObjCObjectPointerType>())
2308 return Iface->getInterfaceDecl();
2309 break;
2310 }
2311
2312 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002313}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002314
Chris Lattner35e564e2007-10-25 00:29:32 +00002315bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002316 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002317}
2318
Nate Begeman48745922009-08-12 02:28:50 +00002319void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2320 unsigned NumExprs) {
2321 if (SubExprs) C.Deallocate(SubExprs);
2322
2323 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002324 this->NumExprs = NumExprs;
2325 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002326}
Nate Begeman48745922009-08-12 02:28:50 +00002327
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002328//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002329// DesignatedInitExpr
2330//===----------------------------------------------------------------------===//
2331
2332IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2333 assert(Kind == FieldDesignator && "Only valid on a field designator");
2334 if (Field.NameOrField & 0x01)
2335 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2336 else
2337 return getField()->getIdentifier();
2338}
2339
Alexis Hunta8136cc2010-05-05 15:23:54 +00002340DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002341 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002342 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002343 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002344 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002345 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002346 unsigned NumIndexExprs,
2347 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002348 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002349 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002350 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002351 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2352 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002353 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002354
2355 // Record the initializer itself.
2356 child_iterator Child = child_begin();
2357 *Child++ = Init;
2358
2359 // Copy the designators and their subexpressions, computing
2360 // value-dependence along the way.
2361 unsigned IndexIdx = 0;
2362 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002363 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002364
2365 if (this->Designators[I].isArrayDesignator()) {
2366 // Compute type- and value-dependence.
2367 Expr *Index = IndexExprs[IndexIdx];
John McCall925b16622010-10-26 08:39:16 +00002368 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002369 Index->isTypeDependent() || Index->isValueDependent();
2370
2371 // Copy the index expressions into permanent storage.
2372 *Child++ = IndexExprs[IndexIdx++];
2373 } else if (this->Designators[I].isArrayRangeDesignator()) {
2374 // Compute type- and value-dependence.
2375 Expr *Start = IndexExprs[IndexIdx];
2376 Expr *End = IndexExprs[IndexIdx + 1];
John McCall925b16622010-10-26 08:39:16 +00002377 ExprBits.ValueDependent = ExprBits.ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002378 Start->isTypeDependent() || Start->isValueDependent() ||
2379 End->isTypeDependent() || End->isValueDependent();
2380
2381 // Copy the start/end expressions into permanent storage.
2382 *Child++ = IndexExprs[IndexIdx++];
2383 *Child++ = IndexExprs[IndexIdx++];
2384 }
2385 }
2386
2387 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002388}
2389
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002390DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002391DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002392 unsigned NumDesignators,
2393 Expr **IndexExprs, unsigned NumIndexExprs,
2394 SourceLocation ColonOrEqualLoc,
2395 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002396 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002397 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002398 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002399 ColonOrEqualLoc, UsesColonSyntax,
2400 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002401}
2402
Mike Stump11289f42009-09-09 15:08:12 +00002403DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002404 unsigned NumIndexExprs) {
2405 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2406 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2407 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2408}
2409
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002410void DesignatedInitExpr::setDesignators(ASTContext &C,
2411 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002412 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002413 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002414 NumDesignators = NumDesigs;
2415 for (unsigned I = 0; I != NumDesigs; ++I)
2416 Designators[I] = Desigs[I];
2417}
2418
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002419SourceRange DesignatedInitExpr::getSourceRange() const {
2420 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002421 Designator &First =
2422 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002423 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002424 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002425 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2426 else
2427 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2428 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002429 StartLoc =
2430 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002431 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2432}
2433
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002434Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2435 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2436 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2437 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002438 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2439 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2440}
2441
2442Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002443 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002444 "Requires array range designator");
2445 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2446 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002447 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2448 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2449}
2450
2451Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002452 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002453 "Requires array range designator");
2454 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2455 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002456 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2457 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2458}
2459
Douglas Gregord5846a12009-04-15 06:41:24 +00002460/// \brief Replaces the designator at index @p Idx with the series
2461/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002462void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002463 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002464 const Designator *Last) {
2465 unsigned NumNewDesignators = Last - First;
2466 if (NumNewDesignators == 0) {
2467 std::copy_backward(Designators + Idx + 1,
2468 Designators + NumDesignators,
2469 Designators + Idx);
2470 --NumNewDesignators;
2471 return;
2472 } else if (NumNewDesignators == 1) {
2473 Designators[Idx] = *First;
2474 return;
2475 }
2476
Mike Stump11289f42009-09-09 15:08:12 +00002477 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002478 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002479 std::copy(Designators, Designators + Idx, NewDesignators);
2480 std::copy(First, Last, NewDesignators + Idx);
2481 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2482 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002483 Designators = NewDesignators;
2484 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2485}
2486
Mike Stump11289f42009-09-09 15:08:12 +00002487ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002488 Expr **exprs, unsigned nexprs,
2489 SourceLocation rparenloc)
John McCall7decc9e2010-11-18 06:31:45 +00002490: Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002491 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002492 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002493 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002494
Nate Begeman5ec4b312009-08-10 23:49:36 +00002495 Exprs = new (C) Stmt*[nexprs];
2496 for (unsigned i = 0; i != nexprs; ++i)
2497 Exprs[i] = exprs[i];
2498}
2499
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002500//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002501// ExprIterator.
2502//===----------------------------------------------------------------------===//
2503
2504Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2505Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2506Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2507const Expr* ConstExprIterator::operator[](size_t idx) const {
2508 return cast<Expr>(I[idx]);
2509}
2510const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2511const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2512
2513//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002514// Child Iterators for iterating over subexpressions/substatements
2515//===----------------------------------------------------------------------===//
2516
2517// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002518Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2519Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002520
Steve Naroffe46504b2007-11-12 14:29:37 +00002521// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002522Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2523Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002524
Steve Naroffebf4cb42008-06-02 23:03:37 +00002525// ObjCPropertyRefExpr
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002526Stmt::child_iterator ObjCPropertyRefExpr::child_begin()
2527{
John McCallb7bd14f2010-12-02 01:19:52 +00002528 if (Receiver.is<Stmt*>()) {
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002529 // Hack alert!
John McCallb7bd14f2010-12-02 01:19:52 +00002530 return reinterpret_cast<Stmt**> (&Receiver);
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002531 }
2532 return child_iterator();
2533}
2534
2535Stmt::child_iterator ObjCPropertyRefExpr::child_end()
John McCallb7bd14f2010-12-02 01:19:52 +00002536{ return Receiver.is<Stmt*>() ?
2537 reinterpret_cast<Stmt**> (&Receiver)+1 :
Fariborz Jahanian681c0752010-10-14 16:04:05 +00002538 child_iterator();
2539}
Steve Naroffec944032008-05-30 00:40:33 +00002540
Steve Naroffe87026a2009-07-24 17:54:45 +00002541// ObjCIsaExpr
2542Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2543Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2544
Chris Lattner6307f192008-08-10 01:53:14 +00002545// PredefinedExpr
2546Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2547Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002548
2549// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002550Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2551Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002552
2553// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002554Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002555Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002556
2557// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002558Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2559Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002560
Chris Lattner1c20a172007-08-26 03:42:43 +00002561// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002562Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2563Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002564
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002565// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002566Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2567Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002568
2569// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002570Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2571Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002572
2573// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002574Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2575Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002576
Douglas Gregor882211c2010-04-28 22:16:22 +00002577// OffsetOfExpr
2578Stmt::child_iterator OffsetOfExpr::child_begin() {
2579 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2580 + NumComps);
2581}
2582Stmt::child_iterator OffsetOfExpr::child_end() {
2583 return child_iterator(&*child_begin() + NumExprs);
2584}
2585
Sebastian Redl6f282892008-11-11 17:56:53 +00002586// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002587Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002588 // If this is of a type and the type is a VLA type (and not a typedef), the
2589 // size expression of the VLA needs to be treated as an executable expression.
2590 // Why isn't this weirdness documented better in StmtIterator?
2591 if (isArgumentType()) {
2592 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2593 getArgumentType().getTypePtr()))
2594 return child_iterator(T);
2595 return child_iterator();
2596 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002597 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002598}
Sebastian Redl6f282892008-11-11 17:56:53 +00002599Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2600 if (isArgumentType())
2601 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002602 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002603}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002604
2605// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002606Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002607 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002608}
Ted Kremenek23702b62007-08-24 20:06:47 +00002609Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002610 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002611}
2612
2613// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002614Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002615 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002616}
Ted Kremenek23702b62007-08-24 20:06:47 +00002617Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002618 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002619}
Ted Kremenek23702b62007-08-24 20:06:47 +00002620
2621// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002622Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2623Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002624
Nate Begemance4d7fc2008-04-18 23:10:10 +00002625// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002626Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2627Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002628
2629// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002630Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2631Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002632
Ted Kremenek23702b62007-08-24 20:06:47 +00002633// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002634Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2635Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002636
2637// BinaryOperator
2638Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002639 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002640}
Ted Kremenek23702b62007-08-24 20:06:47 +00002641Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002642 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002643}
2644
2645// ConditionalOperator
2646Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002647 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002648}
Ted Kremenek23702b62007-08-24 20:06:47 +00002649Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002650 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002651}
2652
2653// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002654Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2655Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002656
Ted Kremenek23702b62007-08-24 20:06:47 +00002657// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002658Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2659Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002660
2661// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002662Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2663 return child_iterator();
2664}
2665
2666Stmt::child_iterator TypesCompatibleExpr::child_end() {
2667 return child_iterator();
2668}
Ted Kremenek23702b62007-08-24 20:06:47 +00002669
2670// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002671Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2672Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002673
Douglas Gregor3be4b122008-11-29 04:51:27 +00002674// GNUNullExpr
2675Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2676Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2677
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002678// ShuffleVectorExpr
2679Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002680 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002681}
2682Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002683 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002684}
2685
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002686// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002687Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2688Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002689
Anders Carlsson4692db02007-08-31 04:56:16 +00002690// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002691Stmt::child_iterator InitListExpr::child_begin() {
2692 return InitExprs.size() ? &InitExprs[0] : 0;
2693}
2694Stmt::child_iterator InitListExpr::child_end() {
2695 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2696}
Anders Carlsson4692db02007-08-31 04:56:16 +00002697
Douglas Gregor0202cb42009-01-29 17:44:32 +00002698// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002699Stmt::child_iterator DesignatedInitExpr::child_begin() {
2700 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2701 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002702 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2703}
2704Stmt::child_iterator DesignatedInitExpr::child_end() {
2705 return child_iterator(&*child_begin() + NumSubExprs);
2706}
2707
Douglas Gregor0202cb42009-01-29 17:44:32 +00002708// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002709Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2710 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002711}
2712
Mike Stump11289f42009-09-09 15:08:12 +00002713Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2714 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002715}
2716
Nate Begeman5ec4b312009-08-10 23:49:36 +00002717// ParenListExpr
2718Stmt::child_iterator ParenListExpr::child_begin() {
2719 return &Exprs[0];
2720}
2721Stmt::child_iterator ParenListExpr::child_end() {
2722 return &Exprs[0]+NumExprs;
2723}
2724
Ted Kremenek23702b62007-08-24 20:06:47 +00002725// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002726Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002727 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002728}
2729Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002730 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002731}
Ted Kremenek23702b62007-08-24 20:06:47 +00002732
2733// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002734Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2735Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002736
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002737// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002738Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002739 return child_iterator();
2740}
2741Stmt::child_iterator ObjCSelectorExpr::child_end() {
2742 return child_iterator();
2743}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002744
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002745// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002746Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2747 return child_iterator();
2748}
2749Stmt::child_iterator ObjCProtocolExpr::child_end() {
2750 return child_iterator();
2751}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002752
Steve Naroffd54978b2007-09-18 23:55:05 +00002753// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002754Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002755 if (getReceiverKind() == Instance)
2756 return reinterpret_cast<Stmt **>(this + 1);
2757 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002758}
2759Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002760 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002761}
2762
Steve Naroffc540d662008-09-03 18:15:37 +00002763// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002764Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2765Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002766
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002767Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2768Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }
John McCall8d69a212010-11-15 23:31:06 +00002769
2770// OpaqueValueExpr
2771SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); }
2772Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); }
2773Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); }
2774