blob: dcf4411d854380f04f7b6d75459b2585a4cb26ba [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
Steve Naroff9358c712007-05-27 23:58:33 +00001216 default:
1217 break;
Steve Naroff47500512007-04-19 23:00:49 +00001218 }
Steve Naroff9358c712007-05-27 23:58:33 +00001219 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001220}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001221
Steve Naroff475cca02007-05-14 17:19:29 +00001222/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1223/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001224/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001225/// recursively, any member or element of all contained aggregates or unions)
1226/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001227Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001228Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001229 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Steve Naroff9358c712007-05-27 23:58:33 +00001231 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001232 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001233 // C++ 3.10p11: Functions cannot be modified, but pointers to
1234 // functions can be modifiable.
1235 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1236 return MLV_NotObjectType;
1237 break;
1238
Chris Lattner1ec5f562007-06-27 05:38:08 +00001239 case LV_NotObjectType: return MLV_NotObjectType;
1240 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001241 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001242 case LV_InvalidExpression:
1243 // If the top level is a C-style cast, and the subexpression is a valid
1244 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1245 // GCC extension. We don't support it, but we want to produce good
1246 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001247 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1248 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1249 if (Loc)
1250 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001251 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001252 }
1253 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001254 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001255 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001256 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Steve Naroff9358c712007-05-27 23:58:33 +00001257 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001258
1259 // The following is illegal:
1260 // void takeclosure(void (^C)(void));
1261 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1262 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001263 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001264 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1265 return MLV_NotBlockQualified;
1266 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001267
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001268 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001269 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001270 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1271 if (Expr->getSetterMethod() == 0)
1272 return MLV_NoSetterProperty;
1273 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001274
Chris Lattner7adf0762008-08-04 07:31:14 +00001275 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001276
Chris Lattner7adf0762008-08-04 07:31:14 +00001277 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001278 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001279 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001280 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001281 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001282 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001284 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001285 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001286 return MLV_ConstQualified;
1287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Mike Stump11289f42009-09-09 15:08:12 +00001289 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001290}
1291
Fariborz Jahanian07735332009-02-22 18:40:18 +00001292/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001293/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001294bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001295 switch (getStmtClass()) {
1296 default:
1297 return false;
1298 case ObjCIvarRefExprClass:
1299 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001300 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001301 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001302 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001303 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001304 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001305 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001306 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001307 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001308 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001309 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001310 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1311 if (VD->hasGlobalStorage())
1312 return true;
1313 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001314 // dereferencing to a pointer is always a gc'able candidate,
1315 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001316 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001317 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001318 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001319 return false;
1320 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001321 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001322 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001323 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001324 }
1325 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001326 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001327 }
1328}
Ted Kremenekfff70962008-01-17 16:57:34 +00001329Expr* Expr::IgnoreParens() {
1330 Expr* E = this;
1331 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1332 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001333
Ted Kremenekfff70962008-01-17 16:57:34 +00001334 return E;
1335}
1336
Chris Lattnerf2660962008-02-13 01:02:39 +00001337/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1338/// or CastExprs or ImplicitCastExprs, returning their operand.
1339Expr *Expr::IgnoreParenCasts() {
1340 Expr *E = this;
1341 while (true) {
1342 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1343 E = P->getSubExpr();
1344 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1345 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001346 else
1347 return E;
1348 }
1349}
1350
Chris Lattneref26c772009-03-13 17:28:01 +00001351/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1352/// value (including ptr->int casts of the same size). Strip off any
1353/// ParenExpr or CastExprs, returning their operand.
1354Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1355 Expr *E = this;
1356 while (true) {
1357 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1358 E = P->getSubExpr();
1359 continue;
1360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattneref26c772009-03-13 17:28:01 +00001362 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1363 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1364 // ptr<->int casts of the same width. We also ignore all identify casts.
1365 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001366
Chris Lattneref26c772009-03-13 17:28:01 +00001367 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1368 E = SE;
1369 continue;
1370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattneref26c772009-03-13 17:28:01 +00001372 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1373 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1374 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1375 E = SE;
1376 continue;
1377 }
1378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattneref26c772009-03-13 17:28:01 +00001380 return E;
1381 }
1382}
1383
Douglas Gregord196a582009-12-14 19:27:10 +00001384bool Expr::isDefaultArgument() const {
1385 const Expr *E = this;
1386 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1387 E = ICE->getSubExprAsWritten();
1388
1389 return isa<CXXDefaultArgExpr>(E);
1390}
Chris Lattneref26c772009-03-13 17:28:01 +00001391
Douglas Gregor4619e432008-12-05 23:32:09 +00001392/// hasAnyTypeDependentArguments - Determines if any of the expressions
1393/// in Exprs is type-dependent.
1394bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1395 for (unsigned I = 0; I < NumExprs; ++I)
1396 if (Exprs[I]->isTypeDependent())
1397 return true;
1398
1399 return false;
1400}
1401
1402/// hasAnyValueDependentArguments - Determines if any of the expressions
1403/// in Exprs is value-dependent.
1404bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1405 for (unsigned I = 0; I < NumExprs; ++I)
1406 if (Exprs[I]->isValueDependent())
1407 return true;
1408
1409 return false;
1410}
1411
Eli Friedman7139af42009-01-25 02:32:41 +00001412bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001413 // This function is attempting whether an expression is an initializer
1414 // which can be evaluated at compile-time. isEvaluatable handles most
1415 // of the cases, but it can't deal with some initializer-specific
1416 // expressions, and it can't deal with aggregates; we deal with those here,
1417 // and fall back to isEvaluatable for the other cases.
1418
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001419 // FIXME: This function assumes the variable being assigned to
1420 // isn't a reference type!
1421
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001422 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001423 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001424 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001425 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001426 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001427 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001428 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001429 // This handles gcc's extension that allows global initializers like
1430 // "struct x {int x;} x = (struct x) {};".
1431 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001432 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001433 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001434 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001435 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001436 // FIXME: This doesn't deal with fields with reference types correctly.
1437 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1438 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001439 const InitListExpr *Exp = cast<InitListExpr>(this);
1440 unsigned numInits = Exp->getNumInits();
1441 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001442 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001443 return false;
1444 }
Eli Friedman384da272009-01-25 03:12:18 +00001445 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001446 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001447 case ImplicitValueInitExprClass:
1448 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001449 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001450 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001451 case UnaryOperatorClass: {
1452 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1453 if (Exp->getOpcode() == UnaryOperator::Extension)
1454 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1455 break;
1456 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001457 case BinaryOperatorClass: {
1458 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1459 // but this handles the common case.
1460 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1461 if (Exp->getOpcode() == BinaryOperator::Sub &&
1462 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1463 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1464 return true;
1465 break;
1466 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001467 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001468 case CStyleCastExprClass:
1469 // Handle casts with a destination that's a struct or union; this
1470 // deals with both the gcc no-op struct cast extension and the
1471 // cast-to-union extension.
1472 if (getType()->isRecordType())
1473 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001474
1475 // Integer->integer casts can be handled here, which is important for
1476 // things like (int)(&&x-&&y). Scary but true.
1477 if (getType()->isIntegerType() &&
1478 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1479 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1480
Eli Friedman384da272009-01-25 03:12:18 +00001481 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001482 }
Eli Friedman384da272009-01-25 03:12:18 +00001483 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001484}
1485
Chris Lattner1f4479e2007-06-05 04:15:44 +00001486/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001487/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001488
1489/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1490/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001491///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001492/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1493/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1494/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001495
Eli Friedman98c56a42009-02-26 09:29:13 +00001496// CheckICE - This function does the fundamental ICE checking: the returned
1497// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1498// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001499// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001500// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001501// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001502//
1503// Meanings of Val:
1504// 0: This expression is an ICE if it can be evaluated by Evaluate.
1505// 1: This expression is not an ICE, but if it isn't evaluated, it's
1506// a legal subexpression for an ICE. This return value is used to handle
1507// the comma operator in C99 mode.
1508// 2: This expression is not an ICE, and is not a legal subexpression for one.
1509
1510struct ICEDiag {
1511 unsigned Val;
1512 SourceLocation Loc;
1513
1514 public:
1515 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1516 ICEDiag() : Val(0) {}
1517};
1518
1519ICEDiag NoDiag() { return ICEDiag(); }
1520
Eli Friedman90afd3d2009-02-27 04:07:58 +00001521static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1522 Expr::EvalResult EVResult;
1523 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1524 !EVResult.Val.isInt()) {
1525 return ICEDiag(2, E->getLocStart());
1526 }
1527 return NoDiag();
1528}
1529
Eli Friedman98c56a42009-02-26 09:29:13 +00001530static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001531 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001532 if (!E->getType()->isIntegralType()) {
1533 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001534 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001535
1536 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001537#define STMT(Node, Base) case Expr::Node##Class:
1538#define EXPR(Node, Base)
1539#include "clang/AST/StmtNodes.def"
1540 case Expr::PredefinedExprClass:
1541 case Expr::FloatingLiteralClass:
1542 case Expr::ImaginaryLiteralClass:
1543 case Expr::StringLiteralClass:
1544 case Expr::ArraySubscriptExprClass:
1545 case Expr::MemberExprClass:
1546 case Expr::CompoundAssignOperatorClass:
1547 case Expr::CompoundLiteralExprClass:
1548 case Expr::ExtVectorElementExprClass:
1549 case Expr::InitListExprClass:
1550 case Expr::DesignatedInitExprClass:
1551 case Expr::ImplicitValueInitExprClass:
1552 case Expr::ParenListExprClass:
1553 case Expr::VAArgExprClass:
1554 case Expr::AddrLabelExprClass:
1555 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001556 case Expr::CXXMemberCallExprClass:
1557 case Expr::CXXDynamicCastExprClass:
1558 case Expr::CXXTypeidExprClass:
1559 case Expr::CXXNullPtrLiteralExprClass:
1560 case Expr::CXXThisExprClass:
1561 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001562 case Expr::CXXNewExprClass:
1563 case Expr::CXXDeleteExprClass:
1564 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001565 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001566 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001567 case Expr::CXXConstructExprClass:
1568 case Expr::CXXBindTemporaryExprClass:
1569 case Expr::CXXExprWithTemporariesClass:
1570 case Expr::CXXTemporaryObjectExprClass:
1571 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001572 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001573 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001574 case Expr::ObjCStringLiteralClass:
1575 case Expr::ObjCEncodeExprClass:
1576 case Expr::ObjCMessageExprClass:
1577 case Expr::ObjCSelectorExprClass:
1578 case Expr::ObjCProtocolExprClass:
1579 case Expr::ObjCIvarRefExprClass:
1580 case Expr::ObjCPropertyRefExprClass:
1581 case Expr::ObjCImplicitSetterGetterRefExprClass:
1582 case Expr::ObjCSuperExprClass:
1583 case Expr::ObjCIsaExprClass:
1584 case Expr::ShuffleVectorExprClass:
1585 case Expr::BlockExprClass:
1586 case Expr::BlockDeclRefExprClass:
1587 case Expr::NoStmtClass:
1588 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001589 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001590
Douglas Gregor73341c42009-09-11 00:18:58 +00001591 case Expr::GNUNullExprClass:
1592 // GCC considers the GNU __null value to be an integral constant expression.
1593 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001594
Eli Friedman98c56a42009-02-26 09:29:13 +00001595 case Expr::ParenExprClass:
1596 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1597 case Expr::IntegerLiteralClass:
1598 case Expr::CharacterLiteralClass:
1599 case Expr::CXXBoolLiteralExprClass:
1600 case Expr::CXXZeroInitValueExprClass:
1601 case Expr::TypesCompatibleExprClass:
1602 case Expr::UnaryTypeTraitExprClass:
1603 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001604 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001605 case Expr::CXXOperatorCallExprClass: {
1606 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001607 if (CE->isBuiltinCall(Ctx))
1608 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001609 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001610 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001611 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001612 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1613 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001614 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001615 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001616 // C++ 7.1.5.1p2
1617 // A variable of non-volatile const-qualified integral or enumeration
1618 // type initialized by an ICE can be used in ICEs.
1619 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001620 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001621 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1622 if (Quals.hasVolatile() || !Quals.hasConst())
1623 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1624
1625 // Look for the definition of this variable, which will actually have
1626 // an initializer.
1627 const VarDecl *Def = 0;
1628 const Expr *Init = Dcl->getDefinition(Def);
1629 if (Init) {
1630 if (Def->isInitKnownICE()) {
1631 // We have already checked whether this subexpression is an
1632 // integral constant expression.
1633 if (Def->isInitICE())
1634 return NoDiag();
1635 else
1636 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1637 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001638
Douglas Gregor0840cc02009-11-01 20:32:48 +00001639 // C++ [class.static.data]p4:
1640 // If a static data member is of const integral or const
1641 // enumeration type, its declaration in the class definition can
1642 // specify a constant-initializer which shall be an integral
1643 // constant expression (5.19). In that case, the member can appear
1644 // in integral constant expressions.
1645 if (Def->isOutOfLine()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +00001646 Dcl->setInitKnownICE(false);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001647 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1648 }
Eli Friedman1d6fb162009-12-03 20:31:57 +00001649
1650 if (Dcl->isCheckingICE()) {
1651 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1652 }
1653
1654 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001655 ICEDiag Result = CheckICE(Init, Ctx);
1656 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001657 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001658 return Result;
1659 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001660 }
1661 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001662 return ICEDiag(2, E->getLocStart());
1663 case Expr::UnaryOperatorClass: {
1664 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001665 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001666 case UnaryOperator::PostInc:
1667 case UnaryOperator::PostDec:
1668 case UnaryOperator::PreInc:
1669 case UnaryOperator::PreDec:
1670 case UnaryOperator::AddrOf:
1671 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001672 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001673
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001674 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001675 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001676 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001677 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001678 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001679 case UnaryOperator::Real:
1680 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001681 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001682 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001683 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1684 // Evaluate matches the proposed gcc behavior for cases like
1685 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1686 // compliance: we should warn earlier for offsetof expressions with
1687 // array subscripts that aren't ICEs, and if the array subscripts
1688 // are ICEs, the value of the offsetof must be an integer constant.
1689 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001690 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001691 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001692 case Expr::SizeOfAlignOfExprClass: {
1693 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1694 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1695 return ICEDiag(2, E->getLocStart());
1696 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001697 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001698 case Expr::BinaryOperatorClass: {
1699 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001700 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001701 case BinaryOperator::PtrMemD:
1702 case BinaryOperator::PtrMemI:
1703 case BinaryOperator::Assign:
1704 case BinaryOperator::MulAssign:
1705 case BinaryOperator::DivAssign:
1706 case BinaryOperator::RemAssign:
1707 case BinaryOperator::AddAssign:
1708 case BinaryOperator::SubAssign:
1709 case BinaryOperator::ShlAssign:
1710 case BinaryOperator::ShrAssign:
1711 case BinaryOperator::AndAssign:
1712 case BinaryOperator::XorAssign:
1713 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001714 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001715
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001716 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001717 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001718 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001719 case BinaryOperator::Add:
1720 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001721 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001722 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001723 case BinaryOperator::LT:
1724 case BinaryOperator::GT:
1725 case BinaryOperator::LE:
1726 case BinaryOperator::GE:
1727 case BinaryOperator::EQ:
1728 case BinaryOperator::NE:
1729 case BinaryOperator::And:
1730 case BinaryOperator::Xor:
1731 case BinaryOperator::Or:
1732 case BinaryOperator::Comma: {
1733 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1734 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001735 if (Exp->getOpcode() == BinaryOperator::Div ||
1736 Exp->getOpcode() == BinaryOperator::Rem) {
1737 // Evaluate gives an error for undefined Div/Rem, so make sure
1738 // we don't evaluate one.
1739 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1740 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1741 if (REval == 0)
1742 return ICEDiag(1, E->getLocStart());
1743 if (REval.isSigned() && REval.isAllOnesValue()) {
1744 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1745 if (LEval.isMinSignedValue())
1746 return ICEDiag(1, E->getLocStart());
1747 }
1748 }
1749 }
1750 if (Exp->getOpcode() == BinaryOperator::Comma) {
1751 if (Ctx.getLangOptions().C99) {
1752 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1753 // if it isn't evaluated.
1754 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1755 return ICEDiag(1, E->getLocStart());
1756 } else {
1757 // In both C89 and C++, commas in ICEs are illegal.
1758 return ICEDiag(2, E->getLocStart());
1759 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001760 }
1761 if (LHSResult.Val >= RHSResult.Val)
1762 return LHSResult;
1763 return RHSResult;
1764 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001765 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001766 case BinaryOperator::LOr: {
1767 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1768 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1769 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1770 // Rare case where the RHS has a comma "side-effect"; we need
1771 // to actually check the condition to see whether the side
1772 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001773 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001774 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001775 return RHSResult;
1776 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001777 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001778
Eli Friedman98c56a42009-02-26 09:29:13 +00001779 if (LHSResult.Val >= RHSResult.Val)
1780 return LHSResult;
1781 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001782 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001783 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001784 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001785 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001786 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001787 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001788 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001789 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001790 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001791 case Expr::CXXStaticCastExprClass:
1792 case Expr::CXXReinterpretCastExprClass:
1793 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001794 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1795 if (SubExpr->getType()->isIntegralType())
1796 return CheckICE(SubExpr, Ctx);
1797 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1798 return NoDiag();
1799 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001800 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001801 case Expr::ConditionalOperatorClass: {
1802 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001803 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001804 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001805 // expression, and it is fully evaluated. This is an important GNU
1806 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001807 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001808 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001809 Expr::EvalResult EVResult;
1810 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1811 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001812 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001813 }
1814 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001815 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001816 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1817 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1818 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1819 if (CondResult.Val == 2)
1820 return CondResult;
1821 if (TrueResult.Val == 2)
1822 return TrueResult;
1823 if (FalseResult.Val == 2)
1824 return FalseResult;
1825 if (CondResult.Val == 1)
1826 return CondResult;
1827 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1828 return NoDiag();
1829 // Rare case where the diagnostics depend on which side is evaluated
1830 // Note that if we get here, CondResult is 0, and at least one of
1831 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001832 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001833 return FalseResult;
1834 }
1835 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001836 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001837 case Expr::CXXDefaultArgExprClass:
1838 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001839 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001840 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001841 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001842 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001843
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001844 // Silence a GCC warning
1845 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001846}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001847
Eli Friedman98c56a42009-02-26 09:29:13 +00001848bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1849 SourceLocation *Loc, bool isEvaluated) const {
1850 ICEDiag d = CheckICE(this, Ctx);
1851 if (d.Val != 0) {
1852 if (Loc) *Loc = d.Loc;
1853 return false;
1854 }
1855 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001856 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001857 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001858 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1859 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001860 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001861 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001862}
1863
Chris Lattner7eef9192007-05-24 01:23:49 +00001864/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1865/// integer constant expression with the value zero, or if this is one that is
1866/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001867bool Expr::isNullPointerConstant(ASTContext &Ctx,
1868 NullPointerConstantValueDependence NPC) const {
1869 if (isValueDependent()) {
1870 switch (NPC) {
1871 case NPC_NeverValueDependent:
1872 assert(false && "Unexpected value dependent expression!");
1873 // If the unthinkable happens, fall through to the safest alternative.
1874
1875 case NPC_ValueDependentIsNull:
1876 return isTypeDependent() || getType()->isIntegralType();
1877
1878 case NPC_ValueDependentIsNotNull:
1879 return false;
1880 }
1881 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001882
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001883 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001884 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001885 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001886 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001887 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001888 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001889 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001890 Pointee->isVoidType() && // to void*
1891 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001892 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001893 }
Steve Naroffada7d422007-05-20 17:54:12 +00001894 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001895 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1896 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001897 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001898 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1899 // Accept ((void*)0) as a null pointer constant, as many other
1900 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001901 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001902 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001903 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001904 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001905 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001906 } else if (isa<GNUNullExpr>(this)) {
1907 // The GNU __null extension is always a null pointer constant.
1908 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001909 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001910
Sebastian Redl576fd422009-05-10 18:38:11 +00001911 // C++0x nullptr_t is always a null pointer constant.
1912 if (getType()->isNullPtrType())
1913 return true;
1914
Steve Naroff4871fe02008-01-14 16:10:57 +00001915 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001916 if (!getType()->isIntegerType() ||
1917 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001918 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001919
Chris Lattner1abbd412007-06-08 17:58:43 +00001920 // If we have an integer constant expression, we need to *evaluate* it and
1921 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001922 llvm::APSInt Result;
1923 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001924}
Steve Narofff7a5da12007-07-28 23:10:27 +00001925
Douglas Gregor71235ec2009-05-02 02:18:30 +00001926FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001927 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001928
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001929 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001930 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001931 if (Field->isBitField())
1932 return Field;
1933
1934 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1935 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1936 return BinOp->getLHS()->getBitField();
1937
1938 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001939}
1940
Chris Lattnerb8211f62009-02-16 22:14:05 +00001941/// isArrow - Return true if the base expression is a pointer to vector,
1942/// return false if the base expression is a vector.
1943bool ExtVectorElementExpr::isArrow() const {
1944 return getBase()->getType()->isPointerType();
1945}
1946
Nate Begemance4d7fc2008-04-18 23:10:10 +00001947unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001948 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001949 return VT->getNumElements();
1950 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001951}
1952
Nate Begemanf322eab2008-05-09 06:41:27 +00001953/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001954bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001955 // FIXME: Refactor this code to an accessor on the AST node which returns the
1956 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001957 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001958
1959 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001960 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001961 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001962
Nate Begeman7e5185b2009-01-18 02:01:21 +00001963 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001964 if (Comp[0] == 's' || Comp[0] == 'S')
1965 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001966
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001967 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1968 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001969 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001970
Steve Naroff0d595ca2007-07-30 03:29:09 +00001971 return false;
1972}
Chris Lattner885b4952007-08-02 23:36:59 +00001973
Nate Begemanf322eab2008-05-09 06:41:27 +00001974/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001975void ExtVectorElementExpr::getEncodedElementAccess(
1976 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001977 llvm::StringRef Comp = Accessor->getName();
1978 if (Comp[0] == 's' || Comp[0] == 'S')
1979 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001980
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001981 bool isHi = Comp == "hi";
1982 bool isLo = Comp == "lo";
1983 bool isEven = Comp == "even";
1984 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001985
Nate Begemanf322eab2008-05-09 06:41:27 +00001986 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1987 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001988
Nate Begemanf322eab2008-05-09 06:41:27 +00001989 if (isHi)
1990 Index = e + i;
1991 else if (isLo)
1992 Index = i;
1993 else if (isEven)
1994 Index = 2 * i;
1995 else if (isOdd)
1996 Index = 2 * i + 1;
1997 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001998 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001999
Nate Begemand3862152008-05-13 21:03:02 +00002000 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002001 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002002}
2003
Steve Narofff73590d2007-09-27 14:38:14 +00002004// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00002005ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002006 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00002007 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002008 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002009 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002010 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002011 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002012 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002013 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002014 if (NumArgs) {
2015 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002016 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2017 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002018 LBracloc = LBrac;
2019 RBracloc = RBrac;
2020}
2021
Mike Stump11289f42009-09-09 15:08:12 +00002022// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002023// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00002024ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002025 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00002026 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002027 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002028 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002029 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002030 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002031 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002032 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002033 if (NumArgs) {
2034 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002035 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2036 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002037 LBracloc = LBrac;
2038 RBracloc = RBrac;
2039}
2040
Mike Stump11289f42009-09-09 15:08:12 +00002041// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002042ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2043 QualType retType, ObjCMethodDecl *mproto,
2044 SourceLocation LBrac, SourceLocation RBrac,
2045 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002046: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002047MethodProto(mproto) {
2048 NumArgs = nargs;
2049 SubExprs = new Stmt*[NumArgs+1];
2050 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2051 if (NumArgs) {
2052 for (unsigned i = 0; i != NumArgs; ++i)
2053 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2054 }
2055 LBracloc = LBrac;
2056 RBracloc = RBrac;
2057}
2058
2059ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2060 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2061 switch (x & Flags) {
2062 default:
2063 assert(false && "Invalid ObjCMessageExpr.");
2064 case IsInstMeth:
2065 return ClassInfo(0, 0);
2066 case IsClsMethDeclUnknown:
2067 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2068 case IsClsMethDeclKnown: {
2069 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2070 return ClassInfo(D, D->getIdentifier());
2071 }
2072 }
2073}
2074
Chris Lattner7ec71da2009-04-26 00:44:05 +00002075void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2076 if (CI.first == 0 && CI.second == 0)
2077 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2078 else if (CI.first == 0)
2079 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2080 else
2081 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2082}
2083
2084
Chris Lattner35e564e2007-10-25 00:29:32 +00002085bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002086 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002087}
2088
Nate Begeman48745922009-08-12 02:28:50 +00002089void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2090 unsigned NumExprs) {
2091 if (SubExprs) C.Deallocate(SubExprs);
2092
2093 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002094 this->NumExprs = NumExprs;
2095 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002096}
Nate Begeman48745922009-08-12 02:28:50 +00002097
2098void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2099 DestroyChildren(C);
2100 if (SubExprs) C.Deallocate(SubExprs);
2101 this->~ShuffleVectorExpr();
2102 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002103}
2104
Douglas Gregore26a2852009-08-07 06:08:38 +00002105void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002106 // Override default behavior of traversing children. If this has a type
2107 // operand and the type is a variable-length array, the child iteration
2108 // will iterate over the size expression. However, this expression belongs
2109 // to the type, not to this, so we don't want to delete it.
2110 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002111 if (isArgumentType()) {
2112 this->~SizeOfAlignOfExpr();
2113 C.Deallocate(this);
2114 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002115 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002116 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002117}
2118
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002119//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002120// DesignatedInitExpr
2121//===----------------------------------------------------------------------===//
2122
2123IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2124 assert(Kind == FieldDesignator && "Only valid on a field designator");
2125 if (Field.NameOrField & 0x01)
2126 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2127 else
2128 return getField()->getIdentifier();
2129}
2130
Mike Stump11289f42009-09-09 15:08:12 +00002131DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002132 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002133 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002134 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002135 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002136 unsigned NumIndexExprs,
2137 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002138 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002139 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002140 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2141 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002142 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002143
2144 // Record the initializer itself.
2145 child_iterator Child = child_begin();
2146 *Child++ = Init;
2147
2148 // Copy the designators and their subexpressions, computing
2149 // value-dependence along the way.
2150 unsigned IndexIdx = 0;
2151 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002152 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002153
2154 if (this->Designators[I].isArrayDesignator()) {
2155 // Compute type- and value-dependence.
2156 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002157 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002158 Index->isTypeDependent() || Index->isValueDependent();
2159
2160 // Copy the index expressions into permanent storage.
2161 *Child++ = IndexExprs[IndexIdx++];
2162 } else if (this->Designators[I].isArrayRangeDesignator()) {
2163 // Compute type- and value-dependence.
2164 Expr *Start = IndexExprs[IndexIdx];
2165 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002166 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002167 Start->isTypeDependent() || Start->isValueDependent() ||
2168 End->isTypeDependent() || End->isValueDependent();
2169
2170 // Copy the start/end expressions into permanent storage.
2171 *Child++ = IndexExprs[IndexIdx++];
2172 *Child++ = IndexExprs[IndexIdx++];
2173 }
2174 }
2175
2176 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002177}
2178
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002179DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002180DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002181 unsigned NumDesignators,
2182 Expr **IndexExprs, unsigned NumIndexExprs,
2183 SourceLocation ColonOrEqualLoc,
2184 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002185 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002186 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002187 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2188 ColonOrEqualLoc, UsesColonSyntax,
2189 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002190}
2191
Mike Stump11289f42009-09-09 15:08:12 +00002192DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002193 unsigned NumIndexExprs) {
2194 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2195 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2196 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2197}
2198
Mike Stump11289f42009-09-09 15:08:12 +00002199void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002200 unsigned NumDesigs) {
2201 if (Designators)
2202 delete [] Designators;
2203
2204 Designators = new Designator[NumDesigs];
2205 NumDesignators = NumDesigs;
2206 for (unsigned I = 0; I != NumDesigs; ++I)
2207 Designators[I] = Desigs[I];
2208}
2209
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002210SourceRange DesignatedInitExpr::getSourceRange() const {
2211 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002212 Designator &First =
2213 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002214 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002215 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002216 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2217 else
2218 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2219 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002220 StartLoc =
2221 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002222 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2223}
2224
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002225Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2226 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2227 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2228 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002229 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2230 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2231}
2232
2233Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002234 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002235 "Requires array range designator");
2236 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2237 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002238 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2239 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2240}
2241
2242Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002243 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002244 "Requires array range designator");
2245 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2246 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002247 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2248 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2249}
2250
Douglas Gregord5846a12009-04-15 06:41:24 +00002251/// \brief Replaces the designator at index @p Idx with the series
2252/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00002253void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2254 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002255 const Designator *Last) {
2256 unsigned NumNewDesignators = Last - First;
2257 if (NumNewDesignators == 0) {
2258 std::copy_backward(Designators + Idx + 1,
2259 Designators + NumDesignators,
2260 Designators + Idx);
2261 --NumNewDesignators;
2262 return;
2263 } else if (NumNewDesignators == 1) {
2264 Designators[Idx] = *First;
2265 return;
2266 }
2267
Mike Stump11289f42009-09-09 15:08:12 +00002268 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002269 = new Designator[NumDesignators - 1 + NumNewDesignators];
2270 std::copy(Designators, Designators + Idx, NewDesignators);
2271 std::copy(First, Last, NewDesignators + Idx);
2272 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2273 NewDesignators + Idx + NumNewDesignators);
2274 delete [] Designators;
2275 Designators = NewDesignators;
2276 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2277}
2278
Douglas Gregore26a2852009-08-07 06:08:38 +00002279void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002280 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002281 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002282}
2283
Mike Stump11289f42009-09-09 15:08:12 +00002284ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002285 Expr **exprs, unsigned nexprs,
2286 SourceLocation rparenloc)
2287: Expr(ParenListExprClass, QualType(),
2288 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002289 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002290 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002291
Nate Begeman5ec4b312009-08-10 23:49:36 +00002292 Exprs = new (C) Stmt*[nexprs];
2293 for (unsigned i = 0; i != nexprs; ++i)
2294 Exprs[i] = exprs[i];
2295}
2296
2297void ParenListExpr::DoDestroy(ASTContext& C) {
2298 DestroyChildren(C);
2299 if (Exprs) C.Deallocate(Exprs);
2300 this->~ParenListExpr();
2301 C.Deallocate(this);
2302}
2303
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002304//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002305// ExprIterator.
2306//===----------------------------------------------------------------------===//
2307
2308Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2309Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2310Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2311const Expr* ConstExprIterator::operator[](size_t idx) const {
2312 return cast<Expr>(I[idx]);
2313}
2314const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2315const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2316
2317//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002318// Child Iterators for iterating over subexpressions/substatements
2319//===----------------------------------------------------------------------===//
2320
2321// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002322Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2323Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002324
Steve Naroffe46504b2007-11-12 14:29:37 +00002325// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002326Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2327Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002328
Steve Naroffebf4cb42008-06-02 23:03:37 +00002329// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002330Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2331Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002332
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002333// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002334Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2335 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002336}
Mike Stump11289f42009-09-09 15:08:12 +00002337Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2338 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002339}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002340
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002341// ObjCSuperExpr
2342Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2343Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2344
Steve Naroffe87026a2009-07-24 17:54:45 +00002345// ObjCIsaExpr
2346Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2347Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2348
Chris Lattner6307f192008-08-10 01:53:14 +00002349// PredefinedExpr
2350Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2351Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002352
2353// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002354Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2355Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002356
2357// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002358Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002359Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002360
2361// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002362Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2363Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002364
Chris Lattner1c20a172007-08-26 03:42:43 +00002365// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002366Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2367Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002368
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002369// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002370Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2371Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002372
2373// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002374Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2375Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002376
2377// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002378Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2379Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002380
Sebastian Redl6f282892008-11-11 17:56:53 +00002381// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002382Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002383 // If this is of a type and the type is a VLA type (and not a typedef), the
2384 // size expression of the VLA needs to be treated as an executable expression.
2385 // Why isn't this weirdness documented better in StmtIterator?
2386 if (isArgumentType()) {
2387 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2388 getArgumentType().getTypePtr()))
2389 return child_iterator(T);
2390 return child_iterator();
2391 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002392 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002393}
Sebastian Redl6f282892008-11-11 17:56:53 +00002394Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2395 if (isArgumentType())
2396 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002397 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002398}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002399
2400// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002401Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002402 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002403}
Ted Kremenek23702b62007-08-24 20:06:47 +00002404Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002405 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002406}
2407
2408// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002409Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002410 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002411}
Ted Kremenek23702b62007-08-24 20:06:47 +00002412Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002413 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002414}
Ted Kremenek23702b62007-08-24 20:06:47 +00002415
2416// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002417Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2418Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002419
Nate Begemance4d7fc2008-04-18 23:10:10 +00002420// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002421Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2422Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002423
2424// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002425Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2426Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002427
Ted Kremenek23702b62007-08-24 20:06:47 +00002428// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002429Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2430Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002431
2432// BinaryOperator
2433Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002434 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002435}
Ted Kremenek23702b62007-08-24 20:06:47 +00002436Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002437 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002438}
2439
2440// ConditionalOperator
2441Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002442 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002443}
Ted Kremenek23702b62007-08-24 20:06:47 +00002444Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002445 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002446}
2447
2448// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002449Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2450Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002451
Ted Kremenek23702b62007-08-24 20:06:47 +00002452// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002453Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2454Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002455
2456// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002457Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2458 return child_iterator();
2459}
2460
2461Stmt::child_iterator TypesCompatibleExpr::child_end() {
2462 return child_iterator();
2463}
Ted Kremenek23702b62007-08-24 20:06:47 +00002464
2465// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002466Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2467Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002468
Douglas Gregor3be4b122008-11-29 04:51:27 +00002469// GNUNullExpr
2470Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2471Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2472
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002473// ShuffleVectorExpr
2474Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002475 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002476}
2477Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002478 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002479}
2480
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002481// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002482Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2483Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002484
Anders Carlsson4692db02007-08-31 04:56:16 +00002485// InitListExpr
2486Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002487 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002488}
2489Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002490 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002491}
2492
Douglas Gregor0202cb42009-01-29 17:44:32 +00002493// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002494Stmt::child_iterator DesignatedInitExpr::child_begin() {
2495 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2496 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002497 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2498}
2499Stmt::child_iterator DesignatedInitExpr::child_end() {
2500 return child_iterator(&*child_begin() + NumSubExprs);
2501}
2502
Douglas Gregor0202cb42009-01-29 17:44:32 +00002503// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002504Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2505 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002506}
2507
Mike Stump11289f42009-09-09 15:08:12 +00002508Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2509 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002510}
2511
Nate Begeman5ec4b312009-08-10 23:49:36 +00002512// ParenListExpr
2513Stmt::child_iterator ParenListExpr::child_begin() {
2514 return &Exprs[0];
2515}
2516Stmt::child_iterator ParenListExpr::child_end() {
2517 return &Exprs[0]+NumExprs;
2518}
2519
Ted Kremenek23702b62007-08-24 20:06:47 +00002520// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002521Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002522 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002523}
2524Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002525 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002526}
Ted Kremenek23702b62007-08-24 20:06:47 +00002527
2528// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002529Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2530Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002531
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002532// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002533Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002534 return child_iterator();
2535}
2536Stmt::child_iterator ObjCSelectorExpr::child_end() {
2537 return child_iterator();
2538}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002539
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002540// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002541Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2542 return child_iterator();
2543}
2544Stmt::child_iterator ObjCProtocolExpr::child_end() {
2545 return child_iterator();
2546}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002547
Steve Naroffd54978b2007-09-18 23:55:05 +00002548// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002549Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002550 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002551}
2552Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002553 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002554}
2555
Steve Naroffc540d662008-09-03 18:15:37 +00002556// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002557Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2558Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002559
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002560Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2561Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }