blob: c08a3a26e1782d31c2eab587503d69681f447e56 [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
Ted Kremenekba7bc552010-02-19 01:50:18 +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),
Ted Kremenekba7bc552010-02-19 01:50:18 +0000721 UnionFieldInit(0), HadArrayRangeDesignator(false)
722{
723 for (unsigned I = 0; I != numInits; ++I) {
724 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000725 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000726 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000727 ValueDependent = true;
728 }
Ted Kremenekba7bc552010-02-19 01:50:18 +0000729
730 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000731}
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Ted Kremenekba7bc552010-02-19 01:50:18 +0000733void InitListExpr::reserveInits(unsigned NumInits) {
734 if (NumInits > InitExprs.size())
735 InitExprs.reserve(NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000736}
737
Ted Kremenekba7bc552010-02-19 01:50:18 +0000738void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
739 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
740 Idx < LastIdx; ++Idx)
741 InitExprs[Idx]->Destroy(Context);
742 InitExprs.resize(NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000743}
744
Ted Kremenekba7bc552010-02-19 01:50:18 +0000745Expr *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;
Douglas Gregor4c678342009-01-28 21:54:33 +0000750 }
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:
Douglas Gregore873fb72010-02-16 21:39:57 +00001123 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1124 return LV_Valid;
1125
1126 // If this is a conversion to a class temporary, make a note of
1127 // that.
1128 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1129 return LV_ClassTemporary;
1130
1131 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001133 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001134 case BinaryOperatorClass:
1135 case CompoundAssignOperatorClass: {
1136 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001137
1138 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1139 BinOp->getOpcode() == BinaryOperator::Comma)
1140 return BinOp->getRHS()->isLvalue(Ctx);
1141
Sebastian Redl22460502009-02-07 00:15:38 +00001142 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001143 // The result of a .* expression is an lvalue only if its first operand is
1144 // an lvalue and its second operand is a pointer to data member.
1145 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001146 !BinOp->getType()->isFunctionType())
1147 return BinOp->getLHS()->isLvalue(Ctx);
1148
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001149 // The result of an ->* expression is an lvalue only if its second operand
1150 // is a pointer to data member.
1151 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1152 !BinOp->getType()->isFunctionType()) {
1153 QualType Ty = BinOp->getRHS()->getType();
1154 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1155 return LV_Valid;
1156 }
1157
Douglas Gregorbf3af052008-11-13 20:12:29 +00001158 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001159 return LV_InvalidExpression;
1160
Douglas Gregorbf3af052008-11-13 20:12:29 +00001161 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001162 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001163 // The result of an assignment operation [...] is an lvalue.
1164 return LV_Valid;
1165
1166
1167 // C99 6.5.16:
1168 // An assignment expression [...] is not an lvalue.
1169 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001172 case CXXOperatorCallExprClass:
1173 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001174 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001175 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001176 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001177 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1178 if (ReturnType->isLValueReferenceType())
1179 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001180
Douglas Gregore873fb72010-02-16 21:39:57 +00001181 // If the function is returning a class temporary, make a note of
1182 // that.
1183 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1184 return LV_ClassTemporary;
1185
Douglas Gregor9d293df2008-10-28 00:22:11 +00001186 break;
1187 }
Steve Naroffe6386392007-12-05 04:00:10 +00001188 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001189 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001190 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001191 case ChooseExprClass:
1192 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001193 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001194 case ExtVectorElementExprClass:
1195 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001196 return LV_DuplicateVectorComponents;
1197 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001198 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1199 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001200 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1201 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001202 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001203 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001204 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001205 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001206 case UnresolvedLookupExprClass:
1207 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001208 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001209 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001210 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001211 case CXXFunctionalCastExprClass:
1212 case CXXStaticCastExprClass:
1213 case CXXDynamicCastExprClass:
1214 case CXXReinterpretCastExprClass:
1215 case CXXConstCastExprClass:
1216 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001217 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001218 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1219 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001220 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1221 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001222 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001223
1224 // If this is a conversion to a class temporary, make a note of
1225 // that.
1226 if (Ctx.getLangOptions().CPlusPlus &&
1227 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1228 return LV_ClassTemporary;
1229
Douglas Gregor9d293df2008-10-28 00:22:11 +00001230 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001231 case CXXTypeidExprClass:
1232 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1233 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001234 case CXXBindTemporaryExprClass:
1235 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1236 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001237 case CXXBindReferenceExprClass:
1238 // Something that's bound to a reference is always an lvalue.
1239 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001240 case ConditionalOperatorClass: {
1241 // Complicated handling is only for C++.
1242 if (!Ctx.getLangOptions().CPlusPlus)
1243 return LV_InvalidExpression;
1244
1245 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1246 // everywhere there's an object converted to an rvalue. Also, any other
1247 // casts should be wrapped by ImplicitCastExprs. There's just the special
1248 // case involving throws to work out.
1249 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001250 Expr *True = Cond->getTrueExpr();
1251 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001252 // C++0x 5.16p2
1253 // If either the second or the third operand has type (cv) void, [...]
1254 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001255 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001256 return LV_InvalidExpression;
1257
1258 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001259 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001260 return LV_InvalidExpression;
1261
1262 // That's it.
1263 return LV_Valid;
1264 }
1265
Douglas Gregor2d48e782009-12-19 07:07:47 +00001266 case Expr::CXXExprWithTemporariesClass:
1267 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1268
1269 case Expr::ObjCMessageExprClass:
1270 if (const ObjCMethodDecl *Method
1271 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1272 if (Method->getResultType()->isLValueReferenceType())
1273 return LV_Valid;
1274 break;
1275
Douglas Gregore873fb72010-02-16 21:39:57 +00001276 case Expr::CXXConstructExprClass:
1277 case Expr::CXXTemporaryObjectExprClass:
1278 case Expr::CXXZeroInitValueExprClass:
1279 return LV_ClassTemporary;
1280
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 default:
1282 break;
1283 }
1284 return LV_InvalidExpression;
1285}
1286
1287/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1288/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001289/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001290/// recursively, any member or element of all contained aggregates or unions)
1291/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001292Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001293Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001294 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001297 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001298 // C++ 3.10p11: Functions cannot be modified, but pointers to
1299 // functions can be modifiable.
1300 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1301 return MLV_NotObjectType;
1302 break;
1303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 case LV_NotObjectType: return MLV_NotObjectType;
1305 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001306 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001307 case LV_InvalidExpression:
1308 // If the top level is a C-style cast, and the subexpression is a valid
1309 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1310 // GCC extension. We don't support it, but we want to produce good
1311 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001312 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1313 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1314 if (Loc)
1315 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001316 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001317 }
1318 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001319 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001320 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001321 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
1322 case LV_SubObjCPropertyGetterSetting:
1323 return MLV_SubObjCPropertyGetterSetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001324 case LV_ClassTemporary:
1325 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001327
1328 // The following is illegal:
1329 // void takeclosure(void (^C)(void));
1330 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1331 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001332 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001333 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1334 return MLV_NotBlockQualified;
1335 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001336
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001337 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001338 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001339 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1340 if (Expr->getSetterMethod() == 0)
1341 return MLV_NoSetterProperty;
1342 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001343
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001344 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001346 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001347 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001348 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001350 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Ted Kremenek6217b802009-07-29 21:53:49 +00001353 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001354 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 return MLV_ConstQualified;
1356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Mike Stump1eb44332009-09-09 15:08:12 +00001358 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001359}
1360
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001361/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001362/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001363bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001364 switch (getStmtClass()) {
1365 default:
1366 return false;
1367 case ObjCIvarRefExprClass:
1368 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001369 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001370 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001371 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001372 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001373 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001374 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001375 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001376 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001377 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001378 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001379 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1380 if (VD->hasGlobalStorage())
1381 return true;
1382 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001383 // dereferencing to a pointer is always a gc'able candidate,
1384 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001385 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001386 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001387 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001388 return false;
1389 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001390 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001391 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001392 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001393 }
1394 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001395 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001396 }
1397}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001398Expr* Expr::IgnoreParens() {
1399 Expr* E = this;
1400 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1401 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001403 return E;
1404}
1405
Chris Lattner56f34942008-02-13 01:02:39 +00001406/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1407/// or CastExprs or ImplicitCastExprs, returning their operand.
1408Expr *Expr::IgnoreParenCasts() {
1409 Expr *E = this;
1410 while (true) {
1411 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1412 E = P->getSubExpr();
1413 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1414 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001415 else
1416 return E;
1417 }
1418}
1419
Chris Lattnerecdd8412009-03-13 17:28:01 +00001420/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1421/// value (including ptr->int casts of the same size). Strip off any
1422/// ParenExpr or CastExprs, returning their operand.
1423Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1424 Expr *E = this;
1425 while (true) {
1426 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1427 E = P->getSubExpr();
1428 continue;
1429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Chris Lattnerecdd8412009-03-13 17:28:01 +00001431 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1432 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1433 // ptr<->int casts of the same width. We also ignore all identify casts.
1434 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Chris Lattnerecdd8412009-03-13 17:28:01 +00001436 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1437 E = SE;
1438 continue;
1439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Chris Lattnerecdd8412009-03-13 17:28:01 +00001441 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1442 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1443 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1444 E = SE;
1445 continue;
1446 }
1447 }
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Chris Lattnerecdd8412009-03-13 17:28:01 +00001449 return E;
1450 }
1451}
1452
Douglas Gregor6eef5192009-12-14 19:27:10 +00001453bool Expr::isDefaultArgument() const {
1454 const Expr *E = this;
1455 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1456 E = ICE->getSubExprAsWritten();
1457
1458 return isa<CXXDefaultArgExpr>(E);
1459}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001460
Douglas Gregor898574e2008-12-05 23:32:09 +00001461/// hasAnyTypeDependentArguments - Determines if any of the expressions
1462/// in Exprs is type-dependent.
1463bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1464 for (unsigned I = 0; I < NumExprs; ++I)
1465 if (Exprs[I]->isTypeDependent())
1466 return true;
1467
1468 return false;
1469}
1470
1471/// hasAnyValueDependentArguments - Determines if any of the expressions
1472/// in Exprs is value-dependent.
1473bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1474 for (unsigned I = 0; I < NumExprs; ++I)
1475 if (Exprs[I]->isValueDependent())
1476 return true;
1477
1478 return false;
1479}
1480
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001481bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001482 // This function is attempting whether an expression is an initializer
1483 // which can be evaluated at compile-time. isEvaluatable handles most
1484 // of the cases, but it can't deal with some initializer-specific
1485 // expressions, and it can't deal with aggregates; we deal with those here,
1486 // and fall back to isEvaluatable for the other cases.
1487
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001488 // FIXME: This function assumes the variable being assigned to
1489 // isn't a reference type!
1490
Anders Carlssone8a32b82008-11-24 05:23:59 +00001491 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001492 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001493 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001494 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001495 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001496 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001497 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001498 // This handles gcc's extension that allows global initializers like
1499 // "struct x {int x;} x = (struct x) {};".
1500 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001501 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001502 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001503 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001504 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001505 // FIXME: This doesn't deal with fields with reference types correctly.
1506 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1507 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001508 const InitListExpr *Exp = cast<InitListExpr>(this);
1509 unsigned numInits = Exp->getNumInits();
1510 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001511 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001512 return false;
1513 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001514 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001515 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001516 case ImplicitValueInitExprClass:
1517 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001518 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001519 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001520 case UnaryOperatorClass: {
1521 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1522 if (Exp->getOpcode() == UnaryOperator::Extension)
1523 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1524 break;
1525 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001526 case BinaryOperatorClass: {
1527 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1528 // but this handles the common case.
1529 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1530 if (Exp->getOpcode() == BinaryOperator::Sub &&
1531 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1532 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1533 return true;
1534 break;
1535 }
Chris Lattner81045d82009-04-21 05:19:11 +00001536 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001537 case CStyleCastExprClass:
1538 // Handle casts with a destination that's a struct or union; this
1539 // deals with both the gcc no-op struct cast extension and the
1540 // cast-to-union extension.
1541 if (getType()->isRecordType())
1542 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001543
1544 // Integer->integer casts can be handled here, which is important for
1545 // things like (int)(&&x-&&y). Scary but true.
1546 if (getType()->isIntegerType() &&
1547 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1548 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1549
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001550 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001551 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001552 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001553}
1554
Reid Spencer5f016e22007-07-11 17:01:13 +00001555/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001556/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001557
1558/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1559/// comma, etc
1560///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001561/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1562/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1563/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001564
Eli Friedmane28d7192009-02-26 09:29:13 +00001565// CheckICE - This function does the fundamental ICE checking: the returned
1566// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1567// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001568// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001569// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001570// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001571//
1572// Meanings of Val:
1573// 0: This expression is an ICE if it can be evaluated by Evaluate.
1574// 1: This expression is not an ICE, but if it isn't evaluated, it's
1575// a legal subexpression for an ICE. This return value is used to handle
1576// the comma operator in C99 mode.
1577// 2: This expression is not an ICE, and is not a legal subexpression for one.
1578
1579struct ICEDiag {
1580 unsigned Val;
1581 SourceLocation Loc;
1582
1583 public:
1584 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1585 ICEDiag() : Val(0) {}
1586};
1587
1588ICEDiag NoDiag() { return ICEDiag(); }
1589
Eli Friedman60ce9632009-02-27 04:07:58 +00001590static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1591 Expr::EvalResult EVResult;
1592 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1593 !EVResult.Val.isInt()) {
1594 return ICEDiag(2, E->getLocStart());
1595 }
1596 return NoDiag();
1597}
1598
Eli Friedmane28d7192009-02-26 09:29:13 +00001599static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001600 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001601 if (!E->getType()->isIntegralType()) {
1602 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001603 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001604
1605 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001606#define STMT(Node, Base) case Expr::Node##Class:
1607#define EXPR(Node, Base)
1608#include "clang/AST/StmtNodes.def"
1609 case Expr::PredefinedExprClass:
1610 case Expr::FloatingLiteralClass:
1611 case Expr::ImaginaryLiteralClass:
1612 case Expr::StringLiteralClass:
1613 case Expr::ArraySubscriptExprClass:
1614 case Expr::MemberExprClass:
1615 case Expr::CompoundAssignOperatorClass:
1616 case Expr::CompoundLiteralExprClass:
1617 case Expr::ExtVectorElementExprClass:
1618 case Expr::InitListExprClass:
1619 case Expr::DesignatedInitExprClass:
1620 case Expr::ImplicitValueInitExprClass:
1621 case Expr::ParenListExprClass:
1622 case Expr::VAArgExprClass:
1623 case Expr::AddrLabelExprClass:
1624 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001625 case Expr::CXXMemberCallExprClass:
1626 case Expr::CXXDynamicCastExprClass:
1627 case Expr::CXXTypeidExprClass:
1628 case Expr::CXXNullPtrLiteralExprClass:
1629 case Expr::CXXThisExprClass:
1630 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001631 case Expr::CXXNewExprClass:
1632 case Expr::CXXDeleteExprClass:
1633 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001634 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001635 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001636 case Expr::CXXConstructExprClass:
1637 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001638 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001639 case Expr::CXXExprWithTemporariesClass:
1640 case Expr::CXXTemporaryObjectExprClass:
1641 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001642 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001643 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001644 case Expr::ObjCStringLiteralClass:
1645 case Expr::ObjCEncodeExprClass:
1646 case Expr::ObjCMessageExprClass:
1647 case Expr::ObjCSelectorExprClass:
1648 case Expr::ObjCProtocolExprClass:
1649 case Expr::ObjCIvarRefExprClass:
1650 case Expr::ObjCPropertyRefExprClass:
1651 case Expr::ObjCImplicitSetterGetterRefExprClass:
1652 case Expr::ObjCSuperExprClass:
1653 case Expr::ObjCIsaExprClass:
1654 case Expr::ShuffleVectorExprClass:
1655 case Expr::BlockExprClass:
1656 case Expr::BlockDeclRefExprClass:
1657 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001658 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001659
Douglas Gregor043cad22009-09-11 00:18:58 +00001660 case Expr::GNUNullExprClass:
1661 // GCC considers the GNU __null value to be an integral constant expression.
1662 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001663
Eli Friedmane28d7192009-02-26 09:29:13 +00001664 case Expr::ParenExprClass:
1665 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1666 case Expr::IntegerLiteralClass:
1667 case Expr::CharacterLiteralClass:
1668 case Expr::CXXBoolLiteralExprClass:
1669 case Expr::CXXZeroInitValueExprClass:
1670 case Expr::TypesCompatibleExprClass:
1671 case Expr::UnaryTypeTraitExprClass:
1672 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001673 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001674 case Expr::CXXOperatorCallExprClass: {
1675 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001676 if (CE->isBuiltinCall(Ctx))
1677 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001678 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001679 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001680 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001681 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1682 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001683 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001684 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001685 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1686
1687 // Parameter variables are never constants. Without this check,
1688 // getAnyInitializer() can find a default argument, which leads
1689 // to chaos.
1690 if (isa<ParmVarDecl>(D))
1691 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1692
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001693 // C++ 7.1.5.1p2
1694 // A variable of non-volatile const-qualified integral or enumeration
1695 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001696 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001697 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1698 if (Quals.hasVolatile() || !Quals.hasConst())
1699 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1700
Sebastian Redl31310a22010-02-01 20:16:42 +00001701 // Look for a declaration of this variable that has an initializer.
1702 const VarDecl *ID = 0;
1703 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001704 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001705 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001706 // We have already checked whether this subexpression is an
1707 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001708 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001709 return NoDiag();
1710 else
1711 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1712 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001713
John McCall1f1b3b32010-02-06 01:07:37 +00001714 // It's an ICE whether or not the definition we found is
1715 // out-of-line. See DR 721 and the discussion in Clang PR
1716 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001717
1718 if (Dcl->isCheckingICE()) {
1719 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1720 }
1721
1722 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001723 ICEDiag Result = CheckICE(Init, Ctx);
1724 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001725 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001726 return Result;
1727 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001728 }
1729 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001730 return ICEDiag(2, E->getLocStart());
1731 case Expr::UnaryOperatorClass: {
1732 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001733 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001734 case UnaryOperator::PostInc:
1735 case UnaryOperator::PostDec:
1736 case UnaryOperator::PreInc:
1737 case UnaryOperator::PreDec:
1738 case UnaryOperator::AddrOf:
1739 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001740 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001741
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001743 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001747 case UnaryOperator::Real:
1748 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001749 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001750 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001751 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1752 // Evaluate matches the proposed gcc behavior for cases like
1753 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1754 // compliance: we should warn earlier for offsetof expressions with
1755 // array subscripts that aren't ICEs, and if the array subscripts
1756 // are ICEs, the value of the offsetof must be an integer constant.
1757 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001760 case Expr::SizeOfAlignOfExprClass: {
1761 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1762 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1763 return ICEDiag(2, E->getLocStart());
1764 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001766 case Expr::BinaryOperatorClass: {
1767 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001769 case BinaryOperator::PtrMemD:
1770 case BinaryOperator::PtrMemI:
1771 case BinaryOperator::Assign:
1772 case BinaryOperator::MulAssign:
1773 case BinaryOperator::DivAssign:
1774 case BinaryOperator::RemAssign:
1775 case BinaryOperator::AddAssign:
1776 case BinaryOperator::SubAssign:
1777 case BinaryOperator::ShlAssign:
1778 case BinaryOperator::ShrAssign:
1779 case BinaryOperator::AndAssign:
1780 case BinaryOperator::XorAssign:
1781 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001782 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001787 case BinaryOperator::Add:
1788 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001791 case BinaryOperator::LT:
1792 case BinaryOperator::GT:
1793 case BinaryOperator::LE:
1794 case BinaryOperator::GE:
1795 case BinaryOperator::EQ:
1796 case BinaryOperator::NE:
1797 case BinaryOperator::And:
1798 case BinaryOperator::Xor:
1799 case BinaryOperator::Or:
1800 case BinaryOperator::Comma: {
1801 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1802 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001803 if (Exp->getOpcode() == BinaryOperator::Div ||
1804 Exp->getOpcode() == BinaryOperator::Rem) {
1805 // Evaluate gives an error for undefined Div/Rem, so make sure
1806 // we don't evaluate one.
1807 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1808 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1809 if (REval == 0)
1810 return ICEDiag(1, E->getLocStart());
1811 if (REval.isSigned() && REval.isAllOnesValue()) {
1812 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1813 if (LEval.isMinSignedValue())
1814 return ICEDiag(1, E->getLocStart());
1815 }
1816 }
1817 }
1818 if (Exp->getOpcode() == BinaryOperator::Comma) {
1819 if (Ctx.getLangOptions().C99) {
1820 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1821 // if it isn't evaluated.
1822 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1823 return ICEDiag(1, E->getLocStart());
1824 } else {
1825 // In both C89 and C++, commas in ICEs are illegal.
1826 return ICEDiag(2, E->getLocStart());
1827 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001828 }
1829 if (LHSResult.Val >= RHSResult.Val)
1830 return LHSResult;
1831 return RHSResult;
1832 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001834 case BinaryOperator::LOr: {
1835 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1836 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1837 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1838 // Rare case where the RHS has a comma "side-effect"; we need
1839 // to actually check the condition to see whether the side
1840 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001841 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001842 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001843 return RHSResult;
1844 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001845 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001846
Eli Friedmane28d7192009-02-26 09:29:13 +00001847 if (LHSResult.Val >= RHSResult.Val)
1848 return LHSResult;
1849 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001851 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001853 case Expr::ImplicitCastExprClass:
1854 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001855 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001856 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001857 case Expr::CXXStaticCastExprClass:
1858 case Expr::CXXReinterpretCastExprClass:
1859 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001860 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1861 if (SubExpr->getType()->isIntegralType())
1862 return CheckICE(SubExpr, Ctx);
1863 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1864 return NoDiag();
1865 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001867 case Expr::ConditionalOperatorClass: {
1868 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001869 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001870 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001871 // expression, and it is fully evaluated. This is an important GNU
1872 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001873 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001874 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001875 Expr::EvalResult EVResult;
1876 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1877 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001878 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001879 }
1880 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001881 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001882 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1883 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1884 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1885 if (CondResult.Val == 2)
1886 return CondResult;
1887 if (TrueResult.Val == 2)
1888 return TrueResult;
1889 if (FalseResult.Val == 2)
1890 return FalseResult;
1891 if (CondResult.Val == 1)
1892 return CondResult;
1893 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1894 return NoDiag();
1895 // Rare case where the diagnostics depend on which side is evaluated
1896 // Note that if we get here, CondResult is 0, and at least one of
1897 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001898 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001899 return FalseResult;
1900 }
1901 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001903 case Expr::CXXDefaultArgExprClass:
1904 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001905 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001906 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001907 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001909
Douglas Gregorf2991242009-09-10 23:31:45 +00001910 // Silence a GCC warning
1911 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001912}
Reid Spencer5f016e22007-07-11 17:01:13 +00001913
Eli Friedmane28d7192009-02-26 09:29:13 +00001914bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1915 SourceLocation *Loc, bool isEvaluated) const {
1916 ICEDiag d = CheckICE(this, Ctx);
1917 if (d.Val != 0) {
1918 if (Loc) *Loc = d.Loc;
1919 return false;
1920 }
1921 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001922 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001923 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001924 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1925 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001926 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 return true;
1928}
1929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1931/// integer constant expression with the value zero, or if this is one that is
1932/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001933bool Expr::isNullPointerConstant(ASTContext &Ctx,
1934 NullPointerConstantValueDependence NPC) const {
1935 if (isValueDependent()) {
1936 switch (NPC) {
1937 case NPC_NeverValueDependent:
1938 assert(false && "Unexpected value dependent expression!");
1939 // If the unthinkable happens, fall through to the safest alternative.
1940
1941 case NPC_ValueDependentIsNull:
1942 return isTypeDependent() || getType()->isIntegralType();
1943
1944 case NPC_ValueDependentIsNotNull:
1945 return false;
1946 }
1947 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001948
Sebastian Redl07779722008-10-31 14:43:28 +00001949 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001950 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001951 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001952 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001953 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001954 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001955 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001956 Pointee->isVoidType() && // to void*
1957 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001958 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001959 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001961 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1962 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001963 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001964 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1965 // Accept ((void*)0) as a null pointer constant, as many other
1966 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001967 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001968 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001969 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001970 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001971 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001972 } else if (isa<GNUNullExpr>(this)) {
1973 // The GNU __null extension is always a null pointer constant.
1974 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001975 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001976
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001977 // C++0x nullptr_t is always a null pointer constant.
1978 if (getType()->isNullPtrType())
1979 return true;
1980
Steve Naroffaa58f002008-01-14 16:10:57 +00001981 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001982 if (!getType()->isIntegerType() ||
1983 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001984 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 // If we have an integer constant expression, we need to *evaluate* it and
1987 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001988 llvm::APSInt Result;
1989 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001990}
Steve Naroff31a45842007-07-28 23:10:27 +00001991
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001992FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001993 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001994
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001995 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1996 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1997 E = ICE->getSubExpr()->IgnoreParens();
1998 else
1999 break;
2000 }
2001
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002002 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002003 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002004 if (Field->isBitField())
2005 return Field;
2006
2007 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2008 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2009 return BinOp->getLHS()->getBitField();
2010
2011 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002012}
2013
Anders Carlsson09380262010-01-31 17:18:49 +00002014bool Expr::refersToVectorElement() const {
2015 const Expr *E = this->IgnoreParens();
2016
2017 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2018 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2019 E = ICE->getSubExpr()->IgnoreParens();
2020 else
2021 break;
2022 }
2023
2024 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2025 return ASE->getBase()->getType()->isVectorType();
2026
2027 if (isa<ExtVectorElementExpr>(E))
2028 return true;
2029
2030 return false;
2031}
2032
Chris Lattner2140e902009-02-16 22:14:05 +00002033/// isArrow - Return true if the base expression is a pointer to vector,
2034/// return false if the base expression is a vector.
2035bool ExtVectorElementExpr::isArrow() const {
2036 return getBase()->getType()->isPointerType();
2037}
2038
Nate Begeman213541a2008-04-18 23:10:10 +00002039unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002040 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002041 return VT->getNumElements();
2042 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002043}
2044
Nate Begeman8a997642008-05-09 06:41:27 +00002045/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002046bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002047 // FIXME: Refactor this code to an accessor on the AST node which returns the
2048 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002049 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002050
2051 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002052 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002053 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002054
Nate Begeman190d6a22009-01-18 02:01:21 +00002055 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002056 if (Comp[0] == 's' || Comp[0] == 'S')
2057 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Daniel Dunbar15027422009-10-17 23:53:04 +00002059 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2060 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002061 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002062
Steve Narofffec0b492007-07-30 03:29:09 +00002063 return false;
2064}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002065
Nate Begeman8a997642008-05-09 06:41:27 +00002066/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002067void ExtVectorElementExpr::getEncodedElementAccess(
2068 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002069 llvm::StringRef Comp = Accessor->getName();
2070 if (Comp[0] == 's' || Comp[0] == 'S')
2071 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002073 bool isHi = Comp == "hi";
2074 bool isLo = Comp == "lo";
2075 bool isEven = Comp == "even";
2076 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Nate Begeman8a997642008-05-09 06:41:27 +00002078 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2079 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Nate Begeman8a997642008-05-09 06:41:27 +00002081 if (isHi)
2082 Index = e + i;
2083 else if (isLo)
2084 Index = i;
2085 else if (isEven)
2086 Index = 2 * i;
2087 else if (isOdd)
2088 Index = 2 * i + 1;
2089 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002090 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002091
Nate Begeman3b8d1162008-05-13 21:03:02 +00002092 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002093 }
Nate Begeman8a997642008-05-09 06:41:27 +00002094}
2095
Steve Naroff68d331a2007-09-27 14:38:14 +00002096// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002097ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2098 Selector selInfo,
2099 QualType retType, ObjCMethodDecl *mproto,
2100 SourceLocation LBrac, SourceLocation RBrac,
2101 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002102 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002103 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002104 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002105 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002106 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002107 if (NumArgs) {
2108 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002109 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2110 }
Steve Naroff563477d2007-09-18 23:55:05 +00002111 LBracloc = LBrac;
2112 RBracloc = RBrac;
2113}
2114
Mike Stump1eb44332009-09-09 15:08:12 +00002115// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002116// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002117ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002118 SourceLocation clsNameLoc, Selector selInfo,
2119 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002120 SourceLocation LBrac, SourceLocation RBrac,
2121 Expr **ArgExprs, unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002122 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2123 SelName(selInfo), MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002124 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002125 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002126 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002127 if (NumArgs) {
2128 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002129 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2130 }
Steve Naroff563477d2007-09-18 23:55:05 +00002131 LBracloc = LBrac;
2132 RBracloc = RBrac;
2133}
2134
Mike Stump1eb44332009-09-09 15:08:12 +00002135// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002136ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002137 SourceLocation clsNameLoc, Selector selInfo,
2138 QualType retType,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002139 ObjCMethodDecl *mproto, SourceLocation LBrac,
2140 SourceLocation RBrac, Expr **ArgExprs,
2141 unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002142 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2143 SelName(selInfo), MethodProto(mproto)
2144{
Ted Kremenek4df728e2008-06-24 15:50:53 +00002145 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002146 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002147 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2148 if (NumArgs) {
2149 for (unsigned i = 0; i != NumArgs; ++i)
2150 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2151 }
2152 LBracloc = LBrac;
2153 RBracloc = RBrac;
2154}
2155
2156ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2157 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2158 switch (x & Flags) {
2159 default:
2160 assert(false && "Invalid ObjCMessageExpr.");
2161 case IsInstMeth:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002162 return ClassInfo(0, 0, SourceLocation());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002163 case IsClsMethDeclUnknown:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002164 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002165 case IsClsMethDeclKnown: {
2166 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002167 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002168 }
2169 }
2170}
2171
Chris Lattner0389e6b2009-04-26 00:44:05 +00002172void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorc2350e52010-03-08 16:40:19 +00002173 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner0389e6b2009-04-26 00:44:05 +00002174 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002175 return;
2176 }
2177
2178 if (CI.Decl == 0)
2179 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner0389e6b2009-04-26 00:44:05 +00002180 else
Douglas Gregorc2350e52010-03-08 16:40:19 +00002181 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2182 ClassNameLoc = CI.Loc;
Chris Lattner0389e6b2009-04-26 00:44:05 +00002183}
2184
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002185void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2186 DestroyChildren(C);
2187 if (SubExprs)
2188 C.Deallocate(SubExprs);
2189 this->~ObjCMessageExpr();
2190 C.Deallocate((void*) this);
2191}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002192
Chris Lattner27437ca2007-10-25 00:29:32 +00002193bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002194 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002195}
2196
Nate Begeman888376a2009-08-12 02:28:50 +00002197void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2198 unsigned NumExprs) {
2199 if (SubExprs) C.Deallocate(SubExprs);
2200
2201 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002202 this->NumExprs = NumExprs;
2203 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002204}
Nate Begeman888376a2009-08-12 02:28:50 +00002205
2206void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2207 DestroyChildren(C);
2208 if (SubExprs) C.Deallocate(SubExprs);
2209 this->~ShuffleVectorExpr();
2210 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002211}
2212
Douglas Gregor42602bb2009-08-07 06:08:38 +00002213void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002214 // Override default behavior of traversing children. If this has a type
2215 // operand and the type is a variable-length array, the child iteration
2216 // will iterate over the size expression. However, this expression belongs
2217 // to the type, not to this, so we don't want to delete it.
2218 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002219 if (isArgumentType()) {
2220 this->~SizeOfAlignOfExpr();
2221 C.Deallocate(this);
2222 }
Sebastian Redl05189992008-11-11 17:56:53 +00002223 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002224 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002225}
2226
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002227//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002228// DesignatedInitExpr
2229//===----------------------------------------------------------------------===//
2230
2231IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2232 assert(Kind == FieldDesignator && "Only valid on a field designator");
2233 if (Field.NameOrField & 0x01)
2234 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2235 else
2236 return getField()->getIdentifier();
2237}
2238
Douglas Gregor319d57f2010-01-06 23:17:19 +00002239DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2240 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002241 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002242 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002243 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002244 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002245 unsigned NumIndexExprs,
2246 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002247 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002248 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002249 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2250 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002251 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002252
2253 // Record the initializer itself.
2254 child_iterator Child = child_begin();
2255 *Child++ = Init;
2256
2257 // Copy the designators and their subexpressions, computing
2258 // value-dependence along the way.
2259 unsigned IndexIdx = 0;
2260 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002261 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002262
2263 if (this->Designators[I].isArrayDesignator()) {
2264 // Compute type- and value-dependence.
2265 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002266 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002267 Index->isTypeDependent() || Index->isValueDependent();
2268
2269 // Copy the index expressions into permanent storage.
2270 *Child++ = IndexExprs[IndexIdx++];
2271 } else if (this->Designators[I].isArrayRangeDesignator()) {
2272 // Compute type- and value-dependence.
2273 Expr *Start = IndexExprs[IndexIdx];
2274 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002275 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002276 Start->isTypeDependent() || Start->isValueDependent() ||
2277 End->isTypeDependent() || End->isValueDependent();
2278
2279 // Copy the start/end expressions into permanent storage.
2280 *Child++ = IndexExprs[IndexIdx++];
2281 *Child++ = IndexExprs[IndexIdx++];
2282 }
2283 }
2284
2285 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002286}
2287
Douglas Gregor05c13a32009-01-22 00:58:24 +00002288DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002289DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002290 unsigned NumDesignators,
2291 Expr **IndexExprs, unsigned NumIndexExprs,
2292 SourceLocation ColonOrEqualLoc,
2293 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002294 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002295 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002296 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002297 ColonOrEqualLoc, UsesColonSyntax,
2298 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002299}
2300
Mike Stump1eb44332009-09-09 15:08:12 +00002301DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002302 unsigned NumIndexExprs) {
2303 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2304 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2305 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2306}
2307
Douglas Gregor319d57f2010-01-06 23:17:19 +00002308void DesignatedInitExpr::setDesignators(ASTContext &C,
2309 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002310 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002311 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002312
Douglas Gregor319d57f2010-01-06 23:17:19 +00002313 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002314 NumDesignators = NumDesigs;
2315 for (unsigned I = 0; I != NumDesigs; ++I)
2316 Designators[I] = Desigs[I];
2317}
2318
Douglas Gregor05c13a32009-01-22 00:58:24 +00002319SourceRange DesignatedInitExpr::getSourceRange() const {
2320 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002321 Designator &First =
2322 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002323 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002324 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002325 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2326 else
2327 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2328 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002329 StartLoc =
2330 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002331 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2332}
2333
Douglas Gregor05c13a32009-01-22 00:58:24 +00002334Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2335 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2336 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2337 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002338 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2339 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2340}
2341
2342Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002343 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002344 "Requires array range designator");
2345 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2346 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002347 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2348 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2349}
2350
2351Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002352 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002353 "Requires array range designator");
2354 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2355 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002356 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2357 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2358}
2359
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002360/// \brief Replaces the designator at index @p Idx with the series
2361/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002362void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002363 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002364 const Designator *Last) {
2365 unsigned NumNewDesignators = Last - First;
2366 if (NumNewDesignators == 0) {
2367 std::copy_backward(Designators + Idx + 1,
2368 Designators + NumDesignators,
2369 Designators + Idx);
2370 --NumNewDesignators;
2371 return;
2372 } else if (NumNewDesignators == 1) {
2373 Designators[Idx] = *First;
2374 return;
2375 }
2376
Mike Stump1eb44332009-09-09 15:08:12 +00002377 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002378 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002379 std::copy(Designators, Designators + Idx, NewDesignators);
2380 std::copy(First, Last, NewDesignators + Idx);
2381 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2382 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002383 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002384 Designators = NewDesignators;
2385 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2386}
2387
Douglas Gregor42602bb2009-08-07 06:08:38 +00002388void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002389 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002390 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002391}
2392
Douglas Gregor319d57f2010-01-06 23:17:19 +00002393void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2394 for (unsigned I = 0; I != NumDesignators; ++I)
2395 Designators[I].~Designator();
2396 C.Deallocate(Designators);
2397 Designators = 0;
2398}
2399
Mike Stump1eb44332009-09-09 15:08:12 +00002400ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002401 Expr **exprs, unsigned nexprs,
2402 SourceLocation rparenloc)
2403: Expr(ParenListExprClass, QualType(),
2404 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002405 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002406 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002407
Nate Begeman2ef13e52009-08-10 23:49:36 +00002408 Exprs = new (C) Stmt*[nexprs];
2409 for (unsigned i = 0; i != nexprs; ++i)
2410 Exprs[i] = exprs[i];
2411}
2412
2413void ParenListExpr::DoDestroy(ASTContext& C) {
2414 DestroyChildren(C);
2415 if (Exprs) C.Deallocate(Exprs);
2416 this->~ParenListExpr();
2417 C.Deallocate(this);
2418}
2419
Douglas Gregor05c13a32009-01-22 00:58:24 +00002420//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002421// ExprIterator.
2422//===----------------------------------------------------------------------===//
2423
2424Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2425Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2426Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2427const Expr* ConstExprIterator::operator[](size_t idx) const {
2428 return cast<Expr>(I[idx]);
2429}
2430const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2431const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2432
2433//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002434// Child Iterators for iterating over subexpressions/substatements
2435//===----------------------------------------------------------------------===//
2436
2437// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002438Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2439Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002440
Steve Naroff7779db42007-11-12 14:29:37 +00002441// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002442Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2443Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002444
Steve Naroffe3e9add2008-06-02 23:03:37 +00002445// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002446Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2447Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002448
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002449// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002450Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2451 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002452}
Mike Stump1eb44332009-09-09 15:08:12 +00002453Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2454 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002455}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002456
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002457// ObjCSuperExpr
2458Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2459Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2460
Steve Narofff242b1b2009-07-24 17:54:45 +00002461// ObjCIsaExpr
2462Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2463Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2464
Chris Lattnerd9f69102008-08-10 01:53:14 +00002465// PredefinedExpr
2466Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2467Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002468
2469// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002470Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2471Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002472
2473// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002474Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002475Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002476
2477// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002478Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2479Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002480
Chris Lattner5d661452007-08-26 03:42:43 +00002481// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002482Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2483Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002484
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002485// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002486Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2487Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002488
2489// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002490Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2491Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002492
2493// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002494Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2495Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002496
Sebastian Redl05189992008-11-11 17:56:53 +00002497// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002498Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002499 // If this is of a type and the type is a VLA type (and not a typedef), the
2500 // size expression of the VLA needs to be treated as an executable expression.
2501 // Why isn't this weirdness documented better in StmtIterator?
2502 if (isArgumentType()) {
2503 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2504 getArgumentType().getTypePtr()))
2505 return child_iterator(T);
2506 return child_iterator();
2507 }
Sebastian Redld4575892008-12-03 23:17:54 +00002508 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002509}
Sebastian Redl05189992008-11-11 17:56:53 +00002510Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2511 if (isArgumentType())
2512 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002513 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002514}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002515
2516// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002517Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002518 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002519}
Ted Kremenek1237c672007-08-24 20:06:47 +00002520Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002521 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002522}
2523
2524// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002525Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002526 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002527}
Ted Kremenek1237c672007-08-24 20:06:47 +00002528Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002529 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002530}
Ted Kremenek1237c672007-08-24 20:06:47 +00002531
2532// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002533Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2534Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002535
Nate Begeman213541a2008-04-18 23:10:10 +00002536// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002537Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2538Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002539
2540// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002541Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2542Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002543
Ted Kremenek1237c672007-08-24 20:06:47 +00002544// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002545Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2546Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002547
2548// BinaryOperator
2549Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002550 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002551}
Ted Kremenek1237c672007-08-24 20:06:47 +00002552Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002553 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002554}
2555
2556// ConditionalOperator
2557Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002558 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002559}
Ted Kremenek1237c672007-08-24 20:06:47 +00002560Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002561 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002562}
2563
2564// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002565Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2566Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002567
Ted Kremenek1237c672007-08-24 20:06:47 +00002568// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002569Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2570Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002571
2572// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002573Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2574 return child_iterator();
2575}
2576
2577Stmt::child_iterator TypesCompatibleExpr::child_end() {
2578 return child_iterator();
2579}
Ted Kremenek1237c672007-08-24 20:06:47 +00002580
2581// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002582Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2583Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002584
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002585// GNUNullExpr
2586Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2587Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2588
Eli Friedmand38617c2008-05-14 19:38:39 +00002589// ShuffleVectorExpr
2590Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002591 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002592}
2593Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002594 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002595}
2596
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002597// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002598Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2599Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002600
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002601// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002602Stmt::child_iterator InitListExpr::child_begin() {
2603 return InitExprs.size() ? &InitExprs[0] : 0;
2604}
2605Stmt::child_iterator InitListExpr::child_end() {
2606 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2607}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002608
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002609// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002610Stmt::child_iterator DesignatedInitExpr::child_begin() {
2611 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2612 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002613 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2614}
2615Stmt::child_iterator DesignatedInitExpr::child_end() {
2616 return child_iterator(&*child_begin() + NumSubExprs);
2617}
2618
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002619// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002620Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2621 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002622}
2623
Mike Stump1eb44332009-09-09 15:08:12 +00002624Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2625 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002626}
2627
Nate Begeman2ef13e52009-08-10 23:49:36 +00002628// ParenListExpr
2629Stmt::child_iterator ParenListExpr::child_begin() {
2630 return &Exprs[0];
2631}
2632Stmt::child_iterator ParenListExpr::child_end() {
2633 return &Exprs[0]+NumExprs;
2634}
2635
Ted Kremenek1237c672007-08-24 20:06:47 +00002636// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002637Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002638 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002639}
2640Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002641 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002642}
Ted Kremenek1237c672007-08-24 20:06:47 +00002643
2644// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002645Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2646Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002647
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002648// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002649Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002650 return child_iterator();
2651}
2652Stmt::child_iterator ObjCSelectorExpr::child_end() {
2653 return child_iterator();
2654}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002655
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002656// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002657Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2658 return child_iterator();
2659}
2660Stmt::child_iterator ObjCProtocolExpr::child_end() {
2661 return child_iterator();
2662}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002663
Steve Naroff563477d2007-09-18 23:55:05 +00002664// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002665Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002666 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002667}
2668Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002669 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002670}
2671
Steve Naroff4eb206b2008-09-03 18:15:37 +00002672// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002673Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2674Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002675
Ted Kremenek9da13f92008-09-26 23:24:14 +00002676Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2677Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }