blob: 4d6d712f43d2d763aa3d9bcf2497f23877f54b08 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000021
Ted Kremeneka758d092007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000026QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
30}
31
Sebastian Redlc42e1182008-11-11 11:37:55 +000032// CXXTypeidExpr - has child iterators if the operand is an expression
33Stmt::child_iterator CXXTypeidExpr::child_begin() {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000034 return isTypeOperand() ? child_iterator()
35 : reinterpret_cast<Stmt **>(&Operand);
Sebastian Redlc42e1182008-11-11 11:37:55 +000036}
37Stmt::child_iterator CXXTypeidExpr::child_end() {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000038 return isTypeOperand() ? child_iterator()
39 : reinterpret_cast<Stmt **>(&Operand) + 1;
Sebastian Redlc42e1182008-11-11 11:37:55 +000040}
Ted Kremeneka758d092007-08-24 20:21:10 +000041
Ted Kremeneka758d092007-08-24 20:21:10 +000042// CXXBoolLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000043Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +000044 return child_iterator();
45}
46Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
47 return child_iterator();
48}
Chris Lattner50dd2892008-02-26 00:51:44 +000049
Sebastian Redl6e8ed162009-05-10 18:38:11 +000050// CXXNullPtrLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000051Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() {
Sebastian Redl6e8ed162009-05-10 18:38:11 +000052 return child_iterator();
53}
54Stmt::child_iterator CXXNullPtrLiteralExpr::child_end() {
55 return child_iterator();
56}
57
Douglas Gregor796da182008-11-04 14:32:21 +000058// CXXThisExpr
59Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
60Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
61
Chris Lattner50dd2892008-02-26 00:51:44 +000062// CXXThrowExpr
Ted Kremenek1060aff2008-06-17 03:11:08 +000063Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
Chris Lattner50dd2892008-02-26 00:51:44 +000064Stmt::child_iterator CXXThrowExpr::child_end() {
65 // If Op is 0, we are processing throw; which has no children.
Ted Kremenek1060aff2008-06-17 03:11:08 +000066 return Op ? &Op+1 : &Op;
Chris Lattner50dd2892008-02-26 00:51:44 +000067}
Chris Lattner04421082008-04-08 04:40:51 +000068
69// CXXDefaultArgExpr
70Stmt::child_iterator CXXDefaultArgExpr::child_begin() {
Chris Lattner8123a952008-04-10 02:22:51 +000071 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000072}
73Stmt::child_iterator CXXDefaultArgExpr::child_end() {
Chris Lattner8123a952008-04-10 02:22:51 +000074 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000075}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000076
77// CXXZeroInitValueExpr
Mike Stump1eb44332009-09-09 15:08:12 +000078Stmt::child_iterator CXXZeroInitValueExpr::child_begin() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000079 return child_iterator();
80}
81Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
82 return child_iterator();
83}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +000084
Sebastian Redl4c5d3202008-11-21 19:14:01 +000085// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000086CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000087 Expr **placementArgs, unsigned numPlaceArgs,
Sebastian Redlcee63fb2008-12-02 14:43:59 +000088 bool parenTypeId, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000089 CXXConstructorDecl *constructor, bool initializer,
90 Expr **constructorArgs, unsigned numConsArgs,
91 FunctionDecl *operatorDelete, QualType ty,
92 SourceLocation startLoc, SourceLocation endLoc)
Sebastian Redl28507842009-02-26 14:39:58 +000093 : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()),
94 GlobalNew(globalNew), ParenTypeId(parenTypeId),
Chris Lattner59218632010-05-10 01:22:27 +000095 Initializer(initializer), SubExprs(0), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000096 OperatorDelete(operatorDelete), Constructor(constructor),
Mike Stump1eb44332009-09-09 15:08:12 +000097 StartLoc(startLoc), EndLoc(endLoc) {
Chris Lattner59218632010-05-10 01:22:27 +000098
99 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000100 unsigned i = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000101 if (Array)
102 SubExprs[i++] = arraySize;
103 for (unsigned j = 0; j < NumPlacementArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000104 SubExprs[i++] = placementArgs[j];
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000105 for (unsigned j = 0; j < NumConstructorArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000106 SubExprs[i++] = constructorArgs[j];
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000107}
108
Chris Lattner59218632010-05-10 01:22:27 +0000109void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
110 unsigned numPlaceArgs, unsigned numConsArgs){
111 assert(SubExprs == 0 && "SubExprs already allocated");
112 Array = isArray;
113 NumPlacementArgs = numPlaceArgs;
114 NumConstructorArgs = numConsArgs;
115
116 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
117 SubExprs = new (C) Stmt*[TotalSize];
118}
119
120
Ted Kremenekad7fe862010-02-11 22:51:03 +0000121void CXXNewExpr::DoDestroy(ASTContext &C) {
122 DestroyChildren(C);
123 if (SubExprs)
124 C.Deallocate(SubExprs);
125 this->~CXXNewExpr();
126 C.Deallocate((void*)this);
127}
128
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000129Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
130Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000131 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000132}
133
134// CXXDeleteExpr
135Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
136Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
137
Douglas Gregora71d8192009-09-04 17:36:40 +0000138// CXXPseudoDestructorExpr
139Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; }
140Stmt::child_iterator CXXPseudoDestructorExpr::child_end() {
141 return &Base + 1;
142}
143
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000144PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
145 : Type(Info)
146{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000147 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000148}
149
150QualType CXXPseudoDestructorExpr::getDestroyedType() const {
151 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
152 return TInfo->getType();
153
154 return QualType();
155}
156
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000157SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000158 SourceLocation End = DestroyedType.getLocation();
159 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000160 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000161 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000162}
163
164
John McCallba135432009-11-21 08:51:07 +0000165// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000166UnresolvedLookupExpr *
167UnresolvedLookupExpr::Create(ASTContext &C, bool Dependent,
John McCallc373d482010-01-27 01:50:18 +0000168 CXXRecordDecl *NamingClass,
John McCallf7a1a742009-11-24 19:00:30 +0000169 NestedNameSpecifier *Qualifier,
170 SourceRange QualifierRange, DeclarationName Name,
171 SourceLocation NameLoc, bool ADL,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000172 const TemplateArgumentListInfo &Args,
173 UnresolvedSetIterator Begin,
174 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000175{
176 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
177 ExplicitTemplateArgumentList::sizeFor(Args));
178 UnresolvedLookupExpr *ULE
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000179 = new (Mem) UnresolvedLookupExpr(C,
180 Dependent ? C.DependentTy : C.OverloadTy,
John McCallc373d482010-01-27 01:50:18 +0000181 Dependent, NamingClass,
182 Qualifier, QualifierRange,
John McCallf7a1a742009-11-24 19:00:30 +0000183 Name, NameLoc, ADL,
184 /*Overload*/ true,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000185 /*ExplicitTemplateArgs*/ true,
186 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000187
188 reinterpret_cast<ExplicitTemplateArgumentList*>(ULE+1)->initializeFrom(Args);
189
190 return ULE;
191}
192
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000193OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, QualType T,
194 bool Dependent, NestedNameSpecifier *Qualifier,
195 SourceRange QRange, DeclarationName Name,
196 SourceLocation NameLoc, bool HasTemplateArgs,
197 UnresolvedSetIterator Begin,
198 UnresolvedSetIterator End)
199 : Expr(K, T, Dependent, Dependent),
200 Results(0), NumResults(End - Begin), Name(Name), Qualifier(Qualifier),
201 QualifierRange(QRange), NameLoc(NameLoc),
202 HasExplicitTemplateArgs(HasTemplateArgs)
203{
204 if (NumResults) {
205 Results = static_cast<DeclAccessPair *>(
206 C.Allocate(sizeof(DeclAccessPair) * NumResults,
207 llvm::alignof<DeclAccessPair>()));
208 memcpy(Results, &*Begin.getIterator(),
209 (End - Begin) * sizeof(DeclAccessPair));
210 }
211}
212
John McCall7bb12da2010-02-02 06:20:04 +0000213bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin,
214 UnresolvedSetIterator End,
215 const TemplateArgumentListInfo *Args) {
John McCalleec51cf2010-01-20 00:46:10 +0000216 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I)
John McCallf7a1a742009-11-24 19:00:30 +0000217 if ((*I)->getDeclContext()->isDependentContext())
218 return true;
219
220 if (Args && TemplateSpecializationType::anyDependentTemplateArguments(*Args))
221 return true;
222
223 return false;
224}
225
John McCalle9ee23e2010-04-22 18:44:12 +0000226CXXRecordDecl *OverloadExpr::getNamingClass() const {
227 if (isa<UnresolvedLookupExpr>(this))
228 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
229 else
230 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
231}
232
John McCallba135432009-11-21 08:51:07 +0000233Stmt::child_iterator UnresolvedLookupExpr::child_begin() {
Mike Stump1eb44332009-09-09 15:08:12 +0000234 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000235}
John McCallba135432009-11-21 08:51:07 +0000236Stmt::child_iterator UnresolvedLookupExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000237 return child_iterator();
238}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000239// UnaryTypeTraitExpr
240Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
241 return child_iterator();
242}
243Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
244 return child_iterator();
245}
246
John McCall865d4472009-11-19 22:55:06 +0000247// DependentScopeDeclRefExpr
John McCallf7a1a742009-11-24 19:00:30 +0000248DependentScopeDeclRefExpr *
249DependentScopeDeclRefExpr::Create(ASTContext &C,
250 NestedNameSpecifier *Qualifier,
251 SourceRange QualifierRange,
252 DeclarationName Name,
253 SourceLocation NameLoc,
254 const TemplateArgumentListInfo *Args) {
255 std::size_t size = sizeof(DependentScopeDeclRefExpr);
256 if (Args) size += ExplicitTemplateArgumentList::sizeFor(*Args);
257 void *Mem = C.Allocate(size);
258
259 DependentScopeDeclRefExpr *DRE
260 = new (Mem) DependentScopeDeclRefExpr(C.DependentTy,
261 Qualifier, QualifierRange,
262 Name, NameLoc,
263 Args != 0);
264
265 if (Args)
266 reinterpret_cast<ExplicitTemplateArgumentList*>(DRE+1)
267 ->initializeFrom(*Args);
268
269 return DRE;
270}
271
John McCall865d4472009-11-19 22:55:06 +0000272StmtIterator DependentScopeDeclRefExpr::child_begin() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000273 return child_iterator();
274}
275
John McCall865d4472009-11-19 22:55:06 +0000276StmtIterator DependentScopeDeclRefExpr::child_end() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000277 return child_iterator();
278}
279
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000280bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000281 switch(UTT) {
282 default: assert(false && "Unknown type trait or not implemented");
283 case UTT_IsPOD: return QueriedType->isPODType();
Sebastian Redlccf43502009-12-03 00:13:20 +0000284 case UTT_IsLiteral: return QueriedType->isLiteralType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000285 case UTT_IsClass: // Fallthrough
286 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000287 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000288 bool Union = Record->getDecl()->isUnion();
289 return UTT == UTT_IsUnion ? Union : !Union;
290 }
291 return false;
292 case UTT_IsEnum: return QueriedType->isEnumeralType();
293 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000294 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000295 // Type traits are only parsed in C++, so we've got CXXRecords.
296 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
297 }
298 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000299 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000300 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000301 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
302 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000303 case UTT_IsEmpty:
304 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
305 return !Record->getDecl()->isUnion()
306 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
307 }
308 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000309 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000310 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
311 // If __is_pod (type) is true then the trait is true, else if type is
312 // a cv class or union type (or array thereof) with a trivial default
313 // constructor ([class.ctor]) then the trait is true, else it is false.
314 if (QueriedType->isPODType())
315 return true;
316 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000317 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000318 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000319 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000320 case UTT_HasTrivialCopy:
321 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
322 // If __is_pod (type) is true or type is a reference type then
323 // the trait is true, else if type is a cv class or union type
324 // with a trivial copy constructor ([class.copy]) then the trait
325 // is true, else it is false.
326 if (QueriedType->isPODType() || QueriedType->isReferenceType())
327 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000328 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000329 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
330 return false;
331 case UTT_HasTrivialAssign:
332 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
333 // If type is const qualified or is a reference type then the
334 // trait is false. Otherwise if __is_pod (type) is true then the
335 // trait is true, else if type is a cv class or union type with
336 // a trivial copy assignment ([class.copy]) then the trait is
337 // true, else it is false.
338 // Note: the const and reference restrictions are interesting,
339 // given that const and reference members don't prevent a class
340 // from having a trivial copy assignment operator (but do cause
341 // errors if the copy assignment operator is actually used, q.v.
342 // [class.copy]p12).
343
344 if (C.getBaseElementType(QueriedType).isConstQualified())
345 return false;
346 if (QueriedType->isPODType())
347 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000348 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000349 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
350 return false;
351 case UTT_HasTrivialDestructor:
352 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
353 // If __is_pod (type) is true or type is a reference type
354 // then the trait is true, else if type is a cv class or union
355 // type (or array thereof) with a trivial destructor
356 // ([class.dtor]) then the trait is true, else it is
357 // false.
358 if (QueriedType->isPODType() || QueriedType->isReferenceType())
359 return true;
360 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000361 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000362 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
363 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000364 }
365}
366
Ted Kremeneke3837682009-12-23 04:00:48 +0000367SourceRange CXXConstructExpr::getSourceRange() const {
368 // FIXME: Should we know where the parentheses are, if there are any?
369 for (std::reverse_iterator<Stmt**> I(&Args[NumArgs]), E(&Args[0]); I!=E;++I) {
370 // Ignore CXXDefaultExprs when computing the range, as they don't
371 // have a range.
372 if (!isa<CXXDefaultArgExpr>(*I))
373 return SourceRange(Loc, (*I)->getLocEnd());
374 }
375
376 return SourceRange(Loc);
377}
378
Douglas Gregorb4609802008-11-14 16:09:21 +0000379SourceRange CXXOperatorCallExpr::getSourceRange() const {
380 OverloadedOperatorKind Kind = getOperator();
381 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
382 if (getNumArgs() == 1)
383 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000384 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000385 getArg(0)->getSourceRange().getEnd());
386 else
387 // Postfix operator
388 return SourceRange(getArg(0)->getSourceRange().getEnd(),
389 getOperatorLoc());
390 } else if (Kind == OO_Call) {
391 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
392 } else if (Kind == OO_Subscript) {
393 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
394 } else if (getNumArgs() == 1) {
395 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
396 } else if (getNumArgs() == 2) {
397 return SourceRange(getArg(0)->getSourceRange().getBegin(),
398 getArg(1)->getSourceRange().getEnd());
399 } else {
400 return SourceRange();
401 }
402}
403
Douglas Gregor88a35142008-12-22 05:46:06 +0000404Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
405 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
406 return MemExpr->getBase();
407
408 // FIXME: Will eventually need to cope with member pointers.
409 return 0;
410}
411
Douglas Gregor00b98c22009-11-12 15:31:47 +0000412SourceRange CXXMemberCallExpr::getSourceRange() const {
413 SourceLocation LocStart = getCallee()->getLocStart();
414 if (LocStart.isInvalid() && getNumArgs() > 0)
415 LocStart = getArg(0)->getLocStart();
416 return SourceRange(LocStart, getRParenLoc());
417}
418
419
Douglas Gregor49badde2008-10-27 19:41:14 +0000420//===----------------------------------------------------------------------===//
421// Named casts
422//===----------------------------------------------------------------------===//
423
424/// getCastName - Get the name of the C++ cast being used, e.g.,
425/// "static_cast", "dynamic_cast", "reinterpret_cast", or
426/// "const_cast". The returned pointer must not be freed.
427const char *CXXNamedCastExpr::getCastName() const {
428 switch (getStmtClass()) {
429 case CXXStaticCastExprClass: return "static_cast";
430 case CXXDynamicCastExprClass: return "dynamic_cast";
431 case CXXReinterpretCastExprClass: return "reinterpret_cast";
432 case CXXConstCastExprClass: return "const_cast";
433 default: return "<invalid cast>";
434 }
435}
Douglas Gregor506ae412009-01-16 18:33:17 +0000436
Douglas Gregor65222e82009-12-23 18:19:08 +0000437CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000438CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
439 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000440 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000441 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
442 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000443}
444
445void CXXDefaultArgExpr::DoDestroy(ASTContext &C) {
446 if (Param.getInt())
447 getExpr()->Destroy(C);
448 this->~CXXDefaultArgExpr();
449 C.Deallocate(this);
450}
451
Mike Stump1eb44332009-09-09 15:08:12 +0000452CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000453 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000454 return new (C) CXXTemporary(Destructor);
455}
456
Douglas Gregor42602bb2009-08-07 06:08:38 +0000457void CXXTemporary::Destroy(ASTContext &Ctx) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000458 this->~CXXTemporary();
Douglas Gregor42602bb2009-08-07 06:08:38 +0000459 Ctx.Deallocate(this);
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000460}
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000463 CXXTemporary *Temp,
464 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000465 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000466 "Expression bound to a temporary must have record type!");
467
Anders Carlssonb859f352009-05-30 20:34:37 +0000468 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000469}
470
Douglas Gregor42602bb2009-08-07 06:08:38 +0000471void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000472 Temp->Destroy(C);
473 this->~CXXBindTemporaryExpr();
474 C.Deallocate(this);
475}
476
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000477CXXBindReferenceExpr *CXXBindReferenceExpr::Create(ASTContext &C, Expr *SubExpr,
478 bool ExtendsLifetime,
479 bool RequiresTemporaryCopy) {
480 return new (C) CXXBindReferenceExpr(SubExpr,
481 ExtendsLifetime,
482 RequiresTemporaryCopy);
483}
484
485void CXXBindReferenceExpr::DoDestroy(ASTContext &C) {
486 this->~CXXBindReferenceExpr();
487 C.Deallocate(this);
488}
489
Anders Carlsson8e587a12009-05-30 20:56:46 +0000490CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000491 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000492 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000493 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000494 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000495 unsigned NumArgs,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000496 SourceLocation rParenLoc,
497 bool ZeroInitialization)
Douglas Gregor99a2e602009-12-16 01:38:02 +0000498 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, tyBeginLoc,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000499 Cons, false, Args, NumArgs, ZeroInitialization),
Anders Carlsson524fa132009-04-24 17:34:38 +0000500 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000501}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000502
Mike Stump1eb44332009-09-09 15:08:12 +0000503CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000504 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000505 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000506 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000507 bool ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000508 ConstructionKind ConstructKind) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000509 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000510 Elidable, Args, NumArgs, ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000511 ConstructKind);
Anders Carlssone349bea2009-04-23 02:32:43 +0000512}
513
Mike Stump1eb44332009-09-09 15:08:12 +0000514CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000515 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000516 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000517 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000518 bool ZeroInitialization,
519 ConstructionKind ConstructKind)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000520: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000521 T->isDependentType(),
522 (T->isDependentType() ||
523 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Douglas Gregor16006c92009-12-16 18:50:27 +0000524 Constructor(D), Loc(Loc), Elidable(elidable),
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000525 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
526 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000527{
528 if (NumArgs) {
529 Args = new (C) Stmt*[NumArgs];
530
531 for (unsigned i = 0; i != NumArgs; ++i) {
532 assert(args[i] && "NULL argument in CXXConstructExpr");
533 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000534 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000535 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000536}
537
Douglas Gregor39da0b82009-09-09 23:08:42 +0000538CXXConstructExpr::CXXConstructExpr(EmptyShell Empty, ASTContext &C,
539 unsigned numargs)
540 : Expr(CXXConstructExprClass, Empty), Args(0), NumArgs(numargs)
541{
542 if (NumArgs)
543 Args = new (C) Stmt*[NumArgs];
544}
545
Douglas Gregor42602bb2009-08-07 06:08:38 +0000546void CXXConstructExpr::DoDestroy(ASTContext &C) {
Anders Carlssone349bea2009-04-23 02:32:43 +0000547 DestroyChildren(C);
548 if (Args)
549 C.Deallocate(Args);
550 this->~CXXConstructExpr();
551 C.Deallocate(this);
552}
553
Ted Kremenekd04ed412010-05-10 20:06:30 +0000554CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C,
555 Expr *subexpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000556 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000557 unsigned numtemps)
Chris Lattnerd2598362010-05-10 00:25:06 +0000558 : Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000559 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000560 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000561 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000562 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000563 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000564 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000565 }
566}
567
Ted Kremenekd04ed412010-05-10 20:06:30 +0000568void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000569 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000570 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000571 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000572}
573
574
Mike Stump1eb44332009-09-09 15:08:12 +0000575CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000576 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000577 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000578 unsigned NumTemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000579 return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000580}
581
Douglas Gregor42602bb2009-08-07 06:08:38 +0000582void CXXExprWithTemporaries::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000583 DestroyChildren(C);
Ted Kremenekd04ed412010-05-10 20:06:30 +0000584 if (Temps)
585 C.Deallocate(Temps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000586 this->~CXXExprWithTemporaries();
587 C.Deallocate(this);
588}
589
Ted Kremenekd04ed412010-05-10 20:06:30 +0000590CXXExprWithTemporaries::~CXXExprWithTemporaries() {}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000591
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000592// CXXBindTemporaryExpr
593Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
594 return &SubExpr;
595}
596
Mike Stump1eb44332009-09-09 15:08:12 +0000597Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000598 return &SubExpr + 1;
599}
600
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000601// CXXBindReferenceExpr
602Stmt::child_iterator CXXBindReferenceExpr::child_begin() {
603 return &SubExpr;
604}
605
606Stmt::child_iterator CXXBindReferenceExpr::child_end() {
607 return &SubExpr + 1;
608}
609
Anders Carlssone349bea2009-04-23 02:32:43 +0000610// CXXConstructExpr
611Stmt::child_iterator CXXConstructExpr::child_begin() {
612 return &Args[0];
613}
614Stmt::child_iterator CXXConstructExpr::child_end() {
615 return &Args[0]+NumArgs;
616}
617
Anders Carlsson55674ac2009-05-01 22:21:22 +0000618// CXXExprWithTemporaries
619Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
620 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000621}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000622
Mike Stump1eb44332009-09-09 15:08:12 +0000623Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000624 return &SubExpr + 1;
625}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000626
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000627CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
628 SourceLocation TyBeginLoc,
629 QualType T,
630 SourceLocation LParenLoc,
631 Expr **Args,
632 unsigned NumArgs,
633 SourceLocation RParenLoc)
634 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
635 T->isDependentType(), true),
636 TyBeginLoc(TyBeginLoc),
637 Type(T),
638 LParenLoc(LParenLoc),
639 RParenLoc(RParenLoc),
640 NumArgs(NumArgs) {
641 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
642 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
643}
644
645CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000646CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000647 SourceLocation TyBegin,
648 QualType T,
649 SourceLocation LParenLoc,
650 Expr **Args,
651 unsigned NumArgs,
652 SourceLocation RParenLoc) {
653 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
654 sizeof(Expr *) * NumArgs);
655 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
656 Args, NumArgs, RParenLoc);
657}
658
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000659CXXUnresolvedConstructExpr *
660CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
661 Stmt::EmptyShell Empty;
662 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
663 sizeof(Expr *) * NumArgs);
664 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
665}
666
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000667Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
668 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
669}
670
671Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
672 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
673}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000674
John McCall865d4472009-11-19 22:55:06 +0000675CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000676 Expr *Base, QualType BaseType,
677 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000678 SourceLocation OperatorLoc,
679 NestedNameSpecifier *Qualifier,
680 SourceRange QualifierRange,
681 NamedDecl *FirstQualifierFoundInScope,
682 DeclarationName Member,
683 SourceLocation MemberLoc,
John McCalld5532b62009-11-23 01:53:49 +0000684 const TemplateArgumentListInfo *TemplateArgs)
John McCall865d4472009-11-19 22:55:06 +0000685 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000686 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
687 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000688 OperatorLoc(OperatorLoc),
689 Qualifier(Qualifier), QualifierRange(QualifierRange),
690 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Mike Stump1eb44332009-09-09 15:08:12 +0000691 Member(Member), MemberLoc(MemberLoc) {
John McCalld5532b62009-11-23 01:53:49 +0000692 if (TemplateArgs)
693 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000694}
695
John McCall865d4472009-11-19 22:55:06 +0000696CXXDependentScopeMemberExpr *
697CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000698 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000699 SourceLocation OperatorLoc,
700 NestedNameSpecifier *Qualifier,
701 SourceRange QualifierRange,
702 NamedDecl *FirstQualifierFoundInScope,
703 DeclarationName Member,
704 SourceLocation MemberLoc,
John McCalld5532b62009-11-23 01:53:49 +0000705 const TemplateArgumentListInfo *TemplateArgs) {
706 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000707 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
708 IsArrow, OperatorLoc,
709 Qualifier, QualifierRange,
710 FirstQualifierFoundInScope,
711 Member, MemberLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
John McCalld5532b62009-11-23 01:53:49 +0000713 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
714 if (TemplateArgs)
715 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
716
717 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000718 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
719 IsArrow, OperatorLoc,
720 Qualifier, QualifierRange,
721 FirstQualifierFoundInScope,
722 Member, MemberLoc, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000723}
724
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000725CXXDependentScopeMemberExpr *
726CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
727 unsigned NumTemplateArgs) {
728 if (NumTemplateArgs == 0)
729 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
730 0, SourceLocation(), 0,
731 SourceRange(), 0,
732 DeclarationName(),
733 SourceLocation());
734
735 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
736 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
737 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
738 CXXDependentScopeMemberExpr *E
739 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
740 0, SourceLocation(), 0,
741 SourceRange(), 0,
742 DeclarationName(),
743 SourceLocation(), 0);
744 E->HasExplicitTemplateArgs = true;
745 return E;
746}
747
John McCall865d4472009-11-19 22:55:06 +0000748Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000749 return child_iterator(&Base);
750}
751
John McCall865d4472009-11-19 22:55:06 +0000752Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000753 if (isImplicitAccess())
754 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000755 return child_iterator(&Base + 1);
756}
John McCall129e2df2009-11-30 22:42:35 +0000757
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000758UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T,
759 bool Dependent,
John McCall129e2df2009-11-30 22:42:35 +0000760 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000761 Expr *Base, QualType BaseType,
762 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000763 SourceLocation OperatorLoc,
764 NestedNameSpecifier *Qualifier,
765 SourceRange QualifierRange,
766 DeclarationName MemberName,
767 SourceLocation MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000768 const TemplateArgumentListInfo *TemplateArgs,
769 UnresolvedSetIterator Begin,
770 UnresolvedSetIterator End)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000771 : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent,
John McCall7bb12da2010-02-02 06:20:04 +0000772 Qualifier, QualifierRange, MemberName, MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000773 TemplateArgs != 0, Begin, End),
John McCall7bb12da2010-02-02 06:20:04 +0000774 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
775 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000776 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000777 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000778}
779
780UnresolvedMemberExpr *
781UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
782 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000783 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000784 SourceLocation OperatorLoc,
785 NestedNameSpecifier *Qualifier,
786 SourceRange QualifierRange,
787 DeclarationName Member,
788 SourceLocation MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000789 const TemplateArgumentListInfo *TemplateArgs,
790 UnresolvedSetIterator Begin,
791 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000792 std::size_t size = sizeof(UnresolvedMemberExpr);
793 if (TemplateArgs)
794 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
795
796 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000797 return new (Mem) UnresolvedMemberExpr(C,
John McCall129e2df2009-11-30 22:42:35 +0000798 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000799 Dependent, HasUnresolvedUsing, Base, BaseType,
800 IsArrow, OperatorLoc, Qualifier, QualifierRange,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000801 Member, MemberLoc, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000802}
803
John McCallc373d482010-01-27 01:50:18 +0000804CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
805 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
806
807 // If there was a nested name specifier, it names the naming class.
808 // It can't be dependent: after all, we were actually able to do the
809 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000810 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000811 if (getQualifier()) {
812 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000813 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000814 Record = T->getAsCXXRecordDecl();
815 assert(Record && "qualifier in member expression does not name record");
816 }
John McCallc373d482010-01-27 01:50:18 +0000817 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000818 else {
John McCallc373d482010-01-27 01:50:18 +0000819 QualType BaseType = getBaseType().getNonReferenceType();
820 if (isArrow()) {
821 const PointerType *PT = BaseType->getAs<PointerType>();
822 assert(PT && "base of arrow member access is not pointer");
823 BaseType = PT->getPointeeType();
824 }
825
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000826 Record = BaseType->getAsCXXRecordDecl();
827 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000828 }
829
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000830 return Record;
John McCallc373d482010-01-27 01:50:18 +0000831}
832
John McCall129e2df2009-11-30 22:42:35 +0000833Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
834 return child_iterator(&Base);
835}
836
837Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000838 if (isImplicitAccess())
839 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +0000840 return child_iterator(&Base + 1);
841}