blob: 4cb0aa4560deaf2cb2c68eda4c61b534d648a48f [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() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000101 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000102 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000103 if (Init->isValueDependent())
104 ValueDependent = true;
105 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000106 }
107 // (TD) - a nested-name-specifier or a qualified-id that names a
108 // member of an unknown specialization.
109 // (handled by DependentScopeDeclRefExpr)
110}
111
Douglas Gregora2813ce2009-10-23 18:54:35 +0000112DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
113 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000114 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000115 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000116 QualType T)
117 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000118 DecoratedD(D,
119 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000120 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000121 Loc(NameLoc) {
122 if (Qualifier) {
123 NameQualifier *NQ = getNameQualifier();
124 NQ->NNS = Qualifier;
125 NQ->Range = QualifierRange;
126 }
127
John McCalld5532b62009-11-23 01:53:49 +0000128 if (TemplateArgs)
129 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000130
131 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000132}
133
134DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
135 NestedNameSpecifier *Qualifier,
136 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000137 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000138 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000139 QualType T,
140 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000141 std::size_t Size = sizeof(DeclRefExpr);
142 if (Qualifier != 0)
143 Size += sizeof(NameQualifier);
144
John McCalld5532b62009-11-23 01:53:49 +0000145 if (TemplateArgs)
146 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000147
148 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
149 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000150 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000151}
152
153SourceRange DeclRefExpr::getSourceRange() const {
154 // FIXME: Does not handle multi-token names well, e.g., operator[].
155 SourceRange R(Loc);
156
157 if (hasQualifier())
158 R.setBegin(getQualifierRange().getBegin());
159 if (hasExplicitTemplateArgumentList())
160 R.setEnd(getRAngleLoc());
161 return R;
162}
163
Anders Carlsson3a082d82009-09-08 18:24:21 +0000164// FIXME: Maybe this should use DeclPrinter with a special "print predefined
165// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000166std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
167 ASTContext &Context = CurrentDecl->getASTContext();
168
Anders Carlsson3a082d82009-09-08 18:24:21 +0000169 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000170 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000171 return FD->getNameAsString();
172
173 llvm::SmallString<256> Name;
174 llvm::raw_svector_ostream Out(Name);
175
176 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000177 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000178 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000179 if (MD->isStatic())
180 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000181 }
182
183 PrintingPolicy Policy(Context.getLangOptions());
184 Policy.SuppressTagKind = true;
185
186 std::string Proto = FD->getQualifiedNameAsString(Policy);
187
John McCall183700f2009-09-21 23:43:11 +0000188 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000189 const FunctionProtoType *FT = 0;
190 if (FD->hasWrittenPrototype())
191 FT = dyn_cast<FunctionProtoType>(AFT);
192
193 Proto += "(";
194 if (FT) {
195 llvm::raw_string_ostream POut(Proto);
196 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
197 if (i) POut << ", ";
198 std::string Param;
199 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
200 POut << Param;
201 }
202
203 if (FT->isVariadic()) {
204 if (FD->getNumParams()) POut << ", ";
205 POut << "...";
206 }
207 }
208 Proto += ")";
209
Sam Weinig4eadcc52009-12-27 01:38:20 +0000210 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
211 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
212 if (ThisQuals.hasConst())
213 Proto += " const";
214 if (ThisQuals.hasVolatile())
215 Proto += " volatile";
216 }
217
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000218 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
219 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000220
221 Out << Proto;
222
223 Out.flush();
224 return Name.str().str();
225 }
226 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
227 llvm::SmallString<256> Name;
228 llvm::raw_svector_ostream Out(Name);
229 Out << (MD->isInstanceMethod() ? '-' : '+');
230 Out << '[';
231 Out << MD->getClassInterface()->getNameAsString();
232 if (const ObjCCategoryImplDecl *CID =
233 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
234 Out << '(';
235 Out << CID->getNameAsString();
236 Out << ')';
237 }
238 Out << ' ';
239 Out << MD->getSelector().getAsString();
240 Out << ']';
241
242 Out.flush();
243 return Name.str().str();
244 }
245 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
246 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
247 return "top level";
248 }
249 return "";
250}
251
Chris Lattnerda8249e2008-06-07 22:13:43 +0000252/// getValueAsApproximateDouble - This returns the value as an inaccurate
253/// double. Note that this may cause loss of precision, but is useful for
254/// debugging dumps, etc.
255double FloatingLiteral::getValueAsApproximateDouble() const {
256 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000257 bool ignored;
258 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
259 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000260 return V.convertToDouble();
261}
262
Chris Lattner2085fd62009-02-18 06:40:38 +0000263StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
264 unsigned ByteLength, bool Wide,
265 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000266 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000267 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000268 // Allocate enough space for the StringLiteral plus an array of locations for
269 // any concatenated string tokens.
270 void *Mem = C.Allocate(sizeof(StringLiteral)+
271 sizeof(SourceLocation)*(NumStrs-1),
272 llvm::alignof<StringLiteral>());
273 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000276 char *AStrData = new (C, 1) char[ByteLength];
277 memcpy(AStrData, StrData, ByteLength);
278 SL->StrData = AStrData;
279 SL->ByteLength = ByteLength;
280 SL->IsWide = Wide;
281 SL->TokLocs[0] = Loc[0];
282 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Chris Lattner726e1682009-02-18 05:49:11 +0000284 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000285 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
286 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000287}
288
Douglas Gregor673ecd62009-04-15 16:35:07 +0000289StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
290 void *Mem = C.Allocate(sizeof(StringLiteral)+
291 sizeof(SourceLocation)*(NumStrs-1),
292 llvm::alignof<StringLiteral>());
293 StringLiteral *SL = new (Mem) StringLiteral(QualType());
294 SL->StrData = 0;
295 SL->ByteLength = 0;
296 SL->NumConcatenated = NumStrs;
297 return SL;
298}
299
Douglas Gregor42602bb2009-08-07 06:08:38 +0000300void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000301 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000302 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Daniel Dunbarb6480232009-09-22 03:27:33 +0000305void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000306 if (StrData)
307 C.Deallocate(const_cast<char*>(StrData));
308
Daniel Dunbarb6480232009-09-22 03:27:33 +0000309 char *AStrData = new (C, 1) char[Str.size()];
310 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000311 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000312 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000313}
314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
316/// corresponds to, e.g. "sizeof" or "[pre]++".
317const char *UnaryOperator::getOpcodeStr(Opcode Op) {
318 switch (Op) {
319 default: assert(0 && "Unknown unary operator");
320 case PostInc: return "++";
321 case PostDec: return "--";
322 case PreInc: return "++";
323 case PreDec: return "--";
324 case AddrOf: return "&";
325 case Deref: return "*";
326 case Plus: return "+";
327 case Minus: return "-";
328 case Not: return "~";
329 case LNot: return "!";
330 case Real: return "__real";
331 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000333 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
335}
336
Mike Stump1eb44332009-09-09 15:08:12 +0000337UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000338UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
339 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000340 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000341 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
342 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
343 case OO_Amp: return AddrOf;
344 case OO_Star: return Deref;
345 case OO_Plus: return Plus;
346 case OO_Minus: return Minus;
347 case OO_Tilde: return Not;
348 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000349 }
350}
351
352OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
353 switch (Opc) {
354 case PostInc: case PreInc: return OO_PlusPlus;
355 case PostDec: case PreDec: return OO_MinusMinus;
356 case AddrOf: return OO_Amp;
357 case Deref: return OO_Star;
358 case Plus: return OO_Plus;
359 case Minus: return OO_Minus;
360 case Not: return OO_Tilde;
361 case LNot: return OO_Exclaim;
362 default: return OO_None;
363 }
364}
365
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367//===----------------------------------------------------------------------===//
368// Postfix Operators.
369//===----------------------------------------------------------------------===//
370
Ted Kremenek668bf912009-02-09 20:51:47 +0000371CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000372 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000373 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000374 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000375 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000376 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek668bf912009-02-09 20:51:47 +0000378 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000379 SubExprs[FN] = fn;
380 for (unsigned i = 0; i != numargs; ++i)
381 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000382
Douglas Gregorb4609802008-11-14 16:09:21 +0000383 RParenLoc = rparenloc;
384}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000385
Ted Kremenek668bf912009-02-09 20:51:47 +0000386CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
387 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000388 : Expr(CallExprClass, t,
389 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000390 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000391 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000392
393 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000394 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000396 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 RParenLoc = rparenloc;
399}
400
Mike Stump1eb44332009-09-09 15:08:12 +0000401CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
402 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000403 SubExprs = new (C) Stmt*[1];
404}
405
Douglas Gregor42602bb2009-08-07 06:08:38 +0000406void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000407 DestroyChildren(C);
408 if (SubExprs) C.Deallocate(SubExprs);
409 this->~CallExpr();
410 C.Deallocate(this);
411}
412
Nuno Lopesd20254f2009-12-20 23:11:08 +0000413Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000414 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000415 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000416 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000417 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
418 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000419
420 return 0;
421}
422
Nuno Lopesd20254f2009-12-20 23:11:08 +0000423FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000424 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000425}
426
Chris Lattnerd18b3292007-12-28 05:25:02 +0000427/// setNumArgs - This changes the number of arguments present in this call.
428/// Any orphaned expressions are deleted by this, and any new operands are set
429/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000430void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000431 // No change, just return.
432 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnerd18b3292007-12-28 05:25:02 +0000434 // If shrinking # arguments, just delete the extras and forgot them.
435 if (NumArgs < getNumArgs()) {
436 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000437 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438 this->NumArgs = NumArgs;
439 return;
440 }
441
442 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000443 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000444 // Copy over args.
445 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
446 NewSubExprs[i] = SubExprs[i];
447 // Null out new args.
448 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
449 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Douglas Gregor88c9a462009-04-17 21:46:47 +0000451 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000452 SubExprs = NewSubExprs;
453 this->NumArgs = NumArgs;
454}
455
Chris Lattnercb888962008-10-06 05:00:53 +0000456/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
457/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000458unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000459 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000461 // ImplicitCastExpr.
462 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
463 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000464 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000466 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
467 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000468 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anders Carlssonbcba2012008-01-31 02:13:57 +0000470 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
471 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000472 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000474 if (!FDecl->getIdentifier())
475 return 0;
476
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000477 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000478}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000479
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000480QualType CallExpr::getCallReturnType() const {
481 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000482 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000483 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000484 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000485 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall183700f2009-09-21 23:43:11 +0000487 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000488 return FnType->getResultType();
489}
Chris Lattnercb888962008-10-06 05:00:53 +0000490
Mike Stump1eb44332009-09-09 15:08:12 +0000491MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000492 SourceRange qualrange, ValueDecl *memberdecl,
John McCalld5532b62009-11-23 01:53:49 +0000493 SourceLocation l, const TemplateArgumentListInfo *targs,
494 QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000495 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000496 base->isTypeDependent() || (qual && qual->isDependent()),
497 base->isValueDependent() || (qual && qual->isDependent())),
498 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCalld5532b62009-11-23 01:53:49 +0000499 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000500 // Initialize the qualifier, if any.
501 if (HasQualifier) {
502 NameQualifier *NQ = getMemberQualifier();
503 NQ->NNS = qual;
504 NQ->Range = qualrange;
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000507 // Initialize the explicit template argument list, if any.
John McCalld5532b62009-11-23 01:53:49 +0000508 if (targs)
509 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
513 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000514 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000515 ValueDecl *memberdecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000517 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000518 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000519 std::size_t Size = sizeof(MemberExpr);
520 if (qual != 0)
521 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
John McCalld5532b62009-11-23 01:53:49 +0000523 if (targs)
524 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000526 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000527 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCalld5532b62009-11-23 01:53:49 +0000528 targs, ty);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000529}
530
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000531const char *CastExpr::getCastKindName() const {
532 switch (getCastKind()) {
533 case CastExpr::CK_Unknown:
534 return "Unknown";
535 case CastExpr::CK_BitCast:
536 return "BitCast";
537 case CastExpr::CK_NoOp:
538 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000539 case CastExpr::CK_BaseToDerived:
540 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000541 case CastExpr::CK_DerivedToBase:
542 return "DerivedToBase";
543 case CastExpr::CK_Dynamic:
544 return "Dynamic";
545 case CastExpr::CK_ToUnion:
546 return "ToUnion";
547 case CastExpr::CK_ArrayToPointerDecay:
548 return "ArrayToPointerDecay";
549 case CastExpr::CK_FunctionToPointerDecay:
550 return "FunctionToPointerDecay";
551 case CastExpr::CK_NullToMemberPointer:
552 return "NullToMemberPointer";
553 case CastExpr::CK_BaseToDerivedMemberPointer:
554 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000555 case CastExpr::CK_DerivedToBaseMemberPointer:
556 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000557 case CastExpr::CK_UserDefinedConversion:
558 return "UserDefinedConversion";
559 case CastExpr::CK_ConstructorConversion:
560 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000561 case CastExpr::CK_IntegralToPointer:
562 return "IntegralToPointer";
563 case CastExpr::CK_PointerToIntegral:
564 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000565 case CastExpr::CK_ToVoid:
566 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000567 case CastExpr::CK_VectorSplat:
568 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000569 case CastExpr::CK_IntegralCast:
570 return "IntegralCast";
571 case CastExpr::CK_IntegralToFloating:
572 return "IntegralToFloating";
573 case CastExpr::CK_FloatingToIntegral:
574 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000575 case CastExpr::CK_FloatingCast:
576 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000577 case CastExpr::CK_MemberPointerToBoolean:
578 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000579 case CastExpr::CK_AnyPointerToObjCPointerCast:
580 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000581 case CastExpr::CK_AnyPointerToBlockPointerCast:
582 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000585 assert(0 && "Unhandled cast kind!");
586 return 0;
587}
588
Douglas Gregor6eef5192009-12-14 19:27:10 +0000589Expr *CastExpr::getSubExprAsWritten() {
590 Expr *SubExpr = 0;
591 CastExpr *E = this;
592 do {
593 SubExpr = E->getSubExpr();
594
595 // Skip any temporary bindings; they're implicit.
596 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
597 SubExpr = Binder->getSubExpr();
598
599 // Conversions by constructor and conversion functions have a
600 // subexpression describing the call; strip it off.
601 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
602 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
603 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
604 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
605
606 // If the subexpression we're left with is an implicit cast, look
607 // through that, too.
608 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
609
610 return SubExpr;
611}
612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
614/// corresponds to, e.g. "<<=".
615const char *BinaryOperator::getOpcodeStr(Opcode Op) {
616 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000617 case PtrMemD: return ".*";
618 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 case Mul: return "*";
620 case Div: return "/";
621 case Rem: return "%";
622 case Add: return "+";
623 case Sub: return "-";
624 case Shl: return "<<";
625 case Shr: return ">>";
626 case LT: return "<";
627 case GT: return ">";
628 case LE: return "<=";
629 case GE: return ">=";
630 case EQ: return "==";
631 case NE: return "!=";
632 case And: return "&";
633 case Xor: return "^";
634 case Or: return "|";
635 case LAnd: return "&&";
636 case LOr: return "||";
637 case Assign: return "=";
638 case MulAssign: return "*=";
639 case DivAssign: return "/=";
640 case RemAssign: return "%=";
641 case AddAssign: return "+=";
642 case SubAssign: return "-=";
643 case ShlAssign: return "<<=";
644 case ShrAssign: return ">>=";
645 case AndAssign: return "&=";
646 case XorAssign: return "^=";
647 case OrAssign: return "|=";
648 case Comma: return ",";
649 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000650
651 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000655BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
656 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000657 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000658 case OO_Plus: return Add;
659 case OO_Minus: return Sub;
660 case OO_Star: return Mul;
661 case OO_Slash: return Div;
662 case OO_Percent: return Rem;
663 case OO_Caret: return Xor;
664 case OO_Amp: return And;
665 case OO_Pipe: return Or;
666 case OO_Equal: return Assign;
667 case OO_Less: return LT;
668 case OO_Greater: return GT;
669 case OO_PlusEqual: return AddAssign;
670 case OO_MinusEqual: return SubAssign;
671 case OO_StarEqual: return MulAssign;
672 case OO_SlashEqual: return DivAssign;
673 case OO_PercentEqual: return RemAssign;
674 case OO_CaretEqual: return XorAssign;
675 case OO_AmpEqual: return AndAssign;
676 case OO_PipeEqual: return OrAssign;
677 case OO_LessLess: return Shl;
678 case OO_GreaterGreater: return Shr;
679 case OO_LessLessEqual: return ShlAssign;
680 case OO_GreaterGreaterEqual: return ShrAssign;
681 case OO_EqualEqual: return EQ;
682 case OO_ExclaimEqual: return NE;
683 case OO_LessEqual: return LE;
684 case OO_GreaterEqual: return GE;
685 case OO_AmpAmp: return LAnd;
686 case OO_PipePipe: return LOr;
687 case OO_Comma: return Comma;
688 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000689 }
690}
691
692OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
693 static const OverloadedOperatorKind OverOps[] = {
694 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
695 OO_Star, OO_Slash, OO_Percent,
696 OO_Plus, OO_Minus,
697 OO_LessLess, OO_GreaterGreater,
698 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
699 OO_EqualEqual, OO_ExclaimEqual,
700 OO_Amp,
701 OO_Caret,
702 OO_Pipe,
703 OO_AmpAmp,
704 OO_PipePipe,
705 OO_Equal, OO_StarEqual,
706 OO_SlashEqual, OO_PercentEqual,
707 OO_PlusEqual, OO_MinusEqual,
708 OO_LessLessEqual, OO_GreaterGreaterEqual,
709 OO_AmpEqual, OO_CaretEqual,
710 OO_PipeEqual,
711 OO_Comma
712 };
713 return OverOps[Opc];
714}
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000717 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000718 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000719 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000720 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor73460a32009-11-19 23:25:22 +0000721 UnionFieldInit(0), HadArrayRangeDesignator(false)
722{
723 for (unsigned I = 0; I != numInits; ++I) {
724 if (initExprs[I]->isTypeDependent())
725 TypeDependent = true;
726 if (initExprs[I]->isValueDependent())
727 ValueDependent = true;
728 }
729
Chris Lattner418f6c72008-10-26 23:43:26 +0000730 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000731}
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Douglas Gregorfa219202009-03-20 23:58:33 +0000733void InitListExpr::reserveInits(unsigned NumInits) {
734 if (NumInits > InitExprs.size())
735 InitExprs.reserve(NumInits);
736}
737
Douglas Gregor4c678342009-01-28 21:54:33 +0000738void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000739 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000740 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000741 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000742 InitExprs.resize(NumInits, 0);
743}
744
745Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
746 if (Init >= InitExprs.size()) {
747 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
748 InitExprs.back() = expr;
749 return 0;
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor4c678342009-01-28 21:54:33 +0000752 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
753 InitExprs[Init] = expr;
754 return Result;
755}
756
Steve Naroffbfdcae62008-09-04 15:31:07 +0000757/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000758///
759const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000760 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000761 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000762}
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764SourceLocation BlockExpr::getCaretLocation() const {
765 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000766}
Mike Stump1eb44332009-09-09 15:08:12 +0000767const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000768 return TheBlock->getBody();
769}
Mike Stump1eb44332009-09-09 15:08:12 +0000770Stmt *BlockExpr::getBody() {
771 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000772}
Steve Naroff56ee6892008-10-08 17:01:13 +0000773
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775//===----------------------------------------------------------------------===//
776// Generic Expression Routines
777//===----------------------------------------------------------------------===//
778
Chris Lattner026dc962009-02-14 07:37:35 +0000779/// isUnusedResultAWarning - Return true if this immediate expression should
780/// be warned about if the result is unused. If so, fill in Loc and Ranges
781/// with location to warn on and the source range[s] to report with the
782/// warning.
783bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000784 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000785 // Don't warn if the expr is type dependent. The type could end up
786 // instantiating to void.
787 if (isTypeDependent())
788 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 switch (getStmtClass()) {
791 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000792 Loc = getExprLoc();
793 R1 = getSourceRange();
794 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000796 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000797 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 case UnaryOperatorClass: {
799 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000802 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 case UnaryOperator::PostInc:
804 case UnaryOperator::PostDec:
805 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000806 case UnaryOperator::PreDec: // ++/--
807 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 case UnaryOperator::Deref:
809 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000810 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000811 return false;
812 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 case UnaryOperator::Real:
814 case UnaryOperator::Imag:
815 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000816 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
817 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000818 return false;
819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000821 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 }
Chris Lattner026dc962009-02-14 07:37:35 +0000823 Loc = UO->getOperatorLoc();
824 R1 = UO->getSubExpr()->getSourceRange();
825 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000827 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000828 const BinaryOperator *BO = cast<BinaryOperator>(this);
829 // Consider comma to have side effects if the LHS or RHS does.
John McCallbf0ee352010-02-16 04:10:53 +0000830 if (BO->getOpcode() == BinaryOperator::Comma) {
831 // ((foo = <blah>), 0) is an idiom for hiding the result (and
832 // lvalue-ness) of an assignment written in a macro.
833 if (IntegerLiteral *IE =
834 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
835 if (IE->getValue() == 0)
836 return false;
837
Mike Stumpdf317bf2009-11-03 23:25:48 +0000838 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
839 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattner026dc962009-02-14 07:37:35 +0000842 if (BO->isAssignmentOp())
843 return false;
844 Loc = BO->getOperatorLoc();
845 R1 = BO->getLHS()->getSourceRange();
846 R2 = BO->getRHS()->getSourceRange();
847 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000848 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000849 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000850 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000851
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000852 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000853 // The condition must be evaluated, but if either the LHS or RHS is a
854 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000855 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000856 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000857 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000858 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000859 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000860 }
861
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000863 // If the base pointer or element is to a volatile pointer/field, accessing
864 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000865 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000866 return false;
867 Loc = cast<MemberExpr>(this)->getMemberLoc();
868 R1 = SourceRange(Loc, Loc);
869 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
870 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 case ArraySubscriptExprClass:
873 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000874 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000875 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000876 return false;
877 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
878 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
879 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
880 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000881
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000883 case CXXOperatorCallExprClass:
884 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000885 // If this is a direct call, get the callee.
886 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000887 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000888 // If the callee has attribute pure, const, or warn_unused_result, warn
889 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000890 //
891 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
892 // updated to match for QoI.
893 if (FD->getAttr<WarnUnusedResultAttr>() ||
894 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
895 Loc = CE->getCallee()->getLocStart();
896 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000898 if (unsigned NumArgs = CE->getNumArgs())
899 R2 = SourceRange(CE->getArg(0)->getLocStart(),
900 CE->getArg(NumArgs-1)->getLocEnd());
901 return true;
902 }
Chris Lattner026dc962009-02-14 07:37:35 +0000903 }
904 return false;
905 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000906
907 case CXXTemporaryObjectExprClass:
908 case CXXConstructExprClass:
909 return false;
910
Chris Lattnera9c01022007-09-26 22:06:30 +0000911 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000912 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000914 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000915#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000916 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000917 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000918 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000919 Loc = Ref->getLocation();
920 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
921 if (Ref->getBase())
922 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000923#else
924 Loc = getExprLoc();
925 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000926#endif
927 return true;
928 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000929 case StmtExprClass: {
930 // Statement exprs don't logically have side effects themselves, but are
931 // sometimes used in macros in ways that give them a type that is unused.
932 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
933 // however, if the result of the stmt expr is dead, we don't want to emit a
934 // warning.
935 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
936 if (!CS->body_empty())
937 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000938 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner026dc962009-02-14 07:37:35 +0000940 Loc = cast<StmtExpr>(this)->getLParenLoc();
941 R1 = getSourceRange();
942 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000943 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000944 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000945 // If this is an explicit cast to void, allow it. People do this when they
946 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000947 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000948 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000949 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
950 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
951 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000952 case CXXFunctionalCastExprClass: {
953 const CastExpr *CE = cast<CastExpr>(this);
954
955 // If this is a cast to void or a constructor conversion, check the operand.
956 // Otherwise, the result of the cast is unused.
957 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
958 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000959 return (cast<CastExpr>(this)->getSubExpr()
960 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000961 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
962 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
963 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Eli Friedman4be1f472008-05-19 21:24:43 +0000966 case ImplicitCastExprClass:
967 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000968 return (cast<ImplicitCastExpr>(this)
969 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000970
Chris Lattner04421082008-04-08 04:40:51 +0000971 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000972 return (cast<CXXDefaultArgExpr>(this)
973 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000974
975 case CXXNewExprClass:
976 // FIXME: In theory, there might be new expressions that don't have side
977 // effects (e.g. a placement new with an uninitialized POD).
978 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000979 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000980 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000981 return (cast<CXXBindTemporaryExpr>(this)
982 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000983 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000984 return (cast<CXXExprWithTemporaries>(this)
985 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000986 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000987}
988
Douglas Gregorba7e2102008-10-22 15:04:37 +0000989/// DeclCanBeLvalue - Determine whether the given declaration can be
990/// an lvalue. This is a helper routine for isLvalue.
991static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000992 // C++ [temp.param]p6:
993 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000994 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000995 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
996 return NTTParm->getType()->isReferenceType();
997
Douglas Gregor44b43212008-12-11 16:49:14 +0000998 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000999 // C++ 3.10p2: An lvalue refers to an object or function.
1000 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001001 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001002}
1003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1005/// incomplete type other than void. Nonarray expressions that can be lvalues:
1006/// - name, where name must be a variable
1007/// - e[i]
1008/// - (e), where e must be an lvalue
1009/// - e.name, where e must be an lvalue
1010/// - e->name
1011/// - *e, the type of e cannot be a function type
1012/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001013/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001014/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001015///
Chris Lattner28be73f2008-07-26 21:30:36 +00001016Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001017 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1018
1019 isLvalueResult Res = isLvalueInternal(Ctx);
1020 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1021 return Res;
1022
Douglas Gregor98cd5992008-10-21 23:43:52 +00001023 // first, check the type (C99 6.3.2.1). Expressions with function
1024 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001025 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 return LV_NotObjectType;
1027
Steve Naroffacb818a2008-02-10 01:39:04 +00001028 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001029 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001030 return LV_IncompleteVoidType;
1031
Eli Friedman53202852009-05-03 22:36:05 +00001032 return LV_Valid;
1033}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001034
Eli Friedman53202852009-05-03 22:36:05 +00001035// Check whether the expression can be sanely treated like an l-value
1036Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001038 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001039 case StringLiteralClass: // C99 6.5.1p4
1040 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001041 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1043 // For vectors, make sure base is an lvalue (i.e. not a function call).
1044 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001045 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001046 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001047 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001048 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1049 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 return LV_Valid;
1051 break;
Chris Lattner41110242008-06-17 18:05:57 +00001052 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001053 case BlockDeclRefExprClass: {
1054 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001055 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001056 return LV_Valid;
1057 break;
1058 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001059 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001061 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1062 NamedDecl *Member = m->getMemberDecl();
1063 // C++ [expr.ref]p4:
1064 // If E2 is declared to have type "reference to T", then E1.E2
1065 // is an lvalue.
1066 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1067 if (Value->getType()->isReferenceType())
1068 return LV_Valid;
1069
1070 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001071 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001072 return LV_Valid;
1073
1074 // -- If E2 is a non-static data member [...]. If E1 is an
1075 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001076 if (isa<FieldDecl>(Member)) {
1077 if (m->isArrow())
1078 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001079 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001080 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001081
1082 // -- If it refers to a static member function [...], then
1083 // E1.E2 is an lvalue.
1084 // -- Otherwise, if E1.E2 refers to a non-static member
1085 // function [...], then E1.E2 is not an lvalue.
1086 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1087 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1088
1089 // -- If E2 is a member enumerator [...], the expression E1.E2
1090 // is not an lvalue.
1091 if (isa<EnumConstantDecl>(Member))
1092 return LV_InvalidExpression;
1093
1094 // Not an lvalue.
1095 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001096 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001097
Douglas Gregor86f19402008-12-20 23:49:58 +00001098 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001099 if (m->isArrow())
1100 return LV_Valid;
1101 Expr *BaseExp = m->getBase();
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001102 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass)
1103 return LV_SubObjCPropertySetting;
1104 return
1105 (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ?
1106 LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001107 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001108 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001110 return LV_Valid; // C99 6.5.3p4
1111
1112 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001113 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1114 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001115 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001116
1117 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1118 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1119 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1120 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001122 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001123 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001124 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001126 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001127 case BinaryOperatorClass:
1128 case CompoundAssignOperatorClass: {
1129 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001130
1131 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1132 BinOp->getOpcode() == BinaryOperator::Comma)
1133 return BinOp->getRHS()->isLvalue(Ctx);
1134
Sebastian Redl22460502009-02-07 00:15:38 +00001135 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001136 // The result of a .* expression is an lvalue only if its first operand is
1137 // an lvalue and its second operand is a pointer to data member.
1138 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001139 !BinOp->getType()->isFunctionType())
1140 return BinOp->getLHS()->isLvalue(Ctx);
1141
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001142 // The result of an ->* expression is an lvalue only if its second operand
1143 // is a pointer to data member.
1144 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1145 !BinOp->getType()->isFunctionType()) {
1146 QualType Ty = BinOp->getRHS()->getType();
1147 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1148 return LV_Valid;
1149 }
1150
Douglas Gregorbf3af052008-11-13 20:12:29 +00001151 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001152 return LV_InvalidExpression;
1153
Douglas Gregorbf3af052008-11-13 20:12:29 +00001154 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001155 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001156 // The result of an assignment operation [...] is an lvalue.
1157 return LV_Valid;
1158
1159
1160 // C99 6.5.16:
1161 // An assignment expression [...] is not an lvalue.
1162 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001165 case CXXOperatorCallExprClass:
1166 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001167 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001168 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001169 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001170 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1171 if (ReturnType->isLValueReferenceType())
1172 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001173
Douglas Gregor9d293df2008-10-28 00:22:11 +00001174 break;
1175 }
Steve Naroffe6386392007-12-05 04:00:10 +00001176 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1177 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001178 case ChooseExprClass:
1179 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001180 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001181 case ExtVectorElementExprClass:
1182 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001183 return LV_DuplicateVectorComponents;
1184 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001185 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1186 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001187 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1188 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001189 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001190 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001191 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001192 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001193 case UnresolvedLookupExprClass:
1194 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001195 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001196 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001197 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001198 case CXXFunctionalCastExprClass:
1199 case CXXStaticCastExprClass:
1200 case CXXDynamicCastExprClass:
1201 case CXXReinterpretCastExprClass:
1202 case CXXConstCastExprClass:
1203 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001204 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001205 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1206 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001207 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1208 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001209 return LV_Valid;
1210 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001211 case CXXTypeidExprClass:
1212 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1213 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001214 case CXXBindTemporaryExprClass:
1215 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1216 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001217 case CXXBindReferenceExprClass:
1218 // Something that's bound to a reference is always an lvalue.
1219 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001220 case ConditionalOperatorClass: {
1221 // Complicated handling is only for C++.
1222 if (!Ctx.getLangOptions().CPlusPlus)
1223 return LV_InvalidExpression;
1224
1225 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1226 // everywhere there's an object converted to an rvalue. Also, any other
1227 // casts should be wrapped by ImplicitCastExprs. There's just the special
1228 // case involving throws to work out.
1229 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001230 Expr *True = Cond->getTrueExpr();
1231 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001232 // C++0x 5.16p2
1233 // If either the second or the third operand has type (cv) void, [...]
1234 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001235 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001236 return LV_InvalidExpression;
1237
1238 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001239 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001240 return LV_InvalidExpression;
1241
1242 // That's it.
1243 return LV_Valid;
1244 }
1245
Douglas Gregor2d48e782009-12-19 07:07:47 +00001246 case Expr::CXXExprWithTemporariesClass:
1247 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1248
1249 case Expr::ObjCMessageExprClass:
1250 if (const ObjCMethodDecl *Method
1251 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1252 if (Method->getResultType()->isLValueReferenceType())
1253 return LV_Valid;
1254 break;
1255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 default:
1257 break;
1258 }
1259 return LV_InvalidExpression;
1260}
1261
1262/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1263/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001264/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001265/// recursively, any member or element of all contained aggregates or unions)
1266/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001267Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001268Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001269 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001272 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001273 // C++ 3.10p11: Functions cannot be modified, but pointers to
1274 // functions can be modifiable.
1275 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1276 return MLV_NotObjectType;
1277 break;
1278
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 case LV_NotObjectType: return MLV_NotObjectType;
1280 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001281 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001282 case LV_InvalidExpression:
1283 // If the top level is a C-style cast, and the subexpression is a valid
1284 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1285 // GCC extension. We don't support it, but we want to produce good
1286 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001287 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1288 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1289 if (Loc)
1290 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001291 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001292 }
1293 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001294 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001295 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001296 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
1297 case LV_SubObjCPropertyGetterSetting:
1298 return MLV_SubObjCPropertyGetterSetting;
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001300
1301 // The following is illegal:
1302 // void takeclosure(void (^C)(void));
1303 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1304 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001305 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001306 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1307 return MLV_NotBlockQualified;
1308 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001309
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001310 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001311 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001312 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1313 if (Expr->getSetterMethod() == 0)
1314 return MLV_NoSetterProperty;
1315 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001316
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001317 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001319 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001321 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001323 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Ted Kremenek6217b802009-07-29 21:53:49 +00001326 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001327 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 return MLV_ConstQualified;
1329 }
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Mike Stump1eb44332009-09-09 15:08:12 +00001331 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001332}
1333
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001334/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001335/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001336bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001337 switch (getStmtClass()) {
1338 default:
1339 return false;
1340 case ObjCIvarRefExprClass:
1341 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001342 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001343 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001344 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001345 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001346 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001347 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001348 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001349 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001350 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001351 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001352 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1353 if (VD->hasGlobalStorage())
1354 return true;
1355 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001356 // dereferencing to a pointer is always a gc'able candidate,
1357 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001358 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001359 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001360 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001361 return false;
1362 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001363 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001364 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001365 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001366 }
1367 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001368 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001369 }
1370}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001371Expr* Expr::IgnoreParens() {
1372 Expr* E = this;
1373 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1374 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001376 return E;
1377}
1378
Chris Lattner56f34942008-02-13 01:02:39 +00001379/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1380/// or CastExprs or ImplicitCastExprs, returning their operand.
1381Expr *Expr::IgnoreParenCasts() {
1382 Expr *E = this;
1383 while (true) {
1384 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1385 E = P->getSubExpr();
1386 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1387 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001388 else
1389 return E;
1390 }
1391}
1392
Chris Lattnerecdd8412009-03-13 17:28:01 +00001393/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1394/// value (including ptr->int casts of the same size). Strip off any
1395/// ParenExpr or CastExprs, returning their operand.
1396Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1397 Expr *E = this;
1398 while (true) {
1399 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1400 E = P->getSubExpr();
1401 continue;
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Chris Lattnerecdd8412009-03-13 17:28:01 +00001404 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1405 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1406 // ptr<->int casts of the same width. We also ignore all identify casts.
1407 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Chris Lattnerecdd8412009-03-13 17:28:01 +00001409 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1410 E = SE;
1411 continue;
1412 }
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Chris Lattnerecdd8412009-03-13 17:28:01 +00001414 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1415 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1416 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1417 E = SE;
1418 continue;
1419 }
1420 }
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Chris Lattnerecdd8412009-03-13 17:28:01 +00001422 return E;
1423 }
1424}
1425
Douglas Gregor6eef5192009-12-14 19:27:10 +00001426bool Expr::isDefaultArgument() const {
1427 const Expr *E = this;
1428 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1429 E = ICE->getSubExprAsWritten();
1430
1431 return isa<CXXDefaultArgExpr>(E);
1432}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001433
Douglas Gregor898574e2008-12-05 23:32:09 +00001434/// hasAnyTypeDependentArguments - Determines if any of the expressions
1435/// in Exprs is type-dependent.
1436bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1437 for (unsigned I = 0; I < NumExprs; ++I)
1438 if (Exprs[I]->isTypeDependent())
1439 return true;
1440
1441 return false;
1442}
1443
1444/// hasAnyValueDependentArguments - Determines if any of the expressions
1445/// in Exprs is value-dependent.
1446bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1447 for (unsigned I = 0; I < NumExprs; ++I)
1448 if (Exprs[I]->isValueDependent())
1449 return true;
1450
1451 return false;
1452}
1453
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001454bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001455 // This function is attempting whether an expression is an initializer
1456 // which can be evaluated at compile-time. isEvaluatable handles most
1457 // of the cases, but it can't deal with some initializer-specific
1458 // expressions, and it can't deal with aggregates; we deal with those here,
1459 // and fall back to isEvaluatable for the other cases.
1460
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001461 // FIXME: This function assumes the variable being assigned to
1462 // isn't a reference type!
1463
Anders Carlssone8a32b82008-11-24 05:23:59 +00001464 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001465 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001466 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001467 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001468 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001469 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001470 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001471 // This handles gcc's extension that allows global initializers like
1472 // "struct x {int x;} x = (struct x) {};".
1473 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001474 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001475 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001476 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001477 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001478 // FIXME: This doesn't deal with fields with reference types correctly.
1479 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1480 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001481 const InitListExpr *Exp = cast<InitListExpr>(this);
1482 unsigned numInits = Exp->getNumInits();
1483 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001484 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001485 return false;
1486 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001487 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001488 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001489 case ImplicitValueInitExprClass:
1490 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001491 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001492 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001493 case UnaryOperatorClass: {
1494 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1495 if (Exp->getOpcode() == UnaryOperator::Extension)
1496 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1497 break;
1498 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001499 case BinaryOperatorClass: {
1500 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1501 // but this handles the common case.
1502 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1503 if (Exp->getOpcode() == BinaryOperator::Sub &&
1504 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1505 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1506 return true;
1507 break;
1508 }
Chris Lattner81045d82009-04-21 05:19:11 +00001509 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001510 case CStyleCastExprClass:
1511 // Handle casts with a destination that's a struct or union; this
1512 // deals with both the gcc no-op struct cast extension and the
1513 // cast-to-union extension.
1514 if (getType()->isRecordType())
1515 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001516
1517 // Integer->integer casts can be handled here, which is important for
1518 // things like (int)(&&x-&&y). Scary but true.
1519 if (getType()->isIntegerType() &&
1520 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1521 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1522
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001523 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001524 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001525 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001526}
1527
Reid Spencer5f016e22007-07-11 17:01:13 +00001528/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001529/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001530
1531/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1532/// comma, etc
1533///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001534/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1535/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1536/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001537
Eli Friedmane28d7192009-02-26 09:29:13 +00001538// CheckICE - This function does the fundamental ICE checking: the returned
1539// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1540// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001541// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001542// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001543// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001544//
1545// Meanings of Val:
1546// 0: This expression is an ICE if it can be evaluated by Evaluate.
1547// 1: This expression is not an ICE, but if it isn't evaluated, it's
1548// a legal subexpression for an ICE. This return value is used to handle
1549// the comma operator in C99 mode.
1550// 2: This expression is not an ICE, and is not a legal subexpression for one.
1551
1552struct ICEDiag {
1553 unsigned Val;
1554 SourceLocation Loc;
1555
1556 public:
1557 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1558 ICEDiag() : Val(0) {}
1559};
1560
1561ICEDiag NoDiag() { return ICEDiag(); }
1562
Eli Friedman60ce9632009-02-27 04:07:58 +00001563static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1564 Expr::EvalResult EVResult;
1565 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1566 !EVResult.Val.isInt()) {
1567 return ICEDiag(2, E->getLocStart());
1568 }
1569 return NoDiag();
1570}
1571
Eli Friedmane28d7192009-02-26 09:29:13 +00001572static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001573 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001574 if (!E->getType()->isIntegralType()) {
1575 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001576 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001577
1578 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001579#define STMT(Node, Base) case Expr::Node##Class:
1580#define EXPR(Node, Base)
1581#include "clang/AST/StmtNodes.def"
1582 case Expr::PredefinedExprClass:
1583 case Expr::FloatingLiteralClass:
1584 case Expr::ImaginaryLiteralClass:
1585 case Expr::StringLiteralClass:
1586 case Expr::ArraySubscriptExprClass:
1587 case Expr::MemberExprClass:
1588 case Expr::CompoundAssignOperatorClass:
1589 case Expr::CompoundLiteralExprClass:
1590 case Expr::ExtVectorElementExprClass:
1591 case Expr::InitListExprClass:
1592 case Expr::DesignatedInitExprClass:
1593 case Expr::ImplicitValueInitExprClass:
1594 case Expr::ParenListExprClass:
1595 case Expr::VAArgExprClass:
1596 case Expr::AddrLabelExprClass:
1597 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001598 case Expr::CXXMemberCallExprClass:
1599 case Expr::CXXDynamicCastExprClass:
1600 case Expr::CXXTypeidExprClass:
1601 case Expr::CXXNullPtrLiteralExprClass:
1602 case Expr::CXXThisExprClass:
1603 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001604 case Expr::CXXNewExprClass:
1605 case Expr::CXXDeleteExprClass:
1606 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001607 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001608 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001609 case Expr::CXXConstructExprClass:
1610 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001611 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001612 case Expr::CXXExprWithTemporariesClass:
1613 case Expr::CXXTemporaryObjectExprClass:
1614 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001615 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001616 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001617 case Expr::ObjCStringLiteralClass:
1618 case Expr::ObjCEncodeExprClass:
1619 case Expr::ObjCMessageExprClass:
1620 case Expr::ObjCSelectorExprClass:
1621 case Expr::ObjCProtocolExprClass:
1622 case Expr::ObjCIvarRefExprClass:
1623 case Expr::ObjCPropertyRefExprClass:
1624 case Expr::ObjCImplicitSetterGetterRefExprClass:
1625 case Expr::ObjCSuperExprClass:
1626 case Expr::ObjCIsaExprClass:
1627 case Expr::ShuffleVectorExprClass:
1628 case Expr::BlockExprClass:
1629 case Expr::BlockDeclRefExprClass:
1630 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001631 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001632
Douglas Gregor043cad22009-09-11 00:18:58 +00001633 case Expr::GNUNullExprClass:
1634 // GCC considers the GNU __null value to be an integral constant expression.
1635 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001636
Eli Friedmane28d7192009-02-26 09:29:13 +00001637 case Expr::ParenExprClass:
1638 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1639 case Expr::IntegerLiteralClass:
1640 case Expr::CharacterLiteralClass:
1641 case Expr::CXXBoolLiteralExprClass:
1642 case Expr::CXXZeroInitValueExprClass:
1643 case Expr::TypesCompatibleExprClass:
1644 case Expr::UnaryTypeTraitExprClass:
1645 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001646 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001647 case Expr::CXXOperatorCallExprClass: {
1648 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001649 if (CE->isBuiltinCall(Ctx))
1650 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001651 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001652 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001653 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001654 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1655 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001656 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001657 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001658 // C++ 7.1.5.1p2
1659 // A variable of non-volatile const-qualified integral or enumeration
1660 // type initialized by an ICE can be used in ICEs.
1661 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001662 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001663 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1664 if (Quals.hasVolatile() || !Quals.hasConst())
1665 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1666
Sebastian Redl31310a22010-02-01 20:16:42 +00001667 // Look for a declaration of this variable that has an initializer.
1668 const VarDecl *ID = 0;
1669 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001670 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001671 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001672 // We have already checked whether this subexpression is an
1673 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001674 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001675 return NoDiag();
1676 else
1677 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1678 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001679
John McCall1f1b3b32010-02-06 01:07:37 +00001680 // It's an ICE whether or not the definition we found is
1681 // out-of-line. See DR 721 and the discussion in Clang PR
1682 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001683
1684 if (Dcl->isCheckingICE()) {
1685 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1686 }
1687
1688 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001689 ICEDiag Result = CheckICE(Init, Ctx);
1690 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001691 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001692 return Result;
1693 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001694 }
1695 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001696 return ICEDiag(2, E->getLocStart());
1697 case Expr::UnaryOperatorClass: {
1698 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001700 case UnaryOperator::PostInc:
1701 case UnaryOperator::PostDec:
1702 case UnaryOperator::PreInc:
1703 case UnaryOperator::PreDec:
1704 case UnaryOperator::AddrOf:
1705 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001706 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001707
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001709 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001711 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001713 case UnaryOperator::Real:
1714 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001715 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001716 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001717 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1718 // Evaluate matches the proposed gcc behavior for cases like
1719 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1720 // compliance: we should warn earlier for offsetof expressions with
1721 // array subscripts that aren't ICEs, and if the array subscripts
1722 // are ICEs, the value of the offsetof must be an integer constant.
1723 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001726 case Expr::SizeOfAlignOfExprClass: {
1727 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1728 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1729 return ICEDiag(2, E->getLocStart());
1730 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001732 case Expr::BinaryOperatorClass: {
1733 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001734 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001735 case BinaryOperator::PtrMemD:
1736 case BinaryOperator::PtrMemI:
1737 case BinaryOperator::Assign:
1738 case BinaryOperator::MulAssign:
1739 case BinaryOperator::DivAssign:
1740 case BinaryOperator::RemAssign:
1741 case BinaryOperator::AddAssign:
1742 case BinaryOperator::SubAssign:
1743 case BinaryOperator::ShlAssign:
1744 case BinaryOperator::ShrAssign:
1745 case BinaryOperator::AndAssign:
1746 case BinaryOperator::XorAssign:
1747 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001748 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001749
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001753 case BinaryOperator::Add:
1754 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001757 case BinaryOperator::LT:
1758 case BinaryOperator::GT:
1759 case BinaryOperator::LE:
1760 case BinaryOperator::GE:
1761 case BinaryOperator::EQ:
1762 case BinaryOperator::NE:
1763 case BinaryOperator::And:
1764 case BinaryOperator::Xor:
1765 case BinaryOperator::Or:
1766 case BinaryOperator::Comma: {
1767 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1768 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001769 if (Exp->getOpcode() == BinaryOperator::Div ||
1770 Exp->getOpcode() == BinaryOperator::Rem) {
1771 // Evaluate gives an error for undefined Div/Rem, so make sure
1772 // we don't evaluate one.
1773 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1774 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1775 if (REval == 0)
1776 return ICEDiag(1, E->getLocStart());
1777 if (REval.isSigned() && REval.isAllOnesValue()) {
1778 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1779 if (LEval.isMinSignedValue())
1780 return ICEDiag(1, E->getLocStart());
1781 }
1782 }
1783 }
1784 if (Exp->getOpcode() == BinaryOperator::Comma) {
1785 if (Ctx.getLangOptions().C99) {
1786 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1787 // if it isn't evaluated.
1788 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1789 return ICEDiag(1, E->getLocStart());
1790 } else {
1791 // In both C89 and C++, commas in ICEs are illegal.
1792 return ICEDiag(2, E->getLocStart());
1793 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001794 }
1795 if (LHSResult.Val >= RHSResult.Val)
1796 return LHSResult;
1797 return RHSResult;
1798 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001800 case BinaryOperator::LOr: {
1801 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1802 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1803 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1804 // Rare case where the RHS has a comma "side-effect"; we need
1805 // to actually check the condition to see whether the side
1806 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001807 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001808 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001809 return RHSResult;
1810 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001811 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001812
Eli Friedmane28d7192009-02-26 09:29:13 +00001813 if (LHSResult.Val >= RHSResult.Val)
1814 return LHSResult;
1815 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001817 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001819 case Expr::ImplicitCastExprClass:
1820 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001821 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001822 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001823 case Expr::CXXStaticCastExprClass:
1824 case Expr::CXXReinterpretCastExprClass:
1825 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001826 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1827 if (SubExpr->getType()->isIntegralType())
1828 return CheckICE(SubExpr, Ctx);
1829 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1830 return NoDiag();
1831 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001833 case Expr::ConditionalOperatorClass: {
1834 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001835 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001836 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001837 // expression, and it is fully evaluated. This is an important GNU
1838 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001839 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001840 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001841 Expr::EvalResult EVResult;
1842 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1843 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001844 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001845 }
1846 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001847 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001848 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1849 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1850 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1851 if (CondResult.Val == 2)
1852 return CondResult;
1853 if (TrueResult.Val == 2)
1854 return TrueResult;
1855 if (FalseResult.Val == 2)
1856 return FalseResult;
1857 if (CondResult.Val == 1)
1858 return CondResult;
1859 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1860 return NoDiag();
1861 // Rare case where the diagnostics depend on which side is evaluated
1862 // Note that if we get here, CondResult is 0, and at least one of
1863 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001864 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001865 return FalseResult;
1866 }
1867 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001869 case Expr::CXXDefaultArgExprClass:
1870 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001871 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001872 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001873 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001875
Douglas Gregorf2991242009-09-10 23:31:45 +00001876 // Silence a GCC warning
1877 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001878}
Reid Spencer5f016e22007-07-11 17:01:13 +00001879
Eli Friedmane28d7192009-02-26 09:29:13 +00001880bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1881 SourceLocation *Loc, bool isEvaluated) const {
1882 ICEDiag d = CheckICE(this, Ctx);
1883 if (d.Val != 0) {
1884 if (Loc) *Loc = d.Loc;
1885 return false;
1886 }
1887 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001888 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001889 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001890 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1891 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001892 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 return true;
1894}
1895
Reid Spencer5f016e22007-07-11 17:01:13 +00001896/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1897/// integer constant expression with the value zero, or if this is one that is
1898/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001899bool Expr::isNullPointerConstant(ASTContext &Ctx,
1900 NullPointerConstantValueDependence NPC) const {
1901 if (isValueDependent()) {
1902 switch (NPC) {
1903 case NPC_NeverValueDependent:
1904 assert(false && "Unexpected value dependent expression!");
1905 // If the unthinkable happens, fall through to the safest alternative.
1906
1907 case NPC_ValueDependentIsNull:
1908 return isTypeDependent() || getType()->isIntegralType();
1909
1910 case NPC_ValueDependentIsNotNull:
1911 return false;
1912 }
1913 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001914
Sebastian Redl07779722008-10-31 14:43:28 +00001915 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001916 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001917 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001918 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001919 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001920 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001921 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001922 Pointee->isVoidType() && // to void*
1923 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001924 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001925 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001927 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1928 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001929 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001930 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1931 // Accept ((void*)0) as a null pointer constant, as many other
1932 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001933 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001934 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001935 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001936 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001937 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001938 } else if (isa<GNUNullExpr>(this)) {
1939 // The GNU __null extension is always a null pointer constant.
1940 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001941 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001942
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001943 // C++0x nullptr_t is always a null pointer constant.
1944 if (getType()->isNullPtrType())
1945 return true;
1946
Steve Naroffaa58f002008-01-14 16:10:57 +00001947 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001948 if (!getType()->isIntegerType() ||
1949 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001950 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 // If we have an integer constant expression, we need to *evaluate* it and
1953 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001954 llvm::APSInt Result;
1955 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001956}
Steve Naroff31a45842007-07-28 23:10:27 +00001957
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001958FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001959 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001960
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001961 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1962 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1963 E = ICE->getSubExpr()->IgnoreParens();
1964 else
1965 break;
1966 }
1967
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001968 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001969 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001970 if (Field->isBitField())
1971 return Field;
1972
1973 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1974 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1975 return BinOp->getLHS()->getBitField();
1976
1977 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001978}
1979
Anders Carlsson09380262010-01-31 17:18:49 +00001980bool Expr::refersToVectorElement() const {
1981 const Expr *E = this->IgnoreParens();
1982
1983 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1984 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1985 E = ICE->getSubExpr()->IgnoreParens();
1986 else
1987 break;
1988 }
1989
1990 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1991 return ASE->getBase()->getType()->isVectorType();
1992
1993 if (isa<ExtVectorElementExpr>(E))
1994 return true;
1995
1996 return false;
1997}
1998
Chris Lattner2140e902009-02-16 22:14:05 +00001999/// isArrow - Return true if the base expression is a pointer to vector,
2000/// return false if the base expression is a vector.
2001bool ExtVectorElementExpr::isArrow() const {
2002 return getBase()->getType()->isPointerType();
2003}
2004
Nate Begeman213541a2008-04-18 23:10:10 +00002005unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002006 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002007 return VT->getNumElements();
2008 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002009}
2010
Nate Begeman8a997642008-05-09 06:41:27 +00002011/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002012bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002013 // FIXME: Refactor this code to an accessor on the AST node which returns the
2014 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002015 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002016
2017 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002018 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002019 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Nate Begeman190d6a22009-01-18 02:01:21 +00002021 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002022 if (Comp[0] == 's' || Comp[0] == 'S')
2023 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Daniel Dunbar15027422009-10-17 23:53:04 +00002025 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2026 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002027 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002028
Steve Narofffec0b492007-07-30 03:29:09 +00002029 return false;
2030}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002031
Nate Begeman8a997642008-05-09 06:41:27 +00002032/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002033void ExtVectorElementExpr::getEncodedElementAccess(
2034 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002035 llvm::StringRef Comp = Accessor->getName();
2036 if (Comp[0] == 's' || Comp[0] == 'S')
2037 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002039 bool isHi = Comp == "hi";
2040 bool isLo = Comp == "lo";
2041 bool isEven = Comp == "even";
2042 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Nate Begeman8a997642008-05-09 06:41:27 +00002044 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2045 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Nate Begeman8a997642008-05-09 06:41:27 +00002047 if (isHi)
2048 Index = e + i;
2049 else if (isLo)
2050 Index = i;
2051 else if (isEven)
2052 Index = 2 * i;
2053 else if (isOdd)
2054 Index = 2 * i + 1;
2055 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002056 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002057
Nate Begeman3b8d1162008-05-13 21:03:02 +00002058 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002059 }
Nate Begeman8a997642008-05-09 06:41:27 +00002060}
2061
Steve Naroff68d331a2007-09-27 14:38:14 +00002062// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002063ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2064 Selector selInfo,
2065 QualType retType, ObjCMethodDecl *mproto,
2066 SourceLocation LBrac, SourceLocation RBrac,
2067 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002068 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002069 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002070 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002071 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002072 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002073 if (NumArgs) {
2074 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002075 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2076 }
Steve Naroff563477d2007-09-18 23:55:05 +00002077 LBracloc = LBrac;
2078 RBracloc = RBrac;
2079}
2080
Mike Stump1eb44332009-09-09 15:08:12 +00002081// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002082// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002083ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
2084 Selector selInfo, QualType retType,
2085 ObjCMethodDecl *mproto,
2086 SourceLocation LBrac, SourceLocation RBrac,
2087 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002088 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002089 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002090 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002091 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002092 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002093 if (NumArgs) {
2094 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002095 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2096 }
Steve Naroff563477d2007-09-18 23:55:05 +00002097 LBracloc = LBrac;
2098 RBracloc = RBrac;
2099}
2100
Mike Stump1eb44332009-09-09 15:08:12 +00002101// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002102ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
2103 Selector selInfo, QualType retType,
2104 ObjCMethodDecl *mproto, SourceLocation LBrac,
2105 SourceLocation RBrac, Expr **ArgExprs,
2106 unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002107: Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00002108MethodProto(mproto) {
2109 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002110 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002111 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2112 if (NumArgs) {
2113 for (unsigned i = 0; i != NumArgs; ++i)
2114 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2115 }
2116 LBracloc = LBrac;
2117 RBracloc = RBrac;
2118}
2119
2120ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2121 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2122 switch (x & Flags) {
2123 default:
2124 assert(false && "Invalid ObjCMessageExpr.");
2125 case IsInstMeth:
2126 return ClassInfo(0, 0);
2127 case IsClsMethDeclUnknown:
2128 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2129 case IsClsMethDeclKnown: {
2130 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2131 return ClassInfo(D, D->getIdentifier());
2132 }
2133 }
2134}
2135
Chris Lattner0389e6b2009-04-26 00:44:05 +00002136void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2137 if (CI.first == 0 && CI.second == 0)
2138 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2139 else if (CI.first == 0)
2140 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2141 else
2142 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2143}
2144
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002145void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2146 DestroyChildren(C);
2147 if (SubExprs)
2148 C.Deallocate(SubExprs);
2149 this->~ObjCMessageExpr();
2150 C.Deallocate((void*) this);
2151}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002152
Chris Lattner27437ca2007-10-25 00:29:32 +00002153bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002154 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002155}
2156
Nate Begeman888376a2009-08-12 02:28:50 +00002157void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2158 unsigned NumExprs) {
2159 if (SubExprs) C.Deallocate(SubExprs);
2160
2161 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002162 this->NumExprs = NumExprs;
2163 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002164}
Nate Begeman888376a2009-08-12 02:28:50 +00002165
2166void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2167 DestroyChildren(C);
2168 if (SubExprs) C.Deallocate(SubExprs);
2169 this->~ShuffleVectorExpr();
2170 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002171}
2172
Douglas Gregor42602bb2009-08-07 06:08:38 +00002173void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002174 // Override default behavior of traversing children. If this has a type
2175 // operand and the type is a variable-length array, the child iteration
2176 // will iterate over the size expression. However, this expression belongs
2177 // to the type, not to this, so we don't want to delete it.
2178 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002179 if (isArgumentType()) {
2180 this->~SizeOfAlignOfExpr();
2181 C.Deallocate(this);
2182 }
Sebastian Redl05189992008-11-11 17:56:53 +00002183 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002184 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002185}
2186
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002187//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002188// DesignatedInitExpr
2189//===----------------------------------------------------------------------===//
2190
2191IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2192 assert(Kind == FieldDesignator && "Only valid on a field designator");
2193 if (Field.NameOrField & 0x01)
2194 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2195 else
2196 return getField()->getIdentifier();
2197}
2198
Douglas Gregor319d57f2010-01-06 23:17:19 +00002199DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2200 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002201 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002202 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002203 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002204 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002205 unsigned NumIndexExprs,
2206 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002207 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002208 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002209 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2210 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002211 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002212
2213 // Record the initializer itself.
2214 child_iterator Child = child_begin();
2215 *Child++ = Init;
2216
2217 // Copy the designators and their subexpressions, computing
2218 // value-dependence along the way.
2219 unsigned IndexIdx = 0;
2220 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002221 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002222
2223 if (this->Designators[I].isArrayDesignator()) {
2224 // Compute type- and value-dependence.
2225 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002226 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002227 Index->isTypeDependent() || Index->isValueDependent();
2228
2229 // Copy the index expressions into permanent storage.
2230 *Child++ = IndexExprs[IndexIdx++];
2231 } else if (this->Designators[I].isArrayRangeDesignator()) {
2232 // Compute type- and value-dependence.
2233 Expr *Start = IndexExprs[IndexIdx];
2234 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002235 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002236 Start->isTypeDependent() || Start->isValueDependent() ||
2237 End->isTypeDependent() || End->isValueDependent();
2238
2239 // Copy the start/end expressions into permanent storage.
2240 *Child++ = IndexExprs[IndexIdx++];
2241 *Child++ = IndexExprs[IndexIdx++];
2242 }
2243 }
2244
2245 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002246}
2247
Douglas Gregor05c13a32009-01-22 00:58:24 +00002248DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002249DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002250 unsigned NumDesignators,
2251 Expr **IndexExprs, unsigned NumIndexExprs,
2252 SourceLocation ColonOrEqualLoc,
2253 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002254 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002255 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002256 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002257 ColonOrEqualLoc, UsesColonSyntax,
2258 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002259}
2260
Mike Stump1eb44332009-09-09 15:08:12 +00002261DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002262 unsigned NumIndexExprs) {
2263 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2264 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2265 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2266}
2267
Douglas Gregor319d57f2010-01-06 23:17:19 +00002268void DesignatedInitExpr::setDesignators(ASTContext &C,
2269 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002270 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002271 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002272
Douglas Gregor319d57f2010-01-06 23:17:19 +00002273 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002274 NumDesignators = NumDesigs;
2275 for (unsigned I = 0; I != NumDesigs; ++I)
2276 Designators[I] = Desigs[I];
2277}
2278
Douglas Gregor05c13a32009-01-22 00:58:24 +00002279SourceRange DesignatedInitExpr::getSourceRange() const {
2280 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002281 Designator &First =
2282 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002283 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002284 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002285 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2286 else
2287 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2288 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002289 StartLoc =
2290 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002291 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2292}
2293
Douglas Gregor05c13a32009-01-22 00:58:24 +00002294Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2295 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2296 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2297 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002298 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2299 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2300}
2301
2302Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002303 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002304 "Requires array range designator");
2305 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2306 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002307 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2308 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2309}
2310
2311Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002312 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002313 "Requires array range designator");
2314 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2315 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002316 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2317 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2318}
2319
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002320/// \brief Replaces the designator at index @p Idx with the series
2321/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002322void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002323 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002324 const Designator *Last) {
2325 unsigned NumNewDesignators = Last - First;
2326 if (NumNewDesignators == 0) {
2327 std::copy_backward(Designators + Idx + 1,
2328 Designators + NumDesignators,
2329 Designators + Idx);
2330 --NumNewDesignators;
2331 return;
2332 } else if (NumNewDesignators == 1) {
2333 Designators[Idx] = *First;
2334 return;
2335 }
2336
Mike Stump1eb44332009-09-09 15:08:12 +00002337 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002338 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002339 std::copy(Designators, Designators + Idx, NewDesignators);
2340 std::copy(First, Last, NewDesignators + Idx);
2341 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2342 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002343 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002344 Designators = NewDesignators;
2345 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2346}
2347
Douglas Gregor42602bb2009-08-07 06:08:38 +00002348void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002349 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002350 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002351}
2352
Douglas Gregor319d57f2010-01-06 23:17:19 +00002353void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2354 for (unsigned I = 0; I != NumDesignators; ++I)
2355 Designators[I].~Designator();
2356 C.Deallocate(Designators);
2357 Designators = 0;
2358}
2359
Mike Stump1eb44332009-09-09 15:08:12 +00002360ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002361 Expr **exprs, unsigned nexprs,
2362 SourceLocation rparenloc)
2363: Expr(ParenListExprClass, QualType(),
2364 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002365 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002366 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002367
Nate Begeman2ef13e52009-08-10 23:49:36 +00002368 Exprs = new (C) Stmt*[nexprs];
2369 for (unsigned i = 0; i != nexprs; ++i)
2370 Exprs[i] = exprs[i];
2371}
2372
2373void ParenListExpr::DoDestroy(ASTContext& C) {
2374 DestroyChildren(C);
2375 if (Exprs) C.Deallocate(Exprs);
2376 this->~ParenListExpr();
2377 C.Deallocate(this);
2378}
2379
Douglas Gregor05c13a32009-01-22 00:58:24 +00002380//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002381// ExprIterator.
2382//===----------------------------------------------------------------------===//
2383
2384Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2385Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2386Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2387const Expr* ConstExprIterator::operator[](size_t idx) const {
2388 return cast<Expr>(I[idx]);
2389}
2390const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2391const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2392
2393//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002394// Child Iterators for iterating over subexpressions/substatements
2395//===----------------------------------------------------------------------===//
2396
2397// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002398Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2399Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002400
Steve Naroff7779db42007-11-12 14:29:37 +00002401// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002402Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2403Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002404
Steve Naroffe3e9add2008-06-02 23:03:37 +00002405// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002406Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2407Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002408
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002409// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002410Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2411 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002412}
Mike Stump1eb44332009-09-09 15:08:12 +00002413Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2414 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002415}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002416
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002417// ObjCSuperExpr
2418Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2419Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2420
Steve Narofff242b1b2009-07-24 17:54:45 +00002421// ObjCIsaExpr
2422Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2423Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2424
Chris Lattnerd9f69102008-08-10 01:53:14 +00002425// PredefinedExpr
2426Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2427Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002428
2429// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002430Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2431Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002432
2433// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002434Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002435Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002436
2437// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002438Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2439Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002440
Chris Lattner5d661452007-08-26 03:42:43 +00002441// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002442Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2443Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002444
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002445// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002446Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2447Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002448
2449// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002450Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2451Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002452
2453// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002454Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2455Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002456
Sebastian Redl05189992008-11-11 17:56:53 +00002457// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002458Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002459 // If this is of a type and the type is a VLA type (and not a typedef), the
2460 // size expression of the VLA needs to be treated as an executable expression.
2461 // Why isn't this weirdness documented better in StmtIterator?
2462 if (isArgumentType()) {
2463 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2464 getArgumentType().getTypePtr()))
2465 return child_iterator(T);
2466 return child_iterator();
2467 }
Sebastian Redld4575892008-12-03 23:17:54 +00002468 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002469}
Sebastian Redl05189992008-11-11 17:56:53 +00002470Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2471 if (isArgumentType())
2472 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002473 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002474}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002475
2476// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002477Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002478 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002479}
Ted Kremenek1237c672007-08-24 20:06:47 +00002480Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002481 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002482}
2483
2484// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002485Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002486 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002487}
Ted Kremenek1237c672007-08-24 20:06:47 +00002488Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002489 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002490}
Ted Kremenek1237c672007-08-24 20:06:47 +00002491
2492// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002493Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2494Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002495
Nate Begeman213541a2008-04-18 23:10:10 +00002496// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002497Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2498Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002499
2500// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002501Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2502Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002503
Ted Kremenek1237c672007-08-24 20:06:47 +00002504// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002505Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2506Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002507
2508// BinaryOperator
2509Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002510 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002511}
Ted Kremenek1237c672007-08-24 20:06:47 +00002512Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002513 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002514}
2515
2516// ConditionalOperator
2517Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002518 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002519}
Ted Kremenek1237c672007-08-24 20:06:47 +00002520Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002521 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002522}
2523
2524// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002525Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2526Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002527
Ted Kremenek1237c672007-08-24 20:06:47 +00002528// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002529Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2530Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002531
2532// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002533Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2534 return child_iterator();
2535}
2536
2537Stmt::child_iterator TypesCompatibleExpr::child_end() {
2538 return child_iterator();
2539}
Ted Kremenek1237c672007-08-24 20:06:47 +00002540
2541// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002542Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2543Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002544
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002545// GNUNullExpr
2546Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2547Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2548
Eli Friedmand38617c2008-05-14 19:38:39 +00002549// ShuffleVectorExpr
2550Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002551 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002552}
2553Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002554 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002555}
2556
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002557// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002558Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2559Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002560
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002561// InitListExpr
2562Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002563 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002564}
2565Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002566 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002567}
2568
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002569// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002570Stmt::child_iterator DesignatedInitExpr::child_begin() {
2571 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2572 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002573 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2574}
2575Stmt::child_iterator DesignatedInitExpr::child_end() {
2576 return child_iterator(&*child_begin() + NumSubExprs);
2577}
2578
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002579// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002580Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2581 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002582}
2583
Mike Stump1eb44332009-09-09 15:08:12 +00002584Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2585 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002586}
2587
Nate Begeman2ef13e52009-08-10 23:49:36 +00002588// ParenListExpr
2589Stmt::child_iterator ParenListExpr::child_begin() {
2590 return &Exprs[0];
2591}
2592Stmt::child_iterator ParenListExpr::child_end() {
2593 return &Exprs[0]+NumExprs;
2594}
2595
Ted Kremenek1237c672007-08-24 20:06:47 +00002596// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002597Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002598 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002599}
2600Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002601 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002602}
Ted Kremenek1237c672007-08-24 20:06:47 +00002603
2604// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002605Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2606Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002607
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002608// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002609Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002610 return child_iterator();
2611}
2612Stmt::child_iterator ObjCSelectorExpr::child_end() {
2613 return child_iterator();
2614}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002615
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002616// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002617Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2618 return child_iterator();
2619}
2620Stmt::child_iterator ObjCProtocolExpr::child_end() {
2621 return child_iterator();
2622}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002623
Steve Naroff563477d2007-09-18 23:55:05 +00002624// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002625Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002626 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002627}
2628Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002629 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002630}
2631
Steve Naroff4eb206b2008-09-03 18:15:37 +00002632// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002633Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2634Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002635
Ted Kremenek9da13f92008-09-26 23:24:14 +00002636Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2637Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }