blob: cb0165c8df7bc753f82f1514feaba70c57b19a45 [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
Nuno Lopesd20254f2009-12-20 23:11:08 +0000401Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000402 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000403 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000404 return DRE->getDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000405
406 return 0;
407}
408
Nuno Lopesd20254f2009-12-20 23:11:08 +0000409FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000410 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000411}
412
Chris Lattnerd18b3292007-12-28 05:25:02 +0000413/// setNumArgs - This changes the number of arguments present in this call.
414/// Any orphaned expressions are deleted by this, and any new operands are set
415/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000416void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000417 // No change, just return.
418 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattnerd18b3292007-12-28 05:25:02 +0000420 // If shrinking # arguments, just delete the extras and forgot them.
421 if (NumArgs < getNumArgs()) {
422 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000423 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000424 this->NumArgs = NumArgs;
425 return;
426 }
427
428 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000429 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000430 // Copy over args.
431 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
432 NewSubExprs[i] = SubExprs[i];
433 // Null out new args.
434 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
435 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor88c9a462009-04-17 21:46:47 +0000437 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438 SubExprs = NewSubExprs;
439 this->NumArgs = NumArgs;
440}
441
Chris Lattnercb888962008-10-06 05:00:53 +0000442/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
443/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000444unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000445 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000447 // ImplicitCastExpr.
448 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
449 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000450 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000452 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
453 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000454 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Anders Carlssonbcba2012008-01-31 02:13:57 +0000456 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
457 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000458 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000460 if (!FDecl->getIdentifier())
461 return 0;
462
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000463 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000464}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000465
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000466QualType CallExpr::getCallReturnType() const {
467 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000468 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000469 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000470 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000471 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000472
John McCall183700f2009-09-21 23:43:11 +0000473 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000474 return FnType->getResultType();
475}
Chris Lattnercb888962008-10-06 05:00:53 +0000476
Mike Stump1eb44332009-09-09 15:08:12 +0000477MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000478 SourceRange qualrange, ValueDecl *memberdecl,
John McCalld5532b62009-11-23 01:53:49 +0000479 SourceLocation l, const TemplateArgumentListInfo *targs,
480 QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000481 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000482 base->isTypeDependent() || (qual && qual->isDependent()),
483 base->isValueDependent() || (qual && qual->isDependent())),
484 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCalld5532b62009-11-23 01:53:49 +0000485 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000486 // Initialize the qualifier, if any.
487 if (HasQualifier) {
488 NameQualifier *NQ = getMemberQualifier();
489 NQ->NNS = qual;
490 NQ->Range = qualrange;
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000493 // Initialize the explicit template argument list, if any.
John McCalld5532b62009-11-23 01:53:49 +0000494 if (targs)
495 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000496}
497
Mike Stump1eb44332009-09-09 15:08:12 +0000498MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
499 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000500 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000501 ValueDecl *memberdecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000502 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000503 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000504 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000505 std::size_t Size = sizeof(MemberExpr);
506 if (qual != 0)
507 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
John McCalld5532b62009-11-23 01:53:49 +0000509 if (targs)
510 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000512 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000513 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCalld5532b62009-11-23 01:53:49 +0000514 targs, ty);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000515}
516
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000517const char *CastExpr::getCastKindName() const {
518 switch (getCastKind()) {
519 case CastExpr::CK_Unknown:
520 return "Unknown";
521 case CastExpr::CK_BitCast:
522 return "BitCast";
523 case CastExpr::CK_NoOp:
524 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000525 case CastExpr::CK_BaseToDerived:
526 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000527 case CastExpr::CK_DerivedToBase:
528 return "DerivedToBase";
529 case CastExpr::CK_Dynamic:
530 return "Dynamic";
531 case CastExpr::CK_ToUnion:
532 return "ToUnion";
533 case CastExpr::CK_ArrayToPointerDecay:
534 return "ArrayToPointerDecay";
535 case CastExpr::CK_FunctionToPointerDecay:
536 return "FunctionToPointerDecay";
537 case CastExpr::CK_NullToMemberPointer:
538 return "NullToMemberPointer";
539 case CastExpr::CK_BaseToDerivedMemberPointer:
540 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000541 case CastExpr::CK_DerivedToBaseMemberPointer:
542 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000543 case CastExpr::CK_UserDefinedConversion:
544 return "UserDefinedConversion";
545 case CastExpr::CK_ConstructorConversion:
546 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000547 case CastExpr::CK_IntegralToPointer:
548 return "IntegralToPointer";
549 case CastExpr::CK_PointerToIntegral:
550 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000551 case CastExpr::CK_ToVoid:
552 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000553 case CastExpr::CK_VectorSplat:
554 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000555 case CastExpr::CK_IntegralCast:
556 return "IntegralCast";
557 case CastExpr::CK_IntegralToFloating:
558 return "IntegralToFloating";
559 case CastExpr::CK_FloatingToIntegral:
560 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000561 case CastExpr::CK_FloatingCast:
562 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000563 case CastExpr::CK_MemberPointerToBoolean:
564 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000565 case CastExpr::CK_AnyPointerToObjCPointerCast:
566 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000567 case CastExpr::CK_AnyPointerToBlockPointerCast:
568 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000571 assert(0 && "Unhandled cast kind!");
572 return 0;
573}
574
Douglas Gregor6eef5192009-12-14 19:27:10 +0000575Expr *CastExpr::getSubExprAsWritten() {
576 Expr *SubExpr = 0;
577 CastExpr *E = this;
578 do {
579 SubExpr = E->getSubExpr();
580
581 // Skip any temporary bindings; they're implicit.
582 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
583 SubExpr = Binder->getSubExpr();
584
585 // Conversions by constructor and conversion functions have a
586 // subexpression describing the call; strip it off.
587 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
588 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
589 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
590 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
591
592 // If the subexpression we're left with is an implicit cast, look
593 // through that, too.
594 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
595
596 return SubExpr;
597}
598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
600/// corresponds to, e.g. "<<=".
601const char *BinaryOperator::getOpcodeStr(Opcode Op) {
602 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000603 case PtrMemD: return ".*";
604 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 case Mul: return "*";
606 case Div: return "/";
607 case Rem: return "%";
608 case Add: return "+";
609 case Sub: return "-";
610 case Shl: return "<<";
611 case Shr: return ">>";
612 case LT: return "<";
613 case GT: return ">";
614 case LE: return "<=";
615 case GE: return ">=";
616 case EQ: return "==";
617 case NE: return "!=";
618 case And: return "&";
619 case Xor: return "^";
620 case Or: return "|";
621 case LAnd: return "&&";
622 case LOr: return "||";
623 case Assign: return "=";
624 case MulAssign: return "*=";
625 case DivAssign: return "/=";
626 case RemAssign: return "%=";
627 case AddAssign: return "+=";
628 case SubAssign: return "-=";
629 case ShlAssign: return "<<=";
630 case ShrAssign: return ">>=";
631 case AndAssign: return "&=";
632 case XorAssign: return "^=";
633 case OrAssign: return "|=";
634 case Comma: return ",";
635 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000636
637 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000638}
639
Mike Stump1eb44332009-09-09 15:08:12 +0000640BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000641BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
642 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000643 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000644 case OO_Plus: return Add;
645 case OO_Minus: return Sub;
646 case OO_Star: return Mul;
647 case OO_Slash: return Div;
648 case OO_Percent: return Rem;
649 case OO_Caret: return Xor;
650 case OO_Amp: return And;
651 case OO_Pipe: return Or;
652 case OO_Equal: return Assign;
653 case OO_Less: return LT;
654 case OO_Greater: return GT;
655 case OO_PlusEqual: return AddAssign;
656 case OO_MinusEqual: return SubAssign;
657 case OO_StarEqual: return MulAssign;
658 case OO_SlashEqual: return DivAssign;
659 case OO_PercentEqual: return RemAssign;
660 case OO_CaretEqual: return XorAssign;
661 case OO_AmpEqual: return AndAssign;
662 case OO_PipeEqual: return OrAssign;
663 case OO_LessLess: return Shl;
664 case OO_GreaterGreater: return Shr;
665 case OO_LessLessEqual: return ShlAssign;
666 case OO_GreaterGreaterEqual: return ShrAssign;
667 case OO_EqualEqual: return EQ;
668 case OO_ExclaimEqual: return NE;
669 case OO_LessEqual: return LE;
670 case OO_GreaterEqual: return GE;
671 case OO_AmpAmp: return LAnd;
672 case OO_PipePipe: return LOr;
673 case OO_Comma: return Comma;
674 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000675 }
676}
677
678OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
679 static const OverloadedOperatorKind OverOps[] = {
680 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
681 OO_Star, OO_Slash, OO_Percent,
682 OO_Plus, OO_Minus,
683 OO_LessLess, OO_GreaterGreater,
684 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
685 OO_EqualEqual, OO_ExclaimEqual,
686 OO_Amp,
687 OO_Caret,
688 OO_Pipe,
689 OO_AmpAmp,
690 OO_PipePipe,
691 OO_Equal, OO_StarEqual,
692 OO_SlashEqual, OO_PercentEqual,
693 OO_PlusEqual, OO_MinusEqual,
694 OO_LessLessEqual, OO_GreaterGreaterEqual,
695 OO_AmpEqual, OO_CaretEqual,
696 OO_PipeEqual,
697 OO_Comma
698 };
699 return OverOps[Opc];
700}
701
Mike Stump1eb44332009-09-09 15:08:12 +0000702InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000703 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000704 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000705 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000706 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor73460a32009-11-19 23:25:22 +0000707 UnionFieldInit(0), HadArrayRangeDesignator(false)
708{
709 for (unsigned I = 0; I != numInits; ++I) {
710 if (initExprs[I]->isTypeDependent())
711 TypeDependent = true;
712 if (initExprs[I]->isValueDependent())
713 ValueDependent = true;
714 }
715
Chris Lattner418f6c72008-10-26 23:43:26 +0000716 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000717}
Reid Spencer5f016e22007-07-11 17:01:13 +0000718
Douglas Gregorfa219202009-03-20 23:58:33 +0000719void InitListExpr::reserveInits(unsigned NumInits) {
720 if (NumInits > InitExprs.size())
721 InitExprs.reserve(NumInits);
722}
723
Douglas Gregor4c678342009-01-28 21:54:33 +0000724void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000725 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000726 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000727 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000728 InitExprs.resize(NumInits, 0);
729}
730
731Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
732 if (Init >= InitExprs.size()) {
733 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
734 InitExprs.back() = expr;
735 return 0;
736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregor4c678342009-01-28 21:54:33 +0000738 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
739 InitExprs[Init] = expr;
740 return Result;
741}
742
Steve Naroffbfdcae62008-09-04 15:31:07 +0000743/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000744///
745const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000746 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000747 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000748}
749
Mike Stump1eb44332009-09-09 15:08:12 +0000750SourceLocation BlockExpr::getCaretLocation() const {
751 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000752}
Mike Stump1eb44332009-09-09 15:08:12 +0000753const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000754 return TheBlock->getBody();
755}
Mike Stump1eb44332009-09-09 15:08:12 +0000756Stmt *BlockExpr::getBody() {
757 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000758}
Steve Naroff56ee6892008-10-08 17:01:13 +0000759
760
Reid Spencer5f016e22007-07-11 17:01:13 +0000761//===----------------------------------------------------------------------===//
762// Generic Expression Routines
763//===----------------------------------------------------------------------===//
764
Chris Lattner026dc962009-02-14 07:37:35 +0000765/// isUnusedResultAWarning - Return true if this immediate expression should
766/// be warned about if the result is unused. If so, fill in Loc and Ranges
767/// with location to warn on and the source range[s] to report with the
768/// warning.
769bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000770 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000771 // Don't warn if the expr is type dependent. The type could end up
772 // instantiating to void.
773 if (isTypeDependent())
774 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 switch (getStmtClass()) {
777 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000778 Loc = getExprLoc();
779 R1 = getSourceRange();
780 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000782 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000783 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 case UnaryOperatorClass: {
785 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000788 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 case UnaryOperator::PostInc:
790 case UnaryOperator::PostDec:
791 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000792 case UnaryOperator::PreDec: // ++/--
793 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000794 case UnaryOperator::Deref:
795 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000796 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000797 return false;
798 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 case UnaryOperator::Real:
800 case UnaryOperator::Imag:
801 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000802 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
803 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000804 return false;
805 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000807 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 }
Chris Lattner026dc962009-02-14 07:37:35 +0000809 Loc = UO->getOperatorLoc();
810 R1 = UO->getSubExpr()->getSourceRange();
811 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000813 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000814 const BinaryOperator *BO = cast<BinaryOperator>(this);
815 // Consider comma to have side effects if the LHS or RHS does.
816 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000817 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
818 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Chris Lattner026dc962009-02-14 07:37:35 +0000820 if (BO->isAssignmentOp())
821 return false;
822 Loc = BO->getOperatorLoc();
823 R1 = BO->getLHS()->getSourceRange();
824 R2 = BO->getRHS()->getSourceRange();
825 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000826 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000827 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000828 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000830 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000831 // The condition must be evaluated, but if either the LHS or RHS is a
832 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000833 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000834 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000835 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000836 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000837 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000838 }
839
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000841 // If the base pointer or element is to a volatile pointer/field, accessing
842 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000843 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000844 return false;
845 Loc = cast<MemberExpr>(this)->getMemberLoc();
846 R1 = SourceRange(Loc, Loc);
847 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
848 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 case ArraySubscriptExprClass:
851 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000852 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000853 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000854 return false;
855 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
856 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
857 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
858 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000861 case CXXOperatorCallExprClass:
862 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000863 // If this is a direct call, get the callee.
864 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000865 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000866 // If the callee has attribute pure, const, or warn_unused_result, warn
867 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000868 //
869 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
870 // updated to match for QoI.
871 if (FD->getAttr<WarnUnusedResultAttr>() ||
872 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
873 Loc = CE->getCallee()->getLocStart();
874 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000876 if (unsigned NumArgs = CE->getNumArgs())
877 R2 = SourceRange(CE->getArg(0)->getLocStart(),
878 CE->getArg(NumArgs-1)->getLocEnd());
879 return true;
880 }
Chris Lattner026dc962009-02-14 07:37:35 +0000881 }
882 return false;
883 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000884
885 case CXXTemporaryObjectExprClass:
886 case CXXConstructExprClass:
887 return false;
888
Chris Lattnera9c01022007-09-26 22:06:30 +0000889 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000890 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000892 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000893#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000894 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000895 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000896 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000897 Loc = Ref->getLocation();
898 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
899 if (Ref->getBase())
900 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000901#else
902 Loc = getExprLoc();
903 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000904#endif
905 return true;
906 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000907 case StmtExprClass: {
908 // Statement exprs don't logically have side effects themselves, but are
909 // sometimes used in macros in ways that give them a type that is unused.
910 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
911 // however, if the result of the stmt expr is dead, we don't want to emit a
912 // warning.
913 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
914 if (!CS->body_empty())
915 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000916 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattner026dc962009-02-14 07:37:35 +0000918 Loc = cast<StmtExpr>(this)->getLParenLoc();
919 R1 = getSourceRange();
920 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000921 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000922 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000923 // If this is an explicit cast to void, allow it. People do this when they
924 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000925 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000926 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000927 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
928 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
929 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000930 case CXXFunctionalCastExprClass: {
931 const CastExpr *CE = cast<CastExpr>(this);
932
933 // If this is a cast to void or a constructor conversion, check the operand.
934 // Otherwise, the result of the cast is unused.
935 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
936 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000937 return (cast<CastExpr>(this)->getSubExpr()
938 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000939 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
940 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
941 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Eli Friedman4be1f472008-05-19 21:24:43 +0000944 case ImplicitCastExprClass:
945 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000946 return (cast<ImplicitCastExpr>(this)
947 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000948
Chris Lattner04421082008-04-08 04:40:51 +0000949 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000950 return (cast<CXXDefaultArgExpr>(this)
951 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000952
953 case CXXNewExprClass:
954 // FIXME: In theory, there might be new expressions that don't have side
955 // effects (e.g. a placement new with an uninitialized POD).
956 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000957 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000958 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000959 return (cast<CXXBindTemporaryExpr>(this)
960 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000961 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000962 return (cast<CXXExprWithTemporaries>(this)
963 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000964 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000965}
966
Douglas Gregorba7e2102008-10-22 15:04:37 +0000967/// DeclCanBeLvalue - Determine whether the given declaration can be
968/// an lvalue. This is a helper routine for isLvalue.
969static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000970 // C++ [temp.param]p6:
971 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000972 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000973 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
974 return NTTParm->getType()->isReferenceType();
975
Douglas Gregor44b43212008-12-11 16:49:14 +0000976 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000977 // C++ 3.10p2: An lvalue refers to an object or function.
978 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +0000979 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000980}
981
Reid Spencer5f016e22007-07-11 17:01:13 +0000982/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
983/// incomplete type other than void. Nonarray expressions that can be lvalues:
984/// - name, where name must be a variable
985/// - e[i]
986/// - (e), where e must be an lvalue
987/// - e.name, where e must be an lvalue
988/// - e->name
989/// - *e, the type of e cannot be a function type
990/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000991/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000992/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000993///
Chris Lattner28be73f2008-07-26 21:30:36 +0000994Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +0000995 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
996
997 isLvalueResult Res = isLvalueInternal(Ctx);
998 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
999 return Res;
1000
Douglas Gregor98cd5992008-10-21 23:43:52 +00001001 // first, check the type (C99 6.3.2.1). Expressions with function
1002 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001003 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 return LV_NotObjectType;
1005
Steve Naroffacb818a2008-02-10 01:39:04 +00001006 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001007 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001008 return LV_IncompleteVoidType;
1009
Eli Friedman53202852009-05-03 22:36:05 +00001010 return LV_Valid;
1011}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001012
Eli Friedman53202852009-05-03 22:36:05 +00001013// Check whether the expression can be sanely treated like an l-value
1014Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001016 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001017 case StringLiteralClass: // C99 6.5.1p4
1018 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001019 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001020 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1021 // For vectors, make sure base is an lvalue (i.e. not a function call).
1022 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001023 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001025 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001026 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1027 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 return LV_Valid;
1029 break;
Chris Lattner41110242008-06-17 18:05:57 +00001030 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001031 case BlockDeclRefExprClass: {
1032 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001033 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001034 return LV_Valid;
1035 break;
1036 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001037 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001039 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1040 NamedDecl *Member = m->getMemberDecl();
1041 // C++ [expr.ref]p4:
1042 // If E2 is declared to have type "reference to T", then E1.E2
1043 // is an lvalue.
1044 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1045 if (Value->getType()->isReferenceType())
1046 return LV_Valid;
1047
1048 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001049 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001050 return LV_Valid;
1051
1052 // -- If E2 is a non-static data member [...]. If E1 is an
1053 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001054 if (isa<FieldDecl>(Member)) {
1055 if (m->isArrow())
1056 return LV_Valid;
1057 Expr *BaseExp = m->getBase();
1058 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1059 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
1060 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001061
1062 // -- If it refers to a static member function [...], then
1063 // E1.E2 is an lvalue.
1064 // -- Otherwise, if E1.E2 refers to a non-static member
1065 // function [...], then E1.E2 is not an lvalue.
1066 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1067 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1068
1069 // -- If E2 is a member enumerator [...], the expression E1.E2
1070 // is not an lvalue.
1071 if (isa<EnumConstantDecl>(Member))
1072 return LV_InvalidExpression;
1073
1074 // Not an lvalue.
1075 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001076 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001077
Douglas Gregor86f19402008-12-20 23:49:58 +00001078 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001079 if (m->isArrow())
1080 return LV_Valid;
1081 Expr *BaseExp = m->getBase();
1082 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1083 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001084 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001085 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001087 return LV_Valid; // C99 6.5.3p4
1088
1089 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001090 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1091 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001092 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001093
1094 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1095 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1096 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1097 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001099 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001100 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001101 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001103 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001104 case BinaryOperatorClass:
1105 case CompoundAssignOperatorClass: {
1106 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001107
1108 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1109 BinOp->getOpcode() == BinaryOperator::Comma)
1110 return BinOp->getRHS()->isLvalue(Ctx);
1111
Sebastian Redl22460502009-02-07 00:15:38 +00001112 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001113 // The result of a .* expression is an lvalue only if its first operand is
1114 // an lvalue and its second operand is a pointer to data member.
1115 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001116 !BinOp->getType()->isFunctionType())
1117 return BinOp->getLHS()->isLvalue(Ctx);
1118
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001119 // The result of an ->* expression is an lvalue only if its second operand
1120 // is a pointer to data member.
1121 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1122 !BinOp->getType()->isFunctionType()) {
1123 QualType Ty = BinOp->getRHS()->getType();
1124 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1125 return LV_Valid;
1126 }
1127
Douglas Gregorbf3af052008-11-13 20:12:29 +00001128 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001129 return LV_InvalidExpression;
1130
Douglas Gregorbf3af052008-11-13 20:12:29 +00001131 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001132 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001133 // The result of an assignment operation [...] is an lvalue.
1134 return LV_Valid;
1135
1136
1137 // C99 6.5.16:
1138 // An assignment expression [...] is not an lvalue.
1139 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001142 case CXXOperatorCallExprClass:
1143 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001144 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001145 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001146 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001147 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1148 if (ReturnType->isLValueReferenceType())
1149 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001150
Douglas Gregor9d293df2008-10-28 00:22:11 +00001151 break;
1152 }
Steve Naroffe6386392007-12-05 04:00:10 +00001153 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1154 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001155 case ChooseExprClass:
1156 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001157 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001158 case ExtVectorElementExprClass:
1159 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001160 return LV_DuplicateVectorComponents;
1161 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001162 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1163 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001164 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1165 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001166 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001167 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001168 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001169 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001170 case UnresolvedLookupExprClass:
1171 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001172 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001173 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001174 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001175 case CXXFunctionalCastExprClass:
1176 case CXXStaticCastExprClass:
1177 case CXXDynamicCastExprClass:
1178 case CXXReinterpretCastExprClass:
1179 case CXXConstCastExprClass:
1180 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001181 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001182 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1183 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001184 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1185 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001186 return LV_Valid;
1187 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001188 case CXXTypeidExprClass:
1189 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1190 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001191 case CXXBindTemporaryExprClass:
1192 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1193 isLvalueInternal(Ctx);
Sebastian Redl76458502009-04-17 16:30:52 +00001194 case ConditionalOperatorClass: {
1195 // Complicated handling is only for C++.
1196 if (!Ctx.getLangOptions().CPlusPlus)
1197 return LV_InvalidExpression;
1198
1199 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1200 // everywhere there's an object converted to an rvalue. Also, any other
1201 // casts should be wrapped by ImplicitCastExprs. There's just the special
1202 // case involving throws to work out.
1203 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001204 Expr *True = Cond->getTrueExpr();
1205 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001206 // C++0x 5.16p2
1207 // If either the second or the third operand has type (cv) void, [...]
1208 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001209 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001210 return LV_InvalidExpression;
1211
1212 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001213 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001214 return LV_InvalidExpression;
1215
1216 // That's it.
1217 return LV_Valid;
1218 }
1219
Douglas Gregor2d48e782009-12-19 07:07:47 +00001220 case Expr::CXXExprWithTemporariesClass:
1221 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1222
1223 case Expr::ObjCMessageExprClass:
1224 if (const ObjCMethodDecl *Method
1225 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1226 if (Method->getResultType()->isLValueReferenceType())
1227 return LV_Valid;
1228 break;
1229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 default:
1231 break;
1232 }
1233 return LV_InvalidExpression;
1234}
1235
1236/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1237/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001238/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001239/// recursively, any member or element of all contained aggregates or unions)
1240/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001241Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001242Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001243 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001246 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001247 // C++ 3.10p11: Functions cannot be modified, but pointers to
1248 // functions can be modifiable.
1249 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1250 return MLV_NotObjectType;
1251 break;
1252
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 case LV_NotObjectType: return MLV_NotObjectType;
1254 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001255 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001256 case LV_InvalidExpression:
1257 // If the top level is a C-style cast, and the subexpression is a valid
1258 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1259 // GCC extension. We don't support it, but we want to produce good
1260 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001261 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1262 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1263 if (Loc)
1264 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001265 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001266 }
1267 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001268 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001269 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001270 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001272
1273 // The following is illegal:
1274 // void takeclosure(void (^C)(void));
1275 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1276 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001277 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001278 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1279 return MLV_NotBlockQualified;
1280 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001281
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001282 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001283 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001284 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1285 if (Expr->getSetterMethod() == 0)
1286 return MLV_NoSetterProperty;
1287 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001288
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001289 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001291 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001293 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001295 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Ted Kremenek6217b802009-07-29 21:53:49 +00001298 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001299 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 return MLV_ConstQualified;
1301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Mike Stump1eb44332009-09-09 15:08:12 +00001303 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001304}
1305
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001306/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001307/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001308bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001309 switch (getStmtClass()) {
1310 default:
1311 return false;
1312 case ObjCIvarRefExprClass:
1313 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001314 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001315 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001316 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001317 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001318 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001319 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001320 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001321 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001322 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001323 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001324 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1325 if (VD->hasGlobalStorage())
1326 return true;
1327 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001328 // dereferencing to a pointer is always a gc'able candidate,
1329 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001330 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001331 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001332 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001333 return false;
1334 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001335 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001336 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001337 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001338 }
1339 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001340 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001341 }
1342}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001343Expr* Expr::IgnoreParens() {
1344 Expr* E = this;
1345 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1346 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001348 return E;
1349}
1350
Chris Lattner56f34942008-02-13 01:02:39 +00001351/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1352/// or CastExprs or ImplicitCastExprs, returning their operand.
1353Expr *Expr::IgnoreParenCasts() {
1354 Expr *E = this;
1355 while (true) {
1356 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1357 E = P->getSubExpr();
1358 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1359 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001360 else
1361 return E;
1362 }
1363}
1364
Chris Lattnerecdd8412009-03-13 17:28:01 +00001365/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1366/// value (including ptr->int casts of the same size). Strip off any
1367/// ParenExpr or CastExprs, returning their operand.
1368Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1369 Expr *E = this;
1370 while (true) {
1371 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1372 E = P->getSubExpr();
1373 continue;
1374 }
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Chris Lattnerecdd8412009-03-13 17:28:01 +00001376 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1377 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1378 // ptr<->int casts of the same width. We also ignore all identify casts.
1379 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Chris Lattnerecdd8412009-03-13 17:28:01 +00001381 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1382 E = SE;
1383 continue;
1384 }
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chris Lattnerecdd8412009-03-13 17:28:01 +00001386 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1387 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1388 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1389 E = SE;
1390 continue;
1391 }
1392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Chris Lattnerecdd8412009-03-13 17:28:01 +00001394 return E;
1395 }
1396}
1397
Douglas Gregor6eef5192009-12-14 19:27:10 +00001398bool Expr::isDefaultArgument() const {
1399 const Expr *E = this;
1400 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1401 E = ICE->getSubExprAsWritten();
1402
1403 return isa<CXXDefaultArgExpr>(E);
1404}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001405
Douglas Gregor898574e2008-12-05 23:32:09 +00001406/// hasAnyTypeDependentArguments - Determines if any of the expressions
1407/// in Exprs is type-dependent.
1408bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1409 for (unsigned I = 0; I < NumExprs; ++I)
1410 if (Exprs[I]->isTypeDependent())
1411 return true;
1412
1413 return false;
1414}
1415
1416/// hasAnyValueDependentArguments - Determines if any of the expressions
1417/// in Exprs is value-dependent.
1418bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1419 for (unsigned I = 0; I < NumExprs; ++I)
1420 if (Exprs[I]->isValueDependent())
1421 return true;
1422
1423 return false;
1424}
1425
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001426bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001427 // This function is attempting whether an expression is an initializer
1428 // which can be evaluated at compile-time. isEvaluatable handles most
1429 // of the cases, but it can't deal with some initializer-specific
1430 // expressions, and it can't deal with aggregates; we deal with those here,
1431 // and fall back to isEvaluatable for the other cases.
1432
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001433 // FIXME: This function assumes the variable being assigned to
1434 // isn't a reference type!
1435
Anders Carlssone8a32b82008-11-24 05:23:59 +00001436 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001437 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001438 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001439 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001440 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001441 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001442 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001443 // This handles gcc's extension that allows global initializers like
1444 // "struct x {int x;} x = (struct x) {};".
1445 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001446 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001447 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001448 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001449 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001450 // FIXME: This doesn't deal with fields with reference types correctly.
1451 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1452 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001453 const InitListExpr *Exp = cast<InitListExpr>(this);
1454 unsigned numInits = Exp->getNumInits();
1455 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001456 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001457 return false;
1458 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001459 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001460 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001461 case ImplicitValueInitExprClass:
1462 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001463 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001464 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001465 case UnaryOperatorClass: {
1466 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1467 if (Exp->getOpcode() == UnaryOperator::Extension)
1468 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1469 break;
1470 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001471 case BinaryOperatorClass: {
1472 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1473 // but this handles the common case.
1474 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1475 if (Exp->getOpcode() == BinaryOperator::Sub &&
1476 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1477 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1478 return true;
1479 break;
1480 }
Chris Lattner81045d82009-04-21 05:19:11 +00001481 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001482 case CStyleCastExprClass:
1483 // Handle casts with a destination that's a struct or union; this
1484 // deals with both the gcc no-op struct cast extension and the
1485 // cast-to-union extension.
1486 if (getType()->isRecordType())
1487 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001488
1489 // Integer->integer casts can be handled here, which is important for
1490 // things like (int)(&&x-&&y). Scary but true.
1491 if (getType()->isIntegerType() &&
1492 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1493 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1494
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001495 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001496 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001497 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001498}
1499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001501/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001502
1503/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1504/// comma, etc
1505///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001506/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1507/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1508/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001509
Eli Friedmane28d7192009-02-26 09:29:13 +00001510// CheckICE - This function does the fundamental ICE checking: the returned
1511// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1512// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001513// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001514// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001515// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001516//
1517// Meanings of Val:
1518// 0: This expression is an ICE if it can be evaluated by Evaluate.
1519// 1: This expression is not an ICE, but if it isn't evaluated, it's
1520// a legal subexpression for an ICE. This return value is used to handle
1521// the comma operator in C99 mode.
1522// 2: This expression is not an ICE, and is not a legal subexpression for one.
1523
1524struct ICEDiag {
1525 unsigned Val;
1526 SourceLocation Loc;
1527
1528 public:
1529 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1530 ICEDiag() : Val(0) {}
1531};
1532
1533ICEDiag NoDiag() { return ICEDiag(); }
1534
Eli Friedman60ce9632009-02-27 04:07:58 +00001535static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1536 Expr::EvalResult EVResult;
1537 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1538 !EVResult.Val.isInt()) {
1539 return ICEDiag(2, E->getLocStart());
1540 }
1541 return NoDiag();
1542}
1543
Eli Friedmane28d7192009-02-26 09:29:13 +00001544static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001545 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001546 if (!E->getType()->isIntegralType()) {
1547 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001548 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001549
1550 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001551#define STMT(Node, Base) case Expr::Node##Class:
1552#define EXPR(Node, Base)
1553#include "clang/AST/StmtNodes.def"
1554 case Expr::PredefinedExprClass:
1555 case Expr::FloatingLiteralClass:
1556 case Expr::ImaginaryLiteralClass:
1557 case Expr::StringLiteralClass:
1558 case Expr::ArraySubscriptExprClass:
1559 case Expr::MemberExprClass:
1560 case Expr::CompoundAssignOperatorClass:
1561 case Expr::CompoundLiteralExprClass:
1562 case Expr::ExtVectorElementExprClass:
1563 case Expr::InitListExprClass:
1564 case Expr::DesignatedInitExprClass:
1565 case Expr::ImplicitValueInitExprClass:
1566 case Expr::ParenListExprClass:
1567 case Expr::VAArgExprClass:
1568 case Expr::AddrLabelExprClass:
1569 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001570 case Expr::CXXMemberCallExprClass:
1571 case Expr::CXXDynamicCastExprClass:
1572 case Expr::CXXTypeidExprClass:
1573 case Expr::CXXNullPtrLiteralExprClass:
1574 case Expr::CXXThisExprClass:
1575 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001576 case Expr::CXXNewExprClass:
1577 case Expr::CXXDeleteExprClass:
1578 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001579 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001580 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001581 case Expr::CXXConstructExprClass:
1582 case Expr::CXXBindTemporaryExprClass:
1583 case Expr::CXXExprWithTemporariesClass:
1584 case Expr::CXXTemporaryObjectExprClass:
1585 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001586 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001587 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001588 case Expr::ObjCStringLiteralClass:
1589 case Expr::ObjCEncodeExprClass:
1590 case Expr::ObjCMessageExprClass:
1591 case Expr::ObjCSelectorExprClass:
1592 case Expr::ObjCProtocolExprClass:
1593 case Expr::ObjCIvarRefExprClass:
1594 case Expr::ObjCPropertyRefExprClass:
1595 case Expr::ObjCImplicitSetterGetterRefExprClass:
1596 case Expr::ObjCSuperExprClass:
1597 case Expr::ObjCIsaExprClass:
1598 case Expr::ShuffleVectorExprClass:
1599 case Expr::BlockExprClass:
1600 case Expr::BlockDeclRefExprClass:
1601 case Expr::NoStmtClass:
1602 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001603 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001604
Douglas Gregor043cad22009-09-11 00:18:58 +00001605 case Expr::GNUNullExprClass:
1606 // GCC considers the GNU __null value to be an integral constant expression.
1607 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001608
Eli Friedmane28d7192009-02-26 09:29:13 +00001609 case Expr::ParenExprClass:
1610 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1611 case Expr::IntegerLiteralClass:
1612 case Expr::CharacterLiteralClass:
1613 case Expr::CXXBoolLiteralExprClass:
1614 case Expr::CXXZeroInitValueExprClass:
1615 case Expr::TypesCompatibleExprClass:
1616 case Expr::UnaryTypeTraitExprClass:
1617 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001618 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001619 case Expr::CXXOperatorCallExprClass: {
1620 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001621 if (CE->isBuiltinCall(Ctx))
1622 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001623 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001624 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001625 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001626 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1627 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001628 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001629 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001630 // C++ 7.1.5.1p2
1631 // A variable of non-volatile const-qualified integral or enumeration
1632 // type initialized by an ICE can be used in ICEs.
1633 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001634 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001635 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1636 if (Quals.hasVolatile() || !Quals.hasConst())
1637 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1638
1639 // Look for the definition of this variable, which will actually have
1640 // an initializer.
1641 const VarDecl *Def = 0;
1642 const Expr *Init = Dcl->getDefinition(Def);
1643 if (Init) {
1644 if (Def->isInitKnownICE()) {
1645 // We have already checked whether this subexpression is an
1646 // integral constant expression.
1647 if (Def->isInitICE())
1648 return NoDiag();
1649 else
1650 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1651 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001652
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001653 // C++ [class.static.data]p4:
1654 // If a static data member is of const integral or const
1655 // enumeration type, its declaration in the class definition can
1656 // specify a constant-initializer which shall be an integral
1657 // constant expression (5.19). In that case, the member can appear
1658 // in integral constant expressions.
1659 if (Def->isOutOfLine()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001660 Dcl->setInitKnownICE(false);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001661 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1662 }
Eli Friedmanc0131182009-12-03 20:31:57 +00001663
1664 if (Dcl->isCheckingICE()) {
1665 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1666 }
1667
1668 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001669 ICEDiag Result = CheckICE(Init, Ctx);
1670 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001671 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001672 return Result;
1673 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001674 }
1675 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001676 return ICEDiag(2, E->getLocStart());
1677 case Expr::UnaryOperatorClass: {
1678 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001680 case UnaryOperator::PostInc:
1681 case UnaryOperator::PostDec:
1682 case UnaryOperator::PreInc:
1683 case UnaryOperator::PreDec:
1684 case UnaryOperator::AddrOf:
1685 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001686 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001687
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001689 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001691 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001693 case UnaryOperator::Real:
1694 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001695 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001696 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001697 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1698 // Evaluate matches the proposed gcc behavior for cases like
1699 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1700 // compliance: we should warn earlier for offsetof expressions with
1701 // array subscripts that aren't ICEs, and if the array subscripts
1702 // are ICEs, the value of the offsetof must be an integer constant.
1703 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001706 case Expr::SizeOfAlignOfExprClass: {
1707 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1708 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1709 return ICEDiag(2, E->getLocStart());
1710 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001712 case Expr::BinaryOperatorClass: {
1713 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001715 case BinaryOperator::PtrMemD:
1716 case BinaryOperator::PtrMemI:
1717 case BinaryOperator::Assign:
1718 case BinaryOperator::MulAssign:
1719 case BinaryOperator::DivAssign:
1720 case BinaryOperator::RemAssign:
1721 case BinaryOperator::AddAssign:
1722 case BinaryOperator::SubAssign:
1723 case BinaryOperator::ShlAssign:
1724 case BinaryOperator::ShrAssign:
1725 case BinaryOperator::AndAssign:
1726 case BinaryOperator::XorAssign:
1727 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001728 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001729
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001733 case BinaryOperator::Add:
1734 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001736 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001737 case BinaryOperator::LT:
1738 case BinaryOperator::GT:
1739 case BinaryOperator::LE:
1740 case BinaryOperator::GE:
1741 case BinaryOperator::EQ:
1742 case BinaryOperator::NE:
1743 case BinaryOperator::And:
1744 case BinaryOperator::Xor:
1745 case BinaryOperator::Or:
1746 case BinaryOperator::Comma: {
1747 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1748 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001749 if (Exp->getOpcode() == BinaryOperator::Div ||
1750 Exp->getOpcode() == BinaryOperator::Rem) {
1751 // Evaluate gives an error for undefined Div/Rem, so make sure
1752 // we don't evaluate one.
1753 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1754 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1755 if (REval == 0)
1756 return ICEDiag(1, E->getLocStart());
1757 if (REval.isSigned() && REval.isAllOnesValue()) {
1758 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1759 if (LEval.isMinSignedValue())
1760 return ICEDiag(1, E->getLocStart());
1761 }
1762 }
1763 }
1764 if (Exp->getOpcode() == BinaryOperator::Comma) {
1765 if (Ctx.getLangOptions().C99) {
1766 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1767 // if it isn't evaluated.
1768 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1769 return ICEDiag(1, E->getLocStart());
1770 } else {
1771 // In both C89 and C++, commas in ICEs are illegal.
1772 return ICEDiag(2, E->getLocStart());
1773 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001774 }
1775 if (LHSResult.Val >= RHSResult.Val)
1776 return LHSResult;
1777 return RHSResult;
1778 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001780 case BinaryOperator::LOr: {
1781 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1782 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1783 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1784 // Rare case where the RHS has a comma "side-effect"; we need
1785 // to actually check the condition to see whether the side
1786 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001787 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001788 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001789 return RHSResult;
1790 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001791 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001792
Eli Friedmane28d7192009-02-26 09:29:13 +00001793 if (LHSResult.Val >= RHSResult.Val)
1794 return LHSResult;
1795 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001797 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001799 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001800 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001801 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001802 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001803 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001804 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001805 case Expr::CXXStaticCastExprClass:
1806 case Expr::CXXReinterpretCastExprClass:
1807 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001808 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1809 if (SubExpr->getType()->isIntegralType())
1810 return CheckICE(SubExpr, Ctx);
1811 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1812 return NoDiag();
1813 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001815 case Expr::ConditionalOperatorClass: {
1816 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001817 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001818 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001819 // expression, and it is fully evaluated. This is an important GNU
1820 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001821 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001822 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001823 Expr::EvalResult EVResult;
1824 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1825 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001826 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001827 }
1828 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001829 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001830 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1831 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1832 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1833 if (CondResult.Val == 2)
1834 return CondResult;
1835 if (TrueResult.Val == 2)
1836 return TrueResult;
1837 if (FalseResult.Val == 2)
1838 return FalseResult;
1839 if (CondResult.Val == 1)
1840 return CondResult;
1841 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1842 return NoDiag();
1843 // Rare case where the diagnostics depend on which side is evaluated
1844 // Note that if we get here, CondResult is 0, and at least one of
1845 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001846 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001847 return FalseResult;
1848 }
1849 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001851 case Expr::CXXDefaultArgExprClass:
1852 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001853 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001854 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001855 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001857
Douglas Gregorf2991242009-09-10 23:31:45 +00001858 // Silence a GCC warning
1859 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001860}
Reid Spencer5f016e22007-07-11 17:01:13 +00001861
Eli Friedmane28d7192009-02-26 09:29:13 +00001862bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1863 SourceLocation *Loc, bool isEvaluated) const {
1864 ICEDiag d = CheckICE(this, Ctx);
1865 if (d.Val != 0) {
1866 if (Loc) *Loc = d.Loc;
1867 return false;
1868 }
1869 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001870 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001871 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001872 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1873 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001874 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 return true;
1876}
1877
Reid Spencer5f016e22007-07-11 17:01:13 +00001878/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1879/// integer constant expression with the value zero, or if this is one that is
1880/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001881bool Expr::isNullPointerConstant(ASTContext &Ctx,
1882 NullPointerConstantValueDependence NPC) const {
1883 if (isValueDependent()) {
1884 switch (NPC) {
1885 case NPC_NeverValueDependent:
1886 assert(false && "Unexpected value dependent expression!");
1887 // If the unthinkable happens, fall through to the safest alternative.
1888
1889 case NPC_ValueDependentIsNull:
1890 return isTypeDependent() || getType()->isIntegralType();
1891
1892 case NPC_ValueDependentIsNotNull:
1893 return false;
1894 }
1895 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001896
Sebastian Redl07779722008-10-31 14:43:28 +00001897 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001898 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001899 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001900 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001901 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001902 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001903 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001904 Pointee->isVoidType() && // to void*
1905 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001906 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001907 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001909 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1910 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001911 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001912 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1913 // Accept ((void*)0) as a null pointer constant, as many other
1914 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001915 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001916 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001917 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001918 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001919 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001920 } else if (isa<GNUNullExpr>(this)) {
1921 // The GNU __null extension is always a null pointer constant.
1922 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001923 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001924
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001925 // C++0x nullptr_t is always a null pointer constant.
1926 if (getType()->isNullPtrType())
1927 return true;
1928
Steve Naroffaa58f002008-01-14 16:10:57 +00001929 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001930 if (!getType()->isIntegerType() ||
1931 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001932 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 // If we have an integer constant expression, we need to *evaluate* it and
1935 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001936 llvm::APSInt Result;
1937 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001938}
Steve Naroff31a45842007-07-28 23:10:27 +00001939
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001940FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001941 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001942
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001943 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001944 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001945 if (Field->isBitField())
1946 return Field;
1947
1948 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1949 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1950 return BinOp->getLHS()->getBitField();
1951
1952 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001953}
1954
Chris Lattner2140e902009-02-16 22:14:05 +00001955/// isArrow - Return true if the base expression is a pointer to vector,
1956/// return false if the base expression is a vector.
1957bool ExtVectorElementExpr::isArrow() const {
1958 return getBase()->getType()->isPointerType();
1959}
1960
Nate Begeman213541a2008-04-18 23:10:10 +00001961unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00001962 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00001963 return VT->getNumElements();
1964 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00001965}
1966
Nate Begeman8a997642008-05-09 06:41:27 +00001967/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00001968bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00001969 // FIXME: Refactor this code to an accessor on the AST node which returns the
1970 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001971 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00001972
1973 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00001974 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00001975 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Nate Begeman190d6a22009-01-18 02:01:21 +00001977 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00001978 if (Comp[0] == 's' || Comp[0] == 'S')
1979 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Daniel Dunbar15027422009-10-17 23:53:04 +00001981 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1982 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00001983 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00001984
Steve Narofffec0b492007-07-30 03:29:09 +00001985 return false;
1986}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00001987
Nate Begeman8a997642008-05-09 06:41:27 +00001988/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00001989void ExtVectorElementExpr::getEncodedElementAccess(
1990 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001991 llvm::StringRef Comp = Accessor->getName();
1992 if (Comp[0] == 's' || Comp[0] == 'S')
1993 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Daniel Dunbar4b55b242009-10-18 02:09:31 +00001995 bool isHi = Comp == "hi";
1996 bool isLo = Comp == "lo";
1997 bool isEven = Comp == "even";
1998 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Nate Begeman8a997642008-05-09 06:41:27 +00002000 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2001 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Nate Begeman8a997642008-05-09 06:41:27 +00002003 if (isHi)
2004 Index = e + i;
2005 else if (isLo)
2006 Index = i;
2007 else if (isEven)
2008 Index = 2 * i;
2009 else if (isOdd)
2010 Index = 2 * i + 1;
2011 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002012 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002013
Nate Begeman3b8d1162008-05-13 21:03:02 +00002014 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002015 }
Nate Begeman8a997642008-05-09 06:41:27 +00002016}
2017
Steve Naroff68d331a2007-09-27 14:38:14 +00002018// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00002019ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002020 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00002021 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00002022 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00002023 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002024 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002025 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00002026 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002027 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002028 if (NumArgs) {
2029 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002030 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2031 }
Steve Naroff563477d2007-09-18 23:55:05 +00002032 LBracloc = LBrac;
2033 RBracloc = RBrac;
2034}
2035
Mike Stump1eb44332009-09-09 15:08:12 +00002036// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002037// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00002038ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002039 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00002040 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00002041 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00002042 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002043 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002044 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00002045 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002046 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002047 if (NumArgs) {
2048 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002049 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2050 }
Steve Naroff563477d2007-09-18 23:55:05 +00002051 LBracloc = LBrac;
2052 RBracloc = RBrac;
2053}
2054
Mike Stump1eb44332009-09-09 15:08:12 +00002055// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00002056ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2057 QualType retType, ObjCMethodDecl *mproto,
2058 SourceLocation LBrac, SourceLocation RBrac,
2059 Expr **ArgExprs, unsigned nargs)
Mike Stump1eb44332009-09-09 15:08:12 +00002060: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00002061MethodProto(mproto) {
2062 NumArgs = nargs;
2063 SubExprs = new Stmt*[NumArgs+1];
2064 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2065 if (NumArgs) {
2066 for (unsigned i = 0; i != NumArgs; ++i)
2067 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2068 }
2069 LBracloc = LBrac;
2070 RBracloc = RBrac;
2071}
2072
2073ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2074 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2075 switch (x & Flags) {
2076 default:
2077 assert(false && "Invalid ObjCMessageExpr.");
2078 case IsInstMeth:
2079 return ClassInfo(0, 0);
2080 case IsClsMethDeclUnknown:
2081 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2082 case IsClsMethDeclKnown: {
2083 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2084 return ClassInfo(D, D->getIdentifier());
2085 }
2086 }
2087}
2088
Chris Lattner0389e6b2009-04-26 00:44:05 +00002089void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2090 if (CI.first == 0 && CI.second == 0)
2091 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2092 else if (CI.first == 0)
2093 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2094 else
2095 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2096}
2097
2098
Chris Lattner27437ca2007-10-25 00:29:32 +00002099bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002100 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002101}
2102
Nate Begeman888376a2009-08-12 02:28:50 +00002103void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2104 unsigned NumExprs) {
2105 if (SubExprs) C.Deallocate(SubExprs);
2106
2107 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002108 this->NumExprs = NumExprs;
2109 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002110}
Nate Begeman888376a2009-08-12 02:28:50 +00002111
2112void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2113 DestroyChildren(C);
2114 if (SubExprs) C.Deallocate(SubExprs);
2115 this->~ShuffleVectorExpr();
2116 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002117}
2118
Douglas Gregor42602bb2009-08-07 06:08:38 +00002119void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002120 // Override default behavior of traversing children. If this has a type
2121 // operand and the type is a variable-length array, the child iteration
2122 // will iterate over the size expression. However, this expression belongs
2123 // to the type, not to this, so we don't want to delete it.
2124 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002125 if (isArgumentType()) {
2126 this->~SizeOfAlignOfExpr();
2127 C.Deallocate(this);
2128 }
Sebastian Redl05189992008-11-11 17:56:53 +00002129 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002130 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002131}
2132
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002133//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002134// DesignatedInitExpr
2135//===----------------------------------------------------------------------===//
2136
2137IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2138 assert(Kind == FieldDesignator && "Only valid on a field designator");
2139 if (Field.NameOrField & 0x01)
2140 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2141 else
2142 return getField()->getIdentifier();
2143}
2144
Mike Stump1eb44332009-09-09 15:08:12 +00002145DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002146 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002147 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002148 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002149 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002150 unsigned NumIndexExprs,
2151 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002152 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002153 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002154 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2155 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002156 this->Designators = new Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002157
2158 // Record the initializer itself.
2159 child_iterator Child = child_begin();
2160 *Child++ = Init;
2161
2162 // Copy the designators and their subexpressions, computing
2163 // value-dependence along the way.
2164 unsigned IndexIdx = 0;
2165 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002166 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002167
2168 if (this->Designators[I].isArrayDesignator()) {
2169 // Compute type- and value-dependence.
2170 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002171 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002172 Index->isTypeDependent() || Index->isValueDependent();
2173
2174 // Copy the index expressions into permanent storage.
2175 *Child++ = IndexExprs[IndexIdx++];
2176 } else if (this->Designators[I].isArrayRangeDesignator()) {
2177 // Compute type- and value-dependence.
2178 Expr *Start = IndexExprs[IndexIdx];
2179 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002180 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002181 Start->isTypeDependent() || Start->isValueDependent() ||
2182 End->isTypeDependent() || End->isValueDependent();
2183
2184 // Copy the start/end expressions into permanent storage.
2185 *Child++ = IndexExprs[IndexIdx++];
2186 *Child++ = IndexExprs[IndexIdx++];
2187 }
2188 }
2189
2190 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002191}
2192
Douglas Gregor05c13a32009-01-22 00:58:24 +00002193DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002194DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002195 unsigned NumDesignators,
2196 Expr **IndexExprs, unsigned NumIndexExprs,
2197 SourceLocation ColonOrEqualLoc,
2198 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002199 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002200 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor9ea62762009-05-21 23:17:49 +00002201 return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators,
2202 ColonOrEqualLoc, UsesColonSyntax,
2203 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002204}
2205
Mike Stump1eb44332009-09-09 15:08:12 +00002206DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002207 unsigned NumIndexExprs) {
2208 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2209 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2210 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2211}
2212
Mike Stump1eb44332009-09-09 15:08:12 +00002213void DesignatedInitExpr::setDesignators(const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002214 unsigned NumDesigs) {
2215 if (Designators)
2216 delete [] Designators;
2217
2218 Designators = new Designator[NumDesigs];
2219 NumDesignators = NumDesigs;
2220 for (unsigned I = 0; I != NumDesigs; ++I)
2221 Designators[I] = Desigs[I];
2222}
2223
Douglas Gregor05c13a32009-01-22 00:58:24 +00002224SourceRange DesignatedInitExpr::getSourceRange() const {
2225 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002226 Designator &First =
2227 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002228 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002229 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002230 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2231 else
2232 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2233 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002234 StartLoc =
2235 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002236 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2237}
2238
Douglas Gregor05c13a32009-01-22 00:58:24 +00002239Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2240 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2241 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2242 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002243 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2244 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2245}
2246
2247Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002248 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002249 "Requires array range designator");
2250 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2251 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002252 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2253 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2254}
2255
2256Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002257 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002258 "Requires array range designator");
2259 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2260 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002261 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2262 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2263}
2264
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002265/// \brief Replaces the designator at index @p Idx with the series
2266/// of designators in [First, Last).
Mike Stump1eb44332009-09-09 15:08:12 +00002267void DesignatedInitExpr::ExpandDesignator(unsigned Idx,
2268 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002269 const Designator *Last) {
2270 unsigned NumNewDesignators = Last - First;
2271 if (NumNewDesignators == 0) {
2272 std::copy_backward(Designators + Idx + 1,
2273 Designators + NumDesignators,
2274 Designators + Idx);
2275 --NumNewDesignators;
2276 return;
2277 } else if (NumNewDesignators == 1) {
2278 Designators[Idx] = *First;
2279 return;
2280 }
2281
Mike Stump1eb44332009-09-09 15:08:12 +00002282 Designator *NewDesignators
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002283 = new Designator[NumDesignators - 1 + NumNewDesignators];
2284 std::copy(Designators, Designators + Idx, NewDesignators);
2285 std::copy(First, Last, NewDesignators + Idx);
2286 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2287 NewDesignators + Idx + NumNewDesignators);
2288 delete [] Designators;
2289 Designators = NewDesignators;
2290 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2291}
2292
Douglas Gregor42602bb2009-08-07 06:08:38 +00002293void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002294 delete [] Designators;
Douglas Gregor42602bb2009-08-07 06:08:38 +00002295 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002296}
2297
Mike Stump1eb44332009-09-09 15:08:12 +00002298ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002299 Expr **exprs, unsigned nexprs,
2300 SourceLocation rparenloc)
2301: Expr(ParenListExprClass, QualType(),
2302 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002303 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002304 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002305
Nate Begeman2ef13e52009-08-10 23:49:36 +00002306 Exprs = new (C) Stmt*[nexprs];
2307 for (unsigned i = 0; i != nexprs; ++i)
2308 Exprs[i] = exprs[i];
2309}
2310
2311void ParenListExpr::DoDestroy(ASTContext& C) {
2312 DestroyChildren(C);
2313 if (Exprs) C.Deallocate(Exprs);
2314 this->~ParenListExpr();
2315 C.Deallocate(this);
2316}
2317
Douglas Gregor05c13a32009-01-22 00:58:24 +00002318//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002319// ExprIterator.
2320//===----------------------------------------------------------------------===//
2321
2322Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2323Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2324Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2325const Expr* ConstExprIterator::operator[](size_t idx) const {
2326 return cast<Expr>(I[idx]);
2327}
2328const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2329const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2330
2331//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002332// Child Iterators for iterating over subexpressions/substatements
2333//===----------------------------------------------------------------------===//
2334
2335// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002336Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2337Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002338
Steve Naroff7779db42007-11-12 14:29:37 +00002339// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002340Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2341Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002342
Steve Naroffe3e9add2008-06-02 23:03:37 +00002343// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002344Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2345Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002346
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002347// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002348Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2349 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002350}
Mike Stump1eb44332009-09-09 15:08:12 +00002351Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2352 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002353}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002354
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002355// ObjCSuperExpr
2356Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2357Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2358
Steve Narofff242b1b2009-07-24 17:54:45 +00002359// ObjCIsaExpr
2360Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2361Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2362
Chris Lattnerd9f69102008-08-10 01:53:14 +00002363// PredefinedExpr
2364Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2365Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002366
2367// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002368Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2369Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002370
2371// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002372Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002373Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002374
2375// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002376Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2377Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002378
Chris Lattner5d661452007-08-26 03:42:43 +00002379// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002380Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2381Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002382
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002383// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002384Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2385Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002386
2387// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002388Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2389Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002390
2391// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002392Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2393Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002394
Sebastian Redl05189992008-11-11 17:56:53 +00002395// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002396Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002397 // If this is of a type and the type is a VLA type (and not a typedef), the
2398 // size expression of the VLA needs to be treated as an executable expression.
2399 // Why isn't this weirdness documented better in StmtIterator?
2400 if (isArgumentType()) {
2401 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2402 getArgumentType().getTypePtr()))
2403 return child_iterator(T);
2404 return child_iterator();
2405 }
Sebastian Redld4575892008-12-03 23:17:54 +00002406 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002407}
Sebastian Redl05189992008-11-11 17:56:53 +00002408Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2409 if (isArgumentType())
2410 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002411 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002412}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002413
2414// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002415Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002416 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002417}
Ted Kremenek1237c672007-08-24 20:06:47 +00002418Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002419 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002420}
2421
2422// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002423Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002424 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002425}
Ted Kremenek1237c672007-08-24 20:06:47 +00002426Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002427 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002428}
Ted Kremenek1237c672007-08-24 20:06:47 +00002429
2430// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002431Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2432Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002433
Nate Begeman213541a2008-04-18 23:10:10 +00002434// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002435Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2436Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002437
2438// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002439Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2440Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002441
Ted Kremenek1237c672007-08-24 20:06:47 +00002442// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002443Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2444Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002445
2446// BinaryOperator
2447Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002448 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002449}
Ted Kremenek1237c672007-08-24 20:06:47 +00002450Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002451 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002452}
2453
2454// ConditionalOperator
2455Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002456 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002457}
Ted Kremenek1237c672007-08-24 20:06:47 +00002458Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002459 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002460}
2461
2462// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002463Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2464Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002465
Ted Kremenek1237c672007-08-24 20:06:47 +00002466// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002467Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2468Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002469
2470// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002471Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2472 return child_iterator();
2473}
2474
2475Stmt::child_iterator TypesCompatibleExpr::child_end() {
2476 return child_iterator();
2477}
Ted Kremenek1237c672007-08-24 20:06:47 +00002478
2479// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002480Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2481Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002482
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002483// GNUNullExpr
2484Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2485Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2486
Eli Friedmand38617c2008-05-14 19:38:39 +00002487// ShuffleVectorExpr
2488Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002489 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002490}
2491Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002492 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002493}
2494
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002495// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002496Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2497Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002498
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002499// InitListExpr
2500Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002501 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002502}
2503Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002504 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002505}
2506
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002507// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002508Stmt::child_iterator DesignatedInitExpr::child_begin() {
2509 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2510 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002511 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2512}
2513Stmt::child_iterator DesignatedInitExpr::child_end() {
2514 return child_iterator(&*child_begin() + NumSubExprs);
2515}
2516
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002517// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002518Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2519 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002520}
2521
Mike Stump1eb44332009-09-09 15:08:12 +00002522Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2523 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002524}
2525
Nate Begeman2ef13e52009-08-10 23:49:36 +00002526// ParenListExpr
2527Stmt::child_iterator ParenListExpr::child_begin() {
2528 return &Exprs[0];
2529}
2530Stmt::child_iterator ParenListExpr::child_end() {
2531 return &Exprs[0]+NumExprs;
2532}
2533
Ted Kremenek1237c672007-08-24 20:06:47 +00002534// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002535Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002536 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002537}
2538Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002539 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002540}
Ted Kremenek1237c672007-08-24 20:06:47 +00002541
2542// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002543Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2544Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002545
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002546// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002547Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002548 return child_iterator();
2549}
2550Stmt::child_iterator ObjCSelectorExpr::child_end() {
2551 return child_iterator();
2552}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002553
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002554// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002555Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2556 return child_iterator();
2557}
2558Stmt::child_iterator ObjCProtocolExpr::child_end() {
2559 return child_iterator();
2560}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002561
Steve Naroff563477d2007-09-18 23:55:05 +00002562// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002563Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002564 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002565}
2566Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002567 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002568}
2569
Steve Naroff4eb206b2008-09-03 18:15:37 +00002570// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002571Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2572Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002573
Ted Kremenek9da13f92008-09-26 23:24:14 +00002574Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2575Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }