blob: 935a2080dccdee7a95633b35ec0b1e661d0ab704 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Douglas Gregorad2c6982010-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
Chris Lattner0eedafe2006-08-24 04:56:27 +000037//===----------------------------------------------------------------------===//
38// Primary Expressions.
39//===----------------------------------------------------------------------===//
40
John McCall6b51f282009-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 Gregored6c7442009-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 Gregor5fcb51c2010-01-15 16:21:02 +0000108 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000109 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000110 if (Init->isValueDependent())
111 ValueDependent = true;
112 }
Douglas Gregored6c7442009-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 Gregor4bd90e52009-10-23 18:54:35 +0000119DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
120 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000121 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000122 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000123 QualType T)
124 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000125 DecoratedD(D,
126 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000127 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-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 McCall6b51f282009-11-23 01:53:49 +0000135 if (TemplateArgs)
136 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000137
138 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000139}
140
141DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
142 NestedNameSpecifier *Qualifier,
143 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000144 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000145 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000146 QualType T,
147 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000148 std::size_t Size = sizeof(DeclRefExpr);
149 if (Qualifier != 0)
150 Size += sizeof(NameQualifier);
151
John McCall6b51f282009-11-23 01:53:49 +0000152 if (TemplateArgs)
153 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregor4bd90e52009-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 Gregored6c7442009-11-23 11:41:28 +0000157 TemplateArgs, T);
Douglas Gregor4bd90e52009-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 Carlsson2fb08242009-09-08 18:24:21 +0000171// FIXME: Maybe this should use DeclPrinter with a special "print predefined
172// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000173std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
174 ASTContext &Context = CurrentDecl->getASTContext();
175
Anders Carlsson2fb08242009-09-08 18:24:21 +0000176 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000177 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-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 Carlsson5bd8d192010-02-11 18:20:28 +0000184 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000185 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000186 if (MD->isStatic())
187 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000188 }
189
190 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000191
192 std::string Proto = FD->getQualifiedNameAsString(Policy);
193
John McCall9dd450b2009-09-21 23:43:11 +0000194 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-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 Weinig4e83bd22009-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 Weinigd060ed42009-12-06 23:55:13 +0000224 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
225 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-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 Kremenek361ffd92010-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 Carlsson2fb08242009-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 Lattnera0173132008-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 Johannesenc48814b2008-10-09 23:02:32 +0000268 bool ignored;
269 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
270 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000271 return V.convertToDouble();
272}
273
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000274StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
275 unsigned ByteLength, bool Wide,
276 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000277 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000278 unsigned NumStrs) {
Chris Lattnerf83b5af2009-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 Stump11289f42009-09-09 15:08:12 +0000285
Steve Naroffdf7855b2007-02-21 23:46:25 +0000286 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-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;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000294
Chris Lattner630970d2009-02-18 05:49:11 +0000295 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000296 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
297 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000298}
299
Douglas Gregor958dfc92009-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 Gregore26a2852009-08-07 06:08:38 +0000311void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000312 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000313 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000314}
315
Daniel Dunbar36217882009-09-22 03:27:33 +0000316void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000317 if (StrData)
318 C.Deallocate(const_cast<char*>(StrData));
319
Daniel Dunbar36217882009-09-22 03:27:33 +0000320 char *AStrData = new (C, 1) char[Str.size()];
321 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000322 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000323 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000324}
325
Chris Lattner1b926492006-08-23 06:42:10 +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) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000330 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000331 case PostInc: return "++";
332 case PostDec: return "--";
333 case PreInc: return "++";
334 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000335 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";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000343 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000344 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000345 }
346}
347
Mike Stump11289f42009-09-09 15:08:12 +0000348UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000349UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
350 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000351 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-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 Gregor084d8552009-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
Chris Lattner0eedafe2006-08-24 04:56:27 +0000378//===----------------------------------------------------------------------===//
379// Postfix Operators.
380//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000381
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000382CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000383 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000384 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000385 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000386 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000387 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000388
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000389 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-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 Kremenekd7b4f402009-02-09 20:51:47 +0000393
Douglas Gregor993603d2008-11-14 16:09:21 +0000394 RParenLoc = rparenloc;
395}
Nate Begeman1e36a852008-01-17 17:46:27 +0000396
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000397CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
398 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000399 : Expr(CallExprClass, t,
400 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000401 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000402 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000403
404 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000405 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000406 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000407 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000408
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000409 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000410}
411
Mike Stump11289f42009-09-09 15:08:12 +0000412CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
413 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000414 SubExprs = new (C) Stmt*[1];
415}
416
Douglas Gregore26a2852009-08-07 06:08:38 +0000417void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000418 DestroyChildren(C);
419 if (SubExprs) C.Deallocate(SubExprs);
420 this->~CallExpr();
421 C.Deallocate(this);
422}
423
Nuno Lopes518e3702009-12-20 23:11:08 +0000424Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000425 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000426 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000427 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000428 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
429 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000430
431 return 0;
432}
433
Nuno Lopes518e3702009-12-20 23:11:08 +0000434FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000435 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000436}
437
Chris Lattnere4407ed2007-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 Kremenek5a201952009-02-07 01:47:29 +0000441void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000442 // No change, just return.
443 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnere4407ed2007-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 Kremenek5a201952009-02-07 01:47:29 +0000448 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-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 Dunbarec5ae3d2009-07-28 06:29:46 +0000454 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-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 Stump11289f42009-09-09 15:08:12 +0000461
Douglas Gregorba6e5572009-04-17 21:46:47 +0000462 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000463 SubExprs = NewSubExprs;
464 this->NumArgs = NumArgs;
465}
466
Chris Lattner01ff98a2008-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 Gregore711f702009-02-14 18:57:46 +0000469unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000470 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000471 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-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 Lattner01ff98a2008-10-06 05:00:53 +0000475 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000476
Steve Narofff6e3b3292008-01-31 01:07:12 +0000477 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
478 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000479 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000481 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
482 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000483 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000485 if (!FDecl->getIdentifier())
486 return 0;
487
Douglas Gregor15fc9562009-09-12 00:22:50 +0000488 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000489}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000490
Anders Carlsson00a27592009-05-26 04:57:27 +0000491QualType CallExpr::getCallReturnType() const {
492 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000493 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000494 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000495 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000496 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000497
John McCall9dd450b2009-09-21 23:43:11 +0000498 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000499 return FnType->getResultType();
500}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000501
Mike Stump11289f42009-09-09 15:08:12 +0000502MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000503 SourceRange qualrange, ValueDecl *memberdecl,
John McCall6b51f282009-11-23 01:53:49 +0000504 SourceLocation l, const TemplateArgumentListInfo *targs,
505 QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000506 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000507 base->isTypeDependent() || (qual && qual->isDependent()),
508 base->isValueDependent() || (qual && qual->isDependent())),
509 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCall6b51f282009-11-23 01:53:49 +0000510 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000511 // Initialize the qualifier, if any.
512 if (HasQualifier) {
513 NameQualifier *NQ = getMemberQualifier();
514 NQ->NNS = qual;
515 NQ->Range = qualrange;
516 }
Mike Stump11289f42009-09-09 15:08:12 +0000517
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000518 // Initialize the explicit template argument list, if any.
John McCall6b51f282009-11-23 01:53:49 +0000519 if (targs)
520 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000521}
522
Mike Stump11289f42009-09-09 15:08:12 +0000523MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
524 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000525 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000526 ValueDecl *memberdecl,
Mike Stump11289f42009-09-09 15:08:12 +0000527 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000528 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000529 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000530 std::size_t Size = sizeof(MemberExpr);
531 if (qual != 0)
532 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000533
John McCall6b51f282009-11-23 01:53:49 +0000534 if (targs)
535 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000536
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000537 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000538 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCall6b51f282009-11-23 01:53:49 +0000539 targs, ty);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000540}
541
Anders Carlsson496335e2009-09-03 00:59:21 +0000542const char *CastExpr::getCastKindName() const {
543 switch (getCastKind()) {
544 case CastExpr::CK_Unknown:
545 return "Unknown";
546 case CastExpr::CK_BitCast:
547 return "BitCast";
548 case CastExpr::CK_NoOp:
549 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000550 case CastExpr::CK_BaseToDerived:
551 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000552 case CastExpr::CK_DerivedToBase:
553 return "DerivedToBase";
554 case CastExpr::CK_Dynamic:
555 return "Dynamic";
556 case CastExpr::CK_ToUnion:
557 return "ToUnion";
558 case CastExpr::CK_ArrayToPointerDecay:
559 return "ArrayToPointerDecay";
560 case CastExpr::CK_FunctionToPointerDecay:
561 return "FunctionToPointerDecay";
562 case CastExpr::CK_NullToMemberPointer:
563 return "NullToMemberPointer";
564 case CastExpr::CK_BaseToDerivedMemberPointer:
565 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000566 case CastExpr::CK_DerivedToBaseMemberPointer:
567 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000568 case CastExpr::CK_UserDefinedConversion:
569 return "UserDefinedConversion";
570 case CastExpr::CK_ConstructorConversion:
571 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000572 case CastExpr::CK_IntegralToPointer:
573 return "IntegralToPointer";
574 case CastExpr::CK_PointerToIntegral:
575 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000576 case CastExpr::CK_ToVoid:
577 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000578 case CastExpr::CK_VectorSplat:
579 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000580 case CastExpr::CK_IntegralCast:
581 return "IntegralCast";
582 case CastExpr::CK_IntegralToFloating:
583 return "IntegralToFloating";
584 case CastExpr::CK_FloatingToIntegral:
585 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000586 case CastExpr::CK_FloatingCast:
587 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000588 case CastExpr::CK_MemberPointerToBoolean:
589 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000590 case CastExpr::CK_AnyPointerToObjCPointerCast:
591 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000592 case CastExpr::CK_AnyPointerToBlockPointerCast:
593 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000594 }
Mike Stump11289f42009-09-09 15:08:12 +0000595
Anders Carlsson496335e2009-09-03 00:59:21 +0000596 assert(0 && "Unhandled cast kind!");
597 return 0;
598}
599
Douglas Gregord196a582009-12-14 19:27:10 +0000600Expr *CastExpr::getSubExprAsWritten() {
601 Expr *SubExpr = 0;
602 CastExpr *E = this;
603 do {
604 SubExpr = E->getSubExpr();
605
606 // Skip any temporary bindings; they're implicit.
607 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
608 SubExpr = Binder->getSubExpr();
609
610 // Conversions by constructor and conversion functions have a
611 // subexpression describing the call; strip it off.
612 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
613 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
614 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
615 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
616
617 // If the subexpression we're left with is an implicit cast, look
618 // through that, too.
619 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
620
621 return SubExpr;
622}
623
Chris Lattner1b926492006-08-23 06:42:10 +0000624/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
625/// corresponds to, e.g. "<<=".
626const char *BinaryOperator::getOpcodeStr(Opcode Op) {
627 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000628 case PtrMemD: return ".*";
629 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000630 case Mul: return "*";
631 case Div: return "/";
632 case Rem: return "%";
633 case Add: return "+";
634 case Sub: return "-";
635 case Shl: return "<<";
636 case Shr: return ">>";
637 case LT: return "<";
638 case GT: return ">";
639 case LE: return "<=";
640 case GE: return ">=";
641 case EQ: return "==";
642 case NE: return "!=";
643 case And: return "&";
644 case Xor: return "^";
645 case Or: return "|";
646 case LAnd: return "&&";
647 case LOr: return "||";
648 case Assign: return "=";
649 case MulAssign: return "*=";
650 case DivAssign: return "/=";
651 case RemAssign: return "%=";
652 case AddAssign: return "+=";
653 case SubAssign: return "-=";
654 case ShlAssign: return "<<=";
655 case ShrAssign: return ">>=";
656 case AndAssign: return "&=";
657 case XorAssign: return "^=";
658 case OrAssign: return "|=";
659 case Comma: return ",";
660 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000661
662 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000663}
Steve Naroff47500512007-04-19 23:00:49 +0000664
Mike Stump11289f42009-09-09 15:08:12 +0000665BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000666BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
667 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000668 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000669 case OO_Plus: return Add;
670 case OO_Minus: return Sub;
671 case OO_Star: return Mul;
672 case OO_Slash: return Div;
673 case OO_Percent: return Rem;
674 case OO_Caret: return Xor;
675 case OO_Amp: return And;
676 case OO_Pipe: return Or;
677 case OO_Equal: return Assign;
678 case OO_Less: return LT;
679 case OO_Greater: return GT;
680 case OO_PlusEqual: return AddAssign;
681 case OO_MinusEqual: return SubAssign;
682 case OO_StarEqual: return MulAssign;
683 case OO_SlashEqual: return DivAssign;
684 case OO_PercentEqual: return RemAssign;
685 case OO_CaretEqual: return XorAssign;
686 case OO_AmpEqual: return AndAssign;
687 case OO_PipeEqual: return OrAssign;
688 case OO_LessLess: return Shl;
689 case OO_GreaterGreater: return Shr;
690 case OO_LessLessEqual: return ShlAssign;
691 case OO_GreaterGreaterEqual: return ShrAssign;
692 case OO_EqualEqual: return EQ;
693 case OO_ExclaimEqual: return NE;
694 case OO_LessEqual: return LE;
695 case OO_GreaterEqual: return GE;
696 case OO_AmpAmp: return LAnd;
697 case OO_PipePipe: return LOr;
698 case OO_Comma: return Comma;
699 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000700 }
701}
702
703OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
704 static const OverloadedOperatorKind OverOps[] = {
705 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
706 OO_Star, OO_Slash, OO_Percent,
707 OO_Plus, OO_Minus,
708 OO_LessLess, OO_GreaterGreater,
709 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
710 OO_EqualEqual, OO_ExclaimEqual,
711 OO_Amp,
712 OO_Caret,
713 OO_Pipe,
714 OO_AmpAmp,
715 OO_PipePipe,
716 OO_Equal, OO_StarEqual,
717 OO_SlashEqual, OO_PercentEqual,
718 OO_PlusEqual, OO_MinusEqual,
719 OO_LessLessEqual, OO_GreaterGreaterEqual,
720 OO_AmpEqual, OO_CaretEqual,
721 OO_PipeEqual,
722 OO_Comma
723 };
724 return OverOps[Opc];
725}
726
Ted Kremenek013041e2010-02-19 01:50:18 +0000727InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000728 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000729 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000730 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump11289f42009-09-09 15:08:12 +0000731 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenek013041e2010-02-19 01:50:18 +0000732 UnionFieldInit(0), HadArrayRangeDesignator(false)
733{
734 for (unsigned I = 0; I != numInits; ++I) {
735 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000736 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000737 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000738 ValueDependent = true;
739 }
Ted Kremenek013041e2010-02-19 01:50:18 +0000740
741 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000742}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000743
Ted Kremenek013041e2010-02-19 01:50:18 +0000744void InitListExpr::reserveInits(unsigned NumInits) {
745 if (NumInits > InitExprs.size())
746 InitExprs.reserve(NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000747}
748
Ted Kremenek013041e2010-02-19 01:50:18 +0000749void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
750 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
751 Idx < LastIdx; ++Idx)
752 InitExprs[Idx]->Destroy(Context);
753 InitExprs.resize(NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000754}
755
Ted Kremenek013041e2010-02-19 01:50:18 +0000756Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
757 if (Init >= InitExprs.size()) {
758 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
759 InitExprs.back() = expr;
760 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000763 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
764 InitExprs[Init] = expr;
765 return Result;
766}
767
Steve Naroff991e99d2008-09-04 15:31:07 +0000768/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000769///
770const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000771 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000772 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000773}
774
Mike Stump11289f42009-09-09 15:08:12 +0000775SourceLocation BlockExpr::getCaretLocation() const {
776 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000777}
Mike Stump11289f42009-09-09 15:08:12 +0000778const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000779 return TheBlock->getBody();
780}
Mike Stump11289f42009-09-09 15:08:12 +0000781Stmt *BlockExpr::getBody() {
782 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000783}
Steve Naroff415d3d52008-10-08 17:01:13 +0000784
785
Chris Lattner1ec5f562007-06-27 05:38:08 +0000786//===----------------------------------------------------------------------===//
787// Generic Expression Routines
788//===----------------------------------------------------------------------===//
789
Chris Lattner237f2752009-02-14 07:37:35 +0000790/// isUnusedResultAWarning - Return true if this immediate expression should
791/// be warned about if the result is unused. If so, fill in Loc and Ranges
792/// with location to warn on and the source range[s] to report with the
793/// warning.
794bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000795 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000796 // Don't warn if the expr is type dependent. The type could end up
797 // instantiating to void.
798 if (isTypeDependent())
799 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattner1ec5f562007-06-27 05:38:08 +0000801 switch (getStmtClass()) {
802 default:
John McCallc493a732010-03-12 07:11:26 +0000803 if (getType()->isVoidType())
804 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000805 Loc = getExprLoc();
806 R1 = getSourceRange();
807 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000808 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000809 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000810 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000811 case UnaryOperatorClass: {
812 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattner1ec5f562007-06-27 05:38:08 +0000814 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000815 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000816 case UnaryOperator::PostInc:
817 case UnaryOperator::PostDec:
818 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000819 case UnaryOperator::PreDec: // ++/--
820 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000821 case UnaryOperator::Deref:
822 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000823 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000824 return false;
825 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000826 case UnaryOperator::Real:
827 case UnaryOperator::Imag:
828 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000829 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
830 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000831 return false;
832 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000833 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000834 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000835 }
Chris Lattner237f2752009-02-14 07:37:35 +0000836 Loc = UO->getOperatorLoc();
837 R1 = UO->getSubExpr()->getSourceRange();
838 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000839 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000840 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000841 const BinaryOperator *BO = cast<BinaryOperator>(this);
842 // Consider comma to have side effects if the LHS or RHS does.
John McCall1e3715a2010-02-16 04:10:53 +0000843 if (BO->getOpcode() == BinaryOperator::Comma) {
844 // ((foo = <blah>), 0) is an idiom for hiding the result (and
845 // lvalue-ness) of an assignment written in a macro.
846 if (IntegerLiteral *IE =
847 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
848 if (IE->getValue() == 0)
849 return false;
850
John McCallc493a732010-03-12 07:11:26 +0000851 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
852 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000853 }
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattner237f2752009-02-14 07:37:35 +0000855 if (BO->isAssignmentOp())
856 return false;
857 Loc = BO->getOperatorLoc();
858 R1 = BO->getLHS()->getSourceRange();
859 R2 = BO->getRHS()->getSourceRange();
860 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000861 }
Chris Lattner86928112007-08-25 02:00:02 +0000862 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000863 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000864
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000865 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000866 // The condition must be evaluated, but if either the LHS or RHS is a
867 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000868 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000869 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000870 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000871 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000872 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000873 }
874
Chris Lattnera44d1162007-06-27 05:58:59 +0000875 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000876 // If the base pointer or element is to a volatile pointer/field, accessing
877 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000878 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000879 return false;
880 Loc = cast<MemberExpr>(this)->getMemberLoc();
881 R1 = SourceRange(Loc, Loc);
882 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
883 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000884
Chris Lattner1ec5f562007-06-27 05:38:08 +0000885 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000886 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000887 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000888 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000889 return false;
890 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
891 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
892 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
893 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000894
Chris Lattner1ec5f562007-06-27 05:38:08 +0000895 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000896 case CXXOperatorCallExprClass:
897 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000898 // If this is a direct call, get the callee.
899 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +0000900 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000901 // If the callee has attribute pure, const, or warn_unused_result, warn
902 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000903 //
904 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
905 // updated to match for QoI.
906 if (FD->getAttr<WarnUnusedResultAttr>() ||
907 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
908 Loc = CE->getCallee()->getLocStart();
909 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner1a6babf2009-10-13 04:53:48 +0000911 if (unsigned NumArgs = CE->getNumArgs())
912 R2 = SourceRange(CE->getArg(0)->getLocStart(),
913 CE->getArg(NumArgs-1)->getLocEnd());
914 return true;
915 }
Chris Lattner237f2752009-02-14 07:37:35 +0000916 }
917 return false;
918 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000919
920 case CXXTemporaryObjectExprClass:
921 case CXXConstructExprClass:
922 return false;
923
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000924 case ObjCMessageExprClass: {
925 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
926 const ObjCMethodDecl *MD = ME->getMethodDecl();
927 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
928 Loc = getExprLoc();
929 return true;
930 }
Chris Lattner237f2752009-02-14 07:37:35 +0000931 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000932 }
Mike Stump11289f42009-09-09 15:08:12 +0000933
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000934 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000935#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000936 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000937 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000938 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000939 Loc = Ref->getLocation();
940 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
941 if (Ref->getBase())
942 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000943#else
944 Loc = getExprLoc();
945 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000946#endif
947 return true;
948 }
Chris Lattner944d3062008-07-26 19:51:01 +0000949 case StmtExprClass: {
950 // Statement exprs don't logically have side effects themselves, but are
951 // sometimes used in macros in ways that give them a type that is unused.
952 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
953 // however, if the result of the stmt expr is dead, we don't want to emit a
954 // warning.
955 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
956 if (!CS->body_empty())
957 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000958 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000959
John McCallc493a732010-03-12 07:11:26 +0000960 if (getType()->isVoidType())
961 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000962 Loc = cast<StmtExpr>(this)->getLParenLoc();
963 R1 = getSourceRange();
964 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000965 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000966 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000967 // If this is an explicit cast to void, allow it. People do this when they
968 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000969 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000970 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000971 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
972 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
973 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000974 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +0000975 if (getType()->isVoidType())
976 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000977 const CastExpr *CE = cast<CastExpr>(this);
978
979 // If this is a cast to void or a constructor conversion, check the operand.
980 // Otherwise, the result of the cast is unused.
981 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
982 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000983 return (cast<CastExpr>(this)->getSubExpr()
984 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000985 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
986 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
987 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000988 }
Mike Stump11289f42009-09-09 15:08:12 +0000989
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000990 case ImplicitCastExprClass:
991 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000992 return (cast<ImplicitCastExpr>(this)
993 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000994
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000995 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000996 return (cast<CXXDefaultArgExpr>(this)
997 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000998
999 case CXXNewExprClass:
1000 // FIXME: In theory, there might be new expressions that don't have side
1001 // effects (e.g. a placement new with an uninitialized POD).
1002 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001003 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001004 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001005 return (cast<CXXBindTemporaryExpr>(this)
1006 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001007 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001008 return (cast<CXXExprWithTemporaries>(this)
1009 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001010 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001011}
1012
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001013/// DeclCanBeLvalue - Determine whether the given declaration can be
1014/// an lvalue. This is a helper routine for isLvalue.
1015static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001016 // C++ [temp.param]p6:
1017 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001018 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001019 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1020 return NTTParm->getType()->isReferenceType();
1021
Douglas Gregor91f84212008-12-11 16:49:14 +00001022 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001023 // C++ 3.10p2: An lvalue refers to an object or function.
1024 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001025 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001026}
1027
Steve Naroff475cca02007-05-14 17:19:29 +00001028/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1029/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001030/// - name, where name must be a variable
1031/// - e[i]
1032/// - (e), where e must be an lvalue
1033/// - e.name, where e must be an lvalue
1034/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001035/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001036/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001037/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001038/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001039///
Chris Lattner67315442008-07-26 21:30:36 +00001040Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001041 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1042
1043 isLvalueResult Res = isLvalueInternal(Ctx);
1044 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1045 return Res;
1046
Douglas Gregor9a657932008-10-21 23:43:52 +00001047 // first, check the type (C99 6.3.2.1). Expressions with function
1048 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001049 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001050 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001051
Steve Naroff1018ea32008-02-10 01:39:04 +00001052 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001053 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001054 return LV_IncompleteVoidType;
1055
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001056 return LV_Valid;
1057}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001058
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001059// Check whether the expression can be sanely treated like an l-value
1060Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001061 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001062 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001063 case StringLiteralClass: // C99 6.5.1p4
1064 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001065 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001066 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001067 // For vectors, make sure base is an lvalue (i.e. not a function call).
1068 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001069 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001070 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001071 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001072 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1073 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001074 return LV_Valid;
1075 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001076 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001077 case BlockDeclRefExprClass: {
1078 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001079 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001080 return LV_Valid;
1081 break;
1082 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001083 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001084 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001085 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1086 NamedDecl *Member = m->getMemberDecl();
1087 // C++ [expr.ref]p4:
1088 // If E2 is declared to have type "reference to T", then E1.E2
1089 // is an lvalue.
1090 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1091 if (Value->getType()->isReferenceType())
1092 return LV_Valid;
1093
1094 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001095 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001096 return LV_Valid;
1097
1098 // -- If E2 is a non-static data member [...]. If E1 is an
1099 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001100 if (isa<FieldDecl>(Member)) {
1101 if (m->isArrow())
1102 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001103 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001104 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001105
1106 // -- If it refers to a static member function [...], then
1107 // E1.E2 is an lvalue.
1108 // -- Otherwise, if E1.E2 refers to a non-static member
1109 // function [...], then E1.E2 is not an lvalue.
1110 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1111 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1112
1113 // -- If E2 is a member enumerator [...], the expression E1.E2
1114 // is not an lvalue.
1115 if (isa<EnumConstantDecl>(Member))
1116 return LV_InvalidExpression;
1117
1118 // Not an lvalue.
1119 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001120 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001121
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001122 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001123 if (m->isArrow())
1124 return LV_Valid;
1125 Expr *BaseExp = m->getBase();
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001126 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1127 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001128 return LV_SubObjCPropertySetting;
1129 return
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001130 BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001131 }
Chris Lattner595db862007-10-30 22:53:42 +00001132 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001133 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001134 return LV_Valid; // C99 6.5.3p4
1135
1136 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001137 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1138 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001139 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001140
1141 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1142 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1143 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1144 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001145 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001146 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001147 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1148 return LV_Valid;
1149
1150 // If this is a conversion to a class temporary, make a note of
1151 // that.
1152 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1153 return LV_ClassTemporary;
1154
1155 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001156 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001157 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001158 case BinaryOperatorClass:
1159 case CompoundAssignOperatorClass: {
1160 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001161
1162 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1163 BinOp->getOpcode() == BinaryOperator::Comma)
1164 return BinOp->getRHS()->isLvalue(Ctx);
1165
Sebastian Redl112a97662009-02-07 00:15:38 +00001166 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001167 // The result of a .* expression is an lvalue only if its first operand is
1168 // an lvalue and its second operand is a pointer to data member.
1169 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001170 !BinOp->getType()->isFunctionType())
1171 return BinOp->getLHS()->isLvalue(Ctx);
1172
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001173 // The result of an ->* expression is an lvalue only if its second operand
1174 // is a pointer to data member.
1175 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1176 !BinOp->getType()->isFunctionType()) {
1177 QualType Ty = BinOp->getRHS()->getType();
1178 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1179 return LV_Valid;
1180 }
1181
Douglas Gregor58e008d2008-11-13 20:12:29 +00001182 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001183 return LV_InvalidExpression;
1184
Douglas Gregor58e008d2008-11-13 20:12:29 +00001185 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001186 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001187 // The result of an assignment operation [...] is an lvalue.
1188 return LV_Valid;
1189
1190
1191 // C99 6.5.16:
1192 // An assignment expression [...] is not an lvalue.
1193 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001194 }
Mike Stump11289f42009-09-09 15:08:12 +00001195 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001196 case CXXOperatorCallExprClass:
1197 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001198 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001199 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001200 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001201 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1202 if (ReturnType->isLValueReferenceType())
1203 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001204
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001205 // If the function is returning a class temporary, make a note of
1206 // that.
1207 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1208 return LV_ClassTemporary;
1209
Douglas Gregor6b754842008-10-28 00:22:11 +00001210 break;
1211 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001212 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001213 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001214 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001215 case ChooseExprClass:
1216 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001217 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001218 case ExtVectorElementExprClass:
1219 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001220 return LV_DuplicateVectorComponents;
1221 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001222 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1223 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001224 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1225 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001226 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001227 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001228 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001229 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001230 case UnresolvedLookupExprClass:
1231 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001232 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001233 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001234 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001235 case CXXFunctionalCastExprClass:
1236 case CXXStaticCastExprClass:
1237 case CXXDynamicCastExprClass:
1238 case CXXReinterpretCastExprClass:
1239 case CXXConstCastExprClass:
1240 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001241 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001242 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1243 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001244 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1245 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001246 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001247
1248 // If this is a conversion to a class temporary, make a note of
1249 // that.
1250 if (Ctx.getLangOptions().CPlusPlus &&
1251 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1252 return LV_ClassTemporary;
1253
Douglas Gregor6b754842008-10-28 00:22:11 +00001254 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001255 case CXXTypeidExprClass:
1256 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1257 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001258 case CXXBindTemporaryExprClass:
1259 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1260 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001261 case CXXBindReferenceExprClass:
1262 // Something that's bound to a reference is always an lvalue.
1263 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001264 case ConditionalOperatorClass: {
1265 // Complicated handling is only for C++.
1266 if (!Ctx.getLangOptions().CPlusPlus)
1267 return LV_InvalidExpression;
1268
1269 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1270 // everywhere there's an object converted to an rvalue. Also, any other
1271 // casts should be wrapped by ImplicitCastExprs. There's just the special
1272 // case involving throws to work out.
1273 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001274 Expr *True = Cond->getTrueExpr();
1275 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001276 // C++0x 5.16p2
1277 // If either the second or the third operand has type (cv) void, [...]
1278 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001279 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001280 return LV_InvalidExpression;
1281
1282 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001283 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001284 return LV_InvalidExpression;
1285
1286 // That's it.
1287 return LV_Valid;
1288 }
1289
Douglas Gregor5103eff2009-12-19 07:07:47 +00001290 case Expr::CXXExprWithTemporariesClass:
1291 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1292
1293 case Expr::ObjCMessageExprClass:
1294 if (const ObjCMethodDecl *Method
1295 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1296 if (Method->getResultType()->isLValueReferenceType())
1297 return LV_Valid;
1298 break;
1299
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001300 case Expr::CXXConstructExprClass:
1301 case Expr::CXXTemporaryObjectExprClass:
1302 case Expr::CXXZeroInitValueExprClass:
1303 return LV_ClassTemporary;
1304
Steve Naroff9358c712007-05-27 23:58:33 +00001305 default:
1306 break;
Steve Naroff47500512007-04-19 23:00:49 +00001307 }
Steve Naroff9358c712007-05-27 23:58:33 +00001308 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001309}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001310
Steve Naroff475cca02007-05-14 17:19:29 +00001311/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1312/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001313/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001314/// recursively, any member or element of all contained aggregates or unions)
1315/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001316Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001317Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001318 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001319
Steve Naroff9358c712007-05-27 23:58:33 +00001320 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001321 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001322 // C++ 3.10p11: Functions cannot be modified, but pointers to
1323 // functions can be modifiable.
1324 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1325 return MLV_NotObjectType;
1326 break;
1327
Chris Lattner1ec5f562007-06-27 05:38:08 +00001328 case LV_NotObjectType: return MLV_NotObjectType;
1329 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001330 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001331 case LV_InvalidExpression:
1332 // If the top level is a C-style cast, and the subexpression is a valid
1333 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1334 // GCC extension. We don't support it, but we want to produce good
1335 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001336 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1337 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1338 if (Loc)
1339 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001340 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001341 }
1342 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001343 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001344 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001345 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001346 case LV_ClassTemporary:
1347 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001348 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001349
1350 // The following is illegal:
1351 // void takeclosure(void (^C)(void));
1352 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1353 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001354 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001355 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1356 return MLV_NotBlockQualified;
1357 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001358
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001359 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001360 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001361 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1362 if (Expr->getSetterMethod() == 0)
1363 return MLV_NoSetterProperty;
1364 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001365
Chris Lattner7adf0762008-08-04 07:31:14 +00001366 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001367
Chris Lattner7adf0762008-08-04 07:31:14 +00001368 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001369 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001370 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001371 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001372 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001373 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001374
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001375 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001376 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001377 return MLV_ConstQualified;
1378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Mike Stump11289f42009-09-09 15:08:12 +00001380 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001381}
1382
Fariborz Jahanian07735332009-02-22 18:40:18 +00001383/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001384/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001385bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001386 switch (getStmtClass()) {
1387 default:
1388 return false;
1389 case ObjCIvarRefExprClass:
1390 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001391 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001392 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001393 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001394 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001395 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001396 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001397 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001398 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001399 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001400 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001401 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1402 if (VD->hasGlobalStorage())
1403 return true;
1404 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001405 // dereferencing to a pointer is always a gc'able candidate,
1406 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001407 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001408 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001409 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001410 return false;
1411 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001412 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001413 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001414 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001415 }
1416 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001417 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001418 }
1419}
Ted Kremenekfff70962008-01-17 16:57:34 +00001420Expr* Expr::IgnoreParens() {
1421 Expr* E = this;
1422 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1423 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001424
Ted Kremenekfff70962008-01-17 16:57:34 +00001425 return E;
1426}
1427
Chris Lattnerf2660962008-02-13 01:02:39 +00001428/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1429/// or CastExprs or ImplicitCastExprs, returning their operand.
1430Expr *Expr::IgnoreParenCasts() {
1431 Expr *E = this;
1432 while (true) {
1433 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1434 E = P->getSubExpr();
1435 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1436 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001437 else
1438 return E;
1439 }
1440}
1441
Chris Lattneref26c772009-03-13 17:28:01 +00001442/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1443/// value (including ptr->int casts of the same size). Strip off any
1444/// ParenExpr or CastExprs, returning their operand.
1445Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1446 Expr *E = this;
1447 while (true) {
1448 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1449 E = P->getSubExpr();
1450 continue;
1451 }
Mike Stump11289f42009-09-09 15:08:12 +00001452
Chris Lattneref26c772009-03-13 17:28:01 +00001453 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1454 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1455 // ptr<->int casts of the same width. We also ignore all identify casts.
1456 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001457
Chris Lattneref26c772009-03-13 17:28:01 +00001458 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1459 E = SE;
1460 continue;
1461 }
Mike Stump11289f42009-09-09 15:08:12 +00001462
Chris Lattneref26c772009-03-13 17:28:01 +00001463 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1464 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1465 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1466 E = SE;
1467 continue;
1468 }
1469 }
Mike Stump11289f42009-09-09 15:08:12 +00001470
Chris Lattneref26c772009-03-13 17:28:01 +00001471 return E;
1472 }
1473}
1474
Douglas Gregord196a582009-12-14 19:27:10 +00001475bool Expr::isDefaultArgument() const {
1476 const Expr *E = this;
1477 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1478 E = ICE->getSubExprAsWritten();
1479
1480 return isa<CXXDefaultArgExpr>(E);
1481}
Chris Lattneref26c772009-03-13 17:28:01 +00001482
Douglas Gregor4619e432008-12-05 23:32:09 +00001483/// hasAnyTypeDependentArguments - Determines if any of the expressions
1484/// in Exprs is type-dependent.
1485bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1486 for (unsigned I = 0; I < NumExprs; ++I)
1487 if (Exprs[I]->isTypeDependent())
1488 return true;
1489
1490 return false;
1491}
1492
1493/// hasAnyValueDependentArguments - Determines if any of the expressions
1494/// in Exprs is value-dependent.
1495bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1496 for (unsigned I = 0; I < NumExprs; ++I)
1497 if (Exprs[I]->isValueDependent())
1498 return true;
1499
1500 return false;
1501}
1502
Eli Friedman7139af42009-01-25 02:32:41 +00001503bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001504 // This function is attempting whether an expression is an initializer
1505 // which can be evaluated at compile-time. isEvaluatable handles most
1506 // of the cases, but it can't deal with some initializer-specific
1507 // expressions, and it can't deal with aggregates; we deal with those here,
1508 // and fall back to isEvaluatable for the other cases.
1509
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001510 // FIXME: This function assumes the variable being assigned to
1511 // isn't a reference type!
1512
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001513 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001514 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001515 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001516 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001517 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001518 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001519 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001520 // This handles gcc's extension that allows global initializers like
1521 // "struct x {int x;} x = (struct x) {};".
1522 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001523 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001524 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001525 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001526 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001527 // FIXME: This doesn't deal with fields with reference types correctly.
1528 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1529 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001530 const InitListExpr *Exp = cast<InitListExpr>(this);
1531 unsigned numInits = Exp->getNumInits();
1532 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001533 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001534 return false;
1535 }
Eli Friedman384da272009-01-25 03:12:18 +00001536 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001537 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001538 case ImplicitValueInitExprClass:
1539 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001540 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001541 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001542 case UnaryOperatorClass: {
1543 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1544 if (Exp->getOpcode() == UnaryOperator::Extension)
1545 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1546 break;
1547 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001548 case BinaryOperatorClass: {
1549 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1550 // but this handles the common case.
1551 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1552 if (Exp->getOpcode() == BinaryOperator::Sub &&
1553 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1554 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1555 return true;
1556 break;
1557 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001558 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001559 case CStyleCastExprClass:
1560 // Handle casts with a destination that's a struct or union; this
1561 // deals with both the gcc no-op struct cast extension and the
1562 // cast-to-union extension.
1563 if (getType()->isRecordType())
1564 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001565
1566 // Integer->integer casts can be handled here, which is important for
1567 // things like (int)(&&x-&&y). Scary but true.
1568 if (getType()->isIntegerType() &&
1569 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1570 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1571
Eli Friedman384da272009-01-25 03:12:18 +00001572 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001573 }
Eli Friedman384da272009-01-25 03:12:18 +00001574 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001575}
1576
Chris Lattner1f4479e2007-06-05 04:15:44 +00001577/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001578/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001579
1580/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1581/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001582///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001583/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1584/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1585/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001586
Eli Friedman98c56a42009-02-26 09:29:13 +00001587// CheckICE - This function does the fundamental ICE checking: the returned
1588// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1589// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001590// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001591// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001592// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001593//
1594// Meanings of Val:
1595// 0: This expression is an ICE if it can be evaluated by Evaluate.
1596// 1: This expression is not an ICE, but if it isn't evaluated, it's
1597// a legal subexpression for an ICE. This return value is used to handle
1598// the comma operator in C99 mode.
1599// 2: This expression is not an ICE, and is not a legal subexpression for one.
1600
1601struct ICEDiag {
1602 unsigned Val;
1603 SourceLocation Loc;
1604
1605 public:
1606 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1607 ICEDiag() : Val(0) {}
1608};
1609
1610ICEDiag NoDiag() { return ICEDiag(); }
1611
Eli Friedman90afd3d2009-02-27 04:07:58 +00001612static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1613 Expr::EvalResult EVResult;
1614 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1615 !EVResult.Val.isInt()) {
1616 return ICEDiag(2, E->getLocStart());
1617 }
1618 return NoDiag();
1619}
1620
Eli Friedman98c56a42009-02-26 09:29:13 +00001621static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001622 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001623 if (!E->getType()->isIntegralType()) {
1624 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001625 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001626
1627 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001628#define STMT(Node, Base) case Expr::Node##Class:
1629#define EXPR(Node, Base)
1630#include "clang/AST/StmtNodes.def"
1631 case Expr::PredefinedExprClass:
1632 case Expr::FloatingLiteralClass:
1633 case Expr::ImaginaryLiteralClass:
1634 case Expr::StringLiteralClass:
1635 case Expr::ArraySubscriptExprClass:
1636 case Expr::MemberExprClass:
1637 case Expr::CompoundAssignOperatorClass:
1638 case Expr::CompoundLiteralExprClass:
1639 case Expr::ExtVectorElementExprClass:
1640 case Expr::InitListExprClass:
1641 case Expr::DesignatedInitExprClass:
1642 case Expr::ImplicitValueInitExprClass:
1643 case Expr::ParenListExprClass:
1644 case Expr::VAArgExprClass:
1645 case Expr::AddrLabelExprClass:
1646 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001647 case Expr::CXXMemberCallExprClass:
1648 case Expr::CXXDynamicCastExprClass:
1649 case Expr::CXXTypeidExprClass:
1650 case Expr::CXXNullPtrLiteralExprClass:
1651 case Expr::CXXThisExprClass:
1652 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001653 case Expr::CXXNewExprClass:
1654 case Expr::CXXDeleteExprClass:
1655 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001656 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001657 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001658 case Expr::CXXConstructExprClass:
1659 case Expr::CXXBindTemporaryExprClass:
Anders Carlssonba6c4372010-01-29 02:39:32 +00001660 case Expr::CXXBindReferenceExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001661 case Expr::CXXExprWithTemporariesClass:
1662 case Expr::CXXTemporaryObjectExprClass:
1663 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001664 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001665 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001666 case Expr::ObjCStringLiteralClass:
1667 case Expr::ObjCEncodeExprClass:
1668 case Expr::ObjCMessageExprClass:
1669 case Expr::ObjCSelectorExprClass:
1670 case Expr::ObjCProtocolExprClass:
1671 case Expr::ObjCIvarRefExprClass:
1672 case Expr::ObjCPropertyRefExprClass:
1673 case Expr::ObjCImplicitSetterGetterRefExprClass:
1674 case Expr::ObjCSuperExprClass:
1675 case Expr::ObjCIsaExprClass:
1676 case Expr::ShuffleVectorExprClass:
1677 case Expr::BlockExprClass:
1678 case Expr::BlockDeclRefExprClass:
1679 case Expr::NoStmtClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001680 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001681
Douglas Gregor73341c42009-09-11 00:18:58 +00001682 case Expr::GNUNullExprClass:
1683 // GCC considers the GNU __null value to be an integral constant expression.
1684 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001685
Eli Friedman98c56a42009-02-26 09:29:13 +00001686 case Expr::ParenExprClass:
1687 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1688 case Expr::IntegerLiteralClass:
1689 case Expr::CharacterLiteralClass:
1690 case Expr::CXXBoolLiteralExprClass:
1691 case Expr::CXXZeroInitValueExprClass:
1692 case Expr::TypesCompatibleExprClass:
1693 case Expr::UnaryTypeTraitExprClass:
1694 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001695 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001696 case Expr::CXXOperatorCallExprClass: {
1697 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001698 if (CE->isBuiltinCall(Ctx))
1699 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001700 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001701 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001702 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001703 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1704 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001705 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001706 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCall6dee4732010-02-24 09:03:18 +00001707 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1708
1709 // Parameter variables are never constants. Without this check,
1710 // getAnyInitializer() can find a default argument, which leads
1711 // to chaos.
1712 if (isa<ParmVarDecl>(D))
1713 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1714
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001715 // C++ 7.1.5.1p2
1716 // A variable of non-volatile const-qualified integral or enumeration
1717 // type initialized by an ICE can be used in ICEs.
John McCall6dee4732010-02-24 09:03:18 +00001718 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001719 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1720 if (Quals.hasVolatile() || !Quals.hasConst())
1721 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1722
Sebastian Redl5ca79842010-02-01 20:16:42 +00001723 // Look for a declaration of this variable that has an initializer.
1724 const VarDecl *ID = 0;
1725 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001726 if (Init) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001727 if (ID->isInitKnownICE()) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001728 // We have already checked whether this subexpression is an
1729 // integral constant expression.
Sebastian Redl5ca79842010-02-01 20:16:42 +00001730 if (ID->isInitICE())
Douglas Gregor0840cc02009-11-01 20:32:48 +00001731 return NoDiag();
1732 else
1733 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1734 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001735
John McCall52cc0892010-02-06 01:07:37 +00001736 // It's an ICE whether or not the definition we found is
1737 // out-of-line. See DR 721 and the discussion in Clang PR
1738 // 6206 for details.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001739
1740 if (Dcl->isCheckingICE()) {
1741 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1742 }
1743
1744 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001745 ICEDiag Result = CheckICE(Init, Ctx);
1746 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001747 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001748 return Result;
1749 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001750 }
1751 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001752 return ICEDiag(2, E->getLocStart());
1753 case Expr::UnaryOperatorClass: {
1754 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001755 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001756 case UnaryOperator::PostInc:
1757 case UnaryOperator::PostDec:
1758 case UnaryOperator::PreInc:
1759 case UnaryOperator::PreDec:
1760 case UnaryOperator::AddrOf:
1761 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001762 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001763
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001764 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001765 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001766 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001767 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001768 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001769 case UnaryOperator::Real:
1770 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001771 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001772 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001773 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1774 // Evaluate matches the proposed gcc behavior for cases like
1775 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1776 // compliance: we should warn earlier for offsetof expressions with
1777 // array subscripts that aren't ICEs, and if the array subscripts
1778 // are ICEs, the value of the offsetof must be an integer constant.
1779 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001780 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001781 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001782 case Expr::SizeOfAlignOfExprClass: {
1783 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1784 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1785 return ICEDiag(2, E->getLocStart());
1786 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001787 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001788 case Expr::BinaryOperatorClass: {
1789 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001790 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001791 case BinaryOperator::PtrMemD:
1792 case BinaryOperator::PtrMemI:
1793 case BinaryOperator::Assign:
1794 case BinaryOperator::MulAssign:
1795 case BinaryOperator::DivAssign:
1796 case BinaryOperator::RemAssign:
1797 case BinaryOperator::AddAssign:
1798 case BinaryOperator::SubAssign:
1799 case BinaryOperator::ShlAssign:
1800 case BinaryOperator::ShrAssign:
1801 case BinaryOperator::AndAssign:
1802 case BinaryOperator::XorAssign:
1803 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001804 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001805
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001806 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001807 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001808 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001809 case BinaryOperator::Add:
1810 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001811 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001812 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001813 case BinaryOperator::LT:
1814 case BinaryOperator::GT:
1815 case BinaryOperator::LE:
1816 case BinaryOperator::GE:
1817 case BinaryOperator::EQ:
1818 case BinaryOperator::NE:
1819 case BinaryOperator::And:
1820 case BinaryOperator::Xor:
1821 case BinaryOperator::Or:
1822 case BinaryOperator::Comma: {
1823 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1824 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001825 if (Exp->getOpcode() == BinaryOperator::Div ||
1826 Exp->getOpcode() == BinaryOperator::Rem) {
1827 // Evaluate gives an error for undefined Div/Rem, so make sure
1828 // we don't evaluate one.
1829 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1830 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1831 if (REval == 0)
1832 return ICEDiag(1, E->getLocStart());
1833 if (REval.isSigned() && REval.isAllOnesValue()) {
1834 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1835 if (LEval.isMinSignedValue())
1836 return ICEDiag(1, E->getLocStart());
1837 }
1838 }
1839 }
1840 if (Exp->getOpcode() == BinaryOperator::Comma) {
1841 if (Ctx.getLangOptions().C99) {
1842 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1843 // if it isn't evaluated.
1844 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1845 return ICEDiag(1, E->getLocStart());
1846 } else {
1847 // In both C89 and C++, commas in ICEs are illegal.
1848 return ICEDiag(2, E->getLocStart());
1849 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001850 }
1851 if (LHSResult.Val >= RHSResult.Val)
1852 return LHSResult;
1853 return RHSResult;
1854 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001855 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001856 case BinaryOperator::LOr: {
1857 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1858 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1859 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1860 // Rare case where the RHS has a comma "side-effect"; we need
1861 // to actually check the condition to see whether the side
1862 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001863 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001864 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001865 return RHSResult;
1866 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001867 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001868
Eli Friedman98c56a42009-02-26 09:29:13 +00001869 if (LHSResult.Val >= RHSResult.Val)
1870 return LHSResult;
1871 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001872 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001873 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001874 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001875 case Expr::ImplicitCastExprClass:
1876 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001877 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001878 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001879 case Expr::CXXStaticCastExprClass:
1880 case Expr::CXXReinterpretCastExprClass:
1881 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001882 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1883 if (SubExpr->getType()->isIntegralType())
1884 return CheckICE(SubExpr, Ctx);
1885 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1886 return NoDiag();
1887 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001888 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001889 case Expr::ConditionalOperatorClass: {
1890 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001891 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001892 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001893 // expression, and it is fully evaluated. This is an important GNU
1894 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001895 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001896 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001897 Expr::EvalResult EVResult;
1898 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1899 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001900 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001901 }
1902 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001903 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001904 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1905 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1906 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1907 if (CondResult.Val == 2)
1908 return CondResult;
1909 if (TrueResult.Val == 2)
1910 return TrueResult;
1911 if (FalseResult.Val == 2)
1912 return FalseResult;
1913 if (CondResult.Val == 1)
1914 return CondResult;
1915 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1916 return NoDiag();
1917 // Rare case where the diagnostics depend on which side is evaluated
1918 // Note that if we get here, CondResult is 0, and at least one of
1919 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001920 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001921 return FalseResult;
1922 }
1923 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001924 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001925 case Expr::CXXDefaultArgExprClass:
1926 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001927 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001928 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001929 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001930 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001931
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001932 // Silence a GCC warning
1933 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001934}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001935
Eli Friedman98c56a42009-02-26 09:29:13 +00001936bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1937 SourceLocation *Loc, bool isEvaluated) const {
1938 ICEDiag d = CheckICE(this, Ctx);
1939 if (d.Val != 0) {
1940 if (Loc) *Loc = d.Loc;
1941 return false;
1942 }
1943 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001944 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001945 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001946 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1947 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001948 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001949 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001950}
1951
Chris Lattner7eef9192007-05-24 01:23:49 +00001952/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1953/// integer constant expression with the value zero, or if this is one that is
1954/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001955bool Expr::isNullPointerConstant(ASTContext &Ctx,
1956 NullPointerConstantValueDependence NPC) const {
1957 if (isValueDependent()) {
1958 switch (NPC) {
1959 case NPC_NeverValueDependent:
1960 assert(false && "Unexpected value dependent expression!");
1961 // If the unthinkable happens, fall through to the safest alternative.
1962
1963 case NPC_ValueDependentIsNull:
1964 return isTypeDependent() || getType()->isIntegralType();
1965
1966 case NPC_ValueDependentIsNotNull:
1967 return false;
1968 }
1969 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001970
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001971 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001972 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001973 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001974 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001975 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001976 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001977 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001978 Pointee->isVoidType() && // to void*
1979 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001980 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001981 }
Steve Naroffada7d422007-05-20 17:54:12 +00001982 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001983 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1984 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001985 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001986 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1987 // Accept ((void*)0) as a null pointer constant, as many other
1988 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001989 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001990 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001991 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001992 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001993 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001994 } else if (isa<GNUNullExpr>(this)) {
1995 // The GNU __null extension is always a null pointer constant.
1996 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001997 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001998
Sebastian Redl576fd422009-05-10 18:38:11 +00001999 // C++0x nullptr_t is always a null pointer constant.
2000 if (getType()->isNullPtrType())
2001 return true;
2002
Steve Naroff4871fe02008-01-14 16:10:57 +00002003 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002004 if (!getType()->isIntegerType() ||
2005 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00002006 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002007
Chris Lattner1abbd412007-06-08 17:58:43 +00002008 // If we have an integer constant expression, we need to *evaluate* it and
2009 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002010 llvm::APSInt Result;
2011 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002012}
Steve Narofff7a5da12007-07-28 23:10:27 +00002013
Douglas Gregor71235ec2009-05-02 02:18:30 +00002014FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002015 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002016
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002017 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2018 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2019 E = ICE->getSubExpr()->IgnoreParens();
2020 else
2021 break;
2022 }
2023
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002024 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002025 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002026 if (Field->isBitField())
2027 return Field;
2028
2029 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2030 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2031 return BinOp->getLHS()->getBitField();
2032
2033 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002034}
2035
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002036bool Expr::refersToVectorElement() const {
2037 const Expr *E = this->IgnoreParens();
2038
2039 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2040 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2041 E = ICE->getSubExpr()->IgnoreParens();
2042 else
2043 break;
2044 }
2045
2046 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2047 return ASE->getBase()->getType()->isVectorType();
2048
2049 if (isa<ExtVectorElementExpr>(E))
2050 return true;
2051
2052 return false;
2053}
2054
Chris Lattnerb8211f62009-02-16 22:14:05 +00002055/// isArrow - Return true if the base expression is a pointer to vector,
2056/// return false if the base expression is a vector.
2057bool ExtVectorElementExpr::isArrow() const {
2058 return getBase()->getType()->isPointerType();
2059}
2060
Nate Begemance4d7fc2008-04-18 23:10:10 +00002061unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002062 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002063 return VT->getNumElements();
2064 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002065}
2066
Nate Begemanf322eab2008-05-09 06:41:27 +00002067/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002068bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002069 // FIXME: Refactor this code to an accessor on the AST node which returns the
2070 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002071 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002072
2073 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002074 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002075 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002076
Nate Begeman7e5185b2009-01-18 02:01:21 +00002077 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002078 if (Comp[0] == 's' || Comp[0] == 'S')
2079 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002080
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002081 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2082 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002083 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002084
Steve Naroff0d595ca2007-07-30 03:29:09 +00002085 return false;
2086}
Chris Lattner885b4952007-08-02 23:36:59 +00002087
Nate Begemanf322eab2008-05-09 06:41:27 +00002088/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002089void ExtVectorElementExpr::getEncodedElementAccess(
2090 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002091 llvm::StringRef Comp = Accessor->getName();
2092 if (Comp[0] == 's' || Comp[0] == 'S')
2093 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002094
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002095 bool isHi = Comp == "hi";
2096 bool isLo = Comp == "lo";
2097 bool isEven = Comp == "even";
2098 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002099
Nate Begemanf322eab2008-05-09 06:41:27 +00002100 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2101 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002102
Nate Begemanf322eab2008-05-09 06:41:27 +00002103 if (isHi)
2104 Index = e + i;
2105 else if (isLo)
2106 Index = i;
2107 else if (isEven)
2108 Index = 2 * i;
2109 else if (isOdd)
2110 Index = 2 * i + 1;
2111 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002112 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002113
Nate Begemand3862152008-05-13 21:03:02 +00002114 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002115 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002116}
2117
Steve Narofff73590d2007-09-27 14:38:14 +00002118// constructor for instance messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002119ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2120 Selector selInfo,
2121 QualType retType, ObjCMethodDecl *mproto,
2122 SourceLocation LBrac, SourceLocation RBrac,
2123 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002124 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002125 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002126 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002127 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002128 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002129 if (NumArgs) {
2130 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002131 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2132 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002133 LBracloc = LBrac;
2134 RBracloc = RBrac;
2135}
2136
Mike Stump11289f42009-09-09 15:08:12 +00002137// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002138// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenek2c809302010-02-11 22:41:21 +00002139ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002140 SourceLocation clsNameLoc, Selector selInfo,
2141 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenek2c809302010-02-11 22:41:21 +00002142 SourceLocation LBrac, SourceLocation RBrac,
2143 Expr **ArgExprs, unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002144 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2145 SelName(selInfo), MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002146 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002147 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002148 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002149 if (NumArgs) {
2150 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002151 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2152 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002153 LBracloc = LBrac;
2154 RBracloc = RBrac;
2155}
2156
Mike Stump11289f42009-09-09 15:08:12 +00002157// constructor for class messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002158ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002159 SourceLocation clsNameLoc, Selector selInfo,
2160 QualType retType,
Ted Kremenek2c809302010-02-11 22:41:21 +00002161 ObjCMethodDecl *mproto, SourceLocation LBrac,
2162 SourceLocation RBrac, Expr **ArgExprs,
2163 unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002164 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2165 SelName(selInfo), MethodProto(mproto)
2166{
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002167 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002168 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002169 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2170 if (NumArgs) {
2171 for (unsigned i = 0; i != NumArgs; ++i)
2172 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2173 }
2174 LBracloc = LBrac;
2175 RBracloc = RBrac;
2176}
2177
2178ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2179 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2180 switch (x & Flags) {
2181 default:
2182 assert(false && "Invalid ObjCMessageExpr.");
2183 case IsInstMeth:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002184 return ClassInfo(0, 0, SourceLocation());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002185 case IsClsMethDeclUnknown:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002186 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002187 case IsClsMethDeclKnown: {
2188 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002189 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002190 }
2191 }
2192}
2193
Chris Lattner7ec71da2009-04-26 00:44:05 +00002194void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorde4827d2010-03-08 16:40:19 +00002195 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner7ec71da2009-04-26 00:44:05 +00002196 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002197 return;
2198 }
2199
2200 if (CI.Decl == 0)
2201 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner7ec71da2009-04-26 00:44:05 +00002202 else
Douglas Gregorde4827d2010-03-08 16:40:19 +00002203 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2204 ClassNameLoc = CI.Loc;
Chris Lattner7ec71da2009-04-26 00:44:05 +00002205}
2206
Ted Kremenek2c809302010-02-11 22:41:21 +00002207void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2208 DestroyChildren(C);
2209 if (SubExprs)
2210 C.Deallocate(SubExprs);
2211 this->~ObjCMessageExpr();
2212 C.Deallocate((void*) this);
2213}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002214
Chris Lattner35e564e2007-10-25 00:29:32 +00002215bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002216 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002217}
2218
Nate Begeman48745922009-08-12 02:28:50 +00002219void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2220 unsigned NumExprs) {
2221 if (SubExprs) C.Deallocate(SubExprs);
2222
2223 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002224 this->NumExprs = NumExprs;
2225 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002226}
Nate Begeman48745922009-08-12 02:28:50 +00002227
2228void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2229 DestroyChildren(C);
2230 if (SubExprs) C.Deallocate(SubExprs);
2231 this->~ShuffleVectorExpr();
2232 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002233}
2234
Douglas Gregore26a2852009-08-07 06:08:38 +00002235void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002236 // Override default behavior of traversing children. If this has a type
2237 // operand and the type is a variable-length array, the child iteration
2238 // will iterate over the size expression. However, this expression belongs
2239 // to the type, not to this, so we don't want to delete it.
2240 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002241 if (isArgumentType()) {
2242 this->~SizeOfAlignOfExpr();
2243 C.Deallocate(this);
2244 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002245 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002246 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002247}
2248
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002249//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002250// DesignatedInitExpr
2251//===----------------------------------------------------------------------===//
2252
2253IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2254 assert(Kind == FieldDesignator && "Only valid on a field designator");
2255 if (Field.NameOrField & 0x01)
2256 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2257 else
2258 return getField()->getIdentifier();
2259}
2260
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002261DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2262 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002263 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002264 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002265 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002266 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002267 unsigned NumIndexExprs,
2268 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002269 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002270 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002271 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2272 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002273 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002274
2275 // Record the initializer itself.
2276 child_iterator Child = child_begin();
2277 *Child++ = Init;
2278
2279 // Copy the designators and their subexpressions, computing
2280 // value-dependence along the way.
2281 unsigned IndexIdx = 0;
2282 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002283 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002284
2285 if (this->Designators[I].isArrayDesignator()) {
2286 // Compute type- and value-dependence.
2287 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002288 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002289 Index->isTypeDependent() || Index->isValueDependent();
2290
2291 // Copy the index expressions into permanent storage.
2292 *Child++ = IndexExprs[IndexIdx++];
2293 } else if (this->Designators[I].isArrayRangeDesignator()) {
2294 // Compute type- and value-dependence.
2295 Expr *Start = IndexExprs[IndexIdx];
2296 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002297 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002298 Start->isTypeDependent() || Start->isValueDependent() ||
2299 End->isTypeDependent() || End->isValueDependent();
2300
2301 // Copy the start/end expressions into permanent storage.
2302 *Child++ = IndexExprs[IndexIdx++];
2303 *Child++ = IndexExprs[IndexIdx++];
2304 }
2305 }
2306
2307 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002308}
2309
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002310DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002311DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002312 unsigned NumDesignators,
2313 Expr **IndexExprs, unsigned NumIndexExprs,
2314 SourceLocation ColonOrEqualLoc,
2315 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002316 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002317 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002318 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002319 ColonOrEqualLoc, UsesColonSyntax,
2320 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002321}
2322
Mike Stump11289f42009-09-09 15:08:12 +00002323DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002324 unsigned NumIndexExprs) {
2325 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2326 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2327 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2328}
2329
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002330void DesignatedInitExpr::setDesignators(ASTContext &C,
2331 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002332 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002333 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002334
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002335 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002336 NumDesignators = NumDesigs;
2337 for (unsigned I = 0; I != NumDesigs; ++I)
2338 Designators[I] = Desigs[I];
2339}
2340
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002341SourceRange DesignatedInitExpr::getSourceRange() const {
2342 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002343 Designator &First =
2344 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002345 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002346 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002347 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2348 else
2349 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2350 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002351 StartLoc =
2352 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002353 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2354}
2355
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002356Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2357 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2358 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2359 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002360 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2361 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2362}
2363
2364Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002365 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002366 "Requires array range designator");
2367 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2368 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002369 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2370 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2371}
2372
2373Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002374 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002375 "Requires array range designator");
2376 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2377 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002378 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2379 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2380}
2381
Douglas Gregord5846a12009-04-15 06:41:24 +00002382/// \brief Replaces the designator at index @p Idx with the series
2383/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002384void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002385 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002386 const Designator *Last) {
2387 unsigned NumNewDesignators = Last - First;
2388 if (NumNewDesignators == 0) {
2389 std::copy_backward(Designators + Idx + 1,
2390 Designators + NumDesignators,
2391 Designators + Idx);
2392 --NumNewDesignators;
2393 return;
2394 } else if (NumNewDesignators == 1) {
2395 Designators[Idx] = *First;
2396 return;
2397 }
2398
Mike Stump11289f42009-09-09 15:08:12 +00002399 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002400 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002401 std::copy(Designators, Designators + Idx, NewDesignators);
2402 std::copy(First, Last, NewDesignators + Idx);
2403 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2404 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002405 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002406 Designators = NewDesignators;
2407 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2408}
2409
Douglas Gregore26a2852009-08-07 06:08:38 +00002410void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002411 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002412 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002413}
2414
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002415void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2416 for (unsigned I = 0; I != NumDesignators; ++I)
2417 Designators[I].~Designator();
2418 C.Deallocate(Designators);
2419 Designators = 0;
2420}
2421
Mike Stump11289f42009-09-09 15:08:12 +00002422ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002423 Expr **exprs, unsigned nexprs,
2424 SourceLocation rparenloc)
2425: Expr(ParenListExprClass, QualType(),
2426 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002427 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002428 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002429
Nate Begeman5ec4b312009-08-10 23:49:36 +00002430 Exprs = new (C) Stmt*[nexprs];
2431 for (unsigned i = 0; i != nexprs; ++i)
2432 Exprs[i] = exprs[i];
2433}
2434
2435void ParenListExpr::DoDestroy(ASTContext& C) {
2436 DestroyChildren(C);
2437 if (Exprs) C.Deallocate(Exprs);
2438 this->~ParenListExpr();
2439 C.Deallocate(this);
2440}
2441
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002442//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002443// ExprIterator.
2444//===----------------------------------------------------------------------===//
2445
2446Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2447Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2448Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2449const Expr* ConstExprIterator::operator[](size_t idx) const {
2450 return cast<Expr>(I[idx]);
2451}
2452const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2453const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2454
2455//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002456// Child Iterators for iterating over subexpressions/substatements
2457//===----------------------------------------------------------------------===//
2458
2459// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002460Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2461Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002462
Steve Naroffe46504b2007-11-12 14:29:37 +00002463// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002464Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2465Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002466
Steve Naroffebf4cb42008-06-02 23:03:37 +00002467// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002468Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2469Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002470
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002471// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002472Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2473 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002474}
Mike Stump11289f42009-09-09 15:08:12 +00002475Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2476 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002477}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002478
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002479// ObjCSuperExpr
2480Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2481Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2482
Steve Naroffe87026a2009-07-24 17:54:45 +00002483// ObjCIsaExpr
2484Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2485Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2486
Chris Lattner6307f192008-08-10 01:53:14 +00002487// PredefinedExpr
2488Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2489Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002490
2491// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002492Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2493Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002494
2495// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002496Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002497Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002498
2499// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002500Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2501Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002502
Chris Lattner1c20a172007-08-26 03:42:43 +00002503// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002504Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2505Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002506
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002507// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002508Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2509Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002510
2511// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002512Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2513Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002514
2515// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002516Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2517Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002518
Sebastian Redl6f282892008-11-11 17:56:53 +00002519// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002520Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002521 // If this is of a type and the type is a VLA type (and not a typedef), the
2522 // size expression of the VLA needs to be treated as an executable expression.
2523 // Why isn't this weirdness documented better in StmtIterator?
2524 if (isArgumentType()) {
2525 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2526 getArgumentType().getTypePtr()))
2527 return child_iterator(T);
2528 return child_iterator();
2529 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002530 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002531}
Sebastian Redl6f282892008-11-11 17:56:53 +00002532Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2533 if (isArgumentType())
2534 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002535 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002536}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002537
2538// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002539Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002540 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002541}
Ted Kremenek23702b62007-08-24 20:06:47 +00002542Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002543 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002544}
2545
2546// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002547Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002548 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002549}
Ted Kremenek23702b62007-08-24 20:06:47 +00002550Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002551 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002552}
Ted Kremenek23702b62007-08-24 20:06:47 +00002553
2554// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002555Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2556Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002557
Nate Begemance4d7fc2008-04-18 23:10:10 +00002558// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002559Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2560Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002561
2562// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002563Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2564Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002565
Ted Kremenek23702b62007-08-24 20:06:47 +00002566// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002567Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2568Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002569
2570// BinaryOperator
2571Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002572 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002573}
Ted Kremenek23702b62007-08-24 20:06:47 +00002574Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002575 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002576}
2577
2578// ConditionalOperator
2579Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002580 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002581}
Ted Kremenek23702b62007-08-24 20:06:47 +00002582Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002583 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002584}
2585
2586// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002587Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2588Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002589
Ted Kremenek23702b62007-08-24 20:06:47 +00002590// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002591Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2592Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002593
2594// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002595Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2596 return child_iterator();
2597}
2598
2599Stmt::child_iterator TypesCompatibleExpr::child_end() {
2600 return child_iterator();
2601}
Ted Kremenek23702b62007-08-24 20:06:47 +00002602
2603// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002604Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2605Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002606
Douglas Gregor3be4b122008-11-29 04:51:27 +00002607// GNUNullExpr
2608Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2609Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2610
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002611// ShuffleVectorExpr
2612Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002613 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002614}
2615Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002616 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002617}
2618
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002619// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002620Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2621Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002622
Anders Carlsson4692db02007-08-31 04:56:16 +00002623// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002624Stmt::child_iterator InitListExpr::child_begin() {
2625 return InitExprs.size() ? &InitExprs[0] : 0;
2626}
2627Stmt::child_iterator InitListExpr::child_end() {
2628 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2629}
Anders Carlsson4692db02007-08-31 04:56:16 +00002630
Douglas Gregor0202cb42009-01-29 17:44:32 +00002631// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002632Stmt::child_iterator DesignatedInitExpr::child_begin() {
2633 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2634 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002635 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2636}
2637Stmt::child_iterator DesignatedInitExpr::child_end() {
2638 return child_iterator(&*child_begin() + NumSubExprs);
2639}
2640
Douglas Gregor0202cb42009-01-29 17:44:32 +00002641// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002642Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2643 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002644}
2645
Mike Stump11289f42009-09-09 15:08:12 +00002646Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2647 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002648}
2649
Nate Begeman5ec4b312009-08-10 23:49:36 +00002650// ParenListExpr
2651Stmt::child_iterator ParenListExpr::child_begin() {
2652 return &Exprs[0];
2653}
2654Stmt::child_iterator ParenListExpr::child_end() {
2655 return &Exprs[0]+NumExprs;
2656}
2657
Ted Kremenek23702b62007-08-24 20:06:47 +00002658// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002659Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002660 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002661}
2662Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002663 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002664}
Ted Kremenek23702b62007-08-24 20:06:47 +00002665
2666// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002667Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2668Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002669
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002670// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002671Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002672 return child_iterator();
2673}
2674Stmt::child_iterator ObjCSelectorExpr::child_end() {
2675 return child_iterator();
2676}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002677
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002678// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002679Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2680 return child_iterator();
2681}
2682Stmt::child_iterator ObjCProtocolExpr::child_end() {
2683 return child_iterator();
2684}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002685
Steve Naroffd54978b2007-09-18 23:55:05 +00002686// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002687Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002688 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002689}
2690Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002691 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002692}
2693
Steve Naroffc540d662008-09-03 18:15:37 +00002694// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002695Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2696Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002697
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002698Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2699Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }