blob: 1e766ada239603a72de6d8c9a81593e2ce92d08a [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Kremeneke3a0c142007-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 Gregor993603d2008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor9da64192010-04-26 22:37:10 +000021
Ted Kremeneke3a0c142007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor9da64192010-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
Francois Pichet9f4f2072010-09-08 12:20:18 +000032QualType CXXUuidofExpr::getTypeOperand() const {
33 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
34 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
35 .getUnqualifiedType();
36}
37
Douglas Gregor747eb782010-07-08 06:14:04 +000038// CXXScalarValueInitExpr
Douglas Gregor2b88c112010-09-08 00:15:04 +000039SourceRange CXXScalarValueInitExpr::getSourceRange() const {
40 SourceLocation Start = RParenLoc;
41 if (TypeInfo)
42 Start = TypeInfo->getTypeLoc().getBeginLoc();
43 return SourceRange(Start, RParenLoc);
44}
45
Sebastian Redlbd150f42008-11-21 19:14:01 +000046// CXXNewExpr
Ted Kremenek9d6eb402010-02-11 22:51:03 +000047CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redlbd150f42008-11-21 19:14:01 +000048 Expr **placementArgs, unsigned numPlaceArgs,
Douglas Gregorf2753b32010-07-13 15:54:32 +000049 SourceRange TypeIdParens, Expr *arraySize,
Sebastian Redlbd150f42008-11-21 19:14:01 +000050 CXXConstructorDecl *constructor, bool initializer,
51 Expr **constructorArgs, unsigned numConsArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +000052 bool HadMultipleCandidates,
John McCall284c48f2011-01-27 09:37:56 +000053 FunctionDecl *operatorDelete,
54 bool usualArrayDeleteWantsSize, QualType ty,
Douglas Gregor0744ef62010-09-07 21:49:58 +000055 TypeSourceInfo *AllocatedTypeInfo,
Chandler Carruth01718152010-10-25 08:47:36 +000056 SourceLocation startLoc, SourceLocation endLoc,
57 SourceLocation constructorLParen,
58 SourceLocation constructorRParen)
John McCall7decc9e2010-11-18 06:31:45 +000059 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000060 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000061 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000062 ty->containsUnexpandedParameterPack()),
John McCall284c48f2011-01-27 09:37:56 +000063 GlobalNew(globalNew), Initializer(initializer),
64 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +000065 HadMultipleCandidates(HadMultipleCandidates),
John McCall284c48f2011-01-27 09:37:56 +000066 SubExprs(0), OperatorNew(operatorNew),
Sebastian Redl351bb782008-12-02 14:43:59 +000067 OperatorDelete(operatorDelete), Constructor(constructor),
Douglas Gregor0744ef62010-09-07 21:49:58 +000068 AllocatedTypeInfo(AllocatedTypeInfo), TypeIdParens(TypeIdParens),
Chandler Carruth01718152010-10-25 08:47:36 +000069 StartLoc(startLoc), EndLoc(endLoc), ConstructorLParen(constructorLParen),
70 ConstructorRParen(constructorRParen) {
Chris Lattnerabfb58d2010-05-10 01:22:27 +000071 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, numConsArgs);
Sebastian Redlbd150f42008-11-21 19:14:01 +000072 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000073 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000074 if (arraySize->isInstantiationDependent())
75 ExprBits.InstantiationDependent = true;
76
Douglas Gregora6e053e2010-12-15 01:34:56 +000077 if (arraySize->containsUnexpandedParameterPack())
78 ExprBits.ContainsUnexpandedParameterPack = true;
79
Sebastian Redl351bb782008-12-02 14:43:59 +000080 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000081 }
82
83 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000084 if (placementArgs[j]->isInstantiationDependent())
85 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +000086 if (placementArgs[j]->containsUnexpandedParameterPack())
87 ExprBits.ContainsUnexpandedParameterPack = true;
88
Sebastian Redlbd150f42008-11-21 19:14:01 +000089 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +000090 }
91
92 for (unsigned j = 0; j < NumConstructorArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000093 if (constructorArgs[j]->isInstantiationDependent())
94 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +000095 if (constructorArgs[j]->containsUnexpandedParameterPack())
96 ExprBits.ContainsUnexpandedParameterPack = true;
97
Sebastian Redlbd150f42008-11-21 19:14:01 +000098 SubExprs[i++] = constructorArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +000099 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000100}
101
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000102void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
103 unsigned numPlaceArgs, unsigned numConsArgs){
104 assert(SubExprs == 0 && "SubExprs already allocated");
105 Array = isArray;
106 NumPlacementArgs = numPlaceArgs;
107 NumConstructorArgs = numConsArgs;
108
109 unsigned TotalSize = Array + NumPlacementArgs + NumConstructorArgs;
110 SubExprs = new (C) Stmt*[TotalSize];
111}
112
Sebastian Redl31ad7542011-03-13 17:09:40 +0000113bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000114 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000115 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000116}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000117
Sebastian Redlbd150f42008-11-21 19:14:01 +0000118// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000119QualType CXXDeleteExpr::getDestroyedType() const {
120 const Expr *Arg = getArgument();
121 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
122 if (ICE->getCastKind() != CK_UserDefinedConversion &&
123 ICE->getType()->isVoidPointerType())
124 Arg = ICE->getSubExpr();
125 else
126 break;
127 }
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000128 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000129 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000130
131 if (ArgType->isDependentType() && !ArgType->isPointerType())
132 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000133
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000134 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000135}
136
Douglas Gregorad8a3362009-09-04 17:36:40 +0000137// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000138PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
139 : Type(Info)
140{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000141 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000142}
143
John McCalldb40c7f2010-12-14 08:05:40 +0000144CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000145 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
146 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
147 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
148 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000149 : Expr(CXXPseudoDestructorExprClass,
150 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
151 FunctionProtoType::ExtProtoInfo())),
152 VK_RValue, OK_Ordinary,
153 /*isTypeDependent=*/(Base->isTypeDependent() ||
154 (DestroyedType.getTypeSourceInfo() &&
155 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000156 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000157 (Base->isInstantiationDependent() ||
158 (QualifierLoc &&
159 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
160 (ScopeType &&
161 ScopeType->getType()->isInstantiationDependentType()) ||
162 (DestroyedType.getTypeSourceInfo() &&
163 DestroyedType.getTypeSourceInfo()->getType()
164 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000165 // ContainsUnexpandedParameterPack
166 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000167 (QualifierLoc &&
168 QualifierLoc.getNestedNameSpecifier()
169 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000170 (ScopeType &&
171 ScopeType->getType()->containsUnexpandedParameterPack()) ||
172 (DestroyedType.getTypeSourceInfo() &&
173 DestroyedType.getTypeSourceInfo()->getType()
174 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000175 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000176 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000177 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
178 DestroyedType(DestroyedType) { }
179
Douglas Gregor678f90d2010-02-25 01:56:36 +0000180QualType CXXPseudoDestructorExpr::getDestroyedType() const {
181 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
182 return TInfo->getType();
183
184 return QualType();
185}
186
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000187SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000188 SourceLocation End = DestroyedType.getLocation();
189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000190 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000191 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000192}
193
194
John McCalld14a8642009-11-21 08:51:07 +0000195// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000196UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000197UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000198 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000199 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000200 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000201 const DeclarationNameInfo &NameInfo,
202 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000203 const TemplateArgumentListInfo *Args,
204 UnresolvedSetIterator Begin,
205 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000206{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000207 assert(Args || TemplateKWLoc.isValid());
208 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000209 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000210 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000211 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
212 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000213 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000214 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000215}
216
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000217UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000218UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
219 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000220 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000221 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000222 if (HasTemplateKWAndArgsInfo)
223 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000224
Chris Lattner5c0b4052010-10-30 05:14:06 +0000225 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000226 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000227 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000228 return E;
229}
230
Douglas Gregora6e053e2010-12-15 01:34:56 +0000231OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000232 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000233 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000234 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000235 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000236 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000237 UnresolvedSetIterator End,
238 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000239 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000240 bool KnownContainsUnexpandedParameterPack)
241 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
242 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000243 (KnownInstantiationDependent ||
244 NameInfo.isInstantiationDependent() ||
245 (QualifierLoc &&
246 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000247 (KnownContainsUnexpandedParameterPack ||
248 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000249 (QualifierLoc &&
250 QualifierLoc.getNestedNameSpecifier()
251 ->containsUnexpandedParameterPack()))),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000252 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
253 QualifierLoc(QualifierLoc),
254 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000255{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000256 NumResults = End - Begin;
257 if (NumResults) {
258 // Determine whether this expression is type-dependent.
259 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
260 if ((*I)->getDeclContext()->isDependentContext() ||
261 isa<UnresolvedUsingValueDecl>(*I)) {
262 ExprBits.TypeDependent = true;
263 ExprBits.ValueDependent = true;
264 }
265 }
266
267 Results = static_cast<DeclAccessPair *>(
268 C.Allocate(sizeof(DeclAccessPair) * NumResults,
269 llvm::alignOf<DeclAccessPair>()));
270 memcpy(Results, &*Begin.getIterator(),
271 NumResults * sizeof(DeclAccessPair));
272 }
273
274 // If we have explicit template arguments, check for dependent
275 // template arguments and whether they contain any unexpanded pack
276 // expansions.
277 if (TemplateArgs) {
278 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000279 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000280 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000281 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
282 Dependent,
283 InstantiationDependent,
284 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000285
286 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000287 ExprBits.TypeDependent = true;
288 ExprBits.ValueDependent = true;
289 }
290 if (InstantiationDependent)
291 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000292 if (ContainsUnexpandedParameterPack)
293 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000294 } else if (TemplateKWLoc.isValid()) {
295 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000296 }
297
298 if (isTypeDependent())
299 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000300}
301
302void OverloadExpr::initializeResults(ASTContext &C,
303 UnresolvedSetIterator Begin,
304 UnresolvedSetIterator End) {
305 assert(Results == 0 && "Results already initialized!");
306 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000307 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000308 Results = static_cast<DeclAccessPair *>(
309 C.Allocate(sizeof(DeclAccessPair) * NumResults,
310
311 llvm::alignOf<DeclAccessPair>()));
312 memcpy(Results, &*Begin.getIterator(),
313 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000314 }
315}
316
John McCall8c12dc42010-04-22 18:44:12 +0000317CXXRecordDecl *OverloadExpr::getNamingClass() const {
318 if (isa<UnresolvedLookupExpr>(this))
319 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
320 else
321 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
322}
323
John McCall8cd78132009-11-19 22:55:06 +0000324// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000325DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000326 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000327 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000328 const DeclarationNameInfo &NameInfo,
329 const TemplateArgumentListInfo *Args)
330 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
331 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000332 (NameInfo.isInstantiationDependent() ||
333 (QualifierLoc &&
334 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000335 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000336 (QualifierLoc &&
337 QualifierLoc.getNestedNameSpecifier()
338 ->containsUnexpandedParameterPack()))),
339 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000340 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000341{
342 if (Args) {
343 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000344 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 bool ContainsUnexpandedParameterPack
346 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000347 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
348 Dependent,
349 InstantiationDependent,
350 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000351 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000352 } else if (TemplateKWLoc.isValid()) {
353 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000354 }
355}
356
John McCalle66edc12009-11-24 19:00:30 +0000357DependentScopeDeclRefExpr *
358DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000359 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000360 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000361 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000362 const TemplateArgumentListInfo *Args) {
363 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000364 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000365 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
366 else if (TemplateKWLoc.isValid())
367 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000368 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000369 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
370 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000371}
372
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000373DependentScopeDeclRefExpr *
374DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000375 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000376 unsigned NumTemplateArgs) {
377 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000378 if (HasTemplateKWAndArgsInfo)
379 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000380 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000381 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000382 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000383 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000384 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000385 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000386 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000387}
388
Chandler Carruth01718152010-10-25 08:47:36 +0000389SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000390 if (isa<CXXTemporaryObjectExpr>(this))
391 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
392
Douglas Gregor15417cf2010-11-03 00:35:38 +0000393 if (ParenRange.isValid())
394 return SourceRange(Loc, ParenRange.getEnd());
395
396 SourceLocation End = Loc;
397 for (unsigned I = getNumArgs(); I > 0; --I) {
398 const Expr *Arg = getArg(I-1);
399 if (!Arg->isDefaultArgument()) {
400 SourceLocation NewEnd = Arg->getLocEnd();
401 if (NewEnd.isValid()) {
402 End = NewEnd;
403 break;
404 }
405 }
406 }
407
408 return SourceRange(Loc, End);
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000409}
410
Douglas Gregor993603d2008-11-14 16:09:21 +0000411SourceRange CXXOperatorCallExpr::getSourceRange() const {
412 OverloadedOperatorKind Kind = getOperator();
413 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
414 if (getNumArgs() == 1)
415 // Prefix operator
Mike Stump11289f42009-09-09 15:08:12 +0000416 return SourceRange(getOperatorLoc(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000417 getArg(0)->getSourceRange().getEnd());
418 else
419 // Postfix operator
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000420 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000421 getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000422 } else if (Kind == OO_Arrow) {
423 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000424 } else if (Kind == OO_Call) {
425 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
426 } else if (Kind == OO_Subscript) {
427 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
428 } else if (getNumArgs() == 1) {
429 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
430 } else if (getNumArgs() == 2) {
431 return SourceRange(getArg(0)->getSourceRange().getBegin(),
432 getArg(1)->getSourceRange().getEnd());
433 } else {
434 return SourceRange();
435 }
436}
437
Ted Kremenek98a24e32011-03-30 17:41:19 +0000438Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
439 if (const MemberExpr *MemExpr =
440 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000441 return MemExpr->getBase();
442
443 // FIXME: Will eventually need to cope with member pointers.
444 return 0;
445}
446
Ted Kremenek98a24e32011-03-30 17:41:19 +0000447CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
448 if (const MemberExpr *MemExpr =
449 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
450 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
451
452 // FIXME: Will eventually need to cope with member pointers.
453 return 0;
454}
455
456
Chandler Carruth00426b42010-10-27 06:55:41 +0000457CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
458 Expr* ThisArg = getImplicitObjectArgument();
459 if (!ThisArg)
460 return 0;
461
462 if (ThisArg->getType()->isAnyPointerType())
463 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
464
465 return ThisArg->getType()->getAsCXXRecordDecl();
466}
467
Douglas Gregoref986e82009-11-12 15:31:47 +0000468
Douglas Gregore200adc2008-10-27 19:41:14 +0000469//===----------------------------------------------------------------------===//
470// Named casts
471//===----------------------------------------------------------------------===//
472
473/// getCastName - Get the name of the C++ cast being used, e.g.,
474/// "static_cast", "dynamic_cast", "reinterpret_cast", or
475/// "const_cast". The returned pointer must not be freed.
476const char *CXXNamedCastExpr::getCastName() const {
477 switch (getStmtClass()) {
478 case CXXStaticCastExprClass: return "static_cast";
479 case CXXDynamicCastExprClass: return "dynamic_cast";
480 case CXXReinterpretCastExprClass: return "reinterpret_cast";
481 case CXXConstCastExprClass: return "const_cast";
482 default: return "<invalid cast>";
483 }
484}
Douglas Gregordd04d332009-01-16 18:33:17 +0000485
John McCallcf142162010-08-07 06:22:56 +0000486CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000487 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000488 CastKind K, Expr *Op,
489 const CXXCastPath *BasePath,
490 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000491 SourceLocation L,
492 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000493 unsigned PathSize = (BasePath ? BasePath->size() : 0);
494 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
495 + PathSize * sizeof(CXXBaseSpecifier*));
496 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000497 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
498 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000499 if (PathSize) E->setCastPath(*BasePath);
500 return E;
501}
502
503CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
504 unsigned PathSize) {
505 void *Buffer =
506 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
507 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
508}
509
510CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000511 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000512 CastKind K, Expr *Op,
513 const CXXCastPath *BasePath,
514 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000515 SourceLocation L,
516 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000517 unsigned PathSize = (BasePath ? BasePath->size() : 0);
518 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
519 + PathSize * sizeof(CXXBaseSpecifier*));
520 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000521 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
522 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000523 if (PathSize) E->setCastPath(*BasePath);
524 return E;
525}
526
527CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
528 unsigned PathSize) {
529 void *Buffer =
530 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
531 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
532}
533
Anders Carlsson267c0c92011-04-11 01:43:55 +0000534/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
535/// to always be null. For example:
536///
537/// struct A { };
538/// struct B final : A { };
539/// struct C { };
540///
541/// C *f(B* b) { return dynamic_cast<C*>(b); }
542bool CXXDynamicCastExpr::isAlwaysNull() const
543{
544 QualType SrcType = getSubExpr()->getType();
545 QualType DestType = getType();
546
547 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
548 SrcType = SrcPTy->getPointeeType();
549 DestType = DestType->castAs<PointerType>()->getPointeeType();
550 }
551
552 const CXXRecordDecl *SrcRD =
553 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
554
555 if (!SrcRD->hasAttr<FinalAttr>())
556 return false;
557
558 const CXXRecordDecl *DestRD =
559 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
560
561 return !DestRD->isDerivedFrom(SrcRD);
562}
563
John McCallcf142162010-08-07 06:22:56 +0000564CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000565CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
566 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000567 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000568 TypeSourceInfo *WrittenTy, SourceLocation L,
569 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000570 unsigned PathSize = (BasePath ? BasePath->size() : 0);
571 void *Buffer =
572 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
573 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000574 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
575 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000576 if (PathSize) E->setCastPath(*BasePath);
577 return E;
578}
579
580CXXReinterpretCastExpr *
581CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
582 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
583 + PathSize * sizeof(CXXBaseSpecifier*));
584 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
585}
586
John McCall7decc9e2010-11-18 06:31:45 +0000587CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
588 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000589 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000590 SourceLocation L,
591 SourceLocation RParenLoc) {
592 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000593}
594
595CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
596 return new (C) CXXConstCastExpr(EmptyShell());
597}
598
599CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000600CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000601 TypeSourceInfo *Written, SourceLocation L,
602 CastKind K, Expr *Op, const CXXCastPath *BasePath,
603 SourceLocation R) {
604 unsigned PathSize = (BasePath ? BasePath->size() : 0);
605 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
606 + PathSize * sizeof(CXXBaseSpecifier*));
607 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000608 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000609 if (PathSize) E->setCastPath(*BasePath);
610 return E;
611}
612
613CXXFunctionalCastExpr *
614CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
615 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
616 + PathSize * sizeof(CXXBaseSpecifier*));
617 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
618}
619
620
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000621CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000622CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
623 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000624 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000625 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
626 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000627}
628
Mike Stump11289f42009-09-09 15:08:12 +0000629CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000630 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000631 return new (C) CXXTemporary(Destructor);
632}
633
Mike Stump11289f42009-09-09 15:08:12 +0000634CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000635 CXXTemporary *Temp,
636 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000637 assert((SubExpr->getType()->isRecordType() ||
638 SubExpr->getType()->isArrayType()) &&
639 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000640
Douglas Gregora6e053e2010-12-15 01:34:56 +0000641 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000642}
643
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000644CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000645 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000646 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000647 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000648 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000649 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000650 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000651 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000652 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
653 Type->getType().getNonReferenceType(),
654 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000655 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000656 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000657 CXXConstructExpr::CK_Complete, parenRange),
658 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000659}
660
661SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000662 return SourceRange(Type->getTypeLoc().getBeginLoc(),
663 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000664}
Anders Carlsson6f287832009-04-21 02:22:11 +0000665
Mike Stump11289f42009-09-09 15:08:12 +0000666CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000667 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000668 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000669 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000670 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000671 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000672 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000673 ConstructionKind ConstructKind,
674 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000675 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000676 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000677 HadMultipleCandidates, ListInitialization,
678 ZeroInitialization, ConstructKind,
679 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000680}
681
Mike Stump11289f42009-09-09 15:08:12 +0000682CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000683 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000684 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000685 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000686 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000687 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000688 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000689 ConstructionKind ConstructKind,
690 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000691 : Expr(SC, T, VK_RValue, OK_Ordinary,
692 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000693 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000694 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000695 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000696 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000697 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000698 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000699 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000700{
701 if (NumArgs) {
702 Args = new (C) Stmt*[NumArgs];
703
704 for (unsigned i = 0; i != NumArgs; ++i) {
705 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000706
707 if (args[i]->isValueDependent())
708 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000709 if (args[i]->isInstantiationDependent())
710 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000711 if (args[i]->containsUnexpandedParameterPack())
712 ExprBits.ContainsUnexpandedParameterPack = true;
713
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000714 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000715 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000716 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000717}
718
Douglas Gregore31e6062012-02-07 10:09:13 +0000719LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
720 LambdaCaptureKind Kind, VarDecl *Var,
721 SourceLocation EllipsisLoc)
722 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
723{
724 unsigned Bits = 0;
725 if (Implicit)
726 Bits |= Capture_Implicit;
727
728 switch (Kind) {
729 case LCK_This:
730 assert(Var == 0 && "'this' capture cannot have a variable!");
731 break;
732
733 case LCK_ByCopy:
734 Bits |= Capture_ByCopy;
735 // Fall through
736 case LCK_ByRef:
737 assert(Var && "capture must have a variable!");
738 break;
739 }
740 VarAndBits.setInt(Bits);
741}
742
743LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
744 if (capturesThis())
745 return LCK_This;
746
747 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
748}
749
750LambdaExpr::LambdaExpr(QualType T,
751 SourceRange IntroducerRange,
752 LambdaCaptureDefault CaptureDefault,
753 ArrayRef<Capture> Captures,
754 bool ExplicitParams,
755 ArrayRef<Expr *> CaptureInits,
Douglas Gregor54fcea62012-02-13 16:35:30 +0000756 ArrayRef<VarDecl *> ArrayElementVars,
757 ArrayRef<unsigned> ArrayElementStarts,
Douglas Gregore31e6062012-02-07 10:09:13 +0000758 SourceLocation ClosingBrace)
759 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
760 T->isDependentType(), T->isDependentType(), T->isDependentType(),
761 /*ContainsUnexpandedParameterPack=*/false),
762 IntroducerRange(IntroducerRange),
Douglas Gregore31e6062012-02-07 10:09:13 +0000763 CaptureDefault(CaptureDefault),
764 ExplicitParams(ExplicitParams),
765 ClosingBrace(ClosingBrace)
766{
767 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000768 CXXRecordDecl *Class = getLambdaClass();
769 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000770 Data.allocateExtra(Captures, CaptureInits, ArrayElementVars,
771 ArrayElementStarts, getCallOperator()->getBody());
Douglas Gregorc8a73492012-02-13 15:44:47 +0000772
773 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore31e6062012-02-07 10:09:13 +0000774}
775
776LambdaExpr *LambdaExpr::Create(ASTContext &Context,
777 CXXRecordDecl *Class,
778 SourceRange IntroducerRange,
779 LambdaCaptureDefault CaptureDefault,
780 ArrayRef<Capture> Captures,
781 bool ExplicitParams,
782 ArrayRef<Expr *> CaptureInits,
Douglas Gregor54fcea62012-02-13 16:35:30 +0000783 ArrayRef<VarDecl *> ArrayElementVars,
784 ArrayRef<unsigned> ArrayElementStarts,
Douglas Gregore31e6062012-02-07 10:09:13 +0000785 SourceLocation ClosingBrace) {
786 // Determine the type of the expression (i.e., the type of the
787 // function object we're creating).
788 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000789
Douglas Gregor1db5f1f2012-02-13 15:51:35 +0000790 return new (Context) LambdaExpr(T, IntroducerRange, CaptureDefault,
791 Captures, ExplicitParams, CaptureInits,
Douglas Gregor54fcea62012-02-13 16:35:30 +0000792 ArrayElementVars, ArrayElementStarts,
Douglas Gregor1db5f1f2012-02-13 15:51:35 +0000793 ClosingBrace);
Douglas Gregore31e6062012-02-07 10:09:13 +0000794}
795
Douglas Gregorc8a73492012-02-13 15:44:47 +0000796LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
797 return getLambdaClass()->getLambdaData().getCaptures();
798}
799
800LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
801 struct CXXRecordDecl::LambdaDefinitionData &Data
802 = getLambdaClass()->getLambdaData();
803 return Data.getCaptures() + Data.NumCaptures;
804}
805
806LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
807 return capture_begin();
808}
809
810LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
811 struct CXXRecordDecl::LambdaDefinitionData &Data
812 = getLambdaClass()->getLambdaData();
813 return Data.getCaptures() + Data.NumExplicitCaptures;
814}
815
816LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
817 return explicit_capture_end();
818}
819
820LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
821 return capture_end();
822}
823
824LambdaExpr::capture_init_iterator LambdaExpr::capture_init_begin() const {
825 return reinterpret_cast<Expr **>(
826 getLambdaClass()->getLambdaData().getStoredStmts());
827}
828
829LambdaExpr::capture_init_iterator LambdaExpr::capture_init_end() const {
830 struct CXXRecordDecl::LambdaDefinitionData &Data
831 = getLambdaClass()->getLambdaData();
832 return reinterpret_cast<Expr **>(Data.getStoredStmts() + Data.NumCaptures);
833}
834
Douglas Gregor54fcea62012-02-13 16:35:30 +0000835ArrayRef<VarDecl *>
836LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
837 CXXRecordDecl::LambdaDefinitionData &Data = getLambdaClass()->getLambdaData();
838 assert(Data.HasArrayIndexVars && "No array index-var data?");
839
840 unsigned Index = Iter - capture_init_begin();
841 assert(Index < Data.NumCaptures && "Capture index out-of-range");
842 VarDecl **IndexVars = Data.getArrayIndexVars();
843 unsigned *IndexStarts = Data.getArrayIndexStarts();
844 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
845 IndexVars + IndexStarts[Index + 1]);
846}
847
Douglas Gregore31e6062012-02-07 10:09:13 +0000848CXXRecordDecl *LambdaExpr::getLambdaClass() const {
849 return getType()->getAsCXXRecordDecl();
850}
851
852CXXMethodDecl *LambdaExpr::getCallOperator() const {
853 CXXRecordDecl *Record = getLambdaClass();
854 DeclarationName Name
855 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
856 DeclContext::lookup_result Calls = Record->lookup(Name);
857 assert(Calls.first != Calls.second && "Missing lambda call operator!");
858 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
859 assert(Calls.first == Calls.second && "More than lambda one call operator?");
860 return Result;
861}
862
Douglas Gregorc8a73492012-02-13 15:44:47 +0000863/// \brief Retrieve the body of the lambda.
864CompoundStmt *LambdaExpr::getBody() const {
865 return cast<CompoundStmt>(*capture_init_end());
866}
867
Douglas Gregore31e6062012-02-07 10:09:13 +0000868bool LambdaExpr::isMutable() const {
869 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
870}
871
Douglas Gregorc8a73492012-02-13 15:44:47 +0000872Stmt::child_range LambdaExpr::children() {
873 struct CXXRecordDecl::LambdaDefinitionData &Data
874 = getLambdaClass()->getLambdaData();
875 return child_range(Data.getStoredStmts(),
876 Data.getStoredStmts() + Data.NumCaptures + 1);
877}
878
John McCall28fc7092011-11-10 05:35:25 +0000879ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
880 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000881 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000882 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000883 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000884 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000885 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000886 SubExpr(subexpr) {
887 ExprWithCleanupsBits.NumObjects = objects.size();
888 for (unsigned i = 0, e = objects.size(); i != e; ++i)
889 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000890}
891
John McCall28fc7092011-11-10 05:35:25 +0000892ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
893 ArrayRef<CleanupObject> objects) {
894 size_t size = sizeof(ExprWithCleanups)
895 + objects.size() * sizeof(CleanupObject);
896 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
897 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000898}
899
John McCall28fc7092011-11-10 05:35:25 +0000900ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
901 : Expr(ExprWithCleanupsClass, empty) {
902 ExprWithCleanupsBits.NumObjects = numObjects;
903}
Chris Lattnercba86142010-05-10 00:25:06 +0000904
John McCall28fc7092011-11-10 05:35:25 +0000905ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
906 unsigned numObjects) {
907 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
908 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
909 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000910}
911
Douglas Gregor2b88c112010-09-08 00:15:04 +0000912CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000913 SourceLocation LParenLoc,
914 Expr **Args,
915 unsigned NumArgs,
916 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000917 : Expr(CXXUnresolvedConstructExprClass,
918 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000919 (Type->getType()->isLValueReferenceType() ? VK_LValue
920 :Type->getType()->isRValueReferenceType()? VK_XValue
921 :VK_RValue),
922 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000923 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000924 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +0000925 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +0000926 LParenLoc(LParenLoc),
927 RParenLoc(RParenLoc),
928 NumArgs(NumArgs) {
929 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000930 for (unsigned I = 0; I != NumArgs; ++I) {
931 if (Args[I]->containsUnexpandedParameterPack())
932 ExprBits.ContainsUnexpandedParameterPack = true;
933
934 StoredArgs[I] = Args[I];
935 }
Douglas Gregorce934142009-05-20 18:46:25 +0000936}
937
938CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +0000939CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000940 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000941 SourceLocation LParenLoc,
942 Expr **Args,
943 unsigned NumArgs,
944 SourceLocation RParenLoc) {
945 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
946 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +0000947 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +0000948 Args, NumArgs, RParenLoc);
949}
950
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000951CXXUnresolvedConstructExpr *
952CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
953 Stmt::EmptyShell Empty;
954 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
955 sizeof(Expr *) * NumArgs);
956 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
957}
958
Douglas Gregor2b88c112010-09-08 00:15:04 +0000959SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
960 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
961}
962
John McCall8cd78132009-11-19 22:55:06 +0000963CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +0000964 Expr *Base, QualType BaseType,
965 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +0000966 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000967 NestedNameSpecifierLoc QualifierLoc,
968 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +0000969 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000970 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +0000971 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +0000972 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000973 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000974 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +0000975 (QualifierLoc &&
976 QualifierLoc.getNestedNameSpecifier()
977 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000978 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +0000979 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000980 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +0000981 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +0000982 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000983 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000984 if (TemplateArgs) {
985 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000986 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000987 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000988 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
989 Dependent,
990 InstantiationDependent,
991 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000992 if (ContainsUnexpandedParameterPack)
993 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000994 } else if (TemplateKWLoc.isValid()) {
995 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000996 }
Douglas Gregor308047d2009-09-09 00:23:06 +0000997}
998
Douglas Gregora6e053e2010-12-15 01:34:56 +0000999CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1000 Expr *Base, QualType BaseType,
1001 bool IsArrow,
1002 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001003 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001004 NamedDecl *FirstQualifierFoundInScope,
1005 DeclarationNameInfo MemberNameInfo)
1006 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001007 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001008 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001009 (QualifierLoc &&
1010 QualifierLoc.getNestedNameSpecifier()->
1011 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001012 MemberNameInfo.containsUnexpandedParameterPack())),
1013 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001014 HasTemplateKWAndArgsInfo(false),
1015 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001016 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1017 MemberNameInfo(MemberNameInfo) { }
1018
John McCall8cd78132009-11-19 22:55:06 +00001019CXXDependentScopeMemberExpr *
1020CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001021 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001022 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001023 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001024 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001025 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001026 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001027 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001028 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001029 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1030 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001031 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001032 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001033 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001034
Abramo Bagnara7945c982012-01-27 09:46:47 +00001035 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1036 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1037 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001038
Chris Lattner5c0b4052010-10-30 05:14:06 +00001039 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001040 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1041 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001042 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001043 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001044 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001045 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001046}
1047
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001048CXXDependentScopeMemberExpr *
1049CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001050 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001051 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001052 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001053 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001054 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001055 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001056 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001057
1058 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001059 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001060 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001061 CXXDependentScopeMemberExpr *E
1062 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001063 0, SourceLocation(),
1064 NestedNameSpecifierLoc(),
1065 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001066 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001067 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001068 return E;
1069}
1070
Douglas Gregor0da1d432011-02-28 20:01:57 +00001071bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1072 if (Base == 0)
1073 return true;
1074
Douglas Gregor25b7e052011-03-02 21:06:53 +00001075 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001076}
1077
John McCall0009fcc2011-04-26 20:42:42 +00001078static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1079 UnresolvedSetIterator end) {
1080 do {
1081 NamedDecl *decl = *begin;
1082 if (isa<UnresolvedUsingValueDecl>(decl))
1083 return false;
1084 if (isa<UsingShadowDecl>(decl))
1085 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1086
1087 // Unresolved member expressions should only contain methods and
1088 // method templates.
1089 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1090
1091 if (isa<FunctionTemplateDecl>(decl))
1092 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1093 if (cast<CXXMethodDecl>(decl)->isStatic())
1094 return false;
1095 } while (++begin != end);
1096
1097 return true;
1098}
1099
Douglas Gregora6e053e2010-12-15 01:34:56 +00001100UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001101 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001102 Expr *Base, QualType BaseType,
1103 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001104 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001105 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001106 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001107 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001108 const TemplateArgumentListInfo *TemplateArgs,
1109 UnresolvedSetIterator Begin,
1110 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001111 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1112 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001113 // Dependent
1114 ((Base && Base->isTypeDependent()) ||
1115 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001116 ((Base && Base->isInstantiationDependent()) ||
1117 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001118 // Contains unexpanded parameter pack
1119 ((Base && Base->containsUnexpandedParameterPack()) ||
1120 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001121 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1122 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001123
1124 // Check whether all of the members are non-static member functions,
1125 // and if so, mark give this bound-member type instead of overload type.
1126 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1127 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001128}
1129
Douglas Gregor0da1d432011-02-28 20:01:57 +00001130bool UnresolvedMemberExpr::isImplicitAccess() const {
1131 if (Base == 0)
1132 return true;
1133
Douglas Gregor25b7e052011-03-02 21:06:53 +00001134 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001135}
1136
John McCall10eae182009-11-30 22:42:35 +00001137UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001138UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001139 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001140 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001141 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001142 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001143 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001144 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001145 const TemplateArgumentListInfo *TemplateArgs,
1146 UnresolvedSetIterator Begin,
1147 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001148 std::size_t size = sizeof(UnresolvedMemberExpr);
1149 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001150 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1151 else if (TemplateKWLoc.isValid())
1152 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001153
Chris Lattner5c0b4052010-10-30 05:14:06 +00001154 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001155 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001156 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001157 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001158 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001159}
1160
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001161UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001162UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001163 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001164 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001165 if (HasTemplateKWAndArgsInfo)
1166 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001167
Chris Lattner5c0b4052010-10-30 05:14:06 +00001168 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001169 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001170 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001171 return E;
1172}
1173
John McCall58cc69d2010-01-27 01:50:18 +00001174CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1175 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1176
1177 // If there was a nested name specifier, it names the naming class.
1178 // It can't be dependent: after all, we were actually able to do the
1179 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001180 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001181 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001182 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001183 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001184 Record = T->getAsCXXRecordDecl();
1185 assert(Record && "qualifier in member expression does not name record");
1186 }
John McCall58cc69d2010-01-27 01:50:18 +00001187 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001188 else {
John McCall58cc69d2010-01-27 01:50:18 +00001189 QualType BaseType = getBaseType().getNonReferenceType();
1190 if (isArrow()) {
1191 const PointerType *PT = BaseType->getAs<PointerType>();
1192 assert(PT && "base of arrow member access is not pointer");
1193 BaseType = PT->getPointeeType();
1194 }
1195
Douglas Gregor9262f472010-04-27 18:19:34 +00001196 Record = BaseType->getAsCXXRecordDecl();
1197 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001198 }
1199
Douglas Gregor9262f472010-04-27 18:19:34 +00001200 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001201}
1202
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001203SubstNonTypeTemplateParmPackExpr::
1204SubstNonTypeTemplateParmPackExpr(QualType T,
1205 NonTypeTemplateParmDecl *Param,
1206 SourceLocation NameLoc,
1207 const TemplateArgument &ArgPack)
1208 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001209 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001210 Param(Param), Arguments(ArgPack.pack_begin()),
1211 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1212
1213TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1214 return TemplateArgument(Arguments, NumArguments);
1215}
1216
David Blaikie68e081d2011-12-20 02:48:34 +00001217void ArrayTypeTraitExpr::anchor() { }