blob: cba0e220952e091e943706c32cf301f7f54b70c7 [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"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Child Iterators for iterating over subexpressions/substatements
22//===----------------------------------------------------------------------===//
23
Sebastian Redlc42e1182008-11-11 11:37:55 +000024// CXXTypeidExpr - has child iterators if the operand is an expression
25Stmt::child_iterator CXXTypeidExpr::child_begin() {
Sebastian Redld4575892008-12-03 23:17:54 +000026 return isTypeOperand() ? child_iterator() : &Operand.Ex;
Sebastian Redlc42e1182008-11-11 11:37:55 +000027}
28Stmt::child_iterator CXXTypeidExpr::child_end() {
Sebastian Redld4575892008-12-03 23:17:54 +000029 return isTypeOperand() ? child_iterator() : &Operand.Ex+1;
Sebastian Redlc42e1182008-11-11 11:37:55 +000030}
Ted Kremeneka758d092007-08-24 20:21:10 +000031
Ted Kremeneka758d092007-08-24 20:21:10 +000032// CXXBoolLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000033Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +000034 return child_iterator();
35}
36Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
37 return child_iterator();
38}
Chris Lattner50dd2892008-02-26 00:51:44 +000039
Sebastian Redl6e8ed162009-05-10 18:38:11 +000040// CXXNullPtrLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000041Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() {
Sebastian Redl6e8ed162009-05-10 18:38:11 +000042 return child_iterator();
43}
44Stmt::child_iterator CXXNullPtrLiteralExpr::child_end() {
45 return child_iterator();
46}
47
Douglas Gregor796da182008-11-04 14:32:21 +000048// CXXThisExpr
49Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
50Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
51
Chris Lattner50dd2892008-02-26 00:51:44 +000052// CXXThrowExpr
Ted Kremenek1060aff2008-06-17 03:11:08 +000053Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
Chris Lattner50dd2892008-02-26 00:51:44 +000054Stmt::child_iterator CXXThrowExpr::child_end() {
55 // If Op is 0, we are processing throw; which has no children.
Ted Kremenek1060aff2008-06-17 03:11:08 +000056 return Op ? &Op+1 : &Op;
Chris Lattner50dd2892008-02-26 00:51:44 +000057}
Chris Lattner04421082008-04-08 04:40:51 +000058
59// CXXDefaultArgExpr
60Stmt::child_iterator CXXDefaultArgExpr::child_begin() {
Chris Lattner8123a952008-04-10 02:22:51 +000061 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000062}
63Stmt::child_iterator CXXDefaultArgExpr::child_end() {
Chris Lattner8123a952008-04-10 02:22:51 +000064 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000065}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000066
67// CXXZeroInitValueExpr
Mike Stump1eb44332009-09-09 15:08:12 +000068Stmt::child_iterator CXXZeroInitValueExpr::child_begin() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000069 return child_iterator();
70}
71Stmt::child_iterator CXXZeroInitValueExpr::child_end() {
72 return child_iterator();
73}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +000074
75// CXXConditionDeclExpr
76Stmt::child_iterator CXXConditionDeclExpr::child_begin() {
77 return getVarDecl();
78}
79Stmt::child_iterator CXXConditionDeclExpr::child_end() {
80 return child_iterator();
81}
Douglas Gregor49badde2008-10-27 19:41:14 +000082
Sebastian Redl4c5d3202008-11-21 19:14:01 +000083// CXXNewExpr
84CXXNewExpr::CXXNewExpr(bool globalNew, FunctionDecl *operatorNew,
85 Expr **placementArgs, unsigned numPlaceArgs,
Sebastian Redlcee63fb2008-12-02 14:43:59 +000086 bool parenTypeId, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +000087 CXXConstructorDecl *constructor, bool initializer,
88 Expr **constructorArgs, unsigned numConsArgs,
89 FunctionDecl *operatorDelete, QualType ty,
90 SourceLocation startLoc, SourceLocation endLoc)
Sebastian Redl28507842009-02-26 14:39:58 +000091 : Expr(CXXNewExprClass, ty, ty->isDependentType(), ty->isDependentType()),
92 GlobalNew(globalNew), ParenTypeId(parenTypeId),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000093 Initializer(initializer), Array(arraySize), NumPlacementArgs(numPlaceArgs),
Sebastian Redl4c5d3202008-11-21 19:14:01 +000094 NumConstructorArgs(numConsArgs), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +000095 OperatorDelete(operatorDelete), Constructor(constructor),
Mike Stump1eb44332009-09-09 15:08:12 +000096 StartLoc(startLoc), EndLoc(endLoc) {
Sebastian Redlcee63fb2008-12-02 14:43:59 +000097 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000098 SubExprs = new Stmt*[TotalSize];
99 unsigned i = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000100 if (Array)
101 SubExprs[i++] = arraySize;
102 for (unsigned j = 0; j < NumPlacementArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000103 SubExprs[i++] = placementArgs[j];
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000104 for (unsigned j = 0; j < NumConstructorArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000105 SubExprs[i++] = constructorArgs[j];
106 assert(i == TotalSize);
107}
108
109Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
110Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000111 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000112}
113
114// CXXDeleteExpr
115Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
116Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
117
Douglas Gregora71d8192009-09-04 17:36:40 +0000118// CXXPseudoDestructorExpr
119Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; }
120Stmt::child_iterator CXXPseudoDestructorExpr::child_end() {
121 return &Base + 1;
122}
123
Douglas Gregor17330012009-02-04 15:01:18 +0000124// UnresolvedFunctionNameExpr
Mike Stump1eb44332009-09-09 15:08:12 +0000125Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() {
126 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000127}
Douglas Gregor17330012009-02-04 15:01:18 +0000128Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000129 return child_iterator();
130}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000131// UnaryTypeTraitExpr
132Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
133 return child_iterator();
134}
135Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
136 return child_iterator();
137}
138
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000139// UnresolvedDeclRefExpr
140StmtIterator UnresolvedDeclRefExpr::child_begin() {
141 return child_iterator();
142}
143
144StmtIterator UnresolvedDeclRefExpr::child_end() {
145 return child_iterator();
146}
147
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000148TemplateIdRefExpr::TemplateIdRefExpr(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000149 NestedNameSpecifier *Qualifier,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000150 SourceRange QualifierRange,
Mike Stump1eb44332009-09-09 15:08:12 +0000151 TemplateName Template,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000152 SourceLocation TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000153 SourceLocation LAngleLoc,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000154 const TemplateArgument *TemplateArgs,
155 unsigned NumTemplateArgs,
156 SourceLocation RAngleLoc)
157 : Expr(TemplateIdRefExprClass, T,
Mike Stump1eb44332009-09-09 15:08:12 +0000158 (Template.isDependent() ||
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000159 TemplateSpecializationType::anyDependentTemplateArguments(
160 TemplateArgs, NumTemplateArgs)),
161 (Template.isDependent() ||
162 TemplateSpecializationType::anyDependentTemplateArguments(
163 TemplateArgs, NumTemplateArgs))),
164 Qualifier(Qualifier), QualifierRange(QualifierRange), Template(Template),
165 TemplateNameLoc(TemplateNameLoc), LAngleLoc(LAngleLoc),
Mike Stump1eb44332009-09-09 15:08:12 +0000166 RAngleLoc(RAngleLoc), NumTemplateArgs(NumTemplateArgs) {
167 TemplateArgument *StoredTemplateArgs
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000168 = reinterpret_cast<TemplateArgument *> (this+1);
169 for (unsigned I = 0; I != NumTemplateArgs; ++I)
170 new (StoredTemplateArgs + I) TemplateArgument(TemplateArgs[I]);
171}
172
173TemplateIdRefExpr *
174TemplateIdRefExpr::Create(ASTContext &Context, QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000175 NestedNameSpecifier *Qualifier,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000176 SourceRange QualifierRange,
177 TemplateName Template, SourceLocation TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000178 SourceLocation LAngleLoc,
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000179 const TemplateArgument *TemplateArgs,
180 unsigned NumTemplateArgs, SourceLocation RAngleLoc) {
181 void *Mem = Context.Allocate(sizeof(TemplateIdRefExpr) +
182 sizeof(TemplateArgument) * NumTemplateArgs);
183 return new (Mem) TemplateIdRefExpr(T, Qualifier, QualifierRange, Template,
184 TemplateNameLoc, LAngleLoc, TemplateArgs,
185 NumTemplateArgs, RAngleLoc);
186}
187
Douglas Gregor42602bb2009-08-07 06:08:38 +0000188void TemplateIdRefExpr::DoDestroy(ASTContext &Context) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000189 const TemplateArgument *TemplateArgs = getTemplateArgs();
190 for (unsigned I = 0; I != NumTemplateArgs; ++I)
191 if (Expr *E = TemplateArgs[I].getAsExpr())
192 E->Destroy(Context);
Douglas Gregor42602bb2009-08-07 06:08:38 +0000193 this->~TemplateIdRefExpr();
194 Context.Deallocate(this);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000195}
196
197Stmt::child_iterator TemplateIdRefExpr::child_begin() {
198 // FIXME: Walk the expressions in the template arguments (?)
199 return Stmt::child_iterator();
200}
201
202Stmt::child_iterator TemplateIdRefExpr::child_end() {
203 // FIXME: Walk the expressions in the template arguments (?)
204 return Stmt::child_iterator();
205}
206
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000207bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000208 switch(UTT) {
209 default: assert(false && "Unknown type trait or not implemented");
210 case UTT_IsPOD: return QueriedType->isPODType();
211 case UTT_IsClass: // Fallthrough
212 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000213 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000214 bool Union = Record->getDecl()->isUnion();
215 return UTT == UTT_IsUnion ? Union : !Union;
216 }
217 return false;
218 case UTT_IsEnum: return QueriedType->isEnumeralType();
219 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000220 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000221 // Type traits are only parsed in C++, so we've got CXXRecords.
222 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
223 }
224 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000225 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000226 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000227 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
228 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000229 case UTT_IsEmpty:
230 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
231 return !Record->getDecl()->isUnion()
232 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
233 }
234 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000235 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000236 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
237 // If __is_pod (type) is true then the trait is true, else if type is
238 // a cv class or union type (or array thereof) with a trivial default
239 // constructor ([class.ctor]) then the trait is true, else it is false.
240 if (QueriedType->isPODType())
241 return true;
242 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000243 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000244 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000245 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000246 case UTT_HasTrivialCopy:
247 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
248 // If __is_pod (type) is true or type is a reference type then
249 // the trait is true, else if type is a cv class or union type
250 // with a trivial copy constructor ([class.copy]) then the trait
251 // is true, else it is false.
252 if (QueriedType->isPODType() || QueriedType->isReferenceType())
253 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000254 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000255 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
256 return false;
257 case UTT_HasTrivialAssign:
258 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
259 // If type is const qualified or is a reference type then the
260 // trait is false. Otherwise if __is_pod (type) is true then the
261 // trait is true, else if type is a cv class or union type with
262 // a trivial copy assignment ([class.copy]) then the trait is
263 // true, else it is false.
264 // Note: the const and reference restrictions are interesting,
265 // given that const and reference members don't prevent a class
266 // from having a trivial copy assignment operator (but do cause
267 // errors if the copy assignment operator is actually used, q.v.
268 // [class.copy]p12).
269
270 if (C.getBaseElementType(QueriedType).isConstQualified())
271 return false;
272 if (QueriedType->isPODType())
273 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000274 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000275 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
276 return false;
277 case UTT_HasTrivialDestructor:
278 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
279 // If __is_pod (type) is true or type is a reference type
280 // then the trait is true, else if type is a cv class or union
281 // type (or array thereof) with a trivial destructor
282 // ([class.dtor]) then the trait is true, else it is
283 // false.
284 if (QueriedType->isPODType() || QueriedType->isReferenceType())
285 return true;
286 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000287 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000288 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
289 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000290 }
291}
292
Douglas Gregorb4609802008-11-14 16:09:21 +0000293SourceRange CXXOperatorCallExpr::getSourceRange() const {
294 OverloadedOperatorKind Kind = getOperator();
295 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
296 if (getNumArgs() == 1)
297 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000298 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000299 getArg(0)->getSourceRange().getEnd());
300 else
301 // Postfix operator
302 return SourceRange(getArg(0)->getSourceRange().getEnd(),
303 getOperatorLoc());
304 } else if (Kind == OO_Call) {
305 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
306 } else if (Kind == OO_Subscript) {
307 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
308 } else if (getNumArgs() == 1) {
309 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
310 } else if (getNumArgs() == 2) {
311 return SourceRange(getArg(0)->getSourceRange().getBegin(),
312 getArg(1)->getSourceRange().getEnd());
313 } else {
314 return SourceRange();
315 }
316}
317
Douglas Gregor88a35142008-12-22 05:46:06 +0000318Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
319 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
320 return MemExpr->getBase();
321
322 // FIXME: Will eventually need to cope with member pointers.
323 return 0;
324}
325
Douglas Gregor49badde2008-10-27 19:41:14 +0000326//===----------------------------------------------------------------------===//
327// Named casts
328//===----------------------------------------------------------------------===//
329
330/// getCastName - Get the name of the C++ cast being used, e.g.,
331/// "static_cast", "dynamic_cast", "reinterpret_cast", or
332/// "const_cast". The returned pointer must not be freed.
333const char *CXXNamedCastExpr::getCastName() const {
334 switch (getStmtClass()) {
335 case CXXStaticCastExprClass: return "static_cast";
336 case CXXDynamicCastExprClass: return "dynamic_cast";
337 case CXXReinterpretCastExprClass: return "reinterpret_cast";
338 case CXXConstCastExprClass: return "const_cast";
339 default: return "<invalid cast>";
340 }
341}
Douglas Gregor506ae412009-01-16 18:33:17 +0000342
Mike Stump1eb44332009-09-09 15:08:12 +0000343CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000344 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000345 return new (C) CXXTemporary(Destructor);
346}
347
Douglas Gregor42602bb2009-08-07 06:08:38 +0000348void CXXTemporary::Destroy(ASTContext &Ctx) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000349 this->~CXXTemporary();
Douglas Gregor42602bb2009-08-07 06:08:38 +0000350 Ctx.Deallocate(this);
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000351}
352
Mike Stump1eb44332009-09-09 15:08:12 +0000353CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000354 CXXTemporary *Temp,
355 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000356 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000357 "Expression bound to a temporary must have record type!");
358
Anders Carlssonb859f352009-05-30 20:34:37 +0000359 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000360}
361
Douglas Gregor42602bb2009-08-07 06:08:38 +0000362void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000363 Temp->Destroy(C);
364 this->~CXXBindTemporaryExpr();
365 C.Deallocate(this);
366}
367
Anders Carlsson8e587a12009-05-30 20:56:46 +0000368CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000369 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000370 QualType writtenTy,
Mike Stump1eb44332009-09-09 15:08:12 +0000371 SourceLocation tyBeginLoc,
Douglas Gregor506ae412009-01-16 18:33:17 +0000372 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000373 unsigned NumArgs,
Douglas Gregor506ae412009-01-16 18:33:17 +0000374 SourceLocation rParenLoc)
Mike Stump1eb44332009-09-09 15:08:12 +0000375 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, Cons,
376 false, Args, NumArgs),
Anders Carlsson524fa132009-04-24 17:34:38 +0000377 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000378}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000379
Mike Stump1eb44332009-09-09 15:08:12 +0000380CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000381 CXXConstructorDecl *D, bool Elidable,
Anders Carlssone349bea2009-04-23 02:32:43 +0000382 Expr **Args, unsigned NumArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000383 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, D, Elidable,
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000384 Args, NumArgs);
Anders Carlssone349bea2009-04-23 02:32:43 +0000385}
386
Mike Stump1eb44332009-09-09 15:08:12 +0000387CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000388 CXXConstructorDecl *D, bool elidable,
Mike Stump1eb44332009-09-09 15:08:12 +0000389 Expr **args, unsigned numargs)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000390: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000391 T->isDependentType(),
392 (T->isDependentType() ||
393 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Anders Carlsson8e587a12009-05-30 20:56:46 +0000394 Constructor(D), Elidable(elidable), Args(0), NumArgs(numargs) {
Douglas Gregor39da0b82009-09-09 23:08:42 +0000395 if (NumArgs) {
396 Args = new (C) Stmt*[NumArgs];
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Douglas Gregor39da0b82009-09-09 23:08:42 +0000398 for (unsigned i = 0; i != NumArgs; ++i) {
399 assert(args[i] && "NULL argument in CXXConstructExpr");
Anders Carlssone349bea2009-04-23 02:32:43 +0000400 Args[i] = args[i];
Douglas Gregor39da0b82009-09-09 23:08:42 +0000401 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000402 }
403}
404
Douglas Gregor39da0b82009-09-09 23:08:42 +0000405CXXConstructExpr::CXXConstructExpr(EmptyShell Empty, ASTContext &C,
406 unsigned numargs)
407 : Expr(CXXConstructExprClass, Empty), Args(0), NumArgs(numargs)
408{
409 if (NumArgs)
410 Args = new (C) Stmt*[NumArgs];
411}
412
Douglas Gregor42602bb2009-08-07 06:08:38 +0000413void CXXConstructExpr::DoDestroy(ASTContext &C) {
Anders Carlssone349bea2009-04-23 02:32:43 +0000414 DestroyChildren(C);
415 if (Args)
416 C.Deallocate(Args);
417 this->~CXXConstructExpr();
418 C.Deallocate(this);
419}
420
Mike Stump1eb44332009-09-09 15:08:12 +0000421CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr,
422 CXXTemporary **temps,
Anders Carlsson99ba36d2009-06-05 15:38:08 +0000423 unsigned numtemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000424 bool shoulddestroytemps)
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000425: Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000426 subexpr->isTypeDependent(), subexpr->isValueDependent()),
427 SubExpr(subexpr), Temps(0), NumTemps(numtemps),
Anders Carlssonf54741e2009-06-16 03:37:31 +0000428 ShouldDestroyTemps(shoulddestroytemps) {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000429 if (NumTemps > 0) {
430 Temps = new CXXTemporary*[NumTemps];
431 for (unsigned i = 0; i < NumTemps; ++i)
432 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000433 }
434}
435
Mike Stump1eb44332009-09-09 15:08:12 +0000436CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000437 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000438 CXXTemporary **Temps,
Anders Carlsson99ba36d2009-06-05 15:38:08 +0000439 unsigned NumTemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000440 bool ShouldDestroyTemps){
Mike Stump1eb44332009-09-09 15:08:12 +0000441 return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000442 ShouldDestroyTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000443}
444
Douglas Gregor42602bb2009-08-07 06:08:38 +0000445void CXXExprWithTemporaries::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000446 DestroyChildren(C);
447 this->~CXXExprWithTemporaries();
448 C.Deallocate(this);
449}
450
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000451CXXExprWithTemporaries::~CXXExprWithTemporaries() {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000452 delete[] Temps;
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000453}
454
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000455// CXXBindTemporaryExpr
456Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
457 return &SubExpr;
458}
459
Mike Stump1eb44332009-09-09 15:08:12 +0000460Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000461 return &SubExpr + 1;
462}
463
Anders Carlssone349bea2009-04-23 02:32:43 +0000464// CXXConstructExpr
465Stmt::child_iterator CXXConstructExpr::child_begin() {
466 return &Args[0];
467}
468Stmt::child_iterator CXXConstructExpr::child_end() {
469 return &Args[0]+NumArgs;
470}
471
Anders Carlsson55674ac2009-05-01 22:21:22 +0000472// CXXExprWithTemporaries
473Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
474 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000475}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000476
Mike Stump1eb44332009-09-09 15:08:12 +0000477Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000478 return &SubExpr + 1;
479}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000480
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000481CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
482 SourceLocation TyBeginLoc,
483 QualType T,
484 SourceLocation LParenLoc,
485 Expr **Args,
486 unsigned NumArgs,
487 SourceLocation RParenLoc)
488 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
489 T->isDependentType(), true),
490 TyBeginLoc(TyBeginLoc),
491 Type(T),
492 LParenLoc(LParenLoc),
493 RParenLoc(RParenLoc),
494 NumArgs(NumArgs) {
495 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
496 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
497}
498
499CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000500CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000501 SourceLocation TyBegin,
502 QualType T,
503 SourceLocation LParenLoc,
504 Expr **Args,
505 unsigned NumArgs,
506 SourceLocation RParenLoc) {
507 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
508 sizeof(Expr *) * NumArgs);
509 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
510 Args, NumArgs, RParenLoc);
511}
512
513Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
514 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
515}
516
517Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
518 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
519}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000520
Mike Stump1eb44332009-09-09 15:08:12 +0000521CXXUnresolvedMemberExpr::CXXUnresolvedMemberExpr(ASTContext &C,
522 Expr *Base, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000523 SourceLocation OperatorLoc,
524 NestedNameSpecifier *Qualifier,
525 SourceRange QualifierRange,
526 NamedDecl *FirstQualifierFoundInScope,
527 DeclarationName Member,
528 SourceLocation MemberLoc,
529 bool HasExplicitTemplateArgs,
530 SourceLocation LAngleLoc,
531 const TemplateArgument *TemplateArgs,
532 unsigned NumTemplateArgs,
533 SourceLocation RAngleLoc)
534 : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true),
Mike Stump1eb44332009-09-09 15:08:12 +0000535 Base(Base), IsArrow(IsArrow),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000536 HasExplicitTemplateArgumentList(HasExplicitTemplateArgs),
537 OperatorLoc(OperatorLoc),
538 Qualifier(Qualifier), QualifierRange(QualifierRange),
539 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Mike Stump1eb44332009-09-09 15:08:12 +0000540 Member(Member), MemberLoc(MemberLoc) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000541 if (HasExplicitTemplateArgumentList) {
Mike Stump1eb44332009-09-09 15:08:12 +0000542 ExplicitTemplateArgumentList *ETemplateArgs
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000543 = getExplicitTemplateArgumentList();
544 ETemplateArgs->LAngleLoc = LAngleLoc;
545 ETemplateArgs->RAngleLoc = RAngleLoc;
546 ETemplateArgs->NumTemplateArgs = NumTemplateArgs;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000548 TemplateArgument *SavedTemplateArgs = ETemplateArgs->getTemplateArgs();
549 for (unsigned I = 0; I < NumTemplateArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000550 new (SavedTemplateArgs + I) TemplateArgument(TemplateArgs[I]);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000551 }
552}
553
554CXXUnresolvedMemberExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000555CXXUnresolvedMemberExpr::Create(ASTContext &C,
556 Expr *Base, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000557 SourceLocation OperatorLoc,
558 NestedNameSpecifier *Qualifier,
559 SourceRange QualifierRange,
560 NamedDecl *FirstQualifierFoundInScope,
561 DeclarationName Member,
562 SourceLocation MemberLoc,
563 bool HasExplicitTemplateArgs,
564 SourceLocation LAngleLoc,
565 const TemplateArgument *TemplateArgs,
566 unsigned NumTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +0000567 SourceLocation RAngleLoc) {
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000568 if (!HasExplicitTemplateArgs)
569 return new (C) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc,
570 Qualifier, QualifierRange,
571 FirstQualifierFoundInScope,
572 Member, MemberLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000574 void *Mem = C.Allocate(sizeof(CXXUnresolvedMemberExpr) +
Mike Stump1eb44332009-09-09 15:08:12 +0000575 sizeof(ExplicitTemplateArgumentList) +
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000576 sizeof(TemplateArgument) * NumTemplateArgs,
577 llvm::alignof<CXXUnresolvedMemberExpr>());
578 return new (Mem) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc,
579 Qualifier, QualifierRange,
580 FirstQualifierFoundInScope,
581 Member,
582 MemberLoc,
583 HasExplicitTemplateArgs,
584 LAngleLoc,
585 TemplateArgs,
586 NumTemplateArgs,
587 RAngleLoc);
588}
589
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000590Stmt::child_iterator CXXUnresolvedMemberExpr::child_begin() {
591 return child_iterator(&Base);
592}
593
594Stmt::child_iterator CXXUnresolvedMemberExpr::child_end() {
595 return child_iterator(&Base + 1);
596}