blob: 07d128ba555bc67995f1ba095a5b5013b1c4e5e2 [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 Trieufd1acbb2017-04-11 21:31:00 +000076 IsParsingBaseSpecifiers(false), HasODRHash(false), ODRHash(0),
77 NumBases(0), NumVBases(0), Bases(), VBases(), Definition(D),
78 FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000079
Benjamin Kramer300c0632012-07-04 17:03:33 +000080CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
81 return Bases.get(Definition->getASTContext().getExternalSource());
82}
83
84CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
85 return VBases.get(Definition->getASTContext().getExternalSource());
86}
87
Richard Smith053f6c62014-05-16 23:01:30 +000088CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
89 DeclContext *DC, SourceLocation StartLoc,
90 SourceLocation IdLoc, IdentifierInfo *Id,
91 CXXRecordDecl *PrevDecl)
92 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
93 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smithb6483992016-05-17 22:44:15 +000094 : nullptr),
Richard Smith053f6c62014-05-16 23:01:30 +000095 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000096
Jay Foad39c79802011-01-12 09:06:06 +000097CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000098 DeclContext *DC, SourceLocation StartLoc,
99 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000100 CXXRecordDecl* PrevDecl,
101 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +0000102 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +0000103 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000104 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000105
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000106 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000107 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000108 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000109 return R;
110}
111
Richard Smith053f6c62014-05-16 23:01:30 +0000112CXXRecordDecl *
113CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
114 TypeSourceInfo *Info, SourceLocation Loc,
115 bool Dependent, bool IsGeneric,
116 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000117 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000118 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
119 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000120 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000121 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000122 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000123 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000124 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000125 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000126 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000127 return R;
128}
129
Douglas Gregor72172e92012-01-05 21:55:30 +0000130CXXRecordDecl *
131CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000132 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000133 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000134 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000135 R->MayHaveOutOfDateDef = false;
136 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000137}
138
Mike Stump11289f42009-09-09 15:08:12 +0000139void
Craig Toppere6337e12015-12-25 00:36:02 +0000140CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
141 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000142 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000143
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000144 if (!data().Bases.isOffset() && data().NumBases > 0)
145 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000146
Craig Toppere6337e12015-12-25 00:36:02 +0000147 if (NumBases) {
Richard Smith872307e2016-03-08 22:17:41 +0000148 if (!C.getLangOpts().CPlusPlus1z) {
149 // C++ [dcl.init.aggr]p1:
150 // An aggregate is [...] a class with [...] no base classes [...].
151 data().Aggregate = false;
152 }
Richard Smithaed32b42011-10-18 20:08:55 +0000153
154 // C++ [class]p4:
155 // A POD-struct is an aggregate class...
156 data().PlainOldData = false;
157 }
158
Anders Carlssonabb20e62010-03-29 05:13:12 +0000159 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000160 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000161
162 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000163 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000164
Craig Toppere6337e12015-12-25 00:36:02 +0000165 data().Bases = new(C) CXXBaseSpecifier [NumBases];
166 data().NumBases = NumBases;
167 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000168 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000169 // Keep track of inherited vbases for this base class.
170 const CXXBaseSpecifier *Base = Bases[i];
171 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000172 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000173 if (BaseType->isDependentType())
174 continue;
175 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000176 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000177
Chandler Carruthb1963742011-04-30 09:17:45 +0000178 if (!BaseClassDecl->isEmpty()) {
179 if (!data().Empty) {
180 // C++0x [class]p7:
181 // A standard-layout class is a class that:
182 // [...]
183 // -- either has no non-static data members in the most derived
184 // class and at most one base class with non-static data members,
185 // or has no base classes with non-static data members, and
186 // If this is the second non-empty base, then neither of these two
187 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000188 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000189 }
190
David Majnemer5cfda6f2016-05-22 05:34:26 +0000191 // C++14 [meta.unary.prop]p4:
192 // T is a class type [...] with [...] no base class B for which
193 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000194 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000195 data().HasNoNonEmptyBases = false;
196 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000197
Richard Smith872307e2016-03-08 22:17:41 +0000198 // C++1z [dcl.init.agg]p1:
199 // An aggregate is a class with [...] no private or protected base classes
200 if (Base->getAccessSpecifier() != AS_public)
201 data().Aggregate = false;
202
Douglas Gregor11c024b2010-09-28 20:50:54 +0000203 // C++ [class.virtual]p1:
204 // A class that declares or inherits a virtual function is called a
205 // polymorphic class.
206 if (BaseClassDecl->isPolymorphic())
207 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000208
Chandler Carruthb1963742011-04-30 09:17:45 +0000209 // C++0x [class]p7:
210 // A standard-layout class is a class that: [...]
211 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000212 if (!BaseClassDecl->isStandardLayout())
213 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000214
Chandler Carruthe71d0622011-04-24 02:49:34 +0000215 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000216 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000217 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000218
Anders Carlssonabb20e62010-03-29 05:13:12 +0000219 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000220 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000221 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000222 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000223 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000224
225 // C++11 [class.copy]p8:
226 // The implicitly-declared copy constructor for a class X will have
227 // the form 'X::X(const X&)' if each [...] virtual base class B of X
228 // has a copy constructor whose first parameter is of type
229 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000230 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000231 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000232 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000233
234 // C++1z [dcl.init.agg]p1:
235 // An aggregate is a class with [...] no virtual base classes
236 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000237 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000238 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000239
240 if (Base->isVirtual()) {
241 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000242 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000243 VBases.push_back(Base);
244
David Majnemer5cfda6f2016-05-22 05:34:26 +0000245 // C++14 [meta.unary.prop] is_empty:
246 // T is a class type, but not a union type, with ... no virtual base
247 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000248 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000249
Richard Smith872307e2016-03-08 22:17:41 +0000250 // C++1z [dcl.init.agg]p1:
251 // An aggregate is a class with [...] no virtual base classes
252 data().Aggregate = false;
253
Richard Smith328aae52012-11-30 05:11:39 +0000254 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
255 // A [default constructor, copy/move constructor, or copy/move assignment
256 // operator for a class X] is trivial [...] if:
257 // -- class X has [...] no virtual base classes
258 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000259
260 // C++0x [class]p7:
261 // A standard-layout class is a class that: [...]
262 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000263 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000264
265 // C++11 [dcl.constexpr]p4:
266 // In the definition of a constexpr constructor [...]
267 // -- the class shall not have any virtual base classes
268 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000269
270 // C++1z [class.copy]p8:
271 // The implicitly-declared copy constructor for a class X will have
272 // the form 'X::X(const X&)' if each potentially constructed subobject
273 // has a copy constructor whose first parameter is of type
274 // 'const B&' or 'const volatile B&' [...]
275 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
276 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000277 } else {
278 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000279 // A default constructor is trivial [...] if:
280 // -- all the direct base classes of its class have trivial default
281 // constructors.
282 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000283 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
284
Chandler Carruthad7d4042011-04-23 23:10:33 +0000285 // C++0x [class.copy]p13:
286 // A copy/move constructor for class X is trivial if [...]
287 // [...]
288 // -- the constructor selected to copy/move each direct base class
289 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000290 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000291 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000292 // If the base class doesn't have a simple move constructor, we'll eagerly
293 // declare it and perform overload resolution to determine which function
294 // it actually calls. If it does have a simple move constructor, this
295 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000296 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000297 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000298
299 // C++0x [class.copy]p27:
300 // A copy/move assignment operator for class X is trivial if [...]
301 // [...]
302 // -- the assignment operator selected to copy/move each direct base
303 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000304 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000305 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000306 // If the base class doesn't have a simple move assignment, we'll eagerly
307 // declare it and perform overload resolution to determine which function
308 // it actually calls. If it does have a simple move assignment, this
309 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000310 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000311 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000312
313 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000314 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000315 // requirements of a constexpr constructor, the implicitly-defined
316 // default constructor is constexpr.
317 if (!BaseClassDecl->hasConstexprDefaultConstructor())
318 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000319
320 // C++1z [class.copy]p8:
321 // The implicitly-declared copy constructor for a class X will have
322 // the form 'X::X(const X&)' if each potentially constructed subobject
323 // has a copy constructor whose first parameter is of type
324 // 'const B&' or 'const volatile B&' [...]
325 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
326 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000327 }
Richard Smith92f241f2012-12-08 02:53:02 +0000328
Douglas Gregor11c024b2010-09-28 20:50:54 +0000329 // C++ [class.ctor]p3:
330 // A destructor is trivial if all the direct base classes of its class
331 // have trivial destructors.
332 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000333 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000334
335 if (!BaseClassDecl->hasIrrelevantDestructor())
336 data().HasIrrelevantDestructor = false;
337
Richard Smith1c33fe82012-11-28 06:23:12 +0000338 // C++11 [class.copy]p18:
339 // The implicitly-declared copy assignment oeprator for a class X will
340 // have the form 'X& X::operator=(const X&)' if each direct base class B
341 // of X has a copy assignment operator whose parameter is of type 'const
342 // B&', 'const volatile B&', or 'B' [...]
343 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
344 data().ImplicitCopyAssignmentHasConstParam = false;
345
John McCall31168b02011-06-15 23:02:42 +0000346 // A class has an Objective-C object member if... or any of its bases
347 // has an Objective-C object member.
348 if (BaseClassDecl->hasObjectMember())
349 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000350
351 if (BaseClassDecl->hasVolatileMember())
352 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000353
Douglas Gregor61226d32011-05-13 01:05:07 +0000354 // Keep track of the presence of mutable fields.
355 if (BaseClassDecl->hasMutableFields())
356 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000357
358 if (BaseClassDecl->hasUninitializedReferenceMember())
359 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000360
Nico Weber6a6376b2016-02-19 01:52:46 +0000361 if (!BaseClassDecl->allowConstDefaultInit())
362 data().HasUninitializedFields = true;
363
Richard Smith6b02d462012-12-08 08:32:28 +0000364 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000365 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000366
David Majnemer5ef4fe72014-06-13 06:43:46 +0000367 if (VBases.empty()) {
368 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000369 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000370 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000371
372 // Create base specifier for any direct or indirect virtual bases.
373 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
374 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000375 for (int I = 0, E = VBases.size(); I != E; ++I) {
376 QualType Type = VBases[I]->getType();
377 if (!Type->isDependentType())
378 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000379 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000380 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000381
382 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000383}
384
Richard Trieufd1acbb2017-04-11 21:31:00 +0000385unsigned CXXRecordDecl::getODRHash() const {
386 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000387
Richard Trieufd1acbb2017-04-11 21:31:00 +0000388 // Previously calculated hash is stored in DefinitionData.
389 if (DefinitionData->HasODRHash)
390 return DefinitionData->ODRHash;
391
392 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000393 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000394 Hash.AddCXXRecordDecl(getDefinition());
395 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000396 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000397
398 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000399}
Richard Trieub6adf542017-02-18 02:09:28 +0000400
Richard Trieufd1acbb2017-04-11 21:31:00 +0000401
Richard Smith6b02d462012-12-08 08:32:28 +0000402void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
403 // C++11 [class.copy]p11:
404 // A defaulted copy/move constructor for a class X is defined as
405 // deleted if X has:
406 // -- a direct or virtual base class B that cannot be copied/moved [...]
407 // -- a non-static data member of class type M (or array thereof)
408 // that cannot be copied or moved [...]
409 if (!Subobj->hasSimpleMoveConstructor())
410 data().NeedOverloadResolutionForMoveConstructor = true;
411
412 // C++11 [class.copy]p23:
413 // A defaulted copy/move assignment operator for a class X is defined as
414 // deleted if X has:
415 // -- a direct or virtual base class B that cannot be copied/moved [...]
416 // -- a non-static data member of class type M (or array thereof)
417 // that cannot be copied or moved [...]
418 if (!Subobj->hasSimpleMoveAssignment())
419 data().NeedOverloadResolutionForMoveAssignment = true;
420
421 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
422 // A defaulted [ctor or dtor] for a class X is defined as
423 // deleted if X has:
424 // -- any direct or virtual base class [...] has a type with a destructor
425 // that is deleted or inaccessible from the defaulted [ctor or dtor].
426 // -- any non-static data member has a type with a destructor
427 // that is deleted or inaccessible from the defaulted [ctor or dtor].
428 if (!Subobj->hasSimpleDestructor()) {
429 data().NeedOverloadResolutionForMoveConstructor = true;
430 data().NeedOverloadResolutionForDestructor = true;
431 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000432}
433
Douglas Gregord2e6a452010-01-14 17:47:39 +0000434bool CXXRecordDecl::hasAnyDependentBases() const {
435 if (!isDependentContext())
436 return false;
437
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000438 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000439}
440
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000441bool CXXRecordDecl::isTriviallyCopyable() const {
442 // C++0x [class]p5:
443 // A trivially copyable class is a class that:
444 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000445 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000446 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000447 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000448 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000449 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000450 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000451 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000452 // -- has a trivial destructor.
453 if (!hasTrivialDestructor()) return false;
454
455 return true;
456}
457
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000458void CXXRecordDecl::markedVirtualFunctionPure() {
459 // C++ [class.abstract]p2:
460 // A class is abstract if it has at least one pure virtual function.
461 data().Abstract = true;
462}
463
464void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000465 if (!D->isImplicit() &&
466 !isa<FieldDecl>(D) &&
467 !isa<IndirectFieldDecl>(D) &&
468 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
469 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
470 data().HasOnlyCMembers = false;
471
472 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000473 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000474 return;
475
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000476 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
477 if (FunTmpl)
478 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000479
480 // FIXME: Pass NamedDecl* to addedMember?
481 Decl *DUnderlying = D;
482 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
483 DUnderlying = ND->getUnderlyingDecl();
484 if (FunctionTemplateDecl *UnderlyingFunTmpl =
485 dyn_cast<FunctionTemplateDecl>(DUnderlying))
486 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
487 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000488
Douglas Gregora832d3e2010-09-28 19:45:33 +0000489 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
490 if (Method->isVirtual()) {
491 // C++ [dcl.init.aggr]p1:
492 // An aggregate is an array or a class with [...] no virtual functions.
493 data().Aggregate = false;
494
495 // C++ [class]p4:
496 // A POD-struct is an aggregate class...
497 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000498
David Majnemer5cfda6f2016-05-22 05:34:26 +0000499 // C++14 [meta.unary.prop]p4:
500 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000501 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000502
503 // C++ [class.virtual]p1:
504 // A class that declares or inherits a virtual function is called a
505 // polymorphic class.
506 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000507
Richard Smith328aae52012-11-30 05:11:39 +0000508 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
509 // A [default constructor, copy/move constructor, or copy/move
510 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000511 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000512 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000513
Chandler Carruthb1963742011-04-30 09:17:45 +0000514 // C++0x [class]p7:
515 // A standard-layout class is a class that: [...]
516 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000517 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000518 }
519 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000520
Richard Smith1c33fe82012-11-28 06:23:12 +0000521 // Notify the listener if an implicit member was added after the definition
522 // was completed.
523 if (!isBeingDefined() && D->isImplicit())
524 if (ASTMutationListener *L = getASTMutationListener())
525 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000526
Richard Smith328aae52012-11-30 05:11:39 +0000527 // The kind of special member this declaration is, if any.
528 unsigned SMKind = 0;
529
Richard Smith1c33fe82012-11-28 06:23:12 +0000530 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000531 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000532 if (!Constructor->isImplicit()) {
533 // Note that we have a user-declared constructor.
534 data().UserDeclaredConstructor = true;
535
536 // C++ [class]p4:
537 // A POD-struct is an aggregate class [...]
538 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
539 // type is technically an aggregate in C++0x since it wouldn't be in 03.
540 data().PlainOldData = false;
541 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000542
Alexis Hunt88c75c32011-05-09 21:45:35 +0000543 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000544 SMKind |= SMF_DefaultConstructor;
545
Richard Smith5179eb72016-06-28 19:03:57 +0000546 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000547 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000548 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000549 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000550 if (Constructor->isDefaulted())
551 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000552 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000553
Chandler Carruthad7d4042011-04-23 23:10:33 +0000554 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000555 unsigned Quals;
556 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000557 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000558
559 if (Quals & Qualifiers::Const)
560 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000561 } else if (Constructor->isMoveConstructor())
562 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000563 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000564
Eric Fiselier283d8d42016-12-03 01:26:47 +0000565 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith505ef812016-12-21 01:57:02 +0000566 // An aggregate is an array or a class with no user-provided, explicit, or
567 // inherited constructors
568 if (Constructor->isUserProvided() || Constructor->isExplicit())
Eric Fiselier283d8d42016-12-03 01:26:47 +0000569 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000570 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000571
Richard Smith5179eb72016-06-28 19:03:57 +0000572 // Handle constructors, including those inherited from base classes.
573 if (CXXConstructorDecl *Constructor =
574 dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000575 // Record if we see any constexpr constructors which are neither copy
576 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000577 // C++1z [basic.types]p10:
578 // [...] has at least one constexpr constructor or constructor template
579 // (possibly inherited from a base class) that is not a copy or move
580 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000581 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000582 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000583 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000584
Richard Smith1c33fe82012-11-28 06:23:12 +0000585 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000586 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000587 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000588
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000589 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000590 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000591 // If the destructor is explicitly defaulted and not trivial or not public
592 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
593 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000594
Richard Smith1c33fe82012-11-28 06:23:12 +0000595 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000596 // A destructor is trivial if [...] the destructor is not virtual.
597 if (DD->isVirtual())
598 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000599 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000600
601 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000602 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000603 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000604 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000605
606 const ReferenceType *ParamTy =
607 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
608 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
609 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000610 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000611
Richard Smith328aae52012-11-30 05:11:39 +0000612 if (Method->isMoveAssignmentOperator())
613 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000614
Douglas Gregor457104e2010-09-29 04:25:11 +0000615 // Keep the list of conversion functions up-to-date.
616 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000617 // FIXME: We use the 'unsafe' accessor for the access specifier here,
618 // because Sema may not have set it yet. That's really just a misdesign
619 // in Sema. However, LLDB *will* have set the access specifier correctly,
620 // and adds declarations after the class is technically completed,
621 // so completeDefinition()'s overriding of the access specifiers doesn't
622 // work.
623 AccessSpecifier AS = Conversion->getAccessUnsafe();
624
Richard Smith328aae52012-11-30 05:11:39 +0000625 if (Conversion->getPrimaryTemplate()) {
626 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000627 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000628 ASTContext &Ctx = getASTContext();
629 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
630 NamedDecl *Primary =
631 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
632 if (Primary->getPreviousDecl())
633 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
634 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000635 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000636 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000637 }
638 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000639
Richard Smith328aae52012-11-30 05:11:39 +0000640 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000641 // If this is the first declaration of a special member, we no longer have
642 // an implicit trivial special member.
643 data().HasTrivialSpecialMembers &=
644 data().DeclaredSpecialMembers | ~SMKind;
645
646 if (!Method->isImplicit() && !Method->isUserProvided()) {
647 // This method is user-declared but not user-provided. We can't work out
648 // whether it's trivial yet (not until we get to the end of the class).
649 // We'll handle this method in finishedDefaultedOrDeletedMember.
650 } else if (Method->isTrivial())
651 data().HasTrivialSpecialMembers |= SMKind;
652 else
653 data().DeclaredNonTrivialSpecialMembers |= SMKind;
654
Richard Smith328aae52012-11-30 05:11:39 +0000655 // Note when we have declared a declared special member, and suppress the
656 // implicit declaration of this special member.
657 data().DeclaredSpecialMembers |= SMKind;
658
659 if (!Method->isImplicit()) {
660 data().UserDeclaredSpecialMembers |= SMKind;
661
662 // C++03 [class]p4:
663 // A POD-struct is an aggregate class that has [...] no user-defined
664 // copy assignment operator and no user-defined destructor.
665 //
666 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
667 // aggregates could not have any constructors, clear it even for an
668 // explicitly defaulted or deleted constructor.
669 // type is technically an aggregate in C++0x since it wouldn't be in 03.
670 //
671 // Also, a user-declared move assignment operator makes a class non-POD.
672 // This is an extension in C++03.
673 data().PlainOldData = false;
674 }
Richard Smith328aae52012-11-30 05:11:39 +0000675 }
676
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000677 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000678 }
Richard Smith328aae52012-11-30 05:11:39 +0000679
Douglas Gregora832d3e2010-09-28 19:45:33 +0000680 // Handle non-static data members.
681 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000682 // C++ [class.bit]p2:
683 // A declaration for a bit-field that omits the identifier declares an
684 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
685 // initialized.
686 if (Field->isUnnamedBitfield())
687 return;
688
Douglas Gregora832d3e2010-09-28 19:45:33 +0000689 // C++ [dcl.init.aggr]p1:
690 // An aggregate is an array or a class (clause 9) with [...] no
691 // private or protected non-static data members (clause 11).
692 //
693 // A POD must be an aggregate.
694 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
695 data().Aggregate = false;
696 data().PlainOldData = false;
697 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000698
699 // C++0x [class]p7:
700 // A standard-layout class is a class that:
701 // [...]
702 // -- has the same access control for all non-static data members,
703 switch (D->getAccess()) {
704 case AS_private: data().HasPrivateFields = true; break;
705 case AS_protected: data().HasProtectedFields = true; break;
706 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000707 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000708 };
709 if ((data().HasPrivateFields + data().HasProtectedFields +
710 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000711 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000712
Douglas Gregor61226d32011-05-13 01:05:07 +0000713 // Keep track of the presence of mutable fields.
714 if (Field->isMutable())
715 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000716
717 // C++11 [class.union]p8, DR1460:
718 // If X is a union, a non-static data member of X that is not an anonymous
719 // union is a variant member of X.
720 if (isUnion() && !Field->isAnonymousStructOrUnion())
721 data().HasVariantMembers = true;
722
Chandler Carruthad7d4042011-04-23 23:10:33 +0000723 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000724 // A POD struct is a class that is both a trivial class and a
725 // standard-layout class, and has no non-static data members of type
726 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000727 //
728 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
729 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000730 ASTContext &Context = getASTContext();
731 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000732 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000733 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000734 // Objective-C Automatic Reference Counting:
735 // If a class has a non-static data member of Objective-C pointer
736 // type (or array thereof), it is a non-POD type and its
737 // default constructor (if any), copy constructor, move constructor,
738 // copy assignment operator, move assignment operator, and destructor are
739 // non-trivial.
740 setHasObjectMember(true);
741 struct DefinitionData &Data = data();
742 Data.PlainOldData = false;
743 Data.HasTrivialSpecialMembers = 0;
744 Data.HasIrrelevantDestructor = false;
Brian Kelley036603a2017-03-29 17:31:42 +0000745 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
746 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000747 }
Eli Friedmana5433322013-07-20 01:06:31 +0000748 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000749 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000750
Chandler Carruthb1963742011-04-30 09:17:45 +0000751 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000752 if (!Field->hasInClassInitializer())
753 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000754
Chandler Carruthb1963742011-04-30 09:17:45 +0000755 // C++0x [class]p7:
756 // A standard-layout class is a class that:
757 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000758 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000759 }
760
Nico Weber6a6376b2016-02-19 01:52:46 +0000761 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +0000762 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000763 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000764 data().HasUninitializedFields = true;
765 } else {
766 data().HasUninitializedFields = true;
767 }
768 }
769
Richard Smith3607ffe2012-02-13 03:54:03 +0000770 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000771 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000772 data().HasNonLiteralTypeFieldsOrBases = true;
773
Richard Smithab44d5b2013-12-10 08:25:00 +0000774 if (Field->hasInClassInitializer() ||
775 (Field->isAnonymousStructOrUnion() &&
776 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000777 data().HasInClassInitializer = true;
778
779 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000780 // A default constructor is trivial if [...] no non-static data member
781 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000782 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000783
Richard Smithe2648ba2012-05-07 01:07:30 +0000784 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000785 // An aggregate is a [...] class with [...] no
786 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000787 //
Richard Smith872307e2016-03-08 22:17:41 +0000788 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000789 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000790 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000791
Richard Smithe2648ba2012-05-07 01:07:30 +0000792 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000793 // A POD struct is [...] a trivial class.
794 data().PlainOldData = false;
795 }
796
Richard Smith6b02d462012-12-08 08:32:28 +0000797 // C++11 [class.copy]p23:
798 // A defaulted copy/move assignment operator for a class X is defined
799 // as deleted if X has:
800 // -- a non-static data member of reference type
801 if (T->isReferenceType())
802 data().DefaultedMoveAssignmentIsDeleted = true;
803
Douglas Gregor11c024b2010-09-28 20:50:54 +0000804 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
805 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
806 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000807 addedClassSubobject(FieldRec);
808
Richard Smithc91d12c2013-11-25 07:07:05 +0000809 // We may need to perform overload resolution to determine whether a
810 // field can be moved if it's const or volatile qualified.
811 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
812 data().NeedOverloadResolutionForMoveConstructor = true;
813 data().NeedOverloadResolutionForMoveAssignment = true;
814 }
815
Richard Smith6b02d462012-12-08 08:32:28 +0000816 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
817 // A defaulted [special member] for a class X is defined as
818 // deleted if:
819 // -- X is a union-like class that has a variant member with a
820 // non-trivial [corresponding special member]
821 if (isUnion()) {
822 if (FieldRec->hasNonTrivialMoveConstructor())
823 data().DefaultedMoveConstructorIsDeleted = true;
824 if (FieldRec->hasNonTrivialMoveAssignment())
825 data().DefaultedMoveAssignmentIsDeleted = true;
826 if (FieldRec->hasNonTrivialDestructor())
827 data().DefaultedDestructorIsDeleted = true;
828 }
829
Richard Smithdd5619f2016-08-16 00:13:47 +0000830 // For an anonymous union member, our overload resolution will perform
831 // overload resolution for its members.
832 if (Field->isAnonymousStructOrUnion()) {
833 data().NeedOverloadResolutionForMoveConstructor |=
834 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
835 data().NeedOverloadResolutionForMoveAssignment |=
836 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
837 data().NeedOverloadResolutionForDestructor |=
838 FieldRec->data().NeedOverloadResolutionForDestructor;
839 }
840
Alexis Huntf479f1b2011-05-09 18:22:59 +0000841 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000842 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000843 // -- for all the non-static data members of its class that are of
844 // class type (or array thereof), each such class has a trivial
845 // default constructor.
846 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000847 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000848
849 // C++0x [class.copy]p13:
850 // A copy/move constructor for class X is trivial if [...]
851 // [...]
852 // -- for each non-static data member of X that is of class type (or
853 // an array thereof), the constructor selected to copy/move that
854 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000855 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000856 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000857 // If the field doesn't have a simple move constructor, we'll eagerly
858 // declare the move constructor for this class and we'll decide whether
859 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000860 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000861 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000862
863 // C++0x [class.copy]p27:
864 // A copy/move assignment operator for class X is trivial if [...]
865 // [...]
866 // -- for each non-static data member of X that is of class type (or
867 // an array thereof), the assignment operator selected to
868 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000869 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000870 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000871 // If the field doesn't have a simple move assignment, we'll eagerly
872 // declare the move assignment for this class and we'll decide whether
873 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000874 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000875 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000876
Douglas Gregor11c024b2010-09-28 20:50:54 +0000877 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000878 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000879 if (!FieldRec->hasIrrelevantDestructor())
880 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000881 if (FieldRec->hasObjectMember())
882 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000883 if (FieldRec->hasVolatileMember())
884 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000885
886 // C++0x [class]p7:
887 // A standard-layout class is a class that:
888 // -- has no non-static data members of type non-standard-layout
889 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000890 if (!FieldRec->isStandardLayout())
891 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000892
893 // C++0x [class]p7:
894 // A standard-layout class is a class that:
895 // [...]
896 // -- has no base classes of the same type as the first non-static
897 // data member.
898 // We don't want to expend bits in the state of the record decl
899 // tracking whether this is the first non-static data member so we
900 // cheat a bit and use some of the existing state: the empty bit.
901 // Virtual bases and virtual methods make a class non-empty, but they
902 // also make it non-standard-layout so we needn't check here.
903 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000904 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000905 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000906 // data member must come through here with Empty still true, and Empty
907 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000908 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000909 for (const auto &BI : bases()) {
910 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000911 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000912 break;
913 }
914 }
915 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000916
917 // Keep track of the presence of mutable fields.
918 if (FieldRec->hasMutableFields())
919 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000920
921 // C++11 [class.copy]p13:
922 // If the implicitly-defined constructor would satisfy the
923 // requirements of a constexpr constructor, the implicitly-defined
924 // constructor is constexpr.
925 // C++11 [dcl.constexpr]p4:
926 // -- every constructor involved in initializing non-static data
927 // members [...] shall be a constexpr constructor
928 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000929 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000930 // The standard requires any in-class initializer to be a constant
931 // expression. We consider this to be a defect.
932 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000933
934 // C++11 [class.copy]p8:
935 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +0000936 // the form 'X::X(const X&)' if each potentially constructed subobject
937 // of a class type M (or array thereof) has a copy constructor whose
938 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +0000939 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000940 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000941
942 // C++11 [class.copy]p18:
943 // The implicitly-declared copy assignment oeprator for a class X will
944 // have the form 'X& X::operator=(const X&)' if [...] for all the
945 // non-static data members of X that are of a class type M (or array
946 // thereof), each such class type has a copy assignment operator whose
947 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
948 if (!FieldRec->hasCopyAssignmentWithConstParam())
949 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000950
951 if (FieldRec->hasUninitializedReferenceMember() &&
952 !Field->hasInClassInitializer())
953 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000954
955 // C++11 [class.union]p8, DR1460:
956 // a non-static data member of an anonymous union that is a member of
957 // X is also a variant member of X.
958 if (FieldRec->hasVariantMembers() &&
959 Field->isAnonymousStructOrUnion())
960 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000961 }
Richard Smithcc36f692011-12-22 02:22:31 +0000962 } else {
963 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000964 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000965 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000966 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000967
968 // C++11 [class.copy]p23:
969 // A defaulted copy/move assignment operator for a class X is defined
970 // as deleted if X has:
971 // -- a non-static data member of const non-class type (or array
972 // thereof)
973 if (T.isConstQualified())
974 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000975 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000976
977 // C++0x [class]p7:
978 // A standard-layout class is a class that:
979 // [...]
980 // -- either has no non-static data members in the most derived
981 // class and at most one base class with non-static data members,
982 // or has no base classes with non-static data members, and
983 // At this point we know that we have a non-static data member, so the last
984 // clause holds.
985 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000986 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000987
David Majnemer5cfda6f2016-05-22 05:34:26 +0000988 // C++14 [meta.unary.prop]p4:
989 // T is a class type [...] with [...] no non-static data members other
990 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000991 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000992 if (!Field->isBitField() ||
993 (!Field->getBitWidth()->isTypeDependent() &&
994 !Field->getBitWidth()->isValueDependent() &&
995 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000996 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000997 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000998 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000999
1000 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001001 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001002 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001003 == DeclarationName::CXXConversionFunctionName) {
1004 ASTContext &Ctx = getASTContext();
1005 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1006 }
1007 }
Richard Smith12e79312016-05-13 06:47:56 +00001008
1009 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
1010 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001011 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001012 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001013 // C++1z [dcl.init.aggr]p1:
1014 // An aggregate is [...] a class [...] with no inherited constructors
1015 data().Aggregate = false;
1016 }
Richard Smith12e79312016-05-13 06:47:56 +00001017
1018 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1019 data().HasInheritedAssignment = true;
1020 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001021}
1022
Richard Smith92f241f2012-12-08 02:53:02 +00001023void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1024 assert(!D->isImplicit() && !D->isUserProvided());
1025
1026 // The kind of special member this declaration is, if any.
1027 unsigned SMKind = 0;
1028
1029 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1030 if (Constructor->isDefaultConstructor()) {
1031 SMKind |= SMF_DefaultConstructor;
1032 if (Constructor->isConstexpr())
1033 data().HasConstexprDefaultConstructor = true;
1034 }
1035 if (Constructor->isCopyConstructor())
1036 SMKind |= SMF_CopyConstructor;
1037 else if (Constructor->isMoveConstructor())
1038 SMKind |= SMF_MoveConstructor;
1039 else if (Constructor->isConstexpr())
1040 // We may now know that the constructor is constexpr.
1041 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001042 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001043 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001044 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1045 data().HasIrrelevantDestructor = false;
1046 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001047 SMKind |= SMF_CopyAssignment;
1048 else if (D->isMoveAssignmentOperator())
1049 SMKind |= SMF_MoveAssignment;
1050
1051 // Update which trivial / non-trivial special members we have.
1052 // addedMember will have skipped this step for this member.
1053 if (D->isTrivial())
1054 data().HasTrivialSpecialMembers |= SMKind;
1055 else
1056 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1057}
1058
Joao Matose9a3ed42012-08-31 22:18:20 +00001059bool CXXRecordDecl::isCLike() const {
1060 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1061 !TemplateOrInstantiation.isNull())
1062 return false;
1063 if (!hasDefinition())
1064 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001065
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001066 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001067}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001068
Faisal Vali2b391ab2013-09-26 19:54:12 +00001069bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001070 if (!isLambda()) return false;
1071 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001072}
1073
1074CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001075 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001076 DeclarationName Name =
1077 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001078 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001079
1080 assert(!Calls.empty() && "Missing lambda call operator!");
1081 assert(Calls.size() == 1 && "More than one lambda call operator!");
1082
1083 NamedDecl *CallOp = Calls.front();
1084 if (FunctionTemplateDecl *CallOpTmpl =
1085 dyn_cast<FunctionTemplateDecl>(CallOp))
1086 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001087
1088 return cast<CXXMethodDecl>(CallOp);
1089}
1090
1091CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001092 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001093 DeclarationName Name =
1094 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001095 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001096 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001097 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1098 NamedDecl *InvokerFun = Invoker.front();
1099 if (FunctionTemplateDecl *InvokerTemplate =
1100 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1101 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1102
Faisal Vali571df122013-09-29 08:45:24 +00001103 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001104}
1105
Douglas Gregor9c702202012-02-10 07:45:31 +00001106void CXXRecordDecl::getCaptureFields(
1107 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001108 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001109 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001110 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001111
Douglas Gregorc8a73492012-02-13 15:44:47 +00001112 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001113 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001114 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001115 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001116 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001117 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001118 else if (C->capturesVariable())
1119 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001120 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001121 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001122}
1123
Faisal Vali2b391ab2013-09-26 19:54:12 +00001124TemplateParameterList *
1125CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001126 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001127 CXXMethodDecl *CallOp = getLambdaCallOperator();
1128 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1129 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001130 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001131}
Douglas Gregor9c702202012-02-10 07:45:31 +00001132
Richard Smith0bae6242016-08-25 00:34:00 +00001133Decl *CXXRecordDecl::getLambdaContextDecl() const {
1134 assert(isLambda() && "Not a lambda closure type!");
1135 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1136 return getLambdaData().ContextDecl.get(Source);
1137}
1138
John McCall1e3a1a72010-03-15 09:07:48 +00001139static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001140 QualType T =
1141 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1142 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001143 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001144}
1145
John McCall1e3a1a72010-03-15 09:07:48 +00001146/// Collect the visible conversions of a base class.
1147///
James Dennettb5d5f812012-06-15 22:28:09 +00001148/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001149/// \param InVirtual whether this base class is a virtual base (or a base
1150/// of a virtual base)
1151/// \param Access the access along the inheritance path to this base
1152/// \param ParentHiddenTypes the conversions provided by the inheritors
1153/// of this base
1154/// \param Output the set to which to add conversions from non-virtual bases
1155/// \param VOutput the set to which to add conversions from virtual bases
1156/// \param HiddenVBaseCs the set of conversions which were hidden in a
1157/// virtual base along some inheritance path
1158static void CollectVisibleConversions(ASTContext &Context,
1159 CXXRecordDecl *Record,
1160 bool InVirtual,
1161 AccessSpecifier Access,
1162 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001163 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001164 UnresolvedSetImpl &VOutput,
1165 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1166 // The set of types which have conversions in this class or its
1167 // subclasses. As an optimization, we don't copy the derived set
1168 // unless it might change.
1169 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1170 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1171
1172 // Collect the direct conversions and figure out which conversions
1173 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001174 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1175 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1176 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001177 HiddenTypesBuffer = ParentHiddenTypes;
1178 HiddenTypes = &HiddenTypesBuffer;
1179
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001180 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001181 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1182 bool Hidden = ParentHiddenTypes.count(ConvType);
1183 if (!Hidden)
1184 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001185
1186 // If this conversion is hidden and we're in a virtual base,
1187 // remember that it's hidden along some inheritance path.
1188 if (Hidden && InVirtual)
1189 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1190
1191 // If this conversion isn't hidden, add it to the appropriate output.
1192 else if (!Hidden) {
1193 AccessSpecifier IAccess
1194 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1195
1196 if (InVirtual)
1197 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001198 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001199 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001200 }
1201 }
1202 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001203
John McCall1e3a1a72010-03-15 09:07:48 +00001204 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001205 for (const auto &I : Record->bases()) {
1206 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001207 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001208
John McCall1e3a1a72010-03-15 09:07:48 +00001209 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001210 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1211 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001212
John McCall1e3a1a72010-03-15 09:07:48 +00001213 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1214 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1215 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001216 }
John McCall1e3a1a72010-03-15 09:07:48 +00001217}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001218
John McCall1e3a1a72010-03-15 09:07:48 +00001219/// Collect the visible conversions of a class.
1220///
1221/// This would be extremely straightforward if it weren't for virtual
1222/// bases. It might be worth special-casing that, really.
1223static void CollectVisibleConversions(ASTContext &Context,
1224 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001225 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001226 // The collection of all conversions in virtual bases that we've
1227 // found. These will be added to the output as long as they don't
1228 // appear in the hidden-conversions set.
1229 UnresolvedSet<8> VBaseCs;
1230
1231 // The set of conversions in virtual bases that we've determined to
1232 // be hidden.
1233 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1234
1235 // The set of types hidden by classes derived from this one.
1236 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1237
1238 // Go ahead and collect the direct conversions and add them to the
1239 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001240 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1241 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001242 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001243 for (; ConvI != ConvE; ++ConvI)
1244 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001245
1246 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001247 for (const auto &I : Record->bases()) {
1248 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001249 if (!RT) continue;
1250
1251 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001252 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001253 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1254 }
1255
1256 // Add any unhidden conversions provided by virtual bases.
1257 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1258 I != E; ++I) {
1259 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001260 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001261 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001262}
1263
1264/// getVisibleConversionFunctions - get all conversion functions visible
1265/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001266llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001267CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001268 ASTContext &Ctx = getASTContext();
1269
1270 ASTUnresolvedSet *Set;
1271 if (bases_begin() == bases_end()) {
1272 // If root class, all conversions are visible.
1273 Set = &data().Conversions.get(Ctx);
1274 } else {
1275 Set = &data().VisibleConversions.get(Ctx);
1276 // If visible conversion list is not evaluated, evaluate it.
1277 if (!data().ComputedVisibleConversions) {
1278 CollectVisibleConversions(Ctx, this, *Set);
1279 data().ComputedVisibleConversions = true;
1280 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001281 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001282 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001283}
1284
John McCallda4458e2010-03-31 01:36:47 +00001285void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1286 // This operation is O(N) but extremely rare. Sema only uses it to
1287 // remove UsingShadowDecls in a class that were followed by a direct
1288 // declaration, e.g.:
1289 // class A : B {
1290 // using B::operator int;
1291 // operator int();
1292 // };
1293 // This is uncommon by itself and even more uncommon in conjunction
1294 // with sufficiently large numbers of directly-declared conversions
1295 // that asymptotic behavior matters.
1296
Richard Smitha4ba74c2013-08-30 04:46:40 +00001297 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001298 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1299 if (Convs[I].getDecl() == ConvDecl) {
1300 Convs.erase(I);
1301 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1302 && "conversion was found multiple times in unresolved set");
1303 return;
1304 }
1305 }
1306
1307 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001308}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001309
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001310CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001311 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001312 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001313
1314 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001315}
1316
Chandler Carruth21c90602015-12-30 03:24:14 +00001317MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1318 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1319}
1320
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001321void
1322CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1323 TemplateSpecializationKind TSK) {
1324 assert(TemplateOrInstantiation.isNull() &&
1325 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001326 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001327 TemplateOrInstantiation
1328 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1329}
1330
Chandler Carruth21c90602015-12-30 03:24:14 +00001331ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1332 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1333}
1334
1335void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1336 TemplateOrInstantiation = Template;
1337}
1338
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001339TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1340 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001341 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1342 return Spec->getSpecializationKind();
1343
Douglas Gregor06db9f52009-10-12 20:18:28 +00001344 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001345 return MSInfo->getTemplateSpecializationKind();
1346
1347 return TSK_Undeclared;
1348}
1349
1350void
1351CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1352 if (ClassTemplateSpecializationDecl *Spec
1353 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1354 Spec->setSpecializationKind(TSK);
1355 return;
1356 }
1357
Douglas Gregor06db9f52009-10-12 20:18:28 +00001358 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001359 MSInfo->setTemplateSpecializationKind(TSK);
1360 return;
1361 }
1362
David Blaikie83d382b2011-09-23 05:06:16 +00001363 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001364}
1365
Reid Klecknere7367d62014-10-14 20:28:40 +00001366const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001367 auto GetDefinitionOrSelf =
1368 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1369 if (auto *Def = D->getDefinition())
1370 return Def;
1371 return D;
1372 };
1373
Reid Klecknere7367d62014-10-14 20:28:40 +00001374 // If it's a class template specialization, find the template or partial
1375 // specialization from which it was instantiated.
1376 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1377 auto From = TD->getInstantiatedFrom();
1378 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1379 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1380 if (NewCTD->isMemberSpecialization())
1381 break;
1382 CTD = NewCTD;
1383 }
Richard Smith2195ec92017-04-21 01:15:13 +00001384 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001385 }
1386 if (auto *CTPSD =
1387 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1388 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1389 if (NewCTPSD->isMemberSpecialization())
1390 break;
1391 CTPSD = NewCTPSD;
1392 }
Richard Smith2195ec92017-04-21 01:15:13 +00001393 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001394 }
1395 }
1396
1397 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1398 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1399 const CXXRecordDecl *RD = this;
1400 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1401 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001402 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001403 }
1404 }
1405
1406 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1407 "couldn't find pattern for class template instantiation");
1408 return nullptr;
1409}
1410
Douglas Gregorbac74902010-07-01 14:13:13 +00001411CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1412 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001413 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001414
1415 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001416 = Context.DeclarationNames.getCXXDestructorName(
1417 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001418
Richard Smithcf4bdde2015-02-21 02:45:19 +00001419 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001420
Hubert Tong3cede512017-06-30 22:43:54 +00001421 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001422}
1423
Richard Trieu95a192a2015-05-28 00:14:02 +00001424bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1425 // Destructor is noreturn.
1426 if (const CXXDestructorDecl *Destructor = getDestructor())
1427 if (Destructor->isNoReturn())
1428 return true;
1429
1430 // Check base classes destructor for noreturn.
1431 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001432 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1433 if (RD->isAnyDestructorNoReturn())
1434 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001435
1436 // Check fields for noreturn.
1437 for (const auto *Field : fields())
1438 if (const CXXRecordDecl *RD =
1439 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1440 if (RD->isAnyDestructorNoReturn())
1441 return true;
1442
1443 // All destructors are not noreturn.
1444 return false;
1445}
1446
Douglas Gregorb11aad82011-02-19 18:51:44 +00001447void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001448 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001449}
1450
1451void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1452 RecordDecl::completeDefinition();
1453
Douglas Gregor8fb95122010-09-29 00:15:42 +00001454 // If the class may be abstract (but hasn't been marked as such), check for
1455 // any pure final overriders.
1456 if (mayBeAbstract()) {
1457 CXXFinalOverriderMap MyFinalOverriders;
1458 if (!FinalOverriders) {
1459 getFinalOverriders(MyFinalOverriders);
1460 FinalOverriders = &MyFinalOverriders;
1461 }
1462
1463 bool Done = false;
1464 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1465 MEnd = FinalOverriders->end();
1466 M != MEnd && !Done; ++M) {
1467 for (OverridingMethods::iterator SO = M->second.begin(),
1468 SOEnd = M->second.end();
1469 SO != SOEnd && !Done; ++SO) {
1470 assert(SO->second.size() > 0 &&
1471 "All virtual functions have overridding virtual functions");
1472
1473 // C++ [class.abstract]p4:
1474 // A class is abstract if it contains or inherits at least one
1475 // pure virtual function for which the final overrider is pure
1476 // virtual.
1477 if (SO->second.front().Method->isPure()) {
1478 data().Abstract = true;
1479 Done = true;
1480 break;
1481 }
1482 }
1483 }
1484 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001485
1486 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001487 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001488 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001489 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001490}
1491
1492bool CXXRecordDecl::mayBeAbstract() const {
1493 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1494 isDependentContext())
1495 return false;
1496
Aaron Ballman574705e2014-03-13 15:41:46 +00001497 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001498 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001499 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001500 if (BaseDecl->isAbstract())
1501 return true;
1502 }
1503
1504 return false;
1505}
1506
Richard Smithbc491202017-02-17 20:05:37 +00001507void CXXDeductionGuideDecl::anchor() { }
1508
1509CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1510 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1511 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1512 SourceLocation EndLocation) {
1513 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1514 NameInfo, T, TInfo, EndLocation);
1515}
1516
1517CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1518 unsigned ID) {
1519 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1520 DeclarationNameInfo(), QualType(),
1521 nullptr, SourceLocation());
1522}
1523
David Blaikie68e081d2011-12-20 02:48:34 +00001524void CXXMethodDecl::anchor() { }
1525
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001526bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001527 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001528
1529 if (MD->getStorageClass() == SC_Static)
1530 return true;
1531
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001532 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1533 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001534}
1535
Rafael Espindola49e860b2012-06-26 17:45:31 +00001536static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1537 const CXXMethodDecl *BaseMD) {
1538 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1539 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1540 const CXXMethodDecl *MD = *I;
1541 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1542 return true;
1543 if (recursivelyOverrides(MD, BaseMD))
1544 return true;
1545 }
1546 return false;
1547}
1548
1549CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001550CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1551 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001552 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1553 return this;
1554
1555 // Lookup doesn't work for destructors, so handle them separately.
1556 if (isa<CXXDestructorDecl>(this)) {
1557 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001558 if (MD) {
1559 if (recursivelyOverrides(MD, this))
1560 return MD;
1561 if (MayBeBase && recursivelyOverrides(this, MD))
1562 return MD;
1563 }
Craig Topper36250ad2014-05-12 05:36:57 +00001564 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001565 }
1566
Richard Smith40c78062015-02-21 02:31:57 +00001567 for (auto *ND : RD->lookup(getDeclName())) {
1568 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001569 if (!MD)
1570 continue;
1571 if (recursivelyOverrides(MD, this))
1572 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001573 if (MayBeBase && recursivelyOverrides(this, MD))
1574 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001575 }
1576
Aaron Ballman574705e2014-03-13 15:41:46 +00001577 for (const auto &I : RD->bases()) {
1578 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001579 if (!RT)
1580 continue;
1581 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1582 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1583 if (T)
1584 return T;
1585 }
1586
Craig Topper36250ad2014-05-12 05:36:57 +00001587 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001588}
1589
Ted Kremenek21475702008-09-05 17:16:31 +00001590CXXMethodDecl *
1591CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001592 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001593 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001594 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001595 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001596 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001597 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1598 T, TInfo, SC, isInline, isConstexpr,
1599 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001600}
1601
Douglas Gregor72172e92012-01-05 21:55:30 +00001602CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001603 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001604 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001605 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001606}
1607
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001608bool CXXMethodDecl::isUsualDeallocationFunction() const {
1609 if (getOverloadedOperator() != OO_Delete &&
1610 getOverloadedOperator() != OO_Array_Delete)
1611 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001612
1613 // C++ [basic.stc.dynamic.deallocation]p2:
1614 // A template instance is never a usual deallocation function,
1615 // regardless of its signature.
1616 if (getPrimaryTemplate())
1617 return false;
1618
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001619 // C++ [basic.stc.dynamic.deallocation]p2:
1620 // If a class T has a member deallocation function named operator delete
1621 // with exactly one parameter, then that function is a usual (non-placement)
1622 // deallocation function. [...]
1623 if (getNumParams() == 1)
1624 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00001625 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00001626
Richard Smithb2f0f052016-10-10 18:54:32 +00001627 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001628 // [...] If class T does not declare such an operator delete but does
1629 // declare a member deallocation function named operator delete with
1630 // exactly two parameters, the second of which has type std::size_t (18.1),
1631 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00001632 //
1633 // C++17 says a usual deallocation function is one with the signature
1634 // (void* [, size_t] [, std::align_val_t] [, ...])
1635 // and all such functions are usual deallocation functions. It's not clear
1636 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001637 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00001638 if (UsualParams < getNumParams() &&
1639 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
1640 Context.getSizeType()))
1641 ++UsualParams;
1642
1643 if (UsualParams < getNumParams() &&
1644 getParamDecl(UsualParams)->getType()->isAlignValT())
1645 ++UsualParams;
1646
1647 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001648 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00001649
1650 // In C++17 onwards, all potential usual deallocation functions are actual
1651 // usual deallocation functions.
1652 if (Context.getLangOpts().AlignedAllocation)
1653 return true;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001654
1655 // This function is a usual deallocation function if there are no
1656 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001657 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1658 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001659 I != E; ++I) {
1660 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001661 if (FD->getNumParams() == 1)
1662 return false;
1663 }
1664
1665 return true;
1666}
1667
Douglas Gregorb139cd52010-05-01 20:49:11 +00001668bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001669 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001670 // A user-declared copy assignment operator X::operator= is a non-static
1671 // non-template member function of class X with exactly one parameter of
1672 // type X, X&, const X&, volatile X& or const volatile X&.
1673 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1674 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001675 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1676 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001677 return false;
1678
1679 QualType ParamType = getParamDecl(0)->getType();
1680 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1681 ParamType = Ref->getPointeeType();
1682
1683 ASTContext &Context = getASTContext();
1684 QualType ClassType
1685 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1686 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1687}
1688
Alexis Huntfcaeae42011-05-25 20:50:04 +00001689bool CXXMethodDecl::isMoveAssignmentOperator() const {
1690 // C++0x [class.copy]p19:
1691 // A user-declared move assignment operator X::operator= is a non-static
1692 // non-template member function of class X with exactly one parameter of type
1693 // X&&, const X&&, volatile X&&, or const volatile X&&.
1694 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001695 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1696 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001697 return false;
1698
1699 QualType ParamType = getParamDecl(0)->getType();
1700 if (!isa<RValueReferenceType>(ParamType))
1701 return false;
1702 ParamType = ParamType->getPointeeType();
1703
1704 ASTContext &Context = getASTContext();
1705 QualType ClassType
1706 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1707 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1708}
1709
Anders Carlsson36d87e12009-05-16 23:58:37 +00001710void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001711 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001712 assert(!MD->getParent()->isDependentContext() &&
1713 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001714 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001715
Douglas Gregor832940b2010-03-02 23:58:15 +00001716 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001717}
1718
1719CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001720 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001721 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001722}
1723
1724CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001725 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001726 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001727}
1728
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001729unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001730 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001731 return getASTContext().overridden_methods_size(this);
1732}
1733
Clement Courbet6ecaec82016-07-05 07:49:31 +00001734CXXMethodDecl::overridden_method_range
1735CXXMethodDecl::overridden_methods() const {
1736 if (isa<CXXConstructorDecl>(this))
1737 return overridden_method_range(nullptr, nullptr);
1738 return getASTContext().overridden_methods(this);
1739}
1740
Ted Kremenek21475702008-09-05 17:16:31 +00001741QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001742 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1743 // If the member function is declared const, the type of this is const X*,
1744 // if the member function is declared volatile, the type of this is
1745 // volatile X*, and if the member function is declared const volatile,
1746 // the type of this is const volatile X*.
1747
Ted Kremenek21475702008-09-05 17:16:31 +00001748 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001749
John McCalle78aac42010-03-10 03:28:59 +00001750 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001751 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00001752 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001753 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001754}
1755
Eli Friedman71a26d82009-12-06 20:50:05 +00001756bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001757 // If this function is a template instantiation, look at the template from
1758 // which it was instantiated.
1759 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1760 if (!CheckFn)
1761 CheckFn = this;
1762
Eli Friedman71a26d82009-12-06 20:50:05 +00001763 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001764 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001765}
1766
Douglas Gregor355efbb2012-02-17 03:02:34 +00001767bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001768 const CXXRecordDecl *P = getParent();
1769 if (P->isLambda()) {
1770 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1771 if (StaticInvoker == this) return true;
1772 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1773 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1774 }
1775 }
1776 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001777}
1778
Alexis Hunt1d792652011-01-08 20:30:50 +00001779CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1780 TypeSourceInfo *TInfo, bool IsVirtual,
1781 SourceLocation L, Expr *Init,
1782 SourceLocation R,
1783 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001784 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001785 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
Richard Smith30e304e2016-12-14 00:03:17 +00001786 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001787{
Douglas Gregore8381c02008-11-05 04:29:56 +00001788}
1789
Alexis Hunt1d792652011-01-08 20:30:50 +00001790CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1791 FieldDecl *Member,
1792 SourceLocation MemberLoc,
1793 SourceLocation L, Expr *Init,
1794 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001795 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001796 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001797 IsWritten(false), SourceOrder(0)
Francois Pichetd583da02010-12-04 09:14:42 +00001798{
1799}
1800
Alexis Hunt1d792652011-01-08 20:30:50 +00001801CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1802 IndirectFieldDecl *Member,
1803 SourceLocation MemberLoc,
1804 SourceLocation L, Expr *Init,
1805 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001806 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001807 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001808 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001809{
Douglas Gregore8381c02008-11-05 04:29:56 +00001810}
1811
Alexis Hunt1d792652011-01-08 20:30:50 +00001812CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001813 TypeSourceInfo *TInfo,
1814 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001815 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001816 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1817 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001818 IsWritten(false), SourceOrder(0)
Alexis Huntc5575cc2011-02-26 19:13:13 +00001819{
1820}
1821
Alexis Hunt1d792652011-01-08 20:30:50 +00001822TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001823 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001824 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001825 else
1826 return TypeLoc();
1827}
1828
Alexis Hunt1d792652011-01-08 20:30:50 +00001829const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001830 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001831 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001832 else
Craig Topper36250ad2014-05-12 05:36:57 +00001833 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001834}
1835
Alexis Hunt1d792652011-01-08 20:30:50 +00001836SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001837 if (isInClassMemberInitializer())
1838 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001839
David Blaikiea81d4102015-01-18 00:12:58 +00001840 if (isAnyMemberInitializer())
1841 return getMemberLocation();
1842
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001843 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1844 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1845
1846 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001847}
1848
Alexis Hunt1d792652011-01-08 20:30:50 +00001849SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001850 if (isInClassMemberInitializer()) {
1851 FieldDecl *D = getAnyMember();
1852 if (Expr *I = D->getInClassInitializer())
1853 return I->getSourceRange();
1854 return SourceRange();
1855 }
1856
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001857 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001858}
1859
David Blaikie68e081d2011-12-20 02:48:34 +00001860void CXXConstructorDecl::anchor() { }
1861
Richard Smith5179eb72016-06-28 19:03:57 +00001862CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
1863 unsigned ID,
1864 bool Inherited) {
1865 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
1866 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
1867 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
1868 false, false, false, false, InheritedConstructor());
1869 Result->IsInheritingConstructor = Inherited;
1870 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00001871}
1872
1873CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001874CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001875 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001876 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001877 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001878 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00001879 bool isImplicitlyDeclared, bool isConstexpr,
1880 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001881 assert(NameInfo.getName().getNameKind()
1882 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001883 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00001884 unsigned Extra =
1885 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
1886 return new (C, RD, Extra) CXXConstructorDecl(
1887 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
1888 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00001889}
1890
Richard Smithc2bb8182015-03-24 06:36:48 +00001891CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1892 return CtorInitializers.get(getASTContext().getExternalSource());
1893}
1894
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001895CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1896 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1897 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1898 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1899 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001900
1901 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001902}
1903
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001904bool CXXConstructorDecl::isDefaultConstructor() const {
1905 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001906 // A default constructor for a class X is a constructor of class
1907 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001908 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001909 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001910}
1911
Mike Stump11289f42009-09-09 15:08:12 +00001912bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001913CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001914 return isCopyOrMoveConstructor(TypeQuals) &&
1915 getParamDecl(0)->getType()->isLValueReferenceType();
1916}
1917
1918bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1919 return isCopyOrMoveConstructor(TypeQuals) &&
1920 getParamDecl(0)->getType()->isRValueReferenceType();
1921}
1922
1923/// \brief Determine whether this is a copy or move constructor.
1924bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001925 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001926 // A non-template constructor for class X is a copy constructor
1927 // if its first parameter is of type X&, const X&, volatile X& or
1928 // const volatile X&, and either there are no other parameters
1929 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001930 // C++0x [class.copy]p3:
1931 // A non-template constructor for class X is a move constructor if its
1932 // first parameter is of type X&&, const X&&, volatile X&&, or
1933 // const volatile X&&, and either there are no other parameters or else
1934 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001935 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001936 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001937 (getPrimaryTemplate() != nullptr) ||
1938 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001939 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001940
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001941 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001942
1943 // Do we have a reference type?
1944 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001945 if (!ParamRefType)
1946 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001947
Douglas Gregorff7028a2009-11-13 23:59:09 +00001948 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001949 ASTContext &Context = getASTContext();
1950
Douglas Gregorff7028a2009-11-13 23:59:09 +00001951 CanQualType PointeeType
1952 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001953 CanQualType ClassTy
1954 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001955 if (PointeeType.getUnqualifiedType() != ClassTy)
1956 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001957
John McCall8ccfcb52009-09-24 19:53:00 +00001958 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001959
1960 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001961 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001962 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001963}
1964
Anders Carlssond20e7952009-08-28 16:57:08 +00001965bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001966 // C++ [class.conv.ctor]p1:
1967 // A constructor declared without the function-specifier explicit
1968 // that can be called with a single parameter specifies a
1969 // conversion from the type of its first parameter to the type of
1970 // its class. Such a constructor is called a converting
1971 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001972 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001973 return false;
1974
Mike Stump11289f42009-09-09 15:08:12 +00001975 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001976 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001977 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001978 (getNumParams() > 1 &&
1979 (getParamDecl(1)->hasDefaultArg() ||
1980 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001981}
Douglas Gregor61956c42008-10-31 09:07:45 +00001982
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001983bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001984 if ((getNumParams() < 1) ||
1985 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001986 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001987 return false;
1988
1989 const ParmVarDecl *Param = getParamDecl(0);
1990
1991 ASTContext &Context = getASTContext();
1992 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1993
Douglas Gregorffe14e32009-11-14 01:20:54 +00001994 // Is it the same as our our class type?
1995 CanQualType ClassTy
1996 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1997 if (ParamType.getUnqualifiedType() != ClassTy)
1998 return false;
1999
2000 return true;
2001}
2002
David Blaikie68e081d2011-12-20 02:48:34 +00002003void CXXDestructorDecl::anchor() { }
2004
Douglas Gregor831c93f2008-11-05 20:51:48 +00002005CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002006CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002007 return new (C, ID)
2008 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2009 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00002010}
2011
2012CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002013CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002014 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002015 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002016 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002017 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002018 assert(NameInfo.getName().getNameKind()
2019 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002020 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002021 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002022 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002023}
2024
Richard Smithf8134002015-03-10 01:41:22 +00002025void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
2026 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2027 if (OD && !First->OperatorDelete) {
2028 First->OperatorDelete = OD;
2029 if (auto *L = getASTMutationListener())
2030 L->ResolvedOperatorDelete(First, OD);
2031 }
2032}
2033
David Blaikie68e081d2011-12-20 02:48:34 +00002034void CXXConversionDecl::anchor() { }
2035
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002036CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002037CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002038 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002039 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002040 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002041 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002042}
2043
2044CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002045CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002046 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002047 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002048 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002049 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002050 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002051 assert(NameInfo.getName().getNameKind()
2052 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002053 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002054 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002055 isInline, isExplicit, isConstexpr,
2056 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002057}
2058
Douglas Gregord3b672c2012-02-16 01:06:16 +00002059bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2060 return isImplicit() && getParent()->isLambda() &&
2061 getConversionType()->isBlockPointerType();
2062}
2063
David Blaikie68e081d2011-12-20 02:48:34 +00002064void LinkageSpecDecl::anchor() { }
2065
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002066LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002067 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002068 SourceLocation ExternLoc,
2069 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002070 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002071 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002072 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002073}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002074
Richard Smithf7981722013-11-22 09:01:48 +00002075LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2076 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002077 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2078 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002079}
2080
David Blaikie68e081d2011-12-20 02:48:34 +00002081void UsingDirectiveDecl::anchor() { }
2082
Douglas Gregor889ceb72009-02-03 19:21:40 +00002083UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2084 SourceLocation L,
2085 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002086 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002087 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002088 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002089 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002090 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2091 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002092 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2093 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002094}
2095
Richard Smithf7981722013-11-22 09:01:48 +00002096UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2097 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002098 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2099 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002100 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002101 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002102}
2103
Sebastian Redla6602e92009-11-23 15:34:23 +00002104NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2105 if (NamespaceAliasDecl *NA =
2106 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2107 return NA->getNamespace();
2108 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2109}
2110
Richard Smith053f6c62014-05-16 23:01:30 +00002111NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2112 SourceLocation StartLoc, SourceLocation IdLoc,
2113 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2114 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2115 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2116 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002117 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002118
Douglas Gregore57e7522012-01-07 09:11:48 +00002119 if (PrevDecl)
2120 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2121}
2122
Douglas Gregor72172e92012-01-05 21:55:30 +00002123NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002124 bool Inline, SourceLocation StartLoc,
2125 SourceLocation IdLoc, IdentifierInfo *Id,
2126 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002127 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2128 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002129}
2130
2131NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002132 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002133 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002134}
2135
Chandler Carruth21c90602015-12-30 03:24:14 +00002136NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2137 if (isFirstDecl())
2138 return this;
2139
2140 return AnonOrFirstNamespaceAndInline.getPointer();
2141}
2142
2143const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2144 if (isFirstDecl())
2145 return this;
2146
2147 return AnonOrFirstNamespaceAndInline.getPointer();
2148}
2149
2150bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2151
Richard Smithd7af8a32014-05-10 01:17:36 +00002152NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2153 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002154}
2155NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2156 return getPreviousDecl();
2157}
2158NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2159 return getMostRecentDecl();
2160}
2161
David Blaikie68e081d2011-12-20 02:48:34 +00002162void NamespaceAliasDecl::anchor() { }
2163
Richard Smithf4634362014-09-03 23:11:22 +00002164NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2165 return getNextRedeclaration();
2166}
2167NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2168 return getPreviousDecl();
2169}
2170NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2171 return getMostRecentDecl();
2172}
2173
Mike Stump11289f42009-09-09 15:08:12 +00002174NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002175 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002176 SourceLocation AliasLoc,
2177 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002178 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002179 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002180 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002181 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002182 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2183 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002184 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002185 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002186}
2187
Douglas Gregor72172e92012-01-05 21:55:30 +00002188NamespaceAliasDecl *
2189NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002190 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002191 SourceLocation(), nullptr,
2192 NestedNameSpecifierLoc(),
2193 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002194}
2195
David Blaikie68e081d2011-12-20 02:48:34 +00002196void UsingShadowDecl::anchor() { }
2197
Richard Smith5179eb72016-06-28 19:03:57 +00002198UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2199 SourceLocation Loc, UsingDecl *Using,
2200 NamedDecl *Target)
2201 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
2202 redeclarable_base(C), Underlying(Target),
2203 UsingOrNextShadow(cast<NamedDecl>(Using)) {
2204 if (Target)
2205 IdentifierNamespace = Target->getIdentifierNamespace();
2206 setImplicit();
2207}
2208
2209UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2210 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
2211 redeclarable_base(C), Underlying(), UsingOrNextShadow() {}
2212
Douglas Gregor72172e92012-01-05 21:55:30 +00002213UsingShadowDecl *
2214UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002215 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002216}
2217
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002218UsingDecl *UsingShadowDecl::getUsingDecl() const {
2219 const UsingShadowDecl *Shadow = this;
2220 while (const UsingShadowDecl *NextShadow =
2221 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2222 Shadow = NextShadow;
2223 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2224}
2225
Richard Smith5179eb72016-06-28 19:03:57 +00002226void ConstructorUsingShadowDecl::anchor() { }
2227
2228ConstructorUsingShadowDecl *
2229ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2230 SourceLocation Loc, UsingDecl *Using,
2231 NamedDecl *Target, bool IsVirtual) {
2232 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2233 IsVirtual);
2234}
2235
2236ConstructorUsingShadowDecl *
2237ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2238 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2239}
2240
2241CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2242 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2243}
2244
David Blaikie68e081d2011-12-20 02:48:34 +00002245void UsingDecl::anchor() { }
2246
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002247void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2248 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2249 "declaration already in set");
2250 assert(S->getUsingDecl() == this);
2251
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002252 if (FirstUsingShadow.getPointer())
2253 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2254 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002255}
2256
2257void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2258 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2259 "declaration not in set");
2260 assert(S->getUsingDecl() == this);
2261
2262 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2263
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002264 if (FirstUsingShadow.getPointer() == S) {
2265 FirstUsingShadow.setPointer(
2266 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002267 S->UsingOrNextShadow = this;
2268 return;
2269 }
2270
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002271 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002272 while (Prev->UsingOrNextShadow != S)
2273 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2274 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2275 S->UsingOrNextShadow = this;
2276}
2277
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002278UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2279 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002280 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002281 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002282 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002283}
2284
Douglas Gregor72172e92012-01-05 21:55:30 +00002285UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002286 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2287 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2288 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002289}
2290
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002291SourceRange UsingDecl::getSourceRange() const {
2292 SourceLocation Begin = isAccessDeclaration()
2293 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2294 return SourceRange(Begin, getNameInfo().getEndLoc());
2295}
2296
Richard Smith151c4562016-12-20 21:35:28 +00002297void UsingPackDecl::anchor() { }
2298
2299UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2300 NamedDecl *InstantiatedFrom,
2301 ArrayRef<NamedDecl *> UsingDecls) {
2302 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2303 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2304}
2305
2306UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2307 unsigned NumExpansions) {
2308 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2309 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2310 Result->NumExpansions = NumExpansions;
2311 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2312 for (unsigned I = 0; I != NumExpansions; ++I)
2313 new (Trail + I) NamedDecl*(nullptr);
2314 return Result;
2315}
2316
David Blaikie68e081d2011-12-20 02:48:34 +00002317void UnresolvedUsingValueDecl::anchor() { }
2318
John McCalle61f2ba2009-11-18 02:36:19 +00002319UnresolvedUsingValueDecl *
2320UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2321 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002322 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002323 const DeclarationNameInfo &NameInfo,
2324 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002325 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002326 QualifierLoc, NameInfo,
2327 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002328}
2329
Douglas Gregor72172e92012-01-05 21:55:30 +00002330UnresolvedUsingValueDecl *
2331UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002332 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2333 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002334 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002335 DeclarationNameInfo(),
2336 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002337}
2338
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002339SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2340 SourceLocation Begin = isAccessDeclaration()
2341 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2342 return SourceRange(Begin, getNameInfo().getEndLoc());
2343}
2344
David Blaikie68e081d2011-12-20 02:48:34 +00002345void UnresolvedUsingTypenameDecl::anchor() { }
2346
John McCalle61f2ba2009-11-18 02:36:19 +00002347UnresolvedUsingTypenameDecl *
2348UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2349 SourceLocation UsingLoc,
2350 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002351 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002352 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002353 DeclarationName TargetName,
2354 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002355 return new (C, DC) UnresolvedUsingTypenameDecl(
2356 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002357 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002358}
2359
Douglas Gregor72172e92012-01-05 21:55:30 +00002360UnresolvedUsingTypenameDecl *
2361UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002362 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002363 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002364 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002365}
2366
David Blaikie68e081d2011-12-20 02:48:34 +00002367void StaticAssertDecl::anchor() { }
2368
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002369StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002370 SourceLocation StaticAssertLoc,
2371 Expr *AssertExpr,
2372 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002373 SourceLocation RParenLoc,
2374 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002375 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2376 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002377}
2378
Richard Smithf7981722013-11-22 09:01:48 +00002379StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002380 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002381 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2382 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002383}
2384
Richard Smithbdb84f32016-07-22 23:36:59 +00002385void BindingDecl::anchor() {}
2386
2387BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2388 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002389 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002390}
2391
2392BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002393 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002394}
2395
Richard Smith97fcf4b2016-08-14 23:15:52 +00002396VarDecl *BindingDecl::getHoldingVar() const {
2397 Expr *B = getBinding();
2398 if (!B)
2399 return nullptr;
2400 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2401 if (!DRE)
2402 return nullptr;
2403
2404 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2405 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2406 return VD;
2407}
2408
Richard Smithbdb84f32016-07-22 23:36:59 +00002409void DecompositionDecl::anchor() {}
2410
2411DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2412 SourceLocation StartLoc,
2413 SourceLocation LSquareLoc,
2414 QualType T, TypeSourceInfo *TInfo,
2415 StorageClass SC,
2416 ArrayRef<BindingDecl *> Bindings) {
2417 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2418 return new (C, DC, Extra)
2419 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2420}
2421
2422DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2423 unsigned ID,
2424 unsigned NumBindings) {
2425 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002426 auto *Result = new (C, ID, Extra)
2427 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2428 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002429 // Set up and clean out the bindings array.
2430 Result->NumBindings = NumBindings;
2431 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2432 for (unsigned I = 0; I != NumBindings; ++I)
2433 new (Trail + I) BindingDecl*(nullptr);
2434 return Result;
2435}
2436
Richard Smith7873de02016-08-11 22:25:46 +00002437void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2438 os << '[';
2439 bool Comma = false;
2440 for (auto *B : bindings()) {
2441 if (Comma)
2442 os << ", ";
2443 B->printName(os);
2444 Comma = true;
2445 }
2446 os << ']';
2447}
2448
Richard Smithf7981722013-11-22 09:01:48 +00002449MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2450 SourceLocation L, DeclarationName N,
2451 QualType T, TypeSourceInfo *TInfo,
2452 SourceLocation StartL,
2453 IdentifierInfo *Getter,
2454 IdentifierInfo *Setter) {
2455 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2456}
2457
2458MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2459 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002460 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2461 DeclarationName(), QualType(), nullptr,
2462 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002463}
2464
Anders Carlsson6750d162009-03-26 23:46:50 +00002465static const char *getAccessName(AccessSpecifier AS) {
2466 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002467 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002468 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002469 case AS_public:
2470 return "public";
2471 case AS_private:
2472 return "private";
2473 case AS_protected:
2474 return "protected";
2475 }
David Blaikief47fa302012-01-17 02:30:50 +00002476 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002477}
2478
2479const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2480 AccessSpecifier AS) {
2481 return DB << getAccessName(AS);
2482}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002483
2484const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2485 AccessSpecifier AS) {
2486 return DB << getAccessName(AS);
2487}