blob: 5d0269f702fea3998cbd113ea424be475cc60387 [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 Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattner4ebae652010-04-16 23:34:13 +000030/// isKnownToHaveBooleanValue - Return true if this is an integer expression
31/// that is known to return 0 or 1. This happens for _Bool/bool expressions
32/// but also int expressions which are produced by things like comparisons in
33/// C.
34bool Expr::isKnownToHaveBooleanValue() const {
35 // If this value has _Bool type, it is obvious 0/1.
36 if (getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000037 // If this is a non-scalar-integer type, we don't care enough to try.
Chris Lattner4ebae652010-04-16 23:34:13 +000038 if (!getType()->isIntegralType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000039
Chris Lattner4ebae652010-04-16 23:34:13 +000040 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
41 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000042
Chris Lattner4ebae652010-04-16 23:34:13 +000043 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
44 switch (UO->getOpcode()) {
45 case UnaryOperator::Plus:
46 case UnaryOperator::Extension:
47 return UO->getSubExpr()->isKnownToHaveBooleanValue();
48 default:
49 return false;
50 }
51 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000052
Chris Lattner4ebae652010-04-16 23:34:13 +000053 if (const CastExpr *CE = dyn_cast<CastExpr>(this))
54 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000055
Chris Lattner4ebae652010-04-16 23:34:13 +000056 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
57 switch (BO->getOpcode()) {
58 default: return false;
59 case BinaryOperator::LT: // Relational operators.
60 case BinaryOperator::GT:
61 case BinaryOperator::LE:
62 case BinaryOperator::GE:
63 case BinaryOperator::EQ: // Equality operators.
64 case BinaryOperator::NE:
65 case BinaryOperator::LAnd: // AND operator.
66 case BinaryOperator::LOr: // Logical OR operator.
67 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000068
Chris Lattner4ebae652010-04-16 23:34:13 +000069 case BinaryOperator::And: // Bitwise AND operator.
70 case BinaryOperator::Xor: // Bitwise XOR operator.
71 case BinaryOperator::Or: // Bitwise OR operator.
72 // Handle things like (x==2)|(y==12).
73 return BO->getLHS()->isKnownToHaveBooleanValue() &&
74 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000075
Chris Lattner4ebae652010-04-16 23:34:13 +000076 case BinaryOperator::Comma:
77 case BinaryOperator::Assign:
78 return BO->getRHS()->isKnownToHaveBooleanValue();
79 }
80 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000081
Chris Lattner4ebae652010-04-16 23:34:13 +000082 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
83 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
84 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Chris Lattner4ebae652010-04-16 23:34:13 +000086 return false;
87}
88
Chris Lattner0eedafe2006-08-24 04:56:27 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCall6b51f282009-11-23 01:53:49 +000093void ExplicitTemplateArgumentList::initializeFrom(
94 const TemplateArgumentListInfo &Info) {
95 LAngleLoc = Info.getLAngleLoc();
96 RAngleLoc = Info.getRAngleLoc();
97 NumTemplateArgs = Info.size();
98
99 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
100 for (unsigned i = 0; i != NumTemplateArgs; ++i)
101 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
102}
103
104void ExplicitTemplateArgumentList::copyInto(
105 TemplateArgumentListInfo &Info) const {
106 Info.setLAngleLoc(LAngleLoc);
107 Info.setRAngleLoc(RAngleLoc);
108 for (unsigned I = 0; I != NumTemplateArgs; ++I)
109 Info.addArgument(getTemplateArgs()[I]);
110}
111
112std::size_t ExplicitTemplateArgumentList::sizeFor(
113 const TemplateArgumentListInfo &Info) {
114 return sizeof(ExplicitTemplateArgumentList) +
115 sizeof(TemplateArgumentLoc) * Info.size();
116}
117
Douglas Gregored6c7442009-11-23 11:41:28 +0000118void DeclRefExpr::computeDependence() {
119 TypeDependent = false;
120 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000121
Douglas Gregored6c7442009-11-23 11:41:28 +0000122 NamedDecl *D = getDecl();
123
124 // (TD) C++ [temp.dep.expr]p3:
125 // An id-expression is type-dependent if it contains:
126 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000127 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000128 //
129 // (VD) C++ [temp.dep.constexpr]p2:
130 // An identifier is value-dependent if it is:
131
132 // (TD) - an identifier that was declared with dependent type
133 // (VD) - a name declared with a dependent type,
134 if (getType()->isDependentType()) {
135 TypeDependent = true;
136 ValueDependent = true;
137 }
138 // (TD) - a conversion-function-id that specifies a dependent type
Alexis Hunta8136cc2010-05-05 15:23:54 +0000139 else if (D->getDeclName().getNameKind()
Douglas Gregored6c7442009-11-23 11:41:28 +0000140 == DeclarationName::CXXConversionFunctionName &&
141 D->getDeclName().getCXXNameType()->isDependentType()) {
142 TypeDependent = true;
143 ValueDependent = true;
144 }
145 // (TD) - a template-id that is dependent,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000146 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000147 TemplateSpecializationType::anyDependentTemplateArguments(
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148 getTemplateArgs(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000149 getNumTemplateArgs())) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (VD) - the name of a non-type template parameter,
154 else if (isa<NonTypeTemplateParmDecl>(D))
155 ValueDependent = true;
156 // (VD) - a constant with integral or enumeration type and is
157 // initialized with an expression that is value-dependent.
158 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor0e4de762010-05-11 08:41:30 +0000159 if (Var->getType()->isIntegralType() && !Var->isStaticDataMember() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000164 }
165 // (VD) - FIXME: Missing from the standard:
166 // - a member function or a static data member of the current
167 // instantiation
168 else if (Var->isStaticDataMember() &&
169 Var->getDeclContext()->isDependentContext())
170 ValueDependent = true;
171 }
172 // (VD) - FIXME: Missing from the standard:
173 // - a member function or a static data member of the current
174 // instantiation
175 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
176 ValueDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000177 // (TD) - a nested-name-specifier or a qualified-id that names a
178 // member of an unknown specialization.
179 // (handled by DependentScopeDeclRefExpr)
180}
181
Alexis Hunta8136cc2010-05-05 15:23:54 +0000182DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000183 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000184 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000185 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000186 QualType T)
187 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000188 DecoratedD(D,
189 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000190 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000191 Loc(NameLoc) {
192 if (Qualifier) {
193 NameQualifier *NQ = getNameQualifier();
194 NQ->NNS = Qualifier;
195 NQ->Range = QualifierRange;
196 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000197
John McCall6b51f282009-11-23 01:53:49 +0000198 if (TemplateArgs)
199 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000200
201 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000202}
203
204DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
205 NestedNameSpecifier *Qualifier,
206 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000207 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000208 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000209 QualType T,
210 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000211 std::size_t Size = sizeof(DeclRefExpr);
212 if (Qualifier != 0)
213 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000214
John McCall6b51f282009-11-23 01:53:49 +0000215 if (TemplateArgs)
216 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000217
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000218 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
219 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000220 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000221}
222
223SourceRange DeclRefExpr::getSourceRange() const {
224 // FIXME: Does not handle multi-token names well, e.g., operator[].
225 SourceRange R(Loc);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000226
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000227 if (hasQualifier())
228 R.setBegin(getQualifierRange().getBegin());
229 if (hasExplicitTemplateArgumentList())
230 R.setEnd(getRAngleLoc());
231 return R;
232}
233
Anders Carlsson2fb08242009-09-08 18:24:21 +0000234// FIXME: Maybe this should use DeclPrinter with a special "print predefined
235// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000236std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
237 ASTContext &Context = CurrentDecl->getASTContext();
238
Anders Carlsson2fb08242009-09-08 18:24:21 +0000239 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000240 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000241 return FD->getNameAsString();
242
243 llvm::SmallString<256> Name;
244 llvm::raw_svector_ostream Out(Name);
245
246 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000247 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000248 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000249 if (MD->isStatic())
250 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000251 }
252
253 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000254
255 std::string Proto = FD->getQualifiedNameAsString(Policy);
256
John McCall9dd450b2009-09-21 23:43:11 +0000257 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000258 const FunctionProtoType *FT = 0;
259 if (FD->hasWrittenPrototype())
260 FT = dyn_cast<FunctionProtoType>(AFT);
261
262 Proto += "(";
263 if (FT) {
264 llvm::raw_string_ostream POut(Proto);
265 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
266 if (i) POut << ", ";
267 std::string Param;
268 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
269 POut << Param;
270 }
271
272 if (FT->isVariadic()) {
273 if (FD->getNumParams()) POut << ", ";
274 POut << "...";
275 }
276 }
277 Proto += ")";
278
Sam Weinig4e83bd22009-12-27 01:38:20 +0000279 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
280 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
281 if (ThisQuals.hasConst())
282 Proto += " const";
283 if (ThisQuals.hasVolatile())
284 Proto += " volatile";
285 }
286
Sam Weinigd060ed42009-12-06 23:55:13 +0000287 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
288 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000289
290 Out << Proto;
291
292 Out.flush();
293 return Name.str().str();
294 }
295 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
296 llvm::SmallString<256> Name;
297 llvm::raw_svector_ostream Out(Name);
298 Out << (MD->isInstanceMethod() ? '-' : '+');
299 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000300
301 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
302 // a null check to avoid a crash.
303 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000304 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000305
Anders Carlsson2fb08242009-09-08 18:24:21 +0000306 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000307 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
308 Out << '(' << CID << ')';
309
Anders Carlsson2fb08242009-09-08 18:24:21 +0000310 Out << ' ';
311 Out << MD->getSelector().getAsString();
312 Out << ']';
313
314 Out.flush();
315 return Name.str().str();
316 }
317 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
318 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
319 return "top level";
320 }
321 return "";
322}
323
Chris Lattnera0173132008-06-07 22:13:43 +0000324/// getValueAsApproximateDouble - This returns the value as an inaccurate
325/// double. Note that this may cause loss of precision, but is useful for
326/// debugging dumps, etc.
327double FloatingLiteral::getValueAsApproximateDouble() const {
328 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000329 bool ignored;
330 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
331 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000332 return V.convertToDouble();
333}
334
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000335StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
336 unsigned ByteLength, bool Wide,
337 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000338 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000339 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000340 // Allocate enough space for the StringLiteral plus an array of locations for
341 // any concatenated string tokens.
342 void *Mem = C.Allocate(sizeof(StringLiteral)+
343 sizeof(SourceLocation)*(NumStrs-1),
344 llvm::alignof<StringLiteral>());
345 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000346
Steve Naroffdf7855b2007-02-21 23:46:25 +0000347 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000348 char *AStrData = new (C, 1) char[ByteLength];
349 memcpy(AStrData, StrData, ByteLength);
350 SL->StrData = AStrData;
351 SL->ByteLength = ByteLength;
352 SL->IsWide = Wide;
353 SL->TokLocs[0] = Loc[0];
354 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000355
Chris Lattner630970d2009-02-18 05:49:11 +0000356 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000357 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
358 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000359}
360
Douglas Gregor958dfc92009-04-15 16:35:07 +0000361StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
362 void *Mem = C.Allocate(sizeof(StringLiteral)+
363 sizeof(SourceLocation)*(NumStrs-1),
364 llvm::alignof<StringLiteral>());
365 StringLiteral *SL = new (Mem) StringLiteral(QualType());
366 SL->StrData = 0;
367 SL->ByteLength = 0;
368 SL->NumConcatenated = NumStrs;
369 return SL;
370}
371
Douglas Gregore26a2852009-08-07 06:08:38 +0000372void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000373 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000374 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000375}
376
Daniel Dunbar36217882009-09-22 03:27:33 +0000377void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000378 if (StrData)
379 C.Deallocate(const_cast<char*>(StrData));
380
Daniel Dunbar36217882009-09-22 03:27:33 +0000381 char *AStrData = new (C, 1) char[Str.size()];
382 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000383 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000384 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000385}
386
Chris Lattner1b926492006-08-23 06:42:10 +0000387/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
388/// corresponds to, e.g. "sizeof" or "[pre]++".
389const char *UnaryOperator::getOpcodeStr(Opcode Op) {
390 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000391 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000392 case PostInc: return "++";
393 case PostDec: return "--";
394 case PreInc: return "++";
395 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000396 case AddrOf: return "&";
397 case Deref: return "*";
398 case Plus: return "+";
399 case Minus: return "-";
400 case Not: return "~";
401 case LNot: return "!";
402 case Real: return "__real";
403 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000404 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000405 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000406 }
407}
408
Mike Stump11289f42009-09-09 15:08:12 +0000409UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000410UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
411 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000412 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000413 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
414 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
415 case OO_Amp: return AddrOf;
416 case OO_Star: return Deref;
417 case OO_Plus: return Plus;
418 case OO_Minus: return Minus;
419 case OO_Tilde: return Not;
420 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000421 }
422}
423
424OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
425 switch (Opc) {
426 case PostInc: case PreInc: return OO_PlusPlus;
427 case PostDec: case PreDec: return OO_MinusMinus;
428 case AddrOf: return OO_Amp;
429 case Deref: return OO_Star;
430 case Plus: return OO_Plus;
431 case Minus: return OO_Minus;
432 case Not: return OO_Tilde;
433 case LNot: return OO_Exclaim;
434 default: return OO_None;
435 }
436}
437
438
Chris Lattner0eedafe2006-08-24 04:56:27 +0000439//===----------------------------------------------------------------------===//
440// Postfix Operators.
441//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000442
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000443CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000444 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000445 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000446 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000447 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000448 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000449
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000450 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000451 SubExprs[FN] = fn;
452 for (unsigned i = 0; i != numargs; ++i)
453 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000454
Douglas Gregor993603d2008-11-14 16:09:21 +0000455 RParenLoc = rparenloc;
456}
Nate Begeman1e36a852008-01-17 17:46:27 +0000457
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000458CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
459 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000460 : Expr(CallExprClass, t,
461 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000462 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000463 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000464
465 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000466 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000467 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000468 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000469
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000470 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000471}
472
Mike Stump11289f42009-09-09 15:08:12 +0000473CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
474 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000475 SubExprs = new (C) Stmt*[1];
476}
477
Douglas Gregore26a2852009-08-07 06:08:38 +0000478void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000479 DestroyChildren(C);
480 if (SubExprs) C.Deallocate(SubExprs);
481 this->~CallExpr();
482 C.Deallocate(this);
483}
484
Nuno Lopes518e3702009-12-20 23:11:08 +0000485Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000486 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000487 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000488 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000489 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
490 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000491
492 return 0;
493}
494
Nuno Lopes518e3702009-12-20 23:11:08 +0000495FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000496 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000497}
498
Chris Lattnere4407ed2007-12-28 05:25:02 +0000499/// setNumArgs - This changes the number of arguments present in this call.
500/// Any orphaned expressions are deleted by this, and any new operands are set
501/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000502void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000503 // No change, just return.
504 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattnere4407ed2007-12-28 05:25:02 +0000506 // If shrinking # arguments, just delete the extras and forgot them.
507 if (NumArgs < getNumArgs()) {
508 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000509 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000510 this->NumArgs = NumArgs;
511 return;
512 }
513
514 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000515 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000516 // Copy over args.
517 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
518 NewSubExprs[i] = SubExprs[i];
519 // Null out new args.
520 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
521 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000522
Douglas Gregorba6e5572009-04-17 21:46:47 +0000523 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000524 SubExprs = NewSubExprs;
525 this->NumArgs = NumArgs;
526}
527
Chris Lattner01ff98a2008-10-06 05:00:53 +0000528/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
529/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000530unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000531 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000532 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000533 // ImplicitCastExpr.
534 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
535 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000536 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Steve Narofff6e3b3292008-01-31 01:07:12 +0000538 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
539 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000540 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000541
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000542 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
543 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000544 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000546 if (!FDecl->getIdentifier())
547 return 0;
548
Douglas Gregor15fc9562009-09-12 00:22:50 +0000549 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000550}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000551
Anders Carlsson00a27592009-05-26 04:57:27 +0000552QualType CallExpr::getCallReturnType() const {
553 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000554 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000555 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000556 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000557 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000558
John McCall9dd450b2009-09-21 23:43:11 +0000559 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000560 return FnType->getResultType();
561}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000562
Alexis Hunta8136cc2010-05-05 15:23:54 +0000563OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000564 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000565 TypeSourceInfo *tsi,
566 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000567 Expr** exprsPtr, unsigned numExprs,
568 SourceLocation RParenLoc) {
569 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000570 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000571 sizeof(Expr*) * numExprs);
572
573 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
574 exprsPtr, numExprs, RParenLoc);
575}
576
577OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
578 unsigned numComps, unsigned numExprs) {
579 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
580 sizeof(OffsetOfNode) * numComps +
581 sizeof(Expr*) * numExprs);
582 return new (Mem) OffsetOfExpr(numComps, numExprs);
583}
584
Alexis Hunta8136cc2010-05-05 15:23:54 +0000585OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000586 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000587 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000588 Expr** exprsPtr, unsigned numExprs,
589 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000590 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000591 /*ValueDependent=*/tsi->getType()->isDependentType() ||
592 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
593 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000594 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
595 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000596{
597 for(unsigned i = 0; i < numComps; ++i) {
598 setComponent(i, compsPtr[i]);
599 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000600
Douglas Gregor882211c2010-04-28 22:16:22 +0000601 for(unsigned i = 0; i < numExprs; ++i) {
602 setIndexExpr(i, exprsPtr[i]);
603 }
604}
605
606IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
607 assert(getKind() == Field || getKind() == Identifier);
608 if (getKind() == Field)
609 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000610
Douglas Gregor882211c2010-04-28 22:16:22 +0000611 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
612}
613
Mike Stump11289f42009-09-09 15:08:12 +0000614MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
615 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000616 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000617 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000618 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000619 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000620 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000621 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000622 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000623
John McCalla8ae2222010-04-06 21:38:20 +0000624 bool hasQualOrFound = (qual != 0 ||
625 founddecl.getDecl() != memberdecl ||
626 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000627 if (hasQualOrFound)
628 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000629
John McCall6b51f282009-11-23 01:53:49 +0000630 if (targs)
631 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000632
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000633 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000634 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
635
636 if (hasQualOrFound) {
637 if (qual && qual->isDependent()) {
638 E->setValueDependent(true);
639 E->setTypeDependent(true);
640 }
641 E->HasQualifierOrFoundDecl = true;
642
643 MemberNameQualifier *NQ = E->getMemberQualifier();
644 NQ->NNS = qual;
645 NQ->Range = qualrange;
646 NQ->FoundDecl = founddecl;
647 }
648
649 if (targs) {
650 E->HasExplicitTemplateArgumentList = true;
651 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
652 }
653
654 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000655}
656
Anders Carlsson496335e2009-09-03 00:59:21 +0000657const char *CastExpr::getCastKindName() const {
658 switch (getCastKind()) {
659 case CastExpr::CK_Unknown:
660 return "Unknown";
661 case CastExpr::CK_BitCast:
662 return "BitCast";
663 case CastExpr::CK_NoOp:
664 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000665 case CastExpr::CK_BaseToDerived:
666 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000667 case CastExpr::CK_DerivedToBase:
668 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000669 case CastExpr::CK_UncheckedDerivedToBase:
670 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000671 case CastExpr::CK_Dynamic:
672 return "Dynamic";
673 case CastExpr::CK_ToUnion:
674 return "ToUnion";
675 case CastExpr::CK_ArrayToPointerDecay:
676 return "ArrayToPointerDecay";
677 case CastExpr::CK_FunctionToPointerDecay:
678 return "FunctionToPointerDecay";
679 case CastExpr::CK_NullToMemberPointer:
680 return "NullToMemberPointer";
681 case CastExpr::CK_BaseToDerivedMemberPointer:
682 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000683 case CastExpr::CK_DerivedToBaseMemberPointer:
684 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000685 case CastExpr::CK_UserDefinedConversion:
686 return "UserDefinedConversion";
687 case CastExpr::CK_ConstructorConversion:
688 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000689 case CastExpr::CK_IntegralToPointer:
690 return "IntegralToPointer";
691 case CastExpr::CK_PointerToIntegral:
692 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000693 case CastExpr::CK_ToVoid:
694 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000695 case CastExpr::CK_VectorSplat:
696 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000697 case CastExpr::CK_IntegralCast:
698 return "IntegralCast";
699 case CastExpr::CK_IntegralToFloating:
700 return "IntegralToFloating";
701 case CastExpr::CK_FloatingToIntegral:
702 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000703 case CastExpr::CK_FloatingCast:
704 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000705 case CastExpr::CK_MemberPointerToBoolean:
706 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000707 case CastExpr::CK_AnyPointerToObjCPointerCast:
708 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000709 case CastExpr::CK_AnyPointerToBlockPointerCast:
710 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Anders Carlsson496335e2009-09-03 00:59:21 +0000713 assert(0 && "Unhandled cast kind!");
714 return 0;
715}
716
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000717void CastExpr::DoDestroy(ASTContext &C)
718{
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000719 BasePath.Destroy();
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000720 Expr::DoDestroy(C);
721}
722
Douglas Gregord196a582009-12-14 19:27:10 +0000723Expr *CastExpr::getSubExprAsWritten() {
724 Expr *SubExpr = 0;
725 CastExpr *E = this;
726 do {
727 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000728
Douglas Gregord196a582009-12-14 19:27:10 +0000729 // Skip any temporary bindings; they're implicit.
730 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
731 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000732
Douglas Gregord196a582009-12-14 19:27:10 +0000733 // Conversions by constructor and conversion functions have a
734 // subexpression describing the call; strip it off.
735 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
736 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
737 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
738 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000739
Douglas Gregord196a582009-12-14 19:27:10 +0000740 // If the subexpression we're left with is an implicit cast, look
741 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000742 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
743
Douglas Gregord196a582009-12-14 19:27:10 +0000744 return SubExpr;
745}
746
Chris Lattner1b926492006-08-23 06:42:10 +0000747/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
748/// corresponds to, e.g. "<<=".
749const char *BinaryOperator::getOpcodeStr(Opcode Op) {
750 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000751 case PtrMemD: return ".*";
752 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000753 case Mul: return "*";
754 case Div: return "/";
755 case Rem: return "%";
756 case Add: return "+";
757 case Sub: return "-";
758 case Shl: return "<<";
759 case Shr: return ">>";
760 case LT: return "<";
761 case GT: return ">";
762 case LE: return "<=";
763 case GE: return ">=";
764 case EQ: return "==";
765 case NE: return "!=";
766 case And: return "&";
767 case Xor: return "^";
768 case Or: return "|";
769 case LAnd: return "&&";
770 case LOr: return "||";
771 case Assign: return "=";
772 case MulAssign: return "*=";
773 case DivAssign: return "/=";
774 case RemAssign: return "%=";
775 case AddAssign: return "+=";
776 case SubAssign: return "-=";
777 case ShlAssign: return "<<=";
778 case ShrAssign: return ">>=";
779 case AndAssign: return "&=";
780 case XorAssign: return "^=";
781 case OrAssign: return "|=";
782 case Comma: return ",";
783 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000784
785 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000786}
Steve Naroff47500512007-04-19 23:00:49 +0000787
Mike Stump11289f42009-09-09 15:08:12 +0000788BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000789BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
790 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000791 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000792 case OO_Plus: return Add;
793 case OO_Minus: return Sub;
794 case OO_Star: return Mul;
795 case OO_Slash: return Div;
796 case OO_Percent: return Rem;
797 case OO_Caret: return Xor;
798 case OO_Amp: return And;
799 case OO_Pipe: return Or;
800 case OO_Equal: return Assign;
801 case OO_Less: return LT;
802 case OO_Greater: return GT;
803 case OO_PlusEqual: return AddAssign;
804 case OO_MinusEqual: return SubAssign;
805 case OO_StarEqual: return MulAssign;
806 case OO_SlashEqual: return DivAssign;
807 case OO_PercentEqual: return RemAssign;
808 case OO_CaretEqual: return XorAssign;
809 case OO_AmpEqual: return AndAssign;
810 case OO_PipeEqual: return OrAssign;
811 case OO_LessLess: return Shl;
812 case OO_GreaterGreater: return Shr;
813 case OO_LessLessEqual: return ShlAssign;
814 case OO_GreaterGreaterEqual: return ShrAssign;
815 case OO_EqualEqual: return EQ;
816 case OO_ExclaimEqual: return NE;
817 case OO_LessEqual: return LE;
818 case OO_GreaterEqual: return GE;
819 case OO_AmpAmp: return LAnd;
820 case OO_PipePipe: return LOr;
821 case OO_Comma: return Comma;
822 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000823 }
824}
825
826OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
827 static const OverloadedOperatorKind OverOps[] = {
828 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
829 OO_Star, OO_Slash, OO_Percent,
830 OO_Plus, OO_Minus,
831 OO_LessLess, OO_GreaterGreater,
832 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
833 OO_EqualEqual, OO_ExclaimEqual,
834 OO_Amp,
835 OO_Caret,
836 OO_Pipe,
837 OO_AmpAmp,
838 OO_PipePipe,
839 OO_Equal, OO_StarEqual,
840 OO_SlashEqual, OO_PercentEqual,
841 OO_PlusEqual, OO_MinusEqual,
842 OO_LessLessEqual, OO_GreaterGreaterEqual,
843 OO_AmpEqual, OO_CaretEqual,
844 OO_PipeEqual,
845 OO_Comma
846 };
847 return OverOps[Opc];
848}
849
Ted Kremenekac034612010-04-13 23:39:13 +0000850InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000851 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000852 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000853 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000854 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000855 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000856 UnionFieldInit(0), HadArrayRangeDesignator(false)
857{
Ted Kremenek013041e2010-02-19 01:50:18 +0000858 for (unsigned I = 0; I != numInits; ++I) {
859 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000860 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000861 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000862 ValueDependent = true;
863 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000864
Ted Kremenekac034612010-04-13 23:39:13 +0000865 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000866}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000867
Ted Kremenekac034612010-04-13 23:39:13 +0000868void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000869 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000870 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000871}
872
Ted Kremenekac034612010-04-13 23:39:13 +0000873void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000874 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
875 Idx < LastIdx; ++Idx)
Ted Kremenekac034612010-04-13 23:39:13 +0000876 InitExprs[Idx]->Destroy(C);
877 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000878}
879
Ted Kremenekac034612010-04-13 23:39:13 +0000880Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000881 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000882 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000883 InitExprs.back() = expr;
884 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000885 }
Mike Stump11289f42009-09-09 15:08:12 +0000886
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000887 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
888 InitExprs[Init] = expr;
889 return Result;
890}
891
Steve Naroff991e99d2008-09-04 15:31:07 +0000892/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000893///
894const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000895 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000896 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000897}
898
Mike Stump11289f42009-09-09 15:08:12 +0000899SourceLocation BlockExpr::getCaretLocation() const {
900 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000901}
Mike Stump11289f42009-09-09 15:08:12 +0000902const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000903 return TheBlock->getBody();
904}
Mike Stump11289f42009-09-09 15:08:12 +0000905Stmt *BlockExpr::getBody() {
906 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000907}
Steve Naroff415d3d52008-10-08 17:01:13 +0000908
909
Chris Lattner1ec5f562007-06-27 05:38:08 +0000910//===----------------------------------------------------------------------===//
911// Generic Expression Routines
912//===----------------------------------------------------------------------===//
913
Chris Lattner237f2752009-02-14 07:37:35 +0000914/// isUnusedResultAWarning - Return true if this immediate expression should
915/// be warned about if the result is unused. If so, fill in Loc and Ranges
916/// with location to warn on and the source range[s] to report with the
917/// warning.
918bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000919 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000920 // Don't warn if the expr is type dependent. The type could end up
921 // instantiating to void.
922 if (isTypeDependent())
923 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattner1ec5f562007-06-27 05:38:08 +0000925 switch (getStmtClass()) {
926 default:
John McCallc493a732010-03-12 07:11:26 +0000927 if (getType()->isVoidType())
928 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000929 Loc = getExprLoc();
930 R1 = getSourceRange();
931 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000932 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000933 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000934 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000935 case UnaryOperatorClass: {
936 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattner1ec5f562007-06-27 05:38:08 +0000938 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000939 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000940 case UnaryOperator::PostInc:
941 case UnaryOperator::PostDec:
942 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000943 case UnaryOperator::PreDec: // ++/--
944 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000945 case UnaryOperator::Deref:
946 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000947 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000948 return false;
949 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000950 case UnaryOperator::Real:
951 case UnaryOperator::Imag:
952 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000953 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
954 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000955 return false;
956 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000957 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000958 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000959 }
Chris Lattner237f2752009-02-14 07:37:35 +0000960 Loc = UO->getOperatorLoc();
961 R1 = UO->getSubExpr()->getSourceRange();
962 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000963 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000964 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000965 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000966 switch (BO->getOpcode()) {
967 default:
968 break;
969 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
970 case BinaryOperator::Comma:
971 // ((foo = <blah>), 0) is an idiom for hiding the result (and
972 // lvalue-ness) of an assignment written in a macro.
973 if (IntegerLiteral *IE =
974 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
975 if (IE->getValue() == 0)
976 return false;
977 case BinaryOperator::LAnd:
978 case BinaryOperator::LOr:
979 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
980 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000981 }
Chris Lattner237f2752009-02-14 07:37:35 +0000982 if (BO->isAssignmentOp())
983 return false;
984 Loc = BO->getOperatorLoc();
985 R1 = BO->getLHS()->getSourceRange();
986 R2 = BO->getRHS()->getSourceRange();
987 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000988 }
Chris Lattner86928112007-08-25 02:00:02 +0000989 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +0000990 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000991 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000992
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000993 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000994 // The condition must be evaluated, but if either the LHS or RHS is a
995 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000996 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000997 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000998 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000999 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001000 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001001 }
1002
Chris Lattnera44d1162007-06-27 05:58:59 +00001003 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001004 // If the base pointer or element is to a volatile pointer/field, accessing
1005 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001006 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001007 return false;
1008 Loc = cast<MemberExpr>(this)->getMemberLoc();
1009 R1 = SourceRange(Loc, Loc);
1010 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1011 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattner1ec5f562007-06-27 05:38:08 +00001013 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001014 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001015 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001016 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001017 return false;
1018 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1019 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1020 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1021 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001022
Chris Lattner1ec5f562007-06-27 05:38:08 +00001023 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001024 case CXXOperatorCallExprClass:
1025 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001026 // If this is a direct call, get the callee.
1027 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001028 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001029 // If the callee has attribute pure, const, or warn_unused_result, warn
1030 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001031 //
1032 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1033 // updated to match for QoI.
1034 if (FD->getAttr<WarnUnusedResultAttr>() ||
1035 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1036 Loc = CE->getCallee()->getLocStart();
1037 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner1a6babf2009-10-13 04:53:48 +00001039 if (unsigned NumArgs = CE->getNumArgs())
1040 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1041 CE->getArg(NumArgs-1)->getLocEnd());
1042 return true;
1043 }
Chris Lattner237f2752009-02-14 07:37:35 +00001044 }
1045 return false;
1046 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001047
1048 case CXXTemporaryObjectExprClass:
1049 case CXXConstructExprClass:
1050 return false;
1051
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001052 case ObjCMessageExprClass: {
1053 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1054 const ObjCMethodDecl *MD = ME->getMethodDecl();
1055 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1056 Loc = getExprLoc();
1057 return true;
1058 }
Chris Lattner237f2752009-02-14 07:37:35 +00001059 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001060 }
Mike Stump11289f42009-09-09 15:08:12 +00001061
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001062 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001063#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001064 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001065 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001066 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001067 Loc = Ref->getLocation();
1068 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1069 if (Ref->getBase())
1070 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001071#else
1072 Loc = getExprLoc();
1073 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001074#endif
1075 return true;
1076 }
Chris Lattner944d3062008-07-26 19:51:01 +00001077 case StmtExprClass: {
1078 // Statement exprs don't logically have side effects themselves, but are
1079 // sometimes used in macros in ways that give them a type that is unused.
1080 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1081 // however, if the result of the stmt expr is dead, we don't want to emit a
1082 // warning.
1083 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1084 if (!CS->body_empty())
1085 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001086 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001087
John McCallc493a732010-03-12 07:11:26 +00001088 if (getType()->isVoidType())
1089 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001090 Loc = cast<StmtExpr>(this)->getLParenLoc();
1091 R1 = getSourceRange();
1092 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001093 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001094 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001095 // If this is an explicit cast to void, allow it. People do this when they
1096 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001097 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001098 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001099 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1100 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1101 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001102 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001103 if (getType()->isVoidType())
1104 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001105 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001106
Anders Carlsson6aa50392009-11-17 17:11:23 +00001107 // If this is a cast to void or a constructor conversion, check the operand.
1108 // Otherwise, the result of the cast is unused.
1109 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1110 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001111 return (cast<CastExpr>(this)->getSubExpr()
1112 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001113 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1114 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1115 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001118 case ImplicitCastExprClass:
1119 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001120 return (cast<ImplicitCastExpr>(this)
1121 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001122
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001123 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001124 return (cast<CXXDefaultArgExpr>(this)
1125 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001126
1127 case CXXNewExprClass:
1128 // FIXME: In theory, there might be new expressions that don't have side
1129 // effects (e.g. a placement new with an uninitialized POD).
1130 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001131 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001132 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001133 return (cast<CXXBindTemporaryExpr>(this)
1134 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001135 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001136 return (cast<CXXExprWithTemporaries>(this)
1137 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001138 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001139}
1140
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001141/// DeclCanBeLvalue - Determine whether the given declaration can be
1142/// an lvalue. This is a helper routine for isLvalue.
1143static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001144 // C++ [temp.param]p6:
1145 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001146 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001147 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1148 return NTTParm->getType()->isReferenceType();
1149
Douglas Gregor91f84212008-12-11 16:49:14 +00001150 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001151 // C++ 3.10p2: An lvalue refers to an object or function.
1152 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001153 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001154}
1155
Steve Naroff475cca02007-05-14 17:19:29 +00001156/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1157/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001158/// - name, where name must be a variable
1159/// - e[i]
1160/// - (e), where e must be an lvalue
1161/// - e.name, where e must be an lvalue
1162/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001163/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001164/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001165/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001166/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001167///
Chris Lattner67315442008-07-26 21:30:36 +00001168Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001169 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1170
1171 isLvalueResult Res = isLvalueInternal(Ctx);
1172 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1173 return Res;
1174
Douglas Gregor9a657932008-10-21 23:43:52 +00001175 // first, check the type (C99 6.3.2.1). Expressions with function
1176 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001177 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001178 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001179
Steve Naroff1018ea32008-02-10 01:39:04 +00001180 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001181 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001182 return LV_IncompleteVoidType;
1183
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001184 return LV_Valid;
1185}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001186
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001187// Check whether the expression can be sanely treated like an l-value
1188Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001189 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001190 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001191 case StringLiteralClass: // C99 6.5.1p4
1192 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001193 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001194 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001195 // For vectors, make sure base is an lvalue (i.e. not a function call).
1196 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001197 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001198 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001199 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001200 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1201 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001202 return LV_Valid;
1203 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001204 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001205 case BlockDeclRefExprClass: {
1206 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001207 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001208 return LV_Valid;
1209 break;
1210 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001211 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001212 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001213 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1214 NamedDecl *Member = m->getMemberDecl();
1215 // C++ [expr.ref]p4:
1216 // If E2 is declared to have type "reference to T", then E1.E2
1217 // is an lvalue.
1218 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1219 if (Value->getType()->isReferenceType())
1220 return LV_Valid;
1221
1222 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001223 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001224 return LV_Valid;
1225
1226 // -- If E2 is a non-static data member [...]. If E1 is an
1227 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001228 if (isa<FieldDecl>(Member)) {
1229 if (m->isArrow())
1230 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001231 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001232 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001233
1234 // -- If it refers to a static member function [...], then
1235 // E1.E2 is an lvalue.
1236 // -- Otherwise, if E1.E2 refers to a non-static member
1237 // function [...], then E1.E2 is not an lvalue.
1238 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1239 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1240
1241 // -- If E2 is a member enumerator [...], the expression E1.E2
1242 // is not an lvalue.
1243 if (isa<EnumConstantDecl>(Member))
1244 return LV_InvalidExpression;
1245
1246 // Not an lvalue.
1247 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001248 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001249
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001250 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001251 if (m->isArrow())
1252 return LV_Valid;
1253 Expr *BaseExp = m->getBase();
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001254 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1255 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001256 return LV_SubObjCPropertySetting;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001257 return
1258 BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001259 }
Chris Lattner595db862007-10-30 22:53:42 +00001260 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001261 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001262 return LV_Valid; // C99 6.5.3p4
1263
1264 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001265 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1266 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001267 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001268
1269 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1270 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1271 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1272 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001273 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001274 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001275 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1276 return LV_Valid;
1277
1278 // If this is a conversion to a class temporary, make a note of
1279 // that.
1280 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1281 return LV_ClassTemporary;
1282
1283 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001284 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001285 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001286 case BinaryOperatorClass:
1287 case CompoundAssignOperatorClass: {
1288 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001289
1290 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1291 BinOp->getOpcode() == BinaryOperator::Comma)
1292 return BinOp->getRHS()->isLvalue(Ctx);
1293
Sebastian Redl112a97662009-02-07 00:15:38 +00001294 // C++ [expr.mptr.oper]p6
Alexis Hunta8136cc2010-05-05 15:23:54 +00001295 // The result of a .* expression is an lvalue only if its first operand is
1296 // an lvalue and its second operand is a pointer to data member.
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001297 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001298 !BinOp->getType()->isFunctionType())
1299 return BinOp->getLHS()->isLvalue(Ctx);
1300
Alexis Hunta8136cc2010-05-05 15:23:54 +00001301 // The result of an ->* expression is an lvalue only if its second operand
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001302 // is a pointer to data member.
1303 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1304 !BinOp->getType()->isFunctionType()) {
1305 QualType Ty = BinOp->getRHS()->getType();
1306 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1307 return LV_Valid;
1308 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001309
Douglas Gregor58e008d2008-11-13 20:12:29 +00001310 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001311 return LV_InvalidExpression;
1312
Douglas Gregor58e008d2008-11-13 20:12:29 +00001313 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001314 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001315 // The result of an assignment operation [...] is an lvalue.
1316 return LV_Valid;
1317
1318
1319 // C99 6.5.16:
1320 // An assignment expression [...] is not an lvalue.
1321 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001322 }
Mike Stump11289f42009-09-09 15:08:12 +00001323 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001324 case CXXOperatorCallExprClass:
1325 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001326 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001327 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001328 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001329 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1330 if (ReturnType->isLValueReferenceType())
1331 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001332
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001333 // If the function is returning a class temporary, make a note of
1334 // that.
1335 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1336 return LV_ClassTemporary;
1337
Douglas Gregor6b754842008-10-28 00:22:11 +00001338 break;
1339 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001340 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001341 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001342 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001343 case ChooseExprClass:
1344 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001345 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001346 case ExtVectorElementExprClass:
1347 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001348 return LV_DuplicateVectorComponents;
1349 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001350 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1351 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001352 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1353 return LV_Valid;
Enea Zaffanellaf2059772010-04-27 07:38:32 +00001354 case ObjCImplicitSetterGetterRefExprClass:
1355 // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001356 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001357 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001358 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001359 case UnresolvedLookupExprClass:
Douglas Gregor980fb162010-04-29 18:24:40 +00001360 case UnresolvedMemberExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001361 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001362 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001363 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001364 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001365 case CXXFunctionalCastExprClass:
1366 case CXXStaticCastExprClass:
1367 case CXXDynamicCastExprClass:
1368 case CXXReinterpretCastExprClass:
1369 case CXXConstCastExprClass:
1370 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001371 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001372 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1373 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001374 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1375 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001376 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001377
1378 // If this is a conversion to a class temporary, make a note of
1379 // that.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001380 if (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001381 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1382 return LV_ClassTemporary;
1383
Douglas Gregor6b754842008-10-28 00:22:11 +00001384 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001385 case CXXTypeidExprClass:
1386 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1387 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001388 case CXXBindTemporaryExprClass:
1389 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1390 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001391 case CXXBindReferenceExprClass:
1392 // Something that's bound to a reference is always an lvalue.
1393 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001394 case ConditionalOperatorClass: {
1395 // Complicated handling is only for C++.
1396 if (!Ctx.getLangOptions().CPlusPlus)
1397 return LV_InvalidExpression;
1398
1399 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1400 // everywhere there's an object converted to an rvalue. Also, any other
1401 // casts should be wrapped by ImplicitCastExprs. There's just the special
1402 // case involving throws to work out.
1403 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001404 Expr *True = Cond->getTrueExpr();
1405 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001406 // C++0x 5.16p2
1407 // If either the second or the third operand has type (cv) void, [...]
1408 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001409 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001410 return LV_InvalidExpression;
1411
1412 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001413 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001414 return LV_InvalidExpression;
1415
1416 // That's it.
1417 return LV_Valid;
1418 }
1419
Douglas Gregor5103eff2009-12-19 07:07:47 +00001420 case Expr::CXXExprWithTemporariesClass:
1421 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1422
1423 case Expr::ObjCMessageExprClass:
1424 if (const ObjCMethodDecl *Method
1425 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1426 if (Method->getResultType()->isLValueReferenceType())
1427 return LV_Valid;
1428 break;
1429
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001430 case Expr::CXXConstructExprClass:
1431 case Expr::CXXTemporaryObjectExprClass:
1432 case Expr::CXXZeroInitValueExprClass:
1433 return LV_ClassTemporary;
1434
Steve Naroff9358c712007-05-27 23:58:33 +00001435 default:
1436 break;
Steve Naroff47500512007-04-19 23:00:49 +00001437 }
Steve Naroff9358c712007-05-27 23:58:33 +00001438 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001439}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001440
Steve Naroff475cca02007-05-14 17:19:29 +00001441/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1442/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001443/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001444/// recursively, any member or element of all contained aggregates or unions)
1445/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001446Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001447Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001448 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001449
Steve Naroff9358c712007-05-27 23:58:33 +00001450 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001451 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001452 // C++ 3.10p11: Functions cannot be modified, but pointers to
1453 // functions can be modifiable.
1454 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1455 return MLV_NotObjectType;
1456 break;
1457
Chris Lattner1ec5f562007-06-27 05:38:08 +00001458 case LV_NotObjectType: return MLV_NotObjectType;
1459 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001460 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001461 case LV_InvalidExpression:
1462 // If the top level is a C-style cast, and the subexpression is a valid
1463 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1464 // GCC extension. We don't support it, but we want to produce good
1465 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001466 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1467 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1468 if (Loc)
1469 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001470 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001471 }
1472 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001473 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001474 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001475 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001476 case LV_ClassTemporary:
1477 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001478 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001479
1480 // The following is illegal:
1481 // void takeclosure(void (^C)(void));
1482 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1483 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001484 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001485 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1486 return MLV_NotBlockQualified;
1487 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001488
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001489 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001490 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001491 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1492 if (Expr->getSetterMethod() == 0)
1493 return MLV_NoSetterProperty;
1494 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001495
Chris Lattner7adf0762008-08-04 07:31:14 +00001496 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001497
Chris Lattner7adf0762008-08-04 07:31:14 +00001498 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001499 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001500 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001501 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001502 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001503 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001504
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001505 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001506 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001507 return MLV_ConstQualified;
1508 }
Mike Stump11289f42009-09-09 15:08:12 +00001509
Mike Stump11289f42009-09-09 15:08:12 +00001510 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001511}
1512
Fariborz Jahanian07735332009-02-22 18:40:18 +00001513/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001514/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001515bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001516 switch (getStmtClass()) {
1517 default:
1518 return false;
1519 case ObjCIvarRefExprClass:
1520 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001521 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001522 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001523 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001524 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001525 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001526 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001527 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001528 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001529 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001530 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001531 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1532 if (VD->hasGlobalStorage())
1533 return true;
1534 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001535 // dereferencing to a pointer is always a gc'able candidate,
1536 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001537 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001538 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001539 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001540 return false;
1541 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001542 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001543 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001544 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001545 }
1546 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001547 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001548 }
1549}
Ted Kremenekfff70962008-01-17 16:57:34 +00001550Expr* Expr::IgnoreParens() {
1551 Expr* E = this;
1552 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1553 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001554
Ted Kremenekfff70962008-01-17 16:57:34 +00001555 return E;
1556}
1557
Chris Lattnerf2660962008-02-13 01:02:39 +00001558/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1559/// or CastExprs or ImplicitCastExprs, returning their operand.
1560Expr *Expr::IgnoreParenCasts() {
1561 Expr *E = this;
1562 while (true) {
1563 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1564 E = P->getSubExpr();
1565 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1566 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001567 else
1568 return E;
1569 }
1570}
1571
John McCalleebc8322010-05-05 22:59:52 +00001572Expr *Expr::IgnoreParenImpCasts() {
1573 Expr *E = this;
1574 while (true) {
1575 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1576 E = P->getSubExpr();
1577 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1578 E = P->getSubExpr();
1579 else
1580 return E;
1581 }
1582}
1583
Chris Lattneref26c772009-03-13 17:28:01 +00001584/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1585/// value (including ptr->int casts of the same size). Strip off any
1586/// ParenExpr or CastExprs, returning their operand.
1587Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1588 Expr *E = this;
1589 while (true) {
1590 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1591 E = P->getSubExpr();
1592 continue;
1593 }
Mike Stump11289f42009-09-09 15:08:12 +00001594
Chris Lattneref26c772009-03-13 17:28:01 +00001595 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1596 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1597 // ptr<->int casts of the same width. We also ignore all identify casts.
1598 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001599
Chris Lattneref26c772009-03-13 17:28:01 +00001600 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1601 E = SE;
1602 continue;
1603 }
Mike Stump11289f42009-09-09 15:08:12 +00001604
Chris Lattneref26c772009-03-13 17:28:01 +00001605 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1606 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1607 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1608 E = SE;
1609 continue;
1610 }
1611 }
Mike Stump11289f42009-09-09 15:08:12 +00001612
Chris Lattneref26c772009-03-13 17:28:01 +00001613 return E;
1614 }
1615}
1616
Douglas Gregord196a582009-12-14 19:27:10 +00001617bool Expr::isDefaultArgument() const {
1618 const Expr *E = this;
1619 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1620 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001621
Douglas Gregord196a582009-12-14 19:27:10 +00001622 return isa<CXXDefaultArgExpr>(E);
1623}
Chris Lattneref26c772009-03-13 17:28:01 +00001624
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001625/// \brief Skip over any no-op casts and any temporary-binding
1626/// expressions.
1627static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1628 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1629 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1630 E = ICE->getSubExpr();
1631 else
1632 break;
1633 }
1634
1635 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1636 E = BE->getSubExpr();
1637
1638 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1639 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1640 E = ICE->getSubExpr();
1641 else
1642 break;
1643 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001644
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001645 return E;
1646}
1647
1648const Expr *Expr::getTemporaryObject() const {
1649 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1650
1651 // A cast can produce a temporary object. The object's construction
1652 // is represented as a CXXConstructExpr.
1653 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1654 // Only user-defined and constructor conversions can produce
1655 // temporary objects.
1656 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1657 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1658 return 0;
1659
1660 // Strip off temporary bindings and no-op casts.
1661 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1662
1663 // If this is a constructor conversion, see if we have an object
1664 // construction.
1665 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1666 return dyn_cast<CXXConstructExpr>(Sub);
1667
1668 // If this is a user-defined conversion, see if we have a call to
1669 // a function that itself returns a temporary object.
1670 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1671 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1672 if (CE->getCallReturnType()->isRecordType())
1673 return CE;
1674
1675 return 0;
1676 }
1677
1678 // A call returning a class type returns a temporary.
1679 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1680 if (CE->getCallReturnType()->isRecordType())
1681 return CE;
1682
1683 return 0;
1684 }
1685
1686 // Explicit temporary object constructors create temporaries.
1687 return dyn_cast<CXXTemporaryObjectExpr>(E);
1688}
1689
Douglas Gregor4619e432008-12-05 23:32:09 +00001690/// hasAnyTypeDependentArguments - Determines if any of the expressions
1691/// in Exprs is type-dependent.
1692bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1693 for (unsigned I = 0; I < NumExprs; ++I)
1694 if (Exprs[I]->isTypeDependent())
1695 return true;
1696
1697 return false;
1698}
1699
1700/// hasAnyValueDependentArguments - Determines if any of the expressions
1701/// in Exprs is value-dependent.
1702bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1703 for (unsigned I = 0; I < NumExprs; ++I)
1704 if (Exprs[I]->isValueDependent())
1705 return true;
1706
1707 return false;
1708}
1709
Eli Friedman7139af42009-01-25 02:32:41 +00001710bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001711 // This function is attempting whether an expression is an initializer
1712 // which can be evaluated at compile-time. isEvaluatable handles most
1713 // of the cases, but it can't deal with some initializer-specific
1714 // expressions, and it can't deal with aggregates; we deal with those here,
1715 // and fall back to isEvaluatable for the other cases.
1716
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001717 // FIXME: This function assumes the variable being assigned to
1718 // isn't a reference type!
1719
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001720 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001721 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001722 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001723 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001724 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001725 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001726 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001727 // This handles gcc's extension that allows global initializers like
1728 // "struct x {int x;} x = (struct x) {};".
1729 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001730 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001731 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001732 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001733 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001734 // FIXME: This doesn't deal with fields with reference types correctly.
1735 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1736 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001737 const InitListExpr *Exp = cast<InitListExpr>(this);
1738 unsigned numInits = Exp->getNumInits();
1739 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001740 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001741 return false;
1742 }
Eli Friedman384da272009-01-25 03:12:18 +00001743 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001744 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001745 case ImplicitValueInitExprClass:
1746 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001747 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001748 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001749 case UnaryOperatorClass: {
1750 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1751 if (Exp->getOpcode() == UnaryOperator::Extension)
1752 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1753 break;
1754 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001755 case BinaryOperatorClass: {
1756 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1757 // but this handles the common case.
1758 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1759 if (Exp->getOpcode() == BinaryOperator::Sub &&
1760 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1761 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1762 return true;
1763 break;
1764 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001765 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001766 case CStyleCastExprClass:
1767 // Handle casts with a destination that's a struct or union; this
1768 // deals with both the gcc no-op struct cast extension and the
1769 // cast-to-union extension.
1770 if (getType()->isRecordType())
1771 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001772
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001773 // Integer->integer casts can be handled here, which is important for
1774 // things like (int)(&&x-&&y). Scary but true.
1775 if (getType()->isIntegerType() &&
1776 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1777 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001778
Eli Friedman384da272009-01-25 03:12:18 +00001779 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001780 }
Eli Friedman384da272009-01-25 03:12:18 +00001781 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001782}
1783
Chris Lattner7eef9192007-05-24 01:23:49 +00001784/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1785/// integer constant expression with the value zero, or if this is one that is
1786/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001787bool Expr::isNullPointerConstant(ASTContext &Ctx,
1788 NullPointerConstantValueDependence NPC) const {
1789 if (isValueDependent()) {
1790 switch (NPC) {
1791 case NPC_NeverValueDependent:
1792 assert(false && "Unexpected value dependent expression!");
1793 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001794
Douglas Gregor56751b52009-09-25 04:25:58 +00001795 case NPC_ValueDependentIsNull:
1796 return isTypeDependent() || getType()->isIntegralType();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001797
Douglas Gregor56751b52009-09-25 04:25:58 +00001798 case NPC_ValueDependentIsNotNull:
1799 return false;
1800 }
1801 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001802
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001803 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001804 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001805 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001806 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001807 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001808 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001809 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001810 Pointee->isVoidType() && // to void*
1811 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001812 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001813 }
Steve Naroffada7d422007-05-20 17:54:12 +00001814 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001815 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1816 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001817 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001818 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1819 // Accept ((void*)0) as a null pointer constant, as many other
1820 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001821 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001822 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001823 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001824 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001825 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001826 } else if (isa<GNUNullExpr>(this)) {
1827 // The GNU __null extension is always a null pointer constant.
1828 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001829 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001830
Sebastian Redl576fd422009-05-10 18:38:11 +00001831 // C++0x nullptr_t is always a null pointer constant.
1832 if (getType()->isNullPtrType())
1833 return true;
1834
Steve Naroff4871fe02008-01-14 16:10:57 +00001835 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001836 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001837 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001838 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001839
Chris Lattner1abbd412007-06-08 17:58:43 +00001840 // If we have an integer constant expression, we need to *evaluate* it and
1841 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001842 llvm::APSInt Result;
1843 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001844}
Steve Narofff7a5da12007-07-28 23:10:27 +00001845
Douglas Gregor71235ec2009-05-02 02:18:30 +00001846FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001847 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001848
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001849 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1850 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1851 E = ICE->getSubExpr()->IgnoreParens();
1852 else
1853 break;
1854 }
1855
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001856 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001857 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001858 if (Field->isBitField())
1859 return Field;
1860
1861 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1862 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1863 return BinOp->getLHS()->getBitField();
1864
1865 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001866}
1867
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001868bool Expr::refersToVectorElement() const {
1869 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001870
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001871 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1872 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1873 E = ICE->getSubExpr()->IgnoreParens();
1874 else
1875 break;
1876 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001877
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001878 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1879 return ASE->getBase()->getType()->isVectorType();
1880
1881 if (isa<ExtVectorElementExpr>(E))
1882 return true;
1883
1884 return false;
1885}
1886
Chris Lattnerb8211f62009-02-16 22:14:05 +00001887/// isArrow - Return true if the base expression is a pointer to vector,
1888/// return false if the base expression is a vector.
1889bool ExtVectorElementExpr::isArrow() const {
1890 return getBase()->getType()->isPointerType();
1891}
1892
Nate Begemance4d7fc2008-04-18 23:10:10 +00001893unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001894 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001895 return VT->getNumElements();
1896 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001897}
1898
Nate Begemanf322eab2008-05-09 06:41:27 +00001899/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001900bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001901 // FIXME: Refactor this code to an accessor on the AST node which returns the
1902 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001903 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001904
1905 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001906 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001907 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001908
Nate Begeman7e5185b2009-01-18 02:01:21 +00001909 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001910 if (Comp[0] == 's' || Comp[0] == 'S')
1911 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001912
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001913 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1914 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001915 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001916
Steve Naroff0d595ca2007-07-30 03:29:09 +00001917 return false;
1918}
Chris Lattner885b4952007-08-02 23:36:59 +00001919
Nate Begemanf322eab2008-05-09 06:41:27 +00001920/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001921void ExtVectorElementExpr::getEncodedElementAccess(
1922 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001923 llvm::StringRef Comp = Accessor->getName();
1924 if (Comp[0] == 's' || Comp[0] == 'S')
1925 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001926
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001927 bool isHi = Comp == "hi";
1928 bool isLo = Comp == "lo";
1929 bool isEven = Comp == "even";
1930 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001931
Nate Begemanf322eab2008-05-09 06:41:27 +00001932 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1933 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001934
Nate Begemanf322eab2008-05-09 06:41:27 +00001935 if (isHi)
1936 Index = e + i;
1937 else if (isLo)
1938 Index = i;
1939 else if (isEven)
1940 Index = 2 * i;
1941 else if (isOdd)
1942 Index = 2 * i + 1;
1943 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001944 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001945
Nate Begemand3862152008-05-13 21:03:02 +00001946 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001947 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001948}
1949
Douglas Gregor9a129192010-04-21 00:45:42 +00001950ObjCMessageExpr::ObjCMessageExpr(QualType T,
1951 SourceLocation LBracLoc,
1952 SourceLocation SuperLoc,
1953 bool IsInstanceSuper,
1954 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001955 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001956 ObjCMethodDecl *Method,
1957 Expr **Args, unsigned NumArgs,
1958 SourceLocation RBracLoc)
1959 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001960 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001961 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1962 HasMethod(Method != 0), SuperLoc(SuperLoc),
1963 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1964 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001965 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001966{
Douglas Gregor9a129192010-04-21 00:45:42 +00001967 setReceiverPointer(SuperType.getAsOpaquePtr());
1968 if (NumArgs)
1969 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001970}
1971
Douglas Gregor9a129192010-04-21 00:45:42 +00001972ObjCMessageExpr::ObjCMessageExpr(QualType T,
1973 SourceLocation LBracLoc,
1974 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001975 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001976 ObjCMethodDecl *Method,
1977 Expr **Args, unsigned NumArgs,
1978 SourceLocation RBracLoc)
1979 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001980 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001981 hasAnyValueDependentArguments(Args, NumArgs))),
1982 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1983 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1984 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001985 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001986{
1987 setReceiverPointer(Receiver);
1988 if (NumArgs)
1989 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001990}
1991
Douglas Gregor9a129192010-04-21 00:45:42 +00001992ObjCMessageExpr::ObjCMessageExpr(QualType T,
1993 SourceLocation LBracLoc,
1994 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001995 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001996 ObjCMethodDecl *Method,
1997 Expr **Args, unsigned NumArgs,
1998 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001999 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002000 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00002001 hasAnyValueDependentArguments(Args, NumArgs))),
2002 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2003 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2004 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00002005 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002006{
2007 setReceiverPointer(Receiver);
2008 if (NumArgs)
2009 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00002010}
2011
Douglas Gregor9a129192010-04-21 00:45:42 +00002012ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2013 SourceLocation LBracLoc,
2014 SourceLocation SuperLoc,
2015 bool IsInstanceSuper,
2016 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002017 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002018 ObjCMethodDecl *Method,
2019 Expr **Args, unsigned NumArgs,
2020 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002021 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002022 NumArgs * sizeof(Expr *);
2023 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2024 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002025 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002026 RBracLoc);
2027}
2028
2029ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2030 SourceLocation LBracLoc,
2031 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002032 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002033 ObjCMethodDecl *Method,
2034 Expr **Args, unsigned NumArgs,
2035 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002036 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002037 NumArgs * sizeof(Expr *);
2038 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002039 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002040 NumArgs, RBracLoc);
2041}
2042
2043ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2044 SourceLocation LBracLoc,
2045 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002046 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002047 ObjCMethodDecl *Method,
2048 Expr **Args, unsigned NumArgs,
2049 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002050 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002051 NumArgs * sizeof(Expr *);
2052 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002053 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002054 NumArgs, RBracLoc);
2055}
2056
Alexis Hunta8136cc2010-05-05 15:23:54 +00002057ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002058 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002059 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002060 NumArgs * sizeof(Expr *);
2061 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2062 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2063}
Alexis Hunta8136cc2010-05-05 15:23:54 +00002064
Douglas Gregor9a129192010-04-21 00:45:42 +00002065Selector ObjCMessageExpr::getSelector() const {
2066 if (HasMethod)
2067 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2068 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002069 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002070}
2071
2072ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2073 switch (getReceiverKind()) {
2074 case Instance:
2075 if (const ObjCObjectPointerType *Ptr
2076 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2077 return Ptr->getInterfaceDecl();
2078 break;
2079
2080 case Class:
2081 if (const ObjCInterfaceType *Iface
2082 = getClassReceiver()->getAs<ObjCInterfaceType>())
2083 return Iface->getDecl();
2084 break;
2085
2086 case SuperInstance:
2087 if (const ObjCObjectPointerType *Ptr
2088 = getSuperType()->getAs<ObjCObjectPointerType>())
2089 return Ptr->getInterfaceDecl();
2090 break;
2091
2092 case SuperClass:
2093 if (const ObjCObjectPointerType *Iface
2094 = getSuperType()->getAs<ObjCObjectPointerType>())
2095 return Iface->getInterfaceDecl();
2096 break;
2097 }
2098
2099 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002100}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002101
Chris Lattner35e564e2007-10-25 00:29:32 +00002102bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002103 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002104}
2105
Nate Begeman48745922009-08-12 02:28:50 +00002106void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2107 unsigned NumExprs) {
2108 if (SubExprs) C.Deallocate(SubExprs);
2109
2110 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002111 this->NumExprs = NumExprs;
2112 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002113}
Nate Begeman48745922009-08-12 02:28:50 +00002114
2115void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2116 DestroyChildren(C);
2117 if (SubExprs) C.Deallocate(SubExprs);
2118 this->~ShuffleVectorExpr();
2119 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002120}
2121
Douglas Gregore26a2852009-08-07 06:08:38 +00002122void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002123 // Override default behavior of traversing children. If this has a type
2124 // operand and the type is a variable-length array, the child iteration
2125 // will iterate over the size expression. However, this expression belongs
2126 // to the type, not to this, so we don't want to delete it.
2127 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002128 if (isArgumentType()) {
2129 this->~SizeOfAlignOfExpr();
2130 C.Deallocate(this);
2131 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002132 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002133 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002134}
2135
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002136//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002137// DesignatedInitExpr
2138//===----------------------------------------------------------------------===//
2139
2140IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2141 assert(Kind == FieldDesignator && "Only valid on a field designator");
2142 if (Field.NameOrField & 0x01)
2143 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2144 else
2145 return getField()->getIdentifier();
2146}
2147
Alexis Hunta8136cc2010-05-05 15:23:54 +00002148DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002149 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002150 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002151 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002152 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002153 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002154 unsigned NumIndexExprs,
2155 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002156 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002157 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002158 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2159 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002160 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002161
2162 // Record the initializer itself.
2163 child_iterator Child = child_begin();
2164 *Child++ = Init;
2165
2166 // Copy the designators and their subexpressions, computing
2167 // value-dependence along the way.
2168 unsigned IndexIdx = 0;
2169 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002170 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002171
2172 if (this->Designators[I].isArrayDesignator()) {
2173 // Compute type- and value-dependence.
2174 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002175 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002176 Index->isTypeDependent() || Index->isValueDependent();
2177
2178 // Copy the index expressions into permanent storage.
2179 *Child++ = IndexExprs[IndexIdx++];
2180 } else if (this->Designators[I].isArrayRangeDesignator()) {
2181 // Compute type- and value-dependence.
2182 Expr *Start = IndexExprs[IndexIdx];
2183 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002184 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002185 Start->isTypeDependent() || Start->isValueDependent() ||
2186 End->isTypeDependent() || End->isValueDependent();
2187
2188 // Copy the start/end expressions into permanent storage.
2189 *Child++ = IndexExprs[IndexIdx++];
2190 *Child++ = IndexExprs[IndexIdx++];
2191 }
2192 }
2193
2194 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002195}
2196
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002197DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002198DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002199 unsigned NumDesignators,
2200 Expr **IndexExprs, unsigned NumIndexExprs,
2201 SourceLocation ColonOrEqualLoc,
2202 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002203 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002204 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002205 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002206 ColonOrEqualLoc, UsesColonSyntax,
2207 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002208}
2209
Mike Stump11289f42009-09-09 15:08:12 +00002210DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002211 unsigned NumIndexExprs) {
2212 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2213 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2214 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2215}
2216
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002217void DesignatedInitExpr::setDesignators(ASTContext &C,
2218 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002219 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002220 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002221
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002222 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002223 NumDesignators = NumDesigs;
2224 for (unsigned I = 0; I != NumDesigs; ++I)
2225 Designators[I] = Desigs[I];
2226}
2227
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002228SourceRange DesignatedInitExpr::getSourceRange() const {
2229 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002230 Designator &First =
2231 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002232 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002233 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002234 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2235 else
2236 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2237 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002238 StartLoc =
2239 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002240 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2241}
2242
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002243Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2244 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2245 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2246 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002247 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2248 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2249}
2250
2251Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002252 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002253 "Requires array range designator");
2254 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2255 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002256 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2257 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2258}
2259
2260Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002261 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002262 "Requires array range designator");
2263 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2264 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002265 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2266 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2267}
2268
Douglas Gregord5846a12009-04-15 06:41:24 +00002269/// \brief Replaces the designator at index @p Idx with the series
2270/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002271void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002272 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002273 const Designator *Last) {
2274 unsigned NumNewDesignators = Last - First;
2275 if (NumNewDesignators == 0) {
2276 std::copy_backward(Designators + Idx + 1,
2277 Designators + NumDesignators,
2278 Designators + Idx);
2279 --NumNewDesignators;
2280 return;
2281 } else if (NumNewDesignators == 1) {
2282 Designators[Idx] = *First;
2283 return;
2284 }
2285
Mike Stump11289f42009-09-09 15:08:12 +00002286 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002287 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002288 std::copy(Designators, Designators + Idx, NewDesignators);
2289 std::copy(First, Last, NewDesignators + Idx);
2290 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2291 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002292 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002293 Designators = NewDesignators;
2294 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2295}
2296
Douglas Gregore26a2852009-08-07 06:08:38 +00002297void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002298 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002299 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002300}
2301
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002302void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2303 for (unsigned I = 0; I != NumDesignators; ++I)
2304 Designators[I].~Designator();
2305 C.Deallocate(Designators);
2306 Designators = 0;
2307}
2308
Mike Stump11289f42009-09-09 15:08:12 +00002309ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002310 Expr **exprs, unsigned nexprs,
2311 SourceLocation rparenloc)
2312: Expr(ParenListExprClass, QualType(),
2313 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002314 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002315 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002316
Nate Begeman5ec4b312009-08-10 23:49:36 +00002317 Exprs = new (C) Stmt*[nexprs];
2318 for (unsigned i = 0; i != nexprs; ++i)
2319 Exprs[i] = exprs[i];
2320}
2321
2322void ParenListExpr::DoDestroy(ASTContext& C) {
2323 DestroyChildren(C);
2324 if (Exprs) C.Deallocate(Exprs);
2325 this->~ParenListExpr();
2326 C.Deallocate(this);
2327}
2328
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002329//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002330// ExprIterator.
2331//===----------------------------------------------------------------------===//
2332
2333Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2334Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2335Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2336const Expr* ConstExprIterator::operator[](size_t idx) const {
2337 return cast<Expr>(I[idx]);
2338}
2339const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2340const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2341
2342//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002343// Child Iterators for iterating over subexpressions/substatements
2344//===----------------------------------------------------------------------===//
2345
2346// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002347Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2348Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002349
Steve Naroffe46504b2007-11-12 14:29:37 +00002350// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002351Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2352Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002353
Steve Naroffebf4cb42008-06-02 23:03:37 +00002354// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002355Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2356Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002357
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002358// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002359Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00002360 // If this is accessing a class member, skip that entry.
2361 if (Base) return &Base;
2362 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002363}
Mike Stump11289f42009-09-09 15:08:12 +00002364Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2365 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002366}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002367
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002368// ObjCSuperExpr
2369Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2370Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2371
Steve Naroffe87026a2009-07-24 17:54:45 +00002372// ObjCIsaExpr
2373Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2374Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2375
Chris Lattner6307f192008-08-10 01:53:14 +00002376// PredefinedExpr
2377Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2378Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002379
2380// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002381Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2382Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002383
2384// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002385Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002386Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002387
2388// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002389Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2390Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002391
Chris Lattner1c20a172007-08-26 03:42:43 +00002392// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002393Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2394Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002395
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002396// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002397Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2398Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002399
2400// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002401Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2402Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002403
2404// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002405Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2406Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002407
Douglas Gregor882211c2010-04-28 22:16:22 +00002408// OffsetOfExpr
2409Stmt::child_iterator OffsetOfExpr::child_begin() {
2410 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2411 + NumComps);
2412}
2413Stmt::child_iterator OffsetOfExpr::child_end() {
2414 return child_iterator(&*child_begin() + NumExprs);
2415}
2416
Sebastian Redl6f282892008-11-11 17:56:53 +00002417// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002418Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002419 // If this is of a type and the type is a VLA type (and not a typedef), the
2420 // size expression of the VLA needs to be treated as an executable expression.
2421 // Why isn't this weirdness documented better in StmtIterator?
2422 if (isArgumentType()) {
2423 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2424 getArgumentType().getTypePtr()))
2425 return child_iterator(T);
2426 return child_iterator();
2427 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002428 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002429}
Sebastian Redl6f282892008-11-11 17:56:53 +00002430Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2431 if (isArgumentType())
2432 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002433 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002434}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002435
2436// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002437Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002438 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002439}
Ted Kremenek23702b62007-08-24 20:06:47 +00002440Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002441 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002442}
2443
2444// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002445Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002446 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002447}
Ted Kremenek23702b62007-08-24 20:06:47 +00002448Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002449 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002450}
Ted Kremenek23702b62007-08-24 20:06:47 +00002451
2452// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002453Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2454Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002455
Nate Begemance4d7fc2008-04-18 23:10:10 +00002456// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002457Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2458Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002459
2460// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002461Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2462Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002463
Ted Kremenek23702b62007-08-24 20:06:47 +00002464// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002465Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2466Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002467
2468// BinaryOperator
2469Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002470 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002471}
Ted Kremenek23702b62007-08-24 20:06:47 +00002472Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002473 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002474}
2475
2476// ConditionalOperator
2477Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002478 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002479}
Ted Kremenek23702b62007-08-24 20:06:47 +00002480Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002481 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002482}
2483
2484// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002485Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2486Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002487
Ted Kremenek23702b62007-08-24 20:06:47 +00002488// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002489Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2490Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002491
2492// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002493Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2494 return child_iterator();
2495}
2496
2497Stmt::child_iterator TypesCompatibleExpr::child_end() {
2498 return child_iterator();
2499}
Ted Kremenek23702b62007-08-24 20:06:47 +00002500
2501// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002502Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2503Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002504
Douglas Gregor3be4b122008-11-29 04:51:27 +00002505// GNUNullExpr
2506Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2507Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2508
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002509// ShuffleVectorExpr
2510Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002511 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002512}
2513Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002514 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002515}
2516
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002517// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002518Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2519Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002520
Anders Carlsson4692db02007-08-31 04:56:16 +00002521// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002522Stmt::child_iterator InitListExpr::child_begin() {
2523 return InitExprs.size() ? &InitExprs[0] : 0;
2524}
2525Stmt::child_iterator InitListExpr::child_end() {
2526 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2527}
Anders Carlsson4692db02007-08-31 04:56:16 +00002528
Douglas Gregor0202cb42009-01-29 17:44:32 +00002529// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002530Stmt::child_iterator DesignatedInitExpr::child_begin() {
2531 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2532 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002533 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2534}
2535Stmt::child_iterator DesignatedInitExpr::child_end() {
2536 return child_iterator(&*child_begin() + NumSubExprs);
2537}
2538
Douglas Gregor0202cb42009-01-29 17:44:32 +00002539// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002540Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2541 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002542}
2543
Mike Stump11289f42009-09-09 15:08:12 +00002544Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2545 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002546}
2547
Nate Begeman5ec4b312009-08-10 23:49:36 +00002548// ParenListExpr
2549Stmt::child_iterator ParenListExpr::child_begin() {
2550 return &Exprs[0];
2551}
2552Stmt::child_iterator ParenListExpr::child_end() {
2553 return &Exprs[0]+NumExprs;
2554}
2555
Ted Kremenek23702b62007-08-24 20:06:47 +00002556// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002557Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002558 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002559}
2560Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002561 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002562}
Ted Kremenek23702b62007-08-24 20:06:47 +00002563
2564// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002565Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2566Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002567
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002568// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002569Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002570 return child_iterator();
2571}
2572Stmt::child_iterator ObjCSelectorExpr::child_end() {
2573 return child_iterator();
2574}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002575
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002576// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002577Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2578 return child_iterator();
2579}
2580Stmt::child_iterator ObjCProtocolExpr::child_end() {
2581 return child_iterator();
2582}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002583
Steve Naroffd54978b2007-09-18 23:55:05 +00002584// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002585Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002586 if (getReceiverKind() == Instance)
2587 return reinterpret_cast<Stmt **>(this + 1);
2588 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002589}
2590Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002591 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002592}
2593
Steve Naroffc540d662008-09-03 18:15:37 +00002594// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002595Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2596Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002597
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002598Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2599Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }