blob: b31c448ff0eba065f58952d373e7f82c31a1630f [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
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000193UnresolvedLookupExpr *
194UnresolvedLookupExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
195 std::size_t size = sizeof(UnresolvedLookupExpr);
196 if (NumTemplateArgs != 0)
197 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
198
199 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedLookupExpr>());
200 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
201 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
202 return E;
203}
204
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000205OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, QualType T,
206 bool Dependent, NestedNameSpecifier *Qualifier,
207 SourceRange QRange, DeclarationName Name,
208 SourceLocation NameLoc, bool HasTemplateArgs,
209 UnresolvedSetIterator Begin,
210 UnresolvedSetIterator End)
211 : Expr(K, T, Dependent, Dependent),
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000212 Results(0), NumResults(0), Name(Name), Qualifier(Qualifier),
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000213 QualifierRange(QRange), NameLoc(NameLoc),
214 HasExplicitTemplateArgs(HasTemplateArgs)
215{
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000216 initializeResults(C, Begin, End);
217}
218
219void OverloadExpr::initializeResults(ASTContext &C,
220 UnresolvedSetIterator Begin,
221 UnresolvedSetIterator End) {
222 assert(Results == 0 && "Results already initialized!");
223 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000224 if (NumResults) {
225 Results = static_cast<DeclAccessPair *>(
226 C.Allocate(sizeof(DeclAccessPair) * NumResults,
227 llvm::alignof<DeclAccessPair>()));
228 memcpy(Results, &*Begin.getIterator(),
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000229 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000230 }
231}
232
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000233
John McCall7bb12da2010-02-02 06:20:04 +0000234bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin,
235 UnresolvedSetIterator End,
236 const TemplateArgumentListInfo *Args) {
John McCalleec51cf2010-01-20 00:46:10 +0000237 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I)
John McCallf7a1a742009-11-24 19:00:30 +0000238 if ((*I)->getDeclContext()->isDependentContext())
239 return true;
240
241 if (Args && TemplateSpecializationType::anyDependentTemplateArguments(*Args))
242 return true;
243
244 return false;
245}
246
John McCalle9ee23e2010-04-22 18:44:12 +0000247CXXRecordDecl *OverloadExpr::getNamingClass() const {
248 if (isa<UnresolvedLookupExpr>(this))
249 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
250 else
251 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
252}
253
John McCallba135432009-11-21 08:51:07 +0000254Stmt::child_iterator UnresolvedLookupExpr::child_begin() {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000256}
John McCallba135432009-11-21 08:51:07 +0000257Stmt::child_iterator UnresolvedLookupExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000258 return child_iterator();
259}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000260// UnaryTypeTraitExpr
261Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
262 return child_iterator();
263}
264Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
265 return child_iterator();
266}
267
John McCall865d4472009-11-19 22:55:06 +0000268// DependentScopeDeclRefExpr
John McCallf7a1a742009-11-24 19:00:30 +0000269DependentScopeDeclRefExpr *
270DependentScopeDeclRefExpr::Create(ASTContext &C,
271 NestedNameSpecifier *Qualifier,
272 SourceRange QualifierRange,
273 DeclarationName Name,
274 SourceLocation NameLoc,
275 const TemplateArgumentListInfo *Args) {
276 std::size_t size = sizeof(DependentScopeDeclRefExpr);
277 if (Args) size += ExplicitTemplateArgumentList::sizeFor(*Args);
278 void *Mem = C.Allocate(size);
279
280 DependentScopeDeclRefExpr *DRE
281 = new (Mem) DependentScopeDeclRefExpr(C.DependentTy,
282 Qualifier, QualifierRange,
283 Name, NameLoc,
284 Args != 0);
285
286 if (Args)
287 reinterpret_cast<ExplicitTemplateArgumentList*>(DRE+1)
288 ->initializeFrom(*Args);
289
290 return DRE;
291}
292
John McCall865d4472009-11-19 22:55:06 +0000293StmtIterator DependentScopeDeclRefExpr::child_begin() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000294 return child_iterator();
295}
296
John McCall865d4472009-11-19 22:55:06 +0000297StmtIterator DependentScopeDeclRefExpr::child_end() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000298 return child_iterator();
299}
300
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000301bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000302 switch(UTT) {
303 default: assert(false && "Unknown type trait or not implemented");
304 case UTT_IsPOD: return QueriedType->isPODType();
Sebastian Redlccf43502009-12-03 00:13:20 +0000305 case UTT_IsLiteral: return QueriedType->isLiteralType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000306 case UTT_IsClass: // Fallthrough
307 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000308 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000309 bool Union = Record->getDecl()->isUnion();
310 return UTT == UTT_IsUnion ? Union : !Union;
311 }
312 return false;
313 case UTT_IsEnum: return QueriedType->isEnumeralType();
314 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000315 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000316 // Type traits are only parsed in C++, so we've got CXXRecords.
317 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
318 }
319 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000320 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000321 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000322 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
323 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000324 case UTT_IsEmpty:
325 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
326 return !Record->getDecl()->isUnion()
327 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
328 }
329 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000330 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000331 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
332 // If __is_pod (type) is true then the trait is true, else if type is
333 // a cv class or union type (or array thereof) with a trivial default
334 // constructor ([class.ctor]) then the trait is true, else it is false.
335 if (QueriedType->isPODType())
336 return true;
337 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000338 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000339 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000340 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000341 case UTT_HasTrivialCopy:
342 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
343 // If __is_pod (type) is true or type is a reference type then
344 // the trait is true, else if type is a cv class or union type
345 // with a trivial copy constructor ([class.copy]) then the trait
346 // is true, else it is false.
347 if (QueriedType->isPODType() || QueriedType->isReferenceType())
348 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000349 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000350 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
351 return false;
352 case UTT_HasTrivialAssign:
353 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
354 // If type is const qualified or is a reference type then the
355 // trait is false. Otherwise if __is_pod (type) is true then the
356 // trait is true, else if type is a cv class or union type with
357 // a trivial copy assignment ([class.copy]) then the trait is
358 // true, else it is false.
359 // Note: the const and reference restrictions are interesting,
360 // given that const and reference members don't prevent a class
361 // from having a trivial copy assignment operator (but do cause
362 // errors if the copy assignment operator is actually used, q.v.
363 // [class.copy]p12).
364
365 if (C.getBaseElementType(QueriedType).isConstQualified())
366 return false;
367 if (QueriedType->isPODType())
368 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000369 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000370 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
371 return false;
372 case UTT_HasTrivialDestructor:
373 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
374 // If __is_pod (type) is true or type is a reference type
375 // then the trait is true, else if type is a cv class or union
376 // type (or array thereof) with a trivial destructor
377 // ([class.dtor]) then the trait is true, else it is
378 // false.
379 if (QueriedType->isPODType() || QueriedType->isReferenceType())
380 return true;
381 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000382 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000383 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
384 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000385 }
386}
387
Ted Kremeneke3837682009-12-23 04:00:48 +0000388SourceRange CXXConstructExpr::getSourceRange() const {
389 // FIXME: Should we know where the parentheses are, if there are any?
390 for (std::reverse_iterator<Stmt**> I(&Args[NumArgs]), E(&Args[0]); I!=E;++I) {
391 // Ignore CXXDefaultExprs when computing the range, as they don't
392 // have a range.
393 if (!isa<CXXDefaultArgExpr>(*I))
394 return SourceRange(Loc, (*I)->getLocEnd());
395 }
396
397 return SourceRange(Loc);
398}
399
Douglas Gregorb4609802008-11-14 16:09:21 +0000400SourceRange CXXOperatorCallExpr::getSourceRange() const {
401 OverloadedOperatorKind Kind = getOperator();
402 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
403 if (getNumArgs() == 1)
404 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000405 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000406 getArg(0)->getSourceRange().getEnd());
407 else
408 // Postfix operator
409 return SourceRange(getArg(0)->getSourceRange().getEnd(),
410 getOperatorLoc());
411 } else if (Kind == OO_Call) {
412 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
413 } else if (Kind == OO_Subscript) {
414 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
415 } else if (getNumArgs() == 1) {
416 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
417 } else if (getNumArgs() == 2) {
418 return SourceRange(getArg(0)->getSourceRange().getBegin(),
419 getArg(1)->getSourceRange().getEnd());
420 } else {
421 return SourceRange();
422 }
423}
424
Douglas Gregor88a35142008-12-22 05:46:06 +0000425Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
426 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
427 return MemExpr->getBase();
428
429 // FIXME: Will eventually need to cope with member pointers.
430 return 0;
431}
432
Douglas Gregor00b98c22009-11-12 15:31:47 +0000433SourceRange CXXMemberCallExpr::getSourceRange() const {
434 SourceLocation LocStart = getCallee()->getLocStart();
435 if (LocStart.isInvalid() && getNumArgs() > 0)
436 LocStart = getArg(0)->getLocStart();
437 return SourceRange(LocStart, getRParenLoc());
438}
439
440
Douglas Gregor49badde2008-10-27 19:41:14 +0000441//===----------------------------------------------------------------------===//
442// Named casts
443//===----------------------------------------------------------------------===//
444
445/// getCastName - Get the name of the C++ cast being used, e.g.,
446/// "static_cast", "dynamic_cast", "reinterpret_cast", or
447/// "const_cast". The returned pointer must not be freed.
448const char *CXXNamedCastExpr::getCastName() const {
449 switch (getStmtClass()) {
450 case CXXStaticCastExprClass: return "static_cast";
451 case CXXDynamicCastExprClass: return "dynamic_cast";
452 case CXXReinterpretCastExprClass: return "reinterpret_cast";
453 case CXXConstCastExprClass: return "const_cast";
454 default: return "<invalid cast>";
455 }
456}
Douglas Gregor506ae412009-01-16 18:33:17 +0000457
Douglas Gregor65222e82009-12-23 18:19:08 +0000458CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000459CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
460 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000461 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000462 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
463 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000464}
465
466void CXXDefaultArgExpr::DoDestroy(ASTContext &C) {
467 if (Param.getInt())
468 getExpr()->Destroy(C);
469 this->~CXXDefaultArgExpr();
470 C.Deallocate(this);
471}
472
Mike Stump1eb44332009-09-09 15:08:12 +0000473CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000474 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000475 return new (C) CXXTemporary(Destructor);
476}
477
Douglas Gregor42602bb2009-08-07 06:08:38 +0000478void CXXTemporary::Destroy(ASTContext &Ctx) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000479 this->~CXXTemporary();
Douglas Gregor42602bb2009-08-07 06:08:38 +0000480 Ctx.Deallocate(this);
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000481}
482
Mike Stump1eb44332009-09-09 15:08:12 +0000483CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000484 CXXTemporary *Temp,
485 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000487 "Expression bound to a temporary must have record type!");
488
Anders Carlssonb859f352009-05-30 20:34:37 +0000489 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000490}
491
Douglas Gregor42602bb2009-08-07 06:08:38 +0000492void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000493 Temp->Destroy(C);
494 this->~CXXBindTemporaryExpr();
495 C.Deallocate(this);
496}
497
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000498CXXBindReferenceExpr *CXXBindReferenceExpr::Create(ASTContext &C, Expr *SubExpr,
499 bool ExtendsLifetime,
500 bool RequiresTemporaryCopy) {
501 return new (C) CXXBindReferenceExpr(SubExpr,
502 ExtendsLifetime,
503 RequiresTemporaryCopy);
504}
505
506void CXXBindReferenceExpr::DoDestroy(ASTContext &C) {
507 this->~CXXBindReferenceExpr();
508 C.Deallocate(this);
509}
510
Anders Carlsson8e587a12009-05-30 20:56:46 +0000511CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000512 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000513 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000514 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000515 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 unsigned NumArgs,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000517 SourceLocation rParenLoc,
518 bool ZeroInitialization)
Douglas Gregor99a2e602009-12-16 01:38:02 +0000519 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, tyBeginLoc,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000520 Cons, false, Args, NumArgs, ZeroInitialization),
Anders Carlsson524fa132009-04-24 17:34:38 +0000521 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000522}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000523
Mike Stump1eb44332009-09-09 15:08:12 +0000524CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000525 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000526 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000527 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000528 bool ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000529 ConstructionKind ConstructKind) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000530 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000531 Elidable, Args, NumArgs, ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000532 ConstructKind);
Anders Carlssone349bea2009-04-23 02:32:43 +0000533}
534
Mike Stump1eb44332009-09-09 15:08:12 +0000535CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000536 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000537 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000538 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000539 bool ZeroInitialization,
540 ConstructionKind ConstructKind)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000541: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000542 T->isDependentType(),
543 (T->isDependentType() ||
544 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Douglas Gregor16006c92009-12-16 18:50:27 +0000545 Constructor(D), Loc(Loc), Elidable(elidable),
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000546 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
547 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000548{
549 if (NumArgs) {
550 Args = new (C) Stmt*[NumArgs];
551
552 for (unsigned i = 0; i != NumArgs; ++i) {
553 assert(args[i] && "NULL argument in CXXConstructExpr");
554 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000555 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000556 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000557}
558
Douglas Gregor39da0b82009-09-09 23:08:42 +0000559CXXConstructExpr::CXXConstructExpr(EmptyShell Empty, ASTContext &C,
560 unsigned numargs)
561 : Expr(CXXConstructExprClass, Empty), Args(0), NumArgs(numargs)
562{
563 if (NumArgs)
564 Args = new (C) Stmt*[NumArgs];
565}
566
Douglas Gregor42602bb2009-08-07 06:08:38 +0000567void CXXConstructExpr::DoDestroy(ASTContext &C) {
Anders Carlssone349bea2009-04-23 02:32:43 +0000568 DestroyChildren(C);
569 if (Args)
570 C.Deallocate(Args);
571 this->~CXXConstructExpr();
572 C.Deallocate(this);
573}
574
Ted Kremenekd04ed412010-05-10 20:06:30 +0000575CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C,
576 Expr *subexpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000577 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000578 unsigned numtemps)
Chris Lattnerd2598362010-05-10 00:25:06 +0000579 : Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000580 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000581 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000582 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000583 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000584 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000585 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000586 }
587}
588
Ted Kremenekd04ed412010-05-10 20:06:30 +0000589void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000590 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000591 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000592 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000593}
594
595
Mike Stump1eb44332009-09-09 15:08:12 +0000596CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000597 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000598 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000599 unsigned NumTemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000600 return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000601}
602
Douglas Gregor42602bb2009-08-07 06:08:38 +0000603void CXXExprWithTemporaries::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000604 DestroyChildren(C);
Ted Kremenekd04ed412010-05-10 20:06:30 +0000605 if (Temps)
606 C.Deallocate(Temps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000607 this->~CXXExprWithTemporaries();
608 C.Deallocate(this);
609}
610
Ted Kremenekd04ed412010-05-10 20:06:30 +0000611CXXExprWithTemporaries::~CXXExprWithTemporaries() {}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000612
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000613// CXXBindTemporaryExpr
614Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
615 return &SubExpr;
616}
617
Mike Stump1eb44332009-09-09 15:08:12 +0000618Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000619 return &SubExpr + 1;
620}
621
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000622// CXXBindReferenceExpr
623Stmt::child_iterator CXXBindReferenceExpr::child_begin() {
624 return &SubExpr;
625}
626
627Stmt::child_iterator CXXBindReferenceExpr::child_end() {
628 return &SubExpr + 1;
629}
630
Anders Carlssone349bea2009-04-23 02:32:43 +0000631// CXXConstructExpr
632Stmt::child_iterator CXXConstructExpr::child_begin() {
633 return &Args[0];
634}
635Stmt::child_iterator CXXConstructExpr::child_end() {
636 return &Args[0]+NumArgs;
637}
638
Anders Carlsson55674ac2009-05-01 22:21:22 +0000639// CXXExprWithTemporaries
640Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
641 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000642}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000643
Mike Stump1eb44332009-09-09 15:08:12 +0000644Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000645 return &SubExpr + 1;
646}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000647
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000648CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
649 SourceLocation TyBeginLoc,
650 QualType T,
651 SourceLocation LParenLoc,
652 Expr **Args,
653 unsigned NumArgs,
654 SourceLocation RParenLoc)
655 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
656 T->isDependentType(), true),
657 TyBeginLoc(TyBeginLoc),
658 Type(T),
659 LParenLoc(LParenLoc),
660 RParenLoc(RParenLoc),
661 NumArgs(NumArgs) {
662 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
663 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
664}
665
666CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000667CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000668 SourceLocation TyBegin,
669 QualType T,
670 SourceLocation LParenLoc,
671 Expr **Args,
672 unsigned NumArgs,
673 SourceLocation RParenLoc) {
674 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
675 sizeof(Expr *) * NumArgs);
676 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
677 Args, NumArgs, RParenLoc);
678}
679
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000680CXXUnresolvedConstructExpr *
681CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
682 Stmt::EmptyShell Empty;
683 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
684 sizeof(Expr *) * NumArgs);
685 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
686}
687
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000688Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
689 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
690}
691
692Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
693 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
694}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000695
John McCall865d4472009-11-19 22:55:06 +0000696CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000697 Expr *Base, QualType BaseType,
698 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)
John McCall865d4472009-11-19 22:55:06 +0000706 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000707 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
708 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000709 OperatorLoc(OperatorLoc),
710 Qualifier(Qualifier), QualifierRange(QualifierRange),
711 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Mike Stump1eb44332009-09-09 15:08:12 +0000712 Member(Member), MemberLoc(MemberLoc) {
John McCalld5532b62009-11-23 01:53:49 +0000713 if (TemplateArgs)
714 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000715}
716
John McCall865d4472009-11-19 22:55:06 +0000717CXXDependentScopeMemberExpr *
718CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000719 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000720 SourceLocation OperatorLoc,
721 NestedNameSpecifier *Qualifier,
722 SourceRange QualifierRange,
723 NamedDecl *FirstQualifierFoundInScope,
724 DeclarationName Member,
725 SourceLocation MemberLoc,
John McCalld5532b62009-11-23 01:53:49 +0000726 const TemplateArgumentListInfo *TemplateArgs) {
727 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000728 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
729 IsArrow, OperatorLoc,
730 Qualifier, QualifierRange,
731 FirstQualifierFoundInScope,
732 Member, MemberLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
John McCalld5532b62009-11-23 01:53:49 +0000734 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
735 if (TemplateArgs)
736 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
737
738 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000739 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
740 IsArrow, OperatorLoc,
741 Qualifier, QualifierRange,
742 FirstQualifierFoundInScope,
743 Member, MemberLoc, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000744}
745
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000746CXXDependentScopeMemberExpr *
747CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
748 unsigned NumTemplateArgs) {
749 if (NumTemplateArgs == 0)
750 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
751 0, SourceLocation(), 0,
752 SourceRange(), 0,
753 DeclarationName(),
754 SourceLocation());
755
756 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
757 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
758 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
759 CXXDependentScopeMemberExpr *E
760 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
761 0, SourceLocation(), 0,
762 SourceRange(), 0,
763 DeclarationName(),
764 SourceLocation(), 0);
765 E->HasExplicitTemplateArgs = true;
766 return E;
767}
768
John McCall865d4472009-11-19 22:55:06 +0000769Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000770 return child_iterator(&Base);
771}
772
John McCall865d4472009-11-19 22:55:06 +0000773Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000774 if (isImplicitAccess())
775 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000776 return child_iterator(&Base + 1);
777}
John McCall129e2df2009-11-30 22:42:35 +0000778
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000779UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T,
780 bool Dependent,
John McCall129e2df2009-11-30 22:42:35 +0000781 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000782 Expr *Base, QualType BaseType,
783 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000784 SourceLocation OperatorLoc,
785 NestedNameSpecifier *Qualifier,
786 SourceRange QualifierRange,
787 DeclarationName MemberName,
788 SourceLocation MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000789 const TemplateArgumentListInfo *TemplateArgs,
790 UnresolvedSetIterator Begin,
791 UnresolvedSetIterator End)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000792 : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent,
John McCall7bb12da2010-02-02 06:20:04 +0000793 Qualifier, QualifierRange, MemberName, MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000794 TemplateArgs != 0, Begin, End),
John McCall7bb12da2010-02-02 06:20:04 +0000795 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
796 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000797 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000798 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000799}
800
801UnresolvedMemberExpr *
802UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
803 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000804 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000805 SourceLocation OperatorLoc,
806 NestedNameSpecifier *Qualifier,
807 SourceRange QualifierRange,
808 DeclarationName Member,
809 SourceLocation MemberLoc,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000810 const TemplateArgumentListInfo *TemplateArgs,
811 UnresolvedSetIterator Begin,
812 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000813 std::size_t size = sizeof(UnresolvedMemberExpr);
814 if (TemplateArgs)
815 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
816
817 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000818 return new (Mem) UnresolvedMemberExpr(C,
John McCall129e2df2009-11-30 22:42:35 +0000819 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000820 Dependent, HasUnresolvedUsing, Base, BaseType,
821 IsArrow, OperatorLoc, Qualifier, QualifierRange,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000822 Member, MemberLoc, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000823}
824
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000825UnresolvedMemberExpr *
826UnresolvedMemberExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
827 std::size_t size = sizeof(UnresolvedMemberExpr);
828 if (NumTemplateArgs != 0)
829 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
830
831 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
832 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
833 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
834 return E;
835}
836
John McCallc373d482010-01-27 01:50:18 +0000837CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
838 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
839
840 // If there was a nested name specifier, it names the naming class.
841 // It can't be dependent: after all, we were actually able to do the
842 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000843 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000844 if (getQualifier()) {
845 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000846 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000847 Record = T->getAsCXXRecordDecl();
848 assert(Record && "qualifier in member expression does not name record");
849 }
John McCallc373d482010-01-27 01:50:18 +0000850 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000851 else {
John McCallc373d482010-01-27 01:50:18 +0000852 QualType BaseType = getBaseType().getNonReferenceType();
853 if (isArrow()) {
854 const PointerType *PT = BaseType->getAs<PointerType>();
855 assert(PT && "base of arrow member access is not pointer");
856 BaseType = PT->getPointeeType();
857 }
858
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000859 Record = BaseType->getAsCXXRecordDecl();
860 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000861 }
862
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000863 return Record;
John McCallc373d482010-01-27 01:50:18 +0000864}
865
John McCall129e2df2009-11-30 22:42:35 +0000866Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
867 return child_iterator(&Base);
868}
869
870Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000871 if (isImplicitAccess())
872 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +0000873 return child_iterator(&Base + 1);
874}