blob: 2a1fac8509eb5881c87e09f941c49175dd557d05 [file] [log] [blame]
Ted Kremenek21475702008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
Ted Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000030
David Blaikie68e081d2011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor72172e92012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000034 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000035}
36
Richard Smitha4ba74c2013-08-30 04:46:40 +000037void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
38 ExternalASTSource *Source = C.getExternalSource();
39 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
40 assert(Source && "getFromExternalSource with no external source");
41
42 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
43 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
44 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
45 Impl.Decls.setLazy(false);
46}
47
John McCall67da35c2010-02-04 22:26:26 +000048CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000049 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
50 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
51 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
52 HasPrivateFields(false), HasProtectedFields(false),
53 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
54 HasOnlyCMembers(true), HasInClassInitializer(false),
55 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000056 HasInheritedConstructor(false), HasInheritedAssignment(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000057 NeedOverloadResolutionForMoveConstructor(false),
58 NeedOverloadResolutionForMoveAssignment(false),
59 NeedOverloadResolutionForDestructor(false),
60 DefaultedMoveConstructorIsDeleted(false),
61 DefaultedMoveAssignmentIsDeleted(false),
62 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
63 DeclaredNonTrivialSpecialMembers(0), HasIrrelevantDestructor(true),
64 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000065 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000066 DefaultedDefaultConstructorIsConstexpr(true),
67 HasConstexprDefaultConstructor(false),
68 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
69 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
70 ImplicitCopyConstructorHasConstParam(true),
71 ImplicitCopyAssignmentHasConstParam(true),
72 HasDeclaredCopyConstructorWithConstParam(false),
73 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
74 IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
75 VBases(), Definition(D), FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000076
Benjamin Kramer300c0632012-07-04 17:03:33 +000077CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
78 return Bases.get(Definition->getASTContext().getExternalSource());
79}
80
81CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
82 return VBases.get(Definition->getASTContext().getExternalSource());
83}
84
Richard Smith053f6c62014-05-16 23:01:30 +000085CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
86 DeclContext *DC, SourceLocation StartLoc,
87 SourceLocation IdLoc, IdentifierInfo *Id,
88 CXXRecordDecl *PrevDecl)
89 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
90 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smithb6483992016-05-17 22:44:15 +000091 : nullptr),
Richard Smith053f6c62014-05-16 23:01:30 +000092 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000093
Jay Foad39c79802011-01-12 09:06:06 +000094CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000095 DeclContext *DC, SourceLocation StartLoc,
96 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000097 CXXRecordDecl* PrevDecl,
98 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +000099 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +0000100 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000103 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000104 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000105 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000106 return R;
107}
108
Richard Smith053f6c62014-05-16 23:01:30 +0000109CXXRecordDecl *
110CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
111 TypeSourceInfo *Info, SourceLocation Loc,
112 bool Dependent, bool IsGeneric,
113 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000114 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000115 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
116 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000117 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000118 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000119 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000120 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000121 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000122 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000123 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000124 return R;
125}
126
Douglas Gregor72172e92012-01-05 21:55:30 +0000127CXXRecordDecl *
128CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000129 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000130 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000131 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000132 R->MayHaveOutOfDateDef = false;
133 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000134}
135
Mike Stump11289f42009-09-09 15:08:12 +0000136void
Craig Toppere6337e12015-12-25 00:36:02 +0000137CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
138 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000139 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000140
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000141 if (!data().Bases.isOffset() && data().NumBases > 0)
142 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000143
Craig Toppere6337e12015-12-25 00:36:02 +0000144 if (NumBases) {
Richard Smith872307e2016-03-08 22:17:41 +0000145 if (!C.getLangOpts().CPlusPlus1z) {
146 // C++ [dcl.init.aggr]p1:
147 // An aggregate is [...] a class with [...] no base classes [...].
148 data().Aggregate = false;
149 }
Richard Smithaed32b42011-10-18 20:08:55 +0000150
151 // C++ [class]p4:
152 // A POD-struct is an aggregate class...
153 data().PlainOldData = false;
154 }
155
Anders Carlssonabb20e62010-03-29 05:13:12 +0000156 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000157 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000158
159 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000160 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000161
Craig Toppere6337e12015-12-25 00:36:02 +0000162 data().Bases = new(C) CXXBaseSpecifier [NumBases];
163 data().NumBases = NumBases;
164 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000165 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000166 // Keep track of inherited vbases for this base class.
167 const CXXBaseSpecifier *Base = Bases[i];
168 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000169 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000170 if (BaseType->isDependentType())
171 continue;
172 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000173 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000174
Chandler Carruthb1963742011-04-30 09:17:45 +0000175 if (!BaseClassDecl->isEmpty()) {
176 if (!data().Empty) {
177 // C++0x [class]p7:
178 // A standard-layout class is a class that:
179 // [...]
180 // -- either has no non-static data members in the most derived
181 // class and at most one base class with non-static data members,
182 // or has no base classes with non-static data members, and
183 // If this is the second non-empty base, then neither of these two
184 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000185 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000186 }
187
David Majnemer5cfda6f2016-05-22 05:34:26 +0000188 // C++14 [meta.unary.prop]p4:
189 // T is a class type [...] with [...] no base class B for which
190 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000191 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000192 data().HasNoNonEmptyBases = false;
193 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000194
Richard Smith872307e2016-03-08 22:17:41 +0000195 // C++1z [dcl.init.agg]p1:
196 // An aggregate is a class with [...] no private or protected base classes
197 if (Base->getAccessSpecifier() != AS_public)
198 data().Aggregate = false;
199
Douglas Gregor11c024b2010-09-28 20:50:54 +0000200 // C++ [class.virtual]p1:
201 // A class that declares or inherits a virtual function is called a
202 // polymorphic class.
203 if (BaseClassDecl->isPolymorphic())
204 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000205
Chandler Carruthb1963742011-04-30 09:17:45 +0000206 // C++0x [class]p7:
207 // A standard-layout class is a class that: [...]
208 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000209 if (!BaseClassDecl->isStandardLayout())
210 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000211
Chandler Carruthe71d0622011-04-24 02:49:34 +0000212 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000213 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000214 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000215
Anders Carlssonabb20e62010-03-29 05:13:12 +0000216 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000217 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000218 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000219 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000220 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000221
222 // C++11 [class.copy]p8:
223 // The implicitly-declared copy constructor for a class X will have
224 // the form 'X::X(const X&)' if each [...] virtual base class B of X
225 // has a copy constructor whose first parameter is of type
226 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000227 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000228 if (!VBaseDecl->hasCopyConstructorWithConstParam())
229 data().ImplicitCopyConstructorHasConstParam = false;
Richard Smith872307e2016-03-08 22:17:41 +0000230
231 // C++1z [dcl.init.agg]p1:
232 // An aggregate is a class with [...] no virtual base classes
233 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000234 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000235 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000236
237 if (Base->isVirtual()) {
238 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000239 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000240 VBases.push_back(Base);
241
David Majnemer5cfda6f2016-05-22 05:34:26 +0000242 // C++14 [meta.unary.prop] is_empty:
243 // T is a class type, but not a union type, with ... no virtual base
244 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000245 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000246
Richard Smith872307e2016-03-08 22:17:41 +0000247 // C++1z [dcl.init.agg]p1:
248 // An aggregate is a class with [...] no virtual base classes
249 data().Aggregate = false;
250
Richard Smith328aae52012-11-30 05:11:39 +0000251 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
252 // A [default constructor, copy/move constructor, or copy/move assignment
253 // operator for a class X] is trivial [...] if:
254 // -- class X has [...] no virtual base classes
255 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000256
257 // C++0x [class]p7:
258 // A standard-layout class is a class that: [...]
259 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000260 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000261
262 // C++11 [dcl.constexpr]p4:
263 // In the definition of a constexpr constructor [...]
264 // -- the class shall not have any virtual base classes
265 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000266 } else {
267 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000268 // A default constructor is trivial [...] if:
269 // -- all the direct base classes of its class have trivial default
270 // constructors.
271 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000272 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
273
Chandler Carruthad7d4042011-04-23 23:10:33 +0000274 // C++0x [class.copy]p13:
275 // A copy/move constructor for class X is trivial if [...]
276 // [...]
277 // -- the constructor selected to copy/move each direct base class
278 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000279 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000280 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000281 // If the base class doesn't have a simple move constructor, we'll eagerly
282 // declare it and perform overload resolution to determine which function
283 // it actually calls. If it does have a simple move constructor, this
284 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000285 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000286 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000287
288 // C++0x [class.copy]p27:
289 // A copy/move assignment operator for class X is trivial if [...]
290 // [...]
291 // -- the assignment operator selected to copy/move each direct base
292 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000293 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000294 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000295 // If the base class doesn't have a simple move assignment, we'll eagerly
296 // declare it and perform overload resolution to determine which function
297 // it actually calls. If it does have a simple move assignment, this
298 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000299 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000300 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000301
302 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000303 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000304 // requirements of a constexpr constructor, the implicitly-defined
305 // default constructor is constexpr.
306 if (!BaseClassDecl->hasConstexprDefaultConstructor())
307 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000308 }
Richard Smith92f241f2012-12-08 02:53:02 +0000309
Douglas Gregor11c024b2010-09-28 20:50:54 +0000310 // C++ [class.ctor]p3:
311 // A destructor is trivial if all the direct base classes of its class
312 // have trivial destructors.
313 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000314 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000315
316 if (!BaseClassDecl->hasIrrelevantDestructor())
317 data().HasIrrelevantDestructor = false;
318
Richard Smith1c33fe82012-11-28 06:23:12 +0000319 // C++11 [class.copy]p18:
320 // The implicitly-declared copy assignment oeprator for a class X will
321 // have the form 'X& X::operator=(const X&)' if each direct base class B
322 // of X has a copy assignment operator whose parameter is of type 'const
323 // B&', 'const volatile B&', or 'B' [...]
324 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
325 data().ImplicitCopyAssignmentHasConstParam = false;
326
327 // C++11 [class.copy]p8:
328 // The implicitly-declared copy constructor for a class X will have
329 // the form 'X::X(const X&)' if each direct [...] base class B of X
330 // has a copy constructor whose first parameter is of type
331 // 'const B&' or 'const volatile B&' [...]
332 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
333 data().ImplicitCopyConstructorHasConstParam = false;
334
John McCall31168b02011-06-15 23:02:42 +0000335 // A class has an Objective-C object member if... or any of its bases
336 // has an Objective-C object member.
337 if (BaseClassDecl->hasObjectMember())
338 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000339
340 if (BaseClassDecl->hasVolatileMember())
341 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000342
Douglas Gregor61226d32011-05-13 01:05:07 +0000343 // Keep track of the presence of mutable fields.
344 if (BaseClassDecl->hasMutableFields())
345 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000346
347 if (BaseClassDecl->hasUninitializedReferenceMember())
348 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000349
Nico Weber6a6376b2016-02-19 01:52:46 +0000350 if (!BaseClassDecl->allowConstDefaultInit())
351 data().HasUninitializedFields = true;
352
Richard Smith6b02d462012-12-08 08:32:28 +0000353 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000354 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000355
David Majnemer5ef4fe72014-06-13 06:43:46 +0000356 if (VBases.empty()) {
357 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000358 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000359 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000360
361 // Create base specifier for any direct or indirect virtual bases.
362 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
363 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000364 for (int I = 0, E = VBases.size(); I != E; ++I) {
365 QualType Type = VBases[I]->getType();
366 if (!Type->isDependentType())
367 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000368 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000369 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000370
371 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000372}
373
374void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
375 // C++11 [class.copy]p11:
376 // A defaulted copy/move constructor for a class X is defined as
377 // deleted if X has:
378 // -- a direct or virtual base class B that cannot be copied/moved [...]
379 // -- a non-static data member of class type M (or array thereof)
380 // that cannot be copied or moved [...]
381 if (!Subobj->hasSimpleMoveConstructor())
382 data().NeedOverloadResolutionForMoveConstructor = true;
383
384 // C++11 [class.copy]p23:
385 // A defaulted copy/move assignment operator for a class X is defined as
386 // deleted if X has:
387 // -- a direct or virtual base class B that cannot be copied/moved [...]
388 // -- a non-static data member of class type M (or array thereof)
389 // that cannot be copied or moved [...]
390 if (!Subobj->hasSimpleMoveAssignment())
391 data().NeedOverloadResolutionForMoveAssignment = true;
392
393 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
394 // A defaulted [ctor or dtor] for a class X is defined as
395 // deleted if X has:
396 // -- any direct or virtual base class [...] has a type with a destructor
397 // that is deleted or inaccessible from the defaulted [ctor or dtor].
398 // -- any non-static data member has a type with a destructor
399 // that is deleted or inaccessible from the defaulted [ctor or dtor].
400 if (!Subobj->hasSimpleDestructor()) {
401 data().NeedOverloadResolutionForMoveConstructor = true;
402 data().NeedOverloadResolutionForDestructor = true;
403 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000404}
405
Douglas Gregord2e6a452010-01-14 17:47:39 +0000406bool CXXRecordDecl::hasAnyDependentBases() const {
407 if (!isDependentContext())
408 return false;
409
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000410 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000411}
412
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000413bool CXXRecordDecl::isTriviallyCopyable() const {
414 // C++0x [class]p5:
415 // A trivially copyable class is a class that:
416 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000417 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000418 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000419 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000420 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000421 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000422 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000423 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000424 // -- has a trivial destructor.
425 if (!hasTrivialDestructor()) return false;
426
427 return true;
428}
429
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000430void CXXRecordDecl::markedVirtualFunctionPure() {
431 // C++ [class.abstract]p2:
432 // A class is abstract if it has at least one pure virtual function.
433 data().Abstract = true;
434}
435
436void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000437 if (!D->isImplicit() &&
438 !isa<FieldDecl>(D) &&
439 !isa<IndirectFieldDecl>(D) &&
440 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
441 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
442 data().HasOnlyCMembers = false;
443
444 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000445 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000446 return;
447
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000448 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
449 if (FunTmpl)
450 D = FunTmpl->getTemplatedDecl();
451
Douglas Gregora832d3e2010-09-28 19:45:33 +0000452 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
453 if (Method->isVirtual()) {
454 // C++ [dcl.init.aggr]p1:
455 // An aggregate is an array or a class with [...] no virtual functions.
456 data().Aggregate = false;
457
458 // C++ [class]p4:
459 // A POD-struct is an aggregate class...
460 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000461
David Majnemer5cfda6f2016-05-22 05:34:26 +0000462 // C++14 [meta.unary.prop]p4:
463 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000464 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000465
466 // C++ [class.virtual]p1:
467 // A class that declares or inherits a virtual function is called a
468 // polymorphic class.
469 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000470
Richard Smith328aae52012-11-30 05:11:39 +0000471 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
472 // A [default constructor, copy/move constructor, or copy/move
473 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000474 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000475 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000476
Chandler Carruthb1963742011-04-30 09:17:45 +0000477 // C++0x [class]p7:
478 // A standard-layout class is a class that: [...]
479 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000480 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000481 }
482 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000483
Richard Smith1c33fe82012-11-28 06:23:12 +0000484 // Notify the listener if an implicit member was added after the definition
485 // was completed.
486 if (!isBeingDefined() && D->isImplicit())
487 if (ASTMutationListener *L = getASTMutationListener())
488 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000489
Richard Smith328aae52012-11-30 05:11:39 +0000490 // The kind of special member this declaration is, if any.
491 unsigned SMKind = 0;
492
Richard Smith1c33fe82012-11-28 06:23:12 +0000493 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000494 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000495 if (!Constructor->isImplicit()) {
496 // Note that we have a user-declared constructor.
497 data().UserDeclaredConstructor = true;
498
499 // C++ [class]p4:
500 // A POD-struct is an aggregate class [...]
501 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
502 // type is technically an aggregate in C++0x since it wouldn't be in 03.
503 data().PlainOldData = false;
504 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000505
Richard Smitha5285072011-09-05 02:13:09 +0000506 // Technically, "user-provided" is only defined for special member
507 // functions, but the intent of the standard is clearly that it should apply
508 // to all functions.
509 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000510
Alexis Hunt88c75c32011-05-09 21:45:35 +0000511 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000512 SMKind |= SMF_DefaultConstructor;
513
514 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000515 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000516 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000517 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000518 if (Constructor->isDefaulted())
519 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000520 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000521
Chandler Carruthad7d4042011-04-23 23:10:33 +0000522 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000523 unsigned Quals;
524 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000525 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000526
527 if (Quals & Qualifiers::Const)
528 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000529 } else if (Constructor->isMoveConstructor())
530 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000531 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000532
533 // Record if we see any constexpr constructors which are neither copy
534 // nor move constructors.
535 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000536 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000537
Alexis Hunt88c75c32011-05-09 21:45:35 +0000538 // C++ [dcl.init.aggr]p1:
539 // An aggregate is an array or a class with no user-declared
540 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000541 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000542 // An aggregate is an array or a class with no user-provided
543 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000544 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000545 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000546 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000547 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000548
Richard Smith1c33fe82012-11-28 06:23:12 +0000549 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000550 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000551 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000552
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000553 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000554 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000555 // If the destructor is explicitly defaulted and not trivial or not public
556 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
557 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000558
Richard Smith1c33fe82012-11-28 06:23:12 +0000559 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000560 // A destructor is trivial if [...] the destructor is not virtual.
561 if (DD->isVirtual())
562 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000563 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000564
565 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000566 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000567 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000568 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000569
570 const ReferenceType *ParamTy =
571 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
572 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
573 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000574 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000575
Richard Smith328aae52012-11-30 05:11:39 +0000576 if (Method->isMoveAssignmentOperator())
577 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000578
Douglas Gregor457104e2010-09-29 04:25:11 +0000579 // Keep the list of conversion functions up-to-date.
580 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000581 // FIXME: We use the 'unsafe' accessor for the access specifier here,
582 // because Sema may not have set it yet. That's really just a misdesign
583 // in Sema. However, LLDB *will* have set the access specifier correctly,
584 // and adds declarations after the class is technically completed,
585 // so completeDefinition()'s overriding of the access specifiers doesn't
586 // work.
587 AccessSpecifier AS = Conversion->getAccessUnsafe();
588
Richard Smith328aae52012-11-30 05:11:39 +0000589 if (Conversion->getPrimaryTemplate()) {
590 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000591 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000592 ASTContext &Ctx = getASTContext();
593 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
594 NamedDecl *Primary =
595 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
596 if (Primary->getPreviousDecl())
597 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
598 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000599 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000600 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000601 }
602 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000603
Richard Smith328aae52012-11-30 05:11:39 +0000604 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000605 // If this is the first declaration of a special member, we no longer have
606 // an implicit trivial special member.
607 data().HasTrivialSpecialMembers &=
608 data().DeclaredSpecialMembers | ~SMKind;
609
610 if (!Method->isImplicit() && !Method->isUserProvided()) {
611 // This method is user-declared but not user-provided. We can't work out
612 // whether it's trivial yet (not until we get to the end of the class).
613 // We'll handle this method in finishedDefaultedOrDeletedMember.
614 } else if (Method->isTrivial())
615 data().HasTrivialSpecialMembers |= SMKind;
616 else
617 data().DeclaredNonTrivialSpecialMembers |= SMKind;
618
Richard Smith328aae52012-11-30 05:11:39 +0000619 // Note when we have declared a declared special member, and suppress the
620 // implicit declaration of this special member.
621 data().DeclaredSpecialMembers |= SMKind;
622
623 if (!Method->isImplicit()) {
624 data().UserDeclaredSpecialMembers |= SMKind;
625
626 // C++03 [class]p4:
627 // A POD-struct is an aggregate class that has [...] no user-defined
628 // copy assignment operator and no user-defined destructor.
629 //
630 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
631 // aggregates could not have any constructors, clear it even for an
632 // explicitly defaulted or deleted constructor.
633 // type is technically an aggregate in C++0x since it wouldn't be in 03.
634 //
635 // Also, a user-declared move assignment operator makes a class non-POD.
636 // This is an extension in C++03.
637 data().PlainOldData = false;
638 }
Richard Smith328aae52012-11-30 05:11:39 +0000639 }
640
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000641 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000642 }
Richard Smith328aae52012-11-30 05:11:39 +0000643
Douglas Gregora832d3e2010-09-28 19:45:33 +0000644 // Handle non-static data members.
645 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000646 // C++ [class.bit]p2:
647 // A declaration for a bit-field that omits the identifier declares an
648 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
649 // initialized.
650 if (Field->isUnnamedBitfield())
651 return;
652
Douglas Gregora832d3e2010-09-28 19:45:33 +0000653 // C++ [dcl.init.aggr]p1:
654 // An aggregate is an array or a class (clause 9) with [...] no
655 // private or protected non-static data members (clause 11).
656 //
657 // A POD must be an aggregate.
658 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
659 data().Aggregate = false;
660 data().PlainOldData = false;
661 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000662
663 // C++0x [class]p7:
664 // A standard-layout class is a class that:
665 // [...]
666 // -- has the same access control for all non-static data members,
667 switch (D->getAccess()) {
668 case AS_private: data().HasPrivateFields = true; break;
669 case AS_protected: data().HasProtectedFields = true; break;
670 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000671 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000672 };
673 if ((data().HasPrivateFields + data().HasProtectedFields +
674 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000675 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000676
Douglas Gregor61226d32011-05-13 01:05:07 +0000677 // Keep track of the presence of mutable fields.
678 if (Field->isMutable())
679 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000680
681 // C++11 [class.union]p8, DR1460:
682 // If X is a union, a non-static data member of X that is not an anonymous
683 // union is a variant member of X.
684 if (isUnion() && !Field->isAnonymousStructOrUnion())
685 data().HasVariantMembers = true;
686
Chandler Carruthad7d4042011-04-23 23:10:33 +0000687 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000688 // A POD struct is a class that is both a trivial class and a
689 // standard-layout class, and has no non-static data members of type
690 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000691 //
692 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
693 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000694 ASTContext &Context = getASTContext();
695 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000696 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000697 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000698 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000699 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
700 // Objective-C Automatic Reference Counting:
701 // If a class has a non-static data member of Objective-C pointer
702 // type (or array thereof), it is a non-POD type and its
703 // default constructor (if any), copy constructor, move constructor,
704 // copy assignment operator, move assignment operator, and destructor are
705 // non-trivial.
706 setHasObjectMember(true);
707 struct DefinitionData &Data = data();
708 Data.PlainOldData = false;
709 Data.HasTrivialSpecialMembers = 0;
710 Data.HasIrrelevantDestructor = false;
711 }
Eli Friedmana5433322013-07-20 01:06:31 +0000712 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000713 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000714
Chandler Carruthb1963742011-04-30 09:17:45 +0000715 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000716 if (!Field->hasInClassInitializer())
717 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000718
Chandler Carruthb1963742011-04-30 09:17:45 +0000719 // C++0x [class]p7:
720 // A standard-layout class is a class that:
721 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000722 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000723 }
724
Nico Weber6a6376b2016-02-19 01:52:46 +0000725 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
726 if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000727 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000728 data().HasUninitializedFields = true;
729 } else {
730 data().HasUninitializedFields = true;
731 }
732 }
733
Richard Smith3607ffe2012-02-13 03:54:03 +0000734 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000735 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000736 data().HasNonLiteralTypeFieldsOrBases = true;
737
Richard Smithab44d5b2013-12-10 08:25:00 +0000738 if (Field->hasInClassInitializer() ||
739 (Field->isAnonymousStructOrUnion() &&
740 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000741 data().HasInClassInitializer = true;
742
743 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000744 // A default constructor is trivial if [...] no non-static data member
745 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000746 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000747
Richard Smithe2648ba2012-05-07 01:07:30 +0000748 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000749 // An aggregate is a [...] class with [...] no
750 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000751 //
Richard Smith872307e2016-03-08 22:17:41 +0000752 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000753 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000754 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000755
Richard Smithe2648ba2012-05-07 01:07:30 +0000756 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000757 // A POD struct is [...] a trivial class.
758 data().PlainOldData = false;
759 }
760
Richard Smith6b02d462012-12-08 08:32:28 +0000761 // C++11 [class.copy]p23:
762 // A defaulted copy/move assignment operator for a class X is defined
763 // as deleted if X has:
764 // -- a non-static data member of reference type
765 if (T->isReferenceType())
766 data().DefaultedMoveAssignmentIsDeleted = true;
767
Douglas Gregor11c024b2010-09-28 20:50:54 +0000768 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
769 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
770 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000771 addedClassSubobject(FieldRec);
772
Richard Smithc91d12c2013-11-25 07:07:05 +0000773 // We may need to perform overload resolution to determine whether a
774 // field can be moved if it's const or volatile qualified.
775 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
776 data().NeedOverloadResolutionForMoveConstructor = true;
777 data().NeedOverloadResolutionForMoveAssignment = true;
778 }
779
Richard Smith6b02d462012-12-08 08:32:28 +0000780 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
781 // A defaulted [special member] for a class X is defined as
782 // deleted if:
783 // -- X is a union-like class that has a variant member with a
784 // non-trivial [corresponding special member]
785 if (isUnion()) {
786 if (FieldRec->hasNonTrivialMoveConstructor())
787 data().DefaultedMoveConstructorIsDeleted = true;
788 if (FieldRec->hasNonTrivialMoveAssignment())
789 data().DefaultedMoveAssignmentIsDeleted = true;
790 if (FieldRec->hasNonTrivialDestructor())
791 data().DefaultedDestructorIsDeleted = true;
792 }
793
Alexis Huntf479f1b2011-05-09 18:22:59 +0000794 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000795 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000796 // -- for all the non-static data members of its class that are of
797 // class type (or array thereof), each such class has a trivial
798 // default constructor.
799 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000800 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000801
802 // C++0x [class.copy]p13:
803 // A copy/move constructor for class X is trivial if [...]
804 // [...]
805 // -- for each non-static data member of X that is of class type (or
806 // an array thereof), the constructor selected to copy/move that
807 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000808 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000809 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000810 // If the field doesn't have a simple move constructor, we'll eagerly
811 // declare the move constructor for this class and we'll decide whether
812 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000813 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000814 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000815
816 // C++0x [class.copy]p27:
817 // A copy/move assignment operator for class X is trivial if [...]
818 // [...]
819 // -- for each non-static data member of X that is of class type (or
820 // an array thereof), the assignment operator selected to
821 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000822 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000823 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000824 // If the field doesn't have a simple move assignment, we'll eagerly
825 // declare the move assignment for this class and we'll decide whether
826 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000827 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000828 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000829
Douglas Gregor11c024b2010-09-28 20:50:54 +0000830 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000831 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000832 if (!FieldRec->hasIrrelevantDestructor())
833 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000834 if (FieldRec->hasObjectMember())
835 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000836 if (FieldRec->hasVolatileMember())
837 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000838
839 // C++0x [class]p7:
840 // A standard-layout class is a class that:
841 // -- has no non-static data members of type non-standard-layout
842 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000843 if (!FieldRec->isStandardLayout())
844 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000845
846 // C++0x [class]p7:
847 // A standard-layout class is a class that:
848 // [...]
849 // -- has no base classes of the same type as the first non-static
850 // data member.
851 // We don't want to expend bits in the state of the record decl
852 // tracking whether this is the first non-static data member so we
853 // cheat a bit and use some of the existing state: the empty bit.
854 // Virtual bases and virtual methods make a class non-empty, but they
855 // also make it non-standard-layout so we needn't check here.
856 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000857 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000858 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000859 // data member must come through here with Empty still true, and Empty
860 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000861 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000862 for (const auto &BI : bases()) {
863 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000864 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000865 break;
866 }
867 }
868 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000869
870 // Keep track of the presence of mutable fields.
871 if (FieldRec->hasMutableFields())
872 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000873
874 // C++11 [class.copy]p13:
875 // If the implicitly-defined constructor would satisfy the
876 // requirements of a constexpr constructor, the implicitly-defined
877 // constructor is constexpr.
878 // C++11 [dcl.constexpr]p4:
879 // -- every constructor involved in initializing non-static data
880 // members [...] shall be a constexpr constructor
881 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000882 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000883 // The standard requires any in-class initializer to be a constant
884 // expression. We consider this to be a defect.
885 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000886
887 // C++11 [class.copy]p8:
888 // The implicitly-declared copy constructor for a class X will have
889 // the form 'X::X(const X&)' if [...] for all the non-static data
890 // members of X that are of a class type M (or array thereof), each
891 // such class type has a copy constructor whose first parameter is
892 // of type 'const M&' or 'const volatile M&'.
893 if (!FieldRec->hasCopyConstructorWithConstParam())
894 data().ImplicitCopyConstructorHasConstParam = false;
895
896 // C++11 [class.copy]p18:
897 // The implicitly-declared copy assignment oeprator for a class X will
898 // have the form 'X& X::operator=(const X&)' if [...] for all the
899 // non-static data members of X that are of a class type M (or array
900 // thereof), each such class type has a copy assignment operator whose
901 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
902 if (!FieldRec->hasCopyAssignmentWithConstParam())
903 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000904
905 if (FieldRec->hasUninitializedReferenceMember() &&
906 !Field->hasInClassInitializer())
907 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000908
909 // C++11 [class.union]p8, DR1460:
910 // a non-static data member of an anonymous union that is a member of
911 // X is also a variant member of X.
912 if (FieldRec->hasVariantMembers() &&
913 Field->isAnonymousStructOrUnion())
914 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000915 }
Richard Smithcc36f692011-12-22 02:22:31 +0000916 } else {
917 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000918 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000919 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000920 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000921
922 // C++11 [class.copy]p23:
923 // A defaulted copy/move assignment operator for a class X is defined
924 // as deleted if X has:
925 // -- a non-static data member of const non-class type (or array
926 // thereof)
927 if (T.isConstQualified())
928 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000929 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000930
931 // C++0x [class]p7:
932 // A standard-layout class is a class that:
933 // [...]
934 // -- either has no non-static data members in the most derived
935 // class and at most one base class with non-static data members,
936 // or has no base classes with non-static data members, and
937 // At this point we know that we have a non-static data member, so the last
938 // clause holds.
939 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000940 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000941
David Majnemer5cfda6f2016-05-22 05:34:26 +0000942 // C++14 [meta.unary.prop]p4:
943 // T is a class type [...] with [...] no non-static data members other
944 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000945 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000946 if (!Field->isBitField() ||
947 (!Field->getBitWidth()->isTypeDependent() &&
948 !Field->getBitWidth()->isValueDependent() &&
949 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000950 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000951 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000952 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000953
954 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000955 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000956 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000957 == DeclarationName::CXXConversionFunctionName) {
958 ASTContext &Ctx = getASTContext();
959 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
960 }
961 }
Richard Smith12e79312016-05-13 06:47:56 +0000962
963 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
964 if (Using->getDeclName().getNameKind() ==
965 DeclarationName::CXXConstructorName)
966 data().HasInheritedConstructor = true;
967
968 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
969 data().HasInheritedAssignment = true;
970 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000971}
972
Richard Smith92f241f2012-12-08 02:53:02 +0000973void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
974 assert(!D->isImplicit() && !D->isUserProvided());
975
976 // The kind of special member this declaration is, if any.
977 unsigned SMKind = 0;
978
979 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
980 if (Constructor->isDefaultConstructor()) {
981 SMKind |= SMF_DefaultConstructor;
982 if (Constructor->isConstexpr())
983 data().HasConstexprDefaultConstructor = true;
984 }
985 if (Constructor->isCopyConstructor())
986 SMKind |= SMF_CopyConstructor;
987 else if (Constructor->isMoveConstructor())
988 SMKind |= SMF_MoveConstructor;
989 else if (Constructor->isConstexpr())
990 // We may now know that the constructor is constexpr.
991 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000992 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000993 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000994 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
995 data().HasIrrelevantDestructor = false;
996 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000997 SMKind |= SMF_CopyAssignment;
998 else if (D->isMoveAssignmentOperator())
999 SMKind |= SMF_MoveAssignment;
1000
1001 // Update which trivial / non-trivial special members we have.
1002 // addedMember will have skipped this step for this member.
1003 if (D->isTrivial())
1004 data().HasTrivialSpecialMembers |= SMKind;
1005 else
1006 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1007}
1008
Joao Matose9a3ed42012-08-31 22:18:20 +00001009bool CXXRecordDecl::isCLike() const {
1010 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1011 !TemplateOrInstantiation.isNull())
1012 return false;
1013 if (!hasDefinition())
1014 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001015
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001016 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001017}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001018
Faisal Vali2b391ab2013-09-26 19:54:12 +00001019bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001020 if (!isLambda()) return false;
1021 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001022}
1023
1024CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001025 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001026 DeclarationName Name =
1027 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001028 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001029
1030 assert(!Calls.empty() && "Missing lambda call operator!");
1031 assert(Calls.size() == 1 && "More than one lambda call operator!");
1032
1033 NamedDecl *CallOp = Calls.front();
1034 if (FunctionTemplateDecl *CallOpTmpl =
1035 dyn_cast<FunctionTemplateDecl>(CallOp))
1036 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001037
1038 return cast<CXXMethodDecl>(CallOp);
1039}
1040
1041CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001042 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001043 DeclarationName Name =
1044 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001045 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001046 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001047 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1048 NamedDecl *InvokerFun = Invoker.front();
1049 if (FunctionTemplateDecl *InvokerTemplate =
1050 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1051 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1052
Faisal Vali571df122013-09-29 08:45:24 +00001053 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001054}
1055
Douglas Gregor9c702202012-02-10 07:45:31 +00001056void CXXRecordDecl::getCaptureFields(
1057 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001058 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001059 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001060 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001061
Douglas Gregorc8a73492012-02-13 15:44:47 +00001062 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001063 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001064 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001065 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001066 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001067 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001068 else if (C->capturesVariable())
1069 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001070 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001071 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001072}
1073
Faisal Vali2b391ab2013-09-26 19:54:12 +00001074TemplateParameterList *
1075CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001076 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001077 CXXMethodDecl *CallOp = getLambdaCallOperator();
1078 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1079 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001080 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001081}
Douglas Gregor9c702202012-02-10 07:45:31 +00001082
John McCall1e3a1a72010-03-15 09:07:48 +00001083static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001084 QualType T =
1085 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1086 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001087 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001088}
1089
John McCall1e3a1a72010-03-15 09:07:48 +00001090/// Collect the visible conversions of a base class.
1091///
James Dennettb5d5f812012-06-15 22:28:09 +00001092/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001093/// \param InVirtual whether this base class is a virtual base (or a base
1094/// of a virtual base)
1095/// \param Access the access along the inheritance path to this base
1096/// \param ParentHiddenTypes the conversions provided by the inheritors
1097/// of this base
1098/// \param Output the set to which to add conversions from non-virtual bases
1099/// \param VOutput the set to which to add conversions from virtual bases
1100/// \param HiddenVBaseCs the set of conversions which were hidden in a
1101/// virtual base along some inheritance path
1102static void CollectVisibleConversions(ASTContext &Context,
1103 CXXRecordDecl *Record,
1104 bool InVirtual,
1105 AccessSpecifier Access,
1106 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001107 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001108 UnresolvedSetImpl &VOutput,
1109 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1110 // The set of types which have conversions in this class or its
1111 // subclasses. As an optimization, we don't copy the derived set
1112 // unless it might change.
1113 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1114 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1115
1116 // Collect the direct conversions and figure out which conversions
1117 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001118 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1119 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1120 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001121 HiddenTypesBuffer = ParentHiddenTypes;
1122 HiddenTypes = &HiddenTypesBuffer;
1123
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001124 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001125 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1126 bool Hidden = ParentHiddenTypes.count(ConvType);
1127 if (!Hidden)
1128 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001129
1130 // If this conversion is hidden and we're in a virtual base,
1131 // remember that it's hidden along some inheritance path.
1132 if (Hidden && InVirtual)
1133 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1134
1135 // If this conversion isn't hidden, add it to the appropriate output.
1136 else if (!Hidden) {
1137 AccessSpecifier IAccess
1138 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1139
1140 if (InVirtual)
1141 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001142 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001143 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001144 }
1145 }
1146 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001147
John McCall1e3a1a72010-03-15 09:07:48 +00001148 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001149 for (const auto &I : Record->bases()) {
1150 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001151 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001152
John McCall1e3a1a72010-03-15 09:07:48 +00001153 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001154 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1155 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001156
John McCall1e3a1a72010-03-15 09:07:48 +00001157 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1158 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1159 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001160 }
John McCall1e3a1a72010-03-15 09:07:48 +00001161}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001162
John McCall1e3a1a72010-03-15 09:07:48 +00001163/// Collect the visible conversions of a class.
1164///
1165/// This would be extremely straightforward if it weren't for virtual
1166/// bases. It might be worth special-casing that, really.
1167static void CollectVisibleConversions(ASTContext &Context,
1168 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001169 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001170 // The collection of all conversions in virtual bases that we've
1171 // found. These will be added to the output as long as they don't
1172 // appear in the hidden-conversions set.
1173 UnresolvedSet<8> VBaseCs;
1174
1175 // The set of conversions in virtual bases that we've determined to
1176 // be hidden.
1177 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1178
1179 // The set of types hidden by classes derived from this one.
1180 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1181
1182 // Go ahead and collect the direct conversions and add them to the
1183 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001184 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1185 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001186 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001187 for (; ConvI != ConvE; ++ConvI)
1188 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001189
1190 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001191 for (const auto &I : Record->bases()) {
1192 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001193 if (!RT) continue;
1194
1195 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001196 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001197 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1198 }
1199
1200 // Add any unhidden conversions provided by virtual bases.
1201 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1202 I != E; ++I) {
1203 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001204 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001205 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001206}
1207
1208/// getVisibleConversionFunctions - get all conversion functions visible
1209/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001210llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001211CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001212 ASTContext &Ctx = getASTContext();
1213
1214 ASTUnresolvedSet *Set;
1215 if (bases_begin() == bases_end()) {
1216 // If root class, all conversions are visible.
1217 Set = &data().Conversions.get(Ctx);
1218 } else {
1219 Set = &data().VisibleConversions.get(Ctx);
1220 // If visible conversion list is not evaluated, evaluate it.
1221 if (!data().ComputedVisibleConversions) {
1222 CollectVisibleConversions(Ctx, this, *Set);
1223 data().ComputedVisibleConversions = true;
1224 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001225 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001226 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001227}
1228
John McCallda4458e2010-03-31 01:36:47 +00001229void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1230 // This operation is O(N) but extremely rare. Sema only uses it to
1231 // remove UsingShadowDecls in a class that were followed by a direct
1232 // declaration, e.g.:
1233 // class A : B {
1234 // using B::operator int;
1235 // operator int();
1236 // };
1237 // This is uncommon by itself and even more uncommon in conjunction
1238 // with sufficiently large numbers of directly-declared conversions
1239 // that asymptotic behavior matters.
1240
Richard Smitha4ba74c2013-08-30 04:46:40 +00001241 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001242 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1243 if (Convs[I].getDecl() == ConvDecl) {
1244 Convs.erase(I);
1245 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1246 && "conversion was found multiple times in unresolved set");
1247 return;
1248 }
1249 }
1250
1251 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001252}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001253
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001254CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001255 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001256 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001257
1258 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001259}
1260
Chandler Carruth21c90602015-12-30 03:24:14 +00001261MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1262 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1263}
1264
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001265void
1266CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1267 TemplateSpecializationKind TSK) {
1268 assert(TemplateOrInstantiation.isNull() &&
1269 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001270 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001271 TemplateOrInstantiation
1272 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1273}
1274
Chandler Carruth21c90602015-12-30 03:24:14 +00001275ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1276 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1277}
1278
1279void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1280 TemplateOrInstantiation = Template;
1281}
1282
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001283TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1284 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001285 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1286 return Spec->getSpecializationKind();
1287
Douglas Gregor06db9f52009-10-12 20:18:28 +00001288 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001289 return MSInfo->getTemplateSpecializationKind();
1290
1291 return TSK_Undeclared;
1292}
1293
1294void
1295CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1296 if (ClassTemplateSpecializationDecl *Spec
1297 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1298 Spec->setSpecializationKind(TSK);
1299 return;
1300 }
1301
Douglas Gregor06db9f52009-10-12 20:18:28 +00001302 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001303 MSInfo->setTemplateSpecializationKind(TSK);
1304 return;
1305 }
1306
David Blaikie83d382b2011-09-23 05:06:16 +00001307 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001308}
1309
Reid Klecknere7367d62014-10-14 20:28:40 +00001310const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1311 // If it's a class template specialization, find the template or partial
1312 // specialization from which it was instantiated.
1313 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1314 auto From = TD->getInstantiatedFrom();
1315 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1316 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1317 if (NewCTD->isMemberSpecialization())
1318 break;
1319 CTD = NewCTD;
1320 }
Richard Smith7a591a42015-07-08 02:22:15 +00001321 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001322 }
1323 if (auto *CTPSD =
1324 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1325 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1326 if (NewCTPSD->isMemberSpecialization())
1327 break;
1328 CTPSD = NewCTPSD;
1329 }
Richard Smith7a591a42015-07-08 02:22:15 +00001330 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001331 }
1332 }
1333
1334 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1335 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1336 const CXXRecordDecl *RD = this;
1337 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1338 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001339 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001340 }
1341 }
1342
1343 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1344 "couldn't find pattern for class template instantiation");
1345 return nullptr;
1346}
1347
Douglas Gregorbac74902010-07-01 14:13:13 +00001348CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1349 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001350 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001351
1352 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001353 = Context.DeclarationNames.getCXXDestructorName(
1354 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001355
Richard Smithcf4bdde2015-02-21 02:45:19 +00001356 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001357 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001358 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001359
David Blaikieff7d47a2012-12-19 00:45:41 +00001360 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001361 return Dtor;
1362}
1363
Richard Trieu95a192a2015-05-28 00:14:02 +00001364bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1365 // Destructor is noreturn.
1366 if (const CXXDestructorDecl *Destructor = getDestructor())
1367 if (Destructor->isNoReturn())
1368 return true;
1369
1370 // Check base classes destructor for noreturn.
1371 for (const auto &Base : bases())
1372 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1373 return true;
1374
1375 // Check fields for noreturn.
1376 for (const auto *Field : fields())
1377 if (const CXXRecordDecl *RD =
1378 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1379 if (RD->isAnyDestructorNoReturn())
1380 return true;
1381
1382 // All destructors are not noreturn.
1383 return false;
1384}
1385
Douglas Gregorb11aad82011-02-19 18:51:44 +00001386void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001387 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001388}
1389
1390void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1391 RecordDecl::completeDefinition();
1392
Douglas Gregor8fb95122010-09-29 00:15:42 +00001393 // If the class may be abstract (but hasn't been marked as such), check for
1394 // any pure final overriders.
1395 if (mayBeAbstract()) {
1396 CXXFinalOverriderMap MyFinalOverriders;
1397 if (!FinalOverriders) {
1398 getFinalOverriders(MyFinalOverriders);
1399 FinalOverriders = &MyFinalOverriders;
1400 }
1401
1402 bool Done = false;
1403 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1404 MEnd = FinalOverriders->end();
1405 M != MEnd && !Done; ++M) {
1406 for (OverridingMethods::iterator SO = M->second.begin(),
1407 SOEnd = M->second.end();
1408 SO != SOEnd && !Done; ++SO) {
1409 assert(SO->second.size() > 0 &&
1410 "All virtual functions have overridding virtual functions");
1411
1412 // C++ [class.abstract]p4:
1413 // A class is abstract if it contains or inherits at least one
1414 // pure virtual function for which the final overrider is pure
1415 // virtual.
1416 if (SO->second.front().Method->isPure()) {
1417 data().Abstract = true;
1418 Done = true;
1419 break;
1420 }
1421 }
1422 }
1423 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001424
1425 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001426 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001427 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001428 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001429}
1430
1431bool CXXRecordDecl::mayBeAbstract() const {
1432 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1433 isDependentContext())
1434 return false;
1435
Aaron Ballman574705e2014-03-13 15:41:46 +00001436 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001437 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001438 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001439 if (BaseDecl->isAbstract())
1440 return true;
1441 }
1442
1443 return false;
1444}
1445
David Blaikie68e081d2011-12-20 02:48:34 +00001446void CXXMethodDecl::anchor() { }
1447
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001448bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001449 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001450
1451 if (MD->getStorageClass() == SC_Static)
1452 return true;
1453
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001454 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1455 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001456}
1457
Rafael Espindola49e860b2012-06-26 17:45:31 +00001458static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1459 const CXXMethodDecl *BaseMD) {
1460 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1461 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1462 const CXXMethodDecl *MD = *I;
1463 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1464 return true;
1465 if (recursivelyOverrides(MD, BaseMD))
1466 return true;
1467 }
1468 return false;
1469}
1470
1471CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001472CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1473 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001474 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1475 return this;
1476
1477 // Lookup doesn't work for destructors, so handle them separately.
1478 if (isa<CXXDestructorDecl>(this)) {
1479 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001480 if (MD) {
1481 if (recursivelyOverrides(MD, this))
1482 return MD;
1483 if (MayBeBase && recursivelyOverrides(this, MD))
1484 return MD;
1485 }
Craig Topper36250ad2014-05-12 05:36:57 +00001486 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001487 }
1488
Richard Smith40c78062015-02-21 02:31:57 +00001489 for (auto *ND : RD->lookup(getDeclName())) {
1490 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001491 if (!MD)
1492 continue;
1493 if (recursivelyOverrides(MD, this))
1494 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001495 if (MayBeBase && recursivelyOverrides(this, MD))
1496 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001497 }
1498
Aaron Ballman574705e2014-03-13 15:41:46 +00001499 for (const auto &I : RD->bases()) {
1500 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001501 if (!RT)
1502 continue;
1503 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1504 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1505 if (T)
1506 return T;
1507 }
1508
Craig Topper36250ad2014-05-12 05:36:57 +00001509 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001510}
1511
Ted Kremenek21475702008-09-05 17:16:31 +00001512CXXMethodDecl *
1513CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001514 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001515 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001516 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001517 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001518 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001519 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1520 T, TInfo, SC, isInline, isConstexpr,
1521 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001522}
1523
Douglas Gregor72172e92012-01-05 21:55:30 +00001524CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001525 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001526 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001527 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001528}
1529
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001530bool CXXMethodDecl::isUsualDeallocationFunction() const {
1531 if (getOverloadedOperator() != OO_Delete &&
1532 getOverloadedOperator() != OO_Array_Delete)
1533 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001534
1535 // C++ [basic.stc.dynamic.deallocation]p2:
1536 // A template instance is never a usual deallocation function,
1537 // regardless of its signature.
1538 if (getPrimaryTemplate())
1539 return false;
1540
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001541 // C++ [basic.stc.dynamic.deallocation]p2:
1542 // If a class T has a member deallocation function named operator delete
1543 // with exactly one parameter, then that function is a usual (non-placement)
1544 // deallocation function. [...]
1545 if (getNumParams() == 1)
1546 return true;
1547
1548 // C++ [basic.stc.dynamic.deallocation]p2:
1549 // [...] If class T does not declare such an operator delete but does
1550 // declare a member deallocation function named operator delete with
1551 // exactly two parameters, the second of which has type std::size_t (18.1),
1552 // then this function is a usual deallocation function.
1553 ASTContext &Context = getASTContext();
1554 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001555 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1556 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001557 return false;
1558
1559 // This function is a usual deallocation function if there are no
1560 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001561 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1562 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001563 I != E; ++I) {
1564 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001565 if (FD->getNumParams() == 1)
1566 return false;
1567 }
1568
1569 return true;
1570}
1571
Douglas Gregorb139cd52010-05-01 20:49:11 +00001572bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001573 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001574 // A user-declared copy assignment operator X::operator= is a non-static
1575 // non-template member function of class X with exactly one parameter of
1576 // type X, X&, const X&, volatile X& or const volatile X&.
1577 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1578 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001579 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1580 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001581 return false;
1582
1583 QualType ParamType = getParamDecl(0)->getType();
1584 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1585 ParamType = Ref->getPointeeType();
1586
1587 ASTContext &Context = getASTContext();
1588 QualType ClassType
1589 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1590 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1591}
1592
Alexis Huntfcaeae42011-05-25 20:50:04 +00001593bool CXXMethodDecl::isMoveAssignmentOperator() const {
1594 // C++0x [class.copy]p19:
1595 // A user-declared move assignment operator X::operator= is a non-static
1596 // non-template member function of class X with exactly one parameter of type
1597 // X&&, const X&&, volatile X&&, or const volatile X&&.
1598 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001599 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1600 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001601 return false;
1602
1603 QualType ParamType = getParamDecl(0)->getType();
1604 if (!isa<RValueReferenceType>(ParamType))
1605 return false;
1606 ParamType = ParamType->getPointeeType();
1607
1608 ASTContext &Context = getASTContext();
1609 QualType ClassType
1610 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1611 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1612}
1613
Anders Carlsson36d87e12009-05-16 23:58:37 +00001614void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001615 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001616 assert(!MD->getParent()->isDependentContext() &&
1617 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001618 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001619
Douglas Gregor832940b2010-03-02 23:58:15 +00001620 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001621}
1622
1623CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001624 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001625 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001626}
1627
1628CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001629 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001630 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001631}
1632
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001633unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001634 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001635 return getASTContext().overridden_methods_size(this);
1636}
1637
Ted Kremenek21475702008-09-05 17:16:31 +00001638QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001639 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1640 // If the member function is declared const, the type of this is const X*,
1641 // if the member function is declared volatile, the type of this is
1642 // volatile X*, and if the member function is declared const volatile,
1643 // the type of this is const volatile X*.
1644
Ted Kremenek21475702008-09-05 17:16:31 +00001645 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001646
John McCalle78aac42010-03-10 03:28:59 +00001647 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001648 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00001649 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001650 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001651}
1652
Eli Friedman71a26d82009-12-06 20:50:05 +00001653bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001654 // If this function is a template instantiation, look at the template from
1655 // which it was instantiated.
1656 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1657 if (!CheckFn)
1658 CheckFn = this;
1659
Eli Friedman71a26d82009-12-06 20:50:05 +00001660 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001661 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001662}
1663
Douglas Gregor355efbb2012-02-17 03:02:34 +00001664bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001665 const CXXRecordDecl *P = getParent();
1666 if (P->isLambda()) {
1667 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1668 if (StaticInvoker == this) return true;
1669 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1670 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1671 }
1672 }
1673 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001674}
1675
Alexis Hunt1d792652011-01-08 20:30:50 +00001676CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1677 TypeSourceInfo *TInfo, bool IsVirtual,
1678 SourceLocation L, Expr *Init,
1679 SourceLocation R,
1680 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001681 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001682 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1683 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001684{
Douglas Gregore8381c02008-11-05 04:29:56 +00001685}
1686
Alexis Hunt1d792652011-01-08 20:30:50 +00001687CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1688 FieldDecl *Member,
1689 SourceLocation MemberLoc,
1690 SourceLocation L, Expr *Init,
1691 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001692 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001693 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001694 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1695{
1696}
1697
Alexis Hunt1d792652011-01-08 20:30:50 +00001698CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1699 IndirectFieldDecl *Member,
1700 SourceLocation MemberLoc,
1701 SourceLocation L, Expr *Init,
1702 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001703 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001704 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001705 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001706{
Douglas Gregore8381c02008-11-05 04:29:56 +00001707}
1708
Alexis Hunt1d792652011-01-08 20:30:50 +00001709CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001710 TypeSourceInfo *TInfo,
1711 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001712 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001713 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1714 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001715 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1716{
1717}
1718
1719CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001720 FieldDecl *Member,
1721 SourceLocation MemberLoc,
1722 SourceLocation L, Expr *Init,
1723 SourceLocation R,
1724 VarDecl **Indices,
1725 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001726 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001727 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001728 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001729{
James Y Knight967eb202015-12-29 22:13:13 +00001730 std::uninitialized_copy(Indices, Indices + NumIndices,
1731 getTrailingObjects<VarDecl *>());
Douglas Gregor94f9a482010-05-05 05:51:00 +00001732}
1733
Alexis Hunt1d792652011-01-08 20:30:50 +00001734CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1735 FieldDecl *Member,
1736 SourceLocation MemberLoc,
1737 SourceLocation L, Expr *Init,
1738 SourceLocation R,
1739 VarDecl **Indices,
1740 unsigned NumIndices) {
James Y Knight967eb202015-12-29 22:13:13 +00001741 void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
Alexis Hunt1d792652011-01-08 20:30:50 +00001742 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001743 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1744 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001745}
1746
Alexis Hunt1d792652011-01-08 20:30:50 +00001747TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001748 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001749 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001750 else
1751 return TypeLoc();
1752}
1753
Alexis Hunt1d792652011-01-08 20:30:50 +00001754const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001755 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001756 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001757 else
Craig Topper36250ad2014-05-12 05:36:57 +00001758 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001759}
1760
Alexis Hunt1d792652011-01-08 20:30:50 +00001761SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001762 if (isInClassMemberInitializer())
1763 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001764
David Blaikiea81d4102015-01-18 00:12:58 +00001765 if (isAnyMemberInitializer())
1766 return getMemberLocation();
1767
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001768 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1769 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1770
1771 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001772}
1773
Alexis Hunt1d792652011-01-08 20:30:50 +00001774SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001775 if (isInClassMemberInitializer()) {
1776 FieldDecl *D = getAnyMember();
1777 if (Expr *I = D->getInClassInitializer())
1778 return I->getSourceRange();
1779 return SourceRange();
1780 }
1781
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001782 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001783}
1784
David Blaikie68e081d2011-12-20 02:48:34 +00001785void CXXConstructorDecl::anchor() { }
1786
Douglas Gregor61956c42008-10-31 09:07:45 +00001787CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001788CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001789 return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001790 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001791 nullptr, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001792}
1793
1794CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001795CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001796 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001797 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001798 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001799 bool isExplicit, bool isInline,
1800 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001801 assert(NameInfo.getName().getNameKind()
1802 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001803 "Name must refer to a constructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001804 return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001805 isExplicit, isInline,
1806 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001807}
1808
Richard Smithc2bb8182015-03-24 06:36:48 +00001809CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1810 return CtorInitializers.get(getASTContext().getExternalSource());
1811}
1812
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001813CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1814 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1815 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1816 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1817 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001818
1819 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001820}
1821
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001822bool CXXConstructorDecl::isDefaultConstructor() const {
1823 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001824 // A default constructor for a class X is a constructor of class
1825 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001826 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001827 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001828}
1829
Mike Stump11289f42009-09-09 15:08:12 +00001830bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001831CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001832 return isCopyOrMoveConstructor(TypeQuals) &&
1833 getParamDecl(0)->getType()->isLValueReferenceType();
1834}
1835
1836bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1837 return isCopyOrMoveConstructor(TypeQuals) &&
1838 getParamDecl(0)->getType()->isRValueReferenceType();
1839}
1840
1841/// \brief Determine whether this is a copy or move constructor.
1842bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001843 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001844 // A non-template constructor for class X is a copy constructor
1845 // if its first parameter is of type X&, const X&, volatile X& or
1846 // const volatile X&, and either there are no other parameters
1847 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001848 // C++0x [class.copy]p3:
1849 // A non-template constructor for class X is a move constructor if its
1850 // first parameter is of type X&&, const X&&, volatile X&&, or
1851 // const volatile X&&, and either there are no other parameters or else
1852 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001853 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001854 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001855 (getPrimaryTemplate() != nullptr) ||
1856 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001857 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001858
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001859 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001860
1861 // Do we have a reference type?
1862 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001863 if (!ParamRefType)
1864 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001865
Douglas Gregorff7028a2009-11-13 23:59:09 +00001866 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001867 ASTContext &Context = getASTContext();
1868
Douglas Gregorff7028a2009-11-13 23:59:09 +00001869 CanQualType PointeeType
1870 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001871 CanQualType ClassTy
1872 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001873 if (PointeeType.getUnqualifiedType() != ClassTy)
1874 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001875
John McCall8ccfcb52009-09-24 19:53:00 +00001876 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001877
1878 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001879 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001880 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001881}
1882
Anders Carlssond20e7952009-08-28 16:57:08 +00001883bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001884 // C++ [class.conv.ctor]p1:
1885 // A constructor declared without the function-specifier explicit
1886 // that can be called with a single parameter specifies a
1887 // conversion from the type of its first parameter to the type of
1888 // its class. Such a constructor is called a converting
1889 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001890 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001891 return false;
1892
Mike Stump11289f42009-09-09 15:08:12 +00001893 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001894 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001895 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001896 (getNumParams() > 1 &&
1897 (getParamDecl(1)->hasDefaultArg() ||
1898 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001899}
Douglas Gregor61956c42008-10-31 09:07:45 +00001900
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001901bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001902 if ((getNumParams() < 1) ||
1903 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001904 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001905 return false;
1906
1907 const ParmVarDecl *Param = getParamDecl(0);
1908
1909 ASTContext &Context = getASTContext();
1910 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1911
Douglas Gregorffe14e32009-11-14 01:20:54 +00001912 // Is it the same as our our class type?
1913 CanQualType ClassTy
1914 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1915 if (ParamType.getUnqualifiedType() != ClassTy)
1916 return false;
1917
1918 return true;
1919}
1920
Sebastian Redl08905022011-02-05 19:23:19 +00001921const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1922 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001923 method_iterator It = getASTContext().overridden_methods_begin(this);
1924 if (It == getASTContext().overridden_methods_end(this))
Craig Topper36250ad2014-05-12 05:36:57 +00001925 return nullptr;
Sebastian Redl08905022011-02-05 19:23:19 +00001926
1927 return cast<CXXConstructorDecl>(*It);
1928}
1929
1930void
1931CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1932 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001933 assert(getASTContext().overridden_methods_size(this) == 0 &&
1934 "Base ctor already set.");
1935 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001936}
1937
David Blaikie68e081d2011-12-20 02:48:34 +00001938void CXXDestructorDecl::anchor() { }
1939
Douglas Gregor831c93f2008-11-05 20:51:48 +00001940CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001941CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001942 return new (C, ID)
1943 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1944 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001945}
1946
1947CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001948CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001949 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001950 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001951 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001952 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001953 assert(NameInfo.getName().getNameKind()
1954 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001955 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001956 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001957 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001958}
1959
Richard Smithf8134002015-03-10 01:41:22 +00001960void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1961 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1962 if (OD && !First->OperatorDelete) {
1963 First->OperatorDelete = OD;
1964 if (auto *L = getASTMutationListener())
1965 L->ResolvedOperatorDelete(First, OD);
1966 }
1967}
1968
David Blaikie68e081d2011-12-20 02:48:34 +00001969void CXXConversionDecl::anchor() { }
1970
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001971CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001972CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001973 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001974 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001975 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00001976 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001977}
1978
1979CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001980CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001981 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001982 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001983 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001984 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001985 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001986 assert(NameInfo.getName().getNameKind()
1987 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001988 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00001989 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001990 isInline, isExplicit, isConstexpr,
1991 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001992}
1993
Douglas Gregord3b672c2012-02-16 01:06:16 +00001994bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1995 return isImplicit() && getParent()->isLambda() &&
1996 getConversionType()->isBlockPointerType();
1997}
1998
David Blaikie68e081d2011-12-20 02:48:34 +00001999void LinkageSpecDecl::anchor() { }
2000
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002001LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002002 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002003 SourceLocation ExternLoc,
2004 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002005 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002006 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002007 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002008}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002009
Richard Smithf7981722013-11-22 09:01:48 +00002010LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2011 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002012 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2013 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002014}
2015
David Blaikie68e081d2011-12-20 02:48:34 +00002016void UsingDirectiveDecl::anchor() { }
2017
Douglas Gregor889ceb72009-02-03 19:21:40 +00002018UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2019 SourceLocation L,
2020 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002021 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002022 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002023 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002024 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002025 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2026 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002027 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2028 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002029}
2030
Richard Smithf7981722013-11-22 09:01:48 +00002031UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2032 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002033 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2034 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002035 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002036 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002037}
2038
Sebastian Redla6602e92009-11-23 15:34:23 +00002039NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2040 if (NamespaceAliasDecl *NA =
2041 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2042 return NA->getNamespace();
2043 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2044}
2045
Richard Smith053f6c62014-05-16 23:01:30 +00002046NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2047 SourceLocation StartLoc, SourceLocation IdLoc,
2048 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2049 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2050 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2051 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002052 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002053
Douglas Gregore57e7522012-01-07 09:11:48 +00002054 if (PrevDecl)
2055 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2056}
2057
Douglas Gregor72172e92012-01-05 21:55:30 +00002058NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002059 bool Inline, SourceLocation StartLoc,
2060 SourceLocation IdLoc, IdentifierInfo *Id,
2061 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002062 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2063 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002064}
2065
2066NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002067 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002068 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002069}
2070
Chandler Carruth21c90602015-12-30 03:24:14 +00002071NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2072 if (isFirstDecl())
2073 return this;
2074
2075 return AnonOrFirstNamespaceAndInline.getPointer();
2076}
2077
2078const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2079 if (isFirstDecl())
2080 return this;
2081
2082 return AnonOrFirstNamespaceAndInline.getPointer();
2083}
2084
2085bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2086
Richard Smithd7af8a32014-05-10 01:17:36 +00002087NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2088 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002089}
2090NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2091 return getPreviousDecl();
2092}
2093NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2094 return getMostRecentDecl();
2095}
2096
David Blaikie68e081d2011-12-20 02:48:34 +00002097void NamespaceAliasDecl::anchor() { }
2098
Richard Smithf4634362014-09-03 23:11:22 +00002099NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2100 return getNextRedeclaration();
2101}
2102NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2103 return getPreviousDecl();
2104}
2105NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2106 return getMostRecentDecl();
2107}
2108
Mike Stump11289f42009-09-09 15:08:12 +00002109NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002110 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002111 SourceLocation AliasLoc,
2112 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002113 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002114 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002115 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002116 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002117 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2118 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002119 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002120 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002121}
2122
Douglas Gregor72172e92012-01-05 21:55:30 +00002123NamespaceAliasDecl *
2124NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002125 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002126 SourceLocation(), nullptr,
2127 NestedNameSpecifierLoc(),
2128 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002129}
2130
David Blaikie68e081d2011-12-20 02:48:34 +00002131void UsingShadowDecl::anchor() { }
2132
Douglas Gregor72172e92012-01-05 21:55:30 +00002133UsingShadowDecl *
2134UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002135 return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2136 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002137}
2138
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002139UsingDecl *UsingShadowDecl::getUsingDecl() const {
2140 const UsingShadowDecl *Shadow = this;
2141 while (const UsingShadowDecl *NextShadow =
2142 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2143 Shadow = NextShadow;
2144 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2145}
2146
David Blaikie68e081d2011-12-20 02:48:34 +00002147void UsingDecl::anchor() { }
2148
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002149void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2150 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2151 "declaration already in set");
2152 assert(S->getUsingDecl() == this);
2153
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002154 if (FirstUsingShadow.getPointer())
2155 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2156 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002157}
2158
2159void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2160 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2161 "declaration not in set");
2162 assert(S->getUsingDecl() == this);
2163
2164 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2165
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002166 if (FirstUsingShadow.getPointer() == S) {
2167 FirstUsingShadow.setPointer(
2168 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002169 S->UsingOrNextShadow = this;
2170 return;
2171 }
2172
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002173 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002174 while (Prev->UsingOrNextShadow != S)
2175 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2176 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2177 S->UsingOrNextShadow = this;
2178}
2179
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002180UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2181 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002182 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002183 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002184 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002185}
2186
Douglas Gregor72172e92012-01-05 21:55:30 +00002187UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002188 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2189 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2190 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002191}
2192
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002193SourceRange UsingDecl::getSourceRange() const {
2194 SourceLocation Begin = isAccessDeclaration()
2195 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2196 return SourceRange(Begin, getNameInfo().getEndLoc());
2197}
2198
David Blaikie68e081d2011-12-20 02:48:34 +00002199void UnresolvedUsingValueDecl::anchor() { }
2200
John McCalle61f2ba2009-11-18 02:36:19 +00002201UnresolvedUsingValueDecl *
2202UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2203 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002204 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002205 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002206 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2207 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002208}
2209
Douglas Gregor72172e92012-01-05 21:55:30 +00002210UnresolvedUsingValueDecl *
2211UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002212 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2213 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002214 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002215 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002216}
2217
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002218SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2219 SourceLocation Begin = isAccessDeclaration()
2220 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2221 return SourceRange(Begin, getNameInfo().getEndLoc());
2222}
2223
David Blaikie68e081d2011-12-20 02:48:34 +00002224void UnresolvedUsingTypenameDecl::anchor() { }
2225
John McCalle61f2ba2009-11-18 02:36:19 +00002226UnresolvedUsingTypenameDecl *
2227UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2228 SourceLocation UsingLoc,
2229 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002230 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002231 SourceLocation TargetNameLoc,
2232 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002233 return new (C, DC) UnresolvedUsingTypenameDecl(
2234 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2235 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002236}
2237
Douglas Gregor72172e92012-01-05 21:55:30 +00002238UnresolvedUsingTypenameDecl *
2239UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002240 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002241 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2242 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002243}
2244
David Blaikie68e081d2011-12-20 02:48:34 +00002245void StaticAssertDecl::anchor() { }
2246
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002247StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002248 SourceLocation StaticAssertLoc,
2249 Expr *AssertExpr,
2250 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002251 SourceLocation RParenLoc,
2252 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002253 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2254 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002255}
2256
Richard Smithf7981722013-11-22 09:01:48 +00002257StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002258 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002259 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2260 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002261}
2262
2263MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2264 SourceLocation L, DeclarationName N,
2265 QualType T, TypeSourceInfo *TInfo,
2266 SourceLocation StartL,
2267 IdentifierInfo *Getter,
2268 IdentifierInfo *Setter) {
2269 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2270}
2271
2272MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2273 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002274 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2275 DeclarationName(), QualType(), nullptr,
2276 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002277}
2278
Anders Carlsson6750d162009-03-26 23:46:50 +00002279static const char *getAccessName(AccessSpecifier AS) {
2280 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002281 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002282 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002283 case AS_public:
2284 return "public";
2285 case AS_private:
2286 return "private";
2287 case AS_protected:
2288 return "protected";
2289 }
David Blaikief47fa302012-01-17 02:30:50 +00002290 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002291}
2292
2293const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2294 AccessSpecifier AS) {
2295 return DB << getAccessName(AS);
2296}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002297
2298const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2299 AccessSpecifier AS) {
2300 return DB << getAccessName(AS);
2301}