blob: 72359c245a5c6f60263bbc50c5f51d2368d6df08 [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());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000184
185 std::string Proto = FD->getQualifiedNameAsString(Policy);
186
John McCall183700f2009-09-21 23:43:11 +0000187 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000188 const FunctionProtoType *FT = 0;
189 if (FD->hasWrittenPrototype())
190 FT = dyn_cast<FunctionProtoType>(AFT);
191
192 Proto += "(";
193 if (FT) {
194 llvm::raw_string_ostream POut(Proto);
195 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
196 if (i) POut << ", ";
197 std::string Param;
198 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
199 POut << Param;
200 }
201
202 if (FT->isVariadic()) {
203 if (FD->getNumParams()) POut << ", ";
204 POut << "...";
205 }
206 }
207 Proto += ")";
208
Sam Weinig4eadcc52009-12-27 01:38:20 +0000209 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
210 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
211 if (ThisQuals.hasConst())
212 Proto += " const";
213 if (ThisQuals.hasVolatile())
214 Proto += " volatile";
215 }
216
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000217 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
218 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000219
220 Out << Proto;
221
222 Out.flush();
223 return Name.str().str();
224 }
225 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
226 llvm::SmallString<256> Name;
227 llvm::raw_svector_ostream Out(Name);
228 Out << (MD->isInstanceMethod() ? '-' : '+');
229 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000230
231 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
232 // a null check to avoid a crash.
233 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
234 Out << ID->getNameAsString();
235
Anders Carlsson3a082d82009-09-08 18:24:21 +0000236 if (const ObjCCategoryImplDecl *CID =
237 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
238 Out << '(';
239 Out << CID->getNameAsString();
240 Out << ')';
241 }
242 Out << ' ';
243 Out << MD->getSelector().getAsString();
244 Out << ']';
245
246 Out.flush();
247 return Name.str().str();
248 }
249 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
250 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
251 return "top level";
252 }
253 return "";
254}
255
Chris Lattnerda8249e2008-06-07 22:13:43 +0000256/// getValueAsApproximateDouble - This returns the value as an inaccurate
257/// double. Note that this may cause loss of precision, but is useful for
258/// debugging dumps, etc.
259double FloatingLiteral::getValueAsApproximateDouble() const {
260 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000261 bool ignored;
262 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
263 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000264 return V.convertToDouble();
265}
266
Chris Lattner2085fd62009-02-18 06:40:38 +0000267StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
268 unsigned ByteLength, bool Wide,
269 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000270 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000271 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000272 // Allocate enough space for the StringLiteral plus an array of locations for
273 // any concatenated string tokens.
274 void *Mem = C.Allocate(sizeof(StringLiteral)+
275 sizeof(SourceLocation)*(NumStrs-1),
276 llvm::alignof<StringLiteral>());
277 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000280 char *AStrData = new (C, 1) char[ByteLength];
281 memcpy(AStrData, StrData, ByteLength);
282 SL->StrData = AStrData;
283 SL->ByteLength = ByteLength;
284 SL->IsWide = Wide;
285 SL->TokLocs[0] = Loc[0];
286 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
Chris Lattner726e1682009-02-18 05:49:11 +0000288 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000289 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
290 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000291}
292
Douglas Gregor673ecd62009-04-15 16:35:07 +0000293StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
294 void *Mem = C.Allocate(sizeof(StringLiteral)+
295 sizeof(SourceLocation)*(NumStrs-1),
296 llvm::alignof<StringLiteral>());
297 StringLiteral *SL = new (Mem) StringLiteral(QualType());
298 SL->StrData = 0;
299 SL->ByteLength = 0;
300 SL->NumConcatenated = NumStrs;
301 return SL;
302}
303
Douglas Gregor42602bb2009-08-07 06:08:38 +0000304void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000305 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000306 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000307}
308
Daniel Dunbarb6480232009-09-22 03:27:33 +0000309void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000310 if (StrData)
311 C.Deallocate(const_cast<char*>(StrData));
312
Daniel Dunbarb6480232009-09-22 03:27:33 +0000313 char *AStrData = new (C, 1) char[Str.size()];
314 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000315 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000316 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000317}
318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
320/// corresponds to, e.g. "sizeof" or "[pre]++".
321const char *UnaryOperator::getOpcodeStr(Opcode Op) {
322 switch (Op) {
323 default: assert(0 && "Unknown unary operator");
324 case PostInc: return "++";
325 case PostDec: return "--";
326 case PreInc: return "++";
327 case PreDec: return "--";
328 case AddrOf: return "&";
329 case Deref: return "*";
330 case Plus: return "+";
331 case Minus: return "-";
332 case Not: return "~";
333 case LNot: return "!";
334 case Real: return "__real";
335 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000337 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 }
339}
340
Mike Stump1eb44332009-09-09 15:08:12 +0000341UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000342UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
343 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000344 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000345 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
346 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
347 case OO_Amp: return AddrOf;
348 case OO_Star: return Deref;
349 case OO_Plus: return Plus;
350 case OO_Minus: return Minus;
351 case OO_Tilde: return Not;
352 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000353 }
354}
355
356OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
357 switch (Opc) {
358 case PostInc: case PreInc: return OO_PlusPlus;
359 case PostDec: case PreDec: return OO_MinusMinus;
360 case AddrOf: return OO_Amp;
361 case Deref: return OO_Star;
362 case Plus: return OO_Plus;
363 case Minus: return OO_Minus;
364 case Not: return OO_Tilde;
365 case LNot: return OO_Exclaim;
366 default: return OO_None;
367 }
368}
369
370
Reid Spencer5f016e22007-07-11 17:01:13 +0000371//===----------------------------------------------------------------------===//
372// Postfix Operators.
373//===----------------------------------------------------------------------===//
374
Ted Kremenek668bf912009-02-09 20:51:47 +0000375CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000376 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000377 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000378 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000379 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000380 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Ted Kremenek668bf912009-02-09 20:51:47 +0000382 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000383 SubExprs[FN] = fn;
384 for (unsigned i = 0; i != numargs; ++i)
385 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000386
Douglas Gregorb4609802008-11-14 16:09:21 +0000387 RParenLoc = rparenloc;
388}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000389
Ted Kremenek668bf912009-02-09 20:51:47 +0000390CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
391 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000392 : Expr(CallExprClass, t,
393 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000394 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000395 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000396
397 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000398 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000400 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 RParenLoc = rparenloc;
403}
404
Mike Stump1eb44332009-09-09 15:08:12 +0000405CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
406 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000407 SubExprs = new (C) Stmt*[1];
408}
409
Douglas Gregor42602bb2009-08-07 06:08:38 +0000410void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000411 DestroyChildren(C);
412 if (SubExprs) C.Deallocate(SubExprs);
413 this->~CallExpr();
414 C.Deallocate(this);
415}
416
Nuno Lopesd20254f2009-12-20 23:11:08 +0000417Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000418 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000419 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000420 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000421 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
422 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000423
424 return 0;
425}
426
Nuno Lopesd20254f2009-12-20 23:11:08 +0000427FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000428 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000429}
430
Chris Lattnerd18b3292007-12-28 05:25:02 +0000431/// setNumArgs - This changes the number of arguments present in this call.
432/// Any orphaned expressions are deleted by this, and any new operands are set
433/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000434void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000435 // No change, just return.
436 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438 // If shrinking # arguments, just delete the extras and forgot them.
439 if (NumArgs < getNumArgs()) {
440 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000441 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000442 this->NumArgs = NumArgs;
443 return;
444 }
445
446 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000447 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000448 // Copy over args.
449 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
450 NewSubExprs[i] = SubExprs[i];
451 // Null out new args.
452 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
453 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Douglas Gregor88c9a462009-04-17 21:46:47 +0000455 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000456 SubExprs = NewSubExprs;
457 this->NumArgs = NumArgs;
458}
459
Chris Lattnercb888962008-10-06 05:00:53 +0000460/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
461/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000462unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000463 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000464 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000465 // ImplicitCastExpr.
466 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
467 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000468 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000470 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
471 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000472 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Anders Carlssonbcba2012008-01-31 02:13:57 +0000474 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
475 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000476 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000478 if (!FDecl->getIdentifier())
479 return 0;
480
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000481 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000482}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000483
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000484QualType CallExpr::getCallReturnType() const {
485 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000486 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000487 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000488 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000489 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000490
John McCall183700f2009-09-21 23:43:11 +0000491 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000492 return FnType->getResultType();
493}
Chris Lattnercb888962008-10-06 05:00:53 +0000494
Mike Stump1eb44332009-09-09 15:08:12 +0000495MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
496 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000497 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000498 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000499 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000500 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000501 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000502 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000503 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000504
John McCall161755a2010-04-06 21:38:20 +0000505 bool hasQualOrFound = (qual != 0 ||
506 founddecl.getDecl() != memberdecl ||
507 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000508 if (hasQualOrFound)
509 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
John McCalld5532b62009-11-23 01:53:49 +0000511 if (targs)
512 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000514 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000515 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
516
517 if (hasQualOrFound) {
518 if (qual && qual->isDependent()) {
519 E->setValueDependent(true);
520 E->setTypeDependent(true);
521 }
522 E->HasQualifierOrFoundDecl = true;
523
524 MemberNameQualifier *NQ = E->getMemberQualifier();
525 NQ->NNS = qual;
526 NQ->Range = qualrange;
527 NQ->FoundDecl = founddecl;
528 }
529
530 if (targs) {
531 E->HasExplicitTemplateArgumentList = true;
532 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
533 }
534
535 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000536}
537
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000538const char *CastExpr::getCastKindName() const {
539 switch (getCastKind()) {
540 case CastExpr::CK_Unknown:
541 return "Unknown";
542 case CastExpr::CK_BitCast:
543 return "BitCast";
544 case CastExpr::CK_NoOp:
545 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000546 case CastExpr::CK_BaseToDerived:
547 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000548 case CastExpr::CK_DerivedToBase:
549 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000550 case CastExpr::CK_UncheckedDerivedToBase:
551 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000552 case CastExpr::CK_Dynamic:
553 return "Dynamic";
554 case CastExpr::CK_ToUnion:
555 return "ToUnion";
556 case CastExpr::CK_ArrayToPointerDecay:
557 return "ArrayToPointerDecay";
558 case CastExpr::CK_FunctionToPointerDecay:
559 return "FunctionToPointerDecay";
560 case CastExpr::CK_NullToMemberPointer:
561 return "NullToMemberPointer";
562 case CastExpr::CK_BaseToDerivedMemberPointer:
563 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000564 case CastExpr::CK_DerivedToBaseMemberPointer:
565 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000566 case CastExpr::CK_UserDefinedConversion:
567 return "UserDefinedConversion";
568 case CastExpr::CK_ConstructorConversion:
569 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000570 case CastExpr::CK_IntegralToPointer:
571 return "IntegralToPointer";
572 case CastExpr::CK_PointerToIntegral:
573 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000574 case CastExpr::CK_ToVoid:
575 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000576 case CastExpr::CK_VectorSplat:
577 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000578 case CastExpr::CK_IntegralCast:
579 return "IntegralCast";
580 case CastExpr::CK_IntegralToFloating:
581 return "IntegralToFloating";
582 case CastExpr::CK_FloatingToIntegral:
583 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000584 case CastExpr::CK_FloatingCast:
585 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000586 case CastExpr::CK_MemberPointerToBoolean:
587 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000588 case CastExpr::CK_AnyPointerToObjCPointerCast:
589 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000590 case CastExpr::CK_AnyPointerToBlockPointerCast:
591 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000594 assert(0 && "Unhandled cast kind!");
595 return 0;
596}
597
Douglas Gregor6eef5192009-12-14 19:27:10 +0000598Expr *CastExpr::getSubExprAsWritten() {
599 Expr *SubExpr = 0;
600 CastExpr *E = this;
601 do {
602 SubExpr = E->getSubExpr();
603
604 // Skip any temporary bindings; they're implicit.
605 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
606 SubExpr = Binder->getSubExpr();
607
608 // Conversions by constructor and conversion functions have a
609 // subexpression describing the call; strip it off.
610 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
611 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
612 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
613 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
614
615 // If the subexpression we're left with is an implicit cast, look
616 // through that, too.
617 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
618
619 return SubExpr;
620}
621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
623/// corresponds to, e.g. "<<=".
624const char *BinaryOperator::getOpcodeStr(Opcode Op) {
625 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000626 case PtrMemD: return ".*";
627 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 case Mul: return "*";
629 case Div: return "/";
630 case Rem: return "%";
631 case Add: return "+";
632 case Sub: return "-";
633 case Shl: return "<<";
634 case Shr: return ">>";
635 case LT: return "<";
636 case GT: return ">";
637 case LE: return "<=";
638 case GE: return ">=";
639 case EQ: return "==";
640 case NE: return "!=";
641 case And: return "&";
642 case Xor: return "^";
643 case Or: return "|";
644 case LAnd: return "&&";
645 case LOr: return "||";
646 case Assign: return "=";
647 case MulAssign: return "*=";
648 case DivAssign: return "/=";
649 case RemAssign: return "%=";
650 case AddAssign: return "+=";
651 case SubAssign: return "-=";
652 case ShlAssign: return "<<=";
653 case ShrAssign: return ">>=";
654 case AndAssign: return "&=";
655 case XorAssign: return "^=";
656 case OrAssign: return "|=";
657 case Comma: return ",";
658 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000659
660 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000661}
662
Mike Stump1eb44332009-09-09 15:08:12 +0000663BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000664BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
665 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000666 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000667 case OO_Plus: return Add;
668 case OO_Minus: return Sub;
669 case OO_Star: return Mul;
670 case OO_Slash: return Div;
671 case OO_Percent: return Rem;
672 case OO_Caret: return Xor;
673 case OO_Amp: return And;
674 case OO_Pipe: return Or;
675 case OO_Equal: return Assign;
676 case OO_Less: return LT;
677 case OO_Greater: return GT;
678 case OO_PlusEqual: return AddAssign;
679 case OO_MinusEqual: return SubAssign;
680 case OO_StarEqual: return MulAssign;
681 case OO_SlashEqual: return DivAssign;
682 case OO_PercentEqual: return RemAssign;
683 case OO_CaretEqual: return XorAssign;
684 case OO_AmpEqual: return AndAssign;
685 case OO_PipeEqual: return OrAssign;
686 case OO_LessLess: return Shl;
687 case OO_GreaterGreater: return Shr;
688 case OO_LessLessEqual: return ShlAssign;
689 case OO_GreaterGreaterEqual: return ShrAssign;
690 case OO_EqualEqual: return EQ;
691 case OO_ExclaimEqual: return NE;
692 case OO_LessEqual: return LE;
693 case OO_GreaterEqual: return GE;
694 case OO_AmpAmp: return LAnd;
695 case OO_PipePipe: return LOr;
696 case OO_Comma: return Comma;
697 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000698 }
699}
700
701OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
702 static const OverloadedOperatorKind OverOps[] = {
703 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
704 OO_Star, OO_Slash, OO_Percent,
705 OO_Plus, OO_Minus,
706 OO_LessLess, OO_GreaterGreater,
707 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
708 OO_EqualEqual, OO_ExclaimEqual,
709 OO_Amp,
710 OO_Caret,
711 OO_Pipe,
712 OO_AmpAmp,
713 OO_PipePipe,
714 OO_Equal, OO_StarEqual,
715 OO_SlashEqual, OO_PercentEqual,
716 OO_PlusEqual, OO_MinusEqual,
717 OO_LessLessEqual, OO_GreaterGreaterEqual,
718 OO_AmpEqual, OO_CaretEqual,
719 OO_PipeEqual,
720 OO_Comma
721 };
722 return OverOps[Opc];
723}
724
Ted Kremenek709210f2010-04-13 23:39:13 +0000725InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000726 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000727 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000728 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000729 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000730 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenekba7bc552010-02-19 01:50:18 +0000731 UnionFieldInit(0), HadArrayRangeDesignator(false)
732{
733 for (unsigned I = 0; I != numInits; ++I) {
734 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000735 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000736 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000737 ValueDependent = true;
738 }
Ted Kremenekba7bc552010-02-19 01:50:18 +0000739
Ted Kremenek709210f2010-04-13 23:39:13 +0000740 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000741}
Reid Spencer5f016e22007-07-11 17:01:13 +0000742
Ted Kremenek709210f2010-04-13 23:39:13 +0000743void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000744 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000745 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000746}
747
Ted Kremenek709210f2010-04-13 23:39:13 +0000748void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000749 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
750 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000751 InitExprs[Idx]->Destroy(C);
752 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000753}
754
Ted Kremenek709210f2010-04-13 23:39:13 +0000755Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000756 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000757 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000758 InitExprs.back() = expr;
759 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Douglas Gregor4c678342009-01-28 21:54:33 +0000762 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
763 InitExprs[Init] = expr;
764 return Result;
765}
766
Steve Naroffbfdcae62008-09-04 15:31:07 +0000767/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000768///
769const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000770 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000771 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000772}
773
Mike Stump1eb44332009-09-09 15:08:12 +0000774SourceLocation BlockExpr::getCaretLocation() const {
775 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000776}
Mike Stump1eb44332009-09-09 15:08:12 +0000777const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000778 return TheBlock->getBody();
779}
Mike Stump1eb44332009-09-09 15:08:12 +0000780Stmt *BlockExpr::getBody() {
781 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000782}
Steve Naroff56ee6892008-10-08 17:01:13 +0000783
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785//===----------------------------------------------------------------------===//
786// Generic Expression Routines
787//===----------------------------------------------------------------------===//
788
Chris Lattner026dc962009-02-14 07:37:35 +0000789/// isUnusedResultAWarning - Return true if this immediate expression should
790/// be warned about if the result is unused. If so, fill in Loc and Ranges
791/// with location to warn on and the source range[s] to report with the
792/// warning.
793bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000794 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000795 // Don't warn if the expr is type dependent. The type could end up
796 // instantiating to void.
797 if (isTypeDependent())
798 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 switch (getStmtClass()) {
801 default:
John McCall0faede62010-03-12 07:11:26 +0000802 if (getType()->isVoidType())
803 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000804 Loc = getExprLoc();
805 R1 = getSourceRange();
806 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000808 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000809 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 case UnaryOperatorClass: {
811 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000814 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 case UnaryOperator::PostInc:
816 case UnaryOperator::PostDec:
817 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000818 case UnaryOperator::PreDec: // ++/--
819 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case UnaryOperator::Deref:
821 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000822 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000823 return false;
824 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 case UnaryOperator::Real:
826 case UnaryOperator::Imag:
827 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000828 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
829 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000830 return false;
831 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000833 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 }
Chris Lattner026dc962009-02-14 07:37:35 +0000835 Loc = UO->getOperatorLoc();
836 R1 = UO->getSubExpr()->getSourceRange();
837 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000839 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000840 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000841 switch (BO->getOpcode()) {
842 default:
843 break;
844 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
845 case BinaryOperator::Comma:
846 // ((foo = <blah>), 0) is an idiom for hiding the result (and
847 // lvalue-ness) of an assignment written in a macro.
848 if (IntegerLiteral *IE =
849 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
850 if (IE->getValue() == 0)
851 return false;
852 case BinaryOperator::LAnd:
853 case BinaryOperator::LOr:
854 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
855 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000856 }
Chris Lattner026dc962009-02-14 07:37:35 +0000857 if (BO->isAssignmentOp())
858 return false;
859 Loc = BO->getOperatorLoc();
860 R1 = BO->getLHS()->getSourceRange();
861 R2 = BO->getRHS()->getSourceRange();
862 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000863 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000864 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000865 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000866
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000867 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000868 // The condition must be evaluated, but if either the LHS or RHS is a
869 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000870 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000871 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000872 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000873 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000874 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000875 }
876
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000878 // If the base pointer or element is to a volatile pointer/field, accessing
879 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000880 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000881 return false;
882 Loc = cast<MemberExpr>(this)->getMemberLoc();
883 R1 = SourceRange(Loc, Loc);
884 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
885 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 case ArraySubscriptExprClass:
888 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000889 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000890 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000891 return false;
892 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
893 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
894 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
895 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000896
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000898 case CXXOperatorCallExprClass:
899 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000900 // If this is a direct call, get the callee.
901 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000902 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000903 // If the callee has attribute pure, const, or warn_unused_result, warn
904 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000905 //
906 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
907 // updated to match for QoI.
908 if (FD->getAttr<WarnUnusedResultAttr>() ||
909 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
910 Loc = CE->getCallee()->getLocStart();
911 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000913 if (unsigned NumArgs = CE->getNumArgs())
914 R2 = SourceRange(CE->getArg(0)->getLocStart(),
915 CE->getArg(NumArgs-1)->getLocEnd());
916 return true;
917 }
Chris Lattner026dc962009-02-14 07:37:35 +0000918 }
919 return false;
920 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000921
922 case CXXTemporaryObjectExprClass:
923 case CXXConstructExprClass:
924 return false;
925
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000926 case ObjCMessageExprClass: {
927 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
928 const ObjCMethodDecl *MD = ME->getMethodDecl();
929 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
930 Loc = getExprLoc();
931 return true;
932 }
Chris Lattner026dc962009-02-14 07:37:35 +0000933 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000936 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000937#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000938 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000939 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000940 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000941 Loc = Ref->getLocation();
942 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
943 if (Ref->getBase())
944 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000945#else
946 Loc = getExprLoc();
947 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000948#endif
949 return true;
950 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000951 case StmtExprClass: {
952 // Statement exprs don't logically have side effects themselves, but are
953 // sometimes used in macros in ways that give them a type that is unused.
954 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
955 // however, if the result of the stmt expr is dead, we don't want to emit a
956 // warning.
957 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
958 if (!CS->body_empty())
959 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000960 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
John McCall0faede62010-03-12 07:11:26 +0000962 if (getType()->isVoidType())
963 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000964 Loc = cast<StmtExpr>(this)->getLParenLoc();
965 R1 = getSourceRange();
966 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000967 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000968 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000969 // If this is an explicit cast to void, allow it. People do this when they
970 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000971 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000972 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000973 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
974 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
975 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000976 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +0000977 if (getType()->isVoidType())
978 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +0000979 const CastExpr *CE = cast<CastExpr>(this);
980
981 // If this is a cast to void or a constructor conversion, check the operand.
982 // Otherwise, the result of the cast is unused.
983 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
984 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000985 return (cast<CastExpr>(this)->getSubExpr()
986 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000987 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
988 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
989 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Eli Friedman4be1f472008-05-19 21:24:43 +0000992 case ImplicitCastExprClass:
993 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000994 return (cast<ImplicitCastExpr>(this)
995 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000996
Chris Lattner04421082008-04-08 04:40:51 +0000997 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000998 return (cast<CXXDefaultArgExpr>(this)
999 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001000
1001 case CXXNewExprClass:
1002 // FIXME: In theory, there might be new expressions that don't have side
1003 // effects (e.g. a placement new with an uninitialized POD).
1004 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001005 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001006 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001007 return (cast<CXXBindTemporaryExpr>(this)
1008 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001009 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001010 return (cast<CXXExprWithTemporaries>(this)
1011 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001012 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001013}
1014
Douglas Gregorba7e2102008-10-22 15:04:37 +00001015/// DeclCanBeLvalue - Determine whether the given declaration can be
1016/// an lvalue. This is a helper routine for isLvalue.
1017static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001018 // C++ [temp.param]p6:
1019 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001020 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001021 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1022 return NTTParm->getType()->isReferenceType();
1023
Douglas Gregor44b43212008-12-11 16:49:14 +00001024 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001025 // C++ 3.10p2: An lvalue refers to an object or function.
1026 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001027 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001028}
1029
Reid Spencer5f016e22007-07-11 17:01:13 +00001030/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1031/// incomplete type other than void. Nonarray expressions that can be lvalues:
1032/// - name, where name must be a variable
1033/// - e[i]
1034/// - (e), where e must be an lvalue
1035/// - e.name, where e must be an lvalue
1036/// - e->name
1037/// - *e, the type of e cannot be a function type
1038/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001039/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001040/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001041///
Chris Lattner28be73f2008-07-26 21:30:36 +00001042Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001043 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1044
1045 isLvalueResult Res = isLvalueInternal(Ctx);
1046 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1047 return Res;
1048
Douglas Gregor98cd5992008-10-21 23:43:52 +00001049 // first, check the type (C99 6.3.2.1). Expressions with function
1050 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001051 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 return LV_NotObjectType;
1053
Steve Naroffacb818a2008-02-10 01:39:04 +00001054 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001055 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001056 return LV_IncompleteVoidType;
1057
Eli Friedman53202852009-05-03 22:36:05 +00001058 return LV_Valid;
1059}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001060
Eli Friedman53202852009-05-03 22:36:05 +00001061// Check whether the expression can be sanely treated like an l-value
1062Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001064 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001065 case StringLiteralClass: // C99 6.5.1p4
1066 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001067 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1069 // For vectors, make sure base is an lvalue (i.e. not a function call).
1070 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001071 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001073 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001074 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1075 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 return LV_Valid;
1077 break;
Chris Lattner41110242008-06-17 18:05:57 +00001078 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001079 case BlockDeclRefExprClass: {
1080 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001081 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001082 return LV_Valid;
1083 break;
1084 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001085 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001087 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1088 NamedDecl *Member = m->getMemberDecl();
1089 // C++ [expr.ref]p4:
1090 // If E2 is declared to have type "reference to T", then E1.E2
1091 // is an lvalue.
1092 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1093 if (Value->getType()->isReferenceType())
1094 return LV_Valid;
1095
1096 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001097 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001098 return LV_Valid;
1099
1100 // -- If E2 is a non-static data member [...]. If E1 is an
1101 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001102 if (isa<FieldDecl>(Member)) {
1103 if (m->isArrow())
1104 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001105 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001106 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001107
1108 // -- If it refers to a static member function [...], then
1109 // E1.E2 is an lvalue.
1110 // -- Otherwise, if E1.E2 refers to a non-static member
1111 // function [...], then E1.E2 is not an lvalue.
1112 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1113 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1114
1115 // -- If E2 is a member enumerator [...], the expression E1.E2
1116 // is not an lvalue.
1117 if (isa<EnumConstantDecl>(Member))
1118 return LV_InvalidExpression;
1119
1120 // Not an lvalue.
1121 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001122 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001123
Douglas Gregor86f19402008-12-20 23:49:58 +00001124 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001125 if (m->isArrow())
1126 return LV_Valid;
1127 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001128 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1129 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001130 return LV_SubObjCPropertySetting;
1131 return
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001132 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001133 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001134 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001136 return LV_Valid; // C99 6.5.3p4
1137
1138 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001139 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1140 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001141 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001142
1143 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1144 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1145 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1146 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001148 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001149 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1150 return LV_Valid;
1151
1152 // If this is a conversion to a class temporary, make a note of
1153 // that.
1154 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1155 return LV_ClassTemporary;
1156
1157 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001159 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001160 case BinaryOperatorClass:
1161 case CompoundAssignOperatorClass: {
1162 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001163
1164 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1165 BinOp->getOpcode() == BinaryOperator::Comma)
1166 return BinOp->getRHS()->isLvalue(Ctx);
1167
Sebastian Redl22460502009-02-07 00:15:38 +00001168 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001169 // The result of a .* expression is an lvalue only if its first operand is
1170 // an lvalue and its second operand is a pointer to data member.
1171 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001172 !BinOp->getType()->isFunctionType())
1173 return BinOp->getLHS()->isLvalue(Ctx);
1174
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001175 // The result of an ->* expression is an lvalue only if its second operand
1176 // is a pointer to data member.
1177 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1178 !BinOp->getType()->isFunctionType()) {
1179 QualType Ty = BinOp->getRHS()->getType();
1180 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1181 return LV_Valid;
1182 }
1183
Douglas Gregorbf3af052008-11-13 20:12:29 +00001184 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001185 return LV_InvalidExpression;
1186
Douglas Gregorbf3af052008-11-13 20:12:29 +00001187 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001188 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001189 // The result of an assignment operation [...] is an lvalue.
1190 return LV_Valid;
1191
1192
1193 // C99 6.5.16:
1194 // An assignment expression [...] is not an lvalue.
1195 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001198 case CXXOperatorCallExprClass:
1199 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001200 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001201 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001202 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001203 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1204 if (ReturnType->isLValueReferenceType())
1205 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001206
Douglas Gregore873fb72010-02-16 21:39:57 +00001207 // If the function is returning a class temporary, make a note of
1208 // that.
1209 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1210 return LV_ClassTemporary;
1211
Douglas Gregor9d293df2008-10-28 00:22:11 +00001212 break;
1213 }
Steve Naroffe6386392007-12-05 04:00:10 +00001214 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001215 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001216 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001217 case ChooseExprClass:
1218 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001219 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001220 case ExtVectorElementExprClass:
1221 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001222 return LV_DuplicateVectorComponents;
1223 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001224 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1225 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001226 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1227 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001228 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001229 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001230 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001231 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001232 case UnresolvedLookupExprClass:
1233 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001234 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001235 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001236 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001237 case CXXFunctionalCastExprClass:
1238 case CXXStaticCastExprClass:
1239 case CXXDynamicCastExprClass:
1240 case CXXReinterpretCastExprClass:
1241 case CXXConstCastExprClass:
1242 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001243 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001244 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1245 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1247 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001248 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001249
1250 // If this is a conversion to a class temporary, make a note of
1251 // that.
1252 if (Ctx.getLangOptions().CPlusPlus &&
1253 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1254 return LV_ClassTemporary;
1255
Douglas Gregor9d293df2008-10-28 00:22:11 +00001256 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001257 case CXXTypeidExprClass:
1258 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1259 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001260 case CXXBindTemporaryExprClass:
1261 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1262 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001263 case CXXBindReferenceExprClass:
1264 // Something that's bound to a reference is always an lvalue.
1265 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001266 case ConditionalOperatorClass: {
1267 // Complicated handling is only for C++.
1268 if (!Ctx.getLangOptions().CPlusPlus)
1269 return LV_InvalidExpression;
1270
1271 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1272 // everywhere there's an object converted to an rvalue. Also, any other
1273 // casts should be wrapped by ImplicitCastExprs. There's just the special
1274 // case involving throws to work out.
1275 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001276 Expr *True = Cond->getTrueExpr();
1277 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001278 // C++0x 5.16p2
1279 // If either the second or the third operand has type (cv) void, [...]
1280 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001281 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001282 return LV_InvalidExpression;
1283
1284 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001285 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001286 return LV_InvalidExpression;
1287
1288 // That's it.
1289 return LV_Valid;
1290 }
1291
Douglas Gregor2d48e782009-12-19 07:07:47 +00001292 case Expr::CXXExprWithTemporariesClass:
1293 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1294
1295 case Expr::ObjCMessageExprClass:
1296 if (const ObjCMethodDecl *Method
1297 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1298 if (Method->getResultType()->isLValueReferenceType())
1299 return LV_Valid;
1300 break;
1301
Douglas Gregore873fb72010-02-16 21:39:57 +00001302 case Expr::CXXConstructExprClass:
1303 case Expr::CXXTemporaryObjectExprClass:
1304 case Expr::CXXZeroInitValueExprClass:
1305 return LV_ClassTemporary;
1306
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 default:
1308 break;
1309 }
1310 return LV_InvalidExpression;
1311}
1312
1313/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1314/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001315/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001316/// recursively, any member or element of all contained aggregates or unions)
1317/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001318Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001319Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001320 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001323 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001324 // C++ 3.10p11: Functions cannot be modified, but pointers to
1325 // functions can be modifiable.
1326 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1327 return MLV_NotObjectType;
1328 break;
1329
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 case LV_NotObjectType: return MLV_NotObjectType;
1331 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001332 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001333 case LV_InvalidExpression:
1334 // If the top level is a C-style cast, and the subexpression is a valid
1335 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1336 // GCC extension. We don't support it, but we want to produce good
1337 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001338 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1339 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1340 if (Loc)
1341 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001342 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001343 }
1344 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001345 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001346 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001347 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001348 case LV_ClassTemporary:
1349 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001351
1352 // The following is illegal:
1353 // void takeclosure(void (^C)(void));
1354 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1355 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001356 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001357 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1358 return MLV_NotBlockQualified;
1359 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001360
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001361 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001362 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001363 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1364 if (Expr->getSetterMethod() == 0)
1365 return MLV_NoSetterProperty;
1366 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001367
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001368 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001370 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001372 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001374 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Ted Kremenek6217b802009-07-29 21:53:49 +00001377 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001378 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 return MLV_ConstQualified;
1380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Mike Stump1eb44332009-09-09 15:08:12 +00001382 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001383}
1384
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001385/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001386/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001387bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001388 switch (getStmtClass()) {
1389 default:
1390 return false;
1391 case ObjCIvarRefExprClass:
1392 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001393 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001394 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001395 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001396 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001397 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001398 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001399 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001400 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001401 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001402 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001403 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1404 if (VD->hasGlobalStorage())
1405 return true;
1406 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001407 // dereferencing to a pointer is always a gc'able candidate,
1408 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001409 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001410 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001411 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001412 return false;
1413 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001414 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001415 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001416 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001417 }
1418 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001419 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001420 }
1421}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001422Expr* Expr::IgnoreParens() {
1423 Expr* E = this;
1424 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1425 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001427 return E;
1428}
1429
Chris Lattner56f34942008-02-13 01:02:39 +00001430/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1431/// or CastExprs or ImplicitCastExprs, returning their operand.
1432Expr *Expr::IgnoreParenCasts() {
1433 Expr *E = this;
1434 while (true) {
1435 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1436 E = P->getSubExpr();
1437 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1438 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001439 else
1440 return E;
1441 }
1442}
1443
Chris Lattnerecdd8412009-03-13 17:28:01 +00001444/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1445/// value (including ptr->int casts of the same size). Strip off any
1446/// ParenExpr or CastExprs, returning their operand.
1447Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1448 Expr *E = this;
1449 while (true) {
1450 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1451 E = P->getSubExpr();
1452 continue;
1453 }
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Chris Lattnerecdd8412009-03-13 17:28:01 +00001455 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1456 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1457 // ptr<->int casts of the same width. We also ignore all identify casts.
1458 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Chris Lattnerecdd8412009-03-13 17:28:01 +00001460 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1461 E = SE;
1462 continue;
1463 }
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Chris Lattnerecdd8412009-03-13 17:28:01 +00001465 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1466 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1467 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1468 E = SE;
1469 continue;
1470 }
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Chris Lattnerecdd8412009-03-13 17:28:01 +00001473 return E;
1474 }
1475}
1476
Douglas Gregor6eef5192009-12-14 19:27:10 +00001477bool Expr::isDefaultArgument() const {
1478 const Expr *E = this;
1479 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1480 E = ICE->getSubExprAsWritten();
1481
1482 return isa<CXXDefaultArgExpr>(E);
1483}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001484
Douglas Gregor2f599792010-04-02 18:24:57 +00001485/// \brief Skip over any no-op casts and any temporary-binding
1486/// expressions.
1487static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1488 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1489 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1490 E = ICE->getSubExpr();
1491 else
1492 break;
1493 }
1494
1495 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1496 E = BE->getSubExpr();
1497
1498 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1499 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1500 E = ICE->getSubExpr();
1501 else
1502 break;
1503 }
1504
1505 return E;
1506}
1507
1508const Expr *Expr::getTemporaryObject() const {
1509 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1510
1511 // A cast can produce a temporary object. The object's construction
1512 // is represented as a CXXConstructExpr.
1513 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1514 // Only user-defined and constructor conversions can produce
1515 // temporary objects.
1516 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1517 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1518 return 0;
1519
1520 // Strip off temporary bindings and no-op casts.
1521 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1522
1523 // If this is a constructor conversion, see if we have an object
1524 // construction.
1525 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1526 return dyn_cast<CXXConstructExpr>(Sub);
1527
1528 // If this is a user-defined conversion, see if we have a call to
1529 // a function that itself returns a temporary object.
1530 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1531 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1532 if (CE->getCallReturnType()->isRecordType())
1533 return CE;
1534
1535 return 0;
1536 }
1537
1538 // A call returning a class type returns a temporary.
1539 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1540 if (CE->getCallReturnType()->isRecordType())
1541 return CE;
1542
1543 return 0;
1544 }
1545
1546 // Explicit temporary object constructors create temporaries.
1547 return dyn_cast<CXXTemporaryObjectExpr>(E);
1548}
1549
Douglas Gregor898574e2008-12-05 23:32:09 +00001550/// hasAnyTypeDependentArguments - Determines if any of the expressions
1551/// in Exprs is type-dependent.
1552bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1553 for (unsigned I = 0; I < NumExprs; ++I)
1554 if (Exprs[I]->isTypeDependent())
1555 return true;
1556
1557 return false;
1558}
1559
1560/// hasAnyValueDependentArguments - Determines if any of the expressions
1561/// in Exprs is value-dependent.
1562bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1563 for (unsigned I = 0; I < NumExprs; ++I)
1564 if (Exprs[I]->isValueDependent())
1565 return true;
1566
1567 return false;
1568}
1569
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001570bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001571 // This function is attempting whether an expression is an initializer
1572 // which can be evaluated at compile-time. isEvaluatable handles most
1573 // of the cases, but it can't deal with some initializer-specific
1574 // expressions, and it can't deal with aggregates; we deal with those here,
1575 // and fall back to isEvaluatable for the other cases.
1576
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001577 // FIXME: This function assumes the variable being assigned to
1578 // isn't a reference type!
1579
Anders Carlssone8a32b82008-11-24 05:23:59 +00001580 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001581 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001582 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001583 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001584 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001585 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001586 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001587 // This handles gcc's extension that allows global initializers like
1588 // "struct x {int x;} x = (struct x) {};".
1589 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001590 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001591 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001592 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001593 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001594 // FIXME: This doesn't deal with fields with reference types correctly.
1595 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1596 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001597 const InitListExpr *Exp = cast<InitListExpr>(this);
1598 unsigned numInits = Exp->getNumInits();
1599 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001600 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001601 return false;
1602 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001603 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001604 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001605 case ImplicitValueInitExprClass:
1606 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001607 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001608 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001609 case UnaryOperatorClass: {
1610 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1611 if (Exp->getOpcode() == UnaryOperator::Extension)
1612 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1613 break;
1614 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001615 case BinaryOperatorClass: {
1616 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1617 // but this handles the common case.
1618 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1619 if (Exp->getOpcode() == BinaryOperator::Sub &&
1620 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1621 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1622 return true;
1623 break;
1624 }
Chris Lattner81045d82009-04-21 05:19:11 +00001625 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001626 case CStyleCastExprClass:
1627 // Handle casts with a destination that's a struct or union; this
1628 // deals with both the gcc no-op struct cast extension and the
1629 // cast-to-union extension.
1630 if (getType()->isRecordType())
1631 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001632
1633 // Integer->integer casts can be handled here, which is important for
1634 // things like (int)(&&x-&&y). Scary but true.
1635 if (getType()->isIntegerType() &&
1636 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1637 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1638
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001639 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001640 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001641 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001642}
1643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001645/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001646
1647/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1648/// comma, etc
1649///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001650/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1651/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1652/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001653
Eli Friedmane28d7192009-02-26 09:29:13 +00001654// CheckICE - This function does the fundamental ICE checking: the returned
1655// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1656// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001657// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001658// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001659// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001660//
1661// Meanings of Val:
1662// 0: This expression is an ICE if it can be evaluated by Evaluate.
1663// 1: This expression is not an ICE, but if it isn't evaluated, it's
1664// a legal subexpression for an ICE. This return value is used to handle
1665// the comma operator in C99 mode.
1666// 2: This expression is not an ICE, and is not a legal subexpression for one.
1667
1668struct ICEDiag {
1669 unsigned Val;
1670 SourceLocation Loc;
1671
1672 public:
1673 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1674 ICEDiag() : Val(0) {}
1675};
1676
1677ICEDiag NoDiag() { return ICEDiag(); }
1678
Eli Friedman60ce9632009-02-27 04:07:58 +00001679static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1680 Expr::EvalResult EVResult;
1681 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1682 !EVResult.Val.isInt()) {
1683 return ICEDiag(2, E->getLocStart());
1684 }
1685 return NoDiag();
1686}
1687
Eli Friedmane28d7192009-02-26 09:29:13 +00001688static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001689 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001690 if (!E->getType()->isIntegralType()) {
1691 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001692 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001693
1694 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001695#define STMT(Node, Base) case Expr::Node##Class:
1696#define EXPR(Node, Base)
1697#include "clang/AST/StmtNodes.def"
1698 case Expr::PredefinedExprClass:
1699 case Expr::FloatingLiteralClass:
1700 case Expr::ImaginaryLiteralClass:
1701 case Expr::StringLiteralClass:
1702 case Expr::ArraySubscriptExprClass:
1703 case Expr::MemberExprClass:
1704 case Expr::CompoundAssignOperatorClass:
1705 case Expr::CompoundLiteralExprClass:
1706 case Expr::ExtVectorElementExprClass:
1707 case Expr::InitListExprClass:
1708 case Expr::DesignatedInitExprClass:
1709 case Expr::ImplicitValueInitExprClass:
1710 case Expr::ParenListExprClass:
1711 case Expr::VAArgExprClass:
1712 case Expr::AddrLabelExprClass:
1713 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001714 case Expr::CXXMemberCallExprClass:
1715 case Expr::CXXDynamicCastExprClass:
1716 case Expr::CXXTypeidExprClass:
1717 case Expr::CXXNullPtrLiteralExprClass:
1718 case Expr::CXXThisExprClass:
1719 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001720 case Expr::CXXNewExprClass:
1721 case Expr::CXXDeleteExprClass:
1722 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001723 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001724 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001725 case Expr::CXXConstructExprClass:
1726 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001727 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001728 case Expr::CXXExprWithTemporariesClass:
1729 case Expr::CXXTemporaryObjectExprClass:
1730 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001731 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001732 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001733 case Expr::ObjCStringLiteralClass:
1734 case Expr::ObjCEncodeExprClass:
1735 case Expr::ObjCMessageExprClass:
1736 case Expr::ObjCSelectorExprClass:
1737 case Expr::ObjCProtocolExprClass:
1738 case Expr::ObjCIvarRefExprClass:
1739 case Expr::ObjCPropertyRefExprClass:
1740 case Expr::ObjCImplicitSetterGetterRefExprClass:
1741 case Expr::ObjCSuperExprClass:
1742 case Expr::ObjCIsaExprClass:
1743 case Expr::ShuffleVectorExprClass:
1744 case Expr::BlockExprClass:
1745 case Expr::BlockDeclRefExprClass:
1746 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001747 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001748
Douglas Gregor043cad22009-09-11 00:18:58 +00001749 case Expr::GNUNullExprClass:
1750 // GCC considers the GNU __null value to be an integral constant expression.
1751 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001752
Eli Friedmane28d7192009-02-26 09:29:13 +00001753 case Expr::ParenExprClass:
1754 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1755 case Expr::IntegerLiteralClass:
1756 case Expr::CharacterLiteralClass:
1757 case Expr::CXXBoolLiteralExprClass:
1758 case Expr::CXXZeroInitValueExprClass:
1759 case Expr::TypesCompatibleExprClass:
1760 case Expr::UnaryTypeTraitExprClass:
1761 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001762 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001763 case Expr::CXXOperatorCallExprClass: {
1764 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001765 if (CE->isBuiltinCall(Ctx))
1766 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001767 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001768 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001769 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001770 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1771 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001772 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001773 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001774 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1775
1776 // Parameter variables are never constants. Without this check,
1777 // getAnyInitializer() can find a default argument, which leads
1778 // to chaos.
1779 if (isa<ParmVarDecl>(D))
1780 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1781
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001782 // C++ 7.1.5.1p2
1783 // A variable of non-volatile const-qualified integral or enumeration
1784 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001785 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001786 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1787 if (Quals.hasVolatile() || !Quals.hasConst())
1788 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1789
Sebastian Redl31310a22010-02-01 20:16:42 +00001790 // Look for a declaration of this variable that has an initializer.
1791 const VarDecl *ID = 0;
1792 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001793 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001794 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001795 // We have already checked whether this subexpression is an
1796 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001797 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001798 return NoDiag();
1799 else
1800 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1801 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001802
John McCall1f1b3b32010-02-06 01:07:37 +00001803 // It's an ICE whether or not the definition we found is
1804 // out-of-line. See DR 721 and the discussion in Clang PR
1805 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001806
1807 if (Dcl->isCheckingICE()) {
1808 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1809 }
1810
1811 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001812 ICEDiag Result = CheckICE(Init, Ctx);
1813 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001814 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001815 return Result;
1816 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001817 }
1818 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001819 return ICEDiag(2, E->getLocStart());
1820 case Expr::UnaryOperatorClass: {
1821 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001823 case UnaryOperator::PostInc:
1824 case UnaryOperator::PostDec:
1825 case UnaryOperator::PreInc:
1826 case UnaryOperator::PreDec:
1827 case UnaryOperator::AddrOf:
1828 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001829 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001830
Reid Spencer5f016e22007-07-11 17:01:13 +00001831 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001832 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001836 case UnaryOperator::Real:
1837 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001838 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001839 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001840 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1841 // Evaluate matches the proposed gcc behavior for cases like
1842 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1843 // compliance: we should warn earlier for offsetof expressions with
1844 // array subscripts that aren't ICEs, and if the array subscripts
1845 // are ICEs, the value of the offsetof must be an integer constant.
1846 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001849 case Expr::SizeOfAlignOfExprClass: {
1850 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1851 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1852 return ICEDiag(2, E->getLocStart());
1853 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001855 case Expr::BinaryOperatorClass: {
1856 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001858 case BinaryOperator::PtrMemD:
1859 case BinaryOperator::PtrMemI:
1860 case BinaryOperator::Assign:
1861 case BinaryOperator::MulAssign:
1862 case BinaryOperator::DivAssign:
1863 case BinaryOperator::RemAssign:
1864 case BinaryOperator::AddAssign:
1865 case BinaryOperator::SubAssign:
1866 case BinaryOperator::ShlAssign:
1867 case BinaryOperator::ShrAssign:
1868 case BinaryOperator::AndAssign:
1869 case BinaryOperator::XorAssign:
1870 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001871 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001872
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001876 case BinaryOperator::Add:
1877 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001880 case BinaryOperator::LT:
1881 case BinaryOperator::GT:
1882 case BinaryOperator::LE:
1883 case BinaryOperator::GE:
1884 case BinaryOperator::EQ:
1885 case BinaryOperator::NE:
1886 case BinaryOperator::And:
1887 case BinaryOperator::Xor:
1888 case BinaryOperator::Or:
1889 case BinaryOperator::Comma: {
1890 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1891 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001892 if (Exp->getOpcode() == BinaryOperator::Div ||
1893 Exp->getOpcode() == BinaryOperator::Rem) {
1894 // Evaluate gives an error for undefined Div/Rem, so make sure
1895 // we don't evaluate one.
1896 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1897 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1898 if (REval == 0)
1899 return ICEDiag(1, E->getLocStart());
1900 if (REval.isSigned() && REval.isAllOnesValue()) {
1901 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1902 if (LEval.isMinSignedValue())
1903 return ICEDiag(1, E->getLocStart());
1904 }
1905 }
1906 }
1907 if (Exp->getOpcode() == BinaryOperator::Comma) {
1908 if (Ctx.getLangOptions().C99) {
1909 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1910 // if it isn't evaluated.
1911 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1912 return ICEDiag(1, E->getLocStart());
1913 } else {
1914 // In both C89 and C++, commas in ICEs are illegal.
1915 return ICEDiag(2, E->getLocStart());
1916 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001917 }
1918 if (LHSResult.Val >= RHSResult.Val)
1919 return LHSResult;
1920 return RHSResult;
1921 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001923 case BinaryOperator::LOr: {
1924 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1925 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1926 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1927 // Rare case where the RHS has a comma "side-effect"; we need
1928 // to actually check the condition to see whether the side
1929 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001930 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001931 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001932 return RHSResult;
1933 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001934 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001935
Eli Friedmane28d7192009-02-26 09:29:13 +00001936 if (LHSResult.Val >= RHSResult.Val)
1937 return LHSResult;
1938 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001940 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001941 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001942 case Expr::ImplicitCastExprClass:
1943 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001944 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001945 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001946 case Expr::CXXStaticCastExprClass:
1947 case Expr::CXXReinterpretCastExprClass:
1948 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001949 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1950 if (SubExpr->getType()->isIntegralType())
1951 return CheckICE(SubExpr, Ctx);
1952 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1953 return NoDiag();
1954 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001956 case Expr::ConditionalOperatorClass: {
1957 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001958 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001959 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001960 // expression, and it is fully evaluated. This is an important GNU
1961 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001962 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001963 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001964 Expr::EvalResult EVResult;
1965 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1966 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001967 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001968 }
1969 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001970 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001971 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1972 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1973 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1974 if (CondResult.Val == 2)
1975 return CondResult;
1976 if (TrueResult.Val == 2)
1977 return TrueResult;
1978 if (FalseResult.Val == 2)
1979 return FalseResult;
1980 if (CondResult.Val == 1)
1981 return CondResult;
1982 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1983 return NoDiag();
1984 // Rare case where the diagnostics depend on which side is evaluated
1985 // Note that if we get here, CondResult is 0, and at least one of
1986 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001987 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001988 return FalseResult;
1989 }
1990 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001992 case Expr::CXXDefaultArgExprClass:
1993 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001994 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001995 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001996 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001998
Douglas Gregorf2991242009-09-10 23:31:45 +00001999 // Silence a GCC warning
2000 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002001}
Reid Spencer5f016e22007-07-11 17:01:13 +00002002
Eli Friedmane28d7192009-02-26 09:29:13 +00002003bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2004 SourceLocation *Loc, bool isEvaluated) const {
2005 ICEDiag d = CheckICE(this, Ctx);
2006 if (d.Val != 0) {
2007 if (Loc) *Loc = d.Loc;
2008 return false;
2009 }
2010 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00002011 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002012 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00002013 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2014 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00002015 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00002016 return true;
2017}
2018
Reid Spencer5f016e22007-07-11 17:01:13 +00002019/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2020/// integer constant expression with the value zero, or if this is one that is
2021/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002022bool Expr::isNullPointerConstant(ASTContext &Ctx,
2023 NullPointerConstantValueDependence NPC) const {
2024 if (isValueDependent()) {
2025 switch (NPC) {
2026 case NPC_NeverValueDependent:
2027 assert(false && "Unexpected value dependent expression!");
2028 // If the unthinkable happens, fall through to the safest alternative.
2029
2030 case NPC_ValueDependentIsNull:
2031 return isTypeDependent() || getType()->isIntegralType();
2032
2033 case NPC_ValueDependentIsNotNull:
2034 return false;
2035 }
2036 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002037
Sebastian Redl07779722008-10-31 14:43:28 +00002038 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002039 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002040 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002041 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002042 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002043 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002044 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002045 Pointee->isVoidType() && // to void*
2046 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002047 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002048 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002049 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002050 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2051 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002052 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002053 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2054 // Accept ((void*)0) as a null pointer constant, as many other
2055 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002056 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002057 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002058 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002059 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002060 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002061 } else if (isa<GNUNullExpr>(this)) {
2062 // The GNU __null extension is always a null pointer constant.
2063 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002064 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002065
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002066 // C++0x nullptr_t is always a null pointer constant.
2067 if (getType()->isNullPtrType())
2068 return true;
2069
Steve Naroffaa58f002008-01-14 16:10:57 +00002070 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002071 if (!getType()->isIntegerType() ||
2072 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002073 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 // If we have an integer constant expression, we need to *evaluate* it and
2076 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002077 llvm::APSInt Result;
2078 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002079}
Steve Naroff31a45842007-07-28 23:10:27 +00002080
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002081FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002082 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002083
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002084 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2085 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2086 E = ICE->getSubExpr()->IgnoreParens();
2087 else
2088 break;
2089 }
2090
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002091 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002092 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002093 if (Field->isBitField())
2094 return Field;
2095
2096 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2097 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2098 return BinOp->getLHS()->getBitField();
2099
2100 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002101}
2102
Anders Carlsson09380262010-01-31 17:18:49 +00002103bool Expr::refersToVectorElement() const {
2104 const Expr *E = this->IgnoreParens();
2105
2106 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2107 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2108 E = ICE->getSubExpr()->IgnoreParens();
2109 else
2110 break;
2111 }
2112
2113 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2114 return ASE->getBase()->getType()->isVectorType();
2115
2116 if (isa<ExtVectorElementExpr>(E))
2117 return true;
2118
2119 return false;
2120}
2121
Chris Lattner2140e902009-02-16 22:14:05 +00002122/// isArrow - Return true if the base expression is a pointer to vector,
2123/// return false if the base expression is a vector.
2124bool ExtVectorElementExpr::isArrow() const {
2125 return getBase()->getType()->isPointerType();
2126}
2127
Nate Begeman213541a2008-04-18 23:10:10 +00002128unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002129 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002130 return VT->getNumElements();
2131 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002132}
2133
Nate Begeman8a997642008-05-09 06:41:27 +00002134/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002135bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002136 // FIXME: Refactor this code to an accessor on the AST node which returns the
2137 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002138 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002139
2140 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002141 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002142 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Nate Begeman190d6a22009-01-18 02:01:21 +00002144 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002145 if (Comp[0] == 's' || Comp[0] == 'S')
2146 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Daniel Dunbar15027422009-10-17 23:53:04 +00002148 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2149 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002150 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002151
Steve Narofffec0b492007-07-30 03:29:09 +00002152 return false;
2153}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002154
Nate Begeman8a997642008-05-09 06:41:27 +00002155/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002156void ExtVectorElementExpr::getEncodedElementAccess(
2157 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002158 llvm::StringRef Comp = Accessor->getName();
2159 if (Comp[0] == 's' || Comp[0] == 'S')
2160 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002162 bool isHi = Comp == "hi";
2163 bool isLo = Comp == "lo";
2164 bool isEven = Comp == "even";
2165 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Nate Begeman8a997642008-05-09 06:41:27 +00002167 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2168 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002169
Nate Begeman8a997642008-05-09 06:41:27 +00002170 if (isHi)
2171 Index = e + i;
2172 else if (isLo)
2173 Index = i;
2174 else if (isEven)
2175 Index = 2 * i;
2176 else if (isOdd)
2177 Index = 2 * i + 1;
2178 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002179 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002180
Nate Begeman3b8d1162008-05-13 21:03:02 +00002181 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002182 }
Nate Begeman8a997642008-05-09 06:41:27 +00002183}
2184
Steve Naroff68d331a2007-09-27 14:38:14 +00002185// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002186ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2187 Selector selInfo,
2188 QualType retType, ObjCMethodDecl *mproto,
2189 SourceLocation LBrac, SourceLocation RBrac,
2190 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002191 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002192 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002193 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002194 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002195 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002196 if (NumArgs) {
2197 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002198 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2199 }
Steve Naroff563477d2007-09-18 23:55:05 +00002200 LBracloc = LBrac;
2201 RBracloc = RBrac;
2202}
2203
Mike Stump1eb44332009-09-09 15:08:12 +00002204// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002205// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002206ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002207 SourceLocation clsNameLoc, Selector selInfo,
2208 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002209 SourceLocation LBrac, SourceLocation RBrac,
2210 Expr **ArgExprs, unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002211 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2212 SelName(selInfo), MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002213 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002214 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002215 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002216 if (NumArgs) {
2217 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002218 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2219 }
Steve Naroff563477d2007-09-18 23:55:05 +00002220 LBracloc = LBrac;
2221 RBracloc = RBrac;
2222}
2223
Mike Stump1eb44332009-09-09 15:08:12 +00002224// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002225ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002226 SourceLocation clsNameLoc, Selector selInfo,
2227 QualType retType,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002228 ObjCMethodDecl *mproto, SourceLocation LBrac,
2229 SourceLocation RBrac, Expr **ArgExprs,
2230 unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002231 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2232 SelName(selInfo), MethodProto(mproto)
2233{
Ted Kremenek4df728e2008-06-24 15:50:53 +00002234 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002235 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002236 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2237 if (NumArgs) {
2238 for (unsigned i = 0; i != NumArgs; ++i)
2239 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2240 }
2241 LBracloc = LBrac;
2242 RBracloc = RBrac;
2243}
2244
2245ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2246 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2247 switch (x & Flags) {
2248 default:
2249 assert(false && "Invalid ObjCMessageExpr.");
2250 case IsInstMeth:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002251 return ClassInfo(0, 0, SourceLocation());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002252 case IsClsMethDeclUnknown:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002253 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002254 case IsClsMethDeclKnown: {
2255 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002256 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002257 }
2258 }
2259}
2260
Chris Lattner0389e6b2009-04-26 00:44:05 +00002261void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorc2350e52010-03-08 16:40:19 +00002262 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner0389e6b2009-04-26 00:44:05 +00002263 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002264 return;
2265 }
2266
2267 if (CI.Decl == 0)
2268 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner0389e6b2009-04-26 00:44:05 +00002269 else
Douglas Gregorc2350e52010-03-08 16:40:19 +00002270 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2271 ClassNameLoc = CI.Loc;
Chris Lattner0389e6b2009-04-26 00:44:05 +00002272}
2273
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002274void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2275 DestroyChildren(C);
2276 if (SubExprs)
2277 C.Deallocate(SubExprs);
2278 this->~ObjCMessageExpr();
2279 C.Deallocate((void*) this);
2280}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002281
Chris Lattner27437ca2007-10-25 00:29:32 +00002282bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002283 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002284}
2285
Nate Begeman888376a2009-08-12 02:28:50 +00002286void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2287 unsigned NumExprs) {
2288 if (SubExprs) C.Deallocate(SubExprs);
2289
2290 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002291 this->NumExprs = NumExprs;
2292 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002293}
Nate Begeman888376a2009-08-12 02:28:50 +00002294
2295void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2296 DestroyChildren(C);
2297 if (SubExprs) C.Deallocate(SubExprs);
2298 this->~ShuffleVectorExpr();
2299 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002300}
2301
Douglas Gregor42602bb2009-08-07 06:08:38 +00002302void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002303 // Override default behavior of traversing children. If this has a type
2304 // operand and the type is a variable-length array, the child iteration
2305 // will iterate over the size expression. However, this expression belongs
2306 // to the type, not to this, so we don't want to delete it.
2307 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002308 if (isArgumentType()) {
2309 this->~SizeOfAlignOfExpr();
2310 C.Deallocate(this);
2311 }
Sebastian Redl05189992008-11-11 17:56:53 +00002312 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002313 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002314}
2315
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002316//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002317// DesignatedInitExpr
2318//===----------------------------------------------------------------------===//
2319
2320IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2321 assert(Kind == FieldDesignator && "Only valid on a field designator");
2322 if (Field.NameOrField & 0x01)
2323 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2324 else
2325 return getField()->getIdentifier();
2326}
2327
Douglas Gregor319d57f2010-01-06 23:17:19 +00002328DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2329 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002330 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002331 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002332 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002333 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002334 unsigned NumIndexExprs,
2335 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002336 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002337 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002338 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2339 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002340 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002341
2342 // Record the initializer itself.
2343 child_iterator Child = child_begin();
2344 *Child++ = Init;
2345
2346 // Copy the designators and their subexpressions, computing
2347 // value-dependence along the way.
2348 unsigned IndexIdx = 0;
2349 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002350 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002351
2352 if (this->Designators[I].isArrayDesignator()) {
2353 // Compute type- and value-dependence.
2354 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002355 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002356 Index->isTypeDependent() || Index->isValueDependent();
2357
2358 // Copy the index expressions into permanent storage.
2359 *Child++ = IndexExprs[IndexIdx++];
2360 } else if (this->Designators[I].isArrayRangeDesignator()) {
2361 // Compute type- and value-dependence.
2362 Expr *Start = IndexExprs[IndexIdx];
2363 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002364 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002365 Start->isTypeDependent() || Start->isValueDependent() ||
2366 End->isTypeDependent() || End->isValueDependent();
2367
2368 // Copy the start/end expressions into permanent storage.
2369 *Child++ = IndexExprs[IndexIdx++];
2370 *Child++ = IndexExprs[IndexIdx++];
2371 }
2372 }
2373
2374 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002375}
2376
Douglas Gregor05c13a32009-01-22 00:58:24 +00002377DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002378DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002379 unsigned NumDesignators,
2380 Expr **IndexExprs, unsigned NumIndexExprs,
2381 SourceLocation ColonOrEqualLoc,
2382 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002383 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002384 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002385 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002386 ColonOrEqualLoc, UsesColonSyntax,
2387 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002388}
2389
Mike Stump1eb44332009-09-09 15:08:12 +00002390DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002391 unsigned NumIndexExprs) {
2392 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2393 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2394 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2395}
2396
Douglas Gregor319d57f2010-01-06 23:17:19 +00002397void DesignatedInitExpr::setDesignators(ASTContext &C,
2398 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002399 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002400 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002401
Douglas Gregor319d57f2010-01-06 23:17:19 +00002402 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002403 NumDesignators = NumDesigs;
2404 for (unsigned I = 0; I != NumDesigs; ++I)
2405 Designators[I] = Desigs[I];
2406}
2407
Douglas Gregor05c13a32009-01-22 00:58:24 +00002408SourceRange DesignatedInitExpr::getSourceRange() const {
2409 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002410 Designator &First =
2411 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002412 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002413 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002414 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2415 else
2416 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2417 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002418 StartLoc =
2419 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002420 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2421}
2422
Douglas Gregor05c13a32009-01-22 00:58:24 +00002423Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2424 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2425 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2426 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002427 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2428 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2429}
2430
2431Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002432 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002433 "Requires array range designator");
2434 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2435 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002436 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2437 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2438}
2439
2440Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002441 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002442 "Requires array range designator");
2443 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2444 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002445 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2446 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2447}
2448
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002449/// \brief Replaces the designator at index @p Idx with the series
2450/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002451void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002452 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002453 const Designator *Last) {
2454 unsigned NumNewDesignators = Last - First;
2455 if (NumNewDesignators == 0) {
2456 std::copy_backward(Designators + Idx + 1,
2457 Designators + NumDesignators,
2458 Designators + Idx);
2459 --NumNewDesignators;
2460 return;
2461 } else if (NumNewDesignators == 1) {
2462 Designators[Idx] = *First;
2463 return;
2464 }
2465
Mike Stump1eb44332009-09-09 15:08:12 +00002466 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002467 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002468 std::copy(Designators, Designators + Idx, NewDesignators);
2469 std::copy(First, Last, NewDesignators + Idx);
2470 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2471 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002472 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002473 Designators = NewDesignators;
2474 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2475}
2476
Douglas Gregor42602bb2009-08-07 06:08:38 +00002477void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002478 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002479 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002480}
2481
Douglas Gregor319d57f2010-01-06 23:17:19 +00002482void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2483 for (unsigned I = 0; I != NumDesignators; ++I)
2484 Designators[I].~Designator();
2485 C.Deallocate(Designators);
2486 Designators = 0;
2487}
2488
Mike Stump1eb44332009-09-09 15:08:12 +00002489ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002490 Expr **exprs, unsigned nexprs,
2491 SourceLocation rparenloc)
2492: Expr(ParenListExprClass, QualType(),
2493 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002494 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002495 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Nate Begeman2ef13e52009-08-10 23:49:36 +00002497 Exprs = new (C) Stmt*[nexprs];
2498 for (unsigned i = 0; i != nexprs; ++i)
2499 Exprs[i] = exprs[i];
2500}
2501
2502void ParenListExpr::DoDestroy(ASTContext& C) {
2503 DestroyChildren(C);
2504 if (Exprs) C.Deallocate(Exprs);
2505 this->~ParenListExpr();
2506 C.Deallocate(this);
2507}
2508
Douglas Gregor05c13a32009-01-22 00:58:24 +00002509//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002510// ExprIterator.
2511//===----------------------------------------------------------------------===//
2512
2513Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2514Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2515Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2516const Expr* ConstExprIterator::operator[](size_t idx) const {
2517 return cast<Expr>(I[idx]);
2518}
2519const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2520const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2521
2522//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002523// Child Iterators for iterating over subexpressions/substatements
2524//===----------------------------------------------------------------------===//
2525
2526// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002527Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2528Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002529
Steve Naroff7779db42007-11-12 14:29:37 +00002530// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002531Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2532Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002533
Steve Naroffe3e9add2008-06-02 23:03:37 +00002534// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002535Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2536Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002537
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002538// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002539Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2540 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002541}
Mike Stump1eb44332009-09-09 15:08:12 +00002542Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2543 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002544}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002545
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002546// ObjCSuperExpr
2547Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2548Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2549
Steve Narofff242b1b2009-07-24 17:54:45 +00002550// ObjCIsaExpr
2551Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2552Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2553
Chris Lattnerd9f69102008-08-10 01:53:14 +00002554// PredefinedExpr
2555Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2556Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002557
2558// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002559Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2560Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002561
2562// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002563Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002564Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002565
2566// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002567Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2568Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002569
Chris Lattner5d661452007-08-26 03:42:43 +00002570// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002571Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2572Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002573
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002574// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002575Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2576Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002577
2578// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002579Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2580Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002581
2582// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002583Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2584Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002585
Sebastian Redl05189992008-11-11 17:56:53 +00002586// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002587Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002588 // If this is of a type and the type is a VLA type (and not a typedef), the
2589 // size expression of the VLA needs to be treated as an executable expression.
2590 // Why isn't this weirdness documented better in StmtIterator?
2591 if (isArgumentType()) {
2592 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2593 getArgumentType().getTypePtr()))
2594 return child_iterator(T);
2595 return child_iterator();
2596 }
Sebastian Redld4575892008-12-03 23:17:54 +00002597 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002598}
Sebastian Redl05189992008-11-11 17:56:53 +00002599Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2600 if (isArgumentType())
2601 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002602 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002603}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002604
2605// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002606Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002607 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002608}
Ted Kremenek1237c672007-08-24 20:06:47 +00002609Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002610 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002611}
2612
2613// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002614Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002615 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002616}
Ted Kremenek1237c672007-08-24 20:06:47 +00002617Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002618 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002619}
Ted Kremenek1237c672007-08-24 20:06:47 +00002620
2621// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002622Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2623Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002624
Nate Begeman213541a2008-04-18 23:10:10 +00002625// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002626Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2627Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002628
2629// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002630Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2631Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002632
Ted Kremenek1237c672007-08-24 20:06:47 +00002633// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002634Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2635Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002636
2637// BinaryOperator
2638Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002639 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002640}
Ted Kremenek1237c672007-08-24 20:06:47 +00002641Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002642 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002643}
2644
2645// ConditionalOperator
2646Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002647 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002648}
Ted Kremenek1237c672007-08-24 20:06:47 +00002649Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002650 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002651}
2652
2653// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002654Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2655Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002656
Ted Kremenek1237c672007-08-24 20:06:47 +00002657// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002658Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2659Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002660
2661// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002662Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2663 return child_iterator();
2664}
2665
2666Stmt::child_iterator TypesCompatibleExpr::child_end() {
2667 return child_iterator();
2668}
Ted Kremenek1237c672007-08-24 20:06:47 +00002669
2670// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002671Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2672Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002673
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002674// GNUNullExpr
2675Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2676Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2677
Eli Friedmand38617c2008-05-14 19:38:39 +00002678// ShuffleVectorExpr
2679Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002680 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002681}
2682Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002683 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002684}
2685
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002686// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002687Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2688Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002689
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002690// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002691Stmt::child_iterator InitListExpr::child_begin() {
2692 return InitExprs.size() ? &InitExprs[0] : 0;
2693}
2694Stmt::child_iterator InitListExpr::child_end() {
2695 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2696}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002697
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002698// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002699Stmt::child_iterator DesignatedInitExpr::child_begin() {
2700 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2701 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002702 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2703}
2704Stmt::child_iterator DesignatedInitExpr::child_end() {
2705 return child_iterator(&*child_begin() + NumSubExprs);
2706}
2707
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002708// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002709Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2710 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002711}
2712
Mike Stump1eb44332009-09-09 15:08:12 +00002713Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2714 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002715}
2716
Nate Begeman2ef13e52009-08-10 23:49:36 +00002717// ParenListExpr
2718Stmt::child_iterator ParenListExpr::child_begin() {
2719 return &Exprs[0];
2720}
2721Stmt::child_iterator ParenListExpr::child_end() {
2722 return &Exprs[0]+NumExprs;
2723}
2724
Ted Kremenek1237c672007-08-24 20:06:47 +00002725// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002726Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002727 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002728}
2729Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002730 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002731}
Ted Kremenek1237c672007-08-24 20:06:47 +00002732
2733// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002734Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2735Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002736
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002737// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002738Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002739 return child_iterator();
2740}
2741Stmt::child_iterator ObjCSelectorExpr::child_end() {
2742 return child_iterator();
2743}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002744
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002745// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002746Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2747 return child_iterator();
2748}
2749Stmt::child_iterator ObjCProtocolExpr::child_end() {
2750 return child_iterator();
2751}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002752
Steve Naroff563477d2007-09-18 23:55:05 +00002753// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002754Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002755 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002756}
2757Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002758 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002759}
2760
Steve Naroff4eb206b2008-09-03 18:15:37 +00002761// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002762Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2763Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002764
Ted Kremenek9da13f92008-09-26 23:24:14 +00002765Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2766Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }