blob: b76048a2b8d144cddb704bfa3f00c0d80b6193db [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) {
102 const VarDecl *Def = 0;
103 if (const Expr *Init = Var->getDefinition(Def))
104 if (Init->isValueDependent())
105 ValueDependent = true;
106 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000107 }
108 // (TD) - a nested-name-specifier or a qualified-id that names a
109 // member of an unknown specialization.
110 // (handled by DependentScopeDeclRefExpr)
111}
112
Douglas Gregora2813ce2009-10-23 18:54:35 +0000113DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
114 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000115 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000116 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000117 QualType T)
118 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000119 DecoratedD(D,
120 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000121 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000122 Loc(NameLoc) {
123 if (Qualifier) {
124 NameQualifier *NQ = getNameQualifier();
125 NQ->NNS = Qualifier;
126 NQ->Range = QualifierRange;
127 }
128
John McCalld5532b62009-11-23 01:53:49 +0000129 if (TemplateArgs)
130 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000131
132 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000133}
134
135DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
136 NestedNameSpecifier *Qualifier,
137 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000138 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000139 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000140 QualType T,
141 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000142 std::size_t Size = sizeof(DeclRefExpr);
143 if (Qualifier != 0)
144 Size += sizeof(NameQualifier);
145
John McCalld5532b62009-11-23 01:53:49 +0000146 if (TemplateArgs)
147 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000148
149 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
150 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000152}
153
154SourceRange DeclRefExpr::getSourceRange() const {
155 // FIXME: Does not handle multi-token names well, e.g., operator[].
156 SourceRange R(Loc);
157
158 if (hasQualifier())
159 R.setBegin(getQualifierRange().getBegin());
160 if (hasExplicitTemplateArgumentList())
161 R.setEnd(getRAngleLoc());
162 return R;
163}
164
Anders Carlsson3a082d82009-09-08 18:24:21 +0000165// FIXME: Maybe this should use DeclPrinter with a special "print predefined
166// expr" policy instead.
167std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT,
168 const Decl *CurrentDecl) {
169 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
170 if (IT != PrettyFunction)
171 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)) {
177 if (MD->isVirtual())
178 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000179 if (MD->isStatic())
180 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000181 }
182
183 PrintingPolicy Policy(Context.getLangOptions());
184 Policy.SuppressTagKind = true;
185
186 std::string Proto = FD->getQualifiedNameAsString(Policy);
187
John McCall183700f2009-09-21 23:43:11 +0000188 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000189 const FunctionProtoType *FT = 0;
190 if (FD->hasWrittenPrototype())
191 FT = dyn_cast<FunctionProtoType>(AFT);
192
193 Proto += "(";
194 if (FT) {
195 llvm::raw_string_ostream POut(Proto);
196 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
197 if (i) POut << ", ";
198 std::string Param;
199 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
200 POut << Param;
201 }
202
203 if (FT->isVariadic()) {
204 if (FD->getNumParams()) POut << ", ";
205 POut << "...";
206 }
207 }
208 Proto += ")";
209
Sam Weinig4eadcc52009-12-27 01:38:20 +0000210 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
211 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
212 if (ThisQuals.hasConst())
213 Proto += " const";
214 if (ThisQuals.hasVolatile())
215 Proto += " volatile";
216 }
217
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000218 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
219 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000220
221 Out << Proto;
222
223 Out.flush();
224 return Name.str().str();
225 }
226 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
227 llvm::SmallString<256> Name;
228 llvm::raw_svector_ostream Out(Name);
229 Out << (MD->isInstanceMethod() ? '-' : '+');
230 Out << '[';
231 Out << MD->getClassInterface()->getNameAsString();
232 if (const ObjCCategoryImplDecl *CID =
233 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
234 Out << '(';
235 Out << CID->getNameAsString();
236 Out << ')';
237 }
238 Out << ' ';
239 Out << MD->getSelector().getAsString();
240 Out << ']';
241
242 Out.flush();
243 return Name.str().str();
244 }
245 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
246 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
247 return "top level";
248 }
249 return "";
250}
251
Chris Lattnerda8249e2008-06-07 22:13:43 +0000252/// getValueAsApproximateDouble - This returns the value as an inaccurate
253/// double. Note that this may cause loss of precision, but is useful for
254/// debugging dumps, etc.
255double FloatingLiteral::getValueAsApproximateDouble() const {
256 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000257 bool ignored;
258 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
259 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000260 return V.convertToDouble();
261}
262
Chris Lattner2085fd62009-02-18 06:40:38 +0000263StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
264 unsigned ByteLength, bool Wide,
265 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000266 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000267 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000268 // Allocate enough space for the StringLiteral plus an array of locations for
269 // any concatenated string tokens.
270 void *Mem = C.Allocate(sizeof(StringLiteral)+
271 sizeof(SourceLocation)*(NumStrs-1),
272 llvm::alignof<StringLiteral>());
273 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Reid Spencer5f016e22007-07-11 17:01:13 +0000275 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000276 char *AStrData = new (C, 1) char[ByteLength];
277 memcpy(AStrData, StrData, ByteLength);
278 SL->StrData = AStrData;
279 SL->ByteLength = ByteLength;
280 SL->IsWide = Wide;
281 SL->TokLocs[0] = Loc[0];
282 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000283
Chris Lattner726e1682009-02-18 05:49:11 +0000284 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000285 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
286 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000287}
288
Douglas Gregor673ecd62009-04-15 16:35:07 +0000289StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
290 void *Mem = C.Allocate(sizeof(StringLiteral)+
291 sizeof(SourceLocation)*(NumStrs-1),
292 llvm::alignof<StringLiteral>());
293 StringLiteral *SL = new (Mem) StringLiteral(QualType());
294 SL->StrData = 0;
295 SL->ByteLength = 0;
296 SL->NumConcatenated = NumStrs;
297 return SL;
298}
299
Douglas Gregor42602bb2009-08-07 06:08:38 +0000300void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000301 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000302 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303}
304
Daniel Dunbarb6480232009-09-22 03:27:33 +0000305void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000306 if (StrData)
307 C.Deallocate(const_cast<char*>(StrData));
308
Daniel Dunbarb6480232009-09-22 03:27:33 +0000309 char *AStrData = new (C, 1) char[Str.size()];
310 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000311 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000312 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000313}
314
Reid Spencer5f016e22007-07-11 17:01:13 +0000315/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
316/// corresponds to, e.g. "sizeof" or "[pre]++".
317const char *UnaryOperator::getOpcodeStr(Opcode Op) {
318 switch (Op) {
319 default: assert(0 && "Unknown unary operator");
320 case PostInc: return "++";
321 case PostDec: return "--";
322 case PreInc: return "++";
323 case PreDec: return "--";
324 case AddrOf: return "&";
325 case Deref: return "*";
326 case Plus: return "+";
327 case Minus: return "-";
328 case Not: return "~";
329 case LNot: return "!";
330 case Real: return "__real";
331 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000333 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
335}
336
Mike Stump1eb44332009-09-09 15:08:12 +0000337UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000338UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
339 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000340 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000341 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
342 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
343 case OO_Amp: return AddrOf;
344 case OO_Star: return Deref;
345 case OO_Plus: return Plus;
346 case OO_Minus: return Minus;
347 case OO_Tilde: return Not;
348 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000349 }
350}
351
352OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
353 switch (Opc) {
354 case PostInc: case PreInc: return OO_PlusPlus;
355 case PostDec: case PreDec: return OO_MinusMinus;
356 case AddrOf: return OO_Amp;
357 case Deref: return OO_Star;
358 case Plus: return OO_Plus;
359 case Minus: return OO_Minus;
360 case Not: return OO_Tilde;
361 case LNot: return OO_Exclaim;
362 default: return OO_None;
363 }
364}
365
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367//===----------------------------------------------------------------------===//
368// Postfix Operators.
369//===----------------------------------------------------------------------===//
370
Ted Kremenek668bf912009-02-09 20:51:47 +0000371CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000372 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000373 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000374 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000375 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000376 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek668bf912009-02-09 20:51:47 +0000378 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000379 SubExprs[FN] = fn;
380 for (unsigned i = 0; i != numargs; ++i)
381 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000382
Douglas Gregorb4609802008-11-14 16:09:21 +0000383 RParenLoc = rparenloc;
384}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000385
Ted Kremenek668bf912009-02-09 20:51:47 +0000386CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
387 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000388 : Expr(CallExprClass, t,
389 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000390 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000391 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000392
393 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000394 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000396 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 RParenLoc = rparenloc;
399}
400
Mike Stump1eb44332009-09-09 15:08:12 +0000401CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
402 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000403 SubExprs = new (C) Stmt*[1];
404}
405
Douglas Gregor42602bb2009-08-07 06:08:38 +0000406void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000407 DestroyChildren(C);
408 if (SubExprs) C.Deallocate(SubExprs);
409 this->~CallExpr();
410 C.Deallocate(this);
411}
412
Nuno Lopesd20254f2009-12-20 23:11:08 +0000413Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000414 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000415 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000416 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000417 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
418 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000419
420 return 0;
421}
422
Nuno Lopesd20254f2009-12-20 23:11:08 +0000423FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000424 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000425}
426
Chris Lattnerd18b3292007-12-28 05:25:02 +0000427/// setNumArgs - This changes the number of arguments present in this call.
428/// Any orphaned expressions are deleted by this, and any new operands are set
429/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000430void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000431 // No change, just return.
432 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattnerd18b3292007-12-28 05:25:02 +0000434 // If shrinking # arguments, just delete the extras and forgot them.
435 if (NumArgs < getNumArgs()) {
436 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000437 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000438 this->NumArgs = NumArgs;
439 return;
440 }
441
442 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000443 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000444 // Copy over args.
445 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
446 NewSubExprs[i] = SubExprs[i];
447 // Null out new args.
448 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
449 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Douglas Gregor88c9a462009-04-17 21:46:47 +0000451 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000452 SubExprs = NewSubExprs;
453 this->NumArgs = NumArgs;
454}
455
Chris Lattnercb888962008-10-06 05:00:53 +0000456/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
457/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000458unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000459 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000461 // ImplicitCastExpr.
462 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
463 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000464 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000466 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
467 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000468 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Anders Carlssonbcba2012008-01-31 02:13:57 +0000470 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
471 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000472 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000474 if (!FDecl->getIdentifier())
475 return 0;
476
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000477 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000478}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000479
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000480QualType CallExpr::getCallReturnType() const {
481 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000482 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000483 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000484 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000485 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000486
John McCall183700f2009-09-21 23:43:11 +0000487 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000488 return FnType->getResultType();
489}
Chris Lattnercb888962008-10-06 05:00:53 +0000490
Mike Stump1eb44332009-09-09 15:08:12 +0000491MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000492 SourceRange qualrange, ValueDecl *memberdecl,
John McCalld5532b62009-11-23 01:53:49 +0000493 SourceLocation l, const TemplateArgumentListInfo *targs,
494 QualType ty)
Mike Stump1eb44332009-09-09 15:08:12 +0000495 : Expr(MemberExprClass, ty,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000496 base->isTypeDependent() || (qual && qual->isDependent()),
497 base->isValueDependent() || (qual && qual->isDependent())),
498 Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow),
John McCalld5532b62009-11-23 01:53:49 +0000499 HasQualifier(qual != 0), HasExplicitTemplateArgumentList(targs) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000500 // Initialize the qualifier, if any.
501 if (HasQualifier) {
502 NameQualifier *NQ = getMemberQualifier();
503 NQ->NNS = qual;
504 NQ->Range = qualrange;
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000507 // Initialize the explicit template argument list, if any.
John McCalld5532b62009-11-23 01:53:49 +0000508 if (targs)
509 getExplicitTemplateArgumentList()->initializeFrom(*targs);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000510}
511
Mike Stump1eb44332009-09-09 15:08:12 +0000512MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
513 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000514 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000515 ValueDecl *memberdecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000517 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000518 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000519 std::size_t Size = sizeof(MemberExpr);
520 if (qual != 0)
521 Size += sizeof(NameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
John McCalld5532b62009-11-23 01:53:49 +0000523 if (targs)
524 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000526 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000527 return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l,
John McCalld5532b62009-11-23 01:53:49 +0000528 targs, ty);
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000529}
530
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000531const char *CastExpr::getCastKindName() const {
532 switch (getCastKind()) {
533 case CastExpr::CK_Unknown:
534 return "Unknown";
535 case CastExpr::CK_BitCast:
536 return "BitCast";
537 case CastExpr::CK_NoOp:
538 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000539 case CastExpr::CK_BaseToDerived:
540 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000541 case CastExpr::CK_DerivedToBase:
542 return "DerivedToBase";
543 case CastExpr::CK_Dynamic:
544 return "Dynamic";
545 case CastExpr::CK_ToUnion:
546 return "ToUnion";
547 case CastExpr::CK_ArrayToPointerDecay:
548 return "ArrayToPointerDecay";
549 case CastExpr::CK_FunctionToPointerDecay:
550 return "FunctionToPointerDecay";
551 case CastExpr::CK_NullToMemberPointer:
552 return "NullToMemberPointer";
553 case CastExpr::CK_BaseToDerivedMemberPointer:
554 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000555 case CastExpr::CK_DerivedToBaseMemberPointer:
556 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000557 case CastExpr::CK_UserDefinedConversion:
558 return "UserDefinedConversion";
559 case CastExpr::CK_ConstructorConversion:
560 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000561 case CastExpr::CK_IntegralToPointer:
562 return "IntegralToPointer";
563 case CastExpr::CK_PointerToIntegral:
564 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000565 case CastExpr::CK_ToVoid:
566 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000567 case CastExpr::CK_VectorSplat:
568 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000569 case CastExpr::CK_IntegralCast:
570 return "IntegralCast";
571 case CastExpr::CK_IntegralToFloating:
572 return "IntegralToFloating";
573 case CastExpr::CK_FloatingToIntegral:
574 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000575 case CastExpr::CK_FloatingCast:
576 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000577 case CastExpr::CK_MemberPointerToBoolean:
578 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000579 case CastExpr::CK_AnyPointerToObjCPointerCast:
580 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000581 case CastExpr::CK_AnyPointerToBlockPointerCast:
582 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000585 assert(0 && "Unhandled cast kind!");
586 return 0;
587}
588
Douglas Gregor6eef5192009-12-14 19:27:10 +0000589Expr *CastExpr::getSubExprAsWritten() {
590 Expr *SubExpr = 0;
591 CastExpr *E = this;
592 do {
593 SubExpr = E->getSubExpr();
594
595 // Skip any temporary bindings; they're implicit.
596 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
597 SubExpr = Binder->getSubExpr();
598
599 // Conversions by constructor and conversion functions have a
600 // subexpression describing the call; strip it off.
601 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
602 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
603 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
604 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
605
606 // If the subexpression we're left with is an implicit cast, look
607 // through that, too.
608 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
609
610 return SubExpr;
611}
612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
614/// corresponds to, e.g. "<<=".
615const char *BinaryOperator::getOpcodeStr(Opcode Op) {
616 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000617 case PtrMemD: return ".*";
618 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 case Mul: return "*";
620 case Div: return "/";
621 case Rem: return "%";
622 case Add: return "+";
623 case Sub: return "-";
624 case Shl: return "<<";
625 case Shr: return ">>";
626 case LT: return "<";
627 case GT: return ">";
628 case LE: return "<=";
629 case GE: return ">=";
630 case EQ: return "==";
631 case NE: return "!=";
632 case And: return "&";
633 case Xor: return "^";
634 case Or: return "|";
635 case LAnd: return "&&";
636 case LOr: return "||";
637 case Assign: return "=";
638 case MulAssign: return "*=";
639 case DivAssign: return "/=";
640 case RemAssign: return "%=";
641 case AddAssign: return "+=";
642 case SubAssign: return "-=";
643 case ShlAssign: return "<<=";
644 case ShrAssign: return ">>=";
645 case AndAssign: return "&=";
646 case XorAssign: return "^=";
647 case OrAssign: return "|=";
648 case Comma: return ",";
649 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000650
651 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000652}
653
Mike Stump1eb44332009-09-09 15:08:12 +0000654BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000655BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
656 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000657 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000658 case OO_Plus: return Add;
659 case OO_Minus: return Sub;
660 case OO_Star: return Mul;
661 case OO_Slash: return Div;
662 case OO_Percent: return Rem;
663 case OO_Caret: return Xor;
664 case OO_Amp: return And;
665 case OO_Pipe: return Or;
666 case OO_Equal: return Assign;
667 case OO_Less: return LT;
668 case OO_Greater: return GT;
669 case OO_PlusEqual: return AddAssign;
670 case OO_MinusEqual: return SubAssign;
671 case OO_StarEqual: return MulAssign;
672 case OO_SlashEqual: return DivAssign;
673 case OO_PercentEqual: return RemAssign;
674 case OO_CaretEqual: return XorAssign;
675 case OO_AmpEqual: return AndAssign;
676 case OO_PipeEqual: return OrAssign;
677 case OO_LessLess: return Shl;
678 case OO_GreaterGreater: return Shr;
679 case OO_LessLessEqual: return ShlAssign;
680 case OO_GreaterGreaterEqual: return ShrAssign;
681 case OO_EqualEqual: return EQ;
682 case OO_ExclaimEqual: return NE;
683 case OO_LessEqual: return LE;
684 case OO_GreaterEqual: return GE;
685 case OO_AmpAmp: return LAnd;
686 case OO_PipePipe: return LOr;
687 case OO_Comma: return Comma;
688 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000689 }
690}
691
692OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
693 static const OverloadedOperatorKind OverOps[] = {
694 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
695 OO_Star, OO_Slash, OO_Percent,
696 OO_Plus, OO_Minus,
697 OO_LessLess, OO_GreaterGreater,
698 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
699 OO_EqualEqual, OO_ExclaimEqual,
700 OO_Amp,
701 OO_Caret,
702 OO_Pipe,
703 OO_AmpAmp,
704 OO_PipePipe,
705 OO_Equal, OO_StarEqual,
706 OO_SlashEqual, OO_PercentEqual,
707 OO_PlusEqual, OO_MinusEqual,
708 OO_LessLessEqual, OO_GreaterGreaterEqual,
709 OO_AmpEqual, OO_CaretEqual,
710 OO_PipeEqual,
711 OO_Comma
712 };
713 return OverOps[Opc];
714}
715
Mike Stump1eb44332009-09-09 15:08:12 +0000716InitListExpr::InitListExpr(SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000717 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000718 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000719 : Expr(InitListExprClass, QualType(), false, false),
Mike Stump1eb44332009-09-09 15:08:12 +0000720 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Douglas Gregor73460a32009-11-19 23:25:22 +0000721 UnionFieldInit(0), HadArrayRangeDesignator(false)
722{
723 for (unsigned I = 0; I != numInits; ++I) {
724 if (initExprs[I]->isTypeDependent())
725 TypeDependent = true;
726 if (initExprs[I]->isValueDependent())
727 ValueDependent = true;
728 }
729
Chris Lattner418f6c72008-10-26 23:43:26 +0000730 InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000731}
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Douglas Gregorfa219202009-03-20 23:58:33 +0000733void InitListExpr::reserveInits(unsigned NumInits) {
734 if (NumInits > InitExprs.size())
735 InitExprs.reserve(NumInits);
736}
737
Douglas Gregor4c678342009-01-28 21:54:33 +0000738void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) {
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000739 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
Daniel Dunbarf592c922009-02-16 22:42:44 +0000740 Idx < LastIdx; ++Idx)
Douglas Gregor06863682009-03-20 23:38:03 +0000741 InitExprs[Idx]->Destroy(Context);
Douglas Gregor4c678342009-01-28 21:54:33 +0000742 InitExprs.resize(NumInits, 0);
743}
744
745Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) {
746 if (Init >= InitExprs.size()) {
747 InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0);
748 InitExprs.back() = expr;
749 return 0;
750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Douglas Gregor4c678342009-01-28 21:54:33 +0000752 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
753 InitExprs[Init] = expr;
754 return Result;
755}
756
Steve Naroffbfdcae62008-09-04 15:31:07 +0000757/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000758///
759const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000760 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000761 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000762}
763
Mike Stump1eb44332009-09-09 15:08:12 +0000764SourceLocation BlockExpr::getCaretLocation() const {
765 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000766}
Mike Stump1eb44332009-09-09 15:08:12 +0000767const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000768 return TheBlock->getBody();
769}
Mike Stump1eb44332009-09-09 15:08:12 +0000770Stmt *BlockExpr::getBody() {
771 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000772}
Steve Naroff56ee6892008-10-08 17:01:13 +0000773
774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775//===----------------------------------------------------------------------===//
776// Generic Expression Routines
777//===----------------------------------------------------------------------===//
778
Chris Lattner026dc962009-02-14 07:37:35 +0000779/// isUnusedResultAWarning - Return true if this immediate expression should
780/// be warned about if the result is unused. If so, fill in Loc and Ranges
781/// with location to warn on and the source range[s] to report with the
782/// warning.
783bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000784 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000785 // Don't warn if the expr is type dependent. The type could end up
786 // instantiating to void.
787 if (isTypeDependent())
788 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 switch (getStmtClass()) {
791 default:
Chris Lattner026dc962009-02-14 07:37:35 +0000792 Loc = getExprLoc();
793 R1 = getSourceRange();
794 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000796 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000797 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 case UnaryOperatorClass: {
799 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000802 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 case UnaryOperator::PostInc:
804 case UnaryOperator::PostDec:
805 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000806 case UnaryOperator::PreDec: // ++/--
807 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 case UnaryOperator::Deref:
809 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000810 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000811 return false;
812 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 case UnaryOperator::Real:
814 case UnaryOperator::Imag:
815 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000816 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
817 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000818 return false;
819 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000820 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000821 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 }
Chris Lattner026dc962009-02-14 07:37:35 +0000823 Loc = UO->getOperatorLoc();
824 R1 = UO->getSubExpr()->getSourceRange();
825 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000827 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000828 const BinaryOperator *BO = cast<BinaryOperator>(this);
829 // Consider comma to have side effects if the LHS or RHS does.
830 if (BO->getOpcode() == BinaryOperator::Comma)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000831 return (BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
832 BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattner026dc962009-02-14 07:37:35 +0000834 if (BO->isAssignmentOp())
835 return false;
836 Loc = BO->getOperatorLoc();
837 R1 = BO->getLHS()->getSourceRange();
838 R2 = BO->getRHS()->getSourceRange();
839 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000840 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000841 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000842 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000843
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000844 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000845 // The condition must be evaluated, but if either the LHS or RHS is a
846 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000847 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000848 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000849 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000850 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000851 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000852 }
853
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000855 // If the base pointer or element is to a volatile pointer/field, accessing
856 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000857 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000858 return false;
859 Loc = cast<MemberExpr>(this)->getMemberLoc();
860 R1 = SourceRange(Loc, Loc);
861 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
862 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 case ArraySubscriptExprClass:
865 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000866 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000867 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000868 return false;
869 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
870 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
871 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
872 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000875 case CXXOperatorCallExprClass:
876 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000877 // If this is a direct call, get the callee.
878 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000879 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000880 // If the callee has attribute pure, const, or warn_unused_result, warn
881 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000882 //
883 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
884 // updated to match for QoI.
885 if (FD->getAttr<WarnUnusedResultAttr>() ||
886 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
887 Loc = CE->getCallee()->getLocStart();
888 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000890 if (unsigned NumArgs = CE->getNumArgs())
891 R2 = SourceRange(CE->getArg(0)->getLocStart(),
892 CE->getArg(NumArgs-1)->getLocEnd());
893 return true;
894 }
Chris Lattner026dc962009-02-14 07:37:35 +0000895 }
896 return false;
897 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000898
899 case CXXTemporaryObjectExprClass:
900 case CXXConstructExprClass:
901 return false;
902
Chris Lattnera9c01022007-09-26 22:06:30 +0000903 case ObjCMessageExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000904 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000906 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000907#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000908 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000909 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000910 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000911 Loc = Ref->getLocation();
912 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
913 if (Ref->getBase())
914 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +0000915#else
916 Loc = getExprLoc();
917 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +0000918#endif
919 return true;
920 }
Chris Lattner611b2ec2008-07-26 19:51:01 +0000921 case StmtExprClass: {
922 // Statement exprs don't logically have side effects themselves, but are
923 // sometimes used in macros in ways that give them a type that is unused.
924 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
925 // however, if the result of the stmt expr is dead, we don't want to emit a
926 // warning.
927 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
928 if (!CS->body_empty())
929 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +0000930 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner026dc962009-02-14 07:37:35 +0000932 Loc = cast<StmtExpr>(this)->getLParenLoc();
933 R1 = getSourceRange();
934 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +0000935 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +0000936 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +0000937 // If this is an explicit cast to void, allow it. People do this when they
938 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +0000939 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +0000940 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000941 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
942 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
943 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000944 case CXXFunctionalCastExprClass: {
945 const CastExpr *CE = cast<CastExpr>(this);
946
947 // If this is a cast to void or a constructor conversion, check the operand.
948 // Otherwise, the result of the cast is unused.
949 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
950 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +0000951 return (cast<CastExpr>(this)->getSubExpr()
952 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +0000953 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
954 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
955 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +0000956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Eli Friedman4be1f472008-05-19 21:24:43 +0000958 case ImplicitCastExprClass:
959 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +0000960 return (cast<ImplicitCastExpr>(this)
961 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +0000962
Chris Lattner04421082008-04-08 04:40:51 +0000963 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000964 return (cast<CXXDefaultArgExpr>(this)
965 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000966
967 case CXXNewExprClass:
968 // FIXME: In theory, there might be new expressions that don't have side
969 // effects (e.g. a placement new with an uninitialized POD).
970 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000971 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +0000972 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000973 return (cast<CXXBindTemporaryExpr>(this)
974 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +0000975 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000976 return (cast<CXXExprWithTemporaries>(this)
977 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000978 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000979}
980
Douglas Gregorba7e2102008-10-22 15:04:37 +0000981/// DeclCanBeLvalue - Determine whether the given declaration can be
982/// an lvalue. This is a helper routine for isLvalue.
983static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000984 // C++ [temp.param]p6:
985 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +0000987 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
988 return NTTParm->getType()->isReferenceType();
989
Douglas Gregor44b43212008-12-11 16:49:14 +0000990 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +0000991 // C++ 3.10p2: An lvalue refers to an object or function.
992 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +0000993 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +0000994}
995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
997/// incomplete type other than void. Nonarray expressions that can be lvalues:
998/// - name, where name must be a variable
999/// - e[i]
1000/// - (e), where e must be an lvalue
1001/// - e.name, where e must be an lvalue
1002/// - e->name
1003/// - *e, the type of e cannot be a function type
1004/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001005/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001006/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001007///
Chris Lattner28be73f2008-07-26 21:30:36 +00001008Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001009 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1010
1011 isLvalueResult Res = isLvalueInternal(Ctx);
1012 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1013 return Res;
1014
Douglas Gregor98cd5992008-10-21 23:43:52 +00001015 // first, check the type (C99 6.3.2.1). Expressions with function
1016 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001017 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 return LV_NotObjectType;
1019
Steve Naroffacb818a2008-02-10 01:39:04 +00001020 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001021 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001022 return LV_IncompleteVoidType;
1023
Eli Friedman53202852009-05-03 22:36:05 +00001024 return LV_Valid;
1025}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001026
Eli Friedman53202852009-05-03 22:36:05 +00001027// Check whether the expression can be sanely treated like an l-value
1028Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001030 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001031 case StringLiteralClass: // C99 6.5.1p4
1032 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001033 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1035 // For vectors, make sure base is an lvalue (i.e. not a function call).
1036 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001037 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001039 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001040 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1041 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 return LV_Valid;
1043 break;
Chris Lattner41110242008-06-17 18:05:57 +00001044 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001045 case BlockDeclRefExprClass: {
1046 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001047 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001048 return LV_Valid;
1049 break;
1050 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001051 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001053 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1054 NamedDecl *Member = m->getMemberDecl();
1055 // C++ [expr.ref]p4:
1056 // If E2 is declared to have type "reference to T", then E1.E2
1057 // is an lvalue.
1058 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1059 if (Value->getType()->isReferenceType())
1060 return LV_Valid;
1061
1062 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001063 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001064 return LV_Valid;
1065
1066 // -- If E2 is a non-static data member [...]. If E1 is an
1067 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001068 if (isa<FieldDecl>(Member)) {
1069 if (m->isArrow())
1070 return LV_Valid;
1071 Expr *BaseExp = m->getBase();
1072 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1073 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
1074 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001075
1076 // -- If it refers to a static member function [...], then
1077 // E1.E2 is an lvalue.
1078 // -- Otherwise, if E1.E2 refers to a non-static member
1079 // function [...], then E1.E2 is not an lvalue.
1080 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1081 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1082
1083 // -- If E2 is a member enumerator [...], the expression E1.E2
1084 // is not an lvalue.
1085 if (isa<EnumConstantDecl>(Member))
1086 return LV_InvalidExpression;
1087
1088 // Not an lvalue.
1089 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001090 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001091
Douglas Gregor86f19402008-12-20 23:49:58 +00001092 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001093 if (m->isArrow())
1094 return LV_Valid;
1095 Expr *BaseExp = m->getBase();
1096 return (BaseExp->getStmtClass() == ObjCPropertyRefExprClass) ?
1097 LV_SubObjCPropertySetting : BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001098 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001099 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001101 return LV_Valid; // C99 6.5.3p4
1102
1103 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001104 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1105 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001106 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001107
1108 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1109 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1110 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1111 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001113 case ImplicitCastExprClass:
Mike Stump1eb44332009-09-09 15:08:12 +00001114 return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001115 : LV_InvalidExpression;
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001117 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001118 case BinaryOperatorClass:
1119 case CompoundAssignOperatorClass: {
1120 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001121
1122 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1123 BinOp->getOpcode() == BinaryOperator::Comma)
1124 return BinOp->getRHS()->isLvalue(Ctx);
1125
Sebastian Redl22460502009-02-07 00:15:38 +00001126 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001127 // The result of a .* expression is an lvalue only if its first operand is
1128 // an lvalue and its second operand is a pointer to data member.
1129 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001130 !BinOp->getType()->isFunctionType())
1131 return BinOp->getLHS()->isLvalue(Ctx);
1132
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001133 // The result of an ->* expression is an lvalue only if its second operand
1134 // is a pointer to data member.
1135 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1136 !BinOp->getType()->isFunctionType()) {
1137 QualType Ty = BinOp->getRHS()->getType();
1138 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1139 return LV_Valid;
1140 }
1141
Douglas Gregorbf3af052008-11-13 20:12:29 +00001142 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001143 return LV_InvalidExpression;
1144
Douglas Gregorbf3af052008-11-13 20:12:29 +00001145 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001146 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001147 // The result of an assignment operation [...] is an lvalue.
1148 return LV_Valid;
1149
1150
1151 // C99 6.5.16:
1152 // An assignment expression [...] is not an lvalue.
1153 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001156 case CXXOperatorCallExprClass:
1157 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001158 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001159 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001160 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001161 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1162 if (ReturnType->isLValueReferenceType())
1163 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001164
Douglas Gregor9d293df2008-10-28 00:22:11 +00001165 break;
1166 }
Steve Naroffe6386392007-12-05 04:00:10 +00001167 case CompoundLiteralExprClass: // C99 6.5.2.5p5
1168 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001169 case ChooseExprClass:
1170 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001171 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001172 case ExtVectorElementExprClass:
1173 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001174 return LV_DuplicateVectorComponents;
1175 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001176 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1177 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001178 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1179 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001180 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001181 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001182 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001183 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001184 case UnresolvedLookupExprClass:
1185 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001186 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001187 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001188 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001189 case CXXFunctionalCastExprClass:
1190 case CXXStaticCastExprClass:
1191 case CXXDynamicCastExprClass:
1192 case CXXReinterpretCastExprClass:
1193 case CXXConstCastExprClass:
1194 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001195 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001196 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1197 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001198 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1199 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001200 return LV_Valid;
1201 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001202 case CXXTypeidExprClass:
1203 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1204 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001205 case CXXBindTemporaryExprClass:
1206 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1207 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001208 case CXXBindReferenceExprClass:
1209 // Something that's bound to a reference is always an lvalue.
1210 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001211 case ConditionalOperatorClass: {
1212 // Complicated handling is only for C++.
1213 if (!Ctx.getLangOptions().CPlusPlus)
1214 return LV_InvalidExpression;
1215
1216 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1217 // everywhere there's an object converted to an rvalue. Also, any other
1218 // casts should be wrapped by ImplicitCastExprs. There's just the special
1219 // case involving throws to work out.
1220 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001221 Expr *True = Cond->getTrueExpr();
1222 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001223 // C++0x 5.16p2
1224 // If either the second or the third operand has type (cv) void, [...]
1225 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001226 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001227 return LV_InvalidExpression;
1228
1229 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001230 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001231 return LV_InvalidExpression;
1232
1233 // That's it.
1234 return LV_Valid;
1235 }
1236
Douglas Gregor2d48e782009-12-19 07:07:47 +00001237 case Expr::CXXExprWithTemporariesClass:
1238 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1239
1240 case Expr::ObjCMessageExprClass:
1241 if (const ObjCMethodDecl *Method
1242 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1243 if (Method->getResultType()->isLValueReferenceType())
1244 return LV_Valid;
1245 break;
1246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 default:
1248 break;
1249 }
1250 return LV_InvalidExpression;
1251}
1252
1253/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1254/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001255/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001256/// recursively, any member or element of all contained aggregates or unions)
1257/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001258Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001259Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001260 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001263 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001264 // C++ 3.10p11: Functions cannot be modified, but pointers to
1265 // functions can be modifiable.
1266 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1267 return MLV_NotObjectType;
1268 break;
1269
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 case LV_NotObjectType: return MLV_NotObjectType;
1271 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001272 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001273 case LV_InvalidExpression:
1274 // If the top level is a C-style cast, and the subexpression is a valid
1275 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1276 // GCC extension. We don't support it, but we want to produce good
1277 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001278 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1279 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1280 if (Loc)
1281 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001282 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001283 }
1284 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001285 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001286 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001287 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001289
1290 // The following is illegal:
1291 // void takeclosure(void (^C)(void));
1292 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1293 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001294 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001295 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1296 return MLV_NotBlockQualified;
1297 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001298
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001299 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001300 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001301 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1302 if (Expr->getSetterMethod() == 0)
1303 return MLV_NoSetterProperty;
1304 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001305
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001306 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001308 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001310 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001312 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Ted Kremenek6217b802009-07-29 21:53:49 +00001315 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001316 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 return MLV_ConstQualified;
1318 }
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Mike Stump1eb44332009-09-09 15:08:12 +00001320 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001321}
1322
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001323/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001324/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001325bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001326 switch (getStmtClass()) {
1327 default:
1328 return false;
1329 case ObjCIvarRefExprClass:
1330 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001331 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001332 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001333 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001334 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001335 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001336 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001337 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001338 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001339 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001340 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001341 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1342 if (VD->hasGlobalStorage())
1343 return true;
1344 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001345 // dereferencing to a pointer is always a gc'able candidate,
1346 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001347 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001348 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001349 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001350 return false;
1351 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001352 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001353 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001354 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001355 }
1356 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001357 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001358 }
1359}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001360Expr* Expr::IgnoreParens() {
1361 Expr* E = this;
1362 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1363 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001365 return E;
1366}
1367
Chris Lattner56f34942008-02-13 01:02:39 +00001368/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1369/// or CastExprs or ImplicitCastExprs, returning their operand.
1370Expr *Expr::IgnoreParenCasts() {
1371 Expr *E = this;
1372 while (true) {
1373 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1374 E = P->getSubExpr();
1375 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1376 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001377 else
1378 return E;
1379 }
1380}
1381
Chris Lattnerecdd8412009-03-13 17:28:01 +00001382/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1383/// value (including ptr->int casts of the same size). Strip off any
1384/// ParenExpr or CastExprs, returning their operand.
1385Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1386 Expr *E = this;
1387 while (true) {
1388 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1389 E = P->getSubExpr();
1390 continue;
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Chris Lattnerecdd8412009-03-13 17:28:01 +00001393 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1394 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1395 // ptr<->int casts of the same width. We also ignore all identify casts.
1396 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Chris Lattnerecdd8412009-03-13 17:28:01 +00001398 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1399 E = SE;
1400 continue;
1401 }
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Chris Lattnerecdd8412009-03-13 17:28:01 +00001403 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1404 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1405 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1406 E = SE;
1407 continue;
1408 }
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Chris Lattnerecdd8412009-03-13 17:28:01 +00001411 return E;
1412 }
1413}
1414
Douglas Gregor6eef5192009-12-14 19:27:10 +00001415bool Expr::isDefaultArgument() const {
1416 const Expr *E = this;
1417 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1418 E = ICE->getSubExprAsWritten();
1419
1420 return isa<CXXDefaultArgExpr>(E);
1421}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001422
Douglas Gregor898574e2008-12-05 23:32:09 +00001423/// hasAnyTypeDependentArguments - Determines if any of the expressions
1424/// in Exprs is type-dependent.
1425bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1426 for (unsigned I = 0; I < NumExprs; ++I)
1427 if (Exprs[I]->isTypeDependent())
1428 return true;
1429
1430 return false;
1431}
1432
1433/// hasAnyValueDependentArguments - Determines if any of the expressions
1434/// in Exprs is value-dependent.
1435bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1436 for (unsigned I = 0; I < NumExprs; ++I)
1437 if (Exprs[I]->isValueDependent())
1438 return true;
1439
1440 return false;
1441}
1442
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001443bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001444 // This function is attempting whether an expression is an initializer
1445 // which can be evaluated at compile-time. isEvaluatable handles most
1446 // of the cases, but it can't deal with some initializer-specific
1447 // expressions, and it can't deal with aggregates; we deal with those here,
1448 // and fall back to isEvaluatable for the other cases.
1449
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001450 // FIXME: This function assumes the variable being assigned to
1451 // isn't a reference type!
1452
Anders Carlssone8a32b82008-11-24 05:23:59 +00001453 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001454 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001455 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001456 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001457 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001458 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001459 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001460 // This handles gcc's extension that allows global initializers like
1461 // "struct x {int x;} x = (struct x) {};".
1462 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001463 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001464 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001465 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001466 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001467 // FIXME: This doesn't deal with fields with reference types correctly.
1468 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1469 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001470 const InitListExpr *Exp = cast<InitListExpr>(this);
1471 unsigned numInits = Exp->getNumInits();
1472 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001473 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001474 return false;
1475 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001476 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001477 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001478 case ImplicitValueInitExprClass:
1479 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001480 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001481 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001482 case UnaryOperatorClass: {
1483 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1484 if (Exp->getOpcode() == UnaryOperator::Extension)
1485 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1486 break;
1487 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001488 case BinaryOperatorClass: {
1489 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1490 // but this handles the common case.
1491 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1492 if (Exp->getOpcode() == BinaryOperator::Sub &&
1493 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1494 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1495 return true;
1496 break;
1497 }
Chris Lattner81045d82009-04-21 05:19:11 +00001498 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001499 case CStyleCastExprClass:
1500 // Handle casts with a destination that's a struct or union; this
1501 // deals with both the gcc no-op struct cast extension and the
1502 // cast-to-union extension.
1503 if (getType()->isRecordType())
1504 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001505
1506 // Integer->integer casts can be handled here, which is important for
1507 // things like (int)(&&x-&&y). Scary but true.
1508 if (getType()->isIntegerType() &&
1509 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1510 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1511
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001512 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001513 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001514 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001515}
1516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001518/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001519
1520/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1521/// comma, etc
1522///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001523/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1524/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1525/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001526
Eli Friedmane28d7192009-02-26 09:29:13 +00001527// CheckICE - This function does the fundamental ICE checking: the returned
1528// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1529// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001530// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001531// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001532// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001533//
1534// Meanings of Val:
1535// 0: This expression is an ICE if it can be evaluated by Evaluate.
1536// 1: This expression is not an ICE, but if it isn't evaluated, it's
1537// a legal subexpression for an ICE. This return value is used to handle
1538// the comma operator in C99 mode.
1539// 2: This expression is not an ICE, and is not a legal subexpression for one.
1540
1541struct ICEDiag {
1542 unsigned Val;
1543 SourceLocation Loc;
1544
1545 public:
1546 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1547 ICEDiag() : Val(0) {}
1548};
1549
1550ICEDiag NoDiag() { return ICEDiag(); }
1551
Eli Friedman60ce9632009-02-27 04:07:58 +00001552static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1553 Expr::EvalResult EVResult;
1554 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1555 !EVResult.Val.isInt()) {
1556 return ICEDiag(2, E->getLocStart());
1557 }
1558 return NoDiag();
1559}
1560
Eli Friedmane28d7192009-02-26 09:29:13 +00001561static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001562 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001563 if (!E->getType()->isIntegralType()) {
1564 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001565 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001566
1567 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001568#define STMT(Node, Base) case Expr::Node##Class:
1569#define EXPR(Node, Base)
1570#include "clang/AST/StmtNodes.def"
1571 case Expr::PredefinedExprClass:
1572 case Expr::FloatingLiteralClass:
1573 case Expr::ImaginaryLiteralClass:
1574 case Expr::StringLiteralClass:
1575 case Expr::ArraySubscriptExprClass:
1576 case Expr::MemberExprClass:
1577 case Expr::CompoundAssignOperatorClass:
1578 case Expr::CompoundLiteralExprClass:
1579 case Expr::ExtVectorElementExprClass:
1580 case Expr::InitListExprClass:
1581 case Expr::DesignatedInitExprClass:
1582 case Expr::ImplicitValueInitExprClass:
1583 case Expr::ParenListExprClass:
1584 case Expr::VAArgExprClass:
1585 case Expr::AddrLabelExprClass:
1586 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001587 case Expr::CXXMemberCallExprClass:
1588 case Expr::CXXDynamicCastExprClass:
1589 case Expr::CXXTypeidExprClass:
1590 case Expr::CXXNullPtrLiteralExprClass:
1591 case Expr::CXXThisExprClass:
1592 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001593 case Expr::CXXNewExprClass:
1594 case Expr::CXXDeleteExprClass:
1595 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001596 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001597 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001598 case Expr::CXXConstructExprClass:
1599 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001600 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001601 case Expr::CXXExprWithTemporariesClass:
1602 case Expr::CXXTemporaryObjectExprClass:
1603 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001604 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001605 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001606 case Expr::ObjCStringLiteralClass:
1607 case Expr::ObjCEncodeExprClass:
1608 case Expr::ObjCMessageExprClass:
1609 case Expr::ObjCSelectorExprClass:
1610 case Expr::ObjCProtocolExprClass:
1611 case Expr::ObjCIvarRefExprClass:
1612 case Expr::ObjCPropertyRefExprClass:
1613 case Expr::ObjCImplicitSetterGetterRefExprClass:
1614 case Expr::ObjCSuperExprClass:
1615 case Expr::ObjCIsaExprClass:
1616 case Expr::ShuffleVectorExprClass:
1617 case Expr::BlockExprClass:
1618 case Expr::BlockDeclRefExprClass:
1619 case Expr::NoStmtClass:
1620 case Expr::ExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001621 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001622
Douglas Gregor043cad22009-09-11 00:18:58 +00001623 case Expr::GNUNullExprClass:
1624 // GCC considers the GNU __null value to be an integral constant expression.
1625 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001626
Eli Friedmane28d7192009-02-26 09:29:13 +00001627 case Expr::ParenExprClass:
1628 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1629 case Expr::IntegerLiteralClass:
1630 case Expr::CharacterLiteralClass:
1631 case Expr::CXXBoolLiteralExprClass:
1632 case Expr::CXXZeroInitValueExprClass:
1633 case Expr::TypesCompatibleExprClass:
1634 case Expr::UnaryTypeTraitExprClass:
1635 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001636 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001637 case Expr::CXXOperatorCallExprClass: {
1638 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001639 if (CE->isBuiltinCall(Ctx))
1640 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001641 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001642 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001643 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001644 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1645 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001646 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001647 E->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001648 // C++ 7.1.5.1p2
1649 // A variable of non-volatile const-qualified integral or enumeration
1650 // type initialized by an ICE can be used in ICEs.
1651 if (const VarDecl *Dcl =
Eli Friedmane28d7192009-02-26 09:29:13 +00001652 dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001653 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1654 if (Quals.hasVolatile() || !Quals.hasConst())
1655 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1656
1657 // Look for the definition of this variable, which will actually have
1658 // an initializer.
1659 const VarDecl *Def = 0;
1660 const Expr *Init = Dcl->getDefinition(Def);
1661 if (Init) {
1662 if (Def->isInitKnownICE()) {
1663 // We have already checked whether this subexpression is an
1664 // integral constant expression.
1665 if (Def->isInitICE())
1666 return NoDiag();
1667 else
1668 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1669 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001670
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001671 // C++ [class.static.data]p4:
1672 // If a static data member is of const integral or const
1673 // enumeration type, its declaration in the class definition can
1674 // specify a constant-initializer which shall be an integral
1675 // constant expression (5.19). In that case, the member can appear
1676 // in integral constant expressions.
1677 if (Def->isOutOfLine()) {
Eli Friedmanc0131182009-12-03 20:31:57 +00001678 Dcl->setInitKnownICE(false);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001679 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1680 }
Eli Friedmanc0131182009-12-03 20:31:57 +00001681
1682 if (Dcl->isCheckingICE()) {
1683 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1684 }
1685
1686 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001687 ICEDiag Result = CheckICE(Init, Ctx);
1688 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001689 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001690 return Result;
1691 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001692 }
1693 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001694 return ICEDiag(2, E->getLocStart());
1695 case Expr::UnaryOperatorClass: {
1696 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001698 case UnaryOperator::PostInc:
1699 case UnaryOperator::PostDec:
1700 case UnaryOperator::PreInc:
1701 case UnaryOperator::PreDec:
1702 case UnaryOperator::AddrOf:
1703 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001704 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001705
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001707 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001711 case UnaryOperator::Real:
1712 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001713 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001714 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001715 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1716 // Evaluate matches the proposed gcc behavior for cases like
1717 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1718 // compliance: we should warn earlier for offsetof expressions with
1719 // array subscripts that aren't ICEs, and if the array subscripts
1720 // are ICEs, the value of the offsetof must be an integer constant.
1721 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001724 case Expr::SizeOfAlignOfExprClass: {
1725 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1726 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1727 return ICEDiag(2, E->getLocStart());
1728 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001729 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001730 case Expr::BinaryOperatorClass: {
1731 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001733 case BinaryOperator::PtrMemD:
1734 case BinaryOperator::PtrMemI:
1735 case BinaryOperator::Assign:
1736 case BinaryOperator::MulAssign:
1737 case BinaryOperator::DivAssign:
1738 case BinaryOperator::RemAssign:
1739 case BinaryOperator::AddAssign:
1740 case BinaryOperator::SubAssign:
1741 case BinaryOperator::ShlAssign:
1742 case BinaryOperator::ShrAssign:
1743 case BinaryOperator::AndAssign:
1744 case BinaryOperator::XorAssign:
1745 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001746 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001747
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001751 case BinaryOperator::Add:
1752 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001755 case BinaryOperator::LT:
1756 case BinaryOperator::GT:
1757 case BinaryOperator::LE:
1758 case BinaryOperator::GE:
1759 case BinaryOperator::EQ:
1760 case BinaryOperator::NE:
1761 case BinaryOperator::And:
1762 case BinaryOperator::Xor:
1763 case BinaryOperator::Or:
1764 case BinaryOperator::Comma: {
1765 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1766 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001767 if (Exp->getOpcode() == BinaryOperator::Div ||
1768 Exp->getOpcode() == BinaryOperator::Rem) {
1769 // Evaluate gives an error for undefined Div/Rem, so make sure
1770 // we don't evaluate one.
1771 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1772 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1773 if (REval == 0)
1774 return ICEDiag(1, E->getLocStart());
1775 if (REval.isSigned() && REval.isAllOnesValue()) {
1776 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1777 if (LEval.isMinSignedValue())
1778 return ICEDiag(1, E->getLocStart());
1779 }
1780 }
1781 }
1782 if (Exp->getOpcode() == BinaryOperator::Comma) {
1783 if (Ctx.getLangOptions().C99) {
1784 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1785 // if it isn't evaluated.
1786 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1787 return ICEDiag(1, E->getLocStart());
1788 } else {
1789 // In both C89 and C++, commas in ICEs are illegal.
1790 return ICEDiag(2, E->getLocStart());
1791 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001792 }
1793 if (LHSResult.Val >= RHSResult.Val)
1794 return LHSResult;
1795 return RHSResult;
1796 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001798 case BinaryOperator::LOr: {
1799 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1800 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1801 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1802 // Rare case where the RHS has a comma "side-effect"; we need
1803 // to actually check the condition to see whether the side
1804 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001805 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001806 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001807 return RHSResult;
1808 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001809 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001810
Eli Friedmane28d7192009-02-26 09:29:13 +00001811 if (LHSResult.Val >= RHSResult.Val)
1812 return LHSResult;
1813 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001815 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 }
Douglas Gregorf2991242009-09-10 23:31:45 +00001817 case Expr::CastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001818 case Expr::ImplicitCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001819 case Expr::ExplicitCastExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001820 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001821 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001822 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00001823 case Expr::CXXStaticCastExprClass:
1824 case Expr::CXXReinterpretCastExprClass:
1825 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00001826 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
1827 if (SubExpr->getType()->isIntegralType())
1828 return CheckICE(SubExpr, Ctx);
1829 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
1830 return NoDiag();
1831 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001833 case Expr::ConditionalOperatorClass: {
1834 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00001835 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00001836 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00001837 // expression, and it is fully evaluated. This is an important GNU
1838 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00001839 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00001840 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001841 Expr::EvalResult EVResult;
1842 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1843 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00001844 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001845 }
1846 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00001847 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001848 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
1849 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
1850 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
1851 if (CondResult.Val == 2)
1852 return CondResult;
1853 if (TrueResult.Val == 2)
1854 return TrueResult;
1855 if (FalseResult.Val == 2)
1856 return FalseResult;
1857 if (CondResult.Val == 1)
1858 return CondResult;
1859 if (TrueResult.Val == 0 && FalseResult.Val == 0)
1860 return NoDiag();
1861 // Rare case where the diagnostics depend on which side is evaluated
1862 // Note that if we get here, CondResult is 0, and at least one of
1863 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00001864 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00001865 return FalseResult;
1866 }
1867 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001869 case Expr::CXXDefaultArgExprClass:
1870 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001871 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00001872 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001873 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001875
Douglas Gregorf2991242009-09-10 23:31:45 +00001876 // Silence a GCC warning
1877 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00001878}
Reid Spencer5f016e22007-07-11 17:01:13 +00001879
Eli Friedmane28d7192009-02-26 09:29:13 +00001880bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
1881 SourceLocation *Loc, bool isEvaluated) const {
1882 ICEDiag d = CheckICE(this, Ctx);
1883 if (d.Val != 0) {
1884 if (Loc) *Loc = d.Loc;
1885 return false;
1886 }
1887 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00001888 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001889 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00001890 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
1891 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001892 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 return true;
1894}
1895
Reid Spencer5f016e22007-07-11 17:01:13 +00001896/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1897/// integer constant expression with the value zero, or if this is one that is
1898/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00001899bool Expr::isNullPointerConstant(ASTContext &Ctx,
1900 NullPointerConstantValueDependence NPC) const {
1901 if (isValueDependent()) {
1902 switch (NPC) {
1903 case NPC_NeverValueDependent:
1904 assert(false && "Unexpected value dependent expression!");
1905 // If the unthinkable happens, fall through to the safest alternative.
1906
1907 case NPC_ValueDependentIsNull:
1908 return isTypeDependent() || getType()->isIntegralType();
1909
1910 case NPC_ValueDependentIsNotNull:
1911 return false;
1912 }
1913 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00001914
Sebastian Redl07779722008-10-31 14:43:28 +00001915 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00001916 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00001917 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00001918 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00001919 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00001920 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001921 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00001922 Pointee->isVoidType() && // to void*
1923 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00001924 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00001925 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 }
Steve Naroffaa58f002008-01-14 16:10:57 +00001927 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1928 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00001929 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00001930 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1931 // Accept ((void*)0) as a null pointer constant, as many other
1932 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00001933 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00001934 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00001935 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00001936 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00001937 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001938 } else if (isa<GNUNullExpr>(this)) {
1939 // The GNU __null extension is always a null pointer constant.
1940 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00001941 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00001942
Sebastian Redl6e8ed162009-05-10 18:38:11 +00001943 // C++0x nullptr_t is always a null pointer constant.
1944 if (getType()->isNullPtrType())
1945 return true;
1946
Steve Naroffaa58f002008-01-14 16:10:57 +00001947 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00001948 if (!getType()->isIntegerType() ||
1949 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00001950 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 // If we have an integer constant expression, we need to *evaluate* it and
1953 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00001954 llvm::APSInt Result;
1955 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001956}
Steve Naroff31a45842007-07-28 23:10:27 +00001957
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001958FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00001959 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001960
Douglas Gregorde4b1d82010-01-29 19:14:02 +00001961 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1962 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1963 E = ICE->getSubExpr()->IgnoreParens();
1964 else
1965 break;
1966 }
1967
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001968 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00001969 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00001970 if (Field->isBitField())
1971 return Field;
1972
1973 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1974 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1975 return BinOp->getLHS()->getBitField();
1976
1977 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00001978}
1979
Anders Carlsson09380262010-01-31 17:18:49 +00001980bool Expr::refersToVectorElement() const {
1981 const Expr *E = this->IgnoreParens();
1982
1983 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1984 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1985 E = ICE->getSubExpr()->IgnoreParens();
1986 else
1987 break;
1988 }
1989
1990 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1991 return ASE->getBase()->getType()->isVectorType();
1992
1993 if (isa<ExtVectorElementExpr>(E))
1994 return true;
1995
1996 return false;
1997}
1998
Chris Lattner2140e902009-02-16 22:14:05 +00001999/// isArrow - Return true if the base expression is a pointer to vector,
2000/// return false if the base expression is a vector.
2001bool ExtVectorElementExpr::isArrow() const {
2002 return getBase()->getType()->isPointerType();
2003}
2004
Nate Begeman213541a2008-04-18 23:10:10 +00002005unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002006 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002007 return VT->getNumElements();
2008 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002009}
2010
Nate Begeman8a997642008-05-09 06:41:27 +00002011/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002012bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002013 // FIXME: Refactor this code to an accessor on the AST node which returns the
2014 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002015 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002016
2017 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002018 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002019 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Nate Begeman190d6a22009-01-18 02:01:21 +00002021 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002022 if (Comp[0] == 's' || Comp[0] == 'S')
2023 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Daniel Dunbar15027422009-10-17 23:53:04 +00002025 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2026 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002027 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002028
Steve Narofffec0b492007-07-30 03:29:09 +00002029 return false;
2030}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002031
Nate Begeman8a997642008-05-09 06:41:27 +00002032/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002033void ExtVectorElementExpr::getEncodedElementAccess(
2034 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002035 llvm::StringRef Comp = Accessor->getName();
2036 if (Comp[0] == 's' || Comp[0] == 'S')
2037 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002038
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002039 bool isHi = Comp == "hi";
2040 bool isLo = Comp == "lo";
2041 bool isEven = Comp == "even";
2042 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Nate Begeman8a997642008-05-09 06:41:27 +00002044 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2045 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Nate Begeman8a997642008-05-09 06:41:27 +00002047 if (isHi)
2048 Index = e + i;
2049 else if (isLo)
2050 Index = i;
2051 else if (isEven)
2052 Index = 2 * i;
2053 else if (isOdd)
2054 Index = 2 * i + 1;
2055 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002056 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002057
Nate Begeman3b8d1162008-05-13 21:03:02 +00002058 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002059 }
Nate Begeman8a997642008-05-09 06:41:27 +00002060}
2061
Steve Naroff68d331a2007-09-27 14:38:14 +00002062// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +00002063ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002064 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00002065 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00002066 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002067 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002068 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002069 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00002070 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002071 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002072 if (NumArgs) {
2073 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002074 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2075 }
Steve Naroff563477d2007-09-18 23:55:05 +00002076 LBracloc = LBrac;
2077 RBracloc = RBrac;
2078}
2079
Mike Stump1eb44332009-09-09 15:08:12 +00002080// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002081// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00002082ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002083 QualType retType, ObjCMethodDecl *mproto,
Steve Naroffdb611d52007-11-03 16:37:59 +00002084 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00002085 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002086 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002087 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002088 NumArgs = nargs;
Ted Kremenek55499762008-06-17 02:43:46 +00002089 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002090 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002091 if (NumArgs) {
2092 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002093 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2094 }
Steve Naroff563477d2007-09-18 23:55:05 +00002095 LBracloc = LBrac;
2096 RBracloc = RBrac;
2097}
2098
Mike Stump1eb44332009-09-09 15:08:12 +00002099// constructor for class messages.
Ted Kremenek4df728e2008-06-24 15:50:53 +00002100ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
2101 QualType retType, ObjCMethodDecl *mproto,
2102 SourceLocation LBrac, SourceLocation RBrac,
2103 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002104: Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenek4df728e2008-06-24 15:50:53 +00002105MethodProto(mproto) {
2106 NumArgs = nargs;
2107 SubExprs = new Stmt*[NumArgs+1];
2108 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2109 if (NumArgs) {
2110 for (unsigned i = 0; i != NumArgs; ++i)
2111 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2112 }
2113 LBracloc = LBrac;
2114 RBracloc = RBrac;
2115}
2116
2117ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2118 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2119 switch (x & Flags) {
2120 default:
2121 assert(false && "Invalid ObjCMessageExpr.");
2122 case IsInstMeth:
2123 return ClassInfo(0, 0);
2124 case IsClsMethDeclUnknown:
2125 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
2126 case IsClsMethDeclKnown: {
2127 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
2128 return ClassInfo(D, D->getIdentifier());
2129 }
2130 }
2131}
2132
Chris Lattner0389e6b2009-04-26 00:44:05 +00002133void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
2134 if (CI.first == 0 && CI.second == 0)
2135 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
2136 else if (CI.first == 0)
2137 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown);
2138 else
2139 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown);
2140}
2141
2142
Chris Lattner27437ca2007-10-25 00:29:32 +00002143bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002144 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002145}
2146
Nate Begeman888376a2009-08-12 02:28:50 +00002147void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2148 unsigned NumExprs) {
2149 if (SubExprs) C.Deallocate(SubExprs);
2150
2151 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002152 this->NumExprs = NumExprs;
2153 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002154}
Nate Begeman888376a2009-08-12 02:28:50 +00002155
2156void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2157 DestroyChildren(C);
2158 if (SubExprs) C.Deallocate(SubExprs);
2159 this->~ShuffleVectorExpr();
2160 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002161}
2162
Douglas Gregor42602bb2009-08-07 06:08:38 +00002163void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002164 // Override default behavior of traversing children. If this has a type
2165 // operand and the type is a variable-length array, the child iteration
2166 // will iterate over the size expression. However, this expression belongs
2167 // to the type, not to this, so we don't want to delete it.
2168 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002169 if (isArgumentType()) {
2170 this->~SizeOfAlignOfExpr();
2171 C.Deallocate(this);
2172 }
Sebastian Redl05189992008-11-11 17:56:53 +00002173 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002174 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002175}
2176
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002177//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002178// DesignatedInitExpr
2179//===----------------------------------------------------------------------===//
2180
2181IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2182 assert(Kind == FieldDesignator && "Only valid on a field designator");
2183 if (Field.NameOrField & 0x01)
2184 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2185 else
2186 return getField()->getIdentifier();
2187}
2188
Douglas Gregor319d57f2010-01-06 23:17:19 +00002189DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2190 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002191 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002192 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002193 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002194 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002195 unsigned NumIndexExprs,
2196 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002197 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002198 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002199 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2200 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002201 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002202
2203 // Record the initializer itself.
2204 child_iterator Child = child_begin();
2205 *Child++ = Init;
2206
2207 // Copy the designators and their subexpressions, computing
2208 // value-dependence along the way.
2209 unsigned IndexIdx = 0;
2210 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002211 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002212
2213 if (this->Designators[I].isArrayDesignator()) {
2214 // Compute type- and value-dependence.
2215 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002216 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002217 Index->isTypeDependent() || Index->isValueDependent();
2218
2219 // Copy the index expressions into permanent storage.
2220 *Child++ = IndexExprs[IndexIdx++];
2221 } else if (this->Designators[I].isArrayRangeDesignator()) {
2222 // Compute type- and value-dependence.
2223 Expr *Start = IndexExprs[IndexIdx];
2224 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002225 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002226 Start->isTypeDependent() || Start->isValueDependent() ||
2227 End->isTypeDependent() || End->isValueDependent();
2228
2229 // Copy the start/end expressions into permanent storage.
2230 *Child++ = IndexExprs[IndexIdx++];
2231 *Child++ = IndexExprs[IndexIdx++];
2232 }
2233 }
2234
2235 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002236}
2237
Douglas Gregor05c13a32009-01-22 00:58:24 +00002238DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002239DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002240 unsigned NumDesignators,
2241 Expr **IndexExprs, unsigned NumIndexExprs,
2242 SourceLocation ColonOrEqualLoc,
2243 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002244 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002245 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002246 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002247 ColonOrEqualLoc, UsesColonSyntax,
2248 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002249}
2250
Mike Stump1eb44332009-09-09 15:08:12 +00002251DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002252 unsigned NumIndexExprs) {
2253 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2254 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2255 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2256}
2257
Douglas Gregor319d57f2010-01-06 23:17:19 +00002258void DesignatedInitExpr::setDesignators(ASTContext &C,
2259 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002260 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002261 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002262
Douglas Gregor319d57f2010-01-06 23:17:19 +00002263 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002264 NumDesignators = NumDesigs;
2265 for (unsigned I = 0; I != NumDesigs; ++I)
2266 Designators[I] = Desigs[I];
2267}
2268
Douglas Gregor05c13a32009-01-22 00:58:24 +00002269SourceRange DesignatedInitExpr::getSourceRange() const {
2270 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002271 Designator &First =
2272 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002273 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002274 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002275 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2276 else
2277 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2278 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002279 StartLoc =
2280 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002281 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2282}
2283
Douglas Gregor05c13a32009-01-22 00:58:24 +00002284Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2285 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2286 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2287 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002288 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2289 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2290}
2291
2292Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002293 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002294 "Requires array range designator");
2295 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2296 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002297 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2298 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2299}
2300
2301Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002302 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002303 "Requires array range designator");
2304 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2305 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002306 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2307 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2308}
2309
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002310/// \brief Replaces the designator at index @p Idx with the series
2311/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002312void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002313 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002314 const Designator *Last) {
2315 unsigned NumNewDesignators = Last - First;
2316 if (NumNewDesignators == 0) {
2317 std::copy_backward(Designators + Idx + 1,
2318 Designators + NumDesignators,
2319 Designators + Idx);
2320 --NumNewDesignators;
2321 return;
2322 } else if (NumNewDesignators == 1) {
2323 Designators[Idx] = *First;
2324 return;
2325 }
2326
Mike Stump1eb44332009-09-09 15:08:12 +00002327 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002328 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002329 std::copy(Designators, Designators + Idx, NewDesignators);
2330 std::copy(First, Last, NewDesignators + Idx);
2331 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2332 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002333 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002334 Designators = NewDesignators;
2335 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2336}
2337
Douglas Gregor42602bb2009-08-07 06:08:38 +00002338void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002339 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002340 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002341}
2342
Douglas Gregor319d57f2010-01-06 23:17:19 +00002343void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2344 for (unsigned I = 0; I != NumDesignators; ++I)
2345 Designators[I].~Designator();
2346 C.Deallocate(Designators);
2347 Designators = 0;
2348}
2349
Mike Stump1eb44332009-09-09 15:08:12 +00002350ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002351 Expr **exprs, unsigned nexprs,
2352 SourceLocation rparenloc)
2353: Expr(ParenListExprClass, QualType(),
2354 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002355 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002356 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002357
Nate Begeman2ef13e52009-08-10 23:49:36 +00002358 Exprs = new (C) Stmt*[nexprs];
2359 for (unsigned i = 0; i != nexprs; ++i)
2360 Exprs[i] = exprs[i];
2361}
2362
2363void ParenListExpr::DoDestroy(ASTContext& C) {
2364 DestroyChildren(C);
2365 if (Exprs) C.Deallocate(Exprs);
2366 this->~ParenListExpr();
2367 C.Deallocate(this);
2368}
2369
Douglas Gregor05c13a32009-01-22 00:58:24 +00002370//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002371// ExprIterator.
2372//===----------------------------------------------------------------------===//
2373
2374Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2375Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2376Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2377const Expr* ConstExprIterator::operator[](size_t idx) const {
2378 return cast<Expr>(I[idx]);
2379}
2380const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2381const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2382
2383//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002384// Child Iterators for iterating over subexpressions/substatements
2385//===----------------------------------------------------------------------===//
2386
2387// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002388Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2389Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002390
Steve Naroff7779db42007-11-12 14:29:37 +00002391// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002392Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2393Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002394
Steve Naroffe3e9add2008-06-02 23:03:37 +00002395// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002396Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2397Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002398
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002399// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002400Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2401 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002402}
Mike Stump1eb44332009-09-09 15:08:12 +00002403Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2404 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002405}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002406
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002407// ObjCSuperExpr
2408Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2409Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2410
Steve Narofff242b1b2009-07-24 17:54:45 +00002411// ObjCIsaExpr
2412Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2413Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2414
Chris Lattnerd9f69102008-08-10 01:53:14 +00002415// PredefinedExpr
2416Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2417Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002418
2419// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002420Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2421Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002422
2423// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002424Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002425Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002426
2427// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002428Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2429Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002430
Chris Lattner5d661452007-08-26 03:42:43 +00002431// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002432Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2433Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002434
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002435// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002436Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2437Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002438
2439// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002440Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2441Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002442
2443// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002444Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2445Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002446
Sebastian Redl05189992008-11-11 17:56:53 +00002447// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002448Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002449 // If this is of a type and the type is a VLA type (and not a typedef), the
2450 // size expression of the VLA needs to be treated as an executable expression.
2451 // Why isn't this weirdness documented better in StmtIterator?
2452 if (isArgumentType()) {
2453 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2454 getArgumentType().getTypePtr()))
2455 return child_iterator(T);
2456 return child_iterator();
2457 }
Sebastian Redld4575892008-12-03 23:17:54 +00002458 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002459}
Sebastian Redl05189992008-11-11 17:56:53 +00002460Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2461 if (isArgumentType())
2462 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002463 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002464}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002465
2466// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002467Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002468 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002469}
Ted Kremenek1237c672007-08-24 20:06:47 +00002470Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002471 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002472}
2473
2474// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002475Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002476 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002477}
Ted Kremenek1237c672007-08-24 20:06:47 +00002478Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002479 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002480}
Ted Kremenek1237c672007-08-24 20:06:47 +00002481
2482// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002483Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2484Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002485
Nate Begeman213541a2008-04-18 23:10:10 +00002486// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002487Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2488Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002489
2490// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002491Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2492Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002493
Ted Kremenek1237c672007-08-24 20:06:47 +00002494// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002495Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2496Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002497
2498// BinaryOperator
2499Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002500 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002501}
Ted Kremenek1237c672007-08-24 20:06:47 +00002502Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002503 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002504}
2505
2506// ConditionalOperator
2507Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002508 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002509}
Ted Kremenek1237c672007-08-24 20:06:47 +00002510Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002511 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002512}
2513
2514// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002515Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2516Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002517
Ted Kremenek1237c672007-08-24 20:06:47 +00002518// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002519Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2520Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002521
2522// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002523Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2524 return child_iterator();
2525}
2526
2527Stmt::child_iterator TypesCompatibleExpr::child_end() {
2528 return child_iterator();
2529}
Ted Kremenek1237c672007-08-24 20:06:47 +00002530
2531// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002532Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2533Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002534
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002535// GNUNullExpr
2536Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2537Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2538
Eli Friedmand38617c2008-05-14 19:38:39 +00002539// ShuffleVectorExpr
2540Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002541 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002542}
2543Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002544 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002545}
2546
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002547// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002548Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2549Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002550
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002551// InitListExpr
2552Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002553 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002554}
2555Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002556 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002557}
2558
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002559// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002560Stmt::child_iterator DesignatedInitExpr::child_begin() {
2561 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2562 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002563 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2564}
2565Stmt::child_iterator DesignatedInitExpr::child_end() {
2566 return child_iterator(&*child_begin() + NumSubExprs);
2567}
2568
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002569// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002570Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2571 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002572}
2573
Mike Stump1eb44332009-09-09 15:08:12 +00002574Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2575 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002576}
2577
Nate Begeman2ef13e52009-08-10 23:49:36 +00002578// ParenListExpr
2579Stmt::child_iterator ParenListExpr::child_begin() {
2580 return &Exprs[0];
2581}
2582Stmt::child_iterator ParenListExpr::child_end() {
2583 return &Exprs[0]+NumExprs;
2584}
2585
Ted Kremenek1237c672007-08-24 20:06:47 +00002586// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002587Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002588 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002589}
2590Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002591 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002592}
Ted Kremenek1237c672007-08-24 20:06:47 +00002593
2594// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002595Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2596Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002597
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002598// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002599Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002600 return child_iterator();
2601}
2602Stmt::child_iterator ObjCSelectorExpr::child_end() {
2603 return child_iterator();
2604}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002605
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002606// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002607Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2608 return child_iterator();
2609}
2610Stmt::child_iterator ObjCProtocolExpr::child_end() {
2611 return child_iterator();
2612}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002613
Steve Naroff563477d2007-09-18 23:55:05 +00002614// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002615Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002616 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002617}
2618Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002619 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002620}
2621
Steve Naroff4eb206b2008-09-03 18:15:37 +00002622// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002623Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2624Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002625
Ted Kremenek9da13f92008-09-26 23:24:14 +00002626Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2627Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }