blob: f52f88105058de8b22ba3b7d0e55b5c69eff2f08 [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 Gregor01d08012012-02-07 10:09:13 +0000767 SourceLocation ClosingBrace)
768 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
769 T->isDependentType(), T->isDependentType(), T->isDependentType(),
770 /*ContainsUnexpandedParameterPack=*/false),
771 IntroducerRange(IntroducerRange),
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000772 NumCaptures(Captures.size()),
Douglas Gregor01d08012012-02-07 10:09:13 +0000773 CaptureDefault(CaptureDefault),
774 ExplicitParams(ExplicitParams),
Douglas Gregordfca6f52012-02-13 22:00:16 +0000775 ExplicitResultType(ExplicitResultType),
Douglas Gregor01d08012012-02-07 10:09:13 +0000776 ClosingBrace(ClosingBrace)
777{
778 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorda8962a2012-02-13 15:44:47 +0000779 CXXRecordDecl *Class = getLambdaClass();
780 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorda8962a2012-02-13 15:44:47 +0000781
782 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000783
784 // Copy captures.
785 ASTContext &Context = Class->getASTContext();
786 Data.NumCaptures = NumCaptures;
787 Data.NumExplicitCaptures = 0;
788 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
789 Capture *ToCapture = Data.Captures;
790 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
791 if (Captures[I].isExplicit())
792 ++Data.NumExplicitCaptures;
793
794 *ToCapture++ = Captures[I];
795 }
796
797 // Copy initialization expressions for the non-static data members.
798 Stmt **Stored = getStoredStmts();
799 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
800 *Stored++ = CaptureInits[I];
801
802 // Copy the body of the lambda.
803 *Stored++ = getCallOperator()->getBody();
804
805 // Copy the array index variables, if any.
806 HasArrayIndexVars = !ArrayIndexVars.empty();
807 if (HasArrayIndexVars) {
808 assert(ArrayIndexStarts.size() == NumCaptures);
809 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
810 sizeof(VarDecl *) * ArrayIndexVars.size());
811 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
812 sizeof(unsigned) * Captures.size());
813 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
814 }
Douglas Gregor01d08012012-02-07 10:09:13 +0000815}
816
817LambdaExpr *LambdaExpr::Create(ASTContext &Context,
818 CXXRecordDecl *Class,
819 SourceRange IntroducerRange,
820 LambdaCaptureDefault CaptureDefault,
821 ArrayRef<Capture> Captures,
822 bool ExplicitParams,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000823 bool ExplicitResultType,
Douglas Gregor01d08012012-02-07 10:09:13 +0000824 ArrayRef<Expr *> CaptureInits,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000825 ArrayRef<VarDecl *> ArrayIndexVars,
826 ArrayRef<unsigned> ArrayIndexStarts,
Douglas Gregor01d08012012-02-07 10:09:13 +0000827 SourceLocation ClosingBrace) {
828 // Determine the type of the expression (i.e., the type of the
829 // function object we're creating).
830 QualType T = Context.getTypeDeclType(Class);
Douglas Gregor01d08012012-02-07 10:09:13 +0000831
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000832 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
833 if (!ArrayIndexVars.empty())
834 Size += sizeof(VarDecl *) * ArrayIndexVars.size()
835 + sizeof(unsigned) * (Captures.size() + 1);
836 void *Mem = Context.Allocate(Size);
837 return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000838 Captures, ExplicitParams, ExplicitResultType,
839 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000840 ClosingBrace);
Douglas Gregor01d08012012-02-07 10:09:13 +0000841}
842
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000843LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
844 unsigned NumArrayIndexVars) {
845 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
846 if (NumArrayIndexVars)
847 Size += sizeof(VarDecl) * NumArrayIndexVars
848 + sizeof(unsigned) * (NumCaptures + 1);
849 void *Mem = C.Allocate(Size);
850 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
851}
852
Douglas Gregorda8962a2012-02-13 15:44:47 +0000853LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000854 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000855}
856
857LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000858 return capture_begin() + NumCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000859}
860
861LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
862 return capture_begin();
863}
864
865LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
866 struct CXXRecordDecl::LambdaDefinitionData &Data
867 = getLambdaClass()->getLambdaData();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000868 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorda8962a2012-02-13 15:44:47 +0000869}
870
871LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
872 return explicit_capture_end();
873}
874
875LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
876 return capture_end();
877}
878
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000879ArrayRef<VarDecl *>
880LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000881 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000882
883 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gay43a1b002012-02-13 19:29:45 +0000884 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
885 "Capture index out-of-range");
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000886 VarDecl **IndexVars = getArrayIndexVars();
887 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000888 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
889 IndexVars + IndexStarts[Index + 1]);
890}
891
Douglas Gregor01d08012012-02-07 10:09:13 +0000892CXXRecordDecl *LambdaExpr::getLambdaClass() const {
893 return getType()->getAsCXXRecordDecl();
894}
895
896CXXMethodDecl *LambdaExpr::getCallOperator() const {
897 CXXRecordDecl *Record = getLambdaClass();
898 DeclarationName Name
899 = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
900 DeclContext::lookup_result Calls = Record->lookup(Name);
901 assert(Calls.first != Calls.second && "Missing lambda call operator!");
902 CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
903 assert(Calls.first == Calls.second && "More than lambda one call operator?");
904 return Result;
905}
906
Douglas Gregor9d36f5d2012-02-14 17:54:36 +0000907CompoundStmt *LambdaExpr::getBody() const {
908 if (!getStoredStmts()[NumCaptures])
909 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
910
911 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
912}
913
Douglas Gregor01d08012012-02-07 10:09:13 +0000914bool LambdaExpr::isMutable() const {
915 return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
916}
917
John McCall80ee6e82011-11-10 05:35:25 +0000918ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
919 ArrayRef<CleanupObject> objects)
John McCall4765fa02010-12-06 08:20:24 +0000920 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCallf89e55a2010-11-18 06:31:45 +0000921 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000922 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000923 subexpr->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000924 subexpr->containsUnexpandedParameterPack()),
John McCall80ee6e82011-11-10 05:35:25 +0000925 SubExpr(subexpr) {
926 ExprWithCleanupsBits.NumObjects = objects.size();
927 for (unsigned i = 0, e = objects.size(); i != e; ++i)
928 getObjectsBuffer()[i] = objects[i];
Anders Carlsson02bbfa32009-04-24 22:47:04 +0000929}
930
John McCall80ee6e82011-11-10 05:35:25 +0000931ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
932 ArrayRef<CleanupObject> objects) {
933 size_t size = sizeof(ExprWithCleanups)
934 + objects.size() * sizeof(CleanupObject);
935 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
936 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnerd2598362010-05-10 00:25:06 +0000937}
938
John McCall80ee6e82011-11-10 05:35:25 +0000939ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
940 : Expr(ExprWithCleanupsClass, empty) {
941 ExprWithCleanupsBits.NumObjects = numObjects;
942}
Chris Lattnerd2598362010-05-10 00:25:06 +0000943
John McCall80ee6e82011-11-10 05:35:25 +0000944ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
945 unsigned numObjects) {
946 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
947 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
948 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson88eaf072009-05-30 22:38:53 +0000949}
950
Douglas Gregorab6677e2010-09-08 00:15:04 +0000951CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000952 SourceLocation LParenLoc,
953 Expr **Args,
954 unsigned NumArgs,
955 SourceLocation RParenLoc)
Douglas Gregorab6677e2010-09-08 00:15:04 +0000956 : Expr(CXXUnresolvedConstructExprClass,
957 Type->getType().getNonReferenceType(),
Douglas Gregor032c8692011-07-08 15:50:43 +0000958 (Type->getType()->isLValueReferenceType() ? VK_LValue
959 :Type->getType()->isRValueReferenceType()? VK_XValue
960 :VK_RValue),
961 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +0000962 Type->getType()->isDependentType(), true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000963 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregorab6677e2010-09-08 00:15:04 +0000964 Type(Type),
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000965 LParenLoc(LParenLoc),
966 RParenLoc(RParenLoc),
967 NumArgs(NumArgs) {
968 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000969 for (unsigned I = 0; I != NumArgs; ++I) {
970 if (Args[I]->containsUnexpandedParameterPack())
971 ExprBits.ContainsUnexpandedParameterPack = true;
972
973 StoredArgs[I] = Args[I];
974 }
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000975}
976
977CXXUnresolvedConstructExpr *
Mike Stump1eb44332009-09-09 15:08:12 +0000978CXXUnresolvedConstructExpr::Create(ASTContext &C,
Douglas Gregorab6677e2010-09-08 00:15:04 +0000979 TypeSourceInfo *Type,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000980 SourceLocation LParenLoc,
981 Expr **Args,
982 unsigned NumArgs,
983 SourceLocation RParenLoc) {
984 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
985 sizeof(Expr *) * NumArgs);
Douglas Gregorab6677e2010-09-08 00:15:04 +0000986 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
Douglas Gregord81e6ca2009-05-20 18:46:25 +0000987 Args, NumArgs, RParenLoc);
988}
989
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000990CXXUnresolvedConstructExpr *
991CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
992 Stmt::EmptyShell Empty;
993 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
994 sizeof(Expr *) * NumArgs);
995 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
996}
997
Douglas Gregorab6677e2010-09-08 00:15:04 +0000998SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
999 return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
1000}
1001
John McCall865d4472009-11-19 22:55:06 +00001002CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001003 Expr *Base, QualType BaseType,
1004 bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001005 SourceLocation OperatorLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001006 NestedNameSpecifierLoc QualifierLoc,
1007 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001008 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001009 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001010 const TemplateArgumentListInfo *TemplateArgs)
John McCallf89e55a2010-11-18 06:31:45 +00001011 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001012 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001013 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001014 (QualifierLoc &&
1015 QualifierLoc.getNestedNameSpecifier()
1016 ->containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001017 MemberNameInfo.containsUnexpandedParameterPack())),
John McCallaa81e162009-12-01 22:10:20 +00001018 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001019 HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001020 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001021 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnara25777432010-08-11 22:01:17 +00001022 MemberNameInfo(MemberNameInfo) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001023 if (TemplateArgs) {
1024 bool Dependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001025 bool InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001026 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001027 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1028 Dependent,
1029 InstantiationDependent,
1030 ContainsUnexpandedParameterPack);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001031 if (ContainsUnexpandedParameterPack)
1032 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001033 } else if (TemplateKWLoc.isValid()) {
1034 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001035 }
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001036}
1037
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001038CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
1039 Expr *Base, QualType BaseType,
1040 bool IsArrow,
1041 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001042 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001043 NamedDecl *FirstQualifierFoundInScope,
1044 DeclarationNameInfo MemberNameInfo)
1045 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor561f8122011-07-01 01:22:09 +00001046 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001047 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001048 (QualifierLoc &&
1049 QualifierLoc.getNestedNameSpecifier()->
1050 containsUnexpandedParameterPack()) ||
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001051 MemberNameInfo.containsUnexpandedParameterPack())),
1052 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001053 HasTemplateKWAndArgsInfo(false),
1054 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001055 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1056 MemberNameInfo(MemberNameInfo) { }
1057
John McCall865d4472009-11-19 22:55:06 +00001058CXXDependentScopeMemberExpr *
1059CXXDependentScopeMemberExpr::Create(ASTContext &C,
John McCallaa81e162009-12-01 22:10:20 +00001060 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001061 SourceLocation OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001062 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001063 SourceLocation TemplateKWLoc,
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001064 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001065 DeclarationNameInfo MemberNameInfo,
John McCalld5532b62009-11-23 01:53:49 +00001066 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001067 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCallaa81e162009-12-01 22:10:20 +00001068 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1069 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001070 QualifierLoc,
John McCallaa81e162009-12-01 22:10:20 +00001071 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001072 MemberNameInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001074 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1075 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1076 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCalld5532b62009-11-23 01:53:49 +00001077
Chris Lattner32488542010-10-30 05:14:06 +00001078 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCallaa81e162009-12-01 22:10:20 +00001079 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1080 IsArrow, OperatorLoc,
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001081 QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001082 TemplateKWLoc,
John McCallaa81e162009-12-01 22:10:20 +00001083 FirstQualifierFoundInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +00001084 MemberNameInfo, TemplateArgs);
Douglas Gregor3b6afbb2009-09-09 00:23:06 +00001085}
1086
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001087CXXDependentScopeMemberExpr *
1088CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001089 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001090 unsigned NumTemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001091 if (!HasTemplateKWAndArgsInfo)
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001092 return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001093 0, SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +00001094 NestedNameSpecifierLoc(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001095 DeclarationNameInfo());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001096
1097 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001098 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner32488542010-10-30 05:14:06 +00001099 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001100 CXXDependentScopeMemberExpr *E
1101 = new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001102 0, SourceLocation(),
1103 NestedNameSpecifierLoc(),
1104 SourceLocation(), 0,
Abramo Bagnara25777432010-08-11 22:01:17 +00001105 DeclarationNameInfo(), 0);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001106 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +00001107 return E;
1108}
1109
Douglas Gregor4c9be892011-02-28 20:01:57 +00001110bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1111 if (Base == 0)
1112 return true;
1113
Douglas Gregor75e85042011-03-02 21:06:53 +00001114 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001115}
1116
John McCall864c0412011-04-26 20:42:42 +00001117static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1118 UnresolvedSetIterator end) {
1119 do {
1120 NamedDecl *decl = *begin;
1121 if (isa<UnresolvedUsingValueDecl>(decl))
1122 return false;
1123 if (isa<UsingShadowDecl>(decl))
1124 decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
1125
1126 // Unresolved member expressions should only contain methods and
1127 // method templates.
1128 assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
1129
1130 if (isa<FunctionTemplateDecl>(decl))
1131 decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
1132 if (cast<CXXMethodDecl>(decl)->isStatic())
1133 return false;
1134 } while (++begin != end);
1135
1136 return true;
1137}
1138
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001139UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001140 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001141 Expr *Base, QualType BaseType,
1142 bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001143 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001144 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001145 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001146 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001147 const TemplateArgumentListInfo *TemplateArgs,
1148 UnresolvedSetIterator Begin,
1149 UnresolvedSetIterator End)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001150 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1151 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001152 // Dependent
1153 ((Base && Base->isTypeDependent()) ||
1154 BaseType->isDependentType()),
Douglas Gregor561f8122011-07-01 01:22:09 +00001155 ((Base && Base->isInstantiationDependent()) ||
1156 BaseType->isInstantiationDependentType()),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001157 // Contains unexpanded parameter pack
1158 ((Base && Base->containsUnexpandedParameterPack()) ||
1159 BaseType->containsUnexpandedParameterPack())),
John McCall7bb12da2010-02-02 06:20:04 +00001160 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1161 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall864c0412011-04-26 20:42:42 +00001162
1163 // Check whether all of the members are non-static member functions,
1164 // and if so, mark give this bound-member type instead of overload type.
1165 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1166 setType(C.BoundMemberTy);
John McCall129e2df2009-11-30 22:42:35 +00001167}
1168
Douglas Gregor4c9be892011-02-28 20:01:57 +00001169bool UnresolvedMemberExpr::isImplicitAccess() const {
1170 if (Base == 0)
1171 return true;
1172
Douglas Gregor75e85042011-03-02 21:06:53 +00001173 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor4c9be892011-02-28 20:01:57 +00001174}
1175
John McCall129e2df2009-11-30 22:42:35 +00001176UnresolvedMemberExpr *
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001177UnresolvedMemberExpr::Create(ASTContext &C,
John McCall129e2df2009-11-30 22:42:35 +00001178 bool HasUnresolvedUsing,
John McCallaa81e162009-12-01 22:10:20 +00001179 Expr *Base, QualType BaseType, bool IsArrow,
John McCall129e2df2009-11-30 22:42:35 +00001180 SourceLocation OperatorLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001181 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001182 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001183 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001184 const TemplateArgumentListInfo *TemplateArgs,
1185 UnresolvedSetIterator Begin,
1186 UnresolvedSetIterator End) {
John McCall129e2df2009-11-30 22:42:35 +00001187 std::size_t size = sizeof(UnresolvedMemberExpr);
1188 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001189 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1190 else if (TemplateKWLoc.isValid())
1191 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall129e2df2009-11-30 22:42:35 +00001192
Chris Lattner32488542010-10-30 05:14:06 +00001193 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregor928e6fc2010-05-23 19:36:40 +00001194 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001195 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001196 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001197 MemberNameInfo, TemplateArgs, Begin, End);
John McCall129e2df2009-11-30 22:42:35 +00001198}
1199
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001200UnresolvedMemberExpr *
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001201UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
Douglas Gregordef03542011-02-04 12:01:24 +00001202 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001203 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001204 if (HasTemplateKWAndArgsInfo)
1205 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001206
Chris Lattner32488542010-10-30 05:14:06 +00001207 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001208 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001209 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisa77eb082010-06-25 09:03:26 +00001210 return E;
1211}
1212
John McCallc373d482010-01-27 01:50:18 +00001213CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1214 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1215
1216 // If there was a nested name specifier, it names the naming class.
1217 // It can't be dependent: after all, we were actually able to do the
1218 // lookup.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001219 CXXRecordDecl *Record = 0;
John McCall7bb12da2010-02-02 06:20:04 +00001220 if (getQualifier()) {
John McCallf4c73712011-01-19 06:33:43 +00001221 const Type *T = getQualifier()->getAsType();
John McCallc373d482010-01-27 01:50:18 +00001222 assert(T && "qualifier in member expression does not name type");
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001223 Record = T->getAsCXXRecordDecl();
1224 assert(Record && "qualifier in member expression does not name record");
1225 }
John McCallc373d482010-01-27 01:50:18 +00001226 // Otherwise the naming class must have been the base class.
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001227 else {
John McCallc373d482010-01-27 01:50:18 +00001228 QualType BaseType = getBaseType().getNonReferenceType();
1229 if (isArrow()) {
1230 const PointerType *PT = BaseType->getAs<PointerType>();
1231 assert(PT && "base of arrow member access is not pointer");
1232 BaseType = PT->getPointeeType();
1233 }
1234
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001235 Record = BaseType->getAsCXXRecordDecl();
1236 assert(Record && "base of member expression does not name record");
John McCallc373d482010-01-27 01:50:18 +00001237 }
1238
Douglas Gregorc96be1e2010-04-27 18:19:34 +00001239 return Record;
John McCallc373d482010-01-27 01:50:18 +00001240}
1241
Douglas Gregorc7793c72011-01-15 01:15:58 +00001242SubstNonTypeTemplateParmPackExpr::
1243SubstNonTypeTemplateParmPackExpr(QualType T,
1244 NonTypeTemplateParmDecl *Param,
1245 SourceLocation NameLoc,
1246 const TemplateArgument &ArgPack)
1247 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00001248 true, true, true, true),
Douglas Gregorc7793c72011-01-15 01:15:58 +00001249 Param(Param), Arguments(ArgPack.pack_begin()),
1250 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1251
1252TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1253 return TemplateArgument(Arguments, NumArguments);
1254}
1255
David Blaikie99ba9e32011-12-20 02:48:34 +00001256void ArrayTypeTraitExpr::anchor() { }