blob: 4cb7712e52c3ec35527251d76a99ffb675de395f [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
Douglas Gregorf5403052010-03-30 18:56:13 +000030#ifndef NDEBUG
31llvm::Statistic clang::objc_expr_checks =
32 { "clang", "Number of checks for Objective-C expression nodes", 0, 0 };
33llvm::Statistic clang::cxx_expr_checks =
34 { "clang", "Number of checks for C++ expression nodes", 0, 0 };
35#endif
36
Reid Spencer5f016e22007-07-11 17:01:13 +000037//===----------------------------------------------------------------------===//
38// Primary Expressions.
39//===----------------------------------------------------------------------===//
40
John McCalld5532b62009-11-23 01:53:49 +000041void ExplicitTemplateArgumentList::initializeFrom(
42 const TemplateArgumentListInfo &Info) {
43 LAngleLoc = Info.getLAngleLoc();
44 RAngleLoc = Info.getRAngleLoc();
45 NumTemplateArgs = Info.size();
46
47 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
48 for (unsigned i = 0; i != NumTemplateArgs; ++i)
49 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
50}
51
52void ExplicitTemplateArgumentList::copyInto(
53 TemplateArgumentListInfo &Info) const {
54 Info.setLAngleLoc(LAngleLoc);
55 Info.setRAngleLoc(RAngleLoc);
56 for (unsigned I = 0; I != NumTemplateArgs; ++I)
57 Info.addArgument(getTemplateArgs()[I]);
58}
59
60std::size_t ExplicitTemplateArgumentList::sizeFor(
61 const TemplateArgumentListInfo &Info) {
62 return sizeof(ExplicitTemplateArgumentList) +
63 sizeof(TemplateArgumentLoc) * Info.size();
64}
65
Douglas Gregor0da76df2009-11-23 11:41:28 +000066void DeclRefExpr::computeDependence() {
67 TypeDependent = false;
68 ValueDependent = false;
69
70 NamedDecl *D = getDecl();
71
72 // (TD) C++ [temp.dep.expr]p3:
73 // An id-expression is type-dependent if it contains:
74 //
75 // and
76 //
77 // (VD) C++ [temp.dep.constexpr]p2:
78 // An identifier is value-dependent if it is:
79
80 // (TD) - an identifier that was declared with dependent type
81 // (VD) - a name declared with a dependent type,
82 if (getType()->isDependentType()) {
83 TypeDependent = true;
84 ValueDependent = true;
85 }
86 // (TD) - a conversion-function-id that specifies a dependent type
87 else if (D->getDeclName().getNameKind()
88 == DeclarationName::CXXConversionFunctionName &&
89 D->getDeclName().getCXXNameType()->isDependentType()) {
90 TypeDependent = true;
91 ValueDependent = true;
92 }
93 // (TD) - a template-id that is dependent,
94 else if (hasExplicitTemplateArgumentList() &&
95 TemplateSpecializationType::anyDependentTemplateArguments(
96 getTemplateArgs(),
97 getNumTemplateArgs())) {
98 TypeDependent = true;
99 ValueDependent = true;
100 }
101 // (VD) - the name of a non-type template parameter,
102 else if (isa<NonTypeTemplateParmDecl>(D))
103 ValueDependent = true;
104 // (VD) - a constant with integral or enumeration type and is
105 // initialized with an expression that is value-dependent.
106 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
107 if (Var->getType()->isIntegralType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000108 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000109 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000110 if (Init->isValueDependent())
111 ValueDependent = true;
112 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000113 }
114 // (TD) - a nested-name-specifier or a qualified-id that names a
115 // member of an unknown specialization.
116 // (handled by DependentScopeDeclRefExpr)
117}
118
Douglas Gregora2813ce2009-10-23 18:54:35 +0000119DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
120 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000121 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000122 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000123 QualType T)
124 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000125 DecoratedD(D,
126 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000127 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000128 Loc(NameLoc) {
129 if (Qualifier) {
130 NameQualifier *NQ = getNameQualifier();
131 NQ->NNS = Qualifier;
132 NQ->Range = QualifierRange;
133 }
134
John McCalld5532b62009-11-23 01:53:49 +0000135 if (TemplateArgs)
136 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000137
138 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000139}
140
141DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
142 NestedNameSpecifier *Qualifier,
143 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000144 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000145 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000146 QualType T,
147 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000148 std::size_t Size = sizeof(DeclRefExpr);
149 if (Qualifier != 0)
150 Size += sizeof(NameQualifier);
151
John McCalld5532b62009-11-23 01:53:49 +0000152 if (TemplateArgs)
153 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000154
155 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
156 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000157 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000158}
159
160SourceRange DeclRefExpr::getSourceRange() const {
161 // FIXME: Does not handle multi-token names well, e.g., operator[].
162 SourceRange R(Loc);
163
164 if (hasQualifier())
165 R.setBegin(getQualifierRange().getBegin());
166 if (hasExplicitTemplateArgumentList())
167 R.setEnd(getRAngleLoc());
168 return R;
169}
170
Anders Carlsson3a082d82009-09-08 18:24:21 +0000171// FIXME: Maybe this should use DeclPrinter with a special "print predefined
172// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000173std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
174 ASTContext &Context = CurrentDecl->getASTContext();
175
Anders Carlsson3a082d82009-09-08 18:24:21 +0000176 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000177 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000178 return FD->getNameAsString();
179
180 llvm::SmallString<256> Name;
181 llvm::raw_svector_ostream Out(Name);
182
183 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000184 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000185 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000186 if (MD->isStatic())
187 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000188 }
189
190 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000191
192 std::string Proto = FD->getQualifiedNameAsString(Policy);
193
John McCall183700f2009-09-21 23:43:11 +0000194 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000195 const FunctionProtoType *FT = 0;
196 if (FD->hasWrittenPrototype())
197 FT = dyn_cast<FunctionProtoType>(AFT);
198
199 Proto += "(";
200 if (FT) {
201 llvm::raw_string_ostream POut(Proto);
202 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
203 if (i) POut << ", ";
204 std::string Param;
205 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
206 POut << Param;
207 }
208
209 if (FT->isVariadic()) {
210 if (FD->getNumParams()) POut << ", ";
211 POut << "...";
212 }
213 }
214 Proto += ")";
215
Sam Weinig4eadcc52009-12-27 01:38:20 +0000216 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
217 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
218 if (ThisQuals.hasConst())
219 Proto += " const";
220 if (ThisQuals.hasVolatile())
221 Proto += " volatile";
222 }
223
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000224 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
225 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000226
227 Out << Proto;
228
229 Out.flush();
230 return Name.str().str();
231 }
232 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
233 llvm::SmallString<256> Name;
234 llvm::raw_svector_ostream Out(Name);
235 Out << (MD->isInstanceMethod() ? '-' : '+');
236 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000237
238 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
239 // a null check to avoid a crash.
240 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
241 Out << ID->getNameAsString();
242
Anders Carlsson3a082d82009-09-08 18:24:21 +0000243 if (const ObjCCategoryImplDecl *CID =
244 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
245 Out << '(';
246 Out << CID->getNameAsString();
247 Out << ')';
248 }
249 Out << ' ';
250 Out << MD->getSelector().getAsString();
251 Out << ']';
252
253 Out.flush();
254 return Name.str().str();
255 }
256 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
257 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
258 return "top level";
259 }
260 return "";
261}
262
Chris Lattnerda8249e2008-06-07 22:13:43 +0000263/// getValueAsApproximateDouble - This returns the value as an inaccurate
264/// double. Note that this may cause loss of precision, but is useful for
265/// debugging dumps, etc.
266double FloatingLiteral::getValueAsApproximateDouble() const {
267 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000268 bool ignored;
269 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
270 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000271 return V.convertToDouble();
272}
273
Chris Lattner2085fd62009-02-18 06:40:38 +0000274StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
275 unsigned ByteLength, bool Wide,
276 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000277 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000278 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000279 // Allocate enough space for the StringLiteral plus an array of locations for
280 // any concatenated string tokens.
281 void *Mem = C.Allocate(sizeof(StringLiteral)+
282 sizeof(SourceLocation)*(NumStrs-1),
283 llvm::alignof<StringLiteral>());
284 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000287 char *AStrData = new (C, 1) char[ByteLength];
288 memcpy(AStrData, StrData, ByteLength);
289 SL->StrData = AStrData;
290 SL->ByteLength = ByteLength;
291 SL->IsWide = Wide;
292 SL->TokLocs[0] = Loc[0];
293 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000294
Chris Lattner726e1682009-02-18 05:49:11 +0000295 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000296 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
297 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000298}
299
Douglas Gregor673ecd62009-04-15 16:35:07 +0000300StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
301 void *Mem = C.Allocate(sizeof(StringLiteral)+
302 sizeof(SourceLocation)*(NumStrs-1),
303 llvm::alignof<StringLiteral>());
304 StringLiteral *SL = new (Mem) StringLiteral(QualType());
305 SL->StrData = 0;
306 SL->ByteLength = 0;
307 SL->NumConcatenated = NumStrs;
308 return SL;
309}
310
Douglas Gregor42602bb2009-08-07 06:08:38 +0000311void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000312 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000313 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000314}
315
Daniel Dunbarb6480232009-09-22 03:27:33 +0000316void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000317 if (StrData)
318 C.Deallocate(const_cast<char*>(StrData));
319
Daniel Dunbarb6480232009-09-22 03:27:33 +0000320 char *AStrData = new (C, 1) char[Str.size()];
321 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000322 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000323 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000324}
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
327/// corresponds to, e.g. "sizeof" or "[pre]++".
328const char *UnaryOperator::getOpcodeStr(Opcode Op) {
329 switch (Op) {
330 default: assert(0 && "Unknown unary operator");
331 case PostInc: return "++";
332 case PostDec: return "--";
333 case PreInc: return "++";
334 case PreDec: return "--";
335 case AddrOf: return "&";
336 case Deref: return "*";
337 case Plus: return "+";
338 case Minus: return "-";
339 case Not: return "~";
340 case LNot: return "!";
341 case Real: return "__real";
342 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000344 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 }
346}
347
Mike Stump1eb44332009-09-09 15:08:12 +0000348UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000349UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
350 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000351 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000352 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
353 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
354 case OO_Amp: return AddrOf;
355 case OO_Star: return Deref;
356 case OO_Plus: return Plus;
357 case OO_Minus: return Minus;
358 case OO_Tilde: return Not;
359 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000360 }
361}
362
363OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
364 switch (Opc) {
365 case PostInc: case PreInc: return OO_PlusPlus;
366 case PostDec: case PreDec: return OO_MinusMinus;
367 case AddrOf: return OO_Amp;
368 case Deref: return OO_Star;
369 case Plus: return OO_Plus;
370 case Minus: return OO_Minus;
371 case Not: return OO_Tilde;
372 case LNot: return OO_Exclaim;
373 default: return OO_None;
374 }
375}
376
377
Reid Spencer5f016e22007-07-11 17:01:13 +0000378//===----------------------------------------------------------------------===//
379// Postfix Operators.
380//===----------------------------------------------------------------------===//
381
Ted Kremenek668bf912009-02-09 20:51:47 +0000382CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000383 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000384 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000385 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000386 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000387 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Ted Kremenek668bf912009-02-09 20:51:47 +0000389 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000390 SubExprs[FN] = fn;
391 for (unsigned i = 0; i != numargs; ++i)
392 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000393
Douglas Gregorb4609802008-11-14 16:09:21 +0000394 RParenLoc = rparenloc;
395}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000396
Ted Kremenek668bf912009-02-09 20:51:47 +0000397CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
398 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000399 : Expr(CallExprClass, t,
400 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000401 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000402 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000403
404 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000405 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000407 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 RParenLoc = rparenloc;
410}
411
Mike Stump1eb44332009-09-09 15:08:12 +0000412CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
413 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000414 SubExprs = new (C) Stmt*[1];
415}
416
Douglas Gregor42602bb2009-08-07 06:08:38 +0000417void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000418 DestroyChildren(C);
419 if (SubExprs) C.Deallocate(SubExprs);
420 this->~CallExpr();
421 C.Deallocate(this);
422}
423
Nuno Lopesd20254f2009-12-20 23:11:08 +0000424Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000425 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000426 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000427 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000428 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
429 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000430
431 return 0;
432}
433
Nuno Lopesd20254f2009-12-20 23:11:08 +0000434FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000435 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000436}
437
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438/// setNumArgs - This changes the number of arguments present in this call.
439/// Any orphaned expressions are deleted by this, and any new operands are set
440/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000441void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000442 // No change, just return.
443 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattnerd18b3292007-12-28 05:25:02 +0000445 // If shrinking # arguments, just delete the extras and forgot them.
446 if (NumArgs < getNumArgs()) {
447 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000448 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000449 this->NumArgs = NumArgs;
450 return;
451 }
452
453 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000454 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000455 // Copy over args.
456 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
457 NewSubExprs[i] = SubExprs[i];
458 // Null out new args.
459 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
460 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Douglas Gregor88c9a462009-04-17 21:46:47 +0000462 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000463 SubExprs = NewSubExprs;
464 this->NumArgs = NumArgs;
465}
466
Chris Lattnercb888962008-10-06 05:00:53 +0000467/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
468/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000469unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000470 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000471 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000472 // ImplicitCastExpr.
473 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
474 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000475 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000477 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
478 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000479 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Anders Carlssonbcba2012008-01-31 02:13:57 +0000481 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
482 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000483 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000485 if (!FDecl->getIdentifier())
486 return 0;
487
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000488 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000489}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000490
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000491QualType CallExpr::getCallReturnType() const {
492 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000493 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000494 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000495 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000496 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000497
John McCall183700f2009-09-21 23:43:11 +0000498 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000499 return FnType->getResultType();
500}
Chris Lattnercb888962008-10-06 05:00:53 +0000501
Mike Stump1eb44332009-09-09 15:08:12 +0000502MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
503 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000504 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000505 ValueDecl *memberdecl,
John McCall6bb80172010-03-30 21:47:33 +0000506 NamedDecl *founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000507 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000508 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000509 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000510 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000511
512 bool hasQualOrFound = (qual != 0 || founddecl != memberdecl);
513 if (hasQualOrFound)
514 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
John McCalld5532b62009-11-23 01:53:49 +0000516 if (targs)
517 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000519 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000520 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
521
522 if (hasQualOrFound) {
523 if (qual && qual->isDependent()) {
524 E->setValueDependent(true);
525 E->setTypeDependent(true);
526 }
527 E->HasQualifierOrFoundDecl = true;
528
529 MemberNameQualifier *NQ = E->getMemberQualifier();
530 NQ->NNS = qual;
531 NQ->Range = qualrange;
532 NQ->FoundDecl = founddecl;
533 }
534
535 if (targs) {
536 E->HasExplicitTemplateArgumentList = true;
537 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
538 }
539
540 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000541}
542
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000543const char *CastExpr::getCastKindName() const {
544 switch (getCastKind()) {
545 case CastExpr::CK_Unknown:
546 return "Unknown";
547 case CastExpr::CK_BitCast:
548 return "BitCast";
549 case CastExpr::CK_NoOp:
550 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000551 case CastExpr::CK_BaseToDerived:
552 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000553 case CastExpr::CK_DerivedToBase:
554 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000555 case CastExpr::CK_UncheckedDerivedToBase:
556 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000557 case CastExpr::CK_Dynamic:
558 return "Dynamic";
559 case CastExpr::CK_ToUnion:
560 return "ToUnion";
561 case CastExpr::CK_ArrayToPointerDecay:
562 return "ArrayToPointerDecay";
563 case CastExpr::CK_FunctionToPointerDecay:
564 return "FunctionToPointerDecay";
565 case CastExpr::CK_NullToMemberPointer:
566 return "NullToMemberPointer";
567 case CastExpr::CK_BaseToDerivedMemberPointer:
568 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000569 case CastExpr::CK_DerivedToBaseMemberPointer:
570 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000571 case CastExpr::CK_UserDefinedConversion:
572 return "UserDefinedConversion";
573 case CastExpr::CK_ConstructorConversion:
574 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000575 case CastExpr::CK_IntegralToPointer:
576 return "IntegralToPointer";
577 case CastExpr::CK_PointerToIntegral:
578 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000579 case CastExpr::CK_ToVoid:
580 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000581 case CastExpr::CK_VectorSplat:
582 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000583 case CastExpr::CK_IntegralCast:
584 return "IntegralCast";
585 case CastExpr::CK_IntegralToFloating:
586 return "IntegralToFloating";
587 case CastExpr::CK_FloatingToIntegral:
588 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000589 case CastExpr::CK_FloatingCast:
590 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000591 case CastExpr::CK_MemberPointerToBoolean:
592 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000593 case CastExpr::CK_AnyPointerToObjCPointerCast:
594 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000595 case CastExpr::CK_AnyPointerToBlockPointerCast:
596 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000599 assert(0 && "Unhandled cast kind!");
600 return 0;
601}
602
Douglas Gregor6eef5192009-12-14 19:27:10 +0000603Expr *CastExpr::getSubExprAsWritten() {
604 Expr *SubExpr = 0;
605 CastExpr *E = this;
606 do {
607 SubExpr = E->getSubExpr();
608
609 // Skip any temporary bindings; they're implicit.
610 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
611 SubExpr = Binder->getSubExpr();
612
613 // Conversions by constructor and conversion functions have a
614 // subexpression describing the call; strip it off.
615 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
616 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
617 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
618 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
619
620 // If the subexpression we're left with is an implicit cast, look
621 // through that, too.
622 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
623
624 return SubExpr;
625}
626
Reid Spencer5f016e22007-07-11 17:01:13 +0000627/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
628/// corresponds to, e.g. "<<=".
629const char *BinaryOperator::getOpcodeStr(Opcode Op) {
630 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000631 case PtrMemD: return ".*";
632 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 case Mul: return "*";
634 case Div: return "/";
635 case Rem: return "%";
636 case Add: return "+";
637 case Sub: return "-";
638 case Shl: return "<<";
639 case Shr: return ">>";
640 case LT: return "<";
641 case GT: return ">";
642 case LE: return "<=";
643 case GE: return ">=";
644 case EQ: return "==";
645 case NE: return "!=";
646 case And: return "&";
647 case Xor: return "^";
648 case Or: return "|";
649 case LAnd: return "&&";
650 case LOr: return "||";
651 case Assign: return "=";
652 case MulAssign: return "*=";
653 case DivAssign: return "/=";
654 case RemAssign: return "%=";
655 case AddAssign: return "+=";
656 case SubAssign: return "-=";
657 case ShlAssign: return "<<=";
658 case ShrAssign: return ">>=";
659 case AndAssign: return "&=";
660 case XorAssign: return "^=";
661 case OrAssign: return "|=";
662 case Comma: return ",";
663 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000664
665 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000666}
667
Mike Stump1eb44332009-09-09 15:08:12 +0000668BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000669BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
670 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000671 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000672 case OO_Plus: return Add;
673 case OO_Minus: return Sub;
674 case OO_Star: return Mul;
675 case OO_Slash: return Div;
676 case OO_Percent: return Rem;
677 case OO_Caret: return Xor;
678 case OO_Amp: return And;
679 case OO_Pipe: return Or;
680 case OO_Equal: return Assign;
681 case OO_Less: return LT;
682 case OO_Greater: return GT;
683 case OO_PlusEqual: return AddAssign;
684 case OO_MinusEqual: return SubAssign;
685 case OO_StarEqual: return MulAssign;
686 case OO_SlashEqual: return DivAssign;
687 case OO_PercentEqual: return RemAssign;
688 case OO_CaretEqual: return XorAssign;
689 case OO_AmpEqual: return AndAssign;
690 case OO_PipeEqual: return OrAssign;
691 case OO_LessLess: return Shl;
692 case OO_GreaterGreater: return Shr;
693 case OO_LessLessEqual: return ShlAssign;
694 case OO_GreaterGreaterEqual: return ShrAssign;
695 case OO_EqualEqual: return EQ;
696 case OO_ExclaimEqual: return NE;
697 case OO_LessEqual: return LE;
698 case OO_GreaterEqual: return GE;
699 case OO_AmpAmp: return LAnd;
700 case OO_PipePipe: return LOr;
701 case OO_Comma: return Comma;
702 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000703 }
704}
705
706OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
707 static const OverloadedOperatorKind OverOps[] = {
708 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
709 OO_Star, OO_Slash, OO_Percent,
710 OO_Plus, OO_Minus,
711 OO_LessLess, OO_GreaterGreater,
712 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
713 OO_EqualEqual, OO_ExclaimEqual,
714 OO_Amp,
715 OO_Caret,
716 OO_Pipe,
717 OO_AmpAmp,
718 OO_PipePipe,
719 OO_Equal, OO_StarEqual,
720 OO_SlashEqual, OO_PercentEqual,
721 OO_PlusEqual, OO_MinusEqual,
722 OO_LessLessEqual, OO_GreaterGreaterEqual,
723 OO_AmpEqual, OO_CaretEqual,
724 OO_PipeEqual,
725 OO_Comma
726 };
727 return OverOps[Opc];
728}
729
Ted Kremenekba7bc552010-02-19 01:50:18 +0000730InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000731 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000732 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000733 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000734 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenekba7bc552010-02-19 01:50:18 +0000735 UnionFieldInit(0), HadArrayRangeDesignator(false)
736{
737 for (unsigned I = 0; I != numInits; ++I) {
738 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000739 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000740 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000741 ValueDependent = true;
742 }
Ted Kremenekba7bc552010-02-19 01:50:18 +0000743
744 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000745}
Reid Spencer5f016e22007-07-11 17:01:13 +0000746
Ted Kremenekba7bc552010-02-19 01:50:18 +0000747void InitListExpr::reserveInits(unsigned NumInits) {
748 if (NumInits > InitExprs.size())
749 InitExprs.reserve(NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000750}
751
Ted Kremenekba7bc552010-02-19 01:50:18 +0000752void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
753 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
754 Idx < LastIdx; ++Idx)
755 InitExprs[Idx]->Destroy(Context);
756 InitExprs.resize(NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000757}
758
Ted Kremenekba7bc552010-02-19 01:50:18 +0000759Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
760 if (Init >= InitExprs.size()) {
761 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
762 InitExprs.back() = expr;
763 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Douglas Gregor4c678342009-01-28 21:54:33 +0000766 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
767 InitExprs[Init] = expr;
768 return Result;
769}
770
Steve Naroffbfdcae62008-09-04 15:31:07 +0000771/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000772///
773const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000774 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000775 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000776}
777
Mike Stump1eb44332009-09-09 15:08:12 +0000778SourceLocation BlockExpr::getCaretLocation() const {
779 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000780}
Mike Stump1eb44332009-09-09 15:08:12 +0000781const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000782 return TheBlock->getBody();
783}
Mike Stump1eb44332009-09-09 15:08:12 +0000784Stmt *BlockExpr::getBody() {
785 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000786}
Steve Naroff56ee6892008-10-08 17:01:13 +0000787
788
Reid Spencer5f016e22007-07-11 17:01:13 +0000789//===----------------------------------------------------------------------===//
790// Generic Expression Routines
791//===----------------------------------------------------------------------===//
792
Chris Lattner026dc962009-02-14 07:37:35 +0000793/// isUnusedResultAWarning - Return true if this immediate expression should
794/// be warned about if the result is unused. If so, fill in Loc and Ranges
795/// with location to warn on and the source range[s] to report with the
796/// warning.
797bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000798 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000799 // Don't warn if the expr is type dependent. The type could end up
800 // instantiating to void.
801 if (isTypeDependent())
802 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 switch (getStmtClass()) {
805 default:
John McCall0faede62010-03-12 07:11:26 +0000806 if (getType()->isVoidType())
807 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000808 Loc = getExprLoc();
809 R1 = getSourceRange();
810 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000812 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000813 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 case UnaryOperatorClass: {
815 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000818 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000819 case UnaryOperator::PostInc:
820 case UnaryOperator::PostDec:
821 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000822 case UnaryOperator::PreDec: // ++/--
823 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 case UnaryOperator::Deref:
825 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000826 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000827 return false;
828 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 case UnaryOperator::Real:
830 case UnaryOperator::Imag:
831 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000832 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
833 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000834 return false;
835 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000837 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
Chris Lattner026dc962009-02-14 07:37:35 +0000839 Loc = UO->getOperatorLoc();
840 R1 = UO->getSubExpr()->getSourceRange();
841 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000843 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000844 const BinaryOperator *BO = cast<BinaryOperator>(this);
845 // Consider comma to have side effects if the LHS or RHS does.
John McCallbf0ee352010-02-16 04:10:53 +0000846 if (BO->getOpcode() == BinaryOperator::Comma) {
847 // ((foo = <blah>), 0) is an idiom for hiding the result (and
848 // lvalue-ness) of an assignment written in a macro.
849 if (IntegerLiteral *IE =
850 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
851 if (IE->getValue() == 0)
852 return false;
853
John McCall0faede62010-03-12 07:11:26 +0000854 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
855 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattner026dc962009-02-14 07:37:35 +0000858 if (BO->isAssignmentOp())
859 return false;
860 Loc = BO->getOperatorLoc();
861 R1 = BO->getLHS()->getSourceRange();
862 R2 = BO->getRHS()->getSourceRange();
863 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000864 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000865 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000866 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000867
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000868 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000869 // The condition must be evaluated, but if either the LHS or RHS is a
870 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000871 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000872 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000873 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000874 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000875 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000876 }
877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000879 // If the base pointer or element is to a volatile pointer/field, accessing
880 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000881 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000882 return false;
883 Loc = cast<MemberExpr>(this)->getMemberLoc();
884 R1 = SourceRange(Loc, Loc);
885 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
886 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 case ArraySubscriptExprClass:
889 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000890 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000891 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000892 return false;
893 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
894 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
895 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
896 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000899 case CXXOperatorCallExprClass:
900 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000901 // If this is a direct call, get the callee.
902 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000903 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000904 // If the callee has attribute pure, const, or warn_unused_result, warn
905 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000906 //
907 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
908 // updated to match for QoI.
909 if (FD->getAttr<WarnUnusedResultAttr>() ||
910 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
911 Loc = CE->getCallee()->getLocStart();
912 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000914 if (unsigned NumArgs = CE->getNumArgs())
915 R2 = SourceRange(CE->getArg(0)->getLocStart(),
916 CE->getArg(NumArgs-1)->getLocEnd());
917 return true;
918 }
Chris Lattner026dc962009-02-14 07:37:35 +0000919 }
920 return false;
921 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000922
923 case CXXTemporaryObjectExprClass:
924 case CXXConstructExprClass:
925 return false;
926
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000927 case ObjCMessageExprClass: {
928 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
929 const ObjCMethodDecl *MD = ME->getMethodDecl();
930 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
931 Loc = getExprLoc();
932 return true;
933 }
Chris Lattner026dc962009-02-14 07:37:35 +0000934 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000937 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000938#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000939 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000940 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000941 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000942 Loc = Ref->getLocation();
943 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
944 if (Ref->getBase())
945 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000946#else
947 Loc = getExprLoc();
948 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000949#endif
950 return true;
951 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000952 case StmtExprClass: {
953 // Statement exprs don't logically have side effects themselves, but are
954 // sometimes used in macros in ways that give them a type that is unused.
955 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
956 // however, if the result of the stmt expr is dead, we don't want to emit a
957 // warning.
958 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
959 if (!CS->body_empty())
960 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000961 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
John McCall0faede62010-03-12 07:11:26 +0000963 if (getType()->isVoidType())
964 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000965 Loc = cast<StmtExpr>(this)->getLParenLoc();
966 R1 = getSourceRange();
967 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000968 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000969 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000970 // If this is an explicit cast to void, allow it. People do this when they
971 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000972 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000973 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000974 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
975 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
976 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000977 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +0000978 if (getType()->isVoidType())
979 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +0000980 const CastExpr *CE = cast<CastExpr>(this);
981
982 // If this is a cast to void or a constructor conversion, check the operand.
983 // Otherwise, the result of the cast is unused.
984 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
985 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000986 return (cast<CastExpr>(this)->getSubExpr()
987 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000988 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
989 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
990 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Eli Friedman4be1f472008-05-19 21:24:43 +0000993 case ImplicitCastExprClass:
994 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000995 return (cast<ImplicitCastExpr>(this)
996 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000997
Chris Lattner04421082008-04-08 04:40:51 +0000998 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000999 return (cast<CXXDefaultArgExpr>(this)
1000 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001001
1002 case CXXNewExprClass:
1003 // FIXME: In theory, there might be new expressions that don't have side
1004 // effects (e.g. a placement new with an uninitialized POD).
1005 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001006 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001007 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001008 return (cast<CXXBindTemporaryExpr>(this)
1009 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001010 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001011 return (cast<CXXExprWithTemporaries>(this)
1012 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001013 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001014}
1015
Douglas Gregorba7e2102008-10-22 15:04:37 +00001016/// DeclCanBeLvalue - Determine whether the given declaration can be
1017/// an lvalue. This is a helper routine for isLvalue.
1018static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001019 // C++ [temp.param]p6:
1020 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001021 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001022 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1023 return NTTParm->getType()->isReferenceType();
1024
Douglas Gregor44b43212008-12-11 16:49:14 +00001025 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001026 // C++ 3.10p2: An lvalue refers to an object or function.
1027 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001028 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001029}
1030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1032/// incomplete type other than void. Nonarray expressions that can be lvalues:
1033/// - name, where name must be a variable
1034/// - e[i]
1035/// - (e), where e must be an lvalue
1036/// - e.name, where e must be an lvalue
1037/// - e->name
1038/// - *e, the type of e cannot be a function type
1039/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001040/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001041/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001042///
Chris Lattner28be73f2008-07-26 21:30:36 +00001043Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001044 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1045
1046 isLvalueResult Res = isLvalueInternal(Ctx);
1047 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1048 return Res;
1049
Douglas Gregor98cd5992008-10-21 23:43:52 +00001050 // first, check the type (C99 6.3.2.1). Expressions with function
1051 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001052 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 return LV_NotObjectType;
1054
Steve Naroffacb818a2008-02-10 01:39:04 +00001055 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001056 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001057 return LV_IncompleteVoidType;
1058
Eli Friedman53202852009-05-03 22:36:05 +00001059 return LV_Valid;
1060}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001061
Eli Friedman53202852009-05-03 22:36:05 +00001062// Check whether the expression can be sanely treated like an l-value
1063Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001065 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001066 case StringLiteralClass: // C99 6.5.1p4
1067 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001068 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1070 // For vectors, make sure base is an lvalue (i.e. not a function call).
1071 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001072 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001074 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001075 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1076 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 return LV_Valid;
1078 break;
Chris Lattner41110242008-06-17 18:05:57 +00001079 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001080 case BlockDeclRefExprClass: {
1081 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001082 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001083 return LV_Valid;
1084 break;
1085 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001086 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001088 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1089 NamedDecl *Member = m->getMemberDecl();
1090 // C++ [expr.ref]p4:
1091 // If E2 is declared to have type "reference to T", then E1.E2
1092 // is an lvalue.
1093 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1094 if (Value->getType()->isReferenceType())
1095 return LV_Valid;
1096
1097 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001098 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001099 return LV_Valid;
1100
1101 // -- If E2 is a non-static data member [...]. If E1 is an
1102 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001103 if (isa<FieldDecl>(Member)) {
1104 if (m->isArrow())
1105 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001106 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001107 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001108
1109 // -- If it refers to a static member function [...], then
1110 // E1.E2 is an lvalue.
1111 // -- Otherwise, if E1.E2 refers to a non-static member
1112 // function [...], then E1.E2 is not an lvalue.
1113 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1114 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1115
1116 // -- If E2 is a member enumerator [...], the expression E1.E2
1117 // is not an lvalue.
1118 if (isa<EnumConstantDecl>(Member))
1119 return LV_InvalidExpression;
1120
1121 // Not an lvalue.
1122 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001123 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001124
Douglas Gregor86f19402008-12-20 23:49:58 +00001125 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001126 if (m->isArrow())
1127 return LV_Valid;
1128 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001129 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1130 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001131 return LV_SubObjCPropertySetting;
1132 return
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001133 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001134 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001135 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001137 return LV_Valid; // C99 6.5.3p4
1138
1139 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001140 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1141 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001142 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001143
1144 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1145 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1146 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1147 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001149 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001150 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1151 return LV_Valid;
1152
1153 // If this is a conversion to a class temporary, make a note of
1154 // that.
1155 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1156 return LV_ClassTemporary;
1157
1158 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001160 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001161 case BinaryOperatorClass:
1162 case CompoundAssignOperatorClass: {
1163 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001164
1165 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1166 BinOp->getOpcode() == BinaryOperator::Comma)
1167 return BinOp->getRHS()->isLvalue(Ctx);
1168
Sebastian Redl22460502009-02-07 00:15:38 +00001169 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001170 // The result of a .* expression is an lvalue only if its first operand is
1171 // an lvalue and its second operand is a pointer to data member.
1172 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001173 !BinOp->getType()->isFunctionType())
1174 return BinOp->getLHS()->isLvalue(Ctx);
1175
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001176 // The result of an ->* expression is an lvalue only if its second operand
1177 // is a pointer to data member.
1178 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1179 !BinOp->getType()->isFunctionType()) {
1180 QualType Ty = BinOp->getRHS()->getType();
1181 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1182 return LV_Valid;
1183 }
1184
Douglas Gregorbf3af052008-11-13 20:12:29 +00001185 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001186 return LV_InvalidExpression;
1187
Douglas Gregorbf3af052008-11-13 20:12:29 +00001188 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001189 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001190 // The result of an assignment operation [...] is an lvalue.
1191 return LV_Valid;
1192
1193
1194 // C99 6.5.16:
1195 // An assignment expression [...] is not an lvalue.
1196 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001199 case CXXOperatorCallExprClass:
1200 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001201 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001202 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001203 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001204 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1205 if (ReturnType->isLValueReferenceType())
1206 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001207
Douglas Gregore873fb72010-02-16 21:39:57 +00001208 // If the function is returning a class temporary, make a note of
1209 // that.
1210 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1211 return LV_ClassTemporary;
1212
Douglas Gregor9d293df2008-10-28 00:22:11 +00001213 break;
1214 }
Steve Naroffe6386392007-12-05 04:00:10 +00001215 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001216 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001217 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001218 case ChooseExprClass:
1219 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001220 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001221 case ExtVectorElementExprClass:
1222 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001223 return LV_DuplicateVectorComponents;
1224 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001225 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1226 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001227 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1228 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001229 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001230 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001231 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001232 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001233 case UnresolvedLookupExprClass:
1234 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001235 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001236 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001237 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001238 case CXXFunctionalCastExprClass:
1239 case CXXStaticCastExprClass:
1240 case CXXDynamicCastExprClass:
1241 case CXXReinterpretCastExprClass:
1242 case CXXConstCastExprClass:
1243 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001244 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001245 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1246 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001247 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1248 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001249 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001250
1251 // If this is a conversion to a class temporary, make a note of
1252 // that.
1253 if (Ctx.getLangOptions().CPlusPlus &&
1254 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1255 return LV_ClassTemporary;
1256
Douglas Gregor9d293df2008-10-28 00:22:11 +00001257 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001258 case CXXTypeidExprClass:
1259 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1260 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001261 case CXXBindTemporaryExprClass:
1262 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1263 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001264 case CXXBindReferenceExprClass:
1265 // Something that's bound to a reference is always an lvalue.
1266 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001267 case ConditionalOperatorClass: {
1268 // Complicated handling is only for C++.
1269 if (!Ctx.getLangOptions().CPlusPlus)
1270 return LV_InvalidExpression;
1271
1272 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1273 // everywhere there's an object converted to an rvalue. Also, any other
1274 // casts should be wrapped by ImplicitCastExprs. There's just the special
1275 // case involving throws to work out.
1276 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001277 Expr *True = Cond->getTrueExpr();
1278 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001279 // C++0x 5.16p2
1280 // If either the second or the third operand has type (cv) void, [...]
1281 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001282 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001283 return LV_InvalidExpression;
1284
1285 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001286 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001287 return LV_InvalidExpression;
1288
1289 // That's it.
1290 return LV_Valid;
1291 }
1292
Douglas Gregor2d48e782009-12-19 07:07:47 +00001293 case Expr::CXXExprWithTemporariesClass:
1294 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1295
1296 case Expr::ObjCMessageExprClass:
1297 if (const ObjCMethodDecl *Method
1298 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1299 if (Method->getResultType()->isLValueReferenceType())
1300 return LV_Valid;
1301 break;
1302
Douglas Gregore873fb72010-02-16 21:39:57 +00001303 case Expr::CXXConstructExprClass:
1304 case Expr::CXXTemporaryObjectExprClass:
1305 case Expr::CXXZeroInitValueExprClass:
1306 return LV_ClassTemporary;
1307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 default:
1309 break;
1310 }
1311 return LV_InvalidExpression;
1312}
1313
1314/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1315/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001316/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001317/// recursively, any member or element of all contained aggregates or unions)
1318/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001319Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001320Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001321 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001324 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001325 // C++ 3.10p11: Functions cannot be modified, but pointers to
1326 // functions can be modifiable.
1327 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1328 return MLV_NotObjectType;
1329 break;
1330
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 case LV_NotObjectType: return MLV_NotObjectType;
1332 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001333 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001334 case LV_InvalidExpression:
1335 // If the top level is a C-style cast, and the subexpression is a valid
1336 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1337 // GCC extension. We don't support it, but we want to produce good
1338 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001339 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1340 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1341 if (Loc)
1342 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001343 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001344 }
1345 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001346 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001347 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001348 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001349 case LV_ClassTemporary:
1350 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001352
1353 // The following is illegal:
1354 // void takeclosure(void (^C)(void));
1355 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1356 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001357 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001358 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1359 return MLV_NotBlockQualified;
1360 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001361
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001362 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001363 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001364 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1365 if (Expr->getSetterMethod() == 0)
1366 return MLV_NoSetterProperty;
1367 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001368
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001369 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001371 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001373 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001375 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Ted Kremenek6217b802009-07-29 21:53:49 +00001378 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001379 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 return MLV_ConstQualified;
1381 }
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Mike Stump1eb44332009-09-09 15:08:12 +00001383 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001384}
1385
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001386/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001387/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001388bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001389 switch (getStmtClass()) {
1390 default:
1391 return false;
1392 case ObjCIvarRefExprClass:
1393 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001394 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001395 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001396 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001397 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001398 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001399 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001400 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001401 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001402 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001403 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001404 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1405 if (VD->hasGlobalStorage())
1406 return true;
1407 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001408 // dereferencing to a pointer is always a gc'able candidate,
1409 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001410 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001411 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001412 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001413 return false;
1414 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001415 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001416 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001417 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001418 }
1419 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001420 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001421 }
1422}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001423Expr* Expr::IgnoreParens() {
1424 Expr* E = this;
1425 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1426 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001428 return E;
1429}
1430
Chris Lattner56f34942008-02-13 01:02:39 +00001431/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1432/// or CastExprs or ImplicitCastExprs, returning their operand.
1433Expr *Expr::IgnoreParenCasts() {
1434 Expr *E = this;
1435 while (true) {
1436 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1437 E = P->getSubExpr();
1438 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1439 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001440 else
1441 return E;
1442 }
1443}
1444
Chris Lattnerecdd8412009-03-13 17:28:01 +00001445/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1446/// value (including ptr->int casts of the same size). Strip off any
1447/// ParenExpr or CastExprs, returning their operand.
1448Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1449 Expr *E = this;
1450 while (true) {
1451 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1452 E = P->getSubExpr();
1453 continue;
1454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Chris Lattnerecdd8412009-03-13 17:28:01 +00001456 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1457 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1458 // ptr<->int casts of the same width. We also ignore all identify casts.
1459 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Chris Lattnerecdd8412009-03-13 17:28:01 +00001461 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1462 E = SE;
1463 continue;
1464 }
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Chris Lattnerecdd8412009-03-13 17:28:01 +00001466 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1467 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1468 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1469 E = SE;
1470 continue;
1471 }
1472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattnerecdd8412009-03-13 17:28:01 +00001474 return E;
1475 }
1476}
1477
Douglas Gregor6eef5192009-12-14 19:27:10 +00001478bool Expr::isDefaultArgument() const {
1479 const Expr *E = this;
1480 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1481 E = ICE->getSubExprAsWritten();
1482
1483 return isa<CXXDefaultArgExpr>(E);
1484}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001485
Douglas Gregor898574e2008-12-05 23:32:09 +00001486/// hasAnyTypeDependentArguments - Determines if any of the expressions
1487/// in Exprs is type-dependent.
1488bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1489 for (unsigned I = 0; I < NumExprs; ++I)
1490 if (Exprs[I]->isTypeDependent())
1491 return true;
1492
1493 return false;
1494}
1495
1496/// hasAnyValueDependentArguments - Determines if any of the expressions
1497/// in Exprs is value-dependent.
1498bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1499 for (unsigned I = 0; I < NumExprs; ++I)
1500 if (Exprs[I]->isValueDependent())
1501 return true;
1502
1503 return false;
1504}
1505
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001506bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001507 // This function is attempting whether an expression is an initializer
1508 // which can be evaluated at compile-time. isEvaluatable handles most
1509 // of the cases, but it can't deal with some initializer-specific
1510 // expressions, and it can't deal with aggregates; we deal with those here,
1511 // and fall back to isEvaluatable for the other cases.
1512
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001513 // FIXME: This function assumes the variable being assigned to
1514 // isn't a reference type!
1515
Anders Carlssone8a32b82008-11-24 05:23:59 +00001516 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001517 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001518 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001519 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001520 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001521 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001522 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001523 // This handles gcc's extension that allows global initializers like
1524 // "struct x {int x;} x = (struct x) {};".
1525 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001526 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001527 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001528 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001529 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001530 // FIXME: This doesn't deal with fields with reference types correctly.
1531 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1532 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001533 const InitListExpr *Exp = cast<InitListExpr>(this);
1534 unsigned numInits = Exp->getNumInits();
1535 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001536 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001537 return false;
1538 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001539 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001540 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001541 case ImplicitValueInitExprClass:
1542 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001543 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001544 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001545 case UnaryOperatorClass: {
1546 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1547 if (Exp->getOpcode() == UnaryOperator::Extension)
1548 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1549 break;
1550 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001551 case BinaryOperatorClass: {
1552 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1553 // but this handles the common case.
1554 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1555 if (Exp->getOpcode() == BinaryOperator::Sub &&
1556 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1557 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1558 return true;
1559 break;
1560 }
Chris Lattner81045d82009-04-21 05:19:11 +00001561 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001562 case CStyleCastExprClass:
1563 // Handle casts with a destination that's a struct or union; this
1564 // deals with both the gcc no-op struct cast extension and the
1565 // cast-to-union extension.
1566 if (getType()->isRecordType())
1567 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001568
1569 // Integer->integer casts can be handled here, which is important for
1570 // things like (int)(&&x-&&y). Scary but true.
1571 if (getType()->isIntegerType() &&
1572 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1573 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1574
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001575 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001576 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001577 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001578}
1579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001581/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001582
1583/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1584/// comma, etc
1585///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001586/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1587/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1588/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001589
Eli Friedmane28d7192009-02-26 09:29:13 +00001590// CheckICE - This function does the fundamental ICE checking: the returned
1591// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1592// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001593// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001594// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001595// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001596//
1597// Meanings of Val:
1598// 0: This expression is an ICE if it can be evaluated by Evaluate.
1599// 1: This expression is not an ICE, but if it isn't evaluated, it's
1600// a legal subexpression for an ICE. This return value is used to handle
1601// the comma operator in C99 mode.
1602// 2: This expression is not an ICE, and is not a legal subexpression for one.
1603
1604struct ICEDiag {
1605 unsigned Val;
1606 SourceLocation Loc;
1607
1608 public:
1609 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1610 ICEDiag() : Val(0) {}
1611};
1612
1613ICEDiag NoDiag() { return ICEDiag(); }
1614
Eli Friedman60ce9632009-02-27 04:07:58 +00001615static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1616 Expr::EvalResult EVResult;
1617 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1618 !EVResult.Val.isInt()) {
1619 return ICEDiag(2, E->getLocStart());
1620 }
1621 return NoDiag();
1622}
1623
Eli Friedmane28d7192009-02-26 09:29:13 +00001624static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001625 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001626 if (!E->getType()->isIntegralType()) {
1627 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001628 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001629
1630 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001631#define STMT(Node, Base) case Expr::Node##Class:
1632#define EXPR(Node, Base)
1633#include "clang/AST/StmtNodes.def"
1634 case Expr::PredefinedExprClass:
1635 case Expr::FloatingLiteralClass:
1636 case Expr::ImaginaryLiteralClass:
1637 case Expr::StringLiteralClass:
1638 case Expr::ArraySubscriptExprClass:
1639 case Expr::MemberExprClass:
1640 case Expr::CompoundAssignOperatorClass:
1641 case Expr::CompoundLiteralExprClass:
1642 case Expr::ExtVectorElementExprClass:
1643 case Expr::InitListExprClass:
1644 case Expr::DesignatedInitExprClass:
1645 case Expr::ImplicitValueInitExprClass:
1646 case Expr::ParenListExprClass:
1647 case Expr::VAArgExprClass:
1648 case Expr::AddrLabelExprClass:
1649 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001650 case Expr::CXXMemberCallExprClass:
1651 case Expr::CXXDynamicCastExprClass:
1652 case Expr::CXXTypeidExprClass:
1653 case Expr::CXXNullPtrLiteralExprClass:
1654 case Expr::CXXThisExprClass:
1655 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001656 case Expr::CXXNewExprClass:
1657 case Expr::CXXDeleteExprClass:
1658 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001659 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001660 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001661 case Expr::CXXConstructExprClass:
1662 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001663 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001664 case Expr::CXXExprWithTemporariesClass:
1665 case Expr::CXXTemporaryObjectExprClass:
1666 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001667 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001668 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001669 case Expr::ObjCStringLiteralClass:
1670 case Expr::ObjCEncodeExprClass:
1671 case Expr::ObjCMessageExprClass:
1672 case Expr::ObjCSelectorExprClass:
1673 case Expr::ObjCProtocolExprClass:
1674 case Expr::ObjCIvarRefExprClass:
1675 case Expr::ObjCPropertyRefExprClass:
1676 case Expr::ObjCImplicitSetterGetterRefExprClass:
1677 case Expr::ObjCSuperExprClass:
1678 case Expr::ObjCIsaExprClass:
1679 case Expr::ShuffleVectorExprClass:
1680 case Expr::BlockExprClass:
1681 case Expr::BlockDeclRefExprClass:
1682 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001683 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001684
Douglas Gregor043cad22009-09-11 00:18:58 +00001685 case Expr::GNUNullExprClass:
1686 // GCC considers the GNU __null value to be an integral constant expression.
1687 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001688
Eli Friedmane28d7192009-02-26 09:29:13 +00001689 case Expr::ParenExprClass:
1690 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1691 case Expr::IntegerLiteralClass:
1692 case Expr::CharacterLiteralClass:
1693 case Expr::CXXBoolLiteralExprClass:
1694 case Expr::CXXZeroInitValueExprClass:
1695 case Expr::TypesCompatibleExprClass:
1696 case Expr::UnaryTypeTraitExprClass:
1697 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001698 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001699 case Expr::CXXOperatorCallExprClass: {
1700 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001701 if (CE->isBuiltinCall(Ctx))
1702 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001703 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001704 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001705 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001706 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1707 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001708 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001709 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001710 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1711
1712 // Parameter variables are never constants. Without this check,
1713 // getAnyInitializer() can find a default argument, which leads
1714 // to chaos.
1715 if (isa<ParmVarDecl>(D))
1716 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1717
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001718 // C++ 7.1.5.1p2
1719 // A variable of non-volatile const-qualified integral or enumeration
1720 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001721 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001722 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1723 if (Quals.hasVolatile() || !Quals.hasConst())
1724 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1725
Sebastian Redl31310a22010-02-01 20:16:42 +00001726 // Look for a declaration of this variable that has an initializer.
1727 const VarDecl *ID = 0;
1728 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001729 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001730 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001731 // We have already checked whether this subexpression is an
1732 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001733 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001734 return NoDiag();
1735 else
1736 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1737 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001738
John McCall1f1b3b32010-02-06 01:07:37 +00001739 // It's an ICE whether or not the definition we found is
1740 // out-of-line. See DR 721 and the discussion in Clang PR
1741 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001742
1743 if (Dcl->isCheckingICE()) {
1744 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1745 }
1746
1747 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001748 ICEDiag Result = CheckICE(Init, Ctx);
1749 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001750 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001751 return Result;
1752 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001753 }
1754 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001755 return ICEDiag(2, E->getLocStart());
1756 case Expr::UnaryOperatorClass: {
1757 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001759 case UnaryOperator::PostInc:
1760 case UnaryOperator::PostDec:
1761 case UnaryOperator::PreInc:
1762 case UnaryOperator::PreDec:
1763 case UnaryOperator::AddrOf:
1764 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001765 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001766
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001768 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001772 case UnaryOperator::Real:
1773 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001774 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001775 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001776 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1777 // Evaluate matches the proposed gcc behavior for cases like
1778 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1779 // compliance: we should warn earlier for offsetof expressions with
1780 // array subscripts that aren't ICEs, and if the array subscripts
1781 // are ICEs, the value of the offsetof must be an integer constant.
1782 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001785 case Expr::SizeOfAlignOfExprClass: {
1786 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1787 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1788 return ICEDiag(2, E->getLocStart());
1789 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001791 case Expr::BinaryOperatorClass: {
1792 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001794 case BinaryOperator::PtrMemD:
1795 case BinaryOperator::PtrMemI:
1796 case BinaryOperator::Assign:
1797 case BinaryOperator::MulAssign:
1798 case BinaryOperator::DivAssign:
1799 case BinaryOperator::RemAssign:
1800 case BinaryOperator::AddAssign:
1801 case BinaryOperator::SubAssign:
1802 case BinaryOperator::ShlAssign:
1803 case BinaryOperator::ShrAssign:
1804 case BinaryOperator::AndAssign:
1805 case BinaryOperator::XorAssign:
1806 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001807 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001808
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001812 case BinaryOperator::Add:
1813 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001816 case BinaryOperator::LT:
1817 case BinaryOperator::GT:
1818 case BinaryOperator::LE:
1819 case BinaryOperator::GE:
1820 case BinaryOperator::EQ:
1821 case BinaryOperator::NE:
1822 case BinaryOperator::And:
1823 case BinaryOperator::Xor:
1824 case BinaryOperator::Or:
1825 case BinaryOperator::Comma: {
1826 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1827 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001828 if (Exp->getOpcode() == BinaryOperator::Div ||
1829 Exp->getOpcode() == BinaryOperator::Rem) {
1830 // Evaluate gives an error for undefined Div/Rem, so make sure
1831 // we don't evaluate one.
1832 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1833 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1834 if (REval == 0)
1835 return ICEDiag(1, E->getLocStart());
1836 if (REval.isSigned() && REval.isAllOnesValue()) {
1837 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1838 if (LEval.isMinSignedValue())
1839 return ICEDiag(1, E->getLocStart());
1840 }
1841 }
1842 }
1843 if (Exp->getOpcode() == BinaryOperator::Comma) {
1844 if (Ctx.getLangOptions().C99) {
1845 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1846 // if it isn't evaluated.
1847 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1848 return ICEDiag(1, E->getLocStart());
1849 } else {
1850 // In both C89 and C++, commas in ICEs are illegal.
1851 return ICEDiag(2, E->getLocStart());
1852 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001853 }
1854 if (LHSResult.Val >= RHSResult.Val)
1855 return LHSResult;
1856 return RHSResult;
1857 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001858 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001859 case BinaryOperator::LOr: {
1860 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1861 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1862 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1863 // Rare case where the RHS has a comma "side-effect"; we need
1864 // to actually check the condition to see whether the side
1865 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001866 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001867 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001868 return RHSResult;
1869 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001870 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001871
Eli Friedmane28d7192009-02-26 09:29:13 +00001872 if (LHSResult.Val >= RHSResult.Val)
1873 return LHSResult;
1874 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001876 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001878 case Expr::ImplicitCastExprClass:
1879 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001880 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001881 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001882 case Expr::CXXStaticCastExprClass:
1883 case Expr::CXXReinterpretCastExprClass:
1884 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001885 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1886 if (SubExpr->getType()->isIntegralType())
1887 return CheckICE(SubExpr, Ctx);
1888 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1889 return NoDiag();
1890 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001892 case Expr::ConditionalOperatorClass: {
1893 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001894 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001895 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001896 // expression, and it is fully evaluated. This is an important GNU
1897 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001898 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001899 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001900 Expr::EvalResult EVResult;
1901 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1902 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001903 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001904 }
1905 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001906 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001907 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1908 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1909 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1910 if (CondResult.Val == 2)
1911 return CondResult;
1912 if (TrueResult.Val == 2)
1913 return TrueResult;
1914 if (FalseResult.Val == 2)
1915 return FalseResult;
1916 if (CondResult.Val == 1)
1917 return CondResult;
1918 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1919 return NoDiag();
1920 // Rare case where the diagnostics depend on which side is evaluated
1921 // Note that if we get here, CondResult is 0, and at least one of
1922 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001923 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001924 return FalseResult;
1925 }
1926 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001928 case Expr::CXXDefaultArgExprClass:
1929 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001930 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001931 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001932 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001934
Douglas Gregorf2991242009-09-10 23:31:45 +00001935 // Silence a GCC warning
1936 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001937}
Reid Spencer5f016e22007-07-11 17:01:13 +00001938
Eli Friedmane28d7192009-02-26 09:29:13 +00001939bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1940 SourceLocation *Loc, bool isEvaluated) const {
1941 ICEDiag d = CheckICE(this, Ctx);
1942 if (d.Val != 0) {
1943 if (Loc) *Loc = d.Loc;
1944 return false;
1945 }
1946 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001947 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001948 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001949 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1950 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001951 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 return true;
1953}
1954
Reid Spencer5f016e22007-07-11 17:01:13 +00001955/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1956/// integer constant expression with the value zero, or if this is one that is
1957/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001958bool Expr::isNullPointerConstant(ASTContext &Ctx,
1959 NullPointerConstantValueDependence NPC) const {
1960 if (isValueDependent()) {
1961 switch (NPC) {
1962 case NPC_NeverValueDependent:
1963 assert(false && "Unexpected value dependent expression!");
1964 // If the unthinkable happens, fall through to the safest alternative.
1965
1966 case NPC_ValueDependentIsNull:
1967 return isTypeDependent() || getType()->isIntegralType();
1968
1969 case NPC_ValueDependentIsNotNull:
1970 return false;
1971 }
1972 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001973
Sebastian Redl07779722008-10-31 14:43:28 +00001974 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001975 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001976 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001977 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001978 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001979 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001980 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001981 Pointee->isVoidType() && // to void*
1982 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001983 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001984 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001986 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1987 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001988 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001989 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1990 // Accept ((void*)0) as a null pointer constant, as many other
1991 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001992 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001993 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001994 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001995 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001996 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001997 } else if (isa<GNUNullExpr>(this)) {
1998 // The GNU __null extension is always a null pointer constant.
1999 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002000 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002001
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002002 // C++0x nullptr_t is always a null pointer constant.
2003 if (getType()->isNullPtrType())
2004 return true;
2005
Steve Naroffaa58f002008-01-14 16:10:57 +00002006 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002007 if (!getType()->isIntegerType() ||
2008 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002009 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 // If we have an integer constant expression, we need to *evaluate* it and
2012 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002013 llvm::APSInt Result;
2014 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002015}
Steve Naroff31a45842007-07-28 23:10:27 +00002016
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002017FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002018 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002019
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002020 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2021 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2022 E = ICE->getSubExpr()->IgnoreParens();
2023 else
2024 break;
2025 }
2026
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002027 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002028 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002029 if (Field->isBitField())
2030 return Field;
2031
2032 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2033 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2034 return BinOp->getLHS()->getBitField();
2035
2036 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002037}
2038
Anders Carlsson09380262010-01-31 17:18:49 +00002039bool Expr::refersToVectorElement() const {
2040 const Expr *E = this->IgnoreParens();
2041
2042 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2043 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2044 E = ICE->getSubExpr()->IgnoreParens();
2045 else
2046 break;
2047 }
2048
2049 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2050 return ASE->getBase()->getType()->isVectorType();
2051
2052 if (isa<ExtVectorElementExpr>(E))
2053 return true;
2054
2055 return false;
2056}
2057
Chris Lattner2140e902009-02-16 22:14:05 +00002058/// isArrow - Return true if the base expression is a pointer to vector,
2059/// return false if the base expression is a vector.
2060bool ExtVectorElementExpr::isArrow() const {
2061 return getBase()->getType()->isPointerType();
2062}
2063
Nate Begeman213541a2008-04-18 23:10:10 +00002064unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002065 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002066 return VT->getNumElements();
2067 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002068}
2069
Nate Begeman8a997642008-05-09 06:41:27 +00002070/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002071bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002072 // FIXME: Refactor this code to an accessor on the AST node which returns the
2073 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002074 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002075
2076 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002077 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002078 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002079
Nate Begeman190d6a22009-01-18 02:01:21 +00002080 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002081 if (Comp[0] == 's' || Comp[0] == 'S')
2082 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002083
Daniel Dunbar15027422009-10-17 23:53:04 +00002084 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2085 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002086 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002087
Steve Narofffec0b492007-07-30 03:29:09 +00002088 return false;
2089}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002090
Nate Begeman8a997642008-05-09 06:41:27 +00002091/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002092void ExtVectorElementExpr::getEncodedElementAccess(
2093 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002094 llvm::StringRef Comp = Accessor->getName();
2095 if (Comp[0] == 's' || Comp[0] == 'S')
2096 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002098 bool isHi = Comp == "hi";
2099 bool isLo = Comp == "lo";
2100 bool isEven = Comp == "even";
2101 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Nate Begeman8a997642008-05-09 06:41:27 +00002103 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2104 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Nate Begeman8a997642008-05-09 06:41:27 +00002106 if (isHi)
2107 Index = e + i;
2108 else if (isLo)
2109 Index = i;
2110 else if (isEven)
2111 Index = 2 * i;
2112 else if (isOdd)
2113 Index = 2 * i + 1;
2114 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002115 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002116
Nate Begeman3b8d1162008-05-13 21:03:02 +00002117 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002118 }
Nate Begeman8a997642008-05-09 06:41:27 +00002119}
2120
Steve Naroff68d331a2007-09-27 14:38:14 +00002121// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002122ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2123 Selector selInfo,
2124 QualType retType, ObjCMethodDecl *mproto,
2125 SourceLocation LBrac, SourceLocation RBrac,
2126 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002127 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002128 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002129 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002130 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002131 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002132 if (NumArgs) {
2133 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002134 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2135 }
Steve Naroff563477d2007-09-18 23:55:05 +00002136 LBracloc = LBrac;
2137 RBracloc = RBrac;
2138}
2139
Mike Stump1eb44332009-09-09 15:08:12 +00002140// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002141// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002142ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002143 SourceLocation clsNameLoc, Selector selInfo,
2144 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002145 SourceLocation LBrac, SourceLocation RBrac,
2146 Expr **ArgExprs, unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002147 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2148 SelName(selInfo), MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002149 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002150 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002151 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002152 if (NumArgs) {
2153 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002154 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2155 }
Steve Naroff563477d2007-09-18 23:55:05 +00002156 LBracloc = LBrac;
2157 RBracloc = RBrac;
2158}
2159
Mike Stump1eb44332009-09-09 15:08:12 +00002160// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002161ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002162 SourceLocation clsNameLoc, Selector selInfo,
2163 QualType retType,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002164 ObjCMethodDecl *mproto, SourceLocation LBrac,
2165 SourceLocation RBrac, Expr **ArgExprs,
2166 unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002167 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2168 SelName(selInfo), MethodProto(mproto)
2169{
Ted Kremenek4df728e2008-06-24 15:50:53 +00002170 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002171 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002172 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2173 if (NumArgs) {
2174 for (unsigned i = 0; i != NumArgs; ++i)
2175 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2176 }
2177 LBracloc = LBrac;
2178 RBracloc = RBrac;
2179}
2180
2181ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2182 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2183 switch (x & Flags) {
2184 default:
2185 assert(false && "Invalid ObjCMessageExpr.");
2186 case IsInstMeth:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002187 return ClassInfo(0, 0, SourceLocation());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002188 case IsClsMethDeclUnknown:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002189 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002190 case IsClsMethDeclKnown: {
2191 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002192 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002193 }
2194 }
2195}
2196
Chris Lattner0389e6b2009-04-26 00:44:05 +00002197void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorc2350e52010-03-08 16:40:19 +00002198 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner0389e6b2009-04-26 00:44:05 +00002199 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002200 return;
2201 }
2202
2203 if (CI.Decl == 0)
2204 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner0389e6b2009-04-26 00:44:05 +00002205 else
Douglas Gregorc2350e52010-03-08 16:40:19 +00002206 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2207 ClassNameLoc = CI.Loc;
Chris Lattner0389e6b2009-04-26 00:44:05 +00002208}
2209
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002210void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2211 DestroyChildren(C);
2212 if (SubExprs)
2213 C.Deallocate(SubExprs);
2214 this->~ObjCMessageExpr();
2215 C.Deallocate((void*) this);
2216}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002217
Chris Lattner27437ca2007-10-25 00:29:32 +00002218bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002219 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002220}
2221
Nate Begeman888376a2009-08-12 02:28:50 +00002222void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2223 unsigned NumExprs) {
2224 if (SubExprs) C.Deallocate(SubExprs);
2225
2226 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002227 this->NumExprs = NumExprs;
2228 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002229}
Nate Begeman888376a2009-08-12 02:28:50 +00002230
2231void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2232 DestroyChildren(C);
2233 if (SubExprs) C.Deallocate(SubExprs);
2234 this->~ShuffleVectorExpr();
2235 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002236}
2237
Douglas Gregor42602bb2009-08-07 06:08:38 +00002238void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002239 // Override default behavior of traversing children. If this has a type
2240 // operand and the type is a variable-length array, the child iteration
2241 // will iterate over the size expression. However, this expression belongs
2242 // to the type, not to this, so we don't want to delete it.
2243 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002244 if (isArgumentType()) {
2245 this->~SizeOfAlignOfExpr();
2246 C.Deallocate(this);
2247 }
Sebastian Redl05189992008-11-11 17:56:53 +00002248 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002249 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002250}
2251
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002252//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002253// DesignatedInitExpr
2254//===----------------------------------------------------------------------===//
2255
2256IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2257 assert(Kind == FieldDesignator && "Only valid on a field designator");
2258 if (Field.NameOrField & 0x01)
2259 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2260 else
2261 return getField()->getIdentifier();
2262}
2263
Douglas Gregor319d57f2010-01-06 23:17:19 +00002264DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2265 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002266 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002267 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002268 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002269 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002270 unsigned NumIndexExprs,
2271 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002272 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002273 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002274 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2275 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002276 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002277
2278 // Record the initializer itself.
2279 child_iterator Child = child_begin();
2280 *Child++ = Init;
2281
2282 // Copy the designators and their subexpressions, computing
2283 // value-dependence along the way.
2284 unsigned IndexIdx = 0;
2285 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002286 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002287
2288 if (this->Designators[I].isArrayDesignator()) {
2289 // Compute type- and value-dependence.
2290 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002291 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002292 Index->isTypeDependent() || Index->isValueDependent();
2293
2294 // Copy the index expressions into permanent storage.
2295 *Child++ = IndexExprs[IndexIdx++];
2296 } else if (this->Designators[I].isArrayRangeDesignator()) {
2297 // Compute type- and value-dependence.
2298 Expr *Start = IndexExprs[IndexIdx];
2299 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002300 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002301 Start->isTypeDependent() || Start->isValueDependent() ||
2302 End->isTypeDependent() || End->isValueDependent();
2303
2304 // Copy the start/end expressions into permanent storage.
2305 *Child++ = IndexExprs[IndexIdx++];
2306 *Child++ = IndexExprs[IndexIdx++];
2307 }
2308 }
2309
2310 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002311}
2312
Douglas Gregor05c13a32009-01-22 00:58:24 +00002313DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002314DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002315 unsigned NumDesignators,
2316 Expr **IndexExprs, unsigned NumIndexExprs,
2317 SourceLocation ColonOrEqualLoc,
2318 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002319 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002320 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002321 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002322 ColonOrEqualLoc, UsesColonSyntax,
2323 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002324}
2325
Mike Stump1eb44332009-09-09 15:08:12 +00002326DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002327 unsigned NumIndexExprs) {
2328 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2329 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2330 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2331}
2332
Douglas Gregor319d57f2010-01-06 23:17:19 +00002333void DesignatedInitExpr::setDesignators(ASTContext &C,
2334 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002335 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002336 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002337
Douglas Gregor319d57f2010-01-06 23:17:19 +00002338 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002339 NumDesignators = NumDesigs;
2340 for (unsigned I = 0; I != NumDesigs; ++I)
2341 Designators[I] = Desigs[I];
2342}
2343
Douglas Gregor05c13a32009-01-22 00:58:24 +00002344SourceRange DesignatedInitExpr::getSourceRange() const {
2345 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002346 Designator &First =
2347 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002348 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002349 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002350 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2351 else
2352 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2353 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002354 StartLoc =
2355 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002356 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2357}
2358
Douglas Gregor05c13a32009-01-22 00:58:24 +00002359Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2360 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2361 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2362 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002363 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2364 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2365}
2366
2367Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002368 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002369 "Requires array range designator");
2370 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2371 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002372 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2373 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2374}
2375
2376Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002377 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002378 "Requires array range designator");
2379 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2380 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002381 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2382 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2383}
2384
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002385/// \brief Replaces the designator at index @p Idx with the series
2386/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002387void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002388 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002389 const Designator *Last) {
2390 unsigned NumNewDesignators = Last - First;
2391 if (NumNewDesignators == 0) {
2392 std::copy_backward(Designators + Idx + 1,
2393 Designators + NumDesignators,
2394 Designators + Idx);
2395 --NumNewDesignators;
2396 return;
2397 } else if (NumNewDesignators == 1) {
2398 Designators[Idx] = *First;
2399 return;
2400 }
2401
Mike Stump1eb44332009-09-09 15:08:12 +00002402 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002403 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002404 std::copy(Designators, Designators + Idx, NewDesignators);
2405 std::copy(First, Last, NewDesignators + Idx);
2406 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2407 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002408 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002409 Designators = NewDesignators;
2410 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2411}
2412
Douglas Gregor42602bb2009-08-07 06:08:38 +00002413void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002414 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002415 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002416}
2417
Douglas Gregor319d57f2010-01-06 23:17:19 +00002418void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2419 for (unsigned I = 0; I != NumDesignators; ++I)
2420 Designators[I].~Designator();
2421 C.Deallocate(Designators);
2422 Designators = 0;
2423}
2424
Mike Stump1eb44332009-09-09 15:08:12 +00002425ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002426 Expr **exprs, unsigned nexprs,
2427 SourceLocation rparenloc)
2428: Expr(ParenListExprClass, QualType(),
2429 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002430 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002431 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Nate Begeman2ef13e52009-08-10 23:49:36 +00002433 Exprs = new (C) Stmt*[nexprs];
2434 for (unsigned i = 0; i != nexprs; ++i)
2435 Exprs[i] = exprs[i];
2436}
2437
2438void ParenListExpr::DoDestroy(ASTContext& C) {
2439 DestroyChildren(C);
2440 if (Exprs) C.Deallocate(Exprs);
2441 this->~ParenListExpr();
2442 C.Deallocate(this);
2443}
2444
Douglas Gregor05c13a32009-01-22 00:58:24 +00002445//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002446// ExprIterator.
2447//===----------------------------------------------------------------------===//
2448
2449Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2450Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2451Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2452const Expr* ConstExprIterator::operator[](size_t idx) const {
2453 return cast<Expr>(I[idx]);
2454}
2455const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2456const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2457
2458//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002459// Child Iterators for iterating over subexpressions/substatements
2460//===----------------------------------------------------------------------===//
2461
2462// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002463Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2464Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002465
Steve Naroff7779db42007-11-12 14:29:37 +00002466// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002467Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2468Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002469
Steve Naroffe3e9add2008-06-02 23:03:37 +00002470// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002471Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2472Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002473
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002474// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002475Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2476 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002477}
Mike Stump1eb44332009-09-09 15:08:12 +00002478Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2479 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002480}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002481
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002482// ObjCSuperExpr
2483Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2484Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2485
Steve Narofff242b1b2009-07-24 17:54:45 +00002486// ObjCIsaExpr
2487Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2488Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2489
Chris Lattnerd9f69102008-08-10 01:53:14 +00002490// PredefinedExpr
2491Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2492Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002493
2494// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002495Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2496Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002497
2498// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002499Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002500Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002501
2502// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002503Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2504Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002505
Chris Lattner5d661452007-08-26 03:42:43 +00002506// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002507Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2508Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002509
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002510// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002511Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2512Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002513
2514// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002515Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2516Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002517
2518// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002519Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2520Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002521
Sebastian Redl05189992008-11-11 17:56:53 +00002522// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002523Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002524 // If this is of a type and the type is a VLA type (and not a typedef), the
2525 // size expression of the VLA needs to be treated as an executable expression.
2526 // Why isn't this weirdness documented better in StmtIterator?
2527 if (isArgumentType()) {
2528 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2529 getArgumentType().getTypePtr()))
2530 return child_iterator(T);
2531 return child_iterator();
2532 }
Sebastian Redld4575892008-12-03 23:17:54 +00002533 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002534}
Sebastian Redl05189992008-11-11 17:56:53 +00002535Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2536 if (isArgumentType())
2537 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002538 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002539}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002540
2541// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002542Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002543 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002544}
Ted Kremenek1237c672007-08-24 20:06:47 +00002545Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002546 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002547}
2548
2549// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002550Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002551 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002552}
Ted Kremenek1237c672007-08-24 20:06:47 +00002553Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002554 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002555}
Ted Kremenek1237c672007-08-24 20:06:47 +00002556
2557// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002558Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2559Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002560
Nate Begeman213541a2008-04-18 23:10:10 +00002561// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002562Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2563Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002564
2565// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002566Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2567Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002568
Ted Kremenek1237c672007-08-24 20:06:47 +00002569// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002570Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2571Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002572
2573// BinaryOperator
2574Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002575 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002576}
Ted Kremenek1237c672007-08-24 20:06:47 +00002577Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002578 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002579}
2580
2581// ConditionalOperator
2582Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002583 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002584}
Ted Kremenek1237c672007-08-24 20:06:47 +00002585Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002586 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002587}
2588
2589// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002590Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2591Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002592
Ted Kremenek1237c672007-08-24 20:06:47 +00002593// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002594Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2595Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002596
2597// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002598Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2599 return child_iterator();
2600}
2601
2602Stmt::child_iterator TypesCompatibleExpr::child_end() {
2603 return child_iterator();
2604}
Ted Kremenek1237c672007-08-24 20:06:47 +00002605
2606// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002607Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2608Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002609
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002610// GNUNullExpr
2611Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2612Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2613
Eli Friedmand38617c2008-05-14 19:38:39 +00002614// ShuffleVectorExpr
2615Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002616 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002617}
2618Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002619 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002620}
2621
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002622// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002623Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2624Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002625
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002626// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002627Stmt::child_iterator InitListExpr::child_begin() {
2628 return InitExprs.size() ? &InitExprs[0] : 0;
2629}
2630Stmt::child_iterator InitListExpr::child_end() {
2631 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2632}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002633
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002634// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002635Stmt::child_iterator DesignatedInitExpr::child_begin() {
2636 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2637 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002638 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2639}
2640Stmt::child_iterator DesignatedInitExpr::child_end() {
2641 return child_iterator(&*child_begin() + NumSubExprs);
2642}
2643
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002644// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002645Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2646 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002647}
2648
Mike Stump1eb44332009-09-09 15:08:12 +00002649Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2650 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002651}
2652
Nate Begeman2ef13e52009-08-10 23:49:36 +00002653// ParenListExpr
2654Stmt::child_iterator ParenListExpr::child_begin() {
2655 return &Exprs[0];
2656}
2657Stmt::child_iterator ParenListExpr::child_end() {
2658 return &Exprs[0]+NumExprs;
2659}
2660
Ted Kremenek1237c672007-08-24 20:06:47 +00002661// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002662Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002663 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002664}
2665Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002666 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002667}
Ted Kremenek1237c672007-08-24 20:06:47 +00002668
2669// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002670Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2671Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002672
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002673// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002674Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002675 return child_iterator();
2676}
2677Stmt::child_iterator ObjCSelectorExpr::child_end() {
2678 return child_iterator();
2679}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002680
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002681// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002682Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2683 return child_iterator();
2684}
2685Stmt::child_iterator ObjCProtocolExpr::child_end() {
2686 return child_iterator();
2687}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002688
Steve Naroff563477d2007-09-18 23:55:05 +00002689// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002690Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002691 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002692}
2693Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002694 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002695}
2696
Steve Naroff4eb206b2008-09-03 18:15:37 +00002697// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002698Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2699Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002700
Ted Kremenek9da13f92008-09-26 23:24:14 +00002701Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2702Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }