blob: e8758d814deceb719394004b382cbf4e835e0ea9 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Primary Expressions.
32//===----------------------------------------------------------------------===//
33
John McCalld5532b62009-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 Gregor0da76df2009-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 Gregora2813ce2009-10-23 18:54:35 +0000111DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
112 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000113 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000114 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000115 QualType T)
116 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000117 DecoratedD(D,
118 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000119 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000120 Loc(NameLoc) {
121 if (Qualifier) {
122 NameQualifier *NQ = getNameQualifier();
123 NQ->NNS = Qualifier;
124 NQ->Range = QualifierRange;
125 }
126
John McCalld5532b62009-11-23 01:53:49 +0000127 if (TemplateArgs)
128 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000129
130 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000131}
132
133DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
134 NestedNameSpecifier *Qualifier,
135 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000136 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000137 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000138 QualType T,
139 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000140 std::size_t Size = sizeof(DeclRefExpr);
141 if (Qualifier != 0)
142 Size += sizeof(NameQualifier);
143
John McCalld5532b62009-11-23 01:53:49 +0000144 if (TemplateArgs)
145 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000146
147 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
148 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000150}
151
152SourceRange DeclRefExpr::getSourceRange() const {
153 // FIXME: Does not handle multi-token names well, e.g., operator[].
154 SourceRange R(Loc);
155
156 if (hasQualifier())
157 R.setBegin(getQualifierRange().getBegin());
158 if (hasExplicitTemplateArgumentList())
159 R.setEnd(getRAngleLoc());
160 return R;
161}
162
Anders Carlsson3a082d82009-09-08 18:24:21 +0000163// FIXME: Maybe this should use DeclPrinter with a special "print predefined
164// expr" policy instead.
165std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
166 const Decl *CurrentDecl) {
167 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
168 if (IT != PrettyFunction)
169 return FD->getNameAsString();
170
171 llvm::SmallString<256> Name;
172 llvm::raw_svector_ostream Out(Name);
173
174 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
175 if (MD->isVirtual())
176 Out << "virtual ";
177 }
178
179 PrintingPolicy Policy(Context.getLangOptions());
180 Policy.SuppressTagKind = true;
181
182 std::string Proto = FD->getQualifiedNameAsString(Policy);
183
John McCall183700f2009-09-21 23:43:11 +0000184 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000185 const FunctionProtoType *FT = 0;
186 if (FD->hasWrittenPrototype())
187 FT = dyn_cast<FunctionProtoType>(AFT);
188
189 Proto += "(";
190 if (FT) {
191 llvm::raw_string_ostream POut(Proto);
192 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
193 if (i) POut << ", ";
194 std::string Param;
195 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
196 POut << Param;
197 }
198
199 if (FT->isVariadic()) {
200 if (FD->getNumParams()) POut << ", ";
201 POut << "...";
202 }
203 }
204 Proto += ")";
205
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000206 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
207 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000208
209 Out << Proto;
210
211 Out.flush();
212 return Name.str().str();
213 }
214 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
215 llvm::SmallString<256> Name;
216 llvm::raw_svector_ostream Out(Name);
217 Out << (MD->isInstanceMethod() ? '-' : '+');
218 Out << '[';
219 Out << MD->getClassInterface()->getNameAsString();
220 if (const ObjCCategoryImplDecl *CID =
221 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
222 Out << '(';
223 Out << CID->getNameAsString();
224 Out << ')';
225 }
226 Out << ' ';
227 Out << MD->getSelector().getAsString();
228 Out << ']';
229
230 Out.flush();
231 return Name.str().str();
232 }
233 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
234 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
235 return "top level";
236 }
237 return "";
238}
239
Chris Lattnerda8249e2008-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 Johannesenee5a7002008-10-09 23:02:32 +0000245 bool ignored;
246 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
247 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000248 return V.convertToDouble();
249}
250
Chris Lattner2085fd62009-02-18 06:40:38 +0000251StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
252 unsigned ByteLength, bool Wide,
253 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000254 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000255 unsigned NumStrs) {
Chris Lattner2085fd62009-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 Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000271
Chris Lattner726e1682009-02-18 05:49:11 +0000272 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000273 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
274 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000275}
276
Douglas Gregor673ecd62009-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 Gregor42602bb2009-08-07 06:08:38 +0000288void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000289 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000290 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
Daniel Dunbarb6480232009-09-22 03:27:33 +0000293void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000294 if (StrData)
295 C.Deallocate(const_cast<char*>(StrData));
296
Daniel Dunbarb6480232009-09-22 03:27:33 +0000297 char *AStrData = new (C, 1) char[Str.size()];
298 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000299 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000300 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000301}
302
Reid Spencer5f016e22007-07-11 17:01:13 +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) {
307 default: assert(0 && "Unknown unary operator");
308 case PostInc: return "++";
309 case PostDec: return "--";
310 case PreInc: return "++";
311 case PreDec: return "--";
312 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";
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000321 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 }
323}
324
Mike Stump1eb44332009-09-09 15:08:12 +0000325UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000326UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
327 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000328 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-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 Gregorbc736fc2009-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000355//===----------------------------------------------------------------------===//
356// Postfix Operators.
357//===----------------------------------------------------------------------===//
358
Ted Kremenek668bf912009-02-09 20:51:47 +0000359CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000360 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000361 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000362 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000363 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000364 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek668bf912009-02-09 20:51:47 +0000366 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-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 Kremenek668bf912009-02-09 20:51:47 +0000370
Douglas Gregorb4609802008-11-14 16:09:21 +0000371 RParenLoc = rparenloc;
372}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000373
Ted Kremenek668bf912009-02-09 20:51:47 +0000374CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
375 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000376 : Expr(CallExprClass, t,
377 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000378 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000379 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000380
381 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000382 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000384 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 RParenLoc = rparenloc;
387}
388
Mike Stump1eb44332009-09-09 15:08:12 +0000389CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
390 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000391 SubExprs = new (C) Stmt*[1];
392}
393
Douglas Gregor42602bb2009-08-07 06:08:38 +0000394void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000395 DestroyChildren(C);
396 if (SubExprs) C.Deallocate(SubExprs);
397 this->~CallExpr();
398 C.Deallocate(this);
399}
400
Zhongxing Xua0042542009-07-17 07:29:51 +0000401FunctionDecl *CallExpr::getDirectCallee() {
402 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000403 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Zhongxing Xua0042542009-07-17 07:29:51 +0000404 return dyn_cast<FunctionDecl>(DRE->getDecl());
Zhongxing Xua0042542009-07-17 07:29:51 +0000405
406 return 0;
407}
408
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000412void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000413 // No change, just return.
414 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnerd18b3292007-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 Kremenek8189cde2009-02-07 01:47:29 +0000419 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-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 Dunbar68a049c2009-07-28 06:29:46 +0000425 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-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 Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor88c9a462009-04-17 21:46:47 +0000433 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000434 SubExprs = NewSubExprs;
435 this->NumArgs = NumArgs;
436}
437
Chris Lattnercb888962008-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 Gregor3c385e52009-02-14 18:57:46 +0000440unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000441 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000442 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-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 Lattnercb888962008-10-06 05:00:53 +0000446 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000448 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
449 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000450 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Anders Carlssonbcba2012008-01-31 02:13:57 +0000452 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
453 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000454 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000456 if (!FDecl->getIdentifier())
457 return 0;
458
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000459 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000460}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000461
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000462QualType CallExpr::getCallReturnType() const {
463 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000464 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000465 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000466 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000467 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000468
John McCall183700f2009-09-21 23:43:11 +0000469 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000470 return FnType->getResultType();
471}
Chris Lattnercb888962008-10-06 05:00:53 +0000472
Mike Stump1eb44332009-09-09 15:08:12 +0000473MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000474 SourceRange qualrange, ValueDecl *memberdecl,
John McCalld5532b62009-11-23 01:53:49 +0000475 SourceLocation l, const TemplateArgumentListInfo *targs,
476 QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000477 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-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 McCalld5532b62009-11-23 01:53:49 +0000481 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregor83f6faf2009-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 Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000489 // Initialize the explicit template argument list, if any.
John McCalld5532b62009-11-23 01:53:49 +0000490 if (targs)
491 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000492}
493
Mike Stump1eb44332009-09-09 15:08:12 +0000494MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
495 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000496 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000497 ValueDecl *memberdecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000498 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000499 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000500 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000501 std::size_t Size = sizeof(MemberExpr);
502 if (qual != 0)
503 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000504
John McCalld5532b62009-11-23 01:53:49 +0000505 if (targs)
506 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000508 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000509 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCalld5532b62009-11-23 01:53:49 +0000510 targs, ty);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000511}
512
Anders Carlssonf8ec55a2009-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 Carlsson11de6de2009-11-12 16:43:42 +0000521 case CastExpr::CK_BaseToDerived:
522 return "BaseToDerived";
Anders Carlssonf8ec55a2009-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 Carlsson1a31a182009-10-30 00:46:35 +0000537 case CastExpr::CK_DerivedToBaseMemberPointer:
538 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000539 case CastExpr::CK_UserDefinedConversion:
540 return "UserDefinedConversion";
541 case CastExpr::CK_ConstructorConversion:
542 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000543 case CastExpr::CK_IntegralToPointer:
544 return "IntegralToPointer";
545 case CastExpr::CK_PointerToIntegral:
546 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000547 case CastExpr::CK_ToVoid:
548 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000549 case CastExpr::CK_VectorSplat:
550 return "VectorSplat";
Anders Carlsson82debc72009-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 Kramerc6b29162009-10-18 19:02:15 +0000557 case CastExpr::CK_FloatingCast:
558 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000559 case CastExpr::CK_MemberPointerToBoolean:
560 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000561 case CastExpr::CK_AnyPointerToObjCPointerCast:
562 return "AnyPointerToObjCPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000565 assert(0 && "Unhandled cast kind!");
566 return 0;
567}
568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
570/// corresponds to, e.g. "<<=".
571const char *BinaryOperator::getOpcodeStr(Opcode Op) {
572 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000573 case PtrMemD: return ".*";
574 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 case Mul: return "*";
576 case Div: return "/";
577 case Rem: return "%";
578 case Add: return "+";
579 case Sub: return "-";
580 case Shl: return "<<";
581 case Shr: return ">>";
582 case LT: return "<";
583 case GT: return ">";
584 case LE: return "<=";
585 case GE: return ">=";
586 case EQ: return "==";
587 case NE: return "!=";
588 case And: return "&";
589 case Xor: return "^";
590 case Or: return "|";
591 case LAnd: return "&&";
592 case LOr: return "||";
593 case Assign: return "=";
594 case MulAssign: return "*=";
595 case DivAssign: return "/=";
596 case RemAssign: return "%=";
597 case AddAssign: return "+=";
598 case SubAssign: return "-=";
599 case ShlAssign: return "<<=";
600 case ShrAssign: return ">>=";
601 case AndAssign: return "&=";
602 case XorAssign: return "^=";
603 case OrAssign: return "|=";
604 case Comma: return ",";
605 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000606
607 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000608}
609
Mike Stump1eb44332009-09-09 15:08:12 +0000610BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000611BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
612 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000613 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000614 case OO_Plus: return Add;
615 case OO_Minus: return Sub;
616 case OO_Star: return Mul;
617 case OO_Slash: return Div;
618 case OO_Percent: return Rem;
619 case OO_Caret: return Xor;
620 case OO_Amp: return And;
621 case OO_Pipe: return Or;
622 case OO_Equal: return Assign;
623 case OO_Less: return LT;
624 case OO_Greater: return GT;
625 case OO_PlusEqual: return AddAssign;
626 case OO_MinusEqual: return SubAssign;
627 case OO_StarEqual: return MulAssign;
628 case OO_SlashEqual: return DivAssign;
629 case OO_PercentEqual: return RemAssign;
630 case OO_CaretEqual: return XorAssign;
631 case OO_AmpEqual: return AndAssign;
632 case OO_PipeEqual: return OrAssign;
633 case OO_LessLess: return Shl;
634 case OO_GreaterGreater: return Shr;
635 case OO_LessLessEqual: return ShlAssign;
636 case OO_GreaterGreaterEqual: return ShrAssign;
637 case OO_EqualEqual: return EQ;
638 case OO_ExclaimEqual: return NE;
639 case OO_LessEqual: return LE;
640 case OO_GreaterEqual: return GE;
641 case OO_AmpAmp: return LAnd;
642 case OO_PipePipe: return LOr;
643 case OO_Comma: return Comma;
644 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000645 }
646}
647
648OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
649 static const OverloadedOperatorKind OverOps[] = {
650 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
651 OO_Star, OO_Slash, OO_Percent,
652 OO_Plus, OO_Minus,
653 OO_LessLess, OO_GreaterGreater,
654 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
655 OO_EqualEqual, OO_ExclaimEqual,
656 OO_Amp,
657 OO_Caret,
658 OO_Pipe,
659 OO_AmpAmp,
660 OO_PipePipe,
661 OO_Equal, OO_StarEqual,
662 OO_SlashEqual, OO_PercentEqual,
663 OO_PlusEqual, OO_MinusEqual,
664 OO_LessLessEqual, OO_GreaterGreaterEqual,
665 OO_AmpEqual, OO_CaretEqual,
666 OO_PipeEqual,
667 OO_Comma
668 };
669 return OverOps[Opc];
670}
671
Mike Stump1eb44332009-09-09 15:08:12 +0000672InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000673 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000674 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000675 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000676 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor73460a32009-11-19 23:25:22 +0000677 UnionFieldInit(0), HadArrayRangeDesignator(false)
678{
679 for (unsigned I = 0; I != numInits; ++I) {
680 if (initExprs[I]->isTypeDependent())
681 TypeDependent = true;
682 if (initExprs[I]->isValueDependent())
683 ValueDependent = true;
684 }
685
Chris Lattner418f6c72008-10-26 23:43:26 +0000686 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000687}
Reid Spencer5f016e22007-07-11 17:01:13 +0000688
Douglas Gregorfa219202009-03-20 23:58:33 +0000689void InitListExpr::reserveInits(unsigned NumInits) {
690 if (NumInits > InitExprs.size())
691 InitExprs.reserve(NumInits);
692}
693
Douglas Gregor4c678342009-01-28 21:54:33 +0000694void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000695 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000696 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000697 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000698 InitExprs.resize(NumInits, 0);
699}
700
701Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
702 if (Init >= InitExprs.size()) {
703 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
704 InitExprs.back() = expr;
705 return 0;
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregor4c678342009-01-28 21:54:33 +0000708 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
709 InitExprs[Init] = expr;
710 return Result;
711}
712
Steve Naroffbfdcae62008-09-04 15:31:07 +0000713/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000714///
715const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000716 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000717 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000718}
719
Mike Stump1eb44332009-09-09 15:08:12 +0000720SourceLocation BlockExpr::getCaretLocation() const {
721 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000722}
Mike Stump1eb44332009-09-09 15:08:12 +0000723const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000724 return TheBlock->getBody();
725}
Mike Stump1eb44332009-09-09 15:08:12 +0000726Stmt *BlockExpr::getBody() {
727 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000728}
Steve Naroff56ee6892008-10-08 17:01:13 +0000729
730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731//===----------------------------------------------------------------------===//
732// Generic Expression Routines
733//===----------------------------------------------------------------------===//
734
Chris Lattner026dc962009-02-14 07:37:35 +0000735/// isUnusedResultAWarning - Return true if this immediate expression should
736/// be warned about if the result is unused. If so, fill in Loc and Ranges
737/// with location to warn on and the source range[s] to report with the
738/// warning.
739bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000740 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000741 // Don't warn if the expr is type dependent. The type could end up
742 // instantiating to void.
743 if (isTypeDependent())
744 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 switch (getStmtClass()) {
747 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000748 Loc = getExprLoc();
749 R1 = getSourceRange();
750 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000752 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000753 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 case UnaryOperatorClass: {
755 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000758 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 case UnaryOperator::PostInc:
760 case UnaryOperator::PostDec:
761 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000762 case UnaryOperator::PreDec: // ++/--
763 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000764 case UnaryOperator::Deref:
765 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000766 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000767 return false;
768 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 case UnaryOperator::Real:
770 case UnaryOperator::Imag:
771 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000772 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
773 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000774 return false;
775 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000777 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 }
Chris Lattner026dc962009-02-14 07:37:35 +0000779 Loc = UO->getOperatorLoc();
780 R1 = UO->getSubExpr()->getSourceRange();
781 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000783 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000784 const BinaryOperator *BO = cast<BinaryOperator>(this);
785 // Consider comma to have side effects if the LHS or RHS does.
786 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000787 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
788 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Chris Lattner026dc962009-02-14 07:37:35 +0000790 if (BO->isAssignmentOp())
791 return false;
792 Loc = BO->getOperatorLoc();
793 R1 = BO->getLHS()->getSourceRange();
794 R2 = BO->getRHS()->getSourceRange();
795 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000796 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000797 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000798 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000800 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000801 // The condition must be evaluated, but if either the LHS or RHS is a
802 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000803 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000804 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000805 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000806 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000807 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000808 }
809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000811 // If the base pointer or element is to a volatile pointer/field, accessing
812 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000813 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000814 return false;
815 Loc = cast<MemberExpr>(this)->getMemberLoc();
816 R1 = SourceRange(Loc, Loc);
817 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
818 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case ArraySubscriptExprClass:
821 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000822 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000823 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000824 return false;
825 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
826 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
827 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
828 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000829
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000831 case CXXOperatorCallExprClass:
832 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000833 // If this is a direct call, get the callee.
834 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000835 if (const FunctionDecl *FD = CE->getDirectCallee()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000836 // If the callee has attribute pure, const, or warn_unused_result, warn
837 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000838 //
839 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
840 // updated to match for QoI.
841 if (FD->getAttr<WarnUnusedResultAttr>() ||
842 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
843 Loc = CE->getCallee()->getLocStart();
844 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000846 if (unsigned NumArgs = CE->getNumArgs())
847 R2 = SourceRange(CE->getArg(0)->getLocStart(),
848 CE->getArg(NumArgs-1)->getLocEnd());
849 return true;
850 }
Chris Lattner026dc962009-02-14 07:37:35 +0000851 }
852 return false;
853 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000854
855 case CXXTemporaryObjectExprClass:
856 case CXXConstructExprClass:
857 return false;
858
Chris Lattnera9c01022007-09-26 22:06:30 +0000859 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000860 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000862 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000863#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000864 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000865 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000866 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000867 Loc = Ref->getLocation();
868 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
869 if (Ref->getBase())
870 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000871#else
872 Loc = getExprLoc();
873 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000874#endif
875 return true;
876 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000877 case StmtExprClass: {
878 // Statement exprs don't logically have side effects themselves, but are
879 // sometimes used in macros in ways that give them a type that is unused.
880 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
881 // however, if the result of the stmt expr is dead, we don't want to emit a
882 // warning.
883 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
884 if (!CS->body_empty())
885 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000886 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattner026dc962009-02-14 07:37:35 +0000888 Loc = cast<StmtExpr>(this)->getLParenLoc();
889 R1 = getSourceRange();
890 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000891 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000892 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000893 // If this is an explicit cast to void, allow it. People do this when they
894 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000895 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000896 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000897 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
898 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
899 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000900 case CXXFunctionalCastExprClass: {
901 const CastExpr *CE = cast<CastExpr>(this);
902
903 // If this is a cast to void or a constructor conversion, check the operand.
904 // Otherwise, the result of the cast is unused.
905 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
906 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000907 return (cast<CastExpr>(this)->getSubExpr()
908 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000909 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
910 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
911 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000912 }
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Eli Friedman4be1f472008-05-19 21:24:43 +0000914 case ImplicitCastExprClass:
915 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000916 return (cast<ImplicitCastExpr>(this)
917 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000918
Chris Lattner04421082008-04-08 04:40:51 +0000919 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000920 return (cast<CXXDefaultArgExpr>(this)
921 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000922
923 case CXXNewExprClass:
924 // FIXME: In theory, there might be new expressions that don't have side
925 // effects (e.g. a placement new with an uninitialized POD).
926 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000927 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000928 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000929 return (cast<CXXBindTemporaryExpr>(this)
930 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000931 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000932 return (cast<CXXExprWithTemporaries>(this)
933 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000934 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000935}
936
Douglas Gregorba7e2102008-10-22 15:04:37 +0000937/// DeclCanBeLvalue - Determine whether the given declaration can be
938/// an lvalue. This is a helper routine for isLvalue.
939static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000940 // C++ [temp.param]p6:
941 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000942 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000943 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
944 return NTTParm->getType()->isReferenceType();
945
Douglas Gregor44b43212008-12-11 16:49:14 +0000946 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000947 // C++ 3.10p2: An lvalue refers to an object or function.
948 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +0000949 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000950}
951
Reid Spencer5f016e22007-07-11 17:01:13 +0000952/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
953/// incomplete type other than void. Nonarray expressions that can be lvalues:
954/// - name, where name must be a variable
955/// - e[i]
956/// - (e), where e must be an lvalue
957/// - e.name, where e must be an lvalue
958/// - e->name
959/// - *e, the type of e cannot be a function type
960/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000961/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000962/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000963///
Chris Lattner28be73f2008-07-26 21:30:36 +0000964Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000965 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
966
967 isLvalueResult Res = isLvalueInternal(Ctx);
968 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
969 return Res;
970
Douglas Gregor98cd5992008-10-21 23:43:52 +0000971 // first, check the type (C99 6.3.2.1). Expressions with function
972 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +0000973 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 return LV_NotObjectType;
975
Steve Naroffacb818a2008-02-10 01:39:04 +0000976 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +0000977 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +0000978 return LV_IncompleteVoidType;
979
Eli Friedman53202852009-05-03 22:36:05 +0000980 return LV_Valid;
981}
Bill Wendling08ad47c2007-07-17 03:52:31 +0000982
Eli Friedman53202852009-05-03 22:36:05 +0000983// Check whether the expression can be sanely treated like an l-value
984Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000986 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000987 case StringLiteralClass: // C99 6.5.1p4
988 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +0000989 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
991 // For vectors, make sure base is an lvalue (i.e. not a function call).
992 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +0000993 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +0000995 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +0000996 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
997 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 return LV_Valid;
999 break;
Chris Lattner41110242008-06-17 18:05:57 +00001000 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001001 case BlockDeclRefExprClass: {
1002 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001003 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001004 return LV_Valid;
1005 break;
1006 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001007 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001009 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1010 NamedDecl *Member = m->getMemberDecl();
1011 // C++ [expr.ref]p4:
1012 // If E2 is declared to have type "reference to T", then E1.E2
1013 // is an lvalue.
1014 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1015 if (Value->getType()->isReferenceType())
1016 return LV_Valid;
1017
1018 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001019 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001020 return LV_Valid;
1021
1022 // -- If E2 is a non-static data member [...]. If E1 is an
1023 // lvalue, then E1.E2 is an lvalue.
1024 if (isa<FieldDecl>(Member))
1025 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
1026
1027 // -- If it refers to a static member function [...], then
1028 // E1.E2 is an lvalue.
1029 // -- Otherwise, if E1.E2 refers to a non-static member
1030 // function [...], then E1.E2 is not an lvalue.
1031 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1032 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1033
1034 // -- If E2 is a member enumerator [...], the expression E1.E2
1035 // is not an lvalue.
1036 if (isa<EnumConstantDecl>(Member))
1037 return LV_InvalidExpression;
1038
1039 // Not an lvalue.
1040 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001041 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001042
1043 // C99 6.5.2.3p4
Chris Lattner28be73f2008-07-26 21:30:36 +00001044 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001045 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001046 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001048 return LV_Valid; // C99 6.5.3p4
1049
1050 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001051 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1052 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001053 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001054
1055 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1056 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1057 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1058 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001060 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001061 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001062 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001064 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001065 case BinaryOperatorClass:
1066 case CompoundAssignOperatorClass: {
1067 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001068
1069 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1070 BinOp->getOpcode() == BinaryOperator::Comma)
1071 return BinOp->getRHS()->isLvalue(Ctx);
1072
Sebastian Redl22460502009-02-07 00:15:38 +00001073 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001074 // The result of a .* expression is an lvalue only if its first operand is
1075 // an lvalue and its second operand is a pointer to data member.
1076 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001077 !BinOp->getType()->isFunctionType())
1078 return BinOp->getLHS()->isLvalue(Ctx);
1079
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001080 // The result of an ->* expression is an lvalue only if its second operand
1081 // is a pointer to data member.
1082 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1083 !BinOp->getType()->isFunctionType()) {
1084 QualType Ty = BinOp->getRHS()->getType();
1085 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1086 return LV_Valid;
1087 }
1088
Douglas Gregorbf3af052008-11-13 20:12:29 +00001089 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001090 return LV_InvalidExpression;
1091
Douglas Gregorbf3af052008-11-13 20:12:29 +00001092 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001093 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001094 // The result of an assignment operation [...] is an lvalue.
1095 return LV_Valid;
1096
1097
1098 // C99 6.5.16:
1099 // An assignment expression [...] is not an lvalue.
1100 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001103 case CXXOperatorCallExprClass:
1104 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001105 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001106 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001107 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001108 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1109 if (ReturnType->isLValueReferenceType())
1110 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001111
Douglas Gregor9d293df2008-10-28 00:22:11 +00001112 break;
1113 }
Steve Naroffe6386392007-12-05 04:00:10 +00001114 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1115 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001116 case ChooseExprClass:
1117 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001118 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001119 case ExtVectorElementExprClass:
1120 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001121 return LV_DuplicateVectorComponents;
1122 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001123 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1124 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001125 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1126 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001127 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001128 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001129 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001130 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001131 case UnresolvedLookupExprClass:
1132 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001133 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001134 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001135 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001136 case CXXFunctionalCastExprClass:
1137 case CXXStaticCastExprClass:
1138 case CXXDynamicCastExprClass:
1139 case CXXReinterpretCastExprClass:
1140 case CXXConstCastExprClass:
1141 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001142 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001143 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1144 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001145 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1146 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001147 return LV_Valid;
1148 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001149 case CXXTypeidExprClass:
1150 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1151 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001152 case CXXBindTemporaryExprClass:
1153 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1154 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +00001155 case ConditionalOperatorClass: {
1156 // Complicated handling is only for C++.
1157 if (!Ctx.getLangOptions().CPlusPlus)
1158 return LV_InvalidExpression;
1159
1160 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1161 // everywhere there's an object converted to an rvalue. Also, any other
1162 // casts should be wrapped by ImplicitCastExprs. There's just the special
1163 // case involving throws to work out.
1164 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001165 Expr *True = Cond->getTrueExpr();
1166 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001167 // C++0x 5.16p2
1168 // If either the second or the third operand has type (cv) void, [...]
1169 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001170 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001171 return LV_InvalidExpression;
1172
1173 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001174 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001175 return LV_InvalidExpression;
1176
1177 // That's it.
1178 return LV_Valid;
1179 }
1180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 default:
1182 break;
1183 }
1184 return LV_InvalidExpression;
1185}
1186
1187/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1188/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001189/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001190/// recursively, any member or element of all contained aggregates or unions)
1191/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001192Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001193Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001194 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001197 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001198 // C++ 3.10p11: Functions cannot be modified, but pointers to
1199 // functions can be modifiable.
1200 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1201 return MLV_NotObjectType;
1202 break;
1203
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 case LV_NotObjectType: return MLV_NotObjectType;
1205 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001206 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001207 case LV_InvalidExpression:
1208 // If the top level is a C-style cast, and the subexpression is a valid
1209 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1210 // GCC extension. We don't support it, but we want to produce good
1211 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001212 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1213 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1214 if (Loc)
1215 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001216 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001217 }
1218 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001219 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001220 case LV_MemberFunction: return MLV_MemberFunction;
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001222
1223 // The following is illegal:
1224 // void takeclosure(void (^C)(void));
1225 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1226 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001227 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001228 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1229 return MLV_NotBlockQualified;
1230 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001231
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001232 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001233 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001234 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1235 if (Expr->getSetterMethod() == 0)
1236 return MLV_NoSetterProperty;
1237 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001238
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001239 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001241 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001243 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001245 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek6217b802009-07-29 21:53:49 +00001248 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001249 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 return MLV_ConstQualified;
1251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Mike Stump1eb44332009-09-09 15:08:12 +00001253 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001254}
1255
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001256/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001257/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001258bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001259 switch (getStmtClass()) {
1260 default:
1261 return false;
1262 case ObjCIvarRefExprClass:
1263 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001264 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001265 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001266 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001267 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001268 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001269 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001270 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001271 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001272 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001273 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001274 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1275 if (VD->hasGlobalStorage())
1276 return true;
1277 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001278 // dereferencing to a pointer is always a gc'able candidate,
1279 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001280 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001281 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001282 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001283 return false;
1284 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001285 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001286 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001287 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001288 }
1289 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001290 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001291 }
1292}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001293Expr* Expr::IgnoreParens() {
1294 Expr* E = this;
1295 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1296 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001298 return E;
1299}
1300
Chris Lattner56f34942008-02-13 01:02:39 +00001301/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1302/// or CastExprs or ImplicitCastExprs, returning their operand.
1303Expr *Expr::IgnoreParenCasts() {
1304 Expr *E = this;
1305 while (true) {
1306 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1307 E = P->getSubExpr();
1308 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1309 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001310 else
1311 return E;
1312 }
1313}
1314
Chris Lattnerecdd8412009-03-13 17:28:01 +00001315/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1316/// value (including ptr->int casts of the same size). Strip off any
1317/// ParenExpr or CastExprs, returning their operand.
1318Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1319 Expr *E = this;
1320 while (true) {
1321 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1322 E = P->getSubExpr();
1323 continue;
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Chris Lattnerecdd8412009-03-13 17:28:01 +00001326 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1327 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1328 // ptr<->int casts of the same width. We also ignore all identify casts.
1329 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Chris Lattnerecdd8412009-03-13 17:28:01 +00001331 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1332 E = SE;
1333 continue;
1334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Chris Lattnerecdd8412009-03-13 17:28:01 +00001336 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1337 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1338 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1339 E = SE;
1340 continue;
1341 }
1342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Chris Lattnerecdd8412009-03-13 17:28:01 +00001344 return E;
1345 }
1346}
1347
1348
Douglas Gregor898574e2008-12-05 23:32:09 +00001349/// hasAnyTypeDependentArguments - Determines if any of the expressions
1350/// in Exprs is type-dependent.
1351bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1352 for (unsigned I = 0; I < NumExprs; ++I)
1353 if (Exprs[I]->isTypeDependent())
1354 return true;
1355
1356 return false;
1357}
1358
1359/// hasAnyValueDependentArguments - Determines if any of the expressions
1360/// in Exprs is value-dependent.
1361bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1362 for (unsigned I = 0; I < NumExprs; ++I)
1363 if (Exprs[I]->isValueDependent())
1364 return true;
1365
1366 return false;
1367}
1368
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001369bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001370 // This function is attempting whether an expression is an initializer
1371 // which can be evaluated at compile-time. isEvaluatable handles most
1372 // of the cases, but it can't deal with some initializer-specific
1373 // expressions, and it can't deal with aggregates; we deal with those here,
1374 // and fall back to isEvaluatable for the other cases.
1375
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001376 // FIXME: This function assumes the variable being assigned to
1377 // isn't a reference type!
1378
Anders Carlssone8a32b82008-11-24 05:23:59 +00001379 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001380 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001381 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001382 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001383 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001384 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001385 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001386 // This handles gcc's extension that allows global initializers like
1387 // "struct x {int x;} x = (struct x) {};".
1388 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001389 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001390 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001391 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001392 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001393 // FIXME: This doesn't deal with fields with reference types correctly.
1394 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1395 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001396 const InitListExpr *Exp = cast<InitListExpr>(this);
1397 unsigned numInits = Exp->getNumInits();
1398 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001399 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001400 return false;
1401 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001402 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001403 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001404 case ImplicitValueInitExprClass:
1405 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001406 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001407 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001408 case UnaryOperatorClass: {
1409 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1410 if (Exp->getOpcode() == UnaryOperator::Extension)
1411 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1412 break;
1413 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001414 case BinaryOperatorClass: {
1415 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1416 // but this handles the common case.
1417 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1418 if (Exp->getOpcode() == BinaryOperator::Sub &&
1419 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1420 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1421 return true;
1422 break;
1423 }
Chris Lattner81045d82009-04-21 05:19:11 +00001424 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001425 case CStyleCastExprClass:
1426 // Handle casts with a destination that's a struct or union; this
1427 // deals with both the gcc no-op struct cast extension and the
1428 // cast-to-union extension.
1429 if (getType()->isRecordType())
1430 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001431
1432 // Integer->integer casts can be handled here, which is important for
1433 // things like (int)(&&x-&&y). Scary but true.
1434 if (getType()->isIntegerType() &&
1435 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1436 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1437
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001438 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001439 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001440 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001441}
1442
Reid Spencer5f016e22007-07-11 17:01:13 +00001443/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001444/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001445
1446/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1447/// comma, etc
1448///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001449/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1450/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1451/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001452
Eli Friedmane28d7192009-02-26 09:29:13 +00001453// CheckICE - This function does the fundamental ICE checking: the returned
1454// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1455// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001456// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001457// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001458// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001459//
1460// Meanings of Val:
1461// 0: This expression is an ICE if it can be evaluated by Evaluate.
1462// 1: This expression is not an ICE, but if it isn't evaluated, it's
1463// a legal subexpression for an ICE. This return value is used to handle
1464// the comma operator in C99 mode.
1465// 2: This expression is not an ICE, and is not a legal subexpression for one.
1466
1467struct ICEDiag {
1468 unsigned Val;
1469 SourceLocation Loc;
1470
1471 public:
1472 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1473 ICEDiag() : Val(0) {}
1474};
1475
1476ICEDiag NoDiag() { return ICEDiag(); }
1477
Eli Friedman60ce9632009-02-27 04:07:58 +00001478static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1479 Expr::EvalResult EVResult;
1480 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1481 !EVResult.Val.isInt()) {
1482 return ICEDiag(2, E->getLocStart());
1483 }
1484 return NoDiag();
1485}
1486
Eli Friedmane28d7192009-02-26 09:29:13 +00001487static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001488 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001489 if (!E->getType()->isIntegralType()) {
1490 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001491 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001492
1493 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001494#define STMT(Node, Base) case Expr::Node##Class:
1495#define EXPR(Node, Base)
1496#include "clang/AST/StmtNodes.def"
1497 case Expr::PredefinedExprClass:
1498 case Expr::FloatingLiteralClass:
1499 case Expr::ImaginaryLiteralClass:
1500 case Expr::StringLiteralClass:
1501 case Expr::ArraySubscriptExprClass:
1502 case Expr::MemberExprClass:
1503 case Expr::CompoundAssignOperatorClass:
1504 case Expr::CompoundLiteralExprClass:
1505 case Expr::ExtVectorElementExprClass:
1506 case Expr::InitListExprClass:
1507 case Expr::DesignatedInitExprClass:
1508 case Expr::ImplicitValueInitExprClass:
1509 case Expr::ParenListExprClass:
1510 case Expr::VAArgExprClass:
1511 case Expr::AddrLabelExprClass:
1512 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001513 case Expr::CXXMemberCallExprClass:
1514 case Expr::CXXDynamicCastExprClass:
1515 case Expr::CXXTypeidExprClass:
1516 case Expr::CXXNullPtrLiteralExprClass:
1517 case Expr::CXXThisExprClass:
1518 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001519 case Expr::CXXNewExprClass:
1520 case Expr::CXXDeleteExprClass:
1521 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001522 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001523 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001524 case Expr::CXXConstructExprClass:
1525 case Expr::CXXBindTemporaryExprClass:
1526 case Expr::CXXExprWithTemporariesClass:
1527 case Expr::CXXTemporaryObjectExprClass:
1528 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001529 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001530 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001531 case Expr::ObjCStringLiteralClass:
1532 case Expr::ObjCEncodeExprClass:
1533 case Expr::ObjCMessageExprClass:
1534 case Expr::ObjCSelectorExprClass:
1535 case Expr::ObjCProtocolExprClass:
1536 case Expr::ObjCIvarRefExprClass:
1537 case Expr::ObjCPropertyRefExprClass:
1538 case Expr::ObjCImplicitSetterGetterRefExprClass:
1539 case Expr::ObjCSuperExprClass:
1540 case Expr::ObjCIsaExprClass:
1541 case Expr::ShuffleVectorExprClass:
1542 case Expr::BlockExprClass:
1543 case Expr::BlockDeclRefExprClass:
1544 case Expr::NoStmtClass:
1545 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001546 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001547
Douglas Gregor043cad22009-09-11 00:18:58 +00001548 case Expr::GNUNullExprClass:
1549 // GCC considers the GNU __null value to be an integral constant expression.
1550 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001551
Eli Friedmane28d7192009-02-26 09:29:13 +00001552 case Expr::ParenExprClass:
1553 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1554 case Expr::IntegerLiteralClass:
1555 case Expr::CharacterLiteralClass:
1556 case Expr::CXXBoolLiteralExprClass:
1557 case Expr::CXXZeroInitValueExprClass:
1558 case Expr::TypesCompatibleExprClass:
1559 case Expr::UnaryTypeTraitExprClass:
1560 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001561 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001562 case Expr::CXXOperatorCallExprClass: {
1563 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001564 if (CE->isBuiltinCall(Ctx))
1565 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001566 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001567 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001568 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001569 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1570 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001571 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001572 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001573 // C++ 7.1.5.1p2
1574 // A variable of non-volatile const-qualified integral or enumeration
1575 // type initialized by an ICE can be used in ICEs.
1576 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001577 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001578 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1579 if (Quals.hasVolatile() || !Quals.hasConst())
1580 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1581
1582 // Look for the definition of this variable, which will actually have
1583 // an initializer.
1584 const VarDecl *Def = 0;
1585 const Expr *Init = Dcl->getDefinition(Def);
1586 if (Init) {
1587 if (Def->isInitKnownICE()) {
1588 // We have already checked whether this subexpression is an
1589 // integral constant expression.
1590 if (Def->isInitICE())
1591 return NoDiag();
1592 else
1593 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1594 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001595
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001596 // C++ [class.static.data]p4:
1597 // If a static data member is of const integral or const
1598 // enumeration type, its declaration in the class definition can
1599 // specify a constant-initializer which shall be an integral
1600 // constant expression (5.19). In that case, the member can appear
1601 // in integral constant expressions.
1602 if (Def->isOutOfLine()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001603 Dcl->setInitKnownICE(false);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001604 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1605 }
Eli Friedmanc0131182009-12-03 20:31:57 +00001606
1607 if (Dcl->isCheckingICE()) {
1608 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1609 }
1610
1611 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001612 ICEDiag Result = CheckICE(Init, Ctx);
1613 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001614 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001615 return Result;
1616 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001617 }
1618 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001619 return ICEDiag(2, E->getLocStart());
1620 case Expr::UnaryOperatorClass: {
1621 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001623 case UnaryOperator::PostInc:
1624 case UnaryOperator::PostDec:
1625 case UnaryOperator::PreInc:
1626 case UnaryOperator::PreDec:
1627 case UnaryOperator::AddrOf:
1628 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001629 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001630
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001632 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001636 case UnaryOperator::Real:
1637 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001638 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001639 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001640 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1641 // Evaluate matches the proposed gcc behavior for cases like
1642 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1643 // compliance: we should warn earlier for offsetof expressions with
1644 // array subscripts that aren't ICEs, and if the array subscripts
1645 // are ICEs, the value of the offsetof must be an integer constant.
1646 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001649 case Expr::SizeOfAlignOfExprClass: {
1650 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1651 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1652 return ICEDiag(2, E->getLocStart());
1653 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001655 case Expr::BinaryOperatorClass: {
1656 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001658 case BinaryOperator::PtrMemD:
1659 case BinaryOperator::PtrMemI:
1660 case BinaryOperator::Assign:
1661 case BinaryOperator::MulAssign:
1662 case BinaryOperator::DivAssign:
1663 case BinaryOperator::RemAssign:
1664 case BinaryOperator::AddAssign:
1665 case BinaryOperator::SubAssign:
1666 case BinaryOperator::ShlAssign:
1667 case BinaryOperator::ShrAssign:
1668 case BinaryOperator::AndAssign:
1669 case BinaryOperator::XorAssign:
1670 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001671 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001672
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001676 case BinaryOperator::Add:
1677 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001680 case BinaryOperator::LT:
1681 case BinaryOperator::GT:
1682 case BinaryOperator::LE:
1683 case BinaryOperator::GE:
1684 case BinaryOperator::EQ:
1685 case BinaryOperator::NE:
1686 case BinaryOperator::And:
1687 case BinaryOperator::Xor:
1688 case BinaryOperator::Or:
1689 case BinaryOperator::Comma: {
1690 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1691 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001692 if (Exp->getOpcode() == BinaryOperator::Div ||
1693 Exp->getOpcode() == BinaryOperator::Rem) {
1694 // Evaluate gives an error for undefined Div/Rem, so make sure
1695 // we don't evaluate one.
1696 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1697 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1698 if (REval == 0)
1699 return ICEDiag(1, E->getLocStart());
1700 if (REval.isSigned() && REval.isAllOnesValue()) {
1701 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1702 if (LEval.isMinSignedValue())
1703 return ICEDiag(1, E->getLocStart());
1704 }
1705 }
1706 }
1707 if (Exp->getOpcode() == BinaryOperator::Comma) {
1708 if (Ctx.getLangOptions().C99) {
1709 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1710 // if it isn't evaluated.
1711 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1712 return ICEDiag(1, E->getLocStart());
1713 } else {
1714 // In both C89 and C++, commas in ICEs are illegal.
1715 return ICEDiag(2, E->getLocStart());
1716 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001717 }
1718 if (LHSResult.Val >= RHSResult.Val)
1719 return LHSResult;
1720 return RHSResult;
1721 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001723 case BinaryOperator::LOr: {
1724 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1725 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1726 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1727 // Rare case where the RHS has a comma "side-effect"; we need
1728 // to actually check the condition to see whether the side
1729 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001730 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001731 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001732 return RHSResult;
1733 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001734 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001735
Eli Friedmane28d7192009-02-26 09:29:13 +00001736 if (LHSResult.Val >= RHSResult.Val)
1737 return LHSResult;
1738 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001739 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001740 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001742 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001743 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001744 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001745 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001746 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001747 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001748 case Expr::CXXStaticCastExprClass:
1749 case Expr::CXXReinterpretCastExprClass:
1750 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001751 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1752 if (SubExpr->getType()->isIntegralType())
1753 return CheckICE(SubExpr, Ctx);
1754 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1755 return NoDiag();
1756 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001758 case Expr::ConditionalOperatorClass: {
1759 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001760 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001761 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001762 // expression, and it is fully evaluated. This is an important GNU
1763 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001764 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001765 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001766 Expr::EvalResult EVResult;
1767 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1768 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001769 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001770 }
1771 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001772 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001773 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1774 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1775 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1776 if (CondResult.Val == 2)
1777 return CondResult;
1778 if (TrueResult.Val == 2)
1779 return TrueResult;
1780 if (FalseResult.Val == 2)
1781 return FalseResult;
1782 if (CondResult.Val == 1)
1783 return CondResult;
1784 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1785 return NoDiag();
1786 // Rare case where the diagnostics depend on which side is evaluated
1787 // Note that if we get here, CondResult is 0, and at least one of
1788 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001789 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001790 return FalseResult;
1791 }
1792 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001794 case Expr::CXXDefaultArgExprClass:
1795 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001796 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001797 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001798 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001800
Douglas Gregorf2991242009-09-10 23:31:45 +00001801 // Silence a GCC warning
1802 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001803}
Reid Spencer5f016e22007-07-11 17:01:13 +00001804
Eli Friedmane28d7192009-02-26 09:29:13 +00001805bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1806 SourceLocation *Loc, bool isEvaluated) const {
1807 ICEDiag d = CheckICE(this, Ctx);
1808 if (d.Val != 0) {
1809 if (Loc) *Loc = d.Loc;
1810 return false;
1811 }
1812 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001813 if (!Evaluate(EvalResult, Ctx))
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001814 llvm::llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001815 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1816 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001817 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 return true;
1819}
1820
Reid Spencer5f016e22007-07-11 17:01:13 +00001821/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1822/// integer constant expression with the value zero, or if this is one that is
1823/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001824bool Expr::isNullPointerConstant(ASTContext &Ctx,
1825 NullPointerConstantValueDependence NPC) const {
1826 if (isValueDependent()) {
1827 switch (NPC) {
1828 case NPC_NeverValueDependent:
1829 assert(false && "Unexpected value dependent expression!");
1830 // If the unthinkable happens, fall through to the safest alternative.
1831
1832 case NPC_ValueDependentIsNull:
1833 return isTypeDependent() || getType()->isIntegralType();
1834
1835 case NPC_ValueDependentIsNotNull:
1836 return false;
1837 }
1838 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001839
Sebastian Redl07779722008-10-31 14:43:28 +00001840 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001841 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001842 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001843 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001844 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001845 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001846 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001847 Pointee->isVoidType() && // to void*
1848 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001849 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001850 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001852 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1853 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001854 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001855 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1856 // Accept ((void*)0) as a null pointer constant, as many other
1857 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001858 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001859 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001860 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001861 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001862 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001863 } else if (isa<GNUNullExpr>(this)) {
1864 // The GNU __null extension is always a null pointer constant.
1865 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001866 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001867
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001868 // C++0x nullptr_t is always a null pointer constant.
1869 if (getType()->isNullPtrType())
1870 return true;
1871
Steve Naroffaa58f002008-01-14 16:10:57 +00001872 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001873 if (!getType()->isIntegerType() ||
1874 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001875 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 // If we have an integer constant expression, we need to *evaluate* it and
1878 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001879 llvm::APSInt Result;
1880 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001881}
Steve Naroff31a45842007-07-28 23:10:27 +00001882
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001883FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001884 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001885
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001886 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001887 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001888 if (Field->isBitField())
1889 return Field;
1890
1891 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1892 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1893 return BinOp->getLHS()->getBitField();
1894
1895 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001896}
1897
Chris Lattner2140e902009-02-16 22:14:05 +00001898/// isArrow - Return true if the base expression is a pointer to vector,
1899/// return false if the base expression is a vector.
1900bool ExtVectorElementExpr::isArrow() const {
1901 return getBase()->getType()->isPointerType();
1902}
1903
Nate Begeman213541a2008-04-18 23:10:10 +00001904unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001905 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001906 return VT->getNumElements();
1907 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001908}
1909
Nate Begeman8a997642008-05-09 06:41:27 +00001910/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001911bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001912 // FIXME: Refactor this code to an accessor on the AST node which returns the
1913 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001914 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001915
1916 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001917 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001918 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Nate Begeman190d6a22009-01-18 02:01:21 +00001920 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001921 if (Comp[0] == 's' || Comp[0] == 'S')
1922 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Daniel Dunbar15027422009-10-17 23:53:04 +00001924 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1925 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001926 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001927
Steve Narofffec0b492007-07-30 03:29:09 +00001928 return false;
1929}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001930
Nate Begeman8a997642008-05-09 06:41:27 +00001931/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001932void ExtVectorElementExpr::getEncodedElementAccess(
1933 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001934 llvm::StringRef Comp = Accessor->getName();
1935 if (Comp[0] == 's' || Comp[0] == 'S')
1936 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001938 bool isHi = Comp == "hi";
1939 bool isLo = Comp == "lo";
1940 bool isEven = Comp == "even";
1941 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Nate Begeman8a997642008-05-09 06:41:27 +00001943 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1944 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Nate Begeman8a997642008-05-09 06:41:27 +00001946 if (isHi)
1947 Index = e + i;
1948 else if (isLo)
1949 Index = i;
1950 else if (isEven)
1951 Index = 2 * i;
1952 else if (isOdd)
1953 Index = 2 * i + 1;
1954 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001955 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001956
Nate Begeman3b8d1162008-05-13 21:03:02 +00001957 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001958 }
Nate Begeman8a997642008-05-09 06:41:27 +00001959}
1960
Steve Naroff68d331a2007-09-27 14:38:14 +00001961// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001962ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001963 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001964 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001965 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001966 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001967 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001968 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001969 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00001970 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00001971 if (NumArgs) {
1972 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001973 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1974 }
Steve Naroff563477d2007-09-18 23:55:05 +00001975 LBracloc = LBrac;
1976 RBracloc = RBrac;
1977}
1978
Mike Stump1eb44332009-09-09 15:08:12 +00001979// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00001980// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001981ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001982 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00001983 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001984 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00001985 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00001986 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001987 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00001988 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00001989 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00001990 if (NumArgs) {
1991 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001992 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1993 }
Steve Naroff563477d2007-09-18 23:55:05 +00001994 LBracloc = LBrac;
1995 RBracloc = RBrac;
1996}
1997
Mike Stump1eb44332009-09-09 15:08:12 +00001998// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00001999ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2000 QualType retType, ObjCMethodDecl *mproto,
2001 SourceLocation LBrac, SourceLocation RBrac,
2002 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00002003: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00002004MethodProto(mproto) {
2005 NumArgs = nargs;
2006 SubExprs = new Stmt*[NumArgs+1];
2007 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2008 if (NumArgs) {
2009 for (unsigned i = 0; i != NumArgs; ++i)
2010 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2011 }
2012 LBracloc = LBrac;
2013 RBracloc = RBrac;
2014}
2015
2016ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2017 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2018 switch (x & Flags) {
2019 default:
2020 assert(false && "Invalid ObjCMessageExpr.");
2021 case IsInstMeth:
2022 return ClassInfo(0, 0);
2023 case IsClsMethDeclUnknown:
2024 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2025 case IsClsMethDeclKnown: {
2026 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2027 return ClassInfo(D, D->getIdentifier());
2028 }
2029 }
2030}
2031
Chris Lattner0389e6b2009-04-26 00:44:05 +00002032void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2033 if (CI.first == 0 && CI.second == 0)
2034 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2035 else if (CI.first == 0)
2036 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2037 else
2038 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2039}
2040
2041
Chris Lattner27437ca2007-10-25 00:29:32 +00002042bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002043 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002044}
2045
Nate Begeman888376a2009-08-12 02:28:50 +00002046void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2047 unsigned NumExprs) {
2048 if (SubExprs) C.Deallocate(SubExprs);
2049
2050 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002051 this->NumExprs = NumExprs;
2052 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002053}
Nate Begeman888376a2009-08-12 02:28:50 +00002054
2055void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2056 DestroyChildren(C);
2057 if (SubExprs) C.Deallocate(SubExprs);
2058 this->~ShuffleVectorExpr();
2059 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002060}
2061
Douglas Gregor42602bb2009-08-07 06:08:38 +00002062void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002063 // Override default behavior of traversing children. If this has a type
2064 // operand and the type is a variable-length array, the child iteration
2065 // will iterate over the size expression. However, this expression belongs
2066 // to the type, not to this, so we don't want to delete it.
2067 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002068 if (isArgumentType()) {
2069 this->~SizeOfAlignOfExpr();
2070 C.Deallocate(this);
2071 }
Sebastian Redl05189992008-11-11 17:56:53 +00002072 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002073 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002074}
2075
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002076//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002077// DesignatedInitExpr
2078//===----------------------------------------------------------------------===//
2079
2080IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2081 assert(Kind == FieldDesignator && "Only valid on a field designator");
2082 if (Field.NameOrField & 0x01)
2083 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2084 else
2085 return getField()->getIdentifier();
2086}
2087
Mike Stump1eb44332009-09-09 15:08:12 +00002088DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002089 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002090 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002091 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002092 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002093 unsigned NumIndexExprs,
2094 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002095 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002096 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002097 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2098 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002099 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002100
2101 // Record the initializer itself.
2102 child_iterator Child = child_begin();
2103 *Child++ = Init;
2104
2105 // Copy the designators and their subexpressions, computing
2106 // value-dependence along the way.
2107 unsigned IndexIdx = 0;
2108 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002109 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002110
2111 if (this->Designators[I].isArrayDesignator()) {
2112 // Compute type- and value-dependence.
2113 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002114 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002115 Index->isTypeDependent() || Index->isValueDependent();
2116
2117 // Copy the index expressions into permanent storage.
2118 *Child++ = IndexExprs[IndexIdx++];
2119 } else if (this->Designators[I].isArrayRangeDesignator()) {
2120 // Compute type- and value-dependence.
2121 Expr *Start = IndexExprs[IndexIdx];
2122 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002123 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002124 Start->isTypeDependent() || Start->isValueDependent() ||
2125 End->isTypeDependent() || End->isValueDependent();
2126
2127 // Copy the start/end expressions into permanent storage.
2128 *Child++ = IndexExprs[IndexIdx++];
2129 *Child++ = IndexExprs[IndexIdx++];
2130 }
2131 }
2132
2133 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002134}
2135
Douglas Gregor05c13a32009-01-22 00:58:24 +00002136DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002137DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002138 unsigned NumDesignators,
2139 Expr **IndexExprs, unsigned NumIndexExprs,
2140 SourceLocation ColonOrEqualLoc,
2141 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002142 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002143 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00002144 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2145 ColonOrEqualLoc, UsesColonSyntax,
2146 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002147}
2148
Mike Stump1eb44332009-09-09 15:08:12 +00002149DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002150 unsigned NumIndexExprs) {
2151 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2152 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2153 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2154}
2155
Mike Stump1eb44332009-09-09 15:08:12 +00002156void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002157 unsigned NumDesigs) {
2158 if (Designators)
2159 delete [] Designators;
2160
2161 Designators = new Designator[NumDesigs];
2162 NumDesignators = NumDesigs;
2163 for (unsigned I = 0; I != NumDesigs; ++I)
2164 Designators[I] = Desigs[I];
2165}
2166
Douglas Gregor05c13a32009-01-22 00:58:24 +00002167SourceRange DesignatedInitExpr::getSourceRange() const {
2168 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002169 Designator &First =
2170 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002171 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002172 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002173 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2174 else
2175 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2176 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002177 StartLoc =
2178 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002179 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2180}
2181
Douglas Gregor05c13a32009-01-22 00:58:24 +00002182Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2183 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2184 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2185 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002186 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2187 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2188}
2189
2190Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002191 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002192 "Requires array range designator");
2193 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2194 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002195 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2196 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2197}
2198
2199Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002200 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002201 "Requires array range designator");
2202 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2203 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002204 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2205 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2206}
2207
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002208/// \brief Replaces the designator at index @p Idx with the series
2209/// of designators in [First, Last).
Mike Stump1eb44332009-09-09 15:08:12 +00002210void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2211 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002212 const Designator *Last) {
2213 unsigned NumNewDesignators = Last - First;
2214 if (NumNewDesignators == 0) {
2215 std::copy_backward(Designators + Idx + 1,
2216 Designators + NumDesignators,
2217 Designators + Idx);
2218 --NumNewDesignators;
2219 return;
2220 } else if (NumNewDesignators == 1) {
2221 Designators[Idx] = *First;
2222 return;
2223 }
2224
Mike Stump1eb44332009-09-09 15:08:12 +00002225 Designator *NewDesignators
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002226 = new Designator[NumDesignators - 1 + NumNewDesignators];
2227 std::copy(Designators, Designators + Idx, NewDesignators);
2228 std::copy(First, Last, NewDesignators + Idx);
2229 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2230 NewDesignators + Idx + NumNewDesignators);
2231 delete [] Designators;
2232 Designators = NewDesignators;
2233 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2234}
2235
Douglas Gregor42602bb2009-08-07 06:08:38 +00002236void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002237 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00002238 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002239}
2240
Mike Stump1eb44332009-09-09 15:08:12 +00002241ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002242 Expr **exprs, unsigned nexprs,
2243 SourceLocation rparenloc)
2244: Expr(ParenListExprClass, QualType(),
2245 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002246 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002247 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002248
Nate Begeman2ef13e52009-08-10 23:49:36 +00002249 Exprs = new (C) Stmt*[nexprs];
2250 for (unsigned i = 0; i != nexprs; ++i)
2251 Exprs[i] = exprs[i];
2252}
2253
2254void ParenListExpr::DoDestroy(ASTContext& C) {
2255 DestroyChildren(C);
2256 if (Exprs) C.Deallocate(Exprs);
2257 this->~ParenListExpr();
2258 C.Deallocate(this);
2259}
2260
Douglas Gregor05c13a32009-01-22 00:58:24 +00002261//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002262// ExprIterator.
2263//===----------------------------------------------------------------------===//
2264
2265Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2266Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2267Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2268const Expr* ConstExprIterator::operator[](size_t idx) const {
2269 return cast<Expr>(I[idx]);
2270}
2271const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2272const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2273
2274//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002275// Child Iterators for iterating over subexpressions/substatements
2276//===----------------------------------------------------------------------===//
2277
2278// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002279Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2280Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002281
Steve Naroff7779db42007-11-12 14:29:37 +00002282// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002283Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2284Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002285
Steve Naroffe3e9add2008-06-02 23:03:37 +00002286// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002287Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2288Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002289
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002290// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002291Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2292 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002293}
Mike Stump1eb44332009-09-09 15:08:12 +00002294Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2295 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002296}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002297
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002298// ObjCSuperExpr
2299Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2300Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2301
Steve Narofff242b1b2009-07-24 17:54:45 +00002302// ObjCIsaExpr
2303Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2304Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2305
Chris Lattnerd9f69102008-08-10 01:53:14 +00002306// PredefinedExpr
2307Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2308Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002309
2310// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002311Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2312Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002313
2314// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002315Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002316Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002317
2318// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002319Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2320Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002321
Chris Lattner5d661452007-08-26 03:42:43 +00002322// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002323Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2324Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002325
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002326// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002327Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2328Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002329
2330// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002331Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2332Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002333
2334// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002335Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2336Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002337
Sebastian Redl05189992008-11-11 17:56:53 +00002338// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002339Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002340 // If this is of a type and the type is a VLA type (and not a typedef), the
2341 // size expression of the VLA needs to be treated as an executable expression.
2342 // Why isn't this weirdness documented better in StmtIterator?
2343 if (isArgumentType()) {
2344 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2345 getArgumentType().getTypePtr()))
2346 return child_iterator(T);
2347 return child_iterator();
2348 }
Sebastian Redld4575892008-12-03 23:17:54 +00002349 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002350}
Sebastian Redl05189992008-11-11 17:56:53 +00002351Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2352 if (isArgumentType())
2353 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002354 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002355}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002356
2357// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002358Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002359 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002360}
Ted Kremenek1237c672007-08-24 20:06:47 +00002361Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002362 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002363}
2364
2365// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002366Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002367 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002368}
Ted Kremenek1237c672007-08-24 20:06:47 +00002369Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002370 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002371}
Ted Kremenek1237c672007-08-24 20:06:47 +00002372
2373// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002374Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2375Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002376
Nate Begeman213541a2008-04-18 23:10:10 +00002377// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002378Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2379Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002380
2381// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002382Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2383Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002384
Ted Kremenek1237c672007-08-24 20:06:47 +00002385// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002386Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2387Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002388
2389// BinaryOperator
2390Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002391 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002392}
Ted Kremenek1237c672007-08-24 20:06:47 +00002393Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002394 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002395}
2396
2397// ConditionalOperator
2398Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002399 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002400}
Ted Kremenek1237c672007-08-24 20:06:47 +00002401Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002402 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002403}
2404
2405// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002406Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2407Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002408
Ted Kremenek1237c672007-08-24 20:06:47 +00002409// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002410Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2411Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002412
2413// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002414Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2415 return child_iterator();
2416}
2417
2418Stmt::child_iterator TypesCompatibleExpr::child_end() {
2419 return child_iterator();
2420}
Ted Kremenek1237c672007-08-24 20:06:47 +00002421
2422// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002423Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2424Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002425
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002426// GNUNullExpr
2427Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2428Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2429
Eli Friedmand38617c2008-05-14 19:38:39 +00002430// ShuffleVectorExpr
2431Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002432 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002433}
2434Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002435 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002436}
2437
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002438// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002439Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2440Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002441
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002442// InitListExpr
2443Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002444 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002445}
2446Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002447 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002448}
2449
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002450// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002451Stmt::child_iterator DesignatedInitExpr::child_begin() {
2452 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2453 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002454 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2455}
2456Stmt::child_iterator DesignatedInitExpr::child_end() {
2457 return child_iterator(&*child_begin() + NumSubExprs);
2458}
2459
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002460// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002461Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2462 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002463}
2464
Mike Stump1eb44332009-09-09 15:08:12 +00002465Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2466 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002467}
2468
Nate Begeman2ef13e52009-08-10 23:49:36 +00002469// ParenListExpr
2470Stmt::child_iterator ParenListExpr::child_begin() {
2471 return &Exprs[0];
2472}
2473Stmt::child_iterator ParenListExpr::child_end() {
2474 return &Exprs[0]+NumExprs;
2475}
2476
Ted Kremenek1237c672007-08-24 20:06:47 +00002477// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002478Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002479 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002480}
2481Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002482 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002483}
Ted Kremenek1237c672007-08-24 20:06:47 +00002484
2485// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002486Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2487Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002488
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002489// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002490Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002491 return child_iterator();
2492}
2493Stmt::child_iterator ObjCSelectorExpr::child_end() {
2494 return child_iterator();
2495}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002496
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002497// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002498Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2499 return child_iterator();
2500}
2501Stmt::child_iterator ObjCProtocolExpr::child_end() {
2502 return child_iterator();
2503}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002504
Steve Naroff563477d2007-09-18 23:55:05 +00002505// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002506Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002507 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002508}
2509Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002510 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002511}
2512
Steve Naroff4eb206b2008-09-03 18:15:37 +00002513// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002514Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2515Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002516
Ted Kremenek9da13f92008-09-26 23:24:14 +00002517Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2518Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }