blob: 7ef3417e6dc5e0fcaf099e26eda2dc7b90843692 [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 Redlc3a3c602012-02-16 11:35:52 +000048 FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +000049 bool usualArrayDeleteWantsSize,
50 Expr **placementArgs, unsigned numPlaceArgs,
51 SourceRange typeIdParens, Expr *arraySize,
52 InitializationStyle initializationStyle,
53 Expr *initializer, QualType ty,
54 TypeSourceInfo *allocatedTypeInfo,
55 SourceLocation startLoc, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +000056 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +000057 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +000058 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +000059 ty->containsUnexpandedParameterPack()),
Sebastian Redl6047f072012-02-16 12:22:20 +000060 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize),
61 SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
62 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
63 StartLoc(startLoc), DirectInitRange(directInitRange) {
64 assert((initializer != 0 || initializationStyle == NoInit) &&
65 "Only NoInit can have no initializer.");
66 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
67 AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
Sebastian Redlbd150f42008-11-21 19:14:01 +000068 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +000069 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000070 if (arraySize->isInstantiationDependent())
71 ExprBits.InstantiationDependent = true;
72
Douglas Gregora6e053e2010-12-15 01:34:56 +000073 if (arraySize->containsUnexpandedParameterPack())
74 ExprBits.ContainsUnexpandedParameterPack = true;
75
Sebastian Redl351bb782008-12-02 14:43:59 +000076 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +000077 }
78
Sebastian Redl6047f072012-02-16 12:22:20 +000079 if (initializer) {
80 if (initializer->isInstantiationDependent())
81 ExprBits.InstantiationDependent = true;
82
83 if (initializer->containsUnexpandedParameterPack())
84 ExprBits.ContainsUnexpandedParameterPack = true;
85
86 SubExprs[i++] = initializer;
87 }
88
Douglas Gregora6e053e2010-12-15 01:34:56 +000089 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +000090 if (placementArgs[j]->isInstantiationDependent())
91 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +000092 if (placementArgs[j]->containsUnexpandedParameterPack())
93 ExprBits.ContainsUnexpandedParameterPack = true;
94
Sebastian Redlbd150f42008-11-21 19:14:01 +000095 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +000096 }
Sebastian Redlbd150f42008-11-21 19:14:01 +000097}
98
Chris Lattnerabfb58d2010-05-10 01:22:27 +000099void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000100 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000101 assert(SubExprs == 0 && "SubExprs already allocated");
102 Array = isArray;
103 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000104
105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000106 SubExprs = new (C) Stmt*[TotalSize];
107}
108
Sebastian Redl31ad7542011-03-13 17:09:40 +0000109bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000110 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000111 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000112}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000113
Sebastian Redl6047f072012-02-16 12:22:20 +0000114SourceLocation CXXNewExpr::getEndLoc() const {
115 switch (getInitializationStyle()) {
116 case NoInit:
117 return AllocatedTypeInfo->getTypeLoc().getEndLoc();
118 case CallInit:
119 return DirectInitRange.getEnd();
120 case ListInit:
121 return getInitializer()->getSourceRange().getEnd();
122 }
Matt Beaumont-Gay2150d382012-02-16 22:15:50 +0000123 llvm_unreachable("bogus initialization style");
Sebastian Redl6047f072012-02-16 12:22:20 +0000124}
125
Sebastian Redlbd150f42008-11-21 19:14:01 +0000126// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000127QualType CXXDeleteExpr::getDestroyedType() const {
128 const Expr *Arg = getArgument();
129 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
130 if (ICE->getCastKind() != CK_UserDefinedConversion &&
131 ICE->getType()->isVoidPointerType())
132 Arg = ICE->getSubExpr();
133 else
134 break;
135 }
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000136 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000137 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000138
139 if (ArgType->isDependentType() && !ArgType->isPointerType())
140 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000141
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000142 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000143}
144
Douglas Gregorad8a3362009-09-04 17:36:40 +0000145// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000146PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
147 : Type(Info)
148{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000150}
151
John McCalldb40c7f2010-12-14 08:05:40 +0000152CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000153 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
154 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
155 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
156 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000157 : Expr(CXXPseudoDestructorExprClass,
158 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
159 FunctionProtoType::ExtProtoInfo())),
160 VK_RValue, OK_Ordinary,
161 /*isTypeDependent=*/(Base->isTypeDependent() ||
162 (DestroyedType.getTypeSourceInfo() &&
163 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000164 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000165 (Base->isInstantiationDependent() ||
166 (QualifierLoc &&
167 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
168 (ScopeType &&
169 ScopeType->getType()->isInstantiationDependentType()) ||
170 (DestroyedType.getTypeSourceInfo() &&
171 DestroyedType.getTypeSourceInfo()->getType()
172 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000173 // ContainsUnexpandedParameterPack
174 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()
177 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000178 (ScopeType &&
179 ScopeType->getType()->containsUnexpandedParameterPack()) ||
180 (DestroyedType.getTypeSourceInfo() &&
181 DestroyedType.getTypeSourceInfo()->getType()
182 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
186 DestroyedType(DestroyedType) { }
187
Douglas Gregor678f90d2010-02-25 01:56:36 +0000188QualType CXXPseudoDestructorExpr::getDestroyedType() const {
189 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
190 return TInfo->getType();
191
192 return QualType();
193}
194
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000200}
201
202
John McCalld14a8642009-11-21 08:51:07 +0000203// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000204UnresolvedLookupExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +0000205UnresolvedLookupExpr::Create(ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000206 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000207 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000208 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000209 const DeclarationNameInfo &NameInfo,
210 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000211 const TemplateArgumentListInfo *Args,
212 UnresolvedSetIterator Begin,
213 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000214{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000215 assert(Args || TemplateKWLoc.isValid());
216 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000217 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000218 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000219 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
220 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000221 ADL, /*Overload*/ true, Args,
Richard Smith02e85f32011-04-14 22:09:26 +0000222 Begin, End, /*StdIsAssociated=*/false);
John McCalle66edc12009-11-24 19:00:30 +0000223}
224
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000225UnresolvedLookupExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +0000226UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
227 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000228 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000229 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000230 if (HasTemplateKWAndArgsInfo)
231 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000232
Chris Lattner5c0b4052010-10-30 05:14:06 +0000233 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000234 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000235 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000236 return E;
237}
238
Douglas Gregora6e053e2010-12-15 01:34:56 +0000239OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000240 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000241 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000242 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000243 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000244 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000245 UnresolvedSetIterator End,
246 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000247 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248 bool KnownContainsUnexpandedParameterPack)
249 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
250 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000251 (KnownInstantiationDependent ||
252 NameInfo.isInstantiationDependent() ||
253 (QualifierLoc &&
254 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000255 (KnownContainsUnexpandedParameterPack ||
256 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000257 (QualifierLoc &&
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()))),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000260 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
261 QualifierLoc(QualifierLoc),
262 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregorc69978f2010-05-23 19:36:40 +0000263{
Douglas Gregora6e053e2010-12-15 01:34:56 +0000264 NumResults = End - Begin;
265 if (NumResults) {
266 // Determine whether this expression is type-dependent.
267 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
268 if ((*I)->getDeclContext()->isDependentContext() ||
269 isa<UnresolvedUsingValueDecl>(*I)) {
270 ExprBits.TypeDependent = true;
271 ExprBits.ValueDependent = true;
272 }
273 }
274
275 Results = static_cast<DeclAccessPair *>(
276 C.Allocate(sizeof(DeclAccessPair) * NumResults,
277 llvm::alignOf<DeclAccessPair>()));
278 memcpy(Results, &*Begin.getIterator(),
279 NumResults * sizeof(DeclAccessPair));
280 }
281
282 // If we have explicit template arguments, check for dependent
283 // template arguments and whether they contain any unexpanded pack
284 // expansions.
285 if (TemplateArgs) {
286 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000287 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000288 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000289 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
290 Dependent,
291 InstantiationDependent,
292 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000293
294 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000295 ExprBits.TypeDependent = true;
296 ExprBits.ValueDependent = true;
297 }
298 if (InstantiationDependent)
299 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000300 if (ContainsUnexpandedParameterPack)
301 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000302 } else if (TemplateKWLoc.isValid()) {
303 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000304 }
305
306 if (isTypeDependent())
307 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000308}
309
310void OverloadExpr::initializeResults(ASTContext &C,
311 UnresolvedSetIterator Begin,
312 UnresolvedSetIterator End) {
313 assert(Results == 0 && "Results already initialized!");
314 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000315 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000316 Results = static_cast<DeclAccessPair *>(
317 C.Allocate(sizeof(DeclAccessPair) * NumResults,
318
319 llvm::alignOf<DeclAccessPair>()));
320 memcpy(Results, &*Begin.getIterator(),
321 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000322 }
323}
324
John McCall8c12dc42010-04-22 18:44:12 +0000325CXXRecordDecl *OverloadExpr::getNamingClass() const {
326 if (isa<UnresolvedLookupExpr>(this))
327 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
328 else
329 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
330}
331
John McCall8cd78132009-11-19 22:55:06 +0000332// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000333DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000334 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000335 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000336 const DeclarationNameInfo &NameInfo,
337 const TemplateArgumentListInfo *Args)
338 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
339 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000340 (NameInfo.isInstantiationDependent() ||
341 (QualifierLoc &&
342 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000343 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000344 (QualifierLoc &&
345 QualifierLoc.getNestedNameSpecifier()
346 ->containsUnexpandedParameterPack()))),
347 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000348 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349{
350 if (Args) {
351 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000352 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000353 bool ContainsUnexpandedParameterPack
354 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000355 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
356 Dependent,
357 InstantiationDependent,
358 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000360 } else if (TemplateKWLoc.isValid()) {
361 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000362 }
363}
364
John McCalle66edc12009-11-24 19:00:30 +0000365DependentScopeDeclRefExpr *
366DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000368 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000369 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *Args) {
371 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000372 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000373 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
374 else if (TemplateKWLoc.isValid())
375 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000376 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000377 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
378 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000379}
380
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000381DependentScopeDeclRefExpr *
382DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000383 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000384 unsigned NumTemplateArgs) {
385 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000386 if (HasTemplateKWAndArgsInfo)
387 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000388 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000389 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000390 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000391 SourceLocation(),
Douglas Gregor87866ce2011-02-04 12:01:24 +0000392 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000393 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000394 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000395}
396
Chandler Carruth01718152010-10-25 08:47:36 +0000397SourceRange CXXConstructExpr::getSourceRange() const {
John McCall701417a2011-02-21 06:23:05 +0000398 if (isa<CXXTemporaryObjectExpr>(this))
399 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
400
Douglas Gregor15417cf2010-11-03 00:35:38 +0000401 if (ParenRange.isValid())
402 return SourceRange(Loc, ParenRange.getEnd());
403
404 SourceLocation End = Loc;
405 for (unsigned I = getNumArgs(); I > 0; --I) {
406 const Expr *Arg = getArg(I-1);
407 if (!Arg->isDefaultArgument()) {
408 SourceLocation NewEnd = Arg->getLocEnd();
409 if (NewEnd.isValid()) {
410 End = NewEnd;
411 break;
412 }
413 }
414 }
415
416 return SourceRange(Loc, End);
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000417}
418
Douglas Gregor993603d2008-11-14 16:09:21 +0000419SourceRange CXXOperatorCallExpr::getSourceRange() const {
420 OverloadedOperatorKind Kind = getOperator();
421 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
422 if (getNumArgs() == 1)
423 // Prefix operator
Mike Stump11289f42009-09-09 15:08:12 +0000424 return SourceRange(getOperatorLoc(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000425 getArg(0)->getSourceRange().getEnd());
426 else
427 // Postfix operator
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000428 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregor993603d2008-11-14 16:09:21 +0000429 getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000430 } else if (Kind == OO_Arrow) {
431 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000432 } else if (Kind == OO_Call) {
433 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
434 } else if (Kind == OO_Subscript) {
435 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
436 } else if (getNumArgs() == 1) {
437 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
438 } else if (getNumArgs() == 2) {
439 return SourceRange(getArg(0)->getSourceRange().getBegin(),
440 getArg(1)->getSourceRange().getEnd());
441 } else {
442 return SourceRange();
443 }
444}
445
Ted Kremenek98a24e32011-03-30 17:41:19 +0000446Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
447 if (const MemberExpr *MemExpr =
448 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000449 return MemExpr->getBase();
450
451 // FIXME: Will eventually need to cope with member pointers.
452 return 0;
453}
454
Ted Kremenek98a24e32011-03-30 17:41:19 +0000455CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
456 if (const MemberExpr *MemExpr =
457 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
458 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
459
460 // FIXME: Will eventually need to cope with member pointers.
461 return 0;
462}
463
464
Chandler Carruth00426b42010-10-27 06:55:41 +0000465CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
466 Expr* ThisArg = getImplicitObjectArgument();
467 if (!ThisArg)
468 return 0;
469
470 if (ThisArg->getType()->isAnyPointerType())
471 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
472
473 return ThisArg->getType()->getAsCXXRecordDecl();
474}
475
Douglas Gregoref986e82009-11-12 15:31:47 +0000476
Douglas Gregore200adc2008-10-27 19:41:14 +0000477//===----------------------------------------------------------------------===//
478// Named casts
479//===----------------------------------------------------------------------===//
480
481/// getCastName - Get the name of the C++ cast being used, e.g.,
482/// "static_cast", "dynamic_cast", "reinterpret_cast", or
483/// "const_cast". The returned pointer must not be freed.
484const char *CXXNamedCastExpr::getCastName() const {
485 switch (getStmtClass()) {
486 case CXXStaticCastExprClass: return "static_cast";
487 case CXXDynamicCastExprClass: return "dynamic_cast";
488 case CXXReinterpretCastExprClass: return "reinterpret_cast";
489 case CXXConstCastExprClass: return "const_cast";
490 default: return "<invalid cast>";
491 }
492}
Douglas Gregordd04d332009-01-16 18:33:17 +0000493
John McCallcf142162010-08-07 06:22:56 +0000494CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000495 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000496 CastKind K, Expr *Op,
497 const CXXCastPath *BasePath,
498 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000499 SourceLocation L,
500 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000501 unsigned PathSize = (BasePath ? BasePath->size() : 0);
502 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
503 + PathSize * sizeof(CXXBaseSpecifier*));
504 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000505 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
506 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000507 if (PathSize) E->setCastPath(*BasePath);
508 return E;
509}
510
511CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
512 unsigned PathSize) {
513 void *Buffer =
514 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
515 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
516}
517
518CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000519 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000520 CastKind K, Expr *Op,
521 const CXXCastPath *BasePath,
522 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000523 SourceLocation L,
524 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000525 unsigned PathSize = (BasePath ? BasePath->size() : 0);
526 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
527 + PathSize * sizeof(CXXBaseSpecifier*));
528 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000529 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
530 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000531 if (PathSize) E->setCastPath(*BasePath);
532 return E;
533}
534
535CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
536 unsigned PathSize) {
537 void *Buffer =
538 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
539 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
540}
541
Anders Carlsson267c0c92011-04-11 01:43:55 +0000542/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
543/// to always be null. For example:
544///
545/// struct A { };
546/// struct B final : A { };
547/// struct C { };
548///
549/// C *f(B* b) { return dynamic_cast<C*>(b); }
550bool CXXDynamicCastExpr::isAlwaysNull() const
551{
552 QualType SrcType = getSubExpr()->getType();
553 QualType DestType = getType();
554
555 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
556 SrcType = SrcPTy->getPointeeType();
557 DestType = DestType->castAs<PointerType>()->getPointeeType();
558 }
559
560 const CXXRecordDecl *SrcRD =
561 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
562
563 if (!SrcRD->hasAttr<FinalAttr>())
564 return false;
565
566 const CXXRecordDecl *DestRD =
567 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
568
569 return !DestRD->isDerivedFrom(SrcRD);
570}
571
John McCallcf142162010-08-07 06:22:56 +0000572CXXReinterpretCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000573CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
574 CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000575 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000576 TypeSourceInfo *WrittenTy, SourceLocation L,
577 SourceLocation RParenLoc) {
John McCallcf142162010-08-07 06:22:56 +0000578 unsigned PathSize = (BasePath ? BasePath->size() : 0);
579 void *Buffer =
580 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
581 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000582 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
583 RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000584 if (PathSize) E->setCastPath(*BasePath);
585 return E;
586}
587
588CXXReinterpretCastExpr *
589CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
590 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
591 + PathSize * sizeof(CXXBaseSpecifier*));
592 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
593}
594
John McCall7decc9e2010-11-18 06:31:45 +0000595CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
596 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000597 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000598 SourceLocation L,
599 SourceLocation RParenLoc) {
600 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallcf142162010-08-07 06:22:56 +0000601}
602
603CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
604 return new (C) CXXConstCastExpr(EmptyShell());
605}
606
607CXXFunctionalCastExpr *
John McCall7decc9e2010-11-18 06:31:45 +0000608CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000609 TypeSourceInfo *Written, SourceLocation L,
610 CastKind K, Expr *Op, const CXXCastPath *BasePath,
611 SourceLocation R) {
612 unsigned PathSize = (BasePath ? BasePath->size() : 0);
613 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
614 + PathSize * sizeof(CXXBaseSpecifier*));
615 CXXFunctionalCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +0000616 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallcf142162010-08-07 06:22:56 +0000617 if (PathSize) E->setCastPath(*BasePath);
618 return E;
619}
620
621CXXFunctionalCastExpr *
622CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
623 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
624 + PathSize * sizeof(CXXBaseSpecifier*));
625 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
626}
627
628
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000629CXXDefaultArgExpr *
Douglas Gregor033f6752009-12-23 23:03:06 +0000630CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
631 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000632 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000633 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
634 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000635}
636
Mike Stump11289f42009-09-09 15:08:12 +0000637CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000638 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000639 return new (C) CXXTemporary(Destructor);
640}
641
Mike Stump11289f42009-09-09 15:08:12 +0000642CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000643 CXXTemporary *Temp,
644 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000645 assert((SubExpr->getType()->isRecordType() ||
646 SubExpr->getType()->isArrayType()) &&
647 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000648
Douglas Gregora6e053e2010-12-15 01:34:56 +0000649 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000650}
651
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000652CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000653 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000654 TypeSourceInfo *Type,
Douglas Gregordd04d332009-01-16 18:33:17 +0000655 Expr **Args,
Mike Stump11289f42009-09-09 15:08:12 +0000656 unsigned NumArgs,
Chandler Carruth01718152010-10-25 08:47:36 +0000657 SourceRange parenRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000658 bool HadMultipleCandidates,
Douglas Gregor199db362010-04-27 20:36:09 +0000659 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000660 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
661 Type->getType().getNonReferenceType(),
662 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000663 Cons, false, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000664 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000665 CXXConstructExpr::CK_Complete, parenRange),
666 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000667}
668
669SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth01718152010-10-25 08:47:36 +0000670 return SourceRange(Type->getTypeLoc().getBeginLoc(),
671 getParenRange().getEnd());
Douglas Gregordd04d332009-01-16 18:33:17 +0000672}
Anders Carlsson6f287832009-04-21 02:22:11 +0000673
Mike Stump11289f42009-09-09 15:08:12 +0000674CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000675 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000676 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000677 Expr **Args, unsigned NumArgs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000678 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000679 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000680 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000681 ConstructionKind ConstructKind,
682 SourceRange ParenRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000683 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000684 Elidable, Args, NumArgs,
Sebastian Redla9351792012-02-11 23:51:47 +0000685 HadMultipleCandidates, ListInitialization,
686 ZeroInitialization, ConstructKind,
687 ParenRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000688}
689
Mike Stump11289f42009-09-09 15:08:12 +0000690CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000691 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000692 CXXConstructorDecl *D, bool elidable,
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000693 Expr **args, unsigned numargs,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000694 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000695 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000696 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000697 ConstructionKind ConstructKind,
698 SourceRange ParenRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000699 : Expr(SC, T, VK_RValue, OK_Ordinary,
700 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000701 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000702 T->containsUnexpandedParameterPack()),
Douglas Gregor488c2312011-09-26 14:47:03 +0000703 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000704 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000705 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000706 ZeroInitialization(ZeroInitialization),
Douglas Gregor488c2312011-09-26 14:47:03 +0000707 ConstructKind(ConstructKind), Args(0)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000708{
709 if (NumArgs) {
710 Args = new (C) Stmt*[NumArgs];
711
712 for (unsigned i = 0; i != NumArgs; ++i) {
713 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000714
715 if (args[i]->isValueDependent())
716 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000717 if (args[i]->isInstantiationDependent())
718 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000719 if (args[i]->containsUnexpandedParameterPack())
720 ExprBits.ContainsUnexpandedParameterPack = true;
721
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000722 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000723 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000724 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000725}
726
Douglas Gregore31e6062012-02-07 10:09:13 +0000727LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
728 LambdaCaptureKind Kind, VarDecl *Var,
729 SourceLocation EllipsisLoc)
730 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
731{
732 unsigned Bits = 0;
733 if (Implicit)
734 Bits |= Capture_Implicit;
735
736 switch (Kind) {
737 case LCK_This:
738 assert(Var == 0 && "'this' capture cannot have a variable!");
739 break;
740
741 case LCK_ByCopy:
742 Bits |= Capture_ByCopy;
743 // Fall through
744 case LCK_ByRef:
745 assert(Var && "capture must have a variable!");
746 break;
747 }
748 VarAndBits.setInt(Bits);
749}
750
751LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
752 if (capturesThis())
753 return LCK_This;
754
755 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
756}
757
758LambdaExpr::LambdaExpr(QualType T,
759 SourceRange IntroducerRange,
760 LambdaCaptureDefault CaptureDefault,
761 ArrayRef<Capture> Captures,
762 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000763 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000764 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000765 ArrayRef<VarDecl *> ArrayIndexVars,
766 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor63798542012-02-20 19:44:39 +0000767 SourceLocation ClosingBrace,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000768 unsigned ManglingNumber,
769 Decl *ContextDecl)
Douglas Gregore31e6062012-02-07 10:09:13 +0000770 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
771 T->isDependentType(), T->isDependentType(), T->isDependentType(),
772 /*ContainsUnexpandedParameterPack=*/false),
773 IntroducerRange(IntroducerRange),
Douglas Gregore5561632012-02-13 17:20:40 +0000774 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000775 CaptureDefault(CaptureDefault),
776 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000777 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000778 ClosingBrace(ClosingBrace)
779{
780 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000781 CXXRecordDecl *Class = getLambdaClass();
782 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000783
784 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000785
786 // Copy captures.
787 ASTContext &Context = Class->getASTContext();
788 Data.NumCaptures = NumCaptures;
789 Data.NumExplicitCaptures = 0;
Douglas Gregor63798542012-02-20 19:44:39 +0000790 Data.ManglingNumber = ManglingNumber;
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000791 Data.ContextDecl = ContextDecl;
Douglas Gregore5561632012-02-13 17:20:40 +0000792 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
793 Capture *ToCapture = Data.Captures;
794 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
795 if (Captures[I].isExplicit())
796 ++Data.NumExplicitCaptures;
797
798 *ToCapture++ = Captures[I];
799 }
800
801 // Copy initialization expressions for the non-static data members.
802 Stmt **Stored = getStoredStmts();
803 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
804 *Stored++ = CaptureInits[I];
805
806 // Copy the body of the lambda.
807 *Stored++ = getCallOperator()->getBody();
808
809 // Copy the array index variables, if any.
810 HasArrayIndexVars = !ArrayIndexVars.empty();
811 if (HasArrayIndexVars) {
812 assert(ArrayIndexStarts.size() == NumCaptures);
813 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
814 sizeof(VarDecl *) * ArrayIndexVars.size());
815 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
816 sizeof(unsigned) * Captures.size());
817 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000818 }
819
820 if (ManglingNumber)
821 Class->ClearLinkageCache();
Douglas Gregore31e6062012-02-07 10:09:13 +0000822}
823
824LambdaExpr *LambdaExpr::Create(ASTContext &Context,
825 CXXRecordDecl *Class,
826 SourceRange IntroducerRange,
827 LambdaCaptureDefault CaptureDefault,
828 ArrayRef<Capture> Captures,
829 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000830 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000831 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000832 ArrayRef<VarDecl *> ArrayIndexVars,
833 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor63798542012-02-20 19:44:39 +0000834 SourceLocation ClosingBrace,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000835 unsigned ManglingNumber,
836 Decl *ContextDecl) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000837 // Determine the type of the expression (i.e., the type of the
838 // function object we're creating).
839 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000840
Douglas Gregore5561632012-02-13 17:20:40 +0000841 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
842 if (!ArrayIndexVars.empty())
843 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
844 + sizeof(unsigned) * (Captures.size() + 1);
845 void *Mem = Context.Allocate(Size);
846 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000847 Captures, ExplicitParams, ExplicitResultType,
848 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregor7fcbd902012-02-21 00:37:24 +0000849 ClosingBrace, ManglingNumber, ContextDecl);
Douglas Gregore31e6062012-02-07 10:09:13 +0000850}
851
Douglas Gregor99ae8062012-02-14 17:54:36 +0000852LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
853 unsigned NumArrayIndexVars) {
854 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
855 if (NumArrayIndexVars)
856 Size += sizeof(VarDecl) * NumArrayIndexVars
857 + sizeof(unsigned) * (NumCaptures + 1);
858 void *Mem = C.Allocate(Size);
859 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
860}
861
Douglas Gregorc8a73492012-02-13 15:44:47 +0000862LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000863 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000864}
865
866LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000867 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000868}
869
870LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
871 return capture_begin();
872}
873
874LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
875 struct CXXRecordDecl::LambdaDefinitionData &Data
876 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000877 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000878}
879
880LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
881 return explicit_capture_end();
882}
883
884LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
885 return capture_end();
886}
887
Douglas Gregor54fcea62012-02-13 16:35:30 +0000888ArrayRef<VarDecl *>
889LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +0000890 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +0000891
892 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +0000893 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
894 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +0000895 VarDecl **IndexVars = getArrayIndexVars();
896 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +0000897 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
898 IndexVars + IndexStarts[Index + 1]);
899}
900
Douglas Gregore31e6062012-02-07 10:09:13 +0000901CXXRecordDecl *LambdaExpr::getLambdaClass() const {
902 return getType()->getAsCXXRecordDecl();
903}
904
905CXXMethodDecl *LambdaExpr::getCallOperator() const {
906 CXXRecordDecl *Record = getLambdaClass();
907 DeclarationName Name
908 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
909 DeclContext::lookup_result Calls = Record->lookup(Name);
910 assert(Calls.first != Calls.second && "Missing lambda call operator!");
911 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
912 assert(Calls.first == Calls.second && "More than lambda one call operator?");
913 return Result;
914}
915
Douglas Gregor99ae8062012-02-14 17:54:36 +0000916CompoundStmt *LambdaExpr::getBody() const {
917 if (!getStoredStmts()[NumCaptures])
918 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
919
920 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
921}
922
Douglas Gregore31e6062012-02-07 10:09:13 +0000923bool LambdaExpr::isMutable() const {
924 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
925}
926
John McCall28fc7092011-11-10 05:35:25 +0000927ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
928 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +0000929 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +0000930 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000931 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000932 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000933 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +0000934 SubExpr(subexpr) {
935 ExprWithCleanupsBits.NumObjects = objects.size();
936 for (unsigned i = 0, e = objects.size(); i != e; ++i)
937 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +0000938}
939
John McCall28fc7092011-11-10 05:35:25 +0000940ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
941 ArrayRef<CleanupObject> objects) {
942 size_t size = sizeof(ExprWithCleanups)
943 + objects.size() * sizeof(CleanupObject);
944 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
945 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +0000946}
947
John McCall28fc7092011-11-10 05:35:25 +0000948ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
949 : Expr(ExprWithCleanupsClass, empty) {
950 ExprWithCleanupsBits.NumObjects = numObjects;
951}
Chris Lattnercba86142010-05-10 00:25:06 +0000952
John McCall28fc7092011-11-10 05:35:25 +0000953ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
954 unsigned numObjects) {
955 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
956 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
957 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +0000958}
959
Douglas Gregor2b88c112010-09-08 00:15:04 +0000960CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000961 SourceLocation LParenLoc,
962 Expr **Args,
963 unsigned NumArgs,
964 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000965 : Expr(CXXUnresolvedConstructExprClass,
966 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +0000967 (Type->getType()->isLValueReferenceType() ? VK_LValue
968 :Type->getType()->isRValueReferenceType()? VK_XValue
969 :VK_RValue),
970 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000971 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000972 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +0000973 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +0000974 LParenLoc(LParenLoc),
975 RParenLoc(RParenLoc),
976 NumArgs(NumArgs) {
977 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000978 for (unsigned I = 0; I != NumArgs; ++I) {
979 if (Args[I]->containsUnexpandedParameterPack())
980 ExprBits.ContainsUnexpandedParameterPack = true;
981
982 StoredArgs[I] = Args[I];
983 }
Douglas Gregorce934142009-05-20 18:46:25 +0000984}
985
986CXXUnresolvedConstructExpr *
Mike Stump11289f42009-09-09 15:08:12 +0000987CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000988 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +0000989 SourceLocation LParenLoc,
990 Expr **Args,
991 unsigned NumArgs,
992 SourceLocation RParenLoc) {
993 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
994 sizeof(Expr *) * NumArgs);
Douglas Gregor2b88c112010-09-08 00:15:04 +0000995 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregorce934142009-05-20 18:46:25 +0000996 Args, NumArgs, RParenLoc);
997}
998
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000999CXXUnresolvedConstructExpr *
1000CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
1001 Stmt::EmptyShell Empty;
1002 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1003 sizeof(Expr *) * NumArgs);
1004 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1005}
1006
Douglas Gregor2b88c112010-09-08 00:15:04 +00001007SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1008 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1009}
1010
John McCall8cd78132009-11-19 22:55:06 +00001011CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001012 Expr *Base, QualType BaseType,
1013 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001014 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001015 NestedNameSpecifierLoc QualifierLoc,
1016 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001017 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001018 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001019 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001020 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001021 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001022 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001023 (QualifierLoc &&
1024 QualifierLoc.getNestedNameSpecifier()
1025 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001026 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001027 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001028 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001029 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001030 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001031 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001032 if (TemplateArgs) {
1033 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001034 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001035 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001036 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1037 Dependent,
1038 InstantiationDependent,
1039 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001040 if (ContainsUnexpandedParameterPack)
1041 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001042 } else if (TemplateKWLoc.isValid()) {
1043 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001044 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001045}
1046
Douglas Gregora6e053e2010-12-15 01:34:56 +00001047CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1048 Expr *Base, QualType BaseType,
1049 bool IsArrow,
1050 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001051 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001052 NamedDecl *FirstQualifierFoundInScope,
1053 DeclarationNameInfo MemberNameInfo)
1054 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001055 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001056 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001057 (QualifierLoc &&
1058 QualifierLoc.getNestedNameSpecifier()->
1059 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001060 MemberNameInfo.containsUnexpandedParameterPack())),
1061 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001062 HasTemplateKWAndArgsInfo(false),
1063 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001064 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1065 MemberNameInfo(MemberNameInfo) { }
1066
John McCall8cd78132009-11-19 22:55:06 +00001067CXXDependentScopeMemberExpr *
1068CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001069 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001070 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001071 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001072 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001073 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001074 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001075 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001076 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001077 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1078 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001079 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001080 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001081 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001082
Abramo Bagnara7945c982012-01-27 09:46:47 +00001083 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1084 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1085 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001086
Chris Lattner5c0b4052010-10-30 05:14:06 +00001087 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001088 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1089 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001090 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001091 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001092 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001093 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001094}
1095
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001096CXXDependentScopeMemberExpr *
1097CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001098 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001099 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001100 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001101 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001102 0, SourceLocation(),
Douglas Gregore16af532011-02-28 18:50:33 +00001103 NestedNameSpecifierLoc(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001104 DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001105
1106 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001107 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001108 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001109 CXXDependentScopeMemberExpr *E
1110 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001111 0, SourceLocation(),
1112 NestedNameSpecifierLoc(),
1113 SourceLocation(), 0,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001114 DeclarationNameInfo(), 0);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001115 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001116 return E;
1117}
1118
Douglas Gregor0da1d432011-02-28 20:01:57 +00001119bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1120 if (Base == 0)
1121 return true;
1122
Douglas Gregor25b7e052011-03-02 21:06:53 +00001123 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001124}
1125
John McCall0009fcc2011-04-26 20:42:42 +00001126static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1127 UnresolvedSetIterator end) {
1128 do {
1129 NamedDecl *decl = *begin;
1130 if (isa<UnresolvedUsingValueDecl>(decl))
1131 return false;
1132 if (isa<UsingShadowDecl>(decl))
1133 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1134
1135 // Unresolved member expressions should only contain methods and
1136 // method templates.
1137 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1138
1139 if (isa<FunctionTemplateDecl>(decl))
1140 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1141 if (cast<CXXMethodDecl>(decl)->isStatic())
1142 return false;
1143 } while (++begin != end);
1144
1145 return true;
1146}
1147
Douglas Gregora6e053e2010-12-15 01:34:56 +00001148UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001149 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001150 Expr *Base, QualType BaseType,
1151 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001152 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001153 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001154 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001155 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001156 const TemplateArgumentListInfo *TemplateArgs,
1157 UnresolvedSetIterator Begin,
1158 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001159 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1160 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001161 // Dependent
1162 ((Base && Base->isTypeDependent()) ||
1163 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001164 ((Base && Base->isInstantiationDependent()) ||
1165 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001166 // Contains unexpanded parameter pack
1167 ((Base && Base->containsUnexpandedParameterPack()) ||
1168 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001169 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1170 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001171
1172 // Check whether all of the members are non-static member functions,
1173 // and if so, mark give this bound-member type instead of overload type.
1174 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1175 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001176}
1177
Douglas Gregor0da1d432011-02-28 20:01:57 +00001178bool UnresolvedMemberExpr::isImplicitAccess() const {
1179 if (Base == 0)
1180 return true;
1181
Douglas Gregor25b7e052011-03-02 21:06:53 +00001182 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001183}
1184
John McCall10eae182009-11-30 22:42:35 +00001185UnresolvedMemberExpr *
Douglas Gregora6e053e2010-12-15 01:34:56 +00001186UnresolvedMemberExpr::Create(ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001187 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001188 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001189 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001190 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001191 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001192 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001193 const TemplateArgumentListInfo *TemplateArgs,
1194 UnresolvedSetIterator Begin,
1195 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001196 std::size_t size = sizeof(UnresolvedMemberExpr);
1197 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001198 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1199 else if (TemplateKWLoc.isValid())
1200 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001201
Chris Lattner5c0b4052010-10-30 05:14:06 +00001202 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001203 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001204 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001205 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001206 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001207}
1208
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001209UnresolvedMemberExpr *
Abramo Bagnara7945c982012-01-27 09:46:47 +00001210UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001211 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001212 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001213 if (HasTemplateKWAndArgsInfo)
1214 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001215
Chris Lattner5c0b4052010-10-30 05:14:06 +00001216 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001217 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001218 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001219 return E;
1220}
1221
John McCall58cc69d2010-01-27 01:50:18 +00001222CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1223 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1224
1225 // If there was a nested name specifier, it names the naming class.
1226 // It can't be dependent: after all, we were actually able to do the
1227 // lookup.
Douglas Gregor9262f472010-04-27 18:19:34 +00001228 CXXRecordDecl *Record = 0;
John McCall1acbbb52010-02-02 06:20:04 +00001229 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001230 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001231 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001232 Record = T->getAsCXXRecordDecl();
1233 assert(Record && "qualifier in member expression does not name record");
1234 }
John McCall58cc69d2010-01-27 01:50:18 +00001235 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001236 else {
John McCall58cc69d2010-01-27 01:50:18 +00001237 QualType BaseType = getBaseType().getNonReferenceType();
1238 if (isArrow()) {
1239 const PointerType *PT = BaseType->getAs<PointerType>();
1240 assert(PT && "base of arrow member access is not pointer");
1241 BaseType = PT->getPointeeType();
1242 }
1243
Douglas Gregor9262f472010-04-27 18:19:34 +00001244 Record = BaseType->getAsCXXRecordDecl();
1245 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001246 }
1247
Douglas Gregor9262f472010-04-27 18:19:34 +00001248 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001249}
1250
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001251SubstNonTypeTemplateParmPackExpr::
1252SubstNonTypeTemplateParmPackExpr(QualType T,
1253 NonTypeTemplateParmDecl *Param,
1254 SourceLocation NameLoc,
1255 const TemplateArgument &ArgPack)
1256 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001257 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001258 Param(Param), Arguments(ArgPack.pack_begin()),
1259 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1260
1261TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1262 return TemplateArgument(Arguments, NumArguments);
1263}
1264
David Blaikie68e081d2011-12-20 02:48:34 +00001265void ArrayTypeTraitExpr::anchor() { }