blob: d7472fcc358cbdcbf6fb877c65574e244bf5466b [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"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000030
David Blaikie68e081d2011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor72172e92012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000034 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000035}
36
Richard Smitha4ba74c2013-08-30 04:46:40 +000037void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
38 ExternalASTSource *Source = C.getExternalSource();
39 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
40 assert(Source && "getFromExternalSource with no external source");
41
42 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
43 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
44 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
45 Impl.Decls.setLazy(false);
46}
47
John McCall67da35c2010-02-04 22:26:26 +000048CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000049 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
50 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
51 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
52 HasPrivateFields(false), HasProtectedFields(false),
53 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
54 HasOnlyCMembers(true), HasInClassInitializer(false),
55 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000056 HasInheritedConstructor(false), HasInheritedAssignment(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000057 NeedOverloadResolutionForMoveConstructor(false),
58 NeedOverloadResolutionForMoveAssignment(false),
59 NeedOverloadResolutionForDestructor(false),
60 DefaultedMoveConstructorIsDeleted(false),
61 DefaultedMoveAssignmentIsDeleted(false),
62 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
63 DeclaredNonTrivialSpecialMembers(0), HasIrrelevantDestructor(true),
64 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000065 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000066 DefaultedDefaultConstructorIsConstexpr(true),
67 HasConstexprDefaultConstructor(false),
68 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
69 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
70 ImplicitCopyConstructorHasConstParam(true),
71 ImplicitCopyAssignmentHasConstParam(true),
72 HasDeclaredCopyConstructorWithConstParam(false),
73 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
74 IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
75 VBases(), Definition(D), FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000076
Benjamin Kramer300c0632012-07-04 17:03:33 +000077CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
78 return Bases.get(Definition->getASTContext().getExternalSource());
79}
80
81CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
82 return VBases.get(Definition->getASTContext().getExternalSource());
83}
84
Richard Smith053f6c62014-05-16 23:01:30 +000085CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
86 DeclContext *DC, SourceLocation StartLoc,
87 SourceLocation IdLoc, IdentifierInfo *Id,
88 CXXRecordDecl *PrevDecl)
89 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
90 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smithb6483992016-05-17 22:44:15 +000091 : nullptr),
Richard Smith053f6c62014-05-16 23:01:30 +000092 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000093
Jay Foad39c79802011-01-12 09:06:06 +000094CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000095 DeclContext *DC, SourceLocation StartLoc,
96 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000097 CXXRecordDecl* PrevDecl,
98 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +000099 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +0000100 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000103 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000104 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000105 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000106 return R;
107}
108
Richard Smith053f6c62014-05-16 23:01:30 +0000109CXXRecordDecl *
110CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
111 TypeSourceInfo *Info, SourceLocation Loc,
112 bool Dependent, bool IsGeneric,
113 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000114 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000115 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
116 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000117 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000118 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000119 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000120 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000121 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000122 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000123 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000124 return R;
125}
126
Douglas Gregor72172e92012-01-05 21:55:30 +0000127CXXRecordDecl *
128CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000129 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000130 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000131 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000132 R->MayHaveOutOfDateDef = false;
133 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000134}
135
Mike Stump11289f42009-09-09 15:08:12 +0000136void
Craig Toppere6337e12015-12-25 00:36:02 +0000137CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
138 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000139 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000140
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000141 if (!data().Bases.isOffset() && data().NumBases > 0)
142 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000143
Craig Toppere6337e12015-12-25 00:36:02 +0000144 if (NumBases) {
Richard Smith872307e2016-03-08 22:17:41 +0000145 if (!C.getLangOpts().CPlusPlus1z) {
146 // C++ [dcl.init.aggr]p1:
147 // An aggregate is [...] a class with [...] no base classes [...].
148 data().Aggregate = false;
149 }
Richard Smithaed32b42011-10-18 20:08:55 +0000150
151 // C++ [class]p4:
152 // A POD-struct is an aggregate class...
153 data().PlainOldData = false;
154 }
155
Anders Carlssonabb20e62010-03-29 05:13:12 +0000156 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000157 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000158
159 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000160 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000161
Craig Toppere6337e12015-12-25 00:36:02 +0000162 data().Bases = new(C) CXXBaseSpecifier [NumBases];
163 data().NumBases = NumBases;
164 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000165 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000166 // Keep track of inherited vbases for this base class.
167 const CXXBaseSpecifier *Base = Bases[i];
168 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000169 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000170 if (BaseType->isDependentType())
171 continue;
172 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000173 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000174
Chandler Carruthb1963742011-04-30 09:17:45 +0000175 if (!BaseClassDecl->isEmpty()) {
176 if (!data().Empty) {
177 // C++0x [class]p7:
178 // A standard-layout class is a class that:
179 // [...]
180 // -- either has no non-static data members in the most derived
181 // class and at most one base class with non-static data members,
182 // or has no base classes with non-static data members, and
183 // If this is the second non-empty base, then neither of these two
184 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000185 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000186 }
187
David Majnemer5cfda6f2016-05-22 05:34:26 +0000188 // C++14 [meta.unary.prop]p4:
189 // T is a class type [...] with [...] no base class B for which
190 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000191 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000192 data().HasNoNonEmptyBases = false;
193 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000194
Richard Smith872307e2016-03-08 22:17:41 +0000195 // C++1z [dcl.init.agg]p1:
196 // An aggregate is a class with [...] no private or protected base classes
197 if (Base->getAccessSpecifier() != AS_public)
198 data().Aggregate = false;
199
Douglas Gregor11c024b2010-09-28 20:50:54 +0000200 // C++ [class.virtual]p1:
201 // A class that declares or inherits a virtual function is called a
202 // polymorphic class.
203 if (BaseClassDecl->isPolymorphic())
204 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000205
Chandler Carruthb1963742011-04-30 09:17:45 +0000206 // C++0x [class]p7:
207 // A standard-layout class is a class that: [...]
208 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000209 if (!BaseClassDecl->isStandardLayout())
210 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000211
Chandler Carruthe71d0622011-04-24 02:49:34 +0000212 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000213 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000214 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000215
Anders Carlssonabb20e62010-03-29 05:13:12 +0000216 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000217 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000218 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000219 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000220 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000221
222 // C++11 [class.copy]p8:
223 // The implicitly-declared copy constructor for a class X will have
224 // the form 'X::X(const X&)' if each [...] virtual base class B of X
225 // has a copy constructor whose first parameter is of type
226 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000227 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000228 if (!VBaseDecl->hasCopyConstructorWithConstParam())
229 data().ImplicitCopyConstructorHasConstParam = false;
Richard Smith872307e2016-03-08 22:17:41 +0000230
231 // C++1z [dcl.init.agg]p1:
232 // An aggregate is a class with [...] no virtual base classes
233 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000234 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000235 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000236
237 if (Base->isVirtual()) {
238 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000239 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000240 VBases.push_back(Base);
241
David Majnemer5cfda6f2016-05-22 05:34:26 +0000242 // C++14 [meta.unary.prop] is_empty:
243 // T is a class type, but not a union type, with ... no virtual base
244 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000245 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000246
Richard Smith872307e2016-03-08 22:17:41 +0000247 // C++1z [dcl.init.agg]p1:
248 // An aggregate is a class with [...] no virtual base classes
249 data().Aggregate = false;
250
Richard Smith328aae52012-11-30 05:11:39 +0000251 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
252 // A [default constructor, copy/move constructor, or copy/move assignment
253 // operator for a class X] is trivial [...] if:
254 // -- class X has [...] no virtual base classes
255 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000256
257 // C++0x [class]p7:
258 // A standard-layout class is a class that: [...]
259 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000260 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000261
262 // C++11 [dcl.constexpr]p4:
263 // In the definition of a constexpr constructor [...]
264 // -- the class shall not have any virtual base classes
265 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000266 } else {
267 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000268 // A default constructor is trivial [...] if:
269 // -- all the direct base classes of its class have trivial default
270 // constructors.
271 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000272 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
273
Chandler Carruthad7d4042011-04-23 23:10:33 +0000274 // C++0x [class.copy]p13:
275 // A copy/move constructor for class X is trivial if [...]
276 // [...]
277 // -- the constructor selected to copy/move each direct base class
278 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000279 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000280 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000281 // If the base class doesn't have a simple move constructor, we'll eagerly
282 // declare it and perform overload resolution to determine which function
283 // it actually calls. If it does have a simple move constructor, this
284 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000285 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000286 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000287
288 // C++0x [class.copy]p27:
289 // A copy/move assignment operator for class X is trivial if [...]
290 // [...]
291 // -- the assignment operator selected to copy/move each direct base
292 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000293 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000294 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000295 // If the base class doesn't have a simple move assignment, we'll eagerly
296 // declare it and perform overload resolution to determine which function
297 // it actually calls. If it does have a simple move assignment, this
298 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000299 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000300 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000301
302 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000303 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000304 // requirements of a constexpr constructor, the implicitly-defined
305 // default constructor is constexpr.
306 if (!BaseClassDecl->hasConstexprDefaultConstructor())
307 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000308 }
Richard Smith92f241f2012-12-08 02:53:02 +0000309
Douglas Gregor11c024b2010-09-28 20:50:54 +0000310 // C++ [class.ctor]p3:
311 // A destructor is trivial if all the direct base classes of its class
312 // have trivial destructors.
313 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000314 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000315
316 if (!BaseClassDecl->hasIrrelevantDestructor())
317 data().HasIrrelevantDestructor = false;
318
Richard Smith1c33fe82012-11-28 06:23:12 +0000319 // C++11 [class.copy]p18:
320 // The implicitly-declared copy assignment oeprator for a class X will
321 // have the form 'X& X::operator=(const X&)' if each direct base class B
322 // of X has a copy assignment operator whose parameter is of type 'const
323 // B&', 'const volatile B&', or 'B' [...]
324 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
325 data().ImplicitCopyAssignmentHasConstParam = false;
326
327 // C++11 [class.copy]p8:
328 // The implicitly-declared copy constructor for a class X will have
329 // the form 'X::X(const X&)' if each direct [...] base class B of X
330 // has a copy constructor whose first parameter is of type
331 // 'const B&' or 'const volatile B&' [...]
332 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
333 data().ImplicitCopyConstructorHasConstParam = false;
334
John McCall31168b02011-06-15 23:02:42 +0000335 // A class has an Objective-C object member if... or any of its bases
336 // has an Objective-C object member.
337 if (BaseClassDecl->hasObjectMember())
338 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000339
340 if (BaseClassDecl->hasVolatileMember())
341 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000342
Douglas Gregor61226d32011-05-13 01:05:07 +0000343 // Keep track of the presence of mutable fields.
344 if (BaseClassDecl->hasMutableFields())
345 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000346
347 if (BaseClassDecl->hasUninitializedReferenceMember())
348 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000349
Nico Weber6a6376b2016-02-19 01:52:46 +0000350 if (!BaseClassDecl->allowConstDefaultInit())
351 data().HasUninitializedFields = true;
352
Richard Smith6b02d462012-12-08 08:32:28 +0000353 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000354 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000355
David Majnemer5ef4fe72014-06-13 06:43:46 +0000356 if (VBases.empty()) {
357 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000358 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000359 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000360
361 // Create base specifier for any direct or indirect virtual bases.
362 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
363 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000364 for (int I = 0, E = VBases.size(); I != E; ++I) {
365 QualType Type = VBases[I]->getType();
366 if (!Type->isDependentType())
367 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000368 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000369 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000370
371 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000372}
373
374void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
375 // C++11 [class.copy]p11:
376 // A defaulted copy/move constructor for a class X is defined as
377 // deleted if X has:
378 // -- a direct or virtual base class B that cannot be copied/moved [...]
379 // -- a non-static data member of class type M (or array thereof)
380 // that cannot be copied or moved [...]
381 if (!Subobj->hasSimpleMoveConstructor())
382 data().NeedOverloadResolutionForMoveConstructor = true;
383
384 // C++11 [class.copy]p23:
385 // A defaulted copy/move assignment operator for a class X is defined as
386 // deleted if X has:
387 // -- a direct or virtual base class B that cannot be copied/moved [...]
388 // -- a non-static data member of class type M (or array thereof)
389 // that cannot be copied or moved [...]
390 if (!Subobj->hasSimpleMoveAssignment())
391 data().NeedOverloadResolutionForMoveAssignment = true;
392
393 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
394 // A defaulted [ctor or dtor] for a class X is defined as
395 // deleted if X has:
396 // -- any direct or virtual base class [...] has a type with a destructor
397 // that is deleted or inaccessible from the defaulted [ctor or dtor].
398 // -- any non-static data member has a type with a destructor
399 // that is deleted or inaccessible from the defaulted [ctor or dtor].
400 if (!Subobj->hasSimpleDestructor()) {
401 data().NeedOverloadResolutionForMoveConstructor = true;
402 data().NeedOverloadResolutionForDestructor = true;
403 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000404}
405
Douglas Gregord2e6a452010-01-14 17:47:39 +0000406bool CXXRecordDecl::hasAnyDependentBases() const {
407 if (!isDependentContext())
408 return false;
409
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000410 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000411}
412
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000413bool CXXRecordDecl::isTriviallyCopyable() const {
414 // C++0x [class]p5:
415 // A trivially copyable class is a class that:
416 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000417 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000418 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000419 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000420 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000421 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000422 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000423 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000424 // -- has a trivial destructor.
425 if (!hasTrivialDestructor()) return false;
426
427 return true;
428}
429
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000430void CXXRecordDecl::markedVirtualFunctionPure() {
431 // C++ [class.abstract]p2:
432 // A class is abstract if it has at least one pure virtual function.
433 data().Abstract = true;
434}
435
436void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000437 if (!D->isImplicit() &&
438 !isa<FieldDecl>(D) &&
439 !isa<IndirectFieldDecl>(D) &&
440 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
441 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
442 data().HasOnlyCMembers = false;
443
444 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000445 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000446 return;
447
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000448 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
449 if (FunTmpl)
450 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000451
452 // FIXME: Pass NamedDecl* to addedMember?
453 Decl *DUnderlying = D;
454 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
455 DUnderlying = ND->getUnderlyingDecl();
456 if (FunctionTemplateDecl *UnderlyingFunTmpl =
457 dyn_cast<FunctionTemplateDecl>(DUnderlying))
458 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
459 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000460
Douglas Gregora832d3e2010-09-28 19:45:33 +0000461 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
462 if (Method->isVirtual()) {
463 // C++ [dcl.init.aggr]p1:
464 // An aggregate is an array or a class with [...] no virtual functions.
465 data().Aggregate = false;
466
467 // C++ [class]p4:
468 // A POD-struct is an aggregate class...
469 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000470
David Majnemer5cfda6f2016-05-22 05:34:26 +0000471 // C++14 [meta.unary.prop]p4:
472 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000473 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000474
475 // C++ [class.virtual]p1:
476 // A class that declares or inherits a virtual function is called a
477 // polymorphic class.
478 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000479
Richard Smith328aae52012-11-30 05:11:39 +0000480 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
481 // A [default constructor, copy/move constructor, or copy/move
482 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000483 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000484 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000485
Chandler Carruthb1963742011-04-30 09:17:45 +0000486 // C++0x [class]p7:
487 // A standard-layout class is a class that: [...]
488 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000489 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000490 }
491 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000492
Richard Smith1c33fe82012-11-28 06:23:12 +0000493 // Notify the listener if an implicit member was added after the definition
494 // was completed.
495 if (!isBeingDefined() && D->isImplicit())
496 if (ASTMutationListener *L = getASTMutationListener())
497 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000498
Richard Smith328aae52012-11-30 05:11:39 +0000499 // The kind of special member this declaration is, if any.
500 unsigned SMKind = 0;
501
Richard Smith1c33fe82012-11-28 06:23:12 +0000502 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000503 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000504 if (!Constructor->isImplicit()) {
505 // Note that we have a user-declared constructor.
506 data().UserDeclaredConstructor = true;
507
508 // C++ [class]p4:
509 // A POD-struct is an aggregate class [...]
510 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
511 // type is technically an aggregate in C++0x since it wouldn't be in 03.
512 data().PlainOldData = false;
513 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000514
Alexis Hunt88c75c32011-05-09 21:45:35 +0000515 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000516 SMKind |= SMF_DefaultConstructor;
517
Richard Smith5179eb72016-06-28 19:03:57 +0000518 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000519 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000520 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000521 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000522 if (Constructor->isDefaulted())
523 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000524 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000525
Chandler Carruthad7d4042011-04-23 23:10:33 +0000526 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000527 unsigned Quals;
528 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000529 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000530
531 if (Quals & Qualifiers::Const)
532 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000533 } else if (Constructor->isMoveConstructor())
534 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000535 }
Richard Smith5179eb72016-06-28 19:03:57 +0000536 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000537
Richard Smith5179eb72016-06-28 19:03:57 +0000538 // Handle constructors, including those inherited from base classes.
539 if (CXXConstructorDecl *Constructor =
540 dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000541 // Record if we see any constexpr constructors which are neither copy
542 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000543 // C++1z [basic.types]p10:
544 // [...] has at least one constexpr constructor or constructor template
545 // (possibly inherited from a base class) that is not a copy or move
546 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000547 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000548 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000549
Alexis Hunt88c75c32011-05-09 21:45:35 +0000550 // C++ [dcl.init.aggr]p1:
551 // An aggregate is an array or a class with no user-declared
552 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000553 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000554 // An aggregate is an array or a class with no user-provided
555 // constructors [...].
Richard Smith5179eb72016-06-28 19:03:57 +0000556 // C++11 [dcl.init.aggr]p1:
557 // An aggregate is an array or a class with no user-provided
558 // constructors (including those inherited from a base class) [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000559 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith5179eb72016-06-28 19:03:57 +0000560 ? Constructor->isUserProvided()
561 : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000562 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000563 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000564
Richard Smith1c33fe82012-11-28 06:23:12 +0000565 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000566 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000567 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000568
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000569 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000570 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000571 // If the destructor is explicitly defaulted and not trivial or not public
572 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
573 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000574
Richard Smith1c33fe82012-11-28 06:23:12 +0000575 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000576 // A destructor is trivial if [...] the destructor is not virtual.
577 if (DD->isVirtual())
578 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000579 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000580
581 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000582 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000583 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000584 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000585
586 const ReferenceType *ParamTy =
587 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
588 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
589 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000590 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000591
Richard Smith328aae52012-11-30 05:11:39 +0000592 if (Method->isMoveAssignmentOperator())
593 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000594
Douglas Gregor457104e2010-09-29 04:25:11 +0000595 // Keep the list of conversion functions up-to-date.
596 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000597 // FIXME: We use the 'unsafe' accessor for the access specifier here,
598 // because Sema may not have set it yet. That's really just a misdesign
599 // in Sema. However, LLDB *will* have set the access specifier correctly,
600 // and adds declarations after the class is technically completed,
601 // so completeDefinition()'s overriding of the access specifiers doesn't
602 // work.
603 AccessSpecifier AS = Conversion->getAccessUnsafe();
604
Richard Smith328aae52012-11-30 05:11:39 +0000605 if (Conversion->getPrimaryTemplate()) {
606 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000607 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000608 ASTContext &Ctx = getASTContext();
609 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
610 NamedDecl *Primary =
611 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
612 if (Primary->getPreviousDecl())
613 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
614 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000615 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000616 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000617 }
618 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000619
Richard Smith328aae52012-11-30 05:11:39 +0000620 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000621 // If this is the first declaration of a special member, we no longer have
622 // an implicit trivial special member.
623 data().HasTrivialSpecialMembers &=
624 data().DeclaredSpecialMembers | ~SMKind;
625
626 if (!Method->isImplicit() && !Method->isUserProvided()) {
627 // This method is user-declared but not user-provided. We can't work out
628 // whether it's trivial yet (not until we get to the end of the class).
629 // We'll handle this method in finishedDefaultedOrDeletedMember.
630 } else if (Method->isTrivial())
631 data().HasTrivialSpecialMembers |= SMKind;
632 else
633 data().DeclaredNonTrivialSpecialMembers |= SMKind;
634
Richard Smith328aae52012-11-30 05:11:39 +0000635 // Note when we have declared a declared special member, and suppress the
636 // implicit declaration of this special member.
637 data().DeclaredSpecialMembers |= SMKind;
638
639 if (!Method->isImplicit()) {
640 data().UserDeclaredSpecialMembers |= SMKind;
641
642 // C++03 [class]p4:
643 // A POD-struct is an aggregate class that has [...] no user-defined
644 // copy assignment operator and no user-defined destructor.
645 //
646 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
647 // aggregates could not have any constructors, clear it even for an
648 // explicitly defaulted or deleted constructor.
649 // type is technically an aggregate in C++0x since it wouldn't be in 03.
650 //
651 // Also, a user-declared move assignment operator makes a class non-POD.
652 // This is an extension in C++03.
653 data().PlainOldData = false;
654 }
Richard Smith328aae52012-11-30 05:11:39 +0000655 }
656
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000657 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000658 }
Richard Smith328aae52012-11-30 05:11:39 +0000659
Douglas Gregora832d3e2010-09-28 19:45:33 +0000660 // Handle non-static data members.
661 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000662 // C++ [class.bit]p2:
663 // A declaration for a bit-field that omits the identifier declares an
664 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
665 // initialized.
666 if (Field->isUnnamedBitfield())
667 return;
668
Douglas Gregora832d3e2010-09-28 19:45:33 +0000669 // C++ [dcl.init.aggr]p1:
670 // An aggregate is an array or a class (clause 9) with [...] no
671 // private or protected non-static data members (clause 11).
672 //
673 // A POD must be an aggregate.
674 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
675 data().Aggregate = false;
676 data().PlainOldData = false;
677 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000678
679 // C++0x [class]p7:
680 // A standard-layout class is a class that:
681 // [...]
682 // -- has the same access control for all non-static data members,
683 switch (D->getAccess()) {
684 case AS_private: data().HasPrivateFields = true; break;
685 case AS_protected: data().HasProtectedFields = true; break;
686 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000687 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000688 };
689 if ((data().HasPrivateFields + data().HasProtectedFields +
690 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000691 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000692
Douglas Gregor61226d32011-05-13 01:05:07 +0000693 // Keep track of the presence of mutable fields.
694 if (Field->isMutable())
695 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000696
697 // C++11 [class.union]p8, DR1460:
698 // If X is a union, a non-static data member of X that is not an anonymous
699 // union is a variant member of X.
700 if (isUnion() && !Field->isAnonymousStructOrUnion())
701 data().HasVariantMembers = true;
702
Chandler Carruthad7d4042011-04-23 23:10:33 +0000703 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000704 // A POD struct is a class that is both a trivial class and a
705 // standard-layout class, and has no non-static data members of type
706 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000707 //
708 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
709 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000710 ASTContext &Context = getASTContext();
711 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000712 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000713 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000714 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000715 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
716 // Objective-C Automatic Reference Counting:
717 // If a class has a non-static data member of Objective-C pointer
718 // type (or array thereof), it is a non-POD type and its
719 // default constructor (if any), copy constructor, move constructor,
720 // copy assignment operator, move assignment operator, and destructor are
721 // non-trivial.
722 setHasObjectMember(true);
723 struct DefinitionData &Data = data();
724 Data.PlainOldData = false;
725 Data.HasTrivialSpecialMembers = 0;
726 Data.HasIrrelevantDestructor = false;
727 }
Eli Friedmana5433322013-07-20 01:06:31 +0000728 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000729 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000730
Chandler Carruthb1963742011-04-30 09:17:45 +0000731 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000732 if (!Field->hasInClassInitializer())
733 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000734
Chandler Carruthb1963742011-04-30 09:17:45 +0000735 // C++0x [class]p7:
736 // A standard-layout class is a class that:
737 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000738 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000739 }
740
Nico Weber6a6376b2016-02-19 01:52:46 +0000741 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
742 if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000743 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000744 data().HasUninitializedFields = true;
745 } else {
746 data().HasUninitializedFields = true;
747 }
748 }
749
Richard Smith3607ffe2012-02-13 03:54:03 +0000750 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000751 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000752 data().HasNonLiteralTypeFieldsOrBases = true;
753
Richard Smithab44d5b2013-12-10 08:25:00 +0000754 if (Field->hasInClassInitializer() ||
755 (Field->isAnonymousStructOrUnion() &&
756 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000757 data().HasInClassInitializer = true;
758
759 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000760 // A default constructor is trivial if [...] no non-static data member
761 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000762 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000763
Richard Smithe2648ba2012-05-07 01:07:30 +0000764 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000765 // An aggregate is a [...] class with [...] no
766 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000767 //
Richard Smith872307e2016-03-08 22:17:41 +0000768 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000769 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000770 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000771
Richard Smithe2648ba2012-05-07 01:07:30 +0000772 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000773 // A POD struct is [...] a trivial class.
774 data().PlainOldData = false;
775 }
776
Richard Smith6b02d462012-12-08 08:32:28 +0000777 // C++11 [class.copy]p23:
778 // A defaulted copy/move assignment operator for a class X is defined
779 // as deleted if X has:
780 // -- a non-static data member of reference type
781 if (T->isReferenceType())
782 data().DefaultedMoveAssignmentIsDeleted = true;
783
Douglas Gregor11c024b2010-09-28 20:50:54 +0000784 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
785 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
786 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000787 addedClassSubobject(FieldRec);
788
Richard Smithc91d12c2013-11-25 07:07:05 +0000789 // We may need to perform overload resolution to determine whether a
790 // field can be moved if it's const or volatile qualified.
791 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
792 data().NeedOverloadResolutionForMoveConstructor = true;
793 data().NeedOverloadResolutionForMoveAssignment = true;
794 }
795
Richard Smith6b02d462012-12-08 08:32:28 +0000796 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
797 // A defaulted [special member] for a class X is defined as
798 // deleted if:
799 // -- X is a union-like class that has a variant member with a
800 // non-trivial [corresponding special member]
801 if (isUnion()) {
802 if (FieldRec->hasNonTrivialMoveConstructor())
803 data().DefaultedMoveConstructorIsDeleted = true;
804 if (FieldRec->hasNonTrivialMoveAssignment())
805 data().DefaultedMoveAssignmentIsDeleted = true;
806 if (FieldRec->hasNonTrivialDestructor())
807 data().DefaultedDestructorIsDeleted = true;
808 }
809
Richard Smithdd5619f2016-08-16 00:13:47 +0000810 // For an anonymous union member, our overload resolution will perform
811 // overload resolution for its members.
812 if (Field->isAnonymousStructOrUnion()) {
813 data().NeedOverloadResolutionForMoveConstructor |=
814 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
815 data().NeedOverloadResolutionForMoveAssignment |=
816 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
817 data().NeedOverloadResolutionForDestructor |=
818 FieldRec->data().NeedOverloadResolutionForDestructor;
819 }
820
Alexis Huntf479f1b2011-05-09 18:22:59 +0000821 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000822 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000823 // -- for all the non-static data members of its class that are of
824 // class type (or array thereof), each such class has a trivial
825 // default constructor.
826 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000827 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000828
829 // C++0x [class.copy]p13:
830 // A copy/move constructor for class X is trivial if [...]
831 // [...]
832 // -- for each non-static data member of X that is of class type (or
833 // an array thereof), the constructor selected to copy/move that
834 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000835 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000836 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000837 // If the field doesn't have a simple move constructor, we'll eagerly
838 // declare the move constructor for this class and we'll decide whether
839 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000840 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000841 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000842
843 // C++0x [class.copy]p27:
844 // A copy/move assignment operator for class X is trivial if [...]
845 // [...]
846 // -- for each non-static data member of X that is of class type (or
847 // an array thereof), the assignment operator selected to
848 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000849 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000850 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000851 // If the field doesn't have a simple move assignment, we'll eagerly
852 // declare the move assignment for this class and we'll decide whether
853 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000854 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000855 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000856
Douglas Gregor11c024b2010-09-28 20:50:54 +0000857 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000858 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000859 if (!FieldRec->hasIrrelevantDestructor())
860 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000861 if (FieldRec->hasObjectMember())
862 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000863 if (FieldRec->hasVolatileMember())
864 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000865
866 // C++0x [class]p7:
867 // A standard-layout class is a class that:
868 // -- has no non-static data members of type non-standard-layout
869 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000870 if (!FieldRec->isStandardLayout())
871 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000872
873 // C++0x [class]p7:
874 // A standard-layout class is a class that:
875 // [...]
876 // -- has no base classes of the same type as the first non-static
877 // data member.
878 // We don't want to expend bits in the state of the record decl
879 // tracking whether this is the first non-static data member so we
880 // cheat a bit and use some of the existing state: the empty bit.
881 // Virtual bases and virtual methods make a class non-empty, but they
882 // also make it non-standard-layout so we needn't check here.
883 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000884 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000885 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000886 // data member must come through here with Empty still true, and Empty
887 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000888 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000889 for (const auto &BI : bases()) {
890 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000891 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000892 break;
893 }
894 }
895 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000896
897 // Keep track of the presence of mutable fields.
898 if (FieldRec->hasMutableFields())
899 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000900
901 // C++11 [class.copy]p13:
902 // If the implicitly-defined constructor would satisfy the
903 // requirements of a constexpr constructor, the implicitly-defined
904 // constructor is constexpr.
905 // C++11 [dcl.constexpr]p4:
906 // -- every constructor involved in initializing non-static data
907 // members [...] shall be a constexpr constructor
908 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000909 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000910 // The standard requires any in-class initializer to be a constant
911 // expression. We consider this to be a defect.
912 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000913
914 // C++11 [class.copy]p8:
915 // The implicitly-declared copy constructor for a class X will have
916 // the form 'X::X(const X&)' if [...] for all the non-static data
917 // members of X that are of a class type M (or array thereof), each
918 // such class type has a copy constructor whose first parameter is
919 // of type 'const M&' or 'const volatile M&'.
920 if (!FieldRec->hasCopyConstructorWithConstParam())
921 data().ImplicitCopyConstructorHasConstParam = false;
922
923 // C++11 [class.copy]p18:
924 // The implicitly-declared copy assignment oeprator for a class X will
925 // have the form 'X& X::operator=(const X&)' if [...] for all the
926 // non-static data members of X that are of a class type M (or array
927 // thereof), each such class type has a copy assignment operator whose
928 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
929 if (!FieldRec->hasCopyAssignmentWithConstParam())
930 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000931
932 if (FieldRec->hasUninitializedReferenceMember() &&
933 !Field->hasInClassInitializer())
934 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000935
936 // C++11 [class.union]p8, DR1460:
937 // a non-static data member of an anonymous union that is a member of
938 // X is also a variant member of X.
939 if (FieldRec->hasVariantMembers() &&
940 Field->isAnonymousStructOrUnion())
941 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000942 }
Richard Smithcc36f692011-12-22 02:22:31 +0000943 } else {
944 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000945 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000946 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000947 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000948
949 // C++11 [class.copy]p23:
950 // A defaulted copy/move assignment operator for a class X is defined
951 // as deleted if X has:
952 // -- a non-static data member of const non-class type (or array
953 // thereof)
954 if (T.isConstQualified())
955 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000956 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000957
958 // C++0x [class]p7:
959 // A standard-layout class is a class that:
960 // [...]
961 // -- either has no non-static data members in the most derived
962 // class and at most one base class with non-static data members,
963 // or has no base classes with non-static data members, and
964 // At this point we know that we have a non-static data member, so the last
965 // clause holds.
966 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000967 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000968
David Majnemer5cfda6f2016-05-22 05:34:26 +0000969 // C++14 [meta.unary.prop]p4:
970 // T is a class type [...] with [...] no non-static data members other
971 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000972 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000973 if (!Field->isBitField() ||
974 (!Field->getBitWidth()->isTypeDependent() &&
975 !Field->getBitWidth()->isValueDependent() &&
976 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000977 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000978 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000979 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000980
981 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000982 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000983 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000984 == DeclarationName::CXXConversionFunctionName) {
985 ASTContext &Ctx = getASTContext();
986 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
987 }
988 }
Richard Smith12e79312016-05-13 06:47:56 +0000989
990 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
991 if (Using->getDeclName().getNameKind() ==
992 DeclarationName::CXXConstructorName)
993 data().HasInheritedConstructor = true;
994
995 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
996 data().HasInheritedAssignment = true;
997 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000998}
999
Richard Smith92f241f2012-12-08 02:53:02 +00001000void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1001 assert(!D->isImplicit() && !D->isUserProvided());
1002
1003 // The kind of special member this declaration is, if any.
1004 unsigned SMKind = 0;
1005
1006 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1007 if (Constructor->isDefaultConstructor()) {
1008 SMKind |= SMF_DefaultConstructor;
1009 if (Constructor->isConstexpr())
1010 data().HasConstexprDefaultConstructor = true;
1011 }
1012 if (Constructor->isCopyConstructor())
1013 SMKind |= SMF_CopyConstructor;
1014 else if (Constructor->isMoveConstructor())
1015 SMKind |= SMF_MoveConstructor;
1016 else if (Constructor->isConstexpr())
1017 // We may now know that the constructor is constexpr.
1018 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001019 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001020 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001021 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1022 data().HasIrrelevantDestructor = false;
1023 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001024 SMKind |= SMF_CopyAssignment;
1025 else if (D->isMoveAssignmentOperator())
1026 SMKind |= SMF_MoveAssignment;
1027
1028 // Update which trivial / non-trivial special members we have.
1029 // addedMember will have skipped this step for this member.
1030 if (D->isTrivial())
1031 data().HasTrivialSpecialMembers |= SMKind;
1032 else
1033 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1034}
1035
Joao Matose9a3ed42012-08-31 22:18:20 +00001036bool CXXRecordDecl::isCLike() const {
1037 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1038 !TemplateOrInstantiation.isNull())
1039 return false;
1040 if (!hasDefinition())
1041 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001042
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001043 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001044}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001045
Faisal Vali2b391ab2013-09-26 19:54:12 +00001046bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001047 if (!isLambda()) return false;
1048 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001049}
1050
1051CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001052 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001053 DeclarationName Name =
1054 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001055 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001056
1057 assert(!Calls.empty() && "Missing lambda call operator!");
1058 assert(Calls.size() == 1 && "More than one lambda call operator!");
1059
1060 NamedDecl *CallOp = Calls.front();
1061 if (FunctionTemplateDecl *CallOpTmpl =
1062 dyn_cast<FunctionTemplateDecl>(CallOp))
1063 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001064
1065 return cast<CXXMethodDecl>(CallOp);
1066}
1067
1068CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001069 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001070 DeclarationName Name =
1071 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001072 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001073 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001074 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1075 NamedDecl *InvokerFun = Invoker.front();
1076 if (FunctionTemplateDecl *InvokerTemplate =
1077 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1078 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1079
Faisal Vali571df122013-09-29 08:45:24 +00001080 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001081}
1082
Douglas Gregor9c702202012-02-10 07:45:31 +00001083void CXXRecordDecl::getCaptureFields(
1084 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001085 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001086 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001087 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001088
Douglas Gregorc8a73492012-02-13 15:44:47 +00001089 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001090 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001091 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001092 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001093 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001094 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001095 else if (C->capturesVariable())
1096 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001097 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001098 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001099}
1100
Faisal Vali2b391ab2013-09-26 19:54:12 +00001101TemplateParameterList *
1102CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001103 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001104 CXXMethodDecl *CallOp = getLambdaCallOperator();
1105 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1106 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001107 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001108}
Douglas Gregor9c702202012-02-10 07:45:31 +00001109
Richard Smith0bae6242016-08-25 00:34:00 +00001110Decl *CXXRecordDecl::getLambdaContextDecl() const {
1111 assert(isLambda() && "Not a lambda closure type!");
1112 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1113 return getLambdaData().ContextDecl.get(Source);
1114}
1115
John McCall1e3a1a72010-03-15 09:07:48 +00001116static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001117 QualType T =
1118 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1119 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001120 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001121}
1122
John McCall1e3a1a72010-03-15 09:07:48 +00001123/// Collect the visible conversions of a base class.
1124///
James Dennettb5d5f812012-06-15 22:28:09 +00001125/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001126/// \param InVirtual whether this base class is a virtual base (or a base
1127/// of a virtual base)
1128/// \param Access the access along the inheritance path to this base
1129/// \param ParentHiddenTypes the conversions provided by the inheritors
1130/// of this base
1131/// \param Output the set to which to add conversions from non-virtual bases
1132/// \param VOutput the set to which to add conversions from virtual bases
1133/// \param HiddenVBaseCs the set of conversions which were hidden in a
1134/// virtual base along some inheritance path
1135static void CollectVisibleConversions(ASTContext &Context,
1136 CXXRecordDecl *Record,
1137 bool InVirtual,
1138 AccessSpecifier Access,
1139 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001140 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001141 UnresolvedSetImpl &VOutput,
1142 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1143 // The set of types which have conversions in this class or its
1144 // subclasses. As an optimization, we don't copy the derived set
1145 // unless it might change.
1146 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1147 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1148
1149 // Collect the direct conversions and figure out which conversions
1150 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001151 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1152 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1153 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001154 HiddenTypesBuffer = ParentHiddenTypes;
1155 HiddenTypes = &HiddenTypesBuffer;
1156
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001157 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001158 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1159 bool Hidden = ParentHiddenTypes.count(ConvType);
1160 if (!Hidden)
1161 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001162
1163 // If this conversion is hidden and we're in a virtual base,
1164 // remember that it's hidden along some inheritance path.
1165 if (Hidden && InVirtual)
1166 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1167
1168 // If this conversion isn't hidden, add it to the appropriate output.
1169 else if (!Hidden) {
1170 AccessSpecifier IAccess
1171 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1172
1173 if (InVirtual)
1174 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001175 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001176 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001177 }
1178 }
1179 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001180
John McCall1e3a1a72010-03-15 09:07:48 +00001181 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001182 for (const auto &I : Record->bases()) {
1183 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001184 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001185
John McCall1e3a1a72010-03-15 09:07:48 +00001186 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001187 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1188 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001189
John McCall1e3a1a72010-03-15 09:07:48 +00001190 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1191 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1192 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001193 }
John McCall1e3a1a72010-03-15 09:07:48 +00001194}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001195
John McCall1e3a1a72010-03-15 09:07:48 +00001196/// Collect the visible conversions of a class.
1197///
1198/// This would be extremely straightforward if it weren't for virtual
1199/// bases. It might be worth special-casing that, really.
1200static void CollectVisibleConversions(ASTContext &Context,
1201 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001202 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001203 // The collection of all conversions in virtual bases that we've
1204 // found. These will be added to the output as long as they don't
1205 // appear in the hidden-conversions set.
1206 UnresolvedSet<8> VBaseCs;
1207
1208 // The set of conversions in virtual bases that we've determined to
1209 // be hidden.
1210 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1211
1212 // The set of types hidden by classes derived from this one.
1213 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1214
1215 // Go ahead and collect the direct conversions and add them to the
1216 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001217 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1218 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001219 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001220 for (; ConvI != ConvE; ++ConvI)
1221 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001222
1223 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001224 for (const auto &I : Record->bases()) {
1225 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001226 if (!RT) continue;
1227
1228 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001229 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001230 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1231 }
1232
1233 // Add any unhidden conversions provided by virtual bases.
1234 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1235 I != E; ++I) {
1236 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001237 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001238 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001239}
1240
1241/// getVisibleConversionFunctions - get all conversion functions visible
1242/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001243llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001244CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001245 ASTContext &Ctx = getASTContext();
1246
1247 ASTUnresolvedSet *Set;
1248 if (bases_begin() == bases_end()) {
1249 // If root class, all conversions are visible.
1250 Set = &data().Conversions.get(Ctx);
1251 } else {
1252 Set = &data().VisibleConversions.get(Ctx);
1253 // If visible conversion list is not evaluated, evaluate it.
1254 if (!data().ComputedVisibleConversions) {
1255 CollectVisibleConversions(Ctx, this, *Set);
1256 data().ComputedVisibleConversions = true;
1257 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001258 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001259 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001260}
1261
John McCallda4458e2010-03-31 01:36:47 +00001262void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1263 // This operation is O(N) but extremely rare. Sema only uses it to
1264 // remove UsingShadowDecls in a class that were followed by a direct
1265 // declaration, e.g.:
1266 // class A : B {
1267 // using B::operator int;
1268 // operator int();
1269 // };
1270 // This is uncommon by itself and even more uncommon in conjunction
1271 // with sufficiently large numbers of directly-declared conversions
1272 // that asymptotic behavior matters.
1273
Richard Smitha4ba74c2013-08-30 04:46:40 +00001274 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001275 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1276 if (Convs[I].getDecl() == ConvDecl) {
1277 Convs.erase(I);
1278 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1279 && "conversion was found multiple times in unresolved set");
1280 return;
1281 }
1282 }
1283
1284 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001285}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001286
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001287CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001288 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001289 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001290
1291 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001292}
1293
Chandler Carruth21c90602015-12-30 03:24:14 +00001294MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1295 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1296}
1297
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001298void
1299CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1300 TemplateSpecializationKind TSK) {
1301 assert(TemplateOrInstantiation.isNull() &&
1302 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001303 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001304 TemplateOrInstantiation
1305 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1306}
1307
Chandler Carruth21c90602015-12-30 03:24:14 +00001308ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1309 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1310}
1311
1312void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1313 TemplateOrInstantiation = Template;
1314}
1315
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001316TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1317 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001318 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1319 return Spec->getSpecializationKind();
1320
Douglas Gregor06db9f52009-10-12 20:18:28 +00001321 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001322 return MSInfo->getTemplateSpecializationKind();
1323
1324 return TSK_Undeclared;
1325}
1326
1327void
1328CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1329 if (ClassTemplateSpecializationDecl *Spec
1330 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1331 Spec->setSpecializationKind(TSK);
1332 return;
1333 }
1334
Douglas Gregor06db9f52009-10-12 20:18:28 +00001335 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001336 MSInfo->setTemplateSpecializationKind(TSK);
1337 return;
1338 }
1339
David Blaikie83d382b2011-09-23 05:06:16 +00001340 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001341}
1342
Reid Klecknere7367d62014-10-14 20:28:40 +00001343const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1344 // If it's a class template specialization, find the template or partial
1345 // specialization from which it was instantiated.
1346 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1347 auto From = TD->getInstantiatedFrom();
1348 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1349 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1350 if (NewCTD->isMemberSpecialization())
1351 break;
1352 CTD = NewCTD;
1353 }
Richard Smith7a591a42015-07-08 02:22:15 +00001354 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001355 }
1356 if (auto *CTPSD =
1357 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1358 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1359 if (NewCTPSD->isMemberSpecialization())
1360 break;
1361 CTPSD = NewCTPSD;
1362 }
Richard Smith7a591a42015-07-08 02:22:15 +00001363 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001364 }
1365 }
1366
1367 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1368 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1369 const CXXRecordDecl *RD = this;
1370 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1371 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001372 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001373 }
1374 }
1375
1376 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1377 "couldn't find pattern for class template instantiation");
1378 return nullptr;
1379}
1380
Douglas Gregorbac74902010-07-01 14:13:13 +00001381CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1382 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001383 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001384
1385 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001386 = Context.DeclarationNames.getCXXDestructorName(
1387 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001388
Richard Smithcf4bdde2015-02-21 02:45:19 +00001389 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001390 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001391 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001392
David Blaikieff7d47a2012-12-19 00:45:41 +00001393 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001394 return Dtor;
1395}
1396
Richard Trieu95a192a2015-05-28 00:14:02 +00001397bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1398 // Destructor is noreturn.
1399 if (const CXXDestructorDecl *Destructor = getDestructor())
1400 if (Destructor->isNoReturn())
1401 return true;
1402
1403 // Check base classes destructor for noreturn.
1404 for (const auto &Base : bases())
1405 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1406 return true;
1407
1408 // Check fields for noreturn.
1409 for (const auto *Field : fields())
1410 if (const CXXRecordDecl *RD =
1411 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1412 if (RD->isAnyDestructorNoReturn())
1413 return true;
1414
1415 // All destructors are not noreturn.
1416 return false;
1417}
1418
Douglas Gregorb11aad82011-02-19 18:51:44 +00001419void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001420 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001421}
1422
1423void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1424 RecordDecl::completeDefinition();
1425
Douglas Gregor8fb95122010-09-29 00:15:42 +00001426 // If the class may be abstract (but hasn't been marked as such), check for
1427 // any pure final overriders.
1428 if (mayBeAbstract()) {
1429 CXXFinalOverriderMap MyFinalOverriders;
1430 if (!FinalOverriders) {
1431 getFinalOverriders(MyFinalOverriders);
1432 FinalOverriders = &MyFinalOverriders;
1433 }
1434
1435 bool Done = false;
1436 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1437 MEnd = FinalOverriders->end();
1438 M != MEnd && !Done; ++M) {
1439 for (OverridingMethods::iterator SO = M->second.begin(),
1440 SOEnd = M->second.end();
1441 SO != SOEnd && !Done; ++SO) {
1442 assert(SO->second.size() > 0 &&
1443 "All virtual functions have overridding virtual functions");
1444
1445 // C++ [class.abstract]p4:
1446 // A class is abstract if it contains or inherits at least one
1447 // pure virtual function for which the final overrider is pure
1448 // virtual.
1449 if (SO->second.front().Method->isPure()) {
1450 data().Abstract = true;
1451 Done = true;
1452 break;
1453 }
1454 }
1455 }
1456 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001457
1458 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001459 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001460 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001461 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001462}
1463
1464bool CXXRecordDecl::mayBeAbstract() const {
1465 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1466 isDependentContext())
1467 return false;
1468
Aaron Ballman574705e2014-03-13 15:41:46 +00001469 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001470 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001471 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001472 if (BaseDecl->isAbstract())
1473 return true;
1474 }
1475
1476 return false;
1477}
1478
David Blaikie68e081d2011-12-20 02:48:34 +00001479void CXXMethodDecl::anchor() { }
1480
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001481bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001482 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001483
1484 if (MD->getStorageClass() == SC_Static)
1485 return true;
1486
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001487 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1488 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001489}
1490
Rafael Espindola49e860b2012-06-26 17:45:31 +00001491static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1492 const CXXMethodDecl *BaseMD) {
1493 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1494 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1495 const CXXMethodDecl *MD = *I;
1496 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1497 return true;
1498 if (recursivelyOverrides(MD, BaseMD))
1499 return true;
1500 }
1501 return false;
1502}
1503
1504CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001505CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1506 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001507 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1508 return this;
1509
1510 // Lookup doesn't work for destructors, so handle them separately.
1511 if (isa<CXXDestructorDecl>(this)) {
1512 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001513 if (MD) {
1514 if (recursivelyOverrides(MD, this))
1515 return MD;
1516 if (MayBeBase && recursivelyOverrides(this, MD))
1517 return MD;
1518 }
Craig Topper36250ad2014-05-12 05:36:57 +00001519 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001520 }
1521
Richard Smith40c78062015-02-21 02:31:57 +00001522 for (auto *ND : RD->lookup(getDeclName())) {
1523 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001524 if (!MD)
1525 continue;
1526 if (recursivelyOverrides(MD, this))
1527 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001528 if (MayBeBase && recursivelyOverrides(this, MD))
1529 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001530 }
1531
Aaron Ballman574705e2014-03-13 15:41:46 +00001532 for (const auto &I : RD->bases()) {
1533 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001534 if (!RT)
1535 continue;
1536 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1537 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1538 if (T)
1539 return T;
1540 }
1541
Craig Topper36250ad2014-05-12 05:36:57 +00001542 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001543}
1544
Ted Kremenek21475702008-09-05 17:16:31 +00001545CXXMethodDecl *
1546CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001547 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001548 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001549 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001550 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001551 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001552 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1553 T, TInfo, SC, isInline, isConstexpr,
1554 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001555}
1556
Douglas Gregor72172e92012-01-05 21:55:30 +00001557CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001558 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001559 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001560 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001561}
1562
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001563bool CXXMethodDecl::isUsualDeallocationFunction() const {
1564 if (getOverloadedOperator() != OO_Delete &&
1565 getOverloadedOperator() != OO_Array_Delete)
1566 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001567
1568 // C++ [basic.stc.dynamic.deallocation]p2:
1569 // A template instance is never a usual deallocation function,
1570 // regardless of its signature.
1571 if (getPrimaryTemplate())
1572 return false;
1573
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001574 // C++ [basic.stc.dynamic.deallocation]p2:
1575 // If a class T has a member deallocation function named operator delete
1576 // with exactly one parameter, then that function is a usual (non-placement)
1577 // deallocation function. [...]
1578 if (getNumParams() == 1)
1579 return true;
Richard Smith96269c52016-09-29 22:49:46 +00001580
Daniel Jaspere9abe642016-10-10 14:13:55 +00001581 // C++ [basic.stc.dynamic.deallocation]p2:
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001582 // [...] If class T does not declare such an operator delete but does
1583 // declare a member deallocation function named operator delete with
1584 // exactly two parameters, the second of which has type std::size_t (18.1),
1585 // then this function is a usual deallocation function.
1586 ASTContext &Context = getASTContext();
Daniel Jaspere9abe642016-10-10 14:13:55 +00001587 if (getNumParams() != 2 ||
1588 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1589 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001590 return false;
1591
1592 // This function is a usual deallocation function if there are no
1593 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001594 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1595 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001596 I != E; ++I) {
1597 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001598 if (FD->getNumParams() == 1)
1599 return false;
1600 }
1601
1602 return true;
1603}
1604
Douglas Gregorb139cd52010-05-01 20:49:11 +00001605bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001606 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001607 // A user-declared copy assignment operator X::operator= is a non-static
1608 // non-template member function of class X with exactly one parameter of
1609 // type X, X&, const X&, volatile X& or const volatile X&.
1610 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1611 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001612 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1613 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001614 return false;
1615
1616 QualType ParamType = getParamDecl(0)->getType();
1617 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1618 ParamType = Ref->getPointeeType();
1619
1620 ASTContext &Context = getASTContext();
1621 QualType ClassType
1622 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1623 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1624}
1625
Alexis Huntfcaeae42011-05-25 20:50:04 +00001626bool CXXMethodDecl::isMoveAssignmentOperator() const {
1627 // C++0x [class.copy]p19:
1628 // A user-declared move assignment operator X::operator= is a non-static
1629 // non-template member function of class X with exactly one parameter of type
1630 // X&&, const X&&, volatile X&&, or const volatile X&&.
1631 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001632 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1633 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001634 return false;
1635
1636 QualType ParamType = getParamDecl(0)->getType();
1637 if (!isa<RValueReferenceType>(ParamType))
1638 return false;
1639 ParamType = ParamType->getPointeeType();
1640
1641 ASTContext &Context = getASTContext();
1642 QualType ClassType
1643 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1644 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1645}
1646
Anders Carlsson36d87e12009-05-16 23:58:37 +00001647void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001648 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001649 assert(!MD->getParent()->isDependentContext() &&
1650 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001651 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001652
Douglas Gregor832940b2010-03-02 23:58:15 +00001653 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001654}
1655
1656CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001657 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001658 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001659}
1660
1661CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001662 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001663 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001664}
1665
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001666unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001667 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001668 return getASTContext().overridden_methods_size(this);
1669}
1670
Clement Courbet6ecaec82016-07-05 07:49:31 +00001671CXXMethodDecl::overridden_method_range
1672CXXMethodDecl::overridden_methods() const {
1673 if (isa<CXXConstructorDecl>(this))
1674 return overridden_method_range(nullptr, nullptr);
1675 return getASTContext().overridden_methods(this);
1676}
1677
Ted Kremenek21475702008-09-05 17:16:31 +00001678QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001679 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1680 // If the member function is declared const, the type of this is const X*,
1681 // if the member function is declared volatile, the type of this is
1682 // volatile X*, and if the member function is declared const volatile,
1683 // the type of this is const volatile X*.
1684
Ted Kremenek21475702008-09-05 17:16:31 +00001685 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001686
John McCalle78aac42010-03-10 03:28:59 +00001687 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001688 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00001689 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001690 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001691}
1692
Eli Friedman71a26d82009-12-06 20:50:05 +00001693bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001694 // If this function is a template instantiation, look at the template from
1695 // which it was instantiated.
1696 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1697 if (!CheckFn)
1698 CheckFn = this;
1699
Eli Friedman71a26d82009-12-06 20:50:05 +00001700 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001701 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001702}
1703
Douglas Gregor355efbb2012-02-17 03:02:34 +00001704bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001705 const CXXRecordDecl *P = getParent();
1706 if (P->isLambda()) {
1707 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1708 if (StaticInvoker == this) return true;
1709 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1710 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1711 }
1712 }
1713 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001714}
1715
Alexis Hunt1d792652011-01-08 20:30:50 +00001716CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1717 TypeSourceInfo *TInfo, bool IsVirtual,
1718 SourceLocation L, Expr *Init,
1719 SourceLocation R,
1720 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001721 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001722 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1723 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001724{
Douglas Gregore8381c02008-11-05 04:29:56 +00001725}
1726
Alexis Hunt1d792652011-01-08 20:30:50 +00001727CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1728 FieldDecl *Member,
1729 SourceLocation MemberLoc,
1730 SourceLocation L, Expr *Init,
1731 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001732 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001733 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001734 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1735{
1736}
1737
Alexis Hunt1d792652011-01-08 20:30:50 +00001738CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1739 IndirectFieldDecl *Member,
1740 SourceLocation MemberLoc,
1741 SourceLocation L, Expr *Init,
1742 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001743 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001744 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001745 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001746{
Douglas Gregore8381c02008-11-05 04:29:56 +00001747}
1748
Alexis Hunt1d792652011-01-08 20:30:50 +00001749CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001750 TypeSourceInfo *TInfo,
1751 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001752 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001753 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1754 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001755 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1756{
1757}
1758
1759CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001760 FieldDecl *Member,
1761 SourceLocation MemberLoc,
1762 SourceLocation L, Expr *Init,
1763 SourceLocation R,
1764 VarDecl **Indices,
1765 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001766 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001767 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001768 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001769{
James Y Knight967eb202015-12-29 22:13:13 +00001770 std::uninitialized_copy(Indices, Indices + NumIndices,
1771 getTrailingObjects<VarDecl *>());
Douglas Gregor94f9a482010-05-05 05:51:00 +00001772}
1773
Alexis Hunt1d792652011-01-08 20:30:50 +00001774CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1775 FieldDecl *Member,
1776 SourceLocation MemberLoc,
1777 SourceLocation L, Expr *Init,
1778 SourceLocation R,
1779 VarDecl **Indices,
1780 unsigned NumIndices) {
James Y Knight967eb202015-12-29 22:13:13 +00001781 void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
Alexis Hunt1d792652011-01-08 20:30:50 +00001782 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001783 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1784 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001785}
1786
Alexis Hunt1d792652011-01-08 20:30:50 +00001787TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001788 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001789 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001790 else
1791 return TypeLoc();
1792}
1793
Alexis Hunt1d792652011-01-08 20:30:50 +00001794const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001795 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001796 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001797 else
Craig Topper36250ad2014-05-12 05:36:57 +00001798 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001799}
1800
Alexis Hunt1d792652011-01-08 20:30:50 +00001801SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001802 if (isInClassMemberInitializer())
1803 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001804
David Blaikiea81d4102015-01-18 00:12:58 +00001805 if (isAnyMemberInitializer())
1806 return getMemberLocation();
1807
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001808 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1809 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1810
1811 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001812}
1813
Alexis Hunt1d792652011-01-08 20:30:50 +00001814SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001815 if (isInClassMemberInitializer()) {
1816 FieldDecl *D = getAnyMember();
1817 if (Expr *I = D->getInClassInitializer())
1818 return I->getSourceRange();
1819 return SourceRange();
1820 }
1821
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001822 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001823}
1824
David Blaikie68e081d2011-12-20 02:48:34 +00001825void CXXConstructorDecl::anchor() { }
1826
Richard Smith5179eb72016-06-28 19:03:57 +00001827CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
1828 unsigned ID,
1829 bool Inherited) {
1830 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
1831 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
1832 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
1833 false, false, false, false, InheritedConstructor());
1834 Result->IsInheritingConstructor = Inherited;
1835 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00001836}
1837
1838CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001839CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001840 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001841 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001842 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001843 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00001844 bool isImplicitlyDeclared, bool isConstexpr,
1845 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001846 assert(NameInfo.getName().getNameKind()
1847 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001848 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00001849 unsigned Extra =
1850 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
1851 return new (C, RD, Extra) CXXConstructorDecl(
1852 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
1853 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00001854}
1855
Richard Smithc2bb8182015-03-24 06:36:48 +00001856CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1857 return CtorInitializers.get(getASTContext().getExternalSource());
1858}
1859
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001860CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1861 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1862 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1863 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1864 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001865
1866 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001867}
1868
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001869bool CXXConstructorDecl::isDefaultConstructor() const {
1870 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001871 // A default constructor for a class X is a constructor of class
1872 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001873 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001874 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001875}
1876
Mike Stump11289f42009-09-09 15:08:12 +00001877bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001878CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001879 return isCopyOrMoveConstructor(TypeQuals) &&
1880 getParamDecl(0)->getType()->isLValueReferenceType();
1881}
1882
1883bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1884 return isCopyOrMoveConstructor(TypeQuals) &&
1885 getParamDecl(0)->getType()->isRValueReferenceType();
1886}
1887
1888/// \brief Determine whether this is a copy or move constructor.
1889bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001890 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001891 // A non-template constructor for class X is a copy constructor
1892 // if its first parameter is of type X&, const X&, volatile X& or
1893 // const volatile X&, and either there are no other parameters
1894 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001895 // C++0x [class.copy]p3:
1896 // A non-template constructor for class X is a move constructor if its
1897 // first parameter is of type X&&, const X&&, volatile X&&, or
1898 // const volatile X&&, and either there are no other parameters or else
1899 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001900 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001901 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001902 (getPrimaryTemplate() != nullptr) ||
1903 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001904 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001905
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001906 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001907
1908 // Do we have a reference type?
1909 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001910 if (!ParamRefType)
1911 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001912
Douglas Gregorff7028a2009-11-13 23:59:09 +00001913 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001914 ASTContext &Context = getASTContext();
1915
Douglas Gregorff7028a2009-11-13 23:59:09 +00001916 CanQualType PointeeType
1917 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001918 CanQualType ClassTy
1919 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001920 if (PointeeType.getUnqualifiedType() != ClassTy)
1921 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001922
John McCall8ccfcb52009-09-24 19:53:00 +00001923 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001924
1925 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001926 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001927 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001928}
1929
Anders Carlssond20e7952009-08-28 16:57:08 +00001930bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001931 // C++ [class.conv.ctor]p1:
1932 // A constructor declared without the function-specifier explicit
1933 // that can be called with a single parameter specifies a
1934 // conversion from the type of its first parameter to the type of
1935 // its class. Such a constructor is called a converting
1936 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001937 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001938 return false;
1939
Mike Stump11289f42009-09-09 15:08:12 +00001940 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001941 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001942 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001943 (getNumParams() > 1 &&
1944 (getParamDecl(1)->hasDefaultArg() ||
1945 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001946}
Douglas Gregor61956c42008-10-31 09:07:45 +00001947
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001948bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001949 if ((getNumParams() < 1) ||
1950 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001951 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001952 return false;
1953
1954 const ParmVarDecl *Param = getParamDecl(0);
1955
1956 ASTContext &Context = getASTContext();
1957 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1958
Douglas Gregorffe14e32009-11-14 01:20:54 +00001959 // Is it the same as our our class type?
1960 CanQualType ClassTy
1961 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1962 if (ParamType.getUnqualifiedType() != ClassTy)
1963 return false;
1964
1965 return true;
1966}
1967
David Blaikie68e081d2011-12-20 02:48:34 +00001968void CXXDestructorDecl::anchor() { }
1969
Douglas Gregor831c93f2008-11-05 20:51:48 +00001970CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001971CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001972 return new (C, ID)
1973 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1974 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001975}
1976
1977CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001978CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001979 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001980 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001981 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001982 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001983 assert(NameInfo.getName().getNameKind()
1984 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001985 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001986 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001987 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001988}
1989
Richard Smithf8134002015-03-10 01:41:22 +00001990void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1991 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1992 if (OD && !First->OperatorDelete) {
1993 First->OperatorDelete = OD;
1994 if (auto *L = getASTMutationListener())
1995 L->ResolvedOperatorDelete(First, OD);
1996 }
1997}
1998
David Blaikie68e081d2011-12-20 02:48:34 +00001999void CXXConversionDecl::anchor() { }
2000
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002001CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002002CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002003 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002004 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002005 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002006 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002007}
2008
2009CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002010CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002011 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002012 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002013 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002014 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002015 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002016 assert(NameInfo.getName().getNameKind()
2017 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002018 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002019 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002020 isInline, isExplicit, isConstexpr,
2021 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002022}
2023
Douglas Gregord3b672c2012-02-16 01:06:16 +00002024bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2025 return isImplicit() && getParent()->isLambda() &&
2026 getConversionType()->isBlockPointerType();
2027}
2028
David Blaikie68e081d2011-12-20 02:48:34 +00002029void LinkageSpecDecl::anchor() { }
2030
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002031LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002032 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002033 SourceLocation ExternLoc,
2034 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002035 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002036 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002037 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002038}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002039
Richard Smithf7981722013-11-22 09:01:48 +00002040LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2041 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002042 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2043 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002044}
2045
David Blaikie68e081d2011-12-20 02:48:34 +00002046void UsingDirectiveDecl::anchor() { }
2047
Douglas Gregor889ceb72009-02-03 19:21:40 +00002048UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2049 SourceLocation L,
2050 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002051 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002052 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002053 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002054 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002055 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2056 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002057 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2058 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002059}
2060
Richard Smithf7981722013-11-22 09:01:48 +00002061UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2062 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002063 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2064 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002065 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002066 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002067}
2068
Sebastian Redla6602e92009-11-23 15:34:23 +00002069NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2070 if (NamespaceAliasDecl *NA =
2071 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2072 return NA->getNamespace();
2073 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2074}
2075
Richard Smith053f6c62014-05-16 23:01:30 +00002076NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2077 SourceLocation StartLoc, SourceLocation IdLoc,
2078 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2079 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2080 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2081 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002082 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002083
Douglas Gregore57e7522012-01-07 09:11:48 +00002084 if (PrevDecl)
2085 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2086}
2087
Douglas Gregor72172e92012-01-05 21:55:30 +00002088NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002089 bool Inline, SourceLocation StartLoc,
2090 SourceLocation IdLoc, IdentifierInfo *Id,
2091 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002092 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2093 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002094}
2095
2096NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002097 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002098 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002099}
2100
Chandler Carruth21c90602015-12-30 03:24:14 +00002101NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2102 if (isFirstDecl())
2103 return this;
2104
2105 return AnonOrFirstNamespaceAndInline.getPointer();
2106}
2107
2108const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2109 if (isFirstDecl())
2110 return this;
2111
2112 return AnonOrFirstNamespaceAndInline.getPointer();
2113}
2114
2115bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2116
Richard Smithd7af8a32014-05-10 01:17:36 +00002117NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2118 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002119}
2120NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2121 return getPreviousDecl();
2122}
2123NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2124 return getMostRecentDecl();
2125}
2126
David Blaikie68e081d2011-12-20 02:48:34 +00002127void NamespaceAliasDecl::anchor() { }
2128
Richard Smithf4634362014-09-03 23:11:22 +00002129NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2130 return getNextRedeclaration();
2131}
2132NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2133 return getPreviousDecl();
2134}
2135NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2136 return getMostRecentDecl();
2137}
2138
Mike Stump11289f42009-09-09 15:08:12 +00002139NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002140 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002141 SourceLocation AliasLoc,
2142 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002143 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002144 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002145 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002146 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002147 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2148 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002149 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002150 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002151}
2152
Douglas Gregor72172e92012-01-05 21:55:30 +00002153NamespaceAliasDecl *
2154NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002155 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002156 SourceLocation(), nullptr,
2157 NestedNameSpecifierLoc(),
2158 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002159}
2160
David Blaikie68e081d2011-12-20 02:48:34 +00002161void UsingShadowDecl::anchor() { }
2162
Richard Smith5179eb72016-06-28 19:03:57 +00002163UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2164 SourceLocation Loc, UsingDecl *Using,
2165 NamedDecl *Target)
2166 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
2167 redeclarable_base(C), Underlying(Target),
2168 UsingOrNextShadow(cast<NamedDecl>(Using)) {
2169 if (Target)
2170 IdentifierNamespace = Target->getIdentifierNamespace();
2171 setImplicit();
2172}
2173
2174UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2175 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
2176 redeclarable_base(C), Underlying(), UsingOrNextShadow() {}
2177
Douglas Gregor72172e92012-01-05 21:55:30 +00002178UsingShadowDecl *
2179UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002180 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002181}
2182
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002183UsingDecl *UsingShadowDecl::getUsingDecl() const {
2184 const UsingShadowDecl *Shadow = this;
2185 while (const UsingShadowDecl *NextShadow =
2186 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2187 Shadow = NextShadow;
2188 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2189}
2190
Richard Smith5179eb72016-06-28 19:03:57 +00002191void ConstructorUsingShadowDecl::anchor() { }
2192
2193ConstructorUsingShadowDecl *
2194ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2195 SourceLocation Loc, UsingDecl *Using,
2196 NamedDecl *Target, bool IsVirtual) {
2197 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2198 IsVirtual);
2199}
2200
2201ConstructorUsingShadowDecl *
2202ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2203 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2204}
2205
2206CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2207 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2208}
2209
David Blaikie68e081d2011-12-20 02:48:34 +00002210void UsingDecl::anchor() { }
2211
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002212void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2213 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2214 "declaration already in set");
2215 assert(S->getUsingDecl() == this);
2216
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002217 if (FirstUsingShadow.getPointer())
2218 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2219 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002220}
2221
2222void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2223 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2224 "declaration not in set");
2225 assert(S->getUsingDecl() == this);
2226
2227 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2228
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002229 if (FirstUsingShadow.getPointer() == S) {
2230 FirstUsingShadow.setPointer(
2231 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002232 S->UsingOrNextShadow = this;
2233 return;
2234 }
2235
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002236 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002237 while (Prev->UsingOrNextShadow != S)
2238 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2239 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2240 S->UsingOrNextShadow = this;
2241}
2242
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002243UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2244 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002245 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002246 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002247 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002248}
2249
Douglas Gregor72172e92012-01-05 21:55:30 +00002250UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002251 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2252 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2253 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002254}
2255
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002256SourceRange UsingDecl::getSourceRange() const {
2257 SourceLocation Begin = isAccessDeclaration()
2258 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2259 return SourceRange(Begin, getNameInfo().getEndLoc());
2260}
2261
David Blaikie68e081d2011-12-20 02:48:34 +00002262void UnresolvedUsingValueDecl::anchor() { }
2263
John McCalle61f2ba2009-11-18 02:36:19 +00002264UnresolvedUsingValueDecl *
2265UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2266 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002267 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002268 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002269 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2270 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002271}
2272
Douglas Gregor72172e92012-01-05 21:55:30 +00002273UnresolvedUsingValueDecl *
2274UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002275 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2276 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002277 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002278 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002279}
2280
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002281SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2282 SourceLocation Begin = isAccessDeclaration()
2283 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2284 return SourceRange(Begin, getNameInfo().getEndLoc());
2285}
2286
David Blaikie68e081d2011-12-20 02:48:34 +00002287void UnresolvedUsingTypenameDecl::anchor() { }
2288
John McCalle61f2ba2009-11-18 02:36:19 +00002289UnresolvedUsingTypenameDecl *
2290UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2291 SourceLocation UsingLoc,
2292 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002293 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002294 SourceLocation TargetNameLoc,
2295 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002296 return new (C, DC) UnresolvedUsingTypenameDecl(
2297 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2298 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002299}
2300
Douglas Gregor72172e92012-01-05 21:55:30 +00002301UnresolvedUsingTypenameDecl *
2302UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002303 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002304 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2305 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002306}
2307
David Blaikie68e081d2011-12-20 02:48:34 +00002308void StaticAssertDecl::anchor() { }
2309
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002310StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002311 SourceLocation StaticAssertLoc,
2312 Expr *AssertExpr,
2313 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002314 SourceLocation RParenLoc,
2315 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002316 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2317 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002318}
2319
Richard Smithf7981722013-11-22 09:01:48 +00002320StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002321 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002322 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2323 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002324}
2325
Richard Smithbdb84f32016-07-22 23:36:59 +00002326void BindingDecl::anchor() {}
2327
2328BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2329 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002330 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002331}
2332
2333BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002334 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002335}
2336
Richard Smith97fcf4b2016-08-14 23:15:52 +00002337VarDecl *BindingDecl::getHoldingVar() const {
2338 Expr *B = getBinding();
2339 if (!B)
2340 return nullptr;
2341 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2342 if (!DRE)
2343 return nullptr;
2344
2345 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2346 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2347 return VD;
2348}
2349
Richard Smithbdb84f32016-07-22 23:36:59 +00002350void DecompositionDecl::anchor() {}
2351
2352DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2353 SourceLocation StartLoc,
2354 SourceLocation LSquareLoc,
2355 QualType T, TypeSourceInfo *TInfo,
2356 StorageClass SC,
2357 ArrayRef<BindingDecl *> Bindings) {
2358 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2359 return new (C, DC, Extra)
2360 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2361}
2362
2363DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2364 unsigned ID,
2365 unsigned NumBindings) {
2366 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002367 auto *Result = new (C, ID, Extra)
2368 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2369 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002370 // Set up and clean out the bindings array.
2371 Result->NumBindings = NumBindings;
2372 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2373 for (unsigned I = 0; I != NumBindings; ++I)
2374 new (Trail + I) BindingDecl*(nullptr);
2375 return Result;
2376}
2377
Richard Smith7873de02016-08-11 22:25:46 +00002378void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2379 os << '[';
2380 bool Comma = false;
2381 for (auto *B : bindings()) {
2382 if (Comma)
2383 os << ", ";
2384 B->printName(os);
2385 Comma = true;
2386 }
2387 os << ']';
2388}
2389
Richard Smithf7981722013-11-22 09:01:48 +00002390MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2391 SourceLocation L, DeclarationName N,
2392 QualType T, TypeSourceInfo *TInfo,
2393 SourceLocation StartL,
2394 IdentifierInfo *Getter,
2395 IdentifierInfo *Setter) {
2396 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2397}
2398
2399MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2400 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002401 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2402 DeclarationName(), QualType(), nullptr,
2403 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002404}
2405
Anders Carlsson6750d162009-03-26 23:46:50 +00002406static const char *getAccessName(AccessSpecifier AS) {
2407 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002408 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002409 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002410 case AS_public:
2411 return "public";
2412 case AS_private:
2413 return "private";
2414 case AS_protected:
2415 return "protected";
2416 }
David Blaikief47fa302012-01-17 02:30:50 +00002417 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002418}
2419
2420const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2421 AccessSpecifier AS) {
2422 return DB << getAccessName(AS);
2423}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002424
2425const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2426 AccessSpecifier AS) {
2427 return DB << getAccessName(AS);
2428}