blob: 58776f47d97e4e8a26325354aab91f8f13a6a4ab [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());
184 Policy.SuppressTagKind = true;
185
186 std::string Proto = FD->getQualifiedNameAsString(Policy);
187
John McCall9dd450b2009-09-21 23:43:11 +0000188 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000189 const FunctionProtoType *FT = 0;
190 if (FD->hasWrittenPrototype())
191 FT = dyn_cast<FunctionProtoType>(AFT);
192
193 Proto += "(";
194 if (FT) {
195 llvm::raw_string_ostream POut(Proto);
196 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
197 if (i) POut << ", ";
198 std::string Param;
199 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
200 POut << Param;
201 }
202
203 if (FT->isVariadic()) {
204 if (FD->getNumParams()) POut << ", ";
205 POut << "...";
206 }
207 }
208 Proto += ")";
209
Sam Weinig4e83bd22009-12-27 01:38:20 +0000210 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
211 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
212 if (ThisQuals.hasConst())
213 Proto += " const";
214 if (ThisQuals.hasVolatile())
215 Proto += " volatile";
216 }
217
Sam Weinigd060ed42009-12-06 23:55:13 +0000218 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
219 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000220
221 Out << Proto;
222
223 Out.flush();
224 return Name.str().str();
225 }
226 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
227 llvm::SmallString<256> Name;
228 llvm::raw_svector_ostream Out(Name);
229 Out << (MD->isInstanceMethod() ? '-' : '+');
230 Out << '[';
231 Out << MD->getClassInterface()->getNameAsString();
232 if (const ObjCCategoryImplDecl *CID =
233 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
234 Out << '(';
235 Out << CID->getNameAsString();
236 Out << ')';
237 }
238 Out << ' ';
239 Out << MD->getSelector().getAsString();
240 Out << ']';
241
242 Out.flush();
243 return Name.str().str();
244 }
245 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
246 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
247 return "top level";
248 }
249 return "";
250}
251
Chris Lattnera0173132008-06-07 22:13:43 +0000252/// getValueAsApproximateDouble - This returns the value as an inaccurate
253/// double. Note that this may cause loss of precision, but is useful for
254/// debugging dumps, etc.
255double FloatingLiteral::getValueAsApproximateDouble() const {
256 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000257 bool ignored;
258 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
259 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000260 return V.convertToDouble();
261}
262
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000263StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
264 unsigned ByteLength, bool Wide,
265 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000266 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000267 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000268 // Allocate enough space for the StringLiteral plus an array of locations for
269 // any concatenated string tokens.
270 void *Mem = C.Allocate(sizeof(StringLiteral)+
271 sizeof(SourceLocation)*(NumStrs-1),
272 llvm::alignof<StringLiteral>());
273 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000274
Steve Naroffdf7855b2007-02-21 23:46:25 +0000275 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000276 char *AStrData = new (C, 1) char[ByteLength];
277 memcpy(AStrData, StrData, ByteLength);
278 SL->StrData = AStrData;
279 SL->ByteLength = ByteLength;
280 SL->IsWide = Wide;
281 SL->TokLocs[0] = Loc[0];
282 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000283
Chris Lattner630970d2009-02-18 05:49:11 +0000284 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000285 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
286 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000287}
288
Douglas Gregor958dfc92009-04-15 16:35:07 +0000289StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
290 void *Mem = C.Allocate(sizeof(StringLiteral)+
291 sizeof(SourceLocation)*(NumStrs-1),
292 llvm::alignof<StringLiteral>());
293 StringLiteral *SL = new (Mem) StringLiteral(QualType());
294 SL->StrData = 0;
295 SL->ByteLength = 0;
296 SL->NumConcatenated = NumStrs;
297 return SL;
298}
299
Douglas Gregore26a2852009-08-07 06:08:38 +0000300void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000301 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000302 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000303}
304
Daniel Dunbar36217882009-09-22 03:27:33 +0000305void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000306 if (StrData)
307 C.Deallocate(const_cast<char*>(StrData));
308
Daniel Dunbar36217882009-09-22 03:27:33 +0000309 char *AStrData = new (C, 1) char[Str.size()];
310 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000311 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000312 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000313}
314
Chris Lattner1b926492006-08-23 06:42:10 +0000315/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
316/// corresponds to, e.g. "sizeof" or "[pre]++".
317const char *UnaryOperator::getOpcodeStr(Opcode Op) {
318 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000319 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000320 case PostInc: return "++";
321 case PostDec: return "--";
322 case PreInc: return "++";
323 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000324 case AddrOf: return "&";
325 case Deref: return "*";
326 case Plus: return "+";
327 case Minus: return "-";
328 case Not: return "~";
329 case LNot: return "!";
330 case Real: return "__real";
331 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000332 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000333 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000334 }
335}
336
Mike Stump11289f42009-09-09 15:08:12 +0000337UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000338UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
339 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000340 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000341 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
342 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
343 case OO_Amp: return AddrOf;
344 case OO_Star: return Deref;
345 case OO_Plus: return Plus;
346 case OO_Minus: return Minus;
347 case OO_Tilde: return Not;
348 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000349 }
350}
351
352OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
353 switch (Opc) {
354 case PostInc: case PreInc: return OO_PlusPlus;
355 case PostDec: case PreDec: return OO_MinusMinus;
356 case AddrOf: return OO_Amp;
357 case Deref: return OO_Star;
358 case Plus: return OO_Plus;
359 case Minus: return OO_Minus;
360 case Not: return OO_Tilde;
361 case LNot: return OO_Exclaim;
362 default: return OO_None;
363 }
364}
365
366
Chris Lattner0eedafe2006-08-24 04:56:27 +0000367//===----------------------------------------------------------------------===//
368// Postfix Operators.
369//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000370
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000371CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000372 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000373 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000374 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000375 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000376 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000378 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000379 SubExprs[FN] = fn;
380 for (unsigned i = 0; i != numargs; ++i)
381 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000382
Douglas Gregor993603d2008-11-14 16:09:21 +0000383 RParenLoc = rparenloc;
384}
Nate Begeman1e36a852008-01-17 17:46:27 +0000385
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000386CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
387 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000388 : Expr(CallExprClass, t,
389 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000390 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000391 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000392
393 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000394 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000395 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000396 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000397
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000398 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000399}
400
Mike Stump11289f42009-09-09 15:08:12 +0000401CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
402 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000403 SubExprs = new (C) Stmt*[1];
404}
405
Douglas Gregore26a2852009-08-07 06:08:38 +0000406void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000407 DestroyChildren(C);
408 if (SubExprs) C.Deallocate(SubExprs);
409 this->~CallExpr();
410 C.Deallocate(this);
411}
412
Nuno Lopes518e3702009-12-20 23:11:08 +0000413Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000414 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000415 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000416 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000417 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
418 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000419
420 return 0;
421}
422
Nuno Lopes518e3702009-12-20 23:11:08 +0000423FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000424 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000425}
426
Chris Lattnere4407ed2007-12-28 05:25:02 +0000427/// setNumArgs - This changes the number of arguments present in this call.
428/// Any orphaned expressions are deleted by this, and any new operands are set
429/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000430void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000431 // No change, just return.
432 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattnere4407ed2007-12-28 05:25:02 +0000434 // If shrinking # arguments, just delete the extras and forgot them.
435 if (NumArgs < getNumArgs()) {
436 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000437 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000438 this->NumArgs = NumArgs;
439 return;
440 }
441
442 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000443 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000444 // Copy over args.
445 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
446 NewSubExprs[i] = SubExprs[i];
447 // Null out new args.
448 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
449 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000450
Douglas Gregorba6e5572009-04-17 21:46:47 +0000451 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000452 SubExprs = NewSubExprs;
453 this->NumArgs = NumArgs;
454}
455
Chris Lattner01ff98a2008-10-06 05:00:53 +0000456/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
457/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000458unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000459 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000460 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000461 // ImplicitCastExpr.
462 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
463 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000464 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000465
Steve Narofff6e3b3292008-01-31 01:07:12 +0000466 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
467 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000468 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000470 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
471 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000472 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000473
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000474 if (!FDecl->getIdentifier())
475 return 0;
476
Douglas Gregor15fc9562009-09-12 00:22:50 +0000477 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000478}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000479
Anders Carlsson00a27592009-05-26 04:57:27 +0000480QualType CallExpr::getCallReturnType() const {
481 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000482 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000483 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000484 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000485 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000486
John McCall9dd450b2009-09-21 23:43:11 +0000487 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000488 return FnType->getResultType();
489}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000490
Mike Stump11289f42009-09-09 15:08:12 +0000491MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000492 SourceRange qualrange, ValueDecl *memberdecl,
John McCall6b51f282009-11-23 01:53:49 +0000493 SourceLocation l, const TemplateArgumentListInfo *targs,
494 QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000495 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000496 base->isTypeDependent() || (qual && qual->isDependent()),
497 base->isValueDependent() || (qual && qual->isDependent())),
498 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCall6b51f282009-11-23 01:53:49 +0000499 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000500 // Initialize the qualifier, if any.
501 if (HasQualifier) {
502 NameQualifier *NQ = getMemberQualifier();
503 NQ->NNS = qual;
504 NQ->Range = qualrange;
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000507 // Initialize the explicit template argument list, if any.
John McCall6b51f282009-11-23 01:53:49 +0000508 if (targs)
509 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000510}
511
Mike Stump11289f42009-09-09 15:08:12 +0000512MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
513 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000514 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000515 ValueDecl *memberdecl,
Mike Stump11289f42009-09-09 15:08:12 +0000516 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000517 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000518 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000519 std::size_t Size = sizeof(MemberExpr);
520 if (qual != 0)
521 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000522
John McCall6b51f282009-11-23 01:53:49 +0000523 if (targs)
524 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000526 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000527 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCall6b51f282009-11-23 01:53:49 +0000528 targs, ty);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000529}
530
Anders Carlsson496335e2009-09-03 00:59:21 +0000531const char *CastExpr::getCastKindName() const {
532 switch (getCastKind()) {
533 case CastExpr::CK_Unknown:
534 return "Unknown";
535 case CastExpr::CK_BitCast:
536 return "BitCast";
537 case CastExpr::CK_NoOp:
538 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000539 case CastExpr::CK_BaseToDerived:
540 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000541 case CastExpr::CK_DerivedToBase:
542 return "DerivedToBase";
543 case CastExpr::CK_Dynamic:
544 return "Dynamic";
545 case CastExpr::CK_ToUnion:
546 return "ToUnion";
547 case CastExpr::CK_ArrayToPointerDecay:
548 return "ArrayToPointerDecay";
549 case CastExpr::CK_FunctionToPointerDecay:
550 return "FunctionToPointerDecay";
551 case CastExpr::CK_NullToMemberPointer:
552 return "NullToMemberPointer";
553 case CastExpr::CK_BaseToDerivedMemberPointer:
554 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000555 case CastExpr::CK_DerivedToBaseMemberPointer:
556 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000557 case CastExpr::CK_UserDefinedConversion:
558 return "UserDefinedConversion";
559 case CastExpr::CK_ConstructorConversion:
560 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000561 case CastExpr::CK_IntegralToPointer:
562 return "IntegralToPointer";
563 case CastExpr::CK_PointerToIntegral:
564 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000565 case CastExpr::CK_ToVoid:
566 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000567 case CastExpr::CK_VectorSplat:
568 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000569 case CastExpr::CK_IntegralCast:
570 return "IntegralCast";
571 case CastExpr::CK_IntegralToFloating:
572 return "IntegralToFloating";
573 case CastExpr::CK_FloatingToIntegral:
574 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000575 case CastExpr::CK_FloatingCast:
576 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000577 case CastExpr::CK_MemberPointerToBoolean:
578 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000579 case CastExpr::CK_AnyPointerToObjCPointerCast:
580 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000581 case CastExpr::CK_AnyPointerToBlockPointerCast:
582 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Anders Carlsson496335e2009-09-03 00:59:21 +0000585 assert(0 && "Unhandled cast kind!");
586 return 0;
587}
588
Douglas Gregord196a582009-12-14 19:27:10 +0000589Expr *CastExpr::getSubExprAsWritten() {
590 Expr *SubExpr = 0;
591 CastExpr *E = this;
592 do {
593 SubExpr = E->getSubExpr();
594
595 // Skip any temporary bindings; they're implicit.
596 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
597 SubExpr = Binder->getSubExpr();
598
599 // Conversions by constructor and conversion functions have a
600 // subexpression describing the call; strip it off.
601 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
602 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
603 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
604 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
605
606 // If the subexpression we're left with is an implicit cast, look
607 // through that, too.
608 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
609
610 return SubExpr;
611}
612
Chris Lattner1b926492006-08-23 06:42:10 +0000613/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
614/// corresponds to, e.g. "<<=".
615const char *BinaryOperator::getOpcodeStr(Opcode Op) {
616 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000617 case PtrMemD: return ".*";
618 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000619 case Mul: return "*";
620 case Div: return "/";
621 case Rem: return "%";
622 case Add: return "+";
623 case Sub: return "-";
624 case Shl: return "<<";
625 case Shr: return ">>";
626 case LT: return "<";
627 case GT: return ">";
628 case LE: return "<=";
629 case GE: return ">=";
630 case EQ: return "==";
631 case NE: return "!=";
632 case And: return "&";
633 case Xor: return "^";
634 case Or: return "|";
635 case LAnd: return "&&";
636 case LOr: return "||";
637 case Assign: return "=";
638 case MulAssign: return "*=";
639 case DivAssign: return "/=";
640 case RemAssign: return "%=";
641 case AddAssign: return "+=";
642 case SubAssign: return "-=";
643 case ShlAssign: return "<<=";
644 case ShrAssign: return ">>=";
645 case AndAssign: return "&=";
646 case XorAssign: return "^=";
647 case OrAssign: return "|=";
648 case Comma: return ",";
649 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000650
651 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000652}
Steve Naroff47500512007-04-19 23:00:49 +0000653
Mike Stump11289f42009-09-09 15:08:12 +0000654BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000655BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
656 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000657 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000658 case OO_Plus: return Add;
659 case OO_Minus: return Sub;
660 case OO_Star: return Mul;
661 case OO_Slash: return Div;
662 case OO_Percent: return Rem;
663 case OO_Caret: return Xor;
664 case OO_Amp: return And;
665 case OO_Pipe: return Or;
666 case OO_Equal: return Assign;
667 case OO_Less: return LT;
668 case OO_Greater: return GT;
669 case OO_PlusEqual: return AddAssign;
670 case OO_MinusEqual: return SubAssign;
671 case OO_StarEqual: return MulAssign;
672 case OO_SlashEqual: return DivAssign;
673 case OO_PercentEqual: return RemAssign;
674 case OO_CaretEqual: return XorAssign;
675 case OO_AmpEqual: return AndAssign;
676 case OO_PipeEqual: return OrAssign;
677 case OO_LessLess: return Shl;
678 case OO_GreaterGreater: return Shr;
679 case OO_LessLessEqual: return ShlAssign;
680 case OO_GreaterGreaterEqual: return ShrAssign;
681 case OO_EqualEqual: return EQ;
682 case OO_ExclaimEqual: return NE;
683 case OO_LessEqual: return LE;
684 case OO_GreaterEqual: return GE;
685 case OO_AmpAmp: return LAnd;
686 case OO_PipePipe: return LOr;
687 case OO_Comma: return Comma;
688 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000689 }
690}
691
692OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
693 static const OverloadedOperatorKind OverOps[] = {
694 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
695 OO_Star, OO_Slash, OO_Percent,
696 OO_Plus, OO_Minus,
697 OO_LessLess, OO_GreaterGreater,
698 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
699 OO_EqualEqual, OO_ExclaimEqual,
700 OO_Amp,
701 OO_Caret,
702 OO_Pipe,
703 OO_AmpAmp,
704 OO_PipePipe,
705 OO_Equal, OO_StarEqual,
706 OO_SlashEqual, OO_PercentEqual,
707 OO_PlusEqual, OO_MinusEqual,
708 OO_LessLessEqual, OO_GreaterGreaterEqual,
709 OO_AmpEqual, OO_CaretEqual,
710 OO_PipeEqual,
711 OO_Comma
712 };
713 return OverOps[Opc];
714}
715
Ted Kremenek445a6032010-02-19 00:42:33 +0000716InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000717 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000718 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000719 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek445a6032010-02-19 00:42:33 +0000720 InitExprs(0), NumInits(numInits), Capacity(numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000721 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenek445a6032010-02-19 00:42:33 +0000722 UnionFieldInit(0), HadArrayRangeDesignator(false)
723{
724 if (NumInits == 0)
725 return;
726
727 InitExprs = new (C) Stmt*[Capacity];
728
729 for (unsigned I = 0; I != NumInits; ++I) {
730 Expr *Ex = initExprs[I];
731 if (Ex->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000732 TypeDependent = true;
Ted Kremenek445a6032010-02-19 00:42:33 +0000733 if (Ex->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000734 ValueDependent = true;
Ted Kremenek445a6032010-02-19 00:42:33 +0000735 InitExprs[I] = Ex;
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000736 }
Anders Carlsson4692db02007-08-31 04:56:16 +0000737}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000738
Ted Kremenek445a6032010-02-19 00:42:33 +0000739void InitListExpr::DoDestroy(ASTContext &C) {
740 DestroyChildren(C);
741 if (InitExprs)
742 C.Deallocate(InitExprs);
743 this->~InitListExpr();
744 C.Deallocate((void*) this);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000745}
746
Ted Kremenek445a6032010-02-19 00:42:33 +0000747void InitListExpr::reserveInits(ASTContext &C, unsigned newCapacity) {
748 if (newCapacity > Capacity) {
749 if (!Capacity)
750 Capacity = newCapacity;
751 else if ((Capacity *= 2) < newCapacity)
752 Capacity = newCapacity;
753
754 Stmt **newInits = new (C) Stmt*[Capacity];
755 if (InitExprs) {
756 memcpy(newInits, InitExprs, NumInits * sizeof(*InitExprs));
757 C.Deallocate(InitExprs);
758 }
759 InitExprs = newInits;
760 }
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000761}
762
Ted Kremenek445a6032010-02-19 00:42:33 +0000763void InitListExpr::resizeInits(ASTContext &C, unsigned N) {
764 // If the new number of expressions is less than the old one, destroy
765 // the expressions that are beyond the new size.
766 for (unsigned i = N, LastIdx = NumInits; i < LastIdx; ++i)
767 InitExprs[i]->Destroy(C);
768
769 // If we are expanding the number of expressions, reserve space.
770 reserveInits(C, N);
771
772 // If we are expanding the number of expressions, zero out beyond our
773 // current capacity.
774 for (unsigned i = NumInits; i < N; ++i)
775 InitExprs[i] = 0;
776
777 NumInits = N;
778}
779
780Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
781 if (Init >= NumInits) {
782 // Resize the number of initializers. This will adjust the amount
783 // of memory allocated as well as zero-pad the initializers.
784 resizeInits(C, Init+1);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000785 }
Mike Stump11289f42009-09-09 15:08:12 +0000786
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000787 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
788 InitExprs[Init] = expr;
789 return Result;
790}
791
Steve Naroff991e99d2008-09-04 15:31:07 +0000792/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000793///
794const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000795 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000796 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000797}
798
Mike Stump11289f42009-09-09 15:08:12 +0000799SourceLocation BlockExpr::getCaretLocation() const {
800 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000801}
Mike Stump11289f42009-09-09 15:08:12 +0000802const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000803 return TheBlock->getBody();
804}
Mike Stump11289f42009-09-09 15:08:12 +0000805Stmt *BlockExpr::getBody() {
806 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000807}
Steve Naroff415d3d52008-10-08 17:01:13 +0000808
809
Chris Lattner1ec5f562007-06-27 05:38:08 +0000810//===----------------------------------------------------------------------===//
811// Generic Expression Routines
812//===----------------------------------------------------------------------===//
813
Chris Lattner237f2752009-02-14 07:37:35 +0000814/// isUnusedResultAWarning - Return true if this immediate expression should
815/// be warned about if the result is unused. If so, fill in Loc and Ranges
816/// with location to warn on and the source range[s] to report with the
817/// warning.
818bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000819 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000820 // Don't warn if the expr is type dependent. The type could end up
821 // instantiating to void.
822 if (isTypeDependent())
823 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000824
Chris Lattner1ec5f562007-06-27 05:38:08 +0000825 switch (getStmtClass()) {
826 default:
Chris Lattner237f2752009-02-14 07:37:35 +0000827 Loc = getExprLoc();
828 R1 = getSourceRange();
829 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000830 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000831 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000832 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000833 case UnaryOperatorClass: {
834 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner1ec5f562007-06-27 05:38:08 +0000836 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000837 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000838 case UnaryOperator::PostInc:
839 case UnaryOperator::PostDec:
840 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000841 case UnaryOperator::PreDec: // ++/--
842 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000843 case UnaryOperator::Deref:
844 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000845 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000846 return false;
847 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000848 case UnaryOperator::Real:
849 case UnaryOperator::Imag:
850 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000851 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
852 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000853 return false;
854 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000855 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000856 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000857 }
Chris Lattner237f2752009-02-14 07:37:35 +0000858 Loc = UO->getOperatorLoc();
859 R1 = UO->getSubExpr()->getSourceRange();
860 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000861 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000862 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000863 const BinaryOperator *BO = cast<BinaryOperator>(this);
864 // Consider comma to have side effects if the LHS or RHS does.
John McCall1e3715a2010-02-16 04:10:53 +0000865 if (BO->getOpcode() == BinaryOperator::Comma) {
866 // ((foo = <blah>), 0) is an idiom for hiding the result (and
867 // lvalue-ness) of an assignment written in a macro.
868 if (IntegerLiteral *IE =
869 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
870 if (IE->getValue() == 0)
871 return false;
872
Mike Stump53f9ded2009-11-03 23:25:48 +0000873 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
874 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattner237f2752009-02-14 07:37:35 +0000877 if (BO->isAssignmentOp())
878 return false;
879 Loc = BO->getOperatorLoc();
880 R1 = BO->getLHS()->getSourceRange();
881 R2 = BO->getRHS()->getSourceRange();
882 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000883 }
Chris Lattner86928112007-08-25 02:00:02 +0000884 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000885 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000886
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000887 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000888 // The condition must be evaluated, but if either the LHS or RHS is a
889 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000890 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000891 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000892 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000893 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000894 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000895 }
896
Chris Lattnera44d1162007-06-27 05:58:59 +0000897 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000898 // If the base pointer or element is to a volatile pointer/field, accessing
899 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000900 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000901 return false;
902 Loc = cast<MemberExpr>(this)->getMemberLoc();
903 R1 = SourceRange(Loc, Loc);
904 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
905 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Chris Lattner1ec5f562007-06-27 05:38:08 +0000907 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000908 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000909 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000910 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000911 return false;
912 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
913 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
914 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
915 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000916
Chris Lattner1ec5f562007-06-27 05:38:08 +0000917 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000918 case CXXOperatorCallExprClass:
919 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000920 // If this is a direct call, get the callee.
921 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +0000922 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000923 // If the callee has attribute pure, const, or warn_unused_result, warn
924 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000925 //
926 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
927 // updated to match for QoI.
928 if (FD->getAttr<WarnUnusedResultAttr>() ||
929 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
930 Loc = CE->getCallee()->getLocStart();
931 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner1a6babf2009-10-13 04:53:48 +0000933 if (unsigned NumArgs = CE->getNumArgs())
934 R2 = SourceRange(CE->getArg(0)->getLocStart(),
935 CE->getArg(NumArgs-1)->getLocEnd());
936 return true;
937 }
Chris Lattner237f2752009-02-14 07:37:35 +0000938 }
939 return false;
940 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000941
942 case CXXTemporaryObjectExprClass:
943 case CXXConstructExprClass:
944 return false;
945
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000946 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000947 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000949 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000950#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000951 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000952 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000953 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000954 Loc = Ref->getLocation();
955 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
956 if (Ref->getBase())
957 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000958#else
959 Loc = getExprLoc();
960 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000961#endif
962 return true;
963 }
Chris Lattner944d3062008-07-26 19:51:01 +0000964 case StmtExprClass: {
965 // Statement exprs don't logically have side effects themselves, but are
966 // sometimes used in macros in ways that give them a type that is unused.
967 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
968 // however, if the result of the stmt expr is dead, we don't want to emit a
969 // warning.
970 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
971 if (!CS->body_empty())
972 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000973 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000974
Chris Lattner237f2752009-02-14 07:37:35 +0000975 Loc = cast<StmtExpr>(this)->getLParenLoc();
976 R1 = getSourceRange();
977 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000978 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000979 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000980 // If this is an explicit cast to void, allow it. People do this when they
981 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000982 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000983 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000984 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
985 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
986 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000987 case CXXFunctionalCastExprClass: {
988 const CastExpr *CE = cast<CastExpr>(this);
989
990 // If this is a cast to void or a constructor conversion, check the operand.
991 // Otherwise, the result of the cast is unused.
992 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
993 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000994 return (cast<CastExpr>(this)->getSubExpr()
995 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000996 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
997 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
998 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001001 case ImplicitCastExprClass:
1002 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001003 return (cast<ImplicitCastExpr>(this)
1004 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001005
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001006 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001007 return (cast<CXXDefaultArgExpr>(this)
1008 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001009
1010 case CXXNewExprClass:
1011 // FIXME: In theory, there might be new expressions that don't have side
1012 // effects (e.g. a placement new with an uninitialized POD).
1013 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001014 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001015 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001016 return (cast<CXXBindTemporaryExpr>(this)
1017 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001018 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001019 return (cast<CXXExprWithTemporaries>(this)
1020 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001021 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001022}
1023
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001024/// DeclCanBeLvalue - Determine whether the given declaration can be
1025/// an lvalue. This is a helper routine for isLvalue.
1026static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001027 // C++ [temp.param]p6:
1028 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001029 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001030 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1031 return NTTParm->getType()->isReferenceType();
1032
Douglas Gregor91f84212008-12-11 16:49:14 +00001033 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001034 // C++ 3.10p2: An lvalue refers to an object or function.
1035 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001036 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001037}
1038
Steve Naroff475cca02007-05-14 17:19:29 +00001039/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1040/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001041/// - name, where name must be a variable
1042/// - e[i]
1043/// - (e), where e must be an lvalue
1044/// - e.name, where e must be an lvalue
1045/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001046/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001047/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001048/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001049/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001050///
Chris Lattner67315442008-07-26 21:30:36 +00001051Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001052 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1053
1054 isLvalueResult Res = isLvalueInternal(Ctx);
1055 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1056 return Res;
1057
Douglas Gregor9a657932008-10-21 23:43:52 +00001058 // first, check the type (C99 6.3.2.1). Expressions with function
1059 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001060 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001061 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001062
Steve Naroff1018ea32008-02-10 01:39:04 +00001063 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001064 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001065 return LV_IncompleteVoidType;
1066
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001067 return LV_Valid;
1068}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001069
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001070// Check whether the expression can be sanely treated like an l-value
1071Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001072 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001073 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001074 case StringLiteralClass: // C99 6.5.1p4
1075 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001076 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001077 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001078 // For vectors, make sure base is an lvalue (i.e. not a function call).
1079 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001080 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001081 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001082 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001083 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1084 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001085 return LV_Valid;
1086 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001087 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001088 case BlockDeclRefExprClass: {
1089 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001090 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001091 return LV_Valid;
1092 break;
1093 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001094 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001095 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001096 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1097 NamedDecl *Member = m->getMemberDecl();
1098 // C++ [expr.ref]p4:
1099 // If E2 is declared to have type "reference to T", then E1.E2
1100 // is an lvalue.
1101 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1102 if (Value->getType()->isReferenceType())
1103 return LV_Valid;
1104
1105 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001106 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001107 return LV_Valid;
1108
1109 // -- If E2 is a non-static data member [...]. If E1 is an
1110 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001111 if (isa<FieldDecl>(Member)) {
1112 if (m->isArrow())
1113 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001114 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001115 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001116
1117 // -- If it refers to a static member function [...], then
1118 // E1.E2 is an lvalue.
1119 // -- Otherwise, if E1.E2 refers to a non-static member
1120 // function [...], then E1.E2 is not an lvalue.
1121 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1122 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1123
1124 // -- If E2 is a member enumerator [...], the expression E1.E2
1125 // is not an lvalue.
1126 if (isa<EnumConstantDecl>(Member))
1127 return LV_InvalidExpression;
1128
1129 // Not an lvalue.
1130 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001131 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001132
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001133 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001134 if (m->isArrow())
1135 return LV_Valid;
1136 Expr *BaseExp = m->getBase();
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001137 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass)
1138 return LV_SubObjCPropertySetting;
1139 return
1140 (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ?
1141 LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001142 }
Chris Lattner595db862007-10-30 22:53:42 +00001143 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001144 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001145 return LV_Valid; // C99 6.5.3p4
1146
1147 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001148 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1149 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001150 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001151
1152 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1153 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1154 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1155 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001156 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001157 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001158 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1159 return LV_Valid;
1160
1161 // If this is a conversion to a class temporary, make a note of
1162 // that.
1163 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1164 return LV_ClassTemporary;
1165
1166 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001167 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001168 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001169 case BinaryOperatorClass:
1170 case CompoundAssignOperatorClass: {
1171 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001172
1173 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1174 BinOp->getOpcode() == BinaryOperator::Comma)
1175 return BinOp->getRHS()->isLvalue(Ctx);
1176
Sebastian Redl112a97662009-02-07 00:15:38 +00001177 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001178 // The result of a .* expression is an lvalue only if its first operand is
1179 // an lvalue and its second operand is a pointer to data member.
1180 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001181 !BinOp->getType()->isFunctionType())
1182 return BinOp->getLHS()->isLvalue(Ctx);
1183
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001184 // The result of an ->* expression is an lvalue only if its second operand
1185 // is a pointer to data member.
1186 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1187 !BinOp->getType()->isFunctionType()) {
1188 QualType Ty = BinOp->getRHS()->getType();
1189 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1190 return LV_Valid;
1191 }
1192
Douglas Gregor58e008d2008-11-13 20:12:29 +00001193 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001194 return LV_InvalidExpression;
1195
Douglas Gregor58e008d2008-11-13 20:12:29 +00001196 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001197 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001198 // The result of an assignment operation [...] is an lvalue.
1199 return LV_Valid;
1200
1201
1202 // C99 6.5.16:
1203 // An assignment expression [...] is not an lvalue.
1204 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001205 }
Mike Stump11289f42009-09-09 15:08:12 +00001206 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001207 case CXXOperatorCallExprClass:
1208 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001209 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001210 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001211 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001212 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1213 if (ReturnType->isLValueReferenceType())
1214 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001215
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001216 // If the function is returning a class temporary, make a note of
1217 // that.
1218 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1219 return LV_ClassTemporary;
1220
Douglas Gregor6b754842008-10-28 00:22:11 +00001221 break;
1222 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001223 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001224 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001225 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001226 case ChooseExprClass:
1227 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001228 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001229 case ExtVectorElementExprClass:
1230 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001231 return LV_DuplicateVectorComponents;
1232 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001233 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1234 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001235 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1236 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001237 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001238 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001239 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001240 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001241 case UnresolvedLookupExprClass:
1242 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001243 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001244 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001245 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001246 case CXXFunctionalCastExprClass:
1247 case CXXStaticCastExprClass:
1248 case CXXDynamicCastExprClass:
1249 case CXXReinterpretCastExprClass:
1250 case CXXConstCastExprClass:
1251 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001252 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001253 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1254 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001255 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1256 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001257 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001258
1259 // If this is a conversion to a class temporary, make a note of
1260 // that.
1261 if (Ctx.getLangOptions().CPlusPlus &&
1262 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1263 return LV_ClassTemporary;
1264
Douglas Gregor6b754842008-10-28 00:22:11 +00001265 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001266 case CXXTypeidExprClass:
1267 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1268 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001269 case CXXBindTemporaryExprClass:
1270 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1271 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001272 case CXXBindReferenceExprClass:
1273 // Something that's bound to a reference is always an lvalue.
1274 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001275 case ConditionalOperatorClass: {
1276 // Complicated handling is only for C++.
1277 if (!Ctx.getLangOptions().CPlusPlus)
1278 return LV_InvalidExpression;
1279
1280 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1281 // everywhere there's an object converted to an rvalue. Also, any other
1282 // casts should be wrapped by ImplicitCastExprs. There's just the special
1283 // case involving throws to work out.
1284 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001285 Expr *True = Cond->getTrueExpr();
1286 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001287 // C++0x 5.16p2
1288 // If either the second or the third operand has type (cv) void, [...]
1289 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001290 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001291 return LV_InvalidExpression;
1292
1293 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001294 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001295 return LV_InvalidExpression;
1296
1297 // That's it.
1298 return LV_Valid;
1299 }
1300
Douglas Gregor5103eff2009-12-19 07:07:47 +00001301 case Expr::CXXExprWithTemporariesClass:
1302 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1303
1304 case Expr::ObjCMessageExprClass:
1305 if (const ObjCMethodDecl *Method
1306 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1307 if (Method->getResultType()->isLValueReferenceType())
1308 return LV_Valid;
1309 break;
1310
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001311 case Expr::CXXConstructExprClass:
1312 case Expr::CXXTemporaryObjectExprClass:
1313 case Expr::CXXZeroInitValueExprClass:
1314 return LV_ClassTemporary;
1315
Steve Naroff9358c712007-05-27 23:58:33 +00001316 default:
1317 break;
Steve Naroff47500512007-04-19 23:00:49 +00001318 }
Steve Naroff9358c712007-05-27 23:58:33 +00001319 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001320}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001321
Steve Naroff475cca02007-05-14 17:19:29 +00001322/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1323/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001324/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001325/// recursively, any member or element of all contained aggregates or unions)
1326/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001327Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001328Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001329 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001330
Steve Naroff9358c712007-05-27 23:58:33 +00001331 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001332 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001333 // C++ 3.10p11: Functions cannot be modified, but pointers to
1334 // functions can be modifiable.
1335 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1336 return MLV_NotObjectType;
1337 break;
1338
Chris Lattner1ec5f562007-06-27 05:38:08 +00001339 case LV_NotObjectType: return MLV_NotObjectType;
1340 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001341 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001342 case LV_InvalidExpression:
1343 // If the top level is a C-style cast, and the subexpression is a valid
1344 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1345 // GCC extension. We don't support it, but we want to produce good
1346 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001347 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1348 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1349 if (Loc)
1350 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001351 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001352 }
1353 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001354 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001355 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001356 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
1357 case LV_SubObjCPropertyGetterSetting:
1358 return MLV_SubObjCPropertyGetterSetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001359 case LV_ClassTemporary:
1360 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001361 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001362
1363 // The following is illegal:
1364 // void takeclosure(void (^C)(void));
1365 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1366 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001367 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001368 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1369 return MLV_NotBlockQualified;
1370 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001371
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001372 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001373 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001374 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1375 if (Expr->getSetterMethod() == 0)
1376 return MLV_NoSetterProperty;
1377 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001378
Chris Lattner7adf0762008-08-04 07:31:14 +00001379 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001380
Chris Lattner7adf0762008-08-04 07:31:14 +00001381 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001382 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001383 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001384 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001385 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001386 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001387
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001388 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001389 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001390 return MLV_ConstQualified;
1391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Mike Stump11289f42009-09-09 15:08:12 +00001393 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001394}
1395
Fariborz Jahanian07735332009-02-22 18:40:18 +00001396/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001397/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001398bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001399 switch (getStmtClass()) {
1400 default:
1401 return false;
1402 case ObjCIvarRefExprClass:
1403 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001404 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001405 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001406 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001407 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001408 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001409 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001410 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001411 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001412 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001413 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001414 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1415 if (VD->hasGlobalStorage())
1416 return true;
1417 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001418 // dereferencing to a pointer is always a gc'able candidate,
1419 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001420 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001421 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001422 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001423 return false;
1424 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001425 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001426 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001427 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001428 }
1429 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001430 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001431 }
1432}
Ted Kremenekfff70962008-01-17 16:57:34 +00001433Expr* Expr::IgnoreParens() {
1434 Expr* E = this;
1435 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1436 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001437
Ted Kremenekfff70962008-01-17 16:57:34 +00001438 return E;
1439}
1440
Chris Lattnerf2660962008-02-13 01:02:39 +00001441/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1442/// or CastExprs or ImplicitCastExprs, returning their operand.
1443Expr *Expr::IgnoreParenCasts() {
1444 Expr *E = this;
1445 while (true) {
1446 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1447 E = P->getSubExpr();
1448 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1449 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001450 else
1451 return E;
1452 }
1453}
1454
Chris Lattneref26c772009-03-13 17:28:01 +00001455/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1456/// value (including ptr->int casts of the same size). Strip off any
1457/// ParenExpr or CastExprs, returning their operand.
1458Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1459 Expr *E = this;
1460 while (true) {
1461 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1462 E = P->getSubExpr();
1463 continue;
1464 }
Mike Stump11289f42009-09-09 15:08:12 +00001465
Chris Lattneref26c772009-03-13 17:28:01 +00001466 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1467 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1468 // ptr<->int casts of the same width. We also ignore all identify casts.
1469 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001470
Chris Lattneref26c772009-03-13 17:28:01 +00001471 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1472 E = SE;
1473 continue;
1474 }
Mike Stump11289f42009-09-09 15:08:12 +00001475
Chris Lattneref26c772009-03-13 17:28:01 +00001476 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1477 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1478 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1479 E = SE;
1480 continue;
1481 }
1482 }
Mike Stump11289f42009-09-09 15:08:12 +00001483
Chris Lattneref26c772009-03-13 17:28:01 +00001484 return E;
1485 }
1486}
1487
Douglas Gregord196a582009-12-14 19:27:10 +00001488bool Expr::isDefaultArgument() const {
1489 const Expr *E = this;
1490 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1491 E = ICE->getSubExprAsWritten();
1492
1493 return isa<CXXDefaultArgExpr>(E);
1494}
Chris Lattneref26c772009-03-13 17:28:01 +00001495
Douglas Gregor4619e432008-12-05 23:32:09 +00001496/// hasAnyTypeDependentArguments - Determines if any of the expressions
1497/// in Exprs is type-dependent.
1498bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1499 for (unsigned I = 0; I < NumExprs; ++I)
1500 if (Exprs[I]->isTypeDependent())
1501 return true;
1502
1503 return false;
1504}
1505
1506/// hasAnyValueDependentArguments - Determines if any of the expressions
1507/// in Exprs is value-dependent.
1508bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1509 for (unsigned I = 0; I < NumExprs; ++I)
1510 if (Exprs[I]->isValueDependent())
1511 return true;
1512
1513 return false;
1514}
1515
Eli Friedman7139af42009-01-25 02:32:41 +00001516bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001517 // This function is attempting whether an expression is an initializer
1518 // which can be evaluated at compile-time. isEvaluatable handles most
1519 // of the cases, but it can't deal with some initializer-specific
1520 // expressions, and it can't deal with aggregates; we deal with those here,
1521 // and fall back to isEvaluatable for the other cases.
1522
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001523 // FIXME: This function assumes the variable being assigned to
1524 // isn't a reference type!
1525
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001526 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001527 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001528 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001529 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001530 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001531 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001532 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001533 // This handles gcc's extension that allows global initializers like
1534 // "struct x {int x;} x = (struct x) {};".
1535 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001536 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001537 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001538 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001539 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001540 // FIXME: This doesn't deal with fields with reference types correctly.
1541 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1542 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001543 const InitListExpr *Exp = cast<InitListExpr>(this);
1544 unsigned numInits = Exp->getNumInits();
1545 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001546 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001547 return false;
1548 }
Eli Friedman384da272009-01-25 03:12:18 +00001549 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001550 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001551 case ImplicitValueInitExprClass:
1552 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001553 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001554 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001555 case UnaryOperatorClass: {
1556 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1557 if (Exp->getOpcode() == UnaryOperator::Extension)
1558 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1559 break;
1560 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001561 case BinaryOperatorClass: {
1562 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1563 // but this handles the common case.
1564 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1565 if (Exp->getOpcode() == BinaryOperator::Sub &&
1566 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1567 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1568 return true;
1569 break;
1570 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001571 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001572 case CStyleCastExprClass:
1573 // Handle casts with a destination that's a struct or union; this
1574 // deals with both the gcc no-op struct cast extension and the
1575 // cast-to-union extension.
1576 if (getType()->isRecordType())
1577 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001578
1579 // Integer->integer casts can be handled here, which is important for
1580 // things like (int)(&&x-&&y). Scary but true.
1581 if (getType()->isIntegerType() &&
1582 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1583 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1584
Eli Friedman384da272009-01-25 03:12:18 +00001585 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001586 }
Eli Friedman384da272009-01-25 03:12:18 +00001587 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001588}
1589
Chris Lattner1f4479e2007-06-05 04:15:44 +00001590/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001591/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001592
1593/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1594/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001595///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001596/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1597/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1598/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001599
Eli Friedman98c56a42009-02-26 09:29:13 +00001600// CheckICE - This function does the fundamental ICE checking: the returned
1601// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1602// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001603// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001604// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001605// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001606//
1607// Meanings of Val:
1608// 0: This expression is an ICE if it can be evaluated by Evaluate.
1609// 1: This expression is not an ICE, but if it isn't evaluated, it's
1610// a legal subexpression for an ICE. This return value is used to handle
1611// the comma operator in C99 mode.
1612// 2: This expression is not an ICE, and is not a legal subexpression for one.
1613
1614struct ICEDiag {
1615 unsigned Val;
1616 SourceLocation Loc;
1617
1618 public:
1619 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1620 ICEDiag() : Val(0) {}
1621};
1622
1623ICEDiag NoDiag() { return ICEDiag(); }
1624
Eli Friedman90afd3d2009-02-27 04:07:58 +00001625static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1626 Expr::EvalResult EVResult;
1627 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1628 !EVResult.Val.isInt()) {
1629 return ICEDiag(2, E->getLocStart());
1630 }
1631 return NoDiag();
1632}
1633
Eli Friedman98c56a42009-02-26 09:29:13 +00001634static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001635 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001636 if (!E->getType()->isIntegralType()) {
1637 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001638 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001639
1640 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001641#define STMT(Node, Base) case Expr::Node##Class:
1642#define EXPR(Node, Base)
1643#include "clang/AST/StmtNodes.def"
1644 case Expr::PredefinedExprClass:
1645 case Expr::FloatingLiteralClass:
1646 case Expr::ImaginaryLiteralClass:
1647 case Expr::StringLiteralClass:
1648 case Expr::ArraySubscriptExprClass:
1649 case Expr::MemberExprClass:
1650 case Expr::CompoundAssignOperatorClass:
1651 case Expr::CompoundLiteralExprClass:
1652 case Expr::ExtVectorElementExprClass:
1653 case Expr::InitListExprClass:
1654 case Expr::DesignatedInitExprClass:
1655 case Expr::ImplicitValueInitExprClass:
1656 case Expr::ParenListExprClass:
1657 case Expr::VAArgExprClass:
1658 case Expr::AddrLabelExprClass:
1659 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001660 case Expr::CXXMemberCallExprClass:
1661 case Expr::CXXDynamicCastExprClass:
1662 case Expr::CXXTypeidExprClass:
1663 case Expr::CXXNullPtrLiteralExprClass:
1664 case Expr::CXXThisExprClass:
1665 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001666 case Expr::CXXNewExprClass:
1667 case Expr::CXXDeleteExprClass:
1668 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001669 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001670 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001671 case Expr::CXXConstructExprClass:
1672 case Expr::CXXBindTemporaryExprClass:
Anders Carlssonba6c4372010-01-29 02:39:32 +00001673 case Expr::CXXBindReferenceExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001674 case Expr::CXXExprWithTemporariesClass:
1675 case Expr::CXXTemporaryObjectExprClass:
1676 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001677 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001678 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001679 case Expr::ObjCStringLiteralClass:
1680 case Expr::ObjCEncodeExprClass:
1681 case Expr::ObjCMessageExprClass:
1682 case Expr::ObjCSelectorExprClass:
1683 case Expr::ObjCProtocolExprClass:
1684 case Expr::ObjCIvarRefExprClass:
1685 case Expr::ObjCPropertyRefExprClass:
1686 case Expr::ObjCImplicitSetterGetterRefExprClass:
1687 case Expr::ObjCSuperExprClass:
1688 case Expr::ObjCIsaExprClass:
1689 case Expr::ShuffleVectorExprClass:
1690 case Expr::BlockExprClass:
1691 case Expr::BlockDeclRefExprClass:
1692 case Expr::NoStmtClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001693 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001694
Douglas Gregor73341c42009-09-11 00:18:58 +00001695 case Expr::GNUNullExprClass:
1696 // GCC considers the GNU __null value to be an integral constant expression.
1697 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001698
Eli Friedman98c56a42009-02-26 09:29:13 +00001699 case Expr::ParenExprClass:
1700 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1701 case Expr::IntegerLiteralClass:
1702 case Expr::CharacterLiteralClass:
1703 case Expr::CXXBoolLiteralExprClass:
1704 case Expr::CXXZeroInitValueExprClass:
1705 case Expr::TypesCompatibleExprClass:
1706 case Expr::UnaryTypeTraitExprClass:
1707 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001708 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001709 case Expr::CXXOperatorCallExprClass: {
1710 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001711 if (CE->isBuiltinCall(Ctx))
1712 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001713 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001714 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001715 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001716 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1717 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001718 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001719 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001720 // C++ 7.1.5.1p2
1721 // A variable of non-volatile const-qualified integral or enumeration
1722 // type initialized by an ICE can be used in ICEs.
1723 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001724 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001725 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1726 if (Quals.hasVolatile() || !Quals.hasConst())
1727 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1728
Sebastian Redl5ca79842010-02-01 20:16:42 +00001729 // Look for a declaration of this variable that has an initializer.
1730 const VarDecl *ID = 0;
1731 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001732 if (Init) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001733 if (ID->isInitKnownICE()) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001734 // We have already checked whether this subexpression is an
1735 // integral constant expression.
Sebastian Redl5ca79842010-02-01 20:16:42 +00001736 if (ID->isInitICE())
Douglas Gregor0840cc02009-11-01 20:32:48 +00001737 return NoDiag();
1738 else
1739 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1740 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001741
John McCall52cc0892010-02-06 01:07:37 +00001742 // It's an ICE whether or not the definition we found is
1743 // out-of-line. See DR 721 and the discussion in Clang PR
1744 // 6206 for details.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001745
1746 if (Dcl->isCheckingICE()) {
1747 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1748 }
1749
1750 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001751 ICEDiag Result = CheckICE(Init, Ctx);
1752 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001753 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001754 return Result;
1755 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001756 }
1757 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001758 return ICEDiag(2, E->getLocStart());
1759 case Expr::UnaryOperatorClass: {
1760 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001761 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001762 case UnaryOperator::PostInc:
1763 case UnaryOperator::PostDec:
1764 case UnaryOperator::PreInc:
1765 case UnaryOperator::PreDec:
1766 case UnaryOperator::AddrOf:
1767 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001768 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001769
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001770 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001771 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001772 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001773 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001774 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001775 case UnaryOperator::Real:
1776 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001777 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001778 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001779 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1780 // Evaluate matches the proposed gcc behavior for cases like
1781 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1782 // compliance: we should warn earlier for offsetof expressions with
1783 // array subscripts that aren't ICEs, and if the array subscripts
1784 // are ICEs, the value of the offsetof must be an integer constant.
1785 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001786 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001787 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001788 case Expr::SizeOfAlignOfExprClass: {
1789 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1790 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1791 return ICEDiag(2, E->getLocStart());
1792 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001793 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001794 case Expr::BinaryOperatorClass: {
1795 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001796 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001797 case BinaryOperator::PtrMemD:
1798 case BinaryOperator::PtrMemI:
1799 case BinaryOperator::Assign:
1800 case BinaryOperator::MulAssign:
1801 case BinaryOperator::DivAssign:
1802 case BinaryOperator::RemAssign:
1803 case BinaryOperator::AddAssign:
1804 case BinaryOperator::SubAssign:
1805 case BinaryOperator::ShlAssign:
1806 case BinaryOperator::ShrAssign:
1807 case BinaryOperator::AndAssign:
1808 case BinaryOperator::XorAssign:
1809 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001810 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001811
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001812 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001813 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001814 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001815 case BinaryOperator::Add:
1816 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001817 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001818 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001819 case BinaryOperator::LT:
1820 case BinaryOperator::GT:
1821 case BinaryOperator::LE:
1822 case BinaryOperator::GE:
1823 case BinaryOperator::EQ:
1824 case BinaryOperator::NE:
1825 case BinaryOperator::And:
1826 case BinaryOperator::Xor:
1827 case BinaryOperator::Or:
1828 case BinaryOperator::Comma: {
1829 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1830 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001831 if (Exp->getOpcode() == BinaryOperator::Div ||
1832 Exp->getOpcode() == BinaryOperator::Rem) {
1833 // Evaluate gives an error for undefined Div/Rem, so make sure
1834 // we don't evaluate one.
1835 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1836 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1837 if (REval == 0)
1838 return ICEDiag(1, E->getLocStart());
1839 if (REval.isSigned() && REval.isAllOnesValue()) {
1840 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1841 if (LEval.isMinSignedValue())
1842 return ICEDiag(1, E->getLocStart());
1843 }
1844 }
1845 }
1846 if (Exp->getOpcode() == BinaryOperator::Comma) {
1847 if (Ctx.getLangOptions().C99) {
1848 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1849 // if it isn't evaluated.
1850 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1851 return ICEDiag(1, E->getLocStart());
1852 } else {
1853 // In both C89 and C++, commas in ICEs are illegal.
1854 return ICEDiag(2, E->getLocStart());
1855 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001856 }
1857 if (LHSResult.Val >= RHSResult.Val)
1858 return LHSResult;
1859 return RHSResult;
1860 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001861 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001862 case BinaryOperator::LOr: {
1863 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1864 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1865 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1866 // Rare case where the RHS has a comma "side-effect"; we need
1867 // to actually check the condition to see whether the side
1868 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001869 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001870 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001871 return RHSResult;
1872 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001873 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001874
Eli Friedman98c56a42009-02-26 09:29:13 +00001875 if (LHSResult.Val >= RHSResult.Val)
1876 return LHSResult;
1877 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001878 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001879 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001880 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001881 case Expr::ImplicitCastExprClass:
1882 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001883 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001884 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001885 case Expr::CXXStaticCastExprClass:
1886 case Expr::CXXReinterpretCastExprClass:
1887 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001888 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1889 if (SubExpr->getType()->isIntegralType())
1890 return CheckICE(SubExpr, Ctx);
1891 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1892 return NoDiag();
1893 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001894 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001895 case Expr::ConditionalOperatorClass: {
1896 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001897 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001898 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001899 // expression, and it is fully evaluated. This is an important GNU
1900 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001901 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001902 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001903 Expr::EvalResult EVResult;
1904 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1905 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001906 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001907 }
1908 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001909 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001910 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1911 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1912 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1913 if (CondResult.Val == 2)
1914 return CondResult;
1915 if (TrueResult.Val == 2)
1916 return TrueResult;
1917 if (FalseResult.Val == 2)
1918 return FalseResult;
1919 if (CondResult.Val == 1)
1920 return CondResult;
1921 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1922 return NoDiag();
1923 // Rare case where the diagnostics depend on which side is evaluated
1924 // Note that if we get here, CondResult is 0, and at least one of
1925 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001926 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001927 return FalseResult;
1928 }
1929 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001930 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001931 case Expr::CXXDefaultArgExprClass:
1932 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001933 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001934 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001935 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001936 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001937
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001938 // Silence a GCC warning
1939 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001940}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001941
Eli Friedman98c56a42009-02-26 09:29:13 +00001942bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1943 SourceLocation *Loc, bool isEvaluated) const {
1944 ICEDiag d = CheckICE(this, Ctx);
1945 if (d.Val != 0) {
1946 if (Loc) *Loc = d.Loc;
1947 return false;
1948 }
1949 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001950 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001951 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001952 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1953 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001954 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001955 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001956}
1957
Chris Lattner7eef9192007-05-24 01:23:49 +00001958/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1959/// integer constant expression with the value zero, or if this is one that is
1960/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001961bool Expr::isNullPointerConstant(ASTContext &Ctx,
1962 NullPointerConstantValueDependence NPC) const {
1963 if (isValueDependent()) {
1964 switch (NPC) {
1965 case NPC_NeverValueDependent:
1966 assert(false && "Unexpected value dependent expression!");
1967 // If the unthinkable happens, fall through to the safest alternative.
1968
1969 case NPC_ValueDependentIsNull:
1970 return isTypeDependent() || getType()->isIntegralType();
1971
1972 case NPC_ValueDependentIsNotNull:
1973 return false;
1974 }
1975 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001976
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001977 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001978 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001979 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001980 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001981 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001982 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001983 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001984 Pointee->isVoidType() && // to void*
1985 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001986 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001987 }
Steve Naroffada7d422007-05-20 17:54:12 +00001988 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001989 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1990 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001991 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001992 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1993 // Accept ((void*)0) as a null pointer constant, as many other
1994 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001995 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001996 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001997 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001998 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001999 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002000 } else if (isa<GNUNullExpr>(this)) {
2001 // The GNU __null extension is always a null pointer constant.
2002 return true;
Steve Naroff09035312008-01-14 02:53:34 +00002003 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002004
Sebastian Redl576fd422009-05-10 18:38:11 +00002005 // C++0x nullptr_t is always a null pointer constant.
2006 if (getType()->isNullPtrType())
2007 return true;
2008
Steve Naroff4871fe02008-01-14 16:10:57 +00002009 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002010 if (!getType()->isIntegerType() ||
2011 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00002012 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002013
Chris Lattner1abbd412007-06-08 17:58:43 +00002014 // If we have an integer constant expression, we need to *evaluate* it and
2015 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002016 llvm::APSInt Result;
2017 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002018}
Steve Narofff7a5da12007-07-28 23:10:27 +00002019
Douglas Gregor71235ec2009-05-02 02:18:30 +00002020FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002021 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002022
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002023 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2024 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2025 E = ICE->getSubExpr()->IgnoreParens();
2026 else
2027 break;
2028 }
2029
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002030 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002031 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002032 if (Field->isBitField())
2033 return Field;
2034
2035 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2036 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2037 return BinOp->getLHS()->getBitField();
2038
2039 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002040}
2041
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002042bool Expr::refersToVectorElement() const {
2043 const Expr *E = this->IgnoreParens();
2044
2045 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2046 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2047 E = ICE->getSubExpr()->IgnoreParens();
2048 else
2049 break;
2050 }
2051
2052 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2053 return ASE->getBase()->getType()->isVectorType();
2054
2055 if (isa<ExtVectorElementExpr>(E))
2056 return true;
2057
2058 return false;
2059}
2060
Chris Lattnerb8211f62009-02-16 22:14:05 +00002061/// isArrow - Return true if the base expression is a pointer to vector,
2062/// return false if the base expression is a vector.
2063bool ExtVectorElementExpr::isArrow() const {
2064 return getBase()->getType()->isPointerType();
2065}
2066
Nate Begemance4d7fc2008-04-18 23:10:10 +00002067unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002068 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002069 return VT->getNumElements();
2070 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002071}
2072
Nate Begemanf322eab2008-05-09 06:41:27 +00002073/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002074bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002075 // FIXME: Refactor this code to an accessor on the AST node which returns the
2076 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002077 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002078
2079 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002080 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002081 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002082
Nate Begeman7e5185b2009-01-18 02:01:21 +00002083 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002084 if (Comp[0] == 's' || Comp[0] == 'S')
2085 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002086
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002087 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2088 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002089 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002090
Steve Naroff0d595ca2007-07-30 03:29:09 +00002091 return false;
2092}
Chris Lattner885b4952007-08-02 23:36:59 +00002093
Nate Begemanf322eab2008-05-09 06:41:27 +00002094/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002095void ExtVectorElementExpr::getEncodedElementAccess(
2096 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002097 llvm::StringRef Comp = Accessor->getName();
2098 if (Comp[0] == 's' || Comp[0] == 'S')
2099 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002100
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002101 bool isHi = Comp == "hi";
2102 bool isLo = Comp == "lo";
2103 bool isEven = Comp == "even";
2104 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002105
Nate Begemanf322eab2008-05-09 06:41:27 +00002106 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2107 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002108
Nate Begemanf322eab2008-05-09 06:41:27 +00002109 if (isHi)
2110 Index = e + i;
2111 else if (isLo)
2112 Index = i;
2113 else if (isEven)
2114 Index = 2 * i;
2115 else if (isOdd)
2116 Index = 2 * i + 1;
2117 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002118 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002119
Nate Begemand3862152008-05-13 21:03:02 +00002120 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002121 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002122}
2123
Steve Narofff73590d2007-09-27 14:38:14 +00002124// constructor for instance messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002125ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2126 Selector selInfo,
2127 QualType retType, ObjCMethodDecl *mproto,
2128 SourceLocation LBrac, SourceLocation RBrac,
2129 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002130 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002131 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002132 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002133 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002134 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002135 if (NumArgs) {
2136 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002137 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2138 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002139 LBracloc = LBrac;
2140 RBracloc = RBrac;
2141}
2142
Mike Stump11289f42009-09-09 15:08:12 +00002143// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002144// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenek2c809302010-02-11 22:41:21 +00002145ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
2146 Selector selInfo, QualType retType,
2147 ObjCMethodDecl *mproto,
2148 SourceLocation LBrac, SourceLocation RBrac,
2149 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002150 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002151 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002152 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002153 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002154 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002155 if (NumArgs) {
2156 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002157 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2158 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002159 LBracloc = LBrac;
2160 RBracloc = RBrac;
2161}
2162
Mike Stump11289f42009-09-09 15:08:12 +00002163// constructor for class messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002164ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
2165 Selector selInfo, QualType retType,
2166 ObjCMethodDecl *mproto, SourceLocation LBrac,
2167 SourceLocation RBrac, Expr **ArgExprs,
2168 unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002169: Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002170MethodProto(mproto) {
2171 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002172 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002173 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2174 if (NumArgs) {
2175 for (unsigned i = 0; i != NumArgs; ++i)
2176 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2177 }
2178 LBracloc = LBrac;
2179 RBracloc = RBrac;
2180}
2181
2182ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2183 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2184 switch (x & Flags) {
2185 default:
2186 assert(false && "Invalid ObjCMessageExpr.");
2187 case IsInstMeth:
2188 return ClassInfo(0, 0);
2189 case IsClsMethDeclUnknown:
2190 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2191 case IsClsMethDeclKnown: {
2192 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2193 return ClassInfo(D, D->getIdentifier());
2194 }
2195 }
2196}
2197
Chris Lattner7ec71da2009-04-26 00:44:05 +00002198void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2199 if (CI.first == 0 && CI.second == 0)
2200 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2201 else if (CI.first == 0)
2202 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2203 else
2204 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2205}
2206
Ted Kremenek2c809302010-02-11 22:41:21 +00002207void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2208 DestroyChildren(C);
2209 if (SubExprs)
2210 C.Deallocate(SubExprs);
2211 this->~ObjCMessageExpr();
2212 C.Deallocate((void*) this);
2213}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002214
Chris Lattner35e564e2007-10-25 00:29:32 +00002215bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002216 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002217}
2218
Nate Begeman48745922009-08-12 02:28:50 +00002219void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2220 unsigned NumExprs) {
2221 if (SubExprs) C.Deallocate(SubExprs);
2222
2223 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002224 this->NumExprs = NumExprs;
2225 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002226}
Nate Begeman48745922009-08-12 02:28:50 +00002227
2228void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2229 DestroyChildren(C);
2230 if (SubExprs) C.Deallocate(SubExprs);
2231 this->~ShuffleVectorExpr();
2232 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002233}
2234
Douglas Gregore26a2852009-08-07 06:08:38 +00002235void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002236 // Override default behavior of traversing children. If this has a type
2237 // operand and the type is a variable-length array, the child iteration
2238 // will iterate over the size expression. However, this expression belongs
2239 // to the type, not to this, so we don't want to delete it.
2240 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002241 if (isArgumentType()) {
2242 this->~SizeOfAlignOfExpr();
2243 C.Deallocate(this);
2244 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002245 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002246 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002247}
2248
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002249//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002250// DesignatedInitExpr
2251//===----------------------------------------------------------------------===//
2252
2253IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2254 assert(Kind == FieldDesignator && "Only valid on a field designator");
2255 if (Field.NameOrField & 0x01)
2256 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2257 else
2258 return getField()->getIdentifier();
2259}
2260
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002261DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2262 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002263 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002264 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002265 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002266 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002267 unsigned NumIndexExprs,
2268 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002269 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002270 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002271 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2272 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002273 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002274
2275 // Record the initializer itself.
2276 child_iterator Child = child_begin();
2277 *Child++ = Init;
2278
2279 // Copy the designators and their subexpressions, computing
2280 // value-dependence along the way.
2281 unsigned IndexIdx = 0;
2282 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002283 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002284
2285 if (this->Designators[I].isArrayDesignator()) {
2286 // Compute type- and value-dependence.
2287 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002288 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002289 Index->isTypeDependent() || Index->isValueDependent();
2290
2291 // Copy the index expressions into permanent storage.
2292 *Child++ = IndexExprs[IndexIdx++];
2293 } else if (this->Designators[I].isArrayRangeDesignator()) {
2294 // Compute type- and value-dependence.
2295 Expr *Start = IndexExprs[IndexIdx];
2296 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002297 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002298 Start->isTypeDependent() || Start->isValueDependent() ||
2299 End->isTypeDependent() || End->isValueDependent();
2300
2301 // Copy the start/end expressions into permanent storage.
2302 *Child++ = IndexExprs[IndexIdx++];
2303 *Child++ = IndexExprs[IndexIdx++];
2304 }
2305 }
2306
2307 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002308}
2309
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002310DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002311DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002312 unsigned NumDesignators,
2313 Expr **IndexExprs, unsigned NumIndexExprs,
2314 SourceLocation ColonOrEqualLoc,
2315 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002316 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002317 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002318 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002319 ColonOrEqualLoc, UsesColonSyntax,
2320 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002321}
2322
Mike Stump11289f42009-09-09 15:08:12 +00002323DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002324 unsigned NumIndexExprs) {
2325 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2326 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2327 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2328}
2329
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002330void DesignatedInitExpr::setDesignators(ASTContext &C,
2331 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002332 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002333 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002334
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002335 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002336 NumDesignators = NumDesigs;
2337 for (unsigned I = 0; I != NumDesigs; ++I)
2338 Designators[I] = Desigs[I];
2339}
2340
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002341SourceRange DesignatedInitExpr::getSourceRange() const {
2342 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002343 Designator &First =
2344 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002345 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002346 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002347 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2348 else
2349 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2350 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002351 StartLoc =
2352 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002353 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2354}
2355
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002356Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2357 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2358 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2359 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002360 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2361 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2362}
2363
2364Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002365 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002366 "Requires array range designator");
2367 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2368 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002369 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2370 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2371}
2372
2373Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002374 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002375 "Requires array range designator");
2376 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2377 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002378 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2379 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2380}
2381
Douglas Gregord5846a12009-04-15 06:41:24 +00002382/// \brief Replaces the designator at index @p Idx with the series
2383/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002384void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002385 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002386 const Designator *Last) {
2387 unsigned NumNewDesignators = Last - First;
2388 if (NumNewDesignators == 0) {
2389 std::copy_backward(Designators + Idx + 1,
2390 Designators + NumDesignators,
2391 Designators + Idx);
2392 --NumNewDesignators;
2393 return;
2394 } else if (NumNewDesignators == 1) {
2395 Designators[Idx] = *First;
2396 return;
2397 }
2398
Mike Stump11289f42009-09-09 15:08:12 +00002399 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002400 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002401 std::copy(Designators, Designators + Idx, NewDesignators);
2402 std::copy(First, Last, NewDesignators + Idx);
2403 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2404 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002405 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002406 Designators = NewDesignators;
2407 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2408}
2409
Douglas Gregore26a2852009-08-07 06:08:38 +00002410void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002411 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002412 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002413}
2414
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002415void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2416 for (unsigned I = 0; I != NumDesignators; ++I)
2417 Designators[I].~Designator();
2418 C.Deallocate(Designators);
2419 Designators = 0;
2420}
2421
Mike Stump11289f42009-09-09 15:08:12 +00002422ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002423 Expr **exprs, unsigned nexprs,
2424 SourceLocation rparenloc)
2425: Expr(ParenListExprClass, QualType(),
2426 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002427 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002428 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002429
Nate Begeman5ec4b312009-08-10 23:49:36 +00002430 Exprs = new (C) Stmt*[nexprs];
2431 for (unsigned i = 0; i != nexprs; ++i)
2432 Exprs[i] = exprs[i];
2433}
2434
2435void ParenListExpr::DoDestroy(ASTContext& C) {
2436 DestroyChildren(C);
2437 if (Exprs) C.Deallocate(Exprs);
2438 this->~ParenListExpr();
2439 C.Deallocate(this);
2440}
2441
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002442//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002443// ExprIterator.
2444//===----------------------------------------------------------------------===//
2445
2446Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2447Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2448Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2449const Expr* ConstExprIterator::operator[](size_t idx) const {
2450 return cast<Expr>(I[idx]);
2451}
2452const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2453const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2454
2455//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002456// Child Iterators for iterating over subexpressions/substatements
2457//===----------------------------------------------------------------------===//
2458
2459// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002460Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2461Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002462
Steve Naroffe46504b2007-11-12 14:29:37 +00002463// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002464Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2465Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002466
Steve Naroffebf4cb42008-06-02 23:03:37 +00002467// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002468Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2469Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002470
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002471// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002472Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2473 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002474}
Mike Stump11289f42009-09-09 15:08:12 +00002475Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2476 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002477}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002478
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002479// ObjCSuperExpr
2480Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2481Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2482
Steve Naroffe87026a2009-07-24 17:54:45 +00002483// ObjCIsaExpr
2484Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2485Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2486
Chris Lattner6307f192008-08-10 01:53:14 +00002487// PredefinedExpr
2488Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2489Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002490
2491// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002492Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2493Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002494
2495// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002496Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002497Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002498
2499// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002500Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2501Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002502
Chris Lattner1c20a172007-08-26 03:42:43 +00002503// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002504Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2505Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002506
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002507// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002508Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2509Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002510
2511// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002512Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2513Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002514
2515// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002516Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2517Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002518
Sebastian Redl6f282892008-11-11 17:56:53 +00002519// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002520Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002521 // If this is of a type and the type is a VLA type (and not a typedef), the
2522 // size expression of the VLA needs to be treated as an executable expression.
2523 // Why isn't this weirdness documented better in StmtIterator?
2524 if (isArgumentType()) {
2525 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2526 getArgumentType().getTypePtr()))
2527 return child_iterator(T);
2528 return child_iterator();
2529 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002530 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002531}
Sebastian Redl6f282892008-11-11 17:56:53 +00002532Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2533 if (isArgumentType())
2534 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002535 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002536}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002537
2538// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002539Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002540 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002541}
Ted Kremenek23702b62007-08-24 20:06:47 +00002542Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002543 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002544}
2545
2546// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002547Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002548 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002549}
Ted Kremenek23702b62007-08-24 20:06:47 +00002550Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002551 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002552}
Ted Kremenek23702b62007-08-24 20:06:47 +00002553
2554// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002555Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2556Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002557
Nate Begemance4d7fc2008-04-18 23:10:10 +00002558// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002559Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2560Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002561
2562// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002563Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2564Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002565
Ted Kremenek23702b62007-08-24 20:06:47 +00002566// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002567Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2568Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002569
2570// BinaryOperator
2571Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002572 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002573}
Ted Kremenek23702b62007-08-24 20:06:47 +00002574Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002575 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002576}
2577
2578// ConditionalOperator
2579Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002580 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002581}
Ted Kremenek23702b62007-08-24 20:06:47 +00002582Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002583 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002584}
2585
2586// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002587Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2588Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002589
Ted Kremenek23702b62007-08-24 20:06:47 +00002590// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002591Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2592Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002593
2594// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002595Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2596 return child_iterator();
2597}
2598
2599Stmt::child_iterator TypesCompatibleExpr::child_end() {
2600 return child_iterator();
2601}
Ted Kremenek23702b62007-08-24 20:06:47 +00002602
2603// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002604Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2605Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002606
Douglas Gregor3be4b122008-11-29 04:51:27 +00002607// GNUNullExpr
2608Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2609Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2610
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002611// ShuffleVectorExpr
2612Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002613 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002614}
2615Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002616 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002617}
2618
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002619// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002620Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2621Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002622
Anders Carlsson4692db02007-08-31 04:56:16 +00002623// InitListExpr
Ted Kremenek445a6032010-02-19 00:42:33 +00002624Stmt::child_iterator InitListExpr::child_begin() { return begin(); }
2625Stmt::child_iterator InitListExpr::child_end() { return end(); }
Anders Carlsson4692db02007-08-31 04:56:16 +00002626
Douglas Gregor0202cb42009-01-29 17:44:32 +00002627// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002628Stmt::child_iterator DesignatedInitExpr::child_begin() {
2629 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2630 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002631 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2632}
2633Stmt::child_iterator DesignatedInitExpr::child_end() {
2634 return child_iterator(&*child_begin() + NumSubExprs);
2635}
2636
Douglas Gregor0202cb42009-01-29 17:44:32 +00002637// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002638Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2639 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002640}
2641
Mike Stump11289f42009-09-09 15:08:12 +00002642Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2643 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002644}
2645
Nate Begeman5ec4b312009-08-10 23:49:36 +00002646// ParenListExpr
2647Stmt::child_iterator ParenListExpr::child_begin() {
2648 return &Exprs[0];
2649}
2650Stmt::child_iterator ParenListExpr::child_end() {
2651 return &Exprs[0]+NumExprs;
2652}
2653
Ted Kremenek23702b62007-08-24 20:06:47 +00002654// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002655Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002656 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002657}
2658Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002659 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002660}
Ted Kremenek23702b62007-08-24 20:06:47 +00002661
2662// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002663Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2664Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002665
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002666// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002667Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002668 return child_iterator();
2669}
2670Stmt::child_iterator ObjCSelectorExpr::child_end() {
2671 return child_iterator();
2672}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002673
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002674// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002675Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2676 return child_iterator();
2677}
2678Stmt::child_iterator ObjCProtocolExpr::child_end() {
2679 return child_iterator();
2680}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002681
Steve Naroffd54978b2007-09-18 23:55:05 +00002682// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002683Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002684 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002685}
2686Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002687 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002688}
2689
Steve Naroffc540d662008-09-03 18:15:37 +00002690// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002691Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2692Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002693
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002694Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2695Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }