blob: cbc5950f8350a03e3970c03e482a954eae7d5de0 [file] [log] [blame]
Ted Kremeneka758d092007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneka758d092007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregorb4609802008-11-14 16:09:21 +000014#include "clang/Basic/IdentifierTable.h"
15#include "clang/AST/DeclCXX.h"
Douglas Gregoredce4dd2009-06-30 22:34:41 +000016#include "clang/AST/DeclTemplate.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000017#include "clang/AST/ExprCXX.h"
Douglas Gregor26d4ac92010-02-24 23:40:28 +000018#include "clang/AST/TypeLoc.h"
Ted Kremeneka758d092007-08-24 20:21:10 +000019using namespace clang;
20
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000021
Ted Kremeneka758d092007-08-24 20:21:10 +000022//===----------------------------------------------------------------------===//
23// Child Iterators for iterating over subexpressions/substatements
24//===----------------------------------------------------------------------===//
25
Douglas Gregor57fdc8a2010-04-26 22:37:10 +000026QualType CXXTypeidExpr::getTypeOperand() const {
27 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
28 return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
29 .getUnqualifiedType();
30}
31
Francois Pichet01b7c302010-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 Gregored8abf12010-07-08 06:14:04 +000038// CXXScalarValueInitExpr
Douglas Gregorab6677e2010-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 Redl4c5d3202008-11-21 19:14:01 +000046// CXXNewExpr
Ted Kremenekad7fe862010-02-11 22:51:03 +000047CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
Sebastian Redl1548d142012-02-16 11:35:52 +000048 FunctionDecl *operatorDelete,
Sebastian Redl2aed8b82012-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 McCallf89e55a2010-11-18 06:31:45 +000056 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000057 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +000058 ty->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000059 ty->containsUnexpandedParameterPack()),
Sebastian Redl2aed8b82012-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 Redl4c5d3202008-11-21 19:14:01 +000068 unsigned i = 0;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000069 if (Array) {
Douglas Gregor561f8122011-07-01 01:22:09 +000070 if (arraySize->isInstantiationDependent())
71 ExprBits.InstantiationDependent = true;
72
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000073 if (arraySize->containsUnexpandedParameterPack())
74 ExprBits.ContainsUnexpandedParameterPack = true;
75
Sebastian Redlcee63fb2008-12-02 14:43:59 +000076 SubExprs[i++] = arraySize;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000077 }
78
Sebastian Redl2aed8b82012-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 Gregorbebbe0d2010-12-15 01:34:56 +000089 for (unsigned j = 0; j < NumPlacementArgs; ++j) {
Douglas Gregor561f8122011-07-01 01:22:09 +000090 if (placementArgs[j]->isInstantiationDependent())
91 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000092 if (placementArgs[j]->containsUnexpandedParameterPack())
93 ExprBits.ContainsUnexpandedParameterPack = true;
94
Sebastian Redl4c5d3202008-11-21 19:14:01 +000095 SubExprs[i++] = placementArgs[j];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +000096 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +000097}
98
Chris Lattner59218632010-05-10 01:22:27 +000099void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000100 unsigned numPlaceArgs, bool hasInitializer){
Chris Lattner59218632010-05-10 01:22:27 +0000101 assert(SubExprs == 0 && "SubExprs already allocated");
102 Array = isArray;
103 NumPlacementArgs = numPlaceArgs;
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000104
105 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattner59218632010-05-10 01:22:27 +0000106 SubExprs = new (C) Stmt*[TotalSize];
107}
108
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000109bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
John McCallc2f3e7f2011-03-07 03:12:35 +0000110 return getOperatorNew()->getType()->
Sebastian Redl8026f6d2011-03-13 17:09:40 +0000111 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCallc2f3e7f2011-03-07 03:12:35 +0000112}
Chris Lattner59218632010-05-10 01:22:27 +0000113
Sebastian Redl2aed8b82012-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-Gayebc6af12012-02-16 22:15:50 +0000123 llvm_unreachable("bogus initialization style");
Sebastian Redl2aed8b82012-02-16 12:22:20 +0000124}
125
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000126// CXXDeleteExpr
Douglas Gregor5833b0b2010-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 Silverstein0fa0b782010-10-20 00:38:15 +0000136 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000137 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000138
139 if (ArgType->isDependentType() && !ArgType->isPointerType())
140 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000141
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000142 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000143}
144
Douglas Gregora71d8192009-09-04 17:36:40 +0000145// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000146PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
147 : Type(Info)
148{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000149 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000150}
151
John McCalle23cf432010-12-14 08:05:40 +0000152CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-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 McCalle23cf432010-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 Gregorbebbe0d2010-12-15 01:34:56 +0000164 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-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 Gregorbebbe0d2010-12-15 01:34:56 +0000173 // ContainsUnexpandedParameterPack
174 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000175 (QualifierLoc &&
176 QualifierLoc.getNestedNameSpecifier()
177 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000178 (ScopeType &&
179 ScopeType->getType()->containsUnexpandedParameterPack()) ||
180 (DestroyedType.getTypeSourceInfo() &&
181 DestroyedType.getTypeSourceInfo()->getType()
182 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000183 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000184 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000185 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
186 DestroyedType(DestroyedType) { }
187
Douglas Gregora2e7dd22010-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 Gregor26d4ac92010-02-24 23:40:28 +0000195SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000196 SourceLocation End = DestroyedType.getLocation();
197 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000198 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000199 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000200}
201
202
John McCallba135432009-11-21 08:51:07 +0000203// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000204UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000205UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000206 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000207 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000208 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000209 const DeclarationNameInfo &NameInfo,
210 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000211 const TemplateArgumentListInfo *Args,
212 UnresolvedSetIterator Begin,
213 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000214{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000215 assert(Args || TemplateKWLoc.isValid());
216 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000217 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000218 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000219 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
220 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000221 ADL, /*Overload*/ true, Args,
Richard Smithad762fc2011-04-14 22:09:26 +0000222 Begin, End, /*StdIsAssociated=*/false);
John McCallf7a1a742009-11-24 19:00:30 +0000223}
224
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000225UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000226UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
227 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000228 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000229 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000230 if (HasTemplateKWAndArgsInfo)
231 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000232
Chris Lattner32488542010-10-30 05:14:06 +0000233 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000234 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000235 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000236 return E;
237}
238
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000239OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000240 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000241 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000242 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000243 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000244 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000245 UnresolvedSetIterator End,
246 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000248 bool KnownContainsUnexpandedParameterPack)
249 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
250 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000251 (KnownInstantiationDependent ||
252 NameInfo.isInstantiationDependent() ||
253 (QualifierLoc &&
254 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000255 (KnownContainsUnexpandedParameterPack ||
256 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000257 (QualifierLoc &&
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()))),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000260 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
261 QualifierLoc(QualifierLoc),
262 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000263{
Douglas Gregorbebbe0d2010-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 Gregor561f8122011-07-01 01:22:09 +0000287 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000288 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000289 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
290 Dependent,
291 InstantiationDependent,
292 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000293
294 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000295 ExprBits.TypeDependent = true;
296 ExprBits.ValueDependent = true;
297 }
298 if (InstantiationDependent)
299 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000300 if (ContainsUnexpandedParameterPack)
301 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000302 } else if (TemplateKWLoc.isValid()) {
303 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000304 }
305
306 if (isTypeDependent())
307 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-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 Gregor928e6fc2010-05-23 19:36:40 +0000315 if (NumResults) {
Douglas Gregorbebbe0d2010-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 Gregor928e6fc2010-05-23 19:36:40 +0000322 }
323}
324
John McCalle9ee23e2010-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 McCall865d4472009-11-19 22:55:06 +0000332// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000333DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000334 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000335 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-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 Gregor561f8122011-07-01 01:22:09 +0000340 (NameInfo.isInstantiationDependent() ||
341 (QualifierLoc &&
342 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000343 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000344 (QualifierLoc &&
345 QualifierLoc.getNestedNameSpecifier()
346 ->containsUnexpandedParameterPack()))),
347 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000348 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000349{
350 if (Args) {
351 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000352 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000353 bool ContainsUnexpandedParameterPack
354 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000355 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
356 Dependent,
357 InstantiationDependent,
358 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000359 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000360 } else if (TemplateKWLoc.isValid()) {
361 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000362 }
363}
364
John McCallf7a1a742009-11-24 19:00:30 +0000365DependentScopeDeclRefExpr *
366DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000367 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000368 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000369 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *Args) {
371 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000372 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000373 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
374 else if (TemplateKWLoc.isValid())
375 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000376 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000377 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
378 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000379}
380
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000381DependentScopeDeclRefExpr *
382DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000383 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000384 unsigned NumTemplateArgs) {
385 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000386 if (HasTemplateKWAndArgsInfo)
387 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000388 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000389 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000390 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000391 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000392 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000393 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000394 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000395}
396
Chandler Carruth428edaf2010-10-25 08:47:36 +0000397SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000398 if (isa<CXXTemporaryObjectExpr>(this))
399 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
400
Douglas Gregor40749ee2010-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 Kremeneke3837682009-12-23 04:00:48 +0000417}
418
Douglas Gregorb4609802008-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 Stump1eb44332009-09-09 15:08:12 +0000424 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000425 getArg(0)->getSourceRange().getEnd());
426 else
427 // Postfix operator
Chandler Carruthd7650612011-04-02 09:47:38 +0000428 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000429 getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000430 } else if (Kind == OO_Arrow) {
431 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-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 Kremenekb2771592011-03-30 17:41:19 +0000446Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
447 if (const MemberExpr *MemExpr =
448 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-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 Kremenekb2771592011-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 Carruth007a9b12010-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 Gregor00b98c22009-11-12 15:31:47 +0000476
Douglas Gregor49badde2008-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 Gregor506ae412009-01-16 18:33:17 +0000493
John McCallf871d0c2010-08-07 06:22:56 +0000494CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000495 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000496 CastKind K, Expr *Op,
497 const CXXCastPath *BasePath,
498 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000499 SourceLocation L,
500 SourceLocation RParenLoc) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000505 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
506 RParenLoc);
John McCallf871d0c2010-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 McCallf89e55a2010-11-18 06:31:45 +0000519 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000520 CastKind K, Expr *Op,
521 const CXXCastPath *BasePath,
522 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000523 SourceLocation L,
524 SourceLocation RParenLoc) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000529 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
530 RParenLoc);
John McCallf871d0c2010-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 Carlsson0fee3302011-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 McCallf871d0c2010-08-07 06:22:56 +0000572CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000573CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
574 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000575 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000576 TypeSourceInfo *WrittenTy, SourceLocation L,
577 SourceLocation RParenLoc) {
John McCallf871d0c2010-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 Gregor1d5d0b92011-01-12 22:41:29 +0000582 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
583 RParenLoc);
John McCallf871d0c2010-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 McCallf89e55a2010-11-18 06:31:45 +0000595CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
596 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000597 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000598 SourceLocation L,
599 SourceLocation RParenLoc) {
600 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000601}
602
603CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
604 return new (C) CXXConstCastExpr(EmptyShell());
605}
606
607CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000608CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-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 McCallf89e55a2010-11-18 06:31:45 +0000616 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-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 Gregor65222e82009-12-23 18:19:08 +0000629CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000630CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
631 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000632 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000633 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
634 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000635}
636
Mike Stump1eb44332009-09-09 15:08:12 +0000637CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000638 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000639 return new (C) CXXTemporary(Destructor);
640}
641
Mike Stump1eb44332009-09-09 15:08:12 +0000642CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000643 CXXTemporary *Temp,
644 Expr* SubExpr) {
Peter Collingbournebceb7552011-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 Carlssonfceb0a82009-05-30 20:03:25 +0000648
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000649 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000650}
651
Anders Carlsson8e587a12009-05-30 20:56:46 +0000652CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000653 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000654 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000655 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000656 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000657 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000658 bool HadMultipleCandidates,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000659 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000660 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
661 Type->getType().getNonReferenceType(),
662 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000663 Cons, false, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000664 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000665 CXXConstructExpr::CK_Complete, parenRange),
666 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000667}
668
669SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000670 return SourceRange(Type->getTypeLoc().getBeginLoc(),
671 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000672}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000673
Mike Stump1eb44332009-09-09 15:08:12 +0000674CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000675 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000676 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000677 Expr **Args, unsigned NumArgs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000678 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000679 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000680 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000681 ConstructionKind ConstructKind,
682 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000683 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000684 Elidable, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000685 HadMultipleCandidates, ListInitialization,
686 ZeroInitialization, ConstructKind,
687 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000688}
689
Mike Stump1eb44332009-09-09 15:08:12 +0000690CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000691 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000692 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000693 Expr **args, unsigned numargs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000694 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000695 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000696 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000697 ConstructionKind ConstructKind,
698 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000699 : Expr(SC, T, VK_RValue, OK_Ordinary,
700 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000701 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000702 T->containsUnexpandedParameterPack()),
Douglas Gregora48e6762011-09-26 14:47:03 +0000703 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000704 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000705 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000706 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000707 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-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 Gregorbebbe0d2010-12-15 01:34:56 +0000714
715 if (args[i]->isValueDependent())
716 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000717 if (args[i]->isInstantiationDependent())
718 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000719 if (args[i]->containsUnexpandedParameterPack())
720 ExprBits.ContainsUnexpandedParameterPack = true;
721
Douglas Gregor16006c92009-12-16 18:50:27 +0000722 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000723 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000724 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000725}
726
Douglas Gregor01d08012012-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 Gregordfca6f52012-02-13 22:00:16 +0000763 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000764 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000765 ArrayRef<VarDecl *> ArrayIndexVars,
766 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000767 SourceLocation ClosingBrace,
768 unsigned ManglingNumber)
Douglas Gregor01d08012012-02-07 10:09:13 +0000769 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
770 T->isDependentType(), T->isDependentType(), T->isDependentType(),
771 /*ContainsUnexpandedParameterPack=*/false),
772 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000773 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000774 CaptureDefault(CaptureDefault),
775 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000776 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000777 ClosingBrace(ClosingBrace)
778{
779 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000780 CXXRecordDecl *Class = getLambdaClass();
781 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000782
783 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000784
785 // Copy captures.
786 ASTContext &Context = Class->getASTContext();
787 Data.NumCaptures = NumCaptures;
788 Data.NumExplicitCaptures = 0;
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000789 Data.ManglingNumber = ManglingNumber;
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000790 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
791 Capture *ToCapture = Data.Captures;
792 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
793 if (Captures[I].isExplicit())
794 ++Data.NumExplicitCaptures;
795
796 *ToCapture++ = Captures[I];
797 }
798
799 // Copy initialization expressions for the non-static data members.
800 Stmt **Stored = getStoredStmts();
801 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
802 *Stored++ = CaptureInits[I];
803
804 // Copy the body of the lambda.
805 *Stored++ = getCallOperator()->getBody();
806
807 // Copy the array index variables, if any.
808 HasArrayIndexVars = !ArrayIndexVars.empty();
809 if (HasArrayIndexVars) {
810 assert(ArrayIndexStarts.size() == NumCaptures);
811 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
812 sizeof(VarDecl *) * ArrayIndexVars.size());
813 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
814 sizeof(unsigned) * Captures.size());
815 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
816 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000817}
818
819LambdaExpr *LambdaExpr::Create(ASTContext &Context,
820 CXXRecordDecl *Class,
821 SourceRange IntroducerRange,
822 LambdaCaptureDefault CaptureDefault,
823 ArrayRef<Capture> Captures,
824 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000825 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000826 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000827 ArrayRef<VarDecl *> ArrayIndexVars,
828 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000829 SourceLocation ClosingBrace,
830 unsigned ManglingNumber) {
Douglas Gregor01d08012012-02-07 10:09:13 +0000831 // Determine the type of the expression (i.e., the type of the
832 // function object we're creating).
833 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000834
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000835 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
836 if (!ArrayIndexVars.empty())
837 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
838 + sizeof(unsigned) * (Captures.size() + 1);
839 void *Mem = Context.Allocate(Size);
840 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000841 Captures, ExplicitParams, ExplicitResultType,
842 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregor9e8c92a2012-02-20 19:44:39 +0000843 ClosingBrace, ManglingNumber);
Douglas Gregor01d08012012-02-07 10:09:13 +0000844}
845
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000846LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
847 unsigned NumArrayIndexVars) {
848 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
849 if (NumArrayIndexVars)
850 Size += sizeof(VarDecl) * NumArrayIndexVars
851 + sizeof(unsigned) * (NumCaptures + 1);
852 void *Mem = C.Allocate(Size);
853 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
854}
855
Douglas Gregorda8962a2012-02-13 15:44:47 +0000856LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000857 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000858}
859
860LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000861 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000862}
863
864LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
865 return capture_begin();
866}
867
868LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
869 struct CXXRecordDecl::LambdaDefinitionData &Data
870 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000871 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000872}
873
874LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
875 return explicit_capture_end();
876}
877
878LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
879 return capture_end();
880}
881
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000882ArrayRef<VarDecl *>
883LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000884 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000885
886 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000887 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
888 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000889 VarDecl **IndexVars = getArrayIndexVars();
890 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000891 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
892 IndexVars + IndexStarts[Index + 1]);
893}
894
Douglas Gregor01d08012012-02-07 10:09:13 +0000895CXXRecordDecl *LambdaExpr::getLambdaClass() const {
896 return getType()->getAsCXXRecordDecl();
897}
898
899CXXMethodDecl *LambdaExpr::getCallOperator() const {
900 CXXRecordDecl *Record = getLambdaClass();
901 DeclarationName Name
902 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
903 DeclContext::lookup_result Calls = Record->lookup(Name);
904 assert(Calls.first != Calls.second && "Missing lambda call operator!");
905 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
906 assert(Calls.first == Calls.second && "More than lambda one call operator?");
907 return Result;
908}
909
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000910CompoundStmt *LambdaExpr::getBody() const {
911 if (!getStoredStmts()[NumCaptures])
912 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
913
914 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
915}
916
Douglas Gregor01d08012012-02-07 10:09:13 +0000917bool LambdaExpr::isMutable() const {
918 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
919}
920
John McCall80ee6e82011-11-10 05:35:25 +0000921ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
922 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000923 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000924 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000925 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000926 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000927 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000928 SubExpr(subexpr) {
929 ExprWithCleanupsBits.NumObjects = objects.size();
930 for (unsigned i = 0, e = objects.size(); i != e; ++i)
931 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000932}
933
John McCall80ee6e82011-11-10 05:35:25 +0000934ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
935 ArrayRef<CleanupObject> objects) {
936 size_t size = sizeof(ExprWithCleanups)
937 + objects.size() * sizeof(CleanupObject);
938 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
939 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000940}
941
John McCall80ee6e82011-11-10 05:35:25 +0000942ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
943 : Expr(ExprWithCleanupsClass, empty) {
944 ExprWithCleanupsBits.NumObjects = numObjects;
945}
Chris Lattnerd2598362010-05-10 00:25:06 +0000946
John McCall80ee6e82011-11-10 05:35:25 +0000947ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
948 unsigned numObjects) {
949 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
950 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
951 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000952}
953
Douglas Gregorab6677e2010-09-08 00:15:04 +0000954CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000955 SourceLocation LParenLoc,
956 Expr **Args,
957 unsigned NumArgs,
958 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000959 : Expr(CXXUnresolvedConstructExprClass,
960 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000961 (Type->getType()->isLValueReferenceType() ? VK_LValue
962 :Type->getType()->isRValueReferenceType()? VK_XValue
963 :VK_RValue),
964 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000965 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000966 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000967 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000968 LParenLoc(LParenLoc),
969 RParenLoc(RParenLoc),
970 NumArgs(NumArgs) {
971 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000972 for (unsigned I = 0; I != NumArgs; ++I) {
973 if (Args[I]->containsUnexpandedParameterPack())
974 ExprBits.ContainsUnexpandedParameterPack = true;
975
976 StoredArgs[I] = Args[I];
977 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000978}
979
980CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000981CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000982 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000983 SourceLocation LParenLoc,
984 Expr **Args,
985 unsigned NumArgs,
986 SourceLocation RParenLoc) {
987 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
988 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000989 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000990 Args, NumArgs, RParenLoc);
991}
992
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000993CXXUnresolvedConstructExpr *
994CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
995 Stmt::EmptyShell Empty;
996 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
997 sizeof(Expr *) * NumArgs);
998 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
999}
1000
Douglas Gregorab6677e2010-09-08 00:15:04 +00001001SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
1002 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1003}
1004
John McCall865d4472009-11-19 22:55:06 +00001005CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001006 Expr *Base, QualType BaseType,
1007 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001008 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001009 NestedNameSpecifierLoc QualifierLoc,
1010 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001011 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001012 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001013 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001014 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001015 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001016 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001017 (QualifierLoc &&
1018 QualifierLoc.getNestedNameSpecifier()
1019 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001020 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001021 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001022 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001023 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001024 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001025 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001026 if (TemplateArgs) {
1027 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001028 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001029 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001030 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1031 Dependent,
1032 InstantiationDependent,
1033 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001034 if (ContainsUnexpandedParameterPack)
1035 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001036 } else if (TemplateKWLoc.isValid()) {
1037 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001038 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001039}
1040
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001041CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1042 Expr *Base, QualType BaseType,
1043 bool IsArrow,
1044 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001045 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001046 NamedDecl *FirstQualifierFoundInScope,
1047 DeclarationNameInfo MemberNameInfo)
1048 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001049 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001050 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001051 (QualifierLoc &&
1052 QualifierLoc.getNestedNameSpecifier()->
1053 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001054 MemberNameInfo.containsUnexpandedParameterPack())),
1055 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001056 HasTemplateKWAndArgsInfo(false),
1057 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001058 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1059 MemberNameInfo(MemberNameInfo) { }
1060
John McCall865d4472009-11-19 22:55:06 +00001061CXXDependentScopeMemberExpr *
1062CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001063 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001064 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001065 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001066 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001067 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001068 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001069 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001070 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001071 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1072 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001073 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001074 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001075 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001077 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1078 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1079 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001080
Chris Lattner32488542010-10-30 05:14:06 +00001081 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001082 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1083 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001084 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001085 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001086 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001087 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001088}
1089
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001090CXXDependentScopeMemberExpr *
1091CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001092 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001093 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001094 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001095 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001096 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001097 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001098 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001099
1100 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001101 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001102 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001103 CXXDependentScopeMemberExpr *E
1104 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001105 0, SourceLocation(),
1106 NestedNameSpecifierLoc(),
1107 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001108 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001109 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001110 return E;
1111}
1112
Douglas Gregor4c9be892011-02-28 20:01:57 +00001113bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1114 if (Base == 0)
1115 return true;
1116
Douglas Gregor75e85042011-03-02 21:06:53 +00001117 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001118}
1119
John McCall864c0412011-04-26 20:42:42 +00001120static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1121 UnresolvedSetIterator end) {
1122 do {
1123 NamedDecl *decl = *begin;
1124 if (isa<UnresolvedUsingValueDecl>(decl))
1125 return false;
1126 if (isa<UsingShadowDecl>(decl))
1127 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1128
1129 // Unresolved member expressions should only contain methods and
1130 // method templates.
1131 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1132
1133 if (isa<FunctionTemplateDecl>(decl))
1134 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1135 if (cast<CXXMethodDecl>(decl)->isStatic())
1136 return false;
1137 } while (++begin != end);
1138
1139 return true;
1140}
1141
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001142UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001143 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001144 Expr *Base, QualType BaseType,
1145 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001146 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001147 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001148 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001149 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001150 const TemplateArgumentListInfo *TemplateArgs,
1151 UnresolvedSetIterator Begin,
1152 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001153 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1154 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001155 // Dependent
1156 ((Base && Base->isTypeDependent()) ||
1157 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001158 ((Base && Base->isInstantiationDependent()) ||
1159 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001160 // Contains unexpanded parameter pack
1161 ((Base && Base->containsUnexpandedParameterPack()) ||
1162 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001163 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1164 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001165
1166 // Check whether all of the members are non-static member functions,
1167 // and if so, mark give this bound-member type instead of overload type.
1168 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1169 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001170}
1171
Douglas Gregor4c9be892011-02-28 20:01:57 +00001172bool UnresolvedMemberExpr::isImplicitAccess() const {
1173 if (Base == 0)
1174 return true;
1175
Douglas Gregor75e85042011-03-02 21:06:53 +00001176 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001177}
1178
John McCall129e2df2009-11-30 22:42:35 +00001179UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001180UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001181 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001182 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001183 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001184 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001185 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001186 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001187 const TemplateArgumentListInfo *TemplateArgs,
1188 UnresolvedSetIterator Begin,
1189 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001190 std::size_t size = sizeof(UnresolvedMemberExpr);
1191 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001192 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1193 else if (TemplateKWLoc.isValid())
1194 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001195
Chris Lattner32488542010-10-30 05:14:06 +00001196 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001197 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001198 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001199 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001200 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001201}
1202
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001203UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001204UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001205 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001206 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001207 if (HasTemplateKWAndArgsInfo)
1208 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001209
Chris Lattner32488542010-10-30 05:14:06 +00001210 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001211 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001212 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001213 return E;
1214}
1215
John McCallc373d482010-01-27 01:50:18 +00001216CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1217 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1218
1219 // If there was a nested name specifier, it names the naming class.
1220 // It can't be dependent: after all, we were actually able to do the
1221 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001222 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001223 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001224 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001225 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001226 Record = T->getAsCXXRecordDecl();
1227 assert(Record && "qualifier in member expression does not name record");
1228 }
John McCallc373d482010-01-27 01:50:18 +00001229 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001230 else {
John McCallc373d482010-01-27 01:50:18 +00001231 QualType BaseType = getBaseType().getNonReferenceType();
1232 if (isArrow()) {
1233 const PointerType *PT = BaseType->getAs<PointerType>();
1234 assert(PT && "base of arrow member access is not pointer");
1235 BaseType = PT->getPointeeType();
1236 }
1237
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001238 Record = BaseType->getAsCXXRecordDecl();
1239 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001240 }
1241
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001242 return Record;
John McCallc373d482010-01-27 01:50:18 +00001243}
1244
Douglas Gregorc7793c72011-01-15 01:15:58 +00001245SubstNonTypeTemplateParmPackExpr::
1246SubstNonTypeTemplateParmPackExpr(QualType T,
1247 NonTypeTemplateParmDecl *Param,
1248 SourceLocation NameLoc,
1249 const TemplateArgument &ArgPack)
1250 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001251 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001252 Param(Param), Arguments(ArgPack.pack_begin()),
1253 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1254
1255TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1256 return TemplateArgument(Arguments, NumArguments);
1257}
1258
David Blaikie99ba9e32011-12-20 02:48:34 +00001259void ArrayTypeTraitExpr::anchor() { }