blob: 139e04b2ed5e14c4b2ee387063e3bf98123d2a96 [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.
1050 if (isa<FieldDecl>(Member))
1051 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
1052
1053 // -- If it refers to a static member function [...], then
1054 // E1.E2 is an lvalue.
1055 // -- Otherwise, if E1.E2 refers to a non-static member
1056 // function [...], then E1.E2 is not an lvalue.
1057 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1058 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1059
1060 // -- If E2 is a member enumerator [...], the expression E1.E2
1061 // is not an lvalue.
1062 if (isa<EnumConstantDecl>(Member))
1063 return LV_InvalidExpression;
1064
1065 // Not an lvalue.
1066 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001067 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001068
1069 // C99 6.5.2.3p4
Chris Lattner67315442008-07-26 21:30:36 +00001070 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001071 }
Chris Lattner595db862007-10-30 22:53:42 +00001072 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001073 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001074 return LV_Valid; // C99 6.5.3p4
1075
1076 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001077 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1078 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001079 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001080
1081 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1082 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1083 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1084 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001085 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001086 case ImplicitCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +00001087 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregora11693b2008-11-12 17:17:38 +00001088 : LV_InvalidExpression;
Steve Naroff475cca02007-05-14 17:19:29 +00001089 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001090 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001091 case BinaryOperatorClass:
1092 case CompoundAssignOperatorClass: {
1093 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001094
1095 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1096 BinOp->getOpcode() == BinaryOperator::Comma)
1097 return BinOp->getRHS()->isLvalue(Ctx);
1098
Sebastian Redl112a97662009-02-07 00:15:38 +00001099 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001100 // The result of a .* expression is an lvalue only if its first operand is
1101 // an lvalue and its second operand is a pointer to data member.
1102 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001103 !BinOp->getType()->isFunctionType())
1104 return BinOp->getLHS()->isLvalue(Ctx);
1105
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001106 // The result of an ->* expression is an lvalue only if its second operand
1107 // is a pointer to data member.
1108 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1109 !BinOp->getType()->isFunctionType()) {
1110 QualType Ty = BinOp->getRHS()->getType();
1111 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1112 return LV_Valid;
1113 }
1114
Douglas Gregor58e008d2008-11-13 20:12:29 +00001115 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001116 return LV_InvalidExpression;
1117
Douglas Gregor58e008d2008-11-13 20:12:29 +00001118 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001119 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001120 // The result of an assignment operation [...] is an lvalue.
1121 return LV_Valid;
1122
1123
1124 // C99 6.5.16:
1125 // An assignment expression [...] is not an lvalue.
1126 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001129 case CXXOperatorCallExprClass:
1130 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001131 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001132 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001133 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001134 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1135 if (ReturnType->isLValueReferenceType())
1136 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001137
Douglas Gregor6b754842008-10-28 00:22:11 +00001138 break;
1139 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001140 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1141 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001142 case ChooseExprClass:
1143 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001144 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001145 case ExtVectorElementExprClass:
1146 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001147 return LV_DuplicateVectorComponents;
1148 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001149 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1150 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001151 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1152 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001153 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001154 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001155 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001156 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001157 case UnresolvedLookupExprClass:
1158 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001159 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001160 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001161 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001162 case CXXFunctionalCastExprClass:
1163 case CXXStaticCastExprClass:
1164 case CXXDynamicCastExprClass:
1165 case CXXReinterpretCastExprClass:
1166 case CXXConstCastExprClass:
1167 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001168 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001169 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1170 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001171 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1172 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001173 return LV_Valid;
1174 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001175 case CXXTypeidExprClass:
1176 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1177 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001178 case CXXBindTemporaryExprClass:
1179 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1180 isLvalueInternal(Ctx);
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001181 case ConditionalOperatorClass: {
1182 // Complicated handling is only for C++.
1183 if (!Ctx.getLangOptions().CPlusPlus)
1184 return LV_InvalidExpression;
1185
1186 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1187 // everywhere there's an object converted to an rvalue. Also, any other
1188 // casts should be wrapped by ImplicitCastExprs. There's just the special
1189 // case involving throws to work out.
1190 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001191 Expr *True = Cond->getTrueExpr();
1192 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001193 // C++0x 5.16p2
1194 // If either the second or the third operand has type (cv) void, [...]
1195 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001196 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001197 return LV_InvalidExpression;
1198
1199 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001200 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001201 return LV_InvalidExpression;
1202
1203 // That's it.
1204 return LV_Valid;
1205 }
1206
Steve Naroff9358c712007-05-27 23:58:33 +00001207 default:
1208 break;
Steve Naroff47500512007-04-19 23:00:49 +00001209 }
Steve Naroff9358c712007-05-27 23:58:33 +00001210 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001211}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001212
Steve Naroff475cca02007-05-14 17:19:29 +00001213/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1214/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001215/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001216/// recursively, any member or element of all contained aggregates or unions)
1217/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001218Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001219Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001220 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001221
Steve Naroff9358c712007-05-27 23:58:33 +00001222 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001223 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001224 // C++ 3.10p11: Functions cannot be modified, but pointers to
1225 // functions can be modifiable.
1226 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1227 return MLV_NotObjectType;
1228 break;
1229
Chris Lattner1ec5f562007-06-27 05:38:08 +00001230 case LV_NotObjectType: return MLV_NotObjectType;
1231 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001232 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001233 case LV_InvalidExpression:
1234 // If the top level is a C-style cast, and the subexpression is a valid
1235 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1236 // GCC extension. We don't support it, but we want to produce good
1237 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001238 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1239 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1240 if (Loc)
1241 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001242 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001243 }
1244 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001245 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001246 case LV_MemberFunction: return MLV_MemberFunction;
Steve Naroff9358c712007-05-27 23:58:33 +00001247 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001248
1249 // The following is illegal:
1250 // void takeclosure(void (^C)(void));
1251 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1252 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001253 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001254 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1255 return MLV_NotBlockQualified;
1256 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001257
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001258 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001259 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001260 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1261 if (Expr->getSetterMethod() == 0)
1262 return MLV_NoSetterProperty;
1263 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001264
Chris Lattner7adf0762008-08-04 07:31:14 +00001265 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattner7adf0762008-08-04 07:31:14 +00001267 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001268 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001269 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001270 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001271 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001272 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001273
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001274 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001275 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001276 return MLV_ConstQualified;
1277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
Mike Stump11289f42009-09-09 15:08:12 +00001279 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001280}
1281
Fariborz Jahanian07735332009-02-22 18:40:18 +00001282/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001283/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001284bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001285 switch (getStmtClass()) {
1286 default:
1287 return false;
1288 case ObjCIvarRefExprClass:
1289 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001290 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001291 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001292 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001293 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001294 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001295 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001296 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001297 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001298 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001299 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001300 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1301 if (VD->hasGlobalStorage())
1302 return true;
1303 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001304 // dereferencing to a pointer is always a gc'able candidate,
1305 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001306 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001307 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001308 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001309 return false;
1310 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001311 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001312 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001313 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001314 }
1315 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001316 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001317 }
1318}
Ted Kremenekfff70962008-01-17 16:57:34 +00001319Expr* Expr::IgnoreParens() {
1320 Expr* E = this;
1321 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1322 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001323
Ted Kremenekfff70962008-01-17 16:57:34 +00001324 return E;
1325}
1326
Chris Lattnerf2660962008-02-13 01:02:39 +00001327/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1328/// or CastExprs or ImplicitCastExprs, returning their operand.
1329Expr *Expr::IgnoreParenCasts() {
1330 Expr *E = this;
1331 while (true) {
1332 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1333 E = P->getSubExpr();
1334 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1335 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001336 else
1337 return E;
1338 }
1339}
1340
Chris Lattneref26c772009-03-13 17:28:01 +00001341/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1342/// value (including ptr->int casts of the same size). Strip off any
1343/// ParenExpr or CastExprs, returning their operand.
1344Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1345 Expr *E = this;
1346 while (true) {
1347 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1348 E = P->getSubExpr();
1349 continue;
1350 }
Mike Stump11289f42009-09-09 15:08:12 +00001351
Chris Lattneref26c772009-03-13 17:28:01 +00001352 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1353 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1354 // ptr<->int casts of the same width. We also ignore all identify casts.
1355 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattneref26c772009-03-13 17:28:01 +00001357 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1358 E = SE;
1359 continue;
1360 }
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattneref26c772009-03-13 17:28:01 +00001362 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1363 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1364 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1365 E = SE;
1366 continue;
1367 }
1368 }
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattneref26c772009-03-13 17:28:01 +00001370 return E;
1371 }
1372}
1373
Douglas Gregord196a582009-12-14 19:27:10 +00001374bool Expr::isDefaultArgument() const {
1375 const Expr *E = this;
1376 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1377 E = ICE->getSubExprAsWritten();
1378
1379 return isa<CXXDefaultArgExpr>(E);
1380}
Chris Lattneref26c772009-03-13 17:28:01 +00001381
Douglas Gregor4619e432008-12-05 23:32:09 +00001382/// hasAnyTypeDependentArguments - Determines if any of the expressions
1383/// in Exprs is type-dependent.
1384bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1385 for (unsigned I = 0; I < NumExprs; ++I)
1386 if (Exprs[I]->isTypeDependent())
1387 return true;
1388
1389 return false;
1390}
1391
1392/// hasAnyValueDependentArguments - Determines if any of the expressions
1393/// in Exprs is value-dependent.
1394bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1395 for (unsigned I = 0; I < NumExprs; ++I)
1396 if (Exprs[I]->isValueDependent())
1397 return true;
1398
1399 return false;
1400}
1401
Eli Friedman7139af42009-01-25 02:32:41 +00001402bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001403 // This function is attempting whether an expression is an initializer
1404 // which can be evaluated at compile-time. isEvaluatable handles most
1405 // of the cases, but it can't deal with some initializer-specific
1406 // expressions, and it can't deal with aggregates; we deal with those here,
1407 // and fall back to isEvaluatable for the other cases.
1408
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001409 // FIXME: This function assumes the variable being assigned to
1410 // isn't a reference type!
1411
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001412 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001413 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001414 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001415 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001416 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001417 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001418 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001419 // This handles gcc's extension that allows global initializers like
1420 // "struct x {int x;} x = (struct x) {};".
1421 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001422 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001423 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001424 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001425 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001426 // FIXME: This doesn't deal with fields with reference types correctly.
1427 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1428 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001429 const InitListExpr *Exp = cast<InitListExpr>(this);
1430 unsigned numInits = Exp->getNumInits();
1431 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001432 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001433 return false;
1434 }
Eli Friedman384da272009-01-25 03:12:18 +00001435 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001436 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001437 case ImplicitValueInitExprClass:
1438 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001439 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001440 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001441 case UnaryOperatorClass: {
1442 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1443 if (Exp->getOpcode() == UnaryOperator::Extension)
1444 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1445 break;
1446 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001447 case BinaryOperatorClass: {
1448 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1449 // but this handles the common case.
1450 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1451 if (Exp->getOpcode() == BinaryOperator::Sub &&
1452 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1453 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1454 return true;
1455 break;
1456 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001457 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001458 case CStyleCastExprClass:
1459 // Handle casts with a destination that's a struct or union; this
1460 // deals with both the gcc no-op struct cast extension and the
1461 // cast-to-union extension.
1462 if (getType()->isRecordType())
1463 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001464
1465 // Integer->integer casts can be handled here, which is important for
1466 // things like (int)(&&x-&&y). Scary but true.
1467 if (getType()->isIntegerType() &&
1468 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1469 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1470
Eli Friedman384da272009-01-25 03:12:18 +00001471 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001472 }
Eli Friedman384da272009-01-25 03:12:18 +00001473 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001474}
1475
Chris Lattner1f4479e2007-06-05 04:15:44 +00001476/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001477/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001478
1479/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1480/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001481///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001482/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1483/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1484/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001485
Eli Friedman98c56a42009-02-26 09:29:13 +00001486// CheckICE - This function does the fundamental ICE checking: the returned
1487// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1488// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001489// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001490// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001491// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001492//
1493// Meanings of Val:
1494// 0: This expression is an ICE if it can be evaluated by Evaluate.
1495// 1: This expression is not an ICE, but if it isn't evaluated, it's
1496// a legal subexpression for an ICE. This return value is used to handle
1497// the comma operator in C99 mode.
1498// 2: This expression is not an ICE, and is not a legal subexpression for one.
1499
1500struct ICEDiag {
1501 unsigned Val;
1502 SourceLocation Loc;
1503
1504 public:
1505 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1506 ICEDiag() : Val(0) {}
1507};
1508
1509ICEDiag NoDiag() { return ICEDiag(); }
1510
Eli Friedman90afd3d2009-02-27 04:07:58 +00001511static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1512 Expr::EvalResult EVResult;
1513 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1514 !EVResult.Val.isInt()) {
1515 return ICEDiag(2, E->getLocStart());
1516 }
1517 return NoDiag();
1518}
1519
Eli Friedman98c56a42009-02-26 09:29:13 +00001520static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001521 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001522 if (!E->getType()->isIntegralType()) {
1523 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001524 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001525
1526 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001527#define STMT(Node, Base) case Expr::Node##Class:
1528#define EXPR(Node, Base)
1529#include "clang/AST/StmtNodes.def"
1530 case Expr::PredefinedExprClass:
1531 case Expr::FloatingLiteralClass:
1532 case Expr::ImaginaryLiteralClass:
1533 case Expr::StringLiteralClass:
1534 case Expr::ArraySubscriptExprClass:
1535 case Expr::MemberExprClass:
1536 case Expr::CompoundAssignOperatorClass:
1537 case Expr::CompoundLiteralExprClass:
1538 case Expr::ExtVectorElementExprClass:
1539 case Expr::InitListExprClass:
1540 case Expr::DesignatedInitExprClass:
1541 case Expr::ImplicitValueInitExprClass:
1542 case Expr::ParenListExprClass:
1543 case Expr::VAArgExprClass:
1544 case Expr::AddrLabelExprClass:
1545 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001546 case Expr::CXXMemberCallExprClass:
1547 case Expr::CXXDynamicCastExprClass:
1548 case Expr::CXXTypeidExprClass:
1549 case Expr::CXXNullPtrLiteralExprClass:
1550 case Expr::CXXThisExprClass:
1551 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001552 case Expr::CXXNewExprClass:
1553 case Expr::CXXDeleteExprClass:
1554 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001555 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001556 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001557 case Expr::CXXConstructExprClass:
1558 case Expr::CXXBindTemporaryExprClass:
1559 case Expr::CXXExprWithTemporariesClass:
1560 case Expr::CXXTemporaryObjectExprClass:
1561 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001562 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001563 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001564 case Expr::ObjCStringLiteralClass:
1565 case Expr::ObjCEncodeExprClass:
1566 case Expr::ObjCMessageExprClass:
1567 case Expr::ObjCSelectorExprClass:
1568 case Expr::ObjCProtocolExprClass:
1569 case Expr::ObjCIvarRefExprClass:
1570 case Expr::ObjCPropertyRefExprClass:
1571 case Expr::ObjCImplicitSetterGetterRefExprClass:
1572 case Expr::ObjCSuperExprClass:
1573 case Expr::ObjCIsaExprClass:
1574 case Expr::ShuffleVectorExprClass:
1575 case Expr::BlockExprClass:
1576 case Expr::BlockDeclRefExprClass:
1577 case Expr::NoStmtClass:
1578 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001579 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001580
Douglas Gregor73341c42009-09-11 00:18:58 +00001581 case Expr::GNUNullExprClass:
1582 // GCC considers the GNU __null value to be an integral constant expression.
1583 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001584
Eli Friedman98c56a42009-02-26 09:29:13 +00001585 case Expr::ParenExprClass:
1586 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1587 case Expr::IntegerLiteralClass:
1588 case Expr::CharacterLiteralClass:
1589 case Expr::CXXBoolLiteralExprClass:
1590 case Expr::CXXZeroInitValueExprClass:
1591 case Expr::TypesCompatibleExprClass:
1592 case Expr::UnaryTypeTraitExprClass:
1593 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001594 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001595 case Expr::CXXOperatorCallExprClass: {
1596 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001597 if (CE->isBuiltinCall(Ctx))
1598 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001599 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001600 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001601 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001602 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1603 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001604 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001605 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001606 // C++ 7.1.5.1p2
1607 // A variable of non-volatile const-qualified integral or enumeration
1608 // type initialized by an ICE can be used in ICEs.
1609 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001610 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001611 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1612 if (Quals.hasVolatile() || !Quals.hasConst())
1613 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1614
1615 // Look for the definition of this variable, which will actually have
1616 // an initializer.
1617 const VarDecl *Def = 0;
1618 const Expr *Init = Dcl->getDefinition(Def);
1619 if (Init) {
1620 if (Def->isInitKnownICE()) {
1621 // We have already checked whether this subexpression is an
1622 // integral constant expression.
1623 if (Def->isInitICE())
1624 return NoDiag();
1625 else
1626 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1627 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001628
Douglas Gregor0840cc02009-11-01 20:32:48 +00001629 // C++ [class.static.data]p4:
1630 // If a static data member is of const integral or const
1631 // enumeration type, its declaration in the class definition can
1632 // specify a constant-initializer which shall be an integral
1633 // constant expression (5.19). In that case, the member can appear
1634 // in integral constant expressions.
1635 if (Def->isOutOfLine()) {
Eli Friedman1d6fb162009-12-03 20:31:57 +00001636 Dcl->setInitKnownICE(false);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001637 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1638 }
Eli Friedman1d6fb162009-12-03 20:31:57 +00001639
1640 if (Dcl->isCheckingICE()) {
1641 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1642 }
1643
1644 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001645 ICEDiag Result = CheckICE(Init, Ctx);
1646 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001647 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001648 return Result;
1649 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001650 }
1651 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001652 return ICEDiag(2, E->getLocStart());
1653 case Expr::UnaryOperatorClass: {
1654 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001655 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001656 case UnaryOperator::PostInc:
1657 case UnaryOperator::PostDec:
1658 case UnaryOperator::PreInc:
1659 case UnaryOperator::PreDec:
1660 case UnaryOperator::AddrOf:
1661 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001662 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001663
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001664 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001665 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001666 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001667 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001668 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001669 case UnaryOperator::Real:
1670 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001671 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001672 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001673 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1674 // Evaluate matches the proposed gcc behavior for cases like
1675 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1676 // compliance: we should warn earlier for offsetof expressions with
1677 // array subscripts that aren't ICEs, and if the array subscripts
1678 // are ICEs, the value of the offsetof must be an integer constant.
1679 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001680 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001681 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001682 case Expr::SizeOfAlignOfExprClass: {
1683 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1684 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1685 return ICEDiag(2, E->getLocStart());
1686 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001687 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001688 case Expr::BinaryOperatorClass: {
1689 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001690 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001691 case BinaryOperator::PtrMemD:
1692 case BinaryOperator::PtrMemI:
1693 case BinaryOperator::Assign:
1694 case BinaryOperator::MulAssign:
1695 case BinaryOperator::DivAssign:
1696 case BinaryOperator::RemAssign:
1697 case BinaryOperator::AddAssign:
1698 case BinaryOperator::SubAssign:
1699 case BinaryOperator::ShlAssign:
1700 case BinaryOperator::ShrAssign:
1701 case BinaryOperator::AndAssign:
1702 case BinaryOperator::XorAssign:
1703 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001704 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001705
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001706 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001707 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001708 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001709 case BinaryOperator::Add:
1710 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001711 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001712 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001713 case BinaryOperator::LT:
1714 case BinaryOperator::GT:
1715 case BinaryOperator::LE:
1716 case BinaryOperator::GE:
1717 case BinaryOperator::EQ:
1718 case BinaryOperator::NE:
1719 case BinaryOperator::And:
1720 case BinaryOperator::Xor:
1721 case BinaryOperator::Or:
1722 case BinaryOperator::Comma: {
1723 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1724 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001725 if (Exp->getOpcode() == BinaryOperator::Div ||
1726 Exp->getOpcode() == BinaryOperator::Rem) {
1727 // Evaluate gives an error for undefined Div/Rem, so make sure
1728 // we don't evaluate one.
1729 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1730 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1731 if (REval == 0)
1732 return ICEDiag(1, E->getLocStart());
1733 if (REval.isSigned() && REval.isAllOnesValue()) {
1734 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1735 if (LEval.isMinSignedValue())
1736 return ICEDiag(1, E->getLocStart());
1737 }
1738 }
1739 }
1740 if (Exp->getOpcode() == BinaryOperator::Comma) {
1741 if (Ctx.getLangOptions().C99) {
1742 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1743 // if it isn't evaluated.
1744 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1745 return ICEDiag(1, E->getLocStart());
1746 } else {
1747 // In both C89 and C++, commas in ICEs are illegal.
1748 return ICEDiag(2, E->getLocStart());
1749 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001750 }
1751 if (LHSResult.Val >= RHSResult.Val)
1752 return LHSResult;
1753 return RHSResult;
1754 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001755 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001756 case BinaryOperator::LOr: {
1757 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1758 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1759 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1760 // Rare case where the RHS has a comma "side-effect"; we need
1761 // to actually check the condition to see whether the side
1762 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001763 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001764 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001765 return RHSResult;
1766 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001767 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001768
Eli Friedman98c56a42009-02-26 09:29:13 +00001769 if (LHSResult.Val >= RHSResult.Val)
1770 return LHSResult;
1771 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001772 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001773 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001774 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001775 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001776 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001777 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001778 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001779 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001780 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001781 case Expr::CXXStaticCastExprClass:
1782 case Expr::CXXReinterpretCastExprClass:
1783 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001784 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1785 if (SubExpr->getType()->isIntegralType())
1786 return CheckICE(SubExpr, Ctx);
1787 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1788 return NoDiag();
1789 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001790 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001791 case Expr::ConditionalOperatorClass: {
1792 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001793 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001794 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001795 // expression, and it is fully evaluated. This is an important GNU
1796 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001797 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001798 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001799 Expr::EvalResult EVResult;
1800 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1801 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001802 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001803 }
1804 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001805 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001806 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1807 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1808 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1809 if (CondResult.Val == 2)
1810 return CondResult;
1811 if (TrueResult.Val == 2)
1812 return TrueResult;
1813 if (FalseResult.Val == 2)
1814 return FalseResult;
1815 if (CondResult.Val == 1)
1816 return CondResult;
1817 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1818 return NoDiag();
1819 // Rare case where the diagnostics depend on which side is evaluated
1820 // Note that if we get here, CondResult is 0, and at least one of
1821 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001822 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001823 return FalseResult;
1824 }
1825 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001826 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001827 case Expr::CXXDefaultArgExprClass:
1828 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001829 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001830 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001831 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001832 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001833
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001834 // Silence a GCC warning
1835 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001836}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001837
Eli Friedman98c56a42009-02-26 09:29:13 +00001838bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1839 SourceLocation *Loc, bool isEvaluated) const {
1840 ICEDiag d = CheckICE(this, Ctx);
1841 if (d.Val != 0) {
1842 if (Loc) *Loc = d.Loc;
1843 return false;
1844 }
1845 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001846 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00001847 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001848 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1849 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001850 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001851 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001852}
1853
Chris Lattner7eef9192007-05-24 01:23:49 +00001854/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1855/// integer constant expression with the value zero, or if this is one that is
1856/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001857bool Expr::isNullPointerConstant(ASTContext &Ctx,
1858 NullPointerConstantValueDependence NPC) const {
1859 if (isValueDependent()) {
1860 switch (NPC) {
1861 case NPC_NeverValueDependent:
1862 assert(false && "Unexpected value dependent expression!");
1863 // If the unthinkable happens, fall through to the safest alternative.
1864
1865 case NPC_ValueDependentIsNull:
1866 return isTypeDependent() || getType()->isIntegralType();
1867
1868 case NPC_ValueDependentIsNotNull:
1869 return false;
1870 }
1871 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001872
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001873 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001874 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001875 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001876 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001877 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001878 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001879 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001880 Pointee->isVoidType() && // to void*
1881 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001882 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001883 }
Steve Naroffada7d422007-05-20 17:54:12 +00001884 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001885 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1886 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001887 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001888 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1889 // Accept ((void*)0) as a null pointer constant, as many other
1890 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001891 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001892 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001893 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001894 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001895 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001896 } else if (isa<GNUNullExpr>(this)) {
1897 // The GNU __null extension is always a null pointer constant.
1898 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001899 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001900
Sebastian Redl576fd422009-05-10 18:38:11 +00001901 // C++0x nullptr_t is always a null pointer constant.
1902 if (getType()->isNullPtrType())
1903 return true;
1904
Steve Naroff4871fe02008-01-14 16:10:57 +00001905 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001906 if (!getType()->isIntegerType() ||
1907 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001908 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattner1abbd412007-06-08 17:58:43 +00001910 // If we have an integer constant expression, we need to *evaluate* it and
1911 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001912 llvm::APSInt Result;
1913 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001914}
Steve Narofff7a5da12007-07-28 23:10:27 +00001915
Douglas Gregor71235ec2009-05-02 02:18:30 +00001916FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001917 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001918
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001919 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001920 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001921 if (Field->isBitField())
1922 return Field;
1923
1924 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1925 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1926 return BinOp->getLHS()->getBitField();
1927
1928 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001929}
1930
Chris Lattnerb8211f62009-02-16 22:14:05 +00001931/// isArrow - Return true if the base expression is a pointer to vector,
1932/// return false if the base expression is a vector.
1933bool ExtVectorElementExpr::isArrow() const {
1934 return getBase()->getType()->isPointerType();
1935}
1936
Nate Begemance4d7fc2008-04-18 23:10:10 +00001937unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001938 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001939 return VT->getNumElements();
1940 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001941}
1942
Nate Begemanf322eab2008-05-09 06:41:27 +00001943/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001944bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001945 // FIXME: Refactor this code to an accessor on the AST node which returns the
1946 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001947 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001948
1949 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001950 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001951 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001952
Nate Begeman7e5185b2009-01-18 02:01:21 +00001953 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001954 if (Comp[0] == 's' || Comp[0] == 'S')
1955 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001956
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001957 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1958 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001959 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001960
Steve Naroff0d595ca2007-07-30 03:29:09 +00001961 return false;
1962}
Chris Lattner885b4952007-08-02 23:36:59 +00001963
Nate Begemanf322eab2008-05-09 06:41:27 +00001964/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001965void ExtVectorElementExpr::getEncodedElementAccess(
1966 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001967 llvm::StringRef Comp = Accessor->getName();
1968 if (Comp[0] == 's' || Comp[0] == 'S')
1969 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001970
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001971 bool isHi = Comp == "hi";
1972 bool isLo = Comp == "lo";
1973 bool isEven = Comp == "even";
1974 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001975
Nate Begemanf322eab2008-05-09 06:41:27 +00001976 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1977 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001978
Nate Begemanf322eab2008-05-09 06:41:27 +00001979 if (isHi)
1980 Index = e + i;
1981 else if (isLo)
1982 Index = i;
1983 else if (isEven)
1984 Index = 2 * i;
1985 else if (isOdd)
1986 Index = 2 * i + 1;
1987 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001988 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001989
Nate Begemand3862152008-05-13 21:03:02 +00001990 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001991 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001992}
1993
Steve Narofff73590d2007-09-27 14:38:14 +00001994// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00001995ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001996 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001997 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001998 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001999 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002000 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002001 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002002 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002003 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002004 if (NumArgs) {
2005 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002006 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2007 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002008 LBracloc = LBrac;
2009 RBracloc = RBrac;
2010}
2011
Mike Stump11289f42009-09-09 15:08:12 +00002012// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002013// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00002014ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002015 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00002016 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002017 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002018 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002019 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002020 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00002021 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002022 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002023 if (NumArgs) {
2024 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002025 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2026 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002027 LBracloc = LBrac;
2028 RBracloc = RBrac;
2029}
2030
Mike Stump11289f42009-09-09 15:08:12 +00002031// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002032ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2033 QualType retType, ObjCMethodDecl *mproto,
2034 SourceLocation LBrac, SourceLocation RBrac,
2035 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002036: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002037MethodProto(mproto) {
2038 NumArgs = nargs;
2039 SubExprs = new Stmt*[NumArgs+1];
2040 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2041 if (NumArgs) {
2042 for (unsigned i = 0; i != NumArgs; ++i)
2043 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2044 }
2045 LBracloc = LBrac;
2046 RBracloc = RBrac;
2047}
2048
2049ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2050 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2051 switch (x & Flags) {
2052 default:
2053 assert(false && "Invalid ObjCMessageExpr.");
2054 case IsInstMeth:
2055 return ClassInfo(0, 0);
2056 case IsClsMethDeclUnknown:
2057 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2058 case IsClsMethDeclKnown: {
2059 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2060 return ClassInfo(D, D->getIdentifier());
2061 }
2062 }
2063}
2064
Chris Lattner7ec71da2009-04-26 00:44:05 +00002065void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2066 if (CI.first == 0 && CI.second == 0)
2067 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2068 else if (CI.first == 0)
2069 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2070 else
2071 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2072}
2073
2074
Chris Lattner35e564e2007-10-25 00:29:32 +00002075bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002076 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002077}
2078
Nate Begeman48745922009-08-12 02:28:50 +00002079void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2080 unsigned NumExprs) {
2081 if (SubExprs) C.Deallocate(SubExprs);
2082
2083 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002084 this->NumExprs = NumExprs;
2085 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002086}
Nate Begeman48745922009-08-12 02:28:50 +00002087
2088void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2089 DestroyChildren(C);
2090 if (SubExprs) C.Deallocate(SubExprs);
2091 this->~ShuffleVectorExpr();
2092 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002093}
2094
Douglas Gregore26a2852009-08-07 06:08:38 +00002095void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002096 // Override default behavior of traversing children. If this has a type
2097 // operand and the type is a variable-length array, the child iteration
2098 // will iterate over the size expression. However, this expression belongs
2099 // to the type, not to this, so we don't want to delete it.
2100 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002101 if (isArgumentType()) {
2102 this->~SizeOfAlignOfExpr();
2103 C.Deallocate(this);
2104 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002105 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002106 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002107}
2108
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002109//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002110// DesignatedInitExpr
2111//===----------------------------------------------------------------------===//
2112
2113IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2114 assert(Kind == FieldDesignator && "Only valid on a field designator");
2115 if (Field.NameOrField & 0x01)
2116 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2117 else
2118 return getField()->getIdentifier();
2119}
2120
Mike Stump11289f42009-09-09 15:08:12 +00002121DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002122 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002123 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002124 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002125 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002126 unsigned NumIndexExprs,
2127 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002128 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002129 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002130 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2131 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002132 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002133
2134 // Record the initializer itself.
2135 child_iterator Child = child_begin();
2136 *Child++ = Init;
2137
2138 // Copy the designators and their subexpressions, computing
2139 // value-dependence along the way.
2140 unsigned IndexIdx = 0;
2141 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002142 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002143
2144 if (this->Designators[I].isArrayDesignator()) {
2145 // Compute type- and value-dependence.
2146 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002147 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002148 Index->isTypeDependent() || Index->isValueDependent();
2149
2150 // Copy the index expressions into permanent storage.
2151 *Child++ = IndexExprs[IndexIdx++];
2152 } else if (this->Designators[I].isArrayRangeDesignator()) {
2153 // Compute type- and value-dependence.
2154 Expr *Start = IndexExprs[IndexIdx];
2155 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002156 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002157 Start->isTypeDependent() || Start->isValueDependent() ||
2158 End->isTypeDependent() || End->isValueDependent();
2159
2160 // Copy the start/end expressions into permanent storage.
2161 *Child++ = IndexExprs[IndexIdx++];
2162 *Child++ = IndexExprs[IndexIdx++];
2163 }
2164 }
2165
2166 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002167}
2168
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002169DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002170DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002171 unsigned NumDesignators,
2172 Expr **IndexExprs, unsigned NumIndexExprs,
2173 SourceLocation ColonOrEqualLoc,
2174 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002175 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002176 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002177 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2178 ColonOrEqualLoc, UsesColonSyntax,
2179 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002180}
2181
Mike Stump11289f42009-09-09 15:08:12 +00002182DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002183 unsigned NumIndexExprs) {
2184 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2185 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2186 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2187}
2188
Mike Stump11289f42009-09-09 15:08:12 +00002189void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002190 unsigned NumDesigs) {
2191 if (Designators)
2192 delete [] Designators;
2193
2194 Designators = new Designator[NumDesigs];
2195 NumDesignators = NumDesigs;
2196 for (unsigned I = 0; I != NumDesigs; ++I)
2197 Designators[I] = Desigs[I];
2198}
2199
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002200SourceRange DesignatedInitExpr::getSourceRange() const {
2201 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002202 Designator &First =
2203 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002204 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002205 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002206 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2207 else
2208 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2209 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002210 StartLoc =
2211 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002212 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2213}
2214
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002215Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2216 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2217 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2218 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002219 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2220 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2221}
2222
2223Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002224 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002225 "Requires array range designator");
2226 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2227 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002228 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2229 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2230}
2231
2232Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002233 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002234 "Requires array range designator");
2235 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2236 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002237 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2238 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2239}
2240
Douglas Gregord5846a12009-04-15 06:41:24 +00002241/// \brief Replaces the designator at index @p Idx with the series
2242/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00002243void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2244 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002245 const Designator *Last) {
2246 unsigned NumNewDesignators = Last - First;
2247 if (NumNewDesignators == 0) {
2248 std::copy_backward(Designators + Idx + 1,
2249 Designators + NumDesignators,
2250 Designators + Idx);
2251 --NumNewDesignators;
2252 return;
2253 } else if (NumNewDesignators == 1) {
2254 Designators[Idx] = *First;
2255 return;
2256 }
2257
Mike Stump11289f42009-09-09 15:08:12 +00002258 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002259 = new Designator[NumDesignators - 1 + NumNewDesignators];
2260 std::copy(Designators, Designators + Idx, NewDesignators);
2261 std::copy(First, Last, NewDesignators + Idx);
2262 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2263 NewDesignators + Idx + NumNewDesignators);
2264 delete [] Designators;
2265 Designators = NewDesignators;
2266 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2267}
2268
Douglas Gregore26a2852009-08-07 06:08:38 +00002269void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002270 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002271 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002272}
2273
Mike Stump11289f42009-09-09 15:08:12 +00002274ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002275 Expr **exprs, unsigned nexprs,
2276 SourceLocation rparenloc)
2277: Expr(ParenListExprClass, QualType(),
2278 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002279 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002280 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002281
Nate Begeman5ec4b312009-08-10 23:49:36 +00002282 Exprs = new (C) Stmt*[nexprs];
2283 for (unsigned i = 0; i != nexprs; ++i)
2284 Exprs[i] = exprs[i];
2285}
2286
2287void ParenListExpr::DoDestroy(ASTContext& C) {
2288 DestroyChildren(C);
2289 if (Exprs) C.Deallocate(Exprs);
2290 this->~ParenListExpr();
2291 C.Deallocate(this);
2292}
2293
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002294//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002295// ExprIterator.
2296//===----------------------------------------------------------------------===//
2297
2298Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2299Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2300Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2301const Expr* ConstExprIterator::operator[](size_t idx) const {
2302 return cast<Expr>(I[idx]);
2303}
2304const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2305const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2306
2307//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002308// Child Iterators for iterating over subexpressions/substatements
2309//===----------------------------------------------------------------------===//
2310
2311// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002312Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2313Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002314
Steve Naroffe46504b2007-11-12 14:29:37 +00002315// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002316Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2317Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002318
Steve Naroffebf4cb42008-06-02 23:03:37 +00002319// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002320Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2321Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002322
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002323// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002324Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2325 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002326}
Mike Stump11289f42009-09-09 15:08:12 +00002327Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2328 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002329}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002330
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002331// ObjCSuperExpr
2332Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2333Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2334
Steve Naroffe87026a2009-07-24 17:54:45 +00002335// ObjCIsaExpr
2336Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2337Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2338
Chris Lattner6307f192008-08-10 01:53:14 +00002339// PredefinedExpr
2340Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2341Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002342
2343// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002344Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2345Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002346
2347// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002348Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002349Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002350
2351// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002352Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2353Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002354
Chris Lattner1c20a172007-08-26 03:42:43 +00002355// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002356Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2357Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002358
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002359// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002360Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2361Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002362
2363// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002364Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2365Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002366
2367// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002368Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2369Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002370
Sebastian Redl6f282892008-11-11 17:56:53 +00002371// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002372Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002373 // If this is of a type and the type is a VLA type (and not a typedef), the
2374 // size expression of the VLA needs to be treated as an executable expression.
2375 // Why isn't this weirdness documented better in StmtIterator?
2376 if (isArgumentType()) {
2377 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2378 getArgumentType().getTypePtr()))
2379 return child_iterator(T);
2380 return child_iterator();
2381 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002382 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002383}
Sebastian Redl6f282892008-11-11 17:56:53 +00002384Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2385 if (isArgumentType())
2386 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002387 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002388}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002389
2390// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002391Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002392 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002393}
Ted Kremenek23702b62007-08-24 20:06:47 +00002394Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002395 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002396}
2397
2398// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002399Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002400 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002401}
Ted Kremenek23702b62007-08-24 20:06:47 +00002402Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002403 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002404}
Ted Kremenek23702b62007-08-24 20:06:47 +00002405
2406// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002407Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2408Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002409
Nate Begemance4d7fc2008-04-18 23:10:10 +00002410// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002411Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2412Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002413
2414// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002415Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2416Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002417
Ted Kremenek23702b62007-08-24 20:06:47 +00002418// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002419Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2420Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002421
2422// BinaryOperator
2423Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002424 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002425}
Ted Kremenek23702b62007-08-24 20:06:47 +00002426Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002427 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002428}
2429
2430// ConditionalOperator
2431Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002432 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002433}
Ted Kremenek23702b62007-08-24 20:06:47 +00002434Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002435 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002436}
2437
2438// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002439Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2440Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002441
Ted Kremenek23702b62007-08-24 20:06:47 +00002442// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002443Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2444Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002445
2446// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002447Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2448 return child_iterator();
2449}
2450
2451Stmt::child_iterator TypesCompatibleExpr::child_end() {
2452 return child_iterator();
2453}
Ted Kremenek23702b62007-08-24 20:06:47 +00002454
2455// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002456Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2457Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002458
Douglas Gregor3be4b122008-11-29 04:51:27 +00002459// GNUNullExpr
2460Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2461Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2462
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002463// ShuffleVectorExpr
2464Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002465 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002466}
2467Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002468 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002469}
2470
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002471// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002472Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2473Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002474
Anders Carlsson4692db02007-08-31 04:56:16 +00002475// InitListExpr
2476Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002477 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002478}
2479Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002480 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002481}
2482
Douglas Gregor0202cb42009-01-29 17:44:32 +00002483// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002484Stmt::child_iterator DesignatedInitExpr::child_begin() {
2485 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2486 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002487 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2488}
2489Stmt::child_iterator DesignatedInitExpr::child_end() {
2490 return child_iterator(&*child_begin() + NumSubExprs);
2491}
2492
Douglas Gregor0202cb42009-01-29 17:44:32 +00002493// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002494Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2495 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002496}
2497
Mike Stump11289f42009-09-09 15:08:12 +00002498Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2499 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002500}
2501
Nate Begeman5ec4b312009-08-10 23:49:36 +00002502// ParenListExpr
2503Stmt::child_iterator ParenListExpr::child_begin() {
2504 return &Exprs[0];
2505}
2506Stmt::child_iterator ParenListExpr::child_end() {
2507 return &Exprs[0]+NumExprs;
2508}
2509
Ted Kremenek23702b62007-08-24 20:06:47 +00002510// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002511Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002512 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002513}
2514Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002515 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002516}
Ted Kremenek23702b62007-08-24 20:06:47 +00002517
2518// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002519Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2520Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002521
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002522// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002523Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002524 return child_iterator();
2525}
2526Stmt::child_iterator ObjCSelectorExpr::child_end() {
2527 return child_iterator();
2528}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002529
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002530// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002531Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2532 return child_iterator();
2533}
2534Stmt::child_iterator ObjCProtocolExpr::child_end() {
2535 return child_iterator();
2536}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002537
Steve Naroffd54978b2007-09-18 23:55:05 +00002538// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002539Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002540 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002541}
2542Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002543 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002544}
2545
Steve Naroffc540d662008-09-03 18:15:37 +00002546// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002547Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2548Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002549
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002550Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2551Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }