blob: 88b5b5e5674f5e8285930d80a790bc1f4318cba0 [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
Chris Lattner2b334bb2010-04-16 23:34:13 +000030/// isKnownToHaveBooleanValue - Return true if this is an integer expression
31/// that is known to return 0 or 1. This happens for _Bool/bool expressions
32/// but also int expressions which are produced by things like comparisons in
33/// C.
34bool Expr::isKnownToHaveBooleanValue() const {
35 // If this value has _Bool type, it is obvious 0/1.
36 if (getType()->isBooleanType()) return true;
37 // If this is a non-scalar-integer type, we don't care enough to try.
38 if (!getType()->isIntegralType()) return false;
39
40 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
41 return PE->getSubExpr()->isKnownToHaveBooleanValue();
42
43 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
44 switch (UO->getOpcode()) {
45 case UnaryOperator::Plus:
46 case UnaryOperator::Extension:
47 return UO->getSubExpr()->isKnownToHaveBooleanValue();
48 default:
49 return false;
50 }
51 }
52
53 if (const CastExpr *CE = dyn_cast<CastExpr>(this))
54 return CE->getSubExpr()->isKnownToHaveBooleanValue();
55
56 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
57 switch (BO->getOpcode()) {
58 default: return false;
59 case BinaryOperator::LT: // Relational operators.
60 case BinaryOperator::GT:
61 case BinaryOperator::LE:
62 case BinaryOperator::GE:
63 case BinaryOperator::EQ: // Equality operators.
64 case BinaryOperator::NE:
65 case BinaryOperator::LAnd: // AND operator.
66 case BinaryOperator::LOr: // Logical OR operator.
67 return true;
68
69 case BinaryOperator::And: // Bitwise AND operator.
70 case BinaryOperator::Xor: // Bitwise XOR operator.
71 case BinaryOperator::Or: // Bitwise OR operator.
72 // Handle things like (x==2)|(y==12).
73 return BO->getLHS()->isKnownToHaveBooleanValue() &&
74 BO->getRHS()->isKnownToHaveBooleanValue();
75
76 case BinaryOperator::Comma:
77 case BinaryOperator::Assign:
78 return BO->getRHS()->isKnownToHaveBooleanValue();
79 }
80 }
81
82 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
83 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
84 CO->getFalseExpr()->isKnownToHaveBooleanValue();
85
86 return false;
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCalld5532b62009-11-23 01:53:49 +000093void ExplicitTemplateArgumentList::initializeFrom(
94 const TemplateArgumentListInfo &Info) {
95 LAngleLoc = Info.getLAngleLoc();
96 RAngleLoc = Info.getRAngleLoc();
97 NumTemplateArgs = Info.size();
98
99 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
100 for (unsigned i = 0; i != NumTemplateArgs; ++i)
101 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
102}
103
104void ExplicitTemplateArgumentList::copyInto(
105 TemplateArgumentListInfo &Info) const {
106 Info.setLAngleLoc(LAngleLoc);
107 Info.setRAngleLoc(RAngleLoc);
108 for (unsigned I = 0; I != NumTemplateArgs; ++I)
109 Info.addArgument(getTemplateArgs()[I]);
110}
111
112std::size_t ExplicitTemplateArgumentList::sizeFor(
113 const TemplateArgumentListInfo &Info) {
114 return sizeof(ExplicitTemplateArgumentList) +
115 sizeof(TemplateArgumentLoc) * Info.size();
116}
117
Douglas Gregor0da76df2009-11-23 11:41:28 +0000118void DeclRefExpr::computeDependence() {
119 TypeDependent = false;
120 ValueDependent = false;
121
122 NamedDecl *D = getDecl();
123
124 // (TD) C++ [temp.dep.expr]p3:
125 // An id-expression is type-dependent if it contains:
126 //
127 // and
128 //
129 // (VD) C++ [temp.dep.constexpr]p2:
130 // An identifier is value-dependent if it is:
131
132 // (TD) - an identifier that was declared with dependent type
133 // (VD) - a name declared with a dependent type,
134 if (getType()->isDependentType()) {
135 TypeDependent = true;
136 ValueDependent = true;
137 }
138 // (TD) - a conversion-function-id that specifies a dependent type
139 else if (D->getDeclName().getNameKind()
140 == DeclarationName::CXXConversionFunctionName &&
141 D->getDeclName().getCXXNameType()->isDependentType()) {
142 TypeDependent = true;
143 ValueDependent = true;
144 }
145 // (TD) - a template-id that is dependent,
146 else if (hasExplicitTemplateArgumentList() &&
147 TemplateSpecializationType::anyDependentTemplateArguments(
148 getTemplateArgs(),
149 getNumTemplateArgs())) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (VD) - the name of a non-type template parameter,
154 else if (isa<NonTypeTemplateParmDecl>(D))
155 ValueDependent = true;
156 // (VD) - a constant with integral or enumeration type and is
157 // initialized with an expression that is value-dependent.
158 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
159 if (Var->getType()->isIntegralType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
164 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000165 }
166 // (TD) - a nested-name-specifier or a qualified-id that names a
167 // member of an unknown specialization.
168 // (handled by DependentScopeDeclRefExpr)
169}
170
Douglas Gregora2813ce2009-10-23 18:54:35 +0000171DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
172 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000173 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000174 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000175 QualType T)
176 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000177 DecoratedD(D,
178 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000179 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000180 Loc(NameLoc) {
181 if (Qualifier) {
182 NameQualifier *NQ = getNameQualifier();
183 NQ->NNS = Qualifier;
184 NQ->Range = QualifierRange;
185 }
186
John McCalld5532b62009-11-23 01:53:49 +0000187 if (TemplateArgs)
188 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000189
190 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000191}
192
193DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
194 NestedNameSpecifier *Qualifier,
195 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000196 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000197 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000198 QualType T,
199 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000200 std::size_t Size = sizeof(DeclRefExpr);
201 if (Qualifier != 0)
202 Size += sizeof(NameQualifier);
203
John McCalld5532b62009-11-23 01:53:49 +0000204 if (TemplateArgs)
205 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000206
207 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
208 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000209 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000210}
211
212SourceRange DeclRefExpr::getSourceRange() const {
213 // FIXME: Does not handle multi-token names well, e.g., operator[].
214 SourceRange R(Loc);
215
216 if (hasQualifier())
217 R.setBegin(getQualifierRange().getBegin());
218 if (hasExplicitTemplateArgumentList())
219 R.setEnd(getRAngleLoc());
220 return R;
221}
222
Anders Carlsson3a082d82009-09-08 18:24:21 +0000223// FIXME: Maybe this should use DeclPrinter with a special "print predefined
224// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000225std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
226 ASTContext &Context = CurrentDecl->getASTContext();
227
Anders Carlsson3a082d82009-09-08 18:24:21 +0000228 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000229 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000230 return FD->getNameAsString();
231
232 llvm::SmallString<256> Name;
233 llvm::raw_svector_ostream Out(Name);
234
235 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000236 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000237 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000238 if (MD->isStatic())
239 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000240 }
241
242 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000243
244 std::string Proto = FD->getQualifiedNameAsString(Policy);
245
John McCall183700f2009-09-21 23:43:11 +0000246 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000247 const FunctionProtoType *FT = 0;
248 if (FD->hasWrittenPrototype())
249 FT = dyn_cast<FunctionProtoType>(AFT);
250
251 Proto += "(";
252 if (FT) {
253 llvm::raw_string_ostream POut(Proto);
254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
255 if (i) POut << ", ";
256 std::string Param;
257 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
258 POut << Param;
259 }
260
261 if (FT->isVariadic()) {
262 if (FD->getNumParams()) POut << ", ";
263 POut << "...";
264 }
265 }
266 Proto += ")";
267
Sam Weinig4eadcc52009-12-27 01:38:20 +0000268 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
269 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
270 if (ThisQuals.hasConst())
271 Proto += " const";
272 if (ThisQuals.hasVolatile())
273 Proto += " volatile";
274 }
275
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000276 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
277 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000278
279 Out << Proto;
280
281 Out.flush();
282 return Name.str().str();
283 }
284 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
285 llvm::SmallString<256> Name;
286 llvm::raw_svector_ostream Out(Name);
287 Out << (MD->isInstanceMethod() ? '-' : '+');
288 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000289
290 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
291 // a null check to avoid a crash.
292 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000293 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000294
Anders Carlsson3a082d82009-09-08 18:24:21 +0000295 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000296 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
297 Out << '(' << CID << ')';
298
Anders Carlsson3a082d82009-09-08 18:24:21 +0000299 Out << ' ';
300 Out << MD->getSelector().getAsString();
301 Out << ']';
302
303 Out.flush();
304 return Name.str().str();
305 }
306 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
307 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
308 return "top level";
309 }
310 return "";
311}
312
Chris Lattnerda8249e2008-06-07 22:13:43 +0000313/// getValueAsApproximateDouble - This returns the value as an inaccurate
314/// double. Note that this may cause loss of precision, but is useful for
315/// debugging dumps, etc.
316double FloatingLiteral::getValueAsApproximateDouble() const {
317 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000318 bool ignored;
319 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
320 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000321 return V.convertToDouble();
322}
323
Chris Lattner2085fd62009-02-18 06:40:38 +0000324StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
325 unsigned ByteLength, bool Wide,
326 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000327 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000328 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000329 // Allocate enough space for the StringLiteral plus an array of locations for
330 // any concatenated string tokens.
331 void *Mem = C.Allocate(sizeof(StringLiteral)+
332 sizeof(SourceLocation)*(NumStrs-1),
333 llvm::alignof<StringLiteral>());
334 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000337 char *AStrData = new (C, 1) char[ByteLength];
338 memcpy(AStrData, StrData, ByteLength);
339 SL->StrData = AStrData;
340 SL->ByteLength = ByteLength;
341 SL->IsWide = Wide;
342 SL->TokLocs[0] = Loc[0];
343 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
Chris Lattner726e1682009-02-18 05:49:11 +0000345 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000346 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
347 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000348}
349
Douglas Gregor673ecd62009-04-15 16:35:07 +0000350StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
351 void *Mem = C.Allocate(sizeof(StringLiteral)+
352 sizeof(SourceLocation)*(NumStrs-1),
353 llvm::alignof<StringLiteral>());
354 StringLiteral *SL = new (Mem) StringLiteral(QualType());
355 SL->StrData = 0;
356 SL->ByteLength = 0;
357 SL->NumConcatenated = NumStrs;
358 return SL;
359}
360
Douglas Gregor42602bb2009-08-07 06:08:38 +0000361void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000362 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000363 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
Daniel Dunbarb6480232009-09-22 03:27:33 +0000366void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000367 if (StrData)
368 C.Deallocate(const_cast<char*>(StrData));
369
Daniel Dunbarb6480232009-09-22 03:27:33 +0000370 char *AStrData = new (C, 1) char[Str.size()];
371 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000372 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000373 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
377/// corresponds to, e.g. "sizeof" or "[pre]++".
378const char *UnaryOperator::getOpcodeStr(Opcode Op) {
379 switch (Op) {
380 default: assert(0 && "Unknown unary operator");
381 case PostInc: return "++";
382 case PostDec: return "--";
383 case PreInc: return "++";
384 case PreDec: return "--";
385 case AddrOf: return "&";
386 case Deref: return "*";
387 case Plus: return "+";
388 case Minus: return "-";
389 case Not: return "~";
390 case LNot: return "!";
391 case Real: return "__real";
392 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000394 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
396}
397
Mike Stump1eb44332009-09-09 15:08:12 +0000398UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000399UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
400 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000401 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000402 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
403 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
404 case OO_Amp: return AddrOf;
405 case OO_Star: return Deref;
406 case OO_Plus: return Plus;
407 case OO_Minus: return Minus;
408 case OO_Tilde: return Not;
409 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000410 }
411}
412
413OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
414 switch (Opc) {
415 case PostInc: case PreInc: return OO_PlusPlus;
416 case PostDec: case PreDec: return OO_MinusMinus;
417 case AddrOf: return OO_Amp;
418 case Deref: return OO_Star;
419 case Plus: return OO_Plus;
420 case Minus: return OO_Minus;
421 case Not: return OO_Tilde;
422 case LNot: return OO_Exclaim;
423 default: return OO_None;
424 }
425}
426
427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428//===----------------------------------------------------------------------===//
429// Postfix Operators.
430//===----------------------------------------------------------------------===//
431
Ted Kremenek668bf912009-02-09 20:51:47 +0000432CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000433 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000434 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000435 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000436 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000437 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek668bf912009-02-09 20:51:47 +0000439 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000440 SubExprs[FN] = fn;
441 for (unsigned i = 0; i != numargs; ++i)
442 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000443
Douglas Gregorb4609802008-11-14 16:09:21 +0000444 RParenLoc = rparenloc;
445}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000446
Ted Kremenek668bf912009-02-09 20:51:47 +0000447CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
448 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000449 : Expr(CallExprClass, t,
450 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000451 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000452 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000453
454 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000455 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000457 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 RParenLoc = rparenloc;
460}
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
463 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000464 SubExprs = new (C) Stmt*[1];
465}
466
Douglas Gregor42602bb2009-08-07 06:08:38 +0000467void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000468 DestroyChildren(C);
469 if (SubExprs) C.Deallocate(SubExprs);
470 this->~CallExpr();
471 C.Deallocate(this);
472}
473
Nuno Lopesd20254f2009-12-20 23:11:08 +0000474Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000475 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000476 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000477 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000478 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
479 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000480
481 return 0;
482}
483
Nuno Lopesd20254f2009-12-20 23:11:08 +0000484FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000485 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000486}
487
Chris Lattnerd18b3292007-12-28 05:25:02 +0000488/// setNumArgs - This changes the number of arguments present in this call.
489/// Any orphaned expressions are deleted by this, and any new operands are set
490/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000491void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000492 // No change, just return.
493 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerd18b3292007-12-28 05:25:02 +0000495 // If shrinking # arguments, just delete the extras and forgot them.
496 if (NumArgs < getNumArgs()) {
497 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000498 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000499 this->NumArgs = NumArgs;
500 return;
501 }
502
503 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000504 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000505 // Copy over args.
506 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
507 NewSubExprs[i] = SubExprs[i];
508 // Null out new args.
509 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
510 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor88c9a462009-04-17 21:46:47 +0000512 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000513 SubExprs = NewSubExprs;
514 this->NumArgs = NumArgs;
515}
516
Chris Lattnercb888962008-10-06 05:00:53 +0000517/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
518/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000519unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000520 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000521 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000522 // ImplicitCastExpr.
523 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
524 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000525 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000527 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
528 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000529 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlssonbcba2012008-01-31 02:13:57 +0000531 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
532 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000533 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000535 if (!FDecl->getIdentifier())
536 return 0;
537
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000538 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000539}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000540
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000541QualType CallExpr::getCallReturnType() const {
542 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000543 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000544 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000545 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000546 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
John McCall183700f2009-09-21 23:43:11 +0000548 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000549 return FnType->getResultType();
550}
Chris Lattnercb888962008-10-06 05:00:53 +0000551
Mike Stump1eb44332009-09-09 15:08:12 +0000552MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
553 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000554 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000555 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000556 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000557 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000558 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000559 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000560 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000561
John McCall161755a2010-04-06 21:38:20 +0000562 bool hasQualOrFound = (qual != 0 ||
563 founddecl.getDecl() != memberdecl ||
564 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000565 if (hasQualOrFound)
566 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
John McCalld5532b62009-11-23 01:53:49 +0000568 if (targs)
569 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000571 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000572 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
573
574 if (hasQualOrFound) {
575 if (qual && qual->isDependent()) {
576 E->setValueDependent(true);
577 E->setTypeDependent(true);
578 }
579 E->HasQualifierOrFoundDecl = true;
580
581 MemberNameQualifier *NQ = E->getMemberQualifier();
582 NQ->NNS = qual;
583 NQ->Range = qualrange;
584 NQ->FoundDecl = founddecl;
585 }
586
587 if (targs) {
588 E->HasExplicitTemplateArgumentList = true;
589 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
590 }
591
592 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000593}
594
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000595const char *CastExpr::getCastKindName() const {
596 switch (getCastKind()) {
597 case CastExpr::CK_Unknown:
598 return "Unknown";
599 case CastExpr::CK_BitCast:
600 return "BitCast";
601 case CastExpr::CK_NoOp:
602 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000603 case CastExpr::CK_BaseToDerived:
604 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000605 case CastExpr::CK_DerivedToBase:
606 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000607 case CastExpr::CK_UncheckedDerivedToBase:
608 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000609 case CastExpr::CK_Dynamic:
610 return "Dynamic";
611 case CastExpr::CK_ToUnion:
612 return "ToUnion";
613 case CastExpr::CK_ArrayToPointerDecay:
614 return "ArrayToPointerDecay";
615 case CastExpr::CK_FunctionToPointerDecay:
616 return "FunctionToPointerDecay";
617 case CastExpr::CK_NullToMemberPointer:
618 return "NullToMemberPointer";
619 case CastExpr::CK_BaseToDerivedMemberPointer:
620 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000621 case CastExpr::CK_DerivedToBaseMemberPointer:
622 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000623 case CastExpr::CK_UserDefinedConversion:
624 return "UserDefinedConversion";
625 case CastExpr::CK_ConstructorConversion:
626 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000627 case CastExpr::CK_IntegralToPointer:
628 return "IntegralToPointer";
629 case CastExpr::CK_PointerToIntegral:
630 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000631 case CastExpr::CK_ToVoid:
632 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000633 case CastExpr::CK_VectorSplat:
634 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000635 case CastExpr::CK_IntegralCast:
636 return "IntegralCast";
637 case CastExpr::CK_IntegralToFloating:
638 return "IntegralToFloating";
639 case CastExpr::CK_FloatingToIntegral:
640 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000641 case CastExpr::CK_FloatingCast:
642 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000643 case CastExpr::CK_MemberPointerToBoolean:
644 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000645 case CastExpr::CK_AnyPointerToObjCPointerCast:
646 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000647 case CastExpr::CK_AnyPointerToBlockPointerCast:
648 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000651 assert(0 && "Unhandled cast kind!");
652 return 0;
653}
654
Douglas Gregor6eef5192009-12-14 19:27:10 +0000655Expr *CastExpr::getSubExprAsWritten() {
656 Expr *SubExpr = 0;
657 CastExpr *E = this;
658 do {
659 SubExpr = E->getSubExpr();
660
661 // Skip any temporary bindings; they're implicit.
662 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
663 SubExpr = Binder->getSubExpr();
664
665 // Conversions by constructor and conversion functions have a
666 // subexpression describing the call; strip it off.
667 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
668 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
669 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
670 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
671
672 // If the subexpression we're left with is an implicit cast, look
673 // through that, too.
674 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
675
676 return SubExpr;
677}
678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
680/// corresponds to, e.g. "<<=".
681const char *BinaryOperator::getOpcodeStr(Opcode Op) {
682 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000683 case PtrMemD: return ".*";
684 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 case Mul: return "*";
686 case Div: return "/";
687 case Rem: return "%";
688 case Add: return "+";
689 case Sub: return "-";
690 case Shl: return "<<";
691 case Shr: return ">>";
692 case LT: return "<";
693 case GT: return ">";
694 case LE: return "<=";
695 case GE: return ">=";
696 case EQ: return "==";
697 case NE: return "!=";
698 case And: return "&";
699 case Xor: return "^";
700 case Or: return "|";
701 case LAnd: return "&&";
702 case LOr: return "||";
703 case Assign: return "=";
704 case MulAssign: return "*=";
705 case DivAssign: return "/=";
706 case RemAssign: return "%=";
707 case AddAssign: return "+=";
708 case SubAssign: return "-=";
709 case ShlAssign: return "<<=";
710 case ShrAssign: return ">>=";
711 case AndAssign: return "&=";
712 case XorAssign: return "^=";
713 case OrAssign: return "|=";
714 case Comma: return ",";
715 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000716
717 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000718}
719
Mike Stump1eb44332009-09-09 15:08:12 +0000720BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000721BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
722 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000723 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000724 case OO_Plus: return Add;
725 case OO_Minus: return Sub;
726 case OO_Star: return Mul;
727 case OO_Slash: return Div;
728 case OO_Percent: return Rem;
729 case OO_Caret: return Xor;
730 case OO_Amp: return And;
731 case OO_Pipe: return Or;
732 case OO_Equal: return Assign;
733 case OO_Less: return LT;
734 case OO_Greater: return GT;
735 case OO_PlusEqual: return AddAssign;
736 case OO_MinusEqual: return SubAssign;
737 case OO_StarEqual: return MulAssign;
738 case OO_SlashEqual: return DivAssign;
739 case OO_PercentEqual: return RemAssign;
740 case OO_CaretEqual: return XorAssign;
741 case OO_AmpEqual: return AndAssign;
742 case OO_PipeEqual: return OrAssign;
743 case OO_LessLess: return Shl;
744 case OO_GreaterGreater: return Shr;
745 case OO_LessLessEqual: return ShlAssign;
746 case OO_GreaterGreaterEqual: return ShrAssign;
747 case OO_EqualEqual: return EQ;
748 case OO_ExclaimEqual: return NE;
749 case OO_LessEqual: return LE;
750 case OO_GreaterEqual: return GE;
751 case OO_AmpAmp: return LAnd;
752 case OO_PipePipe: return LOr;
753 case OO_Comma: return Comma;
754 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000755 }
756}
757
758OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
759 static const OverloadedOperatorKind OverOps[] = {
760 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
761 OO_Star, OO_Slash, OO_Percent,
762 OO_Plus, OO_Minus,
763 OO_LessLess, OO_GreaterGreater,
764 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
765 OO_EqualEqual, OO_ExclaimEqual,
766 OO_Amp,
767 OO_Caret,
768 OO_Pipe,
769 OO_AmpAmp,
770 OO_PipePipe,
771 OO_Equal, OO_StarEqual,
772 OO_SlashEqual, OO_PercentEqual,
773 OO_PlusEqual, OO_MinusEqual,
774 OO_LessLessEqual, OO_GreaterGreaterEqual,
775 OO_AmpEqual, OO_CaretEqual,
776 OO_PipeEqual,
777 OO_Comma
778 };
779 return OverOps[Opc];
780}
781
Ted Kremenek709210f2010-04-13 23:39:13 +0000782InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000783 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000784 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000785 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000786 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000787 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenekba7bc552010-02-19 01:50:18 +0000788 UnionFieldInit(0), HadArrayRangeDesignator(false)
789{
790 for (unsigned I = 0; I != numInits; ++I) {
791 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000792 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000793 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000794 ValueDependent = true;
795 }
Ted Kremenekba7bc552010-02-19 01:50:18 +0000796
Ted Kremenek709210f2010-04-13 23:39:13 +0000797 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000798}
Reid Spencer5f016e22007-07-11 17:01:13 +0000799
Ted Kremenek709210f2010-04-13 23:39:13 +0000800void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000801 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000802 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000803}
804
Ted Kremenek709210f2010-04-13 23:39:13 +0000805void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000806 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
807 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000808 InitExprs[Idx]->Destroy(C);
809 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000810}
811
Ted Kremenek709210f2010-04-13 23:39:13 +0000812Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000813 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000814 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000815 InitExprs.back() = expr;
816 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor4c678342009-01-28 21:54:33 +0000819 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
820 InitExprs[Init] = expr;
821 return Result;
822}
823
Steve Naroffbfdcae62008-09-04 15:31:07 +0000824/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000825///
826const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000827 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000828 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000829}
830
Mike Stump1eb44332009-09-09 15:08:12 +0000831SourceLocation BlockExpr::getCaretLocation() const {
832 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000833}
Mike Stump1eb44332009-09-09 15:08:12 +0000834const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000835 return TheBlock->getBody();
836}
Mike Stump1eb44332009-09-09 15:08:12 +0000837Stmt *BlockExpr::getBody() {
838 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000839}
Steve Naroff56ee6892008-10-08 17:01:13 +0000840
841
Reid Spencer5f016e22007-07-11 17:01:13 +0000842//===----------------------------------------------------------------------===//
843// Generic Expression Routines
844//===----------------------------------------------------------------------===//
845
Chris Lattner026dc962009-02-14 07:37:35 +0000846/// isUnusedResultAWarning - Return true if this immediate expression should
847/// be warned about if the result is unused. If so, fill in Loc and Ranges
848/// with location to warn on and the source range[s] to report with the
849/// warning.
850bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000851 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000852 // Don't warn if the expr is type dependent. The type could end up
853 // instantiating to void.
854 if (isTypeDependent())
855 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 switch (getStmtClass()) {
858 default:
John McCall0faede62010-03-12 07:11:26 +0000859 if (getType()->isVoidType())
860 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000861 Loc = getExprLoc();
862 R1 = getSourceRange();
863 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000865 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000866 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 case UnaryOperatorClass: {
868 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000871 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000872 case UnaryOperator::PostInc:
873 case UnaryOperator::PostDec:
874 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000875 case UnaryOperator::PreDec: // ++/--
876 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 case UnaryOperator::Deref:
878 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000879 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000880 return false;
881 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 case UnaryOperator::Real:
883 case UnaryOperator::Imag:
884 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000885 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
886 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000887 return false;
888 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000890 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 }
Chris Lattner026dc962009-02-14 07:37:35 +0000892 Loc = UO->getOperatorLoc();
893 R1 = UO->getSubExpr()->getSourceRange();
894 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000896 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000897 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000898 switch (BO->getOpcode()) {
899 default:
900 break;
901 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
902 case BinaryOperator::Comma:
903 // ((foo = <blah>), 0) is an idiom for hiding the result (and
904 // lvalue-ness) of an assignment written in a macro.
905 if (IntegerLiteral *IE =
906 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
907 if (IE->getValue() == 0)
908 return false;
909 case BinaryOperator::LAnd:
910 case BinaryOperator::LOr:
911 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
912 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000913 }
Chris Lattner026dc962009-02-14 07:37:35 +0000914 if (BO->isAssignmentOp())
915 return false;
916 Loc = BO->getOperatorLoc();
917 R1 = BO->getLHS()->getSourceRange();
918 R2 = BO->getRHS()->getSourceRange();
919 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000920 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000921 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000922 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000924 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000925 // The condition must be evaluated, but if either the LHS or RHS is a
926 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000927 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000928 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000929 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000930 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000931 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000932 }
933
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000935 // If the base pointer or element is to a volatile pointer/field, accessing
936 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000937 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000938 return false;
939 Loc = cast<MemberExpr>(this)->getMemberLoc();
940 R1 = SourceRange(Loc, Loc);
941 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
942 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 case ArraySubscriptExprClass:
945 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000946 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000947 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000948 return false;
949 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
950 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
951 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
952 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000953
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000955 case CXXOperatorCallExprClass:
956 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000957 // If this is a direct call, get the callee.
958 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000959 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000960 // If the callee has attribute pure, const, or warn_unused_result, warn
961 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000962 //
963 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
964 // updated to match for QoI.
965 if (FD->getAttr<WarnUnusedResultAttr>() ||
966 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
967 Loc = CE->getCallee()->getLocStart();
968 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000970 if (unsigned NumArgs = CE->getNumArgs())
971 R2 = SourceRange(CE->getArg(0)->getLocStart(),
972 CE->getArg(NumArgs-1)->getLocEnd());
973 return true;
974 }
Chris Lattner026dc962009-02-14 07:37:35 +0000975 }
976 return false;
977 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000978
979 case CXXTemporaryObjectExprClass:
980 case CXXConstructExprClass:
981 return false;
982
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000983 case ObjCMessageExprClass: {
984 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
985 const ObjCMethodDecl *MD = ME->getMethodDecl();
986 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
987 Loc = getExprLoc();
988 return true;
989 }
Chris Lattner026dc962009-02-14 07:37:35 +0000990 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000993 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +0000994#if 0
Mike Stump1eb44332009-09-09 15:08:12 +0000995 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000996 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +0000997 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +0000998 Loc = Ref->getLocation();
999 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1000 if (Ref->getBase())
1001 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001002#else
1003 Loc = getExprLoc();
1004 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001005#endif
1006 return true;
1007 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001008 case StmtExprClass: {
1009 // Statement exprs don't logically have side effects themselves, but are
1010 // sometimes used in macros in ways that give them a type that is unused.
1011 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1012 // however, if the result of the stmt expr is dead, we don't want to emit a
1013 // warning.
1014 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1015 if (!CS->body_empty())
1016 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001017 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
John McCall0faede62010-03-12 07:11:26 +00001019 if (getType()->isVoidType())
1020 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001021 Loc = cast<StmtExpr>(this)->getLParenLoc();
1022 R1 = getSourceRange();
1023 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001024 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001025 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001026 // If this is an explicit cast to void, allow it. People do this when they
1027 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001028 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001029 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001030 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1031 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1032 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001033 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001034 if (getType()->isVoidType())
1035 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001036 const CastExpr *CE = cast<CastExpr>(this);
1037
1038 // If this is a cast to void or a constructor conversion, check the operand.
1039 // Otherwise, the result of the cast is unused.
1040 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1041 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001042 return (cast<CastExpr>(this)->getSubExpr()
1043 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001044 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1045 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1046 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Eli Friedman4be1f472008-05-19 21:24:43 +00001049 case ImplicitCastExprClass:
1050 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001051 return (cast<ImplicitCastExpr>(this)
1052 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001053
Chris Lattner04421082008-04-08 04:40:51 +00001054 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001055 return (cast<CXXDefaultArgExpr>(this)
1056 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001057
1058 case CXXNewExprClass:
1059 // FIXME: In theory, there might be new expressions that don't have side
1060 // effects (e.g. a placement new with an uninitialized POD).
1061 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001062 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001063 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001064 return (cast<CXXBindTemporaryExpr>(this)
1065 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001066 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001067 return (cast<CXXExprWithTemporaries>(this)
1068 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001069 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001070}
1071
Douglas Gregorba7e2102008-10-22 15:04:37 +00001072/// DeclCanBeLvalue - Determine whether the given declaration can be
1073/// an lvalue. This is a helper routine for isLvalue.
1074static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001075 // C++ [temp.param]p6:
1076 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001077 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001078 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1079 return NTTParm->getType()->isReferenceType();
1080
Douglas Gregor44b43212008-12-11 16:49:14 +00001081 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001082 // C++ 3.10p2: An lvalue refers to an object or function.
1083 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001084 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001085}
1086
Reid Spencer5f016e22007-07-11 17:01:13 +00001087/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1088/// incomplete type other than void. Nonarray expressions that can be lvalues:
1089/// - name, where name must be a variable
1090/// - e[i]
1091/// - (e), where e must be an lvalue
1092/// - e.name, where e must be an lvalue
1093/// - e->name
1094/// - *e, the type of e cannot be a function type
1095/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001096/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001097/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001098///
Chris Lattner28be73f2008-07-26 21:30:36 +00001099Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001100 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1101
1102 isLvalueResult Res = isLvalueInternal(Ctx);
1103 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1104 return Res;
1105
Douglas Gregor98cd5992008-10-21 23:43:52 +00001106 // first, check the type (C99 6.3.2.1). Expressions with function
1107 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001108 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 return LV_NotObjectType;
1110
Steve Naroffacb818a2008-02-10 01:39:04 +00001111 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001112 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001113 return LV_IncompleteVoidType;
1114
Eli Friedman53202852009-05-03 22:36:05 +00001115 return LV_Valid;
1116}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001117
Eli Friedman53202852009-05-03 22:36:05 +00001118// Check whether the expression can be sanely treated like an l-value
1119Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001121 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001122 case StringLiteralClass: // C99 6.5.1p4
1123 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001124 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001125 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1126 // For vectors, make sure base is an lvalue (i.e. not a function call).
1127 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001128 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001130 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001131 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1132 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 return LV_Valid;
1134 break;
Chris Lattner41110242008-06-17 18:05:57 +00001135 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001136 case BlockDeclRefExprClass: {
1137 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001138 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001139 return LV_Valid;
1140 break;
1141 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001142 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001144 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1145 NamedDecl *Member = m->getMemberDecl();
1146 // C++ [expr.ref]p4:
1147 // If E2 is declared to have type "reference to T", then E1.E2
1148 // is an lvalue.
1149 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1150 if (Value->getType()->isReferenceType())
1151 return LV_Valid;
1152
1153 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001154 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001155 return LV_Valid;
1156
1157 // -- If E2 is a non-static data member [...]. If E1 is an
1158 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001159 if (isa<FieldDecl>(Member)) {
1160 if (m->isArrow())
1161 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001162 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001163 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001164
1165 // -- If it refers to a static member function [...], then
1166 // E1.E2 is an lvalue.
1167 // -- Otherwise, if E1.E2 refers to a non-static member
1168 // function [...], then E1.E2 is not an lvalue.
1169 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1170 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1171
1172 // -- If E2 is a member enumerator [...], the expression E1.E2
1173 // is not an lvalue.
1174 if (isa<EnumConstantDecl>(Member))
1175 return LV_InvalidExpression;
1176
1177 // Not an lvalue.
1178 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001179 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001180
Douglas Gregor86f19402008-12-20 23:49:58 +00001181 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001182 if (m->isArrow())
1183 return LV_Valid;
1184 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001185 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1186 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001187 return LV_SubObjCPropertySetting;
1188 return
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001189 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001190 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001191 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001193 return LV_Valid; // C99 6.5.3p4
1194
1195 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001196 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1197 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001198 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001199
1200 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1201 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1202 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1203 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001205 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001206 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1207 return LV_Valid;
1208
1209 // If this is a conversion to a class temporary, make a note of
1210 // that.
1211 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1212 return LV_ClassTemporary;
1213
1214 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001216 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001217 case BinaryOperatorClass:
1218 case CompoundAssignOperatorClass: {
1219 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001220
1221 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1222 BinOp->getOpcode() == BinaryOperator::Comma)
1223 return BinOp->getRHS()->isLvalue(Ctx);
1224
Sebastian Redl22460502009-02-07 00:15:38 +00001225 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001226 // The result of a .* expression is an lvalue only if its first operand is
1227 // an lvalue and its second operand is a pointer to data member.
1228 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001229 !BinOp->getType()->isFunctionType())
1230 return BinOp->getLHS()->isLvalue(Ctx);
1231
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001232 // The result of an ->* expression is an lvalue only if its second operand
1233 // is a pointer to data member.
1234 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1235 !BinOp->getType()->isFunctionType()) {
1236 QualType Ty = BinOp->getRHS()->getType();
1237 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1238 return LV_Valid;
1239 }
1240
Douglas Gregorbf3af052008-11-13 20:12:29 +00001241 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001242 return LV_InvalidExpression;
1243
Douglas Gregorbf3af052008-11-13 20:12:29 +00001244 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001245 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001246 // The result of an assignment operation [...] is an lvalue.
1247 return LV_Valid;
1248
1249
1250 // C99 6.5.16:
1251 // An assignment expression [...] is not an lvalue.
1252 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001255 case CXXOperatorCallExprClass:
1256 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001257 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001258 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001259 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001260 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1261 if (ReturnType->isLValueReferenceType())
1262 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001263
Douglas Gregore873fb72010-02-16 21:39:57 +00001264 // If the function is returning a class temporary, make a note of
1265 // that.
1266 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1267 return LV_ClassTemporary;
1268
Douglas Gregor9d293df2008-10-28 00:22:11 +00001269 break;
1270 }
Steve Naroffe6386392007-12-05 04:00:10 +00001271 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001272 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001273 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001274 case ChooseExprClass:
1275 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001276 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001277 case ExtVectorElementExprClass:
1278 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001279 return LV_DuplicateVectorComponents;
1280 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001281 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1282 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001283 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1284 return LV_Valid;
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001285 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001286 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001287 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001288 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001289 case UnresolvedLookupExprClass:
1290 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001291 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001292 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001293 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001294 case CXXFunctionalCastExprClass:
1295 case CXXStaticCastExprClass:
1296 case CXXDynamicCastExprClass:
1297 case CXXReinterpretCastExprClass:
1298 case CXXConstCastExprClass:
1299 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001300 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001301 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1302 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001303 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1304 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001305 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001306
1307 // If this is a conversion to a class temporary, make a note of
1308 // that.
1309 if (Ctx.getLangOptions().CPlusPlus &&
1310 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1311 return LV_ClassTemporary;
1312
Douglas Gregor9d293df2008-10-28 00:22:11 +00001313 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001314 case CXXTypeidExprClass:
1315 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1316 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001317 case CXXBindTemporaryExprClass:
1318 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1319 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001320 case CXXBindReferenceExprClass:
1321 // Something that's bound to a reference is always an lvalue.
1322 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001323 case ConditionalOperatorClass: {
1324 // Complicated handling is only for C++.
1325 if (!Ctx.getLangOptions().CPlusPlus)
1326 return LV_InvalidExpression;
1327
1328 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1329 // everywhere there's an object converted to an rvalue. Also, any other
1330 // casts should be wrapped by ImplicitCastExprs. There's just the special
1331 // case involving throws to work out.
1332 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001333 Expr *True = Cond->getTrueExpr();
1334 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001335 // C++0x 5.16p2
1336 // If either the second or the third operand has type (cv) void, [...]
1337 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001338 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001339 return LV_InvalidExpression;
1340
1341 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001342 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001343 return LV_InvalidExpression;
1344
1345 // That's it.
1346 return LV_Valid;
1347 }
1348
Douglas Gregor2d48e782009-12-19 07:07:47 +00001349 case Expr::CXXExprWithTemporariesClass:
1350 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1351
1352 case Expr::ObjCMessageExprClass:
1353 if (const ObjCMethodDecl *Method
1354 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1355 if (Method->getResultType()->isLValueReferenceType())
1356 return LV_Valid;
1357 break;
1358
Douglas Gregore873fb72010-02-16 21:39:57 +00001359 case Expr::CXXConstructExprClass:
1360 case Expr::CXXTemporaryObjectExprClass:
1361 case Expr::CXXZeroInitValueExprClass:
1362 return LV_ClassTemporary;
1363
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 default:
1365 break;
1366 }
1367 return LV_InvalidExpression;
1368}
1369
1370/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1371/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001372/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001373/// recursively, any member or element of all contained aggregates or unions)
1374/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001375Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001376Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001377 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001380 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001381 // C++ 3.10p11: Functions cannot be modified, but pointers to
1382 // functions can be modifiable.
1383 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1384 return MLV_NotObjectType;
1385 break;
1386
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 case LV_NotObjectType: return MLV_NotObjectType;
1388 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001389 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001390 case LV_InvalidExpression:
1391 // If the top level is a C-style cast, and the subexpression is a valid
1392 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1393 // GCC extension. We don't support it, but we want to produce good
1394 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001395 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1396 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1397 if (Loc)
1398 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001399 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001400 }
1401 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001402 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001403 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001404 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001405 case LV_ClassTemporary:
1406 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001408
1409 // The following is illegal:
1410 // void takeclosure(void (^C)(void));
1411 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1412 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001413 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001414 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1415 return MLV_NotBlockQualified;
1416 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001417
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001418 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001419 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001420 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1421 if (Expr->getSetterMethod() == 0)
1422 return MLV_NoSetterProperty;
1423 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001424
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001425 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001427 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001429 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001431 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek6217b802009-07-29 21:53:49 +00001434 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001435 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 return MLV_ConstQualified;
1437 }
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Mike Stump1eb44332009-09-09 15:08:12 +00001439 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001440}
1441
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001442/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001443/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001444bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001445 switch (getStmtClass()) {
1446 default:
1447 return false;
1448 case ObjCIvarRefExprClass:
1449 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001450 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001451 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001452 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001453 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001454 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001455 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001456 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001457 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001458 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001459 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001460 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1461 if (VD->hasGlobalStorage())
1462 return true;
1463 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001464 // dereferencing to a pointer is always a gc'able candidate,
1465 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001466 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001467 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001468 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001469 return false;
1470 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001471 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001472 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001473 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001474 }
1475 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001476 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001477 }
1478}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001479Expr* Expr::IgnoreParens() {
1480 Expr* E = this;
1481 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1482 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001484 return E;
1485}
1486
Chris Lattner56f34942008-02-13 01:02:39 +00001487/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1488/// or CastExprs or ImplicitCastExprs, returning their operand.
1489Expr *Expr::IgnoreParenCasts() {
1490 Expr *E = this;
1491 while (true) {
1492 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1493 E = P->getSubExpr();
1494 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1495 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001496 else
1497 return E;
1498 }
1499}
1500
Chris Lattnerecdd8412009-03-13 17:28:01 +00001501/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1502/// value (including ptr->int casts of the same size). Strip off any
1503/// ParenExpr or CastExprs, returning their operand.
1504Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1505 Expr *E = this;
1506 while (true) {
1507 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1508 E = P->getSubExpr();
1509 continue;
1510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Chris Lattnerecdd8412009-03-13 17:28:01 +00001512 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1513 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1514 // ptr<->int casts of the same width. We also ignore all identify casts.
1515 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattnerecdd8412009-03-13 17:28:01 +00001517 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1518 E = SE;
1519 continue;
1520 }
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Chris Lattnerecdd8412009-03-13 17:28:01 +00001522 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1523 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1524 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1525 E = SE;
1526 continue;
1527 }
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Chris Lattnerecdd8412009-03-13 17:28:01 +00001530 return E;
1531 }
1532}
1533
Douglas Gregor6eef5192009-12-14 19:27:10 +00001534bool Expr::isDefaultArgument() const {
1535 const Expr *E = this;
1536 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1537 E = ICE->getSubExprAsWritten();
1538
1539 return isa<CXXDefaultArgExpr>(E);
1540}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001541
Douglas Gregor2f599792010-04-02 18:24:57 +00001542/// \brief Skip over any no-op casts and any temporary-binding
1543/// expressions.
1544static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1545 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1546 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1547 E = ICE->getSubExpr();
1548 else
1549 break;
1550 }
1551
1552 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1553 E = BE->getSubExpr();
1554
1555 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1556 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1557 E = ICE->getSubExpr();
1558 else
1559 break;
1560 }
1561
1562 return E;
1563}
1564
1565const Expr *Expr::getTemporaryObject() const {
1566 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1567
1568 // A cast can produce a temporary object. The object's construction
1569 // is represented as a CXXConstructExpr.
1570 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1571 // Only user-defined and constructor conversions can produce
1572 // temporary objects.
1573 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1574 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1575 return 0;
1576
1577 // Strip off temporary bindings and no-op casts.
1578 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1579
1580 // If this is a constructor conversion, see if we have an object
1581 // construction.
1582 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1583 return dyn_cast<CXXConstructExpr>(Sub);
1584
1585 // If this is a user-defined conversion, see if we have a call to
1586 // a function that itself returns a temporary object.
1587 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1588 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1589 if (CE->getCallReturnType()->isRecordType())
1590 return CE;
1591
1592 return 0;
1593 }
1594
1595 // A call returning a class type returns a temporary.
1596 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1597 if (CE->getCallReturnType()->isRecordType())
1598 return CE;
1599
1600 return 0;
1601 }
1602
1603 // Explicit temporary object constructors create temporaries.
1604 return dyn_cast<CXXTemporaryObjectExpr>(E);
1605}
1606
Douglas Gregor898574e2008-12-05 23:32:09 +00001607/// hasAnyTypeDependentArguments - Determines if any of the expressions
1608/// in Exprs is type-dependent.
1609bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1610 for (unsigned I = 0; I < NumExprs; ++I)
1611 if (Exprs[I]->isTypeDependent())
1612 return true;
1613
1614 return false;
1615}
1616
1617/// hasAnyValueDependentArguments - Determines if any of the expressions
1618/// in Exprs is value-dependent.
1619bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1620 for (unsigned I = 0; I < NumExprs; ++I)
1621 if (Exprs[I]->isValueDependent())
1622 return true;
1623
1624 return false;
1625}
1626
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001627bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001628 // This function is attempting whether an expression is an initializer
1629 // which can be evaluated at compile-time. isEvaluatable handles most
1630 // of the cases, but it can't deal with some initializer-specific
1631 // expressions, and it can't deal with aggregates; we deal with those here,
1632 // and fall back to isEvaluatable for the other cases.
1633
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001634 // FIXME: This function assumes the variable being assigned to
1635 // isn't a reference type!
1636
Anders Carlssone8a32b82008-11-24 05:23:59 +00001637 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001638 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001639 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001640 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001641 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001642 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001643 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001644 // This handles gcc's extension that allows global initializers like
1645 // "struct x {int x;} x = (struct x) {};".
1646 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001647 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001648 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001649 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001650 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001651 // FIXME: This doesn't deal with fields with reference types correctly.
1652 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1653 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001654 const InitListExpr *Exp = cast<InitListExpr>(this);
1655 unsigned numInits = Exp->getNumInits();
1656 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001657 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001658 return false;
1659 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001660 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001661 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001662 case ImplicitValueInitExprClass:
1663 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001664 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001665 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001666 case UnaryOperatorClass: {
1667 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1668 if (Exp->getOpcode() == UnaryOperator::Extension)
1669 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1670 break;
1671 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001672 case BinaryOperatorClass: {
1673 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1674 // but this handles the common case.
1675 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1676 if (Exp->getOpcode() == BinaryOperator::Sub &&
1677 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1678 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1679 return true;
1680 break;
1681 }
Chris Lattner81045d82009-04-21 05:19:11 +00001682 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001683 case CStyleCastExprClass:
1684 // Handle casts with a destination that's a struct or union; this
1685 // deals with both the gcc no-op struct cast extension and the
1686 // cast-to-union extension.
1687 if (getType()->isRecordType())
1688 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001689
1690 // Integer->integer casts can be handled here, which is important for
1691 // things like (int)(&&x-&&y). Scary but true.
1692 if (getType()->isIntegerType() &&
1693 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1694 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1695
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001696 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001697 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001698 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001699}
1700
Reid Spencer5f016e22007-07-11 17:01:13 +00001701/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001702/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001703
1704/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1705/// comma, etc
1706///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001707/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1708/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1709/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001710
Eli Friedmane28d7192009-02-26 09:29:13 +00001711// CheckICE - This function does the fundamental ICE checking: the returned
1712// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1713// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001714// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001715// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001716// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001717//
1718// Meanings of Val:
1719// 0: This expression is an ICE if it can be evaluated by Evaluate.
1720// 1: This expression is not an ICE, but if it isn't evaluated, it's
1721// a legal subexpression for an ICE. This return value is used to handle
1722// the comma operator in C99 mode.
1723// 2: This expression is not an ICE, and is not a legal subexpression for one.
1724
1725struct ICEDiag {
1726 unsigned Val;
1727 SourceLocation Loc;
1728
1729 public:
1730 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1731 ICEDiag() : Val(0) {}
1732};
1733
1734ICEDiag NoDiag() { return ICEDiag(); }
1735
Eli Friedman60ce9632009-02-27 04:07:58 +00001736static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1737 Expr::EvalResult EVResult;
1738 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1739 !EVResult.Val.isInt()) {
1740 return ICEDiag(2, E->getLocStart());
1741 }
1742 return NoDiag();
1743}
1744
Eli Friedmane28d7192009-02-26 09:29:13 +00001745static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001746 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001747 if (!E->getType()->isIntegralType()) {
1748 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001749 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001750
1751 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001752#define STMT(Node, Base) case Expr::Node##Class:
1753#define EXPR(Node, Base)
1754#include "clang/AST/StmtNodes.def"
1755 case Expr::PredefinedExprClass:
1756 case Expr::FloatingLiteralClass:
1757 case Expr::ImaginaryLiteralClass:
1758 case Expr::StringLiteralClass:
1759 case Expr::ArraySubscriptExprClass:
1760 case Expr::MemberExprClass:
1761 case Expr::CompoundAssignOperatorClass:
1762 case Expr::CompoundLiteralExprClass:
1763 case Expr::ExtVectorElementExprClass:
1764 case Expr::InitListExprClass:
1765 case Expr::DesignatedInitExprClass:
1766 case Expr::ImplicitValueInitExprClass:
1767 case Expr::ParenListExprClass:
1768 case Expr::VAArgExprClass:
1769 case Expr::AddrLabelExprClass:
1770 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001771 case Expr::CXXMemberCallExprClass:
1772 case Expr::CXXDynamicCastExprClass:
1773 case Expr::CXXTypeidExprClass:
1774 case Expr::CXXNullPtrLiteralExprClass:
1775 case Expr::CXXThisExprClass:
1776 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001777 case Expr::CXXNewExprClass:
1778 case Expr::CXXDeleteExprClass:
1779 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001780 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001781 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001782 case Expr::CXXConstructExprClass:
1783 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001784 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001785 case Expr::CXXExprWithTemporariesClass:
1786 case Expr::CXXTemporaryObjectExprClass:
1787 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001788 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001789 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001790 case Expr::ObjCStringLiteralClass:
1791 case Expr::ObjCEncodeExprClass:
1792 case Expr::ObjCMessageExprClass:
1793 case Expr::ObjCSelectorExprClass:
1794 case Expr::ObjCProtocolExprClass:
1795 case Expr::ObjCIvarRefExprClass:
1796 case Expr::ObjCPropertyRefExprClass:
1797 case Expr::ObjCImplicitSetterGetterRefExprClass:
1798 case Expr::ObjCSuperExprClass:
1799 case Expr::ObjCIsaExprClass:
1800 case Expr::ShuffleVectorExprClass:
1801 case Expr::BlockExprClass:
1802 case Expr::BlockDeclRefExprClass:
1803 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001804 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001805
Douglas Gregor043cad22009-09-11 00:18:58 +00001806 case Expr::GNUNullExprClass:
1807 // GCC considers the GNU __null value to be an integral constant expression.
1808 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001809
Eli Friedmane28d7192009-02-26 09:29:13 +00001810 case Expr::ParenExprClass:
1811 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1812 case Expr::IntegerLiteralClass:
1813 case Expr::CharacterLiteralClass:
1814 case Expr::CXXBoolLiteralExprClass:
1815 case Expr::CXXZeroInitValueExprClass:
1816 case Expr::TypesCompatibleExprClass:
1817 case Expr::UnaryTypeTraitExprClass:
1818 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001819 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001820 case Expr::CXXOperatorCallExprClass: {
1821 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001822 if (CE->isBuiltinCall(Ctx))
1823 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001824 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001825 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001826 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001827 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1828 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001829 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001830 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001831 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1832
1833 // Parameter variables are never constants. Without this check,
1834 // getAnyInitializer() can find a default argument, which leads
1835 // to chaos.
1836 if (isa<ParmVarDecl>(D))
1837 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1838
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001839 // C++ 7.1.5.1p2
1840 // A variable of non-volatile const-qualified integral or enumeration
1841 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001842 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001843 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1844 if (Quals.hasVolatile() || !Quals.hasConst())
1845 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1846
Sebastian Redl31310a22010-02-01 20:16:42 +00001847 // Look for a declaration of this variable that has an initializer.
1848 const VarDecl *ID = 0;
1849 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001850 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001851 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001852 // We have already checked whether this subexpression is an
1853 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001854 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001855 return NoDiag();
1856 else
1857 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1858 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001859
John McCall1f1b3b32010-02-06 01:07:37 +00001860 // It's an ICE whether or not the definition we found is
1861 // out-of-line. See DR 721 and the discussion in Clang PR
1862 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001863
1864 if (Dcl->isCheckingICE()) {
1865 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1866 }
1867
1868 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001869 ICEDiag Result = CheckICE(Init, Ctx);
1870 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001871 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001872 return Result;
1873 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001874 }
1875 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001876 return ICEDiag(2, E->getLocStart());
1877 case Expr::UnaryOperatorClass: {
1878 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001880 case UnaryOperator::PostInc:
1881 case UnaryOperator::PostDec:
1882 case UnaryOperator::PreInc:
1883 case UnaryOperator::PreDec:
1884 case UnaryOperator::AddrOf:
1885 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001886 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001887
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001889 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001893 case UnaryOperator::Real:
1894 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001895 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001896 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001897 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1898 // Evaluate matches the proposed gcc behavior for cases like
1899 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1900 // compliance: we should warn earlier for offsetof expressions with
1901 // array subscripts that aren't ICEs, and if the array subscripts
1902 // are ICEs, the value of the offsetof must be an integer constant.
1903 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001906 case Expr::SizeOfAlignOfExprClass: {
1907 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1908 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1909 return ICEDiag(2, E->getLocStart());
1910 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001912 case Expr::BinaryOperatorClass: {
1913 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001915 case BinaryOperator::PtrMemD:
1916 case BinaryOperator::PtrMemI:
1917 case BinaryOperator::Assign:
1918 case BinaryOperator::MulAssign:
1919 case BinaryOperator::DivAssign:
1920 case BinaryOperator::RemAssign:
1921 case BinaryOperator::AddAssign:
1922 case BinaryOperator::SubAssign:
1923 case BinaryOperator::ShlAssign:
1924 case BinaryOperator::ShrAssign:
1925 case BinaryOperator::AndAssign:
1926 case BinaryOperator::XorAssign:
1927 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001928 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001933 case BinaryOperator::Add:
1934 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001935 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001937 case BinaryOperator::LT:
1938 case BinaryOperator::GT:
1939 case BinaryOperator::LE:
1940 case BinaryOperator::GE:
1941 case BinaryOperator::EQ:
1942 case BinaryOperator::NE:
1943 case BinaryOperator::And:
1944 case BinaryOperator::Xor:
1945 case BinaryOperator::Or:
1946 case BinaryOperator::Comma: {
1947 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1948 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001949 if (Exp->getOpcode() == BinaryOperator::Div ||
1950 Exp->getOpcode() == BinaryOperator::Rem) {
1951 // Evaluate gives an error for undefined Div/Rem, so make sure
1952 // we don't evaluate one.
1953 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1954 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1955 if (REval == 0)
1956 return ICEDiag(1, E->getLocStart());
1957 if (REval.isSigned() && REval.isAllOnesValue()) {
1958 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1959 if (LEval.isMinSignedValue())
1960 return ICEDiag(1, E->getLocStart());
1961 }
1962 }
1963 }
1964 if (Exp->getOpcode() == BinaryOperator::Comma) {
1965 if (Ctx.getLangOptions().C99) {
1966 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1967 // if it isn't evaluated.
1968 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1969 return ICEDiag(1, E->getLocStart());
1970 } else {
1971 // In both C89 and C++, commas in ICEs are illegal.
1972 return ICEDiag(2, E->getLocStart());
1973 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001974 }
1975 if (LHSResult.Val >= RHSResult.Val)
1976 return LHSResult;
1977 return RHSResult;
1978 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001980 case BinaryOperator::LOr: {
1981 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1982 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1983 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1984 // Rare case where the RHS has a comma "side-effect"; we need
1985 // to actually check the condition to see whether the side
1986 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001987 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001988 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001989 return RHSResult;
1990 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001991 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001992
Eli Friedmane28d7192009-02-26 09:29:13 +00001993 if (LHSResult.Val >= RHSResult.Val)
1994 return LHSResult;
1995 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001997 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001999 case Expr::ImplicitCastExprClass:
2000 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00002001 case Expr::CXXFunctionalCastExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00002002 case Expr::CXXNamedCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00002003 case Expr::CXXStaticCastExprClass:
2004 case Expr::CXXReinterpretCastExprClass:
2005 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00002006 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2007 if (SubExpr->getType()->isIntegralType())
2008 return CheckICE(SubExpr, Ctx);
2009 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2010 return NoDiag();
2011 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002013 case Expr::ConditionalOperatorClass: {
2014 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002015 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00002016 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00002017 // expression, and it is fully evaluated. This is an important GNU
2018 // extension. See GCC PR38377 for discussion.
Eli Friedmane28d7192009-02-26 09:29:13 +00002019 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00002020 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002021 Expr::EvalResult EVResult;
2022 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2023 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00002024 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002025 }
2026 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00002027 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002028 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2029 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2030 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2031 if (CondResult.Val == 2)
2032 return CondResult;
2033 if (TrueResult.Val == 2)
2034 return TrueResult;
2035 if (FalseResult.Val == 2)
2036 return FalseResult;
2037 if (CondResult.Val == 1)
2038 return CondResult;
2039 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2040 return NoDiag();
2041 // Rare case where the diagnostics depend on which side is evaluated
2042 // Note that if we get here, CondResult is 0, and at least one of
2043 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00002044 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002045 return FalseResult;
2046 }
2047 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00002048 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002049 case Expr::CXXDefaultArgExprClass:
2050 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002051 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00002052 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002053 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002055
Douglas Gregorf2991242009-09-10 23:31:45 +00002056 // Silence a GCC warning
2057 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002058}
Reid Spencer5f016e22007-07-11 17:01:13 +00002059
Eli Friedmane28d7192009-02-26 09:29:13 +00002060bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2061 SourceLocation *Loc, bool isEvaluated) const {
2062 ICEDiag d = CheckICE(this, Ctx);
2063 if (d.Val != 0) {
2064 if (Loc) *Loc = d.Loc;
2065 return false;
2066 }
2067 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00002068 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002069 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00002070 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2071 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00002072 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 return true;
2074}
2075
Reid Spencer5f016e22007-07-11 17:01:13 +00002076/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2077/// integer constant expression with the value zero, or if this is one that is
2078/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002079bool Expr::isNullPointerConstant(ASTContext &Ctx,
2080 NullPointerConstantValueDependence NPC) const {
2081 if (isValueDependent()) {
2082 switch (NPC) {
2083 case NPC_NeverValueDependent:
2084 assert(false && "Unexpected value dependent expression!");
2085 // If the unthinkable happens, fall through to the safest alternative.
2086
2087 case NPC_ValueDependentIsNull:
2088 return isTypeDependent() || getType()->isIntegralType();
2089
2090 case NPC_ValueDependentIsNotNull:
2091 return false;
2092 }
2093 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002094
Sebastian Redl07779722008-10-31 14:43:28 +00002095 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002096 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002097 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002098 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002099 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002100 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002101 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002102 Pointee->isVoidType() && // to void*
2103 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002104 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002105 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002107 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2108 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002109 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002110 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2111 // Accept ((void*)0) as a null pointer constant, as many other
2112 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002113 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002114 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002115 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002116 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002117 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002118 } else if (isa<GNUNullExpr>(this)) {
2119 // The GNU __null extension is always a null pointer constant.
2120 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002121 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002122
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002123 // C++0x nullptr_t is always a null pointer constant.
2124 if (getType()->isNullPtrType())
2125 return true;
2126
Steve Naroffaa58f002008-01-14 16:10:57 +00002127 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002128 if (!getType()->isIntegerType() ||
2129 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002130 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 // If we have an integer constant expression, we need to *evaluate* it and
2133 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002134 llvm::APSInt Result;
2135 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002136}
Steve Naroff31a45842007-07-28 23:10:27 +00002137
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002138FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002139 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002140
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002141 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2142 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2143 E = ICE->getSubExpr()->IgnoreParens();
2144 else
2145 break;
2146 }
2147
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002148 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002149 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002150 if (Field->isBitField())
2151 return Field;
2152
2153 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2154 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2155 return BinOp->getLHS()->getBitField();
2156
2157 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002158}
2159
Anders Carlsson09380262010-01-31 17:18:49 +00002160bool Expr::refersToVectorElement() const {
2161 const Expr *E = this->IgnoreParens();
2162
2163 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2164 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2165 E = ICE->getSubExpr()->IgnoreParens();
2166 else
2167 break;
2168 }
2169
2170 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2171 return ASE->getBase()->getType()->isVectorType();
2172
2173 if (isa<ExtVectorElementExpr>(E))
2174 return true;
2175
2176 return false;
2177}
2178
Chris Lattner2140e902009-02-16 22:14:05 +00002179/// isArrow - Return true if the base expression is a pointer to vector,
2180/// return false if the base expression is a vector.
2181bool ExtVectorElementExpr::isArrow() const {
2182 return getBase()->getType()->isPointerType();
2183}
2184
Nate Begeman213541a2008-04-18 23:10:10 +00002185unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002186 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002187 return VT->getNumElements();
2188 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002189}
2190
Nate Begeman8a997642008-05-09 06:41:27 +00002191/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002192bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002193 // FIXME: Refactor this code to an accessor on the AST node which returns the
2194 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002195 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002196
2197 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002198 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002199 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Nate Begeman190d6a22009-01-18 02:01:21 +00002201 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002202 if (Comp[0] == 's' || Comp[0] == 'S')
2203 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002204
Daniel Dunbar15027422009-10-17 23:53:04 +00002205 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2206 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002207 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002208
Steve Narofffec0b492007-07-30 03:29:09 +00002209 return false;
2210}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002211
Nate Begeman8a997642008-05-09 06:41:27 +00002212/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002213void ExtVectorElementExpr::getEncodedElementAccess(
2214 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002215 llvm::StringRef Comp = Accessor->getName();
2216 if (Comp[0] == 's' || Comp[0] == 'S')
2217 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002219 bool isHi = Comp == "hi";
2220 bool isLo = Comp == "lo";
2221 bool isEven = Comp == "even";
2222 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Nate Begeman8a997642008-05-09 06:41:27 +00002224 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2225 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Nate Begeman8a997642008-05-09 06:41:27 +00002227 if (isHi)
2228 Index = e + i;
2229 else if (isLo)
2230 Index = i;
2231 else if (isEven)
2232 Index = 2 * i;
2233 else if (isOdd)
2234 Index = 2 * i + 1;
2235 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002236 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002237
Nate Begeman3b8d1162008-05-13 21:03:02 +00002238 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002239 }
Nate Begeman8a997642008-05-09 06:41:27 +00002240}
2241
Steve Naroff68d331a2007-09-27 14:38:14 +00002242// constructor for instance messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002243ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2244 Selector selInfo,
2245 QualType retType, ObjCMethodDecl *mproto,
2246 SourceLocation LBrac, SourceLocation RBrac,
2247 Expr **ArgExprs, unsigned nargs)
Eli Friedman2333f772009-12-30 00:13:48 +00002248 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekea958e572008-05-01 17:26:20 +00002249 MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002250 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002251 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +00002252 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +00002253 if (NumArgs) {
2254 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002255 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2256 }
Steve Naroff563477d2007-09-18 23:55:05 +00002257 LBracloc = LBrac;
2258 RBracloc = RBrac;
2259}
2260
Mike Stump1eb44332009-09-09 15:08:12 +00002261// constructor for class messages.
Steve Naroff68d331a2007-09-27 14:38:14 +00002262// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002263ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002264 SourceLocation clsNameLoc, Selector selInfo,
2265 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002266 SourceLocation LBrac, SourceLocation RBrac,
2267 Expr **ArgExprs, unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002268 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2269 SelName(selInfo), MethodProto(mproto) {
Steve Naroff49f109c2007-11-15 13:05:42 +00002270 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002271 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002272 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff49f109c2007-11-15 13:05:42 +00002273 if (NumArgs) {
2274 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00002275 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2276 }
Steve Naroff563477d2007-09-18 23:55:05 +00002277 LBracloc = LBrac;
2278 RBracloc = RBrac;
2279}
2280
Mike Stump1eb44332009-09-09 15:08:12 +00002281// constructor for class messages.
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002282ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorc2350e52010-03-08 16:40:19 +00002283 SourceLocation clsNameLoc, Selector selInfo,
2284 QualType retType,
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002285 ObjCMethodDecl *mproto, SourceLocation LBrac,
2286 SourceLocation RBrac, Expr **ArgExprs,
2287 unsigned nargs)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002288 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2289 SelName(selInfo), MethodProto(mproto)
2290{
Ted Kremenek4df728e2008-06-24 15:50:53 +00002291 NumArgs = nargs;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002292 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremenek4df728e2008-06-24 15:50:53 +00002293 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2294 if (NumArgs) {
2295 for (unsigned i = 0; i != NumArgs; ++i)
2296 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2297 }
2298 LBracloc = LBrac;
2299 RBracloc = RBrac;
2300}
2301
2302ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2303 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2304 switch (x & Flags) {
2305 default:
2306 assert(false && "Invalid ObjCMessageExpr.");
2307 case IsInstMeth:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002308 return ClassInfo(0, 0, SourceLocation());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002309 case IsClsMethDeclUnknown:
Douglas Gregorc2350e52010-03-08 16:40:19 +00002310 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002311 case IsClsMethDeclKnown: {
2312 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002313 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002314 }
2315 }
2316}
2317
Chris Lattner0389e6b2009-04-26 00:44:05 +00002318void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorc2350e52010-03-08 16:40:19 +00002319 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner0389e6b2009-04-26 00:44:05 +00002320 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorc2350e52010-03-08 16:40:19 +00002321 return;
2322 }
2323
2324 if (CI.Decl == 0)
2325 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner0389e6b2009-04-26 00:44:05 +00002326 else
Douglas Gregorc2350e52010-03-08 16:40:19 +00002327 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2328 ClassNameLoc = CI.Loc;
Chris Lattner0389e6b2009-04-26 00:44:05 +00002329}
2330
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002331void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2332 DestroyChildren(C);
2333 if (SubExprs)
2334 C.Deallocate(SubExprs);
2335 this->~ObjCMessageExpr();
2336 C.Deallocate((void*) this);
2337}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002338
Chris Lattner27437ca2007-10-25 00:29:32 +00002339bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002340 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002341}
2342
Nate Begeman888376a2009-08-12 02:28:50 +00002343void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2344 unsigned NumExprs) {
2345 if (SubExprs) C.Deallocate(SubExprs);
2346
2347 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002348 this->NumExprs = NumExprs;
2349 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002350}
Nate Begeman888376a2009-08-12 02:28:50 +00002351
2352void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2353 DestroyChildren(C);
2354 if (SubExprs) C.Deallocate(SubExprs);
2355 this->~ShuffleVectorExpr();
2356 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002357}
2358
Douglas Gregor42602bb2009-08-07 06:08:38 +00002359void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002360 // Override default behavior of traversing children. If this has a type
2361 // operand and the type is a variable-length array, the child iteration
2362 // will iterate over the size expression. However, this expression belongs
2363 // to the type, not to this, so we don't want to delete it.
2364 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002365 if (isArgumentType()) {
2366 this->~SizeOfAlignOfExpr();
2367 C.Deallocate(this);
2368 }
Sebastian Redl05189992008-11-11 17:56:53 +00002369 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002370 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002371}
2372
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002373//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002374// DesignatedInitExpr
2375//===----------------------------------------------------------------------===//
2376
2377IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2378 assert(Kind == FieldDesignator && "Only valid on a field designator");
2379 if (Field.NameOrField & 0x01)
2380 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2381 else
2382 return getField()->getIdentifier();
2383}
2384
Douglas Gregor319d57f2010-01-06 23:17:19 +00002385DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2386 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002387 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002388 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002389 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002390 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002391 unsigned NumIndexExprs,
2392 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002393 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002394 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002395 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2396 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002397 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002398
2399 // Record the initializer itself.
2400 child_iterator Child = child_begin();
2401 *Child++ = Init;
2402
2403 // Copy the designators and their subexpressions, computing
2404 // value-dependence along the way.
2405 unsigned IndexIdx = 0;
2406 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002407 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002408
2409 if (this->Designators[I].isArrayDesignator()) {
2410 // Compute type- and value-dependence.
2411 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002412 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002413 Index->isTypeDependent() || Index->isValueDependent();
2414
2415 // Copy the index expressions into permanent storage.
2416 *Child++ = IndexExprs[IndexIdx++];
2417 } else if (this->Designators[I].isArrayRangeDesignator()) {
2418 // Compute type- and value-dependence.
2419 Expr *Start = IndexExprs[IndexIdx];
2420 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002421 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002422 Start->isTypeDependent() || Start->isValueDependent() ||
2423 End->isTypeDependent() || End->isValueDependent();
2424
2425 // Copy the start/end expressions into permanent storage.
2426 *Child++ = IndexExprs[IndexIdx++];
2427 *Child++ = IndexExprs[IndexIdx++];
2428 }
2429 }
2430
2431 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002432}
2433
Douglas Gregor05c13a32009-01-22 00:58:24 +00002434DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002435DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002436 unsigned NumDesignators,
2437 Expr **IndexExprs, unsigned NumIndexExprs,
2438 SourceLocation ColonOrEqualLoc,
2439 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002440 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002441 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002442 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002443 ColonOrEqualLoc, UsesColonSyntax,
2444 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002445}
2446
Mike Stump1eb44332009-09-09 15:08:12 +00002447DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002448 unsigned NumIndexExprs) {
2449 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2450 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2451 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2452}
2453
Douglas Gregor319d57f2010-01-06 23:17:19 +00002454void DesignatedInitExpr::setDesignators(ASTContext &C,
2455 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002456 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002457 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002458
Douglas Gregor319d57f2010-01-06 23:17:19 +00002459 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002460 NumDesignators = NumDesigs;
2461 for (unsigned I = 0; I != NumDesigs; ++I)
2462 Designators[I] = Desigs[I];
2463}
2464
Douglas Gregor05c13a32009-01-22 00:58:24 +00002465SourceRange DesignatedInitExpr::getSourceRange() const {
2466 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002467 Designator &First =
2468 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002469 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002470 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002471 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2472 else
2473 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2474 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002475 StartLoc =
2476 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002477 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2478}
2479
Douglas Gregor05c13a32009-01-22 00:58:24 +00002480Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2481 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2482 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2483 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002484 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2485 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2486}
2487
2488Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002489 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002490 "Requires array range designator");
2491 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2492 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002493 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2494 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2495}
2496
2497Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002498 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002499 "Requires array range designator");
2500 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2501 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002502 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2503 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2504}
2505
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002506/// \brief Replaces the designator at index @p Idx with the series
2507/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002508void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002509 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002510 const Designator *Last) {
2511 unsigned NumNewDesignators = Last - First;
2512 if (NumNewDesignators == 0) {
2513 std::copy_backward(Designators + Idx + 1,
2514 Designators + NumDesignators,
2515 Designators + Idx);
2516 --NumNewDesignators;
2517 return;
2518 } else if (NumNewDesignators == 1) {
2519 Designators[Idx] = *First;
2520 return;
2521 }
2522
Mike Stump1eb44332009-09-09 15:08:12 +00002523 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002524 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002525 std::copy(Designators, Designators + Idx, NewDesignators);
2526 std::copy(First, Last, NewDesignators + Idx);
2527 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2528 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002529 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002530 Designators = NewDesignators;
2531 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2532}
2533
Douglas Gregor42602bb2009-08-07 06:08:38 +00002534void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002535 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002536 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002537}
2538
Douglas Gregor319d57f2010-01-06 23:17:19 +00002539void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2540 for (unsigned I = 0; I != NumDesignators; ++I)
2541 Designators[I].~Designator();
2542 C.Deallocate(Designators);
2543 Designators = 0;
2544}
2545
Mike Stump1eb44332009-09-09 15:08:12 +00002546ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002547 Expr **exprs, unsigned nexprs,
2548 SourceLocation rparenloc)
2549: Expr(ParenListExprClass, QualType(),
2550 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002551 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002552 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Nate Begeman2ef13e52009-08-10 23:49:36 +00002554 Exprs = new (C) Stmt*[nexprs];
2555 for (unsigned i = 0; i != nexprs; ++i)
2556 Exprs[i] = exprs[i];
2557}
2558
2559void ParenListExpr::DoDestroy(ASTContext& C) {
2560 DestroyChildren(C);
2561 if (Exprs) C.Deallocate(Exprs);
2562 this->~ParenListExpr();
2563 C.Deallocate(this);
2564}
2565
Douglas Gregor05c13a32009-01-22 00:58:24 +00002566//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002567// ExprIterator.
2568//===----------------------------------------------------------------------===//
2569
2570Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2571Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2572Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2573const Expr* ConstExprIterator::operator[](size_t idx) const {
2574 return cast<Expr>(I[idx]);
2575}
2576const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2577const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2578
2579//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002580// Child Iterators for iterating over subexpressions/substatements
2581//===----------------------------------------------------------------------===//
2582
2583// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002584Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2585Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002586
Steve Naroff7779db42007-11-12 14:29:37 +00002587// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002588Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2589Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002590
Steve Naroffe3e9add2008-06-02 23:03:37 +00002591// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002592Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2593Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002594
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002595// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002596Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2597 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002598}
Mike Stump1eb44332009-09-09 15:08:12 +00002599Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2600 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002601}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002602
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002603// ObjCSuperExpr
2604Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2605Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2606
Steve Narofff242b1b2009-07-24 17:54:45 +00002607// ObjCIsaExpr
2608Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2609Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2610
Chris Lattnerd9f69102008-08-10 01:53:14 +00002611// PredefinedExpr
2612Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2613Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002614
2615// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002616Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2617Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002618
2619// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002620Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002621Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002622
2623// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002624Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2625Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002626
Chris Lattner5d661452007-08-26 03:42:43 +00002627// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002628Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2629Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002630
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002631// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002632Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2633Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002634
2635// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002636Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2637Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002638
2639// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002640Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2641Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002642
Sebastian Redl05189992008-11-11 17:56:53 +00002643// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002644Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002645 // If this is of a type and the type is a VLA type (and not a typedef), the
2646 // size expression of the VLA needs to be treated as an executable expression.
2647 // Why isn't this weirdness documented better in StmtIterator?
2648 if (isArgumentType()) {
2649 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2650 getArgumentType().getTypePtr()))
2651 return child_iterator(T);
2652 return child_iterator();
2653 }
Sebastian Redld4575892008-12-03 23:17:54 +00002654 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002655}
Sebastian Redl05189992008-11-11 17:56:53 +00002656Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2657 if (isArgumentType())
2658 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002659 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002660}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002661
2662// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002663Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002664 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002665}
Ted Kremenek1237c672007-08-24 20:06:47 +00002666Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002667 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002668}
2669
2670// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002671Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002672 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002673}
Ted Kremenek1237c672007-08-24 20:06:47 +00002674Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002675 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002676}
Ted Kremenek1237c672007-08-24 20:06:47 +00002677
2678// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002679Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2680Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002681
Nate Begeman213541a2008-04-18 23:10:10 +00002682// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002683Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2684Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002685
2686// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002687Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2688Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002689
Ted Kremenek1237c672007-08-24 20:06:47 +00002690// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002691Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2692Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002693
2694// BinaryOperator
2695Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002696 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002697}
Ted Kremenek1237c672007-08-24 20:06:47 +00002698Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002699 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002700}
2701
2702// ConditionalOperator
2703Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002704 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002705}
Ted Kremenek1237c672007-08-24 20:06:47 +00002706Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002707 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002708}
2709
2710// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002711Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2712Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002713
Ted Kremenek1237c672007-08-24 20:06:47 +00002714// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002715Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2716Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002717
2718// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002719Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2720 return child_iterator();
2721}
2722
2723Stmt::child_iterator TypesCompatibleExpr::child_end() {
2724 return child_iterator();
2725}
Ted Kremenek1237c672007-08-24 20:06:47 +00002726
2727// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002728Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2729Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002730
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002731// GNUNullExpr
2732Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2733Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2734
Eli Friedmand38617c2008-05-14 19:38:39 +00002735// ShuffleVectorExpr
2736Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002737 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002738}
2739Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002740 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002741}
2742
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002743// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002744Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2745Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002746
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002747// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002748Stmt::child_iterator InitListExpr::child_begin() {
2749 return InitExprs.size() ? &InitExprs[0] : 0;
2750}
2751Stmt::child_iterator InitListExpr::child_end() {
2752 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2753}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002754
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002755// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002756Stmt::child_iterator DesignatedInitExpr::child_begin() {
2757 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2758 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002759 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2760}
2761Stmt::child_iterator DesignatedInitExpr::child_end() {
2762 return child_iterator(&*child_begin() + NumSubExprs);
2763}
2764
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002765// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002766Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2767 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002768}
2769
Mike Stump1eb44332009-09-09 15:08:12 +00002770Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2771 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002772}
2773
Nate Begeman2ef13e52009-08-10 23:49:36 +00002774// ParenListExpr
2775Stmt::child_iterator ParenListExpr::child_begin() {
2776 return &Exprs[0];
2777}
2778Stmt::child_iterator ParenListExpr::child_end() {
2779 return &Exprs[0]+NumExprs;
2780}
2781
Ted Kremenek1237c672007-08-24 20:06:47 +00002782// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002783Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002784 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002785}
2786Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002787 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002788}
Ted Kremenek1237c672007-08-24 20:06:47 +00002789
2790// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002791Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2792Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002793
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002794// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002795Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002796 return child_iterator();
2797}
2798Stmt::child_iterator ObjCSelectorExpr::child_end() {
2799 return child_iterator();
2800}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002801
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002802// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002803Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2804 return child_iterator();
2805}
2806Stmt::child_iterator ObjCProtocolExpr::child_end() {
2807 return child_iterator();
2808}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002809
Steve Naroff563477d2007-09-18 23:55:05 +00002810// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002811Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002812 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroff563477d2007-09-18 23:55:05 +00002813}
2814Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002815 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002816}
2817
Steve Naroff4eb206b2008-09-03 18:15:37 +00002818// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002819Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2820Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002821
Ted Kremenek9da13f92008-09-26 23:24:14 +00002822Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2823Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }