blob: 09ab8c916abd7dedec4dccdd52a190f1bb744476 [file] [log] [blame]
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
Ted Kremenek4b7c9832008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Valifad9e132013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor7a39dd02010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlssonfb311762009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregor76852c22011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregor802ab452009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregor7d7e6722008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregorfdfab6b2008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahanianfaebcbb2009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek4b7c9832008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor72c3f312008-12-05 18:15:24 +000030
David Blaikie99ba9e32011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor1e68ecc2012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
Richard Smithc2d77572013-08-30 04:46:40 +000038void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
39 ExternalASTSource *Source = C.getExternalSource();
40 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
41 assert(Source && "getFromExternalSource with no external source");
42
43 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
44 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
45 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
46 Impl.Decls.setLazy(false);
47}
48
John McCall86ff3082010-02-04 22:26:26 +000049CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith7d04d3a2012-11-30 05:11:39 +000050 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedman97c134e2009-08-15 22:23:00 +000051 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruthec997dc2011-04-30 10:07:30 +000052 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carrutha8225442011-04-30 09:17:45 +000053 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidis4fe19b52012-01-26 18:28:08 +000054 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smithd5bc8672012-12-08 02:01:17 +000055 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smithbc2a35d2012-12-08 08:32:28 +000056 NeedOverloadResolutionForMoveConstructor(false),
57 NeedOverloadResolutionForMoveAssignment(false),
58 NeedOverloadResolutionForDestructor(false),
59 DefaultedMoveConstructorIsDeleted(false),
60 DefaultedMoveAssignmentIsDeleted(false),
61 DefaultedDestructorIsDeleted(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000062 HasTrivialSpecialMembers(SMF_All),
Richard Smithac713512012-12-08 02:53:02 +000063 DeclaredNonTrivialSpecialMembers(0),
Richard Smith7d04d3a2012-11-30 05:11:39 +000064 HasIrrelevantDestructor(true),
Richard Smith61802452011-12-22 02:22:31 +000065 HasConstexprNonCopyMoveConstructor(false),
66 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith7d04d3a2012-11-30 05:11:39 +000067 HasConstexprDefaultConstructor(false),
Sean Hunt023df372011-05-09 18:22:59 +000068 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith7d04d3a2012-11-30 05:11:39 +000069 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithacf796b2012-11-28 06:23:12 +000070 ImplicitCopyConstructorHasConstParam(true),
71 ImplicitCopyAssignmentHasConstParam(true),
72 HasDeclaredCopyConstructorWithConstParam(false),
73 HasDeclaredCopyAssignmentWithConstParam(false),
74 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
75 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith4fc50892013-06-26 02:41:25 +000076 Definition(D), FirstFriend() {
John McCall86ff3082010-02-04 22:26:26 +000077}
78
Benjamin Krameree3096a2012-07-04 17:03:33 +000079CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
80 return Bases.get(Definition->getASTContext().getExternalSource());
81}
82
83CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
84 return VBases.get(Definition->getASTContext().getExternalSource());
85}
86
John McCall86ff3082010-02-04 22:26:26 +000087CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000088 SourceLocation StartLoc, SourceLocation IdLoc,
89 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
90 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall86ff3082010-02-04 22:26:26 +000091 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregord475b8d2009-03-25 21:17:03 +000092 TemplateOrInstantiation() { }
Douglas Gregor7d7e6722008-11-12 23:21:09 +000093
Jay Foad4ba2a172011-01-12 09:06:06 +000094CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000095 DeclContext *DC, SourceLocation StartLoc,
96 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregoraafc0cc2009-05-15 19:11:46 +000097 CXXRecordDecl* PrevDecl,
98 bool DelayTypeCreation) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +000099 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
100 Id, PrevDecl);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000103 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000104 if (!DelayTypeCreation)
Mike Stump1eb44332009-09-09 15:08:12 +0000105 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000106 return R;
107}
108
Douglas Gregorda8962a2012-02-13 15:44:47 +0000109CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedman8da8a662012-09-19 01:18:11 +0000110 TypeSourceInfo *Info, SourceLocation Loc,
111 bool Dependent) {
Douglas Gregorda8962a2012-02-13 15:44:47 +0000112 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
113 0, 0);
114 R->IsBeingDefined = true;
Eli Friedman8da8a662012-09-19 01:18:11 +0000115 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregor6bd99292013-02-09 01:35:03 +0000116 R->MayHaveOutOfDateDef = false;
James Dennette3efec22013-09-05 17:46:21 +0000117 R->setImplicit(true);
Douglas Gregorda8962a2012-02-13 15:44:47 +0000118 C.getTypeDeclType(R, /*PrevDecl=*/0);
119 return R;
120}
121
Douglas Gregor1e68ecc2012-01-05 21:55:30 +0000122CXXRecordDecl *
123CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
124 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor6bd99292013-02-09 01:35:03 +0000125 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
126 SourceLocation(), SourceLocation(),
127 0, 0);
128 R->MayHaveOutOfDateDef = false;
129 return R;
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +0000130}
131
Mike Stump1eb44332009-09-09 15:08:12 +0000132void
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000133CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor57c856b2008-10-23 18:13:27 +0000134 unsigned NumBases) {
Douglas Gregor2d5b7032010-02-11 01:30:34 +0000135 ASTContext &C = getASTContext();
Douglas Gregor64bffa92008-11-05 16:20:31 +0000136
Douglas Gregor7c789c12010-10-29 22:39:52 +0000137 if (!data().Bases.isOffset() && data().NumBases > 0)
138 C.Deallocate(data().getBases());
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Richard Smithdd677232011-10-18 20:08:55 +0000140 if (NumBases) {
141 // C++ [dcl.init.aggr]p1:
142 // An aggregate is [...] a class with [...] no base classes [...].
143 data().Aggregate = false;
144
145 // C++ [class]p4:
146 // A POD-struct is an aggregate class...
147 data().PlainOldData = false;
148 }
149
Anders Carlsson6f6de732010-03-29 05:13:12 +0000150 // The set of seen virtual base types.
Anders Carlsson1c363932010-03-29 19:49:09 +0000151 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000152
153 // The virtual bases of this class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCall86ff3082010-02-04 22:26:26 +0000156 data().Bases = new(C) CXXBaseSpecifier [NumBases];
157 data().NumBases = NumBases;
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000158 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregor7c789c12010-10-29 22:39:52 +0000159 data().getBases()[i] = *Bases[i];
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000160 // Keep track of inherited vbases for this base class.
161 const CXXBaseSpecifier *Base = Bases[i];
162 QualType BaseType = Base->getType();
Douglas Gregor5fe8c042010-02-27 00:25:28 +0000163 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000164 if (BaseType->isDependentType())
165 continue;
166 CXXRecordDecl *BaseClassDecl
Ted Kremenek6217b802009-07-29 21:53:49 +0000167 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlsson6f6de732010-03-29 05:13:12 +0000168
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000169 // A class with a non-empty base class is not empty.
170 // FIXME: Standard ref?
Chandler Carrutha8225442011-04-30 09:17:45 +0000171 if (!BaseClassDecl->isEmpty()) {
172 if (!data().Empty) {
173 // C++0x [class]p7:
174 // A standard-layout class is a class that:
175 // [...]
176 // -- either has no non-static data members in the most derived
177 // class and at most one base class with non-static data members,
178 // or has no base classes with non-static data members, and
179 // If this is the second non-empty base, then neither of these two
180 // clauses can be true.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000181 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000182 }
183
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000184 data().Empty = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000185 data().HasNoNonEmptyBases = false;
186 }
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000187
Douglas Gregor85606eb2010-09-28 20:50:54 +0000188 // C++ [class.virtual]p1:
189 // A class that declares or inherits a virtual function is called a
190 // polymorphic class.
191 if (BaseClassDecl->isPolymorphic())
192 data().Polymorphic = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000193
Chandler Carrutha8225442011-04-30 09:17:45 +0000194 // C++0x [class]p7:
195 // A standard-layout class is a class that: [...]
196 // -- has no non-standard-layout base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000197 if (!BaseClassDecl->isStandardLayout())
198 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000199
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000200 // Record if this base is the first non-literal field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000201 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000202 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000203
Anders Carlsson6f6de732010-03-29 05:13:12 +0000204 // Now go through all virtual bases of this base and add them.
Mike Stump1eb44332009-09-09 15:08:12 +0000205 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000206 BaseClassDecl->vbases_begin(),
207 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000208 // Add this base if it's not already in the list.
Richard Smithacf796b2012-11-28 06:23:12 +0000209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlsson6f6de732010-03-29 05:13:12 +0000210 VBases.push_back(VBase);
Richard Smithacf796b2012-11-28 06:23:12 +0000211
212 // C++11 [class.copy]p8:
213 // The implicitly-declared copy constructor for a class X will have
214 // the form 'X::X(const X&)' if each [...] virtual base class B of X
215 // has a copy constructor whose first parameter is of type
216 // 'const B&' or 'const volatile B&' [...]
217 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
218 if (!VBaseDecl->hasCopyConstructorWithConstParam())
219 data().ImplicitCopyConstructorHasConstParam = false;
220 }
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000221 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000222
223 if (Base->isVirtual()) {
224 // Add this base if it's not already in the list.
Anders Carlsson1c363932010-03-29 19:49:09 +0000225 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smithacf796b2012-11-28 06:23:12 +0000226 VBases.push_back(Base);
227
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000228 // C++0x [meta.unary.prop] is_empty:
229 // T is a class type, but not a union type, with ... no virtual base
230 // classes
231 data().Empty = false;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000232
Richard Smith7d04d3a2012-11-30 05:11:39 +0000233 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
234 // A [default constructor, copy/move constructor, or copy/move assignment
235 // operator for a class X] is trivial [...] if:
236 // -- class X has [...] no virtual base classes
237 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carrutha8225442011-04-30 09:17:45 +0000238
239 // C++0x [class]p7:
240 // A standard-layout class is a class that: [...]
241 // -- has [...] no virtual base classes
Chandler Carruthec997dc2011-04-30 10:07:30 +0000242 data().IsStandardLayout = false;
Richard Smith61802452011-12-22 02:22:31 +0000243
244 // C++11 [dcl.constexpr]p4:
245 // In the definition of a constexpr constructor [...]
246 // -- the class shall not have any virtual base classes
247 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000248 } else {
249 // C++ [class.ctor]p5:
Sean Hunt023df372011-05-09 18:22:59 +0000250 // A default constructor is trivial [...] if:
251 // -- all the direct base classes of its class have trivial default
252 // constructors.
253 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
255
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000256 // C++0x [class.copy]p13:
257 // A copy/move constructor for class X is trivial if [...]
258 // [...]
259 // -- the constructor selected to copy/move each direct base class
260 // subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000261 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000262 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000263 // If the base class doesn't have a simple move constructor, we'll eagerly
264 // declare it and perform overload resolution to determine which function
265 // it actually calls. If it does have a simple move constructor, this
266 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000267 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000268 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000269
270 // C++0x [class.copy]p27:
271 // A copy/move assignment operator for class X is trivial if [...]
272 // [...]
273 // -- the assignment operator selected to copy/move each direct base
274 // class subobject is trivial, and
Douglas Gregor85606eb2010-09-28 20:50:54 +0000275 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000276 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000277 // If the base class doesn't have a simple move assignment, we'll eagerly
278 // declare it and perform overload resolution to determine which function
279 // it actually calls. If it does have a simple move assignment, this
280 // check is correct.
Richard Smith426391c2012-11-16 00:53:38 +0000281 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000282 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smith61802452011-12-22 02:22:31 +0000283
284 // C++11 [class.ctor]p6:
Richard Smithde8facc2012-01-11 18:26:05 +0000285 // If that user-written default constructor would satisfy the
Richard Smith61802452011-12-22 02:22:31 +0000286 // requirements of a constexpr constructor, the implicitly-defined
287 // default constructor is constexpr.
288 if (!BaseClassDecl->hasConstexprDefaultConstructor())
289 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlsson6f6de732010-03-29 05:13:12 +0000290 }
Richard Smithac713512012-12-08 02:53:02 +0000291
Douglas Gregor85606eb2010-09-28 20:50:54 +0000292 // C++ [class.ctor]p3:
293 // A destructor is trivial if all the direct base classes of its class
294 // have trivial destructors.
295 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000296 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000297
298 if (!BaseClassDecl->hasIrrelevantDestructor())
299 data().HasIrrelevantDestructor = false;
300
Richard Smithacf796b2012-11-28 06:23:12 +0000301 // C++11 [class.copy]p18:
302 // The implicitly-declared copy assignment oeprator for a class X will
303 // have the form 'X& X::operator=(const X&)' if each direct base class B
304 // of X has a copy assignment operator whose parameter is of type 'const
305 // B&', 'const volatile B&', or 'B' [...]
306 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
307 data().ImplicitCopyAssignmentHasConstParam = false;
308
309 // C++11 [class.copy]p8:
310 // The implicitly-declared copy constructor for a class X will have
311 // the form 'X::X(const X&)' if each direct [...] base class B of X
312 // has a copy constructor whose first parameter is of type
313 // 'const B&' or 'const volatile B&' [...]
314 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
315 data().ImplicitCopyConstructorHasConstParam = false;
316
John McCallf85e1932011-06-15 23:02:42 +0000317 // A class has an Objective-C object member if... or any of its bases
318 // has an Objective-C object member.
319 if (BaseClassDecl->hasObjectMember())
320 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000321
322 if (BaseClassDecl->hasVolatileMember())
323 setHasVolatileMember(true);
John McCallf85e1932011-06-15 23:02:42 +0000324
Douglas Gregor2bb11012011-05-13 01:05:07 +0000325 // Keep track of the presence of mutable fields.
326 if (BaseClassDecl->hasMutableFields())
327 data().HasMutableFields = true;
Richard Smithd5bc8672012-12-08 02:01:17 +0000328
329 if (BaseClassDecl->hasUninitializedReferenceMember())
330 data().HasUninitializedReferenceMember = true;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000331
332 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian40c072f2009-07-10 20:13:23 +0000333 }
Anders Carlsson6f6de732010-03-29 05:13:12 +0000334
335 if (VBases.empty())
336 return;
337
338 // Create base specifier for any direct or indirect virtual bases.
339 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
340 data().NumVBases = VBases.size();
Richard Smithbc2a35d2012-12-08 08:32:28 +0000341 for (int I = 0, E = VBases.size(); I != E; ++I) {
342 QualType Type = VBases[I]->getType();
343 if (!Type->isDependentType())
344 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith9f8ee2e2011-07-12 23:49:11 +0000345 data().getVBases()[I] = *VBases[I];
Richard Smithbc2a35d2012-12-08 08:32:28 +0000346 }
347}
348
349void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
350 // C++11 [class.copy]p11:
351 // A defaulted copy/move constructor for a class X is defined as
352 // deleted if X has:
353 // -- a direct or virtual base class B that cannot be copied/moved [...]
354 // -- a non-static data member of class type M (or array thereof)
355 // that cannot be copied or moved [...]
356 if (!Subobj->hasSimpleMoveConstructor())
357 data().NeedOverloadResolutionForMoveConstructor = true;
358
359 // C++11 [class.copy]p23:
360 // A defaulted copy/move assignment operator for a class X is defined as
361 // deleted if X has:
362 // -- a direct or virtual base class B that cannot be copied/moved [...]
363 // -- a non-static data member of class type M (or array thereof)
364 // that cannot be copied or moved [...]
365 if (!Subobj->hasSimpleMoveAssignment())
366 data().NeedOverloadResolutionForMoveAssignment = true;
367
368 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
369 // A defaulted [ctor or dtor] for a class X is defined as
370 // deleted if X has:
371 // -- any direct or virtual base class [...] has a type with a destructor
372 // that is deleted or inaccessible from the defaulted [ctor or dtor].
373 // -- any non-static data member has a type with a destructor
374 // that is deleted or inaccessible from the defaulted [ctor or dtor].
375 if (!Subobj->hasSimpleDestructor()) {
376 data().NeedOverloadResolutionForMoveConstructor = true;
377 data().NeedOverloadResolutionForDestructor = true;
378 }
Douglas Gregor57c856b2008-10-23 18:13:27 +0000379}
380
Douglas Gregor9edad9b2010-01-14 17:47:39 +0000381/// Callback function for CXXRecordDecl::forallBases that acknowledges
382/// that it saw a base class.
383static bool SawBase(const CXXRecordDecl *, void *) {
384 return true;
385}
386
387bool CXXRecordDecl::hasAnyDependentBases() const {
388 if (!isDependentContext())
389 return false;
390
391 return !forallBases(SawBase, 0);
392}
393
Chandler Carruthb7e95892011-04-23 10:47:28 +0000394bool CXXRecordDecl::isTriviallyCopyable() const {
395 // C++0x [class]p5:
396 // A trivially copyable class is a class that:
397 // -- has no non-trivial copy constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000398 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000399 // -- has no non-trivial move constructors,
Richard Smith426391c2012-11-16 00:53:38 +0000400 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000401 // -- has no non-trivial copy assignment operators,
Richard Smith426391c2012-11-16 00:53:38 +0000402 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000403 // -- has no non-trivial move assignment operators, and
Richard Smith426391c2012-11-16 00:53:38 +0000404 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carruthb7e95892011-04-23 10:47:28 +0000405 // -- has a trivial destructor.
406 if (!hasTrivialDestructor()) return false;
407
408 return true;
409}
410
Douglas Gregor21386642010-09-28 21:55:22 +0000411void CXXRecordDecl::markedVirtualFunctionPure() {
412 // C++ [class.abstract]p2:
413 // A class is abstract if it has at least one pure virtual function.
414 data().Abstract = true;
415}
416
417void CXXRecordDecl::addedMember(Decl *D) {
Joao Matos17d35c32012-08-31 22:18:20 +0000418 if (!D->isImplicit() &&
419 !isa<FieldDecl>(D) &&
420 !isa<IndirectFieldDecl>(D) &&
421 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
422 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
423 data().HasOnlyCMembers = false;
424
425 // Ignore friends and invalid declarations.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000426 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000427 return;
428
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000429 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
430 if (FunTmpl)
431 D = FunTmpl->getTemplatedDecl();
432
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000433 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
434 if (Method->isVirtual()) {
435 // C++ [dcl.init.aggr]p1:
436 // An aggregate is an array or a class with [...] no virtual functions.
437 data().Aggregate = false;
438
439 // C++ [class]p4:
440 // A POD-struct is an aggregate class...
441 data().PlainOldData = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000442
443 // Virtual functions make the class non-empty.
444 // FIXME: Standard ref?
445 data().Empty = false;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000446
447 // C++ [class.virtual]p1:
448 // A class that declares or inherits a virtual function is called a
449 // polymorphic class.
450 data().Polymorphic = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000451
Richard Smith7d04d3a2012-11-30 05:11:39 +0000452 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
453 // A [default constructor, copy/move constructor, or copy/move
454 // assignment operator for a class X] is trivial [...] if:
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000455 // -- class X has no virtual functions [...]
Richard Smith7d04d3a2012-11-30 05:11:39 +0000456 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000457
Chandler Carrutha8225442011-04-30 09:17:45 +0000458 // C++0x [class]p7:
459 // A standard-layout class is a class that: [...]
460 // -- has no virtual functions
Chandler Carruthec997dc2011-04-30 10:07:30 +0000461 data().IsStandardLayout = false;
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000462 }
463 }
Argyrios Kyrtzidis046c03b2010-10-20 23:48:42 +0000464
Richard Smithacf796b2012-11-28 06:23:12 +0000465 // Notify the listener if an implicit member was added after the definition
466 // was completed.
467 if (!isBeingDefined() && D->isImplicit())
468 if (ASTMutationListener *L = getASTMutationListener())
469 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000470
Richard Smith7d04d3a2012-11-30 05:11:39 +0000471 // The kind of special member this declaration is, if any.
472 unsigned SMKind = 0;
473
Richard Smithacf796b2012-11-28 06:23:12 +0000474 // Handle constructors.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000475 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smithacf796b2012-11-28 06:23:12 +0000476 if (!Constructor->isImplicit()) {
477 // Note that we have a user-declared constructor.
478 data().UserDeclaredConstructor = true;
479
480 // C++ [class]p4:
481 // A POD-struct is an aggregate class [...]
482 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
483 // type is technically an aggregate in C++0x since it wouldn't be in 03.
484 data().PlainOldData = false;
485 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000486
Richard Smith017ab772011-09-05 02:13:09 +0000487 // Technically, "user-provided" is only defined for special member
488 // functions, but the intent of the standard is clearly that it should apply
489 // to all functions.
490 bool UserProvided = Constructor->isUserProvided();
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000491
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000492 if (Constructor->isDefaultConstructor()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000493 SMKind |= SMF_DefaultConstructor;
494
495 if (UserProvided)
Sean Huntcdee3fe2011-05-11 22:34:38 +0000496 data().UserProvidedDefaultConstructor = true;
Richard Smithacf796b2012-11-28 06:23:12 +0000497 if (Constructor->isConstexpr())
Richard Smith61802452011-12-22 02:22:31 +0000498 data().HasConstexprDefaultConstructor = true;
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000499 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000500
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000501 if (!FunTmpl) {
Richard Smithacf796b2012-11-28 06:23:12 +0000502 unsigned Quals;
503 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000504 SMKind |= SMF_CopyConstructor;
Richard Smithacf796b2012-11-28 06:23:12 +0000505
506 if (Quals & Qualifiers::Const)
507 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith7d04d3a2012-11-30 05:11:39 +0000508 } else if (Constructor->isMoveConstructor())
509 SMKind |= SMF_MoveConstructor;
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000510 }
Richard Smithacf796b2012-11-28 06:23:12 +0000511
512 // Record if we see any constexpr constructors which are neither copy
513 // nor move constructors.
514 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith6b8bc072011-08-10 18:11:37 +0000515 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000516
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000517 // C++ [dcl.init.aggr]p1:
518 // An aggregate is an array or a class with no user-declared
519 // constructors [...].
Richard Smithc3bf52c2013-04-20 22:23:05 +0000520 // C++11 [dcl.init.aggr]p1:
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000521 // An aggregate is an array or a class with no user-provided
522 // constructors [...].
Richard Smith80ad52f2013-01-02 11:42:31 +0000523 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smithacf796b2012-11-28 06:23:12 +0000524 ? UserProvided : !Constructor->isImplicit())
Sean Hunt37b8c9e2011-05-09 21:45:35 +0000525 data().Aggregate = false;
Douglas Gregor5c0646b2010-09-27 21:17:54 +0000526 }
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000527
Richard Smithacf796b2012-11-28 06:23:12 +0000528 // Handle destructors.
Sean Huntcf34e752011-05-16 22:41:40 +0000529 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000530 SMKind |= SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000531
Richard Smith7d04d3a2012-11-30 05:11:39 +0000532 if (!DD->isImplicit())
Richard Smithacf796b2012-11-28 06:23:12 +0000533 data().HasIrrelevantDestructor = false;
534
Richard Smithacf796b2012-11-28 06:23:12 +0000535 // C++11 [class.dtor]p5:
Richard Smith7d04d3a2012-11-30 05:11:39 +0000536 // A destructor is trivial if [...] the destructor is not virtual.
537 if (DD->isVirtual())
538 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor0ed2e082010-09-27 22:48:58 +0000539 }
Richard Smithacf796b2012-11-28 06:23:12 +0000540
541 // Handle member functions.
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000542 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Sean Huntffe37fd2011-05-25 20:50:04 +0000543 if (Method->isCopyAssignmentOperator()) {
Richard Smith7d04d3a2012-11-30 05:11:39 +0000544 SMKind |= SMF_CopyAssignment;
Richard Smithacf796b2012-11-28 06:23:12 +0000545
546 const ReferenceType *ParamTy =
547 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
548 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
549 data().HasDeclaredCopyAssignmentWithConstParam = true;
Sean Huntffe37fd2011-05-25 20:50:04 +0000550 }
Sean Huntffe37fd2011-05-25 20:50:04 +0000551
Richard Smith7d04d3a2012-11-30 05:11:39 +0000552 if (Method->isMoveAssignmentOperator())
553 SMKind |= SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000554
Douglas Gregore80622f2010-09-29 04:25:11 +0000555 // Keep the list of conversion functions up-to-date.
556 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor109f5fc2013-04-08 17:12:58 +0000557 // FIXME: We use the 'unsafe' accessor for the access specifier here,
558 // because Sema may not have set it yet. That's really just a misdesign
559 // in Sema. However, LLDB *will* have set the access specifier correctly,
560 // and adds declarations after the class is technically completed,
561 // so completeDefinition()'s overriding of the access specifiers doesn't
562 // work.
563 AccessSpecifier AS = Conversion->getAccessUnsafe();
564
Richard Smith7d04d3a2012-11-30 05:11:39 +0000565 if (Conversion->getPrimaryTemplate()) {
566 // We don't record specializations.
Douglas Gregore80622f2010-09-29 04:25:11 +0000567 } else {
Richard Smithc2d77572013-08-30 04:46:40 +0000568 ASTContext &Ctx = getASTContext();
569 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
570 NamedDecl *Primary =
571 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
572 if (Primary->getPreviousDecl())
573 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
574 Primary, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000575 else
Richard Smithc2d77572013-08-30 04:46:40 +0000576 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregore80622f2010-09-29 04:25:11 +0000577 }
578 }
Richard Smithacf796b2012-11-28 06:23:12 +0000579
Richard Smith7d04d3a2012-11-30 05:11:39 +0000580 if (SMKind) {
Richard Smithac713512012-12-08 02:53:02 +0000581 // If this is the first declaration of a special member, we no longer have
582 // an implicit trivial special member.
583 data().HasTrivialSpecialMembers &=
584 data().DeclaredSpecialMembers | ~SMKind;
585
586 if (!Method->isImplicit() && !Method->isUserProvided()) {
587 // This method is user-declared but not user-provided. We can't work out
588 // whether it's trivial yet (not until we get to the end of the class).
589 // We'll handle this method in finishedDefaultedOrDeletedMember.
590 } else if (Method->isTrivial())
591 data().HasTrivialSpecialMembers |= SMKind;
592 else
593 data().DeclaredNonTrivialSpecialMembers |= SMKind;
594
Richard Smith7d04d3a2012-11-30 05:11:39 +0000595 // Note when we have declared a declared special member, and suppress the
596 // implicit declaration of this special member.
597 data().DeclaredSpecialMembers |= SMKind;
598
599 if (!Method->isImplicit()) {
600 data().UserDeclaredSpecialMembers |= SMKind;
601
602 // C++03 [class]p4:
603 // A POD-struct is an aggregate class that has [...] no user-defined
604 // copy assignment operator and no user-defined destructor.
605 //
606 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
607 // aggregates could not have any constructors, clear it even for an
608 // explicitly defaulted or deleted constructor.
609 // type is technically an aggregate in C++0x since it wouldn't be in 03.
610 //
611 // Also, a user-declared move assignment operator makes a class non-POD.
612 // This is an extension in C++03.
613 data().PlainOldData = false;
614 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000615 }
616
Douglas Gregor27c08ab2010-09-27 22:06:20 +0000617 return;
Douglas Gregor1f2023a2009-07-22 18:25:24 +0000618 }
Richard Smith7d04d3a2012-11-30 05:11:39 +0000619
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000620 // Handle non-static data members.
621 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregord61db332011-10-10 17:22:13 +0000622 // C++ [class.bit]p2:
623 // A declaration for a bit-field that omits the identifier declares an
624 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
625 // initialized.
626 if (Field->isUnnamedBitfield())
627 return;
628
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000629 // C++ [dcl.init.aggr]p1:
630 // An aggregate is an array or a class (clause 9) with [...] no
631 // private or protected non-static data members (clause 11).
632 //
633 // A POD must be an aggregate.
634 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
635 data().Aggregate = false;
636 data().PlainOldData = false;
637 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000638
639 // C++0x [class]p7:
640 // A standard-layout class is a class that:
641 // [...]
642 // -- has the same access control for all non-static data members,
643 switch (D->getAccess()) {
644 case AS_private: data().HasPrivateFields = true; break;
645 case AS_protected: data().HasProtectedFields = true; break;
646 case AS_public: data().HasPublicFields = true; break;
David Blaikieb219cfc2011-09-23 05:06:16 +0000647 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carrutha8225442011-04-30 09:17:45 +0000648 };
649 if ((data().HasPrivateFields + data().HasProtectedFields +
650 data().HasPublicFields) > 1)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000651 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000652
Douglas Gregor2bb11012011-05-13 01:05:07 +0000653 // Keep track of the presence of mutable fields.
654 if (Field->isMutable())
655 data().HasMutableFields = true;
656
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000657 // C++0x [class]p9:
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000658 // A POD struct is a class that is both a trivial class and a
659 // standard-layout class, and has no non-static data members of type
660 // non-POD struct, non-POD union (or array of such types).
John McCallf85e1932011-06-15 23:02:42 +0000661 //
662 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
663 // that does not explicitly have no lifetime makes the class a non-POD.
664 // However, we delay setting PlainOldData to false in this case so that
665 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor3fe52ff2012-07-23 04:23:39 +0000666 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCallf85e1932011-06-15 23:02:42 +0000667 // In this case, the class will become a non-POD class when we complete
668 // the definition.
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000669 ASTContext &Context = getASTContext();
670 QualType T = Context.getBaseElementType(Field->getType());
John McCallf85e1932011-06-15 23:02:42 +0000671 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000672 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCallf85e1932011-06-15 23:02:42 +0000673 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
674 setHasObjectMember(true);
Eli Friedman65400522013-07-20 01:06:31 +0000675 } else if (!T.isCXX98PODType(Context))
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000676 data().PlainOldData = false;
John McCallf85e1932011-06-15 23:02:42 +0000677
Chandler Carrutha8225442011-04-30 09:17:45 +0000678 if (T->isReferenceType()) {
Richard Smithd5bc8672012-12-08 02:01:17 +0000679 if (!Field->hasInClassInitializer())
680 data().HasUninitializedReferenceMember = true;
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000681
Chandler Carrutha8225442011-04-30 09:17:45 +0000682 // C++0x [class]p7:
683 // A standard-layout class is a class that:
684 // -- has no non-static data members of type [...] reference,
Chandler Carruthec997dc2011-04-30 10:07:30 +0000685 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000686 }
687
Richard Smith86c3ae42012-02-13 03:54:03 +0000688 // Record if this field is the first non-literal or volatile field or base.
Richard Smitha10b9782013-04-22 15:31:51 +0000689 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruth9b6347c2011-04-24 02:49:34 +0000690 data().HasNonLiteralTypeFieldsOrBases = true;
691
Richard Smith7a614d82011-06-11 17:19:42 +0000692 if (Field->hasInClassInitializer()) {
Richard Smithd079abf2012-05-07 01:07:30 +0000693 data().HasInClassInitializer = true;
694
695 // C++11 [class]p5:
Richard Smith7a614d82011-06-11 17:19:42 +0000696 // A default constructor is trivial if [...] no non-static data member
697 // of its class has a brace-or-equal-initializer.
Richard Smith7d04d3a2012-11-30 05:11:39 +0000698 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith7a614d82011-06-11 17:19:42 +0000699
Richard Smithd079abf2012-05-07 01:07:30 +0000700 // C++11 [dcl.init.aggr]p1:
Richard Smith7a614d82011-06-11 17:19:42 +0000701 // An aggregate is a [...] class with [...] no
702 // brace-or-equal-initializers for non-static data members.
Richard Smithc3bf52c2013-04-20 22:23:05 +0000703 //
704 // This rule was removed in C++1y.
705 if (!getASTContext().getLangOpts().CPlusPlus1y)
706 data().Aggregate = false;
Richard Smith7a614d82011-06-11 17:19:42 +0000707
Richard Smithd079abf2012-05-07 01:07:30 +0000708 // C++11 [class]p10:
Richard Smith7a614d82011-06-11 17:19:42 +0000709 // A POD struct is [...] a trivial class.
710 data().PlainOldData = false;
711 }
712
Richard Smithbc2a35d2012-12-08 08:32:28 +0000713 // C++11 [class.copy]p23:
714 // A defaulted copy/move assignment operator for a class X is defined
715 // as deleted if X has:
716 // -- a non-static data member of reference type
717 if (T->isReferenceType())
718 data().DefaultedMoveAssignmentIsDeleted = true;
719
Douglas Gregor85606eb2010-09-28 20:50:54 +0000720 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
721 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
722 if (FieldRec->getDefinition()) {
Richard Smithbc2a35d2012-12-08 08:32:28 +0000723 addedClassSubobject(FieldRec);
724
725 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
726 // A defaulted [special member] for a class X is defined as
727 // deleted if:
728 // -- X is a union-like class that has a variant member with a
729 // non-trivial [corresponding special member]
730 if (isUnion()) {
731 if (FieldRec->hasNonTrivialMoveConstructor())
732 data().DefaultedMoveConstructorIsDeleted = true;
733 if (FieldRec->hasNonTrivialMoveAssignment())
734 data().DefaultedMoveAssignmentIsDeleted = true;
735 if (FieldRec->hasNonTrivialDestructor())
736 data().DefaultedDestructorIsDeleted = true;
737 }
738
Sean Hunt023df372011-05-09 18:22:59 +0000739 // C++0x [class.ctor]p5:
Richard Smith61802452011-12-22 02:22:31 +0000740 // A default constructor is trivial [...] if:
Sean Hunt023df372011-05-09 18:22:59 +0000741 // -- for all the non-static data members of its class that are of
742 // class type (or array thereof), each such class has a trivial
743 // default constructor.
744 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000745 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000746
747 // C++0x [class.copy]p13:
748 // A copy/move constructor for class X is trivial if [...]
749 // [...]
750 // -- for each non-static data member of X that is of class type (or
751 // an array thereof), the constructor selected to copy/move that
752 // member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000753 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000754 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000755 // If the field doesn't have a simple move constructor, we'll eagerly
756 // declare the move constructor for this class and we'll decide whether
757 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000758 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000759 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000760
761 // C++0x [class.copy]p27:
762 // A copy/move assignment operator for class X is trivial if [...]
763 // [...]
764 // -- for each non-static data member of X that is of class type (or
765 // an array thereof), the assignment operator selected to
766 // copy/move that member is trivial;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000767 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000768 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000769 // If the field doesn't have a simple move assignment, we'll eagerly
770 // declare the move assignment for this class and we'll decide whether
771 // it's trivial then.
Richard Smith426391c2012-11-16 00:53:38 +0000772 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000773 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruth4d6e5a22011-04-23 23:10:33 +0000774
Douglas Gregor85606eb2010-09-28 20:50:54 +0000775 if (!FieldRec->hasTrivialDestructor())
Richard Smith7d04d3a2012-11-30 05:11:39 +0000776 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smithdfefb842012-02-25 07:33:38 +0000777 if (!FieldRec->hasIrrelevantDestructor())
778 data().HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +0000779 if (FieldRec->hasObjectMember())
780 setHasObjectMember(true);
Fariborz Jahanian3ac83d62013-01-25 23:57:05 +0000781 if (FieldRec->hasVolatileMember())
782 setHasVolatileMember(true);
Chandler Carrutha8225442011-04-30 09:17:45 +0000783
784 // C++0x [class]p7:
785 // A standard-layout class is a class that:
786 // -- has no non-static data members of type non-standard-layout
787 // class (or array of such types) [...]
Chandler Carruthec997dc2011-04-30 10:07:30 +0000788 if (!FieldRec->isStandardLayout())
789 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000790
791 // C++0x [class]p7:
792 // A standard-layout class is a class that:
793 // [...]
794 // -- has no base classes of the same type as the first non-static
795 // data member.
796 // We don't want to expend bits in the state of the record decl
797 // tracking whether this is the first non-static data member so we
798 // cheat a bit and use some of the existing state: the empty bit.
799 // Virtual bases and virtual methods make a class non-empty, but they
800 // also make it non-standard-layout so we needn't check here.
801 // A non-empty base class may leave the class standard-layout, but not
802 // if we have arrived here, and have at least on non-static data
Chandler Carruthec997dc2011-04-30 10:07:30 +0000803 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carrutha8225442011-04-30 09:17:45 +0000804 // data member must come through here with Empty still true, and Empty
805 // will subsequently be set to false below.
Chandler Carruthec997dc2011-04-30 10:07:30 +0000806 if (data().IsStandardLayout && data().Empty) {
Chandler Carrutha8225442011-04-30 09:17:45 +0000807 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
808 BE = bases_end();
809 BI != BE; ++BI) {
810 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruthec997dc2011-04-30 10:07:30 +0000811 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000812 break;
813 }
814 }
815 }
Douglas Gregor2bb11012011-05-13 01:05:07 +0000816
817 // Keep track of the presence of mutable fields.
818 if (FieldRec->hasMutableFields())
819 data().HasMutableFields = true;
Richard Smith61802452011-12-22 02:22:31 +0000820
821 // C++11 [class.copy]p13:
822 // If the implicitly-defined constructor would satisfy the
823 // requirements of a constexpr constructor, the implicitly-defined
824 // constructor is constexpr.
825 // C++11 [dcl.constexpr]p4:
826 // -- every constructor involved in initializing non-static data
827 // members [...] shall be a constexpr constructor
828 if (!Field->hasInClassInitializer() &&
Richard Smithd079abf2012-05-07 01:07:30 +0000829 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smith61802452011-12-22 02:22:31 +0000830 // The standard requires any in-class initializer to be a constant
831 // expression. We consider this to be a defect.
832 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithacf796b2012-11-28 06:23:12 +0000833
834 // C++11 [class.copy]p8:
835 // The implicitly-declared copy constructor for a class X will have
836 // the form 'X::X(const X&)' if [...] for all the non-static data
837 // members of X that are of a class type M (or array thereof), each
838 // such class type has a copy constructor whose first parameter is
839 // of type 'const M&' or 'const volatile M&'.
840 if (!FieldRec->hasCopyConstructorWithConstParam())
841 data().ImplicitCopyConstructorHasConstParam = false;
842
843 // C++11 [class.copy]p18:
844 // The implicitly-declared copy assignment oeprator for a class X will
845 // have the form 'X& X::operator=(const X&)' if [...] for all the
846 // non-static data members of X that are of a class type M (or array
847 // thereof), each such class type has a copy assignment operator whose
848 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
849 if (!FieldRec->hasCopyAssignmentWithConstParam())
850 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smithd5bc8672012-12-08 02:01:17 +0000851
852 if (FieldRec->hasUninitializedReferenceMember() &&
853 !Field->hasInClassInitializer())
854 data().HasUninitializedReferenceMember = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000855 }
Richard Smith61802452011-12-22 02:22:31 +0000856 } else {
857 // Base element type of field is a non-class type.
Richard Smitha10b9782013-04-22 15:31:51 +0000858 if (!T->isLiteralType(Context) ||
Richard Smithd3861ce2012-06-10 07:07:24 +0000859 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smith61802452011-12-22 02:22:31 +0000860 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithbc2a35d2012-12-08 08:32:28 +0000861
862 // C++11 [class.copy]p23:
863 // A defaulted copy/move assignment operator for a class X is defined
864 // as deleted if X has:
865 // -- a non-static data member of const non-class type (or array
866 // thereof)
867 if (T.isConstQualified())
868 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor85606eb2010-09-28 20:50:54 +0000869 }
Chandler Carrutha8225442011-04-30 09:17:45 +0000870
871 // C++0x [class]p7:
872 // A standard-layout class is a class that:
873 // [...]
874 // -- either has no non-static data members in the most derived
875 // class and at most one base class with non-static data members,
876 // or has no base classes with non-static data members, and
877 // At this point we know that we have a non-static data member, so the last
878 // clause holds.
879 if (!data().HasNoNonEmptyBases)
Chandler Carruthec997dc2011-04-30 10:07:30 +0000880 data().IsStandardLayout = false;
Chandler Carrutha8225442011-04-30 09:17:45 +0000881
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000882 // If this is not a zero-length bit-field, then the class is not empty.
883 if (data().Empty) {
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000884 if (!Field->isBitField() ||
885 (!Field->getBitWidth()->isTypeDependent() &&
886 !Field->getBitWidth()->isValueDependent() &&
887 Field->getBitWidthValue(Context) != 0))
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000888 data().Empty = false;
Douglas Gregor2cf9d652010-09-28 20:38:10 +0000889 }
Douglas Gregor9fe183a2010-09-28 19:45:33 +0000890 }
Douglas Gregore80622f2010-09-29 04:25:11 +0000891
892 // Handle using declarations of conversion functions.
Richard Smithc2d77572013-08-30 04:46:40 +0000893 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregore80622f2010-09-29 04:25:11 +0000894 if (Shadow->getDeclName().getNameKind()
Richard Smithc2d77572013-08-30 04:46:40 +0000895 == DeclarationName::CXXConversionFunctionName) {
896 ASTContext &Ctx = getASTContext();
897 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
898 }
899 }
Joao Matos17d35c32012-08-31 22:18:20 +0000900}
901
Richard Smithac713512012-12-08 02:53:02 +0000902void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
903 assert(!D->isImplicit() && !D->isUserProvided());
904
905 // The kind of special member this declaration is, if any.
906 unsigned SMKind = 0;
907
908 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
909 if (Constructor->isDefaultConstructor()) {
910 SMKind |= SMF_DefaultConstructor;
911 if (Constructor->isConstexpr())
912 data().HasConstexprDefaultConstructor = true;
913 }
914 if (Constructor->isCopyConstructor())
915 SMKind |= SMF_CopyConstructor;
916 else if (Constructor->isMoveConstructor())
917 SMKind |= SMF_MoveConstructor;
918 else if (Constructor->isConstexpr())
919 // We may now know that the constructor is constexpr.
920 data().HasConstexprNonCopyMoveConstructor = true;
921 } else if (isa<CXXDestructorDecl>(D))
922 SMKind |= SMF_Destructor;
923 else if (D->isCopyAssignmentOperator())
924 SMKind |= SMF_CopyAssignment;
925 else if (D->isMoveAssignmentOperator())
926 SMKind |= SMF_MoveAssignment;
927
928 // Update which trivial / non-trivial special members we have.
929 // addedMember will have skipped this step for this member.
930 if (D->isTrivial())
931 data().HasTrivialSpecialMembers |= SMKind;
932 else
933 data().DeclaredNonTrivialSpecialMembers |= SMKind;
934}
935
Joao Matos17d35c32012-08-31 22:18:20 +0000936bool CXXRecordDecl::isCLike() const {
937 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
938 !TemplateOrInstantiation.isNull())
939 return false;
940 if (!hasDefinition())
941 return true;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000942
Argyrios Kyrtzidisc2214112012-02-01 06:36:44 +0000943 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis277b1562012-01-23 16:58:45 +0000944}
945
Faisal Valifad9e132013-09-26 19:54:12 +0000946bool CXXRecordDecl::isGenericLambda() const {
947 return isLambda() &&
948 getLambdaCallOperator()->getDescribedFunctionTemplate();
949}
950
951CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
952 if (!isLambda()) return 0;
953 DeclarationName Name =
954 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
955 DeclContext::lookup_const_result Calls = lookup(Name);
956
957 assert(!Calls.empty() && "Missing lambda call operator!");
958 assert(Calls.size() == 1 && "More than one lambda call operator!");
959
960 NamedDecl *CallOp = Calls.front();
961 if (FunctionTemplateDecl *CallOpTmpl =
962 dyn_cast<FunctionTemplateDecl>(CallOp))
963 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
964
965 return cast<CXXMethodDecl>(CallOp);
966}
967
968CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
969 if (!isLambda()) return 0;
970 DeclarationName Name =
971 &getASTContext().Idents.get(getLambdaStaticInvokerName());
972 DeclContext::lookup_const_result Invoker = lookup(Name);
973 if (Invoker.empty()) return 0;
974 assert(Invoker.size() == 1 && "More than one static invoker operator!");
975 CXXMethodDecl *Result = cast<CXXMethodDecl>(Invoker.front());
976 return Result;
977
978}
979
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000980void CXXRecordDecl::getCaptureFields(
981 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman41105ad2012-02-11 00:18:00 +0000982 FieldDecl *&ThisCapture) const {
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000983 Captures.clear();
984 ThisCapture = 0;
985
Douglas Gregorda8962a2012-02-13 15:44:47 +0000986 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000987 RecordDecl::field_iterator Field = field_begin();
Douglas Gregor7ae282f2012-02-13 17:20:40 +0000988 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000989 C != CEnd; ++C, ++Field) {
Richard Smith0d8e9642013-05-16 06:20:58 +0000990 if (C->capturesThis())
David Blaikie581deb32012-06-06 20:45:41 +0000991 ThisCapture = *Field;
Richard Smith0d8e9642013-05-16 06:20:58 +0000992 else if (C->capturesVariable())
993 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000994 }
995}
996
Faisal Valifad9e132013-09-26 19:54:12 +0000997TemplateParameterList *
998CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
999 if (!isLambda()) return 0;
1000 CXXMethodDecl *CallOp = getLambdaCallOperator();
1001 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1002 return Tmpl->getTemplateParameters();
1003 return 0;
1004}
Douglas Gregor4d8d22b2012-02-10 07:45:31 +00001005
John McCallb05b5f32010-03-15 09:07:48 +00001006static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1007 QualType T;
John McCall32daa422010-03-31 01:36:47 +00001008 if (isa<UsingShadowDecl>(Conv))
1009 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCallb05b5f32010-03-15 09:07:48 +00001010 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1011 T = ConvTemp->getTemplatedDecl()->getResultType();
1012 else
1013 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1014 return Context.getCanonicalType(T);
Fariborz Jahanian0351a1e2009-10-07 20:43:36 +00001015}
1016
John McCallb05b5f32010-03-15 09:07:48 +00001017/// Collect the visible conversions of a base class.
1018///
James Dennetta1253502012-06-15 22:28:09 +00001019/// \param Record a base class of the class we're considering
John McCallb05b5f32010-03-15 09:07:48 +00001020/// \param InVirtual whether this base class is a virtual base (or a base
1021/// of a virtual base)
1022/// \param Access the access along the inheritance path to this base
1023/// \param ParentHiddenTypes the conversions provided by the inheritors
1024/// of this base
1025/// \param Output the set to which to add conversions from non-virtual bases
1026/// \param VOutput the set to which to add conversions from virtual bases
1027/// \param HiddenVBaseCs the set of conversions which were hidden in a
1028/// virtual base along some inheritance path
1029static void CollectVisibleConversions(ASTContext &Context,
1030 CXXRecordDecl *Record,
1031 bool InVirtual,
1032 AccessSpecifier Access,
1033 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001034 ASTUnresolvedSet &Output,
John McCallb05b5f32010-03-15 09:07:48 +00001035 UnresolvedSetImpl &VOutput,
1036 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1037 // The set of types which have conversions in this class or its
1038 // subclasses. As an optimization, we don't copy the derived set
1039 // unless it might change.
1040 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1041 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1042
1043 // Collect the direct conversions and figure out which conversions
1044 // will be hidden in the subclasses.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001045 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1046 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1047 if (ConvI != ConvE) {
John McCallb05b5f32010-03-15 09:07:48 +00001048 HiddenTypesBuffer = ParentHiddenTypes;
1049 HiddenTypes = &HiddenTypesBuffer;
1050
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001051 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smithf108c632012-05-06 00:04:32 +00001052 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1053 bool Hidden = ParentHiddenTypes.count(ConvType);
1054 if (!Hidden)
1055 HiddenTypesBuffer.insert(ConvType);
John McCallb05b5f32010-03-15 09:07:48 +00001056
1057 // If this conversion is hidden and we're in a virtual base,
1058 // remember that it's hidden along some inheritance path.
1059 if (Hidden && InVirtual)
1060 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1061
1062 // If this conversion isn't hidden, add it to the appropriate output.
1063 else if (!Hidden) {
1064 AccessSpecifier IAccess
1065 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1066
1067 if (InVirtual)
1068 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanian62509212009-09-12 18:26:03 +00001069 else
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001070 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001071 }
1072 }
1073 }
Sebastian Redl9994a342009-10-25 17:03:50 +00001074
John McCallb05b5f32010-03-15 09:07:48 +00001075 // Collect information recursively from any base classes.
1076 for (CXXRecordDecl::base_class_iterator
1077 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1078 const RecordType *RT = I->getType()->getAs<RecordType>();
1079 if (!RT) continue;
Sebastian Redl9994a342009-10-25 17:03:50 +00001080
John McCallb05b5f32010-03-15 09:07:48 +00001081 AccessSpecifier BaseAccess
1082 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1083 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl9994a342009-10-25 17:03:50 +00001084
John McCallb05b5f32010-03-15 09:07:48 +00001085 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1086 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1087 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanian53462782009-09-11 21:44:33 +00001088 }
John McCallb05b5f32010-03-15 09:07:48 +00001089}
Sebastian Redl9994a342009-10-25 17:03:50 +00001090
John McCallb05b5f32010-03-15 09:07:48 +00001091/// Collect the visible conversions of a class.
1092///
1093/// This would be extremely straightforward if it weren't for virtual
1094/// bases. It might be worth special-casing that, really.
1095static void CollectVisibleConversions(ASTContext &Context,
1096 CXXRecordDecl *Record,
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001097 ASTUnresolvedSet &Output) {
John McCallb05b5f32010-03-15 09:07:48 +00001098 // The collection of all conversions in virtual bases that we've
1099 // found. These will be added to the output as long as they don't
1100 // appear in the hidden-conversions set.
1101 UnresolvedSet<8> VBaseCs;
1102
1103 // The set of conversions in virtual bases that we've determined to
1104 // be hidden.
1105 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1106
1107 // The set of types hidden by classes derived from this one.
1108 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1109
1110 // Go ahead and collect the direct conversions and add them to the
1111 // hidden-types set.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001112 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1113 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001114 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001115 for (; ConvI != ConvE; ++ConvI)
1116 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCallb05b5f32010-03-15 09:07:48 +00001117
1118 // Recursively collect conversions from base classes.
1119 for (CXXRecordDecl::base_class_iterator
1120 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1121 const RecordType *RT = I->getType()->getAs<RecordType>();
1122 if (!RT) continue;
1123
1124 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1125 I->isVirtual(), I->getAccessSpecifier(),
1126 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1127 }
1128
1129 // Add any unhidden conversions provided by virtual bases.
1130 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1131 I != E; ++I) {
1132 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001133 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001134 }
Fariborz Jahanian62509212009-09-12 18:26:03 +00001135}
1136
1137/// getVisibleConversionFunctions - get all conversion functions visible
1138/// in current class; including conversion function templates.
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001139std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1140CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smithc2d77572013-08-30 04:46:40 +00001141 ASTContext &Ctx = getASTContext();
1142
1143 ASTUnresolvedSet *Set;
1144 if (bases_begin() == bases_end()) {
1145 // If root class, all conversions are visible.
1146 Set = &data().Conversions.get(Ctx);
1147 } else {
1148 Set = &data().VisibleConversions.get(Ctx);
1149 // If visible conversion list is not evaluated, evaluate it.
1150 if (!data().ComputedVisibleConversions) {
1151 CollectVisibleConversions(Ctx, this, *Set);
1152 data().ComputedVisibleConversions = true;
1153 }
Argyrios Kyrtzidis9d295432012-11-28 03:56:09 +00001154 }
Richard Smithc2d77572013-08-30 04:46:40 +00001155 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanian53462782009-09-11 21:44:33 +00001156}
1157
John McCall32daa422010-03-31 01:36:47 +00001158void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1159 // This operation is O(N) but extremely rare. Sema only uses it to
1160 // remove UsingShadowDecls in a class that were followed by a direct
1161 // declaration, e.g.:
1162 // class A : B {
1163 // using B::operator int;
1164 // operator int();
1165 // };
1166 // This is uncommon by itself and even more uncommon in conjunction
1167 // with sufficiently large numbers of directly-declared conversions
1168 // that asymptotic behavior matters.
1169
Richard Smithc2d77572013-08-30 04:46:40 +00001170 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCall32daa422010-03-31 01:36:47 +00001171 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1172 if (Convs[I].getDecl() == ConvDecl) {
1173 Convs.erase(I);
1174 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1175 && "conversion was found multiple times in unresolved set");
1176 return;
1177 }
1178 }
1179
1180 llvm_unreachable("conversion not found in set!");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001181}
Fariborz Jahanianf8dcb862009-06-19 19:55:27 +00001182
Douglas Gregorf6b11852009-10-08 15:14:33 +00001183CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001184 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001185 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1186
1187 return 0;
1188}
1189
1190void
1191CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1192 TemplateSpecializationKind TSK) {
1193 assert(TemplateOrInstantiation.isNull() &&
1194 "Previous template or instantiation?");
1195 assert(!isa<ClassTemplateSpecializationDecl>(this));
1196 TemplateOrInstantiation
1197 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1198}
1199
Anders Carlssonb13e3572009-12-07 06:33:48 +00001200TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1201 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorf6b11852009-10-08 15:14:33 +00001202 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1203 return Spec->getSpecializationKind();
1204
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001205 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorf6b11852009-10-08 15:14:33 +00001206 return MSInfo->getTemplateSpecializationKind();
1207
1208 return TSK_Undeclared;
1209}
1210
1211void
1212CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1213 if (ClassTemplateSpecializationDecl *Spec
1214 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1215 Spec->setSpecializationKind(TSK);
1216 return;
1217 }
1218
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001219 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00001220 MSInfo->setTemplateSpecializationKind(TSK);
1221 return;
1222 }
1223
David Blaikieb219cfc2011-09-23 05:06:16 +00001224 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorf6b11852009-10-08 15:14:33 +00001225}
1226
Douglas Gregor1d110e02010-07-01 14:13:13 +00001227CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1228 ASTContext &Context = getASTContext();
Anders Carlsson7267c162009-05-29 21:03:38 +00001229 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001230
1231 DeclarationName Name
Douglas Gregor50d62d12009-08-05 05:36:45 +00001232 = Context.DeclarationNames.getCXXDestructorName(
1233 Context.getCanonicalType(ClassType));
Anders Carlsson7267c162009-05-29 21:03:38 +00001234
David Blaikie3bc93e32012-12-19 00:45:41 +00001235 DeclContext::lookup_const_result R = lookup(Name);
1236 if (R.empty())
Sebastian Redld4b25cb2010-09-02 23:19:42 +00001237 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001238
David Blaikie3bc93e32012-12-19 00:45:41 +00001239 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson7267c162009-05-29 21:03:38 +00001240 return Dtor;
1241}
1242
Douglas Gregorda2142f2011-02-19 18:51:44 +00001243void CXXRecordDecl::completeDefinition() {
1244 completeDefinition(0);
1245}
1246
1247void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1248 RecordDecl::completeDefinition();
1249
David Blaikie4e4d0842012-03-11 07:00:24 +00001250 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +00001251 // Objective-C Automatic Reference Counting:
1252 // If a class has a non-static data member of Objective-C pointer
1253 // type (or array thereof), it is a non-POD type and its
Douglas Gregor3fe52ff2012-07-23 04:23:39 +00001254 // default constructor (if any), copy constructor, move constructor,
1255 // copy assignment operator, move assignment operator, and destructor are
1256 // non-trivial.
John McCallf85e1932011-06-15 23:02:42 +00001257 struct DefinitionData &Data = data();
1258 Data.PlainOldData = false;
Richard Smith7d04d3a2012-11-30 05:11:39 +00001259 Data.HasTrivialSpecialMembers = 0;
Richard Smithdfefb842012-02-25 07:33:38 +00001260 Data.HasIrrelevantDestructor = false;
John McCallf85e1932011-06-15 23:02:42 +00001261 }
1262
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001263 // If the class may be abstract (but hasn't been marked as such), check for
1264 // any pure final overriders.
1265 if (mayBeAbstract()) {
1266 CXXFinalOverriderMap MyFinalOverriders;
1267 if (!FinalOverriders) {
1268 getFinalOverriders(MyFinalOverriders);
1269 FinalOverriders = &MyFinalOverriders;
1270 }
1271
1272 bool Done = false;
1273 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1274 MEnd = FinalOverriders->end();
1275 M != MEnd && !Done; ++M) {
1276 for (OverridingMethods::iterator SO = M->second.begin(),
1277 SOEnd = M->second.end();
1278 SO != SOEnd && !Done; ++SO) {
1279 assert(SO->second.size() > 0 &&
1280 "All virtual functions have overridding virtual functions");
1281
1282 // C++ [class.abstract]p4:
1283 // A class is abstract if it contains or inherits at least one
1284 // pure virtual function for which the final overrider is pure
1285 // virtual.
1286 if (SO->second.front().Method->isPure()) {
1287 data().Abstract = true;
1288 Done = true;
1289 break;
1290 }
1291 }
1292 }
1293 }
Douglas Gregore80622f2010-09-29 04:25:11 +00001294
1295 // Set access bits correctly on the directly-declared conversions.
Richard Smithc2d77572013-08-30 04:46:40 +00001296 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregore80622f2010-09-29 04:25:11 +00001297 I != E; ++I)
Argyrios Kyrtzidis2a82ca22012-11-28 03:56:16 +00001298 I.setAccess((*I)->getAccess());
Douglas Gregor7a39dd02010-09-29 00:15:42 +00001299}
1300
1301bool CXXRecordDecl::mayBeAbstract() const {
1302 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1303 isDependentContext())
1304 return false;
1305
1306 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1307 BEnd = bases_end();
1308 B != BEnd; ++B) {
1309 CXXRecordDecl *BaseDecl
1310 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1311 if (BaseDecl->isAbstract())
1312 return true;
1313 }
1314
1315 return false;
1316}
1317
David Blaikie99ba9e32011-12-20 02:48:34 +00001318void CXXMethodDecl::anchor() { }
1319
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001320bool CXXMethodDecl::isStatic() const {
Rafael Espindola72fdc892013-04-15 12:38:20 +00001321 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001322
1323 if (MD->getStorageClass() == SC_Static)
1324 return true;
1325
1326 DeclarationName Name = getDeclName();
1327 // [class.free]p1:
1328 // Any allocation function for a class T is a static member
1329 // (even if not explicitly declared static).
1330 if (Name.getCXXOverloadedOperator() == OO_New ||
1331 Name.getCXXOverloadedOperator() == OO_Array_New)
1332 return true;
1333
1334 // [class.free]p6 Any deallocation function for a class X is a static member
1335 // (even if not explicitly declared static).
1336 if (Name.getCXXOverloadedOperator() == OO_Delete ||
1337 Name.getCXXOverloadedOperator() == OO_Array_Delete)
1338 return true;
1339
1340 return false;
1341}
1342
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001343static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1344 const CXXMethodDecl *BaseMD) {
1345 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1346 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1347 const CXXMethodDecl *MD = *I;
1348 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1349 return true;
1350 if (recursivelyOverrides(MD, BaseMD))
1351 return true;
1352 }
1353 return false;
1354}
1355
1356CXXMethodDecl *
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001357CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1358 bool MayBeBase) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001359 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1360 return this;
1361
1362 // Lookup doesn't work for destructors, so handle them separately.
1363 if (isa<CXXDestructorDecl>(this)) {
1364 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001365 if (MD) {
1366 if (recursivelyOverrides(MD, this))
1367 return MD;
1368 if (MayBeBase && recursivelyOverrides(this, MD))
1369 return MD;
1370 }
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001371 return NULL;
1372 }
1373
1374 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikie3bc93e32012-12-19 00:45:41 +00001375 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001376 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1377 if (!MD)
1378 continue;
1379 if (recursivelyOverrides(MD, this))
1380 return MD;
Jordan Rose4e79fdf2012-08-15 20:07:17 +00001381 if (MayBeBase && recursivelyOverrides(this, MD))
1382 return MD;
Rafael Espindola0b4fe502012-06-26 17:45:31 +00001383 }
1384
1385 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1386 E = RD->bases_end(); I != E; ++I) {
1387 const RecordType *RT = I->getType()->getAs<RecordType>();
1388 if (!RT)
1389 continue;
1390 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1391 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1392 if (T)
1393 return T;
1394 }
1395
1396 return NULL;
1397}
1398
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001399CXXMethodDecl *
1400CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001401 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001402 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001403 QualType T, TypeSourceInfo *TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001404 StorageClass SC, bool isInline,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001405 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001406 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001407 SC, isInline, isConstexpr,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001408 EndLocation);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001409}
1410
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001411CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1412 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1413 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1414 DeclarationNameInfo(), QualType(),
Rafael Espindolad2615cc2013-04-03 19:27:57 +00001415 0, SC_None, false, false,
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001416 SourceLocation());
1417}
1418
Douglas Gregor90916562009-09-29 18:16:17 +00001419bool CXXMethodDecl::isUsualDeallocationFunction() const {
1420 if (getOverloadedOperator() != OO_Delete &&
1421 getOverloadedOperator() != OO_Array_Delete)
1422 return false;
Douglas Gregor6d908702010-02-26 05:06:18 +00001423
1424 // C++ [basic.stc.dynamic.deallocation]p2:
1425 // A template instance is never a usual deallocation function,
1426 // regardless of its signature.
1427 if (getPrimaryTemplate())
1428 return false;
1429
Douglas Gregor90916562009-09-29 18:16:17 +00001430 // C++ [basic.stc.dynamic.deallocation]p2:
1431 // If a class T has a member deallocation function named operator delete
1432 // with exactly one parameter, then that function is a usual (non-placement)
1433 // deallocation function. [...]
1434 if (getNumParams() == 1)
1435 return true;
1436
1437 // C++ [basic.stc.dynamic.deallocation]p2:
1438 // [...] If class T does not declare such an operator delete but does
1439 // declare a member deallocation function named operator delete with
1440 // exactly two parameters, the second of which has type std::size_t (18.1),
1441 // then this function is a usual deallocation function.
1442 ASTContext &Context = getASTContext();
1443 if (getNumParams() != 2 ||
Chandler Carruthe228ba92010-02-08 18:54:05 +00001444 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1445 Context.getSizeType()))
Douglas Gregor90916562009-09-29 18:16:17 +00001446 return false;
1447
1448 // This function is a usual deallocation function if there are no
1449 // single-parameter deallocation functions of the same kind.
David Blaikie3bc93e32012-12-19 00:45:41 +00001450 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1451 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1452 I != E; ++I) {
1453 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregor90916562009-09-29 18:16:17 +00001454 if (FD->getNumParams() == 1)
1455 return false;
1456 }
1457
1458 return true;
1459}
1460
Douglas Gregor06a9f362010-05-01 20:49:11 +00001461bool CXXMethodDecl::isCopyAssignmentOperator() const {
Sean Huntffe37fd2011-05-25 20:50:04 +00001462 // C++0x [class.copy]p17:
Douglas Gregor06a9f362010-05-01 20:49:11 +00001463 // A user-declared copy assignment operator X::operator= is a non-static
1464 // non-template member function of class X with exactly one parameter of
1465 // type X, X&, const X&, volatile X& or const volatile X&.
1466 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1467 /*non-static*/ isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001468 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1469 getNumParams() != 1)
Douglas Gregor06a9f362010-05-01 20:49:11 +00001470 return false;
1471
1472 QualType ParamType = getParamDecl(0)->getType();
1473 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1474 ParamType = Ref->getPointeeType();
1475
1476 ASTContext &Context = getASTContext();
1477 QualType ClassType
1478 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1479 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1480}
1481
Sean Huntffe37fd2011-05-25 20:50:04 +00001482bool CXXMethodDecl::isMoveAssignmentOperator() const {
1483 // C++0x [class.copy]p19:
1484 // A user-declared move assignment operator X::operator= is a non-static
1485 // non-template member function of class X with exactly one parameter of type
1486 // X&&, const X&&, volatile X&&, or const volatile X&&.
1487 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedmanfcb5a252013-07-11 23:55:07 +00001488 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1489 getNumParams() != 1)
Sean Huntffe37fd2011-05-25 20:50:04 +00001490 return false;
1491
1492 QualType ParamType = getParamDecl(0)->getType();
1493 if (!isa<RValueReferenceType>(ParamType))
1494 return false;
1495 ParamType = ParamType->getPointeeType();
1496
1497 ASTContext &Context = getASTContext();
1498 QualType ClassType
1499 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1500 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1501}
1502
Anders Carlsson05eb2442009-05-16 23:58:37 +00001503void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001504 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001505 assert(!MD->getParent()->isDependentContext() &&
1506 "Can't add an overridden method to a class template!");
Eli Friedman540659e2012-03-10 01:39:01 +00001507 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonc076c452010-01-30 17:42:34 +00001508
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001509 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001510}
1511
1512CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001513 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001514 return getASTContext().overridden_methods_begin(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001515}
1516
1517CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001518 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor7d10b7e2010-03-02 23:58:15 +00001519 return getASTContext().overridden_methods_end(this);
Anders Carlsson05eb2442009-05-16 23:58:37 +00001520}
1521
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001522unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman540659e2012-03-10 01:39:01 +00001523 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +00001524 return getASTContext().overridden_methods_size(this);
1525}
1526
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001527QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidisb0d178d2008-10-24 22:28:18 +00001528 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1529 // If the member function is declared const, the type of this is const X*,
1530 // if the member function is declared volatile, the type of this is
1531 // volatile X*, and if the member function is declared const volatile,
1532 // the type of this is const volatile X*.
1533
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001534 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson31a08752009-06-13 02:59:33 +00001535
John McCall3cb0ebd2010-03-10 03:28:59 +00001536 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall0953e762009-09-24 19:53:00 +00001537 ClassTy = C.getQualifiedType(ClassTy,
1538 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson4e579922009-07-10 21:35:09 +00001539 return C.getPointerType(ClassTy);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001540}
1541
Eli Friedmand7d7f672009-12-06 20:50:05 +00001542bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregorbd6d6192010-01-05 19:06:31 +00001543 // If this function is a template instantiation, look at the template from
1544 // which it was instantiated.
1545 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1546 if (!CheckFn)
1547 CheckFn = this;
1548
Eli Friedmand7d7f672009-12-06 20:50:05 +00001549 const FunctionDecl *fn;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001550 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedmand7d7f672009-12-06 20:50:05 +00001551}
1552
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001553bool CXXMethodDecl::isLambdaStaticInvoker() const {
1554 return getParent()->isLambda() &&
Faisal Valifad9e132013-09-26 19:54:12 +00001555 getParent()->getLambdaStaticInvoker() == this;
Douglas Gregor27dd7d92012-02-17 03:02:34 +00001556}
1557
1558
Sean Huntcbb67482011-01-08 20:30:50 +00001559CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1560 TypeSourceInfo *TInfo, bool IsVirtual,
1561 SourceLocation L, Expr *Init,
1562 SourceLocation R,
1563 SourceLocation EllipsisLoc)
Sean Huntf51d0b62011-01-08 23:01:16 +00001564 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001565 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1566 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001567{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001568}
1569
Sean Huntcbb67482011-01-08 20:30:50 +00001570CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1571 FieldDecl *Member,
1572 SourceLocation MemberLoc,
1573 SourceLocation L, Expr *Init,
1574 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001575 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001576 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001577 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1578{
1579}
1580
Sean Huntcbb67482011-01-08 20:30:50 +00001581CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1582 IndirectFieldDecl *Member,
1583 SourceLocation MemberLoc,
1584 SourceLocation L, Expr *Init,
1585 SourceLocation R)
Sean Huntf51d0b62011-01-08 23:01:16 +00001586 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregor76852c22011-11-01 01:16:03 +00001587 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001588 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregor802ab452009-12-02 22:36:29 +00001589{
Douglas Gregor7ad83902008-11-05 04:29:56 +00001590}
1591
Sean Huntcbb67482011-01-08 20:30:50 +00001592CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregor76852c22011-11-01 01:16:03 +00001593 TypeSourceInfo *TInfo,
1594 SourceLocation L, Expr *Init,
Sean Hunt41717662011-02-26 19:13:13 +00001595 SourceLocation R)
Douglas Gregor76852c22011-11-01 01:16:03 +00001596 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1597 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Sean Hunt41717662011-02-26 19:13:13 +00001598 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1599{
1600}
1601
1602CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Sean Huntcbb67482011-01-08 20:30:50 +00001603 FieldDecl *Member,
1604 SourceLocation MemberLoc,
1605 SourceLocation L, Expr *Init,
1606 SourceLocation R,
1607 VarDecl **Indices,
1608 unsigned NumIndices)
Sean Huntf51d0b62011-01-08 23:01:16 +00001609 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichet00eb3f92010-12-04 09:14:42 +00001610 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnaraa0af3b42010-05-26 18:09:23 +00001611 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001612{
1613 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1614 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1615}
1616
Sean Huntcbb67482011-01-08 20:30:50 +00001617CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1618 FieldDecl *Member,
1619 SourceLocation MemberLoc,
1620 SourceLocation L, Expr *Init,
1621 SourceLocation R,
1622 VarDecl **Indices,
1623 unsigned NumIndices) {
1624 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001625 sizeof(VarDecl *) * NumIndices,
Sean Huntcbb67482011-01-08 20:30:50 +00001626 llvm::alignOf<CXXCtorInitializer>());
Sean Huntf51d0b62011-01-08 23:01:16 +00001627 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1628 Indices, NumIndices);
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001629}
1630
Sean Huntcbb67482011-01-08 20:30:50 +00001631TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001632 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001633 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregor802ab452009-12-02 22:36:29 +00001634 else
1635 return TypeLoc();
1636}
1637
Sean Huntcbb67482011-01-08 20:30:50 +00001638const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregor802ab452009-12-02 22:36:29 +00001639 if (isBaseInitializer())
Sean Huntf51d0b62011-01-08 23:01:16 +00001640 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregor802ab452009-12-02 22:36:29 +00001641 else
1642 return 0;
1643}
1644
Sean Huntcbb67482011-01-08 20:30:50 +00001645SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregor76852c22011-11-01 01:16:03 +00001646 if (isAnyMemberInitializer())
Douglas Gregor802ab452009-12-02 22:36:29 +00001647 return getMemberLocation();
Richard Smith7a614d82011-06-11 17:19:42 +00001648
1649 if (isInClassMemberInitializer())
1650 return getAnyMember()->getLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001651
Douglas Gregor76852c22011-11-01 01:16:03 +00001652 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1653 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1654
1655 return SourceLocation();
Douglas Gregor802ab452009-12-02 22:36:29 +00001656}
1657
Sean Huntcbb67482011-01-08 20:30:50 +00001658SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith7a614d82011-06-11 17:19:42 +00001659 if (isInClassMemberInitializer()) {
1660 FieldDecl *D = getAnyMember();
1661 if (Expr *I = D->getInClassInitializer())
1662 return I->getSourceRange();
1663 return SourceRange();
1664 }
1665
Douglas Gregor802ab452009-12-02 22:36:29 +00001666 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregor7ad83902008-11-05 04:29:56 +00001667}
1668
David Blaikie99ba9e32011-12-20 02:48:34 +00001669void CXXConstructorDecl::anchor() { }
1670
Douglas Gregorb48fe382008-10-31 09:07:45 +00001671CXXConstructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001672CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1673 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1674 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1675 QualType(), 0, false, false, false,false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001676}
1677
1678CXXConstructorDecl *
Douglas Gregorb48fe382008-10-31 09:07:45 +00001679CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001680 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001681 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001682 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001683 bool isExplicit, bool isInline,
1684 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001685 assert(NameInfo.getName().getNameKind()
1686 == DeclarationName::CXXConstructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001687 "Name must refer to a constructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001688 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001689 isExplicit, isInline, isImplicitlyDeclared,
1690 isConstexpr);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001691}
1692
Douglas Gregor76852c22011-11-01 01:16:03 +00001693CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1694 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1695 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1696 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1697 return Construct->getConstructor();
1698
1699 return 0;
1700}
1701
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001702bool CXXConstructorDecl::isDefaultConstructor() const {
1703 // C++ [class.ctor]p5:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001704 // A default constructor for a class X is a constructor of class
1705 // X that can be called without an argument.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001706 return (getNumParams() == 0) ||
Anders Carlssonda3f4e22009-08-25 05:12:04 +00001707 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001708}
1709
Mike Stump1eb44332009-09-09 15:08:12 +00001710bool
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001711CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorcc15f012011-01-21 19:38:21 +00001712 return isCopyOrMoveConstructor(TypeQuals) &&
1713 getParamDecl(0)->getType()->isLValueReferenceType();
1714}
1715
1716bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1717 return isCopyOrMoveConstructor(TypeQuals) &&
1718 getParamDecl(0)->getType()->isRValueReferenceType();
1719}
1720
1721/// \brief Determine whether this is a copy or move constructor.
1722bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001723 // C++ [class.copy]p2:
Douglas Gregor64bffa92008-11-05 16:20:31 +00001724 // A non-template constructor for class X is a copy constructor
1725 // if its first parameter is of type X&, const X&, volatile X& or
1726 // const volatile X&, and either there are no other parameters
1727 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorcc15f012011-01-21 19:38:21 +00001728 // C++0x [class.copy]p3:
1729 // A non-template constructor for class X is a move constructor if its
1730 // first parameter is of type X&&, const X&&, volatile X&&, or
1731 // const volatile X&&, and either there are no other parameters or else
1732 // all other parameters have default arguments.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001733 if ((getNumParams() < 1) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001734 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorfd476482009-11-13 23:59:09 +00001735 (getPrimaryTemplate() != 0) ||
Douglas Gregor77da3f42009-10-13 23:45:19 +00001736 (getDescribedFunctionTemplate() != 0))
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001737 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001738
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001739 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorcc15f012011-01-21 19:38:21 +00001740
1741 // Do we have a reference type?
1742 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorfd476482009-11-13 23:59:09 +00001743 if (!ParamRefType)
1744 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001745
Douglas Gregorfd476482009-11-13 23:59:09 +00001746 // Is it a reference to our class type?
Douglas Gregor9e9199d2009-12-22 00:34:07 +00001747 ASTContext &Context = getASTContext();
1748
Douglas Gregorfd476482009-11-13 23:59:09 +00001749 CanQualType PointeeType
1750 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregor14e0b3d2009-09-15 20:50:23 +00001751 CanQualType ClassTy
1752 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001753 if (PointeeType.getUnqualifiedType() != ClassTy)
1754 return false;
Douglas Gregorcc15f012011-01-21 19:38:21 +00001755
John McCall0953e762009-09-24 19:53:00 +00001756 // FIXME: other qualifiers?
Douglas Gregorcc15f012011-01-21 19:38:21 +00001757
1758 // We have a copy or move constructor.
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001759 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorcc15f012011-01-21 19:38:21 +00001760 return true;
Douglas Gregor030ff0c2008-10-31 20:25:05 +00001761}
1762
Anders Carlssonfaccd722009-08-28 16:57:08 +00001763bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor60d62c22008-10-31 16:23:19 +00001764 // C++ [class.conv.ctor]p1:
1765 // A constructor declared without the function-specifier explicit
1766 // that can be called with a single parameter specifies a
1767 // conversion from the type of its first parameter to the type of
1768 // its class. Such a constructor is called a converting
1769 // constructor.
Anders Carlssonfaccd722009-08-28 16:57:08 +00001770 if (isExplicit() && !AllowExplicit)
Douglas Gregor60d62c22008-10-31 16:23:19 +00001771 return false;
1772
Mike Stump1eb44332009-09-09 15:08:12 +00001773 return (getNumParams() == 0 &&
John McCall183700f2009-09-21 23:43:11 +00001774 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor60d62c22008-10-31 16:23:19 +00001775 (getNumParams() == 1) ||
Douglas Gregor113c4442012-06-05 23:44:51 +00001776 (getNumParams() > 1 &&
1777 (getParamDecl(1)->hasDefaultArg() ||
1778 getParamDecl(1)->isParameterPack()));
Douglas Gregor60d62c22008-10-31 16:23:19 +00001779}
Douglas Gregorb48fe382008-10-31 09:07:45 +00001780
Douglas Gregor6493cc52010-11-08 17:16:59 +00001781bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregor66724ea2009-11-14 01:20:54 +00001782 if ((getNumParams() < 1) ||
1783 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1784 (getPrimaryTemplate() == 0) ||
1785 (getDescribedFunctionTemplate() != 0))
1786 return false;
1787
1788 const ParmVarDecl *Param = getParamDecl(0);
1789
1790 ASTContext &Context = getASTContext();
1791 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1792
Douglas Gregor66724ea2009-11-14 01:20:54 +00001793 // Is it the same as our our class type?
1794 CanQualType ClassTy
1795 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1796 if (ParamType.getUnqualifiedType() != ClassTy)
1797 return false;
1798
1799 return true;
1800}
1801
Sebastian Redlf677ea32011-02-05 19:23:19 +00001802const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1803 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001804 method_iterator It = getASTContext().overridden_methods_begin(this);
1805 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redlf677ea32011-02-05 19:23:19 +00001806 return 0;
1807
1808 return cast<CXXConstructorDecl>(*It);
1809}
1810
1811void
1812CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1813 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman540659e2012-03-10 01:39:01 +00001814 assert(getASTContext().overridden_methods_size(this) == 0 &&
1815 "Base ctor already set.");
1816 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redlf677ea32011-02-05 19:23:19 +00001817}
1818
David Blaikie99ba9e32011-12-20 02:48:34 +00001819void CXXDestructorDecl::anchor() { }
1820
Douglas Gregor42a552f2008-11-05 20:51:48 +00001821CXXDestructorDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001822CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1823 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1824 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00001825 QualType(), 0, false, false);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001826}
1827
1828CXXDestructorDecl *
Douglas Gregor42a552f2008-11-05 20:51:48 +00001829CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001830 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001831 const DeclarationNameInfo &NameInfo,
Craig Silversteinb41d8992010-10-21 00:44:50 +00001832 QualType T, TypeSourceInfo *TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001833 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001834 assert(NameInfo.getName().getNameKind()
1835 == DeclarationName::CXXDestructorName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001836 "Name must refer to a destructor");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001837 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnara25777432010-08-11 22:01:17 +00001838 isImplicitlyDeclared);
Douglas Gregor42a552f2008-11-05 20:51:48 +00001839}
1840
David Blaikie99ba9e32011-12-20 02:48:34 +00001841void CXXConversionDecl::anchor() { }
1842
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001843CXXConversionDecl *
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001844CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1846 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1847 QualType(), 0, false, false, false,
1848 SourceLocation());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001849}
1850
1851CXXConversionDecl *
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001852CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001853 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00001854 const DeclarationNameInfo &NameInfo,
John McCalla93c9342009-12-07 02:54:59 +00001855 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf5251602011-03-08 17:10:18 +00001856 bool isInline, bool isExplicit,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001857 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001858 assert(NameInfo.getName().getNameKind()
1859 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001860 "Name must refer to a conversion function");
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001861 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smithaf1fc7a2011-08-15 21:04:07 +00001862 isInline, isExplicit, isConstexpr,
1863 EndLocation);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00001864}
1865
Douglas Gregorf6e2e022012-02-16 01:06:16 +00001866bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1867 return isImplicit() && getParent()->isLambda() &&
1868 getConversionType()->isBlockPointerType();
1869}
1870
David Blaikie99ba9e32011-12-20 02:48:34 +00001871void LinkageSpecDecl::anchor() { }
1872
Chris Lattner21ef7ae2008-11-04 16:51:42 +00001873LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump1eb44332009-09-09 15:08:12 +00001874 DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001875 SourceLocation ExternLoc,
1876 SourceLocation LangLoc,
Abramo Bagnara5f6bcbe2011-03-03 14:52:38 +00001877 LanguageIDs Lang,
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001878 bool HasBraces) {
1879 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregorf44515a2008-12-16 22:23:02 +00001880}
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001881
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001882LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1884 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindolae5e575d2013-04-26 01:30:23 +00001885 lang_c, false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001886}
1887
David Blaikie99ba9e32011-12-20 02:48:34 +00001888void UsingDirectiveDecl::anchor() { }
1889
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001890UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1891 SourceLocation L,
1892 SourceLocation NamespaceLoc,
Douglas Gregordb992412011-02-25 16:33:46 +00001893 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001894 SourceLocation IdentLoc,
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001895 NamedDecl *Used,
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001896 DeclContext *CommonAncestor) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001897 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1898 Used = NS->getOriginalNamespace();
Douglas Gregordb992412011-02-25 16:33:46 +00001899 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1900 IdentLoc, Used, CommonAncestor);
Douglas Gregor2a3009a2009-02-03 19:21:40 +00001901}
1902
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001903UsingDirectiveDecl *
1904UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1905 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1906 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1907 NestedNameSpecifierLoc(),
1908 SourceLocation(), 0, 0);
1909}
1910
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001911NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1912 if (NamespaceAliasDecl *NA =
1913 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1914 return NA->getNamespace();
1915 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1916}
1917
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001918void NamespaceDecl::anchor() { }
1919
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001920NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1921 SourceLocation StartLoc,
1922 SourceLocation IdLoc, IdentifierInfo *Id,
1923 NamespaceDecl *PrevDecl)
1924 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1925 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1926{
1927 setPreviousDeclaration(PrevDecl);
1928
1929 if (PrevDecl)
1930 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1931}
1932
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001933NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001934 bool Inline, SourceLocation StartLoc,
1935 SourceLocation IdLoc, IdentifierInfo *Id,
1936 NamespaceDecl *PrevDecl) {
1937 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001938}
1939
1940NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1941 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00001942 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1943 0, 0);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001944}
1945
David Blaikie99ba9e32011-12-20 02:48:34 +00001946void NamespaceAliasDecl::anchor() { }
1947
Mike Stump1eb44332009-09-09 15:08:12 +00001948NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor0a35bce2010-09-01 03:07:18 +00001949 SourceLocation UsingLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001950 SourceLocation AliasLoc,
1951 IdentifierInfo *Alias,
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001952 NestedNameSpecifierLoc QualifierLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001953 SourceLocation IdentLoc,
Anders Carlsson68771c72009-03-28 22:58:02 +00001954 NamedDecl *Namespace) {
Sebastian Redleb0d8c92009-11-23 15:34:23 +00001955 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1956 Namespace = NS->getOriginalNamespace();
Douglas Gregor0cfaf6a2011-02-25 17:08:07 +00001957 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1958 QualifierLoc, IdentLoc, Namespace);
Anders Carlsson68771c72009-03-28 22:58:02 +00001959}
1960
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001961NamespaceAliasDecl *
1962NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1964 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1965 NestedNameSpecifierLoc(),
1966 SourceLocation(), 0);
1967}
1968
David Blaikie99ba9e32011-12-20 02:48:34 +00001969void UsingShadowDecl::anchor() { }
1970
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00001971UsingShadowDecl *
1972UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1973 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1974 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1975}
1976
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001977UsingDecl *UsingShadowDecl::getUsingDecl() const {
1978 const UsingShadowDecl *Shadow = this;
1979 while (const UsingShadowDecl *NextShadow =
1980 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1981 Shadow = NextShadow;
1982 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1983}
1984
David Blaikie99ba9e32011-12-20 02:48:34 +00001985void UsingDecl::anchor() { }
1986
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001987void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1988 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1989 "declaration already in set");
1990 assert(S->getUsingDecl() == this);
1991
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00001992 if (FirstUsingShadow.getPointer())
1993 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1994 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00001995}
1996
1997void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1998 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1999 "declaration not in set");
2000 assert(S->getUsingDecl() == this);
2001
2002 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2003
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00002004 if (FirstUsingShadow.getPointer() == S) {
2005 FirstUsingShadow.setPointer(
2006 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00002007 S->UsingOrNextShadow = this;
2008 return;
2009 }
2010
Benjamin Kramer9bc6fb62012-01-07 19:09:05 +00002011 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis826faa22010-11-10 05:40:41 +00002012 while (Prev->UsingOrNextShadow != S)
2013 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2014 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2015 S->UsingOrNextShadow = this;
2016}
2017
Douglas Gregordc355712011-02-25 00:36:19 +00002018UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2019 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002020 const DeclarationNameInfo &NameInfo,
Enea Zaffanella8d030c72013-07-22 10:54:09 +00002021 bool HasTypename) {
2022 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregor9cfbe482009-06-20 00:51:54 +00002023}
2024
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002025UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2026 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
2027 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2028 DeclarationNameInfo(), false);
2029}
2030
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00002031SourceRange UsingDecl::getSourceRange() const {
2032 SourceLocation Begin = isAccessDeclaration()
2033 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2034 return SourceRange(Begin, getNameInfo().getEndLoc());
2035}
2036
David Blaikie99ba9e32011-12-20 02:48:34 +00002037void UnresolvedUsingValueDecl::anchor() { }
2038
John McCall7ba107a2009-11-18 02:36:19 +00002039UnresolvedUsingValueDecl *
2040UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2041 SourceLocation UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002042 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnaraef3dce82010-08-12 11:46:03 +00002043 const DeclarationNameInfo &NameInfo) {
John McCall7ba107a2009-11-18 02:36:19 +00002044 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002045 QualifierLoc, NameInfo);
John McCall7ba107a2009-11-18 02:36:19 +00002046}
2047
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002048UnresolvedUsingValueDecl *
2049UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2050 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2051 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2052 NestedNameSpecifierLoc(),
2053 DeclarationNameInfo());
2054}
2055
Enea Zaffanellad4de59d2013-07-17 17:28:56 +00002056SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2057 SourceLocation Begin = isAccessDeclaration()
2058 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2059 return SourceRange(Begin, getNameInfo().getEndLoc());
2060}
2061
David Blaikie99ba9e32011-12-20 02:48:34 +00002062void UnresolvedUsingTypenameDecl::anchor() { }
2063
John McCall7ba107a2009-11-18 02:36:19 +00002064UnresolvedUsingTypenameDecl *
2065UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2066 SourceLocation UsingLoc,
2067 SourceLocation TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002068 NestedNameSpecifierLoc QualifierLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002069 SourceLocation TargetNameLoc,
2070 DeclarationName TargetName) {
2071 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregordc355712011-02-25 00:36:19 +00002072 QualifierLoc, TargetNameLoc,
John McCall7ba107a2009-11-18 02:36:19 +00002073 TargetName.getAsIdentifierInfo());
Anders Carlsson665b49c2009-08-28 05:30:28 +00002074}
2075
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002076UnresolvedUsingTypenameDecl *
2077UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2078 void *Mem = AllocateDeserializedDecl(C, ID,
2079 sizeof(UnresolvedUsingTypenameDecl));
2080 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2081 SourceLocation(),
2082 NestedNameSpecifierLoc(),
2083 SourceLocation(),
2084 0);
2085}
2086
David Blaikie99ba9e32011-12-20 02:48:34 +00002087void StaticAssertDecl::anchor() { }
2088
Anders Carlssonfb311762009-03-14 00:25:26 +00002089StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002090 SourceLocation StaticAssertLoc,
2091 Expr *AssertExpr,
2092 StringLiteral *Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002093 SourceLocation RParenLoc,
2094 bool Failed) {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002095 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithe3f470a2012-07-11 22:37:56 +00002096 RParenLoc, Failed);
Anders Carlssonfb311762009-03-14 00:25:26 +00002097}
2098
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002099StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2100 unsigned ID) {
2101 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithe3f470a2012-07-11 22:37:56 +00002102 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2103 SourceLocation(), false);
Douglas Gregor1e68ecc2012-01-05 21:55:30 +00002104}
2105
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002106static const char *getAccessName(AccessSpecifier AS) {
2107 switch (AS) {
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002108 case AS_none:
David Blaikieb219cfc2011-09-23 05:06:16 +00002109 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002110 case AS_public:
2111 return "public";
2112 case AS_private:
2113 return "private";
2114 case AS_protected:
2115 return "protected";
2116 }
David Blaikie561d3ab2012-01-17 02:30:50 +00002117 llvm_unreachable("Invalid access specifier!");
Anders Carlsson05bf2c72009-03-26 23:46:50 +00002118}
2119
2120const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2121 AccessSpecifier AS) {
2122 return DB << getAccessName(AS);
2123}
Richard Smithf15fda02012-02-02 01:16:57 +00002124
2125const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2126 AccessSpecifier AS) {
2127 return DB << getAccessName(AS);
2128}