blob: 74183701debf8b5c1d5dd7ee67fe3b03306a269e [file] [log] [blame]
Ted Kremenek21475702008-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 Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000021#include "clang/AST/ODRHash.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000022#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000023#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000024#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000025#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Decl Allocation/Deallocation Method Implementations
30//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000031
David Blaikie68e081d2011-12-20 02:48:34 +000032void AccessSpecDecl::anchor() { }
33
Douglas Gregor72172e92012-01-05 21:55:30 +000034AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000035 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000036}
37
Richard Smitha4ba74c2013-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 McCall67da35c2010-02-04 22:26:26 +000049CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000050 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
51 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
52 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
53 HasPrivateFields(false), HasProtectedFields(false),
54 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
55 HasOnlyCMembers(true), HasInClassInitializer(false),
56 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000057 HasInheritedConstructor(false), HasInheritedAssignment(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000058 NeedOverloadResolutionForMoveConstructor(false),
59 NeedOverloadResolutionForMoveAssignment(false),
60 NeedOverloadResolutionForDestructor(false),
61 DefaultedMoveConstructorIsDeleted(false),
62 DefaultedMoveAssignmentIsDeleted(false),
63 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
64 DeclaredNonTrivialSpecialMembers(0), HasIrrelevantDestructor(true),
65 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000066 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000067 DefaultedDefaultConstructorIsConstexpr(true),
68 HasConstexprDefaultConstructor(false),
69 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
70 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +000071 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
72 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000073 ImplicitCopyAssignmentHasConstParam(true),
74 HasDeclaredCopyConstructorWithConstParam(false),
75 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Richard Trieub6adf542017-02-18 02:09:28 +000076 IsParsingBaseSpecifiers(false), ODRHash(0), NumBases(0), NumVBases(0),
77 Bases(), VBases(), Definition(D), FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000078
Benjamin Kramer300c0632012-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
Richard Smith053f6c62014-05-16 23:01:30 +000087CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
88 DeclContext *DC, SourceLocation StartLoc,
89 SourceLocation IdLoc, IdentifierInfo *Id,
90 CXXRecordDecl *PrevDecl)
91 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
92 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smithb6483992016-05-17 22:44:15 +000093 : nullptr),
Richard Smith053f6c62014-05-16 23:01:30 +000094 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000095
Jay Foad39c79802011-01-12 09:06:06 +000096CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000097 DeclContext *DC, SourceLocation StartLoc,
98 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000099 CXXRecordDecl* PrevDecl,
100 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +0000101 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +0000102 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000103 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000104
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000105 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000106 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000107 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000108 return R;
109}
110
Richard Smith053f6c62014-05-16 23:01:30 +0000111CXXRecordDecl *
112CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
113 TypeSourceInfo *Info, SourceLocation Loc,
114 bool Dependent, bool IsGeneric,
115 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000116 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000117 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
118 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000119 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000120 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000121 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000122 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000123 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000124 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000125 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000126 return R;
127}
128
Douglas Gregor72172e92012-01-05 21:55:30 +0000129CXXRecordDecl *
130CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000131 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000132 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000133 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000134 R->MayHaveOutOfDateDef = false;
135 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000136}
137
Mike Stump11289f42009-09-09 15:08:12 +0000138void
Craig Toppere6337e12015-12-25 00:36:02 +0000139CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
140 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000141 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000142
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000143 if (!data().Bases.isOffset() && data().NumBases > 0)
144 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000145
Craig Toppere6337e12015-12-25 00:36:02 +0000146 if (NumBases) {
Richard Smith872307e2016-03-08 22:17:41 +0000147 if (!C.getLangOpts().CPlusPlus1z) {
148 // C++ [dcl.init.aggr]p1:
149 // An aggregate is [...] a class with [...] no base classes [...].
150 data().Aggregate = false;
151 }
Richard Smithaed32b42011-10-18 20:08:55 +0000152
153 // C++ [class]p4:
154 // A POD-struct is an aggregate class...
155 data().PlainOldData = false;
156 }
157
Anders Carlssonabb20e62010-03-29 05:13:12 +0000158 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000159 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000160
161 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000162 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000163
Craig Toppere6337e12015-12-25 00:36:02 +0000164 data().Bases = new(C) CXXBaseSpecifier [NumBases];
165 data().NumBases = NumBases;
166 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000167 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000168 // Keep track of inherited vbases for this base class.
169 const CXXBaseSpecifier *Base = Bases[i];
170 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000171 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000172 if (BaseType->isDependentType())
173 continue;
174 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000175 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000176
Chandler Carruthb1963742011-04-30 09:17:45 +0000177 if (!BaseClassDecl->isEmpty()) {
178 if (!data().Empty) {
179 // C++0x [class]p7:
180 // A standard-layout class is a class that:
181 // [...]
182 // -- either has no non-static data members in the most derived
183 // class and at most one base class with non-static data members,
184 // or has no base classes with non-static data members, and
185 // If this is the second non-empty base, then neither of these two
186 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000187 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000188 }
189
David Majnemer5cfda6f2016-05-22 05:34:26 +0000190 // C++14 [meta.unary.prop]p4:
191 // T is a class type [...] with [...] no base class B for which
192 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000193 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000194 data().HasNoNonEmptyBases = false;
195 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000196
Richard Smith872307e2016-03-08 22:17:41 +0000197 // C++1z [dcl.init.agg]p1:
198 // An aggregate is a class with [...] no private or protected base classes
199 if (Base->getAccessSpecifier() != AS_public)
200 data().Aggregate = false;
201
Douglas Gregor11c024b2010-09-28 20:50:54 +0000202 // C++ [class.virtual]p1:
203 // A class that declares or inherits a virtual function is called a
204 // polymorphic class.
205 if (BaseClassDecl->isPolymorphic())
206 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000207
Chandler Carruthb1963742011-04-30 09:17:45 +0000208 // C++0x [class]p7:
209 // A standard-layout class is a class that: [...]
210 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000211 if (!BaseClassDecl->isStandardLayout())
212 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000213
Chandler Carruthe71d0622011-04-24 02:49:34 +0000214 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000215 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000216 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000217
Anders Carlssonabb20e62010-03-29 05:13:12 +0000218 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000219 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000220 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000221 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000222 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000223
224 // C++11 [class.copy]p8:
225 // The implicitly-declared copy constructor for a class X will have
226 // the form 'X::X(const X&)' if each [...] virtual base class B of X
227 // has a copy constructor whose first parameter is of type
228 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000229 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000230 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000231 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000232
233 // C++1z [dcl.init.agg]p1:
234 // An aggregate is a class with [...] no virtual base classes
235 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000236 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000237 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000238
239 if (Base->isVirtual()) {
240 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000241 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000242 VBases.push_back(Base);
243
David Majnemer5cfda6f2016-05-22 05:34:26 +0000244 // C++14 [meta.unary.prop] is_empty:
245 // T is a class type, but not a union type, with ... no virtual base
246 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000247 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000248
Richard Smith872307e2016-03-08 22:17:41 +0000249 // C++1z [dcl.init.agg]p1:
250 // An aggregate is a class with [...] no virtual base classes
251 data().Aggregate = false;
252
Richard Smith328aae52012-11-30 05:11:39 +0000253 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
254 // A [default constructor, copy/move constructor, or copy/move assignment
255 // operator for a class X] is trivial [...] if:
256 // -- class X has [...] no virtual base classes
257 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000258
259 // C++0x [class]p7:
260 // A standard-layout class is a class that: [...]
261 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000262 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000263
264 // C++11 [dcl.constexpr]p4:
265 // In the definition of a constexpr constructor [...]
266 // -- the class shall not have any virtual base classes
267 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000268
269 // C++1z [class.copy]p8:
270 // The implicitly-declared copy constructor for a class X will have
271 // the form 'X::X(const X&)' if each potentially constructed subobject
272 // has a copy constructor whose first parameter is of type
273 // 'const B&' or 'const volatile B&' [...]
274 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
275 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000276 } else {
277 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000278 // A default constructor is trivial [...] if:
279 // -- all the direct base classes of its class have trivial default
280 // constructors.
281 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000282 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
283
Chandler Carruthad7d4042011-04-23 23:10:33 +0000284 // C++0x [class.copy]p13:
285 // A copy/move constructor for class X is trivial if [...]
286 // [...]
287 // -- the constructor selected to copy/move each direct base class
288 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000289 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000290 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000291 // If the base class doesn't have a simple move constructor, we'll eagerly
292 // declare it and perform overload resolution to determine which function
293 // it actually calls. If it does have a simple move constructor, this
294 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000295 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000296 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000297
298 // C++0x [class.copy]p27:
299 // A copy/move assignment operator for class X is trivial if [...]
300 // [...]
301 // -- the assignment operator selected to copy/move each direct base
302 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000303 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000304 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000305 // If the base class doesn't have a simple move assignment, we'll eagerly
306 // declare it and perform overload resolution to determine which function
307 // it actually calls. If it does have a simple move assignment, this
308 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000309 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000310 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000311
312 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000313 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000314 // requirements of a constexpr constructor, the implicitly-defined
315 // default constructor is constexpr.
316 if (!BaseClassDecl->hasConstexprDefaultConstructor())
317 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000318
319 // C++1z [class.copy]p8:
320 // The implicitly-declared copy constructor for a class X will have
321 // the form 'X::X(const X&)' if each potentially constructed subobject
322 // has a copy constructor whose first parameter is of type
323 // 'const B&' or 'const volatile B&' [...]
324 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
325 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000326 }
Richard Smith92f241f2012-12-08 02:53:02 +0000327
Douglas Gregor11c024b2010-09-28 20:50:54 +0000328 // C++ [class.ctor]p3:
329 // A destructor is trivial if all the direct base classes of its class
330 // have trivial destructors.
331 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000332 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000333
334 if (!BaseClassDecl->hasIrrelevantDestructor())
335 data().HasIrrelevantDestructor = false;
336
Richard Smith1c33fe82012-11-28 06:23:12 +0000337 // C++11 [class.copy]p18:
338 // The implicitly-declared copy assignment oeprator for a class X will
339 // have the form 'X& X::operator=(const X&)' if each direct base class B
340 // of X has a copy assignment operator whose parameter is of type 'const
341 // B&', 'const volatile B&', or 'B' [...]
342 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
343 data().ImplicitCopyAssignmentHasConstParam = false;
344
John McCall31168b02011-06-15 23:02:42 +0000345 // A class has an Objective-C object member if... or any of its bases
346 // has an Objective-C object member.
347 if (BaseClassDecl->hasObjectMember())
348 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000349
350 if (BaseClassDecl->hasVolatileMember())
351 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000352
Douglas Gregor61226d32011-05-13 01:05:07 +0000353 // Keep track of the presence of mutable fields.
354 if (BaseClassDecl->hasMutableFields())
355 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000356
357 if (BaseClassDecl->hasUninitializedReferenceMember())
358 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000359
Nico Weber6a6376b2016-02-19 01:52:46 +0000360 if (!BaseClassDecl->allowConstDefaultInit())
361 data().HasUninitializedFields = true;
362
Richard Smith6b02d462012-12-08 08:32:28 +0000363 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000364 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000365
David Majnemer5ef4fe72014-06-13 06:43:46 +0000366 if (VBases.empty()) {
367 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000368 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000369 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000370
371 // Create base specifier for any direct or indirect virtual bases.
372 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
373 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000374 for (int I = 0, E = VBases.size(); I != E; ++I) {
375 QualType Type = VBases[I]->getType();
376 if (!Type->isDependentType())
377 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000378 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000379 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000380
381 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000382}
383
Richard Trieue7f7ed22017-02-22 01:11:25 +0000384void CXXRecordDecl::computeODRHash() {
385 if (!DefinitionData)
386 return;
387
388 ODRHash Hash;
389 Hash.AddCXXRecordDecl(this);
390
391 DefinitionData->ODRHash = Hash.CalculateHash();
392}
Richard Trieub6adf542017-02-18 02:09:28 +0000393
Richard Smith6b02d462012-12-08 08:32:28 +0000394void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
395 // C++11 [class.copy]p11:
396 // A defaulted copy/move constructor for a class X is defined as
397 // deleted if X has:
398 // -- a direct or virtual base class B that cannot be copied/moved [...]
399 // -- a non-static data member of class type M (or array thereof)
400 // that cannot be copied or moved [...]
401 if (!Subobj->hasSimpleMoveConstructor())
402 data().NeedOverloadResolutionForMoveConstructor = true;
403
404 // C++11 [class.copy]p23:
405 // A defaulted copy/move assignment operator for a class X is defined as
406 // deleted if X has:
407 // -- a direct or virtual base class B that cannot be copied/moved [...]
408 // -- a non-static data member of class type M (or array thereof)
409 // that cannot be copied or moved [...]
410 if (!Subobj->hasSimpleMoveAssignment())
411 data().NeedOverloadResolutionForMoveAssignment = true;
412
413 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
414 // A defaulted [ctor or dtor] for a class X is defined as
415 // deleted if X has:
416 // -- any direct or virtual base class [...] has a type with a destructor
417 // that is deleted or inaccessible from the defaulted [ctor or dtor].
418 // -- any non-static data member has a type with a destructor
419 // that is deleted or inaccessible from the defaulted [ctor or dtor].
420 if (!Subobj->hasSimpleDestructor()) {
421 data().NeedOverloadResolutionForMoveConstructor = true;
422 data().NeedOverloadResolutionForDestructor = true;
423 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000424}
425
Douglas Gregord2e6a452010-01-14 17:47:39 +0000426bool CXXRecordDecl::hasAnyDependentBases() const {
427 if (!isDependentContext())
428 return false;
429
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000430 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000431}
432
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000433bool CXXRecordDecl::isTriviallyCopyable() const {
434 // C++0x [class]p5:
435 // A trivially copyable class is a class that:
436 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000437 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000438 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000439 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000440 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000441 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000442 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000443 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000444 // -- has a trivial destructor.
445 if (!hasTrivialDestructor()) return false;
446
447 return true;
448}
449
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000450void CXXRecordDecl::markedVirtualFunctionPure() {
451 // C++ [class.abstract]p2:
452 // A class is abstract if it has at least one pure virtual function.
453 data().Abstract = true;
454}
455
456void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000457 if (!D->isImplicit() &&
458 !isa<FieldDecl>(D) &&
459 !isa<IndirectFieldDecl>(D) &&
460 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
461 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
462 data().HasOnlyCMembers = false;
463
464 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000465 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000466 return;
467
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000468 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
469 if (FunTmpl)
470 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000471
472 // FIXME: Pass NamedDecl* to addedMember?
473 Decl *DUnderlying = D;
474 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
475 DUnderlying = ND->getUnderlyingDecl();
476 if (FunctionTemplateDecl *UnderlyingFunTmpl =
477 dyn_cast<FunctionTemplateDecl>(DUnderlying))
478 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
479 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000480
Douglas Gregora832d3e2010-09-28 19:45:33 +0000481 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
482 if (Method->isVirtual()) {
483 // C++ [dcl.init.aggr]p1:
484 // An aggregate is an array or a class with [...] no virtual functions.
485 data().Aggregate = false;
486
487 // C++ [class]p4:
488 // A POD-struct is an aggregate class...
489 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000490
David Majnemer5cfda6f2016-05-22 05:34:26 +0000491 // C++14 [meta.unary.prop]p4:
492 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000493 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000494
495 // C++ [class.virtual]p1:
496 // A class that declares or inherits a virtual function is called a
497 // polymorphic class.
498 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000499
Richard Smith328aae52012-11-30 05:11:39 +0000500 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
501 // A [default constructor, copy/move constructor, or copy/move
502 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000503 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000504 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000505
Chandler Carruthb1963742011-04-30 09:17:45 +0000506 // C++0x [class]p7:
507 // A standard-layout class is a class that: [...]
508 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000509 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000510 }
511 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000512
Richard Smith1c33fe82012-11-28 06:23:12 +0000513 // Notify the listener if an implicit member was added after the definition
514 // was completed.
515 if (!isBeingDefined() && D->isImplicit())
516 if (ASTMutationListener *L = getASTMutationListener())
517 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000518
Richard Smith328aae52012-11-30 05:11:39 +0000519 // The kind of special member this declaration is, if any.
520 unsigned SMKind = 0;
521
Richard Smith1c33fe82012-11-28 06:23:12 +0000522 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000523 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000524 if (!Constructor->isImplicit()) {
525 // Note that we have a user-declared constructor.
526 data().UserDeclaredConstructor = true;
527
528 // C++ [class]p4:
529 // A POD-struct is an aggregate class [...]
530 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
531 // type is technically an aggregate in C++0x since it wouldn't be in 03.
532 data().PlainOldData = false;
533 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000534
Alexis Hunt88c75c32011-05-09 21:45:35 +0000535 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000536 SMKind |= SMF_DefaultConstructor;
537
Richard Smith5179eb72016-06-28 19:03:57 +0000538 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000539 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000540 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000541 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000542 if (Constructor->isDefaulted())
543 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000544 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000545
Chandler Carruthad7d4042011-04-23 23:10:33 +0000546 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000547 unsigned Quals;
548 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000549 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000550
551 if (Quals & Qualifiers::Const)
552 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000553 } else if (Constructor->isMoveConstructor())
554 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000555 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000556
Eric Fiselier283d8d42016-12-03 01:26:47 +0000557 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith505ef812016-12-21 01:57:02 +0000558 // An aggregate is an array or a class with no user-provided, explicit, or
559 // inherited constructors
560 if (Constructor->isUserProvided() || Constructor->isExplicit())
Eric Fiselier283d8d42016-12-03 01:26:47 +0000561 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000562 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000563
Richard Smith5179eb72016-06-28 19:03:57 +0000564 // Handle constructors, including those inherited from base classes.
565 if (CXXConstructorDecl *Constructor =
566 dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000567 // Record if we see any constexpr constructors which are neither copy
568 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000569 // C++1z [basic.types]p10:
570 // [...] has at least one constexpr constructor or constructor template
571 // (possibly inherited from a base class) that is not a copy or move
572 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000573 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000574 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000575 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000576
Richard Smith1c33fe82012-11-28 06:23:12 +0000577 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000578 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000579 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000580
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000581 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000582 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000583 // If the destructor is explicitly defaulted and not trivial or not public
584 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
585 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000586
Richard Smith1c33fe82012-11-28 06:23:12 +0000587 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000588 // A destructor is trivial if [...] the destructor is not virtual.
589 if (DD->isVirtual())
590 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000591 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000592
593 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000594 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000595 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000596 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000597
598 const ReferenceType *ParamTy =
599 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
600 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
601 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000602 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000603
Richard Smith328aae52012-11-30 05:11:39 +0000604 if (Method->isMoveAssignmentOperator())
605 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000606
Douglas Gregor457104e2010-09-29 04:25:11 +0000607 // Keep the list of conversion functions up-to-date.
608 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000609 // FIXME: We use the 'unsafe' accessor for the access specifier here,
610 // because Sema may not have set it yet. That's really just a misdesign
611 // in Sema. However, LLDB *will* have set the access specifier correctly,
612 // and adds declarations after the class is technically completed,
613 // so completeDefinition()'s overriding of the access specifiers doesn't
614 // work.
615 AccessSpecifier AS = Conversion->getAccessUnsafe();
616
Richard Smith328aae52012-11-30 05:11:39 +0000617 if (Conversion->getPrimaryTemplate()) {
618 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000619 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000620 ASTContext &Ctx = getASTContext();
621 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
622 NamedDecl *Primary =
623 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
624 if (Primary->getPreviousDecl())
625 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
626 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000627 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000628 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000629 }
630 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000631
Richard Smith328aae52012-11-30 05:11:39 +0000632 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000633 // If this is the first declaration of a special member, we no longer have
634 // an implicit trivial special member.
635 data().HasTrivialSpecialMembers &=
636 data().DeclaredSpecialMembers | ~SMKind;
637
638 if (!Method->isImplicit() && !Method->isUserProvided()) {
639 // This method is user-declared but not user-provided. We can't work out
640 // whether it's trivial yet (not until we get to the end of the class).
641 // We'll handle this method in finishedDefaultedOrDeletedMember.
642 } else if (Method->isTrivial())
643 data().HasTrivialSpecialMembers |= SMKind;
644 else
645 data().DeclaredNonTrivialSpecialMembers |= SMKind;
646
Richard Smith328aae52012-11-30 05:11:39 +0000647 // Note when we have declared a declared special member, and suppress the
648 // implicit declaration of this special member.
649 data().DeclaredSpecialMembers |= SMKind;
650
651 if (!Method->isImplicit()) {
652 data().UserDeclaredSpecialMembers |= SMKind;
653
654 // C++03 [class]p4:
655 // A POD-struct is an aggregate class that has [...] no user-defined
656 // copy assignment operator and no user-defined destructor.
657 //
658 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
659 // aggregates could not have any constructors, clear it even for an
660 // explicitly defaulted or deleted constructor.
661 // type is technically an aggregate in C++0x since it wouldn't be in 03.
662 //
663 // Also, a user-declared move assignment operator makes a class non-POD.
664 // This is an extension in C++03.
665 data().PlainOldData = false;
666 }
Richard Smith328aae52012-11-30 05:11:39 +0000667 }
668
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000669 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000670 }
Richard Smith328aae52012-11-30 05:11:39 +0000671
Douglas Gregora832d3e2010-09-28 19:45:33 +0000672 // Handle non-static data members.
673 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000674 // C++ [class.bit]p2:
675 // A declaration for a bit-field that omits the identifier declares an
676 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
677 // initialized.
678 if (Field->isUnnamedBitfield())
679 return;
680
Douglas Gregora832d3e2010-09-28 19:45:33 +0000681 // C++ [dcl.init.aggr]p1:
682 // An aggregate is an array or a class (clause 9) with [...] no
683 // private or protected non-static data members (clause 11).
684 //
685 // A POD must be an aggregate.
686 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
687 data().Aggregate = false;
688 data().PlainOldData = false;
689 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000690
691 // C++0x [class]p7:
692 // A standard-layout class is a class that:
693 // [...]
694 // -- has the same access control for all non-static data members,
695 switch (D->getAccess()) {
696 case AS_private: data().HasPrivateFields = true; break;
697 case AS_protected: data().HasProtectedFields = true; break;
698 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000699 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000700 };
701 if ((data().HasPrivateFields + data().HasProtectedFields +
702 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000703 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000704
Douglas Gregor61226d32011-05-13 01:05:07 +0000705 // Keep track of the presence of mutable fields.
706 if (Field->isMutable())
707 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000708
709 // C++11 [class.union]p8, DR1460:
710 // If X is a union, a non-static data member of X that is not an anonymous
711 // union is a variant member of X.
712 if (isUnion() && !Field->isAnonymousStructOrUnion())
713 data().HasVariantMembers = true;
714
Chandler Carruthad7d4042011-04-23 23:10:33 +0000715 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000716 // A POD struct is a class that is both a trivial class and a
717 // standard-layout class, and has no non-static data members of type
718 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000719 //
720 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
721 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000722 ASTContext &Context = getASTContext();
723 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000724 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000725 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000726 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000727 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
728 // Objective-C Automatic Reference Counting:
729 // If a class has a non-static data member of Objective-C pointer
730 // type (or array thereof), it is a non-POD type and its
731 // default constructor (if any), copy constructor, move constructor,
732 // copy assignment operator, move assignment operator, and destructor are
733 // non-trivial.
734 setHasObjectMember(true);
735 struct DefinitionData &Data = data();
736 Data.PlainOldData = false;
737 Data.HasTrivialSpecialMembers = 0;
738 Data.HasIrrelevantDestructor = false;
739 }
Eli Friedmana5433322013-07-20 01:06:31 +0000740 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000741 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000742
Chandler Carruthb1963742011-04-30 09:17:45 +0000743 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000744 if (!Field->hasInClassInitializer())
745 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000746
Chandler Carruthb1963742011-04-30 09:17:45 +0000747 // C++0x [class]p7:
748 // A standard-layout class is a class that:
749 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000750 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000751 }
752
Nico Weber6a6376b2016-02-19 01:52:46 +0000753 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +0000754 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000755 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000756 data().HasUninitializedFields = true;
757 } else {
758 data().HasUninitializedFields = true;
759 }
760 }
761
Richard Smith3607ffe2012-02-13 03:54:03 +0000762 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000763 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000764 data().HasNonLiteralTypeFieldsOrBases = true;
765
Richard Smithab44d5b2013-12-10 08:25:00 +0000766 if (Field->hasInClassInitializer() ||
767 (Field->isAnonymousStructOrUnion() &&
768 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000769 data().HasInClassInitializer = true;
770
771 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000772 // A default constructor is trivial if [...] no non-static data member
773 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000774 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000775
Richard Smithe2648ba2012-05-07 01:07:30 +0000776 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000777 // An aggregate is a [...] class with [...] no
778 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000779 //
Richard Smith872307e2016-03-08 22:17:41 +0000780 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000781 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000782 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000783
Richard Smithe2648ba2012-05-07 01:07:30 +0000784 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000785 // A POD struct is [...] a trivial class.
786 data().PlainOldData = false;
787 }
788
Richard Smith6b02d462012-12-08 08:32:28 +0000789 // C++11 [class.copy]p23:
790 // A defaulted copy/move assignment operator for a class X is defined
791 // as deleted if X has:
792 // -- a non-static data member of reference type
793 if (T->isReferenceType())
794 data().DefaultedMoveAssignmentIsDeleted = true;
795
Douglas Gregor11c024b2010-09-28 20:50:54 +0000796 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
797 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
798 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000799 addedClassSubobject(FieldRec);
800
Richard Smithc91d12c2013-11-25 07:07:05 +0000801 // We may need to perform overload resolution to determine whether a
802 // field can be moved if it's const or volatile qualified.
803 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
804 data().NeedOverloadResolutionForMoveConstructor = true;
805 data().NeedOverloadResolutionForMoveAssignment = true;
806 }
807
Richard Smith6b02d462012-12-08 08:32:28 +0000808 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
809 // A defaulted [special member] for a class X is defined as
810 // deleted if:
811 // -- X is a union-like class that has a variant member with a
812 // non-trivial [corresponding special member]
813 if (isUnion()) {
814 if (FieldRec->hasNonTrivialMoveConstructor())
815 data().DefaultedMoveConstructorIsDeleted = true;
816 if (FieldRec->hasNonTrivialMoveAssignment())
817 data().DefaultedMoveAssignmentIsDeleted = true;
818 if (FieldRec->hasNonTrivialDestructor())
819 data().DefaultedDestructorIsDeleted = true;
820 }
821
Richard Smithdd5619f2016-08-16 00:13:47 +0000822 // For an anonymous union member, our overload resolution will perform
823 // overload resolution for its members.
824 if (Field->isAnonymousStructOrUnion()) {
825 data().NeedOverloadResolutionForMoveConstructor |=
826 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
827 data().NeedOverloadResolutionForMoveAssignment |=
828 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
829 data().NeedOverloadResolutionForDestructor |=
830 FieldRec->data().NeedOverloadResolutionForDestructor;
831 }
832
Alexis Huntf479f1b2011-05-09 18:22:59 +0000833 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000834 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000835 // -- for all the non-static data members of its class that are of
836 // class type (or array thereof), each such class has a trivial
837 // default constructor.
838 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000839 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000840
841 // C++0x [class.copy]p13:
842 // A copy/move constructor for class X is trivial if [...]
843 // [...]
844 // -- for each non-static data member of X that is of class type (or
845 // an array thereof), the constructor selected to copy/move that
846 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000847 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000848 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000849 // If the field doesn't have a simple move constructor, we'll eagerly
850 // declare the move constructor for this class and we'll decide whether
851 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000852 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000853 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000854
855 // C++0x [class.copy]p27:
856 // A copy/move assignment operator for class X is trivial if [...]
857 // [...]
858 // -- for each non-static data member of X that is of class type (or
859 // an array thereof), the assignment operator selected to
860 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000861 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000862 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000863 // If the field doesn't have a simple move assignment, we'll eagerly
864 // declare the move assignment for this class and we'll decide whether
865 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000866 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000867 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000868
Douglas Gregor11c024b2010-09-28 20:50:54 +0000869 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000870 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000871 if (!FieldRec->hasIrrelevantDestructor())
872 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000873 if (FieldRec->hasObjectMember())
874 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000875 if (FieldRec->hasVolatileMember())
876 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000877
878 // C++0x [class]p7:
879 // A standard-layout class is a class that:
880 // -- has no non-static data members of type non-standard-layout
881 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000882 if (!FieldRec->isStandardLayout())
883 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000884
885 // C++0x [class]p7:
886 // A standard-layout class is a class that:
887 // [...]
888 // -- has no base classes of the same type as the first non-static
889 // data member.
890 // We don't want to expend bits in the state of the record decl
891 // tracking whether this is the first non-static data member so we
892 // cheat a bit and use some of the existing state: the empty bit.
893 // Virtual bases and virtual methods make a class non-empty, but they
894 // also make it non-standard-layout so we needn't check here.
895 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000896 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000897 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000898 // data member must come through here with Empty still true, and Empty
899 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000900 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000901 for (const auto &BI : bases()) {
902 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000903 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000904 break;
905 }
906 }
907 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000908
909 // Keep track of the presence of mutable fields.
910 if (FieldRec->hasMutableFields())
911 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000912
913 // C++11 [class.copy]p13:
914 // If the implicitly-defined constructor would satisfy the
915 // requirements of a constexpr constructor, the implicitly-defined
916 // constructor is constexpr.
917 // C++11 [dcl.constexpr]p4:
918 // -- every constructor involved in initializing non-static data
919 // members [...] shall be a constexpr constructor
920 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000921 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000922 // The standard requires any in-class initializer to be a constant
923 // expression. We consider this to be a defect.
924 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000925
926 // C++11 [class.copy]p8:
927 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +0000928 // the form 'X::X(const X&)' if each potentially constructed subobject
929 // of a class type M (or array thereof) has a copy constructor whose
930 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +0000931 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000932 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000933
934 // C++11 [class.copy]p18:
935 // The implicitly-declared copy assignment oeprator for a class X will
936 // have the form 'X& X::operator=(const X&)' if [...] for all the
937 // non-static data members of X that are of a class type M (or array
938 // thereof), each such class type has a copy assignment operator whose
939 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
940 if (!FieldRec->hasCopyAssignmentWithConstParam())
941 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000942
943 if (FieldRec->hasUninitializedReferenceMember() &&
944 !Field->hasInClassInitializer())
945 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000946
947 // C++11 [class.union]p8, DR1460:
948 // a non-static data member of an anonymous union that is a member of
949 // X is also a variant member of X.
950 if (FieldRec->hasVariantMembers() &&
951 Field->isAnonymousStructOrUnion())
952 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000953 }
Richard Smithcc36f692011-12-22 02:22:31 +0000954 } else {
955 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000956 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000957 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000958 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000959
960 // C++11 [class.copy]p23:
961 // A defaulted copy/move assignment operator for a class X is defined
962 // as deleted if X has:
963 // -- a non-static data member of const non-class type (or array
964 // thereof)
965 if (T.isConstQualified())
966 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000967 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000968
969 // C++0x [class]p7:
970 // A standard-layout class is a class that:
971 // [...]
972 // -- either has no non-static data members in the most derived
973 // class and at most one base class with non-static data members,
974 // or has no base classes with non-static data members, and
975 // At this point we know that we have a non-static data member, so the last
976 // clause holds.
977 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000978 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000979
David Majnemer5cfda6f2016-05-22 05:34:26 +0000980 // C++14 [meta.unary.prop]p4:
981 // T is a class type [...] with [...] no non-static data members other
982 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000983 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000984 if (!Field->isBitField() ||
985 (!Field->getBitWidth()->isTypeDependent() &&
986 !Field->getBitWidth()->isValueDependent() &&
987 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000988 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000989 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000990 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000991
992 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000993 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000994 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000995 == DeclarationName::CXXConversionFunctionName) {
996 ASTContext &Ctx = getASTContext();
997 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
998 }
999 }
Richard Smith12e79312016-05-13 06:47:56 +00001000
1001 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
1002 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001003 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001004 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001005 // C++1z [dcl.init.aggr]p1:
1006 // An aggregate is [...] a class [...] with no inherited constructors
1007 data().Aggregate = false;
1008 }
Richard Smith12e79312016-05-13 06:47:56 +00001009
1010 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1011 data().HasInheritedAssignment = true;
1012 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001013}
1014
Richard Smith92f241f2012-12-08 02:53:02 +00001015void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1016 assert(!D->isImplicit() && !D->isUserProvided());
1017
1018 // The kind of special member this declaration is, if any.
1019 unsigned SMKind = 0;
1020
1021 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1022 if (Constructor->isDefaultConstructor()) {
1023 SMKind |= SMF_DefaultConstructor;
1024 if (Constructor->isConstexpr())
1025 data().HasConstexprDefaultConstructor = true;
1026 }
1027 if (Constructor->isCopyConstructor())
1028 SMKind |= SMF_CopyConstructor;
1029 else if (Constructor->isMoveConstructor())
1030 SMKind |= SMF_MoveConstructor;
1031 else if (Constructor->isConstexpr())
1032 // We may now know that the constructor is constexpr.
1033 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001034 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001035 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001036 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1037 data().HasIrrelevantDestructor = false;
1038 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001039 SMKind |= SMF_CopyAssignment;
1040 else if (D->isMoveAssignmentOperator())
1041 SMKind |= SMF_MoveAssignment;
1042
1043 // Update which trivial / non-trivial special members we have.
1044 // addedMember will have skipped this step for this member.
1045 if (D->isTrivial())
1046 data().HasTrivialSpecialMembers |= SMKind;
1047 else
1048 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1049}
1050
Joao Matose9a3ed42012-08-31 22:18:20 +00001051bool CXXRecordDecl::isCLike() const {
1052 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1053 !TemplateOrInstantiation.isNull())
1054 return false;
1055 if (!hasDefinition())
1056 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001057
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001058 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001059}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001060
Faisal Vali2b391ab2013-09-26 19:54:12 +00001061bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001062 if (!isLambda()) return false;
1063 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001064}
1065
1066CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001067 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001068 DeclarationName Name =
1069 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001070 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001071
1072 assert(!Calls.empty() && "Missing lambda call operator!");
1073 assert(Calls.size() == 1 && "More than one lambda call operator!");
1074
1075 NamedDecl *CallOp = Calls.front();
1076 if (FunctionTemplateDecl *CallOpTmpl =
1077 dyn_cast<FunctionTemplateDecl>(CallOp))
1078 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001079
1080 return cast<CXXMethodDecl>(CallOp);
1081}
1082
1083CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001084 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001085 DeclarationName Name =
1086 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001087 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001088 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001089 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1090 NamedDecl *InvokerFun = Invoker.front();
1091 if (FunctionTemplateDecl *InvokerTemplate =
1092 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1093 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1094
Faisal Vali571df122013-09-29 08:45:24 +00001095 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001096}
1097
Douglas Gregor9c702202012-02-10 07:45:31 +00001098void CXXRecordDecl::getCaptureFields(
1099 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001100 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001101 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001102 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001103
Douglas Gregorc8a73492012-02-13 15:44:47 +00001104 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001105 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001106 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001107 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001108 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001109 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001110 else if (C->capturesVariable())
1111 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001112 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001113 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001114}
1115
Faisal Vali2b391ab2013-09-26 19:54:12 +00001116TemplateParameterList *
1117CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001118 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001119 CXXMethodDecl *CallOp = getLambdaCallOperator();
1120 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1121 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001122 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001123}
Douglas Gregor9c702202012-02-10 07:45:31 +00001124
Richard Smith0bae6242016-08-25 00:34:00 +00001125Decl *CXXRecordDecl::getLambdaContextDecl() const {
1126 assert(isLambda() && "Not a lambda closure type!");
1127 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1128 return getLambdaData().ContextDecl.get(Source);
1129}
1130
John McCall1e3a1a72010-03-15 09:07:48 +00001131static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001132 QualType T =
1133 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1134 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001135 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001136}
1137
John McCall1e3a1a72010-03-15 09:07:48 +00001138/// Collect the visible conversions of a base class.
1139///
James Dennettb5d5f812012-06-15 22:28:09 +00001140/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001141/// \param InVirtual whether this base class is a virtual base (or a base
1142/// of a virtual base)
1143/// \param Access the access along the inheritance path to this base
1144/// \param ParentHiddenTypes the conversions provided by the inheritors
1145/// of this base
1146/// \param Output the set to which to add conversions from non-virtual bases
1147/// \param VOutput the set to which to add conversions from virtual bases
1148/// \param HiddenVBaseCs the set of conversions which were hidden in a
1149/// virtual base along some inheritance path
1150static void CollectVisibleConversions(ASTContext &Context,
1151 CXXRecordDecl *Record,
1152 bool InVirtual,
1153 AccessSpecifier Access,
1154 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001155 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001156 UnresolvedSetImpl &VOutput,
1157 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1158 // The set of types which have conversions in this class or its
1159 // subclasses. As an optimization, we don't copy the derived set
1160 // unless it might change.
1161 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1162 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1163
1164 // Collect the direct conversions and figure out which conversions
1165 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001166 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1167 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1168 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001169 HiddenTypesBuffer = ParentHiddenTypes;
1170 HiddenTypes = &HiddenTypesBuffer;
1171
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001172 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001173 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1174 bool Hidden = ParentHiddenTypes.count(ConvType);
1175 if (!Hidden)
1176 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001177
1178 // If this conversion is hidden and we're in a virtual base,
1179 // remember that it's hidden along some inheritance path.
1180 if (Hidden && InVirtual)
1181 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1182
1183 // If this conversion isn't hidden, add it to the appropriate output.
1184 else if (!Hidden) {
1185 AccessSpecifier IAccess
1186 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1187
1188 if (InVirtual)
1189 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001190 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001191 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001192 }
1193 }
1194 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001195
John McCall1e3a1a72010-03-15 09:07:48 +00001196 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001197 for (const auto &I : Record->bases()) {
1198 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001199 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001200
John McCall1e3a1a72010-03-15 09:07:48 +00001201 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001202 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1203 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001204
John McCall1e3a1a72010-03-15 09:07:48 +00001205 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1206 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1207 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001208 }
John McCall1e3a1a72010-03-15 09:07:48 +00001209}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001210
John McCall1e3a1a72010-03-15 09:07:48 +00001211/// Collect the visible conversions of a class.
1212///
1213/// This would be extremely straightforward if it weren't for virtual
1214/// bases. It might be worth special-casing that, really.
1215static void CollectVisibleConversions(ASTContext &Context,
1216 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001217 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001218 // The collection of all conversions in virtual bases that we've
1219 // found. These will be added to the output as long as they don't
1220 // appear in the hidden-conversions set.
1221 UnresolvedSet<8> VBaseCs;
1222
1223 // The set of conversions in virtual bases that we've determined to
1224 // be hidden.
1225 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1226
1227 // The set of types hidden by classes derived from this one.
1228 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1229
1230 // Go ahead and collect the direct conversions and add them to the
1231 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001232 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1233 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001234 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001235 for (; ConvI != ConvE; ++ConvI)
1236 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001237
1238 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001239 for (const auto &I : Record->bases()) {
1240 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001241 if (!RT) continue;
1242
1243 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001244 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001245 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1246 }
1247
1248 // Add any unhidden conversions provided by virtual bases.
1249 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1250 I != E; ++I) {
1251 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001252 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001253 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001254}
1255
1256/// getVisibleConversionFunctions - get all conversion functions visible
1257/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001258llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001259CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001260 ASTContext &Ctx = getASTContext();
1261
1262 ASTUnresolvedSet *Set;
1263 if (bases_begin() == bases_end()) {
1264 // If root class, all conversions are visible.
1265 Set = &data().Conversions.get(Ctx);
1266 } else {
1267 Set = &data().VisibleConversions.get(Ctx);
1268 // If visible conversion list is not evaluated, evaluate it.
1269 if (!data().ComputedVisibleConversions) {
1270 CollectVisibleConversions(Ctx, this, *Set);
1271 data().ComputedVisibleConversions = true;
1272 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001273 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001274 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001275}
1276
John McCallda4458e2010-03-31 01:36:47 +00001277void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1278 // This operation is O(N) but extremely rare. Sema only uses it to
1279 // remove UsingShadowDecls in a class that were followed by a direct
1280 // declaration, e.g.:
1281 // class A : B {
1282 // using B::operator int;
1283 // operator int();
1284 // };
1285 // This is uncommon by itself and even more uncommon in conjunction
1286 // with sufficiently large numbers of directly-declared conversions
1287 // that asymptotic behavior matters.
1288
Richard Smitha4ba74c2013-08-30 04:46:40 +00001289 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001290 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1291 if (Convs[I].getDecl() == ConvDecl) {
1292 Convs.erase(I);
1293 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1294 && "conversion was found multiple times in unresolved set");
1295 return;
1296 }
1297 }
1298
1299 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001300}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001301
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001302CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001303 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001304 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001305
1306 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001307}
1308
Chandler Carruth21c90602015-12-30 03:24:14 +00001309MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1310 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1311}
1312
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001313void
1314CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1315 TemplateSpecializationKind TSK) {
1316 assert(TemplateOrInstantiation.isNull() &&
1317 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001318 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001319 TemplateOrInstantiation
1320 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1321}
1322
Chandler Carruth21c90602015-12-30 03:24:14 +00001323ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1324 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1325}
1326
1327void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1328 TemplateOrInstantiation = Template;
1329}
1330
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001331TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1332 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001333 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1334 return Spec->getSpecializationKind();
1335
Douglas Gregor06db9f52009-10-12 20:18:28 +00001336 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001337 return MSInfo->getTemplateSpecializationKind();
1338
1339 return TSK_Undeclared;
1340}
1341
1342void
1343CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1344 if (ClassTemplateSpecializationDecl *Spec
1345 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1346 Spec->setSpecializationKind(TSK);
1347 return;
1348 }
1349
Douglas Gregor06db9f52009-10-12 20:18:28 +00001350 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001351 MSInfo->setTemplateSpecializationKind(TSK);
1352 return;
1353 }
1354
David Blaikie83d382b2011-09-23 05:06:16 +00001355 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001356}
1357
Reid Klecknere7367d62014-10-14 20:28:40 +00001358const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1359 // If it's a class template specialization, find the template or partial
1360 // specialization from which it was instantiated.
1361 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1362 auto From = TD->getInstantiatedFrom();
1363 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1364 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1365 if (NewCTD->isMemberSpecialization())
1366 break;
1367 CTD = NewCTD;
1368 }
Richard Smith7a591a42015-07-08 02:22:15 +00001369 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001370 }
1371 if (auto *CTPSD =
1372 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1373 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1374 if (NewCTPSD->isMemberSpecialization())
1375 break;
1376 CTPSD = NewCTPSD;
1377 }
Richard Smith7a591a42015-07-08 02:22:15 +00001378 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001379 }
1380 }
1381
1382 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1383 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1384 const CXXRecordDecl *RD = this;
1385 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1386 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001387 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001388 }
1389 }
1390
1391 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1392 "couldn't find pattern for class template instantiation");
1393 return nullptr;
1394}
1395
Douglas Gregorbac74902010-07-01 14:13:13 +00001396CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1397 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001398 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001399
1400 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001401 = Context.DeclarationNames.getCXXDestructorName(
1402 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001403
Richard Smithcf4bdde2015-02-21 02:45:19 +00001404 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001405 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001406 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001407
David Blaikieff7d47a2012-12-19 00:45:41 +00001408 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001409 return Dtor;
1410}
1411
Richard Trieu95a192a2015-05-28 00:14:02 +00001412bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1413 // Destructor is noreturn.
1414 if (const CXXDestructorDecl *Destructor = getDestructor())
1415 if (Destructor->isNoReturn())
1416 return true;
1417
1418 // Check base classes destructor for noreturn.
1419 for (const auto &Base : bases())
1420 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1421 return true;
1422
1423 // Check fields for noreturn.
1424 for (const auto *Field : fields())
1425 if (const CXXRecordDecl *RD =
1426 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1427 if (RD->isAnyDestructorNoReturn())
1428 return true;
1429
1430 // All destructors are not noreturn.
1431 return false;
1432}
1433
Douglas Gregorb11aad82011-02-19 18:51:44 +00001434void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001435 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001436}
1437
1438void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1439 RecordDecl::completeDefinition();
1440
Douglas Gregor8fb95122010-09-29 00:15:42 +00001441 // If the class may be abstract (but hasn't been marked as such), check for
1442 // any pure final overriders.
1443 if (mayBeAbstract()) {
1444 CXXFinalOverriderMap MyFinalOverriders;
1445 if (!FinalOverriders) {
1446 getFinalOverriders(MyFinalOverriders);
1447 FinalOverriders = &MyFinalOverriders;
1448 }
1449
1450 bool Done = false;
1451 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1452 MEnd = FinalOverriders->end();
1453 M != MEnd && !Done; ++M) {
1454 for (OverridingMethods::iterator SO = M->second.begin(),
1455 SOEnd = M->second.end();
1456 SO != SOEnd && !Done; ++SO) {
1457 assert(SO->second.size() > 0 &&
1458 "All virtual functions have overridding virtual functions");
1459
1460 // C++ [class.abstract]p4:
1461 // A class is abstract if it contains or inherits at least one
1462 // pure virtual function for which the final overrider is pure
1463 // virtual.
1464 if (SO->second.front().Method->isPure()) {
1465 data().Abstract = true;
1466 Done = true;
1467 break;
1468 }
1469 }
1470 }
1471 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001472
1473 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001474 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001475 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001476 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001477}
1478
1479bool CXXRecordDecl::mayBeAbstract() const {
1480 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1481 isDependentContext())
1482 return false;
1483
Aaron Ballman574705e2014-03-13 15:41:46 +00001484 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001485 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001486 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001487 if (BaseDecl->isAbstract())
1488 return true;
1489 }
1490
1491 return false;
1492}
1493
Richard Smithbc491202017-02-17 20:05:37 +00001494void CXXDeductionGuideDecl::anchor() { }
1495
1496CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1497 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1498 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1499 SourceLocation EndLocation) {
1500 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1501 NameInfo, T, TInfo, EndLocation);
1502}
1503
1504CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1505 unsigned ID) {
1506 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1507 DeclarationNameInfo(), QualType(),
1508 nullptr, SourceLocation());
1509}
1510
David Blaikie68e081d2011-12-20 02:48:34 +00001511void CXXMethodDecl::anchor() { }
1512
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001513bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001514 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001515
1516 if (MD->getStorageClass() == SC_Static)
1517 return true;
1518
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001519 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1520 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001521}
1522
Rafael Espindola49e860b2012-06-26 17:45:31 +00001523static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1524 const CXXMethodDecl *BaseMD) {
1525 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1526 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1527 const CXXMethodDecl *MD = *I;
1528 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1529 return true;
1530 if (recursivelyOverrides(MD, BaseMD))
1531 return true;
1532 }
1533 return false;
1534}
1535
1536CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001537CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1538 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001539 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1540 return this;
1541
1542 // Lookup doesn't work for destructors, so handle them separately.
1543 if (isa<CXXDestructorDecl>(this)) {
1544 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001545 if (MD) {
1546 if (recursivelyOverrides(MD, this))
1547 return MD;
1548 if (MayBeBase && recursivelyOverrides(this, MD))
1549 return MD;
1550 }
Craig Topper36250ad2014-05-12 05:36:57 +00001551 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001552 }
1553
Richard Smith40c78062015-02-21 02:31:57 +00001554 for (auto *ND : RD->lookup(getDeclName())) {
1555 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001556 if (!MD)
1557 continue;
1558 if (recursivelyOverrides(MD, this))
1559 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001560 if (MayBeBase && recursivelyOverrides(this, MD))
1561 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001562 }
1563
Aaron Ballman574705e2014-03-13 15:41:46 +00001564 for (const auto &I : RD->bases()) {
1565 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001566 if (!RT)
1567 continue;
1568 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1569 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1570 if (T)
1571 return T;
1572 }
1573
Craig Topper36250ad2014-05-12 05:36:57 +00001574 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001575}
1576
Ted Kremenek21475702008-09-05 17:16:31 +00001577CXXMethodDecl *
1578CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001579 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001580 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001581 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001582 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001583 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001584 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1585 T, TInfo, SC, isInline, isConstexpr,
1586 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001587}
1588
Douglas Gregor72172e92012-01-05 21:55:30 +00001589CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001590 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001591 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001592 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001593}
1594
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001595bool CXXMethodDecl::isUsualDeallocationFunction() const {
1596 if (getOverloadedOperator() != OO_Delete &&
1597 getOverloadedOperator() != OO_Array_Delete)
1598 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001599
1600 // C++ [basic.stc.dynamic.deallocation]p2:
1601 // A template instance is never a usual deallocation function,
1602 // regardless of its signature.
1603 if (getPrimaryTemplate())
1604 return false;
1605
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001606 // C++ [basic.stc.dynamic.deallocation]p2:
1607 // If a class T has a member deallocation function named operator delete
1608 // with exactly one parameter, then that function is a usual (non-placement)
1609 // deallocation function. [...]
1610 if (getNumParams() == 1)
1611 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00001612 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00001613
Richard Smithb2f0f052016-10-10 18:54:32 +00001614 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001615 // [...] If class T does not declare such an operator delete but does
1616 // declare a member deallocation function named operator delete with
1617 // exactly two parameters, the second of which has type std::size_t (18.1),
1618 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00001619 //
1620 // C++17 says a usual deallocation function is one with the signature
1621 // (void* [, size_t] [, std::align_val_t] [, ...])
1622 // and all such functions are usual deallocation functions. It's not clear
1623 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001624 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00001625 if (UsualParams < getNumParams() &&
1626 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
1627 Context.getSizeType()))
1628 ++UsualParams;
1629
1630 if (UsualParams < getNumParams() &&
1631 getParamDecl(UsualParams)->getType()->isAlignValT())
1632 ++UsualParams;
1633
1634 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001635 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00001636
1637 // In C++17 onwards, all potential usual deallocation functions are actual
1638 // usual deallocation functions.
1639 if (Context.getLangOpts().AlignedAllocation)
1640 return true;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001641
1642 // This function is a usual deallocation function if there are no
1643 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001644 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1645 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001646 I != E; ++I) {
1647 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001648 if (FD->getNumParams() == 1)
1649 return false;
1650 }
1651
1652 return true;
1653}
1654
Douglas Gregorb139cd52010-05-01 20:49:11 +00001655bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001656 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001657 // A user-declared copy assignment operator X::operator= is a non-static
1658 // non-template member function of class X with exactly one parameter of
1659 // type X, X&, const X&, volatile X& or const volatile X&.
1660 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1661 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001662 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1663 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001664 return false;
1665
1666 QualType ParamType = getParamDecl(0)->getType();
1667 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1668 ParamType = Ref->getPointeeType();
1669
1670 ASTContext &Context = getASTContext();
1671 QualType ClassType
1672 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1673 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1674}
1675
Alexis Huntfcaeae42011-05-25 20:50:04 +00001676bool CXXMethodDecl::isMoveAssignmentOperator() const {
1677 // C++0x [class.copy]p19:
1678 // A user-declared move assignment operator X::operator= is a non-static
1679 // non-template member function of class X with exactly one parameter of type
1680 // X&&, const X&&, volatile X&&, or const volatile X&&.
1681 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001682 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1683 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001684 return false;
1685
1686 QualType ParamType = getParamDecl(0)->getType();
1687 if (!isa<RValueReferenceType>(ParamType))
1688 return false;
1689 ParamType = ParamType->getPointeeType();
1690
1691 ASTContext &Context = getASTContext();
1692 QualType ClassType
1693 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1694 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1695}
1696
Anders Carlsson36d87e12009-05-16 23:58:37 +00001697void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001698 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001699 assert(!MD->getParent()->isDependentContext() &&
1700 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001701 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001702
Douglas Gregor832940b2010-03-02 23:58:15 +00001703 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001704}
1705
1706CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001707 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001708 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001709}
1710
1711CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001712 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001713 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001714}
1715
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001716unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001717 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001718 return getASTContext().overridden_methods_size(this);
1719}
1720
Clement Courbet6ecaec82016-07-05 07:49:31 +00001721CXXMethodDecl::overridden_method_range
1722CXXMethodDecl::overridden_methods() const {
1723 if (isa<CXXConstructorDecl>(this))
1724 return overridden_method_range(nullptr, nullptr);
1725 return getASTContext().overridden_methods(this);
1726}
1727
Ted Kremenek21475702008-09-05 17:16:31 +00001728QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001729 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1730 // If the member function is declared const, the type of this is const X*,
1731 // if the member function is declared volatile, the type of this is
1732 // volatile X*, and if the member function is declared const volatile,
1733 // the type of this is const volatile X*.
1734
Ted Kremenek21475702008-09-05 17:16:31 +00001735 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001736
John McCalle78aac42010-03-10 03:28:59 +00001737 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001738 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00001739 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001740 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001741}
1742
Eli Friedman71a26d82009-12-06 20:50:05 +00001743bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001744 // If this function is a template instantiation, look at the template from
1745 // which it was instantiated.
1746 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1747 if (!CheckFn)
1748 CheckFn = this;
1749
Eli Friedman71a26d82009-12-06 20:50:05 +00001750 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001751 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001752}
1753
Douglas Gregor355efbb2012-02-17 03:02:34 +00001754bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001755 const CXXRecordDecl *P = getParent();
1756 if (P->isLambda()) {
1757 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1758 if (StaticInvoker == this) return true;
1759 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1760 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1761 }
1762 }
1763 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001764}
1765
Alexis Hunt1d792652011-01-08 20:30:50 +00001766CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1767 TypeSourceInfo *TInfo, bool IsVirtual,
1768 SourceLocation L, Expr *Init,
1769 SourceLocation R,
1770 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001771 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001772 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
Richard Smith30e304e2016-12-14 00:03:17 +00001773 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001774{
Douglas Gregore8381c02008-11-05 04:29:56 +00001775}
1776
Alexis Hunt1d792652011-01-08 20:30:50 +00001777CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1778 FieldDecl *Member,
1779 SourceLocation MemberLoc,
1780 SourceLocation L, Expr *Init,
1781 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001782 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001783 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001784 IsWritten(false), SourceOrder(0)
Francois Pichetd583da02010-12-04 09:14:42 +00001785{
1786}
1787
Alexis Hunt1d792652011-01-08 20:30:50 +00001788CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1789 IndirectFieldDecl *Member,
1790 SourceLocation MemberLoc,
1791 SourceLocation L, Expr *Init,
1792 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001793 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001794 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001795 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001796{
Douglas Gregore8381c02008-11-05 04:29:56 +00001797}
1798
Alexis Hunt1d792652011-01-08 20:30:50 +00001799CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001800 TypeSourceInfo *TInfo,
1801 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001802 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001803 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1804 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001805 IsWritten(false), SourceOrder(0)
Alexis Huntc5575cc2011-02-26 19:13:13 +00001806{
1807}
1808
Alexis Hunt1d792652011-01-08 20:30:50 +00001809TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001810 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001811 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001812 else
1813 return TypeLoc();
1814}
1815
Alexis Hunt1d792652011-01-08 20:30:50 +00001816const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001817 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001818 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001819 else
Craig Topper36250ad2014-05-12 05:36:57 +00001820 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001821}
1822
Alexis Hunt1d792652011-01-08 20:30:50 +00001823SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001824 if (isInClassMemberInitializer())
1825 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001826
David Blaikiea81d4102015-01-18 00:12:58 +00001827 if (isAnyMemberInitializer())
1828 return getMemberLocation();
1829
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001830 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1831 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1832
1833 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001834}
1835
Alexis Hunt1d792652011-01-08 20:30:50 +00001836SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001837 if (isInClassMemberInitializer()) {
1838 FieldDecl *D = getAnyMember();
1839 if (Expr *I = D->getInClassInitializer())
1840 return I->getSourceRange();
1841 return SourceRange();
1842 }
1843
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001844 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001845}
1846
David Blaikie68e081d2011-12-20 02:48:34 +00001847void CXXConstructorDecl::anchor() { }
1848
Richard Smith5179eb72016-06-28 19:03:57 +00001849CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
1850 unsigned ID,
1851 bool Inherited) {
1852 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
1853 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
1854 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
1855 false, false, false, false, InheritedConstructor());
1856 Result->IsInheritingConstructor = Inherited;
1857 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00001858}
1859
1860CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001861CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001862 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001863 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001864 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001865 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00001866 bool isImplicitlyDeclared, bool isConstexpr,
1867 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001868 assert(NameInfo.getName().getNameKind()
1869 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001870 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00001871 unsigned Extra =
1872 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
1873 return new (C, RD, Extra) CXXConstructorDecl(
1874 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
1875 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00001876}
1877
Richard Smithc2bb8182015-03-24 06:36:48 +00001878CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1879 return CtorInitializers.get(getASTContext().getExternalSource());
1880}
1881
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001882CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1883 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1884 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1885 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1886 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001887
1888 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001889}
1890
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001891bool CXXConstructorDecl::isDefaultConstructor() const {
1892 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001893 // A default constructor for a class X is a constructor of class
1894 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001895 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001896 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001897}
1898
Mike Stump11289f42009-09-09 15:08:12 +00001899bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001900CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001901 return isCopyOrMoveConstructor(TypeQuals) &&
1902 getParamDecl(0)->getType()->isLValueReferenceType();
1903}
1904
1905bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1906 return isCopyOrMoveConstructor(TypeQuals) &&
1907 getParamDecl(0)->getType()->isRValueReferenceType();
1908}
1909
1910/// \brief Determine whether this is a copy or move constructor.
1911bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001912 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001913 // A non-template constructor for class X is a copy constructor
1914 // if its first parameter is of type X&, const X&, volatile X& or
1915 // const volatile X&, and either there are no other parameters
1916 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001917 // C++0x [class.copy]p3:
1918 // A non-template constructor for class X is a move constructor if its
1919 // first parameter is of type X&&, const X&&, volatile X&&, or
1920 // const volatile X&&, and either there are no other parameters or else
1921 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001922 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001923 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001924 (getPrimaryTemplate() != nullptr) ||
1925 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001926 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001927
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001928 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001929
1930 // Do we have a reference type?
1931 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001932 if (!ParamRefType)
1933 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001934
Douglas Gregorff7028a2009-11-13 23:59:09 +00001935 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001936 ASTContext &Context = getASTContext();
1937
Douglas Gregorff7028a2009-11-13 23:59:09 +00001938 CanQualType PointeeType
1939 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001940 CanQualType ClassTy
1941 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001942 if (PointeeType.getUnqualifiedType() != ClassTy)
1943 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001944
John McCall8ccfcb52009-09-24 19:53:00 +00001945 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001946
1947 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001948 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001949 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001950}
1951
Anders Carlssond20e7952009-08-28 16:57:08 +00001952bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001953 // C++ [class.conv.ctor]p1:
1954 // A constructor declared without the function-specifier explicit
1955 // that can be called with a single parameter specifies a
1956 // conversion from the type of its first parameter to the type of
1957 // its class. Such a constructor is called a converting
1958 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001959 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001960 return false;
1961
Mike Stump11289f42009-09-09 15:08:12 +00001962 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001963 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001964 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001965 (getNumParams() > 1 &&
1966 (getParamDecl(1)->hasDefaultArg() ||
1967 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001968}
Douglas Gregor61956c42008-10-31 09:07:45 +00001969
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001970bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001971 if ((getNumParams() < 1) ||
1972 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001973 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001974 return false;
1975
1976 const ParmVarDecl *Param = getParamDecl(0);
1977
1978 ASTContext &Context = getASTContext();
1979 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1980
Douglas Gregorffe14e32009-11-14 01:20:54 +00001981 // Is it the same as our our class type?
1982 CanQualType ClassTy
1983 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1984 if (ParamType.getUnqualifiedType() != ClassTy)
1985 return false;
1986
1987 return true;
1988}
1989
David Blaikie68e081d2011-12-20 02:48:34 +00001990void CXXDestructorDecl::anchor() { }
1991
Douglas Gregor831c93f2008-11-05 20:51:48 +00001992CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001993CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001994 return new (C, ID)
1995 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1996 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001997}
1998
1999CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002000CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002001 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002002 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002003 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002004 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002005 assert(NameInfo.getName().getNameKind()
2006 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002007 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002008 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002009 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002010}
2011
Richard Smithf8134002015-03-10 01:41:22 +00002012void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
2013 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2014 if (OD && !First->OperatorDelete) {
2015 First->OperatorDelete = OD;
2016 if (auto *L = getASTMutationListener())
2017 L->ResolvedOperatorDelete(First, OD);
2018 }
2019}
2020
David Blaikie68e081d2011-12-20 02:48:34 +00002021void CXXConversionDecl::anchor() { }
2022
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002023CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002024CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002025 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002026 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002027 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002028 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002029}
2030
2031CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002032CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002033 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002034 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002035 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002036 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002037 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002038 assert(NameInfo.getName().getNameKind()
2039 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002040 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002041 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002042 isInline, isExplicit, isConstexpr,
2043 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002044}
2045
Douglas Gregord3b672c2012-02-16 01:06:16 +00002046bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2047 return isImplicit() && getParent()->isLambda() &&
2048 getConversionType()->isBlockPointerType();
2049}
2050
David Blaikie68e081d2011-12-20 02:48:34 +00002051void LinkageSpecDecl::anchor() { }
2052
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002053LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002054 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002055 SourceLocation ExternLoc,
2056 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002057 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002058 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002059 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002060}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002061
Richard Smithf7981722013-11-22 09:01:48 +00002062LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2063 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002064 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2065 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002066}
2067
David Blaikie68e081d2011-12-20 02:48:34 +00002068void UsingDirectiveDecl::anchor() { }
2069
Douglas Gregor889ceb72009-02-03 19:21:40 +00002070UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2071 SourceLocation L,
2072 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002073 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002074 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002075 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002076 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002077 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2078 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002079 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2080 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002081}
2082
Richard Smithf7981722013-11-22 09:01:48 +00002083UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2084 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002085 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2086 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002087 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002088 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002089}
2090
Sebastian Redla6602e92009-11-23 15:34:23 +00002091NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2092 if (NamespaceAliasDecl *NA =
2093 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2094 return NA->getNamespace();
2095 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2096}
2097
Richard Smith053f6c62014-05-16 23:01:30 +00002098NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2099 SourceLocation StartLoc, SourceLocation IdLoc,
2100 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2101 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2102 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2103 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002104 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002105
Douglas Gregore57e7522012-01-07 09:11:48 +00002106 if (PrevDecl)
2107 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2108}
2109
Douglas Gregor72172e92012-01-05 21:55:30 +00002110NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002111 bool Inline, SourceLocation StartLoc,
2112 SourceLocation IdLoc, IdentifierInfo *Id,
2113 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002114 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2115 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002116}
2117
2118NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002119 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002120 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002121}
2122
Chandler Carruth21c90602015-12-30 03:24:14 +00002123NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2124 if (isFirstDecl())
2125 return this;
2126
2127 return AnonOrFirstNamespaceAndInline.getPointer();
2128}
2129
2130const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2131 if (isFirstDecl())
2132 return this;
2133
2134 return AnonOrFirstNamespaceAndInline.getPointer();
2135}
2136
2137bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2138
Richard Smithd7af8a32014-05-10 01:17:36 +00002139NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2140 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002141}
2142NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2143 return getPreviousDecl();
2144}
2145NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2146 return getMostRecentDecl();
2147}
2148
David Blaikie68e081d2011-12-20 02:48:34 +00002149void NamespaceAliasDecl::anchor() { }
2150
Richard Smithf4634362014-09-03 23:11:22 +00002151NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2152 return getNextRedeclaration();
2153}
2154NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2155 return getPreviousDecl();
2156}
2157NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2158 return getMostRecentDecl();
2159}
2160
Mike Stump11289f42009-09-09 15:08:12 +00002161NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002162 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002163 SourceLocation AliasLoc,
2164 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002165 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002166 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002167 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002168 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002169 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2170 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002171 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002172 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002173}
2174
Douglas Gregor72172e92012-01-05 21:55:30 +00002175NamespaceAliasDecl *
2176NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002177 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002178 SourceLocation(), nullptr,
2179 NestedNameSpecifierLoc(),
2180 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002181}
2182
David Blaikie68e081d2011-12-20 02:48:34 +00002183void UsingShadowDecl::anchor() { }
2184
Richard Smith5179eb72016-06-28 19:03:57 +00002185UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2186 SourceLocation Loc, UsingDecl *Using,
2187 NamedDecl *Target)
2188 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
2189 redeclarable_base(C), Underlying(Target),
2190 UsingOrNextShadow(cast<NamedDecl>(Using)) {
2191 if (Target)
2192 IdentifierNamespace = Target->getIdentifierNamespace();
2193 setImplicit();
2194}
2195
2196UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2197 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
2198 redeclarable_base(C), Underlying(), UsingOrNextShadow() {}
2199
Douglas Gregor72172e92012-01-05 21:55:30 +00002200UsingShadowDecl *
2201UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002202 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002203}
2204
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002205UsingDecl *UsingShadowDecl::getUsingDecl() const {
2206 const UsingShadowDecl *Shadow = this;
2207 while (const UsingShadowDecl *NextShadow =
2208 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2209 Shadow = NextShadow;
2210 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2211}
2212
Richard Smith5179eb72016-06-28 19:03:57 +00002213void ConstructorUsingShadowDecl::anchor() { }
2214
2215ConstructorUsingShadowDecl *
2216ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2217 SourceLocation Loc, UsingDecl *Using,
2218 NamedDecl *Target, bool IsVirtual) {
2219 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2220 IsVirtual);
2221}
2222
2223ConstructorUsingShadowDecl *
2224ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2225 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2226}
2227
2228CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2229 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2230}
2231
David Blaikie68e081d2011-12-20 02:48:34 +00002232void UsingDecl::anchor() { }
2233
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002234void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2235 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2236 "declaration already in set");
2237 assert(S->getUsingDecl() == this);
2238
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002239 if (FirstUsingShadow.getPointer())
2240 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2241 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002242}
2243
2244void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2245 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2246 "declaration not in set");
2247 assert(S->getUsingDecl() == this);
2248
2249 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2250
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002251 if (FirstUsingShadow.getPointer() == S) {
2252 FirstUsingShadow.setPointer(
2253 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002254 S->UsingOrNextShadow = this;
2255 return;
2256 }
2257
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002258 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002259 while (Prev->UsingOrNextShadow != S)
2260 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2261 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2262 S->UsingOrNextShadow = this;
2263}
2264
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002265UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2266 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002267 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002268 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002269 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002270}
2271
Douglas Gregor72172e92012-01-05 21:55:30 +00002272UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002273 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2274 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2275 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002276}
2277
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002278SourceRange UsingDecl::getSourceRange() const {
2279 SourceLocation Begin = isAccessDeclaration()
2280 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2281 return SourceRange(Begin, getNameInfo().getEndLoc());
2282}
2283
Richard Smith151c4562016-12-20 21:35:28 +00002284void UsingPackDecl::anchor() { }
2285
2286UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2287 NamedDecl *InstantiatedFrom,
2288 ArrayRef<NamedDecl *> UsingDecls) {
2289 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2290 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2291}
2292
2293UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2294 unsigned NumExpansions) {
2295 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2296 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2297 Result->NumExpansions = NumExpansions;
2298 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2299 for (unsigned I = 0; I != NumExpansions; ++I)
2300 new (Trail + I) NamedDecl*(nullptr);
2301 return Result;
2302}
2303
David Blaikie68e081d2011-12-20 02:48:34 +00002304void UnresolvedUsingValueDecl::anchor() { }
2305
John McCalle61f2ba2009-11-18 02:36:19 +00002306UnresolvedUsingValueDecl *
2307UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2308 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002309 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002310 const DeclarationNameInfo &NameInfo,
2311 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002312 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002313 QualifierLoc, NameInfo,
2314 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002315}
2316
Douglas Gregor72172e92012-01-05 21:55:30 +00002317UnresolvedUsingValueDecl *
2318UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002319 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2320 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002321 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002322 DeclarationNameInfo(),
2323 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002324}
2325
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002326SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2327 SourceLocation Begin = isAccessDeclaration()
2328 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2329 return SourceRange(Begin, getNameInfo().getEndLoc());
2330}
2331
David Blaikie68e081d2011-12-20 02:48:34 +00002332void UnresolvedUsingTypenameDecl::anchor() { }
2333
John McCalle61f2ba2009-11-18 02:36:19 +00002334UnresolvedUsingTypenameDecl *
2335UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2336 SourceLocation UsingLoc,
2337 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002338 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002339 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002340 DeclarationName TargetName,
2341 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002342 return new (C, DC) UnresolvedUsingTypenameDecl(
2343 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002344 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002345}
2346
Douglas Gregor72172e92012-01-05 21:55:30 +00002347UnresolvedUsingTypenameDecl *
2348UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002349 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002350 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002351 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002352}
2353
David Blaikie68e081d2011-12-20 02:48:34 +00002354void StaticAssertDecl::anchor() { }
2355
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002356StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002357 SourceLocation StaticAssertLoc,
2358 Expr *AssertExpr,
2359 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002360 SourceLocation RParenLoc,
2361 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002362 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2363 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002364}
2365
Richard Smithf7981722013-11-22 09:01:48 +00002366StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002367 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002368 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2369 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002370}
2371
Richard Smithbdb84f32016-07-22 23:36:59 +00002372void BindingDecl::anchor() {}
2373
2374BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2375 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002376 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002377}
2378
2379BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002380 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002381}
2382
Richard Smith97fcf4b2016-08-14 23:15:52 +00002383VarDecl *BindingDecl::getHoldingVar() const {
2384 Expr *B = getBinding();
2385 if (!B)
2386 return nullptr;
2387 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2388 if (!DRE)
2389 return nullptr;
2390
2391 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2392 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2393 return VD;
2394}
2395
Richard Smithbdb84f32016-07-22 23:36:59 +00002396void DecompositionDecl::anchor() {}
2397
2398DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2399 SourceLocation StartLoc,
2400 SourceLocation LSquareLoc,
2401 QualType T, TypeSourceInfo *TInfo,
2402 StorageClass SC,
2403 ArrayRef<BindingDecl *> Bindings) {
2404 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2405 return new (C, DC, Extra)
2406 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2407}
2408
2409DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2410 unsigned ID,
2411 unsigned NumBindings) {
2412 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002413 auto *Result = new (C, ID, Extra)
2414 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2415 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002416 // Set up and clean out the bindings array.
2417 Result->NumBindings = NumBindings;
2418 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2419 for (unsigned I = 0; I != NumBindings; ++I)
2420 new (Trail + I) BindingDecl*(nullptr);
2421 return Result;
2422}
2423
Richard Smith7873de02016-08-11 22:25:46 +00002424void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2425 os << '[';
2426 bool Comma = false;
2427 for (auto *B : bindings()) {
2428 if (Comma)
2429 os << ", ";
2430 B->printName(os);
2431 Comma = true;
2432 }
2433 os << ']';
2434}
2435
Richard Smithf7981722013-11-22 09:01:48 +00002436MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2437 SourceLocation L, DeclarationName N,
2438 QualType T, TypeSourceInfo *TInfo,
2439 SourceLocation StartL,
2440 IdentifierInfo *Getter,
2441 IdentifierInfo *Setter) {
2442 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2443}
2444
2445MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2446 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002447 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2448 DeclarationName(), QualType(), nullptr,
2449 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002450}
2451
Anders Carlsson6750d162009-03-26 23:46:50 +00002452static const char *getAccessName(AccessSpecifier AS) {
2453 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002454 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002455 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002456 case AS_public:
2457 return "public";
2458 case AS_private:
2459 return "private";
2460 case AS_protected:
2461 return "protected";
2462 }
David Blaikief47fa302012-01-17 02:30:50 +00002463 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002464}
2465
2466const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2467 AccessSpecifier AS) {
2468 return DB << getAccessName(AS);
2469}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002470
2471const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2472 AccessSpecifier AS) {
2473 return DB << getAccessName(AS);
2474}