blob: 669cac014c91fc7db5b406b7478d208db8325f44 [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 Redl64b45f72009-01-05 20:52:13 +0000491 }
492}
493
Ted Kremeneke3837682009-12-23 04:00:48 +0000494SourceRange CXXConstructExpr::getSourceRange() const {
495 // FIXME: Should we know where the parentheses are, if there are any?
496 for (std::reverse_iterator<Stmt**> I(&Args[NumArgs]), E(&Args[0]); I!=E;++I) {
497 // Ignore CXXDefaultExprs when computing the range, as they don't
498 // have a range.
499 if (!isa<CXXDefaultArgExpr>(*I))
500 return SourceRange(Loc, (*I)->getLocEnd());
501 }
502
503 return SourceRange(Loc);
504}
505
Douglas Gregorb4609802008-11-14 16:09:21 +0000506SourceRange CXXOperatorCallExpr::getSourceRange() const {
507 OverloadedOperatorKind Kind = getOperator();
508 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
509 if (getNumArgs() == 1)
510 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000511 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000512 getArg(0)->getSourceRange().getEnd());
513 else
514 // Postfix operator
515 return SourceRange(getArg(0)->getSourceRange().getEnd(),
516 getOperatorLoc());
517 } else if (Kind == OO_Call) {
518 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
519 } else if (Kind == OO_Subscript) {
520 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
521 } else if (getNumArgs() == 1) {
522 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
523 } else if (getNumArgs() == 2) {
524 return SourceRange(getArg(0)->getSourceRange().getBegin(),
525 getArg(1)->getSourceRange().getEnd());
526 } else {
527 return SourceRange();
528 }
529}
530
Douglas Gregor88a35142008-12-22 05:46:06 +0000531Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
532 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
533 return MemExpr->getBase();
534
535 // FIXME: Will eventually need to cope with member pointers.
536 return 0;
537}
538
Douglas Gregor00b98c22009-11-12 15:31:47 +0000539SourceRange CXXMemberCallExpr::getSourceRange() const {
540 SourceLocation LocStart = getCallee()->getLocStart();
541 if (LocStart.isInvalid() && getNumArgs() > 0)
542 LocStart = getArg(0)->getLocStart();
543 return SourceRange(LocStart, getRParenLoc());
544}
545
546
Douglas Gregor49badde2008-10-27 19:41:14 +0000547//===----------------------------------------------------------------------===//
548// Named casts
549//===----------------------------------------------------------------------===//
550
551/// getCastName - Get the name of the C++ cast being used, e.g.,
552/// "static_cast", "dynamic_cast", "reinterpret_cast", or
553/// "const_cast". The returned pointer must not be freed.
554const char *CXXNamedCastExpr::getCastName() const {
555 switch (getStmtClass()) {
556 case CXXStaticCastExprClass: return "static_cast";
557 case CXXDynamicCastExprClass: return "dynamic_cast";
558 case CXXReinterpretCastExprClass: return "reinterpret_cast";
559 case CXXConstCastExprClass: return "const_cast";
560 default: return "<invalid cast>";
561 }
562}
Douglas Gregor506ae412009-01-16 18:33:17 +0000563
John McCallf871d0c2010-08-07 06:22:56 +0000564CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
565 CastKind K, Expr *Op,
566 const CXXCastPath *BasePath,
567 TypeSourceInfo *WrittenTy,
568 SourceLocation L) {
569 unsigned PathSize = (BasePath ? BasePath->size() : 0);
570 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
571 + PathSize * sizeof(CXXBaseSpecifier*));
572 CXXStaticCastExpr *E =
573 new (Buffer) CXXStaticCastExpr(T, K, Op, PathSize, WrittenTy, L);
574 if (PathSize) E->setCastPath(*BasePath);
575 return E;
576}
577
578CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
579 unsigned PathSize) {
580 void *Buffer =
581 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
582 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
583}
584
585CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
586 CastKind K, Expr *Op,
587 const CXXCastPath *BasePath,
588 TypeSourceInfo *WrittenTy,
589 SourceLocation L) {
590 unsigned PathSize = (BasePath ? BasePath->size() : 0);
591 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
592 + PathSize * sizeof(CXXBaseSpecifier*));
593 CXXDynamicCastExpr *E =
594 new (Buffer) CXXDynamicCastExpr(T, K, Op, PathSize, WrittenTy, L);
595 if (PathSize) E->setCastPath(*BasePath);
596 return E;
597}
598
599CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
600 unsigned PathSize) {
601 void *Buffer =
602 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
603 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
604}
605
606CXXReinterpretCastExpr *
607CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, CastKind K, Expr *Op,
608 const CXXCastPath *BasePath,
609 TypeSourceInfo *WrittenTy, SourceLocation L) {
610 unsigned PathSize = (BasePath ? BasePath->size() : 0);
611 void *Buffer =
612 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
613 CXXReinterpretCastExpr *E =
614 new (Buffer) CXXReinterpretCastExpr(T, K, Op, PathSize, WrittenTy, L);
615 if (PathSize) E->setCastPath(*BasePath);
616 return E;
617}
618
619CXXReinterpretCastExpr *
620CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
621 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
622 + PathSize * sizeof(CXXBaseSpecifier*));
623 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
624}
625
626CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T, Expr *Op,
627 TypeSourceInfo *WrittenTy,
628 SourceLocation L) {
629 return new (C) CXXConstCastExpr(T, Op, WrittenTy, L);
630}
631
632CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
633 return new (C) CXXConstCastExpr(EmptyShell());
634}
635
636CXXFunctionalCastExpr *
637CXXFunctionalCastExpr::Create(ASTContext &C, QualType T,
638 TypeSourceInfo *Written, SourceLocation L,
639 CastKind K, Expr *Op, const CXXCastPath *BasePath,
640 SourceLocation R) {
641 unsigned PathSize = (BasePath ? BasePath->size() : 0);
642 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
643 + PathSize * sizeof(CXXBaseSpecifier*));
644 CXXFunctionalCastExpr *E =
645 new (Buffer) CXXFunctionalCastExpr(T, Written, L, K, Op, PathSize, R);
646 if (PathSize) E->setCastPath(*BasePath);
647 return E;
648}
649
650CXXFunctionalCastExpr *
651CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
652 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
653 + PathSize * sizeof(CXXBaseSpecifier*));
654 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
655}
656
657
Douglas Gregor65222e82009-12-23 18:19:08 +0000658CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000659CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
660 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000661 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000662 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
663 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000664}
665
Mike Stump1eb44332009-09-09 15:08:12 +0000666CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000667 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000668 return new (C) CXXTemporary(Destructor);
669}
670
Mike Stump1eb44332009-09-09 15:08:12 +0000671CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000672 CXXTemporary *Temp,
673 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000674 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000675 "Expression bound to a temporary must have record type!");
676
Anders Carlssonb859f352009-05-30 20:34:37 +0000677 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000678}
679
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000680CXXBindReferenceExpr *CXXBindReferenceExpr::Create(ASTContext &C, Expr *SubExpr,
681 bool ExtendsLifetime,
682 bool RequiresTemporaryCopy) {
683 return new (C) CXXBindReferenceExpr(SubExpr,
684 ExtendsLifetime,
685 RequiresTemporaryCopy);
686}
687
Anders Carlsson8e587a12009-05-30 20:56:46 +0000688CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000689 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000690 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000691 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000692 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000693 unsigned NumArgs,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000694 SourceLocation rParenLoc,
695 bool ZeroInitialization)
Douglas Gregor99a2e602009-12-16 01:38:02 +0000696 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, tyBeginLoc,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000697 Cons, false, Args, NumArgs, ZeroInitialization),
Anders Carlsson524fa132009-04-24 17:34:38 +0000698 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000699}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000700
Mike Stump1eb44332009-09-09 15:08:12 +0000701CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000702 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000703 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000704 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000705 bool ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000706 ConstructionKind ConstructKind) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000707 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000708 Elidable, Args, NumArgs, ZeroInitialization,
Anders Carlssonfcaeef22010-05-02 23:53:04 +0000709 ConstructKind);
Anders Carlssone349bea2009-04-23 02:32:43 +0000710}
711
Mike Stump1eb44332009-09-09 15:08:12 +0000712CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000713 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000714 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000715 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000716 bool ZeroInitialization,
717 ConstructionKind ConstructKind)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000718: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000719 T->isDependentType(),
720 (T->isDependentType() ||
721 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Douglas Gregor16006c92009-12-16 18:50:27 +0000722 Constructor(D), Loc(Loc), Elidable(elidable),
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000723 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
724 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000725{
726 if (NumArgs) {
727 Args = new (C) Stmt*[NumArgs];
728
729 for (unsigned i = 0; i != NumArgs; ++i) {
730 assert(args[i] && "NULL argument in CXXConstructExpr");
731 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000732 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000733 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000734}
735
Ted Kremenekd04ed412010-05-10 20:06:30 +0000736CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C,
737 Expr *subexpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000738 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000739 unsigned numtemps)
Chris Lattnerd2598362010-05-10 00:25:06 +0000740 : Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000741 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000742 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000743 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000744 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000745 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000746 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000747 }
748}
749
Ted Kremenekd04ed412010-05-10 20:06:30 +0000750void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000751 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000752 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000753 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000754}
755
756
Mike Stump1eb44332009-09-09 15:08:12 +0000757CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000758 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000759 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000760 unsigned NumTemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000761 return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000762}
763
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000764// CXXBindTemporaryExpr
765Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
766 return &SubExpr;
767}
768
Mike Stump1eb44332009-09-09 15:08:12 +0000769Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000770 return &SubExpr + 1;
771}
772
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000773// CXXBindReferenceExpr
774Stmt::child_iterator CXXBindReferenceExpr::child_begin() {
775 return &SubExpr;
776}
777
778Stmt::child_iterator CXXBindReferenceExpr::child_end() {
779 return &SubExpr + 1;
780}
781
Anders Carlssone349bea2009-04-23 02:32:43 +0000782// CXXConstructExpr
783Stmt::child_iterator CXXConstructExpr::child_begin() {
784 return &Args[0];
785}
786Stmt::child_iterator CXXConstructExpr::child_end() {
787 return &Args[0]+NumArgs;
788}
789
Anders Carlsson55674ac2009-05-01 22:21:22 +0000790// CXXExprWithTemporaries
791Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
792 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000793}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000794
Mike Stump1eb44332009-09-09 15:08:12 +0000795Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000796 return &SubExpr + 1;
797}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000798
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000799CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
800 SourceLocation TyBeginLoc,
801 QualType T,
802 SourceLocation LParenLoc,
803 Expr **Args,
804 unsigned NumArgs,
805 SourceLocation RParenLoc)
806 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
807 T->isDependentType(), true),
808 TyBeginLoc(TyBeginLoc),
809 Type(T),
810 LParenLoc(LParenLoc),
811 RParenLoc(RParenLoc),
812 NumArgs(NumArgs) {
813 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
814 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
815}
816
817CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000818CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000819 SourceLocation TyBegin,
820 QualType T,
821 SourceLocation LParenLoc,
822 Expr **Args,
823 unsigned NumArgs,
824 SourceLocation RParenLoc) {
825 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
826 sizeof(Expr *) * NumArgs);
827 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
828 Args, NumArgs, RParenLoc);
829}
830
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000831CXXUnresolvedConstructExpr *
832CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
833 Stmt::EmptyShell Empty;
834 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
835 sizeof(Expr *) * NumArgs);
836 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
837}
838
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000839Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
840 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
841}
842
843Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
844 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
845}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000846
John McCall865d4472009-11-19 22:55:06 +0000847CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000848 Expr *Base, QualType BaseType,
849 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000850 SourceLocation OperatorLoc,
851 NestedNameSpecifier *Qualifier,
852 SourceRange QualifierRange,
853 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000854 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000855 const TemplateArgumentListInfo *TemplateArgs)
John McCall865d4472009-11-19 22:55:06 +0000856 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000857 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
858 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000859 OperatorLoc(OperatorLoc),
860 Qualifier(Qualifier), QualifierRange(QualifierRange),
861 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000862 MemberNameInfo(MemberNameInfo) {
John McCalld5532b62009-11-23 01:53:49 +0000863 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000864 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000865}
866
John McCall865d4472009-11-19 22:55:06 +0000867CXXDependentScopeMemberExpr *
868CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000869 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000870 SourceLocation OperatorLoc,
871 NestedNameSpecifier *Qualifier,
872 SourceRange QualifierRange,
873 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000874 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000875 const TemplateArgumentListInfo *TemplateArgs) {
876 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000877 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
878 IsArrow, OperatorLoc,
879 Qualifier, QualifierRange,
880 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000881 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
John McCalld5532b62009-11-23 01:53:49 +0000883 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
884 if (TemplateArgs)
885 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
886
887 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000888 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
889 IsArrow, OperatorLoc,
890 Qualifier, QualifierRange,
891 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000892 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000893}
894
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000895CXXDependentScopeMemberExpr *
896CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
897 unsigned NumTemplateArgs) {
898 if (NumTemplateArgs == 0)
899 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
900 0, SourceLocation(), 0,
901 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000902 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000903
904 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
905 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
906 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
907 CXXDependentScopeMemberExpr *E
908 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
909 0, SourceLocation(), 0,
910 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000911 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000912 E->HasExplicitTemplateArgs = true;
913 return E;
914}
915
John McCall865d4472009-11-19 22:55:06 +0000916Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000917 return child_iterator(&Base);
918}
919
John McCall865d4472009-11-19 22:55:06 +0000920Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000921 if (isImplicitAccess())
922 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000923 return child_iterator(&Base + 1);
924}
John McCall129e2df2009-11-30 22:42:35 +0000925
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000926UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T,
927 bool Dependent,
John McCall129e2df2009-11-30 22:42:35 +0000928 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000929 Expr *Base, QualType BaseType,
930 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000931 SourceLocation OperatorLoc,
932 NestedNameSpecifier *Qualifier,
933 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000934 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000935 const TemplateArgumentListInfo *TemplateArgs,
936 UnresolvedSetIterator Begin,
937 UnresolvedSetIterator End)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000938 : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent,
Abramo Bagnara25777432010-08-11 22:01:17 +0000939 Qualifier, QualifierRange, MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000940 TemplateArgs != 0, Begin, End),
John McCall7bb12da2010-02-02 06:20:04 +0000941 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
942 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000943 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000944 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000945}
946
947UnresolvedMemberExpr *
948UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
949 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000950 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000951 SourceLocation OperatorLoc,
952 NestedNameSpecifier *Qualifier,
953 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000954 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000955 const TemplateArgumentListInfo *TemplateArgs,
956 UnresolvedSetIterator Begin,
957 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000958 std::size_t size = sizeof(UnresolvedMemberExpr);
959 if (TemplateArgs)
960 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
961
962 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000963 return new (Mem) UnresolvedMemberExpr(C,
John McCall129e2df2009-11-30 22:42:35 +0000964 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000965 Dependent, HasUnresolvedUsing, Base, BaseType,
966 IsArrow, OperatorLoc, Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000967 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000968}
969
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000970UnresolvedMemberExpr *
971UnresolvedMemberExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
972 std::size_t size = sizeof(UnresolvedMemberExpr);
973 if (NumTemplateArgs != 0)
974 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
975
976 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
977 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
978 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
979 return E;
980}
981
John McCallc373d482010-01-27 01:50:18 +0000982CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
983 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
984
985 // If there was a nested name specifier, it names the naming class.
986 // It can't be dependent: after all, we were actually able to do the
987 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000988 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000989 if (getQualifier()) {
990 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000991 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000992 Record = T->getAsCXXRecordDecl();
993 assert(Record && "qualifier in member expression does not name record");
994 }
John McCallc373d482010-01-27 01:50:18 +0000995 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000996 else {
John McCallc373d482010-01-27 01:50:18 +0000997 QualType BaseType = getBaseType().getNonReferenceType();
998 if (isArrow()) {
999 const PointerType *PT = BaseType->getAs<PointerType>();
1000 assert(PT && "base of arrow member access is not pointer");
1001 BaseType = PT->getPointeeType();
1002 }
1003
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001004 Record = BaseType->getAsCXXRecordDecl();
1005 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001006 }
1007
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001008 return Record;
John McCallc373d482010-01-27 01:50:18 +00001009}
1010
John McCall129e2df2009-11-30 22:42:35 +00001011Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
1012 return child_iterator(&Base);
1013}
1014
1015Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +00001016 if (isImplicitAccess())
1017 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +00001018 return child_iterator(&Base + 1);
1019}