blob: 034b91ed0f5910ec974f9cdfb80a25c7769c26e4 [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() &&
101 Var->getType().getCVRQualifiers() == Qualifiers::Const &&
102 Var->getInit() &&
103 Var->getInit()->isValueDependent())
104 ValueDependent = true;
105 }
106 // (TD) - a nested-name-specifier or a qualified-id that names a
107 // member of an unknown specialization.
108 // (handled by DependentScopeDeclRefExpr)
109}
110
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000111DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
112 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000113 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000114 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000115 QualType T)
116 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000117 DecoratedD(D,
118 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000119 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000120 Loc(NameLoc) {
121 if (Qualifier) {
122 NameQualifier *NQ = getNameQualifier();
123 NQ->NNS = Qualifier;
124 NQ->Range = QualifierRange;
125 }
126
John McCall6b51f282009-11-23 01:53:49 +0000127 if (TemplateArgs)
128 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000129
130 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000131}
132
133DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
134 NestedNameSpecifier *Qualifier,
135 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000136 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000137 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000138 QualType T,
139 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000140 std::size_t Size = sizeof(DeclRefExpr);
141 if (Qualifier != 0)
142 Size += sizeof(NameQualifier);
143
John McCall6b51f282009-11-23 01:53:49 +0000144 if (TemplateArgs)
145 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000146
147 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
148 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000149 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000150}
151
152SourceRange DeclRefExpr::getSourceRange() const {
153 // FIXME: Does not handle multi-token names well, e.g., operator[].
154 SourceRange R(Loc);
155
156 if (hasQualifier())
157 R.setBegin(getQualifierRange().getBegin());
158 if (hasExplicitTemplateArgumentList())
159 R.setEnd(getRAngleLoc());
160 return R;
161}
162
Anders Carlsson2fb08242009-09-08 18:24:21 +0000163// FIXME: Maybe this should use DeclPrinter with a special "print predefined
164// expr" policy instead.
165std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
166 const Decl *CurrentDecl) {
167 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
168 if (IT != PrettyFunction)
169 return FD->getNameAsString();
170
171 llvm::SmallString<256> Name;
172 llvm::raw_svector_ostream Out(Name);
173
174 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
175 if (MD->isVirtual())
176 Out << "virtual ";
177 }
178
179 PrintingPolicy Policy(Context.getLangOptions());
180 Policy.SuppressTagKind = true;
181
182 std::string Proto = FD->getQualifiedNameAsString(Policy);
183
John McCall9dd450b2009-09-21 23:43:11 +0000184 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000185 const FunctionProtoType *FT = 0;
186 if (FD->hasWrittenPrototype())
187 FT = dyn_cast<FunctionProtoType>(AFT);
188
189 Proto += "(";
190 if (FT) {
191 llvm::raw_string_ostream POut(Proto);
192 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
193 if (i) POut << ", ";
194 std::string Param;
195 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
196 POut << Param;
197 }
198
199 if (FT->isVariadic()) {
200 if (FD->getNumParams()) POut << ", ";
201 POut << "...";
202 }
203 }
204 Proto += ")";
205
Sam Weinigd060ed42009-12-06 23:55:13 +0000206 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
207 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000208
209 Out << Proto;
210
211 Out.flush();
212 return Name.str().str();
213 }
214 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
215 llvm::SmallString<256> Name;
216 llvm::raw_svector_ostream Out(Name);
217 Out << (MD->isInstanceMethod() ? '-' : '+');
218 Out << '[';
219 Out << MD->getClassInterface()->getNameAsString();
220 if (const ObjCCategoryImplDecl *CID =
221 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
222 Out << '(';
223 Out << CID->getNameAsString();
224 Out << ')';
225 }
226 Out << ' ';
227 Out << MD->getSelector().getAsString();
228 Out << ']';
229
230 Out.flush();
231 return Name.str().str();
232 }
233 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
234 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
235 return "top level";
236 }
237 return "";
238}
239
Chris Lattnera0173132008-06-07 22:13:43 +0000240/// getValueAsApproximateDouble - This returns the value as an inaccurate
241/// double. Note that this may cause loss of precision, but is useful for
242/// debugging dumps, etc.
243double FloatingLiteral::getValueAsApproximateDouble() const {
244 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000245 bool ignored;
246 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
247 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000248 return V.convertToDouble();
249}
250
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000251StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
252 unsigned ByteLength, bool Wide,
253 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000254 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000255 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000256 // Allocate enough space for the StringLiteral plus an array of locations for
257 // any concatenated string tokens.
258 void *Mem = C.Allocate(sizeof(StringLiteral)+
259 sizeof(SourceLocation)*(NumStrs-1),
260 llvm::alignof<StringLiteral>());
261 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000262
Steve Naroffdf7855b2007-02-21 23:46:25 +0000263 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000264 char *AStrData = new (C, 1) char[ByteLength];
265 memcpy(AStrData, StrData, ByteLength);
266 SL->StrData = AStrData;
267 SL->ByteLength = ByteLength;
268 SL->IsWide = Wide;
269 SL->TokLocs[0] = Loc[0];
270 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000271
Chris Lattner630970d2009-02-18 05:49:11 +0000272 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000273 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
274 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000275}
276
Douglas Gregor958dfc92009-04-15 16:35:07 +0000277StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
278 void *Mem = C.Allocate(sizeof(StringLiteral)+
279 sizeof(SourceLocation)*(NumStrs-1),
280 llvm::alignof<StringLiteral>());
281 StringLiteral *SL = new (Mem) StringLiteral(QualType());
282 SL->StrData = 0;
283 SL->ByteLength = 0;
284 SL->NumConcatenated = NumStrs;
285 return SL;
286}
287
Douglas Gregore26a2852009-08-07 06:08:38 +0000288void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000289 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000290 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000291}
292
Daniel Dunbar36217882009-09-22 03:27:33 +0000293void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000294 if (StrData)
295 C.Deallocate(const_cast<char*>(StrData));
296
Daniel Dunbar36217882009-09-22 03:27:33 +0000297 char *AStrData = new (C, 1) char[Str.size()];
298 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000299 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000300 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000301}
302
Chris Lattner1b926492006-08-23 06:42:10 +0000303/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
304/// corresponds to, e.g. "sizeof" or "[pre]++".
305const char *UnaryOperator::getOpcodeStr(Opcode Op) {
306 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000307 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000308 case PostInc: return "++";
309 case PostDec: return "--";
310 case PreInc: return "++";
311 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000312 case AddrOf: return "&";
313 case Deref: return "*";
314 case Plus: return "+";
315 case Minus: return "-";
316 case Not: return "~";
317 case LNot: return "!";
318 case Real: return "__real";
319 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000320 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000321 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000322 }
323}
324
Mike Stump11289f42009-09-09 15:08:12 +0000325UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000326UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
327 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000328 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000329 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
330 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
331 case OO_Amp: return AddrOf;
332 case OO_Star: return Deref;
333 case OO_Plus: return Plus;
334 case OO_Minus: return Minus;
335 case OO_Tilde: return Not;
336 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000337 }
338}
339
340OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
341 switch (Opc) {
342 case PostInc: case PreInc: return OO_PlusPlus;
343 case PostDec: case PreDec: return OO_MinusMinus;
344 case AddrOf: return OO_Amp;
345 case Deref: return OO_Star;
346 case Plus: return OO_Plus;
347 case Minus: return OO_Minus;
348 case Not: return OO_Tilde;
349 case LNot: return OO_Exclaim;
350 default: return OO_None;
351 }
352}
353
354
Chris Lattner0eedafe2006-08-24 04:56:27 +0000355//===----------------------------------------------------------------------===//
356// Postfix Operators.
357//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000358
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000359CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000360 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000361 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000362 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000363 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000364 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000366 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000367 SubExprs[FN] = fn;
368 for (unsigned i = 0; i != numargs; ++i)
369 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000370
Douglas Gregor993603d2008-11-14 16:09:21 +0000371 RParenLoc = rparenloc;
372}
Nate Begeman1e36a852008-01-17 17:46:27 +0000373
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000374CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
375 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000376 : Expr(CallExprClass, t,
377 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000378 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000379 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000380
381 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000382 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000383 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000384 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000385
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000386 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000387}
388
Mike Stump11289f42009-09-09 15:08:12 +0000389CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
390 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000391 SubExprs = new (C) Stmt*[1];
392}
393
Douglas Gregore26a2852009-08-07 06:08:38 +0000394void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000395 DestroyChildren(C);
396 if (SubExprs) C.Deallocate(SubExprs);
397 this->~CallExpr();
398 C.Deallocate(this);
399}
400
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000401FunctionDecl *CallExpr::getDirectCallee() {
402 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000403 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000404 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000405
406 return 0;
407}
408
Chris Lattnere4407ed2007-12-28 05:25:02 +0000409/// setNumArgs - This changes the number of arguments present in this call.
410/// Any orphaned expressions are deleted by this, and any new operands are set
411/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000412void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000413 // No change, just return.
414 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattnere4407ed2007-12-28 05:25:02 +0000416 // If shrinking # arguments, just delete the extras and forgot them.
417 if (NumArgs < getNumArgs()) {
418 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000419 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000420 this->NumArgs = NumArgs;
421 return;
422 }
423
424 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000425 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000426 // Copy over args.
427 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
428 NewSubExprs[i] = SubExprs[i];
429 // Null out new args.
430 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
431 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000432
Douglas Gregorba6e5572009-04-17 21:46:47 +0000433 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000434 SubExprs = NewSubExprs;
435 this->NumArgs = NumArgs;
436}
437
Chris Lattner01ff98a2008-10-06 05:00:53 +0000438/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
439/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000440unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000441 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000442 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000443 // ImplicitCastExpr.
444 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
445 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000446 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000447
Steve Narofff6e3b3292008-01-31 01:07:12 +0000448 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
449 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000450 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000451
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000452 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
453 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000454 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000455
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000456 if (!FDecl->getIdentifier())
457 return 0;
458
Douglas Gregor15fc9562009-09-12 00:22:50 +0000459 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000460}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000461
Anders Carlsson00a27592009-05-26 04:57:27 +0000462QualType CallExpr::getCallReturnType() const {
463 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000464 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000465 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000466 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000467 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000468
John McCall9dd450b2009-09-21 23:43:11 +0000469 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000470 return FnType->getResultType();
471}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000472
Mike Stump11289f42009-09-09 15:08:12 +0000473MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000474 SourceRange qualrange, ValueDecl *memberdecl,
John McCall6b51f282009-11-23 01:53:49 +0000475 SourceLocation l, const TemplateArgumentListInfo *targs,
476 QualType ty)
Mike Stump11289f42009-09-09 15:08:12 +0000477 : Expr(MemberExprClass, ty,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000478 base->isTypeDependent() || (qual && qual->isDependent()),
479 base->isValueDependent() || (qual && qual->isDependent())),
480 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCall6b51f282009-11-23 01:53:49 +0000481 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000482 // Initialize the qualifier, if any.
483 if (HasQualifier) {
484 NameQualifier *NQ = getMemberQualifier();
485 NQ->NNS = qual;
486 NQ->Range = qualrange;
487 }
Mike Stump11289f42009-09-09 15:08:12 +0000488
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000489 // Initialize the explicit template argument list, if any.
John McCall6b51f282009-11-23 01:53:49 +0000490 if (targs)
491 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000492}
493
Mike Stump11289f42009-09-09 15:08:12 +0000494MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
495 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000496 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000497 ValueDecl *memberdecl,
Mike Stump11289f42009-09-09 15:08:12 +0000498 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000499 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000500 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000501 std::size_t Size = sizeof(MemberExpr);
502 if (qual != 0)
503 Size += sizeof(NameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000504
John McCall6b51f282009-11-23 01:53:49 +0000505 if (targs)
506 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000508 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000509 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCall6b51f282009-11-23 01:53:49 +0000510 targs, ty);
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000511}
512
Anders Carlsson496335e2009-09-03 00:59:21 +0000513const char *CastExpr::getCastKindName() const {
514 switch (getCastKind()) {
515 case CastExpr::CK_Unknown:
516 return "Unknown";
517 case CastExpr::CK_BitCast:
518 return "BitCast";
519 case CastExpr::CK_NoOp:
520 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000521 case CastExpr::CK_BaseToDerived:
522 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000523 case CastExpr::CK_DerivedToBase:
524 return "DerivedToBase";
525 case CastExpr::CK_Dynamic:
526 return "Dynamic";
527 case CastExpr::CK_ToUnion:
528 return "ToUnion";
529 case CastExpr::CK_ArrayToPointerDecay:
530 return "ArrayToPointerDecay";
531 case CastExpr::CK_FunctionToPointerDecay:
532 return "FunctionToPointerDecay";
533 case CastExpr::CK_NullToMemberPointer:
534 return "NullToMemberPointer";
535 case CastExpr::CK_BaseToDerivedMemberPointer:
536 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000537 case CastExpr::CK_DerivedToBaseMemberPointer:
538 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000539 case CastExpr::CK_UserDefinedConversion:
540 return "UserDefinedConversion";
541 case CastExpr::CK_ConstructorConversion:
542 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000543 case CastExpr::CK_IntegralToPointer:
544 return "IntegralToPointer";
545 case CastExpr::CK_PointerToIntegral:
546 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000547 case CastExpr::CK_ToVoid:
548 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000549 case CastExpr::CK_VectorSplat:
550 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000551 case CastExpr::CK_IntegralCast:
552 return "IntegralCast";
553 case CastExpr::CK_IntegralToFloating:
554 return "IntegralToFloating";
555 case CastExpr::CK_FloatingToIntegral:
556 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000557 case CastExpr::CK_FloatingCast:
558 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000559 case CastExpr::CK_MemberPointerToBoolean:
560 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000561 case CastExpr::CK_AnyPointerToObjCPointerCast:
562 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000563 case CastExpr::CK_AnyPointerToBlockPointerCast:
564 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000565 }
Mike Stump11289f42009-09-09 15:08:12 +0000566
Anders Carlsson496335e2009-09-03 00:59:21 +0000567 assert(0 && "Unhandled cast kind!");
568 return 0;
569}
570
Douglas Gregord196a582009-12-14 19:27:10 +0000571Expr *CastExpr::getSubExprAsWritten() {
572 Expr *SubExpr = 0;
573 CastExpr *E = this;
574 do {
575 SubExpr = E->getSubExpr();
576
577 // Skip any temporary bindings; they're implicit.
578 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
579 SubExpr = Binder->getSubExpr();
580
581 // Conversions by constructor and conversion functions have a
582 // subexpression describing the call; strip it off.
583 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
584 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
585 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
586 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
587
588 // If the subexpression we're left with is an implicit cast, look
589 // through that, too.
590 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
591
592 return SubExpr;
593}
594
Chris Lattner1b926492006-08-23 06:42:10 +0000595/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
596/// corresponds to, e.g. "<<=".
597const char *BinaryOperator::getOpcodeStr(Opcode Op) {
598 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000599 case PtrMemD: return ".*";
600 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000601 case Mul: return "*";
602 case Div: return "/";
603 case Rem: return "%";
604 case Add: return "+";
605 case Sub: return "-";
606 case Shl: return "<<";
607 case Shr: return ">>";
608 case LT: return "<";
609 case GT: return ">";
610 case LE: return "<=";
611 case GE: return ">=";
612 case EQ: return "==";
613 case NE: return "!=";
614 case And: return "&";
615 case Xor: return "^";
616 case Or: return "|";
617 case LAnd: return "&&";
618 case LOr: return "||";
619 case Assign: return "=";
620 case MulAssign: return "*=";
621 case DivAssign: return "/=";
622 case RemAssign: return "%=";
623 case AddAssign: return "+=";
624 case SubAssign: return "-=";
625 case ShlAssign: return "<<=";
626 case ShrAssign: return ">>=";
627 case AndAssign: return "&=";
628 case XorAssign: return "^=";
629 case OrAssign: return "|=";
630 case Comma: return ",";
631 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000632
633 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000634}
Steve Naroff47500512007-04-19 23:00:49 +0000635
Mike Stump11289f42009-09-09 15:08:12 +0000636BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000637BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
638 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000639 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000640 case OO_Plus: return Add;
641 case OO_Minus: return Sub;
642 case OO_Star: return Mul;
643 case OO_Slash: return Div;
644 case OO_Percent: return Rem;
645 case OO_Caret: return Xor;
646 case OO_Amp: return And;
647 case OO_Pipe: return Or;
648 case OO_Equal: return Assign;
649 case OO_Less: return LT;
650 case OO_Greater: return GT;
651 case OO_PlusEqual: return AddAssign;
652 case OO_MinusEqual: return SubAssign;
653 case OO_StarEqual: return MulAssign;
654 case OO_SlashEqual: return DivAssign;
655 case OO_PercentEqual: return RemAssign;
656 case OO_CaretEqual: return XorAssign;
657 case OO_AmpEqual: return AndAssign;
658 case OO_PipeEqual: return OrAssign;
659 case OO_LessLess: return Shl;
660 case OO_GreaterGreater: return Shr;
661 case OO_LessLessEqual: return ShlAssign;
662 case OO_GreaterGreaterEqual: return ShrAssign;
663 case OO_EqualEqual: return EQ;
664 case OO_ExclaimEqual: return NE;
665 case OO_LessEqual: return LE;
666 case OO_GreaterEqual: return GE;
667 case OO_AmpAmp: return LAnd;
668 case OO_PipePipe: return LOr;
669 case OO_Comma: return Comma;
670 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000671 }
672}
673
674OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
675 static const OverloadedOperatorKind OverOps[] = {
676 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
677 OO_Star, OO_Slash, OO_Percent,
678 OO_Plus, OO_Minus,
679 OO_LessLess, OO_GreaterGreater,
680 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
681 OO_EqualEqual, OO_ExclaimEqual,
682 OO_Amp,
683 OO_Caret,
684 OO_Pipe,
685 OO_AmpAmp,
686 OO_PipePipe,
687 OO_Equal, OO_StarEqual,
688 OO_SlashEqual, OO_PercentEqual,
689 OO_PlusEqual, OO_MinusEqual,
690 OO_LessLessEqual, OO_GreaterGreaterEqual,
691 OO_AmpEqual, OO_CaretEqual,
692 OO_PipeEqual,
693 OO_Comma
694 };
695 return OverOps[Opc];
696}
697
Mike Stump11289f42009-09-09 15:08:12 +0000698InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000699 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000700 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000701 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump11289f42009-09-09 15:08:12 +0000702 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000703 UnionFieldInit(0), HadArrayRangeDesignator(false)
704{
705 for (unsigned I = 0; I != numInits; ++I) {
706 if (initExprs[I]->isTypeDependent())
707 TypeDependent = true;
708 if (initExprs[I]->isValueDependent())
709 ValueDependent = true;
710 }
711
Chris Lattner07d754a2008-10-26 23:43:26 +0000712 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000713}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000714
Douglas Gregor6d00c992009-03-20 23:58:33 +0000715void InitListExpr::reserveInits(unsigned NumInits) {
716 if (NumInits > InitExprs.size())
717 InitExprs.reserve(NumInits);
718}
719
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000720void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattner8ba22472009-02-16 22:33:34 +0000721 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar45a2a202009-02-16 22:42:44 +0000722 Idx < LastIdx; ++Idx)
Douglas Gregor52a47e92009-03-20 23:38:03 +0000723 InitExprs[Idx]->Destroy(Context);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000724 InitExprs.resize(NumInits, 0);
725}
726
727Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
728 if (Init >= InitExprs.size()) {
729 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
730 InitExprs.back() = expr;
731 return 0;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000734 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
735 InitExprs[Init] = expr;
736 return Result;
737}
738
Steve Naroff991e99d2008-09-04 15:31:07 +0000739/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000740///
741const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000742 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000743 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000744}
745
Mike Stump11289f42009-09-09 15:08:12 +0000746SourceLocation BlockExpr::getCaretLocation() const {
747 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000748}
Mike Stump11289f42009-09-09 15:08:12 +0000749const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000750 return TheBlock->getBody();
751}
Mike Stump11289f42009-09-09 15:08:12 +0000752Stmt *BlockExpr::getBody() {
753 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000754}
Steve Naroff415d3d52008-10-08 17:01:13 +0000755
756
Chris Lattner1ec5f562007-06-27 05:38:08 +0000757//===----------------------------------------------------------------------===//
758// Generic Expression Routines
759//===----------------------------------------------------------------------===//
760
Chris Lattner237f2752009-02-14 07:37:35 +0000761/// isUnusedResultAWarning - Return true if this immediate expression should
762/// be warned about if the result is unused. If so, fill in Loc and Ranges
763/// with location to warn on and the source range[s] to report with the
764/// warning.
765bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000766 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000767 // Don't warn if the expr is type dependent. The type could end up
768 // instantiating to void.
769 if (isTypeDependent())
770 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner1ec5f562007-06-27 05:38:08 +0000772 switch (getStmtClass()) {
773 default:
Chris Lattner237f2752009-02-14 07:37:35 +0000774 Loc = getExprLoc();
775 R1 = getSourceRange();
776 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000777 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000778 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000779 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000780 case UnaryOperatorClass: {
781 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner1ec5f562007-06-27 05:38:08 +0000783 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000784 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000785 case UnaryOperator::PostInc:
786 case UnaryOperator::PostDec:
787 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000788 case UnaryOperator::PreDec: // ++/--
789 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000790 case UnaryOperator::Deref:
791 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000792 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000793 return false;
794 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000795 case UnaryOperator::Real:
796 case UnaryOperator::Imag:
797 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000798 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
799 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000800 return false;
801 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000802 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000803 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000804 }
Chris Lattner237f2752009-02-14 07:37:35 +0000805 Loc = UO->getOperatorLoc();
806 R1 = UO->getSubExpr()->getSourceRange();
807 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000808 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000809 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000810 const BinaryOperator *BO = cast<BinaryOperator>(this);
811 // Consider comma to have side effects if the LHS or RHS does.
812 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stump53f9ded2009-11-03 23:25:48 +0000813 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
814 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner237f2752009-02-14 07:37:35 +0000816 if (BO->isAssignmentOp())
817 return false;
818 Loc = BO->getOperatorLoc();
819 R1 = BO->getLHS()->getSourceRange();
820 R2 = BO->getRHS()->getSourceRange();
821 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000822 }
Chris Lattner86928112007-08-25 02:00:02 +0000823 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000824 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000825
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000826 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000827 // The condition must be evaluated, but if either the LHS or RHS is a
828 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000829 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000830 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000831 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000832 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000833 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000834 }
835
Chris Lattnera44d1162007-06-27 05:58:59 +0000836 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000837 // If the base pointer or element is to a volatile pointer/field, accessing
838 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000839 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000840 return false;
841 Loc = cast<MemberExpr>(this)->getMemberLoc();
842 R1 = SourceRange(Loc, Loc);
843 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
844 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000845
Chris Lattner1ec5f562007-06-27 05:38:08 +0000846 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000847 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000848 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000849 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000850 return false;
851 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
852 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
853 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
854 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000855
Chris Lattner1ec5f562007-06-27 05:38:08 +0000856 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000857 case CXXOperatorCallExprClass:
858 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000859 // If this is a direct call, get the callee.
860 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1a6babf2009-10-13 04:53:48 +0000861 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000862 // If the callee has attribute pure, const, or warn_unused_result, warn
863 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000864 //
865 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
866 // updated to match for QoI.
867 if (FD->getAttr<WarnUnusedResultAttr>() ||
868 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
869 Loc = CE->getCallee()->getLocStart();
870 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner1a6babf2009-10-13 04:53:48 +0000872 if (unsigned NumArgs = CE->getNumArgs())
873 R2 = SourceRange(CE->getArg(0)->getLocStart(),
874 CE->getArg(NumArgs-1)->getLocEnd());
875 return true;
876 }
Chris Lattner237f2752009-02-14 07:37:35 +0000877 }
878 return false;
879 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000880
881 case CXXTemporaryObjectExprClass:
882 case CXXConstructExprClass:
883 return false;
884
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000885 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000886 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000887
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000888 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000889#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000890 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000891 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000892 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000893 Loc = Ref->getLocation();
894 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
895 if (Ref->getBase())
896 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000897#else
898 Loc = getExprLoc();
899 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000900#endif
901 return true;
902 }
Chris Lattner944d3062008-07-26 19:51:01 +0000903 case StmtExprClass: {
904 // Statement exprs don't logically have side effects themselves, but are
905 // sometimes used in macros in ways that give them a type that is unused.
906 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
907 // however, if the result of the stmt expr is dead, we don't want to emit a
908 // warning.
909 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
910 if (!CS->body_empty())
911 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000912 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner237f2752009-02-14 07:37:35 +0000914 Loc = cast<StmtExpr>(this)->getLParenLoc();
915 R1 = getSourceRange();
916 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000917 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000918 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000919 // If this is an explicit cast to void, allow it. People do this when they
920 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000921 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000922 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000923 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
924 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
925 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000926 case CXXFunctionalCastExprClass: {
927 const CastExpr *CE = cast<CastExpr>(this);
928
929 // If this is a cast to void or a constructor conversion, check the operand.
930 // Otherwise, the result of the cast is unused.
931 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
932 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000933 return (cast<CastExpr>(this)->getSubExpr()
934 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000935 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
936 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
937 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000938 }
Mike Stump11289f42009-09-09 15:08:12 +0000939
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000940 case ImplicitCastExprClass:
941 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000942 return (cast<ImplicitCastExpr>(this)
943 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000944
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000945 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000946 return (cast<CXXDefaultArgExpr>(this)
947 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000948
949 case CXXNewExprClass:
950 // FIXME: In theory, there might be new expressions that don't have side
951 // effects (e.g. a placement new with an uninitialized POD).
952 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000953 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000954 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000955 return (cast<CXXBindTemporaryExpr>(this)
956 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +0000957 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000958 return (cast<CXXExprWithTemporaries>(this)
959 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000960 }
Chris Lattner1ec5f562007-06-27 05:38:08 +0000961}
962
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000963/// DeclCanBeLvalue - Determine whether the given declaration can be
964/// an lvalue. This is a helper routine for isLvalue.
965static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000966 // C++ [temp.param]p6:
967 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000968 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +0000969 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
970 return NTTParm->getType()->isReferenceType();
971
Douglas Gregor91f84212008-12-11 16:49:14 +0000972 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000973 // C++ 3.10p2: An lvalue refers to an object or function.
974 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +0000975 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000976}
977
Steve Naroff475cca02007-05-14 17:19:29 +0000978/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
979/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000980/// - name, where name must be a variable
981/// - e[i]
982/// - (e), where e must be an lvalue
983/// - e.name, where e must be an lvalue
984/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000985/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000986/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +0000987/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000988/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000989///
Chris Lattner67315442008-07-26 21:30:36 +0000990Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000991 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
992
993 isLvalueResult Res = isLvalueInternal(Ctx);
994 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
995 return Res;
996
Douglas Gregor9a657932008-10-21 23:43:52 +0000997 // first, check the type (C99 6.3.2.1). Expressions with function
998 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +0000999 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001000 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001001
Steve Naroff1018ea32008-02-10 01:39:04 +00001002 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001003 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001004 return LV_IncompleteVoidType;
1005
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001006 return LV_Valid;
1007}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001008
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001009// Check whether the expression can be sanely treated like an l-value
1010Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001011 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001012 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001013 case StringLiteralClass: // C99 6.5.1p4
1014 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001015 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001016 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001017 // For vectors, make sure base is an lvalue (i.e. not a function call).
1018 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001019 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001020 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001021 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001022 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1023 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001024 return LV_Valid;
1025 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001026 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001027 case BlockDeclRefExprClass: {
1028 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001029 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001030 return LV_Valid;
1031 break;
1032 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001033 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001034 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001035 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1036 NamedDecl *Member = m->getMemberDecl();
1037 // C++ [expr.ref]p4:
1038 // If E2 is declared to have type "reference to T", then E1.E2
1039 // is an lvalue.
1040 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1041 if (Value->getType()->isReferenceType())
1042 return LV_Valid;
1043
1044 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001045 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001046 return LV_Valid;
1047
1048 // -- If E2 is a non-static data member [...]. If E1 is an
1049 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001050 if (isa<FieldDecl>(Member)) {
1051 if (m->isArrow())
1052 return LV_Valid;
1053 Expr *BaseExp = m->getBase();
1054 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1055 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
1056 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001057
1058 // -- If it refers to a static member function [...], then
1059 // E1.E2 is an lvalue.
1060 // -- Otherwise, if E1.E2 refers to a non-static member
1061 // function [...], then E1.E2 is not an lvalue.
1062 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1063 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1064
1065 // -- If E2 is a member enumerator [...], the expression E1.E2
1066 // is not an lvalue.
1067 if (isa<EnumConstantDecl>(Member))
1068 return LV_InvalidExpression;
1069
1070 // Not an lvalue.
1071 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001072 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001073
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001074 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001075 if (m->isArrow())
1076 return LV_Valid;
1077 Expr *BaseExp = m->getBase();
1078 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1079 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001080 }
Chris Lattner595db862007-10-30 22:53:42 +00001081 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001082 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001083 return LV_Valid; // C99 6.5.3p4
1084
1085 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001086 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1087 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001088 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001089
1090 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1091 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1092 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1093 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001094 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001095 case ImplicitCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +00001096 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregora11693b2008-11-12 17:17:38 +00001097 : LV_InvalidExpression;
Steve Naroff475cca02007-05-14 17:19:29 +00001098 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001099 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001100 case BinaryOperatorClass:
1101 case CompoundAssignOperatorClass: {
1102 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001103
1104 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1105 BinOp->getOpcode() == BinaryOperator::Comma)
1106 return BinOp->getRHS()->isLvalue(Ctx);
1107
Sebastian Redl112a97662009-02-07 00:15:38 +00001108 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001109 // The result of a .* expression is an lvalue only if its first operand is
1110 // an lvalue and its second operand is a pointer to data member.
1111 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001112 !BinOp->getType()->isFunctionType())
1113 return BinOp->getLHS()->isLvalue(Ctx);
1114
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001115 // The result of an ->* expression is an lvalue only if its second operand
1116 // is a pointer to data member.
1117 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1118 !BinOp->getType()->isFunctionType()) {
1119 QualType Ty = BinOp->getRHS()->getType();
1120 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1121 return LV_Valid;
1122 }
1123
Douglas Gregor58e008d2008-11-13 20:12:29 +00001124 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001125 return LV_InvalidExpression;
1126
Douglas Gregor58e008d2008-11-13 20:12:29 +00001127 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001128 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001129 // The result of an assignment operation [...] is an lvalue.
1130 return LV_Valid;
1131
1132
1133 // C99 6.5.16:
1134 // An assignment expression [...] is not an lvalue.
1135 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001138 case CXXOperatorCallExprClass:
1139 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001140 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001141 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001142 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001143 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1144 if (ReturnType->isLValueReferenceType())
1145 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001146
Douglas Gregor6b754842008-10-28 00:22:11 +00001147 break;
1148 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001149 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1150 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001151 case ChooseExprClass:
1152 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001153 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001154 case ExtVectorElementExprClass:
1155 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001156 return LV_DuplicateVectorComponents;
1157 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001158 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1159 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001160 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1161 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001162 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001163 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001164 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001165 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001166 case UnresolvedLookupExprClass:
1167 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001168 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001169 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001170 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001171 case CXXFunctionalCastExprClass:
1172 case CXXStaticCastExprClass:
1173 case CXXDynamicCastExprClass:
1174 case CXXReinterpretCastExprClass:
1175 case CXXConstCastExprClass:
1176 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001177 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001178 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1179 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001180 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1181 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001182 return LV_Valid;
1183 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001184 case CXXTypeidExprClass:
1185 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1186 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001187 case CXXBindTemporaryExprClass:
1188 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1189 isLvalueInternal(Ctx);
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001190 case ConditionalOperatorClass: {
1191 // Complicated handling is only for C++.
1192 if (!Ctx.getLangOptions().CPlusPlus)
1193 return LV_InvalidExpression;
1194
1195 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1196 // everywhere there's an object converted to an rvalue. Also, any other
1197 // casts should be wrapped by ImplicitCastExprs. There's just the special
1198 // case involving throws to work out.
1199 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001200 Expr *True = Cond->getTrueExpr();
1201 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001202 // C++0x 5.16p2
1203 // If either the second or the third operand has type (cv) void, [...]
1204 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001205 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001206 return LV_InvalidExpression;
1207
1208 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001209 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001210 return LV_InvalidExpression;
1211
1212 // That's it.
1213 return LV_Valid;
1214 }
1215
Douglas Gregor5103eff2009-12-19 07:07:47 +00001216 case Expr::CXXExprWithTemporariesClass:
1217 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1218
1219 case Expr::ObjCMessageExprClass:
1220 if (const ObjCMethodDecl *Method
1221 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1222 if (Method->getResultType()->isLValueReferenceType())
1223 return LV_Valid;
1224 break;
1225
Steve Naroff9358c712007-05-27 23:58:33 +00001226 default:
1227 break;
Steve Naroff47500512007-04-19 23:00:49 +00001228 }
Steve Naroff9358c712007-05-27 23:58:33 +00001229 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001230}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001231
Steve Naroff475cca02007-05-14 17:19:29 +00001232/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1233/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001234/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001235/// recursively, any member or element of all contained aggregates or unions)
1236/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001237Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001238Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001239 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001240
Steve Naroff9358c712007-05-27 23:58:33 +00001241 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001242 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001243 // C++ 3.10p11: Functions cannot be modified, but pointers to
1244 // functions can be modifiable.
1245 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1246 return MLV_NotObjectType;
1247 break;
1248
Chris Lattner1ec5f562007-06-27 05:38:08 +00001249 case LV_NotObjectType: return MLV_NotObjectType;
1250 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001251 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001252 case LV_InvalidExpression:
1253 // If the top level is a C-style cast, and the subexpression is a valid
1254 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1255 // GCC extension. We don't support it, but we want to produce good
1256 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001257 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1258 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1259 if (Loc)
1260 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001261 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001262 }
1263 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001264 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001265 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001266 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Steve Naroff9358c712007-05-27 23:58:33 +00001267 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001268
1269 // The following is illegal:
1270 // void takeclosure(void (^C)(void));
1271 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1272 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001273 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001274 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1275 return MLV_NotBlockQualified;
1276 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001277
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001278 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001279 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001280 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1281 if (Expr->getSetterMethod() == 0)
1282 return MLV_NoSetterProperty;
1283 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001284
Chris Lattner7adf0762008-08-04 07:31:14 +00001285 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001286
Chris Lattner7adf0762008-08-04 07:31:14 +00001287 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001288 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001289 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001290 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001291 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001292 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001293
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001294 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001295 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001296 return MLV_ConstQualified;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Mike Stump11289f42009-09-09 15:08:12 +00001299 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001300}
1301
Fariborz Jahanian07735332009-02-22 18:40:18 +00001302/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001303/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001304bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001305 switch (getStmtClass()) {
1306 default:
1307 return false;
1308 case ObjCIvarRefExprClass:
1309 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001310 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001311 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001312 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001313 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001314 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001315 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001316 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001317 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001318 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001319 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001320 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1321 if (VD->hasGlobalStorage())
1322 return true;
1323 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001324 // dereferencing to a pointer is always a gc'able candidate,
1325 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001326 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001327 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001328 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001329 return false;
1330 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001331 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001332 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001333 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001334 }
1335 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001336 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001337 }
1338}
Ted Kremenekfff70962008-01-17 16:57:34 +00001339Expr* Expr::IgnoreParens() {
1340 Expr* E = this;
1341 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1342 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001343
Ted Kremenekfff70962008-01-17 16:57:34 +00001344 return E;
1345}
1346
Chris Lattnerf2660962008-02-13 01:02:39 +00001347/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1348/// or CastExprs or ImplicitCastExprs, returning their operand.
1349Expr *Expr::IgnoreParenCasts() {
1350 Expr *E = this;
1351 while (true) {
1352 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1353 E = P->getSubExpr();
1354 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1355 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001356 else
1357 return E;
1358 }
1359}
1360
Chris Lattneref26c772009-03-13 17:28:01 +00001361/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1362/// value (including ptr->int casts of the same size). Strip off any
1363/// ParenExpr or CastExprs, returning their operand.
1364Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1365 Expr *E = this;
1366 while (true) {
1367 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1368 E = P->getSubExpr();
1369 continue;
1370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattneref26c772009-03-13 17:28:01 +00001372 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1373 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1374 // ptr<->int casts of the same width. We also ignore all identify casts.
1375 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattneref26c772009-03-13 17:28:01 +00001377 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1378 E = SE;
1379 continue;
1380 }
Mike Stump11289f42009-09-09 15:08:12 +00001381
Chris Lattneref26c772009-03-13 17:28:01 +00001382 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1383 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1384 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1385 E = SE;
1386 continue;
1387 }
1388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389
Chris Lattneref26c772009-03-13 17:28:01 +00001390 return E;
1391 }
1392}
1393
Douglas Gregord196a582009-12-14 19:27:10 +00001394bool Expr::isDefaultArgument() const {
1395 const Expr *E = this;
1396 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1397 E = ICE->getSubExprAsWritten();
1398
1399 return isa<CXXDefaultArgExpr>(E);
1400}
Chris Lattneref26c772009-03-13 17:28:01 +00001401
Douglas Gregor4619e432008-12-05 23:32:09 +00001402/// hasAnyTypeDependentArguments - Determines if any of the expressions
1403/// in Exprs is type-dependent.
1404bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1405 for (unsigned I = 0; I < NumExprs; ++I)
1406 if (Exprs[I]->isTypeDependent())
1407 return true;
1408
1409 return false;
1410}
1411
1412/// hasAnyValueDependentArguments - Determines if any of the expressions
1413/// in Exprs is value-dependent.
1414bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1415 for (unsigned I = 0; I < NumExprs; ++I)
1416 if (Exprs[I]->isValueDependent())
1417 return true;
1418
1419 return false;
1420}
1421
Eli Friedman7139af42009-01-25 02:32:41 +00001422bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001423 // This function is attempting whether an expression is an initializer
1424 // which can be evaluated at compile-time. isEvaluatable handles most
1425 // of the cases, but it can't deal with some initializer-specific
1426 // expressions, and it can't deal with aggregates; we deal with those here,
1427 // and fall back to isEvaluatable for the other cases.
1428
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001429 // FIXME: This function assumes the variable being assigned to
1430 // isn't a reference type!
1431
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001432 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001433 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001434 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001435 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001436 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001437 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001438 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001439 // This handles gcc's extension that allows global initializers like
1440 // "struct x {int x;} x = (struct x) {};".
1441 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001442 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001443 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001444 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001445 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001446 // FIXME: This doesn't deal with fields with reference types correctly.
1447 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1448 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001449 const InitListExpr *Exp = cast<InitListExpr>(this);
1450 unsigned numInits = Exp->getNumInits();
1451 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001452 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001453 return false;
1454 }
Eli Friedman384da272009-01-25 03:12:18 +00001455 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001456 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001457 case ImplicitValueInitExprClass:
1458 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001459 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001460 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001461 case UnaryOperatorClass: {
1462 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1463 if (Exp->getOpcode() == UnaryOperator::Extension)
1464 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1465 break;
1466 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001467 case BinaryOperatorClass: {
1468 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1469 // but this handles the common case.
1470 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1471 if (Exp->getOpcode() == BinaryOperator::Sub &&
1472 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1473 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1474 return true;
1475 break;
1476 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001477 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001478 case CStyleCastExprClass:
1479 // Handle casts with a destination that's a struct or union; this
1480 // deals with both the gcc no-op struct cast extension and the
1481 // cast-to-union extension.
1482 if (getType()->isRecordType())
1483 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001484
1485 // Integer->integer casts can be handled here, which is important for
1486 // things like (int)(&&x-&&y). Scary but true.
1487 if (getType()->isIntegerType() &&
1488 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1489 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1490
Eli Friedman384da272009-01-25 03:12:18 +00001491 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001492 }
Eli Friedman384da272009-01-25 03:12:18 +00001493 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001494}
1495
Chris Lattner1f4479e2007-06-05 04:15:44 +00001496/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001497/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001498
1499/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1500/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001501///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001502/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1503/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1504/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001505
Eli Friedman98c56a42009-02-26 09:29:13 +00001506// CheckICE - This function does the fundamental ICE checking: the returned
1507// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1508// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001509// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001510// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001511// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001512//
1513// Meanings of Val:
1514// 0: This expression is an ICE if it can be evaluated by Evaluate.
1515// 1: This expression is not an ICE, but if it isn't evaluated, it's
1516// a legal subexpression for an ICE. This return value is used to handle
1517// the comma operator in C99 mode.
1518// 2: This expression is not an ICE, and is not a legal subexpression for one.
1519
1520struct ICEDiag {
1521 unsigned Val;
1522 SourceLocation Loc;
1523
1524 public:
1525 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1526 ICEDiag() : Val(0) {}
1527};
1528
1529ICEDiag NoDiag() { return ICEDiag(); }
1530
Eli Friedman90afd3d2009-02-27 04:07:58 +00001531static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1532 Expr::EvalResult EVResult;
1533 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1534 !EVResult.Val.isInt()) {
1535 return ICEDiag(2, E->getLocStart());
1536 }
1537 return NoDiag();
1538}
1539
Eli Friedman98c56a42009-02-26 09:29:13 +00001540static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001541 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001542 if (!E->getType()->isIntegralType()) {
1543 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001544 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001545
1546 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001547#define STMT(Node, Base) case Expr::Node##Class:
1548#define EXPR(Node, Base)
1549#include "clang/AST/StmtNodes.def"
1550 case Expr::PredefinedExprClass:
1551 case Expr::FloatingLiteralClass:
1552 case Expr::ImaginaryLiteralClass:
1553 case Expr::StringLiteralClass:
1554 case Expr::ArraySubscriptExprClass:
1555 case Expr::MemberExprClass:
1556 case Expr::CompoundAssignOperatorClass:
1557 case Expr::CompoundLiteralExprClass:
1558 case Expr::ExtVectorElementExprClass:
1559 case Expr::InitListExprClass:
1560 case Expr::DesignatedInitExprClass:
1561 case Expr::ImplicitValueInitExprClass:
1562 case Expr::ParenListExprClass:
1563 case Expr::VAArgExprClass:
1564 case Expr::AddrLabelExprClass:
1565 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001566 case Expr::CXXMemberCallExprClass:
1567 case Expr::CXXDynamicCastExprClass:
1568 case Expr::CXXTypeidExprClass:
1569 case Expr::CXXNullPtrLiteralExprClass:
1570 case Expr::CXXThisExprClass:
1571 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001572 case Expr::CXXNewExprClass:
1573 case Expr::CXXDeleteExprClass:
1574 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001575 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001576 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001577 case Expr::CXXConstructExprClass:
1578 case Expr::CXXBindTemporaryExprClass:
1579 case Expr::CXXExprWithTemporariesClass:
1580 case Expr::CXXTemporaryObjectExprClass:
1581 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001582 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001583 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001584 case Expr::ObjCStringLiteralClass:
1585 case Expr::ObjCEncodeExprClass:
1586 case Expr::ObjCMessageExprClass:
1587 case Expr::ObjCSelectorExprClass:
1588 case Expr::ObjCProtocolExprClass:
1589 case Expr::ObjCIvarRefExprClass:
1590 case Expr::ObjCPropertyRefExprClass:
1591 case Expr::ObjCImplicitSetterGetterRefExprClass:
1592 case Expr::ObjCSuperExprClass:
1593 case Expr::ObjCIsaExprClass:
1594 case Expr::ShuffleVectorExprClass:
1595 case Expr::BlockExprClass:
1596 case Expr::BlockDeclRefExprClass:
1597 case Expr::NoStmtClass:
1598 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001599 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001600
Douglas Gregor73341c42009-09-11 00:18:58 +00001601 case Expr::GNUNullExprClass:
1602 // GCC considers the GNU __null value to be an integral constant expression.
1603 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001604
Eli Friedman98c56a42009-02-26 09:29:13 +00001605 case Expr::ParenExprClass:
1606 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1607 case Expr::IntegerLiteralClass:
1608 case Expr::CharacterLiteralClass:
1609 case Expr::CXXBoolLiteralExprClass:
1610 case Expr::CXXZeroInitValueExprClass:
1611 case Expr::TypesCompatibleExprClass:
1612 case Expr::UnaryTypeTraitExprClass:
1613 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001614 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001615 case Expr::CXXOperatorCallExprClass: {
1616 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001617 if (CE->isBuiltinCall(Ctx))
1618 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001619 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001620 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001621 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001622 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1623 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001624 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001625 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001626 // C++ 7.1.5.1p2
1627 // A variable of non-volatile const-qualified integral or enumeration
1628 // type initialized by an ICE can be used in ICEs.
1629 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001630 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001631 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1632 if (Quals.hasVolatile() || !Quals.hasConst())
1633 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1634
1635 // Look for the definition of this variable, which will actually have
1636 // an initializer.
1637 const VarDecl *Def = 0;
1638 const Expr *Init = Dcl->getDefinition(Def);
1639 if (Init) {
1640 if (Def->isInitKnownICE()) {
1641 // We have already checked whether this subexpression is an
1642 // integral constant expression.
1643 if (Def->isInitICE())
1644 return NoDiag();
1645 else
1646 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1647 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001648
Douglas Gregor0840cc02009-11-01 20:32:48 +00001649 // C++ [class.static.data]p4:
1650 // If a static data member is of const integral or const
1651 // enumeration type, its declaration in the class definition can
1652 // specify a constant-initializer which shall be an integral
1653 // constant expression (5.19). In that case, the member can appear
1654 // in integral constant expressions.
1655 if (Def->isOutOfLine()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +00001656 Dcl->setInitKnownICE(false);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001657 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1658 }
Eli Friedman1d6fb162009-12-03 20:31:57 +00001659
1660 if (Dcl->isCheckingICE()) {
1661 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1662 }
1663
1664 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001665 ICEDiag Result = CheckICE(Init, Ctx);
1666 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001667 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001668 return Result;
1669 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001670 }
1671 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001672 return ICEDiag(2, E->getLocStart());
1673 case Expr::UnaryOperatorClass: {
1674 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001675 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001676 case UnaryOperator::PostInc:
1677 case UnaryOperator::PostDec:
1678 case UnaryOperator::PreInc:
1679 case UnaryOperator::PreDec:
1680 case UnaryOperator::AddrOf:
1681 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001682 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001683
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001684 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001685 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001686 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001687 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001688 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001689 case UnaryOperator::Real:
1690 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001691 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001692 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001693 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1694 // Evaluate matches the proposed gcc behavior for cases like
1695 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1696 // compliance: we should warn earlier for offsetof expressions with
1697 // array subscripts that aren't ICEs, and if the array subscripts
1698 // are ICEs, the value of the offsetof must be an integer constant.
1699 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001700 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001701 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001702 case Expr::SizeOfAlignOfExprClass: {
1703 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1704 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1705 return ICEDiag(2, E->getLocStart());
1706 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001707 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001708 case Expr::BinaryOperatorClass: {
1709 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001710 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001711 case BinaryOperator::PtrMemD:
1712 case BinaryOperator::PtrMemI:
1713 case BinaryOperator::Assign:
1714 case BinaryOperator::MulAssign:
1715 case BinaryOperator::DivAssign:
1716 case BinaryOperator::RemAssign:
1717 case BinaryOperator::AddAssign:
1718 case BinaryOperator::SubAssign:
1719 case BinaryOperator::ShlAssign:
1720 case BinaryOperator::ShrAssign:
1721 case BinaryOperator::AndAssign:
1722 case BinaryOperator::XorAssign:
1723 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001724 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001725
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001726 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001727 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001728 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001729 case BinaryOperator::Add:
1730 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001731 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001732 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001733 case BinaryOperator::LT:
1734 case BinaryOperator::GT:
1735 case BinaryOperator::LE:
1736 case BinaryOperator::GE:
1737 case BinaryOperator::EQ:
1738 case BinaryOperator::NE:
1739 case BinaryOperator::And:
1740 case BinaryOperator::Xor:
1741 case BinaryOperator::Or:
1742 case BinaryOperator::Comma: {
1743 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1744 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001745 if (Exp->getOpcode() == BinaryOperator::Div ||
1746 Exp->getOpcode() == BinaryOperator::Rem) {
1747 // Evaluate gives an error for undefined Div/Rem, so make sure
1748 // we don't evaluate one.
1749 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1750 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1751 if (REval == 0)
1752 return ICEDiag(1, E->getLocStart());
1753 if (REval.isSigned() && REval.isAllOnesValue()) {
1754 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1755 if (LEval.isMinSignedValue())
1756 return ICEDiag(1, E->getLocStart());
1757 }
1758 }
1759 }
1760 if (Exp->getOpcode() == BinaryOperator::Comma) {
1761 if (Ctx.getLangOptions().C99) {
1762 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1763 // if it isn't evaluated.
1764 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1765 return ICEDiag(1, E->getLocStart());
1766 } else {
1767 // In both C89 and C++, commas in ICEs are illegal.
1768 return ICEDiag(2, E->getLocStart());
1769 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001770 }
1771 if (LHSResult.Val >= RHSResult.Val)
1772 return LHSResult;
1773 return RHSResult;
1774 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001775 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001776 case BinaryOperator::LOr: {
1777 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1778 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1779 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1780 // Rare case where the RHS has a comma "side-effect"; we need
1781 // to actually check the condition to see whether the side
1782 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001783 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001784 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001785 return RHSResult;
1786 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001787 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001788
Eli Friedman98c56a42009-02-26 09:29:13 +00001789 if (LHSResult.Val >= RHSResult.Val)
1790 return LHSResult;
1791 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001792 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001793 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001794 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001795 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001796 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001797 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001798 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001799 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001800 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001801 case Expr::CXXStaticCastExprClass:
1802 case Expr::CXXReinterpretCastExprClass:
1803 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001804 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1805 if (SubExpr->getType()->isIntegralType())
1806 return CheckICE(SubExpr, Ctx);
1807 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1808 return NoDiag();
1809 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001810 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001811 case Expr::ConditionalOperatorClass: {
1812 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001813 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001814 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001815 // expression, and it is fully evaluated. This is an important GNU
1816 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001817 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001818 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001819 Expr::EvalResult EVResult;
1820 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1821 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001822 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001823 }
1824 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001825 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001826 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1827 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1828 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1829 if (CondResult.Val == 2)
1830 return CondResult;
1831 if (TrueResult.Val == 2)
1832 return TrueResult;
1833 if (FalseResult.Val == 2)
1834 return FalseResult;
1835 if (CondResult.Val == 1)
1836 return CondResult;
1837 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1838 return NoDiag();
1839 // Rare case where the diagnostics depend on which side is evaluated
1840 // Note that if we get here, CondResult is 0, and at least one of
1841 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001842 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001843 return FalseResult;
1844 }
1845 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001846 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001847 case Expr::CXXDefaultArgExprClass:
1848 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001849 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001850 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001851 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001852 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001853
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001854 // Silence a GCC warning
1855 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001856}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001857
Eli Friedman98c56a42009-02-26 09:29:13 +00001858bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1859 SourceLocation *Loc, bool isEvaluated) const {
1860 ICEDiag d = CheckICE(this, Ctx);
1861 if (d.Val != 0) {
1862 if (Loc) *Loc = d.Loc;
1863 return false;
1864 }
1865 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001866 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001867 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001868 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1869 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001870 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001871 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001872}
1873
Chris Lattner7eef9192007-05-24 01:23:49 +00001874/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1875/// integer constant expression with the value zero, or if this is one that is
1876/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001877bool Expr::isNullPointerConstant(ASTContext &Ctx,
1878 NullPointerConstantValueDependence NPC) const {
1879 if (isValueDependent()) {
1880 switch (NPC) {
1881 case NPC_NeverValueDependent:
1882 assert(false && "Unexpected value dependent expression!");
1883 // If the unthinkable happens, fall through to the safest alternative.
1884
1885 case NPC_ValueDependentIsNull:
1886 return isTypeDependent() || getType()->isIntegralType();
1887
1888 case NPC_ValueDependentIsNotNull:
1889 return false;
1890 }
1891 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001892
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001893 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001894 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001895 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001896 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001897 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001898 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001899 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001900 Pointee->isVoidType() && // to void*
1901 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001902 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001903 }
Steve Naroffada7d422007-05-20 17:54:12 +00001904 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001905 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1906 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001907 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001908 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1909 // Accept ((void*)0) as a null pointer constant, as many other
1910 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001911 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001912 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001913 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001914 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001915 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001916 } else if (isa<GNUNullExpr>(this)) {
1917 // The GNU __null extension is always a null pointer constant.
1918 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001919 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001920
Sebastian Redl576fd422009-05-10 18:38:11 +00001921 // C++0x nullptr_t is always a null pointer constant.
1922 if (getType()->isNullPtrType())
1923 return true;
1924
Steve Naroff4871fe02008-01-14 16:10:57 +00001925 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001926 if (!getType()->isIntegerType() ||
1927 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001928 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001929
Chris Lattner1abbd412007-06-08 17:58:43 +00001930 // If we have an integer constant expression, we need to *evaluate* it and
1931 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001932 llvm::APSInt Result;
1933 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001934}
Steve Narofff7a5da12007-07-28 23:10:27 +00001935
Douglas Gregor71235ec2009-05-02 02:18:30 +00001936FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001937 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001938
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001939 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001940 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001941 if (Field->isBitField())
1942 return Field;
1943
1944 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1945 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1946 return BinOp->getLHS()->getBitField();
1947
1948 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001949}
1950
Chris Lattnerb8211f62009-02-16 22:14:05 +00001951/// isArrow - Return true if the base expression is a pointer to vector,
1952/// return false if the base expression is a vector.
1953bool ExtVectorElementExpr::isArrow() const {
1954 return getBase()->getType()->isPointerType();
1955}
1956
Nate Begemance4d7fc2008-04-18 23:10:10 +00001957unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001958 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001959 return VT->getNumElements();
1960 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001961}
1962
Nate Begemanf322eab2008-05-09 06:41:27 +00001963/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001964bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001965 // FIXME: Refactor this code to an accessor on the AST node which returns the
1966 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001967 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001968
1969 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001970 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001971 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001972
Nate Begeman7e5185b2009-01-18 02:01:21 +00001973 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001974 if (Comp[0] == 's' || Comp[0] == 'S')
1975 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001976
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001977 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1978 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001979 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001980
Steve Naroff0d595ca2007-07-30 03:29:09 +00001981 return false;
1982}
Chris Lattner885b4952007-08-02 23:36:59 +00001983
Nate Begemanf322eab2008-05-09 06:41:27 +00001984/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001985void ExtVectorElementExpr::getEncodedElementAccess(
1986 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001987 llvm::StringRef Comp = Accessor->getName();
1988 if (Comp[0] == 's' || Comp[0] == 'S')
1989 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001990
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001991 bool isHi = Comp == "hi";
1992 bool isLo = Comp == "lo";
1993 bool isEven = Comp == "even";
1994 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001995
Nate Begemanf322eab2008-05-09 06:41:27 +00001996 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1997 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001998
Nate Begemanf322eab2008-05-09 06:41:27 +00001999 if (isHi)
2000 Index = e + i;
2001 else if (isLo)
2002 Index = i;
2003 else if (isEven)
2004 Index = 2 * i;
2005 else if (isOdd)
2006 Index = 2 * i + 1;
2007 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002008 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002009
Nate Begemand3862152008-05-13 21:03:02 +00002010 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002011 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002012}
2013
Steve Narofff73590d2007-09-27 14:38:14 +00002014// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00002015ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002016 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00002017 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002018 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002019 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002020 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002021 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002022 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002023 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002024 if (NumArgs) {
2025 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002026 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2027 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002028 LBracloc = LBrac;
2029 RBracloc = RBrac;
2030}
2031
Mike Stump11289f42009-09-09 15:08:12 +00002032// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002033// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00002034ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002035 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00002036 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002037 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002038 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002039 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002040 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002041 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002042 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002043 if (NumArgs) {
2044 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002045 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2046 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002047 LBracloc = LBrac;
2048 RBracloc = RBrac;
2049}
2050
Mike Stump11289f42009-09-09 15:08:12 +00002051// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002052ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2053 QualType retType, ObjCMethodDecl *mproto,
2054 SourceLocation LBrac, SourceLocation RBrac,
2055 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002056: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002057MethodProto(mproto) {
2058 NumArgs = nargs;
2059 SubExprs = new Stmt*[NumArgs+1];
2060 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2061 if (NumArgs) {
2062 for (unsigned i = 0; i != NumArgs; ++i)
2063 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2064 }
2065 LBracloc = LBrac;
2066 RBracloc = RBrac;
2067}
2068
2069ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2070 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2071 switch (x & Flags) {
2072 default:
2073 assert(false && "Invalid ObjCMessageExpr.");
2074 case IsInstMeth:
2075 return ClassInfo(0, 0);
2076 case IsClsMethDeclUnknown:
2077 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2078 case IsClsMethDeclKnown: {
2079 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2080 return ClassInfo(D, D->getIdentifier());
2081 }
2082 }
2083}
2084
Chris Lattner7ec71da2009-04-26 00:44:05 +00002085void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2086 if (CI.first == 0 && CI.second == 0)
2087 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2088 else if (CI.first == 0)
2089 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2090 else
2091 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2092}
2093
2094
Chris Lattner35e564e2007-10-25 00:29:32 +00002095bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002096 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002097}
2098
Nate Begeman48745922009-08-12 02:28:50 +00002099void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2100 unsigned NumExprs) {
2101 if (SubExprs) C.Deallocate(SubExprs);
2102
2103 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002104 this->NumExprs = NumExprs;
2105 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002106}
Nate Begeman48745922009-08-12 02:28:50 +00002107
2108void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2109 DestroyChildren(C);
2110 if (SubExprs) C.Deallocate(SubExprs);
2111 this->~ShuffleVectorExpr();
2112 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002113}
2114
Douglas Gregore26a2852009-08-07 06:08:38 +00002115void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002116 // Override default behavior of traversing children. If this has a type
2117 // operand and the type is a variable-length array, the child iteration
2118 // will iterate over the size expression. However, this expression belongs
2119 // to the type, not to this, so we don't want to delete it.
2120 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002121 if (isArgumentType()) {
2122 this->~SizeOfAlignOfExpr();
2123 C.Deallocate(this);
2124 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002125 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002126 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002127}
2128
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002129//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002130// DesignatedInitExpr
2131//===----------------------------------------------------------------------===//
2132
2133IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2134 assert(Kind == FieldDesignator && "Only valid on a field designator");
2135 if (Field.NameOrField & 0x01)
2136 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2137 else
2138 return getField()->getIdentifier();
2139}
2140
Mike Stump11289f42009-09-09 15:08:12 +00002141DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002142 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002143 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002144 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002145 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002146 unsigned NumIndexExprs,
2147 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002148 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002149 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002150 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2151 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002152 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002153
2154 // Record the initializer itself.
2155 child_iterator Child = child_begin();
2156 *Child++ = Init;
2157
2158 // Copy the designators and their subexpressions, computing
2159 // value-dependence along the way.
2160 unsigned IndexIdx = 0;
2161 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002162 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002163
2164 if (this->Designators[I].isArrayDesignator()) {
2165 // Compute type- and value-dependence.
2166 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002167 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002168 Index->isTypeDependent() || Index->isValueDependent();
2169
2170 // Copy the index expressions into permanent storage.
2171 *Child++ = IndexExprs[IndexIdx++];
2172 } else if (this->Designators[I].isArrayRangeDesignator()) {
2173 // Compute type- and value-dependence.
2174 Expr *Start = IndexExprs[IndexIdx];
2175 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002176 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002177 Start->isTypeDependent() || Start->isValueDependent() ||
2178 End->isTypeDependent() || End->isValueDependent();
2179
2180 // Copy the start/end expressions into permanent storage.
2181 *Child++ = IndexExprs[IndexIdx++];
2182 *Child++ = IndexExprs[IndexIdx++];
2183 }
2184 }
2185
2186 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002187}
2188
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002189DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002190DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002191 unsigned NumDesignators,
2192 Expr **IndexExprs, unsigned NumIndexExprs,
2193 SourceLocation ColonOrEqualLoc,
2194 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002195 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002196 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002197 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2198 ColonOrEqualLoc, UsesColonSyntax,
2199 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002200}
2201
Mike Stump11289f42009-09-09 15:08:12 +00002202DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002203 unsigned NumIndexExprs) {
2204 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2205 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2206 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2207}
2208
Mike Stump11289f42009-09-09 15:08:12 +00002209void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002210 unsigned NumDesigs) {
2211 if (Designators)
2212 delete [] Designators;
2213
2214 Designators = new Designator[NumDesigs];
2215 NumDesignators = NumDesigs;
2216 for (unsigned I = 0; I != NumDesigs; ++I)
2217 Designators[I] = Desigs[I];
2218}
2219
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002220SourceRange DesignatedInitExpr::getSourceRange() const {
2221 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002222 Designator &First =
2223 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002224 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002225 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002226 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2227 else
2228 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2229 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002230 StartLoc =
2231 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002232 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2233}
2234
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002235Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2236 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2237 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2238 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002239 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2240 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2241}
2242
2243Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002244 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002245 "Requires array range designator");
2246 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2247 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002248 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2249 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2250}
2251
2252Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002253 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002254 "Requires array range designator");
2255 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2256 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002257 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2258 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2259}
2260
Douglas Gregord5846a12009-04-15 06:41:24 +00002261/// \brief Replaces the designator at index @p Idx with the series
2262/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00002263void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2264 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002265 const Designator *Last) {
2266 unsigned NumNewDesignators = Last - First;
2267 if (NumNewDesignators == 0) {
2268 std::copy_backward(Designators + Idx + 1,
2269 Designators + NumDesignators,
2270 Designators + Idx);
2271 --NumNewDesignators;
2272 return;
2273 } else if (NumNewDesignators == 1) {
2274 Designators[Idx] = *First;
2275 return;
2276 }
2277
Mike Stump11289f42009-09-09 15:08:12 +00002278 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002279 = new Designator[NumDesignators - 1 + NumNewDesignators];
2280 std::copy(Designators, Designators + Idx, NewDesignators);
2281 std::copy(First, Last, NewDesignators + Idx);
2282 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2283 NewDesignators + Idx + NumNewDesignators);
2284 delete [] Designators;
2285 Designators = NewDesignators;
2286 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2287}
2288
Douglas Gregore26a2852009-08-07 06:08:38 +00002289void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002290 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002291 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002292}
2293
Mike Stump11289f42009-09-09 15:08:12 +00002294ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002295 Expr **exprs, unsigned nexprs,
2296 SourceLocation rparenloc)
2297: Expr(ParenListExprClass, QualType(),
2298 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002299 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002300 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002301
Nate Begeman5ec4b312009-08-10 23:49:36 +00002302 Exprs = new (C) Stmt*[nexprs];
2303 for (unsigned i = 0; i != nexprs; ++i)
2304 Exprs[i] = exprs[i];
2305}
2306
2307void ParenListExpr::DoDestroy(ASTContext& C) {
2308 DestroyChildren(C);
2309 if (Exprs) C.Deallocate(Exprs);
2310 this->~ParenListExpr();
2311 C.Deallocate(this);
2312}
2313
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002314//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002315// ExprIterator.
2316//===----------------------------------------------------------------------===//
2317
2318Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2319Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2320Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2321const Expr* ConstExprIterator::operator[](size_t idx) const {
2322 return cast<Expr>(I[idx]);
2323}
2324const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2325const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2326
2327//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002328// Child Iterators for iterating over subexpressions/substatements
2329//===----------------------------------------------------------------------===//
2330
2331// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002332Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2333Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002334
Steve Naroffe46504b2007-11-12 14:29:37 +00002335// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002336Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2337Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002338
Steve Naroffebf4cb42008-06-02 23:03:37 +00002339// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002340Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2341Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002342
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002343// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002344Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2345 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002346}
Mike Stump11289f42009-09-09 15:08:12 +00002347Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2348 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002349}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002350
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002351// ObjCSuperExpr
2352Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2353Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2354
Steve Naroffe87026a2009-07-24 17:54:45 +00002355// ObjCIsaExpr
2356Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2357Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2358
Chris Lattner6307f192008-08-10 01:53:14 +00002359// PredefinedExpr
2360Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2361Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002362
2363// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002364Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2365Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002366
2367// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002368Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002369Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002370
2371// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002372Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2373Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002374
Chris Lattner1c20a172007-08-26 03:42:43 +00002375// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002376Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2377Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002378
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002379// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002380Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2381Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002382
2383// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002384Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2385Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002386
2387// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002388Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2389Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002390
Sebastian Redl6f282892008-11-11 17:56:53 +00002391// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002392Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002393 // If this is of a type and the type is a VLA type (and not a typedef), the
2394 // size expression of the VLA needs to be treated as an executable expression.
2395 // Why isn't this weirdness documented better in StmtIterator?
2396 if (isArgumentType()) {
2397 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2398 getArgumentType().getTypePtr()))
2399 return child_iterator(T);
2400 return child_iterator();
2401 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002402 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002403}
Sebastian Redl6f282892008-11-11 17:56:53 +00002404Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2405 if (isArgumentType())
2406 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002407 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002408}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002409
2410// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002411Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002412 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002413}
Ted Kremenek23702b62007-08-24 20:06:47 +00002414Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002415 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002416}
2417
2418// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002419Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002420 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002421}
Ted Kremenek23702b62007-08-24 20:06:47 +00002422Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002423 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002424}
Ted Kremenek23702b62007-08-24 20:06:47 +00002425
2426// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002427Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2428Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002429
Nate Begemance4d7fc2008-04-18 23:10:10 +00002430// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002431Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2432Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002433
2434// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002435Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2436Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002437
Ted Kremenek23702b62007-08-24 20:06:47 +00002438// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002439Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2440Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002441
2442// BinaryOperator
2443Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002444 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002445}
Ted Kremenek23702b62007-08-24 20:06:47 +00002446Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002447 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002448}
2449
2450// ConditionalOperator
2451Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002452 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002453}
Ted Kremenek23702b62007-08-24 20:06:47 +00002454Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002455 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002456}
2457
2458// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002459Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2460Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002461
Ted Kremenek23702b62007-08-24 20:06:47 +00002462// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002463Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2464Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002465
2466// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002467Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2468 return child_iterator();
2469}
2470
2471Stmt::child_iterator TypesCompatibleExpr::child_end() {
2472 return child_iterator();
2473}
Ted Kremenek23702b62007-08-24 20:06:47 +00002474
2475// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002476Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2477Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002478
Douglas Gregor3be4b122008-11-29 04:51:27 +00002479// GNUNullExpr
2480Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2481Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2482
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002483// ShuffleVectorExpr
2484Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002485 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002486}
2487Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002488 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002489}
2490
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002491// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002492Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2493Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002494
Anders Carlsson4692db02007-08-31 04:56:16 +00002495// InitListExpr
2496Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002497 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002498}
2499Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002500 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002501}
2502
Douglas Gregor0202cb42009-01-29 17:44:32 +00002503// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002504Stmt::child_iterator DesignatedInitExpr::child_begin() {
2505 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2506 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002507 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2508}
2509Stmt::child_iterator DesignatedInitExpr::child_end() {
2510 return child_iterator(&*child_begin() + NumSubExprs);
2511}
2512
Douglas Gregor0202cb42009-01-29 17:44:32 +00002513// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002514Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2515 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002516}
2517
Mike Stump11289f42009-09-09 15:08:12 +00002518Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2519 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002520}
2521
Nate Begeman5ec4b312009-08-10 23:49:36 +00002522// ParenListExpr
2523Stmt::child_iterator ParenListExpr::child_begin() {
2524 return &Exprs[0];
2525}
2526Stmt::child_iterator ParenListExpr::child_end() {
2527 return &Exprs[0]+NumExprs;
2528}
2529
Ted Kremenek23702b62007-08-24 20:06:47 +00002530// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002531Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002532 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002533}
2534Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002535 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002536}
Ted Kremenek23702b62007-08-24 20:06:47 +00002537
2538// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002539Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2540Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002541
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002542// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002543Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002544 return child_iterator();
2545}
2546Stmt::child_iterator ObjCSelectorExpr::child_end() {
2547 return child_iterator();
2548}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002549
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002550// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002551Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2552 return child_iterator();
2553}
2554Stmt::child_iterator ObjCProtocolExpr::child_end() {
2555 return child_iterator();
2556}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002557
Steve Naroffd54978b2007-09-18 23:55:05 +00002558// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002559Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002560 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002561}
2562Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002563 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002564}
2565
Steve Naroffc540d662008-09-03 18:15:37 +00002566// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002567Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2568Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002569
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002570Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2571Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }