blob: b9a4ee6e4d2c88c8252ae367b31356d5de43d2a5 [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
21//===----------------------------------------------------------------------===//
22// Child Iterators for iterating over subexpressions/substatements
23//===----------------------------------------------------------------------===//
24
Sebastian Redlc42e1182008-11-11 11:37:55 +000025// CXXTypeidExpr - has child iterators if the operand is an expression
26Stmt::child_iterator CXXTypeidExpr::child_begin() {
Sebastian Redld4575892008-12-03 23:17:54 +000027 return isTypeOperand() ? child_iterator() : &Operand.Ex;
Sebastian Redlc42e1182008-11-11 11:37:55 +000028}
29Stmt::child_iterator CXXTypeidExpr::child_end() {
Sebastian Redld4575892008-12-03 23:17:54 +000030 return isTypeOperand() ? child_iterator() : &Operand.Ex+1;
Sebastian Redlc42e1182008-11-11 11:37:55 +000031}
Ted Kremeneka758d092007-08-24 20:21:10 +000032
Ted Kremeneka758d092007-08-24 20:21:10 +000033// CXXBoolLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000034Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +000035 return child_iterator();
36}
37Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
38 return child_iterator();
39}
Chris Lattner50dd2892008-02-26 00:51:44 +000040
Sebastian Redl6e8ed162009-05-10 18:38:11 +000041// CXXNullPtrLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000042Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() {
Sebastian Redl6e8ed162009-05-10 18:38:11 +000043 return child_iterator();
44}
45Stmt::child_iterator CXXNullPtrLiteralExpr::child_end() {
46 return child_iterator();
47}
48
Douglas Gregor796da182008-11-04 14:32:21 +000049// CXXThisExpr
50Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
51Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
52
Chris Lattner50dd2892008-02-26 00:51:44 +000053// CXXThrowExpr
Ted Kremenek1060aff2008-06-17 03:11:08 +000054Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
Chris Lattner50dd2892008-02-26 00:51:44 +000055Stmt::child_iterator CXXThrowExpr::child_end() {
56 // If Op is 0, we are processing throw; which has no children.
Ted Kremenek1060aff2008-06-17 03:11:08 +000057 return Op ? &Op+1 : &Op;
Chris Lattner50dd2892008-02-26 00:51:44 +000058}
Chris Lattner04421082008-04-08 04:40:51 +000059
60// CXXDefaultArgExpr
61Stmt::child_iterator CXXDefaultArgExpr::child_begin() {
Chris Lattner8123a952008-04-10 02:22:51 +000062 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000063}
64Stmt::child_iterator CXXDefaultArgExpr::child_end() {
Chris Lattner8123a952008-04-10 02:22:51 +000065 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000066}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000067
68// CXXZeroInitValueExpr
Mike Stump1eb44332009-09-09 15:08:12 +000069Stmt::child_iterator CXXZeroInitValueExpr::child_begin() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000070 return child_iterator();
71}
72Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
73 return child_iterator();
74}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +000075
Sebastian Redl4c5d3202008-11-21 19:14:01 +000076// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000077CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000078 Expr **placementArgs, unsigned numPlaceArgs,
Sebastian Redlcee63fb2008-12-02 14:43:59 +000079 bool parenTypeId, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000080 CXXConstructorDecl *constructor, bool initializer,
81 Expr **constructorArgs, unsigned numConsArgs,
82 FunctionDecl *operatorDelete, QualType ty,
83 SourceLocation startLoc, SourceLocation endLoc)
Sebastian Redl28507842009-02-26 14:39:58 +000084 : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()),
85 GlobalNew(globalNew), ParenTypeId(parenTypeId),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000086 Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs),
Sebastian Redl4c5d3202008-11-21 19:14:01 +000087 NumConstructorArgs(numConsArgs), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000088 OperatorDelete(operatorDelete), Constructor(constructor),
Mike Stump1eb44332009-09-09 15:08:12 +000089 StartLoc(startLoc), EndLoc(endLoc) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +000090 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
Ted Kremenekad7fe862010-02-11 22:51:03 +000091 SubExprs = new (C) Stmt*[TotalSize];
Sebastian Redl4c5d3202008-11-21 19:14:01 +000092 unsigned i = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +000093 if (Array)
94 SubExprs[i++] = arraySize;
95 for (unsigned j = 0; j < NumPlacementArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +000096 SubExprs[i++] = placementArgs[j];
Sebastian Redlcee63fb2008-12-02 14:43:59 +000097 for (unsigned j = 0; j < NumConstructorArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +000098 SubExprs[i++] = constructorArgs[j];
99 assert(i == TotalSize);
100}
101
Ted Kremenekad7fe862010-02-11 22:51:03 +0000102void CXXNewExpr::DoDestroy(ASTContext &C) {
103 DestroyChildren(C);
104 if (SubExprs)
105 C.Deallocate(SubExprs);
106 this->~CXXNewExpr();
107 C.Deallocate((void*)this);
108}
109
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000110Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
111Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000112 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000113}
114
115// CXXDeleteExpr
116Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
117Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
118
Douglas Gregora71d8192009-09-04 17:36:40 +0000119// CXXPseudoDestructorExpr
120Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; }
121Stmt::child_iterator CXXPseudoDestructorExpr::child_end() {
122 return &Base + 1;
123}
124
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000125PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
126 : Type(Info)
127{
128 Location = Info->getTypeLoc().getSourceRange().getBegin();
129}
130
131QualType CXXPseudoDestructorExpr::getDestroyedType() const {
132 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
133 return TInfo->getType();
134
135 return QualType();
136}
137
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000138SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000139 SourceLocation End = DestroyedType.getLocation();
140 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
141 End = TInfo->getTypeLoc().getSourceRange().getEnd();
142 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000143}
144
145
John McCallba135432009-11-21 08:51:07 +0000146// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000147UnresolvedLookupExpr *
148UnresolvedLookupExpr::Create(ASTContext &C, bool Dependent,
John McCallc373d482010-01-27 01:50:18 +0000149 CXXRecordDecl *NamingClass,
John McCallf7a1a742009-11-24 19:00:30 +0000150 NestedNameSpecifier *Qualifier,
151 SourceRange QualifierRange, DeclarationName Name,
152 SourceLocation NameLoc, bool ADL,
153 const TemplateArgumentListInfo &Args)
154{
155 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
156 ExplicitTemplateArgumentList::sizeFor(Args));
157 UnresolvedLookupExpr *ULE
158 = new (Mem) UnresolvedLookupExpr(Dependent ? C.DependentTy : C.OverloadTy,
John McCallc373d482010-01-27 01:50:18 +0000159 Dependent, NamingClass,
160 Qualifier, QualifierRange,
John McCallf7a1a742009-11-24 19:00:30 +0000161 Name, NameLoc, ADL,
162 /*Overload*/ true,
163 /*ExplicitTemplateArgs*/ true);
164
165 reinterpret_cast<ExplicitTemplateArgumentList*>(ULE+1)->initializeFrom(Args);
166
167 return ULE;
168}
169
John McCall7bb12da2010-02-02 06:20:04 +0000170bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin,
171 UnresolvedSetIterator End,
172 const TemplateArgumentListInfo *Args) {
John McCalleec51cf2010-01-20 00:46:10 +0000173 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I)
John McCallf7a1a742009-11-24 19:00:30 +0000174 if ((*I)->getDeclContext()->isDependentContext())
175 return true;
176
177 if (Args && TemplateSpecializationType::anyDependentTemplateArguments(*Args))
178 return true;
179
180 return false;
181}
182
John McCallba135432009-11-21 08:51:07 +0000183Stmt::child_iterator UnresolvedLookupExpr::child_begin() {
Mike Stump1eb44332009-09-09 15:08:12 +0000184 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000185}
John McCallba135432009-11-21 08:51:07 +0000186Stmt::child_iterator UnresolvedLookupExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000187 return child_iterator();
188}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000189// UnaryTypeTraitExpr
190Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
191 return child_iterator();
192}
193Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
194 return child_iterator();
195}
196
John McCall865d4472009-11-19 22:55:06 +0000197// DependentScopeDeclRefExpr
John McCallf7a1a742009-11-24 19:00:30 +0000198DependentScopeDeclRefExpr *
199DependentScopeDeclRefExpr::Create(ASTContext &C,
200 NestedNameSpecifier *Qualifier,
201 SourceRange QualifierRange,
202 DeclarationName Name,
203 SourceLocation NameLoc,
204 const TemplateArgumentListInfo *Args) {
205 std::size_t size = sizeof(DependentScopeDeclRefExpr);
206 if (Args) size += ExplicitTemplateArgumentList::sizeFor(*Args);
207 void *Mem = C.Allocate(size);
208
209 DependentScopeDeclRefExpr *DRE
210 = new (Mem) DependentScopeDeclRefExpr(C.DependentTy,
211 Qualifier, QualifierRange,
212 Name, NameLoc,
213 Args != 0);
214
215 if (Args)
216 reinterpret_cast<ExplicitTemplateArgumentList*>(DRE+1)
217 ->initializeFrom(*Args);
218
219 return DRE;
220}
221
John McCall865d4472009-11-19 22:55:06 +0000222StmtIterator DependentScopeDeclRefExpr::child_begin() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000223 return child_iterator();
224}
225
John McCall865d4472009-11-19 22:55:06 +0000226StmtIterator DependentScopeDeclRefExpr::child_end() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000227 return child_iterator();
228}
229
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000230bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000231 switch(UTT) {
232 default: assert(false && "Unknown type trait or not implemented");
233 case UTT_IsPOD: return QueriedType->isPODType();
Sebastian Redlccf43502009-12-03 00:13:20 +0000234 case UTT_IsLiteral: return QueriedType->isLiteralType();
Sebastian Redl64b45f72009-01-05 20:52:13 +0000235 case UTT_IsClass: // Fallthrough
236 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000237 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000238 bool Union = Record->getDecl()->isUnion();
239 return UTT == UTT_IsUnion ? Union : !Union;
240 }
241 return false;
242 case UTT_IsEnum: return QueriedType->isEnumeralType();
243 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000244 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000245 // Type traits are only parsed in C++, so we've got CXXRecords.
246 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
247 }
248 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000249 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000250 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000251 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
252 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000253 case UTT_IsEmpty:
254 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
255 return !Record->getDecl()->isUnion()
256 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
257 }
258 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000259 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000260 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
261 // If __is_pod (type) is true then the trait is true, else if type is
262 // a cv class or union type (or array thereof) with a trivial default
263 // constructor ([class.ctor]) then the trait is true, else it is false.
264 if (QueriedType->isPODType())
265 return true;
266 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000267 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000268 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000269 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000270 case UTT_HasTrivialCopy:
271 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
272 // If __is_pod (type) is true or type is a reference type then
273 // the trait is true, else if type is a cv class or union type
274 // with a trivial copy constructor ([class.copy]) then the trait
275 // is true, else it is false.
276 if (QueriedType->isPODType() || QueriedType->isReferenceType())
277 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000278 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000279 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
280 return false;
281 case UTT_HasTrivialAssign:
282 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
283 // If type is const qualified or is a reference type then the
284 // trait is false. Otherwise if __is_pod (type) is true then the
285 // trait is true, else if type is a cv class or union type with
286 // a trivial copy assignment ([class.copy]) then the trait is
287 // true, else it is false.
288 // Note: the const and reference restrictions are interesting,
289 // given that const and reference members don't prevent a class
290 // from having a trivial copy assignment operator (but do cause
291 // errors if the copy assignment operator is actually used, q.v.
292 // [class.copy]p12).
293
294 if (C.getBaseElementType(QueriedType).isConstQualified())
295 return false;
296 if (QueriedType->isPODType())
297 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000298 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000299 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
300 return false;
301 case UTT_HasTrivialDestructor:
302 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
303 // If __is_pod (type) is true or type is a reference type
304 // then the trait is true, else if type is a cv class or union
305 // type (or array thereof) with a trivial destructor
306 // ([class.dtor]) then the trait is true, else it is
307 // false.
308 if (QueriedType->isPODType() || QueriedType->isReferenceType())
309 return true;
310 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000311 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000312 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
313 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000314 }
315}
316
Ted Kremeneke3837682009-12-23 04:00:48 +0000317SourceRange CXXConstructExpr::getSourceRange() const {
318 // FIXME: Should we know where the parentheses are, if there are any?
319 for (std::reverse_iterator<Stmt**> I(&Args[NumArgs]), E(&Args[0]); I!=E;++I) {
320 // Ignore CXXDefaultExprs when computing the range, as they don't
321 // have a range.
322 if (!isa<CXXDefaultArgExpr>(*I))
323 return SourceRange(Loc, (*I)->getLocEnd());
324 }
325
326 return SourceRange(Loc);
327}
328
Douglas Gregorb4609802008-11-14 16:09:21 +0000329SourceRange CXXOperatorCallExpr::getSourceRange() const {
330 OverloadedOperatorKind Kind = getOperator();
331 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
332 if (getNumArgs() == 1)
333 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000334 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000335 getArg(0)->getSourceRange().getEnd());
336 else
337 // Postfix operator
338 return SourceRange(getArg(0)->getSourceRange().getEnd(),
339 getOperatorLoc());
340 } else if (Kind == OO_Call) {
341 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
342 } else if (Kind == OO_Subscript) {
343 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
344 } else if (getNumArgs() == 1) {
345 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
346 } else if (getNumArgs() == 2) {
347 return SourceRange(getArg(0)->getSourceRange().getBegin(),
348 getArg(1)->getSourceRange().getEnd());
349 } else {
350 return SourceRange();
351 }
352}
353
Douglas Gregor88a35142008-12-22 05:46:06 +0000354Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
355 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
356 return MemExpr->getBase();
357
358 // FIXME: Will eventually need to cope with member pointers.
359 return 0;
360}
361
Douglas Gregor00b98c22009-11-12 15:31:47 +0000362SourceRange CXXMemberCallExpr::getSourceRange() const {
363 SourceLocation LocStart = getCallee()->getLocStart();
364 if (LocStart.isInvalid() && getNumArgs() > 0)
365 LocStart = getArg(0)->getLocStart();
366 return SourceRange(LocStart, getRParenLoc());
367}
368
369
Douglas Gregor49badde2008-10-27 19:41:14 +0000370//===----------------------------------------------------------------------===//
371// Named casts
372//===----------------------------------------------------------------------===//
373
374/// getCastName - Get the name of the C++ cast being used, e.g.,
375/// "static_cast", "dynamic_cast", "reinterpret_cast", or
376/// "const_cast". The returned pointer must not be freed.
377const char *CXXNamedCastExpr::getCastName() const {
378 switch (getStmtClass()) {
379 case CXXStaticCastExprClass: return "static_cast";
380 case CXXDynamicCastExprClass: return "dynamic_cast";
381 case CXXReinterpretCastExprClass: return "reinterpret_cast";
382 case CXXConstCastExprClass: return "const_cast";
383 default: return "<invalid cast>";
384 }
385}
Douglas Gregor506ae412009-01-16 18:33:17 +0000386
Douglas Gregor65222e82009-12-23 18:19:08 +0000387CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000388CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
389 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000390 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000391 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
392 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000393}
394
395void CXXDefaultArgExpr::DoDestroy(ASTContext &C) {
396 if (Param.getInt())
397 getExpr()->Destroy(C);
398 this->~CXXDefaultArgExpr();
399 C.Deallocate(this);
400}
401
Mike Stump1eb44332009-09-09 15:08:12 +0000402CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000403 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000404 return new (C) CXXTemporary(Destructor);
405}
406
Douglas Gregor42602bb2009-08-07 06:08:38 +0000407void CXXTemporary::Destroy(ASTContext &Ctx) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000408 this->~CXXTemporary();
Douglas Gregor42602bb2009-08-07 06:08:38 +0000409 Ctx.Deallocate(this);
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000410}
411
Mike Stump1eb44332009-09-09 15:08:12 +0000412CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000413 CXXTemporary *Temp,
414 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000415 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000416 "Expression bound to a temporary must have record type!");
417
Anders Carlssonb859f352009-05-30 20:34:37 +0000418 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000419}
420
Douglas Gregor42602bb2009-08-07 06:08:38 +0000421void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000422 Temp->Destroy(C);
423 this->~CXXBindTemporaryExpr();
424 C.Deallocate(this);
425}
426
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000427CXXBindReferenceExpr *CXXBindReferenceExpr::Create(ASTContext &C, Expr *SubExpr,
428 bool ExtendsLifetime,
429 bool RequiresTemporaryCopy) {
430 return new (C) CXXBindReferenceExpr(SubExpr,
431 ExtendsLifetime,
432 RequiresTemporaryCopy);
433}
434
435void CXXBindReferenceExpr::DoDestroy(ASTContext &C) {
436 this->~CXXBindReferenceExpr();
437 C.Deallocate(this);
438}
439
Anders Carlsson8e587a12009-05-30 20:56:46 +0000440CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000441 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000442 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000443 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000444 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000445 unsigned NumArgs,
Douglas Gregor506ae412009-01-16 18:33:17 +0000446 SourceLocation rParenLoc)
Douglas Gregor99a2e602009-12-16 01:38:02 +0000447 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, tyBeginLoc,
448 Cons, false, Args, NumArgs),
Anders Carlsson524fa132009-04-24 17:34:38 +0000449 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000450}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000451
Mike Stump1eb44332009-09-09 15:08:12 +0000452CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000453 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000454 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000455 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000456 bool ZeroInitialization,
457 bool BaseInitialization) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000458 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000459 Elidable, Args, NumArgs, ZeroInitialization,
460 BaseInitialization);
Anders Carlssone349bea2009-04-23 02:32:43 +0000461}
462
Mike Stump1eb44332009-09-09 15:08:12 +0000463CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000464 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000465 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000466 Expr **args, unsigned numargs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000467 bool ZeroInitialization,
468 bool BaseInitialization)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000469: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000470 T->isDependentType(),
471 (T->isDependentType() ||
472 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Douglas Gregor16006c92009-12-16 18:50:27 +0000473 Constructor(D), Loc(Loc), Elidable(elidable),
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000474 ZeroInitialization(ZeroInitialization),
475 BaseInitialization(BaseInitialization), Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000476{
477 if (NumArgs) {
478 Args = new (C) Stmt*[NumArgs];
479
480 for (unsigned i = 0; i != NumArgs; ++i) {
481 assert(args[i] && "NULL argument in CXXConstructExpr");
482 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000483 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000484 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000485}
486
Douglas Gregor39da0b82009-09-09 23:08:42 +0000487CXXConstructExpr::CXXConstructExpr(EmptyShell Empty, ASTContext &C,
488 unsigned numargs)
489 : Expr(CXXConstructExprClass, Empty), Args(0), NumArgs(numargs)
490{
491 if (NumArgs)
492 Args = new (C) Stmt*[NumArgs];
493}
494
Douglas Gregor42602bb2009-08-07 06:08:38 +0000495void CXXConstructExpr::DoDestroy(ASTContext &C) {
Anders Carlssone349bea2009-04-23 02:32:43 +0000496 DestroyChildren(C);
497 if (Args)
498 C.Deallocate(Args);
499 this->~CXXConstructExpr();
500 C.Deallocate(this);
501}
502
Mike Stump1eb44332009-09-09 15:08:12 +0000503CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr,
504 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000505 unsigned numtemps)
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000506: Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000507 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Anders Carlsson0ece4912009-12-15 20:51:39 +0000508 SubExpr(subexpr), Temps(0), NumTemps(numtemps) {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000509 if (NumTemps > 0) {
510 Temps = new CXXTemporary*[NumTemps];
511 for (unsigned i = 0; i < NumTemps; ++i)
512 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000513 }
514}
515
Mike Stump1eb44332009-09-09 15:08:12 +0000516CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000517 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000518 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000519 unsigned NumTemps) {
520 return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000521}
522
Douglas Gregor42602bb2009-08-07 06:08:38 +0000523void CXXExprWithTemporaries::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000524 DestroyChildren(C);
525 this->~CXXExprWithTemporaries();
526 C.Deallocate(this);
527}
528
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000529CXXExprWithTemporaries::~CXXExprWithTemporaries() {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000530 delete[] Temps;
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000531}
532
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000533// CXXBindTemporaryExpr
534Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
535 return &SubExpr;
536}
537
Mike Stump1eb44332009-09-09 15:08:12 +0000538Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000539 return &SubExpr + 1;
540}
541
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000542// CXXBindReferenceExpr
543Stmt::child_iterator CXXBindReferenceExpr::child_begin() {
544 return &SubExpr;
545}
546
547Stmt::child_iterator CXXBindReferenceExpr::child_end() {
548 return &SubExpr + 1;
549}
550
Anders Carlssone349bea2009-04-23 02:32:43 +0000551// CXXConstructExpr
552Stmt::child_iterator CXXConstructExpr::child_begin() {
553 return &Args[0];
554}
555Stmt::child_iterator CXXConstructExpr::child_end() {
556 return &Args[0]+NumArgs;
557}
558
Anders Carlsson55674ac2009-05-01 22:21:22 +0000559// CXXExprWithTemporaries
560Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
561 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000562}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000563
Mike Stump1eb44332009-09-09 15:08:12 +0000564Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000565 return &SubExpr + 1;
566}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000567
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000568CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
569 SourceLocation TyBeginLoc,
570 QualType T,
571 SourceLocation LParenLoc,
572 Expr **Args,
573 unsigned NumArgs,
574 SourceLocation RParenLoc)
575 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
576 T->isDependentType(), true),
577 TyBeginLoc(TyBeginLoc),
578 Type(T),
579 LParenLoc(LParenLoc),
580 RParenLoc(RParenLoc),
581 NumArgs(NumArgs) {
582 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
583 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
584}
585
586CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000587CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000588 SourceLocation TyBegin,
589 QualType T,
590 SourceLocation LParenLoc,
591 Expr **Args,
592 unsigned NumArgs,
593 SourceLocation RParenLoc) {
594 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
595 sizeof(Expr *) * NumArgs);
596 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
597 Args, NumArgs, RParenLoc);
598}
599
600Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
601 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
602}
603
604Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
605 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
606}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000607
John McCall865d4472009-11-19 22:55:06 +0000608CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000609 Expr *Base, QualType BaseType,
610 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000611 SourceLocation OperatorLoc,
612 NestedNameSpecifier *Qualifier,
613 SourceRange QualifierRange,
614 NamedDecl *FirstQualifierFoundInScope,
615 DeclarationName Member,
616 SourceLocation MemberLoc,
John McCalld5532b62009-11-23 01:53:49 +0000617 const TemplateArgumentListInfo *TemplateArgs)
John McCall865d4472009-11-19 22:55:06 +0000618 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000619 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
620 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000621 OperatorLoc(OperatorLoc),
622 Qualifier(Qualifier), QualifierRange(QualifierRange),
623 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Mike Stump1eb44332009-09-09 15:08:12 +0000624 Member(Member), MemberLoc(MemberLoc) {
John McCalld5532b62009-11-23 01:53:49 +0000625 if (TemplateArgs)
626 getExplicitTemplateArgumentList()->initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000627}
628
John McCall865d4472009-11-19 22:55:06 +0000629CXXDependentScopeMemberExpr *
630CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000631 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000632 SourceLocation OperatorLoc,
633 NestedNameSpecifier *Qualifier,
634 SourceRange QualifierRange,
635 NamedDecl *FirstQualifierFoundInScope,
636 DeclarationName Member,
637 SourceLocation MemberLoc,
John McCalld5532b62009-11-23 01:53:49 +0000638 const TemplateArgumentListInfo *TemplateArgs) {
639 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000640 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
641 IsArrow, OperatorLoc,
642 Qualifier, QualifierRange,
643 FirstQualifierFoundInScope,
644 Member, MemberLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
John McCalld5532b62009-11-23 01:53:49 +0000646 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
647 if (TemplateArgs)
648 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
649
650 void *Mem = C.Allocate(size, llvm::alignof<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000651 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
652 IsArrow, OperatorLoc,
653 Qualifier, QualifierRange,
654 FirstQualifierFoundInScope,
655 Member, MemberLoc, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000656}
657
John McCall865d4472009-11-19 22:55:06 +0000658Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000659 return child_iterator(&Base);
660}
661
John McCall865d4472009-11-19 22:55:06 +0000662Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000663 if (isImplicitAccess())
664 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000665 return child_iterator(&Base + 1);
666}
John McCall129e2df2009-11-30 22:42:35 +0000667
668UnresolvedMemberExpr::UnresolvedMemberExpr(QualType T, bool Dependent,
669 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000670 Expr *Base, QualType BaseType,
671 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000672 SourceLocation OperatorLoc,
673 NestedNameSpecifier *Qualifier,
674 SourceRange QualifierRange,
675 DeclarationName MemberName,
676 SourceLocation MemberLoc,
677 const TemplateArgumentListInfo *TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000678 : OverloadExpr(UnresolvedMemberExprClass, T, Dependent,
679 Qualifier, QualifierRange, MemberName, MemberLoc,
680 TemplateArgs != 0),
681 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
682 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000683 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000684 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000685}
686
687UnresolvedMemberExpr *
688UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
689 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000690 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000691 SourceLocation OperatorLoc,
692 NestedNameSpecifier *Qualifier,
693 SourceRange QualifierRange,
694 DeclarationName Member,
695 SourceLocation MemberLoc,
696 const TemplateArgumentListInfo *TemplateArgs) {
697 std::size_t size = sizeof(UnresolvedMemberExpr);
698 if (TemplateArgs)
699 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
700
701 void *Mem = C.Allocate(size, llvm::alignof<UnresolvedMemberExpr>());
702 return new (Mem) UnresolvedMemberExpr(
703 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000704 Dependent, HasUnresolvedUsing, Base, BaseType,
705 IsArrow, OperatorLoc, Qualifier, QualifierRange,
John McCall129e2df2009-11-30 22:42:35 +0000706 Member, MemberLoc, TemplateArgs);
707}
708
John McCallc373d482010-01-27 01:50:18 +0000709CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
710 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
711
712 // If there was a nested name specifier, it names the naming class.
713 // It can't be dependent: after all, we were actually able to do the
714 // lookup.
715 const RecordType *RT;
John McCall7bb12da2010-02-02 06:20:04 +0000716 if (getQualifier()) {
717 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000718 assert(T && "qualifier in member expression does not name type");
719 RT = T->getAs<RecordType>();
720 assert(RT && "qualifier in member expression does not name record");
721
722 // Otherwise the naming class must have been the base class.
723 } else {
724 QualType BaseType = getBaseType().getNonReferenceType();
725 if (isArrow()) {
726 const PointerType *PT = BaseType->getAs<PointerType>();
727 assert(PT && "base of arrow member access is not pointer");
728 BaseType = PT->getPointeeType();
729 }
730
731 RT = BaseType->getAs<RecordType>();
732 assert(RT && "base of member expression does not name record");
733 }
734
735 return cast<CXXRecordDecl>(RT->getDecl());
736}
737
John McCall129e2df2009-11-30 22:42:35 +0000738Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
739 return child_iterator(&Base);
740}
741
742Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000743 if (isImplicitAccess())
744 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +0000745 return child_iterator(&Base + 1);
746}