blob: 626dbd67f5635e3ef47fb8d770e531c9c913c274 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000023#include "clang/Basic/Builtins.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000024#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000025#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000027#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000028using namespace clang;
29
Chris Lattner4ebae652010-04-16 23:34:13 +000030/// isKnownToHaveBooleanValue - Return true if this is an integer expression
31/// that is known to return 0 or 1. This happens for _Bool/bool expressions
32/// but also int expressions which are produced by things like comparisons in
33/// C.
34bool Expr::isKnownToHaveBooleanValue() const {
35 // If this value has _Bool type, it is obvious 0/1.
36 if (getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000037 // If this is a non-scalar-integer type, we don't care enough to try.
Chris Lattner4ebae652010-04-16 23:34:13 +000038 if (!getType()->isIntegralType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000039
Chris Lattner4ebae652010-04-16 23:34:13 +000040 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
41 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000042
Chris Lattner4ebae652010-04-16 23:34:13 +000043 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 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000052
Chris Lattner4ebae652010-04-16 23:34:13 +000053 if (const CastExpr *CE = dyn_cast<CastExpr>(this))
54 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000055
Chris Lattner4ebae652010-04-16 23:34:13 +000056 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;
Alexis Hunta8136cc2010-05-05 15:23:54 +000068
Chris Lattner4ebae652010-04-16 23:34:13 +000069 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();
Alexis Hunta8136cc2010-05-05 15:23:54 +000075
Chris Lattner4ebae652010-04-16 23:34:13 +000076 case BinaryOperator::Comma:
77 case BinaryOperator::Assign:
78 return BO->getRHS()->isKnownToHaveBooleanValue();
79 }
80 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000081
Chris Lattner4ebae652010-04-16 23:34:13 +000082 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
83 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
84 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Chris Lattner4ebae652010-04-16 23:34:13 +000086 return false;
87}
88
Chris Lattner0eedafe2006-08-24 04:56:27 +000089//===----------------------------------------------------------------------===//
90// Primary Expressions.
91//===----------------------------------------------------------------------===//
92
John McCall6b51f282009-11-23 01:53:49 +000093void ExplicitTemplateArgumentList::initializeFrom(
94 const TemplateArgumentListInfo &Info) {
95 LAngleLoc = Info.getLAngleLoc();
96 RAngleLoc = Info.getRAngleLoc();
97 NumTemplateArgs = Info.size();
98
99 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
100 for (unsigned i = 0; i != NumTemplateArgs; ++i)
101 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
102}
103
104void ExplicitTemplateArgumentList::copyInto(
105 TemplateArgumentListInfo &Info) const {
106 Info.setLAngleLoc(LAngleLoc);
107 Info.setRAngleLoc(RAngleLoc);
108 for (unsigned I = 0; I != NumTemplateArgs; ++I)
109 Info.addArgument(getTemplateArgs()[I]);
110}
111
112std::size_t ExplicitTemplateArgumentList::sizeFor(
113 const TemplateArgumentListInfo &Info) {
114 return sizeof(ExplicitTemplateArgumentList) +
115 sizeof(TemplateArgumentLoc) * Info.size();
116}
117
Douglas Gregored6c7442009-11-23 11:41:28 +0000118void DeclRefExpr::computeDependence() {
119 TypeDependent = false;
120 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000121
Douglas Gregored6c7442009-11-23 11:41:28 +0000122 NamedDecl *D = getDecl();
123
124 // (TD) C++ [temp.dep.expr]p3:
125 // An id-expression is type-dependent if it contains:
126 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000127 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000128 //
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
Alexis Hunta8136cc2010-05-05 15:23:54 +0000139 else if (D->getDeclName().getNameKind()
Douglas Gregored6c7442009-11-23 11:41:28 +0000140 == DeclarationName::CXXConversionFunctionName &&
141 D->getDeclName().getCXXNameType()->isDependentType()) {
142 TypeDependent = true;
143 ValueDependent = true;
144 }
145 // (TD) - a template-id that is dependent,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000146 else if (hasExplicitTemplateArgumentList() &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000147 TemplateSpecializationType::anyDependentTemplateArguments(
Alexis Hunta8136cc2010-05-05 15:23:54 +0000148 getTemplateArgs(),
Douglas Gregored6c7442009-11-23 11:41:28 +0000149 getNumTemplateArgs())) {
150 TypeDependent = true;
151 ValueDependent = true;
152 }
153 // (VD) - the name of a non-type template parameter,
154 else if (isa<NonTypeTemplateParmDecl>(D))
155 ValueDependent = true;
156 // (VD) - a constant with integral or enumeration type and is
157 // initialized with an expression that is value-dependent.
158 else if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
159 if (Var->getType()->isIntegralType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000160 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000161 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000162 if (Init->isValueDependent())
163 ValueDependent = true;
164 }
Douglas Gregored6c7442009-11-23 11:41:28 +0000165 }
166 // (TD) - a nested-name-specifier or a qualified-id that names a
167 // member of an unknown specialization.
168 // (handled by DependentScopeDeclRefExpr)
169}
170
Alexis Hunta8136cc2010-05-05 15:23:54 +0000171DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000172 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000173 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000174 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregored6c7442009-11-23 11:41:28 +0000175 QualType T)
176 : Expr(DeclRefExprClass, T, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000177 DecoratedD(D,
178 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000179 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000180 Loc(NameLoc) {
181 if (Qualifier) {
182 NameQualifier *NQ = getNameQualifier();
183 NQ->NNS = Qualifier;
184 NQ->Range = QualifierRange;
185 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000186
John McCall6b51f282009-11-23 01:53:49 +0000187 if (TemplateArgs)
188 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000189
190 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000191}
192
193DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
194 NestedNameSpecifier *Qualifier,
195 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000196 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000197 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000198 QualType T,
199 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000200 std::size_t Size = sizeof(DeclRefExpr);
201 if (Qualifier != 0)
202 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000203
John McCall6b51f282009-11-23 01:53:49 +0000204 if (TemplateArgs)
205 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000206
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000207 void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>());
208 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000209 TemplateArgs, T);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000210}
211
212SourceRange DeclRefExpr::getSourceRange() const {
213 // FIXME: Does not handle multi-token names well, e.g., operator[].
214 SourceRange R(Loc);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000215
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000216 if (hasQualifier())
217 R.setBegin(getQualifierRange().getBegin());
218 if (hasExplicitTemplateArgumentList())
219 R.setEnd(getRAngleLoc());
220 return R;
221}
222
Anders Carlsson2fb08242009-09-08 18:24:21 +0000223// FIXME: Maybe this should use DeclPrinter with a special "print predefined
224// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000225std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
226 ASTContext &Context = CurrentDecl->getASTContext();
227
Anders Carlsson2fb08242009-09-08 18:24:21 +0000228 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000229 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000230 return FD->getNameAsString();
231
232 llvm::SmallString<256> Name;
233 llvm::raw_svector_ostream Out(Name);
234
235 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000236 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000237 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000238 if (MD->isStatic())
239 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000240 }
241
242 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000243
244 std::string Proto = FD->getQualifiedNameAsString(Policy);
245
John McCall9dd450b2009-09-21 23:43:11 +0000246 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000247 const FunctionProtoType *FT = 0;
248 if (FD->hasWrittenPrototype())
249 FT = dyn_cast<FunctionProtoType>(AFT);
250
251 Proto += "(";
252 if (FT) {
253 llvm::raw_string_ostream POut(Proto);
254 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
255 if (i) POut << ", ";
256 std::string Param;
257 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
258 POut << Param;
259 }
260
261 if (FT->isVariadic()) {
262 if (FD->getNumParams()) POut << ", ";
263 POut << "...";
264 }
265 }
266 Proto += ")";
267
Sam Weinig4e83bd22009-12-27 01:38:20 +0000268 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
269 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
270 if (ThisQuals.hasConst())
271 Proto += " const";
272 if (ThisQuals.hasVolatile())
273 Proto += " volatile";
274 }
275
Sam Weinigd060ed42009-12-06 23:55:13 +0000276 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
277 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000278
279 Out << Proto;
280
281 Out.flush();
282 return Name.str().str();
283 }
284 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
285 llvm::SmallString<256> Name;
286 llvm::raw_svector_ostream Out(Name);
287 Out << (MD->isInstanceMethod() ? '-' : '+');
288 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000289
290 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
291 // a null check to avoid a crash.
292 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000293 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000294
Anders Carlsson2fb08242009-09-08 18:24:21 +0000295 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000296 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
297 Out << '(' << CID << ')';
298
Anders Carlsson2fb08242009-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 Lattnera0173132008-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 Johannesenc48814b2008-10-09 23:02:32 +0000318 bool ignored;
319 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
320 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000321 return V.convertToDouble();
322}
323
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000324StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
325 unsigned ByteLength, bool Wide,
326 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000327 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000328 unsigned NumStrs) {
Chris Lattnerf83b5af2009-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 Stump11289f42009-09-09 15:08:12 +0000335
Steve Naroffdf7855b2007-02-21 23:46:25 +0000336 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-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;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000344
Chris Lattner630970d2009-02-18 05:49:11 +0000345 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000346 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
347 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000348}
349
Douglas Gregor958dfc92009-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 Gregore26a2852009-08-07 06:08:38 +0000361void StringLiteral::DoDestroy(ASTContext &C) {
Ted Kremenek5a201952009-02-07 01:47:29 +0000362 C.Deallocate(const_cast<char*>(StrData));
Douglas Gregore26a2852009-08-07 06:08:38 +0000363 Expr::DoDestroy(C);
Chris Lattnerd3e98952006-10-06 05:22:26 +0000364}
365
Daniel Dunbar36217882009-09-22 03:27:33 +0000366void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Douglas Gregor958dfc92009-04-15 16:35:07 +0000367 if (StrData)
368 C.Deallocate(const_cast<char*>(StrData));
369
Daniel Dunbar36217882009-09-22 03:27:33 +0000370 char *AStrData = new (C, 1) char[Str.size()];
371 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000372 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000373 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000374}
375
Chris Lattner1b926492006-08-23 06:42:10 +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) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000380 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +0000381 case PostInc: return "++";
382 case PostDec: return "--";
383 case PreInc: return "++";
384 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +0000385 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";
Chris Lattnerc52b1182006-10-25 05:45:55 +0000393 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +0000394 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +0000395 }
396}
397
Mike Stump11289f42009-09-09 15:08:12 +0000398UnaryOperator::Opcode
Douglas Gregor084d8552009-03-13 23:49:33 +0000399UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
400 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000401 default: assert(false && "No unary operator for overloaded function");
Chris Lattner17556b22009-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 Gregor084d8552009-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
Chris Lattner0eedafe2006-08-24 04:56:27 +0000428//===----------------------------------------------------------------------===//
429// Postfix Operators.
430//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000431
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000432CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args,
Ted Kremenek5a201952009-02-07 01:47:29 +0000433 unsigned numargs, QualType t, SourceLocation rparenloc)
Mike Stump11289f42009-09-09 15:08:12 +0000434 : Expr(SC, t,
Douglas Gregor4619e432008-12-05 23:32:09 +0000435 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000436 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000437 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000438
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000439 SubExprs = new (C) Stmt*[numargs+1];
Douglas Gregor993603d2008-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 Kremenekd7b4f402009-02-09 20:51:47 +0000443
Douglas Gregor993603d2008-11-14 16:09:21 +0000444 RParenLoc = rparenloc;
445}
Nate Begeman1e36a852008-01-17 17:46:27 +0000446
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000447CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
448 QualType t, SourceLocation rparenloc)
Douglas Gregor4619e432008-12-05 23:32:09 +0000449 : Expr(CallExprClass, t,
450 fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs),
Chris Lattner8ba22472009-02-16 22:33:34 +0000451 fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)),
Douglas Gregor4619e432008-12-05 23:32:09 +0000452 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000453
454 SubExprs = new (C) Stmt*[numargs+1];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000455 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +0000456 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000457 SubExprs[i+ARGS_START] = args[i];
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000458
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000459 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000460}
461
Mike Stump11289f42009-09-09 15:08:12 +0000462CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
463 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregore20a2e52009-04-15 17:43:59 +0000464 SubExprs = new (C) Stmt*[1];
465}
466
Douglas Gregore26a2852009-08-07 06:08:38 +0000467void CallExpr::DoDestroy(ASTContext& C) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000468 DestroyChildren(C);
469 if (SubExprs) C.Deallocate(SubExprs);
470 this->~CallExpr();
471 C.Deallocate(this);
472}
473
Nuno Lopes518e3702009-12-20 23:11:08 +0000474Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000475 Expr *CEE = getCallee()->IgnoreParenCasts();
Chris Lattner52301912009-07-17 15:46:27 +0000476 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000477 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000478 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
479 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000480
481 return 0;
482}
483
Nuno Lopes518e3702009-12-20 23:11:08 +0000484FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000485 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000486}
487
Chris Lattnere4407ed2007-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 Kremenek5a201952009-02-07 01:47:29 +0000491void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000492 // No change, just return.
493 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnere4407ed2007-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 Kremenek5a201952009-02-07 01:47:29 +0000498 getArg(i)->Destroy(C);
Chris Lattnere4407ed2007-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 Dunbarec5ae3d2009-07-28 06:29:46 +0000504 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1];
Chris Lattnere4407ed2007-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 Stump11289f42009-09-09 15:08:12 +0000511
Douglas Gregorba6e5572009-04-17 21:46:47 +0000512 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000513 SubExprs = NewSubExprs;
514 this->NumArgs = NumArgs;
515}
516
Chris Lattner01ff98a2008-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 Gregore711f702009-02-14 18:57:46 +0000519unsigned CallExpr::isBuiltinCall(ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000520 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000521 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-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 Lattner01ff98a2008-10-06 05:00:53 +0000525 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Steve Narofff6e3b3292008-01-31 01:07:12 +0000527 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
528 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000529 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000531 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
532 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000533 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000534
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000535 if (!FDecl->getIdentifier())
536 return 0;
537
Douglas Gregor15fc9562009-09-12 00:22:50 +0000538 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000539}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000540
Anders Carlsson00a27592009-05-26 04:57:27 +0000541QualType CallExpr::getCallReturnType() const {
542 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000543 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000544 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000545 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000546 CalleeType = BPT->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000547
John McCall9dd450b2009-09-21 23:43:11 +0000548 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000549 return FnType->getResultType();
550}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000551
Alexis Hunta8136cc2010-05-05 15:23:54 +0000552OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000553 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000554 TypeSourceInfo *tsi,
555 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000556 Expr** exprsPtr, unsigned numExprs,
557 SourceLocation RParenLoc) {
558 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000559 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000560 sizeof(Expr*) * numExprs);
561
562 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
563 exprsPtr, numExprs, RParenLoc);
564}
565
566OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
567 unsigned numComps, unsigned numExprs) {
568 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
569 sizeof(OffsetOfNode) * numComps +
570 sizeof(Expr*) * numExprs);
571 return new (Mem) OffsetOfExpr(numComps, numExprs);
572}
573
Alexis Hunta8136cc2010-05-05 15:23:54 +0000574OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000575 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000576 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000577 Expr** exprsPtr, unsigned numExprs,
578 SourceLocation RParenLoc)
Alexis Hunta8136cc2010-05-05 15:23:54 +0000579 : Expr(OffsetOfExprClass, type, /*TypeDependent=*/false,
Douglas Gregor882211c2010-04-28 22:16:22 +0000580 /*ValueDependent=*/tsi->getType()->isDependentType() ||
581 hasAnyTypeDependentArguments(exprsPtr, numExprs) ||
582 hasAnyValueDependentArguments(exprsPtr, numExprs)),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000583 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
584 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000585{
586 for(unsigned i = 0; i < numComps; ++i) {
587 setComponent(i, compsPtr[i]);
588 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000589
Douglas Gregor882211c2010-04-28 22:16:22 +0000590 for(unsigned i = 0; i < numExprs; ++i) {
591 setIndexExpr(i, exprsPtr[i]);
592 }
593}
594
595IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
596 assert(getKind() == Field || getKind() == Identifier);
597 if (getKind() == Field)
598 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000599
Douglas Gregor882211c2010-04-28 22:16:22 +0000600 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
601}
602
Mike Stump11289f42009-09-09 15:08:12 +0000603MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
604 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000605 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000606 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000607 DeclAccessPair founddecl,
Mike Stump11289f42009-09-09 15:08:12 +0000608 SourceLocation l,
John McCall6b51f282009-11-23 01:53:49 +0000609 const TemplateArgumentListInfo *targs,
Douglas Gregor84f14dd2009-09-01 00:37:14 +0000610 QualType ty) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000611 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000612
John McCalla8ae2222010-04-06 21:38:20 +0000613 bool hasQualOrFound = (qual != 0 ||
614 founddecl.getDecl() != memberdecl ||
615 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000616 if (hasQualOrFound)
617 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000618
John McCall6b51f282009-11-23 01:53:49 +0000619 if (targs)
620 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000621
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000622 void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>());
John McCall16df1e52010-03-30 21:47:33 +0000623 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, l, ty);
624
625 if (hasQualOrFound) {
626 if (qual && qual->isDependent()) {
627 E->setValueDependent(true);
628 E->setTypeDependent(true);
629 }
630 E->HasQualifierOrFoundDecl = true;
631
632 MemberNameQualifier *NQ = E->getMemberQualifier();
633 NQ->NNS = qual;
634 NQ->Range = qualrange;
635 NQ->FoundDecl = founddecl;
636 }
637
638 if (targs) {
639 E->HasExplicitTemplateArgumentList = true;
640 E->getExplicitTemplateArgumentList()->initializeFrom(*targs);
641 }
642
643 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000644}
645
Anders Carlsson496335e2009-09-03 00:59:21 +0000646const char *CastExpr::getCastKindName() const {
647 switch (getCastKind()) {
648 case CastExpr::CK_Unknown:
649 return "Unknown";
650 case CastExpr::CK_BitCast:
651 return "BitCast";
652 case CastExpr::CK_NoOp:
653 return "NoOp";
Anders Carlssona70ad932009-11-12 16:43:42 +0000654 case CastExpr::CK_BaseToDerived:
655 return "BaseToDerived";
Anders Carlsson496335e2009-09-03 00:59:21 +0000656 case CastExpr::CK_DerivedToBase:
657 return "DerivedToBase";
John McCalld9c7c6562010-03-30 23:58:03 +0000658 case CastExpr::CK_UncheckedDerivedToBase:
659 return "UncheckedDerivedToBase";
Anders Carlsson496335e2009-09-03 00:59:21 +0000660 case CastExpr::CK_Dynamic:
661 return "Dynamic";
662 case CastExpr::CK_ToUnion:
663 return "ToUnion";
664 case CastExpr::CK_ArrayToPointerDecay:
665 return "ArrayToPointerDecay";
666 case CastExpr::CK_FunctionToPointerDecay:
667 return "FunctionToPointerDecay";
668 case CastExpr::CK_NullToMemberPointer:
669 return "NullToMemberPointer";
670 case CastExpr::CK_BaseToDerivedMemberPointer:
671 return "BaseToDerivedMemberPointer";
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000672 case CastExpr::CK_DerivedToBaseMemberPointer:
673 return "DerivedToBaseMemberPointer";
Anders Carlsson496335e2009-09-03 00:59:21 +0000674 case CastExpr::CK_UserDefinedConversion:
675 return "UserDefinedConversion";
676 case CastExpr::CK_ConstructorConversion:
677 return "ConstructorConversion";
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000678 case CastExpr::CK_IntegralToPointer:
679 return "IntegralToPointer";
680 case CastExpr::CK_PointerToIntegral:
681 return "PointerToIntegral";
Anders Carlssonef918ac2009-10-16 02:35:04 +0000682 case CastExpr::CK_ToVoid:
683 return "ToVoid";
Anders Carlsson43d70f82009-10-16 05:23:41 +0000684 case CastExpr::CK_VectorSplat:
685 return "VectorSplat";
Anders Carlsson094c4592009-10-18 18:12:03 +0000686 case CastExpr::CK_IntegralCast:
687 return "IntegralCast";
688 case CastExpr::CK_IntegralToFloating:
689 return "IntegralToFloating";
690 case CastExpr::CK_FloatingToIntegral:
691 return "FloatingToIntegral";
Benjamin Kramerbeb873d2009-10-18 19:02:15 +0000692 case CastExpr::CK_FloatingCast:
693 return "FloatingCast";
Anders Carlsson7fa434c2009-11-23 20:04:44 +0000694 case CastExpr::CK_MemberPointerToBoolean:
695 return "MemberPointerToBoolean";
Fariborz Jahaniane19122f2009-12-08 23:46:15 +0000696 case CastExpr::CK_AnyPointerToObjCPointerCast:
697 return "AnyPointerToObjCPointerCast";
Fariborz Jahanianffe912c2009-12-11 22:40:48 +0000698 case CastExpr::CK_AnyPointerToBlockPointerCast:
699 return "AnyPointerToBlockPointerCast";
Anders Carlsson496335e2009-09-03 00:59:21 +0000700 }
Mike Stump11289f42009-09-09 15:08:12 +0000701
Anders Carlsson496335e2009-09-03 00:59:21 +0000702 assert(0 && "Unhandled cast kind!");
703 return 0;
704}
705
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000706void CastExpr::DoDestroy(ASTContext &C)
707{
Anders Carlsson0c509ee2010-04-24 16:57:13 +0000708 BasePath.Destroy();
Anders Carlssonc20f78c2010-04-23 21:02:34 +0000709 Expr::DoDestroy(C);
710}
711
Douglas Gregord196a582009-12-14 19:27:10 +0000712Expr *CastExpr::getSubExprAsWritten() {
713 Expr *SubExpr = 0;
714 CastExpr *E = this;
715 do {
716 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000717
Douglas Gregord196a582009-12-14 19:27:10 +0000718 // Skip any temporary bindings; they're implicit.
719 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
720 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000721
Douglas Gregord196a582009-12-14 19:27:10 +0000722 // Conversions by constructor and conversion functions have a
723 // subexpression describing the call; strip it off.
724 if (E->getCastKind() == CastExpr::CK_ConstructorConversion)
725 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
726 else if (E->getCastKind() == CastExpr::CK_UserDefinedConversion)
727 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000728
Douglas Gregord196a582009-12-14 19:27:10 +0000729 // If the subexpression we're left with is an implicit cast, look
730 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +0000731 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
732
Douglas Gregord196a582009-12-14 19:27:10 +0000733 return SubExpr;
734}
735
Chris Lattner1b926492006-08-23 06:42:10 +0000736/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
737/// corresponds to, e.g. "<<=".
738const char *BinaryOperator::getOpcodeStr(Opcode Op) {
739 switch (Op) {
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000740 case PtrMemD: return ".*";
741 case PtrMemI: return "->*";
Chris Lattner1b926492006-08-23 06:42:10 +0000742 case Mul: return "*";
743 case Div: return "/";
744 case Rem: return "%";
745 case Add: return "+";
746 case Sub: return "-";
747 case Shl: return "<<";
748 case Shr: return ">>";
749 case LT: return "<";
750 case GT: return ">";
751 case LE: return "<=";
752 case GE: return ">=";
753 case EQ: return "==";
754 case NE: return "!=";
755 case And: return "&";
756 case Xor: return "^";
757 case Or: return "|";
758 case LAnd: return "&&";
759 case LOr: return "||";
760 case Assign: return "=";
761 case MulAssign: return "*=";
762 case DivAssign: return "/=";
763 case RemAssign: return "%=";
764 case AddAssign: return "+=";
765 case SubAssign: return "-=";
766 case ShlAssign: return "<<=";
767 case ShrAssign: return ">>=";
768 case AndAssign: return "&=";
769 case XorAssign: return "^=";
770 case OrAssign: return "|=";
771 case Comma: return ",";
772 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +0000773
774 return "";
Chris Lattner1b926492006-08-23 06:42:10 +0000775}
Steve Naroff47500512007-04-19 23:00:49 +0000776
Mike Stump11289f42009-09-09 15:08:12 +0000777BinaryOperator::Opcode
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000778BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
779 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +0000780 default: assert(false && "Not an overloadable binary operator");
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000781 case OO_Plus: return Add;
782 case OO_Minus: return Sub;
783 case OO_Star: return Mul;
784 case OO_Slash: return Div;
785 case OO_Percent: return Rem;
786 case OO_Caret: return Xor;
787 case OO_Amp: return And;
788 case OO_Pipe: return Or;
789 case OO_Equal: return Assign;
790 case OO_Less: return LT;
791 case OO_Greater: return GT;
792 case OO_PlusEqual: return AddAssign;
793 case OO_MinusEqual: return SubAssign;
794 case OO_StarEqual: return MulAssign;
795 case OO_SlashEqual: return DivAssign;
796 case OO_PercentEqual: return RemAssign;
797 case OO_CaretEqual: return XorAssign;
798 case OO_AmpEqual: return AndAssign;
799 case OO_PipeEqual: return OrAssign;
800 case OO_LessLess: return Shl;
801 case OO_GreaterGreater: return Shr;
802 case OO_LessLessEqual: return ShlAssign;
803 case OO_GreaterGreaterEqual: return ShrAssign;
804 case OO_EqualEqual: return EQ;
805 case OO_ExclaimEqual: return NE;
806 case OO_LessEqual: return LE;
807 case OO_GreaterEqual: return GE;
808 case OO_AmpAmp: return LAnd;
809 case OO_PipePipe: return LOr;
810 case OO_Comma: return Comma;
811 case OO_ArrowStar: return PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +0000812 }
813}
814
815OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
816 static const OverloadedOperatorKind OverOps[] = {
817 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
818 OO_Star, OO_Slash, OO_Percent,
819 OO_Plus, OO_Minus,
820 OO_LessLess, OO_GreaterGreater,
821 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
822 OO_EqualEqual, OO_ExclaimEqual,
823 OO_Amp,
824 OO_Caret,
825 OO_Pipe,
826 OO_AmpAmp,
827 OO_PipePipe,
828 OO_Equal, OO_StarEqual,
829 OO_SlashEqual, OO_PercentEqual,
830 OO_PlusEqual, OO_MinusEqual,
831 OO_LessLessEqual, OO_GreaterGreaterEqual,
832 OO_AmpEqual, OO_CaretEqual,
833 OO_PipeEqual,
834 OO_Comma
835 };
836 return OverOps[Opc];
837}
838
Ted Kremenekac034612010-04-13 23:39:13 +0000839InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +0000840 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000841 SourceLocation rbraceloc)
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000842 : Expr(InitListExprClass, QualType(), false, false),
Ted Kremenekac034612010-04-13 23:39:13 +0000843 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +0000844 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000845 UnionFieldInit(0), HadArrayRangeDesignator(false)
846{
Ted Kremenek013041e2010-02-19 01:50:18 +0000847 for (unsigned I = 0; I != numInits; ++I) {
848 if (initExprs[I]->isTypeDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000849 TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +0000850 if (initExprs[I]->isValueDependent())
Douglas Gregordeebf6e2009-11-19 23:25:22 +0000851 ValueDependent = true;
852 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000853
Ted Kremenekac034612010-04-13 23:39:13 +0000854 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +0000855}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000856
Ted Kremenekac034612010-04-13 23:39:13 +0000857void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000858 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +0000859 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +0000860}
861
Ted Kremenekac034612010-04-13 23:39:13 +0000862void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000863 for (unsigned Idx = NumInits, LastIdx = InitExprs.size();
864 Idx < LastIdx; ++Idx)
Ted Kremenekac034612010-04-13 23:39:13 +0000865 InitExprs[Idx]->Destroy(C);
866 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000867}
868
Ted Kremenekac034612010-04-13 23:39:13 +0000869Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +0000870 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +0000871 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +0000872 InitExprs.back() = expr;
873 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000874 }
Mike Stump11289f42009-09-09 15:08:12 +0000875
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000876 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
877 InitExprs[Init] = expr;
878 return Result;
879}
880
Steve Naroff991e99d2008-09-04 15:31:07 +0000881/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +0000882///
883const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000884 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +0000885 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +0000886}
887
Mike Stump11289f42009-09-09 15:08:12 +0000888SourceLocation BlockExpr::getCaretLocation() const {
889 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +0000890}
Mike Stump11289f42009-09-09 15:08:12 +0000891const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000892 return TheBlock->getBody();
893}
Mike Stump11289f42009-09-09 15:08:12 +0000894Stmt *BlockExpr::getBody() {
895 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +0000896}
Steve Naroff415d3d52008-10-08 17:01:13 +0000897
898
Chris Lattner1ec5f562007-06-27 05:38:08 +0000899//===----------------------------------------------------------------------===//
900// Generic Expression Routines
901//===----------------------------------------------------------------------===//
902
Chris Lattner237f2752009-02-14 07:37:35 +0000903/// isUnusedResultAWarning - Return true if this immediate expression should
904/// be warned about if the result is unused. If so, fill in Loc and Ranges
905/// with location to warn on and the source range[s] to report with the
906/// warning.
907bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +0000908 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +0000909 // Don't warn if the expr is type dependent. The type could end up
910 // instantiating to void.
911 if (isTypeDependent())
912 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattner1ec5f562007-06-27 05:38:08 +0000914 switch (getStmtClass()) {
915 default:
John McCallc493a732010-03-12 07:11:26 +0000916 if (getType()->isVoidType())
917 return false;
Chris Lattner237f2752009-02-14 07:37:35 +0000918 Loc = getExprLoc();
919 R1 = getSourceRange();
920 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000921 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000922 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +0000923 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000924 case UnaryOperatorClass: {
925 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000926
Chris Lattner1ec5f562007-06-27 05:38:08 +0000927 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +0000928 default: break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000929 case UnaryOperator::PostInc:
930 case UnaryOperator::PostDec:
931 case UnaryOperator::PreInc:
Chris Lattner237f2752009-02-14 07:37:35 +0000932 case UnaryOperator::PreDec: // ++/--
933 return false; // Not a warning.
Chris Lattnera44d1162007-06-27 05:58:59 +0000934 case UnaryOperator::Deref:
935 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000936 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000937 return false;
938 break;
Chris Lattnera44d1162007-06-27 05:58:59 +0000939 case UnaryOperator::Real:
940 case UnaryOperator::Imag:
941 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000942 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
943 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000944 return false;
945 break;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000946 case UnaryOperator::Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +0000947 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +0000948 }
Chris Lattner237f2752009-02-14 07:37:35 +0000949 Loc = UO->getOperatorLoc();
950 R1 = UO->getSubExpr()->getSourceRange();
951 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000952 }
Chris Lattnerae7a8342007-12-01 06:07:34 +0000953 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000954 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +0000955 switch (BO->getOpcode()) {
956 default:
957 break;
958 // Consider ',', '||', '&&' to have side effects if the LHS or RHS does.
959 case BinaryOperator::Comma:
960 // ((foo = <blah>), 0) is an idiom for hiding the result (and
961 // lvalue-ness) of an assignment written in a macro.
962 if (IntegerLiteral *IE =
963 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
964 if (IE->getValue() == 0)
965 return false;
966 case BinaryOperator::LAnd:
967 case BinaryOperator::LOr:
968 return (BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
969 BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall1e3715a2010-02-16 04:10:53 +0000970 }
Chris Lattner237f2752009-02-14 07:37:35 +0000971 if (BO->isAssignmentOp())
972 return false;
973 Loc = BO->getOperatorLoc();
974 R1 = BO->getLHS()->getSourceRange();
975 R2 = BO->getRHS()->getSourceRange();
976 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +0000977 }
Chris Lattner86928112007-08-25 02:00:02 +0000978 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +0000979 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000980 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000981
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000982 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +0000983 // The condition must be evaluated, but if either the LHS or RHS is a
984 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000985 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +0000986 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +0000987 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +0000988 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +0000989 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +0000990 }
991
Chris Lattnera44d1162007-06-27 05:58:59 +0000992 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +0000993 // If the base pointer or element is to a volatile pointer/field, accessing
994 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +0000995 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +0000996 return false;
997 Loc = cast<MemberExpr>(this)->getMemberLoc();
998 R1 = SourceRange(Loc, Loc);
999 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1000 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattner1ec5f562007-06-27 05:38:08 +00001002 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001003 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001004 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001005 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001006 return false;
1007 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1008 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1009 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1010 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001011
Chris Lattner1ec5f562007-06-27 05:38:08 +00001012 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001013 case CXXOperatorCallExprClass:
1014 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001015 // If this is a direct call, get the callee.
1016 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001017 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001018 // If the callee has attribute pure, const, or warn_unused_result, warn
1019 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001020 //
1021 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1022 // updated to match for QoI.
1023 if (FD->getAttr<WarnUnusedResultAttr>() ||
1024 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1025 Loc = CE->getCallee()->getLocStart();
1026 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattner1a6babf2009-10-13 04:53:48 +00001028 if (unsigned NumArgs = CE->getNumArgs())
1029 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1030 CE->getArg(NumArgs-1)->getLocEnd());
1031 return true;
1032 }
Chris Lattner237f2752009-02-14 07:37:35 +00001033 }
1034 return false;
1035 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001036
1037 case CXXTemporaryObjectExprClass:
1038 case CXXConstructExprClass:
1039 return false;
1040
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001041 case ObjCMessageExprClass: {
1042 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1043 const ObjCMethodDecl *MD = ME->getMethodDecl();
1044 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1045 Loc = getExprLoc();
1046 return true;
1047 }
Chris Lattner237f2752009-02-14 07:37:35 +00001048 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001051 case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send.
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001052#if 0
Mike Stump11289f42009-09-09 15:08:12 +00001053 const ObjCImplicitSetterGetterRefExpr *Ref =
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001054 cast<ObjCImplicitSetterGetterRefExpr>(this);
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001055 // FIXME: We really want the location of the '.' here.
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00001056 Loc = Ref->getLocation();
1057 R1 = SourceRange(Ref->getLocation(), Ref->getLocation());
1058 if (Ref->getBase())
1059 R2 = Ref->getBase()->getSourceRange();
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001060#else
1061 Loc = getExprLoc();
1062 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001063#endif
1064 return true;
1065 }
Chris Lattner944d3062008-07-26 19:51:01 +00001066 case StmtExprClass: {
1067 // Statement exprs don't logically have side effects themselves, but are
1068 // sometimes used in macros in ways that give them a type that is unused.
1069 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1070 // however, if the result of the stmt expr is dead, we don't want to emit a
1071 // warning.
1072 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
1073 if (!CS->body_empty())
1074 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001075 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001076
John McCallc493a732010-03-12 07:11:26 +00001077 if (getType()->isVoidType())
1078 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001079 Loc = cast<StmtExpr>(this)->getLParenLoc();
1080 R1 = getSourceRange();
1081 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001082 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001083 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001084 // If this is an explicit cast to void, allow it. People do this when they
1085 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001086 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001087 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001088 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1089 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1090 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001091 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001092 if (getType()->isVoidType())
1093 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001094 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001095
Anders Carlsson6aa50392009-11-17 17:11:23 +00001096 // If this is a cast to void or a constructor conversion, check the operand.
1097 // Otherwise, the result of the cast is unused.
1098 if (CE->getCastKind() == CastExpr::CK_ToVoid ||
1099 CE->getCastKind() == CastExpr::CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001100 return (cast<CastExpr>(this)->getSubExpr()
1101 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001102 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1103 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1104 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001107 case ImplicitCastExprClass:
1108 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001109 return (cast<ImplicitCastExpr>(this)
1110 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001111
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001112 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001113 return (cast<CXXDefaultArgExpr>(this)
1114 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001115
1116 case CXXNewExprClass:
1117 // FIXME: In theory, there might be new expressions that don't have side
1118 // effects (e.g. a placement new with an uninitialized POD).
1119 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001120 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001121 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001122 return (cast<CXXBindTemporaryExpr>(this)
1123 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Anders Carlsson24824e52009-05-17 21:11:30 +00001124 case CXXExprWithTemporariesClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001125 return (cast<CXXExprWithTemporaries>(this)
1126 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001127 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001128}
1129
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001130/// DeclCanBeLvalue - Determine whether the given declaration can be
1131/// an lvalue. This is a helper routine for isLvalue.
1132static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) {
Douglas Gregor5101c242008-12-05 18:15:24 +00001133 // C++ [temp.param]p6:
1134 // A non-type non-reference template-parameter is not an lvalue.
Mike Stump11289f42009-09-09 15:08:12 +00001135 if (const NonTypeTemplateParmDecl *NTTParm
Douglas Gregor5101c242008-12-05 18:15:24 +00001136 = dyn_cast<NonTypeTemplateParmDecl>(Decl))
1137 return NTTParm->getType()->isReferenceType();
1138
Douglas Gregor91f84212008-12-11 16:49:14 +00001139 return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) ||
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001140 // C++ 3.10p2: An lvalue refers to an object or function.
1141 (Ctx.getLangOptions().CPlusPlus &&
John McCall3d988d92009-12-02 08:47:38 +00001142 (isa<FunctionDecl>(Decl) || isa<FunctionTemplateDecl>(Decl)));
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001143}
1144
Steve Naroff475cca02007-05-14 17:19:29 +00001145/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
1146/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +00001147/// - name, where name must be a variable
1148/// - e[i]
1149/// - (e), where e must be an lvalue
1150/// - e.name, where e must be an lvalue
1151/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +00001152/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +00001153/// - string-constant
Chris Lattner595db862007-10-30 22:53:42 +00001154/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendlingdfc81072007-07-17 03:52:31 +00001155/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +00001156///
Chris Lattner67315442008-07-26 21:30:36 +00001157Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001158 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
1159
1160 isLvalueResult Res = isLvalueInternal(Ctx);
1161 if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus)
1162 return Res;
1163
Douglas Gregor9a657932008-10-21 23:43:52 +00001164 // first, check the type (C99 6.3.2.1). Expressions with function
1165 // type in C are not lvalues, but they can be lvalues in C++.
Douglas Gregor9b146582009-07-08 20:55:45 +00001166 if (TR->isFunctionType() || TR == Ctx.OverloadTy)
Steve Naroff9358c712007-05-27 23:58:33 +00001167 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +00001168
Steve Naroff1018ea32008-02-10 01:39:04 +00001169 // Allow qualified void which is an incomplete type other than void (yuck).
John McCall8ccfcb52009-09-24 19:53:00 +00001170 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers())
Steve Naroff1018ea32008-02-10 01:39:04 +00001171 return LV_IncompleteVoidType;
1172
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001173 return LV_Valid;
1174}
Bill Wendlingdfc81072007-07-17 03:52:31 +00001175
Eli Friedmanb8c4fd82009-05-03 22:36:05 +00001176// Check whether the expression can be sanely treated like an l-value
1177Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const {
Steve Naroff47500512007-04-19 23:00:49 +00001178 switch (getStmtClass()) {
Fariborz Jahanian531c16f2009-12-09 23:35:29 +00001179 case ObjCIsaExprClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001180 case StringLiteralClass: // C99 6.5.1p4
1181 case ObjCEncodeExprClass: // @encode behaves like its string in every way.
Anders Carlsson7a9a38a2007-11-30 22:47:59 +00001182 return LV_Valid;
Steve Naroff5dd642e2007-05-14 18:14:51 +00001183 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +00001184 // For vectors, make sure base is an lvalue (i.e. not a function call).
1185 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner67315442008-07-26 21:30:36 +00001186 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Steve Naroff9358c712007-05-27 23:58:33 +00001187 return LV_Valid;
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001188 case DeclRefExprClass: { // C99 6.5.1p2
Douglas Gregor4b62ec62008-10-22 15:04:37 +00001189 const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
1190 if (DeclCanBeLvalue(RefdDecl, Ctx))
Steve Naroff9358c712007-05-27 23:58:33 +00001191 return LV_Valid;
1192 break;
Chris Lattner5696e7b2008-06-17 18:05:57 +00001193 }
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001194 case BlockDeclRefExprClass: {
1195 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroffba756cb2008-09-26 14:41:28 +00001196 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroff8de9c3a2008-09-05 22:11:13 +00001197 return LV_Valid;
1198 break;
1199 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001200 case MemberExprClass: {
Steve Naroff47500512007-04-19 23:00:49 +00001201 const MemberExpr *m = cast<MemberExpr>(this);
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001202 if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4:
1203 NamedDecl *Member = m->getMemberDecl();
1204 // C++ [expr.ref]p4:
1205 // If E2 is declared to have type "reference to T", then E1.E2
1206 // is an lvalue.
1207 if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
1208 if (Value->getType()->isReferenceType())
1209 return LV_Valid;
1210
1211 // -- If E2 is a static data member [...] then E1.E2 is an lvalue.
Douglas Gregor212cab32009-03-11 20:22:50 +00001212 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001213 return LV_Valid;
1214
1215 // -- If E2 is a non-static data member [...]. If E1 is an
1216 // lvalue, then E1.E2 is an lvalue.
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001217 if (isa<FieldDecl>(Member)) {
1218 if (m->isArrow())
1219 return LV_Valid;
Fariborz Jahaniane5c118f2010-02-12 21:02:28 +00001220 return m->getBase()->isLvalue(Ctx);
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001221 }
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001222
1223 // -- If it refers to a static member function [...], then
1224 // E1.E2 is an lvalue.
1225 // -- Otherwise, if E1.E2 refers to a non-static member
1226 // function [...], then E1.E2 is not an lvalue.
1227 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
1228 return Method->isStatic()? LV_Valid : LV_MemberFunction;
1229
1230 // -- If E2 is a member enumerator [...], the expression E1.E2
1231 // is not an lvalue.
1232 if (isa<EnumConstantDecl>(Member))
1233 return LV_InvalidExpression;
1234
1235 // Not an lvalue.
1236 return LV_InvalidExpression;
Mike Stump11289f42009-09-09 15:08:12 +00001237 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001238
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001239 // C99 6.5.2.3p4
Fariborz Jahaniane8d28902009-12-15 23:59:41 +00001240 if (m->isArrow())
1241 return LV_Valid;
1242 Expr *BaseExp = m->getBase();
Fariborz Jahanian8342e572010-03-18 18:50:41 +00001243 if (BaseExp->getStmtClass() == ObjCPropertyRefExprClass ||
1244 BaseExp->getStmtClass() == ObjCImplicitSetterGetterRefExprClass)
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001245 return LV_SubObjCPropertySetting;
Alexis Hunta8136cc2010-05-05 15:23:54 +00001246 return
1247 BaseExp->isLvalue(Ctx);
Anton Korobeynikovb76cda02007-07-12 15:26:50 +00001248 }
Chris Lattner595db862007-10-30 22:53:42 +00001249 case UnaryOperatorClass:
Steve Naroff9358c712007-05-27 23:58:33 +00001250 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner595db862007-10-30 22:53:42 +00001251 return LV_Valid; // C99 6.5.3p4
1252
1253 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattnerec8996d2008-07-25 18:07:19 +00001254 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
1255 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner67315442008-07-26 21:30:36 +00001256 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Douglas Gregord08452f2008-11-19 15:42:04 +00001257
1258 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1
1259 (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc ||
1260 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec))
1261 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +00001262 break;
Douglas Gregora11693b2008-11-12 17:17:38 +00001263 case ImplicitCastExprClass:
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001264 if (cast<ImplicitCastExpr>(this)->isLvalueCast())
1265 return LV_Valid;
1266
1267 // If this is a conversion to a class temporary, make a note of
1268 // that.
1269 if (Ctx.getLangOptions().CPlusPlus && getType()->isRecordType())
1270 return LV_ClassTemporary;
1271
1272 break;
Steve Naroff475cca02007-05-14 17:19:29 +00001273 case ParenExprClass: // C99 6.5.1p5
Chris Lattner67315442008-07-26 21:30:36 +00001274 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Douglas Gregora11693b2008-11-12 17:17:38 +00001275 case BinaryOperatorClass:
1276 case CompoundAssignOperatorClass: {
1277 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
Douglas Gregor40412ac2008-11-19 17:17:41 +00001278
1279 if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1
1280 BinOp->getOpcode() == BinaryOperator::Comma)
1281 return BinOp->getRHS()->isLvalue(Ctx);
1282
Sebastian Redl112a97662009-02-07 00:15:38 +00001283 // C++ [expr.mptr.oper]p6
Alexis Hunta8136cc2010-05-05 15:23:54 +00001284 // The result of a .* expression is an lvalue only if its first operand is
1285 // an lvalue and its second operand is a pointer to data member.
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001286 if (BinOp->getOpcode() == BinaryOperator::PtrMemD &&
Sebastian Redl112a97662009-02-07 00:15:38 +00001287 !BinOp->getType()->isFunctionType())
1288 return BinOp->getLHS()->isLvalue(Ctx);
1289
Alexis Hunta8136cc2010-05-05 15:23:54 +00001290 // The result of an ->* expression is an lvalue only if its second operand
Fariborz Jahanian03b4f662009-10-08 18:00:39 +00001291 // is a pointer to data member.
1292 if (BinOp->getOpcode() == BinaryOperator::PtrMemI &&
1293 !BinOp->getType()->isFunctionType()) {
1294 QualType Ty = BinOp->getRHS()->getType();
1295 if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType())
1296 return LV_Valid;
1297 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001298
Douglas Gregor58e008d2008-11-13 20:12:29 +00001299 if (!BinOp->isAssignmentOp())
Douglas Gregora11693b2008-11-12 17:17:38 +00001300 return LV_InvalidExpression;
1301
Douglas Gregor58e008d2008-11-13 20:12:29 +00001302 if (Ctx.getLangOptions().CPlusPlus)
Mike Stump11289f42009-09-09 15:08:12 +00001303 // C++ [expr.ass]p1:
Douglas Gregor58e008d2008-11-13 20:12:29 +00001304 // The result of an assignment operation [...] is an lvalue.
1305 return LV_Valid;
1306
1307
1308 // C99 6.5.16:
1309 // An assignment expression [...] is not an lvalue.
1310 return LV_InvalidExpression;
Douglas Gregora11693b2008-11-12 17:17:38 +00001311 }
Mike Stump11289f42009-09-09 15:08:12 +00001312 case CallExprClass:
Douglas Gregor97fd6e22008-12-22 05:46:06 +00001313 case CXXOperatorCallExprClass:
1314 case CXXMemberCallExprClass: {
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001315 // C++0x [expr.call]p10
Douglas Gregor6b754842008-10-28 00:22:11 +00001316 // A function call is an lvalue if and only if the result type
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001317 // is an lvalue reference.
Anders Carlsson00a27592009-05-26 04:57:27 +00001318 QualType ReturnType = cast<CallExpr>(this)->getCallReturnType();
1319 if (ReturnType->isLValueReferenceType())
1320 return LV_Valid;
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001321
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001322 // If the function is returning a class temporary, make a note of
1323 // that.
1324 if (Ctx.getLangOptions().CPlusPlus && ReturnType->isRecordType())
1325 return LV_ClassTemporary;
1326
Douglas Gregor6b754842008-10-28 00:22:11 +00001327 break;
1328 }
Steve Naroff2644aaf2007-12-05 04:00:10 +00001329 case CompoundLiteralExprClass: // C99 6.5.2.5p5
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001330 // FIXME: Is this what we want in C++?
Steve Naroff2644aaf2007-12-05 04:00:10 +00001331 return LV_Valid;
Chris Lattner053441f2008-12-12 05:35:08 +00001332 case ChooseExprClass:
1333 // __builtin_choose_expr is an lvalue if the selected operand is.
Eli Friedmane0a5b8b2009-03-04 05:52:32 +00001334 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx);
Nate Begemance4d7fc2008-04-18 23:10:10 +00001335 case ExtVectorElementExprClass:
1336 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +00001337 return LV_DuplicateVectorComponents;
1338 return LV_Valid;
Steve Naroffb3423612007-11-12 14:34:27 +00001339 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
1340 return LV_Valid;
Steve Naroff66002282008-05-30 23:23:16 +00001341 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
1342 return LV_Valid;
Enea Zaffanellaf2059772010-04-27 07:38:32 +00001343 case ObjCImplicitSetterGetterRefExprClass:
1344 // FIXME: check if read-only property.
Chris Lattner053441f2008-12-12 05:35:08 +00001345 return LV_Valid;
Chris Lattner6307f192008-08-10 01:53:14 +00001346 case PredefinedExprClass:
Douglas Gregor97a9c812008-11-04 14:32:21 +00001347 return LV_Valid;
John McCalld14a8642009-11-21 08:51:07 +00001348 case UnresolvedLookupExprClass:
Douglas Gregor980fb162010-04-29 18:24:40 +00001349 case UnresolvedMemberExprClass:
John McCalld14a8642009-11-21 08:51:07 +00001350 return LV_Valid;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001351 case CXXDefaultArgExprClass:
Chris Lattner67315442008-07-26 21:30:36 +00001352 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Douglas Gregorf19b2312008-10-28 15:36:24 +00001353 case CStyleCastExprClass:
Douglas Gregor6b754842008-10-28 00:22:11 +00001354 case CXXFunctionalCastExprClass:
1355 case CXXStaticCastExprClass:
1356 case CXXDynamicCastExprClass:
1357 case CXXReinterpretCastExprClass:
1358 case CXXConstCastExprClass:
1359 // The result of an explicit cast is an lvalue if the type we are
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001360 // casting to is an lvalue reference type. See C++ [expr.cast]p1,
Douglas Gregor6b754842008-10-28 00:22:11 +00001361 // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2,
1362 // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1.
Sebastian Redl0f8b23f2009-03-16 23:22:08 +00001363 if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->
1364 isLValueReferenceType())
Douglas Gregor6b754842008-10-28 00:22:11 +00001365 return LV_Valid;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001366
1367 // If this is a conversion to a class temporary, make a note of
1368 // that.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001369 if (Ctx.getLangOptions().CPlusPlus &&
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001370 cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isRecordType())
1371 return LV_ClassTemporary;
1372
Douglas Gregor6b754842008-10-28 00:22:11 +00001373 break;
Sebastian Redlc4704762008-11-11 11:37:55 +00001374 case CXXTypeidExprClass:
1375 // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ...
1376 return LV_Valid;
Anders Carlsson8c84c202009-08-16 03:42:12 +00001377 case CXXBindTemporaryExprClass:
1378 return cast<CXXBindTemporaryExpr>(this)->getSubExpr()->
1379 isLvalueInternal(Ctx);
Anders Carlssonba6c4372010-01-29 02:39:32 +00001380 case CXXBindReferenceExprClass:
1381 // Something that's bound to a reference is always an lvalue.
1382 return LV_Valid;
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001383 case ConditionalOperatorClass: {
1384 // Complicated handling is only for C++.
1385 if (!Ctx.getLangOptions().CPlusPlus)
1386 return LV_InvalidExpression;
1387
1388 // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is
1389 // everywhere there's an object converted to an rvalue. Also, any other
1390 // casts should be wrapped by ImplicitCastExprs. There's just the special
1391 // case involving throws to work out.
1392 const ConditionalOperator *Cond = cast<ConditionalOperator>(this);
Douglas Gregor115652d2009-05-19 20:13:50 +00001393 Expr *True = Cond->getTrueExpr();
1394 Expr *False = Cond->getFalseExpr();
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001395 // C++0x 5.16p2
1396 // If either the second or the third operand has type (cv) void, [...]
1397 // the result [...] is an rvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001398 if (True->getType()->isVoidType() || False->getType()->isVoidType())
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001399 return LV_InvalidExpression;
1400
1401 // Both sides must be lvalues for the result to be an lvalue.
Douglas Gregor115652d2009-05-19 20:13:50 +00001402 if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid)
Sebastian Redl5775af1a2009-04-17 16:30:52 +00001403 return LV_InvalidExpression;
1404
1405 // That's it.
1406 return LV_Valid;
1407 }
1408
Douglas Gregor5103eff2009-12-19 07:07:47 +00001409 case Expr::CXXExprWithTemporariesClass:
1410 return cast<CXXExprWithTemporaries>(this)->getSubExpr()->isLvalue(Ctx);
1411
1412 case Expr::ObjCMessageExprClass:
1413 if (const ObjCMethodDecl *Method
1414 = cast<ObjCMessageExpr>(this)->getMethodDecl())
1415 if (Method->getResultType()->isLValueReferenceType())
1416 return LV_Valid;
1417 break;
1418
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001419 case Expr::CXXConstructExprClass:
1420 case Expr::CXXTemporaryObjectExprClass:
1421 case Expr::CXXZeroInitValueExprClass:
1422 return LV_ClassTemporary;
1423
Steve Naroff9358c712007-05-27 23:58:33 +00001424 default:
1425 break;
Steve Naroff47500512007-04-19 23:00:49 +00001426 }
Steve Naroff9358c712007-05-27 23:58:33 +00001427 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +00001428}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001429
Steve Naroff475cca02007-05-14 17:19:29 +00001430/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
1431/// does not have an incomplete type, does not have a const-qualified type, and
Mike Stump11289f42009-09-09 15:08:12 +00001432/// if it is a structure or union, does not have any member (including,
Steve Naroff475cca02007-05-14 17:19:29 +00001433/// recursively, any member or element of all contained aggregates or unions)
1434/// with a const-qualified type.
Mike Stump11289f42009-09-09 15:08:12 +00001435Expr::isModifiableLvalueResult
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001436Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
Chris Lattner67315442008-07-26 21:30:36 +00001437 isLvalueResult lvalResult = isLvalue(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001438
Steve Naroff9358c712007-05-27 23:58:33 +00001439 switch (lvalResult) {
Mike Stump11289f42009-09-09 15:08:12 +00001440 case LV_Valid:
Douglas Gregor293a3c62008-10-22 00:03:08 +00001441 // C++ 3.10p11: Functions cannot be modified, but pointers to
1442 // functions can be modifiable.
1443 if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
1444 return MLV_NotObjectType;
1445 break;
1446
Chris Lattner1ec5f562007-06-27 05:38:08 +00001447 case LV_NotObjectType: return MLV_NotObjectType;
1448 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +00001449 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001450 case LV_InvalidExpression:
1451 // If the top level is a C-style cast, and the subexpression is a valid
1452 // lvalue, then this is probably a use of the old-school "cast as lvalue"
1453 // GCC extension. We don't support it, but we want to produce good
1454 // diagnostics when it happens so that the user knows why.
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001455 if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) {
1456 if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) {
1457 if (Loc)
1458 *Loc = CE->getLParenLoc();
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001459 return MLV_LValueCast;
Daniel Dunbarc2223ab2009-04-15 00:08:05 +00001460 }
1461 }
Chris Lattner9b3bbe92008-11-17 19:51:54 +00001462 return MLV_InvalidExpression;
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001463 case LV_MemberFunction: return MLV_MemberFunction;
Fariborz Jahanian13b97822010-02-11 01:11:34 +00001464 case LV_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
Douglas Gregorb154fdc2010-02-16 21:39:57 +00001465 case LV_ClassTemporary:
1466 return MLV_ClassTemporary;
Steve Naroff9358c712007-05-27 23:58:33 +00001467 }
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001468
1469 // The following is illegal:
1470 // void takeclosure(void (^C)(void));
1471 // void func() { int x = 1; takeclosure(^{ x = 7; }); }
1472 //
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001473 if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) {
Eli Friedmane8dd7b32009-03-22 23:26:56 +00001474 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
1475 return MLV_NotBlockQualified;
1476 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001477
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001478 // Assigning to an 'implicit' property?
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001479 if (const ObjCImplicitSetterGetterRefExpr* Expr =
Fariborz Jahaniancb1c1912009-09-14 16:40:48 +00001480 dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) {
1481 if (Expr->getSetterMethod() == 0)
1482 return MLV_NoSetterProperty;
1483 }
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001484
Chris Lattner7adf0762008-08-04 07:31:14 +00001485 QualType CT = Ctx.getCanonicalType(getType());
Mike Stump11289f42009-09-09 15:08:12 +00001486
Chris Lattner7adf0762008-08-04 07:31:14 +00001487 if (CT.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +00001488 return MLV_ConstQualified;
Chris Lattner7adf0762008-08-04 07:31:14 +00001489 if (CT->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +00001490 return MLV_ArrayType;
Chris Lattner7adf0762008-08-04 07:31:14 +00001491 if (CT->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +00001492 return MLV_IncompleteType;
Mike Stump11289f42009-09-09 15:08:12 +00001493
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001494 if (const RecordType *r = CT->getAs<RecordType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001495 if (r->hasConstFields())
Steve Naroff9358c712007-05-27 23:58:33 +00001496 return MLV_ConstQualified;
1497 }
Mike Stump11289f42009-09-09 15:08:12 +00001498
Mike Stump11289f42009-09-09 15:08:12 +00001499 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +00001500}
1501
Fariborz Jahanian07735332009-02-22 18:40:18 +00001502/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001503/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001504bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001505 switch (getStmtClass()) {
1506 default:
1507 return false;
1508 case ObjCIvarRefExprClass:
1509 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001510 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001511 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001512 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001513 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001514 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001515 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001516 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001517 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001518 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001519 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001520 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1521 if (VD->hasGlobalStorage())
1522 return true;
1523 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001524 // dereferencing to a pointer is always a gc'able candidate,
1525 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001526 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001527 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001528 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001529 return false;
1530 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001531 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001532 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001533 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001534 }
1535 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001536 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001537 }
1538}
Ted Kremenekfff70962008-01-17 16:57:34 +00001539Expr* Expr::IgnoreParens() {
1540 Expr* E = this;
1541 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
1542 E = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001543
Ted Kremenekfff70962008-01-17 16:57:34 +00001544 return E;
1545}
1546
Chris Lattnerf2660962008-02-13 01:02:39 +00001547/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1548/// or CastExprs or ImplicitCastExprs, returning their operand.
1549Expr *Expr::IgnoreParenCasts() {
1550 Expr *E = this;
1551 while (true) {
1552 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1553 E = P->getSubExpr();
1554 else if (CastExpr *P = dyn_cast<CastExpr>(E))
1555 E = P->getSubExpr();
Chris Lattnerf2660962008-02-13 01:02:39 +00001556 else
1557 return E;
1558 }
1559}
1560
John McCalleebc8322010-05-05 22:59:52 +00001561Expr *Expr::IgnoreParenImpCasts() {
1562 Expr *E = this;
1563 while (true) {
1564 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
1565 E = P->getSubExpr();
1566 else if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E))
1567 E = P->getSubExpr();
1568 else
1569 return E;
1570 }
1571}
1572
Chris Lattneref26c772009-03-13 17:28:01 +00001573/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1574/// value (including ptr->int casts of the same size). Strip off any
1575/// ParenExpr or CastExprs, returning their operand.
1576Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1577 Expr *E = this;
1578 while (true) {
1579 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1580 E = P->getSubExpr();
1581 continue;
1582 }
Mike Stump11289f42009-09-09 15:08:12 +00001583
Chris Lattneref26c772009-03-13 17:28:01 +00001584 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1585 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
1586 // ptr<->int casts of the same width. We also ignore all identify casts.
1587 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001588
Chris Lattneref26c772009-03-13 17:28:01 +00001589 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1590 E = SE;
1591 continue;
1592 }
Mike Stump11289f42009-09-09 15:08:12 +00001593
Chris Lattneref26c772009-03-13 17:28:01 +00001594 if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
1595 (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
1596 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1597 E = SE;
1598 continue;
1599 }
1600 }
Mike Stump11289f42009-09-09 15:08:12 +00001601
Chris Lattneref26c772009-03-13 17:28:01 +00001602 return E;
1603 }
1604}
1605
Douglas Gregord196a582009-12-14 19:27:10 +00001606bool Expr::isDefaultArgument() const {
1607 const Expr *E = this;
1608 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1609 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001610
Douglas Gregord196a582009-12-14 19:27:10 +00001611 return isa<CXXDefaultArgExpr>(E);
1612}
Chris Lattneref26c772009-03-13 17:28:01 +00001613
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001614/// \brief Skip over any no-op casts and any temporary-binding
1615/// expressions.
1616static const Expr *skipTemporaryBindingsAndNoOpCasts(const Expr *E) {
1617 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1618 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1619 E = ICE->getSubExpr();
1620 else
1621 break;
1622 }
1623
1624 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1625 E = BE->getSubExpr();
1626
1627 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1628 if (ICE->getCastKind() == CastExpr::CK_NoOp)
1629 E = ICE->getSubExpr();
1630 else
1631 break;
1632 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001633
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001634 return E;
1635}
1636
1637const Expr *Expr::getTemporaryObject() const {
1638 const Expr *E = skipTemporaryBindingsAndNoOpCasts(this);
1639
1640 // A cast can produce a temporary object. The object's construction
1641 // is represented as a CXXConstructExpr.
1642 if (const CastExpr *Cast = dyn_cast<CastExpr>(E)) {
1643 // Only user-defined and constructor conversions can produce
1644 // temporary objects.
1645 if (Cast->getCastKind() != CastExpr::CK_ConstructorConversion &&
1646 Cast->getCastKind() != CastExpr::CK_UserDefinedConversion)
1647 return 0;
1648
1649 // Strip off temporary bindings and no-op casts.
1650 const Expr *Sub = skipTemporaryBindingsAndNoOpCasts(Cast->getSubExpr());
1651
1652 // If this is a constructor conversion, see if we have an object
1653 // construction.
1654 if (Cast->getCastKind() == CastExpr::CK_ConstructorConversion)
1655 return dyn_cast<CXXConstructExpr>(Sub);
1656
1657 // If this is a user-defined conversion, see if we have a call to
1658 // a function that itself returns a temporary object.
1659 if (Cast->getCastKind() == CastExpr::CK_UserDefinedConversion)
1660 if (const CallExpr *CE = dyn_cast<CallExpr>(Sub))
1661 if (CE->getCallReturnType()->isRecordType())
1662 return CE;
1663
1664 return 0;
1665 }
1666
1667 // A call returning a class type returns a temporary.
1668 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1669 if (CE->getCallReturnType()->isRecordType())
1670 return CE;
1671
1672 return 0;
1673 }
1674
1675 // Explicit temporary object constructors create temporaries.
1676 return dyn_cast<CXXTemporaryObjectExpr>(E);
1677}
1678
Douglas Gregor4619e432008-12-05 23:32:09 +00001679/// hasAnyTypeDependentArguments - Determines if any of the expressions
1680/// in Exprs is type-dependent.
1681bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
1682 for (unsigned I = 0; I < NumExprs; ++I)
1683 if (Exprs[I]->isTypeDependent())
1684 return true;
1685
1686 return false;
1687}
1688
1689/// hasAnyValueDependentArguments - Determines if any of the expressions
1690/// in Exprs is value-dependent.
1691bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
1692 for (unsigned I = 0; I < NumExprs; ++I)
1693 if (Exprs[I]->isValueDependent())
1694 return true;
1695
1696 return false;
1697}
1698
Eli Friedman7139af42009-01-25 02:32:41 +00001699bool Expr::isConstantInitializer(ASTContext &Ctx) const {
Eli Friedman384da272009-01-25 03:12:18 +00001700 // This function is attempting whether an expression is an initializer
1701 // which can be evaluated at compile-time. isEvaluatable handles most
1702 // of the cases, but it can't deal with some initializer-specific
1703 // expressions, and it can't deal with aggregates; we deal with those here,
1704 // and fall back to isEvaluatable for the other cases.
1705
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001706 // FIXME: This function assumes the variable being assigned to
1707 // isn't a reference type!
1708
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001709 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00001710 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001711 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00001712 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00001713 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001714 return true;
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001715 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001716 // This handles gcc's extension that allows global initializers like
1717 // "struct x {int x;} x = (struct x) {};".
1718 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001719 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
Eli Friedman7139af42009-01-25 02:32:41 +00001720 return Exp->isConstantInitializer(Ctx);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00001721 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001722 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00001723 // FIXME: This doesn't deal with fields with reference types correctly.
1724 // FIXME: This incorrectly allows pointers cast to integers to be assigned
1725 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001726 const InitListExpr *Exp = cast<InitListExpr>(this);
1727 unsigned numInits = Exp->getNumInits();
1728 for (unsigned i = 0; i < numInits; i++) {
Mike Stump11289f42009-09-09 15:08:12 +00001729 if (!Exp->getInit(i)->isConstantInitializer(Ctx))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001730 return false;
1731 }
Eli Friedman384da272009-01-25 03:12:18 +00001732 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001733 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00001734 case ImplicitValueInitExprClass:
1735 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00001736 case ParenExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001737 return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Eli Friedman384da272009-01-25 03:12:18 +00001738 case UnaryOperatorClass: {
1739 const UnaryOperator* Exp = cast<UnaryOperator>(this);
1740 if (Exp->getOpcode() == UnaryOperator::Extension)
1741 return Exp->getSubExpr()->isConstantInitializer(Ctx);
1742 break;
1743 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00001744 case BinaryOperatorClass: {
1745 // Special case &&foo - &&bar. It would be nice to generalize this somehow
1746 // but this handles the common case.
1747 const BinaryOperator *Exp = cast<BinaryOperator>(this);
1748 if (Exp->getOpcode() == BinaryOperator::Sub &&
1749 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
1750 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
1751 return true;
1752 break;
1753 }
Chris Lattner1f02e052009-04-21 05:19:11 +00001754 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00001755 case CStyleCastExprClass:
1756 // Handle casts with a destination that's a struct or union; this
1757 // deals with both the gcc no-op struct cast extension and the
1758 // cast-to-union extension.
1759 if (getType()->isRecordType())
1760 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001761
Chris Lattnera2f9bd52009-10-13 22:12:09 +00001762 // Integer->integer casts can be handled here, which is important for
1763 // things like (int)(&&x-&&y). Scary but true.
1764 if (getType()->isIntegerType() &&
1765 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
1766 return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001767
Eli Friedman384da272009-01-25 03:12:18 +00001768 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00001769 }
Eli Friedman384da272009-01-25 03:12:18 +00001770 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00001771}
1772
Chris Lattner7eef9192007-05-24 01:23:49 +00001773/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1774/// integer constant expression with the value zero, or if this is one that is
1775/// cast to void*.
Douglas Gregor56751b52009-09-25 04:25:58 +00001776bool Expr::isNullPointerConstant(ASTContext &Ctx,
1777 NullPointerConstantValueDependence NPC) const {
1778 if (isValueDependent()) {
1779 switch (NPC) {
1780 case NPC_NeverValueDependent:
1781 assert(false && "Unexpected value dependent expression!");
1782 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001783
Douglas Gregor56751b52009-09-25 04:25:58 +00001784 case NPC_ValueDependentIsNull:
1785 return isTypeDependent() || getType()->isIntegralType();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001786
Douglas Gregor56751b52009-09-25 04:25:58 +00001787 case NPC_ValueDependentIsNotNull:
1788 return false;
1789 }
1790 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00001791
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001792 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00001793 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00001794 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001795 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001796 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001797 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001798 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001799 Pointee->isVoidType() && // to void*
1800 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00001801 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00001802 }
Steve Naroffada7d422007-05-20 17:54:12 +00001803 }
Steve Naroff4871fe02008-01-14 16:10:57 +00001804 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1805 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00001806 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00001807 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1808 // Accept ((void*)0) as a null pointer constant, as many other
1809 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00001810 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00001811 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00001812 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001813 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00001814 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00001815 } else if (isa<GNUNullExpr>(this)) {
1816 // The GNU __null extension is always a null pointer constant.
1817 return true;
Steve Naroff09035312008-01-14 02:53:34 +00001818 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00001819
Sebastian Redl576fd422009-05-10 18:38:11 +00001820 // C++0x nullptr_t is always a null pointer constant.
1821 if (getType()->isNullPtrType())
1822 return true;
1823
Steve Naroff4871fe02008-01-14 16:10:57 +00001824 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001825 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00001826 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroff4871fe02008-01-14 16:10:57 +00001827 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001828
Chris Lattner1abbd412007-06-08 17:58:43 +00001829 // If we have an integer constant expression, we need to *evaluate* it and
1830 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00001831 llvm::APSInt Result;
1832 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001833}
Steve Narofff7a5da12007-07-28 23:10:27 +00001834
Douglas Gregor71235ec2009-05-02 02:18:30 +00001835FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00001836 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00001837
Douglas Gregor65eb86e2010-01-29 19:14:02 +00001838 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1839 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1840 E = ICE->getSubExpr()->IgnoreParens();
1841 else
1842 break;
1843 }
1844
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001845 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00001846 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00001847 if (Field->isBitField())
1848 return Field;
1849
1850 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
1851 if (BinOp->isAssignmentOp() && BinOp->getLHS())
1852 return BinOp->getLHS()->getBitField();
1853
1854 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00001855}
1856
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001857bool Expr::refersToVectorElement() const {
1858 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001859
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001860 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
1861 if (ICE->isLvalueCast() && ICE->getCastKind() == CastExpr::CK_NoOp)
1862 E = ICE->getSubExpr()->IgnoreParens();
1863 else
1864 break;
1865 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001866
Anders Carlsson8abde4b2010-01-31 17:18:49 +00001867 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
1868 return ASE->getBase()->getType()->isVectorType();
1869
1870 if (isa<ExtVectorElementExpr>(E))
1871 return true;
1872
1873 return false;
1874}
1875
Chris Lattnerb8211f62009-02-16 22:14:05 +00001876/// isArrow - Return true if the base expression is a pointer to vector,
1877/// return false if the base expression is a vector.
1878bool ExtVectorElementExpr::isArrow() const {
1879 return getBase()->getType()->isPointerType();
1880}
1881
Nate Begemance4d7fc2008-04-18 23:10:10 +00001882unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00001883 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00001884 return VT->getNumElements();
1885 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00001886}
1887
Nate Begemanf322eab2008-05-09 06:41:27 +00001888/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00001889bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00001890 // FIXME: Refactor this code to an accessor on the AST node which returns the
1891 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00001892 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00001893
1894 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001895 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00001896 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001897
Nate Begeman7e5185b2009-01-18 02:01:21 +00001898 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001899 if (Comp[0] == 's' || Comp[0] == 'S')
1900 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001901
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001902 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
1903 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00001904 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00001905
Steve Naroff0d595ca2007-07-30 03:29:09 +00001906 return false;
1907}
Chris Lattner885b4952007-08-02 23:36:59 +00001908
Nate Begemanf322eab2008-05-09 06:41:27 +00001909/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00001910void ExtVectorElementExpr::getEncodedElementAccess(
1911 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001912 llvm::StringRef Comp = Accessor->getName();
1913 if (Comp[0] == 's' || Comp[0] == 'S')
1914 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001915
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001916 bool isHi = Comp == "hi";
1917 bool isLo = Comp == "lo";
1918 bool isEven = Comp == "even";
1919 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00001920
Nate Begemanf322eab2008-05-09 06:41:27 +00001921 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1922 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00001923
Nate Begemanf322eab2008-05-09 06:41:27 +00001924 if (isHi)
1925 Index = e + i;
1926 else if (isLo)
1927 Index = i;
1928 else if (isEven)
1929 Index = 2 * i;
1930 else if (isOdd)
1931 Index = 2 * i + 1;
1932 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00001933 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00001934
Nate Begemand3862152008-05-13 21:03:02 +00001935 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00001936 }
Nate Begemanf322eab2008-05-09 06:41:27 +00001937}
1938
Douglas Gregor9a129192010-04-21 00:45:42 +00001939ObjCMessageExpr::ObjCMessageExpr(QualType T,
1940 SourceLocation LBracLoc,
1941 SourceLocation SuperLoc,
1942 bool IsInstanceSuper,
1943 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001944 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001945 ObjCMethodDecl *Method,
1946 Expr **Args, unsigned NumArgs,
1947 SourceLocation RBracLoc)
1948 : Expr(ObjCMessageExprClass, T, /*TypeDependent=*/false,
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001949 /*ValueDependent=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00001950 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
1951 HasMethod(Method != 0), SuperLoc(SuperLoc),
1952 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1953 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001954 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00001955{
Douglas Gregor9a129192010-04-21 00:45:42 +00001956 setReceiverPointer(SuperType.getAsOpaquePtr());
1957 if (NumArgs)
1958 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001959}
1960
Douglas Gregor9a129192010-04-21 00:45:42 +00001961ObjCMessageExpr::ObjCMessageExpr(QualType T,
1962 SourceLocation LBracLoc,
1963 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001964 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001965 ObjCMethodDecl *Method,
1966 Expr **Args, unsigned NumArgs,
1967 SourceLocation RBracLoc)
1968 : Expr(ObjCMessageExprClass, T, T->isDependentType(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001969 (T->isDependentType() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001970 hasAnyValueDependentArguments(Args, NumArgs))),
1971 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
1972 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1973 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001974 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001975{
1976 setReceiverPointer(Receiver);
1977 if (NumArgs)
1978 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00001979}
1980
Douglas Gregor9a129192010-04-21 00:45:42 +00001981ObjCMessageExpr::ObjCMessageExpr(QualType T,
1982 SourceLocation LBracLoc,
1983 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001984 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00001985 ObjCMethodDecl *Method,
1986 Expr **Args, unsigned NumArgs,
1987 SourceLocation RBracLoc)
Douglas Gregorc298ffc2010-04-22 16:44:27 +00001988 : Expr(ObjCMessageExprClass, T, Receiver->isTypeDependent(),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001989 (Receiver->isTypeDependent() ||
Douglas Gregor9a129192010-04-21 00:45:42 +00001990 hasAnyValueDependentArguments(Args, NumArgs))),
1991 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
1992 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
1993 : Sel.getAsOpaquePtr())),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001994 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00001995{
1996 setReceiverPointer(Receiver);
1997 if (NumArgs)
1998 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Chris Lattner7ec71da2009-04-26 00:44:05 +00001999}
2000
Douglas Gregor9a129192010-04-21 00:45:42 +00002001ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2002 SourceLocation LBracLoc,
2003 SourceLocation SuperLoc,
2004 bool IsInstanceSuper,
2005 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002006 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002007 ObjCMethodDecl *Method,
2008 Expr **Args, unsigned NumArgs,
2009 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002010 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002011 NumArgs * sizeof(Expr *);
2012 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2013 return new (Mem) ObjCMessageExpr(T, LBracLoc, SuperLoc, IsInstanceSuper,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002014 SuperType, Sel, Method, Args, NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002015 RBracLoc);
2016}
2017
2018ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2019 SourceLocation LBracLoc,
2020 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002021 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002022 ObjCMethodDecl *Method,
2023 Expr **Args, unsigned NumArgs,
2024 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002025 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002026 NumArgs * sizeof(Expr *);
2027 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002028 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002029 NumArgs, RBracLoc);
2030}
2031
2032ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
2033 SourceLocation LBracLoc,
2034 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002035 Selector Sel,
Douglas Gregor9a129192010-04-21 00:45:42 +00002036 ObjCMethodDecl *Method,
2037 Expr **Args, unsigned NumArgs,
2038 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002039 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002040 NumArgs * sizeof(Expr *);
2041 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002042 return new (Mem) ObjCMessageExpr(T, LBracLoc, Receiver, Sel, Method, Args,
Douglas Gregor9a129192010-04-21 00:45:42 +00002043 NumArgs, RBracLoc);
2044}
2045
Alexis Hunta8136cc2010-05-05 15:23:54 +00002046ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002047 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002048 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002049 NumArgs * sizeof(Expr *);
2050 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2051 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2052}
Alexis Hunta8136cc2010-05-05 15:23:54 +00002053
Douglas Gregor9a129192010-04-21 00:45:42 +00002054Selector ObjCMessageExpr::getSelector() const {
2055 if (HasMethod)
2056 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2057 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002058 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002059}
2060
2061ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2062 switch (getReceiverKind()) {
2063 case Instance:
2064 if (const ObjCObjectPointerType *Ptr
2065 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2066 return Ptr->getInterfaceDecl();
2067 break;
2068
2069 case Class:
2070 if (const ObjCInterfaceType *Iface
2071 = getClassReceiver()->getAs<ObjCInterfaceType>())
2072 return Iface->getDecl();
2073 break;
2074
2075 case SuperInstance:
2076 if (const ObjCObjectPointerType *Ptr
2077 = getSuperType()->getAs<ObjCObjectPointerType>())
2078 return Ptr->getInterfaceDecl();
2079 break;
2080
2081 case SuperClass:
2082 if (const ObjCObjectPointerType *Iface
2083 = getSuperType()->getAs<ObjCObjectPointerType>())
2084 return Iface->getInterfaceDecl();
2085 break;
2086 }
2087
2088 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002089}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002090
Chris Lattner35e564e2007-10-25 00:29:32 +00002091bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002092 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002093}
2094
Nate Begeman48745922009-08-12 02:28:50 +00002095void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2096 unsigned NumExprs) {
2097 if (SubExprs) C.Deallocate(SubExprs);
2098
2099 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002100 this->NumExprs = NumExprs;
2101 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002102}
Nate Begeman48745922009-08-12 02:28:50 +00002103
2104void ShuffleVectorExpr::DoDestroy(ASTContext& C) {
2105 DestroyChildren(C);
2106 if (SubExprs) C.Deallocate(SubExprs);
2107 this->~ShuffleVectorExpr();
2108 C.Deallocate(this);
Douglas Gregora3c55902009-04-16 00:01:45 +00002109}
2110
Douglas Gregore26a2852009-08-07 06:08:38 +00002111void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) {
Sebastian Redl6f282892008-11-11 17:56:53 +00002112 // Override default behavior of traversing children. If this has a type
2113 // operand and the type is a variable-length array, the child iteration
2114 // will iterate over the size expression. However, this expression belongs
2115 // to the type, not to this, so we don't want to delete it.
2116 // We still want to delete this expression.
Ted Kremenek5a201952009-02-07 01:47:29 +00002117 if (isArgumentType()) {
2118 this->~SizeOfAlignOfExpr();
2119 C.Deallocate(this);
2120 }
Sebastian Redl6f282892008-11-11 17:56:53 +00002121 else
Douglas Gregore26a2852009-08-07 06:08:38 +00002122 Expr::DoDestroy(C);
Daniel Dunbar3e1888e2008-08-28 18:02:04 +00002123}
2124
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002125//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002126// DesignatedInitExpr
2127//===----------------------------------------------------------------------===//
2128
2129IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2130 assert(Kind == FieldDesignator && "Only valid on a field designator");
2131 if (Field.NameOrField & 0x01)
2132 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2133 else
2134 return getField()->getIdentifier();
2135}
2136
Alexis Hunta8136cc2010-05-05 15:23:54 +00002137DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002138 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002139 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002140 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002141 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002142 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002143 unsigned NumIndexExprs,
2144 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002145 : Expr(DesignatedInitExprClass, Ty,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002146 Init->isTypeDependent(), Init->isValueDependent()),
Mike Stump11289f42009-09-09 15:08:12 +00002147 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2148 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002149 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002150
2151 // Record the initializer itself.
2152 child_iterator Child = child_begin();
2153 *Child++ = Init;
2154
2155 // Copy the designators and their subexpressions, computing
2156 // value-dependence along the way.
2157 unsigned IndexIdx = 0;
2158 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002159 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002160
2161 if (this->Designators[I].isArrayDesignator()) {
2162 // Compute type- and value-dependence.
2163 Expr *Index = IndexExprs[IndexIdx];
Mike Stump11289f42009-09-09 15:08:12 +00002164 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002165 Index->isTypeDependent() || Index->isValueDependent();
2166
2167 // Copy the index expressions into permanent storage.
2168 *Child++ = IndexExprs[IndexIdx++];
2169 } else if (this->Designators[I].isArrayRangeDesignator()) {
2170 // Compute type- and value-dependence.
2171 Expr *Start = IndexExprs[IndexIdx];
2172 Expr *End = IndexExprs[IndexIdx + 1];
Mike Stump11289f42009-09-09 15:08:12 +00002173 ValueDependent = ValueDependent ||
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002174 Start->isTypeDependent() || Start->isValueDependent() ||
2175 End->isTypeDependent() || End->isValueDependent();
2176
2177 // Copy the start/end expressions into permanent storage.
2178 *Child++ = IndexExprs[IndexIdx++];
2179 *Child++ = IndexExprs[IndexIdx++];
2180 }
2181 }
2182
2183 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002184}
2185
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002186DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002187DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002188 unsigned NumDesignators,
2189 Expr **IndexExprs, unsigned NumIndexExprs,
2190 SourceLocation ColonOrEqualLoc,
2191 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002192 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002193 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002194 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002195 ColonOrEqualLoc, UsesColonSyntax,
2196 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002197}
2198
Mike Stump11289f42009-09-09 15:08:12 +00002199DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002200 unsigned NumIndexExprs) {
2201 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2202 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2203 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2204}
2205
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002206void DesignatedInitExpr::setDesignators(ASTContext &C,
2207 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002208 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002209 DestroyDesignators(C);
Douglas Gregor38676d52009-04-16 00:55:48 +00002210
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002211 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002212 NumDesignators = NumDesigs;
2213 for (unsigned I = 0; I != NumDesigs; ++I)
2214 Designators[I] = Desigs[I];
2215}
2216
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002217SourceRange DesignatedInitExpr::getSourceRange() const {
2218 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002219 Designator &First =
2220 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002221 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002222 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002223 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2224 else
2225 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2226 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002227 StartLoc =
2228 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002229 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2230}
2231
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002232Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2233 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2234 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2235 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002236 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2237 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2238}
2239
2240Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002241 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002242 "Requires array range designator");
2243 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2244 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002245 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2246 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2247}
2248
2249Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002250 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002251 "Requires array range designator");
2252 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2253 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002254 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2255 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2256}
2257
Douglas Gregord5846a12009-04-15 06:41:24 +00002258/// \brief Replaces the designator at index @p Idx with the series
2259/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002260void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002261 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002262 const Designator *Last) {
2263 unsigned NumNewDesignators = Last - First;
2264 if (NumNewDesignators == 0) {
2265 std::copy_backward(Designators + Idx + 1,
2266 Designators + NumDesignators,
2267 Designators + Idx);
2268 --NumNewDesignators;
2269 return;
2270 } else if (NumNewDesignators == 1) {
2271 Designators[Idx] = *First;
2272 return;
2273 }
2274
Mike Stump11289f42009-09-09 15:08:12 +00002275 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002276 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002277 std::copy(Designators, Designators + Idx, NewDesignators);
2278 std::copy(First, Last, NewDesignators + Idx);
2279 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2280 NewDesignators + Idx + NumNewDesignators);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002281 DestroyDesignators(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002282 Designators = NewDesignators;
2283 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2284}
2285
Douglas Gregore26a2852009-08-07 06:08:38 +00002286void DesignatedInitExpr::DoDestroy(ASTContext &C) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002287 DestroyDesignators(C);
Douglas Gregore26a2852009-08-07 06:08:38 +00002288 Expr::DoDestroy(C);
Douglas Gregord5846a12009-04-15 06:41:24 +00002289}
2290
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002291void DesignatedInitExpr::DestroyDesignators(ASTContext &C) {
2292 for (unsigned I = 0; I != NumDesignators; ++I)
2293 Designators[I].~Designator();
2294 C.Deallocate(Designators);
2295 Designators = 0;
2296}
2297
Mike Stump11289f42009-09-09 15:08:12 +00002298ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002299 Expr **exprs, unsigned nexprs,
2300 SourceLocation rparenloc)
2301: Expr(ParenListExprClass, QualType(),
2302 hasAnyTypeDependentArguments(exprs, nexprs),
Mike Stump11289f42009-09-09 15:08:12 +00002303 hasAnyValueDependentArguments(exprs, nexprs)),
Nate Begeman5ec4b312009-08-10 23:49:36 +00002304 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002305
Nate Begeman5ec4b312009-08-10 23:49:36 +00002306 Exprs = new (C) Stmt*[nexprs];
2307 for (unsigned i = 0; i != nexprs; ++i)
2308 Exprs[i] = exprs[i];
2309}
2310
2311void ParenListExpr::DoDestroy(ASTContext& C) {
2312 DestroyChildren(C);
2313 if (Exprs) C.Deallocate(Exprs);
2314 this->~ParenListExpr();
2315 C.Deallocate(this);
2316}
2317
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002318//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002319// ExprIterator.
2320//===----------------------------------------------------------------------===//
2321
2322Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2323Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2324Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2325const Expr* ConstExprIterator::operator[](size_t idx) const {
2326 return cast<Expr>(I[idx]);
2327}
2328const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2329const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2330
2331//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002332// Child Iterators for iterating over subexpressions/substatements
2333//===----------------------------------------------------------------------===//
2334
2335// DeclRefExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002336Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
2337Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002338
Steve Naroffe46504b2007-11-12 14:29:37 +00002339// ObjCIvarRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002340Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
2341Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroffe46504b2007-11-12 14:29:37 +00002342
Steve Naroffebf4cb42008-06-02 23:03:37 +00002343// ObjCPropertyRefExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002344Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
2345Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroffec944032008-05-30 00:40:33 +00002346
Fariborz Jahanian9a846652009-08-20 17:02:02 +00002347// ObjCImplicitSetterGetterRefExpr
Mike Stump11289f42009-09-09 15:08:12 +00002348Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() {
John McCalleebc8322010-05-05 22:59:52 +00002349 // If this is accessing a class member, skip that entry.
2350 if (Base) return &Base;
2351 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002352}
Mike Stump11289f42009-09-09 15:08:12 +00002353Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() {
2354 return &Base+1;
Fariborz Jahanian88cc2342009-08-18 20:50:23 +00002355}
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +00002356
Douglas Gregor8ea1f532008-11-04 14:56:14 +00002357// ObjCSuperExpr
2358Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); }
2359Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); }
2360
Steve Naroffe87026a2009-07-24 17:54:45 +00002361// ObjCIsaExpr
2362Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; }
2363Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; }
2364
Chris Lattner6307f192008-08-10 01:53:14 +00002365// PredefinedExpr
2366Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
2367Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002368
2369// IntegerLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002370Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
2371Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002372
2373// CharacterLiteral
Chris Lattner8ba22472009-02-16 22:33:34 +00002374Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();}
Ted Kremenek04746ce2007-10-18 23:28:49 +00002375Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002376
2377// FloatingLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002378Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
2379Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002380
Chris Lattner1c20a172007-08-26 03:42:43 +00002381// ImaginaryLiteral
Ted Kremenek08e17112008-06-17 02:43:46 +00002382Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
2383Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1c20a172007-08-26 03:42:43 +00002384
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002385// StringLiteral
Ted Kremenek04746ce2007-10-18 23:28:49 +00002386Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
2387Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002388
2389// ParenExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002390Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
2391Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002392
2393// UnaryOperator
Ted Kremenek08e17112008-06-17 02:43:46 +00002394Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
2395Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002396
Douglas Gregor882211c2010-04-28 22:16:22 +00002397// OffsetOfExpr
2398Stmt::child_iterator OffsetOfExpr::child_begin() {
2399 return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1)
2400 + NumComps);
2401}
2402Stmt::child_iterator OffsetOfExpr::child_end() {
2403 return child_iterator(&*child_begin() + NumExprs);
2404}
2405
Sebastian Redl6f282892008-11-11 17:56:53 +00002406// SizeOfAlignOfExpr
Mike Stump11289f42009-09-09 15:08:12 +00002407Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002408 // If this is of a type and the type is a VLA type (and not a typedef), the
2409 // size expression of the VLA needs to be treated as an executable expression.
2410 // Why isn't this weirdness documented better in StmtIterator?
2411 if (isArgumentType()) {
2412 if (VariableArrayType* T = dyn_cast<VariableArrayType>(
2413 getArgumentType().getTypePtr()))
2414 return child_iterator(T);
2415 return child_iterator();
2416 }
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002417 return child_iterator(&Argument.Ex);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002418}
Sebastian Redl6f282892008-11-11 17:56:53 +00002419Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
2420 if (isArgumentType())
2421 return child_iterator();
Sebastian Redlba3fdfc2008-12-03 23:17:54 +00002422 return child_iterator(&Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002423}
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002424
2425// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002426Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002427 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002428}
Ted Kremenek23702b62007-08-24 20:06:47 +00002429Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002430 return &SubExprs[0]+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002431}
2432
2433// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +00002434Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002435 return &SubExprs[0];
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002436}
Ted Kremenek23702b62007-08-24 20:06:47 +00002437Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002438 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002439}
Ted Kremenek23702b62007-08-24 20:06:47 +00002440
2441// MemberExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002442Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
2443Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002444
Nate Begemance4d7fc2008-04-18 23:10:10 +00002445// ExtVectorElementExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002446Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
2447Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002448
2449// CompoundLiteralExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002450Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
2451Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002452
Ted Kremenek23702b62007-08-24 20:06:47 +00002453// CastExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002454Stmt::child_iterator CastExpr::child_begin() { return &Op; }
2455Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002456
2457// BinaryOperator
2458Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002459 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002460}
Ted Kremenek23702b62007-08-24 20:06:47 +00002461Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002462 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002463}
2464
2465// ConditionalOperator
2466Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002467 return &SubExprs[0];
Ted Kremenek23702b62007-08-24 20:06:47 +00002468}
Ted Kremenek23702b62007-08-24 20:06:47 +00002469Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002470 return &SubExprs[0]+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00002471}
2472
2473// AddrLabelExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002474Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
2475Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002476
Ted Kremenek23702b62007-08-24 20:06:47 +00002477// StmtExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002478Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
2479Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002480
2481// TypesCompatibleExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002482Stmt::child_iterator TypesCompatibleExpr::child_begin() {
2483 return child_iterator();
2484}
2485
2486Stmt::child_iterator TypesCompatibleExpr::child_end() {
2487 return child_iterator();
2488}
Ted Kremenek23702b62007-08-24 20:06:47 +00002489
2490// ChooseExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002491Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
2492Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek23702b62007-08-24 20:06:47 +00002493
Douglas Gregor3be4b122008-11-29 04:51:27 +00002494// GNUNullExpr
2495Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); }
2496Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); }
2497
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002498// ShuffleVectorExpr
2499Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002500 return &SubExprs[0];
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002501}
2502Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek08e17112008-06-17 02:43:46 +00002503 return &SubExprs[0]+NumExprs;
Eli Friedmana1b4ed82008-05-14 19:38:39 +00002504}
2505
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002506// VAArgExpr
Ted Kremenek08e17112008-06-17 02:43:46 +00002507Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
2508Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002509
Anders Carlsson4692db02007-08-31 04:56:16 +00002510// InitListExpr
Ted Kremenek013041e2010-02-19 01:50:18 +00002511Stmt::child_iterator InitListExpr::child_begin() {
2512 return InitExprs.size() ? &InitExprs[0] : 0;
2513}
2514Stmt::child_iterator InitListExpr::child_end() {
2515 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
2516}
Anders Carlsson4692db02007-08-31 04:56:16 +00002517
Douglas Gregor0202cb42009-01-29 17:44:32 +00002518// DesignatedInitExpr
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002519Stmt::child_iterator DesignatedInitExpr::child_begin() {
2520 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2521 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002522 return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2523}
2524Stmt::child_iterator DesignatedInitExpr::child_end() {
2525 return child_iterator(&*child_begin() + NumSubExprs);
2526}
2527
Douglas Gregor0202cb42009-01-29 17:44:32 +00002528// ImplicitValueInitExpr
Mike Stump11289f42009-09-09 15:08:12 +00002529Stmt::child_iterator ImplicitValueInitExpr::child_begin() {
2530 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002531}
2532
Mike Stump11289f42009-09-09 15:08:12 +00002533Stmt::child_iterator ImplicitValueInitExpr::child_end() {
2534 return child_iterator();
Douglas Gregor0202cb42009-01-29 17:44:32 +00002535}
2536
Nate Begeman5ec4b312009-08-10 23:49:36 +00002537// ParenListExpr
2538Stmt::child_iterator ParenListExpr::child_begin() {
2539 return &Exprs[0];
2540}
2541Stmt::child_iterator ParenListExpr::child_end() {
2542 return &Exprs[0]+NumExprs;
2543}
2544
Ted Kremenek23702b62007-08-24 20:06:47 +00002545// ObjCStringLiteral
Mike Stump11289f42009-09-09 15:08:12 +00002546Stmt::child_iterator ObjCStringLiteral::child_begin() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002547 return &String;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002548}
2549Stmt::child_iterator ObjCStringLiteral::child_end() {
Chris Lattner112c2a92009-02-18 06:53:08 +00002550 return &String+1;
Ted Kremenek04746ce2007-10-18 23:28:49 +00002551}
Ted Kremenek23702b62007-08-24 20:06:47 +00002552
2553// ObjCEncodeExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002554Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
2555Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek23702b62007-08-24 20:06:47 +00002556
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002557// ObjCSelectorExpr
Mike Stump11289f42009-09-09 15:08:12 +00002558Stmt::child_iterator ObjCSelectorExpr::child_begin() {
Ted Kremenek04746ce2007-10-18 23:28:49 +00002559 return child_iterator();
2560}
2561Stmt::child_iterator ObjCSelectorExpr::child_end() {
2562 return child_iterator();
2563}
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002564
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002565// ObjCProtocolExpr
Ted Kremenek04746ce2007-10-18 23:28:49 +00002566Stmt::child_iterator ObjCProtocolExpr::child_begin() {
2567 return child_iterator();
2568}
2569Stmt::child_iterator ObjCProtocolExpr::child_end() {
2570 return child_iterator();
2571}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002572
Steve Naroffd54978b2007-09-18 23:55:05 +00002573// ObjCMessageExpr
Mike Stump11289f42009-09-09 15:08:12 +00002574Stmt::child_iterator ObjCMessageExpr::child_begin() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002575 if (getReceiverKind() == Instance)
2576 return reinterpret_cast<Stmt **>(this + 1);
2577 return getArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002578}
2579Stmt::child_iterator ObjCMessageExpr::child_end() {
Douglas Gregor9a129192010-04-21 00:45:42 +00002580 return getArgs() + getNumArgs();
Steve Naroffd54978b2007-09-18 23:55:05 +00002581}
2582
Steve Naroffc540d662008-09-03 18:15:37 +00002583// Blocks
Steve Naroff415d3d52008-10-08 17:01:13 +00002584Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
2585Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroffc540d662008-09-03 18:15:37 +00002586
Ted Kremenek8bafa2c2008-09-26 23:24:14 +00002587Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
2588Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }