blob: 7c80f04be261a427e02a2591a5fba85ab855ae2a [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,
113 NamedDecl *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) {
John McCalld14a8642009-11-21 08:51:07 +0000121 assert(!isa<OverloadedFunctionDecl>(D));
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000122 if (Qualifier) {
123 NameQualifier *NQ = getNameQualifier();
124 NQ->NNS = Qualifier;
125 NQ->Range = QualifierRange;
126 }
127
John McCall6b51f282009-11-23 01:53:49 +0000128 if (TemplateArgs)
129 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000130
131 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000132}
133
134DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
135 NestedNameSpecifier *Qualifier,
136 SourceRange QualifierRange,
137 NamedDecl *D,
138 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000139 QualType T,
140 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000141 std::size_t Size = sizeof(DeclRefExpr);
142 if (Qualifier != 0)
143 Size += sizeof(NameQualifier);
144
John McCall6b51f282009-11-23 01:53:49 +0000145 if (TemplateArgs)
146 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000147
148 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
149 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000150 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000151}
152
153SourceRange DeclRefExpr::getSourceRange() const {
154 // FIXME: Does not handle multi-token names well, e.g., operator[].
155 SourceRange R(Loc);
156
157 if (hasQualifier())
158 R.setBegin(getQualifierRange().getBegin());
159 if (hasExplicitTemplateArgumentList())
160 R.setEnd(getRAngleLoc());
161 return R;
162}
163
Anders Carlsson2fb08242009-09-08 18:24:21 +0000164// FIXME: Maybe this should use DeclPrinter with a special "print predefined
165// expr" policy instead.
166std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
167 const Decl *CurrentDecl) {
168 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
169 if (IT != PrettyFunction)
170 return FD->getNameAsString();
171
172 llvm::SmallString<256> Name;
173 llvm::raw_svector_ostream Out(Name);
174
175 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
176 if (MD->isVirtual())
177 Out << "virtual ";
178 }
179
180 PrintingPolicy Policy(Context.getLangOptions());
181 Policy.SuppressTagKind = true;
182
183 std::string Proto = FD->getQualifiedNameAsString(Policy);
184
John McCall9dd450b2009-09-21 23:43:11 +0000185 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000186 const FunctionProtoType *FT = 0;
187 if (FD->hasWrittenPrototype())
188 FT = dyn_cast<FunctionProtoType>(AFT);
189
190 Proto += "(";
191 if (FT) {
192 llvm::raw_string_ostream POut(Proto);
193 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
194 if (i) POut << ", ";
195 std::string Param;
196 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
197 POut << Param;
198 }
199
200 if (FT->isVariadic()) {
201 if (FD->getNumParams()) POut << ", ";
202 POut << "...";
203 }
204 }
205 Proto += ")";
206
207 AFT->getResultType().getAsStringInternal(Proto, Policy);
208
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,
474 SourceRange qualrange, NamedDecl *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,
Mike Stump11289f42009-09-09 15:08:12 +0000497 NamedDecl *memberdecl,
498 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 Carlsson496335e2009-09-03 00:59:21 +0000559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Anders Carlsson496335e2009-09-03 00:59:21 +0000561 assert(0 && "Unhandled cast kind!");
562 return 0;
563}
564
Chris Lattner1b926492006-08-23 06:42:10 +0000565/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
566/// corresponds to, e.g. "<<=".
567const char *BinaryOperator::getOpcodeStr(Opcode Op) {
568 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000569 case PtrMemD: return ".*";
570 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000571 case Mul: return "*";
572 case Div: return "/";
573 case Rem: return "%";
574 case Add: return "+";
575 case Sub: return "-";
576 case Shl: return "<<";
577 case Shr: return ">>";
578 case LT: return "<";
579 case GT: return ">";
580 case LE: return "<=";
581 case GE: return ">=";
582 case EQ: return "==";
583 case NE: return "!=";
584 case And: return "&";
585 case Xor: return "^";
586 case Or: return "|";
587 case LAnd: return "&&";
588 case LOr: return "||";
589 case Assign: return "=";
590 case MulAssign: return "*=";
591 case DivAssign: return "/=";
592 case RemAssign: return "%=";
593 case AddAssign: return "+=";
594 case SubAssign: return "-=";
595 case ShlAssign: return "<<=";
596 case ShrAssign: return ">>=";
597 case AndAssign: return "&=";
598 case XorAssign: return "^=";
599 case OrAssign: return "|=";
600 case Comma: return ",";
601 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000602
603 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000604}
Steve Naroff47500512007-04-19 23:00:49 +0000605
Mike Stump11289f42009-09-09 15:08:12 +0000606BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000607BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
608 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000609 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000610 case OO_Plus: return Add;
611 case OO_Minus: return Sub;
612 case OO_Star: return Mul;
613 case OO_Slash: return Div;
614 case OO_Percent: return Rem;
615 case OO_Caret: return Xor;
616 case OO_Amp: return And;
617 case OO_Pipe: return Or;
618 case OO_Equal: return Assign;
619 case OO_Less: return LT;
620 case OO_Greater: return GT;
621 case OO_PlusEqual: return AddAssign;
622 case OO_MinusEqual: return SubAssign;
623 case OO_StarEqual: return MulAssign;
624 case OO_SlashEqual: return DivAssign;
625 case OO_PercentEqual: return RemAssign;
626 case OO_CaretEqual: return XorAssign;
627 case OO_AmpEqual: return AndAssign;
628 case OO_PipeEqual: return OrAssign;
629 case OO_LessLess: return Shl;
630 case OO_GreaterGreater: return Shr;
631 case OO_LessLessEqual: return ShlAssign;
632 case OO_GreaterGreaterEqual: return ShrAssign;
633 case OO_EqualEqual: return EQ;
634 case OO_ExclaimEqual: return NE;
635 case OO_LessEqual: return LE;
636 case OO_GreaterEqual: return GE;
637 case OO_AmpAmp: return LAnd;
638 case OO_PipePipe: return LOr;
639 case OO_Comma: return Comma;
640 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000641 }
642}
643
644OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
645 static const OverloadedOperatorKind OverOps[] = {
646 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
647 OO_Star, OO_Slash, OO_Percent,
648 OO_Plus, OO_Minus,
649 OO_LessLess, OO_GreaterGreater,
650 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
651 OO_EqualEqual, OO_ExclaimEqual,
652 OO_Amp,
653 OO_Caret,
654 OO_Pipe,
655 OO_AmpAmp,
656 OO_PipePipe,
657 OO_Equal, OO_StarEqual,
658 OO_SlashEqual, OO_PercentEqual,
659 OO_PlusEqual, OO_MinusEqual,
660 OO_LessLessEqual, OO_GreaterGreaterEqual,
661 OO_AmpEqual, OO_CaretEqual,
662 OO_PipeEqual,
663 OO_Comma
664 };
665 return OverOps[Opc];
666}
667
Mike Stump11289f42009-09-09 15:08:12 +0000668InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000669 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000670 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000671 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump11289f42009-09-09 15:08:12 +0000672 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000673 UnionFieldInit(0), HadArrayRangeDesignator(false)
674{
675 for (unsigned I = 0; I != numInits; ++I) {
676 if (initExprs[I]->isTypeDependent())
677 TypeDependent = true;
678 if (initExprs[I]->isValueDependent())
679 ValueDependent = true;
680 }
681
Chris Lattner07d754a2008-10-26 23:43:26 +0000682 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000683}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000684
Douglas Gregor6d00c992009-03-20 23:58:33 +0000685void InitListExpr::reserveInits(unsigned NumInits) {
686 if (NumInits > InitExprs.size())
687 InitExprs.reserve(NumInits);
688}
689
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000690void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattner8ba22472009-02-16 22:33:34 +0000691 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbar45a2a202009-02-16 22:42:44 +0000692 Idx < LastIdx; ++Idx)
Douglas Gregor52a47e92009-03-20 23:38:03 +0000693 InitExprs[Idx]->Destroy(Context);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000694 InitExprs.resize(NumInits, 0);
695}
696
697Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
698 if (Init >= InitExprs.size()) {
699 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
700 InitExprs.back() = expr;
701 return 0;
702 }
Mike Stump11289f42009-09-09 15:08:12 +0000703
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000704 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
705 InitExprs[Init] = expr;
706 return Result;
707}
708
Steve Naroff991e99d2008-09-04 15:31:07 +0000709/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000710///
711const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000712 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000713 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000714}
715
Mike Stump11289f42009-09-09 15:08:12 +0000716SourceLocation BlockExpr::getCaretLocation() const {
717 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000718}
Mike Stump11289f42009-09-09 15:08:12 +0000719const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000720 return TheBlock->getBody();
721}
Mike Stump11289f42009-09-09 15:08:12 +0000722Stmt *BlockExpr::getBody() {
723 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000724}
Steve Naroff415d3d52008-10-08 17:01:13 +0000725
726
Chris Lattner1ec5f562007-06-27 05:38:08 +0000727//===----------------------------------------------------------------------===//
728// Generic Expression Routines
729//===----------------------------------------------------------------------===//
730
Chris Lattner237f2752009-02-14 07:37:35 +0000731/// isUnusedResultAWarning - Return true if this immediate expression should
732/// be warned about if the result is unused. If so, fill in Loc and Ranges
733/// with location to warn on and the source range[s] to report with the
734/// warning.
735bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000736 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000737 // Don't warn if the expr is type dependent. The type could end up
738 // instantiating to void.
739 if (isTypeDependent())
740 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000741
Chris Lattner1ec5f562007-06-27 05:38:08 +0000742 switch (getStmtClass()) {
743 default:
Chris Lattner237f2752009-02-14 07:37:35 +0000744 Loc = getExprLoc();
745 R1 = getSourceRange();
746 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000747 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000748 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000749 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000750 case UnaryOperatorClass: {
751 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner1ec5f562007-06-27 05:38:08 +0000753 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000754 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000755 case UnaryOperator::PostInc:
756 case UnaryOperator::PostDec:
757 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000758 case UnaryOperator::PreDec: // ++/--
759 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000760 case UnaryOperator::Deref:
761 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000762 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000763 return false;
764 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000765 case UnaryOperator::Real:
766 case UnaryOperator::Imag:
767 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000768 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
769 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000770 return false;
771 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000772 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000773 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000774 }
Chris Lattner237f2752009-02-14 07:37:35 +0000775 Loc = UO->getOperatorLoc();
776 R1 = UO->getSubExpr()->getSourceRange();
777 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000778 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000779 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000780 const BinaryOperator *BO = cast<BinaryOperator>(this);
781 // Consider comma to have side effects if the LHS or RHS does.
782 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stump53f9ded2009-11-03 23:25:48 +0000783 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
784 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump11289f42009-09-09 15:08:12 +0000785
Chris Lattner237f2752009-02-14 07:37:35 +0000786 if (BO->isAssignmentOp())
787 return false;
788 Loc = BO->getOperatorLoc();
789 R1 = BO->getLHS()->getSourceRange();
790 R2 = BO->getRHS()->getSourceRange();
791 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000792 }
Chris Lattner86928112007-08-25 02:00:02 +0000793 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000794 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000795
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000796 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000797 // The condition must be evaluated, but if either the LHS or RHS is a
798 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000799 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000800 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000801 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000802 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000803 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000804 }
805
Chris Lattnera44d1162007-06-27 05:58:59 +0000806 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000807 // If the base pointer or element is to a volatile pointer/field, accessing
808 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000809 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000810 return false;
811 Loc = cast<MemberExpr>(this)->getMemberLoc();
812 R1 = SourceRange(Loc, Loc);
813 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
814 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner1ec5f562007-06-27 05:38:08 +0000816 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000817 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000818 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000819 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000820 return false;
821 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
822 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
823 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
824 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000825
Chris Lattner1ec5f562007-06-27 05:38:08 +0000826 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000827 case CXXOperatorCallExprClass:
828 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000829 // If this is a direct call, get the callee.
830 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner1a6babf2009-10-13 04:53:48 +0000831 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000832 // If the callee has attribute pure, const, or warn_unused_result, warn
833 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000834 //
835 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
836 // updated to match for QoI.
837 if (FD->getAttr<WarnUnusedResultAttr>() ||
838 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
839 Loc = CE->getCallee()->getLocStart();
840 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner1a6babf2009-10-13 04:53:48 +0000842 if (unsigned NumArgs = CE->getNumArgs())
843 R2 = SourceRange(CE->getArg(0)->getLocStart(),
844 CE->getArg(NumArgs-1)->getLocEnd());
845 return true;
846 }
Chris Lattner237f2752009-02-14 07:37:35 +0000847 }
848 return false;
849 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000850
851 case CXXTemporaryObjectExprClass:
852 case CXXConstructExprClass:
853 return false;
854
Chris Lattnere6d9ca52007-09-26 22:06:30 +0000855 case ObjCMessageExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000856 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000857
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000858 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000859#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000860 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000861 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000862 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +0000863 Loc = Ref->getLocation();
864 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
865 if (Ref->getBase())
866 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +0000867#else
868 Loc = getExprLoc();
869 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000870#endif
871 return true;
872 }
Chris Lattner944d3062008-07-26 19:51:01 +0000873 case StmtExprClass: {
874 // Statement exprs don't logically have side effects themselves, but are
875 // sometimes used in macros in ways that give them a type that is unused.
876 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
877 // however, if the result of the stmt expr is dead, we don't want to emit a
878 // warning.
879 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
880 if (!CS->body_empty())
881 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +0000882 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner237f2752009-02-14 07:37:35 +0000884 Loc = cast<StmtExpr>(this)->getLParenLoc();
885 R1 = getSourceRange();
886 return true;
Chris Lattner944d3062008-07-26 19:51:01 +0000887 }
Douglas Gregorf19b2312008-10-28 15:36:24 +0000888 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +0000889 // If this is an explicit cast to void, allow it. People do this when they
890 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +0000891 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +0000892 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000893 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
894 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
895 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000896 case CXXFunctionalCastExprClass: {
897 const CastExpr *CE = cast<CastExpr>(this);
898
899 // If this is a cast to void or a constructor conversion, check the operand.
900 // Otherwise, the result of the cast is unused.
901 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
902 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +0000903 return (cast<CastExpr>(this)->getSubExpr()
904 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +0000905 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
906 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
907 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +0000908 }
Mike Stump11289f42009-09-09 15:08:12 +0000909
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000910 case ImplicitCastExprClass:
911 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +0000912 return (cast<ImplicitCastExpr>(this)
913 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +0000914
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000915 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000916 return (cast<CXXDefaultArgExpr>(this)
917 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000918
919 case CXXNewExprClass:
920 // FIXME: In theory, there might be new expressions that don't have side
921 // effects (e.g. a placement new with an uninitialized POD).
922 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000923 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +0000924 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000925 return (cast<CXXBindTemporaryExpr>(this)
926 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +0000927 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +0000928 return (cast<CXXExprWithTemporaries>(this)
929 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +0000930 }
Chris Lattner1ec5f562007-06-27 05:38:08 +0000931}
932
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000933/// DeclCanBeLvalue - Determine whether the given declaration can be
934/// an lvalue. This is a helper routine for isLvalue.
935static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +0000936 // C++ [temp.param]p6:
937 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +0000938 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +0000939 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
940 return NTTParm->getType()->isReferenceType();
941
Douglas Gregor91f84212008-12-11 16:49:14 +0000942 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000943 // C++ 3.10p2: An lvalue refers to an object or function.
944 (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregor9b146582009-07-08 20:55:45 +0000945 (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) ||
946 isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000947}
948
Steve Naroff475cca02007-05-14 17:19:29 +0000949/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
950/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000951/// - name, where name must be a variable
952/// - e[i]
953/// - (e), where e must be an lvalue
954/// - e.name, where e must be an lvalue
955/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000956/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000957/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +0000958/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000959/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000960///
Chris Lattner67315442008-07-26 21:30:36 +0000961Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000962 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
963
964 isLvalueResult Res = isLvalueInternal(Ctx);
965 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
966 return Res;
967
Douglas Gregor9a657932008-10-21 23:43:52 +0000968 // first, check the type (C99 6.3.2.1). Expressions with function
969 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +0000970 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +0000971 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000972
Steve Naroff1018ea32008-02-10 01:39:04 +0000973 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +0000974 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +0000975 return LV_IncompleteVoidType;
976
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000977 return LV_Valid;
978}
Bill Wendlingdfc81072007-07-17 03:52:31 +0000979
Eli Friedmanb8c4fd82009-05-03 22:36:05 +0000980// Check whether the expression can be sanely treated like an l-value
981Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +0000982 switch (getStmtClass()) {
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000983 case StringLiteralClass: // C99 6.5.1p4
984 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +0000985 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +0000986 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000987 // For vectors, make sure base is an lvalue (i.e. not a function call).
988 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +0000989 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +0000990 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000991 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +0000992 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
993 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +0000994 return LV_Valid;
995 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +0000996 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +0000997 case BlockDeclRefExprClass: {
998 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +0000999 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001000 return LV_Valid;
1001 break;
1002 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001003 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001004 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001005 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1006 NamedDecl *Member = m->getMemberDecl();
1007 // C++ [expr.ref]p4:
1008 // If E2 is declared to have type "reference to T", then E1.E2
1009 // is an lvalue.
1010 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1011 if (Value->getType()->isReferenceType())
1012 return LV_Valid;
1013
1014 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001015 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001016 return LV_Valid;
1017
1018 // -- If E2 is a non-static data member [...]. If E1 is an
1019 // lvalue, then E1.E2 is an lvalue.
1020 if (isa<FieldDecl>(Member))
1021 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
1022
1023 // -- If it refers to a static member function [...], then
1024 // E1.E2 is an lvalue.
1025 // -- Otherwise, if E1.E2 refers to a non-static member
1026 // function [...], then E1.E2 is not an lvalue.
1027 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1028 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1029
1030 // -- If E2 is a member enumerator [...], the expression E1.E2
1031 // is not an lvalue.
1032 if (isa<EnumConstantDecl>(Member))
1033 return LV_InvalidExpression;
1034
1035 // Not an lvalue.
1036 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001037 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001038
1039 // C99 6.5.2.3p4
Chris Lattner67315442008-07-26 21:30:36 +00001040 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001041 }
Chris Lattner595db862007-10-30 22:53:42 +00001042 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001043 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001044 return LV_Valid; // C99 6.5.3p4
1045
1046 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001047 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1048 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001049 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001050
1051 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1052 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1053 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1054 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001055 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001056 case ImplicitCastExprClass:
Mike Stump11289f42009-09-09 15:08:12 +00001057 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregora11693b2008-11-12 17:17:38 +00001058 : LV_InvalidExpression;
Steve Naroff475cca02007-05-14 17:19:29 +00001059 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001060 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001061 case BinaryOperatorClass:
1062 case CompoundAssignOperatorClass: {
1063 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001064
1065 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1066 BinOp->getOpcode() == BinaryOperator::Comma)
1067 return BinOp->getRHS()->isLvalue(Ctx);
1068
Sebastian Redl112a97662009-02-07 00:15:38 +00001069 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001070 // The result of a .* expression is an lvalue only if its first operand is
1071 // an lvalue and its second operand is a pointer to data member.
1072 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001073 !BinOp->getType()->isFunctionType())
1074 return BinOp->getLHS()->isLvalue(Ctx);
1075
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001076 // The result of an ->* expression is an lvalue only if its second operand
1077 // is a pointer to data member.
1078 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1079 !BinOp->getType()->isFunctionType()) {
1080 QualType Ty = BinOp->getRHS()->getType();
1081 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1082 return LV_Valid;
1083 }
1084
Douglas Gregor58e008d2008-11-13 20:12:29 +00001085 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001086 return LV_InvalidExpression;
1087
Douglas Gregor58e008d2008-11-13 20:12:29 +00001088 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001089 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001090 // The result of an assignment operation [...] is an lvalue.
1091 return LV_Valid;
1092
1093
1094 // C99 6.5.16:
1095 // An assignment expression [...] is not an lvalue.
1096 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001097 }
Mike Stump11289f42009-09-09 15:08:12 +00001098 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001099 case CXXOperatorCallExprClass:
1100 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001101 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001102 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001103 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001104 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1105 if (ReturnType->isLValueReferenceType())
1106 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001107
Douglas Gregor6b754842008-10-28 00:22:11 +00001108 break;
1109 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001110 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1111 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001112 case ChooseExprClass:
1113 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001114 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001115 case ExtVectorElementExprClass:
1116 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001117 return LV_DuplicateVectorComponents;
1118 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001119 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1120 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001121 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1122 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001123 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001124 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001125 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001126 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001127 case UnresolvedLookupExprClass:
1128 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001129 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001130 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argyrios Kyrtzidis0fdbd6c2008-09-11 04:22:26 +00001131 case CXXConditionDeclExprClass:
1132 return LV_Valid;
Douglas Gregorf19b2312008-10-28 15:36:24 +00001133 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001134 case CXXFunctionalCastExprClass:
1135 case CXXStaticCastExprClass:
1136 case CXXDynamicCastExprClass:
1137 case CXXReinterpretCastExprClass:
1138 case CXXConstCastExprClass:
1139 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001140 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001141 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1142 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001143 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1144 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001145 return LV_Valid;
1146 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001147 case CXXTypeidExprClass:
1148 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1149 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001150 case CXXBindTemporaryExprClass:
1151 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1152 isLvalueInternal(Ctx);
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001153 case ConditionalOperatorClass: {
1154 // Complicated handling is only for C++.
1155 if (!Ctx.getLangOptions().CPlusPlus)
1156 return LV_InvalidExpression;
1157
1158 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1159 // everywhere there's an object converted to an rvalue. Also, any other
1160 // casts should be wrapped by ImplicitCastExprs. There's just the special
1161 // case involving throws to work out.
1162 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001163 Expr *True = Cond->getTrueExpr();
1164 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001165 // C++0x 5.16p2
1166 // If either the second or the third operand has type (cv) void, [...]
1167 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001168 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001169 return LV_InvalidExpression;
1170
1171 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001172 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001173 return LV_InvalidExpression;
1174
1175 // That's it.
1176 return LV_Valid;
1177 }
1178
Douglas Gregord3319842009-10-24 04:59:53 +00001179 case TemplateIdRefExprClass: {
1180 const TemplateIdRefExpr *TID = cast<TemplateIdRefExpr>(this);
1181 TemplateName Template = TID->getTemplateName();
1182 NamedDecl *ND = Template.getAsTemplateDecl();
1183 if (!ND)
1184 ND = Template.getAsOverloadedFunctionDecl();
1185 if (ND && DeclCanBeLvalue(ND, Ctx))
1186 return LV_Valid;
1187
1188 break;
1189 }
1190
Steve Naroff9358c712007-05-27 23:58:33 +00001191 default:
1192 break;
Steve Naroff47500512007-04-19 23:00:49 +00001193 }
Steve Naroff9358c712007-05-27 23:58:33 +00001194 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001195}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001196
Steve Naroff475cca02007-05-14 17:19:29 +00001197/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1198/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001199/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001200/// recursively, any member or element of all contained aggregates or unions)
1201/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001202Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001203Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001204 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001205
Steve Naroff9358c712007-05-27 23:58:33 +00001206 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001207 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001208 // C++ 3.10p11: Functions cannot be modified, but pointers to
1209 // functions can be modifiable.
1210 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1211 return MLV_NotObjectType;
1212 break;
1213
Chris Lattner1ec5f562007-06-27 05:38:08 +00001214 case LV_NotObjectType: return MLV_NotObjectType;
1215 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001216 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001217 case LV_InvalidExpression:
1218 // If the top level is a C-style cast, and the subexpression is a valid
1219 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1220 // GCC extension. We don't support it, but we want to produce good
1221 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001222 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1223 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1224 if (Loc)
1225 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001226 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001227 }
1228 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001229 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001230 case LV_MemberFunction: return MLV_MemberFunction;
Steve Naroff9358c712007-05-27 23:58:33 +00001231 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001232
1233 // The following is illegal:
1234 // void takeclosure(void (^C)(void));
1235 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1236 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001237 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001238 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1239 return MLV_NotBlockQualified;
1240 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001241
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001242 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001243 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001244 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1245 if (Expr->getSetterMethod() == 0)
1246 return MLV_NoSetterProperty;
1247 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001248
Chris Lattner7adf0762008-08-04 07:31:14 +00001249 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001250
Chris Lattner7adf0762008-08-04 07:31:14 +00001251 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001252 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001253 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001254 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001255 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001256 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001258 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001259 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001260 return MLV_ConstQualified;
1261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Mike Stump11289f42009-09-09 15:08:12 +00001263 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001264}
1265
Fariborz Jahanian07735332009-02-22 18:40:18 +00001266/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001267/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001268bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001269 switch (getStmtClass()) {
1270 default:
1271 return false;
1272 case ObjCIvarRefExprClass:
1273 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001274 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001275 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001276 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001277 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001278 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001279 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001280 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001281 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001282 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001283 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001284 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1285 if (VD->hasGlobalStorage())
1286 return true;
1287 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001288 // dereferencing to a pointer is always a gc'able candidate,
1289 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001290 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001291 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001292 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001293 return false;
1294 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001295 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001296 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001297 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001298 }
1299 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001300 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001301 }
1302}
Ted Kremenekfff70962008-01-17 16:57:34 +00001303Expr* Expr::IgnoreParens() {
1304 Expr* E = this;
1305 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1306 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001307
Ted Kremenekfff70962008-01-17 16:57:34 +00001308 return E;
1309}
1310
Chris Lattnerf2660962008-02-13 01:02:39 +00001311/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1312/// or CastExprs or ImplicitCastExprs, returning their operand.
1313Expr *Expr::IgnoreParenCasts() {
1314 Expr *E = this;
1315 while (true) {
1316 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1317 E = P->getSubExpr();
1318 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1319 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001320 else
1321 return E;
1322 }
1323}
1324
Chris Lattneref26c772009-03-13 17:28:01 +00001325/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1326/// value (including ptr->int casts of the same size). Strip off any
1327/// ParenExpr or CastExprs, returning their operand.
1328Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1329 Expr *E = this;
1330 while (true) {
1331 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1332 E = P->getSubExpr();
1333 continue;
1334 }
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattneref26c772009-03-13 17:28:01 +00001336 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1337 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1338 // ptr<->int casts of the same width. We also ignore all identify casts.
1339 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattneref26c772009-03-13 17:28:01 +00001341 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1342 E = SE;
1343 continue;
1344 }
Mike Stump11289f42009-09-09 15:08:12 +00001345
Chris Lattneref26c772009-03-13 17:28:01 +00001346 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1347 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1348 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1349 E = SE;
1350 continue;
1351 }
1352 }
Mike Stump11289f42009-09-09 15:08:12 +00001353
Chris Lattneref26c772009-03-13 17:28:01 +00001354 return E;
1355 }
1356}
1357
1358
Douglas Gregor4619e432008-12-05 23:32:09 +00001359/// hasAnyTypeDependentArguments - Determines if any of the expressions
1360/// in Exprs is type-dependent.
1361bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1362 for (unsigned I = 0; I < NumExprs; ++I)
1363 if (Exprs[I]->isTypeDependent())
1364 return true;
1365
1366 return false;
1367}
1368
1369/// hasAnyValueDependentArguments - Determines if any of the expressions
1370/// in Exprs is value-dependent.
1371bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1372 for (unsigned I = 0; I < NumExprs; ++I)
1373 if (Exprs[I]->isValueDependent())
1374 return true;
1375
1376 return false;
1377}
1378
Eli Friedman7139af42009-01-25 02:32:41 +00001379bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001380 // This function is attempting whether an expression is an initializer
1381 // which can be evaluated at compile-time. isEvaluatable handles most
1382 // of the cases, but it can't deal with some initializer-specific
1383 // expressions, and it can't deal with aggregates; we deal with those here,
1384 // and fall back to isEvaluatable for the other cases.
1385
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001386 // FIXME: This function assumes the variable being assigned to
1387 // isn't a reference type!
1388
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001389 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001390 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001391 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001392 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001393 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001394 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001395 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001396 // This handles gcc's extension that allows global initializers like
1397 // "struct x {int x;} x = (struct x) {};".
1398 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001399 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001400 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001401 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001402 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001403 // FIXME: This doesn't deal with fields with reference types correctly.
1404 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1405 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001406 const InitListExpr *Exp = cast<InitListExpr>(this);
1407 unsigned numInits = Exp->getNumInits();
1408 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001409 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001410 return false;
1411 }
Eli Friedman384da272009-01-25 03:12:18 +00001412 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001413 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001414 case ImplicitValueInitExprClass:
1415 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001416 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001417 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001418 case UnaryOperatorClass: {
1419 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1420 if (Exp->getOpcode() == UnaryOperator::Extension)
1421 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1422 break;
1423 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001424 case BinaryOperatorClass: {
1425 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1426 // but this handles the common case.
1427 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1428 if (Exp->getOpcode() == BinaryOperator::Sub &&
1429 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1430 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1431 return true;
1432 break;
1433 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001434 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001435 case CStyleCastExprClass:
1436 // Handle casts with a destination that's a struct or union; this
1437 // deals with both the gcc no-op struct cast extension and the
1438 // cast-to-union extension.
1439 if (getType()->isRecordType())
1440 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001441
1442 // Integer->integer casts can be handled here, which is important for
1443 // things like (int)(&&x-&&y). Scary but true.
1444 if (getType()->isIntegerType() &&
1445 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1446 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1447
Eli Friedman384da272009-01-25 03:12:18 +00001448 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001449 }
Eli Friedman384da272009-01-25 03:12:18 +00001450 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001451}
1452
Chris Lattner1f4479e2007-06-05 04:15:44 +00001453/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001454/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001455
1456/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1457/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001458///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001459/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1460/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1461/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001462
Eli Friedman98c56a42009-02-26 09:29:13 +00001463// CheckICE - This function does the fundamental ICE checking: the returned
1464// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1465// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001466// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001467// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001468// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001469//
1470// Meanings of Val:
1471// 0: This expression is an ICE if it can be evaluated by Evaluate.
1472// 1: This expression is not an ICE, but if it isn't evaluated, it's
1473// a legal subexpression for an ICE. This return value is used to handle
1474// the comma operator in C99 mode.
1475// 2: This expression is not an ICE, and is not a legal subexpression for one.
1476
1477struct ICEDiag {
1478 unsigned Val;
1479 SourceLocation Loc;
1480
1481 public:
1482 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1483 ICEDiag() : Val(0) {}
1484};
1485
1486ICEDiag NoDiag() { return ICEDiag(); }
1487
Eli Friedman90afd3d2009-02-27 04:07:58 +00001488static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1489 Expr::EvalResult EVResult;
1490 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1491 !EVResult.Val.isInt()) {
1492 return ICEDiag(2, E->getLocStart());
1493 }
1494 return NoDiag();
1495}
1496
Eli Friedman98c56a42009-02-26 09:29:13 +00001497static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001498 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001499 if (!E->getType()->isIntegralType()) {
1500 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001501 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001502
1503 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001504#define STMT(Node, Base) case Expr::Node##Class:
1505#define EXPR(Node, Base)
1506#include "clang/AST/StmtNodes.def"
1507 case Expr::PredefinedExprClass:
1508 case Expr::FloatingLiteralClass:
1509 case Expr::ImaginaryLiteralClass:
1510 case Expr::StringLiteralClass:
1511 case Expr::ArraySubscriptExprClass:
1512 case Expr::MemberExprClass:
1513 case Expr::CompoundAssignOperatorClass:
1514 case Expr::CompoundLiteralExprClass:
1515 case Expr::ExtVectorElementExprClass:
1516 case Expr::InitListExprClass:
1517 case Expr::DesignatedInitExprClass:
1518 case Expr::ImplicitValueInitExprClass:
1519 case Expr::ParenListExprClass:
1520 case Expr::VAArgExprClass:
1521 case Expr::AddrLabelExprClass:
1522 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001523 case Expr::CXXMemberCallExprClass:
1524 case Expr::CXXDynamicCastExprClass:
1525 case Expr::CXXTypeidExprClass:
1526 case Expr::CXXNullPtrLiteralExprClass:
1527 case Expr::CXXThisExprClass:
1528 case Expr::CXXThrowExprClass:
1529 case Expr::CXXConditionDeclExprClass: // FIXME: is this correct?
1530 case Expr::CXXNewExprClass:
1531 case Expr::CXXDeleteExprClass:
1532 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001533 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001534 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001535 case Expr::TemplateIdRefExprClass:
1536 case Expr::CXXConstructExprClass:
1537 case Expr::CXXBindTemporaryExprClass:
1538 case Expr::CXXExprWithTemporariesClass:
1539 case Expr::CXXTemporaryObjectExprClass:
1540 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001541 case Expr::CXXDependentScopeMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001542 case Expr::ObjCStringLiteralClass:
1543 case Expr::ObjCEncodeExprClass:
1544 case Expr::ObjCMessageExprClass:
1545 case Expr::ObjCSelectorExprClass:
1546 case Expr::ObjCProtocolExprClass:
1547 case Expr::ObjCIvarRefExprClass:
1548 case Expr::ObjCPropertyRefExprClass:
1549 case Expr::ObjCImplicitSetterGetterRefExprClass:
1550 case Expr::ObjCSuperExprClass:
1551 case Expr::ObjCIsaExprClass:
1552 case Expr::ShuffleVectorExprClass:
1553 case Expr::BlockExprClass:
1554 case Expr::BlockDeclRefExprClass:
1555 case Expr::NoStmtClass:
1556 case Expr::ExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001557 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001558
Douglas Gregor73341c42009-09-11 00:18:58 +00001559 case Expr::GNUNullExprClass:
1560 // GCC considers the GNU __null value to be an integral constant expression.
1561 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001562
Eli Friedman98c56a42009-02-26 09:29:13 +00001563 case Expr::ParenExprClass:
1564 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1565 case Expr::IntegerLiteralClass:
1566 case Expr::CharacterLiteralClass:
1567 case Expr::CXXBoolLiteralExprClass:
1568 case Expr::CXXZeroInitValueExprClass:
1569 case Expr::TypesCompatibleExprClass:
1570 case Expr::UnaryTypeTraitExprClass:
1571 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001572 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001573 case Expr::CXXOperatorCallExprClass: {
1574 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001575 if (CE->isBuiltinCall(Ctx))
1576 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001577 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001578 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001579 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001580 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1581 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001582 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001583 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001584 // C++ 7.1.5.1p2
1585 // A variable of non-volatile const-qualified integral or enumeration
1586 // type initialized by an ICE can be used in ICEs.
1587 if (const VarDecl *Dcl =
Eli Friedman98c56a42009-02-26 09:29:13 +00001588 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001589 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1590 if (Quals.hasVolatile() || !Quals.hasConst())
1591 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1592
1593 // Look for the definition of this variable, which will actually have
1594 // an initializer.
1595 const VarDecl *Def = 0;
1596 const Expr *Init = Dcl->getDefinition(Def);
1597 if (Init) {
1598 if (Def->isInitKnownICE()) {
1599 // We have already checked whether this subexpression is an
1600 // integral constant expression.
1601 if (Def->isInitICE())
1602 return NoDiag();
1603 else
1604 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1605 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001606
Douglas Gregor0840cc02009-11-01 20:32:48 +00001607 // C++ [class.static.data]p4:
1608 // If a static data member is of const integral or const
1609 // enumeration type, its declaration in the class definition can
1610 // specify a constant-initializer which shall be an integral
1611 // constant expression (5.19). In that case, the member can appear
1612 // in integral constant expressions.
1613 if (Def->isOutOfLine()) {
1614 Dcl->setInitKnownICE(Ctx, false);
1615 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1616 }
1617
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001618 ICEDiag Result = CheckICE(Init, Ctx);
1619 // Cache the result of the ICE test.
1620 Dcl->setInitKnownICE(Ctx, Result.Val == 0);
1621 return Result;
1622 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001623 }
1624 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001625 return ICEDiag(2, E->getLocStart());
1626 case Expr::UnaryOperatorClass: {
1627 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001628 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001629 case UnaryOperator::PostInc:
1630 case UnaryOperator::PostDec:
1631 case UnaryOperator::PreInc:
1632 case UnaryOperator::PreDec:
1633 case UnaryOperator::AddrOf:
1634 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001635 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001636
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001637 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001638 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001639 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001640 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001641 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001642 case UnaryOperator::Real:
1643 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001644 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001645 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001646 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1647 // Evaluate matches the proposed gcc behavior for cases like
1648 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1649 // compliance: we should warn earlier for offsetof expressions with
1650 // array subscripts that aren't ICEs, and if the array subscripts
1651 // are ICEs, the value of the offsetof must be an integer constant.
1652 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001653 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001654 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001655 case Expr::SizeOfAlignOfExprClass: {
1656 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1657 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1658 return ICEDiag(2, E->getLocStart());
1659 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001660 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001661 case Expr::BinaryOperatorClass: {
1662 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001663 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001664 case BinaryOperator::PtrMemD:
1665 case BinaryOperator::PtrMemI:
1666 case BinaryOperator::Assign:
1667 case BinaryOperator::MulAssign:
1668 case BinaryOperator::DivAssign:
1669 case BinaryOperator::RemAssign:
1670 case BinaryOperator::AddAssign:
1671 case BinaryOperator::SubAssign:
1672 case BinaryOperator::ShlAssign:
1673 case BinaryOperator::ShrAssign:
1674 case BinaryOperator::AndAssign:
1675 case BinaryOperator::XorAssign:
1676 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001677 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001678
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001679 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001680 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001681 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001682 case BinaryOperator::Add:
1683 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001684 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001685 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001686 case BinaryOperator::LT:
1687 case BinaryOperator::GT:
1688 case BinaryOperator::LE:
1689 case BinaryOperator::GE:
1690 case BinaryOperator::EQ:
1691 case BinaryOperator::NE:
1692 case BinaryOperator::And:
1693 case BinaryOperator::Xor:
1694 case BinaryOperator::Or:
1695 case BinaryOperator::Comma: {
1696 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1697 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001698 if (Exp->getOpcode() == BinaryOperator::Div ||
1699 Exp->getOpcode() == BinaryOperator::Rem) {
1700 // Evaluate gives an error for undefined Div/Rem, so make sure
1701 // we don't evaluate one.
1702 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1703 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1704 if (REval == 0)
1705 return ICEDiag(1, E->getLocStart());
1706 if (REval.isSigned() && REval.isAllOnesValue()) {
1707 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1708 if (LEval.isMinSignedValue())
1709 return ICEDiag(1, E->getLocStart());
1710 }
1711 }
1712 }
1713 if (Exp->getOpcode() == BinaryOperator::Comma) {
1714 if (Ctx.getLangOptions().C99) {
1715 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1716 // if it isn't evaluated.
1717 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1718 return ICEDiag(1, E->getLocStart());
1719 } else {
1720 // In both C89 and C++, commas in ICEs are illegal.
1721 return ICEDiag(2, E->getLocStart());
1722 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001723 }
1724 if (LHSResult.Val >= RHSResult.Val)
1725 return LHSResult;
1726 return RHSResult;
1727 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001728 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001729 case BinaryOperator::LOr: {
1730 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1731 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1732 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1733 // Rare case where the RHS has a comma "side-effect"; we need
1734 // to actually check the condition to see whether the side
1735 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001736 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001737 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001738 return RHSResult;
1739 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001740 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001741
Eli Friedman98c56a42009-02-26 09:29:13 +00001742 if (LHSResult.Val >= RHSResult.Val)
1743 return LHSResult;
1744 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001745 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001746 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001747 }
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001748 case Expr::CastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001749 case Expr::ImplicitCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001750 case Expr::ExplicitCastExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001751 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001752 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001753 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00001754 case Expr::CXXStaticCastExprClass:
1755 case Expr::CXXReinterpretCastExprClass:
1756 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00001757 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1758 if (SubExpr->getType()->isIntegralType())
1759 return CheckICE(SubExpr, Ctx);
1760 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1761 return NoDiag();
1762 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001763 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001764 case Expr::ConditionalOperatorClass: {
1765 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00001766 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00001767 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00001768 // expression, and it is fully evaluated. This is an important GNU
1769 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00001770 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00001771 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001772 Expr::EvalResult EVResult;
1773 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1774 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00001775 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001776 }
1777 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00001778 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001779 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1780 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1781 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1782 if (CondResult.Val == 2)
1783 return CondResult;
1784 if (TrueResult.Val == 2)
1785 return TrueResult;
1786 if (FalseResult.Val == 2)
1787 return FalseResult;
1788 if (CondResult.Val == 1)
1789 return CondResult;
1790 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1791 return NoDiag();
1792 // Rare case where the diagnostics depend on which side is evaluated
1793 // Note that if we get here, CondResult is 0, and at least one of
1794 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00001795 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00001796 return FalseResult;
1797 }
1798 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001799 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001800 case Expr::CXXDefaultArgExprClass:
1801 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001802 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001803 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001804 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001805 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001806
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001807 // Silence a GCC warning
1808 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00001809}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001810
Eli Friedman98c56a42009-02-26 09:29:13 +00001811bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1812 SourceLocation *Loc, bool isEvaluated) const {
1813 ICEDiag d = CheckICE(this, Ctx);
1814 if (d.Val != 0) {
1815 if (Loc) *Loc = d.Loc;
1816 return false;
1817 }
1818 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00001819 if (!Evaluate(EvalResult, Ctx))
Douglas Gregor0840cc02009-11-01 20:32:48 +00001820 llvm::llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00001821 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1822 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001823 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001824 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00001825}
1826
Chris Lattner7eef9192007-05-24 01:23:49 +00001827/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1828/// integer constant expression with the value zero, or if this is one that is
1829/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001830bool Expr::isNullPointerConstant(ASTContext &Ctx,
1831 NullPointerConstantValueDependence NPC) const {
1832 if (isValueDependent()) {
1833 switch (NPC) {
1834 case NPC_NeverValueDependent:
1835 assert(false && "Unexpected value dependent expression!");
1836 // If the unthinkable happens, fall through to the safest alternative.
1837
1838 case NPC_ValueDependentIsNull:
1839 return isTypeDependent() || getType()->isIntegralType();
1840
1841 case NPC_ValueDependentIsNotNull:
1842 return false;
1843 }
1844 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001845
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001846 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001847 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001848 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001849 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001850 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001851 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001852 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001853 Pointee->isVoidType() && // to void*
1854 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001855 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001856 }
Steve Naroffada7d422007-05-20 17:54:12 +00001857 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001858 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1859 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001860 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001861 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1862 // Accept ((void*)0) as a null pointer constant, as many other
1863 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001864 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001865 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001866 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001867 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001868 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001869 } else if (isa<GNUNullExpr>(this)) {
1870 // The GNU __null extension is always a null pointer constant.
1871 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001872 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001873
Sebastian Redl576fd422009-05-10 18:38:11 +00001874 // C++0x nullptr_t is always a null pointer constant.
1875 if (getType()->isNullPtrType())
1876 return true;
1877
Steve Naroff4871fe02008-01-14 16:10:57 +00001878 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001879 if (!getType()->isIntegerType() ||
1880 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001881 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001882
Chris Lattner1abbd412007-06-08 17:58:43 +00001883 // If we have an integer constant expression, we need to *evaluate* it and
1884 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001885 llvm::APSInt Result;
1886 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001887}
Steve Narofff7a5da12007-07-28 23:10:27 +00001888
Douglas Gregor71235ec2009-05-02 02:18:30 +00001889FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001890 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001891
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001892 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001893 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001894 if (Field->isBitField())
1895 return Field;
1896
1897 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1898 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1899 return BinOp->getLHS()->getBitField();
1900
1901 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001902}
1903
Chris Lattnerb8211f62009-02-16 22:14:05 +00001904/// isArrow - Return true if the base expression is a pointer to vector,
1905/// return false if the base expression is a vector.
1906bool ExtVectorElementExpr::isArrow() const {
1907 return getBase()->getType()->isPointerType();
1908}
1909
Nate Begemance4d7fc2008-04-18 23:10:10 +00001910unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001911 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001912 return VT->getNumElements();
1913 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001914}
1915
Nate Begemanf322eab2008-05-09 06:41:27 +00001916/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001917bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001918 // FIXME: Refactor this code to an accessor on the AST node which returns the
1919 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001920 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001921
1922 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001923 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001924 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001925
Nate Begeman7e5185b2009-01-18 02:01:21 +00001926 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001927 if (Comp[0] == 's' || Comp[0] == 'S')
1928 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001929
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001930 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1931 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001932 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001933
Steve Naroff0d595ca2007-07-30 03:29:09 +00001934 return false;
1935}
Chris Lattner885b4952007-08-02 23:36:59 +00001936
Nate Begemanf322eab2008-05-09 06:41:27 +00001937/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001938void ExtVectorElementExpr::getEncodedElementAccess(
1939 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001940 llvm::StringRef Comp = Accessor->getName();
1941 if (Comp[0] == 's' || Comp[0] == 'S')
1942 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001943
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001944 bool isHi = Comp == "hi";
1945 bool isLo = Comp == "lo";
1946 bool isEven = Comp == "even";
1947 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001948
Nate Begemanf322eab2008-05-09 06:41:27 +00001949 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1950 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Nate Begemanf322eab2008-05-09 06:41:27 +00001952 if (isHi)
1953 Index = e + i;
1954 else if (isLo)
1955 Index = i;
1956 else if (isEven)
1957 Index = 2 * i;
1958 else if (isOdd)
1959 Index = 2 * i + 1;
1960 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001961 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001962
Nate Begemand3862152008-05-13 21:03:02 +00001963 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001964 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001965}
1966
Steve Narofff73590d2007-09-27 14:38:14 +00001967// constructor for instance messages.
Steve Naroff80175062007-09-28 22:22:11 +00001968ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001969 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001970 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001971 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001972 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001973 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001974 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001975 SubExprs = new Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00001976 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001977 if (NumArgs) {
1978 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001979 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1980 }
Steve Naroffd54978b2007-09-18 23:55:05 +00001981 LBracloc = LBrac;
1982 RBracloc = RBrac;
1983}
1984
Mike Stump11289f42009-09-09 15:08:12 +00001985// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00001986// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff80175062007-09-28 22:22:11 +00001987ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001988 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff66697aa2007-11-03 16:37:59 +00001989 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001990 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00001991 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00001992 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001993 NumArgs = nargs;
Ted Kremenek08e17112008-06-17 02:43:46 +00001994 SubExprs = new Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001995 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001996 if (NumArgs) {
1997 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00001998 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1999 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002000 LBracloc = LBrac;
2001 RBracloc = RBrac;
2002}
2003
Mike Stump11289f42009-09-09 15:08:12 +00002004// constructor for class messages.
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002005ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2006 QualType retType, ObjCMethodDecl *mproto,
2007 SourceLocation LBrac, SourceLocation RBrac,
2008 Expr **ArgExprs, unsigned nargs)
Mike Stump11289f42009-09-09 15:08:12 +00002009: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002010MethodProto(mproto) {
2011 NumArgs = nargs;
2012 SubExprs = new Stmt*[NumArgs+1];
2013 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2014 if (NumArgs) {
2015 for (unsigned i = 0; i != NumArgs; ++i)
2016 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2017 }
2018 LBracloc = LBrac;
2019 RBracloc = RBrac;
2020}
2021
2022ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2023 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2024 switch (x & Flags) {
2025 default:
2026 assert(false && "Invalid ObjCMessageExpr.");
2027 case IsInstMeth:
2028 return ClassInfo(0, 0);
2029 case IsClsMethDeclUnknown:
2030 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2031 case IsClsMethDeclKnown: {
2032 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2033 return ClassInfo(D, D->getIdentifier());
2034 }
2035 }
2036}
2037
Chris Lattner7ec71da2009-04-26 00:44:05 +00002038void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2039 if (CI.first == 0 && CI.second == 0)
2040 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2041 else if (CI.first == 0)
2042 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2043 else
2044 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2045}
2046
2047
Chris Lattner35e564e2007-10-25 00:29:32 +00002048bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002049 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002050}
2051
Nate Begeman48745922009-08-12 02:28:50 +00002052void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2053 unsigned NumExprs) {
2054 if (SubExprs) C.Deallocate(SubExprs);
2055
2056 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002057 this->NumExprs = NumExprs;
2058 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002059}
Nate Begeman48745922009-08-12 02:28:50 +00002060
2061void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2062 DestroyChildren(C);
2063 if (SubExprs) C.Deallocate(SubExprs);
2064 this->~ShuffleVectorExpr();
2065 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002066}
2067
Douglas Gregore26a2852009-08-07 06:08:38 +00002068void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002069 // Override default behavior of traversing children. If this has a type
2070 // operand and the type is a variable-length array, the child iteration
2071 // will iterate over the size expression. However, this expression belongs
2072 // to the type, not to this, so we don't want to delete it.
2073 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002074 if (isArgumentType()) {
2075 this->~SizeOfAlignOfExpr();
2076 C.Deallocate(this);
2077 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002078 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002079 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002080}
2081
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002082//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002083// DesignatedInitExpr
2084//===----------------------------------------------------------------------===//
2085
2086IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2087 assert(Kind == FieldDesignator && "Only valid on a field designator");
2088 if (Field.NameOrField & 0x01)
2089 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2090 else
2091 return getField()->getIdentifier();
2092}
2093
Mike Stump11289f42009-09-09 15:08:12 +00002094DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002095 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002096 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002097 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002098 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002099 unsigned NumIndexExprs,
2100 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002101 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002102 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002103 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2104 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002105 this->Designators = new Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002106
2107 // Record the initializer itself.
2108 child_iterator Child = child_begin();
2109 *Child++ = Init;
2110
2111 // Copy the designators and their subexpressions, computing
2112 // value-dependence along the way.
2113 unsigned IndexIdx = 0;
2114 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002115 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002116
2117 if (this->Designators[I].isArrayDesignator()) {
2118 // Compute type- and value-dependence.
2119 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002120 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002121 Index->isTypeDependent() || Index->isValueDependent();
2122
2123 // Copy the index expressions into permanent storage.
2124 *Child++ = IndexExprs[IndexIdx++];
2125 } else if (this->Designators[I].isArrayRangeDesignator()) {
2126 // Compute type- and value-dependence.
2127 Expr *Start = IndexExprs[IndexIdx];
2128 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002129 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002130 Start->isTypeDependent() || Start->isValueDependent() ||
2131 End->isTypeDependent() || End->isValueDependent();
2132
2133 // Copy the start/end expressions into permanent storage.
2134 *Child++ = IndexExprs[IndexIdx++];
2135 *Child++ = IndexExprs[IndexIdx++];
2136 }
2137 }
2138
2139 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002140}
2141
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002142DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002143DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002144 unsigned NumDesignators,
2145 Expr **IndexExprs, unsigned NumIndexExprs,
2146 SourceLocation ColonOrEqualLoc,
2147 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002148 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002149 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002150 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2151 ColonOrEqualLoc, UsesColonSyntax,
2152 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002153}
2154
Mike Stump11289f42009-09-09 15:08:12 +00002155DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002156 unsigned NumIndexExprs) {
2157 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2158 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2159 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2160}
2161
Mike Stump11289f42009-09-09 15:08:12 +00002162void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002163 unsigned NumDesigs) {
2164 if (Designators)
2165 delete [] Designators;
2166
2167 Designators = new Designator[NumDesigs];
2168 NumDesignators = NumDesigs;
2169 for (unsigned I = 0; I != NumDesigs; ++I)
2170 Designators[I] = Desigs[I];
2171}
2172
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002173SourceRange DesignatedInitExpr::getSourceRange() const {
2174 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002175 Designator &First =
2176 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002177 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002178 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002179 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2180 else
2181 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2182 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002183 StartLoc =
2184 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002185 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2186}
2187
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002188Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2189 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2190 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2191 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002192 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2193 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2194}
2195
2196Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002197 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002198 "Requires array range designator");
2199 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2200 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002201 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2202 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2203}
2204
2205Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002206 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002207 "Requires array range designator");
2208 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2209 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002210 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2211 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2212}
2213
Douglas Gregord5846a12009-04-15 06:41:24 +00002214/// \brief Replaces the designator at index @p Idx with the series
2215/// of designators in [First, Last).
Mike Stump11289f42009-09-09 15:08:12 +00002216void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2217 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002218 const Designator *Last) {
2219 unsigned NumNewDesignators = Last - First;
2220 if (NumNewDesignators == 0) {
2221 std::copy_backward(Designators + Idx + 1,
2222 Designators + NumDesignators,
2223 Designators + Idx);
2224 --NumNewDesignators;
2225 return;
2226 } else if (NumNewDesignators == 1) {
2227 Designators[Idx] = *First;
2228 return;
2229 }
2230
Mike Stump11289f42009-09-09 15:08:12 +00002231 Designator *NewDesignators
Douglas Gregord5846a12009-04-15 06:41:24 +00002232 = new Designator[NumDesignators - 1 + NumNewDesignators];
2233 std::copy(Designators, Designators + Idx, NewDesignators);
2234 std::copy(First, Last, NewDesignators + Idx);
2235 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2236 NewDesignators + Idx + NumNewDesignators);
2237 delete [] Designators;
2238 Designators = NewDesignators;
2239 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2240}
2241
Douglas Gregore26a2852009-08-07 06:08:38 +00002242void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002243 delete [] Designators;
Douglas Gregore26a2852009-08-07 06:08:38 +00002244 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002245}
2246
Mike Stump11289f42009-09-09 15:08:12 +00002247ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002248 Expr **exprs, unsigned nexprs,
2249 SourceLocation rparenloc)
2250: Expr(ParenListExprClass, QualType(),
2251 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002252 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002253 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002254
Nate Begeman5ec4b312009-08-10 23:49:36 +00002255 Exprs = new (C) Stmt*[nexprs];
2256 for (unsigned i = 0; i != nexprs; ++i)
2257 Exprs[i] = exprs[i];
2258}
2259
2260void ParenListExpr::DoDestroy(ASTContext& C) {
2261 DestroyChildren(C);
2262 if (Exprs) C.Deallocate(Exprs);
2263 this->~ParenListExpr();
2264 C.Deallocate(this);
2265}
2266
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002267//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002268// ExprIterator.
2269//===----------------------------------------------------------------------===//
2270
2271Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2272Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2273Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2274const Expr* ConstExprIterator::operator[](size_t idx) const {
2275 return cast<Expr>(I[idx]);
2276}
2277const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2278const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2279
2280//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002281// Child Iterators for iterating over subexpressions/substatements
2282//===----------------------------------------------------------------------===//
2283
2284// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002285Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2286Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002287
Steve Naroffe46504b2007-11-12 14:29:37 +00002288// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002289Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2290Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002291
Steve Naroffebf4cb42008-06-02 23:03:37 +00002292// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002293Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2294Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002295
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002296// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002297Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2298 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002299}
Mike Stump11289f42009-09-09 15:08:12 +00002300Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2301 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002302}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002303
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002304// ObjCSuperExpr
2305Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2306Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2307
Steve Naroffe87026a2009-07-24 17:54:45 +00002308// ObjCIsaExpr
2309Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2310Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2311
Chris Lattner6307f192008-08-10 01:53:14 +00002312// PredefinedExpr
2313Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2314Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002315
2316// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002317Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2318Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002319
2320// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002321Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002322Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002323
2324// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002325Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2326Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002327
Chris Lattner1c20a172007-08-26 03:42:43 +00002328// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002329Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2330Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002331
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002332// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002333Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2334Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002335
2336// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002337Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2338Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002339
2340// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002341Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2342Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002343
Sebastian Redl6f282892008-11-11 17:56:53 +00002344// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002345Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002346 // If this is of a type and the type is a VLA type (and not a typedef), the
2347 // size expression of the VLA needs to be treated as an executable expression.
2348 // Why isn't this weirdness documented better in StmtIterator?
2349 if (isArgumentType()) {
2350 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2351 getArgumentType().getTypePtr()))
2352 return child_iterator(T);
2353 return child_iterator();
2354 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002355 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002356}
Sebastian Redl6f282892008-11-11 17:56:53 +00002357Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2358 if (isArgumentType())
2359 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002360 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002361}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002362
2363// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002364Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002365 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002366}
Ted Kremenek23702b62007-08-24 20:06:47 +00002367Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002368 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002369}
2370
2371// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002372Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002373 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002374}
Ted Kremenek23702b62007-08-24 20:06:47 +00002375Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002376 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002377}
Ted Kremenek23702b62007-08-24 20:06:47 +00002378
2379// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002380Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2381Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002382
Nate Begemance4d7fc2008-04-18 23:10:10 +00002383// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002384Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2385Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002386
2387// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002388Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2389Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002390
Ted Kremenek23702b62007-08-24 20:06:47 +00002391// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002392Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2393Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002394
2395// BinaryOperator
2396Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002397 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002398}
Ted Kremenek23702b62007-08-24 20:06:47 +00002399Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002400 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002401}
2402
2403// ConditionalOperator
2404Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002405 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002406}
Ted Kremenek23702b62007-08-24 20:06:47 +00002407Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002408 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002409}
2410
2411// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002412Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2413Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002414
Ted Kremenek23702b62007-08-24 20:06:47 +00002415// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002416Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2417Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002418
2419// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002420Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2421 return child_iterator();
2422}
2423
2424Stmt::child_iterator TypesCompatibleExpr::child_end() {
2425 return child_iterator();
2426}
Ted Kremenek23702b62007-08-24 20:06:47 +00002427
2428// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002429Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2430Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002431
Douglas Gregor3be4b122008-11-29 04:51:27 +00002432// GNUNullExpr
2433Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2434Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2435
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002436// ShuffleVectorExpr
2437Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002438 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002439}
2440Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002441 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002442}
2443
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002444// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002445Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2446Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002447
Anders Carlsson4692db02007-08-31 04:56:16 +00002448// InitListExpr
2449Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002450 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002451}
2452Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002453 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson4692db02007-08-31 04:56:16 +00002454}
2455
Douglas Gregor0202cb42009-01-29 17:44:32 +00002456// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002457Stmt::child_iterator DesignatedInitExpr::child_begin() {
2458 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2459 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002460 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2461}
2462Stmt::child_iterator DesignatedInitExpr::child_end() {
2463 return child_iterator(&*child_begin() + NumSubExprs);
2464}
2465
Douglas Gregor0202cb42009-01-29 17:44:32 +00002466// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002467Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2468 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002469}
2470
Mike Stump11289f42009-09-09 15:08:12 +00002471Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2472 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002473}
2474
Nate Begeman5ec4b312009-08-10 23:49:36 +00002475// ParenListExpr
2476Stmt::child_iterator ParenListExpr::child_begin() {
2477 return &Exprs[0];
2478}
2479Stmt::child_iterator ParenListExpr::child_end() {
2480 return &Exprs[0]+NumExprs;
2481}
2482
Ted Kremenek23702b62007-08-24 20:06:47 +00002483// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002484Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002485 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002486}
2487Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002488 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002489}
Ted Kremenek23702b62007-08-24 20:06:47 +00002490
2491// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002492Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2493Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002494
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002495// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002496Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002497 return child_iterator();
2498}
2499Stmt::child_iterator ObjCSelectorExpr::child_end() {
2500 return child_iterator();
2501}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002502
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002503// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002504Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2505 return child_iterator();
2506}
2507Stmt::child_iterator ObjCProtocolExpr::child_end() {
2508 return child_iterator();
2509}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002510
Steve Naroffd54978b2007-09-18 23:55:05 +00002511// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002512Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002513 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002514}
2515Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002516 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002517}
2518
Steve Naroffc540d662008-09-03 18:15:37 +00002519// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002520Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2521Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002522
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002523Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2524Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }