blob: 1b3202dd42825f09228b8ae415a43c2e1e334ba8 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattner0eedafe2006-08-24 04:56:27 +000030//===----------------------------------------------------------------------===//
31// Primary Expressions.
32//===----------------------------------------------------------------------===//
33
John McCall6b51f282009-11-23 01:53:49 +000034void ExplicitTemplateArgumentList::initializeFrom(
35 const TemplateArgumentListInfo &Info) {
36 LAngleLoc = Info.getLAngleLoc();
37 RAngleLoc = Info.getRAngleLoc();
38 NumTemplateArgs = Info.size();
39
40 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
41 for (unsigned i = 0; i != NumTemplateArgs; ++i)
42 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
43}
44
45void ExplicitTemplateArgumentList::copyInto(
46 TemplateArgumentListInfo &Info) const {
47 Info.setLAngleLoc(LAngleLoc);
48 Info.setRAngleLoc(RAngleLoc);
49 for (unsigned I = 0; I != NumTemplateArgs; ++I)
50 Info.addArgument(getTemplateArgs()[I]);
51}
52
53std::size_t ExplicitTemplateArgumentList::sizeFor(
54 const TemplateArgumentListInfo &Info) {
55 return sizeof(ExplicitTemplateArgumentList) +
56 sizeof(TemplateArgumentLoc) * Info.size();
57}
58
Douglas Gregored6c7442009-11-23 11:41:28 +000059void DeclRefExpr::computeDependence() {
60 TypeDependent = false;
61 ValueDependent = false;
62
63 NamedDecl *D = getDecl();
64
65 // (TD) C++ [temp.dep.expr]p3:
66 // An id-expression is type-dependent if it contains:
67 //
68 // and
69 //
70 // (VD) C++ [temp.dep.constexpr]p2:
71 // An identifier is value-dependent if it is:
72
73 // (TD) - an identifier that was declared with dependent type
74 // (VD) - a name declared with a dependent type,
75 if (getType()->isDependentType()) {
76 TypeDependent = true;
77 ValueDependent = true;
78 }
79 // (TD) - a conversion-function-id that specifies a dependent type
80 else if (D->getDeclName().getNameKind()
81 == DeclarationName::CXXConversionFunctionName &&
82 D->getDeclName().getCXXNameType()->isDependentType()) {
83 TypeDependent = true;
84 ValueDependent = true;
85 }
86 // (TD) - a template-id that is dependent,
87 else if (hasExplicitTemplateArgumentList() &&
88 TemplateSpecializationType::anyDependentTemplateArguments(
89 getTemplateArgs(),
90 getNumTemplateArgs())) {
91 TypeDependent = true;
92 ValueDependent = true;
93 }
94 // (VD) - the name of a non-type template parameter,
95 else if (isa<NonTypeTemplateParmDecl>(D))
96 ValueDependent = true;
97 // (VD) - a constant with integral or enumeration type and is
98 // initialized with an expression that is value-dependent.
99 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
100 if (Var->getType()->isIntegralType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000101 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000102 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000103 if (Init->isValueDependent())
104 ValueDependent = true;
105 }
Douglas Gregored6c7442009-11-23 11:41:28 +0000106 }
107 // (TD) - a nested-name-specifier or a qualified-id that names a
108 // member of an unknown specialization.
109 // (handled by DependentScopeDeclRefExpr)
110}
111
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000112DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
113 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000114 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000115 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000116 QualType T)
117 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000118 DecoratedD(D,
119 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000120 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000121 Loc(NameLoc) {
122 if (Qualifier) {
123 NameQualifier *NQ = getNameQualifier();
124 NQ->NNS = Qualifier;
125 NQ->Range = QualifierRange;
126 }
127
John McCall6b51f282009-11-23 01:53:49 +0000128 if (TemplateArgs)
129 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000130
131 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000132}
133
134DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
135 NestedNameSpecifier *Qualifier,
136 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000137 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000138 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000139 QualType T,
140 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000141 std::size_t Size = sizeof(DeclRefExpr);
142 if (Qualifier != 0)
143 Size += sizeof(NameQualifier);
144
John McCall6b51f282009-11-23 01:53:49 +0000145 if (TemplateArgs)
146 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000147
148 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
149 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000150 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000151}
152
153SourceRange DeclRefExpr::getSourceRange() const {
154 // FIXME: Does not handle multi-token names well, e.g., operator[].
155 SourceRange R(Loc);
156
157 if (hasQualifier())
158 R.setBegin(getQualifierRange().getBegin());
159 if (hasExplicitTemplateArgumentList())
160 R.setEnd(getRAngleLoc());
161 return R;
162}
163
Anders Carlsson2fb08242009-09-08 18:24:21 +0000164// FIXME: Maybe this should use DeclPrinter with a special "print predefined
165// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000166std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
167 ASTContext &Context = CurrentDecl->getASTContext();
168
Anders Carlsson2fb08242009-09-08 18:24:21 +0000169 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000170 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000171 return FD->getNameAsString();
172
173 llvm::SmallString<256> Name;
174 llvm::raw_svector_ostream Out(Name);
175
176 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000177 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000178 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000179 if (MD->isStatic())
180 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000181 }
182
183 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000184
185 std::string Proto = FD->getQualifiedNameAsString(Policy);
186
John McCall9dd450b2009-09-21 23:43:11 +0000187 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000188 const FunctionProtoType *FT = 0;
189 if (FD->hasWrittenPrototype())
190 FT = dyn_cast<FunctionProtoType>(AFT);
191
192 Proto += "(";
193 if (FT) {
194 llvm::raw_string_ostream POut(Proto);
195 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
196 if (i) POut << ", ";
197 std::string Param;
198 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
199 POut << Param;
200 }
201
202 if (FT->isVariadic()) {
203 if (FD->getNumParams()) POut << ", ";
204 POut << "...";
205 }
206 }
207 Proto += ")";
208
Sam Weinig4e83bd22009-12-27 01:38:20 +0000209 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
210 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
211 if (ThisQuals.hasConst())
212 Proto += " const";
213 if (ThisQuals.hasVolatile())
214 Proto += " volatile";
215 }
216
Sam Weinigd060ed42009-12-06 23:55:13 +0000217 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
218 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000219
220 Out << Proto;
221
222 Out.flush();
223 return Name.str().str();
224 }
225 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
226 llvm::SmallString<256> Name;
227 llvm::raw_svector_ostream Out(Name);
228 Out << (MD->isInstanceMethod() ? '-' : '+');
229 Out << '[';
230 Out << MD->getClassInterface()->getNameAsString();
231 if (const ObjCCategoryImplDecl *CID =
232 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
233 Out << '(';
234 Out << CID->getNameAsString();
235 Out << ')';
236 }
237 Out << ' ';
238 Out << MD->getSelector().getAsString();
239 Out << ']';
240
241 Out.flush();
242 return Name.str().str();
243 }
244 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
245 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
246 return "top level";
247 }
248 return "";
249}
250
Chris Lattnera0173132008-06-07 22:13:43 +0000251/// getValueAsApproximateDouble - This returns the value as an inaccurate
252/// double. Note that this may cause loss of precision, but is useful for
253/// debugging dumps, etc.
254double FloatingLiteral::getValueAsApproximateDouble() const {
255 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000256 bool ignored;
257 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
258 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000259 return V.convertToDouble();
260}
261
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000262StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
263 unsigned ByteLength, bool Wide,
264 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000265 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000266 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000267 // Allocate enough space for the StringLiteral plus an array of locations for
268 // any concatenated string tokens.
269 void *Mem = C.Allocate(sizeof(StringLiteral)+
270 sizeof(SourceLocation)*(NumStrs-1),
271 llvm::alignof<StringLiteral>());
272 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Steve Naroffdf7855b2007-02-21 23:46:25 +0000274 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000275 char *AStrData = new (C, 1) char[ByteLength];
276 memcpy(AStrData, StrData, ByteLength);
277 SL->StrData = AStrData;
278 SL->ByteLength = ByteLength;
279 SL->IsWide = Wide;
280 SL->TokLocs[0] = Loc[0];
281 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000282
Chris Lattner630970d2009-02-18 05:49:11 +0000283 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000284 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
285 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000286}
287
Douglas Gregor958dfc92009-04-15 16:35:07 +0000288StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
289 void *Mem = C.Allocate(sizeof(StringLiteral)+
290 sizeof(SourceLocation)*(NumStrs-1),
291 llvm::alignof<StringLiteral>());
292 StringLiteral *SL = new (Mem) StringLiteral(QualType());
293 SL->StrData = 0;
294 SL->ByteLength = 0;
295 SL->NumConcatenated = NumStrs;
296 return SL;
297}
298
Douglas Gregore26a2852009-08-07 06:08:38 +0000299void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000300 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000301 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000302}
303
Daniel Dunbar36217882009-09-22 03:27:33 +0000304void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000305 if (StrData)
306 C.Deallocate(const_cast<char*>(StrData));
307
Daniel Dunbar36217882009-09-22 03:27:33 +0000308 char *AStrData = new (C, 1) char[Str.size()];
309 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000310 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000311 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000312}
313
Chris Lattner1b926492006-08-23 06:42:10 +0000314/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
315/// corresponds to, e.g. "sizeof" or "[pre]++".
316const char *UnaryOperator::getOpcodeStr(Opcode Op) {
317 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000318 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000319 case PostInc: return "++";
320 case PostDec: return "--";
321 case PreInc: return "++";
322 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000323 case AddrOf: return "&";
324 case Deref: return "*";
325 case Plus: return "+";
326 case Minus: return "-";
327 case Not: return "~";
328 case LNot: return "!";
329 case Real: return "__real";
330 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000331 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000332 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000333 }
334}
335
Mike Stump11289f42009-09-09 15:08:12 +0000336UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000337UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
338 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000339 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000340 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
341 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
342 case OO_Amp: return AddrOf;
343 case OO_Star: return Deref;
344 case OO_Plus: return Plus;
345 case OO_Minus: return Minus;
346 case OO_Tilde: return Not;
347 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000348 }
349}
350
351OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
352 switch (Opc) {
353 case PostInc: case PreInc: return OO_PlusPlus;
354 case PostDec: case PreDec: return OO_MinusMinus;
355 case AddrOf: return OO_Amp;
356 case Deref: return OO_Star;
357 case Plus: return OO_Plus;
358 case Minus: return OO_Minus;
359 case Not: return OO_Tilde;
360 case LNot: return OO_Exclaim;
361 default: return OO_None;
362 }
363}
364
365
Chris Lattner0eedafe2006-08-24 04:56:27 +0000366//===----------------------------------------------------------------------===//
367// Postfix Operators.
368//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000369
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000370CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000371 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000372 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000373 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000374 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000375 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000376
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000377 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000378 SubExprs[FN] = fn;
379 for (unsigned i = 0; i != numargs; ++i)
380 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000381
Douglas Gregor993603d2008-11-14 16:09:21 +0000382 RParenLoc = rparenloc;
383}
Nate Begeman1e36a852008-01-17 17:46:27 +0000384
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000385CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
386 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000387 : Expr(CallExprClass, t,
388 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000389 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000390 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000391
392 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000393 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000394 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000395 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000396
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000397 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000398}
399
Mike Stump11289f42009-09-09 15:08:12 +0000400CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
401 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000402 SubExprs = new (C) Stmt*[1];
403}
404
Douglas Gregore26a2852009-08-07 06:08:38 +0000405void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000406 DestroyChildren(C);
407 if (SubExprs) C.Deallocate(SubExprs);
408 this->~CallExpr();
409 C.Deallocate(this);
410}
411
Nuno Lopes518e3702009-12-20 23:11:08 +0000412Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000413 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000414 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000415 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000416 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
417 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000418
419 return 0;
420}
421
Nuno Lopes518e3702009-12-20 23:11:08 +0000422FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000423 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000424}
425
Chris Lattnere4407ed2007-12-28 05:25:02 +0000426/// setNumArgs - This changes the number of arguments present in this call.
427/// Any orphaned expressions are deleted by this, and any new operands are set
428/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000429void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000430 // No change, just return.
431 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnere4407ed2007-12-28 05:25:02 +0000433 // If shrinking # arguments, just delete the extras and forgot them.
434 if (NumArgs < getNumArgs()) {
435 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000436 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000437 this->NumArgs = NumArgs;
438 return;
439 }
440
441 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000442 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000443 // Copy over args.
444 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
445 NewSubExprs[i] = SubExprs[i];
446 // Null out new args.
447 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
448 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000449
Douglas Gregorba6e5572009-04-17 21:46:47 +0000450 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000451 SubExprs = NewSubExprs;
452 this->NumArgs = NumArgs;
453}
454
Chris Lattner01ff98a2008-10-06 05:00:53 +0000455/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
456/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000457unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000458 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000459 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000460 // ImplicitCastExpr.
461 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
462 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000463 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000464
Steve Narofff6e3b3292008-01-31 01:07:12 +0000465 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
466 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000467 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000468
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000469 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
470 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000471 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000473 if (!FDecl->getIdentifier())
474 return 0;
475
Douglas Gregor15fc9562009-09-12 00:22:50 +0000476 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000477}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000478
Anders Carlsson00a27592009-05-26 04:57:27 +0000479QualType CallExpr::getCallReturnType() const {
480 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000481 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000482 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000483 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000484 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000485
John McCall9dd450b2009-09-21 23:43:11 +0000486 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000487 return FnType->getResultType();
488}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000489
Mike Stump11289f42009-09-09 15:08:12 +0000490MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000491 SourceRange qualrange, ValueDecl *memberdecl,
John McCall6b51f282009-11-23 01:53:49 +0000492 SourceLocation l, const TemplateArgumentListInfo *targs,
493 QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000494 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000495 base->isTypeDependent() || (qual && qual->isDependent()),
496 base->isValueDependent() || (qual && qual->isDependent())),
497 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCall6b51f282009-11-23 01:53:49 +0000498 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000499 // Initialize the qualifier, if any.
500 if (HasQualifier) {
501 NameQualifier *NQ = getMemberQualifier();
502 NQ->NNS = qual;
503 NQ->Range = qualrange;
504 }
Mike Stump11289f42009-09-09 15:08:12 +0000505
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000506 // Initialize the explicit template argument list, if any.
John McCall6b51f282009-11-23 01:53:49 +0000507 if (targs)
508 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000509}
510
Mike Stump11289f42009-09-09 15:08:12 +0000511MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
512 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000513 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000514 ValueDecl *memberdecl,
Mike Stump11289f42009-09-09 15:08:12 +0000515 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000516 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000517 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000518 std::size_t Size = sizeof(MemberExpr);
519 if (qual != 0)
520 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000521
John McCall6b51f282009-11-23 01:53:49 +0000522 if (targs)
523 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000525 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000526 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCall6b51f282009-11-23 01:53:49 +0000527 targs, ty);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000528}
529
Anders Carlsson496335e2009-09-03 00:59:21 +0000530const char *CastExpr::getCastKindName() const {
531 switch (getCastKind()) {
532 case CastExpr::CK_Unknown:
533 return "Unknown";
534 case CastExpr::CK_BitCast:
535 return "BitCast";
536 case CastExpr::CK_NoOp:
537 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000538 case CastExpr::CK_BaseToDerived:
539 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000540 case CastExpr::CK_DerivedToBase:
541 return "DerivedToBase";
542 case CastExpr::CK_Dynamic:
543 return "Dynamic";
544 case CastExpr::CK_ToUnion:
545 return "ToUnion";
546 case CastExpr::CK_ArrayToPointerDecay:
547 return "ArrayToPointerDecay";
548 case CastExpr::CK_FunctionToPointerDecay:
549 return "FunctionToPointerDecay";
550 case CastExpr::CK_NullToMemberPointer:
551 return "NullToMemberPointer";
552 case CastExpr::CK_BaseToDerivedMemberPointer:
553 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000554 case CastExpr::CK_DerivedToBaseMemberPointer:
555 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000556 case CastExpr::CK_UserDefinedConversion:
557 return "UserDefinedConversion";
558 case CastExpr::CK_ConstructorConversion:
559 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000560 case CastExpr::CK_IntegralToPointer:
561 return "IntegralToPointer";
562 case CastExpr::CK_PointerToIntegral:
563 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000564 case CastExpr::CK_ToVoid:
565 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000566 case CastExpr::CK_VectorSplat:
567 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000568 case CastExpr::CK_IntegralCast:
569 return "IntegralCast";
570 case CastExpr::CK_IntegralToFloating:
571 return "IntegralToFloating";
572 case CastExpr::CK_FloatingToIntegral:
573 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000574 case CastExpr::CK_FloatingCast:
575 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000576 case CastExpr::CK_MemberPointerToBoolean:
577 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000578 case CastExpr::CK_AnyPointerToObjCPointerCast:
579 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000580 case CastExpr::CK_AnyPointerToBlockPointerCast:
581 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Anders Carlsson496335e2009-09-03 00:59:21 +0000584 assert(0 && "Unhandled cast kind!");
585 return 0;
586}
587
Douglas Gregord196a582009-12-14 19:27:10 +0000588Expr *CastExpr::getSubExprAsWritten() {
589 Expr *SubExpr = 0;
590 CastExpr *E = this;
591 do {
592 SubExpr = E->getSubExpr();
593
594 // Skip any temporary bindings; they're implicit.
595 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
596 SubExpr = Binder->getSubExpr();
597
598 // Conversions by constructor and conversion functions have a
599 // subexpression describing the call; strip it off.
600 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
601 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
602 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
603 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
604
605 // If the subexpression we're left with is an implicit cast, look
606 // through that, too.
607 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
608
609 return SubExpr;
610}
611
Chris Lattner1b926492006-08-23 06:42:10 +0000612/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
613/// corresponds to, e.g. "<<=".
614const char *BinaryOperator::getOpcodeStr(Opcode Op) {
615 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000616 case PtrMemD: return ".*";
617 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000618 case Mul: return "*";
619 case Div: return "/";
620 case Rem: return "%";
621 case Add: return "+";
622 case Sub: return "-";
623 case Shl: return "<<";
624 case Shr: return ">>";
625 case LT: return "<";
626 case GT: return ">";
627 case LE: return "<=";
628 case GE: return ">=";
629 case EQ: return "==";
630 case NE: return "!=";
631 case And: return "&";
632 case Xor: return "^";
633 case Or: return "|";
634 case LAnd: return "&&";
635 case LOr: return "||";
636 case Assign: return "=";
637 case MulAssign: return "*=";
638 case DivAssign: return "/=";
639 case RemAssign: return "%=";
640 case AddAssign: return "+=";
641 case SubAssign: return "-=";
642 case ShlAssign: return "<<=";
643 case ShrAssign: return ">>=";
644 case AndAssign: return "&=";
645 case XorAssign: return "^=";
646 case OrAssign: return "|=";
647 case Comma: return ",";
648 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000649
650 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000651}
Steve Naroff47500512007-04-19 23:00:49 +0000652
Mike Stump11289f42009-09-09 15:08:12 +0000653BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000654BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
655 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000656 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000657 case OO_Plus: return Add;
658 case OO_Minus: return Sub;
659 case OO_Star: return Mul;
660 case OO_Slash: return Div;
661 case OO_Percent: return Rem;
662 case OO_Caret: return Xor;
663 case OO_Amp: return And;
664 case OO_Pipe: return Or;
665 case OO_Equal: return Assign;
666 case OO_Less: return LT;
667 case OO_Greater: return GT;
668 case OO_PlusEqual: return AddAssign;
669 case OO_MinusEqual: return SubAssign;
670 case OO_StarEqual: return MulAssign;
671 case OO_SlashEqual: return DivAssign;
672 case OO_PercentEqual: return RemAssign;
673 case OO_CaretEqual: return XorAssign;
674 case OO_AmpEqual: return AndAssign;
675 case OO_PipeEqual: return OrAssign;
676 case OO_LessLess: return Shl;
677 case OO_GreaterGreater: return Shr;
678 case OO_LessLessEqual: return ShlAssign;
679 case OO_GreaterGreaterEqual: return ShrAssign;
680 case OO_EqualEqual: return EQ;
681 case OO_ExclaimEqual: return NE;
682 case OO_LessEqual: return LE;
683 case OO_GreaterEqual: return GE;
684 case OO_AmpAmp: return LAnd;
685 case OO_PipePipe: return LOr;
686 case OO_Comma: return Comma;
687 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000688 }
689}
690
691OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
692 static const OverloadedOperatorKind OverOps[] = {
693 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
694 OO_Star, OO_Slash, OO_Percent,
695 OO_Plus, OO_Minus,
696 OO_LessLess, OO_GreaterGreater,
697 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
698 OO_EqualEqual, OO_ExclaimEqual,
699 OO_Amp,
700 OO_Caret,
701 OO_Pipe,
702 OO_AmpAmp,
703 OO_PipePipe,
704 OO_Equal, OO_StarEqual,
705 OO_SlashEqual, OO_PercentEqual,
706 OO_PlusEqual, OO_MinusEqual,
707 OO_LessLessEqual, OO_GreaterGreaterEqual,
708 OO_AmpEqual, OO_CaretEqual,
709 OO_PipeEqual,
710 OO_Comma
711 };
712 return OverOps[Opc];
713}
714
Ted Kremenek013041e2010-02-19 01:50:18 +0000715InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000716 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000717 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000718 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump11289f42009-09-09 15:08:12 +0000719 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenek013041e2010-02-19 01:50:18 +0000720 UnionFieldInit(0), HadArrayRangeDesignator(false)
721{
722 for (unsigned I = 0; I != numInits; ++I) {
723 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000724 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000725 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000726 ValueDependent = true;
727 }
Ted Kremenek013041e2010-02-19 01:50:18 +0000728
729 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000730}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000731
Ted Kremenek013041e2010-02-19 01:50:18 +0000732void InitListExpr::reserveInits(unsigned NumInits) {
733 if (NumInits > InitExprs.size())
734 InitExprs.reserve(NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000735}
736
Ted Kremenek013041e2010-02-19 01:50:18 +0000737void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
738 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
739 Idx < LastIdx; ++Idx)
740 InitExprs[Idx]->Destroy(Context);
741 InitExprs.resize(NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000742}
743
Ted Kremenek013041e2010-02-19 01:50:18 +0000744Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
745 if (Init >= InitExprs.size()) {
746 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
747 InitExprs.back() = expr;
748 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000751 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
752 InitExprs[Init] = expr;
753 return Result;
754}
755
Steve Naroff991e99d2008-09-04 15:31:07 +0000756/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000757///
758const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000759 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000760 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000761}
762
Mike Stump11289f42009-09-09 15:08:12 +0000763SourceLocation BlockExpr::getCaretLocation() const {
764 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000765}
Mike Stump11289f42009-09-09 15:08:12 +0000766const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000767 return TheBlock->getBody();
768}
Mike Stump11289f42009-09-09 15:08:12 +0000769Stmt *BlockExpr::getBody() {
770 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000771}
Steve Naroff415d3d52008-10-08 17:01:13 +0000772
773
Chris Lattner1ec5f562007-06-27 05:38:08 +0000774//===----------------------------------------------------------------------===//
775// Generic Expression Routines
776//===----------------------------------------------------------------------===//
777
Chris Lattner237f2752009-02-14 07:37:35 +0000778/// isUnusedResultAWarning - Return true if this immediate expression should
779/// be warned about if the result is unused. If so, fill in Loc and Ranges
780/// with location to warn on and the source range[s] to report with the
781/// warning.
782bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000783 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000784 // Don't warn if the expr is type dependent. The type could end up
785 // instantiating to void.
786 if (isTypeDependent())
787 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Chris Lattner1ec5f562007-06-27 05:38:08 +0000789 switch (getStmtClass()) {
790 default:
John McCallc493a732010-03-12 07:11:26 +0000791 if (getType()->isVoidType())
792 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000793 Loc = getExprLoc();
794 R1 = getSourceRange();
795 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000796 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000797 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000798 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000799 case UnaryOperatorClass: {
800 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattner1ec5f562007-06-27 05:38:08 +0000802 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000803 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000804 case UnaryOperator::PostInc:
805 case UnaryOperator::PostDec:
806 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000807 case UnaryOperator::PreDec: // ++/--
808 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000809 case UnaryOperator::Deref:
810 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000811 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000812 return false;
813 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000814 case UnaryOperator::Real:
815 case UnaryOperator::Imag:
816 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000817 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
818 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000819 return false;
820 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000821 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000822 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000823 }
Chris Lattner237f2752009-02-14 07:37:35 +0000824 Loc = UO->getOperatorLoc();
825 R1 = UO->getSubExpr()->getSourceRange();
826 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000827 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000828 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000829 const BinaryOperator *BO = cast<BinaryOperator>(this);
830 // Consider comma to have side effects if the LHS or RHS does.
John McCall1e3715a2010-02-16 04:10:53 +0000831 if (BO->getOpcode() == BinaryOperator::Comma) {
832 // ((foo = <blah>), 0) is an idiom for hiding the result (and
833 // lvalue-ness) of an assignment written in a macro.
834 if (IntegerLiteral *IE =
835 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
836 if (IE->getValue() == 0)
837 return false;
838
John McCallc493a732010-03-12 07:11:26 +0000839 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
840 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000841 }
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattner237f2752009-02-14 07:37:35 +0000843 if (BO->isAssignmentOp())
844 return false;
845 Loc = BO->getOperatorLoc();
846 R1 = BO->getLHS()->getSourceRange();
847 R2 = BO->getRHS()->getSourceRange();
848 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000849 }
Chris Lattner86928112007-08-25 02:00:02 +0000850 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000851 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000852
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000853 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000854 // The condition must be evaluated, but if either the LHS or RHS is a
855 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000856 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000857 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000858 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000859 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000860 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000861 }
862
Chris Lattnera44d1162007-06-27 05:58:59 +0000863 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000864 // If the base pointer or element is to a volatile pointer/field, accessing
865 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000866 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000867 return false;
868 Loc = cast<MemberExpr>(this)->getMemberLoc();
869 R1 = SourceRange(Loc, Loc);
870 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
871 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner1ec5f562007-06-27 05:38:08 +0000873 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000874 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000875 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000876 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000877 return false;
878 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
879 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
880 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
881 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000882
Chris Lattner1ec5f562007-06-27 05:38:08 +0000883 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000884 case CXXOperatorCallExprClass:
885 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000886 // If this is a direct call, get the callee.
887 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +0000888 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000889 // If the callee has attribute pure, const, or warn_unused_result, warn
890 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000891 //
892 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
893 // updated to match for QoI.
894 if (FD->getAttr<WarnUnusedResultAttr>() ||
895 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
896 Loc = CE->getCallee()->getLocStart();
897 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000898
Chris Lattner1a6babf2009-10-13 04:53:48 +0000899 if (unsigned NumArgs = CE->getNumArgs())
900 R2 = SourceRange(CE->getArg(0)->getLocStart(),
901 CE->getArg(NumArgs-1)->getLocEnd());
902 return true;
903 }
Chris Lattner237f2752009-02-14 07:37:35 +0000904 }
905 return false;
906 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000907
908 case CXXTemporaryObjectExprClass:
909 case CXXConstructExprClass:
910 return false;
911
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000912 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000913 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000914
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000915 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000916#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000917 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000918 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000919 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000920 Loc = Ref->getLocation();
921 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
922 if (Ref->getBase())
923 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000924#else
925 Loc = getExprLoc();
926 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000927#endif
928 return true;
929 }
Chris Lattner944d3062008-07-26 19:51:01 +0000930 case StmtExprClass: {
931 // Statement exprs don't logically have side effects themselves, but are
932 // sometimes used in macros in ways that give them a type that is unused.
933 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
934 // however, if the result of the stmt expr is dead, we don't want to emit a
935 // warning.
936 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
937 if (!CS->body_empty())
938 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000939 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000940
John McCallc493a732010-03-12 07:11:26 +0000941 if (getType()->isVoidType())
942 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000943 Loc = cast<StmtExpr>(this)->getLParenLoc();
944 R1 = getSourceRange();
945 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000946 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000947 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000948 // If this is an explicit cast to void, allow it. People do this when they
949 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000950 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000951 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000952 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
953 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
954 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000955 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +0000956 if (getType()->isVoidType())
957 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000958 const CastExpr *CE = cast<CastExpr>(this);
959
960 // If this is a cast to void or a constructor conversion, check the operand.
961 // Otherwise, the result of the cast is unused.
962 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
963 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000964 return (cast<CastExpr>(this)->getSubExpr()
965 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000966 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
967 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
968 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000969 }
Mike Stump11289f42009-09-09 15:08:12 +0000970
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000971 case ImplicitCastExprClass:
972 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000973 return (cast<ImplicitCastExpr>(this)
974 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000975
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000976 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000977 return (cast<CXXDefaultArgExpr>(this)
978 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000979
980 case CXXNewExprClass:
981 // FIXME: In theory, there might be new expressions that don't have side
982 // effects (e.g. a placement new with an uninitialized POD).
983 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000984 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000985 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000986 return (cast<CXXBindTemporaryExpr>(this)
987 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +0000988 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000989 return (cast<CXXExprWithTemporaries>(this)
990 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000991 }
Chris Lattner1ec5f562007-06-27 05:38:08 +0000992}
993
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000994/// DeclCanBeLvalue - Determine whether the given declaration can be
995/// an lvalue. This is a helper routine for isLvalue.
996static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000997 // C++ [temp.param]p6:
998 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000999 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001000 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1001 return NTTParm->getType()->isReferenceType();
1002
Douglas Gregor91f84212008-12-11 16:49:14 +00001003 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001004 // C++ 3.10p2: An lvalue refers to an object or function.
1005 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001006 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001007}
1008
Steve Naroff475cca02007-05-14 17:19:29 +00001009/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1010/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001011/// - name, where name must be a variable
1012/// - e[i]
1013/// - (e), where e must be an lvalue
1014/// - e.name, where e must be an lvalue
1015/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001016/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001017/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001018/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001019/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001020///
Chris Lattner67315442008-07-26 21:30:36 +00001021Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001022 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1023
1024 isLvalueResult Res = isLvalueInternal(Ctx);
1025 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1026 return Res;
1027
Douglas Gregor9a657932008-10-21 23:43:52 +00001028 // first, check the type (C99 6.3.2.1). Expressions with function
1029 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001030 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001031 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001032
Steve Naroff1018ea32008-02-10 01:39:04 +00001033 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001034 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001035 return LV_IncompleteVoidType;
1036
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001037 return LV_Valid;
1038}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001039
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001040// Check whether the expression can be sanely treated like an l-value
1041Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001042 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001043 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001044 case StringLiteralClass: // C99 6.5.1p4
1045 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001046 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001047 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001048 // For vectors, make sure base is an lvalue (i.e. not a function call).
1049 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001050 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001051 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001052 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001053 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1054 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001055 return LV_Valid;
1056 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001057 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001058 case BlockDeclRefExprClass: {
1059 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001060 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001061 return LV_Valid;
1062 break;
1063 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001064 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001065 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001066 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1067 NamedDecl *Member = m->getMemberDecl();
1068 // C++ [expr.ref]p4:
1069 // If E2 is declared to have type "reference to T", then E1.E2
1070 // is an lvalue.
1071 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1072 if (Value->getType()->isReferenceType())
1073 return LV_Valid;
1074
1075 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001076 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001077 return LV_Valid;
1078
1079 // -- If E2 is a non-static data member [...]. If E1 is an
1080 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001081 if (isa<FieldDecl>(Member)) {
1082 if (m->isArrow())
1083 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001084 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001085 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001086
1087 // -- If it refers to a static member function [...], then
1088 // E1.E2 is an lvalue.
1089 // -- Otherwise, if E1.E2 refers to a non-static member
1090 // function [...], then E1.E2 is not an lvalue.
1091 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1092 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1093
1094 // -- If E2 is a member enumerator [...], the expression E1.E2
1095 // is not an lvalue.
1096 if (isa<EnumConstantDecl>(Member))
1097 return LV_InvalidExpression;
1098
1099 // Not an lvalue.
1100 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001101 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001102
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001103 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001104 if (m->isArrow())
1105 return LV_Valid;
1106 Expr *BaseExp = m->getBase();
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001107 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass)
1108 return LV_SubObjCPropertySetting;
1109 return
1110 (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ?
1111 LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001112 }
Chris Lattner595db862007-10-30 22:53:42 +00001113 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001114 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001115 return LV_Valid; // C99 6.5.3p4
1116
1117 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001118 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1119 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001120 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001121
1122 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1123 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1124 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1125 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001126 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001127 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001128 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1129 return LV_Valid;
1130
1131 // If this is a conversion to a class temporary, make a note of
1132 // that.
1133 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1134 return LV_ClassTemporary;
1135
1136 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001137 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001138 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001139 case BinaryOperatorClass:
1140 case CompoundAssignOperatorClass: {
1141 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001142
1143 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1144 BinOp->getOpcode() == BinaryOperator::Comma)
1145 return BinOp->getRHS()->isLvalue(Ctx);
1146
Sebastian Redl112a97662009-02-07 00:15:38 +00001147 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001148 // The result of a .* expression is an lvalue only if its first operand is
1149 // an lvalue and its second operand is a pointer to data member.
1150 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001151 !BinOp->getType()->isFunctionType())
1152 return BinOp->getLHS()->isLvalue(Ctx);
1153
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001154 // The result of an ->* expression is an lvalue only if its second operand
1155 // is a pointer to data member.
1156 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1157 !BinOp->getType()->isFunctionType()) {
1158 QualType Ty = BinOp->getRHS()->getType();
1159 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1160 return LV_Valid;
1161 }
1162
Douglas Gregor58e008d2008-11-13 20:12:29 +00001163 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001164 return LV_InvalidExpression;
1165
Douglas Gregor58e008d2008-11-13 20:12:29 +00001166 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001167 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001168 // The result of an assignment operation [...] is an lvalue.
1169 return LV_Valid;
1170
1171
1172 // C99 6.5.16:
1173 // An assignment expression [...] is not an lvalue.
1174 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001177 case CXXOperatorCallExprClass:
1178 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001179 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001180 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001181 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001182 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1183 if (ReturnType->isLValueReferenceType())
1184 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001185
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001186 // If the function is returning a class temporary, make a note of
1187 // that.
1188 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1189 return LV_ClassTemporary;
1190
Douglas Gregor6b754842008-10-28 00:22:11 +00001191 break;
1192 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001193 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001194 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001195 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001196 case ChooseExprClass:
1197 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001198 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001199 case ExtVectorElementExprClass:
1200 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001201 return LV_DuplicateVectorComponents;
1202 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001203 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1204 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001205 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1206 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001207 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001208 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001209 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001210 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001211 case UnresolvedLookupExprClass:
1212 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001213 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001214 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001215 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001216 case CXXFunctionalCastExprClass:
1217 case CXXStaticCastExprClass:
1218 case CXXDynamicCastExprClass:
1219 case CXXReinterpretCastExprClass:
1220 case CXXConstCastExprClass:
1221 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001222 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001223 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1224 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001225 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1226 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001227 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001228
1229 // If this is a conversion to a class temporary, make a note of
1230 // that.
1231 if (Ctx.getLangOptions().CPlusPlus &&
1232 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1233 return LV_ClassTemporary;
1234
Douglas Gregor6b754842008-10-28 00:22:11 +00001235 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001236 case CXXTypeidExprClass:
1237 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1238 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001239 case CXXBindTemporaryExprClass:
1240 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1241 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001242 case CXXBindReferenceExprClass:
1243 // Something that's bound to a reference is always an lvalue.
1244 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001245 case ConditionalOperatorClass: {
1246 // Complicated handling is only for C++.
1247 if (!Ctx.getLangOptions().CPlusPlus)
1248 return LV_InvalidExpression;
1249
1250 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1251 // everywhere there's an object converted to an rvalue. Also, any other
1252 // casts should be wrapped by ImplicitCastExprs. There's just the special
1253 // case involving throws to work out.
1254 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001255 Expr *True = Cond->getTrueExpr();
1256 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001257 // C++0x 5.16p2
1258 // If either the second or the third operand has type (cv) void, [...]
1259 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001260 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001261 return LV_InvalidExpression;
1262
1263 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001264 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001265 return LV_InvalidExpression;
1266
1267 // That's it.
1268 return LV_Valid;
1269 }
1270
Douglas Gregor5103eff2009-12-19 07:07:47 +00001271 case Expr::CXXExprWithTemporariesClass:
1272 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1273
1274 case Expr::ObjCMessageExprClass:
1275 if (const ObjCMethodDecl *Method
1276 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1277 if (Method->getResultType()->isLValueReferenceType())
1278 return LV_Valid;
1279 break;
1280
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001281 case Expr::CXXConstructExprClass:
1282 case Expr::CXXTemporaryObjectExprClass:
1283 case Expr::CXXZeroInitValueExprClass:
1284 return LV_ClassTemporary;
1285
Steve Naroff9358c712007-05-27 23:58:33 +00001286 default:
1287 break;
Steve Naroff47500512007-04-19 23:00:49 +00001288 }
Steve Naroff9358c712007-05-27 23:58:33 +00001289 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001290}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001291
Steve Naroff475cca02007-05-14 17:19:29 +00001292/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1293/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001294/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001295/// recursively, any member or element of all contained aggregates or unions)
1296/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001297Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001298Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001299 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Steve Naroff9358c712007-05-27 23:58:33 +00001301 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001302 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001303 // C++ 3.10p11: Functions cannot be modified, but pointers to
1304 // functions can be modifiable.
1305 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1306 return MLV_NotObjectType;
1307 break;
1308
Chris Lattner1ec5f562007-06-27 05:38:08 +00001309 case LV_NotObjectType: return MLV_NotObjectType;
1310 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001311 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001312 case LV_InvalidExpression:
1313 // If the top level is a C-style cast, and the subexpression is a valid
1314 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1315 // GCC extension. We don't support it, but we want to produce good
1316 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001317 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1318 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1319 if (Loc)
1320 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001321 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001322 }
1323 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001324 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001325 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001326 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
1327 case LV_SubObjCPropertyGetterSetting:
1328 return MLV_SubObjCPropertyGetterSetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001329 case LV_ClassTemporary:
1330 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001331 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001332
1333 // The following is illegal:
1334 // void takeclosure(void (^C)(void));
1335 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1336 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001337 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001338 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1339 return MLV_NotBlockQualified;
1340 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001341
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001342 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001343 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001344 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1345 if (Expr->getSetterMethod() == 0)
1346 return MLV_NoSetterProperty;
1347 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001348
Chris Lattner7adf0762008-08-04 07:31:14 +00001349 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001350
Chris Lattner7adf0762008-08-04 07:31:14 +00001351 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001352 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001353 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001354 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001355 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001356 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001357
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001358 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001359 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001360 return MLV_ConstQualified;
1361 }
Mike Stump11289f42009-09-09 15:08:12 +00001362
Mike Stump11289f42009-09-09 15:08:12 +00001363 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001364}
1365
Fariborz Jahanian07735332009-02-22 18:40:18 +00001366/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001367/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001368bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001369 switch (getStmtClass()) {
1370 default:
1371 return false;
1372 case ObjCIvarRefExprClass:
1373 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001374 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001375 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001376 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001377 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001378 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001379 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001380 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001381 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001382 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001383 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001384 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1385 if (VD->hasGlobalStorage())
1386 return true;
1387 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001388 // dereferencing to a pointer is always a gc'able candidate,
1389 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001390 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001391 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001392 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001393 return false;
1394 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001395 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001396 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001397 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001398 }
1399 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001400 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001401 }
1402}
Ted Kremenekfff70962008-01-17 16:57:34 +00001403Expr* Expr::IgnoreParens() {
1404 Expr* E = this;
1405 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1406 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001407
Ted Kremenekfff70962008-01-17 16:57:34 +00001408 return E;
1409}
1410
Chris Lattnerf2660962008-02-13 01:02:39 +00001411/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1412/// or CastExprs or ImplicitCastExprs, returning their operand.
1413Expr *Expr::IgnoreParenCasts() {
1414 Expr *E = this;
1415 while (true) {
1416 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1417 E = P->getSubExpr();
1418 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1419 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001420 else
1421 return E;
1422 }
1423}
1424
Chris Lattneref26c772009-03-13 17:28:01 +00001425/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1426/// value (including ptr->int casts of the same size). Strip off any
1427/// ParenExpr or CastExprs, returning their operand.
1428Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1429 Expr *E = this;
1430 while (true) {
1431 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1432 E = P->getSubExpr();
1433 continue;
1434 }
Mike Stump11289f42009-09-09 15:08:12 +00001435
Chris Lattneref26c772009-03-13 17:28:01 +00001436 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1437 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1438 // ptr<->int casts of the same width. We also ignore all identify casts.
1439 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001440
Chris Lattneref26c772009-03-13 17:28:01 +00001441 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1442 E = SE;
1443 continue;
1444 }
Mike Stump11289f42009-09-09 15:08:12 +00001445
Chris Lattneref26c772009-03-13 17:28:01 +00001446 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1447 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1448 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1449 E = SE;
1450 continue;
1451 }
1452 }
Mike Stump11289f42009-09-09 15:08:12 +00001453
Chris Lattneref26c772009-03-13 17:28:01 +00001454 return E;
1455 }
1456}
1457
Douglas Gregord196a582009-12-14 19:27:10 +00001458bool Expr::isDefaultArgument() const {
1459 const Expr *E = this;
1460 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1461 E = ICE->getSubExprAsWritten();
1462
1463 return isa<CXXDefaultArgExpr>(E);
1464}
Chris Lattneref26c772009-03-13 17:28:01 +00001465
Douglas Gregor4619e432008-12-05 23:32:09 +00001466/// hasAnyTypeDependentArguments - Determines if any of the expressions
1467/// in Exprs is type-dependent.
1468bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1469 for (unsigned I = 0; I < NumExprs; ++I)
1470 if (Exprs[I]->isTypeDependent())
1471 return true;
1472
1473 return false;
1474}
1475
1476/// hasAnyValueDependentArguments - Determines if any of the expressions
1477/// in Exprs is value-dependent.
1478bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1479 for (unsigned I = 0; I < NumExprs; ++I)
1480 if (Exprs[I]->isValueDependent())
1481 return true;
1482
1483 return false;
1484}
1485
Eli Friedman7139af42009-01-25 02:32:41 +00001486bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001487 // This function is attempting whether an expression is an initializer
1488 // which can be evaluated at compile-time. isEvaluatable handles most
1489 // of the cases, but it can't deal with some initializer-specific
1490 // expressions, and it can't deal with aggregates; we deal with those here,
1491 // and fall back to isEvaluatable for the other cases.
1492
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001493 // FIXME: This function assumes the variable being assigned to
1494 // isn't a reference type!
1495
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001496 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001497 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001498 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001499 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001500 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001501 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001502 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001503 // This handles gcc's extension that allows global initializers like
1504 // "struct x {int x;} x = (struct x) {};".
1505 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001506 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001507 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001508 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001509 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001510 // FIXME: This doesn't deal with fields with reference types correctly.
1511 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1512 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001513 const InitListExpr *Exp = cast<InitListExpr>(this);
1514 unsigned numInits = Exp->getNumInits();
1515 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001516 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001517 return false;
1518 }
Eli Friedman384da272009-01-25 03:12:18 +00001519 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001520 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001521 case ImplicitValueInitExprClass:
1522 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001523 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001524 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001525 case UnaryOperatorClass: {
1526 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1527 if (Exp->getOpcode() == UnaryOperator::Extension)
1528 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1529 break;
1530 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001531 case BinaryOperatorClass: {
1532 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1533 // but this handles the common case.
1534 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1535 if (Exp->getOpcode() == BinaryOperator::Sub &&
1536 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1537 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1538 return true;
1539 break;
1540 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001541 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001542 case CStyleCastExprClass:
1543 // Handle casts with a destination that's a struct or union; this
1544 // deals with both the gcc no-op struct cast extension and the
1545 // cast-to-union extension.
1546 if (getType()->isRecordType())
1547 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001548
1549 // Integer->integer casts can be handled here, which is important for
1550 // things like (int)(&&x-&&y). Scary but true.
1551 if (getType()->isIntegerType() &&
1552 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1553 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1554
Eli Friedman384da272009-01-25 03:12:18 +00001555 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001556 }
Eli Friedman384da272009-01-25 03:12:18 +00001557 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001558}
1559
Chris Lattner1f4479e2007-06-05 04:15:44 +00001560/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001561/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001562
1563/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1564/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001565///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001566/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1567/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1568/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001569
Eli Friedman98c56a42009-02-26 09:29:13 +00001570// CheckICE - This function does the fundamental ICE checking: the returned
1571// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1572// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001573// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001574// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001575// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001576//
1577// Meanings of Val:
1578// 0: This expression is an ICE if it can be evaluated by Evaluate.
1579// 1: This expression is not an ICE, but if it isn't evaluated, it's
1580// a legal subexpression for an ICE. This return value is used to handle
1581// the comma operator in C99 mode.
1582// 2: This expression is not an ICE, and is not a legal subexpression for one.
1583
1584struct ICEDiag {
1585 unsigned Val;
1586 SourceLocation Loc;
1587
1588 public:
1589 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1590 ICEDiag() : Val(0) {}
1591};
1592
1593ICEDiag NoDiag() { return ICEDiag(); }
1594
Eli Friedman90afd3d2009-02-27 04:07:58 +00001595static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1596 Expr::EvalResult EVResult;
1597 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1598 !EVResult.Val.isInt()) {
1599 return ICEDiag(2, E->getLocStart());
1600 }
1601 return NoDiag();
1602}
1603
Eli Friedman98c56a42009-02-26 09:29:13 +00001604static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001605 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001606 if (!E->getType()->isIntegralType()) {
1607 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001608 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001609
1610 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001611#define STMT(Node, Base) case Expr::Node##Class:
1612#define EXPR(Node, Base)
1613#include "clang/AST/StmtNodes.def"
1614 case Expr::PredefinedExprClass:
1615 case Expr::FloatingLiteralClass:
1616 case Expr::ImaginaryLiteralClass:
1617 case Expr::StringLiteralClass:
1618 case Expr::ArraySubscriptExprClass:
1619 case Expr::MemberExprClass:
1620 case Expr::CompoundAssignOperatorClass:
1621 case Expr::CompoundLiteralExprClass:
1622 case Expr::ExtVectorElementExprClass:
1623 case Expr::InitListExprClass:
1624 case Expr::DesignatedInitExprClass:
1625 case Expr::ImplicitValueInitExprClass:
1626 case Expr::ParenListExprClass:
1627 case Expr::VAArgExprClass:
1628 case Expr::AddrLabelExprClass:
1629 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001630 case Expr::CXXMemberCallExprClass:
1631 case Expr::CXXDynamicCastExprClass:
1632 case Expr::CXXTypeidExprClass:
1633 case Expr::CXXNullPtrLiteralExprClass:
1634 case Expr::CXXThisExprClass:
1635 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001636 case Expr::CXXNewExprClass:
1637 case Expr::CXXDeleteExprClass:
1638 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001639 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001640 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001641 case Expr::CXXConstructExprClass:
1642 case Expr::CXXBindTemporaryExprClass:
Anders Carlssonba6c4372010-01-29 02:39:32 +00001643 case Expr::CXXBindReferenceExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001644 case Expr::CXXExprWithTemporariesClass:
1645 case Expr::CXXTemporaryObjectExprClass:
1646 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001647 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001648 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001649 case Expr::ObjCStringLiteralClass:
1650 case Expr::ObjCEncodeExprClass:
1651 case Expr::ObjCMessageExprClass:
1652 case Expr::ObjCSelectorExprClass:
1653 case Expr::ObjCProtocolExprClass:
1654 case Expr::ObjCIvarRefExprClass:
1655 case Expr::ObjCPropertyRefExprClass:
1656 case Expr::ObjCImplicitSetterGetterRefExprClass:
1657 case Expr::ObjCSuperExprClass:
1658 case Expr::ObjCIsaExprClass:
1659 case Expr::ShuffleVectorExprClass:
1660 case Expr::BlockExprClass:
1661 case Expr::BlockDeclRefExprClass:
1662 case Expr::NoStmtClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001663 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001664
Douglas Gregor73341c42009-09-11 00:18:58 +00001665 case Expr::GNUNullExprClass:
1666 // GCC considers the GNU __null value to be an integral constant expression.
1667 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001668
Eli Friedman98c56a42009-02-26 09:29:13 +00001669 case Expr::ParenExprClass:
1670 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1671 case Expr::IntegerLiteralClass:
1672 case Expr::CharacterLiteralClass:
1673 case Expr::CXXBoolLiteralExprClass:
1674 case Expr::CXXZeroInitValueExprClass:
1675 case Expr::TypesCompatibleExprClass:
1676 case Expr::UnaryTypeTraitExprClass:
1677 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001678 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001679 case Expr::CXXOperatorCallExprClass: {
1680 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001681 if (CE->isBuiltinCall(Ctx))
1682 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001683 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001684 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001685 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001686 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1687 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001688 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001689 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCall6dee4732010-02-24 09:03:18 +00001690 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1691
1692 // Parameter variables are never constants. Without this check,
1693 // getAnyInitializer() can find a default argument, which leads
1694 // to chaos.
1695 if (isa<ParmVarDecl>(D))
1696 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1697
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001698 // C++ 7.1.5.1p2
1699 // A variable of non-volatile const-qualified integral or enumeration
1700 // type initialized by an ICE can be used in ICEs.
John McCall6dee4732010-02-24 09:03:18 +00001701 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001702 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1703 if (Quals.hasVolatile() || !Quals.hasConst())
1704 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1705
Sebastian Redl5ca79842010-02-01 20:16:42 +00001706 // Look for a declaration of this variable that has an initializer.
1707 const VarDecl *ID = 0;
1708 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001709 if (Init) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001710 if (ID->isInitKnownICE()) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001711 // We have already checked whether this subexpression is an
1712 // integral constant expression.
Sebastian Redl5ca79842010-02-01 20:16:42 +00001713 if (ID->isInitICE())
Douglas Gregor0840cc02009-11-01 20:32:48 +00001714 return NoDiag();
1715 else
1716 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1717 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001718
John McCall52cc0892010-02-06 01:07:37 +00001719 // It's an ICE whether or not the definition we found is
1720 // out-of-line. See DR 721 and the discussion in Clang PR
1721 // 6206 for details.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001722
1723 if (Dcl->isCheckingICE()) {
1724 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1725 }
1726
1727 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001728 ICEDiag Result = CheckICE(Init, Ctx);
1729 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001730 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001731 return Result;
1732 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001733 }
1734 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001735 return ICEDiag(2, E->getLocStart());
1736 case Expr::UnaryOperatorClass: {
1737 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001738 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001739 case UnaryOperator::PostInc:
1740 case UnaryOperator::PostDec:
1741 case UnaryOperator::PreInc:
1742 case UnaryOperator::PreDec:
1743 case UnaryOperator::AddrOf:
1744 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001745 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001746
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001747 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001748 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001749 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001750 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001751 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001752 case UnaryOperator::Real:
1753 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001754 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001755 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001756 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1757 // Evaluate matches the proposed gcc behavior for cases like
1758 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1759 // compliance: we should warn earlier for offsetof expressions with
1760 // array subscripts that aren't ICEs, and if the array subscripts
1761 // are ICEs, the value of the offsetof must be an integer constant.
1762 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001763 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001764 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001765 case Expr::SizeOfAlignOfExprClass: {
1766 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1767 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1768 return ICEDiag(2, E->getLocStart());
1769 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001770 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001771 case Expr::BinaryOperatorClass: {
1772 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001773 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001774 case BinaryOperator::PtrMemD:
1775 case BinaryOperator::PtrMemI:
1776 case BinaryOperator::Assign:
1777 case BinaryOperator::MulAssign:
1778 case BinaryOperator::DivAssign:
1779 case BinaryOperator::RemAssign:
1780 case BinaryOperator::AddAssign:
1781 case BinaryOperator::SubAssign:
1782 case BinaryOperator::ShlAssign:
1783 case BinaryOperator::ShrAssign:
1784 case BinaryOperator::AndAssign:
1785 case BinaryOperator::XorAssign:
1786 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001787 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001788
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001789 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001790 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001791 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001792 case BinaryOperator::Add:
1793 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001794 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001795 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001796 case BinaryOperator::LT:
1797 case BinaryOperator::GT:
1798 case BinaryOperator::LE:
1799 case BinaryOperator::GE:
1800 case BinaryOperator::EQ:
1801 case BinaryOperator::NE:
1802 case BinaryOperator::And:
1803 case BinaryOperator::Xor:
1804 case BinaryOperator::Or:
1805 case BinaryOperator::Comma: {
1806 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1807 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001808 if (Exp->getOpcode() == BinaryOperator::Div ||
1809 Exp->getOpcode() == BinaryOperator::Rem) {
1810 // Evaluate gives an error for undefined Div/Rem, so make sure
1811 // we don't evaluate one.
1812 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1813 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1814 if (REval == 0)
1815 return ICEDiag(1, E->getLocStart());
1816 if (REval.isSigned() && REval.isAllOnesValue()) {
1817 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1818 if (LEval.isMinSignedValue())
1819 return ICEDiag(1, E->getLocStart());
1820 }
1821 }
1822 }
1823 if (Exp->getOpcode() == BinaryOperator::Comma) {
1824 if (Ctx.getLangOptions().C99) {
1825 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1826 // if it isn't evaluated.
1827 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1828 return ICEDiag(1, E->getLocStart());
1829 } else {
1830 // In both C89 and C++, commas in ICEs are illegal.
1831 return ICEDiag(2, E->getLocStart());
1832 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001833 }
1834 if (LHSResult.Val >= RHSResult.Val)
1835 return LHSResult;
1836 return RHSResult;
1837 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001838 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001839 case BinaryOperator::LOr: {
1840 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1841 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1842 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1843 // Rare case where the RHS has a comma "side-effect"; we need
1844 // to actually check the condition to see whether the side
1845 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001846 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001847 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001848 return RHSResult;
1849 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001850 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001851
Eli Friedman98c56a42009-02-26 09:29:13 +00001852 if (LHSResult.Val >= RHSResult.Val)
1853 return LHSResult;
1854 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001855 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001856 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001857 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001858 case Expr::ImplicitCastExprClass:
1859 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001860 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001861 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001862 case Expr::CXXStaticCastExprClass:
1863 case Expr::CXXReinterpretCastExprClass:
1864 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001865 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1866 if (SubExpr->getType()->isIntegralType())
1867 return CheckICE(SubExpr, Ctx);
1868 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1869 return NoDiag();
1870 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001871 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001872 case Expr::ConditionalOperatorClass: {
1873 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001874 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001875 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001876 // expression, and it is fully evaluated. This is an important GNU
1877 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001878 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001879 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001880 Expr::EvalResult EVResult;
1881 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1882 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001883 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001884 }
1885 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001886 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001887 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1888 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1889 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1890 if (CondResult.Val == 2)
1891 return CondResult;
1892 if (TrueResult.Val == 2)
1893 return TrueResult;
1894 if (FalseResult.Val == 2)
1895 return FalseResult;
1896 if (CondResult.Val == 1)
1897 return CondResult;
1898 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1899 return NoDiag();
1900 // Rare case where the diagnostics depend on which side is evaluated
1901 // Note that if we get here, CondResult is 0, and at least one of
1902 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001903 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001904 return FalseResult;
1905 }
1906 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001907 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001908 case Expr::CXXDefaultArgExprClass:
1909 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001910 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001911 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001912 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001913 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001914
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001915 // Silence a GCC warning
1916 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001917}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001918
Eli Friedman98c56a42009-02-26 09:29:13 +00001919bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1920 SourceLocation *Loc, bool isEvaluated) const {
1921 ICEDiag d = CheckICE(this, Ctx);
1922 if (d.Val != 0) {
1923 if (Loc) *Loc = d.Loc;
1924 return false;
1925 }
1926 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001927 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001928 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001929 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1930 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001931 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001932 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001933}
1934
Chris Lattner7eef9192007-05-24 01:23:49 +00001935/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1936/// integer constant expression with the value zero, or if this is one that is
1937/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001938bool Expr::isNullPointerConstant(ASTContext &Ctx,
1939 NullPointerConstantValueDependence NPC) const {
1940 if (isValueDependent()) {
1941 switch (NPC) {
1942 case NPC_NeverValueDependent:
1943 assert(false && "Unexpected value dependent expression!");
1944 // If the unthinkable happens, fall through to the safest alternative.
1945
1946 case NPC_ValueDependentIsNull:
1947 return isTypeDependent() || getType()->isIntegralType();
1948
1949 case NPC_ValueDependentIsNotNull:
1950 return false;
1951 }
1952 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001953
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001954 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001955 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001956 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001957 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001958 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001959 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001960 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001961 Pointee->isVoidType() && // to void*
1962 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001963 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001964 }
Steve Naroffada7d422007-05-20 17:54:12 +00001965 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001966 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1967 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001968 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001969 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1970 // Accept ((void*)0) as a null pointer constant, as many other
1971 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001972 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001973 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001974 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001975 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001976 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001977 } else if (isa<GNUNullExpr>(this)) {
1978 // The GNU __null extension is always a null pointer constant.
1979 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001980 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001981
Sebastian Redl576fd422009-05-10 18:38:11 +00001982 // C++0x nullptr_t is always a null pointer constant.
1983 if (getType()->isNullPtrType())
1984 return true;
1985
Steve Naroff4871fe02008-01-14 16:10:57 +00001986 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001987 if (!getType()->isIntegerType() ||
1988 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001989 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001990
Chris Lattner1abbd412007-06-08 17:58:43 +00001991 // If we have an integer constant expression, we need to *evaluate* it and
1992 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001993 llvm::APSInt Result;
1994 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001995}
Steve Narofff7a5da12007-07-28 23:10:27 +00001996
Douglas Gregor71235ec2009-05-02 02:18:30 +00001997FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001998 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001999
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002000 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2001 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2002 E = ICE->getSubExpr()->IgnoreParens();
2003 else
2004 break;
2005 }
2006
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002007 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002008 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002009 if (Field->isBitField())
2010 return Field;
2011
2012 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2013 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2014 return BinOp->getLHS()->getBitField();
2015
2016 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002017}
2018
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002019bool Expr::refersToVectorElement() const {
2020 const Expr *E = this->IgnoreParens();
2021
2022 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2023 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2024 E = ICE->getSubExpr()->IgnoreParens();
2025 else
2026 break;
2027 }
2028
2029 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2030 return ASE->getBase()->getType()->isVectorType();
2031
2032 if (isa<ExtVectorElementExpr>(E))
2033 return true;
2034
2035 return false;
2036}
2037
Chris Lattnerb8211f62009-02-16 22:14:05 +00002038/// isArrow - Return true if the base expression is a pointer to vector,
2039/// return false if the base expression is a vector.
2040bool ExtVectorElementExpr::isArrow() const {
2041 return getBase()->getType()->isPointerType();
2042}
2043
Nate Begemance4d7fc2008-04-18 23:10:10 +00002044unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002045 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002046 return VT->getNumElements();
2047 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002048}
2049
Nate Begemanf322eab2008-05-09 06:41:27 +00002050/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002051bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002052 // FIXME: Refactor this code to an accessor on the AST node which returns the
2053 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002054 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002055
2056 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002057 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002058 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002059
Nate Begeman7e5185b2009-01-18 02:01:21 +00002060 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002061 if (Comp[0] == 's' || Comp[0] == 'S')
2062 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002063
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002064 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2065 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002066 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002067
Steve Naroff0d595ca2007-07-30 03:29:09 +00002068 return false;
2069}
Chris Lattner885b4952007-08-02 23:36:59 +00002070
Nate Begemanf322eab2008-05-09 06:41:27 +00002071/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002072void ExtVectorElementExpr::getEncodedElementAccess(
2073 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002074 llvm::StringRef Comp = Accessor->getName();
2075 if (Comp[0] == 's' || Comp[0] == 'S')
2076 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002077
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002078 bool isHi = Comp == "hi";
2079 bool isLo = Comp == "lo";
2080 bool isEven = Comp == "even";
2081 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002082
Nate Begemanf322eab2008-05-09 06:41:27 +00002083 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2084 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002085
Nate Begemanf322eab2008-05-09 06:41:27 +00002086 if (isHi)
2087 Index = e + i;
2088 else if (isLo)
2089 Index = i;
2090 else if (isEven)
2091 Index = 2 * i;
2092 else if (isOdd)
2093 Index = 2 * i + 1;
2094 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002095 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002096
Nate Begemand3862152008-05-13 21:03:02 +00002097 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002098 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002099}
2100
Steve Narofff73590d2007-09-27 14:38:14 +00002101// constructor for instance messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002102ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2103 Selector selInfo,
2104 QualType retType, ObjCMethodDecl *mproto,
2105 SourceLocation LBrac, SourceLocation RBrac,
2106 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002107 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002108 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002109 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002110 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002111 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002112 if (NumArgs) {
2113 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002114 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2115 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002116 LBracloc = LBrac;
2117 RBracloc = RBrac;
2118}
2119
Mike Stump11289f42009-09-09 15:08:12 +00002120// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002121// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenek2c809302010-02-11 22:41:21 +00002122ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002123 SourceLocation clsNameLoc, Selector selInfo,
2124 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenek2c809302010-02-11 22:41:21 +00002125 SourceLocation LBrac, SourceLocation RBrac,
2126 Expr **ArgExprs, unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002127 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2128 SelName(selInfo), MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002129 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002130 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002131 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002132 if (NumArgs) {
2133 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002134 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2135 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002136 LBracloc = LBrac;
2137 RBracloc = RBrac;
2138}
2139
Mike Stump11289f42009-09-09 15:08:12 +00002140// constructor for class messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002141ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002142 SourceLocation clsNameLoc, Selector selInfo,
2143 QualType retType,
Ted Kremenek2c809302010-02-11 22:41:21 +00002144 ObjCMethodDecl *mproto, SourceLocation LBrac,
2145 SourceLocation RBrac, Expr **ArgExprs,
2146 unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002147 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2148 SelName(selInfo), MethodProto(mproto)
2149{
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002150 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002151 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002152 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2153 if (NumArgs) {
2154 for (unsigned i = 0; i != NumArgs; ++i)
2155 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2156 }
2157 LBracloc = LBrac;
2158 RBracloc = RBrac;
2159}
2160
2161ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2162 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2163 switch (x & Flags) {
2164 default:
2165 assert(false && "Invalid ObjCMessageExpr.");
2166 case IsInstMeth:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002167 return ClassInfo(0, 0, SourceLocation());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002168 case IsClsMethDeclUnknown:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002169 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002170 case IsClsMethDeclKnown: {
2171 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002172 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002173 }
2174 }
2175}
2176
Chris Lattner7ec71da2009-04-26 00:44:05 +00002177void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorde4827d2010-03-08 16:40:19 +00002178 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner7ec71da2009-04-26 00:44:05 +00002179 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002180 return;
2181 }
2182
2183 if (CI.Decl == 0)
2184 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner7ec71da2009-04-26 00:44:05 +00002185 else
Douglas Gregorde4827d2010-03-08 16:40:19 +00002186 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2187 ClassNameLoc = CI.Loc;
Chris Lattner7ec71da2009-04-26 00:44:05 +00002188}
2189
Ted Kremenek2c809302010-02-11 22:41:21 +00002190void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2191 DestroyChildren(C);
2192 if (SubExprs)
2193 C.Deallocate(SubExprs);
2194 this->~ObjCMessageExpr();
2195 C.Deallocate((void*) this);
2196}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002197
Chris Lattner35e564e2007-10-25 00:29:32 +00002198bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002199 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002200}
2201
Nate Begeman48745922009-08-12 02:28:50 +00002202void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2203 unsigned NumExprs) {
2204 if (SubExprs) C.Deallocate(SubExprs);
2205
2206 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002207 this->NumExprs = NumExprs;
2208 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002209}
Nate Begeman48745922009-08-12 02:28:50 +00002210
2211void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2212 DestroyChildren(C);
2213 if (SubExprs) C.Deallocate(SubExprs);
2214 this->~ShuffleVectorExpr();
2215 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002216}
2217
Douglas Gregore26a2852009-08-07 06:08:38 +00002218void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002219 // Override default behavior of traversing children. If this has a type
2220 // operand and the type is a variable-length array, the child iteration
2221 // will iterate over the size expression. However, this expression belongs
2222 // to the type, not to this, so we don't want to delete it.
2223 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002224 if (isArgumentType()) {
2225 this->~SizeOfAlignOfExpr();
2226 C.Deallocate(this);
2227 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002228 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002229 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002230}
2231
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002232//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002233// DesignatedInitExpr
2234//===----------------------------------------------------------------------===//
2235
2236IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2237 assert(Kind == FieldDesignator && "Only valid on a field designator");
2238 if (Field.NameOrField & 0x01)
2239 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2240 else
2241 return getField()->getIdentifier();
2242}
2243
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002244DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2245 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002246 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002247 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002248 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002249 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002250 unsigned NumIndexExprs,
2251 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002252 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002253 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002254 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2255 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002256 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002257
2258 // Record the initializer itself.
2259 child_iterator Child = child_begin();
2260 *Child++ = Init;
2261
2262 // Copy the designators and their subexpressions, computing
2263 // value-dependence along the way.
2264 unsigned IndexIdx = 0;
2265 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002266 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002267
2268 if (this->Designators[I].isArrayDesignator()) {
2269 // Compute type- and value-dependence.
2270 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002271 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002272 Index->isTypeDependent() || Index->isValueDependent();
2273
2274 // Copy the index expressions into permanent storage.
2275 *Child++ = IndexExprs[IndexIdx++];
2276 } else if (this->Designators[I].isArrayRangeDesignator()) {
2277 // Compute type- and value-dependence.
2278 Expr *Start = IndexExprs[IndexIdx];
2279 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002280 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002281 Start->isTypeDependent() || Start->isValueDependent() ||
2282 End->isTypeDependent() || End->isValueDependent();
2283
2284 // Copy the start/end expressions into permanent storage.
2285 *Child++ = IndexExprs[IndexIdx++];
2286 *Child++ = IndexExprs[IndexIdx++];
2287 }
2288 }
2289
2290 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002291}
2292
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002293DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002294DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002295 unsigned NumDesignators,
2296 Expr **IndexExprs, unsigned NumIndexExprs,
2297 SourceLocation ColonOrEqualLoc,
2298 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002299 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002300 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002301 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002302 ColonOrEqualLoc, UsesColonSyntax,
2303 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002304}
2305
Mike Stump11289f42009-09-09 15:08:12 +00002306DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002307 unsigned NumIndexExprs) {
2308 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2309 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2310 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2311}
2312
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002313void DesignatedInitExpr::setDesignators(ASTContext &C,
2314 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002315 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002316 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002317
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002318 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002319 NumDesignators = NumDesigs;
2320 for (unsigned I = 0; I != NumDesigs; ++I)
2321 Designators[I] = Desigs[I];
2322}
2323
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002324SourceRange DesignatedInitExpr::getSourceRange() const {
2325 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002326 Designator &First =
2327 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002328 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002329 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002330 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2331 else
2332 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2333 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002334 StartLoc =
2335 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002336 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2337}
2338
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002339Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2340 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2341 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2342 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002343 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2344 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2345}
2346
2347Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002348 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002349 "Requires array range designator");
2350 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2351 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002352 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2353 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2354}
2355
2356Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002357 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002358 "Requires array range designator");
2359 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2360 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002361 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2362 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2363}
2364
Douglas Gregord5846a12009-04-15 06:41:24 +00002365/// \brief Replaces the designator at index @p Idx with the series
2366/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002367void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002368 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002369 const Designator *Last) {
2370 unsigned NumNewDesignators = Last - First;
2371 if (NumNewDesignators == 0) {
2372 std::copy_backward(Designators + Idx + 1,
2373 Designators + NumDesignators,
2374 Designators + Idx);
2375 --NumNewDesignators;
2376 return;
2377 } else if (NumNewDesignators == 1) {
2378 Designators[Idx] = *First;
2379 return;
2380 }
2381
Mike Stump11289f42009-09-09 15:08:12 +00002382 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002383 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002384 std::copy(Designators, Designators + Idx, NewDesignators);
2385 std::copy(First, Last, NewDesignators + Idx);
2386 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2387 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002388 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002389 Designators = NewDesignators;
2390 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2391}
2392
Douglas Gregore26a2852009-08-07 06:08:38 +00002393void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002394 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002395 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002396}
2397
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002398void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2399 for (unsigned I = 0; I != NumDesignators; ++I)
2400 Designators[I].~Designator();
2401 C.Deallocate(Designators);
2402 Designators = 0;
2403}
2404
Mike Stump11289f42009-09-09 15:08:12 +00002405ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002406 Expr **exprs, unsigned nexprs,
2407 SourceLocation rparenloc)
2408: Expr(ParenListExprClass, QualType(),
2409 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002410 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002411 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002412
Nate Begeman5ec4b312009-08-10 23:49:36 +00002413 Exprs = new (C) Stmt*[nexprs];
2414 for (unsigned i = 0; i != nexprs; ++i)
2415 Exprs[i] = exprs[i];
2416}
2417
2418void ParenListExpr::DoDestroy(ASTContext& C) {
2419 DestroyChildren(C);
2420 if (Exprs) C.Deallocate(Exprs);
2421 this->~ParenListExpr();
2422 C.Deallocate(this);
2423}
2424
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002425//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002426// ExprIterator.
2427//===----------------------------------------------------------------------===//
2428
2429Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2430Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2431Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2432const Expr* ConstExprIterator::operator[](size_t idx) const {
2433 return cast<Expr>(I[idx]);
2434}
2435const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2436const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2437
2438//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002439// Child Iterators for iterating over subexpressions/substatements
2440//===----------------------------------------------------------------------===//
2441
2442// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002443Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2444Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002445
Steve Naroffe46504b2007-11-12 14:29:37 +00002446// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002447Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2448Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002449
Steve Naroffebf4cb42008-06-02 23:03:37 +00002450// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002451Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2452Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002453
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002454// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002455Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2456 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002457}
Mike Stump11289f42009-09-09 15:08:12 +00002458Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2459 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002460}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002461
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002462// ObjCSuperExpr
2463Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2464Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2465
Steve Naroffe87026a2009-07-24 17:54:45 +00002466// ObjCIsaExpr
2467Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2468Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2469
Chris Lattner6307f192008-08-10 01:53:14 +00002470// PredefinedExpr
2471Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2472Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002473
2474// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002475Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2476Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002477
2478// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002479Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002480Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002481
2482// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002483Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2484Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002485
Chris Lattner1c20a172007-08-26 03:42:43 +00002486// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002487Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2488Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002489
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002490// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002491Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2492Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002493
2494// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002495Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2496Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002497
2498// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002499Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2500Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002501
Sebastian Redl6f282892008-11-11 17:56:53 +00002502// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002503Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002504 // If this is of a type and the type is a VLA type (and not a typedef), the
2505 // size expression of the VLA needs to be treated as an executable expression.
2506 // Why isn't this weirdness documented better in StmtIterator?
2507 if (isArgumentType()) {
2508 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2509 getArgumentType().getTypePtr()))
2510 return child_iterator(T);
2511 return child_iterator();
2512 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002513 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002514}
Sebastian Redl6f282892008-11-11 17:56:53 +00002515Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2516 if (isArgumentType())
2517 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002518 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002519}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002520
2521// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002522Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002523 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002524}
Ted Kremenek23702b62007-08-24 20:06:47 +00002525Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002526 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002527}
2528
2529// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002530Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002531 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002532}
Ted Kremenek23702b62007-08-24 20:06:47 +00002533Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002534 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002535}
Ted Kremenek23702b62007-08-24 20:06:47 +00002536
2537// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002538Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2539Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002540
Nate Begemance4d7fc2008-04-18 23:10:10 +00002541// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002542Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2543Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002544
2545// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002546Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2547Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002548
Ted Kremenek23702b62007-08-24 20:06:47 +00002549// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002550Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2551Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002552
2553// BinaryOperator
2554Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002555 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002556}
Ted Kremenek23702b62007-08-24 20:06:47 +00002557Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002558 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002559}
2560
2561// ConditionalOperator
2562Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002563 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002564}
Ted Kremenek23702b62007-08-24 20:06:47 +00002565Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002566 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002567}
2568
2569// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002570Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2571Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002572
Ted Kremenek23702b62007-08-24 20:06:47 +00002573// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002574Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2575Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002576
2577// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002578Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2579 return child_iterator();
2580}
2581
2582Stmt::child_iterator TypesCompatibleExpr::child_end() {
2583 return child_iterator();
2584}
Ted Kremenek23702b62007-08-24 20:06:47 +00002585
2586// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002587Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2588Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002589
Douglas Gregor3be4b122008-11-29 04:51:27 +00002590// GNUNullExpr
2591Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2592Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2593
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002594// ShuffleVectorExpr
2595Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002596 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002597}
2598Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002599 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002600}
2601
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002602// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002603Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2604Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002605
Anders Carlsson4692db02007-08-31 04:56:16 +00002606// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002607Stmt::child_iterator InitListExpr::child_begin() {
2608 return InitExprs.size() ? &InitExprs[0] : 0;
2609}
2610Stmt::child_iterator InitListExpr::child_end() {
2611 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2612}
Anders Carlsson4692db02007-08-31 04:56:16 +00002613
Douglas Gregor0202cb42009-01-29 17:44:32 +00002614// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002615Stmt::child_iterator DesignatedInitExpr::child_begin() {
2616 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2617 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002618 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2619}
2620Stmt::child_iterator DesignatedInitExpr::child_end() {
2621 return child_iterator(&*child_begin() + NumSubExprs);
2622}
2623
Douglas Gregor0202cb42009-01-29 17:44:32 +00002624// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002625Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2626 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002627}
2628
Mike Stump11289f42009-09-09 15:08:12 +00002629Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2630 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002631}
2632
Nate Begeman5ec4b312009-08-10 23:49:36 +00002633// ParenListExpr
2634Stmt::child_iterator ParenListExpr::child_begin() {
2635 return &Exprs[0];
2636}
2637Stmt::child_iterator ParenListExpr::child_end() {
2638 return &Exprs[0]+NumExprs;
2639}
2640
Ted Kremenek23702b62007-08-24 20:06:47 +00002641// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002642Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002643 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002644}
2645Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002646 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002647}
Ted Kremenek23702b62007-08-24 20:06:47 +00002648
2649// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002650Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2651Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002652
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002653// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002654Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002655 return child_iterator();
2656}
2657Stmt::child_iterator ObjCSelectorExpr::child_end() {
2658 return child_iterator();
2659}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002660
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002661// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002662Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2663 return child_iterator();
2664}
2665Stmt::child_iterator ObjCProtocolExpr::child_end() {
2666 return child_iterator();
2667}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002668
Steve Naroffd54978b2007-09-18 23:55:05 +00002669// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002670Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002671 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002672}
2673Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002674 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002675}
2676
Steve Naroffc540d662008-09-03 18:15:37 +00002677// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002678Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2679Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002680
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002681Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2682Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }