blob: 19ffe1a720b375454c5eaa744665926a957c7fc2 [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),
Richard Trieub6adf542017-02-18 02:09:28 +000074 IsParsingBaseSpecifiers(false), ODRHash(0), NumBases(0), NumVBases(0),
75 Bases(), 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
Richard Trieub6adf542017-02-18 02:09:28 +0000374void CXXRecordDecl::computeODRHash() {}
375
Richard Smith6b02d462012-12-08 08:32:28 +0000376void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
377 // C++11 [class.copy]p11:
378 // A defaulted copy/move constructor for a class X is defined as
379 // deleted if X has:
380 // -- a direct or virtual base class B that cannot be copied/moved [...]
381 // -- a non-static data member of class type M (or array thereof)
382 // that cannot be copied or moved [...]
383 if (!Subobj->hasSimpleMoveConstructor())
384 data().NeedOverloadResolutionForMoveConstructor = true;
385
386 // C++11 [class.copy]p23:
387 // A defaulted copy/move assignment operator for a class X is defined as
388 // deleted if X has:
389 // -- a direct or virtual base class B that cannot be copied/moved [...]
390 // -- a non-static data member of class type M (or array thereof)
391 // that cannot be copied or moved [...]
392 if (!Subobj->hasSimpleMoveAssignment())
393 data().NeedOverloadResolutionForMoveAssignment = true;
394
395 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
396 // A defaulted [ctor or dtor] for a class X is defined as
397 // deleted if X has:
398 // -- any direct or virtual base class [...] has a type with a destructor
399 // that is deleted or inaccessible from the defaulted [ctor or dtor].
400 // -- any non-static data member has a type with a destructor
401 // that is deleted or inaccessible from the defaulted [ctor or dtor].
402 if (!Subobj->hasSimpleDestructor()) {
403 data().NeedOverloadResolutionForMoveConstructor = true;
404 data().NeedOverloadResolutionForDestructor = true;
405 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000406}
407
Douglas Gregord2e6a452010-01-14 17:47:39 +0000408bool CXXRecordDecl::hasAnyDependentBases() const {
409 if (!isDependentContext())
410 return false;
411
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000412 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000413}
414
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000415bool CXXRecordDecl::isTriviallyCopyable() const {
416 // C++0x [class]p5:
417 // A trivially copyable class is a class that:
418 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000419 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000420 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000421 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000422 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000423 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000424 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000425 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000426 // -- has a trivial destructor.
427 if (!hasTrivialDestructor()) return false;
428
429 return true;
430}
431
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000432void CXXRecordDecl::markedVirtualFunctionPure() {
433 // C++ [class.abstract]p2:
434 // A class is abstract if it has at least one pure virtual function.
435 data().Abstract = true;
436}
437
438void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000439 if (!D->isImplicit() &&
440 !isa<FieldDecl>(D) &&
441 !isa<IndirectFieldDecl>(D) &&
442 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
443 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
444 data().HasOnlyCMembers = false;
445
446 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000447 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000448 return;
449
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000450 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
451 if (FunTmpl)
452 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000453
454 // FIXME: Pass NamedDecl* to addedMember?
455 Decl *DUnderlying = D;
456 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
457 DUnderlying = ND->getUnderlyingDecl();
458 if (FunctionTemplateDecl *UnderlyingFunTmpl =
459 dyn_cast<FunctionTemplateDecl>(DUnderlying))
460 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
461 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000462
Douglas Gregora832d3e2010-09-28 19:45:33 +0000463 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
464 if (Method->isVirtual()) {
465 // C++ [dcl.init.aggr]p1:
466 // An aggregate is an array or a class with [...] no virtual functions.
467 data().Aggregate = false;
468
469 // C++ [class]p4:
470 // A POD-struct is an aggregate class...
471 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000472
David Majnemer5cfda6f2016-05-22 05:34:26 +0000473 // C++14 [meta.unary.prop]p4:
474 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000475 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000476
477 // C++ [class.virtual]p1:
478 // A class that declares or inherits a virtual function is called a
479 // polymorphic class.
480 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000481
Richard Smith328aae52012-11-30 05:11:39 +0000482 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
483 // A [default constructor, copy/move constructor, or copy/move
484 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000485 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000486 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000487
Chandler Carruthb1963742011-04-30 09:17:45 +0000488 // C++0x [class]p7:
489 // A standard-layout class is a class that: [...]
490 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000491 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000492 }
493 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000494
Richard Smith1c33fe82012-11-28 06:23:12 +0000495 // Notify the listener if an implicit member was added after the definition
496 // was completed.
497 if (!isBeingDefined() && D->isImplicit())
498 if (ASTMutationListener *L = getASTMutationListener())
499 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000500
Richard Smith328aae52012-11-30 05:11:39 +0000501 // The kind of special member this declaration is, if any.
502 unsigned SMKind = 0;
503
Richard Smith1c33fe82012-11-28 06:23:12 +0000504 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000505 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000506 if (!Constructor->isImplicit()) {
507 // Note that we have a user-declared constructor.
508 data().UserDeclaredConstructor = true;
509
510 // C++ [class]p4:
511 // A POD-struct is an aggregate class [...]
512 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
513 // type is technically an aggregate in C++0x since it wouldn't be in 03.
514 data().PlainOldData = false;
515 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000516
Alexis Hunt88c75c32011-05-09 21:45:35 +0000517 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000518 SMKind |= SMF_DefaultConstructor;
519
Richard Smith5179eb72016-06-28 19:03:57 +0000520 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000521 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000522 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000523 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000524 if (Constructor->isDefaulted())
525 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000526 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000527
Chandler Carruthad7d4042011-04-23 23:10:33 +0000528 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000529 unsigned Quals;
530 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000531 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000532
533 if (Quals & Qualifiers::Const)
534 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000535 } else if (Constructor->isMoveConstructor())
536 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000537 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000538
Eric Fiselier283d8d42016-12-03 01:26:47 +0000539 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith505ef812016-12-21 01:57:02 +0000540 // An aggregate is an array or a class with no user-provided, explicit, or
541 // inherited constructors
542 if (Constructor->isUserProvided() || Constructor->isExplicit())
Eric Fiselier283d8d42016-12-03 01:26:47 +0000543 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000544 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000545
Richard Smith5179eb72016-06-28 19:03:57 +0000546 // Handle constructors, including those inherited from base classes.
547 if (CXXConstructorDecl *Constructor =
548 dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000549 // Record if we see any constexpr constructors which are neither copy
550 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000551 // C++1z [basic.types]p10:
552 // [...] has at least one constexpr constructor or constructor template
553 // (possibly inherited from a base class) that is not a copy or move
554 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000555 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000556 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000557 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000558
Richard Smith1c33fe82012-11-28 06:23:12 +0000559 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000560 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000561 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000562
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000563 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000564 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000565 // If the destructor is explicitly defaulted and not trivial or not public
566 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
567 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000568
Richard Smith1c33fe82012-11-28 06:23:12 +0000569 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000570 // A destructor is trivial if [...] the destructor is not virtual.
571 if (DD->isVirtual())
572 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000573 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000574
575 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000576 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000577 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000578 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000579
580 const ReferenceType *ParamTy =
581 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
582 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
583 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000584 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000585
Richard Smith328aae52012-11-30 05:11:39 +0000586 if (Method->isMoveAssignmentOperator())
587 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000588
Douglas Gregor457104e2010-09-29 04:25:11 +0000589 // Keep the list of conversion functions up-to-date.
590 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000591 // FIXME: We use the 'unsafe' accessor for the access specifier here,
592 // because Sema may not have set it yet. That's really just a misdesign
593 // in Sema. However, LLDB *will* have set the access specifier correctly,
594 // and adds declarations after the class is technically completed,
595 // so completeDefinition()'s overriding of the access specifiers doesn't
596 // work.
597 AccessSpecifier AS = Conversion->getAccessUnsafe();
598
Richard Smith328aae52012-11-30 05:11:39 +0000599 if (Conversion->getPrimaryTemplate()) {
600 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000601 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000602 ASTContext &Ctx = getASTContext();
603 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
604 NamedDecl *Primary =
605 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
606 if (Primary->getPreviousDecl())
607 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
608 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000609 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000610 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000611 }
612 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000613
Richard Smith328aae52012-11-30 05:11:39 +0000614 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000615 // If this is the first declaration of a special member, we no longer have
616 // an implicit trivial special member.
617 data().HasTrivialSpecialMembers &=
618 data().DeclaredSpecialMembers | ~SMKind;
619
620 if (!Method->isImplicit() && !Method->isUserProvided()) {
621 // This method is user-declared but not user-provided. We can't work out
622 // whether it's trivial yet (not until we get to the end of the class).
623 // We'll handle this method in finishedDefaultedOrDeletedMember.
624 } else if (Method->isTrivial())
625 data().HasTrivialSpecialMembers |= SMKind;
626 else
627 data().DeclaredNonTrivialSpecialMembers |= SMKind;
628
Richard Smith328aae52012-11-30 05:11:39 +0000629 // Note when we have declared a declared special member, and suppress the
630 // implicit declaration of this special member.
631 data().DeclaredSpecialMembers |= SMKind;
632
633 if (!Method->isImplicit()) {
634 data().UserDeclaredSpecialMembers |= SMKind;
635
636 // C++03 [class]p4:
637 // A POD-struct is an aggregate class that has [...] no user-defined
638 // copy assignment operator and no user-defined destructor.
639 //
640 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
641 // aggregates could not have any constructors, clear it even for an
642 // explicitly defaulted or deleted constructor.
643 // type is technically an aggregate in C++0x since it wouldn't be in 03.
644 //
645 // Also, a user-declared move assignment operator makes a class non-POD.
646 // This is an extension in C++03.
647 data().PlainOldData = false;
648 }
Richard Smith328aae52012-11-30 05:11:39 +0000649 }
650
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000651 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000652 }
Richard Smith328aae52012-11-30 05:11:39 +0000653
Douglas Gregora832d3e2010-09-28 19:45:33 +0000654 // Handle non-static data members.
655 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000656 // C++ [class.bit]p2:
657 // A declaration for a bit-field that omits the identifier declares an
658 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
659 // initialized.
660 if (Field->isUnnamedBitfield())
661 return;
662
Douglas Gregora832d3e2010-09-28 19:45:33 +0000663 // C++ [dcl.init.aggr]p1:
664 // An aggregate is an array or a class (clause 9) with [...] no
665 // private or protected non-static data members (clause 11).
666 //
667 // A POD must be an aggregate.
668 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
669 data().Aggregate = false;
670 data().PlainOldData = false;
671 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000672
673 // C++0x [class]p7:
674 // A standard-layout class is a class that:
675 // [...]
676 // -- has the same access control for all non-static data members,
677 switch (D->getAccess()) {
678 case AS_private: data().HasPrivateFields = true; break;
679 case AS_protected: data().HasProtectedFields = true; break;
680 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000681 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000682 };
683 if ((data().HasPrivateFields + data().HasProtectedFields +
684 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000685 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000686
Douglas Gregor61226d32011-05-13 01:05:07 +0000687 // Keep track of the presence of mutable fields.
688 if (Field->isMutable())
689 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000690
691 // C++11 [class.union]p8, DR1460:
692 // If X is a union, a non-static data member of X that is not an anonymous
693 // union is a variant member of X.
694 if (isUnion() && !Field->isAnonymousStructOrUnion())
695 data().HasVariantMembers = true;
696
Chandler Carruthad7d4042011-04-23 23:10:33 +0000697 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000698 // A POD struct is a class that is both a trivial class and a
699 // standard-layout class, and has no non-static data members of type
700 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000701 //
702 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
703 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000704 ASTContext &Context = getASTContext();
705 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000706 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000707 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000708 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000709 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
710 // Objective-C Automatic Reference Counting:
711 // If a class has a non-static data member of Objective-C pointer
712 // type (or array thereof), it is a non-POD type and its
713 // default constructor (if any), copy constructor, move constructor,
714 // copy assignment operator, move assignment operator, and destructor are
715 // non-trivial.
716 setHasObjectMember(true);
717 struct DefinitionData &Data = data();
718 Data.PlainOldData = false;
719 Data.HasTrivialSpecialMembers = 0;
720 Data.HasIrrelevantDestructor = false;
721 }
Eli Friedmana5433322013-07-20 01:06:31 +0000722 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000723 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000724
Chandler Carruthb1963742011-04-30 09:17:45 +0000725 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000726 if (!Field->hasInClassInitializer())
727 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000728
Chandler Carruthb1963742011-04-30 09:17:45 +0000729 // C++0x [class]p7:
730 // A standard-layout class is a class that:
731 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000732 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000733 }
734
Nico Weber6a6376b2016-02-19 01:52:46 +0000735 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +0000736 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000737 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000738 data().HasUninitializedFields = true;
739 } else {
740 data().HasUninitializedFields = true;
741 }
742 }
743
Richard Smith3607ffe2012-02-13 03:54:03 +0000744 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000745 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000746 data().HasNonLiteralTypeFieldsOrBases = true;
747
Richard Smithab44d5b2013-12-10 08:25:00 +0000748 if (Field->hasInClassInitializer() ||
749 (Field->isAnonymousStructOrUnion() &&
750 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000751 data().HasInClassInitializer = true;
752
753 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000754 // A default constructor is trivial if [...] no non-static data member
755 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000756 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000757
Richard Smithe2648ba2012-05-07 01:07:30 +0000758 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000759 // An aggregate is a [...] class with [...] no
760 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000761 //
Richard Smith872307e2016-03-08 22:17:41 +0000762 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000763 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000764 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000765
Richard Smithe2648ba2012-05-07 01:07:30 +0000766 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000767 // A POD struct is [...] a trivial class.
768 data().PlainOldData = false;
769 }
770
Richard Smith6b02d462012-12-08 08:32:28 +0000771 // C++11 [class.copy]p23:
772 // A defaulted copy/move assignment operator for a class X is defined
773 // as deleted if X has:
774 // -- a non-static data member of reference type
775 if (T->isReferenceType())
776 data().DefaultedMoveAssignmentIsDeleted = true;
777
Douglas Gregor11c024b2010-09-28 20:50:54 +0000778 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
779 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
780 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000781 addedClassSubobject(FieldRec);
782
Richard Smithc91d12c2013-11-25 07:07:05 +0000783 // We may need to perform overload resolution to determine whether a
784 // field can be moved if it's const or volatile qualified.
785 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
786 data().NeedOverloadResolutionForMoveConstructor = true;
787 data().NeedOverloadResolutionForMoveAssignment = true;
788 }
789
Richard Smith6b02d462012-12-08 08:32:28 +0000790 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
791 // A defaulted [special member] for a class X is defined as
792 // deleted if:
793 // -- X is a union-like class that has a variant member with a
794 // non-trivial [corresponding special member]
795 if (isUnion()) {
796 if (FieldRec->hasNonTrivialMoveConstructor())
797 data().DefaultedMoveConstructorIsDeleted = true;
798 if (FieldRec->hasNonTrivialMoveAssignment())
799 data().DefaultedMoveAssignmentIsDeleted = true;
800 if (FieldRec->hasNonTrivialDestructor())
801 data().DefaultedDestructorIsDeleted = true;
802 }
803
Richard Smithdd5619f2016-08-16 00:13:47 +0000804 // For an anonymous union member, our overload resolution will perform
805 // overload resolution for its members.
806 if (Field->isAnonymousStructOrUnion()) {
807 data().NeedOverloadResolutionForMoveConstructor |=
808 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
809 data().NeedOverloadResolutionForMoveAssignment |=
810 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
811 data().NeedOverloadResolutionForDestructor |=
812 FieldRec->data().NeedOverloadResolutionForDestructor;
813 }
814
Alexis Huntf479f1b2011-05-09 18:22:59 +0000815 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000816 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000817 // -- for all the non-static data members of its class that are of
818 // class type (or array thereof), each such class has a trivial
819 // default constructor.
820 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000821 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000822
823 // C++0x [class.copy]p13:
824 // A copy/move constructor for class X is trivial if [...]
825 // [...]
826 // -- for each non-static data member of X that is of class type (or
827 // an array thereof), the constructor selected to copy/move that
828 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000829 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000830 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000831 // If the field doesn't have a simple move constructor, we'll eagerly
832 // declare the move constructor for this class and we'll decide whether
833 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000834 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000835 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000836
837 // C++0x [class.copy]p27:
838 // A copy/move assignment operator for class X is trivial if [...]
839 // [...]
840 // -- for each non-static data member of X that is of class type (or
841 // an array thereof), the assignment operator selected to
842 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000843 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000844 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000845 // If the field doesn't have a simple move assignment, we'll eagerly
846 // declare the move assignment for this class and we'll decide whether
847 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000848 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000849 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000850
Douglas Gregor11c024b2010-09-28 20:50:54 +0000851 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000852 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000853 if (!FieldRec->hasIrrelevantDestructor())
854 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000855 if (FieldRec->hasObjectMember())
856 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000857 if (FieldRec->hasVolatileMember())
858 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000859
860 // C++0x [class]p7:
861 // A standard-layout class is a class that:
862 // -- has no non-static data members of type non-standard-layout
863 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000864 if (!FieldRec->isStandardLayout())
865 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000866
867 // C++0x [class]p7:
868 // A standard-layout class is a class that:
869 // [...]
870 // -- has no base classes of the same type as the first non-static
871 // data member.
872 // We don't want to expend bits in the state of the record decl
873 // tracking whether this is the first non-static data member so we
874 // cheat a bit and use some of the existing state: the empty bit.
875 // Virtual bases and virtual methods make a class non-empty, but they
876 // also make it non-standard-layout so we needn't check here.
877 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000878 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000879 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000880 // data member must come through here with Empty still true, and Empty
881 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000882 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000883 for (const auto &BI : bases()) {
884 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000885 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000886 break;
887 }
888 }
889 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000890
891 // Keep track of the presence of mutable fields.
892 if (FieldRec->hasMutableFields())
893 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000894
895 // C++11 [class.copy]p13:
896 // If the implicitly-defined constructor would satisfy the
897 // requirements of a constexpr constructor, the implicitly-defined
898 // constructor is constexpr.
899 // C++11 [dcl.constexpr]p4:
900 // -- every constructor involved in initializing non-static data
901 // members [...] shall be a constexpr constructor
902 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000903 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000904 // The standard requires any in-class initializer to be a constant
905 // expression. We consider this to be a defect.
906 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000907
908 // C++11 [class.copy]p8:
909 // The implicitly-declared copy constructor for a class X will have
910 // the form 'X::X(const X&)' if [...] for all the non-static data
911 // members of X that are of a class type M (or array thereof), each
912 // such class type has a copy constructor whose first parameter is
913 // of type 'const M&' or 'const volatile M&'.
914 if (!FieldRec->hasCopyConstructorWithConstParam())
915 data().ImplicitCopyConstructorHasConstParam = false;
916
917 // C++11 [class.copy]p18:
918 // The implicitly-declared copy assignment oeprator for a class X will
919 // have the form 'X& X::operator=(const X&)' if [...] for all the
920 // non-static data members of X that are of a class type M (or array
921 // thereof), each such class type has a copy assignment operator whose
922 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
923 if (!FieldRec->hasCopyAssignmentWithConstParam())
924 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000925
926 if (FieldRec->hasUninitializedReferenceMember() &&
927 !Field->hasInClassInitializer())
928 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000929
930 // C++11 [class.union]p8, DR1460:
931 // a non-static data member of an anonymous union that is a member of
932 // X is also a variant member of X.
933 if (FieldRec->hasVariantMembers() &&
934 Field->isAnonymousStructOrUnion())
935 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000936 }
Richard Smithcc36f692011-12-22 02:22:31 +0000937 } else {
938 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000939 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000940 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000941 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000942
943 // C++11 [class.copy]p23:
944 // A defaulted copy/move assignment operator for a class X is defined
945 // as deleted if X has:
946 // -- a non-static data member of const non-class type (or array
947 // thereof)
948 if (T.isConstQualified())
949 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000950 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000951
952 // C++0x [class]p7:
953 // A standard-layout class is a class that:
954 // [...]
955 // -- either has no non-static data members in the most derived
956 // class and at most one base class with non-static data members,
957 // or has no base classes with non-static data members, and
958 // At this point we know that we have a non-static data member, so the last
959 // clause holds.
960 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000961 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000962
David Majnemer5cfda6f2016-05-22 05:34:26 +0000963 // C++14 [meta.unary.prop]p4:
964 // T is a class type [...] with [...] no non-static data members other
965 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000966 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000967 if (!Field->isBitField() ||
968 (!Field->getBitWidth()->isTypeDependent() &&
969 !Field->getBitWidth()->isValueDependent() &&
970 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000971 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000972 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000973 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000974
975 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000976 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000977 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000978 == DeclarationName::CXXConversionFunctionName) {
979 ASTContext &Ctx = getASTContext();
980 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
981 }
982 }
Richard Smith12e79312016-05-13 06:47:56 +0000983
984 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
985 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +0000986 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +0000987 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +0000988 // C++1z [dcl.init.aggr]p1:
989 // An aggregate is [...] a class [...] with no inherited constructors
990 data().Aggregate = false;
991 }
Richard Smith12e79312016-05-13 06:47:56 +0000992
993 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
994 data().HasInheritedAssignment = true;
995 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000996}
997
Richard Smith92f241f2012-12-08 02:53:02 +0000998void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
999 assert(!D->isImplicit() && !D->isUserProvided());
1000
1001 // The kind of special member this declaration is, if any.
1002 unsigned SMKind = 0;
1003
1004 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1005 if (Constructor->isDefaultConstructor()) {
1006 SMKind |= SMF_DefaultConstructor;
1007 if (Constructor->isConstexpr())
1008 data().HasConstexprDefaultConstructor = true;
1009 }
1010 if (Constructor->isCopyConstructor())
1011 SMKind |= SMF_CopyConstructor;
1012 else if (Constructor->isMoveConstructor())
1013 SMKind |= SMF_MoveConstructor;
1014 else if (Constructor->isConstexpr())
1015 // We may now know that the constructor is constexpr.
1016 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001017 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001018 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001019 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1020 data().HasIrrelevantDestructor = false;
1021 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001022 SMKind |= SMF_CopyAssignment;
1023 else if (D->isMoveAssignmentOperator())
1024 SMKind |= SMF_MoveAssignment;
1025
1026 // Update which trivial / non-trivial special members we have.
1027 // addedMember will have skipped this step for this member.
1028 if (D->isTrivial())
1029 data().HasTrivialSpecialMembers |= SMKind;
1030 else
1031 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1032}
1033
Joao Matose9a3ed42012-08-31 22:18:20 +00001034bool CXXRecordDecl::isCLike() const {
1035 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1036 !TemplateOrInstantiation.isNull())
1037 return false;
1038 if (!hasDefinition())
1039 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001040
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001041 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001042}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001043
Faisal Vali2b391ab2013-09-26 19:54:12 +00001044bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001045 if (!isLambda()) return false;
1046 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001047}
1048
1049CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001050 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001051 DeclarationName Name =
1052 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001053 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001054
1055 assert(!Calls.empty() && "Missing lambda call operator!");
1056 assert(Calls.size() == 1 && "More than one lambda call operator!");
1057
1058 NamedDecl *CallOp = Calls.front();
1059 if (FunctionTemplateDecl *CallOpTmpl =
1060 dyn_cast<FunctionTemplateDecl>(CallOp))
1061 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001062
1063 return cast<CXXMethodDecl>(CallOp);
1064}
1065
1066CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001067 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001068 DeclarationName Name =
1069 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001070 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001071 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001072 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1073 NamedDecl *InvokerFun = Invoker.front();
1074 if (FunctionTemplateDecl *InvokerTemplate =
1075 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1076 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1077
Faisal Vali571df122013-09-29 08:45:24 +00001078 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001079}
1080
Douglas Gregor9c702202012-02-10 07:45:31 +00001081void CXXRecordDecl::getCaptureFields(
1082 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001083 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001084 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001085 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001086
Douglas Gregorc8a73492012-02-13 15:44:47 +00001087 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001088 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001089 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001090 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001091 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001092 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001093 else if (C->capturesVariable())
1094 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001095 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001096 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001097}
1098
Faisal Vali2b391ab2013-09-26 19:54:12 +00001099TemplateParameterList *
1100CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001101 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001102 CXXMethodDecl *CallOp = getLambdaCallOperator();
1103 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1104 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001105 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001106}
Douglas Gregor9c702202012-02-10 07:45:31 +00001107
Richard Smith0bae6242016-08-25 00:34:00 +00001108Decl *CXXRecordDecl::getLambdaContextDecl() const {
1109 assert(isLambda() && "Not a lambda closure type!");
1110 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1111 return getLambdaData().ContextDecl.get(Source);
1112}
1113
John McCall1e3a1a72010-03-15 09:07:48 +00001114static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001115 QualType T =
1116 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1117 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001118 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001119}
1120
John McCall1e3a1a72010-03-15 09:07:48 +00001121/// Collect the visible conversions of a base class.
1122///
James Dennettb5d5f812012-06-15 22:28:09 +00001123/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001124/// \param InVirtual whether this base class is a virtual base (or a base
1125/// of a virtual base)
1126/// \param Access the access along the inheritance path to this base
1127/// \param ParentHiddenTypes the conversions provided by the inheritors
1128/// of this base
1129/// \param Output the set to which to add conversions from non-virtual bases
1130/// \param VOutput the set to which to add conversions from virtual bases
1131/// \param HiddenVBaseCs the set of conversions which were hidden in a
1132/// virtual base along some inheritance path
1133static void CollectVisibleConversions(ASTContext &Context,
1134 CXXRecordDecl *Record,
1135 bool InVirtual,
1136 AccessSpecifier Access,
1137 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001138 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001139 UnresolvedSetImpl &VOutput,
1140 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1141 // The set of types which have conversions in this class or its
1142 // subclasses. As an optimization, we don't copy the derived set
1143 // unless it might change.
1144 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1145 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1146
1147 // Collect the direct conversions and figure out which conversions
1148 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001149 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1150 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1151 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001152 HiddenTypesBuffer = ParentHiddenTypes;
1153 HiddenTypes = &HiddenTypesBuffer;
1154
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001155 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001156 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1157 bool Hidden = ParentHiddenTypes.count(ConvType);
1158 if (!Hidden)
1159 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001160
1161 // If this conversion is hidden and we're in a virtual base,
1162 // remember that it's hidden along some inheritance path.
1163 if (Hidden && InVirtual)
1164 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1165
1166 // If this conversion isn't hidden, add it to the appropriate output.
1167 else if (!Hidden) {
1168 AccessSpecifier IAccess
1169 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1170
1171 if (InVirtual)
1172 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001173 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001174 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001175 }
1176 }
1177 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001178
John McCall1e3a1a72010-03-15 09:07:48 +00001179 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001180 for (const auto &I : Record->bases()) {
1181 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001182 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001183
John McCall1e3a1a72010-03-15 09:07:48 +00001184 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001185 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1186 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001187
John McCall1e3a1a72010-03-15 09:07:48 +00001188 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1189 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1190 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001191 }
John McCall1e3a1a72010-03-15 09:07:48 +00001192}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001193
John McCall1e3a1a72010-03-15 09:07:48 +00001194/// Collect the visible conversions of a class.
1195///
1196/// This would be extremely straightforward if it weren't for virtual
1197/// bases. It might be worth special-casing that, really.
1198static void CollectVisibleConversions(ASTContext &Context,
1199 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001200 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001201 // The collection of all conversions in virtual bases that we've
1202 // found. These will be added to the output as long as they don't
1203 // appear in the hidden-conversions set.
1204 UnresolvedSet<8> VBaseCs;
1205
1206 // The set of conversions in virtual bases that we've determined to
1207 // be hidden.
1208 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1209
1210 // The set of types hidden by classes derived from this one.
1211 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1212
1213 // Go ahead and collect the direct conversions and add them to the
1214 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001215 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1216 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001217 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001218 for (; ConvI != ConvE; ++ConvI)
1219 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001220
1221 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001222 for (const auto &I : Record->bases()) {
1223 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001224 if (!RT) continue;
1225
1226 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001227 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001228 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1229 }
1230
1231 // Add any unhidden conversions provided by virtual bases.
1232 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1233 I != E; ++I) {
1234 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001235 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001236 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001237}
1238
1239/// getVisibleConversionFunctions - get all conversion functions visible
1240/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001241llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001242CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001243 ASTContext &Ctx = getASTContext();
1244
1245 ASTUnresolvedSet *Set;
1246 if (bases_begin() == bases_end()) {
1247 // If root class, all conversions are visible.
1248 Set = &data().Conversions.get(Ctx);
1249 } else {
1250 Set = &data().VisibleConversions.get(Ctx);
1251 // If visible conversion list is not evaluated, evaluate it.
1252 if (!data().ComputedVisibleConversions) {
1253 CollectVisibleConversions(Ctx, this, *Set);
1254 data().ComputedVisibleConversions = true;
1255 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001256 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001257 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001258}
1259
John McCallda4458e2010-03-31 01:36:47 +00001260void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1261 // This operation is O(N) but extremely rare. Sema only uses it to
1262 // remove UsingShadowDecls in a class that were followed by a direct
1263 // declaration, e.g.:
1264 // class A : B {
1265 // using B::operator int;
1266 // operator int();
1267 // };
1268 // This is uncommon by itself and even more uncommon in conjunction
1269 // with sufficiently large numbers of directly-declared conversions
1270 // that asymptotic behavior matters.
1271
Richard Smitha4ba74c2013-08-30 04:46:40 +00001272 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001273 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1274 if (Convs[I].getDecl() == ConvDecl) {
1275 Convs.erase(I);
1276 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1277 && "conversion was found multiple times in unresolved set");
1278 return;
1279 }
1280 }
1281
1282 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001283}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001284
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001285CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001286 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001287 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001288
1289 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001290}
1291
Chandler Carruth21c90602015-12-30 03:24:14 +00001292MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1293 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1294}
1295
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001296void
1297CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1298 TemplateSpecializationKind TSK) {
1299 assert(TemplateOrInstantiation.isNull() &&
1300 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001301 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001302 TemplateOrInstantiation
1303 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1304}
1305
Chandler Carruth21c90602015-12-30 03:24:14 +00001306ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1307 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1308}
1309
1310void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1311 TemplateOrInstantiation = Template;
1312}
1313
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001314TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1315 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001316 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1317 return Spec->getSpecializationKind();
1318
Douglas Gregor06db9f52009-10-12 20:18:28 +00001319 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001320 return MSInfo->getTemplateSpecializationKind();
1321
1322 return TSK_Undeclared;
1323}
1324
1325void
1326CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1327 if (ClassTemplateSpecializationDecl *Spec
1328 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1329 Spec->setSpecializationKind(TSK);
1330 return;
1331 }
1332
Douglas Gregor06db9f52009-10-12 20:18:28 +00001333 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001334 MSInfo->setTemplateSpecializationKind(TSK);
1335 return;
1336 }
1337
David Blaikie83d382b2011-09-23 05:06:16 +00001338 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001339}
1340
Reid Klecknere7367d62014-10-14 20:28:40 +00001341const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1342 // If it's a class template specialization, find the template or partial
1343 // specialization from which it was instantiated.
1344 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1345 auto From = TD->getInstantiatedFrom();
1346 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1347 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1348 if (NewCTD->isMemberSpecialization())
1349 break;
1350 CTD = NewCTD;
1351 }
Richard Smith7a591a42015-07-08 02:22:15 +00001352 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001353 }
1354 if (auto *CTPSD =
1355 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1356 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1357 if (NewCTPSD->isMemberSpecialization())
1358 break;
1359 CTPSD = NewCTPSD;
1360 }
Richard Smith7a591a42015-07-08 02:22:15 +00001361 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001362 }
1363 }
1364
1365 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1366 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1367 const CXXRecordDecl *RD = this;
1368 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1369 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001370 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001371 }
1372 }
1373
1374 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1375 "couldn't find pattern for class template instantiation");
1376 return nullptr;
1377}
1378
Douglas Gregorbac74902010-07-01 14:13:13 +00001379CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1380 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001381 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001382
1383 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001384 = Context.DeclarationNames.getCXXDestructorName(
1385 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001386
Richard Smithcf4bdde2015-02-21 02:45:19 +00001387 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001388 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001389 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001390
David Blaikieff7d47a2012-12-19 00:45:41 +00001391 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001392 return Dtor;
1393}
1394
Richard Trieu95a192a2015-05-28 00:14:02 +00001395bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1396 // Destructor is noreturn.
1397 if (const CXXDestructorDecl *Destructor = getDestructor())
1398 if (Destructor->isNoReturn())
1399 return true;
1400
1401 // Check base classes destructor for noreturn.
1402 for (const auto &Base : bases())
1403 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1404 return true;
1405
1406 // Check fields for noreturn.
1407 for (const auto *Field : fields())
1408 if (const CXXRecordDecl *RD =
1409 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1410 if (RD->isAnyDestructorNoReturn())
1411 return true;
1412
1413 // All destructors are not noreturn.
1414 return false;
1415}
1416
Douglas Gregorb11aad82011-02-19 18:51:44 +00001417void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001418 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001419}
1420
1421void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1422 RecordDecl::completeDefinition();
1423
Douglas Gregor8fb95122010-09-29 00:15:42 +00001424 // If the class may be abstract (but hasn't been marked as such), check for
1425 // any pure final overriders.
1426 if (mayBeAbstract()) {
1427 CXXFinalOverriderMap MyFinalOverriders;
1428 if (!FinalOverriders) {
1429 getFinalOverriders(MyFinalOverriders);
1430 FinalOverriders = &MyFinalOverriders;
1431 }
1432
1433 bool Done = false;
1434 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1435 MEnd = FinalOverriders->end();
1436 M != MEnd && !Done; ++M) {
1437 for (OverridingMethods::iterator SO = M->second.begin(),
1438 SOEnd = M->second.end();
1439 SO != SOEnd && !Done; ++SO) {
1440 assert(SO->second.size() > 0 &&
1441 "All virtual functions have overridding virtual functions");
1442
1443 // C++ [class.abstract]p4:
1444 // A class is abstract if it contains or inherits at least one
1445 // pure virtual function for which the final overrider is pure
1446 // virtual.
1447 if (SO->second.front().Method->isPure()) {
1448 data().Abstract = true;
1449 Done = true;
1450 break;
1451 }
1452 }
1453 }
1454 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001455
1456 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001457 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001458 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001459 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001460}
1461
1462bool CXXRecordDecl::mayBeAbstract() const {
1463 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1464 isDependentContext())
1465 return false;
1466
Aaron Ballman574705e2014-03-13 15:41:46 +00001467 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001468 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001469 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001470 if (BaseDecl->isAbstract())
1471 return true;
1472 }
1473
1474 return false;
1475}
1476
Richard Smithbc491202017-02-17 20:05:37 +00001477void CXXDeductionGuideDecl::anchor() { }
1478
1479CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1480 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1481 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1482 SourceLocation EndLocation) {
1483 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1484 NameInfo, T, TInfo, EndLocation);
1485}
1486
1487CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1488 unsigned ID) {
1489 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1490 DeclarationNameInfo(), QualType(),
1491 nullptr, SourceLocation());
1492}
1493
David Blaikie68e081d2011-12-20 02:48:34 +00001494void CXXMethodDecl::anchor() { }
1495
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001496bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001497 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001498
1499 if (MD->getStorageClass() == SC_Static)
1500 return true;
1501
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001502 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1503 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001504}
1505
Rafael Espindola49e860b2012-06-26 17:45:31 +00001506static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1507 const CXXMethodDecl *BaseMD) {
1508 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1509 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1510 const CXXMethodDecl *MD = *I;
1511 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1512 return true;
1513 if (recursivelyOverrides(MD, BaseMD))
1514 return true;
1515 }
1516 return false;
1517}
1518
1519CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001520CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1521 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001522 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1523 return this;
1524
1525 // Lookup doesn't work for destructors, so handle them separately.
1526 if (isa<CXXDestructorDecl>(this)) {
1527 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001528 if (MD) {
1529 if (recursivelyOverrides(MD, this))
1530 return MD;
1531 if (MayBeBase && recursivelyOverrides(this, MD))
1532 return MD;
1533 }
Craig Topper36250ad2014-05-12 05:36:57 +00001534 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001535 }
1536
Richard Smith40c78062015-02-21 02:31:57 +00001537 for (auto *ND : RD->lookup(getDeclName())) {
1538 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001539 if (!MD)
1540 continue;
1541 if (recursivelyOverrides(MD, this))
1542 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001543 if (MayBeBase && recursivelyOverrides(this, MD))
1544 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001545 }
1546
Aaron Ballman574705e2014-03-13 15:41:46 +00001547 for (const auto &I : RD->bases()) {
1548 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001549 if (!RT)
1550 continue;
1551 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1552 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1553 if (T)
1554 return T;
1555 }
1556
Craig Topper36250ad2014-05-12 05:36:57 +00001557 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001558}
1559
Ted Kremenek21475702008-09-05 17:16:31 +00001560CXXMethodDecl *
1561CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001562 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001563 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001564 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001565 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001566 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001567 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1568 T, TInfo, SC, isInline, isConstexpr,
1569 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001570}
1571
Douglas Gregor72172e92012-01-05 21:55:30 +00001572CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001573 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001574 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001575 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001576}
1577
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001578bool CXXMethodDecl::isUsualDeallocationFunction() const {
1579 if (getOverloadedOperator() != OO_Delete &&
1580 getOverloadedOperator() != OO_Array_Delete)
1581 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001582
1583 // C++ [basic.stc.dynamic.deallocation]p2:
1584 // A template instance is never a usual deallocation function,
1585 // regardless of its signature.
1586 if (getPrimaryTemplate())
1587 return false;
1588
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001589 // C++ [basic.stc.dynamic.deallocation]p2:
1590 // If a class T has a member deallocation function named operator delete
1591 // with exactly one parameter, then that function is a usual (non-placement)
1592 // deallocation function. [...]
1593 if (getNumParams() == 1)
1594 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00001595 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00001596
Richard Smithb2f0f052016-10-10 18:54:32 +00001597 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001598 // [...] If class T does not declare such an operator delete but does
1599 // declare a member deallocation function named operator delete with
1600 // exactly two parameters, the second of which has type std::size_t (18.1),
1601 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00001602 //
1603 // C++17 says a usual deallocation function is one with the signature
1604 // (void* [, size_t] [, std::align_val_t] [, ...])
1605 // and all such functions are usual deallocation functions. It's not clear
1606 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001607 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00001608 if (UsualParams < getNumParams() &&
1609 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
1610 Context.getSizeType()))
1611 ++UsualParams;
1612
1613 if (UsualParams < getNumParams() &&
1614 getParamDecl(UsualParams)->getType()->isAlignValT())
1615 ++UsualParams;
1616
1617 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001618 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00001619
1620 // In C++17 onwards, all potential usual deallocation functions are actual
1621 // usual deallocation functions.
1622 if (Context.getLangOpts().AlignedAllocation)
1623 return true;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001624
1625 // This function is a usual deallocation function if there are no
1626 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001627 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1628 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001629 I != E; ++I) {
1630 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001631 if (FD->getNumParams() == 1)
1632 return false;
1633 }
1634
1635 return true;
1636}
1637
Douglas Gregorb139cd52010-05-01 20:49:11 +00001638bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001639 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001640 // A user-declared copy assignment operator X::operator= is a non-static
1641 // non-template member function of class X with exactly one parameter of
1642 // type X, X&, const X&, volatile X& or const volatile X&.
1643 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1644 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001645 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1646 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001647 return false;
1648
1649 QualType ParamType = getParamDecl(0)->getType();
1650 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1651 ParamType = Ref->getPointeeType();
1652
1653 ASTContext &Context = getASTContext();
1654 QualType ClassType
1655 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1656 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1657}
1658
Alexis Huntfcaeae42011-05-25 20:50:04 +00001659bool CXXMethodDecl::isMoveAssignmentOperator() const {
1660 // C++0x [class.copy]p19:
1661 // A user-declared move assignment operator X::operator= is a non-static
1662 // non-template member function of class X with exactly one parameter of type
1663 // X&&, const X&&, volatile X&&, or const volatile X&&.
1664 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001665 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1666 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001667 return false;
1668
1669 QualType ParamType = getParamDecl(0)->getType();
1670 if (!isa<RValueReferenceType>(ParamType))
1671 return false;
1672 ParamType = ParamType->getPointeeType();
1673
1674 ASTContext &Context = getASTContext();
1675 QualType ClassType
1676 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1677 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1678}
1679
Anders Carlsson36d87e12009-05-16 23:58:37 +00001680void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001681 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001682 assert(!MD->getParent()->isDependentContext() &&
1683 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001684 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001685
Douglas Gregor832940b2010-03-02 23:58:15 +00001686 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001687}
1688
1689CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001690 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001691 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001692}
1693
1694CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001695 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001696 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001697}
1698
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001699unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001700 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001701 return getASTContext().overridden_methods_size(this);
1702}
1703
Clement Courbet6ecaec82016-07-05 07:49:31 +00001704CXXMethodDecl::overridden_method_range
1705CXXMethodDecl::overridden_methods() const {
1706 if (isa<CXXConstructorDecl>(this))
1707 return overridden_method_range(nullptr, nullptr);
1708 return getASTContext().overridden_methods(this);
1709}
1710
Ted Kremenek21475702008-09-05 17:16:31 +00001711QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001712 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1713 // If the member function is declared const, the type of this is const X*,
1714 // if the member function is declared volatile, the type of this is
1715 // volatile X*, and if the member function is declared const volatile,
1716 // the type of this is const volatile X*.
1717
Ted Kremenek21475702008-09-05 17:16:31 +00001718 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001719
John McCalle78aac42010-03-10 03:28:59 +00001720 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001721 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00001722 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001723 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001724}
1725
Eli Friedman71a26d82009-12-06 20:50:05 +00001726bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001727 // If this function is a template instantiation, look at the template from
1728 // which it was instantiated.
1729 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1730 if (!CheckFn)
1731 CheckFn = this;
1732
Eli Friedman71a26d82009-12-06 20:50:05 +00001733 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001734 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001735}
1736
Douglas Gregor355efbb2012-02-17 03:02:34 +00001737bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001738 const CXXRecordDecl *P = getParent();
1739 if (P->isLambda()) {
1740 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1741 if (StaticInvoker == this) return true;
1742 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1743 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1744 }
1745 }
1746 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001747}
1748
Alexis Hunt1d792652011-01-08 20:30:50 +00001749CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1750 TypeSourceInfo *TInfo, bool IsVirtual,
1751 SourceLocation L, Expr *Init,
1752 SourceLocation R,
1753 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001754 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001755 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
Richard Smith30e304e2016-12-14 00:03:17 +00001756 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001757{
Douglas Gregore8381c02008-11-05 04:29:56 +00001758}
1759
Alexis Hunt1d792652011-01-08 20:30:50 +00001760CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1761 FieldDecl *Member,
1762 SourceLocation MemberLoc,
1763 SourceLocation L, Expr *Init,
1764 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001765 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001766 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001767 IsWritten(false), SourceOrder(0)
Francois Pichetd583da02010-12-04 09:14:42 +00001768{
1769}
1770
Alexis Hunt1d792652011-01-08 20:30:50 +00001771CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1772 IndirectFieldDecl *Member,
1773 SourceLocation MemberLoc,
1774 SourceLocation L, Expr *Init,
1775 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001776 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001777 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001778 IsWritten(false), SourceOrder(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001779{
Douglas Gregore8381c02008-11-05 04:29:56 +00001780}
1781
Alexis Hunt1d792652011-01-08 20:30:50 +00001782CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001783 TypeSourceInfo *TInfo,
1784 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001785 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001786 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1787 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Richard Smith30e304e2016-12-14 00:03:17 +00001788 IsWritten(false), SourceOrder(0)
Alexis Huntc5575cc2011-02-26 19:13:13 +00001789{
1790}
1791
Alexis Hunt1d792652011-01-08 20:30:50 +00001792TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001793 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001794 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001795 else
1796 return TypeLoc();
1797}
1798
Alexis Hunt1d792652011-01-08 20:30:50 +00001799const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001800 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001801 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001802 else
Craig Topper36250ad2014-05-12 05:36:57 +00001803 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001804}
1805
Alexis Hunt1d792652011-01-08 20:30:50 +00001806SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001807 if (isInClassMemberInitializer())
1808 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001809
David Blaikiea81d4102015-01-18 00:12:58 +00001810 if (isAnyMemberInitializer())
1811 return getMemberLocation();
1812
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001813 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1814 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1815
1816 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001817}
1818
Alexis Hunt1d792652011-01-08 20:30:50 +00001819SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001820 if (isInClassMemberInitializer()) {
1821 FieldDecl *D = getAnyMember();
1822 if (Expr *I = D->getInClassInitializer())
1823 return I->getSourceRange();
1824 return SourceRange();
1825 }
1826
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001827 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001828}
1829
David Blaikie68e081d2011-12-20 02:48:34 +00001830void CXXConstructorDecl::anchor() { }
1831
Richard Smith5179eb72016-06-28 19:03:57 +00001832CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
1833 unsigned ID,
1834 bool Inherited) {
1835 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
1836 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
1837 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
1838 false, false, false, false, InheritedConstructor());
1839 Result->IsInheritingConstructor = Inherited;
1840 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00001841}
1842
1843CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001844CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001845 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001846 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001847 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001848 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00001849 bool isImplicitlyDeclared, bool isConstexpr,
1850 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001851 assert(NameInfo.getName().getNameKind()
1852 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001853 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00001854 unsigned Extra =
1855 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
1856 return new (C, RD, Extra) CXXConstructorDecl(
1857 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
1858 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00001859}
1860
Richard Smithc2bb8182015-03-24 06:36:48 +00001861CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1862 return CtorInitializers.get(getASTContext().getExternalSource());
1863}
1864
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001865CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1866 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1867 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1868 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1869 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001870
1871 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001872}
1873
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001874bool CXXConstructorDecl::isDefaultConstructor() const {
1875 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001876 // A default constructor for a class X is a constructor of class
1877 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001878 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001879 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001880}
1881
Mike Stump11289f42009-09-09 15:08:12 +00001882bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001883CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001884 return isCopyOrMoveConstructor(TypeQuals) &&
1885 getParamDecl(0)->getType()->isLValueReferenceType();
1886}
1887
1888bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1889 return isCopyOrMoveConstructor(TypeQuals) &&
1890 getParamDecl(0)->getType()->isRValueReferenceType();
1891}
1892
1893/// \brief Determine whether this is a copy or move constructor.
1894bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001895 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001896 // A non-template constructor for class X is a copy constructor
1897 // if its first parameter is of type X&, const X&, volatile X& or
1898 // const volatile X&, and either there are no other parameters
1899 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001900 // C++0x [class.copy]p3:
1901 // A non-template constructor for class X is a move constructor if its
1902 // first parameter is of type X&&, const X&&, volatile X&&, or
1903 // const volatile X&&, and either there are no other parameters or else
1904 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001905 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001906 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001907 (getPrimaryTemplate() != nullptr) ||
1908 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001909 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001910
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001911 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001912
1913 // Do we have a reference type?
1914 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001915 if (!ParamRefType)
1916 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001917
Douglas Gregorff7028a2009-11-13 23:59:09 +00001918 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001919 ASTContext &Context = getASTContext();
1920
Douglas Gregorff7028a2009-11-13 23:59:09 +00001921 CanQualType PointeeType
1922 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001923 CanQualType ClassTy
1924 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001925 if (PointeeType.getUnqualifiedType() != ClassTy)
1926 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001927
John McCall8ccfcb52009-09-24 19:53:00 +00001928 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001929
1930 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001931 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001932 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001933}
1934
Anders Carlssond20e7952009-08-28 16:57:08 +00001935bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001936 // C++ [class.conv.ctor]p1:
1937 // A constructor declared without the function-specifier explicit
1938 // that can be called with a single parameter specifies a
1939 // conversion from the type of its first parameter to the type of
1940 // its class. Such a constructor is called a converting
1941 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001942 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001943 return false;
1944
Mike Stump11289f42009-09-09 15:08:12 +00001945 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001946 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001947 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001948 (getNumParams() > 1 &&
1949 (getParamDecl(1)->hasDefaultArg() ||
1950 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001951}
Douglas Gregor61956c42008-10-31 09:07:45 +00001952
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001953bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001954 if ((getNumParams() < 1) ||
1955 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001956 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001957 return false;
1958
1959 const ParmVarDecl *Param = getParamDecl(0);
1960
1961 ASTContext &Context = getASTContext();
1962 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1963
Douglas Gregorffe14e32009-11-14 01:20:54 +00001964 // Is it the same as our our class type?
1965 CanQualType ClassTy
1966 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1967 if (ParamType.getUnqualifiedType() != ClassTy)
1968 return false;
1969
1970 return true;
1971}
1972
David Blaikie68e081d2011-12-20 02:48:34 +00001973void CXXDestructorDecl::anchor() { }
1974
Douglas Gregor831c93f2008-11-05 20:51:48 +00001975CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001976CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001977 return new (C, ID)
1978 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1979 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001980}
1981
1982CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001983CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001984 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001985 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001986 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001987 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001988 assert(NameInfo.getName().getNameKind()
1989 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001990 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001991 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001992 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001993}
1994
Richard Smithf8134002015-03-10 01:41:22 +00001995void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1996 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1997 if (OD && !First->OperatorDelete) {
1998 First->OperatorDelete = OD;
1999 if (auto *L = getASTMutationListener())
2000 L->ResolvedOperatorDelete(First, OD);
2001 }
2002}
2003
David Blaikie68e081d2011-12-20 02:48:34 +00002004void CXXConversionDecl::anchor() { }
2005
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002006CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002007CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002008 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002009 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002010 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002011 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002012}
2013
2014CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002015CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002016 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002017 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002018 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002019 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002020 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002021 assert(NameInfo.getName().getNameKind()
2022 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002023 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002024 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002025 isInline, isExplicit, isConstexpr,
2026 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002027}
2028
Douglas Gregord3b672c2012-02-16 01:06:16 +00002029bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2030 return isImplicit() && getParent()->isLambda() &&
2031 getConversionType()->isBlockPointerType();
2032}
2033
David Blaikie68e081d2011-12-20 02:48:34 +00002034void LinkageSpecDecl::anchor() { }
2035
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002036LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002037 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002038 SourceLocation ExternLoc,
2039 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002040 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002041 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002042 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002043}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002044
Richard Smithf7981722013-11-22 09:01:48 +00002045LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2046 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002047 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2048 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002049}
2050
David Blaikie68e081d2011-12-20 02:48:34 +00002051void UsingDirectiveDecl::anchor() { }
2052
Douglas Gregor889ceb72009-02-03 19:21:40 +00002053UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2054 SourceLocation L,
2055 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002056 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002057 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002058 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002059 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002060 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2061 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002062 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2063 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002064}
2065
Richard Smithf7981722013-11-22 09:01:48 +00002066UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2067 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002068 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2069 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002070 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002071 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002072}
2073
Sebastian Redla6602e92009-11-23 15:34:23 +00002074NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2075 if (NamespaceAliasDecl *NA =
2076 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2077 return NA->getNamespace();
2078 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2079}
2080
Richard Smith053f6c62014-05-16 23:01:30 +00002081NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2082 SourceLocation StartLoc, SourceLocation IdLoc,
2083 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2084 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2085 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2086 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002087 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002088
Douglas Gregore57e7522012-01-07 09:11:48 +00002089 if (PrevDecl)
2090 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2091}
2092
Douglas Gregor72172e92012-01-05 21:55:30 +00002093NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002094 bool Inline, SourceLocation StartLoc,
2095 SourceLocation IdLoc, IdentifierInfo *Id,
2096 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002097 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2098 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002099}
2100
2101NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002102 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002103 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002104}
2105
Chandler Carruth21c90602015-12-30 03:24:14 +00002106NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2107 if (isFirstDecl())
2108 return this;
2109
2110 return AnonOrFirstNamespaceAndInline.getPointer();
2111}
2112
2113const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2114 if (isFirstDecl())
2115 return this;
2116
2117 return AnonOrFirstNamespaceAndInline.getPointer();
2118}
2119
2120bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2121
Richard Smithd7af8a32014-05-10 01:17:36 +00002122NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2123 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002124}
2125NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2126 return getPreviousDecl();
2127}
2128NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2129 return getMostRecentDecl();
2130}
2131
David Blaikie68e081d2011-12-20 02:48:34 +00002132void NamespaceAliasDecl::anchor() { }
2133
Richard Smithf4634362014-09-03 23:11:22 +00002134NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2135 return getNextRedeclaration();
2136}
2137NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2138 return getPreviousDecl();
2139}
2140NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2141 return getMostRecentDecl();
2142}
2143
Mike Stump11289f42009-09-09 15:08:12 +00002144NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002145 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002146 SourceLocation AliasLoc,
2147 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002148 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002149 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002150 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002151 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002152 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2153 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002154 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002155 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002156}
2157
Douglas Gregor72172e92012-01-05 21:55:30 +00002158NamespaceAliasDecl *
2159NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002160 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002161 SourceLocation(), nullptr,
2162 NestedNameSpecifierLoc(),
2163 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002164}
2165
David Blaikie68e081d2011-12-20 02:48:34 +00002166void UsingShadowDecl::anchor() { }
2167
Richard Smith5179eb72016-06-28 19:03:57 +00002168UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2169 SourceLocation Loc, UsingDecl *Using,
2170 NamedDecl *Target)
2171 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
2172 redeclarable_base(C), Underlying(Target),
2173 UsingOrNextShadow(cast<NamedDecl>(Using)) {
2174 if (Target)
2175 IdentifierNamespace = Target->getIdentifierNamespace();
2176 setImplicit();
2177}
2178
2179UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2180 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
2181 redeclarable_base(C), Underlying(), UsingOrNextShadow() {}
2182
Douglas Gregor72172e92012-01-05 21:55:30 +00002183UsingShadowDecl *
2184UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002185 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002186}
2187
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002188UsingDecl *UsingShadowDecl::getUsingDecl() const {
2189 const UsingShadowDecl *Shadow = this;
2190 while (const UsingShadowDecl *NextShadow =
2191 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2192 Shadow = NextShadow;
2193 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2194}
2195
Richard Smith5179eb72016-06-28 19:03:57 +00002196void ConstructorUsingShadowDecl::anchor() { }
2197
2198ConstructorUsingShadowDecl *
2199ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2200 SourceLocation Loc, UsingDecl *Using,
2201 NamedDecl *Target, bool IsVirtual) {
2202 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2203 IsVirtual);
2204}
2205
2206ConstructorUsingShadowDecl *
2207ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2208 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2209}
2210
2211CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2212 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2213}
2214
David Blaikie68e081d2011-12-20 02:48:34 +00002215void UsingDecl::anchor() { }
2216
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002217void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2218 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2219 "declaration already in set");
2220 assert(S->getUsingDecl() == this);
2221
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002222 if (FirstUsingShadow.getPointer())
2223 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2224 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002225}
2226
2227void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2228 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2229 "declaration not in set");
2230 assert(S->getUsingDecl() == this);
2231
2232 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2233
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002234 if (FirstUsingShadow.getPointer() == S) {
2235 FirstUsingShadow.setPointer(
2236 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002237 S->UsingOrNextShadow = this;
2238 return;
2239 }
2240
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002241 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002242 while (Prev->UsingOrNextShadow != S)
2243 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2244 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2245 S->UsingOrNextShadow = this;
2246}
2247
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002248UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2249 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002250 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002251 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002252 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002253}
2254
Douglas Gregor72172e92012-01-05 21:55:30 +00002255UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002256 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2257 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2258 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002259}
2260
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002261SourceRange UsingDecl::getSourceRange() const {
2262 SourceLocation Begin = isAccessDeclaration()
2263 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2264 return SourceRange(Begin, getNameInfo().getEndLoc());
2265}
2266
Richard Smith151c4562016-12-20 21:35:28 +00002267void UsingPackDecl::anchor() { }
2268
2269UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2270 NamedDecl *InstantiatedFrom,
2271 ArrayRef<NamedDecl *> UsingDecls) {
2272 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2273 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2274}
2275
2276UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2277 unsigned NumExpansions) {
2278 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2279 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2280 Result->NumExpansions = NumExpansions;
2281 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2282 for (unsigned I = 0; I != NumExpansions; ++I)
2283 new (Trail + I) NamedDecl*(nullptr);
2284 return Result;
2285}
2286
David Blaikie68e081d2011-12-20 02:48:34 +00002287void UnresolvedUsingValueDecl::anchor() { }
2288
John McCalle61f2ba2009-11-18 02:36:19 +00002289UnresolvedUsingValueDecl *
2290UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2291 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002292 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002293 const DeclarationNameInfo &NameInfo,
2294 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002295 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002296 QualifierLoc, NameInfo,
2297 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002298}
2299
Douglas Gregor72172e92012-01-05 21:55:30 +00002300UnresolvedUsingValueDecl *
2301UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002302 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2303 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002304 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002305 DeclarationNameInfo(),
2306 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002307}
2308
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002309SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2310 SourceLocation Begin = isAccessDeclaration()
2311 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2312 return SourceRange(Begin, getNameInfo().getEndLoc());
2313}
2314
David Blaikie68e081d2011-12-20 02:48:34 +00002315void UnresolvedUsingTypenameDecl::anchor() { }
2316
John McCalle61f2ba2009-11-18 02:36:19 +00002317UnresolvedUsingTypenameDecl *
2318UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2319 SourceLocation UsingLoc,
2320 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002321 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002322 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002323 DeclarationName TargetName,
2324 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002325 return new (C, DC) UnresolvedUsingTypenameDecl(
2326 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002327 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002328}
2329
Douglas Gregor72172e92012-01-05 21:55:30 +00002330UnresolvedUsingTypenameDecl *
2331UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002332 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002333 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002334 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002335}
2336
David Blaikie68e081d2011-12-20 02:48:34 +00002337void StaticAssertDecl::anchor() { }
2338
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002339StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002340 SourceLocation StaticAssertLoc,
2341 Expr *AssertExpr,
2342 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002343 SourceLocation RParenLoc,
2344 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002345 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2346 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002347}
2348
Richard Smithf7981722013-11-22 09:01:48 +00002349StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002350 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002351 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2352 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002353}
2354
Richard Smithbdb84f32016-07-22 23:36:59 +00002355void BindingDecl::anchor() {}
2356
2357BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2358 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002359 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002360}
2361
2362BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002363 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002364}
2365
Richard Smith97fcf4b2016-08-14 23:15:52 +00002366VarDecl *BindingDecl::getHoldingVar() const {
2367 Expr *B = getBinding();
2368 if (!B)
2369 return nullptr;
2370 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2371 if (!DRE)
2372 return nullptr;
2373
2374 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2375 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2376 return VD;
2377}
2378
Richard Smithbdb84f32016-07-22 23:36:59 +00002379void DecompositionDecl::anchor() {}
2380
2381DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2382 SourceLocation StartLoc,
2383 SourceLocation LSquareLoc,
2384 QualType T, TypeSourceInfo *TInfo,
2385 StorageClass SC,
2386 ArrayRef<BindingDecl *> Bindings) {
2387 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2388 return new (C, DC, Extra)
2389 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2390}
2391
2392DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2393 unsigned ID,
2394 unsigned NumBindings) {
2395 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002396 auto *Result = new (C, ID, Extra)
2397 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2398 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002399 // Set up and clean out the bindings array.
2400 Result->NumBindings = NumBindings;
2401 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2402 for (unsigned I = 0; I != NumBindings; ++I)
2403 new (Trail + I) BindingDecl*(nullptr);
2404 return Result;
2405}
2406
Richard Smith7873de02016-08-11 22:25:46 +00002407void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2408 os << '[';
2409 bool Comma = false;
2410 for (auto *B : bindings()) {
2411 if (Comma)
2412 os << ", ";
2413 B->printName(os);
2414 Comma = true;
2415 }
2416 os << ']';
2417}
2418
Richard Smithf7981722013-11-22 09:01:48 +00002419MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2420 SourceLocation L, DeclarationName N,
2421 QualType T, TypeSourceInfo *TInfo,
2422 SourceLocation StartL,
2423 IdentifierInfo *Getter,
2424 IdentifierInfo *Setter) {
2425 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2426}
2427
2428MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2429 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002430 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2431 DeclarationName(), QualType(), nullptr,
2432 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002433}
2434
Anders Carlsson6750d162009-03-26 23:46:50 +00002435static const char *getAccessName(AccessSpecifier AS) {
2436 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002437 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002438 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002439 case AS_public:
2440 return "public";
2441 case AS_private:
2442 return "private";
2443 case AS_protected:
2444 return "protected";
2445 }
David Blaikief47fa302012-01-17 02:30:50 +00002446 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002447}
2448
2449const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2450 AccessSpecifier AS) {
2451 return DB << getAccessName(AS);
2452}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002453
2454const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2455 AccessSpecifier AS) {
2456 return DB << getAccessName(AS);
2457}