blob: 7c36caad5851a873a6b6be940a53f7f064eb3d6e [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
Ted Kremenek9ac59282007-10-18 23:28:49 +000033Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
34 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
41Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() {
42 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
68Stmt::child_iterator CXXZeroInitValueExpr::child_begin() {
69 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),
Sebastian Redl4c5d3202008-11-21 19:14:01 +000096 StartLoc(startLoc), EndLoc(endLoc)
97{
Sebastian Redlcee63fb2008-12-02 14:43:59 +000098 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
Sebastian Redl4c5d3202008-11-21 19:14:01 +000099 SubExprs = new Stmt*[TotalSize];
100 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];
107 assert(i == TotalSize);
108}
109
110Stmt::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 Gregor17330012009-02-04 15:01:18 +0000125// UnresolvedFunctionNameExpr
126Stmt::child_iterator UnresolvedFunctionNameExpr::child_begin() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000127 return child_iterator();
128}
Douglas Gregor17330012009-02-04 15:01:18 +0000129Stmt::child_iterator UnresolvedFunctionNameExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000130 return child_iterator();
131}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000132// UnaryTypeTraitExpr
133Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
134 return child_iterator();
135}
136Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
137 return child_iterator();
138}
139
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000140// UnresolvedDeclRefExpr
141StmtIterator UnresolvedDeclRefExpr::child_begin() {
142 return child_iterator();
143}
144
145StmtIterator UnresolvedDeclRefExpr::child_end() {
146 return child_iterator();
147}
148
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000149TemplateIdRefExpr::TemplateIdRefExpr(QualType T,
150 NestedNameSpecifier *Qualifier,
151 SourceRange QualifierRange,
152 TemplateName Template,
153 SourceLocation TemplateNameLoc,
154 SourceLocation LAngleLoc,
155 const TemplateArgument *TemplateArgs,
156 unsigned NumTemplateArgs,
157 SourceLocation RAngleLoc)
158 : Expr(TemplateIdRefExprClass, T,
159 (Template.isDependent() ||
160 TemplateSpecializationType::anyDependentTemplateArguments(
161 TemplateArgs, NumTemplateArgs)),
162 (Template.isDependent() ||
163 TemplateSpecializationType::anyDependentTemplateArguments(
164 TemplateArgs, NumTemplateArgs))),
165 Qualifier(Qualifier), QualifierRange(QualifierRange), Template(Template),
166 TemplateNameLoc(TemplateNameLoc), LAngleLoc(LAngleLoc),
167 RAngleLoc(RAngleLoc), NumTemplateArgs(NumTemplateArgs)
168
169{
170 TemplateArgument *StoredTemplateArgs
171 = reinterpret_cast<TemplateArgument *> (this+1);
172 for (unsigned I = 0; I != NumTemplateArgs; ++I)
173 new (StoredTemplateArgs + I) TemplateArgument(TemplateArgs[I]);
174}
175
176TemplateIdRefExpr *
177TemplateIdRefExpr::Create(ASTContext &Context, QualType T,
178 NestedNameSpecifier *Qualifier,
179 SourceRange QualifierRange,
180 TemplateName Template, SourceLocation TemplateNameLoc,
181 SourceLocation LAngleLoc,
182 const TemplateArgument *TemplateArgs,
183 unsigned NumTemplateArgs, SourceLocation RAngleLoc) {
184 void *Mem = Context.Allocate(sizeof(TemplateIdRefExpr) +
185 sizeof(TemplateArgument) * NumTemplateArgs);
186 return new (Mem) TemplateIdRefExpr(T, Qualifier, QualifierRange, Template,
187 TemplateNameLoc, LAngleLoc, TemplateArgs,
188 NumTemplateArgs, RAngleLoc);
189}
190
Douglas Gregor42602bb2009-08-07 06:08:38 +0000191void TemplateIdRefExpr::DoDestroy(ASTContext &Context) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000192 const TemplateArgument *TemplateArgs = getTemplateArgs();
193 for (unsigned I = 0; I != NumTemplateArgs; ++I)
194 if (Expr *E = TemplateArgs[I].getAsExpr())
195 E->Destroy(Context);
Douglas Gregor42602bb2009-08-07 06:08:38 +0000196 this->~TemplateIdRefExpr();
197 Context.Deallocate(this);
Douglas Gregoredce4dd2009-06-30 22:34:41 +0000198}
199
200Stmt::child_iterator TemplateIdRefExpr::child_begin() {
201 // FIXME: Walk the expressions in the template arguments (?)
202 return Stmt::child_iterator();
203}
204
205Stmt::child_iterator TemplateIdRefExpr::child_end() {
206 // FIXME: Walk the expressions in the template arguments (?)
207 return Stmt::child_iterator();
208}
209
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000210bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000211 switch(UTT) {
212 default: assert(false && "Unknown type trait or not implemented");
213 case UTT_IsPOD: return QueriedType->isPODType();
214 case UTT_IsClass: // Fallthrough
215 case UTT_IsUnion:
Ted Kremenek6217b802009-07-29 21:53:49 +0000216 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000217 bool Union = Record->getDecl()->isUnion();
218 return UTT == UTT_IsUnion ? Union : !Union;
219 }
220 return false;
221 case UTT_IsEnum: return QueriedType->isEnumeralType();
222 case UTT_IsPolymorphic:
Ted Kremenek6217b802009-07-29 21:53:49 +0000223 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
Sebastian Redl64b45f72009-01-05 20:52:13 +0000224 // Type traits are only parsed in C++, so we've got CXXRecords.
225 return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic();
226 }
227 return false;
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000228 case UTT_IsAbstract:
Ted Kremenek6217b802009-07-29 21:53:49 +0000229 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Anders Carlsson67e4dd22009-03-22 01:52:17 +0000230 return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
231 return false;
Eli Friedman1d954f62009-08-15 21:55:26 +0000232 case UTT_IsEmpty:
233 if (const RecordType *Record = QueriedType->getAs<RecordType>()) {
234 return !Record->getDecl()->isUnion()
235 && cast<CXXRecordDecl>(Record->getDecl())->isEmpty();
236 }
237 return false;
Anders Carlsson347ba892009-04-16 00:08:20 +0000238 case UTT_HasTrivialConstructor:
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000239 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
240 // If __is_pod (type) is true then the trait is true, else if type is
241 // a cv class or union type (or array thereof) with a trivial default
242 // constructor ([class.ctor]) then the trait is true, else it is false.
243 if (QueriedType->isPODType())
244 return true;
245 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000246 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson347ba892009-04-16 00:08:20 +0000247 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
Anders Carlsson072abef2009-04-17 02:34:54 +0000248 return false;
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000249 case UTT_HasTrivialCopy:
250 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
251 // If __is_pod (type) is true or type is a reference type then
252 // the trait is true, else if type is a cv class or union type
253 // with a trivial copy constructor ([class.copy]) then the trait
254 // is true, else it is false.
255 if (QueriedType->isPODType() || QueriedType->isReferenceType())
256 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000257 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000258 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor();
259 return false;
260 case UTT_HasTrivialAssign:
261 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
262 // If type is const qualified or is a reference type then the
263 // trait is false. Otherwise if __is_pod (type) is true then the
264 // trait is true, else if type is a cv class or union type with
265 // a trivial copy assignment ([class.copy]) then the trait is
266 // true, else it is false.
267 // Note: the const and reference restrictions are interesting,
268 // given that const and reference members don't prevent a class
269 // from having a trivial copy assignment operator (but do cause
270 // errors if the copy assignment operator is actually used, q.v.
271 // [class.copy]p12).
272
273 if (C.getBaseElementType(QueriedType).isConstQualified())
274 return false;
275 if (QueriedType->isPODType())
276 return true;
Ted Kremenek6217b802009-07-29 21:53:49 +0000277 if (const RecordType *RT = QueriedType->getAs<RecordType>())
Douglas Gregor5e03f9e2009-07-23 23:49:00 +0000278 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment();
279 return false;
280 case UTT_HasTrivialDestructor:
281 // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html:
282 // If __is_pod (type) is true or type is a reference type
283 // then the trait is true, else if type is a cv class or union
284 // type (or array thereof) with a trivial destructor
285 // ([class.dtor]) then the trait is true, else it is
286 // false.
287 if (QueriedType->isPODType() || QueriedType->isReferenceType())
288 return true;
289 if (const RecordType *RT =
Ted Kremenek6217b802009-07-29 21:53:49 +0000290 C.getBaseElementType(QueriedType)->getAs<RecordType>())
Anders Carlsson072abef2009-04-17 02:34:54 +0000291 return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor();
292 return false;
Sebastian Redl64b45f72009-01-05 20:52:13 +0000293 }
294}
295
Douglas Gregorb4609802008-11-14 16:09:21 +0000296SourceRange CXXOperatorCallExpr::getSourceRange() const {
297 OverloadedOperatorKind Kind = getOperator();
298 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
299 if (getNumArgs() == 1)
300 // Prefix operator
301 return SourceRange(getOperatorLoc(),
302 getArg(0)->getSourceRange().getEnd());
303 else
304 // Postfix operator
305 return SourceRange(getArg(0)->getSourceRange().getEnd(),
306 getOperatorLoc());
307 } else if (Kind == OO_Call) {
308 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
309 } else if (Kind == OO_Subscript) {
310 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
311 } else if (getNumArgs() == 1) {
312 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
313 } else if (getNumArgs() == 2) {
314 return SourceRange(getArg(0)->getSourceRange().getBegin(),
315 getArg(1)->getSourceRange().getEnd());
316 } else {
317 return SourceRange();
318 }
319}
320
Douglas Gregor88a35142008-12-22 05:46:06 +0000321Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
322 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
323 return MemExpr->getBase();
324
325 // FIXME: Will eventually need to cope with member pointers.
326 return 0;
327}
328
Douglas Gregor49badde2008-10-27 19:41:14 +0000329//===----------------------------------------------------------------------===//
330// Named casts
331//===----------------------------------------------------------------------===//
332
333/// getCastName - Get the name of the C++ cast being used, e.g.,
334/// "static_cast", "dynamic_cast", "reinterpret_cast", or
335/// "const_cast". The returned pointer must not be freed.
336const char *CXXNamedCastExpr::getCastName() const {
337 switch (getStmtClass()) {
338 case CXXStaticCastExprClass: return "static_cast";
339 case CXXDynamicCastExprClass: return "dynamic_cast";
340 case CXXReinterpretCastExprClass: return "reinterpret_cast";
341 case CXXConstCastExprClass: return "const_cast";
342 default: return "<invalid cast>";
343 }
344}
Douglas Gregor506ae412009-01-16 18:33:17 +0000345
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000346CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000347 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000348 return new (C) CXXTemporary(Destructor);
349}
350
Douglas Gregor42602bb2009-08-07 06:08:38 +0000351void CXXTemporary::Destroy(ASTContext &Ctx) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000352 this->~CXXTemporary();
Douglas Gregor42602bb2009-08-07 06:08:38 +0000353 Ctx.Deallocate(this);
Anders Carlssonc1ce4772009-05-30 19:54:15 +0000354}
355
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000356CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
357 CXXTemporary *Temp,
358 Expr* SubExpr) {
359 assert(SubExpr->getType()->isRecordType() &&
360 "Expression bound to a temporary must have record type!");
361
Anders Carlssonb859f352009-05-30 20:34:37 +0000362 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000363}
364
Douglas Gregor42602bb2009-08-07 06:08:38 +0000365void CXXBindTemporaryExpr::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000366 Temp->Destroy(C);
367 this->~CXXBindTemporaryExpr();
368 C.Deallocate(this);
369}
370
Anders Carlsson8e587a12009-05-30 20:56:46 +0000371CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000372 CXXConstructorDecl *Cons,
Douglas Gregor506ae412009-01-16 18:33:17 +0000373 QualType writtenTy,
374 SourceLocation tyBeginLoc,
375 Expr **Args,
376 unsigned NumArgs,
377 SourceLocation rParenLoc)
Anders Carlsson8e587a12009-05-30 20:56:46 +0000378 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, writtenTy, Cons,
Anders Carlsson524fa132009-04-24 17:34:38 +0000379 false, Args, NumArgs),
380 TyBeginLoc(tyBeginLoc), RParenLoc(rParenLoc) {
Douglas Gregor506ae412009-01-16 18:33:17 +0000381}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000382
Anders Carlsson8e587a12009-05-30 20:56:46 +0000383CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
384 CXXConstructorDecl *D, bool Elidable,
Anders Carlssone349bea2009-04-23 02:32:43 +0000385 Expr **Args, unsigned NumArgs) {
Anders Carlsson8e587a12009-05-30 20:56:46 +0000386 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, D, Elidable,
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000387 Args, NumArgs);
Anders Carlssone349bea2009-04-23 02:32:43 +0000388}
389
Anders Carlsson8e587a12009-05-30 20:56:46 +0000390CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
391 CXXConstructorDecl *D, bool elidable,
Anders Carlssone349bea2009-04-23 02:32:43 +0000392 Expr **args, unsigned numargs)
Anders Carlssonbd6734e2009-04-24 05:04:04 +0000393: Expr(SC, T,
Anders Carlssone349bea2009-04-23 02:32:43 +0000394 T->isDependentType(),
395 (T->isDependentType() ||
396 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Anders Carlsson8e587a12009-05-30 20:56:46 +0000397 Constructor(D), Elidable(elidable), Args(0), NumArgs(numargs) {
Fariborz Jahanian2eeed7b2009-08-05 00:26:10 +0000398 // leave room for default arguments;
Anders Carlssonbcb11d02009-09-08 01:23:37 +0000399 const FunctionProtoType *FTy =
400 cast<FunctionDecl>(D)->getType()->getAsFunctionProtoType();
401
402 unsigned NumArgsInProto = FTy->getNumArgs();
403 unsigned NumArgsToAllocate = FTy->isVariadic() ? NumArgs : NumArgsInProto;
404 if (NumArgsToAllocate) {
405 Args = new (C) Stmt*[NumArgsToAllocate];
406
407 for (unsigned i = 0; i != NumArgs; ++i)
Anders Carlssone349bea2009-04-23 02:32:43 +0000408 Args[i] = args[i];
Anders Carlssonbcb11d02009-09-08 01:23:37 +0000409
410 // Set default arguments to 0.
411 for (unsigned i = NumArgs; i != NumArgsToAllocate; ++i)
Anders Carlsson21e1c4e2009-09-06 16:54:02 +0000412 Args[i] = 0;
Anders Carlssonbcb11d02009-09-08 01:23:37 +0000413
414 NumArgs = NumArgsToAllocate;
Anders Carlssone349bea2009-04-23 02:32:43 +0000415 }
416}
417
Douglas Gregor42602bb2009-08-07 06:08:38 +0000418void CXXConstructExpr::DoDestroy(ASTContext &C) {
Anders Carlssone349bea2009-04-23 02:32:43 +0000419 DestroyChildren(C);
420 if (Args)
421 C.Deallocate(Args);
422 this->~CXXConstructExpr();
423 C.Deallocate(this);
424}
425
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000426CXXExprWithTemporaries::CXXExprWithTemporaries(Expr *subexpr,
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000427 CXXTemporary **temps,
Anders Carlsson99ba36d2009-06-05 15:38:08 +0000428 unsigned numtemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000429 bool shoulddestroytemps)
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000430: Expr(CXXExprWithTemporariesClass, subexpr->getType(),
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000431 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Anders Carlssonf54741e2009-06-16 03:37:31 +0000432 SubExpr(subexpr), Temps(0), NumTemps(numtemps),
433 ShouldDestroyTemps(shoulddestroytemps) {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000434 if (NumTemps > 0) {
435 Temps = new CXXTemporary*[NumTemps];
436 for (unsigned i = 0; i < NumTemps; ++i)
437 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000438 }
439}
440
Anders Carlsson88eaf072009-05-30 22:38:53 +0000441CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
442 Expr *SubExpr,
443 CXXTemporary **Temps,
Anders Carlsson99ba36d2009-06-05 15:38:08 +0000444 unsigned NumTemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000445 bool ShouldDestroyTemps){
Anders Carlsson99ba36d2009-06-05 15:38:08 +0000446 return new (C) CXXExprWithTemporaries(SubExpr, Temps, NumTemps,
Anders Carlssonf54741e2009-06-16 03:37:31 +0000447 ShouldDestroyTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000448}
449
Douglas Gregor42602bb2009-08-07 06:08:38 +0000450void CXXExprWithTemporaries::DoDestroy(ASTContext &C) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000451 DestroyChildren(C);
452 this->~CXXExprWithTemporaries();
453 C.Deallocate(this);
454}
455
Anders Carlsson2d44e8a2009-05-01 22:18:43 +0000456CXXExprWithTemporaries::~CXXExprWithTemporaries() {
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000457 delete[] Temps;
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000458}
459
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000460// CXXBindTemporaryExpr
461Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
462 return &SubExpr;
463}
464
465Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
466 return &SubExpr + 1;
467}
468
Anders Carlssone349bea2009-04-23 02:32:43 +0000469// CXXConstructExpr
470Stmt::child_iterator CXXConstructExpr::child_begin() {
471 return &Args[0];
472}
473Stmt::child_iterator CXXConstructExpr::child_end() {
474 return &Args[0]+NumArgs;
475}
476
Anders Carlsson55674ac2009-05-01 22:21:22 +0000477// CXXExprWithTemporaries
478Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
479 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000480}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000481
Anders Carlsson55674ac2009-05-01 22:21:22 +0000482Stmt::child_iterator CXXExprWithTemporaries::child_end() {
483 return &SubExpr + 1;
484}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000485
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000486CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(
487 SourceLocation TyBeginLoc,
488 QualType T,
489 SourceLocation LParenLoc,
490 Expr **Args,
491 unsigned NumArgs,
492 SourceLocation RParenLoc)
493 : Expr(CXXUnresolvedConstructExprClass, T.getNonReferenceType(),
494 T->isDependentType(), true),
495 TyBeginLoc(TyBeginLoc),
496 Type(T),
497 LParenLoc(LParenLoc),
498 RParenLoc(RParenLoc),
499 NumArgs(NumArgs) {
500 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
501 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
502}
503
504CXXUnresolvedConstructExpr *
505CXXUnresolvedConstructExpr::Create(ASTContext &C,
506 SourceLocation TyBegin,
507 QualType T,
508 SourceLocation LParenLoc,
509 Expr **Args,
510 unsigned NumArgs,
511 SourceLocation RParenLoc) {
512 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
513 sizeof(Expr *) * NumArgs);
514 return new (Mem) CXXUnresolvedConstructExpr(TyBegin, T, LParenLoc,
515 Args, NumArgs, RParenLoc);
516}
517
518Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
519 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
520}
521
522Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
523 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
524}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000525
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000526CXXUnresolvedMemberExpr::CXXUnresolvedMemberExpr(ASTContext &C,
527 Expr *Base, bool IsArrow,
528 SourceLocation OperatorLoc,
529 NestedNameSpecifier *Qualifier,
530 SourceRange QualifierRange,
531 NamedDecl *FirstQualifierFoundInScope,
532 DeclarationName Member,
533 SourceLocation MemberLoc,
534 bool HasExplicitTemplateArgs,
535 SourceLocation LAngleLoc,
536 const TemplateArgument *TemplateArgs,
537 unsigned NumTemplateArgs,
538 SourceLocation RAngleLoc)
539 : Expr(CXXUnresolvedMemberExprClass, C.DependentTy, true, true),
540 Base(Base), IsArrow(IsArrow),
541 HasExplicitTemplateArgumentList(HasExplicitTemplateArgs),
542 OperatorLoc(OperatorLoc),
543 Qualifier(Qualifier), QualifierRange(QualifierRange),
544 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
545 Member(Member), MemberLoc(MemberLoc)
546{
547 if (HasExplicitTemplateArgumentList) {
548 ExplicitTemplateArgumentList *ETemplateArgs
549 = getExplicitTemplateArgumentList();
550 ETemplateArgs->LAngleLoc = LAngleLoc;
551 ETemplateArgs->RAngleLoc = RAngleLoc;
552 ETemplateArgs->NumTemplateArgs = NumTemplateArgs;
553
554 TemplateArgument *SavedTemplateArgs = ETemplateArgs->getTemplateArgs();
555 for (unsigned I = 0; I < NumTemplateArgs; ++I)
556 new (SavedTemplateArgs + I) TemplateArgument(TemplateArgs[I]);
557 }
558}
559
560CXXUnresolvedMemberExpr *
561CXXUnresolvedMemberExpr::Create(ASTContext &C,
562 Expr *Base, bool IsArrow,
563 SourceLocation OperatorLoc,
564 NestedNameSpecifier *Qualifier,
565 SourceRange QualifierRange,
566 NamedDecl *FirstQualifierFoundInScope,
567 DeclarationName Member,
568 SourceLocation MemberLoc,
569 bool HasExplicitTemplateArgs,
570 SourceLocation LAngleLoc,
571 const TemplateArgument *TemplateArgs,
572 unsigned NumTemplateArgs,
573 SourceLocation RAngleLoc)
574{
575 if (!HasExplicitTemplateArgs)
576 return new (C) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc,
577 Qualifier, QualifierRange,
578 FirstQualifierFoundInScope,
579 Member, MemberLoc);
580
581 void *Mem = C.Allocate(sizeof(CXXUnresolvedMemberExpr) +
582 sizeof(ExplicitTemplateArgumentList) +
583 sizeof(TemplateArgument) * NumTemplateArgs,
584 llvm::alignof<CXXUnresolvedMemberExpr>());
585 return new (Mem) CXXUnresolvedMemberExpr(C, Base, IsArrow, OperatorLoc,
586 Qualifier, QualifierRange,
587 FirstQualifierFoundInScope,
588 Member,
589 MemberLoc,
590 HasExplicitTemplateArgs,
591 LAngleLoc,
592 TemplateArgs,
593 NumTemplateArgs,
594 RAngleLoc);
595}
596
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000597Stmt::child_iterator CXXUnresolvedMemberExpr::child_begin() {
598 return child_iterator(&Base);
599}
600
601Stmt::child_iterator CXXUnresolvedMemberExpr::child_end() {
602 return child_iterator(&Base + 1);
603}