blob: 718010b81ed4883a551c89cf81e603d28b3288b3 [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,
John McCall6ec278d2011-01-27 09:37:56 +000048 FunctionDecl *operatorDelete,
Sebastian Redl5f688f42012-02-16 10:58:10 +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 Redl5f688f42012-02-16 10:58:10 +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 Redl5f688f42012-02-16 10:58:10 +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 Redl5f688f42012-02-16 10:58:10 +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 Redl5f688f42012-02-16 10:58:10 +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 Redl5f688f42012-02-16 10:58:10 +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 }
123}
124
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000125// CXXDeleteExpr
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000126QualType CXXDeleteExpr::getDestroyedType() const {
127 const Expr *Arg = getArgument();
128 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
129 if (ICE->getCastKind() != CK_UserDefinedConversion &&
130 ICE->getType()->isVoidPointerType())
131 Arg = ICE->getSubExpr();
132 else
133 break;
134 }
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000135 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silversteinc87fa062010-10-20 00:56:01 +0000136 const QualType ArgType = Arg->getType();
Craig Silversteina437ad32010-11-16 07:16:25 +0000137
138 if (ArgType->isDependentType() && !ArgType->isPointerType())
139 return QualType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000140
Craig Silverstein0fa0b782010-10-20 00:38:15 +0000141 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor5833b0b2010-09-14 22:55:20 +0000142}
143
Douglas Gregora71d8192009-09-04 17:36:40 +0000144// CXXPseudoDestructorExpr
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000145PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
146 : Type(Info)
147{
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000148 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000149}
150
John McCalle23cf432010-12-14 08:05:40 +0000151CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000152 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
153 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
154 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
155 PseudoDestructorTypeStorage DestroyedType)
John McCalle23cf432010-12-14 08:05:40 +0000156 : Expr(CXXPseudoDestructorExprClass,
157 Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
158 FunctionProtoType::ExtProtoInfo())),
159 VK_RValue, OK_Ordinary,
160 /*isTypeDependent=*/(Base->isTypeDependent() ||
161 (DestroyedType.getTypeSourceInfo() &&
162 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000163 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000164 (Base->isInstantiationDependent() ||
165 (QualifierLoc &&
166 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
167 (ScopeType &&
168 ScopeType->getType()->isInstantiationDependentType()) ||
169 (DestroyedType.getTypeSourceInfo() &&
170 DestroyedType.getTypeSourceInfo()->getType()
171 ->isInstantiationDependentType())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000172 // ContainsUnexpandedParameterPack
173 (Base->containsUnexpandedParameterPack() ||
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000174 (QualifierLoc &&
175 QualifierLoc.getNestedNameSpecifier()
176 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000177 (ScopeType &&
178 ScopeType->getType()->containsUnexpandedParameterPack()) ||
179 (DestroyedType.getTypeSourceInfo() &&
180 DestroyedType.getTypeSourceInfo()->getType()
181 ->containsUnexpandedParameterPack()))),
John McCalle23cf432010-12-14 08:05:40 +0000182 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregorf3db29f2011-02-25 18:19:59 +0000183 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalle23cf432010-12-14 08:05:40 +0000184 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
185 DestroyedType(DestroyedType) { }
186
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000187QualType CXXPseudoDestructorExpr::getDestroyedType() const {
188 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
189 return TInfo->getType();
190
191 return QualType();
192}
193
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000194SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000195 SourceLocation End = DestroyedType.getLocation();
196 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnarabd054db2010-05-20 10:00:11 +0000197 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Douglas Gregora2e7dd22010-02-25 01:56:36 +0000198 return SourceRange(Base->getLocStart(), End);
Douglas Gregor26d4ac92010-02-24 23:40:28 +0000199}
200
201
John McCallba135432009-11-21 08:51:07 +0000202// UnresolvedLookupExpr
John McCallf7a1a742009-11-24 19:00:30 +0000203UnresolvedLookupExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000204UnresolvedLookupExpr::Create(ASTContext &C,
John McCallc373d482010-01-27 01:50:18 +0000205 CXXRecordDecl *NamingClass,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000206 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000207 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000208 const DeclarationNameInfo &NameInfo,
209 bool ADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000210 const TemplateArgumentListInfo *Args,
211 UnresolvedSetIterator Begin,
212 UnresolvedSetIterator End)
John McCallf7a1a742009-11-24 19:00:30 +0000213{
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000214 assert(Args || TemplateKWLoc.isValid());
215 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000216 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000217 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000218 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
219 TemplateKWLoc, NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +0000220 ADL, /*Overload*/ true, Args,
Richard Smithad762fc2011-04-14 22:09:26 +0000221 Begin, End, /*StdIsAssociated=*/false);
John McCallf7a1a742009-11-24 19:00:30 +0000222}
223
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000224UnresolvedLookupExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000225UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
226 bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +0000227 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000228 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000229 if (HasTemplateKWAndArgsInfo)
230 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000231
Chris Lattner32488542010-10-30 05:14:06 +0000232 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000233 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000234 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbd65bb52010-06-25 09:03:34 +0000235 return E;
236}
237
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000238OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C,
Douglas Gregor4c9be892011-02-28 20:01:57 +0000239 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000240 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000241 const DeclarationNameInfo &NameInfo,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000242 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000243 UnresolvedSetIterator Begin,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000244 UnresolvedSetIterator End,
245 bool KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000246 bool KnownInstantiationDependent,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247 bool KnownContainsUnexpandedParameterPack)
248 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
249 KnownDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000250 (KnownInstantiationDependent ||
251 NameInfo.isInstantiationDependent() ||
252 (QualifierLoc &&
253 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000254 (KnownContainsUnexpandedParameterPack ||
255 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor4c9be892011-02-28 20:01:57 +0000256 (QualifierLoc &&
257 QualifierLoc.getNestedNameSpecifier()
258 ->containsUnexpandedParameterPack()))),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000259 Results(0), NumResults(End - Begin), NameInfo(NameInfo),
260 QualifierLoc(QualifierLoc),
261 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000262{
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000263 NumResults = End - Begin;
264 if (NumResults) {
265 // Determine whether this expression is type-dependent.
266 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
267 if ((*I)->getDeclContext()->isDependentContext() ||
268 isa<UnresolvedUsingValueDecl>(*I)) {
269 ExprBits.TypeDependent = true;
270 ExprBits.ValueDependent = true;
271 }
272 }
273
274 Results = static_cast<DeclAccessPair *>(
275 C.Allocate(sizeof(DeclAccessPair) * NumResults,
276 llvm::alignOf<DeclAccessPair>()));
277 memcpy(Results, &*Begin.getIterator(),
278 NumResults * sizeof(DeclAccessPair));
279 }
280
281 // If we have explicit template arguments, check for dependent
282 // template arguments and whether they contain any unexpanded pack
283 // expansions.
284 if (TemplateArgs) {
285 bool Dependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000286 bool InstantiationDependent = false;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000287 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000288 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
289 Dependent,
290 InstantiationDependent,
291 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000292
293 if (Dependent) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 ExprBits.TypeDependent = true;
295 ExprBits.ValueDependent = true;
296 }
297 if (InstantiationDependent)
298 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000299 if (ContainsUnexpandedParameterPack)
300 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000301 } else if (TemplateKWLoc.isValid()) {
302 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000303 }
304
305 if (isTypeDependent())
306 setType(C.DependentTy);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +0000307}
308
309void OverloadExpr::initializeResults(ASTContext &C,
310 UnresolvedSetIterator Begin,
311 UnresolvedSetIterator End) {
312 assert(Results == 0 && "Results already initialized!");
313 NumResults = End - Begin;
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000314 if (NumResults) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000315 Results = static_cast<DeclAccessPair *>(
316 C.Allocate(sizeof(DeclAccessPair) * NumResults,
317
318 llvm::alignOf<DeclAccessPair>()));
319 memcpy(Results, &*Begin.getIterator(),
320 NumResults * sizeof(DeclAccessPair));
Douglas Gregor928e6fc2010-05-23 19:36:40 +0000321 }
322}
323
John McCalle9ee23e2010-04-22 18:44:12 +0000324CXXRecordDecl *OverloadExpr::getNamingClass() const {
325 if (isa<UnresolvedLookupExpr>(this))
326 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
327 else
328 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
329}
330
John McCall865d4472009-11-19 22:55:06 +0000331// DependentScopeDeclRefExpr
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000332DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000333 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000334 SourceLocation TemplateKWLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000335 const DeclarationNameInfo &NameInfo,
336 const TemplateArgumentListInfo *Args)
337 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
338 true, true,
Douglas Gregor561f8122011-07-01 01:22:09 +0000339 (NameInfo.isInstantiationDependent() ||
340 (QualifierLoc &&
341 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000342 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000343 (QualifierLoc &&
344 QualifierLoc.getNestedNameSpecifier()
345 ->containsUnexpandedParameterPack()))),
346 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000347 HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000348{
349 if (Args) {
350 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000351 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000352 bool ContainsUnexpandedParameterPack
353 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000354 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
355 Dependent,
356 InstantiationDependent,
357 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000358 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000359 } else if (TemplateKWLoc.isValid()) {
360 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000361 }
362}
363
John McCallf7a1a742009-11-24 19:00:30 +0000364DependentScopeDeclRefExpr *
365DependentScopeDeclRefExpr::Create(ASTContext &C,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000366 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000367 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000369 const TemplateArgumentListInfo *Args) {
370 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCallf7a1a742009-11-24 19:00:30 +0000371 if (Args)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000372 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
373 else if (TemplateKWLoc.isValid())
374 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000375 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000376 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
377 TemplateKWLoc, NameInfo, Args);
John McCallf7a1a742009-11-24 19:00:30 +0000378}
379
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000380DependentScopeDeclRefExpr *
381DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000382 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000383 unsigned NumTemplateArgs) {
384 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000385 if (HasTemplateKWAndArgsInfo)
386 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000387 void *Mem = C.Allocate(size);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000388 DependentScopeDeclRefExpr *E
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000389 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000390 SourceLocation(),
Douglas Gregordef03542011-02-04 12:01:24 +0000391 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000392 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregordef03542011-02-04 12:01:24 +0000393 return E;
Argyrios Kyrtzidis12dffcd2010-06-28 09:31:56 +0000394}
395
Chandler Carruth428edaf2010-10-25 08:47:36 +0000396SourceRange CXXConstructExpr::getSourceRange() const {
John McCall2882eca2011-02-21 06:23:05 +0000397 if (isa<CXXTemporaryObjectExpr>(this))
398 return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
399
Douglas Gregor40749ee2010-11-03 00:35:38 +0000400 if (ParenRange.isValid())
401 return SourceRange(Loc, ParenRange.getEnd());
402
403 SourceLocation End = Loc;
404 for (unsigned I = getNumArgs(); I > 0; --I) {
405 const Expr *Arg = getArg(I-1);
406 if (!Arg->isDefaultArgument()) {
407 SourceLocation NewEnd = Arg->getLocEnd();
408 if (NewEnd.isValid()) {
409 End = NewEnd;
410 break;
411 }
412 }
413 }
414
415 return SourceRange(Loc, End);
Ted Kremeneke3837682009-12-23 04:00:48 +0000416}
417
Douglas Gregorb4609802008-11-14 16:09:21 +0000418SourceRange CXXOperatorCallExpr::getSourceRange() const {
419 OverloadedOperatorKind Kind = getOperator();
420 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
421 if (getNumArgs() == 1)
422 // Prefix operator
Mike Stump1eb44332009-09-09 15:08:12 +0000423 return SourceRange(getOperatorLoc(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000424 getArg(0)->getSourceRange().getEnd());
425 else
426 // Postfix operator
Chandler Carruthd7650612011-04-02 09:47:38 +0000427 return SourceRange(getArg(0)->getSourceRange().getBegin(),
Douglas Gregorb4609802008-11-14 16:09:21 +0000428 getOperatorLoc());
Chandler Carruthd7650612011-04-02 09:47:38 +0000429 } else if (Kind == OO_Arrow) {
430 return getArg(0)->getSourceRange();
Douglas Gregorb4609802008-11-14 16:09:21 +0000431 } else if (Kind == OO_Call) {
432 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
433 } else if (Kind == OO_Subscript) {
434 return SourceRange(getArg(0)->getSourceRange().getBegin(), getRParenLoc());
435 } else if (getNumArgs() == 1) {
436 return SourceRange(getOperatorLoc(), getArg(0)->getSourceRange().getEnd());
437 } else if (getNumArgs() == 2) {
438 return SourceRange(getArg(0)->getSourceRange().getBegin(),
439 getArg(1)->getSourceRange().getEnd());
440 } else {
441 return SourceRange();
442 }
443}
444
Ted Kremenekb2771592011-03-30 17:41:19 +0000445Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
446 if (const MemberExpr *MemExpr =
447 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Douglas Gregor88a35142008-12-22 05:46:06 +0000448 return MemExpr->getBase();
449
450 // FIXME: Will eventually need to cope with member pointers.
451 return 0;
452}
453
Ted Kremenekb2771592011-03-30 17:41:19 +0000454CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
455 if (const MemberExpr *MemExpr =
456 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
457 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
458
459 // FIXME: Will eventually need to cope with member pointers.
460 return 0;
461}
462
463
Chandler Carruth007a9b12010-10-27 06:55:41 +0000464CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() {
465 Expr* ThisArg = getImplicitObjectArgument();
466 if (!ThisArg)
467 return 0;
468
469 if (ThisArg->getType()->isAnyPointerType())
470 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
471
472 return ThisArg->getType()->getAsCXXRecordDecl();
473}
474
Douglas Gregor00b98c22009-11-12 15:31:47 +0000475
Douglas Gregor49badde2008-10-27 19:41:14 +0000476//===----------------------------------------------------------------------===//
477// Named casts
478//===----------------------------------------------------------------------===//
479
480/// getCastName - Get the name of the C++ cast being used, e.g.,
481/// "static_cast", "dynamic_cast", "reinterpret_cast", or
482/// "const_cast". The returned pointer must not be freed.
483const char *CXXNamedCastExpr::getCastName() const {
484 switch (getStmtClass()) {
485 case CXXStaticCastExprClass: return "static_cast";
486 case CXXDynamicCastExprClass: return "dynamic_cast";
487 case CXXReinterpretCastExprClass: return "reinterpret_cast";
488 case CXXConstCastExprClass: return "const_cast";
489 default: return "<invalid cast>";
490 }
491}
Douglas Gregor506ae412009-01-16 18:33:17 +0000492
John McCallf871d0c2010-08-07 06:22:56 +0000493CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000494 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000495 CastKind K, Expr *Op,
496 const CXXCastPath *BasePath,
497 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000498 SourceLocation L,
499 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000500 unsigned PathSize = (BasePath ? BasePath->size() : 0);
501 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
502 + PathSize * sizeof(CXXBaseSpecifier*));
503 CXXStaticCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000504 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
505 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000506 if (PathSize) E->setCastPath(*BasePath);
507 return E;
508}
509
510CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
511 unsigned PathSize) {
512 void *Buffer =
513 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
514 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
515}
516
517CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000518 ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000519 CastKind K, Expr *Op,
520 const CXXCastPath *BasePath,
521 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000522 SourceLocation L,
523 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000524 unsigned PathSize = (BasePath ? BasePath->size() : 0);
525 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
526 + PathSize * sizeof(CXXBaseSpecifier*));
527 CXXDynamicCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000528 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
529 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000530 if (PathSize) E->setCastPath(*BasePath);
531 return E;
532}
533
534CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
535 unsigned PathSize) {
536 void *Buffer =
537 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
538 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
539}
540
Anders Carlsson0fee3302011-04-11 01:43:55 +0000541/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
542/// to always be null. For example:
543///
544/// struct A { };
545/// struct B final : A { };
546/// struct C { };
547///
548/// C *f(B* b) { return dynamic_cast<C*>(b); }
549bool CXXDynamicCastExpr::isAlwaysNull() const
550{
551 QualType SrcType = getSubExpr()->getType();
552 QualType DestType = getType();
553
554 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
555 SrcType = SrcPTy->getPointeeType();
556 DestType = DestType->castAs<PointerType>()->getPointeeType();
557 }
558
559 const CXXRecordDecl *SrcRD =
560 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
561
562 if (!SrcRD->hasAttr<FinalAttr>())
563 return false;
564
565 const CXXRecordDecl *DestRD =
566 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
567
568 return !DestRD->isDerivedFrom(SrcRD);
569}
570
John McCallf871d0c2010-08-07 06:22:56 +0000571CXXReinterpretCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000572CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
573 CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000574 const CXXCastPath *BasePath,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000575 TypeSourceInfo *WrittenTy, SourceLocation L,
576 SourceLocation RParenLoc) {
John McCallf871d0c2010-08-07 06:22:56 +0000577 unsigned PathSize = (BasePath ? BasePath->size() : 0);
578 void *Buffer =
579 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
580 CXXReinterpretCastExpr *E =
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000581 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
582 RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000583 if (PathSize) E->setCastPath(*BasePath);
584 return E;
585}
586
587CXXReinterpretCastExpr *
588CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
589 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
590 + PathSize * sizeof(CXXBaseSpecifier*));
591 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
592}
593
John McCallf89e55a2010-11-18 06:31:45 +0000594CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
595 ExprValueKind VK, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +0000596 TypeSourceInfo *WrittenTy,
Douglas Gregor1d5d0b92011-01-12 22:41:29 +0000597 SourceLocation L,
598 SourceLocation RParenLoc) {
599 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
John McCallf871d0c2010-08-07 06:22:56 +0000600}
601
602CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
603 return new (C) CXXConstCastExpr(EmptyShell());
604}
605
606CXXFunctionalCastExpr *
John McCallf89e55a2010-11-18 06:31:45 +0000607CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
John McCallf871d0c2010-08-07 06:22:56 +0000608 TypeSourceInfo *Written, SourceLocation L,
609 CastKind K, Expr *Op, const CXXCastPath *BasePath,
610 SourceLocation R) {
611 unsigned PathSize = (BasePath ? BasePath->size() : 0);
612 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
613 + PathSize * sizeof(CXXBaseSpecifier*));
614 CXXFunctionalCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +0000615 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
John McCallf871d0c2010-08-07 06:22:56 +0000616 if (PathSize) E->setCastPath(*BasePath);
617 return E;
618}
619
620CXXFunctionalCastExpr *
621CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
622 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
623 + PathSize * sizeof(CXXBaseSpecifier*));
624 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
625}
626
627
Douglas Gregor65222e82009-12-23 18:19:08 +0000628CXXDefaultArgExpr *
Douglas Gregor036aed12009-12-23 23:03:06 +0000629CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc,
630 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor65222e82009-12-23 18:19:08 +0000631 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor036aed12009-12-23 23:03:06 +0000632 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
633 SubExpr);
Douglas Gregor65222e82009-12-23 18:19:08 +0000634}
635
Mike Stump1eb44332009-09-09 15:08:12 +0000636CXXTemporary *CXXTemporary::Create(ASTContext &C,
Anders Carlssonb859f352009-05-30 20:34:37 +0000637 const CXXDestructorDecl *Destructor) {
Anders Carlsson88eaf072009-05-30 22:38:53 +0000638 return new (C) CXXTemporary(Destructor);
639}
640
Mike Stump1eb44332009-09-09 15:08:12 +0000641CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000642 CXXTemporary *Temp,
643 Expr* SubExpr) {
Peter Collingbournebceb7552011-11-27 22:09:28 +0000644 assert((SubExpr->getType()->isRecordType() ||
645 SubExpr->getType()->isArrayType()) &&
646 "Expression bound to a temporary must have record or array type!");
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000647
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000648 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlssonfceb0a82009-05-30 20:03:25 +0000649}
650
Anders Carlsson8e587a12009-05-30 20:56:46 +0000651CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
Anders Carlsson26de5492009-04-24 05:23:13 +0000652 CXXConstructorDecl *Cons,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000653 TypeSourceInfo *Type,
Douglas Gregor506ae412009-01-16 18:33:17 +0000654 Expr **Args,
Mike Stump1eb44332009-09-09 15:08:12 +0000655 unsigned NumArgs,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000656 SourceRange parenRange,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000657 bool HadMultipleCandidates,
Douglas Gregor1c63b9c2010-04-27 20:36:09 +0000658 bool ZeroInitialization)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000659 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
660 Type->getType().getNonReferenceType(),
661 Type->getTypeLoc().getBeginLoc(),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000662 Cons, false, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000663 HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000664 CXXConstructExpr::CK_Complete, parenRange),
665 Type(Type) {
Douglas Gregorab6677e2010-09-08 00:15:04 +0000666}
667
668SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
Chandler Carruth428edaf2010-10-25 08:47:36 +0000669 return SourceRange(Type->getTypeLoc().getBeginLoc(),
670 getParenRange().getEnd());
Douglas Gregor506ae412009-01-16 18:33:17 +0000671}
Anders Carlsson19d28a62009-04-21 02:22:11 +0000672
Mike Stump1eb44332009-09-09 15:08:12 +0000673CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000674 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000675 CXXConstructorDecl *D, bool Elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000676 Expr **Args, unsigned NumArgs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000677 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000678 bool ListInitialization,
Douglas Gregor9db7dbb2010-01-31 09:12:51 +0000679 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000680 ConstructionKind ConstructKind,
681 SourceRange ParenRange) {
Douglas Gregor99a2e602009-12-16 01:38:02 +0000682 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000683 Elidable, Args, NumArgs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000684 HadMultipleCandidates, ListInitialization,
685 ZeroInitialization, ConstructKind,
686 ParenRange);
Anders Carlssone349bea2009-04-23 02:32:43 +0000687}
688
Mike Stump1eb44332009-09-09 15:08:12 +0000689CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
Douglas Gregor99a2e602009-12-16 01:38:02 +0000690 SourceLocation Loc,
Anders Carlsson8e587a12009-05-30 20:56:46 +0000691 CXXConstructorDecl *D, bool elidable,
Douglas Gregor16006c92009-12-16 18:50:27 +0000692 Expr **args, unsigned numargs,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000693 bool HadMultipleCandidates,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000694 bool ListInitialization,
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000695 bool ZeroInitialization,
Chandler Carruth428edaf2010-10-25 08:47:36 +0000696 ConstructionKind ConstructKind,
697 SourceRange ParenRange)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000698 : Expr(SC, T, VK_RValue, OK_Ordinary,
699 T->isDependentType(), T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000700 T->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000701 T->containsUnexpandedParameterPack()),
Douglas Gregora48e6762011-09-26 14:47:03 +0000702 Constructor(D), Loc(Loc), ParenRange(ParenRange), NumArgs(numargs),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000703 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +0000704 ListInitialization(ListInitialization),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +0000705 ZeroInitialization(ZeroInitialization),
Douglas Gregora48e6762011-09-26 14:47:03 +0000706 ConstructKind(ConstructKind), Args(0)
Douglas Gregor16006c92009-12-16 18:50:27 +0000707{
708 if (NumArgs) {
709 Args = new (C) Stmt*[NumArgs];
710
711 for (unsigned i = 0; i != NumArgs; ++i) {
712 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713
714 if (args[i]->isValueDependent())
715 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000716 if (args[i]->isInstantiationDependent())
717 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 if (args[i]->containsUnexpandedParameterPack())
719 ExprBits.ContainsUnexpandedParameterPack = true;
720
Douglas Gregor16006c92009-12-16 18:50:27 +0000721 Args[i] = args[i];
Anders Carlssone349bea2009-04-23 02:32:43 +0000722 }
Douglas Gregor16006c92009-12-16 18:50:27 +0000723 }
Anders Carlssone349bea2009-04-23 02:32:43 +0000724}
725
Douglas Gregor01d08012012-02-07 10:09:13 +0000726LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
727 LambdaCaptureKind Kind, VarDecl *Var,
728 SourceLocation EllipsisLoc)
729 : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
730{
731 unsigned Bits = 0;
732 if (Implicit)
733 Bits |= Capture_Implicit;
734
735 switch (Kind) {
736 case LCK_This:
737 assert(Var == 0 && "'this' capture cannot have a variable!");
738 break;
739
740 case LCK_ByCopy:
741 Bits |= Capture_ByCopy;
742 // Fall through
743 case LCK_ByRef:
744 assert(Var && "capture must have a variable!");
745 break;
746 }
747 VarAndBits.setInt(Bits);
748}
749
750LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
751 if (capturesThis())
752 return LCK_This;
753
754 return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
755}
756
757LambdaExpr::LambdaExpr(QualType T,
758 SourceRange IntroducerRange,
759 LambdaCaptureDefault CaptureDefault,
760 ArrayRef<Capture> Captures,
761 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000762 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000763 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000764 ArrayRef<VarDecl *> ArrayIndexVars,
765 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor01d08012012-02-07 10:09:13 +0000766 SourceLocation ClosingBrace)
767 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
768 T->isDependentType(), T->isDependentType(), T->isDependentType(),
769 /*ContainsUnexpandedParameterPack=*/false),
770 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000771 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000772 CaptureDefault(CaptureDefault),
773 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000774 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000775 ClosingBrace(ClosingBrace)
776{
777 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000778 CXXRecordDecl *Class = getLambdaClass();
779 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000780
781 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000782
783 // Copy captures.
784 ASTContext &Context = Class->getASTContext();
785 Data.NumCaptures = NumCaptures;
786 Data.NumExplicitCaptures = 0;
787 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
788 Capture *ToCapture = Data.Captures;
789 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
790 if (Captures[I].isExplicit())
791 ++Data.NumExplicitCaptures;
792
793 *ToCapture++ = Captures[I];
794 }
795
796 // Copy initialization expressions for the non-static data members.
797 Stmt **Stored = getStoredStmts();
798 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
799 *Stored++ = CaptureInits[I];
800
801 // Copy the body of the lambda.
802 *Stored++ = getCallOperator()->getBody();
803
804 // Copy the array index variables, if any.
805 HasArrayIndexVars = !ArrayIndexVars.empty();
806 if (HasArrayIndexVars) {
807 assert(ArrayIndexStarts.size() == NumCaptures);
808 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
809 sizeof(VarDecl *) * ArrayIndexVars.size());
810 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
811 sizeof(unsigned) * Captures.size());
812 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
813 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000814}
815
816LambdaExpr *LambdaExpr::Create(ASTContext &Context,
817 CXXRecordDecl *Class,
818 SourceRange IntroducerRange,
819 LambdaCaptureDefault CaptureDefault,
820 ArrayRef<Capture> Captures,
821 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000822 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000823 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000824 ArrayRef<VarDecl *> ArrayIndexVars,
825 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor01d08012012-02-07 10:09:13 +0000826 SourceLocation ClosingBrace) {
827 // Determine the type of the expression (i.e., the type of the
828 // function object we're creating).
829 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000830
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000831 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
832 if (!ArrayIndexVars.empty())
833 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
834 + sizeof(unsigned) * (Captures.size() + 1);
835 void *Mem = Context.Allocate(Size);
836 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000837 Captures, ExplicitParams, ExplicitResultType,
838 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000839 ClosingBrace);
Douglas Gregor01d08012012-02-07 10:09:13 +0000840}
841
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000842LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
843 unsigned NumArrayIndexVars) {
844 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
845 if (NumArrayIndexVars)
846 Size += sizeof(VarDecl) * NumArrayIndexVars
847 + sizeof(unsigned) * (NumCaptures + 1);
848 void *Mem = C.Allocate(Size);
849 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
850}
851
Douglas Gregorda8962a2012-02-13 15:44:47 +0000852LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000853 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000854}
855
856LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000857 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000858}
859
860LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
861 return capture_begin();
862}
863
864LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
865 struct CXXRecordDecl::LambdaDefinitionData &Data
866 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000867 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000868}
869
870LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
871 return explicit_capture_end();
872}
873
874LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
875 return capture_end();
876}
877
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000878ArrayRef<VarDecl *>
879LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000880 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000881
882 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000883 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
884 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000885 VarDecl **IndexVars = getArrayIndexVars();
886 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000887 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
888 IndexVars + IndexStarts[Index + 1]);
889}
890
Douglas Gregor01d08012012-02-07 10:09:13 +0000891CXXRecordDecl *LambdaExpr::getLambdaClass() const {
892 return getType()->getAsCXXRecordDecl();
893}
894
895CXXMethodDecl *LambdaExpr::getCallOperator() const {
896 CXXRecordDecl *Record = getLambdaClass();
897 DeclarationName Name
898 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
899 DeclContext::lookup_result Calls = Record->lookup(Name);
900 assert(Calls.first != Calls.second && "Missing lambda call operator!");
901 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
902 assert(Calls.first == Calls.second && "More than lambda one call operator?");
903 return Result;
904}
905
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000906CompoundStmt *LambdaExpr::getBody() const {
907 if (!getStoredStmts()[NumCaptures])
908 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
909
910 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
911}
912
Douglas Gregor01d08012012-02-07 10:09:13 +0000913bool LambdaExpr::isMutable() const {
914 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
915}
916
John McCall80ee6e82011-11-10 05:35:25 +0000917ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
918 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000919 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000920 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000921 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000922 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000923 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000924 SubExpr(subexpr) {
925 ExprWithCleanupsBits.NumObjects = objects.size();
926 for (unsigned i = 0, e = objects.size(); i != e; ++i)
927 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000928}
929
John McCall80ee6e82011-11-10 05:35:25 +0000930ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
931 ArrayRef<CleanupObject> objects) {
932 size_t size = sizeof(ExprWithCleanups)
933 + objects.size() * sizeof(CleanupObject);
934 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
935 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000936}
937
John McCall80ee6e82011-11-10 05:35:25 +0000938ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
939 : Expr(ExprWithCleanupsClass, empty) {
940 ExprWithCleanupsBits.NumObjects = numObjects;
941}
Chris Lattnerd2598362010-05-10 00:25:06 +0000942
John McCall80ee6e82011-11-10 05:35:25 +0000943ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
944 unsigned numObjects) {
945 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
946 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
947 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000948}
949
Douglas Gregorab6677e2010-09-08 00:15:04 +0000950CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000951 SourceLocation LParenLoc,
952 Expr **Args,
953 unsigned NumArgs,
954 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000955 : Expr(CXXUnresolvedConstructExprClass,
956 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000957 (Type->getType()->isLValueReferenceType() ? VK_LValue
958 :Type->getType()->isRValueReferenceType()? VK_XValue
959 :VK_RValue),
960 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000961 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000962 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000963 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000964 LParenLoc(LParenLoc),
965 RParenLoc(RParenLoc),
966 NumArgs(NumArgs) {
967 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000968 for (unsigned I = 0; I != NumArgs; ++I) {
969 if (Args[I]->containsUnexpandedParameterPack())
970 ExprBits.ContainsUnexpandedParameterPack = true;
971
972 StoredArgs[I] = Args[I];
973 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000974}
975
976CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000977CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000978 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000979 SourceLocation LParenLoc,
980 Expr **Args,
981 unsigned NumArgs,
982 SourceLocation RParenLoc) {
983 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
984 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000985 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000986 Args, NumArgs, RParenLoc);
987}
988
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000989CXXUnresolvedConstructExpr *
990CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
991 Stmt::EmptyShell Empty;
992 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
993 sizeof(Expr *) * NumArgs);
994 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
995}
996
Douglas Gregorab6677e2010-09-08 00:15:04 +0000997SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
998 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
999}
1000
John McCall865d4472009-11-19 22:55:06 +00001001CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001002 Expr *Base, QualType BaseType,
1003 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001004 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001005 NestedNameSpecifierLoc QualifierLoc,
1006 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001007 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001008 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001009 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001010 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001011 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001012 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001013 (QualifierLoc &&
1014 QualifierLoc.getNestedNameSpecifier()
1015 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001016 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001017 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001018 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001019 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001020 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001021 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001022 if (TemplateArgs) {
1023 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001024 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001025 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001026 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1027 Dependent,
1028 InstantiationDependent,
1029 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001030 if (ContainsUnexpandedParameterPack)
1031 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001032 } else if (TemplateKWLoc.isValid()) {
1033 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001034 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001035}
1036
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001037CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1038 Expr *Base, QualType BaseType,
1039 bool IsArrow,
1040 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001041 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001042 NamedDecl *FirstQualifierFoundInScope,
1043 DeclarationNameInfo MemberNameInfo)
1044 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001045 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001046 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001047 (QualifierLoc &&
1048 QualifierLoc.getNestedNameSpecifier()->
1049 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001050 MemberNameInfo.containsUnexpandedParameterPack())),
1051 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001052 HasTemplateKWAndArgsInfo(false),
1053 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001054 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1055 MemberNameInfo(MemberNameInfo) { }
1056
John McCall865d4472009-11-19 22:55:06 +00001057CXXDependentScopeMemberExpr *
1058CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001059 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001060 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001061 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001062 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001063 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001064 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001065 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001066 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001067 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1068 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001069 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001070 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001071 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001073 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1074 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1075 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001076
Chris Lattner32488542010-10-30 05:14:06 +00001077 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001078 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1079 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001080 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001081 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001082 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001083 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001084}
1085
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001086CXXDependentScopeMemberExpr *
1087CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001088 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001089 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001090 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001091 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001092 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001093 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001094 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001095
1096 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001097 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001098 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001099 CXXDependentScopeMemberExpr *E
1100 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001101 0, SourceLocation(),
1102 NestedNameSpecifierLoc(),
1103 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001104 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001105 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001106 return E;
1107}
1108
Douglas Gregor4c9be892011-02-28 20:01:57 +00001109bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1110 if (Base == 0)
1111 return true;
1112
Douglas Gregor75e85042011-03-02 21:06:53 +00001113 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001114}
1115
John McCall864c0412011-04-26 20:42:42 +00001116static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1117 UnresolvedSetIterator end) {
1118 do {
1119 NamedDecl *decl = *begin;
1120 if (isa<UnresolvedUsingValueDecl>(decl))
1121 return false;
1122 if (isa<UsingShadowDecl>(decl))
1123 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1124
1125 // Unresolved member expressions should only contain methods and
1126 // method templates.
1127 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1128
1129 if (isa<FunctionTemplateDecl>(decl))
1130 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1131 if (cast<CXXMethodDecl>(decl)->isStatic())
1132 return false;
1133 } while (++begin != end);
1134
1135 return true;
1136}
1137
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001138UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001139 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001140 Expr *Base, QualType BaseType,
1141 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001142 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001143 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001144 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001145 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001146 const TemplateArgumentListInfo *TemplateArgs,
1147 UnresolvedSetIterator Begin,
1148 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001149 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1150 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001151 // Dependent
1152 ((Base && Base->isTypeDependent()) ||
1153 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001154 ((Base && Base->isInstantiationDependent()) ||
1155 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001156 // Contains unexpanded parameter pack
1157 ((Base && Base->containsUnexpandedParameterPack()) ||
1158 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001159 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1160 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001161
1162 // Check whether all of the members are non-static member functions,
1163 // and if so, mark give this bound-member type instead of overload type.
1164 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1165 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001166}
1167
Douglas Gregor4c9be892011-02-28 20:01:57 +00001168bool UnresolvedMemberExpr::isImplicitAccess() const {
1169 if (Base == 0)
1170 return true;
1171
Douglas Gregor75e85042011-03-02 21:06:53 +00001172 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001173}
1174
John McCall129e2df2009-11-30 22:42:35 +00001175UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001176UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001177 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001178 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001179 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001180 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001181 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001182 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001183 const TemplateArgumentListInfo *TemplateArgs,
1184 UnresolvedSetIterator Begin,
1185 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001186 std::size_t size = sizeof(UnresolvedMemberExpr);
1187 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001188 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1189 else if (TemplateKWLoc.isValid())
1190 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001191
Chris Lattner32488542010-10-30 05:14:06 +00001192 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001193 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001194 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001195 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001196 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001197}
1198
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001199UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001200UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001201 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001202 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001203 if (HasTemplateKWAndArgsInfo)
1204 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001205
Chris Lattner32488542010-10-30 05:14:06 +00001206 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001207 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001208 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001209 return E;
1210}
1211
John McCallc373d482010-01-27 01:50:18 +00001212CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1213 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1214
1215 // If there was a nested name specifier, it names the naming class.
1216 // It can't be dependent: after all, we were actually able to do the
1217 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001218 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001219 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001220 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001221 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001222 Record = T->getAsCXXRecordDecl();
1223 assert(Record && "qualifier in member expression does not name record");
1224 }
John McCallc373d482010-01-27 01:50:18 +00001225 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001226 else {
John McCallc373d482010-01-27 01:50:18 +00001227 QualType BaseType = getBaseType().getNonReferenceType();
1228 if (isArrow()) {
1229 const PointerType *PT = BaseType->getAs<PointerType>();
1230 assert(PT && "base of arrow member access is not pointer");
1231 BaseType = PT->getPointeeType();
1232 }
1233
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001234 Record = BaseType->getAsCXXRecordDecl();
1235 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001236 }
1237
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001238 return Record;
John McCallc373d482010-01-27 01:50:18 +00001239}
1240
Douglas Gregorc7793c72011-01-15 01:15:58 +00001241SubstNonTypeTemplateParmPackExpr::
1242SubstNonTypeTemplateParmPackExpr(QualType T,
1243 NonTypeTemplateParmDecl *Param,
1244 SourceLocation NameLoc,
1245 const TemplateArgument &ArgPack)
1246 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001247 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001248 Param(Param), Arguments(ArgPack.pack_begin()),
1249 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1250
1251TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1252 return TemplateArgument(Arguments, NumArguments);
1253}
1254
David Blaikie99ba9e32011-12-20 02:48:34 +00001255void ArrayTypeTraitExpr::anchor() { }