blob: 7c715bd3c3a55784421cdcfe246aaa107508d000 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattner4ebae652010-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
Chris Lattner0eedafe2006-08-24 04:56:27 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCall6b51f282009-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 Gregored6c7442009-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 Gregor5fcb51c2010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
164 }
Douglas Gregored6c7442009-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 Gregor4bd90e52009-10-23 18:54:35 +0000171DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
172 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000173 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000174 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000175 QualType T)
176 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000177 DecoratedD(D,
178 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000179 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-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 McCall6b51f282009-11-23 01:53:49 +0000187 if (TemplateArgs)
188 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000189
190 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000191}
192
193DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
194 NestedNameSpecifier *Qualifier,
195 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000196 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000197 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000198 QualType T,
199 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000200 std::size_t Size = sizeof(DeclRefExpr);
201 if (Qualifier != 0)
202 Size += sizeof(NameQualifier);
203
John McCall6b51f282009-11-23 01:53:49 +0000204 if (TemplateArgs)
205 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregor4bd90e52009-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 Gregored6c7442009-11-23 11:41:28 +0000209 TemplateArgs, T);
Douglas Gregor4bd90e52009-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 Carlsson2fb08242009-09-08 18:24:21 +0000223// FIXME: Maybe this should use DeclPrinter with a special "print predefined
224// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000225std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
226 ASTContext &Context = CurrentDecl->getASTContext();
227
Anders Carlsson2fb08242009-09-08 18:24:21 +0000228 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000229 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-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 Carlsson5bd8d192010-02-11 18:20:28 +0000236 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000237 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000238 if (MD->isStatic())
239 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000240 }
241
242 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000243
244 std::string Proto = FD->getQualifiedNameAsString(Policy);
245
John McCall9dd450b2009-09-21 23:43:11 +0000246 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-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 Weinig4e83bd22009-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 Weinigd060ed42009-12-06 23:55:13 +0000276 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
277 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-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 Kremenek361ffd92010-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())
293 Out << ID->getNameAsString();
294
Anders Carlsson2fb08242009-09-08 18:24:21 +0000295 if (const ObjCCategoryImplDecl *CID =
296 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) {
297 Out << '(';
298 Out << CID->getNameAsString();
299 Out << ')';
300 }
301 Out << ' ';
302 Out << MD->getSelector().getAsString();
303 Out << ']';
304
305 Out.flush();
306 return Name.str().str();
307 }
308 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
309 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
310 return "top level";
311 }
312 return "";
313}
314
Chris Lattnera0173132008-06-07 22:13:43 +0000315/// getValueAsApproximateDouble - This returns the value as an inaccurate
316/// double. Note that this may cause loss of precision, but is useful for
317/// debugging dumps, etc.
318double FloatingLiteral::getValueAsApproximateDouble() const {
319 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000320 bool ignored;
321 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
322 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000323 return V.convertToDouble();
324}
325
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000326StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
327 unsigned ByteLength, bool Wide,
328 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000329 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000330 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000331 // Allocate enough space for the StringLiteral plus an array of locations for
332 // any concatenated string tokens.
333 void *Mem = C.Allocate(sizeof(StringLiteral)+
334 sizeof(SourceLocation)*(NumStrs-1),
335 llvm::alignof<StringLiteral>());
336 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000337
Steve Naroffdf7855b2007-02-21 23:46:25 +0000338 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000339 char *AStrData = new (C, 1) char[ByteLength];
340 memcpy(AStrData, StrData, ByteLength);
341 SL->StrData = AStrData;
342 SL->ByteLength = ByteLength;
343 SL->IsWide = Wide;
344 SL->TokLocs[0] = Loc[0];
345 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000346
Chris Lattner630970d2009-02-18 05:49:11 +0000347 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000348 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
349 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000350}
351
Douglas Gregor958dfc92009-04-15 16:35:07 +0000352StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
353 void *Mem = C.Allocate(sizeof(StringLiteral)+
354 sizeof(SourceLocation)*(NumStrs-1),
355 llvm::alignof<StringLiteral>());
356 StringLiteral *SL = new (Mem) StringLiteral(QualType());
357 SL->StrData = 0;
358 SL->ByteLength = 0;
359 SL->NumConcatenated = NumStrs;
360 return SL;
361}
362
Douglas Gregore26a2852009-08-07 06:08:38 +0000363void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000364 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000365 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000366}
367
Daniel Dunbar36217882009-09-22 03:27:33 +0000368void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000369 if (StrData)
370 C.Deallocate(const_cast<char*>(StrData));
371
Daniel Dunbar36217882009-09-22 03:27:33 +0000372 char *AStrData = new (C, 1) char[Str.size()];
373 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000374 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000375 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000376}
377
Chris Lattner1b926492006-08-23 06:42:10 +0000378/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
379/// corresponds to, e.g. "sizeof" or "[pre]++".
380const char *UnaryOperator::getOpcodeStr(Opcode Op) {
381 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000382 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000383 case PostInc: return "++";
384 case PostDec: return "--";
385 case PreInc: return "++";
386 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000387 case AddrOf: return "&";
388 case Deref: return "*";
389 case Plus: return "+";
390 case Minus: return "-";
391 case Not: return "~";
392 case LNot: return "!";
393 case Real: return "__real";
394 case Imag: return "__imag";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000395 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000396 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000397 }
398}
399
Mike Stump11289f42009-09-09 15:08:12 +0000400UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000401UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
402 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000403 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-03-22 00:10:22 +0000404 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
405 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
406 case OO_Amp: return AddrOf;
407 case OO_Star: return Deref;
408 case OO_Plus: return Plus;
409 case OO_Minus: return Minus;
410 case OO_Tilde: return Not;
411 case OO_Exclaim: return LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000412 }
413}
414
415OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
416 switch (Opc) {
417 case PostInc: case PreInc: return OO_PlusPlus;
418 case PostDec: case PreDec: return OO_MinusMinus;
419 case AddrOf: return OO_Amp;
420 case Deref: return OO_Star;
421 case Plus: return OO_Plus;
422 case Minus: return OO_Minus;
423 case Not: return OO_Tilde;
424 case LNot: return OO_Exclaim;
425 default: return OO_None;
426 }
427}
428
429
Chris Lattner0eedafe2006-08-24 04:56:27 +0000430//===----------------------------------------------------------------------===//
431// Postfix Operators.
432//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000433
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000434CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000435 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000436 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000437 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000438 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000439 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000440
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000441 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-11-14 16:09:21 +0000442 SubExprs[FN] = fn;
443 for (unsigned i = 0; i != numargs; ++i)
444 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000445
Douglas Gregor993603d2008-11-14 16:09:21 +0000446 RParenLoc = rparenloc;
447}
Nate Begeman1e36a852008-01-17 17:46:27 +0000448
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000449CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
450 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000451 : Expr(CallExprClass, t,
452 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000453 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000454 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000455
456 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000457 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000458 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000459 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000460
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000461 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000462}
463
Mike Stump11289f42009-09-09 15:08:12 +0000464CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
465 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000466 SubExprs = new (C) Stmt*[1];
467}
468
Douglas Gregore26a2852009-08-07 06:08:38 +0000469void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000470 DestroyChildren(C);
471 if (SubExprs) C.Deallocate(SubExprs);
472 this->~CallExpr();
473 C.Deallocate(this);
474}
475
Nuno Lopes518e3702009-12-20 23:11:08 +0000476Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000477 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000478 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000479 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000480 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
481 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000482
483 return 0;
484}
485
Nuno Lopes518e3702009-12-20 23:11:08 +0000486FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000487 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000488}
489
Chris Lattnere4407ed2007-12-28 05:25:02 +0000490/// setNumArgs - This changes the number of arguments present in this call.
491/// Any orphaned expressions are deleted by this, and any new operands are set
492/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000493void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000494 // No change, just return.
495 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chris Lattnere4407ed2007-12-28 05:25:02 +0000497 // If shrinking # arguments, just delete the extras and forgot them.
498 if (NumArgs < getNumArgs()) {
499 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek5a201952009-02-07 01:47:29 +0000500 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000501 this->NumArgs = NumArgs;
502 return;
503 }
504
505 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbarec5ae3d2009-07-28 06:29:46 +0000506 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000507 // Copy over args.
508 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
509 NewSubExprs[i] = SubExprs[i];
510 // Null out new args.
511 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
512 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Douglas Gregorba6e5572009-04-17 21:46:47 +0000514 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000515 SubExprs = NewSubExprs;
516 this->NumArgs = NumArgs;
517}
518
Chris Lattner01ff98a2008-10-06 05:00:53 +0000519/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
520/// not, return 0.
Douglas Gregore711f702009-02-14 18:57:46 +0000521unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000522 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000523 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000524 // ImplicitCastExpr.
525 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
526 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000527 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000528
Steve Narofff6e3b3292008-01-31 01:07:12 +0000529 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
530 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000531 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000532
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000533 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
534 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000535 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000537 if (!FDecl->getIdentifier())
538 return 0;
539
Douglas Gregor15fc9562009-09-12 00:22:50 +0000540 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000541}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000542
Anders Carlsson00a27592009-05-26 04:57:27 +0000543QualType CallExpr::getCallReturnType() const {
544 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000545 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000546 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000547 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000548 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000549
John McCall9dd450b2009-09-21 23:43:11 +0000550 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000551 return FnType->getResultType();
552}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000553
Mike Stump11289f42009-09-09 15:08:12 +0000554MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
555 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000556 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000557 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000558 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000559 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000560 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000561 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000562 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000563
John McCalla8ae2222010-04-06 21:38:20 +0000564 bool hasQualOrFound = (qual != 0 ||
565 founddecl.getDecl() != memberdecl ||
566 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000567 if (hasQualOrFound)
568 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000569
John McCall6b51f282009-11-23 01:53:49 +0000570 if (targs)
571 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000572
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000573 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000574 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
575
576 if (hasQualOrFound) {
577 if (qual && qual->isDependent()) {
578 E->setValueDependent(true);
579 E->setTypeDependent(true);
580 }
581 E->HasQualifierOrFoundDecl = true;
582
583 MemberNameQualifier *NQ = E->getMemberQualifier();
584 NQ->NNS = qual;
585 NQ->Range = qualrange;
586 NQ->FoundDecl = founddecl;
587 }
588
589 if (targs) {
590 E->HasExplicitTemplateArgumentList = true;
591 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
592 }
593
594 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000595}
596
Anders Carlsson496335e2009-09-03 00:59:21 +0000597const char *CastExpr::getCastKindName() const {
598 switch (getCastKind()) {
599 case CastExpr::CK_Unknown:
600 return "Unknown";
601 case CastExpr::CK_BitCast:
602 return "BitCast";
603 case CastExpr::CK_NoOp:
604 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000605 case CastExpr::CK_BaseToDerived:
606 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000607 case CastExpr::CK_DerivedToBase:
608 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000609 case CastExpr::CK_UncheckedDerivedToBase:
610 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000611 case CastExpr::CK_Dynamic:
612 return "Dynamic";
613 case CastExpr::CK_ToUnion:
614 return "ToUnion";
615 case CastExpr::CK_ArrayToPointerDecay:
616 return "ArrayToPointerDecay";
617 case CastExpr::CK_FunctionToPointerDecay:
618 return "FunctionToPointerDecay";
619 case CastExpr::CK_NullToMemberPointer:
620 return "NullToMemberPointer";
621 case CastExpr::CK_BaseToDerivedMemberPointer:
622 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000623 case CastExpr::CK_DerivedToBaseMemberPointer:
624 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000625 case CastExpr::CK_UserDefinedConversion:
626 return "UserDefinedConversion";
627 case CastExpr::CK_ConstructorConversion:
628 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000629 case CastExpr::CK_IntegralToPointer:
630 return "IntegralToPointer";
631 case CastExpr::CK_PointerToIntegral:
632 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000633 case CastExpr::CK_ToVoid:
634 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000635 case CastExpr::CK_VectorSplat:
636 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000637 case CastExpr::CK_IntegralCast:
638 return "IntegralCast";
639 case CastExpr::CK_IntegralToFloating:
640 return "IntegralToFloating";
641 case CastExpr::CK_FloatingToIntegral:
642 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000643 case CastExpr::CK_FloatingCast:
644 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000645 case CastExpr::CK_MemberPointerToBoolean:
646 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000647 case CastExpr::CK_AnyPointerToObjCPointerCast:
648 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000649 case CastExpr::CK_AnyPointerToBlockPointerCast:
650 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000651 }
Mike Stump11289f42009-09-09 15:08:12 +0000652
Anders Carlsson496335e2009-09-03 00:59:21 +0000653 assert(0 && "Unhandled cast kind!");
654 return 0;
655}
656
Douglas Gregord196a582009-12-14 19:27:10 +0000657Expr *CastExpr::getSubExprAsWritten() {
658 Expr *SubExpr = 0;
659 CastExpr *E = this;
660 do {
661 SubExpr = E->getSubExpr();
662
663 // Skip any temporary bindings; they're implicit.
664 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
665 SubExpr = Binder->getSubExpr();
666
667 // Conversions by constructor and conversion functions have a
668 // subexpression describing the call; strip it off.
669 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
670 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
671 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
672 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
673
674 // If the subexpression we're left with is an implicit cast, look
675 // through that, too.
676 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
677
678 return SubExpr;
679}
680
Chris Lattner1b926492006-08-23 06:42:10 +0000681/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
682/// corresponds to, e.g. "<<=".
683const char *BinaryOperator::getOpcodeStr(Opcode Op) {
684 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000685 case PtrMemD: return ".*";
686 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000687 case Mul: return "*";
688 case Div: return "/";
689 case Rem: return "%";
690 case Add: return "+";
691 case Sub: return "-";
692 case Shl: return "<<";
693 case Shr: return ">>";
694 case LT: return "<";
695 case GT: return ">";
696 case LE: return "<=";
697 case GE: return ">=";
698 case EQ: return "==";
699 case NE: return "!=";
700 case And: return "&";
701 case Xor: return "^";
702 case Or: return "|";
703 case LAnd: return "&&";
704 case LOr: return "||";
705 case Assign: return "=";
706 case MulAssign: return "*=";
707 case DivAssign: return "/=";
708 case RemAssign: return "%=";
709 case AddAssign: return "+=";
710 case SubAssign: return "-=";
711 case ShlAssign: return "<<=";
712 case ShrAssign: return ">>=";
713 case AndAssign: return "&=";
714 case XorAssign: return "^=";
715 case OrAssign: return "|=";
716 case Comma: return ",";
717 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000718
719 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000720}
Steve Naroff47500512007-04-19 23:00:49 +0000721
Mike Stump11289f42009-09-09 15:08:12 +0000722BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000723BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
724 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000725 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000726 case OO_Plus: return Add;
727 case OO_Minus: return Sub;
728 case OO_Star: return Mul;
729 case OO_Slash: return Div;
730 case OO_Percent: return Rem;
731 case OO_Caret: return Xor;
732 case OO_Amp: return And;
733 case OO_Pipe: return Or;
734 case OO_Equal: return Assign;
735 case OO_Less: return LT;
736 case OO_Greater: return GT;
737 case OO_PlusEqual: return AddAssign;
738 case OO_MinusEqual: return SubAssign;
739 case OO_StarEqual: return MulAssign;
740 case OO_SlashEqual: return DivAssign;
741 case OO_PercentEqual: return RemAssign;
742 case OO_CaretEqual: return XorAssign;
743 case OO_AmpEqual: return AndAssign;
744 case OO_PipeEqual: return OrAssign;
745 case OO_LessLess: return Shl;
746 case OO_GreaterGreater: return Shr;
747 case OO_LessLessEqual: return ShlAssign;
748 case OO_GreaterGreaterEqual: return ShrAssign;
749 case OO_EqualEqual: return EQ;
750 case OO_ExclaimEqual: return NE;
751 case OO_LessEqual: return LE;
752 case OO_GreaterEqual: return GE;
753 case OO_AmpAmp: return LAnd;
754 case OO_PipePipe: return LOr;
755 case OO_Comma: return Comma;
756 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000757 }
758}
759
760OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
761 static const OverloadedOperatorKind OverOps[] = {
762 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
763 OO_Star, OO_Slash, OO_Percent,
764 OO_Plus, OO_Minus,
765 OO_LessLess, OO_GreaterGreater,
766 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
767 OO_EqualEqual, OO_ExclaimEqual,
768 OO_Amp,
769 OO_Caret,
770 OO_Pipe,
771 OO_AmpAmp,
772 OO_PipePipe,
773 OO_Equal, OO_StarEqual,
774 OO_SlashEqual, OO_PercentEqual,
775 OO_PlusEqual, OO_MinusEqual,
776 OO_LessLessEqual, OO_GreaterGreaterEqual,
777 OO_AmpEqual, OO_CaretEqual,
778 OO_PipeEqual,
779 OO_Comma
780 };
781 return OverOps[Opc];
782}
783
Ted Kremenekac034612010-04-13 23:39:13 +0000784InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000785 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000786 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000787 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000788 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000789 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenek013041e2010-02-19 01:50:18 +0000790 UnionFieldInit(0), HadArrayRangeDesignator(false)
791{
792 for (unsigned I = 0; I != numInits; ++I) {
793 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000794 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000795 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000796 ValueDependent = true;
797 }
Ted Kremenek013041e2010-02-19 01:50:18 +0000798
Ted Kremenekac034612010-04-13 23:39:13 +0000799 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000800}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000801
Ted Kremenekac034612010-04-13 23:39:13 +0000802void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000803 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000804 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000805}
806
Ted Kremenekac034612010-04-13 23:39:13 +0000807void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000808 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
809 Idx < LastIdx; ++Idx)
Ted Kremenekac034612010-04-13 23:39:13 +0000810 InitExprs[Idx]->Destroy(C);
811 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000812}
813
Ted Kremenekac034612010-04-13 23:39:13 +0000814Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000815 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000816 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000817 InitExprs.back() = expr;
818 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000819 }
Mike Stump11289f42009-09-09 15:08:12 +0000820
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000821 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
822 InitExprs[Init] = expr;
823 return Result;
824}
825
Steve Naroff991e99d2008-09-04 15:31:07 +0000826/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000827///
828const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000829 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000830 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000831}
832
Mike Stump11289f42009-09-09 15:08:12 +0000833SourceLocation BlockExpr::getCaretLocation() const {
834 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000835}
Mike Stump11289f42009-09-09 15:08:12 +0000836const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000837 return TheBlock->getBody();
838}
Mike Stump11289f42009-09-09 15:08:12 +0000839Stmt *BlockExpr::getBody() {
840 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000841}
Steve Naroff415d3d52008-10-08 17:01:13 +0000842
843
Chris Lattner1ec5f562007-06-27 05:38:08 +0000844//===----------------------------------------------------------------------===//
845// Generic Expression Routines
846//===----------------------------------------------------------------------===//
847
Chris Lattner237f2752009-02-14 07:37:35 +0000848/// isUnusedResultAWarning - Return true if this immediate expression should
849/// be warned about if the result is unused. If so, fill in Loc and Ranges
850/// with location to warn on and the source range[s] to report with the
851/// warning.
852bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000853 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000854 // Don't warn if the expr is type dependent. The type could end up
855 // instantiating to void.
856 if (isTypeDependent())
857 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000858
Chris Lattner1ec5f562007-06-27 05:38:08 +0000859 switch (getStmtClass()) {
860 default:
John McCallc493a732010-03-12 07:11:26 +0000861 if (getType()->isVoidType())
862 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000863 Loc = getExprLoc();
864 R1 = getSourceRange();
865 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000866 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000867 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000868 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000869 case UnaryOperatorClass: {
870 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner1ec5f562007-06-27 05:38:08 +0000872 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000873 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000874 case UnaryOperator::PostInc:
875 case UnaryOperator::PostDec:
876 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000877 case UnaryOperator::PreDec: // ++/--
878 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000879 case UnaryOperator::Deref:
880 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000881 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000882 return false;
883 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000884 case UnaryOperator::Real:
885 case UnaryOperator::Imag:
886 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000887 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
888 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000889 return false;
890 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000891 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000892 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000893 }
Chris Lattner237f2752009-02-14 07:37:35 +0000894 Loc = UO->getOperatorLoc();
895 R1 = UO->getSubExpr()->getSourceRange();
896 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000897 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000898 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000899 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000900 switch (BO->getOpcode()) {
901 default:
902 break;
903 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
904 case BinaryOperator::Comma:
905 // ((foo = <blah>), 0) is an idiom for hiding the result (and
906 // lvalue-ness) of an assignment written in a macro.
907 if (IntegerLiteral *IE =
908 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
909 if (IE->getValue() == 0)
910 return false;
911 case BinaryOperator::LAnd:
912 case BinaryOperator::LOr:
913 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
914 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000915 }
Chris Lattner237f2752009-02-14 07:37:35 +0000916 if (BO->isAssignmentOp())
917 return false;
918 Loc = BO->getOperatorLoc();
919 R1 = BO->getLHS()->getSourceRange();
920 R2 = BO->getRHS()->getSourceRange();
921 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000922 }
Chris Lattner86928112007-08-25 02:00:02 +0000923 case CompoundAssignOperatorClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000924 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000925
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000926 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000927 // The condition must be evaluated, but if either the LHS or RHS is a
928 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000929 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000930 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000931 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000932 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000933 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000934 }
935
Chris Lattnera44d1162007-06-27 05:58:59 +0000936 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000937 // If the base pointer or element is to a volatile pointer/field, accessing
938 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000939 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000940 return false;
941 Loc = cast<MemberExpr>(this)->getMemberLoc();
942 R1 = SourceRange(Loc, Loc);
943 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
944 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner1ec5f562007-06-27 05:38:08 +0000946 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000947 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +0000948 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000949 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000950 return false;
951 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
952 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
953 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
954 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +0000955
Chris Lattner1ec5f562007-06-27 05:38:08 +0000956 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +0000957 case CXXOperatorCallExprClass:
958 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000959 // If this is a direct call, get the callee.
960 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +0000961 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000962 // If the callee has attribute pure, const, or warn_unused_result, warn
963 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +0000964 //
965 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
966 // updated to match for QoI.
967 if (FD->getAttr<WarnUnusedResultAttr>() ||
968 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
969 Loc = CE->getCallee()->getLocStart();
970 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +0000971
Chris Lattner1a6babf2009-10-13 04:53:48 +0000972 if (unsigned NumArgs = CE->getNumArgs())
973 R2 = SourceRange(CE->getArg(0)->getLocStart(),
974 CE->getArg(NumArgs-1)->getLocEnd());
975 return true;
976 }
Chris Lattner237f2752009-02-14 07:37:35 +0000977 }
978 return false;
979 }
Anders Carlsson6aa50392009-11-17 17:11:23 +0000980
981 case CXXTemporaryObjectExprClass:
982 case CXXConstructExprClass:
983 return false;
984
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000985 case ObjCMessageExprClass: {
986 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
987 const ObjCMethodDecl *MD = ME->getMethodDecl();
988 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
989 Loc = getExprLoc();
990 return true;
991 }
Chris Lattner237f2752009-02-14 07:37:35 +0000992 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +0000993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000995 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000996#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000997 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000998 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +0000999 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001000 Loc = Ref->getLocation();
1001 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1002 if (Ref->getBase())
1003 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001004#else
1005 Loc = getExprLoc();
1006 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001007#endif
1008 return true;
1009 }
Chris Lattner944d3062008-07-26 19:51:01 +00001010 case StmtExprClass: {
1011 // Statement exprs don't logically have side effects themselves, but are
1012 // sometimes used in macros in ways that give them a type that is unused.
1013 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1014 // however, if the result of the stmt expr is dead, we don't want to emit a
1015 // warning.
1016 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1017 if (!CS->body_empty())
1018 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001019 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001020
John McCallc493a732010-03-12 07:11:26 +00001021 if (getType()->isVoidType())
1022 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001023 Loc = cast<StmtExpr>(this)->getLParenLoc();
1024 R1 = getSourceRange();
1025 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001026 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001027 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001028 // If this is an explicit cast to void, allow it. People do this when they
1029 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001030 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001031 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001032 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1033 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1034 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001035 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001036 if (getType()->isVoidType())
1037 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001038 const CastExpr *CE = cast<CastExpr>(this);
1039
1040 // If this is a cast to void or a constructor conversion, check the operand.
1041 // Otherwise, the result of the cast is unused.
1042 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1043 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001044 return (cast<CastExpr>(this)->getSubExpr()
1045 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001046 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1047 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1048 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001051 case ImplicitCastExprClass:
1052 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001053 return (cast<ImplicitCastExpr>(this)
1054 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001055
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001056 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001057 return (cast<CXXDefaultArgExpr>(this)
1058 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001059
1060 case CXXNewExprClass:
1061 // FIXME: In theory, there might be new expressions that don't have side
1062 // effects (e.g. a placement new with an uninitialized POD).
1063 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001064 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001065 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001066 return (cast<CXXBindTemporaryExpr>(this)
1067 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001068 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001069 return (cast<CXXExprWithTemporaries>(this)
1070 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001071 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001072}
1073
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001074/// DeclCanBeLvalue - Determine whether the given declaration can be
1075/// an lvalue. This is a helper routine for isLvalue.
1076static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001077 // C++ [temp.param]p6:
1078 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001079 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001080 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1081 return NTTParm->getType()->isReferenceType();
1082
Douglas Gregor91f84212008-12-11 16:49:14 +00001083 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001084 // C++ 3.10p2: An lvalue refers to an object or function.
1085 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001086 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001087}
1088
Steve Naroff475cca02007-05-14 17:19:29 +00001089/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1090/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001091/// - name, where name must be a variable
1092/// - e[i]
1093/// - (e), where e must be an lvalue
1094/// - e.name, where e must be an lvalue
1095/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001096/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001097/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001098/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001099/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001100///
Chris Lattner67315442008-07-26 21:30:36 +00001101Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001102 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1103
1104 isLvalueResult Res = isLvalueInternal(Ctx);
1105 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1106 return Res;
1107
Douglas Gregor9a657932008-10-21 23:43:52 +00001108 // first, check the type (C99 6.3.2.1). Expressions with function
1109 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001110 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001111 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001112
Steve Naroff1018ea32008-02-10 01:39:04 +00001113 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001114 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001115 return LV_IncompleteVoidType;
1116
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001117 return LV_Valid;
1118}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001119
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001120// Check whether the expression can be sanely treated like an l-value
1121Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001122 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001123 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001124 case StringLiteralClass: // C99 6.5.1p4
1125 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001126 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001127 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001128 // For vectors, make sure base is an lvalue (i.e. not a function call).
1129 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001130 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001131 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001132 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001133 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1134 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001135 return LV_Valid;
1136 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001137 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001138 case BlockDeclRefExprClass: {
1139 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001140 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001141 return LV_Valid;
1142 break;
1143 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001144 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001145 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001146 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1147 NamedDecl *Member = m->getMemberDecl();
1148 // C++ [expr.ref]p4:
1149 // If E2 is declared to have type "reference to T", then E1.E2
1150 // is an lvalue.
1151 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1152 if (Value->getType()->isReferenceType())
1153 return LV_Valid;
1154
1155 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001156 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001157 return LV_Valid;
1158
1159 // -- If E2 is a non-static data member [...]. If E1 is an
1160 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001161 if (isa<FieldDecl>(Member)) {
1162 if (m->isArrow())
1163 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001164 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001165 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001166
1167 // -- If it refers to a static member function [...], then
1168 // E1.E2 is an lvalue.
1169 // -- Otherwise, if E1.E2 refers to a non-static member
1170 // function [...], then E1.E2 is not an lvalue.
1171 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1172 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1173
1174 // -- If E2 is a member enumerator [...], the expression E1.E2
1175 // is not an lvalue.
1176 if (isa<EnumConstantDecl>(Member))
1177 return LV_InvalidExpression;
1178
1179 // Not an lvalue.
1180 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001181 }
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001182
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001183 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001184 if (m->isArrow())
1185 return LV_Valid;
1186 Expr *BaseExp = m->getBase();
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001187 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1188 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001189 return LV_SubObjCPropertySetting;
1190 return
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001191 BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001192 }
Chris Lattner595db862007-10-30 22:53:42 +00001193 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001194 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001195 return LV_Valid; // C99 6.5.3p4
1196
1197 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001198 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1199 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001200 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001201
1202 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1203 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1204 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1205 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001206 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001207 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001208 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1209 return LV_Valid;
1210
1211 // If this is a conversion to a class temporary, make a note of
1212 // that.
1213 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1214 return LV_ClassTemporary;
1215
1216 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001217 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001218 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001219 case BinaryOperatorClass:
1220 case CompoundAssignOperatorClass: {
1221 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001222
1223 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1224 BinOp->getOpcode() == BinaryOperator::Comma)
1225 return BinOp->getRHS()->isLvalue(Ctx);
1226
Sebastian Redl112a97662009-02-07 00:15:38 +00001227 // C++ [expr.mptr.oper]p6
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001228 // The result of a .* expression is an lvalue only if its first operand is
1229 // an lvalue and its second operand is a pointer to data member.
1230 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001231 !BinOp->getType()->isFunctionType())
1232 return BinOp->getLHS()->isLvalue(Ctx);
1233
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001234 // The result of an ->* expression is an lvalue only if its second operand
1235 // is a pointer to data member.
1236 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1237 !BinOp->getType()->isFunctionType()) {
1238 QualType Ty = BinOp->getRHS()->getType();
1239 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1240 return LV_Valid;
1241 }
1242
Douglas Gregor58e008d2008-11-13 20:12:29 +00001243 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001244 return LV_InvalidExpression;
1245
Douglas Gregor58e008d2008-11-13 20:12:29 +00001246 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001247 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001248 // The result of an assignment operation [...] is an lvalue.
1249 return LV_Valid;
1250
1251
1252 // C99 6.5.16:
1253 // An assignment expression [...] is not an lvalue.
1254 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001255 }
Mike Stump11289f42009-09-09 15:08:12 +00001256 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001257 case CXXOperatorCallExprClass:
1258 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001259 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001260 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001261 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001262 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1263 if (ReturnType->isLValueReferenceType())
1264 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001265
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001266 // If the function is returning a class temporary, make a note of
1267 // that.
1268 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1269 return LV_ClassTemporary;
1270
Douglas Gregor6b754842008-10-28 00:22:11 +00001271 break;
1272 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001273 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001274 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001275 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001276 case ChooseExprClass:
1277 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001278 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001279 case ExtVectorElementExprClass:
1280 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001281 return LV_DuplicateVectorComponents;
1282 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001283 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1284 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001285 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1286 return LV_Valid;
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001287 case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001288 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001289 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001290 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001291 case UnresolvedLookupExprClass:
1292 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001293 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001294 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001295 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001296 case CXXFunctionalCastExprClass:
1297 case CXXStaticCastExprClass:
1298 case CXXDynamicCastExprClass:
1299 case CXXReinterpretCastExprClass:
1300 case CXXConstCastExprClass:
1301 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001302 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001303 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1304 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001305 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1306 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001307 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001308
1309 // If this is a conversion to a class temporary, make a note of
1310 // that.
1311 if (Ctx.getLangOptions().CPlusPlus &&
1312 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1313 return LV_ClassTemporary;
1314
Douglas Gregor6b754842008-10-28 00:22:11 +00001315 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001316 case CXXTypeidExprClass:
1317 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1318 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001319 case CXXBindTemporaryExprClass:
1320 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1321 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001322 case CXXBindReferenceExprClass:
1323 // Something that's bound to a reference is always an lvalue.
1324 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001325 case ConditionalOperatorClass: {
1326 // Complicated handling is only for C++.
1327 if (!Ctx.getLangOptions().CPlusPlus)
1328 return LV_InvalidExpression;
1329
1330 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1331 // everywhere there's an object converted to an rvalue. Also, any other
1332 // casts should be wrapped by ImplicitCastExprs. There's just the special
1333 // case involving throws to work out.
1334 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001335 Expr *True = Cond->getTrueExpr();
1336 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001337 // C++0x 5.16p2
1338 // If either the second or the third operand has type (cv) void, [...]
1339 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001340 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001341 return LV_InvalidExpression;
1342
1343 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001344 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001345 return LV_InvalidExpression;
1346
1347 // That's it.
1348 return LV_Valid;
1349 }
1350
Douglas Gregor5103eff2009-12-19 07:07:47 +00001351 case Expr::CXXExprWithTemporariesClass:
1352 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1353
1354 case Expr::ObjCMessageExprClass:
1355 if (const ObjCMethodDecl *Method
1356 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1357 if (Method->getResultType()->isLValueReferenceType())
1358 return LV_Valid;
1359 break;
1360
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001361 case Expr::CXXConstructExprClass:
1362 case Expr::CXXTemporaryObjectExprClass:
1363 case Expr::CXXZeroInitValueExprClass:
1364 return LV_ClassTemporary;
1365
Steve Naroff9358c712007-05-27 23:58:33 +00001366 default:
1367 break;
Steve Naroff47500512007-04-19 23:00:49 +00001368 }
Steve Naroff9358c712007-05-27 23:58:33 +00001369 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001370}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001371
Steve Naroff475cca02007-05-14 17:19:29 +00001372/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1373/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001374/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001375/// recursively, any member or element of all contained aggregates or unions)
1376/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001377Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001378Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001379 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001380
Steve Naroff9358c712007-05-27 23:58:33 +00001381 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001382 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001383 // C++ 3.10p11: Functions cannot be modified, but pointers to
1384 // functions can be modifiable.
1385 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1386 return MLV_NotObjectType;
1387 break;
1388
Chris Lattner1ec5f562007-06-27 05:38:08 +00001389 case LV_NotObjectType: return MLV_NotObjectType;
1390 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001391 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001392 case LV_InvalidExpression:
1393 // If the top level is a C-style cast, and the subexpression is a valid
1394 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1395 // GCC extension. We don't support it, but we want to produce good
1396 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001397 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1398 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1399 if (Loc)
1400 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001401 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001402 }
1403 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001404 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001405 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001406 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001407 case LV_ClassTemporary:
1408 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001409 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001410
1411 // The following is illegal:
1412 // void takeclosure(void (^C)(void));
1413 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1414 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001415 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001416 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1417 return MLV_NotBlockQualified;
1418 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001419
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001420 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001421 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001422 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1423 if (Expr->getSetterMethod() == 0)
1424 return MLV_NoSetterProperty;
1425 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001426
Chris Lattner7adf0762008-08-04 07:31:14 +00001427 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001428
Chris Lattner7adf0762008-08-04 07:31:14 +00001429 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001430 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001431 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001432 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001433 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001434 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001435
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001436 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001437 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001438 return MLV_ConstQualified;
1439 }
Mike Stump11289f42009-09-09 15:08:12 +00001440
Mike Stump11289f42009-09-09 15:08:12 +00001441 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001442}
1443
Fariborz Jahanian07735332009-02-22 18:40:18 +00001444/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001445/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001446bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001447 switch (getStmtClass()) {
1448 default:
1449 return false;
1450 case ObjCIvarRefExprClass:
1451 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001452 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001453 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001454 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001455 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001456 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001457 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001458 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001459 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001460 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001461 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001462 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1463 if (VD->hasGlobalStorage())
1464 return true;
1465 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001466 // dereferencing to a pointer is always a gc'able candidate,
1467 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001468 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001469 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001470 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001471 return false;
1472 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001473 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001474 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001475 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001476 }
1477 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001478 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001479 }
1480}
Ted Kremenekfff70962008-01-17 16:57:34 +00001481Expr* Expr::IgnoreParens() {
1482 Expr* E = this;
1483 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1484 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001485
Ted Kremenekfff70962008-01-17 16:57:34 +00001486 return E;
1487}
1488
Chris Lattnerf2660962008-02-13 01:02:39 +00001489/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1490/// or CastExprs or ImplicitCastExprs, returning their operand.
1491Expr *Expr::IgnoreParenCasts() {
1492 Expr *E = this;
1493 while (true) {
1494 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1495 E = P->getSubExpr();
1496 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1497 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001498 else
1499 return E;
1500 }
1501}
1502
Chris Lattneref26c772009-03-13 17:28:01 +00001503/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1504/// value (including ptr->int casts of the same size). Strip off any
1505/// ParenExpr or CastExprs, returning their operand.
1506Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1507 Expr *E = this;
1508 while (true) {
1509 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1510 E = P->getSubExpr();
1511 continue;
1512 }
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattneref26c772009-03-13 17:28:01 +00001514 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1515 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1516 // ptr<->int casts of the same width. We also ignore all identify casts.
1517 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001518
Chris Lattneref26c772009-03-13 17:28:01 +00001519 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1520 E = SE;
1521 continue;
1522 }
Mike Stump11289f42009-09-09 15:08:12 +00001523
Chris Lattneref26c772009-03-13 17:28:01 +00001524 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1525 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1526 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1527 E = SE;
1528 continue;
1529 }
1530 }
Mike Stump11289f42009-09-09 15:08:12 +00001531
Chris Lattneref26c772009-03-13 17:28:01 +00001532 return E;
1533 }
1534}
1535
Douglas Gregord196a582009-12-14 19:27:10 +00001536bool Expr::isDefaultArgument() const {
1537 const Expr *E = this;
1538 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1539 E = ICE->getSubExprAsWritten();
1540
1541 return isa<CXXDefaultArgExpr>(E);
1542}
Chris Lattneref26c772009-03-13 17:28:01 +00001543
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001544/// \brief Skip over any no-op casts and any temporary-binding
1545/// expressions.
1546static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1547 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1548 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1549 E = ICE->getSubExpr();
1550 else
1551 break;
1552 }
1553
1554 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1555 E = BE->getSubExpr();
1556
1557 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1558 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1559 E = ICE->getSubExpr();
1560 else
1561 break;
1562 }
1563
1564 return E;
1565}
1566
1567const Expr *Expr::getTemporaryObject() const {
1568 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1569
1570 // A cast can produce a temporary object. The object's construction
1571 // is represented as a CXXConstructExpr.
1572 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1573 // Only user-defined and constructor conversions can produce
1574 // temporary objects.
1575 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1576 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1577 return 0;
1578
1579 // Strip off temporary bindings and no-op casts.
1580 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1581
1582 // If this is a constructor conversion, see if we have an object
1583 // construction.
1584 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1585 return dyn_cast<CXXConstructExpr>(Sub);
1586
1587 // If this is a user-defined conversion, see if we have a call to
1588 // a function that itself returns a temporary object.
1589 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1590 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1591 if (CE->getCallReturnType()->isRecordType())
1592 return CE;
1593
1594 return 0;
1595 }
1596
1597 // A call returning a class type returns a temporary.
1598 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1599 if (CE->getCallReturnType()->isRecordType())
1600 return CE;
1601
1602 return 0;
1603 }
1604
1605 // Explicit temporary object constructors create temporaries.
1606 return dyn_cast<CXXTemporaryObjectExpr>(E);
1607}
1608
Douglas Gregor4619e432008-12-05 23:32:09 +00001609/// hasAnyTypeDependentArguments - Determines if any of the expressions
1610/// in Exprs is type-dependent.
1611bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1612 for (unsigned I = 0; I < NumExprs; ++I)
1613 if (Exprs[I]->isTypeDependent())
1614 return true;
1615
1616 return false;
1617}
1618
1619/// hasAnyValueDependentArguments - Determines if any of the expressions
1620/// in Exprs is value-dependent.
1621bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1622 for (unsigned I = 0; I < NumExprs; ++I)
1623 if (Exprs[I]->isValueDependent())
1624 return true;
1625
1626 return false;
1627}
1628
Eli Friedman7139af42009-01-25 02:32:41 +00001629bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001630 // This function is attempting whether an expression is an initializer
1631 // which can be evaluated at compile-time. isEvaluatable handles most
1632 // of the cases, but it can't deal with some initializer-specific
1633 // expressions, and it can't deal with aggregates; we deal with those here,
1634 // and fall back to isEvaluatable for the other cases.
1635
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001636 // FIXME: This function assumes the variable being assigned to
1637 // isn't a reference type!
1638
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001639 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001640 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001641 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001642 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001643 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001644 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001645 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001646 // This handles gcc's extension that allows global initializers like
1647 // "struct x {int x;} x = (struct x) {};".
1648 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001649 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001650 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001651 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001652 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001653 // FIXME: This doesn't deal with fields with reference types correctly.
1654 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1655 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001656 const InitListExpr *Exp = cast<InitListExpr>(this);
1657 unsigned numInits = Exp->getNumInits();
1658 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001659 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001660 return false;
1661 }
Eli Friedman384da272009-01-25 03:12:18 +00001662 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001663 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001664 case ImplicitValueInitExprClass:
1665 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001666 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001667 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001668 case UnaryOperatorClass: {
1669 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1670 if (Exp->getOpcode() == UnaryOperator::Extension)
1671 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1672 break;
1673 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001674 case BinaryOperatorClass: {
1675 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1676 // but this handles the common case.
1677 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1678 if (Exp->getOpcode() == BinaryOperator::Sub &&
1679 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1680 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1681 return true;
1682 break;
1683 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001684 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001685 case CStyleCastExprClass:
1686 // Handle casts with a destination that's a struct or union; this
1687 // deals with both the gcc no-op struct cast extension and the
1688 // cast-to-union extension.
1689 if (getType()->isRecordType())
1690 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001691
1692 // Integer->integer casts can be handled here, which is important for
1693 // things like (int)(&&x-&&y). Scary but true.
1694 if (getType()->isIntegerType() &&
1695 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1696 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1697
Eli Friedman384da272009-01-25 03:12:18 +00001698 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001699 }
Eli Friedman384da272009-01-25 03:12:18 +00001700 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001701}
1702
Chris Lattner1f4479e2007-06-05 04:15:44 +00001703/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedman98c56a42009-02-26 09:29:13 +00001704/// an integer constant expression.
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001705
1706/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1707/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +00001708///
Chris Lattnerd7372ba2007-07-18 05:21:20 +00001709/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1710/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1711/// cast+dereference.
Daniel Dunbar4750e632009-02-18 00:47:45 +00001712
Eli Friedman98c56a42009-02-26 09:29:13 +00001713// CheckICE - This function does the fundamental ICE checking: the returned
1714// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1715// Note that to reduce code duplication, this helper does no evaluation
Mike Stump11289f42009-09-09 15:08:12 +00001716// itself; the caller checks whether the expression is evaluatable, and
Eli Friedman98c56a42009-02-26 09:29:13 +00001717// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump11289f42009-09-09 15:08:12 +00001718// value, it calls into Evalute.
Eli Friedman98c56a42009-02-26 09:29:13 +00001719//
1720// Meanings of Val:
1721// 0: This expression is an ICE if it can be evaluated by Evaluate.
1722// 1: This expression is not an ICE, but if it isn't evaluated, it's
1723// a legal subexpression for an ICE. This return value is used to handle
1724// the comma operator in C99 mode.
1725// 2: This expression is not an ICE, and is not a legal subexpression for one.
1726
1727struct ICEDiag {
1728 unsigned Val;
1729 SourceLocation Loc;
1730
1731 public:
1732 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1733 ICEDiag() : Val(0) {}
1734};
1735
1736ICEDiag NoDiag() { return ICEDiag(); }
1737
Eli Friedman90afd3d2009-02-27 04:07:58 +00001738static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1739 Expr::EvalResult EVResult;
1740 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1741 !EVResult.Val.isInt()) {
1742 return ICEDiag(2, E->getLocStart());
1743 }
1744 return NoDiag();
1745}
1746
Eli Friedman98c56a42009-02-26 09:29:13 +00001747static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlsson54b26982009-03-14 00:33:21 +00001748 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedman98c56a42009-02-26 09:29:13 +00001749 if (!E->getType()->isIntegralType()) {
1750 return ICEDiag(2, E->getLocStart());
Eli Friedman5a332ea2008-11-13 06:09:17 +00001751 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001752
1753 switch (E->getStmtClass()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001754#define STMT(Node, Base) case Expr::Node##Class:
1755#define EXPR(Node, Base)
1756#include "clang/AST/StmtNodes.def"
1757 case Expr::PredefinedExprClass:
1758 case Expr::FloatingLiteralClass:
1759 case Expr::ImaginaryLiteralClass:
1760 case Expr::StringLiteralClass:
1761 case Expr::ArraySubscriptExprClass:
1762 case Expr::MemberExprClass:
1763 case Expr::CompoundAssignOperatorClass:
1764 case Expr::CompoundLiteralExprClass:
1765 case Expr::ExtVectorElementExprClass:
1766 case Expr::InitListExprClass:
1767 case Expr::DesignatedInitExprClass:
1768 case Expr::ImplicitValueInitExprClass:
1769 case Expr::ParenListExprClass:
1770 case Expr::VAArgExprClass:
1771 case Expr::AddrLabelExprClass:
1772 case Expr::StmtExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001773 case Expr::CXXMemberCallExprClass:
1774 case Expr::CXXDynamicCastExprClass:
1775 case Expr::CXXTypeidExprClass:
1776 case Expr::CXXNullPtrLiteralExprClass:
1777 case Expr::CXXThisExprClass:
1778 case Expr::CXXThrowExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001779 case Expr::CXXNewExprClass:
1780 case Expr::CXXDeleteExprClass:
1781 case Expr::CXXPseudoDestructorExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001782 case Expr::UnresolvedLookupExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001783 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001784 case Expr::CXXConstructExprClass:
1785 case Expr::CXXBindTemporaryExprClass:
Anders Carlssonba6c4372010-01-29 02:39:32 +00001786 case Expr::CXXBindReferenceExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001787 case Expr::CXXExprWithTemporariesClass:
1788 case Expr::CXXTemporaryObjectExprClass:
1789 case Expr::CXXUnresolvedConstructExprClass:
John McCall8cd78132009-11-19 22:55:06 +00001790 case Expr::CXXDependentScopeMemberExprClass:
John McCall10eae182009-11-30 22:42:35 +00001791 case Expr::UnresolvedMemberExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001792 case Expr::ObjCStringLiteralClass:
1793 case Expr::ObjCEncodeExprClass:
1794 case Expr::ObjCMessageExprClass:
1795 case Expr::ObjCSelectorExprClass:
1796 case Expr::ObjCProtocolExprClass:
1797 case Expr::ObjCIvarRefExprClass:
1798 case Expr::ObjCPropertyRefExprClass:
1799 case Expr::ObjCImplicitSetterGetterRefExprClass:
1800 case Expr::ObjCSuperExprClass:
1801 case Expr::ObjCIsaExprClass:
1802 case Expr::ShuffleVectorExprClass:
1803 case Expr::BlockExprClass:
1804 case Expr::BlockDeclRefExprClass:
1805 case Expr::NoStmtClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001806 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001807
Douglas Gregor73341c42009-09-11 00:18:58 +00001808 case Expr::GNUNullExprClass:
1809 // GCC considers the GNU __null value to be an integral constant expression.
1810 return NoDiag();
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001811
Eli Friedman98c56a42009-02-26 09:29:13 +00001812 case Expr::ParenExprClass:
1813 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1814 case Expr::IntegerLiteralClass:
1815 case Expr::CharacterLiteralClass:
1816 case Expr::CXXBoolLiteralExprClass:
1817 case Expr::CXXZeroInitValueExprClass:
1818 case Expr::TypesCompatibleExprClass:
1819 case Expr::UnaryTypeTraitExprClass:
1820 return NoDiag();
Mike Stump11289f42009-09-09 15:08:12 +00001821 case Expr::CallExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001822 case Expr::CXXOperatorCallExprClass: {
1823 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001824 if (CE->isBuiltinCall(Ctx))
1825 return CheckEvalInICE(E, Ctx);
Eli Friedman98c56a42009-02-26 09:29:13 +00001826 return ICEDiag(2, E->getLocStart());
Chris Lattner5c4664e2007-07-15 23:32:58 +00001827 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001828 case Expr::DeclRefExprClass:
Eli Friedman98c56a42009-02-26 09:29:13 +00001829 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1830 return NoDiag();
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001831 if (Ctx.getLangOptions().CPlusPlus &&
John McCall8ccfcb52009-09-24 19:53:00 +00001832 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCall6dee4732010-02-24 09:03:18 +00001833 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1834
1835 // Parameter variables are never constants. Without this check,
1836 // getAnyInitializer() can find a default argument, which leads
1837 // to chaos.
1838 if (isa<ParmVarDecl>(D))
1839 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1840
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001841 // C++ 7.1.5.1p2
1842 // A variable of non-volatile const-qualified integral or enumeration
1843 // type initialized by an ICE can be used in ICEs.
John McCall6dee4732010-02-24 09:03:18 +00001844 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001845 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1846 if (Quals.hasVolatile() || !Quals.hasConst())
1847 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1848
Sebastian Redl5ca79842010-02-01 20:16:42 +00001849 // Look for a declaration of this variable that has an initializer.
1850 const VarDecl *ID = 0;
1851 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregor0840cc02009-11-01 20:32:48 +00001852 if (Init) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001853 if (ID->isInitKnownICE()) {
Douglas Gregor0840cc02009-11-01 20:32:48 +00001854 // We have already checked whether this subexpression is an
1855 // integral constant expression.
Sebastian Redl5ca79842010-02-01 20:16:42 +00001856 if (ID->isInitICE())
Douglas Gregor0840cc02009-11-01 20:32:48 +00001857 return NoDiag();
1858 else
1859 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1860 }
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001861
John McCall52cc0892010-02-06 01:07:37 +00001862 // It's an ICE whether or not the definition we found is
1863 // out-of-line. See DR 721 and the discussion in Clang PR
1864 // 6206 for details.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001865
1866 if (Dcl->isCheckingICE()) {
1867 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1868 }
1869
1870 Dcl->setCheckingICE();
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001871 ICEDiag Result = CheckICE(Init, Ctx);
1872 // Cache the result of the ICE test.
Eli Friedman1d6fb162009-12-03 20:31:57 +00001873 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor31cf12c2009-05-26 18:54:04 +00001874 return Result;
1875 }
Sebastian Redlf3b5e272009-02-07 13:06:23 +00001876 }
1877 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001878 return ICEDiag(2, E->getLocStart());
1879 case Expr::UnaryOperatorClass: {
1880 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001881 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001882 case UnaryOperator::PostInc:
1883 case UnaryOperator::PostDec:
1884 case UnaryOperator::PreInc:
1885 case UnaryOperator::PreDec:
1886 case UnaryOperator::AddrOf:
1887 case UnaryOperator::Deref:
Eli Friedman98c56a42009-02-26 09:29:13 +00001888 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001889
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001890 case UnaryOperator::Extension:
Eli Friedman98c56a42009-02-26 09:29:13 +00001891 case UnaryOperator::LNot:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001892 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001893 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001894 case UnaryOperator::Not:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001895 case UnaryOperator::Real:
1896 case UnaryOperator::Imag:
Eli Friedman98c56a42009-02-26 09:29:13 +00001897 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlssona8dc3e62008-01-29 15:56:48 +00001898 case UnaryOperator::OffsetOf:
Eli Friedman90afd3d2009-02-27 04:07:58 +00001899 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1900 // Evaluate matches the proposed gcc behavior for cases like
1901 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1902 // compliance: we should warn earlier for offsetof expressions with
1903 // array subscripts that aren't ICEs, and if the array subscripts
1904 // are ICEs, the value of the offsetof must be an integer constant.
1905 return CheckEvalInICE(E, Ctx);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001906 }
Steve Naroff8eeeb132007-05-08 21:09:37 +00001907 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001908 case Expr::SizeOfAlignOfExprClass: {
1909 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1910 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1911 return ICEDiag(2, E->getLocStart());
1912 return NoDiag();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001913 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001914 case Expr::BinaryOperatorClass: {
1915 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001916 switch (Exp->getOpcode()) {
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00001917 case BinaryOperator::PtrMemD:
1918 case BinaryOperator::PtrMemI:
1919 case BinaryOperator::Assign:
1920 case BinaryOperator::MulAssign:
1921 case BinaryOperator::DivAssign:
1922 case BinaryOperator::RemAssign:
1923 case BinaryOperator::AddAssign:
1924 case BinaryOperator::SubAssign:
1925 case BinaryOperator::ShlAssign:
1926 case BinaryOperator::ShrAssign:
1927 case BinaryOperator::AndAssign:
1928 case BinaryOperator::XorAssign:
1929 case BinaryOperator::OrAssign:
Eli Friedman98c56a42009-02-26 09:29:13 +00001930 return ICEDiag(2, E->getLocStart());
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001931
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001932 case BinaryOperator::Mul:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001933 case BinaryOperator::Div:
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001934 case BinaryOperator::Rem:
Eli Friedman98c56a42009-02-26 09:29:13 +00001935 case BinaryOperator::Add:
1936 case BinaryOperator::Sub:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001937 case BinaryOperator::Shl:
Chris Lattner901ae1f2007-06-08 21:54:26 +00001938 case BinaryOperator::Shr:
Eli Friedman98c56a42009-02-26 09:29:13 +00001939 case BinaryOperator::LT:
1940 case BinaryOperator::GT:
1941 case BinaryOperator::LE:
1942 case BinaryOperator::GE:
1943 case BinaryOperator::EQ:
1944 case BinaryOperator::NE:
1945 case BinaryOperator::And:
1946 case BinaryOperator::Xor:
1947 case BinaryOperator::Or:
1948 case BinaryOperator::Comma: {
1949 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1950 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00001951 if (Exp->getOpcode() == BinaryOperator::Div ||
1952 Exp->getOpcode() == BinaryOperator::Rem) {
1953 // Evaluate gives an error for undefined Div/Rem, so make sure
1954 // we don't evaluate one.
1955 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1956 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1957 if (REval == 0)
1958 return ICEDiag(1, E->getLocStart());
1959 if (REval.isSigned() && REval.isAllOnesValue()) {
1960 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1961 if (LEval.isMinSignedValue())
1962 return ICEDiag(1, E->getLocStart());
1963 }
1964 }
1965 }
1966 if (Exp->getOpcode() == BinaryOperator::Comma) {
1967 if (Ctx.getLangOptions().C99) {
1968 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1969 // if it isn't evaluated.
1970 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1971 return ICEDiag(1, E->getLocStart());
1972 } else {
1973 // In both C89 and C++, commas in ICEs are illegal.
1974 return ICEDiag(2, E->getLocStart());
1975 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001976 }
1977 if (LHSResult.Val >= RHSResult.Val)
1978 return LHSResult;
1979 return RHSResult;
1980 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001981 case BinaryOperator::LAnd:
Eli Friedman98c56a42009-02-26 09:29:13 +00001982 case BinaryOperator::LOr: {
1983 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1984 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1985 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1986 // Rare case where the RHS has a comma "side-effect"; we need
1987 // to actually check the condition to see whether the side
1988 // with the comma is evaluated.
Eli Friedman98c56a42009-02-26 09:29:13 +00001989 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman90afd3d2009-02-27 04:07:58 +00001990 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedman98c56a42009-02-26 09:29:13 +00001991 return RHSResult;
1992 return NoDiag();
Eli Friedman8553a982008-11-13 02:13:11 +00001993 }
Eli Friedman90afd3d2009-02-27 04:07:58 +00001994
Eli Friedman98c56a42009-02-26 09:29:13 +00001995 if (LHSResult.Val >= RHSResult.Val)
1996 return LHSResult;
1997 return RHSResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00001998 }
Eli Friedman98c56a42009-02-26 09:29:13 +00001999 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002000 }
Eli Friedman98c56a42009-02-26 09:29:13 +00002001 case Expr::ImplicitCastExprClass:
2002 case Expr::CStyleCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00002003 case Expr::CXXFunctionalCastExprClass:
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00002004 case Expr::CXXNamedCastExprClass:
Douglas Gregor7736e2a2009-09-10 17:44:23 +00002005 case Expr::CXXStaticCastExprClass:
2006 case Expr::CXXReinterpretCastExprClass:
2007 case Expr::CXXConstCastExprClass: {
Eli Friedman98c56a42009-02-26 09:29:13 +00002008 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2009 if (SubExpr->getType()->isIntegralType())
2010 return CheckICE(SubExpr, Ctx);
2011 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2012 return NoDiag();
2013 return ICEDiag(2, E->getLocStart());
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002014 }
Eli Friedman98c56a42009-02-26 09:29:13 +00002015 case Expr::ConditionalOperatorClass: {
2016 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump11289f42009-09-09 15:08:12 +00002017 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner85b25bc2008-12-12 06:55:44 +00002018 // then only the true side is actually considered in an integer constant
Chris Lattner04397352008-12-12 18:00:51 +00002019 // expression, and it is fully evaluated. This is an important GNU
2020 // extension. See GCC PR38377 for discussion.
Eli Friedman98c56a42009-02-26 09:29:13 +00002021 if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregore711f702009-02-14 18:57:46 +00002022 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedman98c56a42009-02-26 09:29:13 +00002023 Expr::EvalResult EVResult;
2024 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2025 !EVResult.Val.isInt()) {
Eli Friedman90afd3d2009-02-27 04:07:58 +00002026 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00002027 }
2028 return NoDiag();
Chris Lattner04397352008-12-12 18:00:51 +00002029 }
Eli Friedman98c56a42009-02-26 09:29:13 +00002030 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2031 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2032 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2033 if (CondResult.Val == 2)
2034 return CondResult;
2035 if (TrueResult.Val == 2)
2036 return TrueResult;
2037 if (FalseResult.Val == 2)
2038 return FalseResult;
2039 if (CondResult.Val == 1)
2040 return CondResult;
2041 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2042 return NoDiag();
2043 // Rare case where the diagnostics depend on which side is evaluated
2044 // Note that if we get here, CondResult is 0, and at least one of
2045 // TrueResult and FalseResult is non-zero.
Eli Friedman90afd3d2009-02-27 04:07:58 +00002046 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedman98c56a42009-02-26 09:29:13 +00002047 return FalseResult;
2048 }
2049 return TrueResult;
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002050 }
Eli Friedman98c56a42009-02-26 09:29:13 +00002051 case Expr::CXXDefaultArgExprClass:
2052 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00002053 case Expr::ChooseExprClass: {
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00002054 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman90afd3d2009-02-27 04:07:58 +00002055 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002056 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002057
Douglas Gregor8ef65fb2009-09-10 23:31:45 +00002058 // Silence a GCC warning
2059 return ICEDiag(2, E->getLocStart());
Eli Friedman98c56a42009-02-26 09:29:13 +00002060}
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002061
Eli Friedman98c56a42009-02-26 09:29:13 +00002062bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2063 SourceLocation *Loc, bool isEvaluated) const {
2064 ICEDiag d = CheckICE(this, Ctx);
2065 if (d.Val != 0) {
2066 if (Loc) *Loc = d.Loc;
2067 return false;
2068 }
2069 EvalResult EvalResult;
Eli Friedman90afd3d2009-02-27 04:07:58 +00002070 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin1615d452009-12-12 05:05:38 +00002071 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman90afd3d2009-02-27 04:07:58 +00002072 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2073 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedman98c56a42009-02-26 09:29:13 +00002074 Result = EvalResult.Val.getInt();
Chris Lattnere0da5dc2007-06-05 05:58:31 +00002075 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +00002076}
2077
Chris Lattner7eef9192007-05-24 01:23:49 +00002078/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2079/// integer constant expression with the value zero, or if this is one that is
2080/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00002081bool Expr::isNullPointerConstant(ASTContext &Ctx,
2082 NullPointerConstantValueDependence NPC) const {
2083 if (isValueDependent()) {
2084 switch (NPC) {
2085 case NPC_NeverValueDependent:
2086 assert(false && "Unexpected value dependent expression!");
2087 // If the unthinkable happens, fall through to the safest alternative.
2088
2089 case NPC_ValueDependentIsNull:
2090 return isTypeDependent() || getType()->isIntegralType();
2091
2092 case NPC_ValueDependentIsNotNull:
2093 return false;
2094 }
2095 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002096
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002097 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002098 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002099 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002100 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002101 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002102 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002103 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002104 Pointee->isVoidType() && // to void*
2105 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002106 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002107 }
Steve Naroffada7d422007-05-20 17:54:12 +00002108 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002109 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2110 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002111 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002112 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2113 // Accept ((void*)0) as a null pointer constant, as many other
2114 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002115 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002116 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002117 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002118 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002119 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002120 } else if (isa<GNUNullExpr>(this)) {
2121 // The GNU __null extension is always a null pointer constant.
2122 return true;
Steve Naroff09035312008-01-14 02:53:34 +00002123 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002124
Sebastian Redl576fd422009-05-10 18:38:11 +00002125 // C++0x nullptr_t is always a null pointer constant.
2126 if (getType()->isNullPtrType())
2127 return true;
2128
Steve Naroff4871fe02008-01-14 16:10:57 +00002129 // This expression must be an integer type.
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002130 if (!getType()->isIntegerType() ||
2131 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00002132 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002133
Chris Lattner1abbd412007-06-08 17:58:43 +00002134 // If we have an integer constant expression, we need to *evaluate* it and
2135 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002136 llvm::APSInt Result;
2137 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002138}
Steve Narofff7a5da12007-07-28 23:10:27 +00002139
Douglas Gregor71235ec2009-05-02 02:18:30 +00002140FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002141 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002142
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002143 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2144 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2145 E = ICE->getSubExpr()->IgnoreParens();
2146 else
2147 break;
2148 }
2149
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002150 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002151 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002152 if (Field->isBitField())
2153 return Field;
2154
2155 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2156 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2157 return BinOp->getLHS()->getBitField();
2158
2159 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002160}
2161
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002162bool Expr::refersToVectorElement() const {
2163 const Expr *E = this->IgnoreParens();
2164
2165 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2166 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2167 E = ICE->getSubExpr()->IgnoreParens();
2168 else
2169 break;
2170 }
2171
2172 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2173 return ASE->getBase()->getType()->isVectorType();
2174
2175 if (isa<ExtVectorElementExpr>(E))
2176 return true;
2177
2178 return false;
2179}
2180
Chris Lattnerb8211f62009-02-16 22:14:05 +00002181/// isArrow - Return true if the base expression is a pointer to vector,
2182/// return false if the base expression is a vector.
2183bool ExtVectorElementExpr::isArrow() const {
2184 return getBase()->getType()->isPointerType();
2185}
2186
Nate Begemance4d7fc2008-04-18 23:10:10 +00002187unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002188 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002189 return VT->getNumElements();
2190 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002191}
2192
Nate Begemanf322eab2008-05-09 06:41:27 +00002193/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002194bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002195 // FIXME: Refactor this code to an accessor on the AST node which returns the
2196 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002197 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002198
2199 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002200 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002201 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002202
Nate Begeman7e5185b2009-01-18 02:01:21 +00002203 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002204 if (Comp[0] == 's' || Comp[0] == 'S')
2205 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002206
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002207 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2208 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002209 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002210
Steve Naroff0d595ca2007-07-30 03:29:09 +00002211 return false;
2212}
Chris Lattner885b4952007-08-02 23:36:59 +00002213
Nate Begemanf322eab2008-05-09 06:41:27 +00002214/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002215void ExtVectorElementExpr::getEncodedElementAccess(
2216 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002217 llvm::StringRef Comp = Accessor->getName();
2218 if (Comp[0] == 's' || Comp[0] == 'S')
2219 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002220
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002221 bool isHi = Comp == "hi";
2222 bool isLo = Comp == "lo";
2223 bool isEven = Comp == "even";
2224 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002225
Nate Begemanf322eab2008-05-09 06:41:27 +00002226 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2227 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002228
Nate Begemanf322eab2008-05-09 06:41:27 +00002229 if (isHi)
2230 Index = e + i;
2231 else if (isLo)
2232 Index = i;
2233 else if (isEven)
2234 Index = 2 * i;
2235 else if (isOdd)
2236 Index = 2 * i + 1;
2237 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002238 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002239
Nate Begemand3862152008-05-13 21:03:02 +00002240 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002241 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002242}
2243
Steve Narofff73590d2007-09-27 14:38:14 +00002244// constructor for instance messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002245ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, Expr *receiver,
2246 Selector selInfo,
2247 QualType retType, ObjCMethodDecl *mproto,
2248 SourceLocation LBrac, SourceLocation RBrac,
2249 Expr **ArgExprs, unsigned nargs)
Eli Friedman84341cd2009-12-30 00:13:48 +00002250 : Expr(ObjCMessageExprClass, retType, false, false), SelName(selInfo),
Ted Kremenekb8861a62008-05-01 17:26:20 +00002251 MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002252 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002253 SubExprs = new (C) Stmt*[NumArgs+1];
Steve Narofff73590d2007-09-27 14:38:14 +00002254 SubExprs[RECEIVER] = receiver;
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002255 if (NumArgs) {
2256 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002257 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2258 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002259 LBracloc = LBrac;
2260 RBracloc = RBrac;
2261}
2262
Mike Stump11289f42009-09-09 15:08:12 +00002263// constructor for class messages.
Steve Narofff73590d2007-09-27 14:38:14 +00002264// FIXME: clsName should be typed to ObjCInterfaceType
Ted Kremenek2c809302010-02-11 22:41:21 +00002265ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, IdentifierInfo *clsName,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002266 SourceLocation clsNameLoc, Selector selInfo,
2267 QualType retType, ObjCMethodDecl *mproto,
Ted Kremenek2c809302010-02-11 22:41:21 +00002268 SourceLocation LBrac, SourceLocation RBrac,
2269 Expr **ArgExprs, unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002270 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2271 SelName(selInfo), MethodProto(mproto) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002272 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002273 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002274 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002275 if (NumArgs) {
2276 for (unsigned i = 0; i != NumArgs; ++i)
Steve Narofff73590d2007-09-27 14:38:14 +00002277 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2278 }
Steve Naroffd54978b2007-09-18 23:55:05 +00002279 LBracloc = LBrac;
2280 RBracloc = RBrac;
2281}
2282
Mike Stump11289f42009-09-09 15:08:12 +00002283// constructor for class messages.
Ted Kremenek2c809302010-02-11 22:41:21 +00002284ObjCMessageExpr::ObjCMessageExpr(ASTContext &C, ObjCInterfaceDecl *cls,
Douglas Gregorde4827d2010-03-08 16:40:19 +00002285 SourceLocation clsNameLoc, Selector selInfo,
2286 QualType retType,
Ted Kremenek2c809302010-02-11 22:41:21 +00002287 ObjCMethodDecl *mproto, SourceLocation LBrac,
2288 SourceLocation RBrac, Expr **ArgExprs,
2289 unsigned nargs)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002290 : Expr(ObjCMessageExprClass, retType, false, false), ClassNameLoc(clsNameLoc),
2291 SelName(selInfo), MethodProto(mproto)
2292{
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002293 NumArgs = nargs;
Ted Kremenek2c809302010-02-11 22:41:21 +00002294 SubExprs = new (C) Stmt*[NumArgs+1];
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002295 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
2296 if (NumArgs) {
2297 for (unsigned i = 0; i != NumArgs; ++i)
2298 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
2299 }
2300 LBracloc = LBrac;
2301 RBracloc = RBrac;
2302}
2303
2304ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
2305 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
2306 switch (x & Flags) {
2307 default:
2308 assert(false && "Invalid ObjCMessageExpr.");
2309 case IsInstMeth:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002310 return ClassInfo(0, 0, SourceLocation());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002311 case IsClsMethDeclUnknown:
Douglas Gregorde4827d2010-03-08 16:40:19 +00002312 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002313 case IsClsMethDeclKnown: {
2314 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002315 return ClassInfo(D, D->getIdentifier(), ClassNameLoc);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002316 }
2317 }
2318}
2319
Chris Lattner7ec71da2009-04-26 00:44:05 +00002320void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) {
Douglas Gregorde4827d2010-03-08 16:40:19 +00002321 if (CI.Decl == 0 && CI.Name == 0) {
Chris Lattner7ec71da2009-04-26 00:44:05 +00002322 SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth);
Douglas Gregorde4827d2010-03-08 16:40:19 +00002323 return;
2324 }
2325
2326 if (CI.Decl == 0)
2327 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Name | IsClsMethDeclUnknown);
Chris Lattner7ec71da2009-04-26 00:44:05 +00002328 else
Douglas Gregorde4827d2010-03-08 16:40:19 +00002329 SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.Decl | IsClsMethDeclKnown);
2330 ClassNameLoc = CI.Loc;
Chris Lattner7ec71da2009-04-26 00:44:05 +00002331}
2332
Ted Kremenek2c809302010-02-11 22:41:21 +00002333void ObjCMessageExpr::DoDestroy(ASTContext &C) {
2334 DestroyChildren(C);
2335 if (SubExprs)
2336 C.Deallocate(SubExprs);
2337 this->~ObjCMessageExpr();
2338 C.Deallocate((void*) this);
2339}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002340
Chris Lattner35e564e2007-10-25 00:29:32 +00002341bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002342 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002343}
2344
Nate Begeman48745922009-08-12 02:28:50 +00002345void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2346 unsigned NumExprs) {
2347 if (SubExprs) C.Deallocate(SubExprs);
2348
2349 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002350 this->NumExprs = NumExprs;
2351 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002352}
Nate Begeman48745922009-08-12 02:28:50 +00002353
2354void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2355 DestroyChildren(C);
2356 if (SubExprs) C.Deallocate(SubExprs);
2357 this->~ShuffleVectorExpr();
2358 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002359}
2360
Douglas Gregore26a2852009-08-07 06:08:38 +00002361void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002362 // Override default behavior of traversing children. If this has a type
2363 // operand and the type is a variable-length array, the child iteration
2364 // will iterate over the size expression. However, this expression belongs
2365 // to the type, not to this, so we don't want to delete it.
2366 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002367 if (isArgumentType()) {
2368 this->~SizeOfAlignOfExpr();
2369 C.Deallocate(this);
2370 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002371 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002372 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002373}
2374
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002375//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002376// DesignatedInitExpr
2377//===----------------------------------------------------------------------===//
2378
2379IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2380 assert(Kind == FieldDesignator && "Only valid on a field designator");
2381 if (Field.NameOrField & 0x01)
2382 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2383 else
2384 return getField()->getIdentifier();
2385}
2386
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002387DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2388 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002389 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002390 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002391 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002392 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002393 unsigned NumIndexExprs,
2394 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002395 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002396 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002397 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2398 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002399 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002400
2401 // Record the initializer itself.
2402 child_iterator Child = child_begin();
2403 *Child++ = Init;
2404
2405 // Copy the designators and their subexpressions, computing
2406 // value-dependence along the way.
2407 unsigned IndexIdx = 0;
2408 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002409 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002410
2411 if (this->Designators[I].isArrayDesignator()) {
2412 // Compute type- and value-dependence.
2413 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002414 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002415 Index->isTypeDependent() || Index->isValueDependent();
2416
2417 // Copy the index expressions into permanent storage.
2418 *Child++ = IndexExprs[IndexIdx++];
2419 } else if (this->Designators[I].isArrayRangeDesignator()) {
2420 // Compute type- and value-dependence.
2421 Expr *Start = IndexExprs[IndexIdx];
2422 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002423 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002424 Start->isTypeDependent() || Start->isValueDependent() ||
2425 End->isTypeDependent() || End->isValueDependent();
2426
2427 // Copy the start/end expressions into permanent storage.
2428 *Child++ = IndexExprs[IndexIdx++];
2429 *Child++ = IndexExprs[IndexIdx++];
2430 }
2431 }
2432
2433 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002434}
2435
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002436DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002437DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002438 unsigned NumDesignators,
2439 Expr **IndexExprs, unsigned NumIndexExprs,
2440 SourceLocation ColonOrEqualLoc,
2441 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002442 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002443 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002444 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002445 ColonOrEqualLoc, UsesColonSyntax,
2446 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002447}
2448
Mike Stump11289f42009-09-09 15:08:12 +00002449DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002450 unsigned NumIndexExprs) {
2451 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2452 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2453 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2454}
2455
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002456void DesignatedInitExpr::setDesignators(ASTContext &C,
2457 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002458 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002459 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002460
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002461 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002462 NumDesignators = NumDesigs;
2463 for (unsigned I = 0; I != NumDesigs; ++I)
2464 Designators[I] = Desigs[I];
2465}
2466
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002467SourceRange DesignatedInitExpr::getSourceRange() const {
2468 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002469 Designator &First =
2470 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002471 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002472 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002473 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2474 else
2475 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2476 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002477 StartLoc =
2478 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002479 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2480}
2481
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002482Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2483 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2484 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2485 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002486 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2487 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2488}
2489
2490Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002491 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002492 "Requires array range designator");
2493 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2494 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002495 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2496 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2497}
2498
2499Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002500 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002501 "Requires array range designator");
2502 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2503 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002504 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2505 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2506}
2507
Douglas Gregord5846a12009-04-15 06:41:24 +00002508/// \brief Replaces the designator at index @p Idx with the series
2509/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002510void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002511 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002512 const Designator *Last) {
2513 unsigned NumNewDesignators = Last - First;
2514 if (NumNewDesignators == 0) {
2515 std::copy_backward(Designators + Idx + 1,
2516 Designators + NumDesignators,
2517 Designators + Idx);
2518 --NumNewDesignators;
2519 return;
2520 } else if (NumNewDesignators == 1) {
2521 Designators[Idx] = *First;
2522 return;
2523 }
2524
Mike Stump11289f42009-09-09 15:08:12 +00002525 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002526 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002527 std::copy(Designators, Designators + Idx, NewDesignators);
2528 std::copy(First, Last, NewDesignators + Idx);
2529 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2530 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002531 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002532 Designators = NewDesignators;
2533 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2534}
2535
Douglas Gregore26a2852009-08-07 06:08:38 +00002536void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002537 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002538 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002539}
2540
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002541void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2542 for (unsigned I = 0; I != NumDesignators; ++I)
2543 Designators[I].~Designator();
2544 C.Deallocate(Designators);
2545 Designators = 0;
2546}
2547
Mike Stump11289f42009-09-09 15:08:12 +00002548ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002549 Expr **exprs, unsigned nexprs,
2550 SourceLocation rparenloc)
2551: Expr(ParenListExprClass, QualType(),
2552 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002553 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002554 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002555
Nate Begeman5ec4b312009-08-10 23:49:36 +00002556 Exprs = new (C) Stmt*[nexprs];
2557 for (unsigned i = 0; i != nexprs; ++i)
2558 Exprs[i] = exprs[i];
2559}
2560
2561void ParenListExpr::DoDestroy(ASTContext& C) {
2562 DestroyChildren(C);
2563 if (Exprs) C.Deallocate(Exprs);
2564 this->~ParenListExpr();
2565 C.Deallocate(this);
2566}
2567
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002568//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002569// ExprIterator.
2570//===----------------------------------------------------------------------===//
2571
2572Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2573Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2574Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2575const Expr* ConstExprIterator::operator[](size_t idx) const {
2576 return cast<Expr>(I[idx]);
2577}
2578const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2579const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2580
2581//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002582// Child Iterators for iterating over subexpressions/substatements
2583//===----------------------------------------------------------------------===//
2584
2585// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002586Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2587Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002588
Steve Naroffe46504b2007-11-12 14:29:37 +00002589// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002590Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2591Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002592
Steve Naroffebf4cb42008-06-02 23:03:37 +00002593// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002594Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2595Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002596
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002597// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002598Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2599 return &Base;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002600}
Mike Stump11289f42009-09-09 15:08:12 +00002601Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2602 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002603}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002604
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002605// ObjCSuperExpr
2606Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2607Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2608
Steve Naroffe87026a2009-07-24 17:54:45 +00002609// ObjCIsaExpr
2610Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2611Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2612
Chris Lattner6307f192008-08-10 01:53:14 +00002613// PredefinedExpr
2614Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2615Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002616
2617// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002618Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2619Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002620
2621// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002622Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002623Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002624
2625// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002626Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2627Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002628
Chris Lattner1c20a172007-08-26 03:42:43 +00002629// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002630Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2631Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002632
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002633// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002634Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2635Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002636
2637// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002638Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2639Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002640
2641// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002642Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2643Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002644
Sebastian Redl6f282892008-11-11 17:56:53 +00002645// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002646Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002647 // If this is of a type and the type is a VLA type (and not a typedef), the
2648 // size expression of the VLA needs to be treated as an executable expression.
2649 // Why isn't this weirdness documented better in StmtIterator?
2650 if (isArgumentType()) {
2651 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2652 getArgumentType().getTypePtr()))
2653 return child_iterator(T);
2654 return child_iterator();
2655 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002656 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002657}
Sebastian Redl6f282892008-11-11 17:56:53 +00002658Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2659 if (isArgumentType())
2660 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002661 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002662}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002663
2664// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002665Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002666 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002667}
Ted Kremenek23702b62007-08-24 20:06:47 +00002668Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002669 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002670}
2671
2672// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002673Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002674 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002675}
Ted Kremenek23702b62007-08-24 20:06:47 +00002676Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002677 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002678}
Ted Kremenek23702b62007-08-24 20:06:47 +00002679
2680// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002681Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2682Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002683
Nate Begemance4d7fc2008-04-18 23:10:10 +00002684// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002685Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2686Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002687
2688// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002689Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2690Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002691
Ted Kremenek23702b62007-08-24 20:06:47 +00002692// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002693Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2694Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002695
2696// BinaryOperator
2697Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002698 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002699}
Ted Kremenek23702b62007-08-24 20:06:47 +00002700Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002701 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002702}
2703
2704// ConditionalOperator
2705Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002706 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002707}
Ted Kremenek23702b62007-08-24 20:06:47 +00002708Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002709 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002710}
2711
2712// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002713Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2714Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002715
Ted Kremenek23702b62007-08-24 20:06:47 +00002716// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002717Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2718Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002719
2720// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002721Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2722 return child_iterator();
2723}
2724
2725Stmt::child_iterator TypesCompatibleExpr::child_end() {
2726 return child_iterator();
2727}
Ted Kremenek23702b62007-08-24 20:06:47 +00002728
2729// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002730Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2731Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002732
Douglas Gregor3be4b122008-11-29 04:51:27 +00002733// GNUNullExpr
2734Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2735Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2736
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002737// ShuffleVectorExpr
2738Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002739 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002740}
2741Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002742 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002743}
2744
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002745// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002746Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2747Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002748
Anders Carlsson4692db02007-08-31 04:56:16 +00002749// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002750Stmt::child_iterator InitListExpr::child_begin() {
2751 return InitExprs.size() ? &InitExprs[0] : 0;
2752}
2753Stmt::child_iterator InitListExpr::child_end() {
2754 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2755}
Anders Carlsson4692db02007-08-31 04:56:16 +00002756
Douglas Gregor0202cb42009-01-29 17:44:32 +00002757// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002758Stmt::child_iterator DesignatedInitExpr::child_begin() {
2759 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2760 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002761 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2762}
2763Stmt::child_iterator DesignatedInitExpr::child_end() {
2764 return child_iterator(&*child_begin() + NumSubExprs);
2765}
2766
Douglas Gregor0202cb42009-01-29 17:44:32 +00002767// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002768Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2769 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002770}
2771
Mike Stump11289f42009-09-09 15:08:12 +00002772Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2773 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002774}
2775
Nate Begeman5ec4b312009-08-10 23:49:36 +00002776// ParenListExpr
2777Stmt::child_iterator ParenListExpr::child_begin() {
2778 return &Exprs[0];
2779}
2780Stmt::child_iterator ParenListExpr::child_end() {
2781 return &Exprs[0]+NumExprs;
2782}
2783
Ted Kremenek23702b62007-08-24 20:06:47 +00002784// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002785Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002786 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002787}
2788Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002789 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002790}
Ted Kremenek23702b62007-08-24 20:06:47 +00002791
2792// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002793Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2794Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002795
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002796// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002797Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002798 return child_iterator();
2799}
2800Stmt::child_iterator ObjCSelectorExpr::child_end() {
2801 return child_iterator();
2802}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002803
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002804// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002805Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2806 return child_iterator();
2807}
2808Stmt::child_iterator ObjCProtocolExpr::child_end() {
2809 return child_iterator();
2810}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002811
Steve Naroffd54978b2007-09-18 23:55:05 +00002812// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002813Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002814 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffd54978b2007-09-18 23:55:05 +00002815}
2816Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002817 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002818}
2819
Steve Naroffc540d662008-09-03 18:15:37 +00002820// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002821Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2822Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002823
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002824Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2825Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }