blob: 6260330f9ff31e7a02f41b532f62d5e7584db594 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000021
Ted Kremeneka758d092007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000026QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
30}
31
Sebastian Redlc42e1182008-11-11 11:37:55 +000032// CXXTypeidExpr - has child iterators if the operand is an expression
33Stmt::child_iterator CXXTypeidExpr::child_begin() {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000034 return isTypeOperand() ? child_iterator()
35 : reinterpret_cast<Stmt **>(&Operand);
Sebastian Redlc42e1182008-11-11 11:37:55 +000036}
37Stmt::child_iterator CXXTypeidExpr::child_end() {
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000038 return isTypeOperand() ? child_iterator()
39 : reinterpret_cast<Stmt **>(&Operand) + 1;
Sebastian Redlc42e1182008-11-11 11:37:55 +000040}
Ted Kremeneka758d092007-08-24 20:21:10 +000041
Francois Pichet01b7c302010-09-08 12:20:18 +000042QualType CXXUuidofExpr::getTypeOperand() const {
43 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
44 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
45 .getUnqualifiedType();
46}
47
48// CXXUuidofExpr - has child iterators if the operand is an expression
49Stmt::child_iterator CXXUuidofExpr::child_begin() {
50 return isTypeOperand() ? child_iterator()
51 : reinterpret_cast<Stmt **>(&Operand);
52}
53Stmt::child_iterator CXXUuidofExpr::child_end() {
54 return isTypeOperand() ? child_iterator()
55 : reinterpret_cast<Stmt **>(&Operand) + 1;
56}
57
Ted Kremeneka758d092007-08-24 20:21:10 +000058// CXXBoolLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000059Stmt::child_iterator CXXBoolLiteralExpr::child_begin() {
Ted Kremenek9ac59282007-10-18 23:28:49 +000060 return child_iterator();
61}
62Stmt::child_iterator CXXBoolLiteralExpr::child_end() {
63 return child_iterator();
64}
Chris Lattner50dd2892008-02-26 00:51:44 +000065
Sebastian Redl6e8ed162009-05-10 18:38:11 +000066// CXXNullPtrLiteralExpr
Mike Stump1eb44332009-09-09 15:08:12 +000067Stmt::child_iterator CXXNullPtrLiteralExpr::child_begin() {
Sebastian Redl6e8ed162009-05-10 18:38:11 +000068 return child_iterator();
69}
70Stmt::child_iterator CXXNullPtrLiteralExpr::child_end() {
71 return child_iterator();
72}
73
Douglas Gregor796da182008-11-04 14:32:21 +000074// CXXThisExpr
75Stmt::child_iterator CXXThisExpr::child_begin() { return child_iterator(); }
76Stmt::child_iterator CXXThisExpr::child_end() { return child_iterator(); }
77
Chris Lattner50dd2892008-02-26 00:51:44 +000078// CXXThrowExpr
Ted Kremenek1060aff2008-06-17 03:11:08 +000079Stmt::child_iterator CXXThrowExpr::child_begin() { return &Op; }
Chris Lattner50dd2892008-02-26 00:51:44 +000080Stmt::child_iterator CXXThrowExpr::child_end() {
81 // If Op is 0, we are processing throw; which has no children.
Ted Kremenek1060aff2008-06-17 03:11:08 +000082 return Op ? &Op+1 : &Op;
Chris Lattner50dd2892008-02-26 00:51:44 +000083}
Chris Lattner04421082008-04-08 04:40:51 +000084
85// CXXDefaultArgExpr
86Stmt::child_iterator CXXDefaultArgExpr::child_begin() {
Chris Lattner8123a952008-04-10 02:22:51 +000087 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000088}
89Stmt::child_iterator CXXDefaultArgExpr::child_end() {
Chris Lattner8123a952008-04-10 02:22:51 +000090 return child_iterator();
Chris Lattner04421082008-04-08 04:40:51 +000091}
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +000092
Douglas Gregored8abf12010-07-08 06:14:04 +000093// CXXScalarValueInitExpr
Douglas Gregorab6677e2010-09-08 00:15:04 +000094SourceRange CXXScalarValueInitExpr::getSourceRange() const {
95 SourceLocation Start = RParenLoc;
96 if (TypeInfo)
97 Start = TypeInfo->getTypeLoc().getBeginLoc();
98 return SourceRange(Start, RParenLoc);
99}
100
Douglas Gregored8abf12010-07-08 06:14:04 +0000101Stmt::child_iterator CXXScalarValueInitExpr::child_begin() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000102 return child_iterator();
103}
Douglas Gregored8abf12010-07-08 06:14:04 +0000104Stmt::child_iterator CXXScalarValueInitExpr::child_end() {
Argyrios Kyrtzidis987a14b2008-08-22 15:38:55 +0000105 return child_iterator();
106}
Argyrios Kyrtzidis9e922b12008-09-09 23:47:53 +0000107
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000108// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +0000109CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000110 Expr **placementArgs, unsigned numPlaceArgs,
Douglas Gregor4bd40312010-07-13 15:54:32 +0000111 SourceRange TypeIdParens, Expr *arraySize,
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000112 CXXConstructorDecl *constructor, bool initializer,
113 Expr **constructorArgs, unsigned numConsArgs,
114 FunctionDecl *operatorDelete, QualType ty,
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000115 TypeSourceInfo *AllocatedTypeInfo,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000116 SourceLocation startLoc, SourceLocation endLoc,
117 SourceLocation constructorLParen,
118 SourceLocation constructorRParen)
John McCallf89e55a2010-11-18 06:31:45 +0000119 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
120 ty->isDependentType(), ty->isDependentType()),
Douglas Gregor4bd40312010-07-13 15:54:32 +0000121 GlobalNew(globalNew),
Chris Lattner59218632010-05-10 01:22:27 +0000122 Initializer(initializer), SubExprs(0), OperatorNew(operatorNew),
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000123 OperatorDelete(operatorDelete), Constructor(constructor),
Douglas Gregor1bb2a932010-09-07 21:49:58 +0000124 AllocatedTypeInfo(AllocatedTypeInfo), TypeIdParens(TypeIdParens),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000125 StartLoc(startLoc), EndLoc(endLoc), ConstructorLParen(constructorLParen),
126 ConstructorRParen(constructorRParen) {
Chris Lattner59218632010-05-10 01:22:27 +0000127 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs);
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000128 unsigned i = 0;
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000129 if (Array)
130 SubExprs[i++] = arraySize;
131 for (unsigned j = 0; j < NumPlacementArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000132 SubExprs[i++] = placementArgs[j];
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000133 for (unsigned j = 0; j < NumConstructorArgs; ++j)
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000134 SubExprs[i++] = constructorArgs[j];
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000135}
136
Chris Lattner59218632010-05-10 01:22:27 +0000137void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
138 unsigned numPlaceArgs, unsigned numConsArgs){
139 assert(SubExprs == 0 && "SubExprs already allocated");
140 Array = isArray;
141 NumPlacementArgs = numPlaceArgs;
142 NumConstructorArgs = numConsArgs;
143
144 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
145 SubExprs = new (C) Stmt*[TotalSize];
146}
147
148
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000149Stmt::child_iterator CXXNewExpr::child_begin() { return &SubExprs[0]; }
150Stmt::child_iterator CXXNewExpr::child_end() {
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000151 return &SubExprs[0] + Array + getNumPlacementArgs() + getNumConstructorArgs();
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000152}
153
154// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000155QualType CXXDeleteExpr::getDestroyedType() const {
156 const Expr *Arg = getArgument();
157 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
158 if (ICE->getCastKind() != CK_UserDefinedConversion &&
159 ICE->getType()->isVoidPointerType())
160 Arg = ICE->getSubExpr();
161 else
162 break;
163 }
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000164 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000165 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000166
167 if (ArgType->isDependentType() && !ArgType->isPointerType())
168 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000169
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000170 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000171}
172
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000173Stmt::child_iterator CXXDeleteExpr::child_begin() { return &Argument; }
174Stmt::child_iterator CXXDeleteExpr::child_end() { return &Argument+1; }
175
Douglas Gregora71d8192009-09-04 17:36:40 +0000176// CXXPseudoDestructorExpr
177Stmt::child_iterator CXXPseudoDestructorExpr::child_begin() { return &Base; }
178Stmt::child_iterator CXXPseudoDestructorExpr::child_end() {
179 return &Base + 1;
180}
181
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000182PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
183 : Type(Info)
184{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000185 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000186}
187
188QualType CXXPseudoDestructorExpr::getDestroyedType() const {
189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
190 return TInfo->getType();
191
192 return QualType();
193}
194
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000200}
201
202
John McCallba135432009-11-21 08:51:07 +0000203// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000204UnresolvedLookupExpr *
205UnresolvedLookupExpr::Create(ASTContext &C, bool Dependent,
John McCallc373d482010-01-27 01:50:18 +0000206 CXXRecordDecl *NamingClass,
John McCallf7a1a742009-11-24 19:00:30 +0000207 NestedNameSpecifier *Qualifier,
Abramo Bagnara25777432010-08-11 22:01:17 +0000208 SourceRange QualifierRange,
209 const DeclarationNameInfo &NameInfo,
210 bool ADL,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000211 const TemplateArgumentListInfo &Args,
212 UnresolvedSetIterator Begin,
213 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000214{
215 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
216 ExplicitTemplateArgumentList::sizeFor(Args));
217 UnresolvedLookupExpr *ULE
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000218 = new (Mem) UnresolvedLookupExpr(C,
219 Dependent ? C.DependentTy : C.OverloadTy,
John McCallc373d482010-01-27 01:50:18 +0000220 Dependent, NamingClass,
Abramo Bagnara25777432010-08-11 22:01:17 +0000221 Qualifier, QualifierRange, NameInfo,
222 ADL,
John McCallf7a1a742009-11-24 19:00:30 +0000223 /*Overload*/ true,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000224 /*ExplicitTemplateArgs*/ true,
225 Begin, End);
John McCallf7a1a742009-11-24 19:00:30 +0000226
227 reinterpret_cast<ExplicitTemplateArgumentList*>(ULE+1)->initializeFrom(Args);
228
229 return ULE;
230}
231
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000232UnresolvedLookupExpr *
233UnresolvedLookupExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
234 std::size_t size = sizeof(UnresolvedLookupExpr);
235 if (NumTemplateArgs != 0)
236 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
237
Chris Lattner32488542010-10-30 05:14:06 +0000238 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000239 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
240 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
241 return E;
242}
243
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000244OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, QualType T,
245 bool Dependent, NestedNameSpecifier *Qualifier,
Abramo Bagnara25777432010-08-11 22:01:17 +0000246 SourceRange QRange,
247 const DeclarationNameInfo &NameInfo,
248 bool HasTemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000249 UnresolvedSetIterator Begin,
250 UnresolvedSetIterator End)
John McCallf89e55a2010-11-18 06:31:45 +0000251 : Expr(K, T, VK_LValue, OK_Ordinary, Dependent, Dependent),
Abramo Bagnara25777432010-08-11 22:01:17 +0000252 Results(0), NumResults(0), NameInfo(NameInfo), Qualifier(Qualifier),
253 QualifierRange(QRange), HasExplicitTemplateArgs(HasTemplateArgs)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000254{
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000255 initializeResults(C, Begin, End);
256}
257
258void OverloadExpr::initializeResults(ASTContext &C,
259 UnresolvedSetIterator Begin,
260 UnresolvedSetIterator End) {
261 assert(Results == 0 && "Results already initialized!");
262 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000263 if (NumResults) {
264 Results = static_cast<DeclAccessPair *>(
265 C.Allocate(sizeof(DeclAccessPair) * NumResults,
Chris Lattner32488542010-10-30 05:14:06 +0000266 llvm::alignOf<DeclAccessPair>()));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000267 memcpy(Results, &*Begin.getIterator(),
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000268 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000269 }
270}
271
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000272
John McCall7bb12da2010-02-02 06:20:04 +0000273bool OverloadExpr::ComputeDependence(UnresolvedSetIterator Begin,
274 UnresolvedSetIterator End,
275 const TemplateArgumentListInfo *Args) {
John McCalleec51cf2010-01-20 00:46:10 +0000276 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I)
John McCallf7a1a742009-11-24 19:00:30 +0000277 if ((*I)->getDeclContext()->isDependentContext())
278 return true;
279
280 if (Args && TemplateSpecializationType::anyDependentTemplateArguments(*Args))
281 return true;
282
283 return false;
284}
285
John McCalle9ee23e2010-04-22 18:44:12 +0000286CXXRecordDecl *OverloadExpr::getNamingClass() const {
287 if (isa<UnresolvedLookupExpr>(this))
288 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
289 else
290 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
291}
292
John McCallba135432009-11-21 08:51:07 +0000293Stmt::child_iterator UnresolvedLookupExpr::child_begin() {
Mike Stump1eb44332009-09-09 15:08:12 +0000294 return child_iterator();
Douglas Gregor5c37de72008-12-06 00:22:45 +0000295}
John McCallba135432009-11-21 08:51:07 +0000296Stmt::child_iterator UnresolvedLookupExpr::child_end() {
Douglas Gregor5c37de72008-12-06 00:22:45 +0000297 return child_iterator();
298}
Sebastian Redl64b45f72009-01-05 20:52:13 +0000299// UnaryTypeTraitExpr
300Stmt::child_iterator UnaryTypeTraitExpr::child_begin() {
301 return child_iterator();
302}
303Stmt::child_iterator UnaryTypeTraitExpr::child_end() {
304 return child_iterator();
305}
306
John McCall865d4472009-11-19 22:55:06 +0000307// DependentScopeDeclRefExpr
John McCallf7a1a742009-11-24 19:00:30 +0000308DependentScopeDeclRefExpr *
309DependentScopeDeclRefExpr::Create(ASTContext &C,
310 NestedNameSpecifier *Qualifier,
311 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000313 const TemplateArgumentListInfo *Args) {
314 std::size_t size = sizeof(DependentScopeDeclRefExpr);
315 if (Args) size += ExplicitTemplateArgumentList::sizeFor(*Args);
316 void *Mem = C.Allocate(size);
317
318 DependentScopeDeclRefExpr *DRE
319 = new (Mem) DependentScopeDeclRefExpr(C.DependentTy,
320 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000321 NameInfo, Args != 0);
John McCallf7a1a742009-11-24 19:00:30 +0000322
323 if (Args)
324 reinterpret_cast<ExplicitTemplateArgumentList*>(DRE+1)
325 ->initializeFrom(*Args);
326
327 return DRE;
328}
329
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000330DependentScopeDeclRefExpr *
331DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
332 unsigned NumTemplateArgs) {
333 std::size_t size = sizeof(DependentScopeDeclRefExpr);
334 if (NumTemplateArgs)
335 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
336 void *Mem = C.Allocate(size);
337
338 return new (Mem) DependentScopeDeclRefExpr(QualType(), 0, SourceRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +0000339 DeclarationNameInfo(),
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000340 NumTemplateArgs != 0);
341}
342
John McCall865d4472009-11-19 22:55:06 +0000343StmtIterator DependentScopeDeclRefExpr::child_begin() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000344 return child_iterator();
345}
346
John McCall865d4472009-11-19 22:55:06 +0000347StmtIterator DependentScopeDeclRefExpr::child_end() {
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000348 return child_iterator();
349}
350
Chandler Carruth428edaf2010-10-25 08:47:36 +0000351SourceRange CXXConstructExpr::getSourceRange() const {
Douglas Gregor40749ee2010-11-03 00:35:38 +0000352 if (ParenRange.isValid())
353 return SourceRange(Loc, ParenRange.getEnd());
354
355 SourceLocation End = Loc;
356 for (unsigned I = getNumArgs(); I > 0; --I) {
357 const Expr *Arg = getArg(I-1);
358 if (!Arg->isDefaultArgument()) {
359 SourceLocation NewEnd = Arg->getLocEnd();
360 if (NewEnd.isValid()) {
361 End = NewEnd;
362 break;
363 }
364 }
365 }
366
367 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000368}
369
Douglas Gregorb4609802008-11-14 16:09:21 +0000370SourceRange CXXOperatorCallExpr::getSourceRange() const {
371 OverloadedOperatorKind Kind = getOperator();
372 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
373 if (getNumArgs() == 1)
374 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000375 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000376 getArg(0)->getSourceRange().getEnd());
377 else
378 // Postfix operator
379 return SourceRange(getArg(0)->getSourceRange().getEnd(),
380 getOperatorLoc());
381 } else if (Kind == OO_Call) {
382 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
383 } else if (Kind == OO_Subscript) {
384 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
385 } else if (getNumArgs() == 1) {
386 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
387 } else if (getNumArgs() == 2) {
388 return SourceRange(getArg(0)->getSourceRange().getBegin(),
389 getArg(1)->getSourceRange().getEnd());
390 } else {
391 return SourceRange();
392 }
393}
394
Douglas Gregor88a35142008-12-22 05:46:06 +0000395Expr *CXXMemberCallExpr::getImplicitObjectArgument() {
396 if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
397 return MemExpr->getBase();
398
399 // FIXME: Will eventually need to cope with member pointers.
400 return 0;
401}
402
Chandler Carruth007a9b12010-10-27 06:55:41 +0000403CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
404 Expr* ThisArg = getImplicitObjectArgument();
405 if (!ThisArg)
406 return 0;
407
408 if (ThisArg->getType()->isAnyPointerType())
409 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
410
411 return ThisArg->getType()->getAsCXXRecordDecl();
412}
413
Douglas Gregor00b98c22009-11-12 15:31:47 +0000414SourceRange CXXMemberCallExpr::getSourceRange() const {
415 SourceLocation LocStart = getCallee()->getLocStart();
416 if (LocStart.isInvalid() && getNumArgs() > 0)
417 LocStart = getArg(0)->getLocStart();
418 return SourceRange(LocStart, getRParenLoc());
419}
420
421
Douglas Gregor49badde2008-10-27 19:41:14 +0000422//===----------------------------------------------------------------------===//
423// Named casts
424//===----------------------------------------------------------------------===//
425
426/// getCastName - Get the name of the C++ cast being used, e.g.,
427/// "static_cast", "dynamic_cast", "reinterpret_cast", or
428/// "const_cast". The returned pointer must not be freed.
429const char *CXXNamedCastExpr::getCastName() const {
430 switch (getStmtClass()) {
431 case CXXStaticCastExprClass: return "static_cast";
432 case CXXDynamicCastExprClass: return "dynamic_cast";
433 case CXXReinterpretCastExprClass: return "reinterpret_cast";
434 case CXXConstCastExprClass: return "const_cast";
435 default: return "<invalid cast>";
436 }
437}
Douglas Gregor506ae412009-01-16 18:33:17 +0000438
John McCallf871d0c2010-08-07 06:22:56 +0000439CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000440 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000441 CastKind K, Expr *Op,
442 const CXXCastPath *BasePath,
443 TypeSourceInfo *WrittenTy,
444 SourceLocation L) {
445 unsigned PathSize = (BasePath ? BasePath->size() : 0);
446 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
447 + PathSize * sizeof(CXXBaseSpecifier*));
448 CXXStaticCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000449 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L);
John McCallf871d0c2010-08-07 06:22:56 +0000450 if (PathSize) E->setCastPath(*BasePath);
451 return E;
452}
453
454CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
455 unsigned PathSize) {
456 void *Buffer =
457 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
458 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
459}
460
461CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000462 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000463 CastKind K, Expr *Op,
464 const CXXCastPath *BasePath,
465 TypeSourceInfo *WrittenTy,
466 SourceLocation L) {
467 unsigned PathSize = (BasePath ? BasePath->size() : 0);
468 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
469 + PathSize * sizeof(CXXBaseSpecifier*));
470 CXXDynamicCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000471 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L);
John McCallf871d0c2010-08-07 06:22:56 +0000472 if (PathSize) E->setCastPath(*BasePath);
473 return E;
474}
475
476CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
477 unsigned PathSize) {
478 void *Buffer =
479 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
480 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
481}
482
483CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000484CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
485 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000486 const CXXCastPath *BasePath,
487 TypeSourceInfo *WrittenTy, SourceLocation L) {
488 unsigned PathSize = (BasePath ? BasePath->size() : 0);
489 void *Buffer =
490 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
491 CXXReinterpretCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000492 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L);
John McCallf871d0c2010-08-07 06:22:56 +0000493 if (PathSize) E->setCastPath(*BasePath);
494 return E;
495}
496
497CXXReinterpretCastExpr *
498CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
499 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
500 + PathSize * sizeof(CXXBaseSpecifier*));
501 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
502}
503
John McCallf89e55a2010-11-18 06:31:45 +0000504CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
505 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000506 TypeSourceInfo *WrittenTy,
507 SourceLocation L) {
John McCallf89e55a2010-11-18 06:31:45 +0000508 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L);
John McCallf871d0c2010-08-07 06:22:56 +0000509}
510
511CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
512 return new (C) CXXConstCastExpr(EmptyShell());
513}
514
515CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000516CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000517 TypeSourceInfo *Written, SourceLocation L,
518 CastKind K, Expr *Op, const CXXCastPath *BasePath,
519 SourceLocation R) {
520 unsigned PathSize = (BasePath ? BasePath->size() : 0);
521 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
522 + PathSize * sizeof(CXXBaseSpecifier*));
523 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000524 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000525 if (PathSize) E->setCastPath(*BasePath);
526 return E;
527}
528
529CXXFunctionalCastExpr *
530CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
531 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
532 + PathSize * sizeof(CXXBaseSpecifier*));
533 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
534}
535
536
Douglas Gregor65222e82009-12-23 18:19:08 +0000537CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000538CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
539 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000540 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000541 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
542 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000543}
544
Mike Stump1eb44332009-09-09 15:08:12 +0000545CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000546 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000547 return new (C) CXXTemporary(Destructor);
548}
549
Mike Stump1eb44332009-09-09 15:08:12 +0000550CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000551 CXXTemporary *Temp,
552 Expr* SubExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000553 assert(SubExpr->getType()->isRecordType() &&
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000554 "Expression bound to a temporary must have record type!");
555
Fariborz Jahanianb8e39232010-11-02 21:05:53 +0000556 return new (C) CXXBindTemporaryExpr(Temp, SubExpr,
557 SubExpr->isTypeDependent(),
558 SubExpr->isValueDependent());
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000559}
560
Anders Carlsson8e587a12009-05-30 20:56:46 +0000561CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000562 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000563 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000564 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000565 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000566 SourceRange parenRange,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000567 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000568 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
569 Type->getType().getNonReferenceType(),
570 Type->getTypeLoc().getBeginLoc(),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000571 Cons, false, Args, NumArgs, ZeroInitialization,
572 CXXConstructExpr::CK_Complete, parenRange),
573 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000574}
575
576SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000577 return SourceRange(Type->getTypeLoc().getBeginLoc(),
578 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000579}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000580
Mike Stump1eb44332009-09-09 15:08:12 +0000581CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000582 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000583 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000584 Expr **Args, unsigned NumArgs,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000585 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000586 ConstructionKind ConstructKind,
587 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000588 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000589 Elidable, Args, NumArgs, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000590 ConstructKind, ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000591}
592
Mike Stump1eb44332009-09-09 15:08:12 +0000593CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000594 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000595 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000596 Expr **args, unsigned numargs,
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000597 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000598 ConstructionKind ConstructKind,
599 SourceRange ParenRange)
John McCallf89e55a2010-11-18 06:31:45 +0000600: Expr(SC, T, VK_RValue, OK_Ordinary,
Anders Carlssone349bea2009-04-23 02:32:43 +0000601 T->isDependentType(),
602 (T->isDependentType() ||
603 CallExpr::hasAnyValueDependentArguments(args, numargs))),
Chandler Carruth428edaf2010-10-25 08:47:36 +0000604 Constructor(D), Loc(Loc), ParenRange(ParenRange), Elidable(elidable),
Anders Carlsson72e96fd2010-05-02 22:54:08 +0000605 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind),
606 Args(0), NumArgs(numargs)
Douglas Gregor16006c92009-12-16 18:50:27 +0000607{
608 if (NumArgs) {
609 Args = new (C) Stmt*[NumArgs];
610
611 for (unsigned i = 0; i != NumArgs; ++i) {
612 assert(args[i] && "NULL argument in CXXConstructExpr");
613 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000614 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000615 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000616}
617
Ted Kremenekd04ed412010-05-10 20:06:30 +0000618CXXExprWithTemporaries::CXXExprWithTemporaries(ASTContext &C,
619 Expr *subexpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000620 CXXTemporary **temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000621 unsigned numtemps)
Chris Lattnerd2598362010-05-10 00:25:06 +0000622 : Expr(CXXExprWithTemporariesClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000623 subexpr->getValueKind(), subexpr->getObjectKind(),
624 subexpr->isTypeDependent(), subexpr->isValueDependent()),
Chris Lattnerd2598362010-05-10 00:25:06 +0000625 SubExpr(subexpr), Temps(0), NumTemps(0) {
Chris Lattneraff32cb2010-05-10 00:45:12 +0000626 if (numtemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000627 setNumTemporaries(C, numtemps);
Chris Lattnerd2598362010-05-10 00:25:06 +0000628 for (unsigned i = 0; i != numtemps; ++i)
Anders Carlssonff6b3d62009-05-30 21:05:25 +0000629 Temps[i] = temps[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000630 }
631}
632
Ted Kremenekd04ed412010-05-10 20:06:30 +0000633void CXXExprWithTemporaries::setNumTemporaries(ASTContext &C, unsigned N) {
Chris Lattnerd2598362010-05-10 00:25:06 +0000634 assert(Temps == 0 && "Cannot resize with this");
Daniel Dunbar90556d42010-05-10 15:59:37 +0000635 NumTemps = N;
Ted Kremenekd04ed412010-05-10 20:06:30 +0000636 Temps = new (C) CXXTemporary*[NumTemps];
Chris Lattnerd2598362010-05-10 00:25:06 +0000637}
638
639
Mike Stump1eb44332009-09-09 15:08:12 +0000640CXXExprWithTemporaries *CXXExprWithTemporaries::Create(ASTContext &C,
Anders Carlsson88eaf072009-05-30 22:38:53 +0000641 Expr *SubExpr,
Mike Stump1eb44332009-09-09 15:08:12 +0000642 CXXTemporary **Temps,
Anders Carlsson0ece4912009-12-15 20:51:39 +0000643 unsigned NumTemps) {
Ted Kremenekd04ed412010-05-10 20:06:30 +0000644 return new (C) CXXExprWithTemporaries(C, SubExpr, Temps, NumTemps);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000645}
646
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000647// CXXBindTemporaryExpr
648Stmt::child_iterator CXXBindTemporaryExpr::child_begin() {
649 return &SubExpr;
650}
651
Mike Stump1eb44332009-09-09 15:08:12 +0000652Stmt::child_iterator CXXBindTemporaryExpr::child_end() {
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000653 return &SubExpr + 1;
654}
655
Anders Carlssone349bea2009-04-23 02:32:43 +0000656// CXXConstructExpr
657Stmt::child_iterator CXXConstructExpr::child_begin() {
658 return &Args[0];
659}
660Stmt::child_iterator CXXConstructExpr::child_end() {
661 return &Args[0]+NumArgs;
662}
663
Anders Carlsson55674ac2009-05-01 22:21:22 +0000664// CXXExprWithTemporaries
665Stmt::child_iterator CXXExprWithTemporaries::child_begin() {
666 return &SubExpr;
Anders Carlsson19d28a62009-04-21 02:22:11 +0000667}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000668
Mike Stump1eb44332009-09-09 15:08:12 +0000669Stmt::child_iterator CXXExprWithTemporaries::child_end() {
Anders Carlsson55674ac2009-05-01 22:21:22 +0000670 return &SubExpr + 1;
671}
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000672
Douglas Gregorab6677e2010-09-08 00:15:04 +0000673CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000674 SourceLocation LParenLoc,
675 Expr **Args,
676 unsigned NumArgs,
677 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000678 : Expr(CXXUnresolvedConstructExprClass,
679 Type->getType().getNonReferenceType(),
John McCall09431682010-11-18 19:01:18 +0000680 VK_LValue, OK_Ordinary,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000681 Type->getType()->isDependentType(), true),
682 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000683 LParenLoc(LParenLoc),
684 RParenLoc(RParenLoc),
685 NumArgs(NumArgs) {
686 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
687 memcpy(StoredArgs, Args, sizeof(Expr *) * NumArgs);
688}
689
690CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000691CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000692 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000693 SourceLocation LParenLoc,
694 Expr **Args,
695 unsigned NumArgs,
696 SourceLocation RParenLoc) {
697 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
698 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000699 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000700 Args, NumArgs, RParenLoc);
701}
702
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000703CXXUnresolvedConstructExpr *
704CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
705 Stmt::EmptyShell Empty;
706 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
707 sizeof(Expr *) * NumArgs);
708 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
709}
710
Douglas Gregorab6677e2010-09-08 00:15:04 +0000711SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
712 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
713}
714
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000715Stmt::child_iterator CXXUnresolvedConstructExpr::child_begin() {
716 return child_iterator(reinterpret_cast<Stmt **>(this + 1));
717}
718
719Stmt::child_iterator CXXUnresolvedConstructExpr::child_end() {
720 return child_iterator(reinterpret_cast<Stmt **>(this + 1) + NumArgs);
721}
Sebastian Redl8b0b4752009-05-16 18:50:46 +0000722
John McCall865d4472009-11-19 22:55:06 +0000723CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000724 Expr *Base, QualType BaseType,
725 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000726 SourceLocation OperatorLoc,
727 NestedNameSpecifier *Qualifier,
728 SourceRange QualifierRange,
729 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000730 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000731 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +0000732 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
733 VK_LValue, OK_Ordinary, true, true),
John McCallaa81e162009-12-01 22:10:20 +0000734 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
735 HasExplicitTemplateArgs(TemplateArgs != 0),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000736 OperatorLoc(OperatorLoc),
737 Qualifier(Qualifier), QualifierRange(QualifierRange),
738 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +0000739 MemberNameInfo(MemberNameInfo) {
John McCalld5532b62009-11-23 01:53:49 +0000740 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000741 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000742}
743
John McCall865d4472009-11-19 22:55:06 +0000744CXXDependentScopeMemberExpr *
745CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +0000746 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000747 SourceLocation OperatorLoc,
748 NestedNameSpecifier *Qualifier,
749 SourceRange QualifierRange,
750 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000751 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +0000752 const TemplateArgumentListInfo *TemplateArgs) {
753 if (!TemplateArgs)
John McCallaa81e162009-12-01 22:10:20 +0000754 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
755 IsArrow, OperatorLoc,
756 Qualifier, QualifierRange,
757 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000758 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
John McCalld5532b62009-11-23 01:53:49 +0000760 std::size_t size = sizeof(CXXDependentScopeMemberExpr);
761 if (TemplateArgs)
762 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
763
Chris Lattner32488542010-10-30 05:14:06 +0000764 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +0000765 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
766 IsArrow, OperatorLoc,
767 Qualifier, QualifierRange,
768 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000769 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +0000770}
771
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000772CXXDependentScopeMemberExpr *
773CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
774 unsigned NumTemplateArgs) {
775 if (NumTemplateArgs == 0)
776 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
777 0, SourceLocation(), 0,
778 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000779 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000780
781 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
782 ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +0000783 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000784 CXXDependentScopeMemberExpr *E
785 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
786 0, SourceLocation(), 0,
787 SourceRange(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +0000788 DeclarationNameInfo(), 0);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000789 E->HasExplicitTemplateArgs = true;
790 return E;
791}
792
John McCall865d4472009-11-19 22:55:06 +0000793Stmt::child_iterator CXXDependentScopeMemberExpr::child_begin() {
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000794 return child_iterator(&Base);
795}
796
John McCall865d4472009-11-19 22:55:06 +0000797Stmt::child_iterator CXXDependentScopeMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000798 if (isImplicitAccess())
799 return child_iterator(&Base);
Douglas Gregor1c0ca592009-05-22 21:13:27 +0000800 return child_iterator(&Base + 1);
801}
John McCall129e2df2009-11-30 22:42:35 +0000802
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000803UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, QualType T,
804 bool Dependent,
John McCall129e2df2009-11-30 22:42:35 +0000805 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000806 Expr *Base, QualType BaseType,
807 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000808 SourceLocation OperatorLoc,
809 NestedNameSpecifier *Qualifier,
810 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000811 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000812 const TemplateArgumentListInfo *TemplateArgs,
813 UnresolvedSetIterator Begin,
814 UnresolvedSetIterator End)
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000815 : OverloadExpr(UnresolvedMemberExprClass, C, T, Dependent,
Abramo Bagnara25777432010-08-11 22:01:17 +0000816 Qualifier, QualifierRange, MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000817 TemplateArgs != 0, Begin, End),
John McCall7bb12da2010-02-02 06:20:04 +0000818 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
819 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall129e2df2009-11-30 22:42:35 +0000820 if (TemplateArgs)
John McCall7bb12da2010-02-02 06:20:04 +0000821 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
John McCall129e2df2009-11-30 22:42:35 +0000822}
823
824UnresolvedMemberExpr *
825UnresolvedMemberExpr::Create(ASTContext &C, bool Dependent,
826 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +0000827 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +0000828 SourceLocation OperatorLoc,
829 NestedNameSpecifier *Qualifier,
830 SourceRange QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000831 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +0000832 const TemplateArgumentListInfo *TemplateArgs,
833 UnresolvedSetIterator Begin,
834 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +0000835 std::size_t size = sizeof(UnresolvedMemberExpr);
836 if (TemplateArgs)
837 size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
838
Chris Lattner32488542010-10-30 05:14:06 +0000839 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000840 return new (Mem) UnresolvedMemberExpr(C,
John McCall129e2df2009-11-30 22:42:35 +0000841 Dependent ? C.DependentTy : C.OverloadTy,
John McCallaa81e162009-12-01 22:10:20 +0000842 Dependent, HasUnresolvedUsing, Base, BaseType,
843 IsArrow, OperatorLoc, Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +0000844 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +0000845}
846
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000847UnresolvedMemberExpr *
848UnresolvedMemberExpr::CreateEmpty(ASTContext &C, unsigned NumTemplateArgs) {
849 std::size_t size = sizeof(UnresolvedMemberExpr);
850 if (NumTemplateArgs != 0)
851 size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
852
Chris Lattner32488542010-10-30 05:14:06 +0000853 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000854 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
855 E->HasExplicitTemplateArgs = NumTemplateArgs != 0;
856 return E;
857}
858
John McCallc373d482010-01-27 01:50:18 +0000859CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
860 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
861
862 // If there was a nested name specifier, it names the naming class.
863 // It can't be dependent: after all, we were actually able to do the
864 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000865 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +0000866 if (getQualifier()) {
867 Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +0000868 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000869 Record = T->getAsCXXRecordDecl();
870 assert(Record && "qualifier in member expression does not name record");
871 }
John McCallc373d482010-01-27 01:50:18 +0000872 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000873 else {
John McCallc373d482010-01-27 01:50:18 +0000874 QualType BaseType = getBaseType().getNonReferenceType();
875 if (isArrow()) {
876 const PointerType *PT = BaseType->getAs<PointerType>();
877 assert(PT && "base of arrow member access is not pointer");
878 BaseType = PT->getPointeeType();
879 }
880
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000881 Record = BaseType->getAsCXXRecordDecl();
882 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +0000883 }
884
Douglas Gregorc96be1e2010-04-27 18:19:34 +0000885 return Record;
John McCallc373d482010-01-27 01:50:18 +0000886}
887
John McCall129e2df2009-11-30 22:42:35 +0000888Stmt::child_iterator UnresolvedMemberExpr::child_begin() {
889 return child_iterator(&Base);
890}
891
892Stmt::child_iterator UnresolvedMemberExpr::child_end() {
John McCallaa81e162009-12-01 22:10:20 +0000893 if (isImplicitAccess())
894 return child_iterator(&Base);
John McCall129e2df2009-11-30 22:42:35 +0000895 return child_iterator(&Base + 1);
896}
Sebastian Redl2e156222010-09-10 20:55:43 +0000897
898Stmt::child_iterator CXXNoexceptExpr::child_begin() {
899 return child_iterator(&Operand);
900}
901Stmt::child_iterator CXXNoexceptExpr::child_end() {
902 return child_iterator(&Operand + 1);
903}