blob: 73a650f346448f3e87c16a7f7ab3bb89c838eb83 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Primary Expressions.
32//===----------------------------------------------------------------------===//
33
John McCalld5532b62009-11-23 01:53:49 +000034void ExplicitTemplateArgumentList::initializeFrom(
35 const TemplateArgumentListInfo &Info) {
36 LAngleLoc = Info.getLAngleLoc();
37 RAngleLoc = Info.getRAngleLoc();
38 NumTemplateArgs = Info.size();
39
40 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
41 for (unsigned i = 0; i != NumTemplateArgs; ++i)
42 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
43}
44
45void ExplicitTemplateArgumentList::copyInto(
46 TemplateArgumentListInfo &Info) const {
47 Info.setLAngleLoc(LAngleLoc);
48 Info.setRAngleLoc(RAngleLoc);
49 for (unsigned I = 0; I != NumTemplateArgs; ++I)
50 Info.addArgument(getTemplateArgs()[I]);
51}
52
53std::size_t ExplicitTemplateArgumentList::sizeFor(
54 const TemplateArgumentListInfo &Info) {
55 return sizeof(ExplicitTemplateArgumentList) +
56 sizeof(TemplateArgumentLoc) * Info.size();
57}
58
Douglas Gregor0da76df2009-11-23 11:41:28 +000059void DeclRefExpr::computeDependence() {
60 TypeDependent = false;
61 ValueDependent = false;
62
63 NamedDecl *D = getDecl();
64
65 // (TD) C++ [temp.dep.expr]p3:
66 // An id-expression is type-dependent if it contains:
67 //
68 // and
69 //
70 // (VD) C++ [temp.dep.constexpr]p2:
71 // An identifier is value-dependent if it is:
72
73 // (TD) - an identifier that was declared with dependent type
74 // (VD) - a name declared with a dependent type,
75 if (getType()->isDependentType()) {
76 TypeDependent = true;
77 ValueDependent = true;
78 }
79 // (TD) - a conversion-function-id that specifies a dependent type
80 else if (D->getDeclName().getNameKind()
81 == DeclarationName::CXXConversionFunctionName &&
82 D->getDeclName().getCXXNameType()->isDependentType()) {
83 TypeDependent = true;
84 ValueDependent = true;
85 }
86 // (TD) - a template-id that is dependent,
87 else if (hasExplicitTemplateArgumentList() &&
88 TemplateSpecializationType::anyDependentTemplateArguments(
89 getTemplateArgs(),
90 getNumTemplateArgs())) {
91 TypeDependent = true;
92 ValueDependent = true;
93 }
94 // (VD) - the name of a non-type template parameter,
95 else if (isa<NonTypeTemplateParmDecl>(D))
96 ValueDependent = true;
97 // (VD) - a constant with integral or enumeration type and is
98 // initialized with an expression that is value-dependent.
99 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
100 if (Var->getType()->isIntegralType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000101 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000102 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000103 if (Init->isValueDependent())
104 ValueDependent = true;
105 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000106 }
107 // (TD) - a nested-name-specifier or a qualified-id that names a
108 // member of an unknown specialization.
109 // (handled by DependentScopeDeclRefExpr)
110}
111
Douglas Gregora2813ce2009-10-23 18:54:35 +0000112DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
113 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000114 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000115 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000116 QualType T)
117 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000118 DecoratedD(D,
119 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000120 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000121 Loc(NameLoc) {
122 if (Qualifier) {
123 NameQualifier *NQ = getNameQualifier();
124 NQ->NNS = Qualifier;
125 NQ->Range = QualifierRange;
126 }
127
John McCalld5532b62009-11-23 01:53:49 +0000128 if (TemplateArgs)
129 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000130
131 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000132}
133
134DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
135 NestedNameSpecifier *Qualifier,
136 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000137 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000138 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000139 QualType T,
140 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000141 std::size_t Size = sizeof(DeclRefExpr);
142 if (Qualifier != 0)
143 Size += sizeof(NameQualifier);
144
John McCalld5532b62009-11-23 01:53:49 +0000145 if (TemplateArgs)
146 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000147
148 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
149 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000150 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000151}
152
153SourceRange DeclRefExpr::getSourceRange() const {
154 // FIXME: Does not handle multi-token names well, e.g., operator[].
155 SourceRange R(Loc);
156
157 if (hasQualifier())
158 R.setBegin(getQualifierRange().getBegin());
159 if (hasExplicitTemplateArgumentList())
160 R.setEnd(getRAngleLoc());
161 return R;
162}
163
Anders Carlsson3a082d82009-09-08 18:24:21 +0000164// FIXME: Maybe this should use DeclPrinter with a special "print predefined
165// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000166std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
167 ASTContext &Context = CurrentDecl->getASTContext();
168
Anders Carlsson3a082d82009-09-08 18:24:21 +0000169 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000170 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000171 return FD->getNameAsString();
172
173 llvm::SmallString<256> Name;
174 llvm::raw_svector_ostream Out(Name);
175
176 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000177 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000178 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000179 if (MD->isStatic())
180 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000181 }
182
183 PrintingPolicy Policy(Context.getLangOptions());
184 Policy.SuppressTagKind = true;
185
186 std::string Proto = FD->getQualifiedNameAsString(Policy);
187
John McCall183700f2009-09-21 23:43:11 +0000188 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000189 const FunctionProtoType *FT = 0;
190 if (FD->hasWrittenPrototype())
191 FT = dyn_cast<FunctionProtoType>(AFT);
192
193 Proto += "(";
194 if (FT) {
195 llvm::raw_string_ostream POut(Proto);
196 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
197 if (i) POut << ", ";
198 std::string Param;
199 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
200 POut << Param;
201 }
202
203 if (FT->isVariadic()) {
204 if (FD->getNumParams()) POut << ", ";
205 POut << "...";
206 }
207 }
208 Proto += ")";
209
Sam Weinig4eadcc52009-12-27 01:38:20 +0000210 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
211 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
212 if (ThisQuals.hasConst())
213 Proto += " const";
214 if (ThisQuals.hasVolatile())
215 Proto += " volatile";
216 }
217
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000218 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
219 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000220
221 Out << Proto;
222
223 Out.flush();
224 return Name.str().str();
225 }
226 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
227 llvm::SmallString<256> Name;
228 llvm::raw_svector_ostream Out(Name);
229 Out << (MD->isInstanceMethod() ? '-' : '+');
230 Out << '[';
231 Out << MD->getClassInterface()->getNameAsString();
232 if (const ObjCCategoryImplDecl *CID =
233 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
234 Out << '(';
235 Out << CID->getNameAsString();
236 Out << ')';
237 }
238 Out << ' ';
239 Out << MD->getSelector().getAsString();
240 Out << ']';
241
242 Out.flush();
243 return Name.str().str();
244 }
245 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
246 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
247 return "top level";
248 }
249 return "";
250}
251
Chris Lattnerda8249e2008-06-07 22:13:43 +0000252/// getValueAsApproximateDouble - This returns the value as an inaccurate
253/// double. Note that this may cause loss of precision, but is useful for
254/// debugging dumps, etc.
255double FloatingLiteral::getValueAsApproximateDouble() const {
256 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000257 bool ignored;
258 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
259 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000260 return V.convertToDouble();
261}
262
Chris Lattner2085fd62009-02-18 06:40:38 +0000263StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
264 unsigned ByteLength, bool Wide,
265 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000266 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000267 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000268 // Allocate enough space for the StringLiteral plus an array of locations for
269 // any concatenated string tokens.
270 void *Mem = C.Allocate(sizeof(StringLiteral)+
271 sizeof(SourceLocation)*(NumStrs-1),
272 llvm::alignof<StringLiteral>());
273 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000276 char *AStrData = new (C, 1) char[ByteLength];
277 memcpy(AStrData, StrData, ByteLength);
278 SL->StrData = AStrData;
279 SL->ByteLength = ByteLength;
280 SL->IsWide = Wide;
281 SL->TokLocs[0] = Loc[0];
282 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Chris Lattner726e1682009-02-18 05:49:11 +0000284 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000285 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
286 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000287}
288
Douglas Gregor673ecd62009-04-15 16:35:07 +0000289StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
290 void *Mem = C.Allocate(sizeof(StringLiteral)+
291 sizeof(SourceLocation)*(NumStrs-1),
292 llvm::alignof<StringLiteral>());
293 StringLiteral *SL = new (Mem) StringLiteral(QualType());
294 SL->StrData = 0;
295 SL->ByteLength = 0;
296 SL->NumConcatenated = NumStrs;
297 return SL;
298}
299
Douglas Gregor42602bb2009-08-07 06:08:38 +0000300void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000301 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000302 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Daniel Dunbarb6480232009-09-22 03:27:33 +0000305void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000306 if (StrData)
307 C.Deallocate(const_cast<char*>(StrData));
308
Daniel Dunbarb6480232009-09-22 03:27:33 +0000309 char *AStrData = new (C, 1) char[Str.size()];
310 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000311 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000312 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000313}
314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
316/// corresponds to, e.g. "sizeof" or "[pre]++".
317const char *UnaryOperator::getOpcodeStr(Opcode Op) {
318 switch (Op) {
319 default: assert(0 && "Unknown unary operator");
320 case PostInc: return "++";
321 case PostDec: return "--";
322 case PreInc: return "++";
323 case PreDec: return "--";
324 case AddrOf: return "&";
325 case Deref: return "*";
326 case Plus: return "+";
327 case Minus: return "-";
328 case Not: return "~";
329 case LNot: return "!";
330 case Real: return "__real";
331 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000333 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
335}
336
Mike Stump1eb44332009-09-09 15:08:12 +0000337UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000338UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
339 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000340 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000341 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
342 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
343 case OO_Amp: return AddrOf;
344 case OO_Star: return Deref;
345 case OO_Plus: return Plus;
346 case OO_Minus: return Minus;
347 case OO_Tilde: return Not;
348 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000349 }
350}
351
352OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
353 switch (Opc) {
354 case PostInc: case PreInc: return OO_PlusPlus;
355 case PostDec: case PreDec: return OO_MinusMinus;
356 case AddrOf: return OO_Amp;
357 case Deref: return OO_Star;
358 case Plus: return OO_Plus;
359 case Minus: return OO_Minus;
360 case Not: return OO_Tilde;
361 case LNot: return OO_Exclaim;
362 default: return OO_None;
363 }
364}
365
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367//===----------------------------------------------------------------------===//
368// Postfix Operators.
369//===----------------------------------------------------------------------===//
370
Ted Kremenek668bf912009-02-09 20:51:47 +0000371CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000372 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000373 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000374 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000375 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000376 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek668bf912009-02-09 20:51:47 +0000378 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000379 SubExprs[FN] = fn;
380 for (unsigned i = 0; i != numargs; ++i)
381 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000382
Douglas Gregorb4609802008-11-14 16:09:21 +0000383 RParenLoc = rparenloc;
384}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000385
Ted Kremenek668bf912009-02-09 20:51:47 +0000386CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
387 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000388 : Expr(CallExprClass, t,
389 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000390 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000391 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000392
393 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000394 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000396 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 RParenLoc = rparenloc;
399}
400
Mike Stump1eb44332009-09-09 15:08:12 +0000401CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
402 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000403 SubExprs = new (C) Stmt*[1];
404}
405
Douglas Gregor42602bb2009-08-07 06:08:38 +0000406void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000407 DestroyChildren(C);
408 if (SubExprs) C.Deallocate(SubExprs);
409 this->~CallExpr();
410 C.Deallocate(this);
411}
412
Nuno Lopesd20254f2009-12-20 23:11:08 +0000413Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000414 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000415 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000416 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000417 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
418 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000419
420 return 0;
421}
422
Nuno Lopesd20254f2009-12-20 23:11:08 +0000423FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000424 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000425}
426
Chris Lattnerd18b3292007-12-28 05:25:02 +0000427/// setNumArgs - This changes the number of arguments present in this call.
428/// Any orphaned expressions are deleted by this, and any new operands are set
429/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000430void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000431 // No change, just return.
432 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnerd18b3292007-12-28 05:25:02 +0000434 // If shrinking # arguments, just delete the extras and forgot them.
435 if (NumArgs < getNumArgs()) {
436 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000437 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438 this->NumArgs = NumArgs;
439 return;
440 }
441
442 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000443 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000444 // Copy over args.
445 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
446 NewSubExprs[i] = SubExprs[i];
447 // Null out new args.
448 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
449 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Douglas Gregor88c9a462009-04-17 21:46:47 +0000451 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000452 SubExprs = NewSubExprs;
453 this->NumArgs = NumArgs;
454}
455
Chris Lattnercb888962008-10-06 05:00:53 +0000456/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
457/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000458unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000459 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000461 // ImplicitCastExpr.
462 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
463 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000464 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000466 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
467 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000468 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anders Carlssonbcba2012008-01-31 02:13:57 +0000470 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
471 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000472 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000474 if (!FDecl->getIdentifier())
475 return 0;
476
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000477 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000478}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000479
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000480QualType CallExpr::getCallReturnType() const {
481 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000482 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000483 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000484 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000485 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall183700f2009-09-21 23:43:11 +0000487 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000488 return FnType->getResultType();
489}
Chris Lattnercb888962008-10-06 05:00:53 +0000490
Mike Stump1eb44332009-09-09 15:08:12 +0000491MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000492 SourceRange qualrange, ValueDecl *memberdecl,
John McCalld5532b62009-11-23 01:53:49 +0000493 SourceLocation l, const TemplateArgumentListInfo *targs,
494 QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000495 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000496 base->isTypeDependent() || (qual && qual->isDependent()),
497 base->isValueDependent() || (qual && qual->isDependent())),
498 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCalld5532b62009-11-23 01:53:49 +0000499 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000500 // Initialize the qualifier, if any.
501 if (HasQualifier) {
502 NameQualifier *NQ = getMemberQualifier();
503 NQ->NNS = qual;
504 NQ->Range = qualrange;
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000507 // Initialize the explicit template argument list, if any.
John McCalld5532b62009-11-23 01:53:49 +0000508 if (targs)
509 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
513 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000514 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000515 ValueDecl *memberdecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000517 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000518 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000519 std::size_t Size = sizeof(MemberExpr);
520 if (qual != 0)
521 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
John McCalld5532b62009-11-23 01:53:49 +0000523 if (targs)
524 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000526 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000527 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCalld5532b62009-11-23 01:53:49 +0000528 targs, ty);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000529}
530
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000531const char *CastExpr::getCastKindName() const {
532 switch (getCastKind()) {
533 case CastExpr::CK_Unknown:
534 return "Unknown";
535 case CastExpr::CK_BitCast:
536 return "BitCast";
537 case CastExpr::CK_NoOp:
538 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000539 case CastExpr::CK_BaseToDerived:
540 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000541 case CastExpr::CK_DerivedToBase:
542 return "DerivedToBase";
543 case CastExpr::CK_Dynamic:
544 return "Dynamic";
545 case CastExpr::CK_ToUnion:
546 return "ToUnion";
547 case CastExpr::CK_ArrayToPointerDecay:
548 return "ArrayToPointerDecay";
549 case CastExpr::CK_FunctionToPointerDecay:
550 return "FunctionToPointerDecay";
551 case CastExpr::CK_NullToMemberPointer:
552 return "NullToMemberPointer";
553 case CastExpr::CK_BaseToDerivedMemberPointer:
554 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000555 case CastExpr::CK_DerivedToBaseMemberPointer:
556 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000557 case CastExpr::CK_UserDefinedConversion:
558 return "UserDefinedConversion";
559 case CastExpr::CK_ConstructorConversion:
560 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000561 case CastExpr::CK_IntegralToPointer:
562 return "IntegralToPointer";
563 case CastExpr::CK_PointerToIntegral:
564 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000565 case CastExpr::CK_ToVoid:
566 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000567 case CastExpr::CK_VectorSplat:
568 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000569 case CastExpr::CK_IntegralCast:
570 return "IntegralCast";
571 case CastExpr::CK_IntegralToFloating:
572 return "IntegralToFloating";
573 case CastExpr::CK_FloatingToIntegral:
574 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000575 case CastExpr::CK_FloatingCast:
576 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000577 case CastExpr::CK_MemberPointerToBoolean:
578 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000579 case CastExpr::CK_AnyPointerToObjCPointerCast:
580 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000581 case CastExpr::CK_AnyPointerToBlockPointerCast:
582 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000585 assert(0 && "Unhandled cast kind!");
586 return 0;
587}
588
Douglas Gregor6eef5192009-12-14 19:27:10 +0000589Expr *CastExpr::getSubExprAsWritten() {
590 Expr *SubExpr = 0;
591 CastExpr *E = this;
592 do {
593 SubExpr = E->getSubExpr();
594
595 // Skip any temporary bindings; they're implicit.
596 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
597 SubExpr = Binder->getSubExpr();
598
599 // Conversions by constructor and conversion functions have a
600 // subexpression describing the call; strip it off.
601 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
602 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
603 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
604 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
605
606 // If the subexpression we're left with is an implicit cast, look
607 // through that, too.
608 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
609
610 return SubExpr;
611}
612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
614/// corresponds to, e.g. "<<=".
615const char *BinaryOperator::getOpcodeStr(Opcode Op) {
616 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000617 case PtrMemD: return ".*";
618 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 case Mul: return "*";
620 case Div: return "/";
621 case Rem: return "%";
622 case Add: return "+";
623 case Sub: return "-";
624 case Shl: return "<<";
625 case Shr: return ">>";
626 case LT: return "<";
627 case GT: return ">";
628 case LE: return "<=";
629 case GE: return ">=";
630 case EQ: return "==";
631 case NE: return "!=";
632 case And: return "&";
633 case Xor: return "^";
634 case Or: return "|";
635 case LAnd: return "&&";
636 case LOr: return "||";
637 case Assign: return "=";
638 case MulAssign: return "*=";
639 case DivAssign: return "/=";
640 case RemAssign: return "%=";
641 case AddAssign: return "+=";
642 case SubAssign: return "-=";
643 case ShlAssign: return "<<=";
644 case ShrAssign: return ">>=";
645 case AndAssign: return "&=";
646 case XorAssign: return "^=";
647 case OrAssign: return "|=";
648 case Comma: return ",";
649 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000650
651 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000655BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
656 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000657 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000658 case OO_Plus: return Add;
659 case OO_Minus: return Sub;
660 case OO_Star: return Mul;
661 case OO_Slash: return Div;
662 case OO_Percent: return Rem;
663 case OO_Caret: return Xor;
664 case OO_Amp: return And;
665 case OO_Pipe: return Or;
666 case OO_Equal: return Assign;
667 case OO_Less: return LT;
668 case OO_Greater: return GT;
669 case OO_PlusEqual: return AddAssign;
670 case OO_MinusEqual: return SubAssign;
671 case OO_StarEqual: return MulAssign;
672 case OO_SlashEqual: return DivAssign;
673 case OO_PercentEqual: return RemAssign;
674 case OO_CaretEqual: return XorAssign;
675 case OO_AmpEqual: return AndAssign;
676 case OO_PipeEqual: return OrAssign;
677 case OO_LessLess: return Shl;
678 case OO_GreaterGreater: return Shr;
679 case OO_LessLessEqual: return ShlAssign;
680 case OO_GreaterGreaterEqual: return ShrAssign;
681 case OO_EqualEqual: return EQ;
682 case OO_ExclaimEqual: return NE;
683 case OO_LessEqual: return LE;
684 case OO_GreaterEqual: return GE;
685 case OO_AmpAmp: return LAnd;
686 case OO_PipePipe: return LOr;
687 case OO_Comma: return Comma;
688 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000689 }
690}
691
692OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
693 static const OverloadedOperatorKind OverOps[] = {
694 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
695 OO_Star, OO_Slash, OO_Percent,
696 OO_Plus, OO_Minus,
697 OO_LessLess, OO_GreaterGreater,
698 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
699 OO_EqualEqual, OO_ExclaimEqual,
700 OO_Amp,
701 OO_Caret,
702 OO_Pipe,
703 OO_AmpAmp,
704 OO_PipePipe,
705 OO_Equal, OO_StarEqual,
706 OO_SlashEqual, OO_PercentEqual,
707 OO_PlusEqual, OO_MinusEqual,
708 OO_LessLessEqual, OO_GreaterGreaterEqual,
709 OO_AmpEqual, OO_CaretEqual,
710 OO_PipeEqual,
711 OO_Comma
712 };
713 return OverOps[Opc];
714}
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000717 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000718 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000719 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000720 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor73460a32009-11-19 23:25:22 +0000721 UnionFieldInit(0), HadArrayRangeDesignator(false)
722{
723 for (unsigned I = 0; I != numInits; ++I) {
724 if (initExprs[I]->isTypeDependent())
725 TypeDependent = true;
726 if (initExprs[I]->isValueDependent())
727 ValueDependent = true;
728 }
729
Chris Lattner418f6c72008-10-26 23:43:26 +0000730 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000731}
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Douglas Gregorfa219202009-03-20 23:58:33 +0000733void InitListExpr::reserveInits(unsigned NumInits) {
734 if (NumInits > InitExprs.size())
735 InitExprs.reserve(NumInits);
736}
737
Douglas Gregor4c678342009-01-28 21:54:33 +0000738void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000739 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000740 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000741 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000742 InitExprs.resize(NumInits, 0);
743}
744
745Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
746 if (Init >= InitExprs.size()) {
747 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
748 InitExprs.back() = expr;
749 return 0;
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor4c678342009-01-28 21:54:33 +0000752 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
753 InitExprs[Init] = expr;
754 return Result;
755}
756
Steve Naroffbfdcae62008-09-04 15:31:07 +0000757/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000758///
759const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000760 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000761 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000762}
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764SourceLocation BlockExpr::getCaretLocation() const {
765 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000766}
Mike Stump1eb44332009-09-09 15:08:12 +0000767const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000768 return TheBlock->getBody();
769}
Mike Stump1eb44332009-09-09 15:08:12 +0000770Stmt *BlockExpr::getBody() {
771 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000772}
Steve Naroff56ee6892008-10-08 17:01:13 +0000773
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775//===----------------------------------------------------------------------===//
776// Generic Expression Routines
777//===----------------------------------------------------------------------===//
778
Chris Lattner026dc962009-02-14 07:37:35 +0000779/// isUnusedResultAWarning - Return true if this immediate expression should
780/// be warned about if the result is unused. If so, fill in Loc and Ranges
781/// with location to warn on and the source range[s] to report with the
782/// warning.
783bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000784 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000785 // Don't warn if the expr is type dependent. The type could end up
786 // instantiating to void.
787 if (isTypeDependent())
788 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 switch (getStmtClass()) {
791 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000792 Loc = getExprLoc();
793 R1 = getSourceRange();
794 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000796 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000797 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 case UnaryOperatorClass: {
799 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000802 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 case UnaryOperator::PostInc:
804 case UnaryOperator::PostDec:
805 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000806 case UnaryOperator::PreDec: // ++/--
807 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 case UnaryOperator::Deref:
809 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000810 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000811 return false;
812 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 case UnaryOperator::Real:
814 case UnaryOperator::Imag:
815 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000816 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
817 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000818 return false;
819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000821 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 }
Chris Lattner026dc962009-02-14 07:37:35 +0000823 Loc = UO->getOperatorLoc();
824 R1 = UO->getSubExpr()->getSourceRange();
825 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000827 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000828 const BinaryOperator *BO = cast<BinaryOperator>(this);
829 // Consider comma to have side effects if the LHS or RHS does.
830 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000831 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
832 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattner026dc962009-02-14 07:37:35 +0000834 if (BO->isAssignmentOp())
835 return false;
836 Loc = BO->getOperatorLoc();
837 R1 = BO->getLHS()->getSourceRange();
838 R2 = BO->getRHS()->getSourceRange();
839 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000840 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000841 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000842 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000843
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000844 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000845 // The condition must be evaluated, but if either the LHS or RHS is a
846 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000847 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000848 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000849 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000850 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000851 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000852 }
853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000855 // If the base pointer or element is to a volatile pointer/field, accessing
856 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000857 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000858 return false;
859 Loc = cast<MemberExpr>(this)->getMemberLoc();
860 R1 = SourceRange(Loc, Loc);
861 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
862 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 case ArraySubscriptExprClass:
865 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000866 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000867 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000868 return false;
869 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
870 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
871 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
872 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000875 case CXXOperatorCallExprClass:
876 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000877 // If this is a direct call, get the callee.
878 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000879 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000880 // If the callee has attribute pure, const, or warn_unused_result, warn
881 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000882 //
883 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
884 // updated to match for QoI.
885 if (FD->getAttr<WarnUnusedResultAttr>() ||
886 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
887 Loc = CE->getCallee()->getLocStart();
888 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000890 if (unsigned NumArgs = CE->getNumArgs())
891 R2 = SourceRange(CE->getArg(0)->getLocStart(),
892 CE->getArg(NumArgs-1)->getLocEnd());
893 return true;
894 }
Chris Lattner026dc962009-02-14 07:37:35 +0000895 }
896 return false;
897 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000898
899 case CXXTemporaryObjectExprClass:
900 case CXXConstructExprClass:
901 return false;
902
Chris Lattnera9c01022007-09-26 22:06:30 +0000903 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000904 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000906 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000907#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000908 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000909 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000910 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000911 Loc = Ref->getLocation();
912 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
913 if (Ref->getBase())
914 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000915#else
916 Loc = getExprLoc();
917 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000918#endif
919 return true;
920 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000921 case StmtExprClass: {
922 // Statement exprs don't logically have side effects themselves, but are
923 // sometimes used in macros in ways that give them a type that is unused.
924 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
925 // however, if the result of the stmt expr is dead, we don't want to emit a
926 // warning.
927 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
928 if (!CS->body_empty())
929 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000930 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner026dc962009-02-14 07:37:35 +0000932 Loc = cast<StmtExpr>(this)->getLParenLoc();
933 R1 = getSourceRange();
934 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000935 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000936 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000937 // If this is an explicit cast to void, allow it. People do this when they
938 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000939 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000940 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000941 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
942 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
943 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000944 case CXXFunctionalCastExprClass: {
945 const CastExpr *CE = cast<CastExpr>(this);
946
947 // If this is a cast to void or a constructor conversion, check the operand.
948 // Otherwise, the result of the cast is unused.
949 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
950 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000951 return (cast<CastExpr>(this)->getSubExpr()
952 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000953 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
954 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
955 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Eli Friedman4be1f472008-05-19 21:24:43 +0000958 case ImplicitCastExprClass:
959 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000960 return (cast<ImplicitCastExpr>(this)
961 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000962
Chris Lattner04421082008-04-08 04:40:51 +0000963 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000964 return (cast<CXXDefaultArgExpr>(this)
965 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966
967 case CXXNewExprClass:
968 // FIXME: In theory, there might be new expressions that don't have side
969 // effects (e.g. a placement new with an uninitialized POD).
970 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000971 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000972 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000973 return (cast<CXXBindTemporaryExpr>(this)
974 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000975 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000976 return (cast<CXXExprWithTemporaries>(this)
977 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000978 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000979}
980
Douglas Gregorba7e2102008-10-22 15:04:37 +0000981/// DeclCanBeLvalue - Determine whether the given declaration can be
982/// an lvalue. This is a helper routine for isLvalue.
983static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000984 // C++ [temp.param]p6:
985 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000987 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
988 return NTTParm->getType()->isReferenceType();
989
Douglas Gregor44b43212008-12-11 16:49:14 +0000990 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000991 // C++ 3.10p2: An lvalue refers to an object or function.
992 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +0000993 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000994}
995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
997/// incomplete type other than void. Nonarray expressions that can be lvalues:
998/// - name, where name must be a variable
999/// - e[i]
1000/// - (e), where e must be an lvalue
1001/// - e.name, where e must be an lvalue
1002/// - e->name
1003/// - *e, the type of e cannot be a function type
1004/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001005/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001006/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001007///
Chris Lattner28be73f2008-07-26 21:30:36 +00001008Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001009 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1010
1011 isLvalueResult Res = isLvalueInternal(Ctx);
1012 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1013 return Res;
1014
Douglas Gregor98cd5992008-10-21 23:43:52 +00001015 // first, check the type (C99 6.3.2.1). Expressions with function
1016 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001017 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 return LV_NotObjectType;
1019
Steve Naroffacb818a2008-02-10 01:39:04 +00001020 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001021 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001022 return LV_IncompleteVoidType;
1023
Eli Friedman53202852009-05-03 22:36:05 +00001024 return LV_Valid;
1025}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001026
Eli Friedman53202852009-05-03 22:36:05 +00001027// Check whether the expression can be sanely treated like an l-value
1028Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001030 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001031 case StringLiteralClass: // C99 6.5.1p4
1032 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001033 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1035 // For vectors, make sure base is an lvalue (i.e. not a function call).
1036 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001037 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001039 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001040 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1041 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 return LV_Valid;
1043 break;
Chris Lattner41110242008-06-17 18:05:57 +00001044 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001045 case BlockDeclRefExprClass: {
1046 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001047 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001048 return LV_Valid;
1049 break;
1050 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001051 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001053 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1054 NamedDecl *Member = m->getMemberDecl();
1055 // C++ [expr.ref]p4:
1056 // If E2 is declared to have type "reference to T", then E1.E2
1057 // is an lvalue.
1058 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1059 if (Value->getType()->isReferenceType())
1060 return LV_Valid;
1061
1062 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001063 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001064 return LV_Valid;
1065
1066 // -- If E2 is a non-static data member [...]. If E1 is an
1067 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001068 if (isa<FieldDecl>(Member)) {
1069 if (m->isArrow())
1070 return LV_Valid;
1071 Expr *BaseExp = m->getBase();
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001072 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass)
1073 return LV_SubObjCPropertySetting;
1074 return
1075 (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ?
1076 LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001077 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001078
1079 // -- If it refers to a static member function [...], then
1080 // E1.E2 is an lvalue.
1081 // -- Otherwise, if E1.E2 refers to a non-static member
1082 // function [...], then E1.E2 is not an lvalue.
1083 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1084 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1085
1086 // -- If E2 is a member enumerator [...], the expression E1.E2
1087 // is not an lvalue.
1088 if (isa<EnumConstantDecl>(Member))
1089 return LV_InvalidExpression;
1090
1091 // Not an lvalue.
1092 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001093 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001094
Douglas Gregor86f19402008-12-20 23:49:58 +00001095 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001096 if (m->isArrow())
1097 return LV_Valid;
1098 Expr *BaseExp = m->getBase();
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001099 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass)
1100 return LV_SubObjCPropertySetting;
1101 return
1102 (BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass) ?
1103 LV_SubObjCPropertyGetterSetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001104 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001105 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001107 return LV_Valid; // C99 6.5.3p4
1108
1109 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001110 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1111 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001112 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001113
1114 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1115 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1116 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1117 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001119 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001120 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001121 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001122 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001123 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001124 case BinaryOperatorClass:
1125 case CompoundAssignOperatorClass: {
1126 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001127
1128 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1129 BinOp->getOpcode() == BinaryOperator::Comma)
1130 return BinOp->getRHS()->isLvalue(Ctx);
1131
Sebastian Redl22460502009-02-07 00:15:38 +00001132 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001133 // The result of a .* expression is an lvalue only if its first operand is
1134 // an lvalue and its second operand is a pointer to data member.
1135 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001136 !BinOp->getType()->isFunctionType())
1137 return BinOp->getLHS()->isLvalue(Ctx);
1138
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001139 // The result of an ->* expression is an lvalue only if its second operand
1140 // is a pointer to data member.
1141 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1142 !BinOp->getType()->isFunctionType()) {
1143 QualType Ty = BinOp->getRHS()->getType();
1144 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1145 return LV_Valid;
1146 }
1147
Douglas Gregorbf3af052008-11-13 20:12:29 +00001148 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001149 return LV_InvalidExpression;
1150
Douglas Gregorbf3af052008-11-13 20:12:29 +00001151 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001152 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001153 // The result of an assignment operation [...] is an lvalue.
1154 return LV_Valid;
1155
1156
1157 // C99 6.5.16:
1158 // An assignment expression [...] is not an lvalue.
1159 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001162 case CXXOperatorCallExprClass:
1163 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001164 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001165 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001166 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001167 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1168 if (ReturnType->isLValueReferenceType())
1169 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001170
Douglas Gregor9d293df2008-10-28 00:22:11 +00001171 break;
1172 }
Steve Naroffe6386392007-12-05 04:00:10 +00001173 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1174 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001175 case ChooseExprClass:
1176 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001177 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001178 case ExtVectorElementExprClass:
1179 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001180 return LV_DuplicateVectorComponents;
1181 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001182 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1183 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001184 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1185 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001186 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001187 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001188 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001189 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001190 case UnresolvedLookupExprClass:
1191 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001192 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001193 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001194 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001195 case CXXFunctionalCastExprClass:
1196 case CXXStaticCastExprClass:
1197 case CXXDynamicCastExprClass:
1198 case CXXReinterpretCastExprClass:
1199 case CXXConstCastExprClass:
1200 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001201 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001202 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1203 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001204 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1205 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001206 return LV_Valid;
1207 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001208 case CXXTypeidExprClass:
1209 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1210 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001211 case CXXBindTemporaryExprClass:
1212 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1213 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001214 case CXXBindReferenceExprClass:
1215 // Something that's bound to a reference is always an lvalue.
1216 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001217 case ConditionalOperatorClass: {
1218 // Complicated handling is only for C++.
1219 if (!Ctx.getLangOptions().CPlusPlus)
1220 return LV_InvalidExpression;
1221
1222 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1223 // everywhere there's an object converted to an rvalue. Also, any other
1224 // casts should be wrapped by ImplicitCastExprs. There's just the special
1225 // case involving throws to work out.
1226 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001227 Expr *True = Cond->getTrueExpr();
1228 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001229 // C++0x 5.16p2
1230 // If either the second or the third operand has type (cv) void, [...]
1231 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001232 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001233 return LV_InvalidExpression;
1234
1235 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001236 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001237 return LV_InvalidExpression;
1238
1239 // That's it.
1240 return LV_Valid;
1241 }
1242
Douglas Gregor2d48e782009-12-19 07:07:47 +00001243 case Expr::CXXExprWithTemporariesClass:
1244 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1245
1246 case Expr::ObjCMessageExprClass:
1247 if (const ObjCMethodDecl *Method
1248 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1249 if (Method->getResultType()->isLValueReferenceType())
1250 return LV_Valid;
1251 break;
1252
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 default:
1254 break;
1255 }
1256 return LV_InvalidExpression;
1257}
1258
1259/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1260/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001261/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001262/// recursively, any member or element of all contained aggregates or unions)
1263/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001264Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001265Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001266 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001269 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001270 // C++ 3.10p11: Functions cannot be modified, but pointers to
1271 // functions can be modifiable.
1272 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1273 return MLV_NotObjectType;
1274 break;
1275
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 case LV_NotObjectType: return MLV_NotObjectType;
1277 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001278 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001279 case LV_InvalidExpression:
1280 // If the top level is a C-style cast, and the subexpression is a valid
1281 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1282 // GCC extension. We don't support it, but we want to produce good
1283 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001284 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1285 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1286 if (Loc)
1287 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001288 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001289 }
1290 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001291 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001292 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001293 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
1294 case LV_SubObjCPropertyGetterSetting:
1295 return MLV_SubObjCPropertyGetterSetting;
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001297
1298 // The following is illegal:
1299 // void takeclosure(void (^C)(void));
1300 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1301 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001302 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001303 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1304 return MLV_NotBlockQualified;
1305 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001306
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001307 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001308 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001309 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1310 if (Expr->getSetterMethod() == 0)
1311 return MLV_NoSetterProperty;
1312 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001313
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001314 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001316 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001318 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001320 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Ted Kremenek6217b802009-07-29 21:53:49 +00001323 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001324 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001325 return MLV_ConstQualified;
1326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Mike Stump1eb44332009-09-09 15:08:12 +00001328 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001329}
1330
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001331/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001332/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001333bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001334 switch (getStmtClass()) {
1335 default:
1336 return false;
1337 case ObjCIvarRefExprClass:
1338 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001339 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001340 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001341 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001342 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001343 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001344 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001345 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001346 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001347 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001348 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001349 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1350 if (VD->hasGlobalStorage())
1351 return true;
1352 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001353 // dereferencing to a pointer is always a gc'able candidate,
1354 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001355 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001356 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001357 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001358 return false;
1359 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001360 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001361 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001362 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001363 }
1364 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001365 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001366 }
1367}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001368Expr* Expr::IgnoreParens() {
1369 Expr* E = this;
1370 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1371 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001373 return E;
1374}
1375
Chris Lattner56f34942008-02-13 01:02:39 +00001376/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1377/// or CastExprs or ImplicitCastExprs, returning their operand.
1378Expr *Expr::IgnoreParenCasts() {
1379 Expr *E = this;
1380 while (true) {
1381 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1382 E = P->getSubExpr();
1383 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1384 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001385 else
1386 return E;
1387 }
1388}
1389
Chris Lattnerecdd8412009-03-13 17:28:01 +00001390/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1391/// value (including ptr->int casts of the same size). Strip off any
1392/// ParenExpr or CastExprs, returning their operand.
1393Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1394 Expr *E = this;
1395 while (true) {
1396 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1397 E = P->getSubExpr();
1398 continue;
1399 }
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattnerecdd8412009-03-13 17:28:01 +00001401 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1402 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1403 // ptr<->int casts of the same width. We also ignore all identify casts.
1404 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Chris Lattnerecdd8412009-03-13 17:28:01 +00001406 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1407 E = SE;
1408 continue;
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Chris Lattnerecdd8412009-03-13 17:28:01 +00001411 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1412 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1413 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1414 E = SE;
1415 continue;
1416 }
1417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Chris Lattnerecdd8412009-03-13 17:28:01 +00001419 return E;
1420 }
1421}
1422
Douglas Gregor6eef5192009-12-14 19:27:10 +00001423bool Expr::isDefaultArgument() const {
1424 const Expr *E = this;
1425 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1426 E = ICE->getSubExprAsWritten();
1427
1428 return isa<CXXDefaultArgExpr>(E);
1429}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001430
Douglas Gregor898574e2008-12-05 23:32:09 +00001431/// hasAnyTypeDependentArguments - Determines if any of the expressions
1432/// in Exprs is type-dependent.
1433bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1434 for (unsigned I = 0; I < NumExprs; ++I)
1435 if (Exprs[I]->isTypeDependent())
1436 return true;
1437
1438 return false;
1439}
1440
1441/// hasAnyValueDependentArguments - Determines if any of the expressions
1442/// in Exprs is value-dependent.
1443bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1444 for (unsigned I = 0; I < NumExprs; ++I)
1445 if (Exprs[I]->isValueDependent())
1446 return true;
1447
1448 return false;
1449}
1450
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001451bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001452 // This function is attempting whether an expression is an initializer
1453 // which can be evaluated at compile-time. isEvaluatable handles most
1454 // of the cases, but it can't deal with some initializer-specific
1455 // expressions, and it can't deal with aggregates; we deal with those here,
1456 // and fall back to isEvaluatable for the other cases.
1457
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001458 // FIXME: This function assumes the variable being assigned to
1459 // isn't a reference type!
1460
Anders Carlssone8a32b82008-11-24 05:23:59 +00001461 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001462 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001463 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001464 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001465 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001466 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001467 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001468 // This handles gcc's extension that allows global initializers like
1469 // "struct x {int x;} x = (struct x) {};".
1470 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001471 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001472 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001473 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001474 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001475 // FIXME: This doesn't deal with fields with reference types correctly.
1476 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1477 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001478 const InitListExpr *Exp = cast<InitListExpr>(this);
1479 unsigned numInits = Exp->getNumInits();
1480 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001481 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001482 return false;
1483 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001484 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001485 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001486 case ImplicitValueInitExprClass:
1487 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001488 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001489 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001490 case UnaryOperatorClass: {
1491 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1492 if (Exp->getOpcode() == UnaryOperator::Extension)
1493 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1494 break;
1495 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001496 case BinaryOperatorClass: {
1497 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1498 // but this handles the common case.
1499 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1500 if (Exp->getOpcode() == BinaryOperator::Sub &&
1501 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1502 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1503 return true;
1504 break;
1505 }
Chris Lattner81045d82009-04-21 05:19:11 +00001506 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001507 case CStyleCastExprClass:
1508 // Handle casts with a destination that's a struct or union; this
1509 // deals with both the gcc no-op struct cast extension and the
1510 // cast-to-union extension.
1511 if (getType()->isRecordType())
1512 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001513
1514 // Integer->integer casts can be handled here, which is important for
1515 // things like (int)(&&x-&&y). Scary but true.
1516 if (getType()->isIntegerType() &&
1517 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1518 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1519
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001520 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001521 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001522 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001523}
1524
Reid Spencer5f016e22007-07-11 17:01:13 +00001525/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001526/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001527
1528/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1529/// comma, etc
1530///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001531/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1532/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1533/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001534
Eli Friedmane28d7192009-02-26 09:29:13 +00001535// CheckICE - This function does the fundamental ICE checking: the returned
1536// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1537// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001538// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001539// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001540// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001541//
1542// Meanings of Val:
1543// 0: This expression is an ICE if it can be evaluated by Evaluate.
1544// 1: This expression is not an ICE, but if it isn't evaluated, it's
1545// a legal subexpression for an ICE. This return value is used to handle
1546// the comma operator in C99 mode.
1547// 2: This expression is not an ICE, and is not a legal subexpression for one.
1548
1549struct ICEDiag {
1550 unsigned Val;
1551 SourceLocation Loc;
1552
1553 public:
1554 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1555 ICEDiag() : Val(0) {}
1556};
1557
1558ICEDiag NoDiag() { return ICEDiag(); }
1559
Eli Friedman60ce9632009-02-27 04:07:58 +00001560static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1561 Expr::EvalResult EVResult;
1562 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1563 !EVResult.Val.isInt()) {
1564 return ICEDiag(2, E->getLocStart());
1565 }
1566 return NoDiag();
1567}
1568
Eli Friedmane28d7192009-02-26 09:29:13 +00001569static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001570 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001571 if (!E->getType()->isIntegralType()) {
1572 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001573 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001574
1575 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001576#define STMT(Node, Base) case Expr::Node##Class:
1577#define EXPR(Node, Base)
1578#include "clang/AST/StmtNodes.def"
1579 case Expr::PredefinedExprClass:
1580 case Expr::FloatingLiteralClass:
1581 case Expr::ImaginaryLiteralClass:
1582 case Expr::StringLiteralClass:
1583 case Expr::ArraySubscriptExprClass:
1584 case Expr::MemberExprClass:
1585 case Expr::CompoundAssignOperatorClass:
1586 case Expr::CompoundLiteralExprClass:
1587 case Expr::ExtVectorElementExprClass:
1588 case Expr::InitListExprClass:
1589 case Expr::DesignatedInitExprClass:
1590 case Expr::ImplicitValueInitExprClass:
1591 case Expr::ParenListExprClass:
1592 case Expr::VAArgExprClass:
1593 case Expr::AddrLabelExprClass:
1594 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001595 case Expr::CXXMemberCallExprClass:
1596 case Expr::CXXDynamicCastExprClass:
1597 case Expr::CXXTypeidExprClass:
1598 case Expr::CXXNullPtrLiteralExprClass:
1599 case Expr::CXXThisExprClass:
1600 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001601 case Expr::CXXNewExprClass:
1602 case Expr::CXXDeleteExprClass:
1603 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001604 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001605 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001606 case Expr::CXXConstructExprClass:
1607 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001608 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001609 case Expr::CXXExprWithTemporariesClass:
1610 case Expr::CXXTemporaryObjectExprClass:
1611 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001612 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001613 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001614 case Expr::ObjCStringLiteralClass:
1615 case Expr::ObjCEncodeExprClass:
1616 case Expr::ObjCMessageExprClass:
1617 case Expr::ObjCSelectorExprClass:
1618 case Expr::ObjCProtocolExprClass:
1619 case Expr::ObjCIvarRefExprClass:
1620 case Expr::ObjCPropertyRefExprClass:
1621 case Expr::ObjCImplicitSetterGetterRefExprClass:
1622 case Expr::ObjCSuperExprClass:
1623 case Expr::ObjCIsaExprClass:
1624 case Expr::ShuffleVectorExprClass:
1625 case Expr::BlockExprClass:
1626 case Expr::BlockDeclRefExprClass:
1627 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001628 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001629
Douglas Gregor043cad22009-09-11 00:18:58 +00001630 case Expr::GNUNullExprClass:
1631 // GCC considers the GNU __null value to be an integral constant expression.
1632 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001633
Eli Friedmane28d7192009-02-26 09:29:13 +00001634 case Expr::ParenExprClass:
1635 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1636 case Expr::IntegerLiteralClass:
1637 case Expr::CharacterLiteralClass:
1638 case Expr::CXXBoolLiteralExprClass:
1639 case Expr::CXXZeroInitValueExprClass:
1640 case Expr::TypesCompatibleExprClass:
1641 case Expr::UnaryTypeTraitExprClass:
1642 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001643 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001644 case Expr::CXXOperatorCallExprClass: {
1645 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001646 if (CE->isBuiltinCall(Ctx))
1647 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001648 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001649 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001650 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001651 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1652 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001653 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001654 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001655 // C++ 7.1.5.1p2
1656 // A variable of non-volatile const-qualified integral or enumeration
1657 // type initialized by an ICE can be used in ICEs.
1658 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001659 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001660 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1661 if (Quals.hasVolatile() || !Quals.hasConst())
1662 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1663
Sebastian Redl31310a22010-02-01 20:16:42 +00001664 // Look for a declaration of this variable that has an initializer.
1665 const VarDecl *ID = 0;
1666 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001667 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001668 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001669 // We have already checked whether this subexpression is an
1670 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001671 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001672 return NoDiag();
1673 else
1674 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1675 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001676
John McCall1f1b3b32010-02-06 01:07:37 +00001677 // It's an ICE whether or not the definition we found is
1678 // out-of-line. See DR 721 and the discussion in Clang PR
1679 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001680
1681 if (Dcl->isCheckingICE()) {
1682 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1683 }
1684
1685 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001686 ICEDiag Result = CheckICE(Init, Ctx);
1687 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001688 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001689 return Result;
1690 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001691 }
1692 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001693 return ICEDiag(2, E->getLocStart());
1694 case Expr::UnaryOperatorClass: {
1695 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001697 case UnaryOperator::PostInc:
1698 case UnaryOperator::PostDec:
1699 case UnaryOperator::PreInc:
1700 case UnaryOperator::PreDec:
1701 case UnaryOperator::AddrOf:
1702 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001703 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001704
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001706 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001710 case UnaryOperator::Real:
1711 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001712 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001713 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001714 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1715 // Evaluate matches the proposed gcc behavior for cases like
1716 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1717 // compliance: we should warn earlier for offsetof expressions with
1718 // array subscripts that aren't ICEs, and if the array subscripts
1719 // are ICEs, the value of the offsetof must be an integer constant.
1720 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001721 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001723 case Expr::SizeOfAlignOfExprClass: {
1724 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1725 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1726 return ICEDiag(2, E->getLocStart());
1727 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001729 case Expr::BinaryOperatorClass: {
1730 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001732 case BinaryOperator::PtrMemD:
1733 case BinaryOperator::PtrMemI:
1734 case BinaryOperator::Assign:
1735 case BinaryOperator::MulAssign:
1736 case BinaryOperator::DivAssign:
1737 case BinaryOperator::RemAssign:
1738 case BinaryOperator::AddAssign:
1739 case BinaryOperator::SubAssign:
1740 case BinaryOperator::ShlAssign:
1741 case BinaryOperator::ShrAssign:
1742 case BinaryOperator::AndAssign:
1743 case BinaryOperator::XorAssign:
1744 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001745 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001746
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001750 case BinaryOperator::Add:
1751 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001754 case BinaryOperator::LT:
1755 case BinaryOperator::GT:
1756 case BinaryOperator::LE:
1757 case BinaryOperator::GE:
1758 case BinaryOperator::EQ:
1759 case BinaryOperator::NE:
1760 case BinaryOperator::And:
1761 case BinaryOperator::Xor:
1762 case BinaryOperator::Or:
1763 case BinaryOperator::Comma: {
1764 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1765 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001766 if (Exp->getOpcode() == BinaryOperator::Div ||
1767 Exp->getOpcode() == BinaryOperator::Rem) {
1768 // Evaluate gives an error for undefined Div/Rem, so make sure
1769 // we don't evaluate one.
1770 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1771 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1772 if (REval == 0)
1773 return ICEDiag(1, E->getLocStart());
1774 if (REval.isSigned() && REval.isAllOnesValue()) {
1775 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1776 if (LEval.isMinSignedValue())
1777 return ICEDiag(1, E->getLocStart());
1778 }
1779 }
1780 }
1781 if (Exp->getOpcode() == BinaryOperator::Comma) {
1782 if (Ctx.getLangOptions().C99) {
1783 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1784 // if it isn't evaluated.
1785 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1786 return ICEDiag(1, E->getLocStart());
1787 } else {
1788 // In both C89 and C++, commas in ICEs are illegal.
1789 return ICEDiag(2, E->getLocStart());
1790 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001791 }
1792 if (LHSResult.Val >= RHSResult.Val)
1793 return LHSResult;
1794 return RHSResult;
1795 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001797 case BinaryOperator::LOr: {
1798 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1799 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1800 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1801 // Rare case where the RHS has a comma "side-effect"; we need
1802 // to actually check the condition to see whether the side
1803 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001804 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001805 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001806 return RHSResult;
1807 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001808 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001809
Eli Friedmane28d7192009-02-26 09:29:13 +00001810 if (LHSResult.Val >= RHSResult.Val)
1811 return LHSResult;
1812 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001814 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001816 case Expr::ImplicitCastExprClass:
1817 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001818 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001819 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001820 case Expr::CXXStaticCastExprClass:
1821 case Expr::CXXReinterpretCastExprClass:
1822 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001823 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1824 if (SubExpr->getType()->isIntegralType())
1825 return CheckICE(SubExpr, Ctx);
1826 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1827 return NoDiag();
1828 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001830 case Expr::ConditionalOperatorClass: {
1831 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001832 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001833 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001834 // expression, and it is fully evaluated. This is an important GNU
1835 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001836 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001837 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001838 Expr::EvalResult EVResult;
1839 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1840 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001841 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001842 }
1843 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001844 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001845 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1846 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1847 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1848 if (CondResult.Val == 2)
1849 return CondResult;
1850 if (TrueResult.Val == 2)
1851 return TrueResult;
1852 if (FalseResult.Val == 2)
1853 return FalseResult;
1854 if (CondResult.Val == 1)
1855 return CondResult;
1856 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1857 return NoDiag();
1858 // Rare case where the diagnostics depend on which side is evaluated
1859 // Note that if we get here, CondResult is 0, and at least one of
1860 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001861 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001862 return FalseResult;
1863 }
1864 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001866 case Expr::CXXDefaultArgExprClass:
1867 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001868 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001869 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001870 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001872
Douglas Gregorf2991242009-09-10 23:31:45 +00001873 // Silence a GCC warning
1874 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001875}
Reid Spencer5f016e22007-07-11 17:01:13 +00001876
Eli Friedmane28d7192009-02-26 09:29:13 +00001877bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1878 SourceLocation *Loc, bool isEvaluated) const {
1879 ICEDiag d = CheckICE(this, Ctx);
1880 if (d.Val != 0) {
1881 if (Loc) *Loc = d.Loc;
1882 return false;
1883 }
1884 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001885 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001886 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001887 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1888 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001889 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 return true;
1891}
1892
Reid Spencer5f016e22007-07-11 17:01:13 +00001893/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1894/// integer constant expression with the value zero, or if this is one that is
1895/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001896bool Expr::isNullPointerConstant(ASTContext &Ctx,
1897 NullPointerConstantValueDependence NPC) const {
1898 if (isValueDependent()) {
1899 switch (NPC) {
1900 case NPC_NeverValueDependent:
1901 assert(false && "Unexpected value dependent expression!");
1902 // If the unthinkable happens, fall through to the safest alternative.
1903
1904 case NPC_ValueDependentIsNull:
1905 return isTypeDependent() || getType()->isIntegralType();
1906
1907 case NPC_ValueDependentIsNotNull:
1908 return false;
1909 }
1910 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001911
Sebastian Redl07779722008-10-31 14:43:28 +00001912 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001913 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001914 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001915 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001916 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001917 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001918 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001919 Pointee->isVoidType() && // to void*
1920 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001921 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001922 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001924 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1925 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001926 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001927 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1928 // Accept ((void*)0) as a null pointer constant, as many other
1929 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001930 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001931 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001932 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001933 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001934 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001935 } else if (isa<GNUNullExpr>(this)) {
1936 // The GNU __null extension is always a null pointer constant.
1937 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001938 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001939
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001940 // C++0x nullptr_t is always a null pointer constant.
1941 if (getType()->isNullPtrType())
1942 return true;
1943
Steve Naroffaa58f002008-01-14 16:10:57 +00001944 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001945 if (!getType()->isIntegerType() ||
1946 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001947 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 // If we have an integer constant expression, we need to *evaluate* it and
1950 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001951 llvm::APSInt Result;
1952 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001953}
Steve Naroff31a45842007-07-28 23:10:27 +00001954
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001955FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001956 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001957
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001958 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1959 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1960 E = ICE->getSubExpr()->IgnoreParens();
1961 else
1962 break;
1963 }
1964
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001965 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001966 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001967 if (Field->isBitField())
1968 return Field;
1969
1970 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1971 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1972 return BinOp->getLHS()->getBitField();
1973
1974 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001975}
1976
Anders Carlsson09380262010-01-31 17:18:49 +00001977bool Expr::refersToVectorElement() const {
1978 const Expr *E = this->IgnoreParens();
1979
1980 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1981 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1982 E = ICE->getSubExpr()->IgnoreParens();
1983 else
1984 break;
1985 }
1986
1987 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1988 return ASE->getBase()->getType()->isVectorType();
1989
1990 if (isa<ExtVectorElementExpr>(E))
1991 return true;
1992
1993 return false;
1994}
1995
Chris Lattner2140e902009-02-16 22:14:05 +00001996/// isArrow - Return true if the base expression is a pointer to vector,
1997/// return false if the base expression is a vector.
1998bool ExtVectorElementExpr::isArrow() const {
1999 return getBase()->getType()->isPointerType();
2000}
2001
Nate Begeman213541a2008-04-18 23:10:10 +00002002unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002003 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002004 return VT->getNumElements();
2005 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002006}
2007
Nate Begeman8a997642008-05-09 06:41:27 +00002008/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002009bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002010 // FIXME: Refactor this code to an accessor on the AST node which returns the
2011 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002012 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002013
2014 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002015 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002016 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Nate Begeman190d6a22009-01-18 02:01:21 +00002018 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002019 if (Comp[0] == 's' || Comp[0] == 'S')
2020 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Daniel Dunbar15027422009-10-17 23:53:04 +00002022 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2023 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002024 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002025
Steve Narofffec0b492007-07-30 03:29:09 +00002026 return false;
2027}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002028
Nate Begeman8a997642008-05-09 06:41:27 +00002029/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002030void ExtVectorElementExpr::getEncodedElementAccess(
2031 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002032 llvm::StringRef Comp = Accessor->getName();
2033 if (Comp[0] == 's' || Comp[0] == 'S')
2034 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002036 bool isHi = Comp == "hi";
2037 bool isLo = Comp == "lo";
2038 bool isEven = Comp == "even";
2039 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Nate Begeman8a997642008-05-09 06:41:27 +00002041 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2042 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Nate Begeman8a997642008-05-09 06:41:27 +00002044 if (isHi)
2045 Index = e + i;
2046 else if (isLo)
2047 Index = i;
2048 else if (isEven)
2049 Index = 2 * i;
2050 else if (isOdd)
2051 Index = 2 * i + 1;
2052 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002053 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002054
Nate Begeman3b8d1162008-05-13 21:03:02 +00002055 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002056 }
Nate Begeman8a997642008-05-09 06:41:27 +00002057}
2058
Steve Naroff68d331a2007-09-27 14:38:14 +00002059// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002060ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2061 Selector selInfo,
2062 QualType retType, ObjCMethodDecl *mproto,
2063 SourceLocation LBrac, SourceLocation RBrac,
2064 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002065 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002066 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002067 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002068 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002069 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002070 if (NumArgs) {
2071 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002072 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2073 }
Steve Naroff563477d2007-09-18 23:55:05 +00002074 LBracloc = LBrac;
2075 RBracloc = RBrac;
2076}
2077
Mike Stump1eb44332009-09-09 15:08:12 +00002078// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002079// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002080ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
2081 Selector selInfo, QualType retType,
2082 ObjCMethodDecl *mproto,
2083 SourceLocation LBrac, SourceLocation RBrac,
2084 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002085 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002086 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002087 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002088 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002089 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002090 if (NumArgs) {
2091 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002092 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2093 }
Steve Naroff563477d2007-09-18 23:55:05 +00002094 LBracloc = LBrac;
2095 RBracloc = RBrac;
2096}
2097
Mike Stump1eb44332009-09-09 15:08:12 +00002098// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002099ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
2100 Selector selInfo, QualType retType,
2101 ObjCMethodDecl *mproto, SourceLocation LBrac,
2102 SourceLocation RBrac, Expr **ArgExprs,
2103 unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002104: Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00002105MethodProto(mproto) {
2106 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002107 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002108 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2109 if (NumArgs) {
2110 for (unsigned i = 0; i != NumArgs; ++i)
2111 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2112 }
2113 LBracloc = LBrac;
2114 RBracloc = RBrac;
2115}
2116
2117ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2118 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2119 switch (x & Flags) {
2120 default:
2121 assert(false && "Invalid ObjCMessageExpr.");
2122 case IsInstMeth:
2123 return ClassInfo(0, 0);
2124 case IsClsMethDeclUnknown:
2125 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2126 case IsClsMethDeclKnown: {
2127 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2128 return ClassInfo(D, D->getIdentifier());
2129 }
2130 }
2131}
2132
Chris Lattner0389e6b2009-04-26 00:44:05 +00002133void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2134 if (CI.first == 0 && CI.second == 0)
2135 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2136 else if (CI.first == 0)
2137 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2138 else
2139 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2140}
2141
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002142void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2143 DestroyChildren(C);
2144 if (SubExprs)
2145 C.Deallocate(SubExprs);
2146 this->~ObjCMessageExpr();
2147 C.Deallocate((void*) this);
2148}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002149
Chris Lattner27437ca2007-10-25 00:29:32 +00002150bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002151 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002152}
2153
Nate Begeman888376a2009-08-12 02:28:50 +00002154void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2155 unsigned NumExprs) {
2156 if (SubExprs) C.Deallocate(SubExprs);
2157
2158 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002159 this->NumExprs = NumExprs;
2160 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002161}
Nate Begeman888376a2009-08-12 02:28:50 +00002162
2163void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2164 DestroyChildren(C);
2165 if (SubExprs) C.Deallocate(SubExprs);
2166 this->~ShuffleVectorExpr();
2167 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002168}
2169
Douglas Gregor42602bb2009-08-07 06:08:38 +00002170void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002171 // Override default behavior of traversing children. If this has a type
2172 // operand and the type is a variable-length array, the child iteration
2173 // will iterate over the size expression. However, this expression belongs
2174 // to the type, not to this, so we don't want to delete it.
2175 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002176 if (isArgumentType()) {
2177 this->~SizeOfAlignOfExpr();
2178 C.Deallocate(this);
2179 }
Sebastian Redl05189992008-11-11 17:56:53 +00002180 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002181 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002182}
2183
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002184//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002185// DesignatedInitExpr
2186//===----------------------------------------------------------------------===//
2187
2188IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2189 assert(Kind == FieldDesignator && "Only valid on a field designator");
2190 if (Field.NameOrField & 0x01)
2191 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2192 else
2193 return getField()->getIdentifier();
2194}
2195
Douglas Gregor319d57f2010-01-06 23:17:19 +00002196DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2197 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002198 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002199 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002200 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002201 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002202 unsigned NumIndexExprs,
2203 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002204 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002205 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002206 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2207 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002208 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002209
2210 // Record the initializer itself.
2211 child_iterator Child = child_begin();
2212 *Child++ = Init;
2213
2214 // Copy the designators and their subexpressions, computing
2215 // value-dependence along the way.
2216 unsigned IndexIdx = 0;
2217 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002218 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002219
2220 if (this->Designators[I].isArrayDesignator()) {
2221 // Compute type- and value-dependence.
2222 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002223 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002224 Index->isTypeDependent() || Index->isValueDependent();
2225
2226 // Copy the index expressions into permanent storage.
2227 *Child++ = IndexExprs[IndexIdx++];
2228 } else if (this->Designators[I].isArrayRangeDesignator()) {
2229 // Compute type- and value-dependence.
2230 Expr *Start = IndexExprs[IndexIdx];
2231 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002232 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002233 Start->isTypeDependent() || Start->isValueDependent() ||
2234 End->isTypeDependent() || End->isValueDependent();
2235
2236 // Copy the start/end expressions into permanent storage.
2237 *Child++ = IndexExprs[IndexIdx++];
2238 *Child++ = IndexExprs[IndexIdx++];
2239 }
2240 }
2241
2242 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002243}
2244
Douglas Gregor05c13a32009-01-22 00:58:24 +00002245DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002246DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002247 unsigned NumDesignators,
2248 Expr **IndexExprs, unsigned NumIndexExprs,
2249 SourceLocation ColonOrEqualLoc,
2250 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002251 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002252 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002253 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002254 ColonOrEqualLoc, UsesColonSyntax,
2255 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002256}
2257
Mike Stump1eb44332009-09-09 15:08:12 +00002258DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002259 unsigned NumIndexExprs) {
2260 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2261 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2262 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2263}
2264
Douglas Gregor319d57f2010-01-06 23:17:19 +00002265void DesignatedInitExpr::setDesignators(ASTContext &C,
2266 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002267 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002268 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002269
Douglas Gregor319d57f2010-01-06 23:17:19 +00002270 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002271 NumDesignators = NumDesigs;
2272 for (unsigned I = 0; I != NumDesigs; ++I)
2273 Designators[I] = Desigs[I];
2274}
2275
Douglas Gregor05c13a32009-01-22 00:58:24 +00002276SourceRange DesignatedInitExpr::getSourceRange() const {
2277 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002278 Designator &First =
2279 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002280 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002281 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002282 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2283 else
2284 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2285 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002286 StartLoc =
2287 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002288 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2289}
2290
Douglas Gregor05c13a32009-01-22 00:58:24 +00002291Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2292 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2293 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2294 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002295 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2296 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2297}
2298
2299Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002300 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002301 "Requires array range designator");
2302 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2303 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002304 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2305 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2306}
2307
2308Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002309 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002310 "Requires array range designator");
2311 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2312 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002313 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2314 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2315}
2316
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002317/// \brief Replaces the designator at index @p Idx with the series
2318/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002319void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002320 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002321 const Designator *Last) {
2322 unsigned NumNewDesignators = Last - First;
2323 if (NumNewDesignators == 0) {
2324 std::copy_backward(Designators + Idx + 1,
2325 Designators + NumDesignators,
2326 Designators + Idx);
2327 --NumNewDesignators;
2328 return;
2329 } else if (NumNewDesignators == 1) {
2330 Designators[Idx] = *First;
2331 return;
2332 }
2333
Mike Stump1eb44332009-09-09 15:08:12 +00002334 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002335 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002336 std::copy(Designators, Designators + Idx, NewDesignators);
2337 std::copy(First, Last, NewDesignators + Idx);
2338 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2339 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002340 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002341 Designators = NewDesignators;
2342 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2343}
2344
Douglas Gregor42602bb2009-08-07 06:08:38 +00002345void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002346 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002347 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002348}
2349
Douglas Gregor319d57f2010-01-06 23:17:19 +00002350void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2351 for (unsigned I = 0; I != NumDesignators; ++I)
2352 Designators[I].~Designator();
2353 C.Deallocate(Designators);
2354 Designators = 0;
2355}
2356
Mike Stump1eb44332009-09-09 15:08:12 +00002357ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002358 Expr **exprs, unsigned nexprs,
2359 SourceLocation rparenloc)
2360: Expr(ParenListExprClass, QualType(),
2361 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002362 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002363 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Nate Begeman2ef13e52009-08-10 23:49:36 +00002365 Exprs = new (C) Stmt*[nexprs];
2366 for (unsigned i = 0; i != nexprs; ++i)
2367 Exprs[i] = exprs[i];
2368}
2369
2370void ParenListExpr::DoDestroy(ASTContext& C) {
2371 DestroyChildren(C);
2372 if (Exprs) C.Deallocate(Exprs);
2373 this->~ParenListExpr();
2374 C.Deallocate(this);
2375}
2376
Douglas Gregor05c13a32009-01-22 00:58:24 +00002377//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002378// ExprIterator.
2379//===----------------------------------------------------------------------===//
2380
2381Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2382Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2383Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2384const Expr* ConstExprIterator::operator[](size_t idx) const {
2385 return cast<Expr>(I[idx]);
2386}
2387const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2388const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2389
2390//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002391// Child Iterators for iterating over subexpressions/substatements
2392//===----------------------------------------------------------------------===//
2393
2394// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002395Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2396Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002397
Steve Naroff7779db42007-11-12 14:29:37 +00002398// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002399Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2400Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002401
Steve Naroffe3e9add2008-06-02 23:03:37 +00002402// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002403Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2404Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002405
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002406// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002407Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2408 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002409}
Mike Stump1eb44332009-09-09 15:08:12 +00002410Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2411 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002412}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002413
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002414// ObjCSuperExpr
2415Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2416Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2417
Steve Narofff242b1b2009-07-24 17:54:45 +00002418// ObjCIsaExpr
2419Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2420Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2421
Chris Lattnerd9f69102008-08-10 01:53:14 +00002422// PredefinedExpr
2423Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2424Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002425
2426// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002427Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2428Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002429
2430// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002431Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002432Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002433
2434// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002435Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2436Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002437
Chris Lattner5d661452007-08-26 03:42:43 +00002438// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002439Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2440Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002441
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002442// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002443Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2444Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002445
2446// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002447Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2448Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002449
2450// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002451Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2452Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002453
Sebastian Redl05189992008-11-11 17:56:53 +00002454// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002455Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002456 // If this is of a type and the type is a VLA type (and not a typedef), the
2457 // size expression of the VLA needs to be treated as an executable expression.
2458 // Why isn't this weirdness documented better in StmtIterator?
2459 if (isArgumentType()) {
2460 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2461 getArgumentType().getTypePtr()))
2462 return child_iterator(T);
2463 return child_iterator();
2464 }
Sebastian Redld4575892008-12-03 23:17:54 +00002465 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002466}
Sebastian Redl05189992008-11-11 17:56:53 +00002467Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2468 if (isArgumentType())
2469 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002470 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002471}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002472
2473// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002474Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002475 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002476}
Ted Kremenek1237c672007-08-24 20:06:47 +00002477Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002478 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002479}
2480
2481// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002482Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002483 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002484}
Ted Kremenek1237c672007-08-24 20:06:47 +00002485Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002486 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002487}
Ted Kremenek1237c672007-08-24 20:06:47 +00002488
2489// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002490Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2491Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002492
Nate Begeman213541a2008-04-18 23:10:10 +00002493// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002494Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2495Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002496
2497// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002498Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2499Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002500
Ted Kremenek1237c672007-08-24 20:06:47 +00002501// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002502Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2503Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002504
2505// BinaryOperator
2506Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002507 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002508}
Ted Kremenek1237c672007-08-24 20:06:47 +00002509Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002510 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002511}
2512
2513// ConditionalOperator
2514Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002515 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002516}
Ted Kremenek1237c672007-08-24 20:06:47 +00002517Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002518 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002519}
2520
2521// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002522Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2523Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002524
Ted Kremenek1237c672007-08-24 20:06:47 +00002525// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002526Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2527Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002528
2529// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002530Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2531 return child_iterator();
2532}
2533
2534Stmt::child_iterator TypesCompatibleExpr::child_end() {
2535 return child_iterator();
2536}
Ted Kremenek1237c672007-08-24 20:06:47 +00002537
2538// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002539Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2540Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002541
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002542// GNUNullExpr
2543Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2544Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2545
Eli Friedmand38617c2008-05-14 19:38:39 +00002546// ShuffleVectorExpr
2547Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002548 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002549}
2550Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002551 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002552}
2553
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002554// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002555Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2556Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002557
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002558// InitListExpr
2559Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002560 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002561}
2562Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002563 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002564}
2565
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002566// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002567Stmt::child_iterator DesignatedInitExpr::child_begin() {
2568 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2569 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002570 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2571}
2572Stmt::child_iterator DesignatedInitExpr::child_end() {
2573 return child_iterator(&*child_begin() + NumSubExprs);
2574}
2575
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002576// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002577Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2578 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002579}
2580
Mike Stump1eb44332009-09-09 15:08:12 +00002581Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2582 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002583}
2584
Nate Begeman2ef13e52009-08-10 23:49:36 +00002585// ParenListExpr
2586Stmt::child_iterator ParenListExpr::child_begin() {
2587 return &Exprs[0];
2588}
2589Stmt::child_iterator ParenListExpr::child_end() {
2590 return &Exprs[0]+NumExprs;
2591}
2592
Ted Kremenek1237c672007-08-24 20:06:47 +00002593// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002594Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002595 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002596}
2597Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002598 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002599}
Ted Kremenek1237c672007-08-24 20:06:47 +00002600
2601// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002602Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2603Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002604
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002605// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002606Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002607 return child_iterator();
2608}
2609Stmt::child_iterator ObjCSelectorExpr::child_end() {
2610 return child_iterator();
2611}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002612
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002613// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002614Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2615 return child_iterator();
2616}
2617Stmt::child_iterator ObjCProtocolExpr::child_end() {
2618 return child_iterator();
2619}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002620
Steve Naroff563477d2007-09-18 23:55:05 +00002621// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002622Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002623 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002624}
2625Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002626 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002627}
2628
Steve Naroff4eb206b2008-09-03 18:15:37 +00002629// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002630Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2631Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002632
Ted Kremenek9da13f92008-09-26 23:24:14 +00002633Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2634Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }