blob: 8770bfd3aa4ec79dac9ccb6851e685e532378a79 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000027#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Chris Lattner2b334bb2010-04-16 23:34:13 +000030/// isKnownToHaveBooleanValue - Return true if this is an integer expression
31/// that is known to return 0 or 1. This happens for _Bool/bool expressions
32/// but also int expressions which are produced by things like comparisons in
33/// C.
34bool Expr::isKnownToHaveBooleanValue() const {
35 // If this value has _Bool type, it is obvious 0/1.
36 if (getType()->isBooleanType()) return true;
37 // If this is a non-scalar-integer type, we don't care enough to try.
38 if (!getType()->isIntegralType()) return false;
39
40 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
41 return PE->getSubExpr()->isKnownToHaveBooleanValue();
42
43 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
44 switch (UO->getOpcode()) {
45 case UnaryOperator::Plus:
46 case UnaryOperator::Extension:
47 return UO->getSubExpr()->isKnownToHaveBooleanValue();
48 default:
49 return false;
50 }
51 }
52
53 if (const CastExpr *CE = dyn_cast<CastExpr>(this))
54 return CE->getSubExpr()->isKnownToHaveBooleanValue();
55
56 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
57 switch (BO->getOpcode()) {
58 default: return false;
59 case BinaryOperator::LT: // Relational operators.
60 case BinaryOperator::GT:
61 case BinaryOperator::LE:
62 case BinaryOperator::GE:
63 case BinaryOperator::EQ: // Equality operators.
64 case BinaryOperator::NE:
65 case BinaryOperator::LAnd: // AND operator.
66 case BinaryOperator::LOr: // Logical OR operator.
67 return true;
68
69 case BinaryOperator::And: // Bitwise AND operator.
70 case BinaryOperator::Xor: // Bitwise XOR operator.
71 case BinaryOperator::Or: // Bitwise OR operator.
72 // Handle things like (x==2)|(y==12).
73 return BO->getLHS()->isKnownToHaveBooleanValue() &&
74 BO->getRHS()->isKnownToHaveBooleanValue();
75
76 case BinaryOperator::Comma:
77 case BinaryOperator::Assign:
78 return BO->getRHS()->isKnownToHaveBooleanValue();
79 }
80 }
81
82 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
83 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
84 CO->getFalseExpr()->isKnownToHaveBooleanValue();
85
86 return false;
87}
88
Reid Spencer5f016e22007-07-11 17:01:13 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCalld5532b62009-11-23 01:53:49 +000093void ExplicitTemplateArgumentList::initializeFrom(
94 const TemplateArgumentListInfo &Info) {
95 LAngleLoc = Info.getLAngleLoc();
96 RAngleLoc = Info.getRAngleLoc();
97 NumTemplateArgs = Info.size();
98
99 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
100 for (unsigned i = 0; i != NumTemplateArgs; ++i)
101 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
102}
103
104void ExplicitTemplateArgumentList::copyInto(
105 TemplateArgumentListInfo &Info) const {
106 Info.setLAngleLoc(LAngleLoc);
107 Info.setRAngleLoc(RAngleLoc);
108 for (unsigned I = 0; I != NumTemplateArgs; ++I)
109 Info.addArgument(getTemplateArgs()[I]);
110}
111
112std::size_t ExplicitTemplateArgumentList::sizeFor(
113 const TemplateArgumentListInfo &Info) {
114 return sizeof(ExplicitTemplateArgumentList) +
115 sizeof(TemplateArgumentLoc) * Info.size();
116}
117
Douglas Gregor0da76df2009-11-23 11:41:28 +0000118void DeclRefExpr::computeDependence() {
119 TypeDependent = false;
120 ValueDependent = false;
121
122 NamedDecl *D = getDecl();
123
124 // (TD) C++ [temp.dep.expr]p3:
125 // An id-expression is type-dependent if it contains:
126 //
127 // and
128 //
129 // (VD) C++ [temp.dep.constexpr]p2:
130 // An identifier is value-dependent if it is:
131
132 // (TD) - an identifier that was declared with dependent type
133 // (VD) - a name declared with a dependent type,
134 if (getType()->isDependentType()) {
135 TypeDependent = true;
136 ValueDependent = true;
137 }
138 // (TD) - a conversion-function-id that specifies a dependent type
139 else if (D->getDeclName().getNameKind()
140 == DeclarationName::CXXConversionFunctionName &&
141 D->getDeclName().getCXXNameType()->isDependentType()) {
142 TypeDependent = true;
143 ValueDependent = true;
144 }
145 // (TD) - a template-id that is dependent,
146 else if (hasExplicitTemplateArgumentList() &&
147 TemplateSpecializationType::anyDependentTemplateArguments(
148 getTemplateArgs(),
149 getNumTemplateArgs())) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (VD) - the name of a non-type template parameter,
154 else if (isa<NonTypeTemplateParmDecl>(D))
155 ValueDependent = true;
156 // (VD) - a constant with integral or enumeration type and is
157 // initialized with an expression that is value-dependent.
158 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
159 if (Var->getType()->isIntegralType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
164 }
Douglas Gregor0da76df2009-11-23 11:41:28 +0000165 }
166 // (TD) - a nested-name-specifier or a qualified-id that names a
167 // member of an unknown specialization.
168 // (handled by DependentScopeDeclRefExpr)
169}
170
Douglas Gregora2813ce2009-10-23 18:54:35 +0000171DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
172 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000173 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000174 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000175 QualType T)
176 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000177 DecoratedD(D,
178 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000179 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000180 Loc(NameLoc) {
181 if (Qualifier) {
182 NameQualifier *NQ = getNameQualifier();
183 NQ->NNS = Qualifier;
184 NQ->Range = QualifierRange;
185 }
186
John McCalld5532b62009-11-23 01:53:49 +0000187 if (TemplateArgs)
188 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000189
190 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000191}
192
193DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
194 NestedNameSpecifier *Qualifier,
195 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000196 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000197 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000198 QualType T,
199 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000200 std::size_t Size = sizeof(DeclRefExpr);
201 if (Qualifier != 0)
202 Size += sizeof(NameQualifier);
203
John McCalld5532b62009-11-23 01:53:49 +0000204 if (TemplateArgs)
205 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000206
207 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
208 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000209 TemplateArgs, T);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000210}
211
212SourceRange DeclRefExpr::getSourceRange() const {
213 // FIXME: Does not handle multi-token names well, e.g., operator[].
214 SourceRange R(Loc);
215
216 if (hasQualifier())
217 R.setBegin(getQualifierRange().getBegin());
218 if (hasExplicitTemplateArgumentList())
219 R.setEnd(getRAngleLoc());
220 return R;
221}
222
Anders Carlsson3a082d82009-09-08 18:24:21 +0000223// FIXME: Maybe this should use DeclPrinter with a special "print predefined
224// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000225std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
226 ASTContext &Context = CurrentDecl->getASTContext();
227
Anders Carlsson3a082d82009-09-08 18:24:21 +0000228 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000229 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000230 return FD->getNameAsString();
231
232 llvm::SmallString<256> Name;
233 llvm::raw_svector_ostream Out(Name);
234
235 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000236 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000237 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000238 if (MD->isStatic())
239 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000240 }
241
242 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000243
244 std::string Proto = FD->getQualifiedNameAsString(Policy);
245
John McCall183700f2009-09-21 23:43:11 +0000246 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000247 const FunctionProtoType *FT = 0;
248 if (FD->hasWrittenPrototype())
249 FT = dyn_cast<FunctionProtoType>(AFT);
250
251 Proto += "(";
252 if (FT) {
253 llvm::raw_string_ostream POut(Proto);
254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
255 if (i) POut << ", ";
256 std::string Param;
257 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
258 POut << Param;
259 }
260
261 if (FT->isVariadic()) {
262 if (FD->getNumParams()) POut << ", ";
263 POut << "...";
264 }
265 }
266 Proto += ")";
267
Sam Weinig4eadcc52009-12-27 01:38:20 +0000268 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
269 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
270 if (ThisQuals.hasConst())
271 Proto += " const";
272 if (ThisQuals.hasVolatile())
273 Proto += " volatile";
274 }
275
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000276 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
277 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000278
279 Out << Proto;
280
281 Out.flush();
282 return Name.str().str();
283 }
284 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
285 llvm::SmallString<256> Name;
286 llvm::raw_svector_ostream Out(Name);
287 Out << (MD->isInstanceMethod() ? '-' : '+');
288 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000289
290 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
291 // a null check to avoid a crash.
292 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000293 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000294
Anders Carlsson3a082d82009-09-08 18:24:21 +0000295 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000296 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
297 Out << '(' << CID << ')';
298
Anders Carlsson3a082d82009-09-08 18:24:21 +0000299 Out << ' ';
300 Out << MD->getSelector().getAsString();
301 Out << ']';
302
303 Out.flush();
304 return Name.str().str();
305 }
306 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
307 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
308 return "top level";
309 }
310 return "";
311}
312
Chris Lattnerda8249e2008-06-07 22:13:43 +0000313/// getValueAsApproximateDouble - This returns the value as an inaccurate
314/// double. Note that this may cause loss of precision, but is useful for
315/// debugging dumps, etc.
316double FloatingLiteral::getValueAsApproximateDouble() const {
317 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000318 bool ignored;
319 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
320 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000321 return V.convertToDouble();
322}
323
Chris Lattner2085fd62009-02-18 06:40:38 +0000324StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
325 unsigned ByteLength, bool Wide,
326 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000327 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000328 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000329 // Allocate enough space for the StringLiteral plus an array of locations for
330 // any concatenated string tokens.
331 void *Mem = C.Allocate(sizeof(StringLiteral)+
332 sizeof(SourceLocation)*(NumStrs-1),
333 llvm::alignof<StringLiteral>());
334 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000337 char *AStrData = new (C, 1) char[ByteLength];
338 memcpy(AStrData, StrData, ByteLength);
339 SL->StrData = AStrData;
340 SL->ByteLength = ByteLength;
341 SL->IsWide = Wide;
342 SL->TokLocs[0] = Loc[0];
343 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344
Chris Lattner726e1682009-02-18 05:49:11 +0000345 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000346 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
347 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000348}
349
Douglas Gregor673ecd62009-04-15 16:35:07 +0000350StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
351 void *Mem = C.Allocate(sizeof(StringLiteral)+
352 sizeof(SourceLocation)*(NumStrs-1),
353 llvm::alignof<StringLiteral>());
354 StringLiteral *SL = new (Mem) StringLiteral(QualType());
355 SL->StrData = 0;
356 SL->ByteLength = 0;
357 SL->NumConcatenated = NumStrs;
358 return SL;
359}
360
Douglas Gregor42602bb2009-08-07 06:08:38 +0000361void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek8189cde2009-02-07 01:47:29 +0000362 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregor42602bb2009-08-07 06:08:38 +0000363 Expr::DoDestroy(C);
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
Daniel Dunbarb6480232009-09-22 03:27:33 +0000366void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor673ecd62009-04-15 16:35:07 +0000367 if (StrData)
368 C.Deallocate(const_cast<char*>(StrData));
369
Daniel Dunbarb6480232009-09-22 03:27:33 +0000370 char *AStrData = new (C, 1) char[Str.size()];
371 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000372 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000373 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
377/// corresponds to, e.g. "sizeof" or "[pre]++".
378const char *UnaryOperator::getOpcodeStr(Opcode Op) {
379 switch (Op) {
380 default: assert(0 && "Unknown unary operator");
381 case PostInc: return "++";
382 case PostDec: return "--";
383 case PreInc: return "++";
384 case PreDec: return "--";
385 case AddrOf: return "&";
386 case Deref: return "*";
387 case Plus: return "+";
388 case Minus: return "-";
389 case Not: return "~";
390 case LNot: return "!";
391 case Real: return "__real";
392 case Imag: return "__imag";
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 case Extension: return "__extension__";
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000394 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 }
396}
397
Mike Stump1eb44332009-09-09 15:08:12 +0000398UnaryOperator::Opcode
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000399UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
400 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000401 default: assert(false && "No unary operator for overloaded function");
Chris Lattnerb7beee92009-03-22 00:10:22 +0000402 case OO_PlusPlus: return Postfix ? PostInc : PreInc;
403 case OO_MinusMinus: return Postfix ? PostDec : PreDec;
404 case OO_Amp: return AddrOf;
405 case OO_Star: return Deref;
406 case OO_Plus: return Plus;
407 case OO_Minus: return Minus;
408 case OO_Tilde: return Not;
409 case OO_Exclaim: return LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000410 }
411}
412
413OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
414 switch (Opc) {
415 case PostInc: case PreInc: return OO_PlusPlus;
416 case PostDec: case PreDec: return OO_MinusMinus;
417 case AddrOf: return OO_Amp;
418 case Deref: return OO_Star;
419 case Plus: return OO_Plus;
420 case Minus: return OO_Minus;
421 case Not: return OO_Tilde;
422 case LNot: return OO_Exclaim;
423 default: return OO_None;
424 }
425}
426
427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428//===----------------------------------------------------------------------===//
429// Postfix Operators.
430//===----------------------------------------------------------------------===//
431
Ted Kremenek668bf912009-02-09 20:51:47 +0000432CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek8189cde2009-02-07 01:47:29 +0000433 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump1eb44332009-09-09 15:08:12 +0000434 : Expr(SC, t,
Douglas Gregor898574e2008-12-05 23:32:09 +0000435 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000436 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000437 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Ted Kremenek668bf912009-02-09 20:51:47 +0000439 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregorb4609802008-11-14 16:09:21 +0000440 SubExprs[FN] = fn;
441 for (unsigned i = 0; i != numargs; ++i)
442 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000443
Douglas Gregorb4609802008-11-14 16:09:21 +0000444 RParenLoc = rparenloc;
445}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000446
Ted Kremenek668bf912009-02-09 20:51:47 +0000447CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
448 QualType t, SourceLocation rparenloc)
Douglas Gregor898574e2008-12-05 23:32:09 +0000449 : Expr(CallExprClass, t,
450 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattnerd603eaa2009-02-16 22:33:34 +0000451 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor898574e2008-12-05 23:32:09 +0000452 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000453
454 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000455 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000457 SubExprs[i+ARGS_START] = args[i];
Ted Kremenek668bf912009-02-09 20:51:47 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 RParenLoc = rparenloc;
460}
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
463 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000464 SubExprs = new (C) Stmt*[1];
465}
466
Douglas Gregor42602bb2009-08-07 06:08:38 +0000467void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000468 DestroyChildren(C);
469 if (SubExprs) C.Deallocate(SubExprs);
470 this->~CallExpr();
471 C.Deallocate(this);
472}
473
Nuno Lopesd20254f2009-12-20 23:11:08 +0000474Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000475 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner6346f962009-07-17 15:46:27 +0000476 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000477 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000478 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
479 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000480
481 return 0;
482}
483
Nuno Lopesd20254f2009-12-20 23:11:08 +0000484FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000485 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000486}
487
Chris Lattnerd18b3292007-12-28 05:25:02 +0000488/// setNumArgs - This changes the number of arguments present in this call.
489/// Any orphaned expressions are deleted by this, and any new operands are set
490/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000491void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000492 // No change, just return.
493 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattnerd18b3292007-12-28 05:25:02 +0000495 // If shrinking # arguments, just delete the extras and forgot them.
496 if (NumArgs < getNumArgs()) {
497 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
Ted Kremenek8189cde2009-02-07 01:47:29 +0000498 getArg(i)->Destroy(C);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000499 this->NumArgs = NumArgs;
500 return;
501 }
502
503 // Otherwise, we are growing the # arguments. New an bigger argument array.
Daniel Dunbar68a049c2009-07-28 06:29:46 +0000504 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000505 // Copy over args.
506 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
507 NewSubExprs[i] = SubExprs[i];
508 // Null out new args.
509 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
510 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor88c9a462009-04-17 21:46:47 +0000512 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000513 SubExprs = NewSubExprs;
514 this->NumArgs = NumArgs;
515}
516
Chris Lattnercb888962008-10-06 05:00:53 +0000517/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
518/// not, return 0.
Douglas Gregor3c385e52009-02-14 18:57:46 +0000519unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000520 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000521 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000522 // ImplicitCastExpr.
523 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
524 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000525 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000527 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
528 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000529 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlssonbcba2012008-01-31 02:13:57 +0000531 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
532 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000533 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000535 if (!FDecl->getIdentifier())
536 return 0;
537
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000538 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000539}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000540
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000541QualType CallExpr::getCallReturnType() const {
542 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000543 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000544 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000545 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000546 CalleeType = BPT->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547
John McCall183700f2009-09-21 23:43:11 +0000548 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000549 return FnType->getResultType();
550}
Chris Lattnercb888962008-10-06 05:00:53 +0000551
Mike Stump1eb44332009-09-09 15:08:12 +0000552MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
553 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000554 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000555 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000556 DeclAccessPair founddecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000557 SourceLocation l,
John McCalld5532b62009-11-23 01:53:49 +0000558 const TemplateArgumentListInfo *targs,
Douglas Gregorc4bf26f2009-09-01 00:37:14 +0000559 QualType ty) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000560 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000561
John McCall161755a2010-04-06 21:38:20 +0000562 bool hasQualOrFound = (qual != 0 ||
563 founddecl.getDecl() != memberdecl ||
564 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000565 if (hasQualOrFound)
566 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000567
John McCalld5532b62009-11-23 01:53:49 +0000568 if (targs)
569 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000571 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall6bb80172010-03-30 21:47:33 +0000572 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
573
574 if (hasQualOrFound) {
575 if (qual && qual->isDependent()) {
576 E->setValueDependent(true);
577 E->setTypeDependent(true);
578 }
579 E->HasQualifierOrFoundDecl = true;
580
581 MemberNameQualifier *NQ = E->getMemberQualifier();
582 NQ->NNS = qual;
583 NQ->Range = qualrange;
584 NQ->FoundDecl = founddecl;
585 }
586
587 if (targs) {
588 E->HasExplicitTemplateArgumentList = true;
589 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
590 }
591
592 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000593}
594
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000595const char *CastExpr::getCastKindName() const {
596 switch (getCastKind()) {
597 case CastExpr::CK_Unknown:
598 return "Unknown";
599 case CastExpr::CK_BitCast:
600 return "BitCast";
601 case CastExpr::CK_NoOp:
602 return "NoOp";
Anders Carlsson11de6de2009-11-12 16:43:42 +0000603 case CastExpr::CK_BaseToDerived:
604 return "BaseToDerived";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000605 case CastExpr::CK_DerivedToBase:
606 return "DerivedToBase";
John McCall23cba802010-03-30 23:58:03 +0000607 case CastExpr::CK_UncheckedDerivedToBase:
608 return "UncheckedDerivedToBase";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000609 case CastExpr::CK_Dynamic:
610 return "Dynamic";
611 case CastExpr::CK_ToUnion:
612 return "ToUnion";
613 case CastExpr::CK_ArrayToPointerDecay:
614 return "ArrayToPointerDecay";
615 case CastExpr::CK_FunctionToPointerDecay:
616 return "FunctionToPointerDecay";
617 case CastExpr::CK_NullToMemberPointer:
618 return "NullToMemberPointer";
619 case CastExpr::CK_BaseToDerivedMemberPointer:
620 return "BaseToDerivedMemberPointer";
Anders Carlsson1a31a182009-10-30 00:46:35 +0000621 case CastExpr::CK_DerivedToBaseMemberPointer:
622 return "DerivedToBaseMemberPointer";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000623 case CastExpr::CK_UserDefinedConversion:
624 return "UserDefinedConversion";
625 case CastExpr::CK_ConstructorConversion:
626 return "ConstructorConversion";
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000627 case CastExpr::CK_IntegralToPointer:
628 return "IntegralToPointer";
629 case CastExpr::CK_PointerToIntegral:
630 return "PointerToIntegral";
Anders Carlssonebeaf202009-10-16 02:35:04 +0000631 case CastExpr::CK_ToVoid:
632 return "ToVoid";
Anders Carlsson16a89042009-10-16 05:23:41 +0000633 case CastExpr::CK_VectorSplat:
634 return "VectorSplat";
Anders Carlsson82debc72009-10-18 18:12:03 +0000635 case CastExpr::CK_IntegralCast:
636 return "IntegralCast";
637 case CastExpr::CK_IntegralToFloating:
638 return "IntegralToFloating";
639 case CastExpr::CK_FloatingToIntegral:
640 return "FloatingToIntegral";
Benjamin Kramerc6b29162009-10-18 19:02:15 +0000641 case CastExpr::CK_FloatingCast:
642 return "FloatingCast";
Anders Carlssonbc0e0782009-11-23 20:04:44 +0000643 case CastExpr::CK_MemberPointerToBoolean:
644 return "MemberPointerToBoolean";
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +0000645 case CastExpr::CK_AnyPointerToObjCPointerCast:
646 return "AnyPointerToObjCPointerCast";
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +0000647 case CastExpr::CK_AnyPointerToBlockPointerCast:
648 return "AnyPointerToBlockPointerCast";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000649 }
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000651 assert(0 && "Unhandled cast kind!");
652 return 0;
653}
654
Anders Carlssona3bdded2010-04-23 21:02:34 +0000655void CastExpr::DoDestroy(ASTContext &C)
656{
Anders Carlssonf1b48b72010-04-24 16:57:13 +0000657 BasePath.Destroy();
Anders Carlssona3bdded2010-04-23 21:02:34 +0000658 Expr::DoDestroy(C);
659}
660
Douglas Gregor6eef5192009-12-14 19:27:10 +0000661Expr *CastExpr::getSubExprAsWritten() {
662 Expr *SubExpr = 0;
663 CastExpr *E = this;
664 do {
665 SubExpr = E->getSubExpr();
666
667 // Skip any temporary bindings; they're implicit.
668 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
669 SubExpr = Binder->getSubExpr();
670
671 // Conversions by constructor and conversion functions have a
672 // subexpression describing the call; strip it off.
673 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
674 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
675 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
676 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
677
678 // If the subexpression we're left with is an implicit cast, look
679 // through that, too.
680 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
681
682 return SubExpr;
683}
684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
686/// corresponds to, e.g. "<<=".
687const char *BinaryOperator::getOpcodeStr(Opcode Op) {
688 switch (Op) {
Douglas Gregorbaf53482009-03-12 22:51:37 +0000689 case PtrMemD: return ".*";
690 case PtrMemI: return "->*";
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 case Mul: return "*";
692 case Div: return "/";
693 case Rem: return "%";
694 case Add: return "+";
695 case Sub: return "-";
696 case Shl: return "<<";
697 case Shr: return ">>";
698 case LT: return "<";
699 case GT: return ">";
700 case LE: return "<=";
701 case GE: return ">=";
702 case EQ: return "==";
703 case NE: return "!=";
704 case And: return "&";
705 case Xor: return "^";
706 case Or: return "|";
707 case LAnd: return "&&";
708 case LOr: return "||";
709 case Assign: return "=";
710 case MulAssign: return "*=";
711 case DivAssign: return "/=";
712 case RemAssign: return "%=";
713 case AddAssign: return "+=";
714 case SubAssign: return "-=";
715 case ShlAssign: return "<<=";
716 case ShrAssign: return ">>=";
717 case AndAssign: return "&=";
718 case XorAssign: return "^=";
719 case OrAssign: return "|=";
720 case Comma: return ",";
721 }
Douglas Gregorbaf53482009-03-12 22:51:37 +0000722
723 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +0000724}
725
Mike Stump1eb44332009-09-09 15:08:12 +0000726BinaryOperator::Opcode
Douglas Gregor063daf62009-03-13 18:40:31 +0000727BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
728 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +0000729 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor063daf62009-03-13 18:40:31 +0000730 case OO_Plus: return Add;
731 case OO_Minus: return Sub;
732 case OO_Star: return Mul;
733 case OO_Slash: return Div;
734 case OO_Percent: return Rem;
735 case OO_Caret: return Xor;
736 case OO_Amp: return And;
737 case OO_Pipe: return Or;
738 case OO_Equal: return Assign;
739 case OO_Less: return LT;
740 case OO_Greater: return GT;
741 case OO_PlusEqual: return AddAssign;
742 case OO_MinusEqual: return SubAssign;
743 case OO_StarEqual: return MulAssign;
744 case OO_SlashEqual: return DivAssign;
745 case OO_PercentEqual: return RemAssign;
746 case OO_CaretEqual: return XorAssign;
747 case OO_AmpEqual: return AndAssign;
748 case OO_PipeEqual: return OrAssign;
749 case OO_LessLess: return Shl;
750 case OO_GreaterGreater: return Shr;
751 case OO_LessLessEqual: return ShlAssign;
752 case OO_GreaterGreaterEqual: return ShrAssign;
753 case OO_EqualEqual: return EQ;
754 case OO_ExclaimEqual: return NE;
755 case OO_LessEqual: return LE;
756 case OO_GreaterEqual: return GE;
757 case OO_AmpAmp: return LAnd;
758 case OO_PipePipe: return LOr;
759 case OO_Comma: return Comma;
760 case OO_ArrowStar: return PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +0000761 }
762}
763
764OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
765 static const OverloadedOperatorKind OverOps[] = {
766 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
767 OO_Star, OO_Slash, OO_Percent,
768 OO_Plus, OO_Minus,
769 OO_LessLess, OO_GreaterGreater,
770 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
771 OO_EqualEqual, OO_ExclaimEqual,
772 OO_Amp,
773 OO_Caret,
774 OO_Pipe,
775 OO_AmpAmp,
776 OO_PipePipe,
777 OO_Equal, OO_StarEqual,
778 OO_SlashEqual, OO_PercentEqual,
779 OO_PlusEqual, OO_MinusEqual,
780 OO_LessLessEqual, OO_GreaterGreaterEqual,
781 OO_AmpEqual, OO_CaretEqual,
782 OO_PipeEqual,
783 OO_Comma
784 };
785 return OverOps[Opc];
786}
787
Ted Kremenek709210f2010-04-13 23:39:13 +0000788InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +0000789 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +0000790 SourceLocation rbraceloc)
Douglas Gregor73460a32009-11-19 23:25:22 +0000791 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +0000792 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +0000793 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Ted Kremenekba7bc552010-02-19 01:50:18 +0000794 UnionFieldInit(0), HadArrayRangeDesignator(false)
795{
796 for (unsigned I = 0; I != numInits; ++I) {
797 if (initExprs[I]->isTypeDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000798 TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +0000799 if (initExprs[I]->isValueDependent())
Douglas Gregor73460a32009-11-19 23:25:22 +0000800 ValueDependent = true;
801 }
Ted Kremenekba7bc552010-02-19 01:50:18 +0000802
Ted Kremenek709210f2010-04-13 23:39:13 +0000803 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000804}
Reid Spencer5f016e22007-07-11 17:01:13 +0000805
Ted Kremenek709210f2010-04-13 23:39:13 +0000806void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000807 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +0000808 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +0000809}
810
Ted Kremenek709210f2010-04-13 23:39:13 +0000811void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000812 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
813 Idx < LastIdx; ++Idx)
Ted Kremenek709210f2010-04-13 23:39:13 +0000814 InitExprs[Idx]->Destroy(C);
815 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +0000816}
817
Ted Kremenek709210f2010-04-13 23:39:13 +0000818Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +0000819 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +0000820 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +0000821 InitExprs.back() = expr;
822 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregor4c678342009-01-28 21:54:33 +0000825 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
826 InitExprs[Init] = expr;
827 return Result;
828}
829
Steve Naroffbfdcae62008-09-04 15:31:07 +0000830/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +0000831///
832const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +0000833 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +0000834 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +0000835}
836
Mike Stump1eb44332009-09-09 15:08:12 +0000837SourceLocation BlockExpr::getCaretLocation() const {
838 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +0000839}
Mike Stump1eb44332009-09-09 15:08:12 +0000840const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +0000841 return TheBlock->getBody();
842}
Mike Stump1eb44332009-09-09 15:08:12 +0000843Stmt *BlockExpr::getBody() {
844 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +0000845}
Steve Naroff56ee6892008-10-08 17:01:13 +0000846
847
Reid Spencer5f016e22007-07-11 17:01:13 +0000848//===----------------------------------------------------------------------===//
849// Generic Expression Routines
850//===----------------------------------------------------------------------===//
851
Chris Lattner026dc962009-02-14 07:37:35 +0000852/// isUnusedResultAWarning - Return true if this immediate expression should
853/// be warned about if the result is unused. If so, fill in Loc and Ranges
854/// with location to warn on and the source range[s] to report with the
855/// warning.
856bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +0000857 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +0000858 // Don't warn if the expr is type dependent. The type could end up
859 // instantiating to void.
860 if (isTypeDependent())
861 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 switch (getStmtClass()) {
864 default:
John McCall0faede62010-03-12 07:11:26 +0000865 if (getType()->isVoidType())
866 return false;
Chris Lattner026dc962009-02-14 07:37:35 +0000867 Loc = getExprLoc();
868 R1 = getSourceRange();
869 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000871 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +0000872 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 case UnaryOperatorClass: {
874 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000877 default: break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 case UnaryOperator::PostInc:
879 case UnaryOperator::PostDec:
880 case UnaryOperator::PreInc:
Chris Lattner026dc962009-02-14 07:37:35 +0000881 case UnaryOperator::PreDec: // ++/--
882 return false; // Not a warning.
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 case UnaryOperator::Deref:
884 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000885 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000886 return false;
887 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 case UnaryOperator::Real:
889 case UnaryOperator::Imag:
890 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000891 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
892 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000893 return false;
894 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 case UnaryOperator::Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +0000896 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000897 }
Chris Lattner026dc962009-02-14 07:37:35 +0000898 Loc = UO->getOperatorLoc();
899 R1 = UO->getSubExpr()->getSourceRange();
900 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000902 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000903 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +0000904 switch (BO->getOpcode()) {
905 default:
906 break;
907 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
908 case BinaryOperator::Comma:
909 // ((foo = <blah>), 0) is an idiom for hiding the result (and
910 // lvalue-ness) of an assignment written in a macro.
911 if (IntegerLiteral *IE =
912 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
913 if (IE->getValue() == 0)
914 return false;
915 case BinaryOperator::LAnd:
916 case BinaryOperator::LOr:
917 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
918 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCallbf0ee352010-02-16 04:10:53 +0000919 }
Chris Lattner026dc962009-02-14 07:37:35 +0000920 if (BO->isAssignmentOp())
921 return false;
922 Loc = BO->getOperatorLoc();
923 R1 = BO->getLHS()->getSourceRange();
924 R2 = BO->getRHS()->getSourceRange();
925 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +0000926 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000927 case CompoundAssignOperatorClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000928 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000929
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000930 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000931 // The condition must be evaluated, but if either the LHS or RHS is a
932 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000933 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +0000934 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +0000935 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +0000936 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +0000937 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000938 }
939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +0000941 // If the base pointer or element is to a volatile pointer/field, accessing
942 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000943 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000944 return false;
945 Loc = cast<MemberExpr>(this)->getMemberLoc();
946 R1 = SourceRange(Loc, Loc);
947 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
948 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 case ArraySubscriptExprClass:
951 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +0000952 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +0000953 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +0000954 return false;
955 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
956 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
957 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
958 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +0000959
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +0000961 case CXXOperatorCallExprClass:
962 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +0000963 // If this is a direct call, get the callee.
964 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +0000965 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +0000966 // If the callee has attribute pure, const, or warn_unused_result, warn
967 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000968 //
969 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
970 // updated to match for QoI.
971 if (FD->getAttr<WarnUnusedResultAttr>() ||
972 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
973 Loc = CE->getCallee()->getLocStart();
974 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattnerbc8d42c2009-10-13 04:53:48 +0000976 if (unsigned NumArgs = CE->getNumArgs())
977 R2 = SourceRange(CE->getArg(0)->getLocStart(),
978 CE->getArg(NumArgs-1)->getLocEnd());
979 return true;
980 }
Chris Lattner026dc962009-02-14 07:37:35 +0000981 }
982 return false;
983 }
Anders Carlsson58beed92009-11-17 17:11:23 +0000984
985 case CXXTemporaryObjectExprClass:
986 case CXXConstructExprClass:
987 return false;
988
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000989 case ObjCMessageExprClass: {
990 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
991 const ObjCMethodDecl *MD = ME->getMethodDecl();
992 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
993 Loc = getExprLoc();
994 return true;
995 }
Chris Lattner026dc962009-02-14 07:37:35 +0000996 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +0000997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000999 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnera50089e2009-08-16 16:45:18 +00001000#if 0
Mike Stump1eb44332009-09-09 15:08:12 +00001001 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001002 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnera50089e2009-08-16 16:45:18 +00001003 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian154440e2009-08-18 20:50:23 +00001004 Loc = Ref->getLocation();
1005 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1006 if (Ref->getBase())
1007 R2 = Ref->getBase()->getSourceRange();
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001008#else
1009 Loc = getExprLoc();
1010 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001011#endif
1012 return true;
1013 }
Chris Lattner611b2ec2008-07-26 19:51:01 +00001014 case StmtExprClass: {
1015 // Statement exprs don't logically have side effects themselves, but are
1016 // sometimes used in macros in ways that give them a type that is unused.
1017 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1018 // however, if the result of the stmt expr is dead, we don't want to emit a
1019 // warning.
1020 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1021 if (!CS->body_empty())
1022 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001023 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
John McCall0faede62010-03-12 07:11:26 +00001025 if (getType()->isVoidType())
1026 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001027 Loc = cast<StmtExpr>(this)->getLParenLoc();
1028 R1 = getSourceRange();
1029 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001030 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001031 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001032 // If this is an explicit cast to void, allow it. People do this when they
1033 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001034 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001035 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001036 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1037 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1038 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001039 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001040 if (getType()->isVoidType())
1041 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001042 const CastExpr *CE = cast<CastExpr>(this);
1043
1044 // If this is a cast to void or a constructor conversion, check the operand.
1045 // Otherwise, the result of the cast is unused.
1046 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1047 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001048 return (cast<CastExpr>(this)->getSubExpr()
1049 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001050 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1051 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1052 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Eli Friedman4be1f472008-05-19 21:24:43 +00001055 case ImplicitCastExprClass:
1056 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001057 return (cast<ImplicitCastExpr>(this)
1058 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001059
Chris Lattner04421082008-04-08 04:40:51 +00001060 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001061 return (cast<CXXDefaultArgExpr>(this)
1062 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001063
1064 case CXXNewExprClass:
1065 // FIXME: In theory, there might be new expressions that don't have side
1066 // effects (e.g. a placement new with an uninitialized POD).
1067 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001068 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001069 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001070 return (cast<CXXBindTemporaryExpr>(this)
1071 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001072 case CXXExprWithTemporariesClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001073 return (cast<CXXExprWithTemporaries>(this)
1074 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001075 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
Douglas Gregorba7e2102008-10-22 15:04:37 +00001078/// DeclCanBeLvalue - Determine whether the given declaration can be
1079/// an lvalue. This is a helper routine for isLvalue.
1080static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001081 // C++ [temp.param]p6:
1082 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump1eb44332009-09-09 15:08:12 +00001083 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor72c3f312008-12-05 18:15:24 +00001084 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1085 return NTTParm->getType()->isReferenceType();
1086
Douglas Gregor44b43212008-12-11 16:49:14 +00001087 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregorba7e2102008-10-22 15:04:37 +00001088 // C++ 3.10p2: An lvalue refers to an object or function.
1089 (Ctx.getLangOptions().CPlusPlus &&
John McCall51fa86f2009-12-02 08:47:38 +00001090 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregorba7e2102008-10-22 15:04:37 +00001091}
1092
Reid Spencer5f016e22007-07-11 17:01:13 +00001093/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1094/// incomplete type other than void. Nonarray expressions that can be lvalues:
1095/// - name, where name must be a variable
1096/// - e[i]
1097/// - (e), where e must be an lvalue
1098/// - e.name, where e must be an lvalue
1099/// - e->name
1100/// - *e, the type of e cannot be a function type
1101/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +00001102/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +00001103/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +00001104///
Chris Lattner28be73f2008-07-26 21:30:36 +00001105Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedman53202852009-05-03 22:36:05 +00001106 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1107
1108 isLvalueResult Res = isLvalueInternal(Ctx);
1109 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1110 return Res;
1111
Douglas Gregor98cd5992008-10-21 23:43:52 +00001112 // first, check the type (C99 6.3.2.1). Expressions with function
1113 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001114 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Reid Spencer5f016e22007-07-11 17:01:13 +00001115 return LV_NotObjectType;
1116
Steve Naroffacb818a2008-02-10 01:39:04 +00001117 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall0953e762009-09-24 19:53:00 +00001118 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroffacb818a2008-02-10 01:39:04 +00001119 return LV_IncompleteVoidType;
1120
Eli Friedman53202852009-05-03 22:36:05 +00001121 return LV_Valid;
1122}
Bill Wendling08ad47c2007-07-17 03:52:31 +00001123
Eli Friedman53202852009-05-03 22:36:05 +00001124// Check whether the expression can be sanely treated like an l-value
1125Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 switch (getStmtClass()) {
Fariborz Jahanian820bca42009-12-09 23:35:29 +00001127 case ObjCIsaExprClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001128 case StringLiteralClass: // C99 6.5.1p4
1129 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7323a622007-11-30 22:47:59 +00001130 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
1132 // For vectors, make sure base is an lvalue (i.e. not a function call).
1133 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner28be73f2008-07-26 21:30:36 +00001134 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 return LV_Valid;
Douglas Gregora2813ce2009-10-23 18:54:35 +00001136 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregorba7e2102008-10-22 15:04:37 +00001137 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1138 if (DeclCanBeLvalue(RefdDecl, Ctx))
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 return LV_Valid;
1140 break;
Chris Lattner41110242008-06-17 18:05:57 +00001141 }
Steve Naroffdd972f22008-09-05 22:11:13 +00001142 case BlockDeclRefExprClass: {
1143 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff4f6a7d72008-09-26 14:41:28 +00001144 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffdd972f22008-09-05 22:11:13 +00001145 return LV_Valid;
1146 break;
1147 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001148 case MemberExprClass: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor86f19402008-12-20 23:49:58 +00001150 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1151 NamedDecl *Member = m->getMemberDecl();
1152 // C++ [expr.ref]p4:
1153 // If E2 is declared to have type "reference to T", then E1.E2
1154 // is an lvalue.
1155 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1156 if (Value->getType()->isReferenceType())
1157 return LV_Valid;
1158
1159 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor2d2e9cf2009-03-11 20:22:50 +00001160 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor86f19402008-12-20 23:49:58 +00001161 return LV_Valid;
1162
1163 // -- If E2 is a non-static data member [...]. If E1 is an
1164 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001165 if (isa<FieldDecl>(Member)) {
1166 if (m->isArrow())
1167 return LV_Valid;
Fariborz Jahanian2d901df2010-02-12 21:02:28 +00001168 return m->getBase()->isLvalue(Ctx);
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001169 }
Douglas Gregor86f19402008-12-20 23:49:58 +00001170
1171 // -- If it refers to a static member function [...], then
1172 // E1.E2 is an lvalue.
1173 // -- Otherwise, if E1.E2 refers to a non-static member
1174 // function [...], then E1.E2 is not an lvalue.
1175 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1176 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1177
1178 // -- If E2 is a member enumerator [...], the expression E1.E2
1179 // is not an lvalue.
1180 if (isa<EnumConstantDecl>(Member))
1181 return LV_InvalidExpression;
1182
1183 // Not an lvalue.
1184 return LV_InvalidExpression;
Mike Stump1eb44332009-09-09 15:08:12 +00001185 }
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001186
Douglas Gregor86f19402008-12-20 23:49:58 +00001187 // C99 6.5.2.3p4
Fariborz Jahanian2514a302009-12-15 23:59:41 +00001188 if (m->isArrow())
1189 return LV_Valid;
1190 Expr *BaseExp = m->getBase();
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001191 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1192 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001193 return LV_SubObjCPropertySetting;
1194 return
Fariborz Jahanian90c71262010-03-18 18:50:41 +00001195 BaseExp->isLvalue(Ctx);
Anton Korobeynikovfdd75662007-07-12 15:26:50 +00001196 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001197 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +00001199 return LV_Valid; // C99 6.5.3p4
1200
1201 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerbaf0d662008-07-25 18:07:19 +00001202 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1203 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner28be73f2008-07-26 21:30:36 +00001204 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregor74253732008-11-19 15:42:04 +00001205
1206 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1207 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1208 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1209 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 break;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001211 case ImplicitCastExprClass:
Douglas Gregore873fb72010-02-16 21:39:57 +00001212 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1213 return LV_Valid;
1214
1215 // If this is a conversion to a class temporary, make a note of
1216 // that.
1217 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1218 return LV_ClassTemporary;
1219
1220 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 case ParenExprClass: // C99 6.5.1p5
Chris Lattner28be73f2008-07-26 21:30:36 +00001222 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001223 case BinaryOperatorClass:
1224 case CompoundAssignOperatorClass: {
1225 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor337c6b92008-11-19 17:17:41 +00001226
1227 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1228 BinOp->getOpcode() == BinaryOperator::Comma)
1229 return BinOp->getRHS()->isLvalue(Ctx);
1230
Sebastian Redl22460502009-02-07 00:15:38 +00001231 // C++ [expr.mptr.oper]p6
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001232 // The result of a .* expression is an lvalue only if its first operand is
1233 // an lvalue and its second operand is a pointer to data member.
1234 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl22460502009-02-07 00:15:38 +00001235 !BinOp->getType()->isFunctionType())
1236 return BinOp->getLHS()->isLvalue(Ctx);
1237
Fariborz Jahanian27d4be52009-10-08 18:00:39 +00001238 // The result of an ->* expression is an lvalue only if its second operand
1239 // is a pointer to data member.
1240 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1241 !BinOp->getType()->isFunctionType()) {
1242 QualType Ty = BinOp->getRHS()->getType();
1243 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1244 return LV_Valid;
1245 }
1246
Douglas Gregorbf3af052008-11-13 20:12:29 +00001247 if (!BinOp->isAssignmentOp())
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001248 return LV_InvalidExpression;
1249
Douglas Gregorbf3af052008-11-13 20:12:29 +00001250 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump1eb44332009-09-09 15:08:12 +00001251 // C++ [expr.ass]p1:
Douglas Gregorbf3af052008-11-13 20:12:29 +00001252 // The result of an assignment operation [...] is an lvalue.
1253 return LV_Valid;
1254
1255
1256 // C99 6.5.16:
1257 // An assignment expression [...] is not an lvalue.
1258 return LV_InvalidExpression;
Douglas Gregoreb8f3062008-11-12 17:17:38 +00001259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260 case CallExprClass:
Douglas Gregor88a35142008-12-22 05:46:06 +00001261 case CXXOperatorCallExprClass:
1262 case CXXMemberCallExprClass: {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001263 // C++0x [expr.call]p10
Douglas Gregor9d293df2008-10-28 00:22:11 +00001264 // A function call is an lvalue if and only if the result type
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001265 // is an lvalue reference.
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001266 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1267 if (ReturnType->isLValueReferenceType())
1268 return LV_Valid;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001269
Douglas Gregore873fb72010-02-16 21:39:57 +00001270 // If the function is returning a class temporary, make a note of
1271 // that.
1272 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1273 return LV_ClassTemporary;
1274
Douglas Gregor9d293df2008-10-28 00:22:11 +00001275 break;
1276 }
Steve Naroffe6386392007-12-05 04:00:10 +00001277 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregore873fb72010-02-16 21:39:57 +00001278 // FIXME: Is this what we want in C++?
Steve Naroffe6386392007-12-05 04:00:10 +00001279 return LV_Valid;
Chris Lattner670a62c2008-12-12 05:35:08 +00001280 case ChooseExprClass:
1281 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedman79769322009-03-04 05:52:32 +00001282 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begeman213541a2008-04-18 23:10:10 +00001283 case ExtVectorElementExprClass:
1284 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +00001285 return LV_DuplicateVectorComponents;
1286 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +00001287 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1288 return LV_Valid;
Steve Naroff799a6a62008-05-30 23:23:16 +00001289 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1290 return LV_Valid;
Enea Zaffanella049c51e2010-04-27 07:38:32 +00001291 case ObjCImplicitSetterGetterRefExprClass:
1292 // FIXME: check if read-only property.
Chris Lattner670a62c2008-12-12 05:35:08 +00001293 return LV_Valid;
Chris Lattnerd9f69102008-08-10 01:53:14 +00001294 case PredefinedExprClass:
Douglas Gregor796da182008-11-04 14:32:21 +00001295 return LV_Valid;
John McCallba135432009-11-21 08:51:07 +00001296 case UnresolvedLookupExprClass:
1297 return LV_Valid;
Chris Lattner04421082008-04-08 04:40:51 +00001298 case CXXDefaultArgExprClass:
Chris Lattner28be73f2008-07-26 21:30:36 +00001299 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001300 case CStyleCastExprClass:
Douglas Gregor9d293df2008-10-28 00:22:11 +00001301 case CXXFunctionalCastExprClass:
1302 case CXXStaticCastExprClass:
1303 case CXXDynamicCastExprClass:
1304 case CXXReinterpretCastExprClass:
1305 case CXXConstCastExprClass:
1306 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001307 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor9d293df2008-10-28 00:22:11 +00001308 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1309 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001310 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1311 isLValueReferenceType())
Douglas Gregor9d293df2008-10-28 00:22:11 +00001312 return LV_Valid;
Douglas Gregore873fb72010-02-16 21:39:57 +00001313
1314 // If this is a conversion to a class temporary, make a note of
1315 // that.
1316 if (Ctx.getLangOptions().CPlusPlus &&
1317 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1318 return LV_ClassTemporary;
1319
Douglas Gregor9d293df2008-10-28 00:22:11 +00001320 break;
Sebastian Redlc42e1182008-11-11 11:37:55 +00001321 case CXXTypeidExprClass:
1322 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1323 return LV_Valid;
Anders Carlsson6f680272009-08-16 03:42:12 +00001324 case CXXBindTemporaryExprClass:
1325 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1326 isLvalueInternal(Ctx);
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001327 case CXXBindReferenceExprClass:
1328 // Something that's bound to a reference is always an lvalue.
1329 return LV_Valid;
Sebastian Redl76458502009-04-17 16:30:52 +00001330 case ConditionalOperatorClass: {
1331 // Complicated handling is only for C++.
1332 if (!Ctx.getLangOptions().CPlusPlus)
1333 return LV_InvalidExpression;
1334
1335 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1336 // everywhere there's an object converted to an rvalue. Also, any other
1337 // casts should be wrapped by ImplicitCastExprs. There's just the special
1338 // case involving throws to work out.
1339 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001340 Expr *True = Cond->getTrueExpr();
1341 Expr *False = Cond->getFalseExpr();
Sebastian Redl76458502009-04-17 16:30:52 +00001342 // C++0x 5.16p2
1343 // If either the second or the third operand has type (cv) void, [...]
1344 // the result [...] is an rvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001345 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl76458502009-04-17 16:30:52 +00001346 return LV_InvalidExpression;
1347
1348 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregord5f3a0f2009-05-19 20:13:50 +00001349 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl76458502009-04-17 16:30:52 +00001350 return LV_InvalidExpression;
1351
1352 // That's it.
1353 return LV_Valid;
1354 }
1355
Douglas Gregor2d48e782009-12-19 07:07:47 +00001356 case Expr::CXXExprWithTemporariesClass:
1357 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1358
1359 case Expr::ObjCMessageExprClass:
1360 if (const ObjCMethodDecl *Method
1361 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1362 if (Method->getResultType()->isLValueReferenceType())
1363 return LV_Valid;
1364 break;
1365
Douglas Gregore873fb72010-02-16 21:39:57 +00001366 case Expr::CXXConstructExprClass:
1367 case Expr::CXXTemporaryObjectExprClass:
1368 case Expr::CXXZeroInitValueExprClass:
1369 return LV_ClassTemporary;
1370
Reid Spencer5f016e22007-07-11 17:01:13 +00001371 default:
1372 break;
1373 }
1374 return LV_InvalidExpression;
1375}
1376
1377/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1378/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump1eb44332009-09-09 15:08:12 +00001379/// if it is a structure or union, does not have any member (including,
Reid Spencer5f016e22007-07-11 17:01:13 +00001380/// recursively, any member or element of all contained aggregates or unions)
1381/// with a const-qualified type.
Mike Stump1eb44332009-09-09 15:08:12 +00001382Expr::isModifiableLvalueResult
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001383Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner28be73f2008-07-26 21:30:36 +00001384 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 switch (lvalResult) {
Mike Stump1eb44332009-09-09 15:08:12 +00001387 case LV_Valid:
Douglas Gregorae8d4672008-10-22 00:03:08 +00001388 // C++ 3.10p11: Functions cannot be modified, but pointers to
1389 // functions can be modifiable.
1390 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1391 return MLV_NotObjectType;
1392 break;
1393
Reid Spencer5f016e22007-07-11 17:01:13 +00001394 case LV_NotObjectType: return MLV_NotObjectType;
1395 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +00001396 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattnerca354fa2008-11-17 19:51:54 +00001397 case LV_InvalidExpression:
1398 // If the top level is a C-style cast, and the subexpression is a valid
1399 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1400 // GCC extension. We don't support it, but we want to produce good
1401 // diagnostics when it happens so that the user knows why.
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001402 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1403 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1404 if (Loc)
1405 *Loc = CE->getLParenLoc();
Chris Lattnerca354fa2008-11-17 19:51:54 +00001406 return MLV_LValueCast;
Daniel Dunbar44e35f72009-04-15 00:08:05 +00001407 }
1408 }
Chris Lattnerca354fa2008-11-17 19:51:54 +00001409 return MLV_InvalidExpression;
Douglas Gregor86f19402008-12-20 23:49:58 +00001410 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahaniane9ff4432010-02-11 01:11:34 +00001411 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregore873fb72010-02-16 21:39:57 +00001412 case LV_ClassTemporary:
1413 return MLV_ClassTemporary;
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 }
Eli Friedman04831aa2009-03-22 23:26:56 +00001415
1416 // The following is illegal:
1417 // void takeclosure(void (^C)(void));
1418 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1419 //
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001420 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedman04831aa2009-03-22 23:26:56 +00001421 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1422 return MLV_NotBlockQualified;
1423 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001424
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001425 // Assigning to an 'implicit' property?
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001426 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahanianc3f48cd2009-09-14 16:40:48 +00001427 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1428 if (Expr->getSetterMethod() == 0)
1429 return MLV_NoSetterProperty;
1430 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001431
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001432 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001434 if (CT.isConstQualified())
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 return MLV_ConstQualified;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001436 if (CT->isArrayType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001437 return MLV_ArrayType;
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001438 if (CT->isIncompleteType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 return MLV_IncompleteType;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Ted Kremenek6217b802009-07-29 21:53:49 +00001441 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001442 if (r->hasConstFields())
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 return MLV_ConstQualified;
1444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Mike Stump1eb44332009-09-09 15:08:12 +00001446 return MLV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +00001447}
1448
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001449/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001450/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001451bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001452 switch (getStmtClass()) {
1453 default:
1454 return false;
1455 case ObjCIvarRefExprClass:
1456 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001457 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001458 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001459 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001460 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001461 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001462 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001463 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001464 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001465 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001466 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001467 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1468 if (VD->hasGlobalStorage())
1469 return true;
1470 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001471 // dereferencing to a pointer is always a gc'able candidate,
1472 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001473 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001474 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001475 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001476 return false;
1477 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001478 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001479 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001480 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001481 }
1482 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001483 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001484 }
1485}
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001486Expr* Expr::IgnoreParens() {
1487 Expr* E = this;
1488 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1489 E = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001491 return E;
1492}
1493
Chris Lattner56f34942008-02-13 01:02:39 +00001494/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1495/// or CastExprs or ImplicitCastExprs, returning their operand.
1496Expr *Expr::IgnoreParenCasts() {
1497 Expr *E = this;
1498 while (true) {
1499 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1500 E = P->getSubExpr();
1501 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1502 E = P->getSubExpr();
Chris Lattner56f34942008-02-13 01:02:39 +00001503 else
1504 return E;
1505 }
1506}
1507
Chris Lattnerecdd8412009-03-13 17:28:01 +00001508/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1509/// value (including ptr->int casts of the same size). Strip off any
1510/// ParenExpr or CastExprs, returning their operand.
1511Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1512 Expr *E = this;
1513 while (true) {
1514 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1515 E = P->getSubExpr();
1516 continue;
1517 }
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Chris Lattnerecdd8412009-03-13 17:28:01 +00001519 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1520 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1521 // ptr<->int casts of the same width. We also ignore all identify casts.
1522 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Chris Lattnerecdd8412009-03-13 17:28:01 +00001524 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1525 E = SE;
1526 continue;
1527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Chris Lattnerecdd8412009-03-13 17:28:01 +00001529 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1530 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1531 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1532 E = SE;
1533 continue;
1534 }
1535 }
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Chris Lattnerecdd8412009-03-13 17:28:01 +00001537 return E;
1538 }
1539}
1540
Douglas Gregor6eef5192009-12-14 19:27:10 +00001541bool Expr::isDefaultArgument() const {
1542 const Expr *E = this;
1543 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1544 E = ICE->getSubExprAsWritten();
1545
1546 return isa<CXXDefaultArgExpr>(E);
1547}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001548
Douglas Gregor2f599792010-04-02 18:24:57 +00001549/// \brief Skip over any no-op casts and any temporary-binding
1550/// expressions.
1551static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1552 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1553 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1554 E = ICE->getSubExpr();
1555 else
1556 break;
1557 }
1558
1559 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1560 E = BE->getSubExpr();
1561
1562 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1563 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1564 E = ICE->getSubExpr();
1565 else
1566 break;
1567 }
1568
1569 return E;
1570}
1571
1572const Expr *Expr::getTemporaryObject() const {
1573 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1574
1575 // A cast can produce a temporary object. The object's construction
1576 // is represented as a CXXConstructExpr.
1577 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1578 // Only user-defined and constructor conversions can produce
1579 // temporary objects.
1580 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1581 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1582 return 0;
1583
1584 // Strip off temporary bindings and no-op casts.
1585 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1586
1587 // If this is a constructor conversion, see if we have an object
1588 // construction.
1589 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1590 return dyn_cast<CXXConstructExpr>(Sub);
1591
1592 // If this is a user-defined conversion, see if we have a call to
1593 // a function that itself returns a temporary object.
1594 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1595 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1596 if (CE->getCallReturnType()->isRecordType())
1597 return CE;
1598
1599 return 0;
1600 }
1601
1602 // A call returning a class type returns a temporary.
1603 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1604 if (CE->getCallReturnType()->isRecordType())
1605 return CE;
1606
1607 return 0;
1608 }
1609
1610 // Explicit temporary object constructors create temporaries.
1611 return dyn_cast<CXXTemporaryObjectExpr>(E);
1612}
1613
Douglas Gregor898574e2008-12-05 23:32:09 +00001614/// hasAnyTypeDependentArguments - Determines if any of the expressions
1615/// in Exprs is type-dependent.
1616bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1617 for (unsigned I = 0; I < NumExprs; ++I)
1618 if (Exprs[I]->isTypeDependent())
1619 return true;
1620
1621 return false;
1622}
1623
1624/// hasAnyValueDependentArguments - Determines if any of the expressions
1625/// in Exprs is value-dependent.
1626bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1627 for (unsigned I = 0; I < NumExprs; ++I)
1628 if (Exprs[I]->isValueDependent())
1629 return true;
1630
1631 return false;
1632}
1633
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001634bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001635 // This function is attempting whether an expression is an initializer
1636 // which can be evaluated at compile-time. isEvaluatable handles most
1637 // of the cases, but it can't deal with some initializer-specific
1638 // expressions, and it can't deal with aggregates; we deal with those here,
1639 // and fall back to isEvaluatable for the other cases.
1640
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001641 // FIXME: This function assumes the variable being assigned to
1642 // isn't a reference type!
1643
Anders Carlssone8a32b82008-11-24 05:23:59 +00001644 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001645 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001646 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00001647 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001648 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00001649 return true;
Nate Begeman59b5da62009-01-18 03:20:47 +00001650 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001651 // This handles gcc's extension that allows global initializers like
1652 // "struct x {int x;} x = (struct x) {};".
1653 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00001654 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedmanc9e8f602009-01-25 02:32:41 +00001655 return Exp->isConstantInitializer(Ctx);
Nate Begeman59b5da62009-01-18 03:20:47 +00001656 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00001657 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00001658 // FIXME: This doesn't deal with fields with reference types correctly.
1659 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1660 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00001661 const InitListExpr *Exp = cast<InitListExpr>(this);
1662 unsigned numInits = Exp->getNumInits();
1663 for (unsigned i = 0; i < numInits; i++) {
Mike Stump1eb44332009-09-09 15:08:12 +00001664 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssone8a32b82008-11-24 05:23:59 +00001665 return false;
1666 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001667 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001668 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00001669 case ImplicitValueInitExprClass:
1670 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00001671 case ParenExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001672 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001673 case UnaryOperatorClass: {
1674 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1675 if (Exp->getOpcode() == UnaryOperator::Extension)
1676 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1677 break;
1678 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00001679 case BinaryOperatorClass: {
1680 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1681 // but this handles the common case.
1682 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1683 if (Exp->getOpcode() == BinaryOperator::Sub &&
1684 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1685 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1686 return true;
1687 break;
1688 }
Chris Lattner81045d82009-04-21 05:19:11 +00001689 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001690 case CStyleCastExprClass:
1691 // Handle casts with a destination that's a struct or union; this
1692 // deals with both the gcc no-op struct cast extension and the
1693 // cast-to-union extension.
1694 if (getType()->isRecordType())
1695 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Chris Lattner430656e2009-10-13 22:12:09 +00001696
1697 // Integer->integer casts can be handled here, which is important for
1698 // things like (int)(&&x-&&y). Scary but true.
1699 if (getType()->isIntegerType() &&
1700 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1701 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
1702
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001703 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00001704 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00001705 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00001706}
1707
Reid Spencer5f016e22007-07-11 17:01:13 +00001708/// isIntegerConstantExpr - this recursive routine will test if an expression is
Eli Friedmane28d7192009-02-26 09:29:13 +00001709/// an integer constant expression.
Reid Spencer5f016e22007-07-11 17:01:13 +00001710
1711/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
1712/// comma, etc
1713///
Chris Lattnerce0afc02007-07-18 05:21:20 +00001714/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
1715/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
1716/// cast+dereference.
Daniel Dunbar2d6744f2009-02-18 00:47:45 +00001717
Eli Friedmane28d7192009-02-26 09:29:13 +00001718// CheckICE - This function does the fundamental ICE checking: the returned
1719// ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation.
1720// Note that to reduce code duplication, this helper does no evaluation
Mike Stump1eb44332009-09-09 15:08:12 +00001721// itself; the caller checks whether the expression is evaluatable, and
Eli Friedmane28d7192009-02-26 09:29:13 +00001722// in the rare cases where CheckICE actually cares about the evaluated
Mike Stump1eb44332009-09-09 15:08:12 +00001723// value, it calls into Evalute.
Eli Friedmane28d7192009-02-26 09:29:13 +00001724//
1725// Meanings of Val:
1726// 0: This expression is an ICE if it can be evaluated by Evaluate.
1727// 1: This expression is not an ICE, but if it isn't evaluated, it's
1728// a legal subexpression for an ICE. This return value is used to handle
1729// the comma operator in C99 mode.
1730// 2: This expression is not an ICE, and is not a legal subexpression for one.
1731
1732struct ICEDiag {
1733 unsigned Val;
1734 SourceLocation Loc;
1735
1736 public:
1737 ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {}
1738 ICEDiag() : Val(0) {}
1739};
1740
1741ICEDiag NoDiag() { return ICEDiag(); }
1742
Eli Friedman60ce9632009-02-27 04:07:58 +00001743static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
1744 Expr::EvalResult EVResult;
1745 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
1746 !EVResult.Val.isInt()) {
1747 return ICEDiag(2, E->getLocStart());
1748 }
1749 return NoDiag();
1750}
1751
Eli Friedmane28d7192009-02-26 09:29:13 +00001752static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
Anders Carlssonc3082412009-03-14 00:33:21 +00001753 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
Eli Friedmane28d7192009-02-26 09:29:13 +00001754 if (!E->getType()->isIntegralType()) {
1755 return ICEDiag(2, E->getLocStart());
Eli Friedmana6afa762008-11-13 06:09:17 +00001756 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001757
1758 switch (E->getStmtClass()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001759#define STMT(Node, Base) case Expr::Node##Class:
1760#define EXPR(Node, Base)
1761#include "clang/AST/StmtNodes.def"
1762 case Expr::PredefinedExprClass:
1763 case Expr::FloatingLiteralClass:
1764 case Expr::ImaginaryLiteralClass:
1765 case Expr::StringLiteralClass:
1766 case Expr::ArraySubscriptExprClass:
1767 case Expr::MemberExprClass:
1768 case Expr::CompoundAssignOperatorClass:
1769 case Expr::CompoundLiteralExprClass:
1770 case Expr::ExtVectorElementExprClass:
1771 case Expr::InitListExprClass:
1772 case Expr::DesignatedInitExprClass:
1773 case Expr::ImplicitValueInitExprClass:
1774 case Expr::ParenListExprClass:
1775 case Expr::VAArgExprClass:
1776 case Expr::AddrLabelExprClass:
1777 case Expr::StmtExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001778 case Expr::CXXMemberCallExprClass:
1779 case Expr::CXXDynamicCastExprClass:
1780 case Expr::CXXTypeidExprClass:
1781 case Expr::CXXNullPtrLiteralExprClass:
1782 case Expr::CXXThisExprClass:
1783 case Expr::CXXThrowExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001784 case Expr::CXXNewExprClass:
1785 case Expr::CXXDeleteExprClass:
1786 case Expr::CXXPseudoDestructorExprClass:
John McCallba135432009-11-21 08:51:07 +00001787 case Expr::UnresolvedLookupExprClass:
John McCall865d4472009-11-19 22:55:06 +00001788 case Expr::DependentScopeDeclRefExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001789 case Expr::CXXConstructExprClass:
1790 case Expr::CXXBindTemporaryExprClass:
Anders Carlssoneb60edf2010-01-29 02:39:32 +00001791 case Expr::CXXBindReferenceExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001792 case Expr::CXXExprWithTemporariesClass:
1793 case Expr::CXXTemporaryObjectExprClass:
1794 case Expr::CXXUnresolvedConstructExprClass:
John McCall865d4472009-11-19 22:55:06 +00001795 case Expr::CXXDependentScopeMemberExprClass:
John McCall129e2df2009-11-30 22:42:35 +00001796 case Expr::UnresolvedMemberExprClass:
Douglas Gregorf2991242009-09-10 23:31:45 +00001797 case Expr::ObjCStringLiteralClass:
1798 case Expr::ObjCEncodeExprClass:
1799 case Expr::ObjCMessageExprClass:
1800 case Expr::ObjCSelectorExprClass:
1801 case Expr::ObjCProtocolExprClass:
1802 case Expr::ObjCIvarRefExprClass:
1803 case Expr::ObjCPropertyRefExprClass:
1804 case Expr::ObjCImplicitSetterGetterRefExprClass:
1805 case Expr::ObjCSuperExprClass:
1806 case Expr::ObjCIsaExprClass:
1807 case Expr::ShuffleVectorExprClass:
1808 case Expr::BlockExprClass:
1809 case Expr::BlockDeclRefExprClass:
1810 case Expr::NoStmtClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001811 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001812
Douglas Gregor043cad22009-09-11 00:18:58 +00001813 case Expr::GNUNullExprClass:
1814 // GCC considers the GNU __null value to be an integral constant expression.
1815 return NoDiag();
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001816
Eli Friedmane28d7192009-02-26 09:29:13 +00001817 case Expr::ParenExprClass:
1818 return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx);
1819 case Expr::IntegerLiteralClass:
1820 case Expr::CharacterLiteralClass:
1821 case Expr::CXXBoolLiteralExprClass:
1822 case Expr::CXXZeroInitValueExprClass:
1823 case Expr::TypesCompatibleExprClass:
1824 case Expr::UnaryTypeTraitExprClass:
1825 return NoDiag();
Mike Stump1eb44332009-09-09 15:08:12 +00001826 case Expr::CallExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001827 case Expr::CXXOperatorCallExprClass: {
1828 const CallExpr *CE = cast<CallExpr>(E);
Eli Friedman60ce9632009-02-27 04:07:58 +00001829 if (CE->isBuiltinCall(Ctx))
1830 return CheckEvalInICE(E, Ctx);
Eli Friedmane28d7192009-02-26 09:29:13 +00001831 return ICEDiag(2, E->getLocStart());
Chris Lattner2eadfb62007-07-15 23:32:58 +00001832 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001833 case Expr::DeclRefExprClass:
Eli Friedmane28d7192009-02-26 09:29:13 +00001834 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl()))
1835 return NoDiag();
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001836 if (Ctx.getLangOptions().CPlusPlus &&
John McCall0953e762009-09-24 19:53:00 +00001837 E->getType().getCVRQualifiers() == Qualifiers::Const) {
John McCallf604a562010-02-24 09:03:18 +00001838 const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl();
1839
1840 // Parameter variables are never constants. Without this check,
1841 // getAnyInitializer() can find a default argument, which leads
1842 // to chaos.
1843 if (isa<ParmVarDecl>(D))
1844 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1845
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001846 // C++ 7.1.5.1p2
1847 // A variable of non-volatile const-qualified integral or enumeration
1848 // type initialized by an ICE can be used in ICEs.
John McCallf604a562010-02-24 09:03:18 +00001849 if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001850 Qualifiers Quals = Ctx.getCanonicalType(Dcl->getType()).getQualifiers();
1851 if (Quals.hasVolatile() || !Quals.hasConst())
1852 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1853
Sebastian Redl31310a22010-02-01 20:16:42 +00001854 // Look for a declaration of this variable that has an initializer.
1855 const VarDecl *ID = 0;
1856 const Expr *Init = Dcl->getAnyInitializer(ID);
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001857 if (Init) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001858 if (ID->isInitKnownICE()) {
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001859 // We have already checked whether this subexpression is an
1860 // integral constant expression.
Sebastian Redl31310a22010-02-01 20:16:42 +00001861 if (ID->isInitICE())
Douglas Gregorcf3293e2009-11-01 20:32:48 +00001862 return NoDiag();
1863 else
1864 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1865 }
Douglas Gregor78d15832009-05-26 18:54:04 +00001866
John McCall1f1b3b32010-02-06 01:07:37 +00001867 // It's an ICE whether or not the definition we found is
1868 // out-of-line. See DR 721 and the discussion in Clang PR
1869 // 6206 for details.
Eli Friedmanc0131182009-12-03 20:31:57 +00001870
1871 if (Dcl->isCheckingICE()) {
1872 return ICEDiag(2, cast<DeclRefExpr>(E)->getLocation());
1873 }
1874
1875 Dcl->setCheckingICE();
Douglas Gregor78d15832009-05-26 18:54:04 +00001876 ICEDiag Result = CheckICE(Init, Ctx);
1877 // Cache the result of the ICE test.
Eli Friedmanc0131182009-12-03 20:31:57 +00001878 Dcl->setInitKnownICE(Result.Val == 0);
Douglas Gregor78d15832009-05-26 18:54:04 +00001879 return Result;
1880 }
Sebastian Redl4a4251b2009-02-07 13:06:23 +00001881 }
1882 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001883 return ICEDiag(2, E->getLocStart());
1884 case Expr::UnaryOperatorClass: {
1885 const UnaryOperator *Exp = cast<UnaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001887 case UnaryOperator::PostInc:
1888 case UnaryOperator::PostDec:
1889 case UnaryOperator::PreInc:
1890 case UnaryOperator::PreDec:
1891 case UnaryOperator::AddrOf:
1892 case UnaryOperator::Deref:
Eli Friedmane28d7192009-02-26 09:29:13 +00001893 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001894
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 case UnaryOperator::Extension:
Eli Friedmane28d7192009-02-26 09:29:13 +00001896 case UnaryOperator::LNot:
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 case UnaryOperator::Not:
Eli Friedman60ce9632009-02-27 04:07:58 +00001900 case UnaryOperator::Real:
1901 case UnaryOperator::Imag:
Eli Friedmane28d7192009-02-26 09:29:13 +00001902 return CheckICE(Exp->getSubExpr(), Ctx);
Anders Carlsson5a1deb82008-01-29 15:56:48 +00001903 case UnaryOperator::OffsetOf:
Eli Friedman60ce9632009-02-27 04:07:58 +00001904 // Note that per C99, offsetof must be an ICE. And AFAIK, using
1905 // Evaluate matches the proposed gcc behavior for cases like
1906 // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect
1907 // compliance: we should warn earlier for offsetof expressions with
1908 // array subscripts that aren't ICEs, and if the array subscripts
1909 // are ICEs, the value of the offsetof must be an integer constant.
1910 return CheckEvalInICE(E, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001913 case Expr::SizeOfAlignOfExprClass: {
1914 const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E);
1915 if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType())
1916 return ICEDiag(2, E->getLocStart());
1917 return NoDiag();
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001919 case Expr::BinaryOperatorClass: {
1920 const BinaryOperator *Exp = cast<BinaryOperator>(E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 switch (Exp->getOpcode()) {
Douglas Gregorf2991242009-09-10 23:31:45 +00001922 case BinaryOperator::PtrMemD:
1923 case BinaryOperator::PtrMemI:
1924 case BinaryOperator::Assign:
1925 case BinaryOperator::MulAssign:
1926 case BinaryOperator::DivAssign:
1927 case BinaryOperator::RemAssign:
1928 case BinaryOperator::AddAssign:
1929 case BinaryOperator::SubAssign:
1930 case BinaryOperator::ShlAssign:
1931 case BinaryOperator::ShrAssign:
1932 case BinaryOperator::AndAssign:
1933 case BinaryOperator::XorAssign:
1934 case BinaryOperator::OrAssign:
Eli Friedmane28d7192009-02-26 09:29:13 +00001935 return ICEDiag(2, E->getLocStart());
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001936
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 case BinaryOperator::Mul:
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 case BinaryOperator::Div:
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 case BinaryOperator::Rem:
Eli Friedmane28d7192009-02-26 09:29:13 +00001940 case BinaryOperator::Add:
1941 case BinaryOperator::Sub:
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 case BinaryOperator::Shl:
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 case BinaryOperator::Shr:
Eli Friedmane28d7192009-02-26 09:29:13 +00001944 case BinaryOperator::LT:
1945 case BinaryOperator::GT:
1946 case BinaryOperator::LE:
1947 case BinaryOperator::GE:
1948 case BinaryOperator::EQ:
1949 case BinaryOperator::NE:
1950 case BinaryOperator::And:
1951 case BinaryOperator::Xor:
1952 case BinaryOperator::Or:
1953 case BinaryOperator::Comma: {
1954 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1955 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00001956 if (Exp->getOpcode() == BinaryOperator::Div ||
1957 Exp->getOpcode() == BinaryOperator::Rem) {
1958 // Evaluate gives an error for undefined Div/Rem, so make sure
1959 // we don't evaluate one.
1960 if (LHSResult.Val != 2 && RHSResult.Val != 2) {
1961 llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx);
1962 if (REval == 0)
1963 return ICEDiag(1, E->getLocStart());
1964 if (REval.isSigned() && REval.isAllOnesValue()) {
1965 llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx);
1966 if (LEval.isMinSignedValue())
1967 return ICEDiag(1, E->getLocStart());
1968 }
1969 }
1970 }
1971 if (Exp->getOpcode() == BinaryOperator::Comma) {
1972 if (Ctx.getLangOptions().C99) {
1973 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
1974 // if it isn't evaluated.
1975 if (LHSResult.Val == 0 && RHSResult.Val == 0)
1976 return ICEDiag(1, E->getLocStart());
1977 } else {
1978 // In both C89 and C++, commas in ICEs are illegal.
1979 return ICEDiag(2, E->getLocStart());
1980 }
Eli Friedmane28d7192009-02-26 09:29:13 +00001981 }
1982 if (LHSResult.Val >= RHSResult.Val)
1983 return LHSResult;
1984 return RHSResult;
1985 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 case BinaryOperator::LAnd:
Eli Friedmane28d7192009-02-26 09:29:13 +00001987 case BinaryOperator::LOr: {
1988 ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx);
1989 ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx);
1990 if (LHSResult.Val == 0 && RHSResult.Val == 1) {
1991 // Rare case where the RHS has a comma "side-effect"; we need
1992 // to actually check the condition to see whether the side
1993 // with the comma is evaluated.
Eli Friedmane28d7192009-02-26 09:29:13 +00001994 if ((Exp->getOpcode() == BinaryOperator::LAnd) !=
Eli Friedman60ce9632009-02-27 04:07:58 +00001995 (Exp->getLHS()->EvaluateAsInt(Ctx) == 0))
Eli Friedmane28d7192009-02-26 09:29:13 +00001996 return RHSResult;
1997 return NoDiag();
Eli Friedmanb11e7782008-11-13 02:13:11 +00001998 }
Eli Friedman60ce9632009-02-27 04:07:58 +00001999
Eli Friedmane28d7192009-02-26 09:29:13 +00002000 if (LHSResult.Val >= RHSResult.Val)
2001 return LHSResult;
2002 return RHSResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002004 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002006 case Expr::ImplicitCastExprClass:
2007 case Expr::CStyleCastExprClass:
Douglas Gregor59600d82009-09-10 17:44:23 +00002008 case Expr::CXXFunctionalCastExprClass:
2009 case Expr::CXXStaticCastExprClass:
2010 case Expr::CXXReinterpretCastExprClass:
2011 case Expr::CXXConstCastExprClass: {
Eli Friedmane28d7192009-02-26 09:29:13 +00002012 const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr();
2013 if (SubExpr->getType()->isIntegralType())
2014 return CheckICE(SubExpr, Ctx);
2015 if (isa<FloatingLiteral>(SubExpr->IgnoreParens()))
2016 return NoDiag();
2017 return ICEDiag(2, E->getLocStart());
Reid Spencer5f016e22007-07-11 17:01:13 +00002018 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002019 case Expr::ConditionalOperatorClass: {
2020 const ConditionalOperator *Exp = cast<ConditionalOperator>(E);
Mike Stump1eb44332009-09-09 15:08:12 +00002021 // If the condition (ignoring parens) is a __builtin_constant_p call,
Chris Lattner28daa532008-12-12 06:55:44 +00002022 // then only the true side is actually considered in an integer constant
Chris Lattner42b83dd2008-12-12 18:00:51 +00002023 // expression, and it is fully evaluated. This is an important GNU
2024 // extension. See GCC PR38377 for discussion.
Enea Zaffanella049c51e2010-04-27 07:38:32 +00002025 if (const CallExpr *CallCE
2026 = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts()))
Douglas Gregor3c385e52009-02-14 18:57:46 +00002027 if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002028 Expr::EvalResult EVResult;
2029 if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects ||
2030 !EVResult.Val.isInt()) {
Eli Friedman60ce9632009-02-27 04:07:58 +00002031 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002032 }
2033 return NoDiag();
Chris Lattner42b83dd2008-12-12 18:00:51 +00002034 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002035 ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx);
2036 ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx);
2037 ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx);
2038 if (CondResult.Val == 2)
2039 return CondResult;
2040 if (TrueResult.Val == 2)
2041 return TrueResult;
2042 if (FalseResult.Val == 2)
2043 return FalseResult;
2044 if (CondResult.Val == 1)
2045 return CondResult;
2046 if (TrueResult.Val == 0 && FalseResult.Val == 0)
2047 return NoDiag();
2048 // Rare case where the diagnostics depend on which side is evaluated
2049 // Note that if we get here, CondResult is 0, and at least one of
2050 // TrueResult and FalseResult is non-zero.
Eli Friedman60ce9632009-02-27 04:07:58 +00002051 if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) {
Eli Friedmane28d7192009-02-26 09:29:13 +00002052 return FalseResult;
2053 }
2054 return TrueResult;
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 }
Eli Friedmane28d7192009-02-26 09:29:13 +00002056 case Expr::CXXDefaultArgExprClass:
2057 return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002058 case Expr::ChooseExprClass: {
Eli Friedman79769322009-03-04 05:52:32 +00002059 return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx);
Eli Friedman60ce9632009-02-27 04:07:58 +00002060 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 }
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002062
Douglas Gregorf2991242009-09-10 23:31:45 +00002063 // Silence a GCC warning
2064 return ICEDiag(2, E->getLocStart());
Eli Friedmane28d7192009-02-26 09:29:13 +00002065}
Reid Spencer5f016e22007-07-11 17:01:13 +00002066
Eli Friedmane28d7192009-02-26 09:29:13 +00002067bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
2068 SourceLocation *Loc, bool isEvaluated) const {
2069 ICEDiag d = CheckICE(this, Ctx);
2070 if (d.Val != 0) {
2071 if (Loc) *Loc = d.Loc;
2072 return false;
2073 }
2074 EvalResult EvalResult;
Eli Friedman60ce9632009-02-27 04:07:58 +00002075 if (!Evaluate(EvalResult, Ctx))
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002076 llvm_unreachable("ICE cannot be evaluated!");
Eli Friedman60ce9632009-02-27 04:07:58 +00002077 assert(!EvalResult.HasSideEffects && "ICE with side effects!");
2078 assert(EvalResult.Val.isInt() && "ICE that isn't integer!");
Eli Friedmane28d7192009-02-26 09:29:13 +00002079 Result = EvalResult.Val.getInt();
Reid Spencer5f016e22007-07-11 17:01:13 +00002080 return true;
2081}
2082
Reid Spencer5f016e22007-07-11 17:01:13 +00002083/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2084/// integer constant expression with the value zero, or if this is one that is
2085/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002086bool Expr::isNullPointerConstant(ASTContext &Ctx,
2087 NullPointerConstantValueDependence NPC) const {
2088 if (isValueDependent()) {
2089 switch (NPC) {
2090 case NPC_NeverValueDependent:
2091 assert(false && "Unexpected value dependent expression!");
2092 // If the unthinkable happens, fall through to the safest alternative.
2093
2094 case NPC_ValueDependentIsNull:
2095 return isTypeDependent() || getType()->isIntegralType();
2096
2097 case NPC_ValueDependentIsNotNull:
2098 return false;
2099 }
2100 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002101
Sebastian Redl07779722008-10-31 14:43:28 +00002102 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002103 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002104 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002105 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002106 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002107 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002108 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002109 Pointee->isVoidType() && // to void*
2110 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002111 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002112 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002114 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2115 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002116 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002117 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2118 // Accept ((void*)0) as a null pointer constant, as many other
2119 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002120 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002121 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002122 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002123 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002124 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002125 } else if (isa<GNUNullExpr>(this)) {
2126 // The GNU __null extension is always a null pointer constant.
2127 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002128 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002129
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002130 // C++0x nullptr_t is always a null pointer constant.
2131 if (getType()->isNullPtrType())
2132 return true;
2133
Steve Naroffaa58f002008-01-14 16:10:57 +00002134 // This expression must be an integer type.
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002135 if (!getType()->isIntegerType() ||
2136 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002137 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002138
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 // If we have an integer constant expression, we need to *evaluate* it and
2140 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002141 llvm::APSInt Result;
2142 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002143}
Steve Naroff31a45842007-07-28 23:10:27 +00002144
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002145FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002146 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002147
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002148 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2149 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2150 E = ICE->getSubExpr()->IgnoreParens();
2151 else
2152 break;
2153 }
2154
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002155 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002156 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002157 if (Field->isBitField())
2158 return Field;
2159
2160 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2161 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2162 return BinOp->getLHS()->getBitField();
2163
2164 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002165}
2166
Anders Carlsson09380262010-01-31 17:18:49 +00002167bool Expr::refersToVectorElement() const {
2168 const Expr *E = this->IgnoreParens();
2169
2170 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2171 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
2172 E = ICE->getSubExpr()->IgnoreParens();
2173 else
2174 break;
2175 }
2176
2177 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2178 return ASE->getBase()->getType()->isVectorType();
2179
2180 if (isa<ExtVectorElementExpr>(E))
2181 return true;
2182
2183 return false;
2184}
2185
Chris Lattner2140e902009-02-16 22:14:05 +00002186/// isArrow - Return true if the base expression is a pointer to vector,
2187/// return false if the base expression is a vector.
2188bool ExtVectorElementExpr::isArrow() const {
2189 return getBase()->getType()->isPointerType();
2190}
2191
Nate Begeman213541a2008-04-18 23:10:10 +00002192unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002193 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002194 return VT->getNumElements();
2195 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002196}
2197
Nate Begeman8a997642008-05-09 06:41:27 +00002198/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002199bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002200 // FIXME: Refactor this code to an accessor on the AST node which returns the
2201 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002202 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002203
2204 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002205 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002206 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002207
Nate Begeman190d6a22009-01-18 02:01:21 +00002208 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002209 if (Comp[0] == 's' || Comp[0] == 'S')
2210 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Daniel Dunbar15027422009-10-17 23:53:04 +00002212 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2213 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002214 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002215
Steve Narofffec0b492007-07-30 03:29:09 +00002216 return false;
2217}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002218
Nate Begeman8a997642008-05-09 06:41:27 +00002219/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002220void ExtVectorElementExpr::getEncodedElementAccess(
2221 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002222 llvm::StringRef Comp = Accessor->getName();
2223 if (Comp[0] == 's' || Comp[0] == 'S')
2224 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002226 bool isHi = Comp == "hi";
2227 bool isLo = Comp == "lo";
2228 bool isEven = Comp == "even";
2229 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002230
Nate Begeman8a997642008-05-09 06:41:27 +00002231 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2232 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002233
Nate Begeman8a997642008-05-09 06:41:27 +00002234 if (isHi)
2235 Index = e + i;
2236 else if (isLo)
2237 Index = i;
2238 else if (isEven)
2239 Index = 2 * i;
2240 else if (isOdd)
2241 Index = 2 * i + 1;
2242 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002243 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002244
Nate Begeman3b8d1162008-05-13 21:03:02 +00002245 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002246 }
Nate Begeman8a997642008-05-09 06:41:27 +00002247}
2248
Douglas Gregor04badcf2010-04-21 00:45:42 +00002249ObjCMessageExpr::ObjCMessageExpr(QualType T,
2250 SourceLocation LBracLoc,
2251 SourceLocation SuperLoc,
2252 bool IsInstanceSuper,
2253 QualType SuperType,
2254 Selector Sel,
2255 ObjCMethodDecl *Method,
2256 Expr **Args, unsigned NumArgs,
2257 SourceLocation RBracLoc)
2258 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregor92e986e2010-04-22 16:44:27 +00002259 /*ValueDependent=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002260 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2261 HasMethod(Method != 0), SuperLoc(SuperLoc),
2262 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2263 : Sel.getAsOpaquePtr())),
2264 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002265{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002266 setReceiverPointer(SuperType.getAsOpaquePtr());
2267 if (NumArgs)
2268 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002269}
2270
Douglas Gregor04badcf2010-04-21 00:45:42 +00002271ObjCMessageExpr::ObjCMessageExpr(QualType T,
2272 SourceLocation LBracLoc,
2273 TypeSourceInfo *Receiver,
2274 Selector Sel,
2275 ObjCMethodDecl *Method,
2276 Expr **Args, unsigned NumArgs,
2277 SourceLocation RBracLoc)
2278 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
2279 (T->isDependentType() ||
2280 hasAnyValueDependentArguments(Args, NumArgs))),
2281 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2282 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2283 : Sel.getAsOpaquePtr())),
2284 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2285{
2286 setReceiverPointer(Receiver);
2287 if (NumArgs)
2288 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002289}
2290
Douglas Gregor04badcf2010-04-21 00:45:42 +00002291ObjCMessageExpr::ObjCMessageExpr(QualType T,
2292 SourceLocation LBracLoc,
2293 Expr *Receiver,
2294 Selector Sel,
2295 ObjCMethodDecl *Method,
2296 Expr **Args, unsigned NumArgs,
2297 SourceLocation RBracLoc)
Douglas Gregor92e986e2010-04-22 16:44:27 +00002298 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
2299 (Receiver->isTypeDependent() ||
Douglas Gregor04badcf2010-04-21 00:45:42 +00002300 hasAnyValueDependentArguments(Args, NumArgs))),
2301 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2302 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2303 : Sel.getAsOpaquePtr())),
2304 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
2305{
2306 setReceiverPointer(Receiver);
2307 if (NumArgs)
2308 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner0389e6b2009-04-26 00:44:05 +00002309}
2310
Douglas Gregor04badcf2010-04-21 00:45:42 +00002311ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2312 SourceLocation LBracLoc,
2313 SourceLocation SuperLoc,
2314 bool IsInstanceSuper,
2315 QualType SuperType,
2316 Selector Sel,
2317 ObjCMethodDecl *Method,
2318 Expr **Args, unsigned NumArgs,
2319 SourceLocation RBracLoc) {
2320 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2321 NumArgs * sizeof(Expr *);
2322 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2323 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
2324 SuperType, Sel, Method, Args, NumArgs,
2325 RBracLoc);
2326}
2327
2328ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2329 SourceLocation LBracLoc,
2330 TypeSourceInfo *Receiver,
2331 Selector Sel,
2332 ObjCMethodDecl *Method,
2333 Expr **Args, unsigned NumArgs,
2334 SourceLocation RBracLoc) {
2335 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2336 NumArgs * sizeof(Expr *);
2337 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2338 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
2339 NumArgs, RBracLoc);
2340}
2341
2342ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2343 SourceLocation LBracLoc,
2344 Expr *Receiver,
2345 Selector Sel,
2346 ObjCMethodDecl *Method,
2347 Expr **Args, unsigned NumArgs,
2348 SourceLocation RBracLoc) {
2349 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2350 NumArgs * sizeof(Expr *);
2351 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2352 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
2353 NumArgs, RBracLoc);
2354}
2355
2356ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
2357 unsigned NumArgs) {
2358 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2359 NumArgs * sizeof(Expr *);
2360 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2361 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2362}
2363
2364Selector ObjCMessageExpr::getSelector() const {
2365 if (HasMethod)
2366 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2367 ->getSelector();
2368 return Selector(SelectorOrMethod);
2369}
2370
2371ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2372 switch (getReceiverKind()) {
2373 case Instance:
2374 if (const ObjCObjectPointerType *Ptr
2375 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2376 return Ptr->getInterfaceDecl();
2377 break;
2378
2379 case Class:
2380 if (const ObjCInterfaceType *Iface
2381 = getClassReceiver()->getAs<ObjCInterfaceType>())
2382 return Iface->getDecl();
2383 break;
2384
2385 case SuperInstance:
2386 if (const ObjCObjectPointerType *Ptr
2387 = getSuperType()->getAs<ObjCObjectPointerType>())
2388 return Ptr->getInterfaceDecl();
2389 break;
2390
2391 case SuperClass:
2392 if (const ObjCObjectPointerType *Iface
2393 = getSuperType()->getAs<ObjCObjectPointerType>())
2394 return Iface->getInterfaceDecl();
2395 break;
2396 }
2397
2398 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002399}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002400
Chris Lattner27437ca2007-10-25 00:29:32 +00002401bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002402 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002403}
2404
Nate Begeman888376a2009-08-12 02:28:50 +00002405void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2406 unsigned NumExprs) {
2407 if (SubExprs) C.Deallocate(SubExprs);
2408
2409 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002410 this->NumExprs = NumExprs;
2411 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002412}
Nate Begeman888376a2009-08-12 02:28:50 +00002413
2414void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2415 DestroyChildren(C);
2416 if (SubExprs) C.Deallocate(SubExprs);
2417 this->~ShuffleVectorExpr();
2418 C.Deallocate(this);
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002419}
2420
Douglas Gregor42602bb2009-08-07 06:08:38 +00002421void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl05189992008-11-11 17:56:53 +00002422 // Override default behavior of traversing children. If this has a type
2423 // operand and the type is a variable-length array, the child iteration
2424 // will iterate over the size expression. However, this expression belongs
2425 // to the type, not to this, so we don't want to delete it.
2426 // We still want to delete this expression.
Ted Kremenek8189cde2009-02-07 01:47:29 +00002427 if (isArgumentType()) {
2428 this->~SizeOfAlignOfExpr();
2429 C.Deallocate(this);
2430 }
Sebastian Redl05189992008-11-11 17:56:53 +00002431 else
Douglas Gregor42602bb2009-08-07 06:08:38 +00002432 Expr::DoDestroy(C);
Daniel Dunbar90488912008-08-28 18:02:04 +00002433}
2434
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002435//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002436// DesignatedInitExpr
2437//===----------------------------------------------------------------------===//
2438
2439IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2440 assert(Kind == FieldDesignator && "Only valid on a field designator");
2441 if (Field.NameOrField & 0x01)
2442 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2443 else
2444 return getField()->getIdentifier();
2445}
2446
Douglas Gregor319d57f2010-01-06 23:17:19 +00002447DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
2448 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002449 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002450 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002451 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002452 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002453 unsigned NumIndexExprs,
2454 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002455 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002456 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump1eb44332009-09-09 15:08:12 +00002457 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2458 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002459 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002460
2461 // Record the initializer itself.
2462 child_iterator Child = child_begin();
2463 *Child++ = Init;
2464
2465 // Copy the designators and their subexpressions, computing
2466 // value-dependence along the way.
2467 unsigned IndexIdx = 0;
2468 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002469 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002470
2471 if (this->Designators[I].isArrayDesignator()) {
2472 // Compute type- and value-dependence.
2473 Expr *Index = IndexExprs[IndexIdx];
Mike Stump1eb44332009-09-09 15:08:12 +00002474 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002475 Index->isTypeDependent() || Index->isValueDependent();
2476
2477 // Copy the index expressions into permanent storage.
2478 *Child++ = IndexExprs[IndexIdx++];
2479 } else if (this->Designators[I].isArrayRangeDesignator()) {
2480 // Compute type- and value-dependence.
2481 Expr *Start = IndexExprs[IndexIdx];
2482 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump1eb44332009-09-09 15:08:12 +00002483 ValueDependent = ValueDependent ||
Douglas Gregor9ea62762009-05-21 23:17:49 +00002484 Start->isTypeDependent() || Start->isValueDependent() ||
2485 End->isTypeDependent() || End->isValueDependent();
2486
2487 // Copy the start/end expressions into permanent storage.
2488 *Child++ = IndexExprs[IndexIdx++];
2489 *Child++ = IndexExprs[IndexIdx++];
2490 }
2491 }
2492
2493 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002494}
2495
Douglas Gregor05c13a32009-01-22 00:58:24 +00002496DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002497DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002498 unsigned NumDesignators,
2499 Expr **IndexExprs, unsigned NumIndexExprs,
2500 SourceLocation ColonOrEqualLoc,
2501 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002502 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002503 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002504 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002505 ColonOrEqualLoc, UsesColonSyntax,
2506 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002507}
2508
Mike Stump1eb44332009-09-09 15:08:12 +00002509DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002510 unsigned NumIndexExprs) {
2511 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2512 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2513 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2514}
2515
Douglas Gregor319d57f2010-01-06 23:17:19 +00002516void DesignatedInitExpr::setDesignators(ASTContext &C,
2517 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002518 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002519 DestroyDesignators(C);
Douglas Gregord077d752009-04-16 00:55:48 +00002520
Douglas Gregor319d57f2010-01-06 23:17:19 +00002521 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002522 NumDesignators = NumDesigs;
2523 for (unsigned I = 0; I != NumDesigs; ++I)
2524 Designators[I] = Desigs[I];
2525}
2526
Douglas Gregor05c13a32009-01-22 00:58:24 +00002527SourceRange DesignatedInitExpr::getSourceRange() const {
2528 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002529 Designator &First =
2530 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002531 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002532 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002533 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2534 else
2535 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2536 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002537 StartLoc =
2538 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002539 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2540}
2541
Douglas Gregor05c13a32009-01-22 00:58:24 +00002542Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2543 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2544 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2545 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002546 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2547 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2548}
2549
2550Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002551 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002552 "Requires array range designator");
2553 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2554 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002555 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2556 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2557}
2558
2559Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002560 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002561 "Requires array range designator");
2562 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2563 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002564 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2565 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2566}
2567
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002568/// \brief Replaces the designator at index @p Idx with the series
2569/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002570void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002571 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002572 const Designator *Last) {
2573 unsigned NumNewDesignators = Last - First;
2574 if (NumNewDesignators == 0) {
2575 std::copy_backward(Designators + Idx + 1,
2576 Designators + NumDesignators,
2577 Designators + Idx);
2578 --NumNewDesignators;
2579 return;
2580 } else if (NumNewDesignators == 1) {
2581 Designators[Idx] = *First;
2582 return;
2583 }
2584
Mike Stump1eb44332009-09-09 15:08:12 +00002585 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002586 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002587 std::copy(Designators, Designators + Idx, NewDesignators);
2588 std::copy(First, Last, NewDesignators + Idx);
2589 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2590 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002591 DestroyDesignators(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002592 Designators = NewDesignators;
2593 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2594}
2595
Douglas Gregor42602bb2009-08-07 06:08:38 +00002596void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002597 DestroyDesignators(C);
Douglas Gregor42602bb2009-08-07 06:08:38 +00002598 Expr::DoDestroy(C);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002599}
2600
Douglas Gregor319d57f2010-01-06 23:17:19 +00002601void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2602 for (unsigned I = 0; I != NumDesignators; ++I)
2603 Designators[I].~Designator();
2604 C.Deallocate(Designators);
2605 Designators = 0;
2606}
2607
Mike Stump1eb44332009-09-09 15:08:12 +00002608ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002609 Expr **exprs, unsigned nexprs,
2610 SourceLocation rparenloc)
2611: Expr(ParenListExprClass, QualType(),
2612 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump1eb44332009-09-09 15:08:12 +00002613 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman2ef13e52009-08-10 23:49:36 +00002614 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002615
Nate Begeman2ef13e52009-08-10 23:49:36 +00002616 Exprs = new (C) Stmt*[nexprs];
2617 for (unsigned i = 0; i != nexprs; ++i)
2618 Exprs[i] = exprs[i];
2619}
2620
2621void ParenListExpr::DoDestroy(ASTContext& C) {
2622 DestroyChildren(C);
2623 if (Exprs) C.Deallocate(Exprs);
2624 this->~ParenListExpr();
2625 C.Deallocate(this);
2626}
2627
Douglas Gregor05c13a32009-01-22 00:58:24 +00002628//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002629// ExprIterator.
2630//===----------------------------------------------------------------------===//
2631
2632Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2633Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2634Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2635const Expr* ConstExprIterator::operator[](size_t idx) const {
2636 return cast<Expr>(I[idx]);
2637}
2638const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2639const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2640
2641//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002642// Child Iterators for iterating over subexpressions/substatements
2643//===----------------------------------------------------------------------===//
2644
2645// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002646Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2647Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002648
Steve Naroff7779db42007-11-12 14:29:37 +00002649// ObjCIvarRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002650Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2651Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff7779db42007-11-12 14:29:37 +00002652
Steve Naroffe3e9add2008-06-02 23:03:37 +00002653// ObjCPropertyRefExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002654Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2655Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffae784072008-05-30 00:40:33 +00002656
Fariborz Jahanian09105f52009-08-20 17:02:02 +00002657// ObjCImplicitSetterGetterRefExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002658Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
2659 return &Base;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002660}
Mike Stump1eb44332009-09-09 15:08:12 +00002661Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2662 return &Base+1;
Fariborz Jahanian154440e2009-08-18 20:50:23 +00002663}
Fariborz Jahanian5daf5702008-11-22 18:39:36 +00002664
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00002665// ObjCSuperExpr
2666Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2667Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2668
Steve Narofff242b1b2009-07-24 17:54:45 +00002669// ObjCIsaExpr
2670Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2671Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2672
Chris Lattnerd9f69102008-08-10 01:53:14 +00002673// PredefinedExpr
2674Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2675Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002676
2677// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002678Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2679Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002680
2681// CharacterLiteral
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002682Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek9ac59282007-10-18 23:28:49 +00002683Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002684
2685// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002686Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2687Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002688
Chris Lattner5d661452007-08-26 03:42:43 +00002689// ImaginaryLiteral
Ted Kremenek55499762008-06-17 02:43:46 +00002690Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2691Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner5d661452007-08-26 03:42:43 +00002692
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002693// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00002694Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2695Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002696
2697// ParenExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002698Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2699Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002700
2701// UnaryOperator
Ted Kremenek55499762008-06-17 02:43:46 +00002702Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2703Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002704
Sebastian Redl05189992008-11-11 17:56:53 +00002705// SizeOfAlignOfExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002706Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl05189992008-11-11 17:56:53 +00002707 // If this is of a type and the type is a VLA type (and not a typedef), the
2708 // size expression of the VLA needs to be treated as an executable expression.
2709 // Why isn't this weirdness documented better in StmtIterator?
2710 if (isArgumentType()) {
2711 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2712 getArgumentType().getTypePtr()))
2713 return child_iterator(T);
2714 return child_iterator();
2715 }
Sebastian Redld4575892008-12-03 23:17:54 +00002716 return child_iterator(&Argument.Ex);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002717}
Sebastian Redl05189992008-11-11 17:56:53 +00002718Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2719 if (isArgumentType())
2720 return child_iterator();
Sebastian Redld4575892008-12-03 23:17:54 +00002721 return child_iterator(&Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002722}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002723
2724// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002725Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002726 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002727}
Ted Kremenek1237c672007-08-24 20:06:47 +00002728Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002729 return &SubExprs[0]+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002730}
2731
2732// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00002733Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002734 return &SubExprs[0];
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002735}
Ted Kremenek1237c672007-08-24 20:06:47 +00002736Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002737 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002738}
Ted Kremenek1237c672007-08-24 20:06:47 +00002739
2740// MemberExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002741Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2742Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002743
Nate Begeman213541a2008-04-18 23:10:10 +00002744// ExtVectorElementExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002745Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2746Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002747
2748// CompoundLiteralExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002749Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2750Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002751
Ted Kremenek1237c672007-08-24 20:06:47 +00002752// CastExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002753Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2754Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002755
2756// BinaryOperator
2757Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002758 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002759}
Ted Kremenek1237c672007-08-24 20:06:47 +00002760Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002761 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002762}
2763
2764// ConditionalOperator
2765Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002766 return &SubExprs[0];
Ted Kremenek1237c672007-08-24 20:06:47 +00002767}
Ted Kremenek1237c672007-08-24 20:06:47 +00002768Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002769 return &SubExprs[0]+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00002770}
2771
2772// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002773Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2774Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002775
Ted Kremenek1237c672007-08-24 20:06:47 +00002776// StmtExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002777Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2778Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002779
2780// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002781Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2782 return child_iterator();
2783}
2784
2785Stmt::child_iterator TypesCompatibleExpr::child_end() {
2786 return child_iterator();
2787}
Ted Kremenek1237c672007-08-24 20:06:47 +00002788
2789// ChooseExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002790Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2791Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek1237c672007-08-24 20:06:47 +00002792
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002793// GNUNullExpr
2794Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2795Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2796
Eli Friedmand38617c2008-05-14 19:38:39 +00002797// ShuffleVectorExpr
2798Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek55499762008-06-17 02:43:46 +00002799 return &SubExprs[0];
Eli Friedmand38617c2008-05-14 19:38:39 +00002800}
2801Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek55499762008-06-17 02:43:46 +00002802 return &SubExprs[0]+NumExprs;
Eli Friedmand38617c2008-05-14 19:38:39 +00002803}
2804
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002805// VAArgExpr
Ted Kremenek55499762008-06-17 02:43:46 +00002806Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2807Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002808
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002809// InitListExpr
Ted Kremenekba7bc552010-02-19 01:50:18 +00002810Stmt::child_iterator InitListExpr::child_begin() {
2811 return InitExprs.size() ? &InitExprs[0] : 0;
2812}
2813Stmt::child_iterator InitListExpr::child_end() {
2814 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2815}
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00002816
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002817// DesignatedInitExpr
Douglas Gregor05c13a32009-01-22 00:58:24 +00002818Stmt::child_iterator DesignatedInitExpr::child_begin() {
2819 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2820 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002821 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2822}
2823Stmt::child_iterator DesignatedInitExpr::child_end() {
2824 return child_iterator(&*child_begin() + NumSubExprs);
2825}
2826
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002827// ImplicitValueInitExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002828Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2829 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002830}
2831
Mike Stump1eb44332009-09-09 15:08:12 +00002832Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2833 return child_iterator();
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002834}
2835
Nate Begeman2ef13e52009-08-10 23:49:36 +00002836// ParenListExpr
2837Stmt::child_iterator ParenListExpr::child_begin() {
2838 return &Exprs[0];
2839}
2840Stmt::child_iterator ParenListExpr::child_end() {
2841 return &Exprs[0]+NumExprs;
2842}
2843
Ted Kremenek1237c672007-08-24 20:06:47 +00002844// ObjCStringLiteral
Mike Stump1eb44332009-09-09 15:08:12 +00002845Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002846 return &String;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002847}
2848Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattnerc6c16af2009-02-18 06:53:08 +00002849 return &String+1;
Ted Kremenek9ac59282007-10-18 23:28:49 +00002850}
Ted Kremenek1237c672007-08-24 20:06:47 +00002851
2852// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002853Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2854Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00002855
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002856// ObjCSelectorExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002857Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +00002858 return child_iterator();
2859}
2860Stmt::child_iterator ObjCSelectorExpr::child_end() {
2861 return child_iterator();
2862}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002863
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002864// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00002865Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2866 return child_iterator();
2867}
2868Stmt::child_iterator ObjCProtocolExpr::child_end() {
2869 return child_iterator();
2870}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002871
Steve Naroff563477d2007-09-18 23:55:05 +00002872// ObjCMessageExpr
Mike Stump1eb44332009-09-09 15:08:12 +00002873Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002874 if (getReceiverKind() == Instance)
2875 return reinterpret_cast<Stmt **>(this + 1);
2876 return getArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002877}
2878Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor04badcf2010-04-21 00:45:42 +00002879 return getArgs() + getNumArgs();
Steve Naroff563477d2007-09-18 23:55:05 +00002880}
2881
Steve Naroff4eb206b2008-09-03 18:15:37 +00002882// Blocks
Steve Naroff56ee6892008-10-08 17:01:13 +00002883Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2884Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff4eb206b2008-09-03 18:15:37 +00002885
Ted Kremenek9da13f92008-09-26 23:24:14 +00002886Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2887Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }