blob: 0a101300d8fa1ac11a8fdb112da6854f71851e36 [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
Douglas Gregored8abf12010-07-08 06:14:04 +000077// CXXScalarValueInitExpr
78Stmt::child_iterator CXXScalarValueInitExpr::child_begin() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000079 return child_iterator();
80}
Douglas Gregored8abf12010-07-08 06:14:04 +000081Stmt::child_iterator CXXScalarValueInitExpr::child_end() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000082 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,
Douglas Gregor4bd40312010-07-13 15:54:32 +000088 SourceRange TypeIdParens, 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()),
Douglas Gregor4bd40312010-07-13 15:54:32 +000094 GlobalNew(globalNew),
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),
Douglas Gregor4bd40312010-07-13 15:54:32 +000097 TypeIdParens(TypeIdParens), 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
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000121Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
122Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000123 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000124}
125
126// CXXDeleteExpr
127Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
128Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
129
Douglas Gregora71d8192009-09-04 17:36:40 +0000130// CXXPseudoDestructorExpr
131Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; }
132Stmt::child_iterator CXXPseudoDestructorExpr::child_end() {
133 return &Base + 1;
134}
135
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000136PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
137 : Type(Info)
138{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000139 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000140}
141
142QualType CXXPseudoDestructorExpr::getDestroyedType() const {
143 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
144 return TInfo->getType();
145
146 return QualType();
147}
148
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000149SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000150 SourceLocation End = DestroyedType.getLocation();
151 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000152 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000153 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000154}
155
156
John McCallba135432009-11-21 08:51:07 +0000157// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000158UnresolvedLookupExpr *
159UnresolvedLookupExpr::Create(ASTContext &C, bool Dependent,
John McCallc373d482010-01-27 01:50:18 +0000160 CXXRecordDecl *NamingClass,
John McCallf7a1a742009-11-24 19:00:30 +0000161 NestedNameSpecifier *Qualifier,
Abramo Bagnara25777432010-08-11 22:01:17 +0000162 SourceRange QualifierRange,
163 const DeclarationNameInfo &NameInfo,
164 bool ADL,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000165 const TemplateArgumentListInfo &Args,
166 UnresolvedSetIterator Begin,
167 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000168{
169 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
170 ExplicitTemplateArgumentList::sizeFor(Args));
171 UnresolvedLookupExpr *ULE
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000172 = new (Mem) UnresolvedLookupExpr(C,
173 Dependent ? C.DependentTy : C.OverloadTy,
John McCallc373d482010-01-27 01:50:18 +0000174 Dependent, NamingClass,
Abramo Bagnara25777432010-08-11 22:01:17 +0000175 Qualifier, QualifierRange, NameInfo,
176 ADL,
John McCallf7a1a742009-11-24 19:00:30 +0000177 /*Overload*/ true,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000178 /*ExplicitTemplateArgs*/ true,
179 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000180
181 reinterpret_cast<ExplicitTemplateArgumentList*>(ULE+1)->initializeFrom(Args);
182
183 return ULE;
184}
185
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000186UnresolvedLookupExpr *
187UnresolvedLookupExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
188 std::size_t size = sizeof(UnresolvedLookupExpr);
189 if (NumTemplateArgs != 0)
190 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
191
192 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedLookupExpr>());
193 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
194 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
195 return E;
196}
197
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000198OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, QualType T,
199 bool Dependent, NestedNameSpecifier *Qualifier,
Abramo Bagnara25777432010-08-11 22:01:17 +0000200 SourceRange QRange,
201 const DeclarationNameInfo &NameInfo,
202 bool HasTemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000203 UnresolvedSetIterator Begin,
204 UnresolvedSetIterator End)
205 : Expr(K, T, Dependent, Dependent),
Abramo Bagnara25777432010-08-11 22:01:17 +0000206 Results(0), NumResults(0), NameInfo(NameInfo), Qualifier(Qualifier),
207 QualifierRange(QRange), HasExplicitTemplateArgs(HasTemplateArgs)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000208{
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000209 initializeResults(C, Begin, End);
210}
211
212void OverloadExpr::initializeResults(ASTContext &C,
213 UnresolvedSetIterator Begin,
214 UnresolvedSetIterator End) {
215 assert(Results == 0 && "Results already initialized!");
216 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000217 if (NumResults) {
218 Results = static_cast<DeclAccessPair *>(
219 C.Allocate(sizeof(DeclAccessPair) * NumResults,
220 llvm::alignof<DeclAccessPair>()));
221 memcpy(Results, &*Begin.getIterator(),
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000222 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000223 }
224}
225
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000226
John McCall7bb12da2010-02-02 06:20:04 +0000227bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin,
228 UnresolvedSetIterator End,
229 const TemplateArgumentListInfo *Args) {
John McCalleec51cf2010-01-20 00:46:10 +0000230 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I)
John McCallf7a1a742009-11-24 19:00:30 +0000231 if ((*I)->getDeclContext()->isDependentContext())
232 return true;
233
234 if (Args && TemplateSpecializationType::anyDependentTemplateArguments(*Args))
235 return true;
236
237 return false;
238}
239
John McCalle9ee23e2010-04-22 18:44:12 +0000240CXXRecordDecl *OverloadExpr::getNamingClass() const {
241 if (isa<UnresolvedLookupExpr>(this))
242 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
243 else
244 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
245}
246
John McCallba135432009-11-21 08:51:07 +0000247Stmt::child_iterator UnresolvedLookupExpr::child_begin() {
Mike Stump1eb44332009-09-09 15:08:12 +0000248 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000249}
John McCallba135432009-11-21 08:51:07 +0000250Stmt::child_iterator UnresolvedLookupExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000251 return child_iterator();
252}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000253// UnaryTypeTraitExpr
254Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
255 return child_iterator();
256}
257Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
258 return child_iterator();
259}
260
John McCall865d4472009-11-19 22:55:06 +0000261// DependentScopeDeclRefExpr
John McCallf7a1a742009-11-24 19:00:30 +0000262DependentScopeDeclRefExpr *
263DependentScopeDeclRefExpr::Create(ASTContext &C,
264 NestedNameSpecifier *Qualifier,
265 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000266 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000267 const TemplateArgumentListInfo *Args) {
268 std::size_t size = sizeof(DependentScopeDeclRefExpr);
269 if (Args) size += ExplicitTemplateArgumentList::sizeFor(*Args);
270 void *Mem = C.Allocate(size);
271
272 DependentScopeDeclRefExpr *DRE
273 = new (Mem) DependentScopeDeclRefExpr(C.DependentTy,
274 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000275 NameInfo, Args != 0);
John McCallf7a1a742009-11-24 19:00:30 +0000276
277 if (Args)
278 reinterpret_cast<ExplicitTemplateArgumentList*>(DRE+1)
279 ->initializeFrom(*Args);
280
281 return DRE;
282}
283
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000284DependentScopeDeclRefExpr *
285DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
286 unsigned NumTemplateArgs) {
287 std::size_t size = sizeof(DependentScopeDeclRefExpr);
288 if (NumTemplateArgs)
289 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
290 void *Mem = C.Allocate(size);
291
292 return new (Mem) DependentScopeDeclRefExpr(QualType(), 0, SourceRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +0000293 DeclarationNameInfo(),
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000294 NumTemplateArgs != 0);
295}
296
John McCall865d4472009-11-19 22:55:06 +0000297StmtIterator DependentScopeDeclRefExpr::child_begin() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000298 return child_iterator();
299}
300
John McCall865d4472009-11-19 22:55:06 +0000301StmtIterator DependentScopeDeclRefExpr::child_end() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000302 return child_iterator();
303}
304
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000305bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000306 switch(UTT) {
307 default: assert(false && "Unknown type trait or not implemented");
308 case UTT_IsPOD: return QueriedType->isPODType();
Sebastian Redlccf43502009-12-03 00:13:20 +0000309 case UTT_IsLiteral: return QueriedType->isLiteralType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000310 case UTT_IsClass: // Fallthrough
311 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000312 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000313 bool Union = Record->getDecl()->isUnion();
314 return UTT == UTT_IsUnion ? Union : !Union;
315 }
316 return false;
317 case UTT_IsEnum: return QueriedType->isEnumeralType();
318 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000319 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000320 // Type traits are only parsed in C++, so we've got CXXRecords.
321 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
322 }
323 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000324 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000325 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000326 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
327 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000328 case UTT_IsEmpty:
329 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
330 return !Record->getDecl()->isUnion()
331 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
332 }
333 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000334 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000335 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
336 // If __is_pod (type) is true then the trait is true, else if type is
337 // a cv class or union type (or array thereof) with a trivial default
338 // constructor ([class.ctor]) then the trait is true, else it is false.
339 if (QueriedType->isPODType())
340 return true;
341 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000342 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000343 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000344 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000345 case UTT_HasTrivialCopy:
346 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
347 // If __is_pod (type) is true or type is a reference type then
348 // the trait is true, else if type is a cv class or union type
349 // with a trivial copy constructor ([class.copy]) then the trait
350 // is true, else it is false.
351 if (QueriedType->isPODType() || QueriedType->isReferenceType())
352 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000353 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000354 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
355 return false;
356 case UTT_HasTrivialAssign:
357 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
358 // If type is const qualified or is a reference type then the
359 // trait is false. Otherwise if __is_pod (type) is true then the
360 // trait is true, else if type is a cv class or union type with
361 // a trivial copy assignment ([class.copy]) then the trait is
362 // true, else it is false.
363 // Note: the const and reference restrictions are interesting,
364 // given that const and reference members don't prevent a class
365 // from having a trivial copy assignment operator (but do cause
366 // errors if the copy assignment operator is actually used, q.v.
367 // [class.copy]p12).
368
369 if (C.getBaseElementType(QueriedType).isConstQualified())
370 return false;
371 if (QueriedType->isPODType())
372 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000373 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000374 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
375 return false;
376 case UTT_HasTrivialDestructor:
377 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
378 // If __is_pod (type) is true or type is a reference type
379 // then the trait is true, else if type is a cv class or union
380 // type (or array thereof) with a trivial destructor
381 // ([class.dtor]) then the trait is true, else it is
382 // false.
383 if (QueriedType->isPODType() || QueriedType->isReferenceType())
384 return true;
385 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000386 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000387 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
388 return false;
Sebastian Redlc238f092010-08-31 04:59:00 +0000389 // TODO: Propagate nothrowness for implicitly declared special members.
390 case UTT_HasNothrowAssign:
391 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
392 // If type is const qualified or is a reference type then the
393 // trait is false. Otherwise if __has_trivial_assign (type)
394 // is true then the trait is true, else if type is a cv class
395 // or union type with copy assignment operators that are known
396 // not to throw an exception then the trait is true, else it is
397 // false.
398 if (C.getBaseElementType(QueriedType).isConstQualified())
399 return false;
400 if (QueriedType->isReferenceType())
401 return false;
402 if (QueriedType->isPODType())
403 return true;
404 if (const RecordType *RT = QueriedType->getAs<RecordType>()) {
405 CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl());
406 if (RD->hasTrivialCopyAssignment())
407 return true;
408
409 bool FoundAssign = false;
410 bool AllNoThrow = true;
411 DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal);
412 DeclContext::lookup_const_iterator Op, OpEnd;
413 for (llvm::tie(Op, OpEnd) = RD->lookup(Name);
414 Op != OpEnd; ++Op) {
415 CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op);
416 if (Operator->isCopyAssignmentOperator()) {
417 FoundAssign = true;
418 const FunctionProtoType *CPT
419 = Operator->getType()->getAs<FunctionProtoType>();
420 if (!CPT->hasEmptyExceptionSpec()) {
421 AllNoThrow = false;
422 break;
423 }
424 }
425 }
426
427 return FoundAssign && AllNoThrow;
428 }
429 return false;
430 case UTT_HasNothrowCopy:
431 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
432 // If __has_trivial_copy (type) is true then the trait is true, else
433 // if type is a cv class or union type with copy constructors that are
434 // known not to throw an exception then the trait is true, else it is
435 // false.
436 if (QueriedType->isPODType() || QueriedType->isReferenceType())
437 return true;
438 if (const RecordType *RT = QueriedType->getAs<RecordType>()) {
439 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
440 if (RD->hasTrivialCopyConstructor())
441 return true;
442
443 bool FoundConstructor = false;
444 bool AllNoThrow = true;
445 unsigned FoundTQs;
446 DeclarationName ConstructorName
447 = C.DeclarationNames.getCXXConstructorName(
448 C.getCanonicalType(QueriedType));
449 DeclContext::lookup_const_iterator Con, ConEnd;
450 for (llvm::tie(Con, ConEnd) = RD->lookup(ConstructorName);
451 Con != ConEnd; ++Con) {
452 CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con);
453 if (Constructor->isCopyConstructor(FoundTQs)) {
454 FoundConstructor = true;
455 const FunctionProtoType *CPT
456 = Constructor->getType()->getAs<FunctionProtoType>();
457 if (!CPT->hasEmptyExceptionSpec()) {
458 AllNoThrow = false;
459 break;
460 }
461 }
462 }
463
464 return FoundConstructor && AllNoThrow;
465 }
466 return false;
467 case UTT_HasNothrowConstructor:
468 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
469 // If __has_trivial_constructor (type) is true then the trait is
470 // true, else if type is a cv class or union type (or array
471 // thereof) with a default constructor that is known not to
472 // throw an exception then the trait is true, else it is false.
473 if (QueriedType->isPODType())
474 return true;
475 if (const RecordType *RT =
476 C.getBaseElementType(QueriedType)->getAs<RecordType>()) {
477 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
478 if (RD->hasTrivialConstructor())
479 return true;
480
481 if (CXXConstructorDecl *Constructor = RD->getDefaultConstructor()) {
482 const FunctionProtoType *CPT
483 = Constructor->getType()->getAs<FunctionProtoType>();
484 // TODO: check whether evaluating default arguments can throw.
485 // For now, we'll be conservative and assume that they can throw.
486 if (CPT->hasEmptyExceptionSpec() && CPT->getNumArgs() == 0)
487 return true;
488 }
489 }
490 return false;
Sebastian Redld4b25cb2010-09-02 23:19:42 +0000491 case UTT_HasVirtualDestructor:
492 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
493 // If type is a class type with a virtual destructor ([class.dtor])
494 // then the trait is true, else it is false.
495 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
496 CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
497 if (CXXDestructorDecl *Destructor = RD->getDestructor())
498 return Destructor->isVirtual();
499 }
500 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000501 }
502}
503
Ted Kremeneke3837682009-12-23 04:00:48 +0000504SourceRange CXXConstructExpr::getSourceRange() const {
505 // FIXME: Should we know where the parentheses are, if there are any?
506 for (std::reverse_iterator<Stmt**> I(&Args[NumArgs]), E(&Args[0]); I!=E;++I) {
507 // Ignore CXXDefaultExprs when computing the range, as they don't
508 // have a range.
509 if (!isa<CXXDefaultArgExpr>(*I))
510 return SourceRange(Loc, (*I)->getLocEnd());
511 }
512
513 return SourceRange(Loc);
514}
515
Douglas Gregorb4609802008-11-14 16:09:21 +0000516SourceRange CXXOperatorCallExpr::getSourceRange() const {
517 OverloadedOperatorKind Kind = getOperator();
518 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
519 if (getNumArgs() == 1)
520 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000521 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000522 getArg(0)->getSourceRange().getEnd());
523 else
524 // Postfix operator
525 return SourceRange(getArg(0)->getSourceRange().getEnd(),
526 getOperatorLoc());
527 } else if (Kind == OO_Call) {
528 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
529 } else if (Kind == OO_Subscript) {
530 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
531 } else if (getNumArgs() == 1) {
532 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
533 } else if (getNumArgs() == 2) {
534 return SourceRange(getArg(0)->getSourceRange().getBegin(),
535 getArg(1)->getSourceRange().getEnd());
536 } else {
537 return SourceRange();
538 }
539}
540
Douglas Gregor88a35142008-12-22 05:46:06 +0000541Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
542 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
543 return MemExpr->getBase();
544
545 // FIXME: Will eventually need to cope with member pointers.
546 return 0;
547}
548
Douglas Gregor00b98c22009-11-12 15:31:47 +0000549SourceRange CXXMemberCallExpr::getSourceRange() const {
550 SourceLocation LocStart = getCallee()->getLocStart();
551 if (LocStart.isInvalid() && getNumArgs() > 0)
552 LocStart = getArg(0)->getLocStart();
553 return SourceRange(LocStart, getRParenLoc());
554}
555
556
Douglas Gregor49badde2008-10-27 19:41:14 +0000557//===----------------------------------------------------------------------===//
558// Named casts
559//===----------------------------------------------------------------------===//
560
561/// getCastName - Get the name of the C++ cast being used, e.g.,
562/// "static_cast", "dynamic_cast", "reinterpret_cast", or
563/// "const_cast". The returned pointer must not be freed.
564const char *CXXNamedCastExpr::getCastName() const {
565 switch (getStmtClass()) {
566 case CXXStaticCastExprClass: return "static_cast";
567 case CXXDynamicCastExprClass: return "dynamic_cast";
568 case CXXReinterpretCastExprClass: return "reinterpret_cast";
569 case CXXConstCastExprClass: return "const_cast";
570 default: return "<invalid cast>";
571 }
572}
Douglas Gregor506ae412009-01-16 18:33:17 +0000573
John McCallf871d0c2010-08-07 06:22:56 +0000574CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
575 CastKind K, Expr *Op,
576 const CXXCastPath *BasePath,
577 TypeSourceInfo *WrittenTy,
578 SourceLocation L) {
579 unsigned PathSize = (BasePath ? BasePath->size() : 0);
580 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
581 + PathSize * sizeof(CXXBaseSpecifier*));
582 CXXStaticCastExpr *E =
583 new (Buffer) CXXStaticCastExpr(T, K, Op, PathSize, WrittenTy, L);
584 if (PathSize) E->setCastPath(*BasePath);
585 return E;
586}
587
588CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
589 unsigned PathSize) {
590 void *Buffer =
591 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
592 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
593}
594
595CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
596 CastKind K, Expr *Op,
597 const CXXCastPath *BasePath,
598 TypeSourceInfo *WrittenTy,
599 SourceLocation L) {
600 unsigned PathSize = (BasePath ? BasePath->size() : 0);
601 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
602 + PathSize * sizeof(CXXBaseSpecifier*));
603 CXXDynamicCastExpr *E =
604 new (Buffer) CXXDynamicCastExpr(T, K, Op, PathSize, WrittenTy, L);
605 if (PathSize) E->setCastPath(*BasePath);
606 return E;
607}
608
609CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
610 unsigned PathSize) {
611 void *Buffer =
612 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
613 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
614}
615
616CXXReinterpretCastExpr *
617CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, CastKind K, Expr *Op,
618 const CXXCastPath *BasePath,
619 TypeSourceInfo *WrittenTy, SourceLocation L) {
620 unsigned PathSize = (BasePath ? BasePath->size() : 0);
621 void *Buffer =
622 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
623 CXXReinterpretCastExpr *E =
624 new (Buffer) CXXReinterpretCastExpr(T, K, Op, PathSize, WrittenTy, L);
625 if (PathSize) E->setCastPath(*BasePath);
626 return E;
627}
628
629CXXReinterpretCastExpr *
630CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
631 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
632 + PathSize * sizeof(CXXBaseSpecifier*));
633 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
634}
635
636CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T, Expr *Op,
637 TypeSourceInfo *WrittenTy,
638 SourceLocation L) {
639 return new (C) CXXConstCastExpr(T, Op, WrittenTy, L);
640}
641
642CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
643 return new (C) CXXConstCastExpr(EmptyShell());
644}
645
646CXXFunctionalCastExpr *
647CXXFunctionalCastExpr::Create(ASTContext &C, QualType T,
648 TypeSourceInfo *Written, SourceLocation L,
649 CastKind K, Expr *Op, const CXXCastPath *BasePath,
650 SourceLocation R) {
651 unsigned PathSize = (BasePath ? BasePath->size() : 0);
652 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
653 + PathSize * sizeof(CXXBaseSpecifier*));
654 CXXFunctionalCastExpr *E =
655 new (Buffer) CXXFunctionalCastExpr(T, Written, L, K, Op, PathSize, R);
656 if (PathSize) E->setCastPath(*BasePath);
657 return E;
658}
659
660CXXFunctionalCastExpr *
661CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
662 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
663 + PathSize * sizeof(CXXBaseSpecifier*));
664 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
665}
666
667
Douglas Gregor65222e82009-12-23 18:19:08 +0000668CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000669CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
670 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000671 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000672 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
673 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000674}
675
Mike Stump1eb44332009-09-09 15:08:12 +0000676CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000677 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000678 return new (C) CXXTemporary(Destructor);
679}
680
Mike Stump1eb44332009-09-09 15:08:12 +0000681CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000682 CXXTemporary *Temp,
683 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000684 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000685 "Expression bound to a temporary must have record type!");
686
Anders Carlssonb859f352009-05-30 20:34:37 +0000687 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000688}
689
Anders Carlsson8e587a12009-05-30 20:56:46 +0000690CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000691 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000692 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000693 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000694 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000695 unsigned NumArgs,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000696 SourceLocation rParenLoc,
697 bool ZeroInitialization)
Douglas Gregor99a2e602009-12-16 01:38:02 +0000698 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, tyBeginLoc,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000699 Cons, false, Args, NumArgs, ZeroInitialization),
Anders Carlsson524fa132009-04-24 17:34:38 +0000700 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000701}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000702
Mike Stump1eb44332009-09-09 15:08:12 +0000703CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000704 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000705 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000706 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000707 bool ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000708 ConstructionKind ConstructKind) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000709 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000710 Elidable, Args, NumArgs, ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000711 ConstructKind);
Anders Carlssone349bea2009-04-23 02:32:43 +0000712}
713
Mike Stump1eb44332009-09-09 15:08:12 +0000714CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000715 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000716 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000717 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000718 bool ZeroInitialization,
719 ConstructionKind ConstructKind)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000720: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000721 T->isDependentType(),
722 (T->isDependentType() ||
723 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Douglas Gregor16006c92009-12-16 18:50:27 +0000724 Constructor(D), Loc(Loc), Elidable(elidable),
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000725 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
726 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000727{
728 if (NumArgs) {
729 Args = new (C) Stmt*[NumArgs];
730
731 for (unsigned i = 0; i != NumArgs; ++i) {
732 assert(args[i] && "NULL argument in CXXConstructExpr");
733 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000734 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000735 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000736}
737
Ted Kremenekd04ed412010-05-10 20:06:30 +0000738CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C,
739 Expr *subexpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000740 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000741 unsigned numtemps)
Chris Lattnerd2598362010-05-10 00:25:06 +0000742 : Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000743 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000744 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000745 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000746 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000747 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000748 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000749 }
750}
751
Ted Kremenekd04ed412010-05-10 20:06:30 +0000752void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000753 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000754 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000755 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000756}
757
758
Mike Stump1eb44332009-09-09 15:08:12 +0000759CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000760 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000761 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000762 unsigned NumTemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000763 return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000764}
765
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000766// CXXBindTemporaryExpr
767Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
768 return &SubExpr;
769}
770
Mike Stump1eb44332009-09-09 15:08:12 +0000771Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000772 return &SubExpr + 1;
773}
774
Anders Carlssone349bea2009-04-23 02:32:43 +0000775// CXXConstructExpr
776Stmt::child_iterator CXXConstructExpr::child_begin() {
777 return &Args[0];
778}
779Stmt::child_iterator CXXConstructExpr::child_end() {
780 return &Args[0]+NumArgs;
781}
782
Anders Carlsson55674ac2009-05-01 22:21:22 +0000783// CXXExprWithTemporaries
784Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
785 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000786}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000787
Mike Stump1eb44332009-09-09 15:08:12 +0000788Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000789 return &SubExpr + 1;
790}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000791
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000792CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
793 SourceLocation TyBeginLoc,
794 QualType T,
795 SourceLocation LParenLoc,
796 Expr **Args,
797 unsigned NumArgs,
798 SourceLocation RParenLoc)
799 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
800 T->isDependentType(), true),
801 TyBeginLoc(TyBeginLoc),
802 Type(T),
803 LParenLoc(LParenLoc),
804 RParenLoc(RParenLoc),
805 NumArgs(NumArgs) {
806 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
807 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
808}
809
810CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000811CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000812 SourceLocation TyBegin,
813 QualType T,
814 SourceLocation LParenLoc,
815 Expr **Args,
816 unsigned NumArgs,
817 SourceLocation RParenLoc) {
818 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
819 sizeof(Expr *) * NumArgs);
820 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
821 Args, NumArgs, RParenLoc);
822}
823
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000824CXXUnresolvedConstructExpr *
825CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
826 Stmt::EmptyShell Empty;
827 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
828 sizeof(Expr *) * NumArgs);
829 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
830}
831
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000832Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
833 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
834}
835
836Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
837 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
838}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000839
John McCall865d4472009-11-19 22:55:06 +0000840CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000841 Expr *Base, QualType BaseType,
842 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000843 SourceLocation OperatorLoc,
844 NestedNameSpecifier *Qualifier,
845 SourceRange QualifierRange,
846 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000847 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000848 const TemplateArgumentListInfo *TemplateArgs)
John McCall865d4472009-11-19 22:55:06 +0000849 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000850 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
851 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000852 OperatorLoc(OperatorLoc),
853 Qualifier(Qualifier), QualifierRange(QualifierRange),
854 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000855 MemberNameInfo(MemberNameInfo) {
John McCalld5532b62009-11-23 01:53:49 +0000856 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000857 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000858}
859
John McCall865d4472009-11-19 22:55:06 +0000860CXXDependentScopeMemberExpr *
861CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000862 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000863 SourceLocation OperatorLoc,
864 NestedNameSpecifier *Qualifier,
865 SourceRange QualifierRange,
866 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000867 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000868 const TemplateArgumentListInfo *TemplateArgs) {
869 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000870 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
871 IsArrow, OperatorLoc,
872 Qualifier, QualifierRange,
873 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000874 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000875
John McCalld5532b62009-11-23 01:53:49 +0000876 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
877 if (TemplateArgs)
878 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
879
880 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000881 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
882 IsArrow, OperatorLoc,
883 Qualifier, QualifierRange,
884 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000885 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000886}
887
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000888CXXDependentScopeMemberExpr *
889CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
890 unsigned NumTemplateArgs) {
891 if (NumTemplateArgs == 0)
892 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
893 0, SourceLocation(), 0,
894 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000895 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000896
897 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
898 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
899 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
900 CXXDependentScopeMemberExpr *E
901 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
902 0, SourceLocation(), 0,
903 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000904 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000905 E->HasExplicitTemplateArgs = true;
906 return E;
907}
908
John McCall865d4472009-11-19 22:55:06 +0000909Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000910 return child_iterator(&Base);
911}
912
John McCall865d4472009-11-19 22:55:06 +0000913Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000914 if (isImplicitAccess())
915 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000916 return child_iterator(&Base + 1);
917}
John McCall129e2df2009-11-30 22:42:35 +0000918
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000919UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T,
920 bool Dependent,
John McCall129e2df2009-11-30 22:42:35 +0000921 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000922 Expr *Base, QualType BaseType,
923 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000924 SourceLocation OperatorLoc,
925 NestedNameSpecifier *Qualifier,
926 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000927 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000928 const TemplateArgumentListInfo *TemplateArgs,
929 UnresolvedSetIterator Begin,
930 UnresolvedSetIterator End)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000931 : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent,
Abramo Bagnara25777432010-08-11 22:01:17 +0000932 Qualifier, QualifierRange, MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000933 TemplateArgs != 0, Begin, End),
John McCall7bb12da2010-02-02 06:20:04 +0000934 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
935 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000936 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000937 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000938}
939
940UnresolvedMemberExpr *
941UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
942 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000943 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000944 SourceLocation OperatorLoc,
945 NestedNameSpecifier *Qualifier,
946 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000947 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000948 const TemplateArgumentListInfo *TemplateArgs,
949 UnresolvedSetIterator Begin,
950 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000951 std::size_t size = sizeof(UnresolvedMemberExpr);
952 if (TemplateArgs)
953 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
954
955 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000956 return new (Mem) UnresolvedMemberExpr(C,
John McCall129e2df2009-11-30 22:42:35 +0000957 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000958 Dependent, HasUnresolvedUsing, Base, BaseType,
959 IsArrow, OperatorLoc, Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000960 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000961}
962
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000963UnresolvedMemberExpr *
964UnresolvedMemberExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
965 std::size_t size = sizeof(UnresolvedMemberExpr);
966 if (NumTemplateArgs != 0)
967 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
968
969 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
970 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
971 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
972 return E;
973}
974
John McCallc373d482010-01-27 01:50:18 +0000975CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
976 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
977
978 // If there was a nested name specifier, it names the naming class.
979 // It can't be dependent: after all, we were actually able to do the
980 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000981 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000982 if (getQualifier()) {
983 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000984 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000985 Record = T->getAsCXXRecordDecl();
986 assert(Record && "qualifier in member expression does not name record");
987 }
John McCallc373d482010-01-27 01:50:18 +0000988 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000989 else {
John McCallc373d482010-01-27 01:50:18 +0000990 QualType BaseType = getBaseType().getNonReferenceType();
991 if (isArrow()) {
992 const PointerType *PT = BaseType->getAs<PointerType>();
993 assert(PT && "base of arrow member access is not pointer");
994 BaseType = PT->getPointeeType();
995 }
996
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000997 Record = BaseType->getAsCXXRecordDecl();
998 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000999 }
1000
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001001 return Record;
John McCallc373d482010-01-27 01:50:18 +00001002}
1003
John McCall129e2df2009-11-30 22:42:35 +00001004Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
1005 return child_iterator(&Base);
1006}
1007
1008Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +00001009 if (isImplicitAccess())
1010 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +00001011 return child_iterator(&Base + 1);
1012}