blob: e36ae41ce46ec872defee3135a021a84c734c148 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Chris Lattnerbef0efd2010-05-13 01:02:19 +000030void Expr::ANCHOR() {} // key function for Expr class.
31
Chris Lattner2b334bb2010-04-16 23:34:13 +000032/// isKnownToHaveBooleanValue - Return true if this is an integer expression
33/// that is known to return 0 or 1. This happens for _Bool/bool expressions
34/// but also int expressions which are produced by things like comparisons in
35/// C.
36bool Expr::isKnownToHaveBooleanValue() const {
37 // If this value has _Bool type, it is obvious 0/1.
38 if (getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000039 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregor2ade35e2010-06-16 00:17:44 +000040 if (!getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000041
Chris Lattner2b334bb2010-04-16 23:34:13 +000042 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
43 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000044
Chris Lattner2b334bb2010-04-16 23:34:13 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
46 switch (UO->getOpcode()) {
47 case UnaryOperator::Plus:
48 case UnaryOperator::Extension:
49 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Sean Huntc3021132010-05-05 15:23:54 +000054
John McCall6907fbe2010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
57 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner2b334bb2010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000059
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
61 switch (BO->getOpcode()) {
62 default: return false;
63 case BinaryOperator::LT: // Relational operators.
64 case BinaryOperator::GT:
65 case BinaryOperator::LE:
66 case BinaryOperator::GE:
67 case BinaryOperator::EQ: // Equality operators.
68 case BinaryOperator::NE:
69 case BinaryOperator::LAnd: // AND operator.
70 case BinaryOperator::LOr: // Logical OR operator.
71 return true;
Sean Huntc3021132010-05-05 15:23:54 +000072
Chris Lattner2b334bb2010-04-16 23:34:13 +000073 case BinaryOperator::And: // Bitwise AND operator.
74 case BinaryOperator::Xor: // Bitwise XOR operator.
75 case BinaryOperator::Or: // Bitwise OR operator.
76 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000079
Chris Lattner2b334bb2010-04-16 23:34:13 +000080 case BinaryOperator::Comma:
81 case BinaryOperator::Assign:
82 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Sean Huntc3021132010-05-05 15:23:54 +000085
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
87 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000089
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 return false;
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093//===----------------------------------------------------------------------===//
94// Primary Expressions.
95//===----------------------------------------------------------------------===//
96
John McCalld5532b62009-11-23 01:53:49 +000097void ExplicitTemplateArgumentList::initializeFrom(
98 const TemplateArgumentListInfo &Info) {
99 LAngleLoc = Info.getLAngleLoc();
100 RAngleLoc = Info.getRAngleLoc();
101 NumTemplateArgs = Info.size();
102
103 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
104 for (unsigned i = 0; i != NumTemplateArgs; ++i)
105 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
106}
107
108void ExplicitTemplateArgumentList::copyInto(
109 TemplateArgumentListInfo &Info) const {
110 Info.setLAngleLoc(LAngleLoc);
111 Info.setRAngleLoc(RAngleLoc);
112 for (unsigned I = 0; I != NumTemplateArgs; ++I)
113 Info.addArgument(getTemplateArgs()[I]);
114}
115
116std::size_t ExplicitTemplateArgumentList::sizeFor(
117 const TemplateArgumentListInfo &Info) {
118 return sizeof(ExplicitTemplateArgumentList) +
119 sizeof(TemplateArgumentLoc) * Info.size();
120}
121
Douglas Gregor0da76df2009-11-23 11:41:28 +0000122void DeclRefExpr::computeDependence() {
123 TypeDependent = false;
124 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000125
Douglas Gregor0da76df2009-11-23 11:41:28 +0000126 NamedDecl *D = getDecl();
127
128 // (TD) C++ [temp.dep.expr]p3:
129 // An id-expression is type-dependent if it contains:
130 //
Sean Huntc3021132010-05-05 15:23:54 +0000131 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000132 //
133 // (VD) C++ [temp.dep.constexpr]p2:
134 // An identifier is value-dependent if it is:
135
136 // (TD) - an identifier that was declared with dependent type
137 // (VD) - a name declared with a dependent type,
138 if (getType()->isDependentType()) {
139 TypeDependent = true;
140 ValueDependent = true;
141 }
142 // (TD) - a conversion-function-id that specifies a dependent type
Sean Huntc3021132010-05-05 15:23:54 +0000143 else if (D->getDeclName().getNameKind()
Douglas Gregor0da76df2009-11-23 11:41:28 +0000144 == DeclarationName::CXXConversionFunctionName &&
145 D->getDeclName().getCXXNameType()->isDependentType()) {
146 TypeDependent = true;
147 ValueDependent = true;
148 }
149 // (TD) - a template-id that is dependent,
Sean Huntc3021132010-05-05 15:23:54 +0000150 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 TemplateSpecializationType::anyDependentTemplateArguments(
Sean Huntc3021132010-05-05 15:23:54 +0000152 getTemplateArgs(),
Douglas Gregor0da76df2009-11-23 11:41:28 +0000153 getNumTemplateArgs())) {
154 TypeDependent = true;
155 ValueDependent = true;
156 }
157 // (VD) - the name of a non-type template parameter,
158 else if (isa<NonTypeTemplateParmDecl>(D))
159 ValueDependent = true;
160 // (VD) - a constant with integral or enumeration type and is
161 // initialized with an expression that is value-dependent.
162 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000163 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000164 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000165 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000166 if (Init->isValueDependent())
167 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000168 }
169 // (VD) - FIXME: Missing from the standard:
170 // - a member function or a static data member of the current
171 // instantiation
172 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000173 Var->getDeclContext()->isDependentContext())
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000174 ValueDependent = true;
175 }
176 // (VD) - FIXME: Missing from the standard:
177 // - a member function or a static data member of the current
178 // instantiation
179 else if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext())
180 ValueDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000181 // (TD) - a nested-name-specifier or a qualified-id that names a
182 // member of an unknown specialization.
183 // (handled by DependentScopeDeclRefExpr)
184}
185
Sean Huntc3021132010-05-05 15:23:54 +0000186DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000187 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000188 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000189 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000190 QualType T)
191 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000192 DecoratedD(D,
193 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000194 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000195 Loc(NameLoc) {
196 if (Qualifier) {
197 NameQualifier *NQ = getNameQualifier();
198 NQ->NNS = Qualifier;
199 NQ->Range = QualifierRange;
200 }
Sean Huntc3021132010-05-05 15:23:54 +0000201
John McCalld5532b62009-11-23 01:53:49 +0000202 if (TemplateArgs)
203 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000204
205 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000206}
207
208DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
209 NestedNameSpecifier *Qualifier,
210 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000211 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000212 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000213 QualType T,
214 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000215 std::size_t Size = sizeof(DeclRefExpr);
216 if (Qualifier != 0)
217 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000218
John McCalld5532b62009-11-23 01:53:49 +0000219 if (TemplateArgs)
220 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000221
Douglas Gregora2813ce2009-10-23 18:54:35 +0000222 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
223 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000224 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000225}
226
227SourceRange DeclRefExpr::getSourceRange() const {
228 // FIXME: Does not handle multi-token names well, e.g., operator[].
229 SourceRange R(Loc);
Sean Huntc3021132010-05-05 15:23:54 +0000230
Douglas Gregora2813ce2009-10-23 18:54:35 +0000231 if (hasQualifier())
232 R.setBegin(getQualifierRange().getBegin());
233 if (hasExplicitTemplateArgumentList())
234 R.setEnd(getRAngleLoc());
235 return R;
236}
237
Anders Carlsson3a082d82009-09-08 18:24:21 +0000238// FIXME: Maybe this should use DeclPrinter with a special "print predefined
239// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000240std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
241 ASTContext &Context = CurrentDecl->getASTContext();
242
Anders Carlsson3a082d82009-09-08 18:24:21 +0000243 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000244 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000245 return FD->getNameAsString();
246
247 llvm::SmallString<256> Name;
248 llvm::raw_svector_ostream Out(Name);
249
250 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000251 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000252 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000253 if (MD->isStatic())
254 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000255 }
256
257 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000258
259 std::string Proto = FD->getQualifiedNameAsString(Policy);
260
John McCall183700f2009-09-21 23:43:11 +0000261 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000262 const FunctionProtoType *FT = 0;
263 if (FD->hasWrittenPrototype())
264 FT = dyn_cast<FunctionProtoType>(AFT);
265
266 Proto += "(";
267 if (FT) {
268 llvm::raw_string_ostream POut(Proto);
269 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
270 if (i) POut << ", ";
271 std::string Param;
272 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
273 POut << Param;
274 }
275
276 if (FT->isVariadic()) {
277 if (FD->getNumParams()) POut << ", ";
278 POut << "...";
279 }
280 }
281 Proto += ")";
282
Sam Weinig4eadcc52009-12-27 01:38:20 +0000283 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
284 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
285 if (ThisQuals.hasConst())
286 Proto += " const";
287 if (ThisQuals.hasVolatile())
288 Proto += " volatile";
289 }
290
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000291 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
292 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000293
294 Out << Proto;
295
296 Out.flush();
297 return Name.str().str();
298 }
299 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
300 llvm::SmallString<256> Name;
301 llvm::raw_svector_ostream Out(Name);
302 Out << (MD->isInstanceMethod() ? '-' : '+');
303 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000304
305 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
306 // a null check to avoid a crash.
307 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000308 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000309
Anders Carlsson3a082d82009-09-08 18:24:21 +0000310 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000311 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
312 Out << '(' << CID << ')';
313
Anders Carlsson3a082d82009-09-08 18:24:21 +0000314 Out << ' ';
315 Out << MD->getSelector().getAsString();
316 Out << ']';
317
318 Out.flush();
319 return Name.str().str();
320 }
321 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
322 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
323 return "top level";
324 }
325 return "";
326}
327
Chris Lattnerda8249e2008-06-07 22:13:43 +0000328/// getValueAsApproximateDouble - This returns the value as an inaccurate
329/// double. Note that this may cause loss of precision, but is useful for
330/// debugging dumps, etc.
331double FloatingLiteral::getValueAsApproximateDouble() const {
332 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000333 bool ignored;
334 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
335 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000336 return V.convertToDouble();
337}
338
Chris Lattner2085fd62009-02-18 06:40:38 +0000339StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
340 unsigned ByteLength, bool Wide,
341 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000342 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000343 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000344 // Allocate enough space for the StringLiteral plus an array of locations for
345 // any concatenated string tokens.
346 void *Mem = C.Allocate(sizeof(StringLiteral)+
347 sizeof(SourceLocation)*(NumStrs-1),
348 llvm::alignof<StringLiteral>());
349 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000352 char *AStrData = new (C, 1) char[ByteLength];
353 memcpy(AStrData, StrData, ByteLength);
354 SL->StrData = AStrData;
355 SL->ByteLength = ByteLength;
356 SL->IsWide = Wide;
357 SL->TokLocs[0] = Loc[0];
358 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000359
Chris Lattner726e1682009-02-18 05:49:11 +0000360 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000361 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
362 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000363}
364
Douglas Gregor673ecd62009-04-15 16:35:07 +0000365StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
366 void *Mem = C.Allocate(sizeof(StringLiteral)+
367 sizeof(SourceLocation)*(NumStrs-1),
368 llvm::alignof<StringLiteral>());
369 StringLiteral *SL = new (Mem) StringLiteral(QualType());
370 SL->StrData = 0;
371 SL->ByteLength = 0;
372 SL->NumConcatenated = NumStrs;
373 return SL;
374}
375
Douglas Gregor42602bb2009-08-07 06:08:38 +0000376void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000377 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000378 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000379}
380
Daniel Dunbarb6480232009-09-22 03:27:33 +0000381void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000382 if (StrData)
383 C.Deallocate(const_cast<char*>(StrData));
384
Daniel Dunbarb6480232009-09-22 03:27:33 +0000385 char *AStrData = new (C, 1) char[Str.size()];
386 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000387 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000388 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000389}
390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
392/// corresponds to, e.g. "sizeof" or "[pre]++".
393const char *UnaryOperator::getOpcodeStr(Opcode Op) {
394 switch (Op) {
395 default: assert(0 && "Unknown unary operator");
396 case PostInc: return "++";
397 case PostDec: return "--";
398 case PreInc: return "++";
399 case PreDec: return "--";
400 case AddrOf: return "&";
401 case Deref: return "*";
402 case Plus: return "+";
403 case Minus: return "-";
404 case Not: return "~";
405 case LNot: return "!";
406 case Real: return "__real";
407 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000409 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 }
411}
412
Mike Stump1eb44332009-09-09 15:08:12 +0000413UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000414UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
415 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000416 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000417 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
418 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
419 case OO_Amp: return AddrOf;
420 case OO_Star: return Deref;
421 case OO_Plus: return Plus;
422 case OO_Minus: return Minus;
423 case OO_Tilde: return Not;
424 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000425 }
426}
427
428OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
429 switch (Opc) {
430 case PostInc: case PreInc: return OO_PlusPlus;
431 case PostDec: case PreDec: return OO_MinusMinus;
432 case AddrOf: return OO_Amp;
433 case Deref: return OO_Star;
434 case Plus: return OO_Plus;
435 case Minus: return OO_Minus;
436 case Not: return OO_Tilde;
437 case LNot: return OO_Exclaim;
438 default: return OO_None;
439 }
440}
441
442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443//===----------------------------------------------------------------------===//
444// Postfix Operators.
445//===----------------------------------------------------------------------===//
446
Ted Kremenek668bf912009-02-09 20:51:47 +0000447CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000448 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000449 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000450 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000451 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000452 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremenek668bf912009-02-09 20:51:47 +0000454 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000455 SubExprs[FN] = fn;
456 for (unsigned i = 0; i != numargs; ++i)
457 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000458
Douglas Gregorb4609802008-11-14 16:09:21 +0000459 RParenLoc = rparenloc;
460}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000461
Ted Kremenek668bf912009-02-09 20:51:47 +0000462CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
463 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000464 : Expr(CallExprClass, t,
465 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000466 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000467 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000468
469 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000470 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000472 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000473
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 RParenLoc = rparenloc;
475}
476
Mike Stump1eb44332009-09-09 15:08:12 +0000477CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
478 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000479 SubExprs = new (C) Stmt*[1];
480}
481
Douglas Gregor42602bb2009-08-07 06:08:38 +0000482void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000483 DestroyChildren(C);
484 if (SubExprs) C.Deallocate(SubExprs);
485 this->~CallExpr();
486 C.Deallocate(this);
487}
488
Nuno Lopesd20254f2009-12-20 23:11:08 +0000489Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000490 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000491 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000492 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000493 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
494 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000495
496 return 0;
497}
498
Nuno Lopesd20254f2009-12-20 23:11:08 +0000499FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000500 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000501}
502
Chris Lattnerd18b3292007-12-28 05:25:02 +0000503/// setNumArgs - This changes the number of arguments present in this call.
504/// Any orphaned expressions are deleted by this, and any new operands are set
505/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000506void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000507 // No change, just return.
508 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Chris Lattnerd18b3292007-12-28 05:25:02 +0000510 // If shrinking # arguments, just delete the extras and forgot them.
511 if (NumArgs < getNumArgs()) {
512 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000513 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000514 this->NumArgs = NumArgs;
515 return;
516 }
517
518 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000519 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000520 // Copy over args.
521 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
522 NewSubExprs[i] = SubExprs[i];
523 // Null out new args.
524 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
525 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Douglas Gregor88c9a462009-04-17 21:46:47 +0000527 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000528 SubExprs = NewSubExprs;
529 this->NumArgs = NumArgs;
530}
531
Chris Lattnercb888962008-10-06 05:00:53 +0000532/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
533/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000534unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000535 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000536 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000537 // ImplicitCastExpr.
538 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
539 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000540 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000542 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
543 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000544 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Anders Carlssonbcba2012008-01-31 02:13:57 +0000546 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
547 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000548 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000550 if (!FDecl->getIdentifier())
551 return 0;
552
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000553 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000554}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000555
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000556QualType CallExpr::getCallReturnType() const {
557 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000558 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000559 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000560 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000561 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000562
John McCall183700f2009-09-21 23:43:11 +0000563 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000564 return FnType->getResultType();
565}
Chris Lattnercb888962008-10-06 05:00:53 +0000566
Sean Huntc3021132010-05-05 15:23:54 +0000567OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000568 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000569 TypeSourceInfo *tsi,
570 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000571 Expr** exprsPtr, unsigned numExprs,
572 SourceLocation RParenLoc) {
573 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000574 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000575 sizeof(Expr*) * numExprs);
576
577 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
578 exprsPtr, numExprs, RParenLoc);
579}
580
581OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
582 unsigned numComps, unsigned numExprs) {
583 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
584 sizeof(OffsetOfNode) * numComps +
585 sizeof(Expr*) * numExprs);
586 return new (Mem) OffsetOfExpr(numComps, numExprs);
587}
588
Sean Huntc3021132010-05-05 15:23:54 +0000589OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000590 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000591 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000592 Expr** exprsPtr, unsigned numExprs,
593 SourceLocation RParenLoc)
Sean Huntc3021132010-05-05 15:23:54 +0000594 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000595 /*ValueDependent=*/tsi->getType()->isDependentType() ||
596 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
597 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Sean Huntc3021132010-05-05 15:23:54 +0000598 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
599 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000600{
601 for(unsigned i = 0; i < numComps; ++i) {
602 setComponent(i, compsPtr[i]);
603 }
Sean Huntc3021132010-05-05 15:23:54 +0000604
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000605 for(unsigned i = 0; i < numExprs; ++i) {
606 setIndexExpr(i, exprsPtr[i]);
607 }
608}
609
610IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
611 assert(getKind() == Field || getKind() == Identifier);
612 if (getKind() == Field)
613 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000614
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000615 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
616}
617
Mike Stump1eb44332009-09-09 15:08:12 +0000618MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
619 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000620 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000621 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000622 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000623 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000624 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000625 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000626 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000627
John McCall161755a2010-04-06 21:38:20 +0000628 bool hasQualOrFound = (qual != 0 ||
629 founddecl.getDecl() != memberdecl ||
630 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000631 if (hasQualOrFound)
632 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
John McCalld5532b62009-11-23 01:53:49 +0000634 if (targs)
635 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000637 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000638 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
639
640 if (hasQualOrFound) {
641 if (qual && qual->isDependent()) {
642 E->setValueDependent(true);
643 E->setTypeDependent(true);
644 }
645 E->HasQualifierOrFoundDecl = true;
646
647 MemberNameQualifier *NQ = E->getMemberQualifier();
648 NQ->NNS = qual;
649 NQ->Range = qualrange;
650 NQ->FoundDecl = founddecl;
651 }
652
653 if (targs) {
654 E->HasExplicitTemplateArgumentList = true;
655 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
656 }
657
658 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000659}
660
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000661const char *CastExpr::getCastKindName() const {
662 switch (getCastKind()) {
663 case CastExpr::CK_Unknown:
664 return "Unknown";
665 case CastExpr::CK_BitCast:
666 return "BitCast";
667 case CastExpr::CK_NoOp:
668 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000669 case CastExpr::CK_BaseToDerived:
670 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000671 case CastExpr::CK_DerivedToBase:
672 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000673 case CastExpr::CK_UncheckedDerivedToBase:
674 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000675 case CastExpr::CK_Dynamic:
676 return "Dynamic";
677 case CastExpr::CK_ToUnion:
678 return "ToUnion";
679 case CastExpr::CK_ArrayToPointerDecay:
680 return "ArrayToPointerDecay";
681 case CastExpr::CK_FunctionToPointerDecay:
682 return "FunctionToPointerDecay";
683 case CastExpr::CK_NullToMemberPointer:
684 return "NullToMemberPointer";
685 case CastExpr::CK_BaseToDerivedMemberPointer:
686 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000687 case CastExpr::CK_DerivedToBaseMemberPointer:
688 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000689 case CastExpr::CK_UserDefinedConversion:
690 return "UserDefinedConversion";
691 case CastExpr::CK_ConstructorConversion:
692 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000693 case CastExpr::CK_IntegralToPointer:
694 return "IntegralToPointer";
695 case CastExpr::CK_PointerToIntegral:
696 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000697 case CastExpr::CK_ToVoid:
698 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000699 case CastExpr::CK_VectorSplat:
700 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000701 case CastExpr::CK_IntegralCast:
702 return "IntegralCast";
703 case CastExpr::CK_IntegralToFloating:
704 return "IntegralToFloating";
705 case CastExpr::CK_FloatingToIntegral:
706 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000707 case CastExpr::CK_FloatingCast:
708 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000709 case CastExpr::CK_MemberPointerToBoolean:
710 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000711 case CastExpr::CK_AnyPointerToObjCPointerCast:
712 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000713 case CastExpr::CK_AnyPointerToBlockPointerCast:
714 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000717 assert(0 && "Unhandled cast kind!");
718 return 0;
719}
720
Anders Carlssona3bdded2010-04-23 21:02:34 +0000721void CastExpr::DoDestroy(ASTContext &C)
722{
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000723 BasePath.Destroy();
Anders Carlssona3bdded2010-04-23 21:02:34 +0000724 Expr::DoDestroy(C);
725}
726
Douglas Gregor6eef5192009-12-14 19:27:10 +0000727Expr *CastExpr::getSubExprAsWritten() {
728 Expr *SubExpr = 0;
729 CastExpr *E = this;
730 do {
731 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000732
Douglas Gregor6eef5192009-12-14 19:27:10 +0000733 // Skip any temporary bindings; they're implicit.
734 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
735 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +0000736
Douglas Gregor6eef5192009-12-14 19:27:10 +0000737 // Conversions by constructor and conversion functions have a
738 // subexpression describing the call; strip it off.
739 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
740 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
741 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
742 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +0000743
Douglas Gregor6eef5192009-12-14 19:27:10 +0000744 // If the subexpression we're left with is an implicit cast, look
745 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +0000746 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
747
Douglas Gregor6eef5192009-12-14 19:27:10 +0000748 return SubExpr;
749}
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
752/// corresponds to, e.g. "<<=".
753const char *BinaryOperator::getOpcodeStr(Opcode Op) {
754 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000755 case PtrMemD: return ".*";
756 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 case Mul: return "*";
758 case Div: return "/";
759 case Rem: return "%";
760 case Add: return "+";
761 case Sub: return "-";
762 case Shl: return "<<";
763 case Shr: return ">>";
764 case LT: return "<";
765 case GT: return ">";
766 case LE: return "<=";
767 case GE: return ">=";
768 case EQ: return "==";
769 case NE: return "!=";
770 case And: return "&";
771 case Xor: return "^";
772 case Or: return "|";
773 case LAnd: return "&&";
774 case LOr: return "||";
775 case Assign: return "=";
776 case MulAssign: return "*=";
777 case DivAssign: return "/=";
778 case RemAssign: return "%=";
779 case AddAssign: return "+=";
780 case SubAssign: return "-=";
781 case ShlAssign: return "<<=";
782 case ShrAssign: return ">>=";
783 case AndAssign: return "&=";
784 case XorAssign: return "^=";
785 case OrAssign: return "|=";
786 case Comma: return ",";
787 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000788
789 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000790}
791
Mike Stump1eb44332009-09-09 15:08:12 +0000792BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000793BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
794 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000795 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000796 case OO_Plus: return Add;
797 case OO_Minus: return Sub;
798 case OO_Star: return Mul;
799 case OO_Slash: return Div;
800 case OO_Percent: return Rem;
801 case OO_Caret: return Xor;
802 case OO_Amp: return And;
803 case OO_Pipe: return Or;
804 case OO_Equal: return Assign;
805 case OO_Less: return LT;
806 case OO_Greater: return GT;
807 case OO_PlusEqual: return AddAssign;
808 case OO_MinusEqual: return SubAssign;
809 case OO_StarEqual: return MulAssign;
810 case OO_SlashEqual: return DivAssign;
811 case OO_PercentEqual: return RemAssign;
812 case OO_CaretEqual: return XorAssign;
813 case OO_AmpEqual: return AndAssign;
814 case OO_PipeEqual: return OrAssign;
815 case OO_LessLess: return Shl;
816 case OO_GreaterGreater: return Shr;
817 case OO_LessLessEqual: return ShlAssign;
818 case OO_GreaterGreaterEqual: return ShrAssign;
819 case OO_EqualEqual: return EQ;
820 case OO_ExclaimEqual: return NE;
821 case OO_LessEqual: return LE;
822 case OO_GreaterEqual: return GE;
823 case OO_AmpAmp: return LAnd;
824 case OO_PipePipe: return LOr;
825 case OO_Comma: return Comma;
826 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000827 }
828}
829
830OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
831 static const OverloadedOperatorKind OverOps[] = {
832 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
833 OO_Star, OO_Slash, OO_Percent,
834 OO_Plus, OO_Minus,
835 OO_LessLess, OO_GreaterGreater,
836 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
837 OO_EqualEqual, OO_ExclaimEqual,
838 OO_Amp,
839 OO_Caret,
840 OO_Pipe,
841 OO_AmpAmp,
842 OO_PipePipe,
843 OO_Equal, OO_StarEqual,
844 OO_SlashEqual, OO_PercentEqual,
845 OO_PlusEqual, OO_MinusEqual,
846 OO_LessLessEqual, OO_GreaterGreaterEqual,
847 OO_AmpEqual, OO_CaretEqual,
848 OO_PipeEqual,
849 OO_Comma
850 };
851 return OverOps[Opc];
852}
853
Ted Kremenek709210f2010-04-13 23:39:13 +0000854InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000855 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000856 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000857 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000858 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000859 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +0000860 UnionFieldInit(0), HadArrayRangeDesignator(false)
861{
Ted Kremenekba7bc552010-02-19 01:50:18 +0000862 for (unsigned I = 0; I != numInits; ++I) {
863 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000864 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000865 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000866 ValueDependent = true;
867 }
Sean Huntc3021132010-05-05 15:23:54 +0000868
Ted Kremenek709210f2010-04-13 23:39:13 +0000869 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000870}
Reid Spencer5f016e22007-07-11 17:01:13 +0000871
Ted Kremenek709210f2010-04-13 23:39:13 +0000872void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000873 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000874 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000875}
876
Ted Kremenek709210f2010-04-13 23:39:13 +0000877void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000878 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
879 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000880 InitExprs[Idx]->Destroy(C);
881 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000882}
883
Ted Kremenek709210f2010-04-13 23:39:13 +0000884Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000885 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000886 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000887 InitExprs.back() = expr;
888 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregor4c678342009-01-28 21:54:33 +0000891 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
892 InitExprs[Init] = expr;
893 return Result;
894}
895
Steve Naroffbfdcae62008-09-04 15:31:07 +0000896/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000897///
898const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000899 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000900 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000901}
902
Mike Stump1eb44332009-09-09 15:08:12 +0000903SourceLocation BlockExpr::getCaretLocation() const {
904 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000905}
Mike Stump1eb44332009-09-09 15:08:12 +0000906const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000907 return TheBlock->getBody();
908}
Mike Stump1eb44332009-09-09 15:08:12 +0000909Stmt *BlockExpr::getBody() {
910 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000911}
Steve Naroff56ee6892008-10-08 17:01:13 +0000912
913
Reid Spencer5f016e22007-07-11 17:01:13 +0000914//===----------------------------------------------------------------------===//
915// Generic Expression Routines
916//===----------------------------------------------------------------------===//
917
Chris Lattner026dc962009-02-14 07:37:35 +0000918/// isUnusedResultAWarning - Return true if this immediate expression should
919/// be warned about if the result is unused. If so, fill in Loc and Ranges
920/// with location to warn on and the source range[s] to report with the
921/// warning.
922bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000923 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000924 // Don't warn if the expr is type dependent. The type could end up
925 // instantiating to void.
926 if (isTypeDependent())
927 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 switch (getStmtClass()) {
930 default:
John McCall0faede62010-03-12 07:11:26 +0000931 if (getType()->isVoidType())
932 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000933 Loc = getExprLoc();
934 R1 = getSourceRange();
935 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000937 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000938 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 case UnaryOperatorClass: {
940 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000943 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 case UnaryOperator::PostInc:
945 case UnaryOperator::PostDec:
946 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000947 case UnaryOperator::PreDec: // ++/--
948 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 case UnaryOperator::Deref:
950 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000951 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000952 return false;
953 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 case UnaryOperator::Real:
955 case UnaryOperator::Imag:
956 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000957 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
958 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000959 return false;
960 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000962 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 }
Chris Lattner026dc962009-02-14 07:37:35 +0000964 Loc = UO->getOperatorLoc();
965 R1 = UO->getSubExpr()->getSourceRange();
966 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000968 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000969 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000970 switch (BO->getOpcode()) {
971 default:
972 break;
973 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
974 case BinaryOperator::Comma:
975 // ((foo = <blah>), 0) is an idiom for hiding the result (and
976 // lvalue-ness) of an assignment written in a macro.
977 if (IntegerLiteral *IE =
978 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
979 if (IE->getValue() == 0)
980 return false;
981 case BinaryOperator::LAnd:
982 case BinaryOperator::LOr:
983 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
984 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000985 }
Chris Lattner026dc962009-02-14 07:37:35 +0000986 if (BO->isAssignmentOp())
987 return false;
988 Loc = BO->getOperatorLoc();
989 R1 = BO->getLHS()->getSourceRange();
990 R2 = BO->getRHS()->getSourceRange();
991 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000992 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000993 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +0000994 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000995 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000996
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000997 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000998 // The condition must be evaluated, but if either the LHS or RHS is a
999 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001000 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001001 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001002 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001003 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001004 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001005 }
1006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001008 // If the base pointer or element is to a volatile pointer/field, accessing
1009 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001010 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001011 return false;
1012 Loc = cast<MemberExpr>(this)->getMemberLoc();
1013 R1 = SourceRange(Loc, Loc);
1014 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1015 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 case ArraySubscriptExprClass:
1018 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001019 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001020 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001021 return false;
1022 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1023 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1024 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1025 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001026
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001028 case CXXOperatorCallExprClass:
1029 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001030 // If this is a direct call, get the callee.
1031 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001032 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001033 // If the callee has attribute pure, const, or warn_unused_result, warn
1034 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001035 //
1036 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1037 // updated to match for QoI.
1038 if (FD->getAttr<WarnUnusedResultAttr>() ||
1039 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1040 Loc = CE->getCallee()->getLocStart();
1041 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001043 if (unsigned NumArgs = CE->getNumArgs())
1044 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1045 CE->getArg(NumArgs-1)->getLocEnd());
1046 return true;
1047 }
Chris Lattner026dc962009-02-14 07:37:35 +00001048 }
1049 return false;
1050 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001051
1052 case CXXTemporaryObjectExprClass:
1053 case CXXConstructExprClass:
1054 return false;
1055
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001056 case ObjCMessageExprClass: {
1057 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1058 const ObjCMethodDecl *MD = ME->getMethodDecl();
1059 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1060 Loc = getExprLoc();
1061 return true;
1062 }
Chris Lattner026dc962009-02-14 07:37:35 +00001063 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001064 }
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001066 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001067#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001068 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001069 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001070 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001071 Loc = Ref->getLocation();
1072 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1073 if (Ref->getBase())
1074 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001075#else
1076 Loc = getExprLoc();
1077 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001078#endif
1079 return true;
1080 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001081 case StmtExprClass: {
1082 // Statement exprs don't logically have side effects themselves, but are
1083 // sometimes used in macros in ways that give them a type that is unused.
1084 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1085 // however, if the result of the stmt expr is dead, we don't want to emit a
1086 // warning.
1087 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1088 if (!CS->body_empty())
1089 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001090 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
John McCall0faede62010-03-12 07:11:26 +00001092 if (getType()->isVoidType())
1093 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001094 Loc = cast<StmtExpr>(this)->getLParenLoc();
1095 R1 = getSourceRange();
1096 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001097 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001098 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001099 // If this is an explicit cast to void, allow it. People do this when they
1100 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001101 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001102 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001103 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1104 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1105 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001106 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001107 if (getType()->isVoidType())
1108 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001109 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001110
Anders Carlsson58beed92009-11-17 17:11:23 +00001111 // If this is a cast to void or a constructor conversion, check the operand.
1112 // Otherwise, the result of the cast is unused.
1113 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1114 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001115 return (cast<CastExpr>(this)->getSubExpr()
1116 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001117 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1118 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1119 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Eli Friedman4be1f472008-05-19 21:24:43 +00001122 case ImplicitCastExprClass:
1123 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001124 return (cast<ImplicitCastExpr>(this)
1125 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001126
Chris Lattner04421082008-04-08 04:40:51 +00001127 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001128 return (cast<CXXDefaultArgExpr>(this)
1129 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001130
1131 case CXXNewExprClass:
1132 // FIXME: In theory, there might be new expressions that don't have side
1133 // effects (e.g. a placement new with an uninitialized POD).
1134 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001135 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001136 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001137 return (cast<CXXBindTemporaryExpr>(this)
1138 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001139 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001140 return (cast<CXXExprWithTemporaries>(this)
1141 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001142 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001143}
1144
Douglas Gregorba7e2102008-10-22 15:04:37 +00001145/// DeclCanBeLvalue - Determine whether the given declaration can be
1146/// an lvalue. This is a helper routine for isLvalue.
1147static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001148 // C++ [temp.param]p6:
1149 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001150 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001151 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1152 return NTTParm->getType()->isReferenceType();
1153
Douglas Gregor44b43212008-12-11 16:49:14 +00001154 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001155 // C++ 3.10p2: An lvalue refers to an object or function.
1156 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001157 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001158}
1159
Reid Spencer5f016e22007-07-11 17:01:13 +00001160/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1161/// incomplete type other than void. Nonarray expressions that can be lvalues:
1162/// - name, where name must be a variable
1163/// - e[i]
1164/// - (e), where e must be an lvalue
1165/// - e.name, where e must be an lvalue
1166/// - e->name
1167/// - *e, the type of e cannot be a function type
1168/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001169/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001170/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001171///
Chris Lattner28be73f2008-07-26 21:30:36 +00001172Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001173 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1174
1175 isLvalueResult Res = isLvalueInternal(Ctx);
1176 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1177 return Res;
1178
Douglas Gregor98cd5992008-10-21 23:43:52 +00001179 // first, check the type (C99 6.3.2.1). Expressions with function
1180 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001181 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 return LV_NotObjectType;
1183
Steve Naroffacb818a2008-02-10 01:39:04 +00001184 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001185 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001186 return LV_IncompleteVoidType;
1187
Eli Friedman53202852009-05-03 22:36:05 +00001188 return LV_Valid;
1189}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001190
Eli Friedman53202852009-05-03 22:36:05 +00001191// Check whether the expression can be sanely treated like an l-value
1192Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001194 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001195 case StringLiteralClass: // C99 6.5.1p4
1196 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001197 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1199 // For vectors, make sure base is an lvalue (i.e. not a function call).
1200 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001201 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001203 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001204 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1205 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 return LV_Valid;
1207 break;
Chris Lattner41110242008-06-17 18:05:57 +00001208 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001209 case BlockDeclRefExprClass: {
1210 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001211 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001212 return LV_Valid;
1213 break;
1214 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001215 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001217 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1218 NamedDecl *Member = m->getMemberDecl();
1219 // C++ [expr.ref]p4:
1220 // If E2 is declared to have type "reference to T", then E1.E2
1221 // is an lvalue.
1222 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1223 if (Value->getType()->isReferenceType())
1224 return LV_Valid;
1225
1226 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001227 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001228 return LV_Valid;
1229
1230 // -- If E2 is a non-static data member [...]. If E1 is an
1231 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001232 if (isa<FieldDecl>(Member)) {
1233 if (m->isArrow())
1234 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001235 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001236 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001237
1238 // -- If it refers to a static member function [...], then
1239 // E1.E2 is an lvalue.
1240 // -- Otherwise, if E1.E2 refers to a non-static member
1241 // function [...], then E1.E2 is not an lvalue.
1242 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1243 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1244
1245 // -- If E2 is a member enumerator [...], the expression E1.E2
1246 // is not an lvalue.
1247 if (isa<EnumConstantDecl>(Member))
1248 return LV_InvalidExpression;
1249
1250 // Not an lvalue.
1251 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001252 }
Sean Huntc3021132010-05-05 15:23:54 +00001253
Douglas Gregor86f19402008-12-20 23:49:58 +00001254 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001255 if (m->isArrow())
1256 return LV_Valid;
1257 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001258 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1259 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001260 return LV_SubObjCPropertySetting;
Sean Huntc3021132010-05-05 15:23:54 +00001261 return
1262 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001263 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001264 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001266 return LV_Valid; // C99 6.5.3p4
1267
1268 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001269 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1270 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001271 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001272
1273 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1274 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1275 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1276 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001278 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001279 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1280 return LV_Valid;
1281
1282 // If this is a conversion to a class temporary, make a note of
1283 // that.
1284 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1285 return LV_ClassTemporary;
1286
1287 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001289 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001290 case BinaryOperatorClass:
1291 case CompoundAssignOperatorClass: {
1292 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001293
1294 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1295 BinOp->getOpcode() == BinaryOperator::Comma)
1296 return BinOp->getRHS()->isLvalue(Ctx);
1297
Sebastian Redl22460502009-02-07 00:15:38 +00001298 // C++ [expr.mptr.oper]p6
Sean Huntc3021132010-05-05 15:23:54 +00001299 // The result of a .* expression is an lvalue only if its first operand is
1300 // an lvalue and its second operand is a pointer to data member.
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001301 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001302 !BinOp->getType()->isFunctionType())
1303 return BinOp->getLHS()->isLvalue(Ctx);
1304
Sean Huntc3021132010-05-05 15:23:54 +00001305 // The result of an ->* expression is an lvalue only if its second operand
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001306 // is a pointer to data member.
1307 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1308 !BinOp->getType()->isFunctionType()) {
1309 QualType Ty = BinOp->getRHS()->getType();
1310 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1311 return LV_Valid;
1312 }
Sean Huntc3021132010-05-05 15:23:54 +00001313
Douglas Gregorbf3af052008-11-13 20:12:29 +00001314 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001315 return LV_InvalidExpression;
1316
Douglas Gregorbf3af052008-11-13 20:12:29 +00001317 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001318 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001319 // The result of an assignment operation [...] is an lvalue.
1320 return LV_Valid;
1321
1322
1323 // C99 6.5.16:
1324 // An assignment expression [...] is not an lvalue.
1325 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001328 case CXXOperatorCallExprClass:
1329 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001330 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001331 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001332 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001333 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1334 if (ReturnType->isLValueReferenceType())
1335 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001336
Douglas Gregore873fb72010-02-16 21:39:57 +00001337 // If the function is returning a class temporary, make a note of
1338 // that.
1339 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1340 return LV_ClassTemporary;
1341
Douglas Gregor9d293df2008-10-28 00:22:11 +00001342 break;
1343 }
Steve Naroffe6386392007-12-05 04:00:10 +00001344 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001345 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001346 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001347 case ChooseExprClass:
1348 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001349 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001350 case ExtVectorElementExprClass:
1351 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001352 return LV_DuplicateVectorComponents;
1353 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001354 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1355 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001356 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1357 return LV_Valid;
Enea Zaffanella049c51e2010-04-27 07:38:32 +00001358 case ObjCImplicitSetterGetterRefExprClass:
1359 // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001360 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001361 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001362 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001363 case UnresolvedLookupExprClass:
Douglas Gregorad4e02f2010-04-29 18:24:40 +00001364 case UnresolvedMemberExprClass:
John McCallba135432009-11-21 08:51:07 +00001365 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001366 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001367 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001368 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001369 case CXXFunctionalCastExprClass:
1370 case CXXStaticCastExprClass:
1371 case CXXDynamicCastExprClass:
1372 case CXXReinterpretCastExprClass:
1373 case CXXConstCastExprClass:
1374 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001375 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001376 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1377 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001378 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1379 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001380 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001381
1382 // If this is a conversion to a class temporary, make a note of
1383 // that.
Sean Huntc3021132010-05-05 15:23:54 +00001384 if (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregore873fb72010-02-16 21:39:57 +00001385 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1386 return LV_ClassTemporary;
1387
Douglas Gregor9d293df2008-10-28 00:22:11 +00001388 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001389 case CXXTypeidExprClass:
1390 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1391 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001392 case CXXBindTemporaryExprClass:
1393 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1394 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001395 case CXXBindReferenceExprClass:
1396 // Something that's bound to a reference is always an lvalue.
1397 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001398 case ConditionalOperatorClass: {
1399 // Complicated handling is only for C++.
1400 if (!Ctx.getLangOptions().CPlusPlus)
1401 return LV_InvalidExpression;
1402
1403 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1404 // everywhere there's an object converted to an rvalue. Also, any other
1405 // casts should be wrapped by ImplicitCastExprs. There's just the special
1406 // case involving throws to work out.
1407 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001408 Expr *True = Cond->getTrueExpr();
1409 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001410 // C++0x 5.16p2
1411 // If either the second or the third operand has type (cv) void, [...]
1412 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001413 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001414 return LV_InvalidExpression;
1415
1416 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001417 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001418 return LV_InvalidExpression;
1419
1420 // That's it.
1421 return LV_Valid;
1422 }
1423
Douglas Gregor2d48e782009-12-19 07:07:47 +00001424 case Expr::CXXExprWithTemporariesClass:
1425 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1426
1427 case Expr::ObjCMessageExprClass:
1428 if (const ObjCMethodDecl *Method
1429 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1430 if (Method->getResultType()->isLValueReferenceType())
1431 return LV_Valid;
1432 break;
1433
Douglas Gregore873fb72010-02-16 21:39:57 +00001434 case Expr::CXXConstructExprClass:
1435 case Expr::CXXTemporaryObjectExprClass:
1436 case Expr::CXXZeroInitValueExprClass:
1437 return LV_ClassTemporary;
1438
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 default:
1440 break;
1441 }
1442 return LV_InvalidExpression;
1443}
1444
1445/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1446/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001447/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001448/// recursively, any member or element of all contained aggregates or unions)
1449/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001450Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001451Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001452 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001453
Reid Spencer5f016e22007-07-11 17:01:13 +00001454 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001455 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001456 // C++ 3.10p11: Functions cannot be modified, but pointers to
1457 // functions can be modifiable.
1458 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1459 return MLV_NotObjectType;
1460 break;
1461
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 case LV_NotObjectType: return MLV_NotObjectType;
1463 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001464 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001465 case LV_InvalidExpression:
1466 // If the top level is a C-style cast, and the subexpression is a valid
1467 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1468 // GCC extension. We don't support it, but we want to produce good
1469 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001470 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1471 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1472 if (Loc)
1473 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001474 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001475 }
1476 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001477 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001478 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001479 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001480 case LV_ClassTemporary:
1481 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001483
1484 // The following is illegal:
1485 // void takeclosure(void (^C)(void));
1486 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1487 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001488 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001489 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1490 return MLV_NotBlockQualified;
1491 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001492
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001493 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001494 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001495 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1496 if (Expr->getSetterMethod() == 0)
1497 return MLV_NoSetterProperty;
1498 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001499
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001500 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001502 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001504 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001506 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Ted Kremenek6217b802009-07-29 21:53:49 +00001509 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001510 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 return MLV_ConstQualified;
1512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Mike Stump1eb44332009-09-09 15:08:12 +00001514 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001515}
1516
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001517/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001518/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001519bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001520 switch (getStmtClass()) {
1521 default:
1522 return false;
1523 case ObjCIvarRefExprClass:
1524 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001525 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001526 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001527 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001528 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001529 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001530 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001531 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001532 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001533 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001534 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001535 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1536 if (VD->hasGlobalStorage())
1537 return true;
1538 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001539 // dereferencing to a pointer is always a gc'able candidate,
1540 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001541 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001542 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001543 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001544 return false;
1545 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001546 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001547 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001548 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001549 }
1550 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001551 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001552 }
1553}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001554Expr* Expr::IgnoreParens() {
1555 Expr* E = this;
1556 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1557 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001559 return E;
1560}
1561
Chris Lattner56f34942008-02-13 01:02:39 +00001562/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1563/// or CastExprs or ImplicitCastExprs, returning their operand.
1564Expr *Expr::IgnoreParenCasts() {
1565 Expr *E = this;
1566 while (true) {
1567 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1568 E = P->getSubExpr();
1569 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1570 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001571 else
1572 return E;
1573 }
1574}
1575
John McCall2fc46bf2010-05-05 22:59:52 +00001576Expr *Expr::IgnoreParenImpCasts() {
1577 Expr *E = this;
1578 while (true) {
1579 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1580 E = P->getSubExpr();
1581 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1582 E = P->getSubExpr();
1583 else
1584 return E;
1585 }
1586}
1587
Chris Lattnerecdd8412009-03-13 17:28:01 +00001588/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1589/// value (including ptr->int casts of the same size). Strip off any
1590/// ParenExpr or CastExprs, returning their operand.
1591Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1592 Expr *E = this;
1593 while (true) {
1594 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1595 E = P->getSubExpr();
1596 continue;
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Chris Lattnerecdd8412009-03-13 17:28:01 +00001599 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1600 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001601 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001602 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Chris Lattnerecdd8412009-03-13 17:28:01 +00001604 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1605 E = SE;
1606 continue;
1607 }
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001609 if ((E->getType()->isPointerType() ||
1610 E->getType()->isIntegralType(Ctx)) &&
1611 (SE->getType()->isPointerType() ||
1612 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001613 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1614 E = SE;
1615 continue;
1616 }
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Chris Lattnerecdd8412009-03-13 17:28:01 +00001619 return E;
1620 }
1621}
1622
Douglas Gregor6eef5192009-12-14 19:27:10 +00001623bool Expr::isDefaultArgument() const {
1624 const Expr *E = this;
1625 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1626 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001627
Douglas Gregor6eef5192009-12-14 19:27:10 +00001628 return isa<CXXDefaultArgExpr>(E);
1629}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001630
Douglas Gregor2f599792010-04-02 18:24:57 +00001631/// \brief Skip over any no-op casts and any temporary-binding
1632/// expressions.
1633static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1634 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1635 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1636 E = ICE->getSubExpr();
1637 else
1638 break;
1639 }
1640
1641 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1642 E = BE->getSubExpr();
1643
1644 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1645 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1646 E = ICE->getSubExpr();
1647 else
1648 break;
1649 }
Sean Huntc3021132010-05-05 15:23:54 +00001650
Douglas Gregor2f599792010-04-02 18:24:57 +00001651 return E;
1652}
1653
1654const Expr *Expr::getTemporaryObject() const {
1655 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1656
1657 // A cast can produce a temporary object. The object's construction
1658 // is represented as a CXXConstructExpr.
1659 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1660 // Only user-defined and constructor conversions can produce
1661 // temporary objects.
1662 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1663 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1664 return 0;
1665
1666 // Strip off temporary bindings and no-op casts.
1667 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1668
1669 // If this is a constructor conversion, see if we have an object
1670 // construction.
1671 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1672 return dyn_cast<CXXConstructExpr>(Sub);
1673
1674 // If this is a user-defined conversion, see if we have a call to
1675 // a function that itself returns a temporary object.
1676 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1677 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1678 if (CE->getCallReturnType()->isRecordType())
1679 return CE;
1680
1681 return 0;
1682 }
1683
1684 // A call returning a class type returns a temporary.
1685 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1686 if (CE->getCallReturnType()->isRecordType())
1687 return CE;
1688
1689 return 0;
1690 }
1691
1692 // Explicit temporary object constructors create temporaries.
1693 return dyn_cast<CXXTemporaryObjectExpr>(E);
1694}
1695
Douglas Gregor898574e2008-12-05 23:32:09 +00001696/// hasAnyTypeDependentArguments - Determines if any of the expressions
1697/// in Exprs is type-dependent.
1698bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1699 for (unsigned I = 0; I < NumExprs; ++I)
1700 if (Exprs[I]->isTypeDependent())
1701 return true;
1702
1703 return false;
1704}
1705
1706/// hasAnyValueDependentArguments - Determines if any of the expressions
1707/// in Exprs is value-dependent.
1708bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1709 for (unsigned I = 0; I < NumExprs; ++I)
1710 if (Exprs[I]->isValueDependent())
1711 return true;
1712
1713 return false;
1714}
1715
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001716bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001717 // This function is attempting whether an expression is an initializer
1718 // which can be evaluated at compile-time. isEvaluatable handles most
1719 // of the cases, but it can't deal with some initializer-specific
1720 // expressions, and it can't deal with aggregates; we deal with those here,
1721 // and fall back to isEvaluatable for the other cases.
1722
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001723 // FIXME: This function assumes the variable being assigned to
1724 // isn't a reference type!
1725
Anders Carlssone8a32b82008-11-24 05:23:59 +00001726 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001727 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001728 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001729 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001730 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001731 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001732 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001733 // This handles gcc's extension that allows global initializers like
1734 // "struct x {int x;} x = (struct x) {};".
1735 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001736 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001737 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001738 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001739 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001740 // FIXME: This doesn't deal with fields with reference types correctly.
1741 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1742 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001743 const InitListExpr *Exp = cast<InitListExpr>(this);
1744 unsigned numInits = Exp->getNumInits();
1745 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001746 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001747 return false;
1748 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001749 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001750 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001751 case ImplicitValueInitExprClass:
1752 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001753 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001754 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001755 case UnaryOperatorClass: {
1756 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1757 if (Exp->getOpcode() == UnaryOperator::Extension)
1758 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1759 break;
1760 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001761 case BinaryOperatorClass: {
1762 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1763 // but this handles the common case.
1764 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1765 if (Exp->getOpcode() == BinaryOperator::Sub &&
1766 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1767 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1768 return true;
1769 break;
1770 }
Chris Lattner81045d82009-04-21 05:19:11 +00001771 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001772 case CStyleCastExprClass:
1773 // Handle casts with a destination that's a struct or union; this
1774 // deals with both the gcc no-op struct cast extension and the
1775 // cast-to-union extension.
1776 if (getType()->isRecordType())
1777 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001778
Chris Lattner430656e2009-10-13 22:12:09 +00001779 // Integer->integer casts can be handled here, which is important for
1780 // things like (int)(&&x-&&y). Scary but true.
1781 if (getType()->isIntegerType() &&
1782 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1783 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001784
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001785 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001786 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001787 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001788}
1789
Reid Spencer5f016e22007-07-11 17:01:13 +00001790/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1791/// integer constant expression with the value zero, or if this is one that is
1792/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001793bool Expr::isNullPointerConstant(ASTContext &Ctx,
1794 NullPointerConstantValueDependence NPC) const {
1795 if (isValueDependent()) {
1796 switch (NPC) {
1797 case NPC_NeverValueDependent:
1798 assert(false && "Unexpected value dependent expression!");
1799 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00001800
Douglas Gregorce940492009-09-25 04:25:58 +00001801 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001802 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00001803
Douglas Gregorce940492009-09-25 04:25:58 +00001804 case NPC_ValueDependentIsNotNull:
1805 return false;
1806 }
1807 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001808
Sebastian Redl07779722008-10-31 14:43:28 +00001809 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001810 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001811 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001812 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001813 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001814 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001815 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001816 Pointee->isVoidType() && // to void*
1817 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001818 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001819 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001821 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1822 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001823 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001824 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1825 // Accept ((void*)0) as a null pointer constant, as many other
1826 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001827 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001828 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001829 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001830 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001831 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001832 } else if (isa<GNUNullExpr>(this)) {
1833 // The GNU __null extension is always a null pointer constant.
1834 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001835 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001836
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001837 // C++0x nullptr_t is always a null pointer constant.
1838 if (getType()->isNullPtrType())
1839 return true;
1840
Steve Naroffaa58f002008-01-14 16:10:57 +00001841 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00001842 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001843 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001844 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 // If we have an integer constant expression, we need to *evaluate* it and
1847 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001848 llvm::APSInt Result;
1849 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001850}
Steve Naroff31a45842007-07-28 23:10:27 +00001851
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001852FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001853 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001854
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001855 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1856 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1857 E = ICE->getSubExpr()->IgnoreParens();
1858 else
1859 break;
1860 }
1861
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001862 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001863 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001864 if (Field->isBitField())
1865 return Field;
1866
1867 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1868 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1869 return BinOp->getLHS()->getBitField();
1870
1871 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001872}
1873
Anders Carlsson09380262010-01-31 17:18:49 +00001874bool Expr::refersToVectorElement() const {
1875 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00001876
Anders Carlsson09380262010-01-31 17:18:49 +00001877 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1878 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1879 E = ICE->getSubExpr()->IgnoreParens();
1880 else
1881 break;
1882 }
Sean Huntc3021132010-05-05 15:23:54 +00001883
Anders Carlsson09380262010-01-31 17:18:49 +00001884 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1885 return ASE->getBase()->getType()->isVectorType();
1886
1887 if (isa<ExtVectorElementExpr>(E))
1888 return true;
1889
1890 return false;
1891}
1892
Chris Lattner2140e902009-02-16 22:14:05 +00001893/// isArrow - Return true if the base expression is a pointer to vector,
1894/// return false if the base expression is a vector.
1895bool ExtVectorElementExpr::isArrow() const {
1896 return getBase()->getType()->isPointerType();
1897}
1898
Nate Begeman213541a2008-04-18 23:10:10 +00001899unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001900 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001901 return VT->getNumElements();
1902 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001903}
1904
Nate Begeman8a997642008-05-09 06:41:27 +00001905/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001906bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001907 // FIXME: Refactor this code to an accessor on the AST node which returns the
1908 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001909 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001910
1911 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001912 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001913 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Nate Begeman190d6a22009-01-18 02:01:21 +00001915 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001916 if (Comp[0] == 's' || Comp[0] == 'S')
1917 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Daniel Dunbar15027422009-10-17 23:53:04 +00001919 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1920 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001921 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001922
Steve Narofffec0b492007-07-30 03:29:09 +00001923 return false;
1924}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001925
Nate Begeman8a997642008-05-09 06:41:27 +00001926/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001927void ExtVectorElementExpr::getEncodedElementAccess(
1928 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001929 llvm::StringRef Comp = Accessor->getName();
1930 if (Comp[0] == 's' || Comp[0] == 'S')
1931 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001933 bool isHi = Comp == "hi";
1934 bool isLo = Comp == "lo";
1935 bool isEven = Comp == "even";
1936 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Nate Begeman8a997642008-05-09 06:41:27 +00001938 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1939 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Nate Begeman8a997642008-05-09 06:41:27 +00001941 if (isHi)
1942 Index = e + i;
1943 else if (isLo)
1944 Index = i;
1945 else if (isEven)
1946 Index = 2 * i;
1947 else if (isOdd)
1948 Index = 2 * i + 1;
1949 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001950 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001951
Nate Begeman3b8d1162008-05-13 21:03:02 +00001952 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001953 }
Nate Begeman8a997642008-05-09 06:41:27 +00001954}
1955
Douglas Gregor04badcf2010-04-21 00:45:42 +00001956ObjCMessageExpr::ObjCMessageExpr(QualType T,
1957 SourceLocation LBracLoc,
1958 SourceLocation SuperLoc,
1959 bool IsInstanceSuper,
1960 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00001961 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001962 ObjCMethodDecl *Method,
1963 Expr **Args, unsigned NumArgs,
1964 SourceLocation RBracLoc)
1965 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregor92e986e2010-04-22 16:44:27 +00001966 /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00001967 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1968 HasMethod(Method != 0), SuperLoc(SuperLoc),
1969 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1970 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00001971 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00001972{
Douglas Gregor04badcf2010-04-21 00:45:42 +00001973 setReceiverPointer(SuperType.getAsOpaquePtr());
1974 if (NumArgs)
1975 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00001976}
1977
Douglas Gregor04badcf2010-04-21 00:45:42 +00001978ObjCMessageExpr::ObjCMessageExpr(QualType T,
1979 SourceLocation LBracLoc,
1980 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00001981 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00001982 ObjCMethodDecl *Method,
1983 Expr **Args, unsigned NumArgs,
1984 SourceLocation RBracLoc)
1985 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Sean Huntc3021132010-05-05 15:23:54 +00001986 (T->isDependentType() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00001987 hasAnyValueDependentArguments(Args, NumArgs))),
1988 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1989 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1990 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00001991 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00001992{
1993 setReceiverPointer(Receiver);
1994 if (NumArgs)
1995 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00001996}
1997
Douglas Gregor04badcf2010-04-21 00:45:42 +00001998ObjCMessageExpr::ObjCMessageExpr(QualType T,
1999 SourceLocation LBracLoc,
2000 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002001 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002002 ObjCMethodDecl *Method,
2003 Expr **Args, unsigned NumArgs,
2004 SourceLocation RBracLoc)
Douglas Gregor92e986e2010-04-22 16:44:27 +00002005 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Sean Huntc3021132010-05-05 15:23:54 +00002006 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002007 hasAnyValueDependentArguments(Args, NumArgs))),
2008 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2009 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2010 : Sel.getAsOpaquePtr())),
Sean Huntc3021132010-05-05 15:23:54 +00002011 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002012{
2013 setReceiverPointer(Receiver);
2014 if (NumArgs)
2015 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002016}
2017
Douglas Gregor04badcf2010-04-21 00:45:42 +00002018ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2019 SourceLocation LBracLoc,
2020 SourceLocation SuperLoc,
2021 bool IsInstanceSuper,
2022 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002023 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002024 ObjCMethodDecl *Method,
2025 Expr **Args, unsigned NumArgs,
2026 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002027 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002028 NumArgs * sizeof(Expr *);
2029 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2030 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Sean Huntc3021132010-05-05 15:23:54 +00002031 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002032 RBracLoc);
2033}
2034
2035ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2036 SourceLocation LBracLoc,
2037 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002038 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002039 ObjCMethodDecl *Method,
2040 Expr **Args, unsigned NumArgs,
2041 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002042 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002043 NumArgs * sizeof(Expr *);
2044 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00002045 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002046 NumArgs, RBracLoc);
2047}
2048
2049ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2050 SourceLocation LBracLoc,
2051 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002052 Selector Sel,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002053 ObjCMethodDecl *Method,
2054 Expr **Args, unsigned NumArgs,
2055 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002056 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002057 NumArgs * sizeof(Expr *);
2058 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Sean Huntc3021132010-05-05 15:23:54 +00002059 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002060 NumArgs, RBracLoc);
2061}
2062
Sean Huntc3021132010-05-05 15:23:54 +00002063ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002064 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002065 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002066 NumArgs * sizeof(Expr *);
2067 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2068 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2069}
Sean Huntc3021132010-05-05 15:23:54 +00002070
Douglas Gregor04badcf2010-04-21 00:45:42 +00002071Selector ObjCMessageExpr::getSelector() const {
2072 if (HasMethod)
2073 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2074 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002075 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002076}
2077
2078ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2079 switch (getReceiverKind()) {
2080 case Instance:
2081 if (const ObjCObjectPointerType *Ptr
2082 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2083 return Ptr->getInterfaceDecl();
2084 break;
2085
2086 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002087 if (const ObjCObjectType *Ty
2088 = getClassReceiver()->getAs<ObjCObjectType>())
2089 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002090 break;
2091
2092 case SuperInstance:
2093 if (const ObjCObjectPointerType *Ptr
2094 = getSuperType()->getAs<ObjCObjectPointerType>())
2095 return Ptr->getInterfaceDecl();
2096 break;
2097
2098 case SuperClass:
2099 if (const ObjCObjectPointerType *Iface
2100 = getSuperType()->getAs<ObjCObjectPointerType>())
2101 return Iface->getInterfaceDecl();
2102 break;
2103 }
2104
2105 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002106}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002107
Chris Lattner27437ca2007-10-25 00:29:32 +00002108bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002109 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002110}
2111
Nate Begeman888376a2009-08-12 02:28:50 +00002112void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2113 unsigned NumExprs) {
2114 if (SubExprs) C.Deallocate(SubExprs);
2115
2116 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002117 this->NumExprs = NumExprs;
2118 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002119}
Nate Begeman888376a2009-08-12 02:28:50 +00002120
2121void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2122 DestroyChildren(C);
2123 if (SubExprs) C.Deallocate(SubExprs);
2124 this->~ShuffleVectorExpr();
2125 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002126}
2127
Douglas Gregor42602bb2009-08-07 06:08:38 +00002128void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002129 // Override default behavior of traversing children. If this has a type
2130 // operand and the type is a variable-length array, the child iteration
2131 // will iterate over the size expression. However, this expression belongs
2132 // to the type, not to this, so we don't want to delete it.
2133 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002134 if (isArgumentType()) {
2135 this->~SizeOfAlignOfExpr();
2136 C.Deallocate(this);
2137 }
Sebastian Redl05189992008-11-11 17:56:53 +00002138 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002139 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002140}
2141
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002142//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002143// DesignatedInitExpr
2144//===----------------------------------------------------------------------===//
2145
2146IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2147 assert(Kind == FieldDesignator && "Only valid on a field designator");
2148 if (Field.NameOrField & 0x01)
2149 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2150 else
2151 return getField()->getIdentifier();
2152}
2153
Sean Huntc3021132010-05-05 15:23:54 +00002154DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002155 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002156 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002157 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002158 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002159 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002160 unsigned NumIndexExprs,
2161 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002162 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002163 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002164 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2165 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002166 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002167
2168 // Record the initializer itself.
2169 child_iterator Child = child_begin();
2170 *Child++ = Init;
2171
2172 // Copy the designators and their subexpressions, computing
2173 // value-dependence along the way.
2174 unsigned IndexIdx = 0;
2175 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002176 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002177
2178 if (this->Designators[I].isArrayDesignator()) {
2179 // Compute type- and value-dependence.
2180 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002181 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002182 Index->isTypeDependent() || Index->isValueDependent();
2183
2184 // Copy the index expressions into permanent storage.
2185 *Child++ = IndexExprs[IndexIdx++];
2186 } else if (this->Designators[I].isArrayRangeDesignator()) {
2187 // Compute type- and value-dependence.
2188 Expr *Start = IndexExprs[IndexIdx];
2189 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002190 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002191 Start->isTypeDependent() || Start->isValueDependent() ||
2192 End->isTypeDependent() || End->isValueDependent();
2193
2194 // Copy the start/end expressions into permanent storage.
2195 *Child++ = IndexExprs[IndexIdx++];
2196 *Child++ = IndexExprs[IndexIdx++];
2197 }
2198 }
2199
2200 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002201}
2202
Douglas Gregor05c13a32009-01-22 00:58:24 +00002203DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002204DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002205 unsigned NumDesignators,
2206 Expr **IndexExprs, unsigned NumIndexExprs,
2207 SourceLocation ColonOrEqualLoc,
2208 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002209 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002210 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002211 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002212 ColonOrEqualLoc, UsesColonSyntax,
2213 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002214}
2215
Mike Stump1eb44332009-09-09 15:08:12 +00002216DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002217 unsigned NumIndexExprs) {
2218 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2219 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2220 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2221}
2222
Douglas Gregor319d57f2010-01-06 23:17:19 +00002223void DesignatedInitExpr::setDesignators(ASTContext &C,
2224 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002225 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002226 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002227
Douglas Gregor319d57f2010-01-06 23:17:19 +00002228 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002229 NumDesignators = NumDesigs;
2230 for (unsigned I = 0; I != NumDesigs; ++I)
2231 Designators[I] = Desigs[I];
2232}
2233
Douglas Gregor05c13a32009-01-22 00:58:24 +00002234SourceRange DesignatedInitExpr::getSourceRange() const {
2235 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002236 Designator &First =
2237 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002238 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002239 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002240 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2241 else
2242 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2243 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002244 StartLoc =
2245 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002246 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2247}
2248
Douglas Gregor05c13a32009-01-22 00:58:24 +00002249Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2250 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2251 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2252 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002253 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2254 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2255}
2256
2257Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002258 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002259 "Requires array range designator");
2260 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2261 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002262 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2263 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2264}
2265
2266Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002267 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002268 "Requires array range designator");
2269 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2270 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002271 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2272 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2273}
2274
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002275/// \brief Replaces the designator at index @p Idx with the series
2276/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002277void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002278 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002279 const Designator *Last) {
2280 unsigned NumNewDesignators = Last - First;
2281 if (NumNewDesignators == 0) {
2282 std::copy_backward(Designators + Idx + 1,
2283 Designators + NumDesignators,
2284 Designators + Idx);
2285 --NumNewDesignators;
2286 return;
2287 } else if (NumNewDesignators == 1) {
2288 Designators[Idx] = *First;
2289 return;
2290 }
2291
Mike Stump1eb44332009-09-09 15:08:12 +00002292 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002293 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002294 std::copy(Designators, Designators + Idx, NewDesignators);
2295 std::copy(First, Last, NewDesignators + Idx);
2296 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2297 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002298 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002299 Designators = NewDesignators;
2300 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2301}
2302
Douglas Gregor42602bb2009-08-07 06:08:38 +00002303void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002304 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002305 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002306}
2307
Douglas Gregor319d57f2010-01-06 23:17:19 +00002308void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2309 for (unsigned I = 0; I != NumDesignators; ++I)
2310 Designators[I].~Designator();
2311 C.Deallocate(Designators);
2312 Designators = 0;
2313}
2314
Mike Stump1eb44332009-09-09 15:08:12 +00002315ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002316 Expr **exprs, unsigned nexprs,
2317 SourceLocation rparenloc)
2318: Expr(ParenListExprClass, QualType(),
2319 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002320 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002321 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Nate Begeman2ef13e52009-08-10 23:49:36 +00002323 Exprs = new (C) Stmt*[nexprs];
2324 for (unsigned i = 0; i != nexprs; ++i)
2325 Exprs[i] = exprs[i];
2326}
2327
2328void ParenListExpr::DoDestroy(ASTContext& C) {
2329 DestroyChildren(C);
2330 if (Exprs) C.Deallocate(Exprs);
2331 this->~ParenListExpr();
2332 C.Deallocate(this);
2333}
2334
Douglas Gregor05c13a32009-01-22 00:58:24 +00002335//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002336// ExprIterator.
2337//===----------------------------------------------------------------------===//
2338
2339Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2340Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2341Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2342const Expr* ConstExprIterator::operator[](size_t idx) const {
2343 return cast<Expr>(I[idx]);
2344}
2345const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2346const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2347
2348//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002349// Child Iterators for iterating over subexpressions/substatements
2350//===----------------------------------------------------------------------===//
2351
2352// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002353Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2354Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002355
Steve Naroff7779db42007-11-12 14:29:37 +00002356// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002357Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2358Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002359
Steve Naroffe3e9add2008-06-02 23:03:37 +00002360// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002361Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2362Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002363
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002364// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002365Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCall2fc46bf2010-05-05 22:59:52 +00002366 // If this is accessing a class member, skip that entry.
2367 if (Base) return &Base;
2368 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002369}
Mike Stump1eb44332009-09-09 15:08:12 +00002370Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2371 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002372}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002373
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002374// ObjCSuperExpr
2375Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2376Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2377
Steve Narofff242b1b2009-07-24 17:54:45 +00002378// ObjCIsaExpr
2379Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2380Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2381
Chris Lattnerd9f69102008-08-10 01:53:14 +00002382// PredefinedExpr
2383Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2384Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002385
2386// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002387Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2388Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002389
2390// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002391Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002392Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002393
2394// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002395Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2396Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002397
Chris Lattner5d661452007-08-26 03:42:43 +00002398// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002399Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2400Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002401
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002402// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002403Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2404Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002405
2406// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002407Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2408Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002409
2410// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002411Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2412Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002413
Douglas Gregor8ecdb652010-04-28 22:16:22 +00002414// OffsetOfExpr
2415Stmt::child_iterator OffsetOfExpr::child_begin() {
2416 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2417 + NumComps);
2418}
2419Stmt::child_iterator OffsetOfExpr::child_end() {
2420 return child_iterator(&*child_begin() + NumExprs);
2421}
2422
Sebastian Redl05189992008-11-11 17:56:53 +00002423// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002424Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002425 // If this is of a type and the type is a VLA type (and not a typedef), the
2426 // size expression of the VLA needs to be treated as an executable expression.
2427 // Why isn't this weirdness documented better in StmtIterator?
2428 if (isArgumentType()) {
2429 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2430 getArgumentType().getTypePtr()))
2431 return child_iterator(T);
2432 return child_iterator();
2433 }
Sebastian Redld4575892008-12-03 23:17:54 +00002434 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002435}
Sebastian Redl05189992008-11-11 17:56:53 +00002436Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2437 if (isArgumentType())
2438 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002439 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002440}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002441
2442// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002443Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002444 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002445}
Ted Kremenek1237c672007-08-24 20:06:47 +00002446Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002447 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002448}
2449
2450// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002451Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002452 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002453}
Ted Kremenek1237c672007-08-24 20:06:47 +00002454Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002455 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002456}
Ted Kremenek1237c672007-08-24 20:06:47 +00002457
2458// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002459Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2460Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002461
Nate Begeman213541a2008-04-18 23:10:10 +00002462// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002463Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2464Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002465
2466// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002467Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2468Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002469
Ted Kremenek1237c672007-08-24 20:06:47 +00002470// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002471Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2472Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002473
2474// BinaryOperator
2475Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002476 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002477}
Ted Kremenek1237c672007-08-24 20:06:47 +00002478Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002479 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002480}
2481
2482// ConditionalOperator
2483Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002484 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002485}
Ted Kremenek1237c672007-08-24 20:06:47 +00002486Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002487 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002488}
2489
2490// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002491Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2492Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002493
Ted Kremenek1237c672007-08-24 20:06:47 +00002494// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002495Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2496Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002497
2498// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002499Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2500 return child_iterator();
2501}
2502
2503Stmt::child_iterator TypesCompatibleExpr::child_end() {
2504 return child_iterator();
2505}
Ted Kremenek1237c672007-08-24 20:06:47 +00002506
2507// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002508Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2509Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002510
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002511// GNUNullExpr
2512Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2513Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2514
Eli Friedmand38617c2008-05-14 19:38:39 +00002515// ShuffleVectorExpr
2516Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002517 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002518}
2519Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002520 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002521}
2522
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002523// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002524Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2525Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002526
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002527// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002528Stmt::child_iterator InitListExpr::child_begin() {
2529 return InitExprs.size() ? &InitExprs[0] : 0;
2530}
2531Stmt::child_iterator InitListExpr::child_end() {
2532 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2533}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002534
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002535// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002536Stmt::child_iterator DesignatedInitExpr::child_begin() {
2537 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2538 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002539 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2540}
2541Stmt::child_iterator DesignatedInitExpr::child_end() {
2542 return child_iterator(&*child_begin() + NumSubExprs);
2543}
2544
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002545// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002546Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2547 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002548}
2549
Mike Stump1eb44332009-09-09 15:08:12 +00002550Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2551 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002552}
2553
Nate Begeman2ef13e52009-08-10 23:49:36 +00002554// ParenListExpr
2555Stmt::child_iterator ParenListExpr::child_begin() {
2556 return &Exprs[0];
2557}
2558Stmt::child_iterator ParenListExpr::child_end() {
2559 return &Exprs[0]+NumExprs;
2560}
2561
Ted Kremenek1237c672007-08-24 20:06:47 +00002562// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002563Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002564 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002565}
2566Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002567 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002568}
Ted Kremenek1237c672007-08-24 20:06:47 +00002569
2570// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002571Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2572Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002573
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002574// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002575Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002576 return child_iterator();
2577}
2578Stmt::child_iterator ObjCSelectorExpr::child_end() {
2579 return child_iterator();
2580}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002581
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002582// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002583Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2584 return child_iterator();
2585}
2586Stmt::child_iterator ObjCProtocolExpr::child_end() {
2587 return child_iterator();
2588}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002589
Steve Naroff563477d2007-09-18 23:55:05 +00002590// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002591Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002592 if (getReceiverKind() == Instance)
2593 return reinterpret_cast<Stmt **>(this + 1);
2594 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002595}
2596Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002597 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002598}
2599
Steve Naroff4eb206b2008-09-03 18:15:37 +00002600// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002601Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2602Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002603
Ted Kremenek9da13f92008-09-26 23:24:14 +00002604Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2605Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }