blob: b4e5a5d96028fef72c3a571060f194f7e93d5496 [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 << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000230
231 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
232 // a null check to avoid a crash.
233 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
234 Out << ID->getNameAsString();
235
Anders Carlsson2fb08242009-09-08 18:24:21 +0000236 if (const ObjCCategoryImplDecl *CID =
237 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
238 Out << '(';
239 Out << CID->getNameAsString();
240 Out << ')';
241 }
242 Out << ' ';
243 Out << MD->getSelector().getAsString();
244 Out << ']';
245
246 Out.flush();
247 return Name.str().str();
248 }
249 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
250 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
251 return "top level";
252 }
253 return "";
254}
255
Chris Lattnera0173132008-06-07 22:13:43 +0000256/// getValueAsApproximateDouble - This returns the value as an inaccurate
257/// double. Note that this may cause loss of precision, but is useful for
258/// debugging dumps, etc.
259double FloatingLiteral::getValueAsApproximateDouble() const {
260 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000261 bool ignored;
262 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
263 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000264 return V.convertToDouble();
265}
266
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000267StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
268 unsigned ByteLength, bool Wide,
269 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000270 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000271 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000272 // Allocate enough space for the StringLiteral plus an array of locations for
273 // any concatenated string tokens.
274 void *Mem = C.Allocate(sizeof(StringLiteral)+
275 sizeof(SourceLocation)*(NumStrs-1),
276 llvm::alignof<StringLiteral>());
277 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000278
Steve Naroffdf7855b2007-02-21 23:46:25 +0000279 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000280 char *AStrData = new (C, 1) char[ByteLength];
281 memcpy(AStrData, StrData, ByteLength);
282 SL->StrData = AStrData;
283 SL->ByteLength = ByteLength;
284 SL->IsWide = Wide;
285 SL->TokLocs[0] = Loc[0];
286 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000287
Chris Lattner630970d2009-02-18 05:49:11 +0000288 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000289 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
290 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000291}
292
Douglas Gregor958dfc92009-04-15 16:35:07 +0000293StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
294 void *Mem = C.Allocate(sizeof(StringLiteral)+
295 sizeof(SourceLocation)*(NumStrs-1),
296 llvm::alignof<StringLiteral>());
297 StringLiteral *SL = new (Mem) StringLiteral(QualType());
298 SL->StrData = 0;
299 SL->ByteLength = 0;
300 SL->NumConcatenated = NumStrs;
301 return SL;
302}
303
Douglas Gregore26a2852009-08-07 06:08:38 +0000304void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000305 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000306 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000307}
308
Daniel Dunbar36217882009-09-22 03:27:33 +0000309void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000310 if (StrData)
311 C.Deallocate(const_cast<char*>(StrData));
312
Daniel Dunbar36217882009-09-22 03:27:33 +0000313 char *AStrData = new (C, 1) char[Str.size()];
314 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000315 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000316 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000317}
318
Chris Lattner1b926492006-08-23 06:42:10 +0000319/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
320/// corresponds to, e.g. "sizeof" or "[pre]++".
321const char *UnaryOperator::getOpcodeStr(Opcode Op) {
322 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000323 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000324 case PostInc: return "++";
325 case PostDec: return "--";
326 case PreInc: return "++";
327 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000328 case AddrOf: return "&";
329 case Deref: return "*";
330 case Plus: return "+";
331 case Minus: return "-";
332 case Not: return "~";
333 case LNot: return "!";
334 case Real: return "__real";
335 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000336 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000337 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000338 }
339}
340
Mike Stump11289f42009-09-09 15:08:12 +0000341UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000342UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
343 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000344 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000345 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
346 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
347 case OO_Amp: return AddrOf;
348 case OO_Star: return Deref;
349 case OO_Plus: return Plus;
350 case OO_Minus: return Minus;
351 case OO_Tilde: return Not;
352 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000353 }
354}
355
356OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
357 switch (Opc) {
358 case PostInc: case PreInc: return OO_PlusPlus;
359 case PostDec: case PreDec: return OO_MinusMinus;
360 case AddrOf: return OO_Amp;
361 case Deref: return OO_Star;
362 case Plus: return OO_Plus;
363 case Minus: return OO_Minus;
364 case Not: return OO_Tilde;
365 case LNot: return OO_Exclaim;
366 default: return OO_None;
367 }
368}
369
370
Chris Lattner0eedafe2006-08-24 04:56:27 +0000371//===----------------------------------------------------------------------===//
372// Postfix Operators.
373//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000374
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000375CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000376 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000377 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000378 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000379 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000380 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000382 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000383 SubExprs[FN] = fn;
384 for (unsigned i = 0; i != numargs; ++i)
385 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000386
Douglas Gregor993603d2008-11-14 16:09:21 +0000387 RParenLoc = rparenloc;
388}
Nate Begeman1e36a852008-01-17 17:46:27 +0000389
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000390CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
391 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000392 : Expr(CallExprClass, t,
393 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000394 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000395 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000396
397 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000398 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000399 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000400 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000401
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000402 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000403}
404
Mike Stump11289f42009-09-09 15:08:12 +0000405CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
406 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000407 SubExprs = new (C) Stmt*[1];
408}
409
Douglas Gregore26a2852009-08-07 06:08:38 +0000410void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000411 DestroyChildren(C);
412 if (SubExprs) C.Deallocate(SubExprs);
413 this->~CallExpr();
414 C.Deallocate(this);
415}
416
Nuno Lopes518e3702009-12-20 23:11:08 +0000417Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000418 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000419 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000420 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000421 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
422 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000423
424 return 0;
425}
426
Nuno Lopes518e3702009-12-20 23:11:08 +0000427FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000428 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000429}
430
Chris Lattnere4407ed2007-12-28 05:25:02 +0000431/// setNumArgs - This changes the number of arguments present in this call.
432/// Any orphaned expressions are deleted by this, and any new operands are set
433/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000434void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000435 // No change, just return.
436 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattnere4407ed2007-12-28 05:25:02 +0000438 // If shrinking # arguments, just delete the extras and forgot them.
439 if (NumArgs < getNumArgs()) {
440 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000441 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000442 this->NumArgs = NumArgs;
443 return;
444 }
445
446 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000447 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000448 // Copy over args.
449 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
450 NewSubExprs[i] = SubExprs[i];
451 // Null out new args.
452 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
453 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregorba6e5572009-04-17 21:46:47 +0000455 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000456 SubExprs = NewSubExprs;
457 this->NumArgs = NumArgs;
458}
459
Chris Lattner01ff98a2008-10-06 05:00:53 +0000460/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
461/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000462unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000463 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000464 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000465 // ImplicitCastExpr.
466 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
467 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000468 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Steve Narofff6e3b3292008-01-31 01:07:12 +0000470 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
471 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000472 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000473
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000474 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
475 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000476 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000477
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000478 if (!FDecl->getIdentifier())
479 return 0;
480
Douglas Gregor15fc9562009-09-12 00:22:50 +0000481 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000482}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000483
Anders Carlsson00a27592009-05-26 04:57:27 +0000484QualType CallExpr::getCallReturnType() const {
485 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000486 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000487 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000488 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000489 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000490
John McCall9dd450b2009-09-21 23:43:11 +0000491 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000492 return FnType->getResultType();
493}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000494
Mike Stump11289f42009-09-09 15:08:12 +0000495MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000496 SourceRange qualrange, ValueDecl *memberdecl,
John McCall6b51f282009-11-23 01:53:49 +0000497 SourceLocation l, const TemplateArgumentListInfo *targs,
498 QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000499 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000500 base->isTypeDependent() || (qual && qual->isDependent()),
501 base->isValueDependent() || (qual && qual->isDependent())),
502 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCall6b51f282009-11-23 01:53:49 +0000503 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000504 // Initialize the qualifier, if any.
505 if (HasQualifier) {
506 NameQualifier *NQ = getMemberQualifier();
507 NQ->NNS = qual;
508 NQ->Range = qualrange;
509 }
Mike Stump11289f42009-09-09 15:08:12 +0000510
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000511 // Initialize the explicit template argument list, if any.
John McCall6b51f282009-11-23 01:53:49 +0000512 if (targs)
513 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000514}
515
Mike Stump11289f42009-09-09 15:08:12 +0000516MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
517 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000518 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000519 ValueDecl *memberdecl,
Mike Stump11289f42009-09-09 15:08:12 +0000520 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000521 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000522 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000523 std::size_t Size = sizeof(MemberExpr);
524 if (qual != 0)
525 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000526
John McCall6b51f282009-11-23 01:53:49 +0000527 if (targs)
528 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000529
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000530 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000531 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCall6b51f282009-11-23 01:53:49 +0000532 targs, ty);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000533}
534
Anders Carlsson496335e2009-09-03 00:59:21 +0000535const char *CastExpr::getCastKindName() const {
536 switch (getCastKind()) {
537 case CastExpr::CK_Unknown:
538 return "Unknown";
539 case CastExpr::CK_BitCast:
540 return "BitCast";
541 case CastExpr::CK_NoOp:
542 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000543 case CastExpr::CK_BaseToDerived:
544 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000545 case CastExpr::CK_DerivedToBase:
546 return "DerivedToBase";
547 case CastExpr::CK_Dynamic:
548 return "Dynamic";
549 case CastExpr::CK_ToUnion:
550 return "ToUnion";
551 case CastExpr::CK_ArrayToPointerDecay:
552 return "ArrayToPointerDecay";
553 case CastExpr::CK_FunctionToPointerDecay:
554 return "FunctionToPointerDecay";
555 case CastExpr::CK_NullToMemberPointer:
556 return "NullToMemberPointer";
557 case CastExpr::CK_BaseToDerivedMemberPointer:
558 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000559 case CastExpr::CK_DerivedToBaseMemberPointer:
560 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000561 case CastExpr::CK_UserDefinedConversion:
562 return "UserDefinedConversion";
563 case CastExpr::CK_ConstructorConversion:
564 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000565 case CastExpr::CK_IntegralToPointer:
566 return "IntegralToPointer";
567 case CastExpr::CK_PointerToIntegral:
568 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000569 case CastExpr::CK_ToVoid:
570 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000571 case CastExpr::CK_VectorSplat:
572 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000573 case CastExpr::CK_IntegralCast:
574 return "IntegralCast";
575 case CastExpr::CK_IntegralToFloating:
576 return "IntegralToFloating";
577 case CastExpr::CK_FloatingToIntegral:
578 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000579 case CastExpr::CK_FloatingCast:
580 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000581 case CastExpr::CK_MemberPointerToBoolean:
582 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000583 case CastExpr::CK_AnyPointerToObjCPointerCast:
584 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000585 case CastExpr::CK_AnyPointerToBlockPointerCast:
586 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Anders Carlsson496335e2009-09-03 00:59:21 +0000589 assert(0 && "Unhandled cast kind!");
590 return 0;
591}
592
Douglas Gregord196a582009-12-14 19:27:10 +0000593Expr *CastExpr::getSubExprAsWritten() {
594 Expr *SubExpr = 0;
595 CastExpr *E = this;
596 do {
597 SubExpr = E->getSubExpr();
598
599 // Skip any temporary bindings; they're implicit.
600 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
601 SubExpr = Binder->getSubExpr();
602
603 // Conversions by constructor and conversion functions have a
604 // subexpression describing the call; strip it off.
605 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
606 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
607 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
608 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
609
610 // If the subexpression we're left with is an implicit cast, look
611 // through that, too.
612 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
613
614 return SubExpr;
615}
616
Chris Lattner1b926492006-08-23 06:42:10 +0000617/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
618/// corresponds to, e.g. "<<=".
619const char *BinaryOperator::getOpcodeStr(Opcode Op) {
620 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000621 case PtrMemD: return ".*";
622 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000623 case Mul: return "*";
624 case Div: return "/";
625 case Rem: return "%";
626 case Add: return "+";
627 case Sub: return "-";
628 case Shl: return "<<";
629 case Shr: return ">>";
630 case LT: return "<";
631 case GT: return ">";
632 case LE: return "<=";
633 case GE: return ">=";
634 case EQ: return "==";
635 case NE: return "!=";
636 case And: return "&";
637 case Xor: return "^";
638 case Or: return "|";
639 case LAnd: return "&&";
640 case LOr: return "||";
641 case Assign: return "=";
642 case MulAssign: return "*=";
643 case DivAssign: return "/=";
644 case RemAssign: return "%=";
645 case AddAssign: return "+=";
646 case SubAssign: return "-=";
647 case ShlAssign: return "<<=";
648 case ShrAssign: return ">>=";
649 case AndAssign: return "&=";
650 case XorAssign: return "^=";
651 case OrAssign: return "|=";
652 case Comma: return ",";
653 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000654
655 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000656}
Steve Naroff47500512007-04-19 23:00:49 +0000657
Mike Stump11289f42009-09-09 15:08:12 +0000658BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000659BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
660 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000661 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000662 case OO_Plus: return Add;
663 case OO_Minus: return Sub;
664 case OO_Star: return Mul;
665 case OO_Slash: return Div;
666 case OO_Percent: return Rem;
667 case OO_Caret: return Xor;
668 case OO_Amp: return And;
669 case OO_Pipe: return Or;
670 case OO_Equal: return Assign;
671 case OO_Less: return LT;
672 case OO_Greater: return GT;
673 case OO_PlusEqual: return AddAssign;
674 case OO_MinusEqual: return SubAssign;
675 case OO_StarEqual: return MulAssign;
676 case OO_SlashEqual: return DivAssign;
677 case OO_PercentEqual: return RemAssign;
678 case OO_CaretEqual: return XorAssign;
679 case OO_AmpEqual: return AndAssign;
680 case OO_PipeEqual: return OrAssign;
681 case OO_LessLess: return Shl;
682 case OO_GreaterGreater: return Shr;
683 case OO_LessLessEqual: return ShlAssign;
684 case OO_GreaterGreaterEqual: return ShrAssign;
685 case OO_EqualEqual: return EQ;
686 case OO_ExclaimEqual: return NE;
687 case OO_LessEqual: return LE;
688 case OO_GreaterEqual: return GE;
689 case OO_AmpAmp: return LAnd;
690 case OO_PipePipe: return LOr;
691 case OO_Comma: return Comma;
692 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000693 }
694}
695
696OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
697 static const OverloadedOperatorKind OverOps[] = {
698 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
699 OO_Star, OO_Slash, OO_Percent,
700 OO_Plus, OO_Minus,
701 OO_LessLess, OO_GreaterGreater,
702 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
703 OO_EqualEqual, OO_ExclaimEqual,
704 OO_Amp,
705 OO_Caret,
706 OO_Pipe,
707 OO_AmpAmp,
708 OO_PipePipe,
709 OO_Equal, OO_StarEqual,
710 OO_SlashEqual, OO_PercentEqual,
711 OO_PlusEqual, OO_MinusEqual,
712 OO_LessLessEqual, OO_GreaterGreaterEqual,
713 OO_AmpEqual, OO_CaretEqual,
714 OO_PipeEqual,
715 OO_Comma
716 };
717 return OverOps[Opc];
718}
719
Ted Kremenek013041e2010-02-19 01:50:18 +0000720InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000721 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000722 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000723 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump11289f42009-09-09 15:08:12 +0000724 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenek013041e2010-02-19 01:50:18 +0000725 UnionFieldInit(0), HadArrayRangeDesignator(false)
726{
727 for (unsigned I = 0; I != numInits; ++I) {
728 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000729 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000730 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000731 ValueDependent = true;
732 }
Ted Kremenek013041e2010-02-19 01:50:18 +0000733
734 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000735}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000736
Ted Kremenek013041e2010-02-19 01:50:18 +0000737void InitListExpr::reserveInits(unsigned NumInits) {
738 if (NumInits > InitExprs.size())
739 InitExprs.reserve(NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000740}
741
Ted Kremenek013041e2010-02-19 01:50:18 +0000742void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
743 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
744 Idx < LastIdx; ++Idx)
745 InitExprs[Idx]->Destroy(Context);
746 InitExprs.resize(NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000747}
748
Ted Kremenek013041e2010-02-19 01:50:18 +0000749Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
750 if (Init >= InitExprs.size()) {
751 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
752 InitExprs.back() = expr;
753 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000756 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
757 InitExprs[Init] = expr;
758 return Result;
759}
760
Steve Naroff991e99d2008-09-04 15:31:07 +0000761/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000762///
763const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000764 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000765 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000766}
767
Mike Stump11289f42009-09-09 15:08:12 +0000768SourceLocation BlockExpr::getCaretLocation() const {
769 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000770}
Mike Stump11289f42009-09-09 15:08:12 +0000771const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000772 return TheBlock->getBody();
773}
Mike Stump11289f42009-09-09 15:08:12 +0000774Stmt *BlockExpr::getBody() {
775 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000776}
Steve Naroff415d3d52008-10-08 17:01:13 +0000777
778
Chris Lattner1ec5f562007-06-27 05:38:08 +0000779//===----------------------------------------------------------------------===//
780// Generic Expression Routines
781//===----------------------------------------------------------------------===//
782
Chris Lattner237f2752009-02-14 07:37:35 +0000783/// isUnusedResultAWarning - Return true if this immediate expression should
784/// be warned about if the result is unused. If so, fill in Loc and Ranges
785/// with location to warn on and the source range[s] to report with the
786/// warning.
787bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000788 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000789 // Don't warn if the expr is type dependent. The type could end up
790 // instantiating to void.
791 if (isTypeDependent())
792 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner1ec5f562007-06-27 05:38:08 +0000794 switch (getStmtClass()) {
795 default:
John McCallc493a732010-03-12 07:11:26 +0000796 if (getType()->isVoidType())
797 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000798 Loc = getExprLoc();
799 R1 = getSourceRange();
800 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000801 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000802 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000803 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000804 case UnaryOperatorClass: {
805 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000806
Chris Lattner1ec5f562007-06-27 05:38:08 +0000807 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000808 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000809 case UnaryOperator::PostInc:
810 case UnaryOperator::PostDec:
811 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000812 case UnaryOperator::PreDec: // ++/--
813 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000814 case UnaryOperator::Deref:
815 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000816 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000817 return false;
818 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000819 case UnaryOperator::Real:
820 case UnaryOperator::Imag:
821 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000822 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
823 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000824 return false;
825 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000826 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000827 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000828 }
Chris Lattner237f2752009-02-14 07:37:35 +0000829 Loc = UO->getOperatorLoc();
830 R1 = UO->getSubExpr()->getSourceRange();
831 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000832 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000833 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000834 const BinaryOperator *BO = cast<BinaryOperator>(this);
835 // Consider comma to have side effects if the LHS or RHS does.
John McCall1e3715a2010-02-16 04:10:53 +0000836 if (BO->getOpcode() == BinaryOperator::Comma) {
837 // ((foo = <blah>), 0) is an idiom for hiding the result (and
838 // lvalue-ness) of an assignment written in a macro.
839 if (IntegerLiteral *IE =
840 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
841 if (IE->getValue() == 0)
842 return false;
843
John McCallc493a732010-03-12 07:11:26 +0000844 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
845 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000846 }
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattner237f2752009-02-14 07:37:35 +0000848 if (BO->isAssignmentOp())
849 return false;
850 Loc = BO->getOperatorLoc();
851 R1 = BO->getLHS()->getSourceRange();
852 R2 = BO->getRHS()->getSourceRange();
853 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000854 }
Chris Lattner86928112007-08-25 02:00:02 +0000855 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000856 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000857
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000858 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000859 // The condition must be evaluated, but if either the LHS or RHS is a
860 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000861 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000862 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000863 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000864 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000865 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000866 }
867
Chris Lattnera44d1162007-06-27 05:58:59 +0000868 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000869 // If the base pointer or element is to a volatile pointer/field, accessing
870 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000871 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000872 return false;
873 Loc = cast<MemberExpr>(this)->getMemberLoc();
874 R1 = SourceRange(Loc, Loc);
875 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
876 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner1ec5f562007-06-27 05:38:08 +0000878 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000879 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000880 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000881 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000882 return false;
883 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
884 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
885 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
886 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000887
Chris Lattner1ec5f562007-06-27 05:38:08 +0000888 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000889 case CXXOperatorCallExprClass:
890 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000891 // If this is a direct call, get the callee.
892 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +0000893 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000894 // If the callee has attribute pure, const, or warn_unused_result, warn
895 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000896 //
897 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
898 // updated to match for QoI.
899 if (FD->getAttr<WarnUnusedResultAttr>() ||
900 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
901 Loc = CE->getCallee()->getLocStart();
902 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000903
Chris Lattner1a6babf2009-10-13 04:53:48 +0000904 if (unsigned NumArgs = CE->getNumArgs())
905 R2 = SourceRange(CE->getArg(0)->getLocStart(),
906 CE->getArg(NumArgs-1)->getLocEnd());
907 return true;
908 }
Chris Lattner237f2752009-02-14 07:37:35 +0000909 }
910 return false;
911 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000912
913 case CXXTemporaryObjectExprClass:
914 case CXXConstructExprClass:
915 return false;
916
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000917 case ObjCMessageExprClass: {
918 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
919 const ObjCMethodDecl *MD = ME->getMethodDecl();
920 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
921 Loc = getExprLoc();
922 return true;
923 }
Chris Lattner237f2752009-02-14 07:37:35 +0000924 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000925 }
Mike Stump11289f42009-09-09 15:08:12 +0000926
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000927 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000928#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000929 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000930 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000931 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000932 Loc = Ref->getLocation();
933 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
934 if (Ref->getBase())
935 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000936#else
937 Loc = getExprLoc();
938 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000939#endif
940 return true;
941 }
Chris Lattner944d3062008-07-26 19:51:01 +0000942 case StmtExprClass: {
943 // Statement exprs don't logically have side effects themselves, but are
944 // sometimes used in macros in ways that give them a type that is unused.
945 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
946 // however, if the result of the stmt expr is dead, we don't want to emit a
947 // warning.
948 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
949 if (!CS->body_empty())
950 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000951 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000952
John McCallc493a732010-03-12 07:11:26 +0000953 if (getType()->isVoidType())
954 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000955 Loc = cast<StmtExpr>(this)->getLParenLoc();
956 R1 = getSourceRange();
957 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000958 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000959 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000960 // If this is an explicit cast to void, allow it. People do this when they
961 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000962 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000963 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000964 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
965 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
966 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000967 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +0000968 if (getType()->isVoidType())
969 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000970 const CastExpr *CE = cast<CastExpr>(this);
971
972 // If this is a cast to void or a constructor conversion, check the operand.
973 // Otherwise, the result of the cast is unused.
974 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
975 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000976 return (cast<CastExpr>(this)->getSubExpr()
977 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000978 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
979 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
980 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000981 }
Mike Stump11289f42009-09-09 15:08:12 +0000982
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000983 case ImplicitCastExprClass:
984 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000985 return (cast<ImplicitCastExpr>(this)
986 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000987
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000988 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000989 return (cast<CXXDefaultArgExpr>(this)
990 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000991
992 case CXXNewExprClass:
993 // FIXME: In theory, there might be new expressions that don't have side
994 // effects (e.g. a placement new with an uninitialized POD).
995 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000996 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000997 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000998 return (cast<CXXBindTemporaryExpr>(this)
999 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001000 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001001 return (cast<CXXExprWithTemporaries>(this)
1002 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001003 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001004}
1005
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001006/// DeclCanBeLvalue - Determine whether the given declaration can be
1007/// an lvalue. This is a helper routine for isLvalue.
1008static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001009 // C++ [temp.param]p6:
1010 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001011 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001012 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1013 return NTTParm->getType()->isReferenceType();
1014
Douglas Gregor91f84212008-12-11 16:49:14 +00001015 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001016 // C++ 3.10p2: An lvalue refers to an object or function.
1017 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001018 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001019}
1020
Steve Naroff475cca02007-05-14 17:19:29 +00001021/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1022/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001023/// - name, where name must be a variable
1024/// - e[i]
1025/// - (e), where e must be an lvalue
1026/// - e.name, where e must be an lvalue
1027/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001028/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001029/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001030/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001031/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001032///
Chris Lattner67315442008-07-26 21:30:36 +00001033Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001034 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1035
1036 isLvalueResult Res = isLvalueInternal(Ctx);
1037 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1038 return Res;
1039
Douglas Gregor9a657932008-10-21 23:43:52 +00001040 // first, check the type (C99 6.3.2.1). Expressions with function
1041 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001042 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001043 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001044
Steve Naroff1018ea32008-02-10 01:39:04 +00001045 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001046 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001047 return LV_IncompleteVoidType;
1048
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001049 return LV_Valid;
1050}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001051
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001052// Check whether the expression can be sanely treated like an l-value
1053Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001054 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001055 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001056 case StringLiteralClass: // C99 6.5.1p4
1057 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001058 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001059 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001060 // For vectors, make sure base is an lvalue (i.e. not a function call).
1061 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001062 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001063 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001064 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001065 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1066 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001067 return LV_Valid;
1068 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001069 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001070 case BlockDeclRefExprClass: {
1071 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001072 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001073 return LV_Valid;
1074 break;
1075 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001076 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001077 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001078 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1079 NamedDecl *Member = m->getMemberDecl();
1080 // C++ [expr.ref]p4:
1081 // If E2 is declared to have type "reference to T", then E1.E2
1082 // is an lvalue.
1083 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1084 if (Value->getType()->isReferenceType())
1085 return LV_Valid;
1086
1087 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001088 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001089 return LV_Valid;
1090
1091 // -- If E2 is a non-static data member [...]. If E1 is an
1092 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001093 if (isa<FieldDecl>(Member)) {
1094 if (m->isArrow())
1095 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001096 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001097 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001098
1099 // -- If it refers to a static member function [...], then
1100 // E1.E2 is an lvalue.
1101 // -- Otherwise, if E1.E2 refers to a non-static member
1102 // function [...], then E1.E2 is not an lvalue.
1103 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1104 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1105
1106 // -- If E2 is a member enumerator [...], the expression E1.E2
1107 // is not an lvalue.
1108 if (isa<EnumConstantDecl>(Member))
1109 return LV_InvalidExpression;
1110
1111 // Not an lvalue.
1112 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001113 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001114
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001115 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001116 if (m->isArrow())
1117 return LV_Valid;
1118 Expr *BaseExp = m->getBase();
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001119 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1120 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001121 return LV_SubObjCPropertySetting;
1122 return
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001123 BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001124 }
Chris Lattner595db862007-10-30 22:53:42 +00001125 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001126 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001127 return LV_Valid; // C99 6.5.3p4
1128
1129 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001130 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1131 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001132 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001133
1134 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1135 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1136 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1137 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001138 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001139 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001140 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1141 return LV_Valid;
1142
1143 // If this is a conversion to a class temporary, make a note of
1144 // that.
1145 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1146 return LV_ClassTemporary;
1147
1148 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001149 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001150 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001151 case BinaryOperatorClass:
1152 case CompoundAssignOperatorClass: {
1153 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001154
1155 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1156 BinOp->getOpcode() == BinaryOperator::Comma)
1157 return BinOp->getRHS()->isLvalue(Ctx);
1158
Sebastian Redl112a97662009-02-07 00:15:38 +00001159 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001160 // The result of a .* expression is an lvalue only if its first operand is
1161 // an lvalue and its second operand is a pointer to data member.
1162 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001163 !BinOp->getType()->isFunctionType())
1164 return BinOp->getLHS()->isLvalue(Ctx);
1165
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001166 // The result of an ->* expression is an lvalue only if its second operand
1167 // is a pointer to data member.
1168 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1169 !BinOp->getType()->isFunctionType()) {
1170 QualType Ty = BinOp->getRHS()->getType();
1171 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1172 return LV_Valid;
1173 }
1174
Douglas Gregor58e008d2008-11-13 20:12:29 +00001175 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001176 return LV_InvalidExpression;
1177
Douglas Gregor58e008d2008-11-13 20:12:29 +00001178 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001179 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001180 // The result of an assignment operation [...] is an lvalue.
1181 return LV_Valid;
1182
1183
1184 // C99 6.5.16:
1185 // An assignment expression [...] is not an lvalue.
1186 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001187 }
Mike Stump11289f42009-09-09 15:08:12 +00001188 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001189 case CXXOperatorCallExprClass:
1190 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001191 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001192 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001193 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001194 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1195 if (ReturnType->isLValueReferenceType())
1196 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001197
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001198 // If the function is returning a class temporary, make a note of
1199 // that.
1200 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1201 return LV_ClassTemporary;
1202
Douglas Gregor6b754842008-10-28 00:22:11 +00001203 break;
1204 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001205 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001206 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001207 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001208 case ChooseExprClass:
1209 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001210 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001211 case ExtVectorElementExprClass:
1212 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001213 return LV_DuplicateVectorComponents;
1214 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001215 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1216 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001217 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1218 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001219 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001220 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001221 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001222 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001223 case UnresolvedLookupExprClass:
1224 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001225 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001226 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001227 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001228 case CXXFunctionalCastExprClass:
1229 case CXXStaticCastExprClass:
1230 case CXXDynamicCastExprClass:
1231 case CXXReinterpretCastExprClass:
1232 case CXXConstCastExprClass:
1233 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001234 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001235 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1236 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001237 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1238 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001239 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001240
1241 // If this is a conversion to a class temporary, make a note of
1242 // that.
1243 if (Ctx.getLangOptions().CPlusPlus &&
1244 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1245 return LV_ClassTemporary;
1246
Douglas Gregor6b754842008-10-28 00:22:11 +00001247 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001248 case CXXTypeidExprClass:
1249 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1250 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001251 case CXXBindTemporaryExprClass:
1252 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1253 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001254 case CXXBindReferenceExprClass:
1255 // Something that's bound to a reference is always an lvalue.
1256 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001257 case ConditionalOperatorClass: {
1258 // Complicated handling is only for C++.
1259 if (!Ctx.getLangOptions().CPlusPlus)
1260 return LV_InvalidExpression;
1261
1262 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1263 // everywhere there's an object converted to an rvalue. Also, any other
1264 // casts should be wrapped by ImplicitCastExprs. There's just the special
1265 // case involving throws to work out.
1266 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001267 Expr *True = Cond->getTrueExpr();
1268 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001269 // C++0x 5.16p2
1270 // If either the second or the third operand has type (cv) void, [...]
1271 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001272 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001273 return LV_InvalidExpression;
1274
1275 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001276 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001277 return LV_InvalidExpression;
1278
1279 // That's it.
1280 return LV_Valid;
1281 }
1282
Douglas Gregor5103eff2009-12-19 07:07:47 +00001283 case Expr::CXXExprWithTemporariesClass:
1284 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1285
1286 case Expr::ObjCMessageExprClass:
1287 if (const ObjCMethodDecl *Method
1288 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1289 if (Method->getResultType()->isLValueReferenceType())
1290 return LV_Valid;
1291 break;
1292
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001293 case Expr::CXXConstructExprClass:
1294 case Expr::CXXTemporaryObjectExprClass:
1295 case Expr::CXXZeroInitValueExprClass:
1296 return LV_ClassTemporary;
1297
Steve Naroff9358c712007-05-27 23:58:33 +00001298 default:
1299 break;
Steve Naroff47500512007-04-19 23:00:49 +00001300 }
Steve Naroff9358c712007-05-27 23:58:33 +00001301 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001302}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001303
Steve Naroff475cca02007-05-14 17:19:29 +00001304/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1305/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001306/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001307/// recursively, any member or element of all contained aggregates or unions)
1308/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001309Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001310Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001311 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001312
Steve Naroff9358c712007-05-27 23:58:33 +00001313 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001314 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001315 // C++ 3.10p11: Functions cannot be modified, but pointers to
1316 // functions can be modifiable.
1317 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1318 return MLV_NotObjectType;
1319 break;
1320
Chris Lattner1ec5f562007-06-27 05:38:08 +00001321 case LV_NotObjectType: return MLV_NotObjectType;
1322 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001323 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001324 case LV_InvalidExpression:
1325 // If the top level is a C-style cast, and the subexpression is a valid
1326 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1327 // GCC extension. We don't support it, but we want to produce good
1328 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001329 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1330 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1331 if (Loc)
1332 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001333 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001334 }
1335 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001336 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001337 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001338 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001339 case LV_ClassTemporary:
1340 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001341 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001342
1343 // The following is illegal:
1344 // void takeclosure(void (^C)(void));
1345 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1346 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001347 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001348 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1349 return MLV_NotBlockQualified;
1350 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001351
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001352 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001353 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001354 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1355 if (Expr->getSetterMethod() == 0)
1356 return MLV_NoSetterProperty;
1357 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001358
Chris Lattner7adf0762008-08-04 07:31:14 +00001359 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001360
Chris Lattner7adf0762008-08-04 07:31:14 +00001361 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001362 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001363 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001364 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001365 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001366 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001367
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001368 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001369 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001370 return MLV_ConstQualified;
1371 }
Mike Stump11289f42009-09-09 15:08:12 +00001372
Mike Stump11289f42009-09-09 15:08:12 +00001373 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001374}
1375
Fariborz Jahanian07735332009-02-22 18:40:18 +00001376/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001377/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001378bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001379 switch (getStmtClass()) {
1380 default:
1381 return false;
1382 case ObjCIvarRefExprClass:
1383 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001384 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001385 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001386 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001387 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001388 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001389 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001390 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001391 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001392 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001393 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001394 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1395 if (VD->hasGlobalStorage())
1396 return true;
1397 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001398 // dereferencing to a pointer is always a gc'able candidate,
1399 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001400 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001401 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001402 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001403 return false;
1404 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001405 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001406 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001407 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001408 }
1409 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001410 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001411 }
1412}
Ted Kremenekfff70962008-01-17 16:57:34 +00001413Expr* Expr::IgnoreParens() {
1414 Expr* E = this;
1415 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1416 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001417
Ted Kremenekfff70962008-01-17 16:57:34 +00001418 return E;
1419}
1420
Chris Lattnerf2660962008-02-13 01:02:39 +00001421/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1422/// or CastExprs or ImplicitCastExprs, returning their operand.
1423Expr *Expr::IgnoreParenCasts() {
1424 Expr *E = this;
1425 while (true) {
1426 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1427 E = P->getSubExpr();
1428 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1429 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001430 else
1431 return E;
1432 }
1433}
1434
Chris Lattneref26c772009-03-13 17:28:01 +00001435/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1436/// value (including ptr->int casts of the same size). Strip off any
1437/// ParenExpr or CastExprs, returning their operand.
1438Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1439 Expr *E = this;
1440 while (true) {
1441 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1442 E = P->getSubExpr();
1443 continue;
1444 }
Mike Stump11289f42009-09-09 15:08:12 +00001445
Chris Lattneref26c772009-03-13 17:28:01 +00001446 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1447 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1448 // ptr<->int casts of the same width. We also ignore all identify casts.
1449 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattneref26c772009-03-13 17:28:01 +00001451 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1452 E = SE;
1453 continue;
1454 }
Mike Stump11289f42009-09-09 15:08:12 +00001455
Chris Lattneref26c772009-03-13 17:28:01 +00001456 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1457 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1458 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1459 E = SE;
1460 continue;
1461 }
1462 }
Mike Stump11289f42009-09-09 15:08:12 +00001463
Chris Lattneref26c772009-03-13 17:28:01 +00001464 return E;
1465 }
1466}
1467
Douglas Gregord196a582009-12-14 19:27:10 +00001468bool Expr::isDefaultArgument() const {
1469 const Expr *E = this;
1470 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1471 E = ICE->getSubExprAsWritten();
1472
1473 return isa<CXXDefaultArgExpr>(E);
1474}
Chris Lattneref26c772009-03-13 17:28:01 +00001475
Douglas Gregor4619e432008-12-05 23:32:09 +00001476/// hasAnyTypeDependentArguments - Determines if any of the expressions
1477/// in Exprs is type-dependent.
1478bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1479 for (unsigned I = 0; I < NumExprs; ++I)
1480 if (Exprs[I]->isTypeDependent())
1481 return true;
1482
1483 return false;
1484}
1485
1486/// hasAnyValueDependentArguments - Determines if any of the expressions
1487/// in Exprs is value-dependent.
1488bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1489 for (unsigned I = 0; I < NumExprs; ++I)
1490 if (Exprs[I]->isValueDependent())
1491 return true;
1492
1493 return false;
1494}
1495
Eli Friedman7139af42009-01-25 02:32:41 +00001496bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001497 // This function is attempting whether an expression is an initializer
1498 // which can be evaluated at compile-time. isEvaluatable handles most
1499 // of the cases, but it can't deal with some initializer-specific
1500 // expressions, and it can't deal with aggregates; we deal with those here,
1501 // and fall back to isEvaluatable for the other cases.
1502
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001503 // FIXME: This function assumes the variable being assigned to
1504 // isn't a reference type!
1505
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001506 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001507 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001508 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001509 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001510 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001511 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001512 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001513 // This handles gcc's extension that allows global initializers like
1514 // "struct x {int x;} x = (struct x) {};".
1515 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001516 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001517 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001518 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001519 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001520 // FIXME: This doesn't deal with fields with reference types correctly.
1521 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1522 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001523 const InitListExpr *Exp = cast<InitListExpr>(this);
1524 unsigned numInits = Exp->getNumInits();
1525 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001526 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001527 return false;
1528 }
Eli Friedman384da272009-01-25 03:12:18 +00001529 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001530 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001531 case ImplicitValueInitExprClass:
1532 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001533 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001534 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001535 case UnaryOperatorClass: {
1536 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1537 if (Exp->getOpcode() == UnaryOperator::Extension)
1538 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1539 break;
1540 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001541 case BinaryOperatorClass: {
1542 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1543 // but this handles the common case.
1544 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1545 if (Exp->getOpcode() == BinaryOperator::Sub &&
1546 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1547 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1548 return true;
1549 break;
1550 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001551 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001552 case CStyleCastExprClass:
1553 // Handle casts with a destination that's a struct or union; this
1554 // deals with both the gcc no-op struct cast extension and the
1555 // cast-to-union extension.
1556 if (getType()->isRecordType())
1557 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001558
1559 // Integer->integer casts can be handled here, which is important for
1560 // things like (int)(&&x-&&y). Scary but true.
1561 if (getType()->isIntegerType() &&
1562 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1563 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1564
Eli Friedman384da272009-01-25 03:12:18 +00001565 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001566 }
Eli Friedman384da272009-01-25 03:12:18 +00001567 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001568}
1569
Chris Lattner1f4479e2007-06-05 04:15:44 +00001570/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001571/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001572
1573/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1574/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001575///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001576/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1577/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1578/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001579
Eli Friedman98c56a42009-02-26 09:29:13 +00001580// CheckICE - This function does the fundamental ICE checking: the returned
1581// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1582// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001583// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001584// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001585// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001586//
1587// Meanings of Val:
1588// 0: This expression is an ICE if it can be evaluated by Evaluate.
1589// 1: This expression is not an ICE, but if it isn't evaluated, it's
1590// a legal subexpression for an ICE. This return value is used to handle
1591// the comma operator in C99 mode.
1592// 2: This expression is not an ICE, and is not a legal subexpression for one.
1593
1594struct ICEDiag {
1595 unsigned Val;
1596 SourceLocation Loc;
1597
1598 public:
1599 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1600 ICEDiag() : Val(0) {}
1601};
1602
1603ICEDiag NoDiag() { return ICEDiag(); }
1604
Eli Friedman90afd3d2009-02-27 04:07:58 +00001605static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1606 Expr::EvalResult EVResult;
1607 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1608 !EVResult.Val.isInt()) {
1609 return ICEDiag(2, E->getLocStart());
1610 }
1611 return NoDiag();
1612}
1613
Eli Friedman98c56a42009-02-26 09:29:13 +00001614static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001615 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001616 if (!E->getType()->isIntegralType()) {
1617 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001618 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001619
1620 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001621#define STMT(Node, Base) case Expr::Node##Class:
1622#define EXPR(Node, Base)
1623#include "clang/AST/StmtNodes.def"
1624 case Expr::PredefinedExprClass:
1625 case Expr::FloatingLiteralClass:
1626 case Expr::ImaginaryLiteralClass:
1627 case Expr::StringLiteralClass:
1628 case Expr::ArraySubscriptExprClass:
1629 case Expr::MemberExprClass:
1630 case Expr::CompoundAssignOperatorClass:
1631 case Expr::CompoundLiteralExprClass:
1632 case Expr::ExtVectorElementExprClass:
1633 case Expr::InitListExprClass:
1634 case Expr::DesignatedInitExprClass:
1635 case Expr::ImplicitValueInitExprClass:
1636 case Expr::ParenListExprClass:
1637 case Expr::VAArgExprClass:
1638 case Expr::AddrLabelExprClass:
1639 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001640 case Expr::CXXMemberCallExprClass:
1641 case Expr::CXXDynamicCastExprClass:
1642 case Expr::CXXTypeidExprClass:
1643 case Expr::CXXNullPtrLiteralExprClass:
1644 case Expr::CXXThisExprClass:
1645 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001646 case Expr::CXXNewExprClass:
1647 case Expr::CXXDeleteExprClass:
1648 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001649 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001650 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001651 case Expr::CXXConstructExprClass:
1652 case Expr::CXXBindTemporaryExprClass:
Anders Carlssonba6c4372010-01-29 02:39:32 +00001653 case Expr::CXXBindReferenceExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001654 case Expr::CXXExprWithTemporariesClass:
1655 case Expr::CXXTemporaryObjectExprClass:
1656 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001657 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001658 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001659 case Expr::ObjCStringLiteralClass:
1660 case Expr::ObjCEncodeExprClass:
1661 case Expr::ObjCMessageExprClass:
1662 case Expr::ObjCSelectorExprClass:
1663 case Expr::ObjCProtocolExprClass:
1664 case Expr::ObjCIvarRefExprClass:
1665 case Expr::ObjCPropertyRefExprClass:
1666 case Expr::ObjCImplicitSetterGetterRefExprClass:
1667 case Expr::ObjCSuperExprClass:
1668 case Expr::ObjCIsaExprClass:
1669 case Expr::ShuffleVectorExprClass:
1670 case Expr::BlockExprClass:
1671 case Expr::BlockDeclRefExprClass:
1672 case Expr::NoStmtClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001673 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001674
Douglas Gregor73341c42009-09-11 00:18:58 +00001675 case Expr::GNUNullExprClass:
1676 // GCC considers the GNU __null value to be an integral constant expression.
1677 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001678
Eli Friedman98c56a42009-02-26 09:29:13 +00001679 case Expr::ParenExprClass:
1680 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1681 case Expr::IntegerLiteralClass:
1682 case Expr::CharacterLiteralClass:
1683 case Expr::CXXBoolLiteralExprClass:
1684 case Expr::CXXZeroInitValueExprClass:
1685 case Expr::TypesCompatibleExprClass:
1686 case Expr::UnaryTypeTraitExprClass:
1687 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001688 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001689 case Expr::CXXOperatorCallExprClass: {
1690 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001691 if (CE->isBuiltinCall(Ctx))
1692 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001693 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001694 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001695 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001696 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1697 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001698 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001699 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCall6dee4732010-02-24 09:03:18 +00001700 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1701
1702 // Parameter variables are never constants. Without this check,
1703 // getAnyInitializer() can find a default argument, which leads
1704 // to chaos.
1705 if (isa<ParmVarDecl>(D))
1706 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1707
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001708 // C++ 7.1.5.1p2
1709 // A variable of non-volatile const-qualified integral or enumeration
1710 // type initialized by an ICE can be used in ICEs.
John McCall6dee4732010-02-24 09:03:18 +00001711 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001712 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1713 if (Quals.hasVolatile() || !Quals.hasConst())
1714 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1715
Sebastian Redl5ca79842010-02-01 20:16:42 +00001716 // Look for a declaration of this variable that has an initializer.
1717 const VarDecl *ID = 0;
1718 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001719 if (Init) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001720 if (ID->isInitKnownICE()) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001721 // We have already checked whether this subexpression is an
1722 // integral constant expression.
Sebastian Redl5ca79842010-02-01 20:16:42 +00001723 if (ID->isInitICE())
Douglas Gregor0840cc02009-11-01 20:32:48 +00001724 return NoDiag();
1725 else
1726 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1727 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001728
John McCall52cc0892010-02-06 01:07:37 +00001729 // It's an ICE whether or not the definition we found is
1730 // out-of-line. See DR 721 and the discussion in Clang PR
1731 // 6206 for details.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001732
1733 if (Dcl->isCheckingICE()) {
1734 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1735 }
1736
1737 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001738 ICEDiag Result = CheckICE(Init, Ctx);
1739 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001740 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001741 return Result;
1742 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001743 }
1744 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001745 return ICEDiag(2, E->getLocStart());
1746 case Expr::UnaryOperatorClass: {
1747 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001748 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001749 case UnaryOperator::PostInc:
1750 case UnaryOperator::PostDec:
1751 case UnaryOperator::PreInc:
1752 case UnaryOperator::PreDec:
1753 case UnaryOperator::AddrOf:
1754 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001755 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001756
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001757 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001758 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001759 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001760 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001761 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001762 case UnaryOperator::Real:
1763 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001764 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001765 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001766 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1767 // Evaluate matches the proposed gcc behavior for cases like
1768 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1769 // compliance: we should warn earlier for offsetof expressions with
1770 // array subscripts that aren't ICEs, and if the array subscripts
1771 // are ICEs, the value of the offsetof must be an integer constant.
1772 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001773 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001774 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001775 case Expr::SizeOfAlignOfExprClass: {
1776 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1777 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1778 return ICEDiag(2, E->getLocStart());
1779 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001780 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001781 case Expr::BinaryOperatorClass: {
1782 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001783 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001784 case BinaryOperator::PtrMemD:
1785 case BinaryOperator::PtrMemI:
1786 case BinaryOperator::Assign:
1787 case BinaryOperator::MulAssign:
1788 case BinaryOperator::DivAssign:
1789 case BinaryOperator::RemAssign:
1790 case BinaryOperator::AddAssign:
1791 case BinaryOperator::SubAssign:
1792 case BinaryOperator::ShlAssign:
1793 case BinaryOperator::ShrAssign:
1794 case BinaryOperator::AndAssign:
1795 case BinaryOperator::XorAssign:
1796 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001797 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001798
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001799 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001800 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001801 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001802 case BinaryOperator::Add:
1803 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001804 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001805 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001806 case BinaryOperator::LT:
1807 case BinaryOperator::GT:
1808 case BinaryOperator::LE:
1809 case BinaryOperator::GE:
1810 case BinaryOperator::EQ:
1811 case BinaryOperator::NE:
1812 case BinaryOperator::And:
1813 case BinaryOperator::Xor:
1814 case BinaryOperator::Or:
1815 case BinaryOperator::Comma: {
1816 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1817 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001818 if (Exp->getOpcode() == BinaryOperator::Div ||
1819 Exp->getOpcode() == BinaryOperator::Rem) {
1820 // Evaluate gives an error for undefined Div/Rem, so make sure
1821 // we don't evaluate one.
1822 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1823 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1824 if (REval == 0)
1825 return ICEDiag(1, E->getLocStart());
1826 if (REval.isSigned() && REval.isAllOnesValue()) {
1827 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1828 if (LEval.isMinSignedValue())
1829 return ICEDiag(1, E->getLocStart());
1830 }
1831 }
1832 }
1833 if (Exp->getOpcode() == BinaryOperator::Comma) {
1834 if (Ctx.getLangOptions().C99) {
1835 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1836 // if it isn't evaluated.
1837 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1838 return ICEDiag(1, E->getLocStart());
1839 } else {
1840 // In both C89 and C++, commas in ICEs are illegal.
1841 return ICEDiag(2, E->getLocStart());
1842 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001843 }
1844 if (LHSResult.Val >= RHSResult.Val)
1845 return LHSResult;
1846 return RHSResult;
1847 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001848 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001849 case BinaryOperator::LOr: {
1850 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1851 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1852 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1853 // Rare case where the RHS has a comma "side-effect"; we need
1854 // to actually check the condition to see whether the side
1855 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001856 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001857 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001858 return RHSResult;
1859 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001860 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001861
Eli Friedman98c56a42009-02-26 09:29:13 +00001862 if (LHSResult.Val >= RHSResult.Val)
1863 return LHSResult;
1864 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001865 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001866 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001867 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001868 case Expr::ImplicitCastExprClass:
1869 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001870 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001871 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001872 case Expr::CXXStaticCastExprClass:
1873 case Expr::CXXReinterpretCastExprClass:
1874 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001875 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1876 if (SubExpr->getType()->isIntegralType())
1877 return CheckICE(SubExpr, Ctx);
1878 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1879 return NoDiag();
1880 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001881 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001882 case Expr::ConditionalOperatorClass: {
1883 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001884 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001885 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001886 // expression, and it is fully evaluated. This is an important GNU
1887 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001888 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001889 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001890 Expr::EvalResult EVResult;
1891 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1892 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001893 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001894 }
1895 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001896 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001897 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1898 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1899 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1900 if (CondResult.Val == 2)
1901 return CondResult;
1902 if (TrueResult.Val == 2)
1903 return TrueResult;
1904 if (FalseResult.Val == 2)
1905 return FalseResult;
1906 if (CondResult.Val == 1)
1907 return CondResult;
1908 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1909 return NoDiag();
1910 // Rare case where the diagnostics depend on which side is evaluated
1911 // Note that if we get here, CondResult is 0, and at least one of
1912 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001913 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001914 return FalseResult;
1915 }
1916 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001917 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001918 case Expr::CXXDefaultArgExprClass:
1919 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001920 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001921 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001922 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001923 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001924
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001925 // Silence a GCC warning
1926 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001927}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001928
Eli Friedman98c56a42009-02-26 09:29:13 +00001929bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1930 SourceLocation *Loc, bool isEvaluated) const {
1931 ICEDiag d = CheckICE(this, Ctx);
1932 if (d.Val != 0) {
1933 if (Loc) *Loc = d.Loc;
1934 return false;
1935 }
1936 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001937 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001938 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001939 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1940 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001941 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001942 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001943}
1944
Chris Lattner7eef9192007-05-24 01:23:49 +00001945/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1946/// integer constant expression with the value zero, or if this is one that is
1947/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001948bool Expr::isNullPointerConstant(ASTContext &Ctx,
1949 NullPointerConstantValueDependence NPC) const {
1950 if (isValueDependent()) {
1951 switch (NPC) {
1952 case NPC_NeverValueDependent:
1953 assert(false && "Unexpected value dependent expression!");
1954 // If the unthinkable happens, fall through to the safest alternative.
1955
1956 case NPC_ValueDependentIsNull:
1957 return isTypeDependent() || getType()->isIntegralType();
1958
1959 case NPC_ValueDependentIsNotNull:
1960 return false;
1961 }
1962 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001963
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001964 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001965 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001966 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001967 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001968 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001969 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001970 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001971 Pointee->isVoidType() && // to void*
1972 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001973 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001974 }
Steve Naroffada7d422007-05-20 17:54:12 +00001975 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001976 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1977 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001978 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001979 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1980 // Accept ((void*)0) as a null pointer constant, as many other
1981 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001982 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001983 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001984 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001985 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001986 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001987 } else if (isa<GNUNullExpr>(this)) {
1988 // The GNU __null extension is always a null pointer constant.
1989 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001990 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001991
Sebastian Redl576fd422009-05-10 18:38:11 +00001992 // C++0x nullptr_t is always a null pointer constant.
1993 if (getType()->isNullPtrType())
1994 return true;
1995
Steve Naroff4871fe02008-01-14 16:10:57 +00001996 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001997 if (!getType()->isIntegerType() ||
1998 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001999 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattner1abbd412007-06-08 17:58:43 +00002001 // If we have an integer constant expression, we need to *evaluate* it and
2002 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002003 llvm::APSInt Result;
2004 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002005}
Steve Narofff7a5da12007-07-28 23:10:27 +00002006
Douglas Gregor71235ec2009-05-02 02:18:30 +00002007FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002008 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002009
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002010 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2011 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2012 E = ICE->getSubExpr()->IgnoreParens();
2013 else
2014 break;
2015 }
2016
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002017 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002018 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002019 if (Field->isBitField())
2020 return Field;
2021
2022 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2023 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2024 return BinOp->getLHS()->getBitField();
2025
2026 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002027}
2028
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002029bool Expr::refersToVectorElement() const {
2030 const Expr *E = this->IgnoreParens();
2031
2032 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2033 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2034 E = ICE->getSubExpr()->IgnoreParens();
2035 else
2036 break;
2037 }
2038
2039 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2040 return ASE->getBase()->getType()->isVectorType();
2041
2042 if (isa<ExtVectorElementExpr>(E))
2043 return true;
2044
2045 return false;
2046}
2047
Chris Lattnerb8211f62009-02-16 22:14:05 +00002048/// isArrow - Return true if the base expression is a pointer to vector,
2049/// return false if the base expression is a vector.
2050bool ExtVectorElementExpr::isArrow() const {
2051 return getBase()->getType()->isPointerType();
2052}
2053
Nate Begemance4d7fc2008-04-18 23:10:10 +00002054unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002055 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002056 return VT->getNumElements();
2057 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002058}
2059
Nate Begemanf322eab2008-05-09 06:41:27 +00002060/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002061bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002062 // FIXME: Refactor this code to an accessor on the AST node which returns the
2063 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002064 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002065
2066 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002067 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002068 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002069
Nate Begeman7e5185b2009-01-18 02:01:21 +00002070 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002071 if (Comp[0] == 's' || Comp[0] == 'S')
2072 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002073
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002074 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2075 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002076 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002077
Steve Naroff0d595ca2007-07-30 03:29:09 +00002078 return false;
2079}
Chris Lattner885b4952007-08-02 23:36:59 +00002080
Nate Begemanf322eab2008-05-09 06:41:27 +00002081/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002082void ExtVectorElementExpr::getEncodedElementAccess(
2083 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002084 llvm::StringRef Comp = Accessor->getName();
2085 if (Comp[0] == 's' || Comp[0] == 'S')
2086 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002087
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002088 bool isHi = Comp == "hi";
2089 bool isLo = Comp == "lo";
2090 bool isEven = Comp == "even";
2091 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002092
Nate Begemanf322eab2008-05-09 06:41:27 +00002093 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2094 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002095
Nate Begemanf322eab2008-05-09 06:41:27 +00002096 if (isHi)
2097 Index = e + i;
2098 else if (isLo)
2099 Index = i;
2100 else if (isEven)
2101 Index = 2 * i;
2102 else if (isOdd)
2103 Index = 2 * i + 1;
2104 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002105 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002106
Nate Begemand3862152008-05-13 21:03:02 +00002107 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002108 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002109}
2110
Steve Narofff73590d2007-09-27 14:38:14 +00002111// constructor for instance messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002112ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2113 Selector selInfo,
2114 QualType retType, ObjCMethodDecl *mproto,
2115 SourceLocation LBrac, SourceLocation RBrac,
2116 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002117 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002118 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002119 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002120 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002121 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002122 if (NumArgs) {
2123 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002124 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2125 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002126 LBracloc = LBrac;
2127 RBracloc = RBrac;
2128}
2129
Mike Stump11289f42009-09-09 15:08:12 +00002130// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002131// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenek2c809302010-02-11 22:41:21 +00002132ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002133 SourceLocation clsNameLoc, Selector selInfo,
2134 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenek2c809302010-02-11 22:41:21 +00002135 SourceLocation LBrac, SourceLocation RBrac,
2136 Expr **ArgExprs, unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002137 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2138 SelName(selInfo), MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002139 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002140 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002141 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002142 if (NumArgs) {
2143 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002144 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2145 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002146 LBracloc = LBrac;
2147 RBracloc = RBrac;
2148}
2149
Mike Stump11289f42009-09-09 15:08:12 +00002150// constructor for class messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002151ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002152 SourceLocation clsNameLoc, Selector selInfo,
2153 QualType retType,
Ted Kremenek2c809302010-02-11 22:41:21 +00002154 ObjCMethodDecl *mproto, SourceLocation LBrac,
2155 SourceLocation RBrac, Expr **ArgExprs,
2156 unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002157 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2158 SelName(selInfo), MethodProto(mproto)
2159{
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002160 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002161 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002162 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2163 if (NumArgs) {
2164 for (unsigned i = 0; i != NumArgs; ++i)
2165 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2166 }
2167 LBracloc = LBrac;
2168 RBracloc = RBrac;
2169}
2170
2171ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2172 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2173 switch (x & Flags) {
2174 default:
2175 assert(false && "Invalid ObjCMessageExpr.");
2176 case IsInstMeth:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002177 return ClassInfo(0, 0, SourceLocation());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002178 case IsClsMethDeclUnknown:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002179 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002180 case IsClsMethDeclKnown: {
2181 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002182 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002183 }
2184 }
2185}
2186
Chris Lattner7ec71da2009-04-26 00:44:05 +00002187void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorde4827d2010-03-08 16:40:19 +00002188 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner7ec71da2009-04-26 00:44:05 +00002189 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002190 return;
2191 }
2192
2193 if (CI.Decl == 0)
2194 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner7ec71da2009-04-26 00:44:05 +00002195 else
Douglas Gregorde4827d2010-03-08 16:40:19 +00002196 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2197 ClassNameLoc = CI.Loc;
Chris Lattner7ec71da2009-04-26 00:44:05 +00002198}
2199
Ted Kremenek2c809302010-02-11 22:41:21 +00002200void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2201 DestroyChildren(C);
2202 if (SubExprs)
2203 C.Deallocate(SubExprs);
2204 this->~ObjCMessageExpr();
2205 C.Deallocate((void*) this);
2206}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002207
Chris Lattner35e564e2007-10-25 00:29:32 +00002208bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002209 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002210}
2211
Nate Begeman48745922009-08-12 02:28:50 +00002212void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2213 unsigned NumExprs) {
2214 if (SubExprs) C.Deallocate(SubExprs);
2215
2216 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002217 this->NumExprs = NumExprs;
2218 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002219}
Nate Begeman48745922009-08-12 02:28:50 +00002220
2221void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2222 DestroyChildren(C);
2223 if (SubExprs) C.Deallocate(SubExprs);
2224 this->~ShuffleVectorExpr();
2225 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002226}
2227
Douglas Gregore26a2852009-08-07 06:08:38 +00002228void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002229 // Override default behavior of traversing children. If this has a type
2230 // operand and the type is a variable-length array, the child iteration
2231 // will iterate over the size expression. However, this expression belongs
2232 // to the type, not to this, so we don't want to delete it.
2233 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002234 if (isArgumentType()) {
2235 this->~SizeOfAlignOfExpr();
2236 C.Deallocate(this);
2237 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002238 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002239 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002240}
2241
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002242//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002243// DesignatedInitExpr
2244//===----------------------------------------------------------------------===//
2245
2246IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2247 assert(Kind == FieldDesignator && "Only valid on a field designator");
2248 if (Field.NameOrField & 0x01)
2249 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2250 else
2251 return getField()->getIdentifier();
2252}
2253
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002254DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2255 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002256 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002257 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002258 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002259 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002260 unsigned NumIndexExprs,
2261 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002262 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002263 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002264 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2265 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002266 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002267
2268 // Record the initializer itself.
2269 child_iterator Child = child_begin();
2270 *Child++ = Init;
2271
2272 // Copy the designators and their subexpressions, computing
2273 // value-dependence along the way.
2274 unsigned IndexIdx = 0;
2275 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002276 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002277
2278 if (this->Designators[I].isArrayDesignator()) {
2279 // Compute type- and value-dependence.
2280 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002281 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002282 Index->isTypeDependent() || Index->isValueDependent();
2283
2284 // Copy the index expressions into permanent storage.
2285 *Child++ = IndexExprs[IndexIdx++];
2286 } else if (this->Designators[I].isArrayRangeDesignator()) {
2287 // Compute type- and value-dependence.
2288 Expr *Start = IndexExprs[IndexIdx];
2289 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002290 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002291 Start->isTypeDependent() || Start->isValueDependent() ||
2292 End->isTypeDependent() || End->isValueDependent();
2293
2294 // Copy the start/end expressions into permanent storage.
2295 *Child++ = IndexExprs[IndexIdx++];
2296 *Child++ = IndexExprs[IndexIdx++];
2297 }
2298 }
2299
2300 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002301}
2302
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002303DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002304DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002305 unsigned NumDesignators,
2306 Expr **IndexExprs, unsigned NumIndexExprs,
2307 SourceLocation ColonOrEqualLoc,
2308 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002309 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002310 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002311 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002312 ColonOrEqualLoc, UsesColonSyntax,
2313 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002314}
2315
Mike Stump11289f42009-09-09 15:08:12 +00002316DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002317 unsigned NumIndexExprs) {
2318 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2319 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2320 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2321}
2322
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002323void DesignatedInitExpr::setDesignators(ASTContext &C,
2324 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002325 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002326 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002327
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002328 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002329 NumDesignators = NumDesigs;
2330 for (unsigned I = 0; I != NumDesigs; ++I)
2331 Designators[I] = Desigs[I];
2332}
2333
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002334SourceRange DesignatedInitExpr::getSourceRange() const {
2335 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002336 Designator &First =
2337 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002338 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002339 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002340 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2341 else
2342 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2343 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002344 StartLoc =
2345 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002346 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2347}
2348
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002349Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2350 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2351 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2352 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002353 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2354 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2355}
2356
2357Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002358 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002359 "Requires array range designator");
2360 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2361 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002362 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2363 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2364}
2365
2366Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002367 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002368 "Requires array range designator");
2369 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2370 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002371 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2372 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2373}
2374
Douglas Gregord5846a12009-04-15 06:41:24 +00002375/// \brief Replaces the designator at index @p Idx with the series
2376/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002377void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002378 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002379 const Designator *Last) {
2380 unsigned NumNewDesignators = Last - First;
2381 if (NumNewDesignators == 0) {
2382 std::copy_backward(Designators + Idx + 1,
2383 Designators + NumDesignators,
2384 Designators + Idx);
2385 --NumNewDesignators;
2386 return;
2387 } else if (NumNewDesignators == 1) {
2388 Designators[Idx] = *First;
2389 return;
2390 }
2391
Mike Stump11289f42009-09-09 15:08:12 +00002392 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002393 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002394 std::copy(Designators, Designators + Idx, NewDesignators);
2395 std::copy(First, Last, NewDesignators + Idx);
2396 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2397 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002398 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002399 Designators = NewDesignators;
2400 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2401}
2402
Douglas Gregore26a2852009-08-07 06:08:38 +00002403void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002404 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002405 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002406}
2407
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002408void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2409 for (unsigned I = 0; I != NumDesignators; ++I)
2410 Designators[I].~Designator();
2411 C.Deallocate(Designators);
2412 Designators = 0;
2413}
2414
Mike Stump11289f42009-09-09 15:08:12 +00002415ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002416 Expr **exprs, unsigned nexprs,
2417 SourceLocation rparenloc)
2418: Expr(ParenListExprClass, QualType(),
2419 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002420 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002421 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002422
Nate Begeman5ec4b312009-08-10 23:49:36 +00002423 Exprs = new (C) Stmt*[nexprs];
2424 for (unsigned i = 0; i != nexprs; ++i)
2425 Exprs[i] = exprs[i];
2426}
2427
2428void ParenListExpr::DoDestroy(ASTContext& C) {
2429 DestroyChildren(C);
2430 if (Exprs) C.Deallocate(Exprs);
2431 this->~ParenListExpr();
2432 C.Deallocate(this);
2433}
2434
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002435//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002436// ExprIterator.
2437//===----------------------------------------------------------------------===//
2438
2439Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2440Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2441Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2442const Expr* ConstExprIterator::operator[](size_t idx) const {
2443 return cast<Expr>(I[idx]);
2444}
2445const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2446const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2447
2448//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002449// Child Iterators for iterating over subexpressions/substatements
2450//===----------------------------------------------------------------------===//
2451
2452// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002453Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2454Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002455
Steve Naroffe46504b2007-11-12 14:29:37 +00002456// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002457Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2458Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002459
Steve Naroffebf4cb42008-06-02 23:03:37 +00002460// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002461Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2462Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002463
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002464// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002465Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2466 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002467}
Mike Stump11289f42009-09-09 15:08:12 +00002468Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2469 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002470}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002471
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002472// ObjCSuperExpr
2473Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2474Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2475
Steve Naroffe87026a2009-07-24 17:54:45 +00002476// ObjCIsaExpr
2477Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2478Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2479
Chris Lattner6307f192008-08-10 01:53:14 +00002480// PredefinedExpr
2481Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2482Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002483
2484// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002485Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2486Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002487
2488// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002489Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002490Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002491
2492// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002493Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2494Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002495
Chris Lattner1c20a172007-08-26 03:42:43 +00002496// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002497Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2498Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002499
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002500// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002501Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2502Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002503
2504// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002505Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2506Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002507
2508// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002509Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2510Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002511
Sebastian Redl6f282892008-11-11 17:56:53 +00002512// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002513Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002514 // If this is of a type and the type is a VLA type (and not a typedef), the
2515 // size expression of the VLA needs to be treated as an executable expression.
2516 // Why isn't this weirdness documented better in StmtIterator?
2517 if (isArgumentType()) {
2518 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2519 getArgumentType().getTypePtr()))
2520 return child_iterator(T);
2521 return child_iterator();
2522 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002523 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002524}
Sebastian Redl6f282892008-11-11 17:56:53 +00002525Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2526 if (isArgumentType())
2527 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002528 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002529}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002530
2531// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002532Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002533 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002534}
Ted Kremenek23702b62007-08-24 20:06:47 +00002535Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002536 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002537}
2538
2539// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002540Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002541 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002542}
Ted Kremenek23702b62007-08-24 20:06:47 +00002543Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002544 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002545}
Ted Kremenek23702b62007-08-24 20:06:47 +00002546
2547// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002548Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2549Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002550
Nate Begemance4d7fc2008-04-18 23:10:10 +00002551// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002552Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2553Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002554
2555// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002556Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2557Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002558
Ted Kremenek23702b62007-08-24 20:06:47 +00002559// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002560Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2561Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002562
2563// BinaryOperator
2564Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002565 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002566}
Ted Kremenek23702b62007-08-24 20:06:47 +00002567Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002568 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002569}
2570
2571// ConditionalOperator
2572Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002573 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002574}
Ted Kremenek23702b62007-08-24 20:06:47 +00002575Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002576 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002577}
2578
2579// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002580Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2581Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002582
Ted Kremenek23702b62007-08-24 20:06:47 +00002583// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002584Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2585Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002586
2587// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002588Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2589 return child_iterator();
2590}
2591
2592Stmt::child_iterator TypesCompatibleExpr::child_end() {
2593 return child_iterator();
2594}
Ted Kremenek23702b62007-08-24 20:06:47 +00002595
2596// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002597Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2598Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002599
Douglas Gregor3be4b122008-11-29 04:51:27 +00002600// GNUNullExpr
2601Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2602Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2603
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002604// ShuffleVectorExpr
2605Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002606 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002607}
2608Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002609 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002610}
2611
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002612// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002613Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2614Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002615
Anders Carlsson4692db02007-08-31 04:56:16 +00002616// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002617Stmt::child_iterator InitListExpr::child_begin() {
2618 return InitExprs.size() ? &InitExprs[0] : 0;
2619}
2620Stmt::child_iterator InitListExpr::child_end() {
2621 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2622}
Anders Carlsson4692db02007-08-31 04:56:16 +00002623
Douglas Gregor0202cb42009-01-29 17:44:32 +00002624// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002625Stmt::child_iterator DesignatedInitExpr::child_begin() {
2626 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2627 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002628 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2629}
2630Stmt::child_iterator DesignatedInitExpr::child_end() {
2631 return child_iterator(&*child_begin() + NumSubExprs);
2632}
2633
Douglas Gregor0202cb42009-01-29 17:44:32 +00002634// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002635Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2636 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002637}
2638
Mike Stump11289f42009-09-09 15:08:12 +00002639Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2640 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002641}
2642
Nate Begeman5ec4b312009-08-10 23:49:36 +00002643// ParenListExpr
2644Stmt::child_iterator ParenListExpr::child_begin() {
2645 return &Exprs[0];
2646}
2647Stmt::child_iterator ParenListExpr::child_end() {
2648 return &Exprs[0]+NumExprs;
2649}
2650
Ted Kremenek23702b62007-08-24 20:06:47 +00002651// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002652Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002653 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002654}
2655Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002656 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002657}
Ted Kremenek23702b62007-08-24 20:06:47 +00002658
2659// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002660Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2661Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002662
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002663// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002664Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002665 return child_iterator();
2666}
2667Stmt::child_iterator ObjCSelectorExpr::child_end() {
2668 return child_iterator();
2669}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002670
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002671// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002672Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2673 return child_iterator();
2674}
2675Stmt::child_iterator ObjCProtocolExpr::child_end() {
2676 return child_iterator();
2677}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002678
Steve Naroffd54978b2007-09-18 23:55:05 +00002679// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002680Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002681 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002682}
2683Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002684 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002685}
2686
Steve Naroffc540d662008-09-03 18:15:37 +00002687// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002688Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2689Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002690
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002691Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2692Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }