blob: 9dc0771eadbdac19e451b90e86b64ae6bb9012ee [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
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000175 // A class with a non-empty base class is not empty.
176 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000177 if (!BaseClassDecl->isEmpty()) {
178 if (!data().Empty) {
179 // C++0x [class]p7:
180 // A standard-layout class is a class that:
181 // [...]
182 // -- either has no non-static data members in the most derived
183 // class and at most one base class with non-static data members,
184 // or has no base classes with non-static data members, and
185 // If this is the second non-empty base, then neither of these two
186 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000187 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000188 }
189
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000190 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000191 data().HasNoNonEmptyBases = false;
192 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000193
Richard Smith872307e2016-03-08 22:17:41 +0000194 // C++1z [dcl.init.agg]p1:
195 // An aggregate is a class with [...] no private or protected base classes
196 if (Base->getAccessSpecifier() != AS_public)
197 data().Aggregate = false;
198
Douglas Gregor11c024b2010-09-28 20:50:54 +0000199 // C++ [class.virtual]p1:
200 // A class that declares or inherits a virtual function is called a
201 // polymorphic class.
202 if (BaseClassDecl->isPolymorphic())
203 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000204
Chandler Carruthb1963742011-04-30 09:17:45 +0000205 // C++0x [class]p7:
206 // A standard-layout class is a class that: [...]
207 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000208 if (!BaseClassDecl->isStandardLayout())
209 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000210
Chandler Carruthe71d0622011-04-24 02:49:34 +0000211 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000212 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000213 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000214
Anders Carlssonabb20e62010-03-29 05:13:12 +0000215 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000216 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000217 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000218 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000219 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000220
221 // C++11 [class.copy]p8:
222 // The implicitly-declared copy constructor for a class X will have
223 // the form 'X::X(const X&)' if each [...] virtual base class B of X
224 // has a copy constructor whose first parameter is of type
225 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000226 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000227 if (!VBaseDecl->hasCopyConstructorWithConstParam())
228 data().ImplicitCopyConstructorHasConstParam = false;
Richard Smith872307e2016-03-08 22:17:41 +0000229
230 // C++1z [dcl.init.agg]p1:
231 // An aggregate is a class with [...] no virtual base classes
232 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000233 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000234 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000235
236 if (Base->isVirtual()) {
237 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000238 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000239 VBases.push_back(Base);
240
Richard Smith872307e2016-03-08 22:17:41 +0000241 // C++11 [meta.unary.prop] is_empty:
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000242 // T is a class type, but not a union type, with ... no virtual base
243 // classes
244 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000245
Richard Smith872307e2016-03-08 22:17:41 +0000246 // C++1z [dcl.init.agg]p1:
247 // An aggregate is a class with [...] no virtual base classes
248 data().Aggregate = false;
249
Richard Smith328aae52012-11-30 05:11:39 +0000250 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
251 // A [default constructor, copy/move constructor, or copy/move assignment
252 // operator for a class X] is trivial [...] if:
253 // -- class X has [...] no virtual base classes
254 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000255
256 // C++0x [class]p7:
257 // A standard-layout class is a class that: [...]
258 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000259 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000260
261 // C++11 [dcl.constexpr]p4:
262 // In the definition of a constexpr constructor [...]
263 // -- the class shall not have any virtual base classes
264 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000265 } else {
266 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000267 // A default constructor is trivial [...] if:
268 // -- all the direct base classes of its class have trivial default
269 // constructors.
270 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000271 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
272
Chandler Carruthad7d4042011-04-23 23:10:33 +0000273 // C++0x [class.copy]p13:
274 // A copy/move constructor for class X is trivial if [...]
275 // [...]
276 // -- the constructor selected to copy/move each direct base class
277 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000278 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000279 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000280 // If the base class doesn't have a simple move constructor, we'll eagerly
281 // declare it and perform overload resolution to determine which function
282 // it actually calls. If it does have a simple move constructor, this
283 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000284 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000285 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000286
287 // C++0x [class.copy]p27:
288 // A copy/move assignment operator for class X is trivial if [...]
289 // [...]
290 // -- the assignment operator selected to copy/move each direct base
291 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000292 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000293 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000294 // If the base class doesn't have a simple move assignment, we'll eagerly
295 // declare it and perform overload resolution to determine which function
296 // it actually calls. If it does have a simple move assignment, this
297 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000298 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000299 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000300
301 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000302 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000303 // requirements of a constexpr constructor, the implicitly-defined
304 // default constructor is constexpr.
305 if (!BaseClassDecl->hasConstexprDefaultConstructor())
306 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000307 }
Richard Smith92f241f2012-12-08 02:53:02 +0000308
Douglas Gregor11c024b2010-09-28 20:50:54 +0000309 // C++ [class.ctor]p3:
310 // A destructor is trivial if all the direct base classes of its class
311 // have trivial destructors.
312 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000313 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000314
315 if (!BaseClassDecl->hasIrrelevantDestructor())
316 data().HasIrrelevantDestructor = false;
317
Richard Smith1c33fe82012-11-28 06:23:12 +0000318 // C++11 [class.copy]p18:
319 // The implicitly-declared copy assignment oeprator for a class X will
320 // have the form 'X& X::operator=(const X&)' if each direct base class B
321 // of X has a copy assignment operator whose parameter is of type 'const
322 // B&', 'const volatile B&', or 'B' [...]
323 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
324 data().ImplicitCopyAssignmentHasConstParam = false;
325
326 // C++11 [class.copy]p8:
327 // The implicitly-declared copy constructor for a class X will have
328 // the form 'X::X(const X&)' if each direct [...] base class B of X
329 // has a copy constructor whose first parameter is of type
330 // 'const B&' or 'const volatile B&' [...]
331 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
332 data().ImplicitCopyConstructorHasConstParam = false;
333
John McCall31168b02011-06-15 23:02:42 +0000334 // A class has an Objective-C object member if... or any of its bases
335 // has an Objective-C object member.
336 if (BaseClassDecl->hasObjectMember())
337 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000338
339 if (BaseClassDecl->hasVolatileMember())
340 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000341
Douglas Gregor61226d32011-05-13 01:05:07 +0000342 // Keep track of the presence of mutable fields.
343 if (BaseClassDecl->hasMutableFields())
344 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000345
346 if (BaseClassDecl->hasUninitializedReferenceMember())
347 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000348
Nico Weber6a6376b2016-02-19 01:52:46 +0000349 if (!BaseClassDecl->allowConstDefaultInit())
350 data().HasUninitializedFields = true;
351
Richard Smith6b02d462012-12-08 08:32:28 +0000352 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000353 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000354
David Majnemer5ef4fe72014-06-13 06:43:46 +0000355 if (VBases.empty()) {
356 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000357 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000358 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000359
360 // Create base specifier for any direct or indirect virtual bases.
361 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
362 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000363 for (int I = 0, E = VBases.size(); I != E; ++I) {
364 QualType Type = VBases[I]->getType();
365 if (!Type->isDependentType())
366 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000367 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000368 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000369
370 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000371}
372
373void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
374 // C++11 [class.copy]p11:
375 // A defaulted copy/move constructor for a class X is defined as
376 // deleted if X has:
377 // -- a direct or virtual base class B that cannot be copied/moved [...]
378 // -- a non-static data member of class type M (or array thereof)
379 // that cannot be copied or moved [...]
380 if (!Subobj->hasSimpleMoveConstructor())
381 data().NeedOverloadResolutionForMoveConstructor = true;
382
383 // C++11 [class.copy]p23:
384 // A defaulted copy/move assignment operator for a class X is defined as
385 // deleted if X has:
386 // -- a direct or virtual base class B that cannot be copied/moved [...]
387 // -- a non-static data member of class type M (or array thereof)
388 // that cannot be copied or moved [...]
389 if (!Subobj->hasSimpleMoveAssignment())
390 data().NeedOverloadResolutionForMoveAssignment = true;
391
392 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
393 // A defaulted [ctor or dtor] for a class X is defined as
394 // deleted if X has:
395 // -- any direct or virtual base class [...] has a type with a destructor
396 // that is deleted or inaccessible from the defaulted [ctor or dtor].
397 // -- any non-static data member has a type with a destructor
398 // that is deleted or inaccessible from the defaulted [ctor or dtor].
399 if (!Subobj->hasSimpleDestructor()) {
400 data().NeedOverloadResolutionForMoveConstructor = true;
401 data().NeedOverloadResolutionForDestructor = true;
402 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000403}
404
Douglas Gregord2e6a452010-01-14 17:47:39 +0000405bool CXXRecordDecl::hasAnyDependentBases() const {
406 if (!isDependentContext())
407 return false;
408
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000409 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000410}
411
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000412bool CXXRecordDecl::isTriviallyCopyable() const {
413 // C++0x [class]p5:
414 // A trivially copyable class is a class that:
415 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000416 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000417 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000418 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000419 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000420 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000421 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000422 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000423 // -- has a trivial destructor.
424 if (!hasTrivialDestructor()) return false;
425
426 return true;
427}
428
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000429void CXXRecordDecl::markedVirtualFunctionPure() {
430 // C++ [class.abstract]p2:
431 // A class is abstract if it has at least one pure virtual function.
432 data().Abstract = true;
433}
434
435void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000436 if (!D->isImplicit() &&
437 !isa<FieldDecl>(D) &&
438 !isa<IndirectFieldDecl>(D) &&
439 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
440 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
441 data().HasOnlyCMembers = false;
442
443 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000444 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000445 return;
446
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000447 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
448 if (FunTmpl)
449 D = FunTmpl->getTemplatedDecl();
450
Douglas Gregora832d3e2010-09-28 19:45:33 +0000451 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
452 if (Method->isVirtual()) {
453 // C++ [dcl.init.aggr]p1:
454 // An aggregate is an array or a class with [...] no virtual functions.
455 data().Aggregate = false;
456
457 // C++ [class]p4:
458 // A POD-struct is an aggregate class...
459 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000460
461 // Virtual functions make the class non-empty.
462 // FIXME: Standard ref?
463 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000464
465 // C++ [class.virtual]p1:
466 // A class that declares or inherits a virtual function is called a
467 // polymorphic class.
468 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000469
Richard Smith328aae52012-11-30 05:11:39 +0000470 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
471 // A [default constructor, copy/move constructor, or copy/move
472 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000473 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000474 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000475
Chandler Carruthb1963742011-04-30 09:17:45 +0000476 // C++0x [class]p7:
477 // A standard-layout class is a class that: [...]
478 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000479 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000480 }
481 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000482
Richard Smith1c33fe82012-11-28 06:23:12 +0000483 // Notify the listener if an implicit member was added after the definition
484 // was completed.
485 if (!isBeingDefined() && D->isImplicit())
486 if (ASTMutationListener *L = getASTMutationListener())
487 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000488
Richard Smith328aae52012-11-30 05:11:39 +0000489 // The kind of special member this declaration is, if any.
490 unsigned SMKind = 0;
491
Richard Smith1c33fe82012-11-28 06:23:12 +0000492 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000493 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000494 if (!Constructor->isImplicit()) {
495 // Note that we have a user-declared constructor.
496 data().UserDeclaredConstructor = true;
497
498 // C++ [class]p4:
499 // A POD-struct is an aggregate class [...]
500 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
501 // type is technically an aggregate in C++0x since it wouldn't be in 03.
502 data().PlainOldData = false;
503 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000504
Richard Smitha5285072011-09-05 02:13:09 +0000505 // Technically, "user-provided" is only defined for special member
506 // functions, but the intent of the standard is clearly that it should apply
507 // to all functions.
508 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000509
Alexis Hunt88c75c32011-05-09 21:45:35 +0000510 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000511 SMKind |= SMF_DefaultConstructor;
512
513 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000514 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000515 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000516 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000517 if (Constructor->isDefaulted())
518 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000519 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000520
Chandler Carruthad7d4042011-04-23 23:10:33 +0000521 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000522 unsigned Quals;
523 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000524 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000525
526 if (Quals & Qualifiers::Const)
527 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000528 } else if (Constructor->isMoveConstructor())
529 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000530 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000531
532 // Record if we see any constexpr constructors which are neither copy
533 // nor move constructors.
534 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000535 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000536
Alexis Hunt88c75c32011-05-09 21:45:35 +0000537 // C++ [dcl.init.aggr]p1:
538 // An aggregate is an array or a class with no user-declared
539 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000540 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000541 // An aggregate is an array or a class with no user-provided
542 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000543 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000544 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000545 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000546 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000547
Richard Smith1c33fe82012-11-28 06:23:12 +0000548 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000549 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000550 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000551
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000552 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000553 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000554 // If the destructor is explicitly defaulted and not trivial or not public
555 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
556 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000557
Richard Smith1c33fe82012-11-28 06:23:12 +0000558 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000559 // A destructor is trivial if [...] the destructor is not virtual.
560 if (DD->isVirtual())
561 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000562 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000563
564 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000565 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000566 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000567 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000568
569 const ReferenceType *ParamTy =
570 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
571 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
572 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000573 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000574
Richard Smith328aae52012-11-30 05:11:39 +0000575 if (Method->isMoveAssignmentOperator())
576 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000577
Douglas Gregor457104e2010-09-29 04:25:11 +0000578 // Keep the list of conversion functions up-to-date.
579 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000580 // FIXME: We use the 'unsafe' accessor for the access specifier here,
581 // because Sema may not have set it yet. That's really just a misdesign
582 // in Sema. However, LLDB *will* have set the access specifier correctly,
583 // and adds declarations after the class is technically completed,
584 // so completeDefinition()'s overriding of the access specifiers doesn't
585 // work.
586 AccessSpecifier AS = Conversion->getAccessUnsafe();
587
Richard Smith328aae52012-11-30 05:11:39 +0000588 if (Conversion->getPrimaryTemplate()) {
589 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000590 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000591 ASTContext &Ctx = getASTContext();
592 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
593 NamedDecl *Primary =
594 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
595 if (Primary->getPreviousDecl())
596 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
597 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000598 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000599 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000600 }
601 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000602
Richard Smith328aae52012-11-30 05:11:39 +0000603 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000604 // If this is the first declaration of a special member, we no longer have
605 // an implicit trivial special member.
606 data().HasTrivialSpecialMembers &=
607 data().DeclaredSpecialMembers | ~SMKind;
608
609 if (!Method->isImplicit() && !Method->isUserProvided()) {
610 // This method is user-declared but not user-provided. We can't work out
611 // whether it's trivial yet (not until we get to the end of the class).
612 // We'll handle this method in finishedDefaultedOrDeletedMember.
613 } else if (Method->isTrivial())
614 data().HasTrivialSpecialMembers |= SMKind;
615 else
616 data().DeclaredNonTrivialSpecialMembers |= SMKind;
617
Richard Smith328aae52012-11-30 05:11:39 +0000618 // Note when we have declared a declared special member, and suppress the
619 // implicit declaration of this special member.
620 data().DeclaredSpecialMembers |= SMKind;
621
622 if (!Method->isImplicit()) {
623 data().UserDeclaredSpecialMembers |= SMKind;
624
625 // C++03 [class]p4:
626 // A POD-struct is an aggregate class that has [...] no user-defined
627 // copy assignment operator and no user-defined destructor.
628 //
629 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
630 // aggregates could not have any constructors, clear it even for an
631 // explicitly defaulted or deleted constructor.
632 // type is technically an aggregate in C++0x since it wouldn't be in 03.
633 //
634 // Also, a user-declared move assignment operator makes a class non-POD.
635 // This is an extension in C++03.
636 data().PlainOldData = false;
637 }
Richard Smith328aae52012-11-30 05:11:39 +0000638 }
639
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000640 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000641 }
Richard Smith328aae52012-11-30 05:11:39 +0000642
Douglas Gregora832d3e2010-09-28 19:45:33 +0000643 // Handle non-static data members.
644 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000645 // C++ [class.bit]p2:
646 // A declaration for a bit-field that omits the identifier declares an
647 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
648 // initialized.
649 if (Field->isUnnamedBitfield())
650 return;
651
Douglas Gregora832d3e2010-09-28 19:45:33 +0000652 // C++ [dcl.init.aggr]p1:
653 // An aggregate is an array or a class (clause 9) with [...] no
654 // private or protected non-static data members (clause 11).
655 //
656 // A POD must be an aggregate.
657 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
658 data().Aggregate = false;
659 data().PlainOldData = false;
660 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000661
662 // C++0x [class]p7:
663 // A standard-layout class is a class that:
664 // [...]
665 // -- has the same access control for all non-static data members,
666 switch (D->getAccess()) {
667 case AS_private: data().HasPrivateFields = true; break;
668 case AS_protected: data().HasProtectedFields = true; break;
669 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000670 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000671 };
672 if ((data().HasPrivateFields + data().HasProtectedFields +
673 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000674 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000675
Douglas Gregor61226d32011-05-13 01:05:07 +0000676 // Keep track of the presence of mutable fields.
677 if (Field->isMutable())
678 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000679
680 // C++11 [class.union]p8, DR1460:
681 // If X is a union, a non-static data member of X that is not an anonymous
682 // union is a variant member of X.
683 if (isUnion() && !Field->isAnonymousStructOrUnion())
684 data().HasVariantMembers = true;
685
Chandler Carruthad7d4042011-04-23 23:10:33 +0000686 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000687 // A POD struct is a class that is both a trivial class and a
688 // standard-layout class, and has no non-static data members of type
689 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000690 //
691 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
692 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000693 ASTContext &Context = getASTContext();
694 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000695 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000696 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000697 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000698 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
699 // Objective-C Automatic Reference Counting:
700 // If a class has a non-static data member of Objective-C pointer
701 // type (or array thereof), it is a non-POD type and its
702 // default constructor (if any), copy constructor, move constructor,
703 // copy assignment operator, move assignment operator, and destructor are
704 // non-trivial.
705 setHasObjectMember(true);
706 struct DefinitionData &Data = data();
707 Data.PlainOldData = false;
708 Data.HasTrivialSpecialMembers = 0;
709 Data.HasIrrelevantDestructor = false;
710 }
Eli Friedmana5433322013-07-20 01:06:31 +0000711 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000712 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000713
Chandler Carruthb1963742011-04-30 09:17:45 +0000714 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000715 if (!Field->hasInClassInitializer())
716 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000717
Chandler Carruthb1963742011-04-30 09:17:45 +0000718 // C++0x [class]p7:
719 // A standard-layout class is a class that:
720 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000721 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000722 }
723
Nico Weber6a6376b2016-02-19 01:52:46 +0000724 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
725 if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000726 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000727 data().HasUninitializedFields = true;
728 } else {
729 data().HasUninitializedFields = true;
730 }
731 }
732
Richard Smith3607ffe2012-02-13 03:54:03 +0000733 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000734 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000735 data().HasNonLiteralTypeFieldsOrBases = true;
736
Richard Smithab44d5b2013-12-10 08:25:00 +0000737 if (Field->hasInClassInitializer() ||
738 (Field->isAnonymousStructOrUnion() &&
739 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000740 data().HasInClassInitializer = true;
741
742 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000743 // A default constructor is trivial if [...] no non-static data member
744 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000745 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000746
Richard Smithe2648ba2012-05-07 01:07:30 +0000747 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000748 // An aggregate is a [...] class with [...] no
749 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000750 //
Richard Smith872307e2016-03-08 22:17:41 +0000751 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000752 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000753 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000754
Richard Smithe2648ba2012-05-07 01:07:30 +0000755 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000756 // A POD struct is [...] a trivial class.
757 data().PlainOldData = false;
758 }
759
Richard Smith6b02d462012-12-08 08:32:28 +0000760 // C++11 [class.copy]p23:
761 // A defaulted copy/move assignment operator for a class X is defined
762 // as deleted if X has:
763 // -- a non-static data member of reference type
764 if (T->isReferenceType())
765 data().DefaultedMoveAssignmentIsDeleted = true;
766
Douglas Gregor11c024b2010-09-28 20:50:54 +0000767 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
768 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
769 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000770 addedClassSubobject(FieldRec);
771
Richard Smithc91d12c2013-11-25 07:07:05 +0000772 // We may need to perform overload resolution to determine whether a
773 // field can be moved if it's const or volatile qualified.
774 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
775 data().NeedOverloadResolutionForMoveConstructor = true;
776 data().NeedOverloadResolutionForMoveAssignment = true;
777 }
778
Richard Smith6b02d462012-12-08 08:32:28 +0000779 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
780 // A defaulted [special member] for a class X is defined as
781 // deleted if:
782 // -- X is a union-like class that has a variant member with a
783 // non-trivial [corresponding special member]
784 if (isUnion()) {
785 if (FieldRec->hasNonTrivialMoveConstructor())
786 data().DefaultedMoveConstructorIsDeleted = true;
787 if (FieldRec->hasNonTrivialMoveAssignment())
788 data().DefaultedMoveAssignmentIsDeleted = true;
789 if (FieldRec->hasNonTrivialDestructor())
790 data().DefaultedDestructorIsDeleted = true;
791 }
792
Alexis Huntf479f1b2011-05-09 18:22:59 +0000793 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000794 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000795 // -- for all the non-static data members of its class that are of
796 // class type (or array thereof), each such class has a trivial
797 // default constructor.
798 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000799 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000800
801 // C++0x [class.copy]p13:
802 // A copy/move constructor for class X is trivial if [...]
803 // [...]
804 // -- for each non-static data member of X that is of class type (or
805 // an array thereof), the constructor selected to copy/move that
806 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000807 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000808 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000809 // If the field doesn't have a simple move constructor, we'll eagerly
810 // declare the move constructor for this class and we'll decide whether
811 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000812 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000813 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000814
815 // C++0x [class.copy]p27:
816 // A copy/move assignment operator for class X is trivial if [...]
817 // [...]
818 // -- for each non-static data member of X that is of class type (or
819 // an array thereof), the assignment operator selected to
820 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000821 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000822 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000823 // If the field doesn't have a simple move assignment, we'll eagerly
824 // declare the move assignment for this class and we'll decide whether
825 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000826 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000827 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000828
Douglas Gregor11c024b2010-09-28 20:50:54 +0000829 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000830 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000831 if (!FieldRec->hasIrrelevantDestructor())
832 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000833 if (FieldRec->hasObjectMember())
834 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000835 if (FieldRec->hasVolatileMember())
836 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000837
838 // C++0x [class]p7:
839 // A standard-layout class is a class that:
840 // -- has no non-static data members of type non-standard-layout
841 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000842 if (!FieldRec->isStandardLayout())
843 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000844
845 // C++0x [class]p7:
846 // A standard-layout class is a class that:
847 // [...]
848 // -- has no base classes of the same type as the first non-static
849 // data member.
850 // We don't want to expend bits in the state of the record decl
851 // tracking whether this is the first non-static data member so we
852 // cheat a bit and use some of the existing state: the empty bit.
853 // Virtual bases and virtual methods make a class non-empty, but they
854 // also make it non-standard-layout so we needn't check here.
855 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000856 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000857 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000858 // data member must come through here with Empty still true, and Empty
859 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000860 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000861 for (const auto &BI : bases()) {
862 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000863 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000864 break;
865 }
866 }
867 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000868
869 // Keep track of the presence of mutable fields.
870 if (FieldRec->hasMutableFields())
871 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000872
873 // C++11 [class.copy]p13:
874 // If the implicitly-defined constructor would satisfy the
875 // requirements of a constexpr constructor, the implicitly-defined
876 // constructor is constexpr.
877 // C++11 [dcl.constexpr]p4:
878 // -- every constructor involved in initializing non-static data
879 // members [...] shall be a constexpr constructor
880 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000881 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000882 // The standard requires any in-class initializer to be a constant
883 // expression. We consider this to be a defect.
884 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000885
886 // C++11 [class.copy]p8:
887 // The implicitly-declared copy constructor for a class X will have
888 // the form 'X::X(const X&)' if [...] for all the non-static data
889 // members of X that are of a class type M (or array thereof), each
890 // such class type has a copy constructor whose first parameter is
891 // of type 'const M&' or 'const volatile M&'.
892 if (!FieldRec->hasCopyConstructorWithConstParam())
893 data().ImplicitCopyConstructorHasConstParam = false;
894
895 // C++11 [class.copy]p18:
896 // The implicitly-declared copy assignment oeprator for a class X will
897 // have the form 'X& X::operator=(const X&)' if [...] for all the
898 // non-static data members of X that are of a class type M (or array
899 // thereof), each such class type has a copy assignment operator whose
900 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
901 if (!FieldRec->hasCopyAssignmentWithConstParam())
902 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000903
904 if (FieldRec->hasUninitializedReferenceMember() &&
905 !Field->hasInClassInitializer())
906 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000907
908 // C++11 [class.union]p8, DR1460:
909 // a non-static data member of an anonymous union that is a member of
910 // X is also a variant member of X.
911 if (FieldRec->hasVariantMembers() &&
912 Field->isAnonymousStructOrUnion())
913 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000914 }
Richard Smithcc36f692011-12-22 02:22:31 +0000915 } else {
916 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000917 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000918 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000919 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000920
921 // C++11 [class.copy]p23:
922 // A defaulted copy/move assignment operator for a class X is defined
923 // as deleted if X has:
924 // -- a non-static data member of const non-class type (or array
925 // thereof)
926 if (T.isConstQualified())
927 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000928 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000929
930 // C++0x [class]p7:
931 // A standard-layout class is a class that:
932 // [...]
933 // -- either has no non-static data members in the most derived
934 // class and at most one base class with non-static data members,
935 // or has no base classes with non-static data members, and
936 // At this point we know that we have a non-static data member, so the last
937 // clause holds.
938 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000939 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000940
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000941 // If this is not a zero-length bit-field, then the class is not empty.
942 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000943 if (!Field->isBitField() ||
944 (!Field->getBitWidth()->isTypeDependent() &&
945 !Field->getBitWidth()->isValueDependent() &&
946 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000947 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000948 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000949 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000950
951 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000952 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000953 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000954 == DeclarationName::CXXConversionFunctionName) {
955 ASTContext &Ctx = getASTContext();
956 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
957 }
958 }
Richard Smith12e79312016-05-13 06:47:56 +0000959
960 if (UsingDecl *Using = dyn_cast<UsingDecl>(D)) {
961 if (Using->getDeclName().getNameKind() ==
962 DeclarationName::CXXConstructorName)
963 data().HasInheritedConstructor = true;
964
965 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
966 data().HasInheritedAssignment = true;
967 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000968}
969
Richard Smith92f241f2012-12-08 02:53:02 +0000970void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
971 assert(!D->isImplicit() && !D->isUserProvided());
972
973 // The kind of special member this declaration is, if any.
974 unsigned SMKind = 0;
975
976 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
977 if (Constructor->isDefaultConstructor()) {
978 SMKind |= SMF_DefaultConstructor;
979 if (Constructor->isConstexpr())
980 data().HasConstexprDefaultConstructor = true;
981 }
982 if (Constructor->isCopyConstructor())
983 SMKind |= SMF_CopyConstructor;
984 else if (Constructor->isMoveConstructor())
985 SMKind |= SMF_MoveConstructor;
986 else if (Constructor->isConstexpr())
987 // We may now know that the constructor is constexpr.
988 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000989 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000990 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000991 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
992 data().HasIrrelevantDestructor = false;
993 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000994 SMKind |= SMF_CopyAssignment;
995 else if (D->isMoveAssignmentOperator())
996 SMKind |= SMF_MoveAssignment;
997
998 // Update which trivial / non-trivial special members we have.
999 // addedMember will have skipped this step for this member.
1000 if (D->isTrivial())
1001 data().HasTrivialSpecialMembers |= SMKind;
1002 else
1003 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1004}
1005
Joao Matose9a3ed42012-08-31 22:18:20 +00001006bool CXXRecordDecl::isCLike() const {
1007 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1008 !TemplateOrInstantiation.isNull())
1009 return false;
1010 if (!hasDefinition())
1011 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001012
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001013 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001014}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001015
Faisal Vali2b391ab2013-09-26 19:54:12 +00001016bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001017 if (!isLambda()) return false;
1018 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001019}
1020
1021CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001022 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001023 DeclarationName Name =
1024 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001025 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001026
1027 assert(!Calls.empty() && "Missing lambda call operator!");
1028 assert(Calls.size() == 1 && "More than one lambda call operator!");
1029
1030 NamedDecl *CallOp = Calls.front();
1031 if (FunctionTemplateDecl *CallOpTmpl =
1032 dyn_cast<FunctionTemplateDecl>(CallOp))
1033 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001034
1035 return cast<CXXMethodDecl>(CallOp);
1036}
1037
1038CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001039 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001040 DeclarationName Name =
1041 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001042 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001043 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001044 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1045 NamedDecl *InvokerFun = Invoker.front();
1046 if (FunctionTemplateDecl *InvokerTemplate =
1047 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1048 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1049
Faisal Vali571df122013-09-29 08:45:24 +00001050 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001051}
1052
Douglas Gregor9c702202012-02-10 07:45:31 +00001053void CXXRecordDecl::getCaptureFields(
1054 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001055 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001056 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001057 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001058
Douglas Gregorc8a73492012-02-13 15:44:47 +00001059 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001060 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001061 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001062 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001063 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001064 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001065 else if (C->capturesVariable())
1066 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001067 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001068 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001069}
1070
Faisal Vali2b391ab2013-09-26 19:54:12 +00001071TemplateParameterList *
1072CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001073 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001074 CXXMethodDecl *CallOp = getLambdaCallOperator();
1075 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1076 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001077 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001078}
Douglas Gregor9c702202012-02-10 07:45:31 +00001079
John McCall1e3a1a72010-03-15 09:07:48 +00001080static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001081 QualType T =
1082 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1083 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001084 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001085}
1086
John McCall1e3a1a72010-03-15 09:07:48 +00001087/// Collect the visible conversions of a base class.
1088///
James Dennettb5d5f812012-06-15 22:28:09 +00001089/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001090/// \param InVirtual whether this base class is a virtual base (or a base
1091/// of a virtual base)
1092/// \param Access the access along the inheritance path to this base
1093/// \param ParentHiddenTypes the conversions provided by the inheritors
1094/// of this base
1095/// \param Output the set to which to add conversions from non-virtual bases
1096/// \param VOutput the set to which to add conversions from virtual bases
1097/// \param HiddenVBaseCs the set of conversions which were hidden in a
1098/// virtual base along some inheritance path
1099static void CollectVisibleConversions(ASTContext &Context,
1100 CXXRecordDecl *Record,
1101 bool InVirtual,
1102 AccessSpecifier Access,
1103 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001104 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001105 UnresolvedSetImpl &VOutput,
1106 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1107 // The set of types which have conversions in this class or its
1108 // subclasses. As an optimization, we don't copy the derived set
1109 // unless it might change.
1110 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1111 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1112
1113 // Collect the direct conversions and figure out which conversions
1114 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001115 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1116 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1117 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001118 HiddenTypesBuffer = ParentHiddenTypes;
1119 HiddenTypes = &HiddenTypesBuffer;
1120
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001121 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001122 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1123 bool Hidden = ParentHiddenTypes.count(ConvType);
1124 if (!Hidden)
1125 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001126
1127 // If this conversion is hidden and we're in a virtual base,
1128 // remember that it's hidden along some inheritance path.
1129 if (Hidden && InVirtual)
1130 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1131
1132 // If this conversion isn't hidden, add it to the appropriate output.
1133 else if (!Hidden) {
1134 AccessSpecifier IAccess
1135 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1136
1137 if (InVirtual)
1138 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001139 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001140 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001141 }
1142 }
1143 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001144
John McCall1e3a1a72010-03-15 09:07:48 +00001145 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001146 for (const auto &I : Record->bases()) {
1147 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001148 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001149
John McCall1e3a1a72010-03-15 09:07:48 +00001150 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001151 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1152 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001153
John McCall1e3a1a72010-03-15 09:07:48 +00001154 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1155 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1156 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001157 }
John McCall1e3a1a72010-03-15 09:07:48 +00001158}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001159
John McCall1e3a1a72010-03-15 09:07:48 +00001160/// Collect the visible conversions of a class.
1161///
1162/// This would be extremely straightforward if it weren't for virtual
1163/// bases. It might be worth special-casing that, really.
1164static void CollectVisibleConversions(ASTContext &Context,
1165 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001166 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001167 // The collection of all conversions in virtual bases that we've
1168 // found. These will be added to the output as long as they don't
1169 // appear in the hidden-conversions set.
1170 UnresolvedSet<8> VBaseCs;
1171
1172 // The set of conversions in virtual bases that we've determined to
1173 // be hidden.
1174 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1175
1176 // The set of types hidden by classes derived from this one.
1177 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1178
1179 // Go ahead and collect the direct conversions and add them to the
1180 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001181 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1182 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001183 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001184 for (; ConvI != ConvE; ++ConvI)
1185 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001186
1187 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001188 for (const auto &I : Record->bases()) {
1189 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001190 if (!RT) continue;
1191
1192 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001193 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001194 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1195 }
1196
1197 // Add any unhidden conversions provided by virtual bases.
1198 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1199 I != E; ++I) {
1200 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001201 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001202 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001203}
1204
1205/// getVisibleConversionFunctions - get all conversion functions visible
1206/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001207llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001208CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001209 ASTContext &Ctx = getASTContext();
1210
1211 ASTUnresolvedSet *Set;
1212 if (bases_begin() == bases_end()) {
1213 // If root class, all conversions are visible.
1214 Set = &data().Conversions.get(Ctx);
1215 } else {
1216 Set = &data().VisibleConversions.get(Ctx);
1217 // If visible conversion list is not evaluated, evaluate it.
1218 if (!data().ComputedVisibleConversions) {
1219 CollectVisibleConversions(Ctx, this, *Set);
1220 data().ComputedVisibleConversions = true;
1221 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001222 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001223 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001224}
1225
John McCallda4458e2010-03-31 01:36:47 +00001226void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1227 // This operation is O(N) but extremely rare. Sema only uses it to
1228 // remove UsingShadowDecls in a class that were followed by a direct
1229 // declaration, e.g.:
1230 // class A : B {
1231 // using B::operator int;
1232 // operator int();
1233 // };
1234 // This is uncommon by itself and even more uncommon in conjunction
1235 // with sufficiently large numbers of directly-declared conversions
1236 // that asymptotic behavior matters.
1237
Richard Smitha4ba74c2013-08-30 04:46:40 +00001238 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001239 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1240 if (Convs[I].getDecl() == ConvDecl) {
1241 Convs.erase(I);
1242 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1243 && "conversion was found multiple times in unresolved set");
1244 return;
1245 }
1246 }
1247
1248 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001249}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001250
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001251CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001252 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001253 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001254
1255 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001256}
1257
Chandler Carruth21c90602015-12-30 03:24:14 +00001258MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1259 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1260}
1261
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001262void
1263CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1264 TemplateSpecializationKind TSK) {
1265 assert(TemplateOrInstantiation.isNull() &&
1266 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001267 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001268 TemplateOrInstantiation
1269 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1270}
1271
Chandler Carruth21c90602015-12-30 03:24:14 +00001272ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1273 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1274}
1275
1276void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1277 TemplateOrInstantiation = Template;
1278}
1279
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001280TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1281 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001282 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1283 return Spec->getSpecializationKind();
1284
Douglas Gregor06db9f52009-10-12 20:18:28 +00001285 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001286 return MSInfo->getTemplateSpecializationKind();
1287
1288 return TSK_Undeclared;
1289}
1290
1291void
1292CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1293 if (ClassTemplateSpecializationDecl *Spec
1294 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1295 Spec->setSpecializationKind(TSK);
1296 return;
1297 }
1298
Douglas Gregor06db9f52009-10-12 20:18:28 +00001299 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001300 MSInfo->setTemplateSpecializationKind(TSK);
1301 return;
1302 }
1303
David Blaikie83d382b2011-09-23 05:06:16 +00001304 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001305}
1306
Reid Klecknere7367d62014-10-14 20:28:40 +00001307const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1308 // If it's a class template specialization, find the template or partial
1309 // specialization from which it was instantiated.
1310 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1311 auto From = TD->getInstantiatedFrom();
1312 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1313 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1314 if (NewCTD->isMemberSpecialization())
1315 break;
1316 CTD = NewCTD;
1317 }
Richard Smith7a591a42015-07-08 02:22:15 +00001318 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001319 }
1320 if (auto *CTPSD =
1321 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1322 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1323 if (NewCTPSD->isMemberSpecialization())
1324 break;
1325 CTPSD = NewCTPSD;
1326 }
Richard Smith7a591a42015-07-08 02:22:15 +00001327 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001328 }
1329 }
1330
1331 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1332 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1333 const CXXRecordDecl *RD = this;
1334 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1335 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001336 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001337 }
1338 }
1339
1340 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1341 "couldn't find pattern for class template instantiation");
1342 return nullptr;
1343}
1344
Douglas Gregorbac74902010-07-01 14:13:13 +00001345CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1346 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001347 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001348
1349 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001350 = Context.DeclarationNames.getCXXDestructorName(
1351 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001352
Richard Smithcf4bdde2015-02-21 02:45:19 +00001353 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001354 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001355 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001356
David Blaikieff7d47a2012-12-19 00:45:41 +00001357 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001358 return Dtor;
1359}
1360
Richard Trieu95a192a2015-05-28 00:14:02 +00001361bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1362 // Destructor is noreturn.
1363 if (const CXXDestructorDecl *Destructor = getDestructor())
1364 if (Destructor->isNoReturn())
1365 return true;
1366
1367 // Check base classes destructor for noreturn.
1368 for (const auto &Base : bases())
1369 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1370 return true;
1371
1372 // Check fields for noreturn.
1373 for (const auto *Field : fields())
1374 if (const CXXRecordDecl *RD =
1375 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1376 if (RD->isAnyDestructorNoReturn())
1377 return true;
1378
1379 // All destructors are not noreturn.
1380 return false;
1381}
1382
Douglas Gregorb11aad82011-02-19 18:51:44 +00001383void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001384 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001385}
1386
1387void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1388 RecordDecl::completeDefinition();
1389
Douglas Gregor8fb95122010-09-29 00:15:42 +00001390 // If the class may be abstract (but hasn't been marked as such), check for
1391 // any pure final overriders.
1392 if (mayBeAbstract()) {
1393 CXXFinalOverriderMap MyFinalOverriders;
1394 if (!FinalOverriders) {
1395 getFinalOverriders(MyFinalOverriders);
1396 FinalOverriders = &MyFinalOverriders;
1397 }
1398
1399 bool Done = false;
1400 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1401 MEnd = FinalOverriders->end();
1402 M != MEnd && !Done; ++M) {
1403 for (OverridingMethods::iterator SO = M->second.begin(),
1404 SOEnd = M->second.end();
1405 SO != SOEnd && !Done; ++SO) {
1406 assert(SO->second.size() > 0 &&
1407 "All virtual functions have overridding virtual functions");
1408
1409 // C++ [class.abstract]p4:
1410 // A class is abstract if it contains or inherits at least one
1411 // pure virtual function for which the final overrider is pure
1412 // virtual.
1413 if (SO->second.front().Method->isPure()) {
1414 data().Abstract = true;
1415 Done = true;
1416 break;
1417 }
1418 }
1419 }
1420 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001421
1422 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001423 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001424 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001425 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001426}
1427
1428bool CXXRecordDecl::mayBeAbstract() const {
1429 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1430 isDependentContext())
1431 return false;
1432
Aaron Ballman574705e2014-03-13 15:41:46 +00001433 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001434 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001435 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001436 if (BaseDecl->isAbstract())
1437 return true;
1438 }
1439
1440 return false;
1441}
1442
David Blaikie68e081d2011-12-20 02:48:34 +00001443void CXXMethodDecl::anchor() { }
1444
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001445bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001446 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001447
1448 if (MD->getStorageClass() == SC_Static)
1449 return true;
1450
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001451 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1452 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001453}
1454
Rafael Espindola49e860b2012-06-26 17:45:31 +00001455static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1456 const CXXMethodDecl *BaseMD) {
1457 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1458 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1459 const CXXMethodDecl *MD = *I;
1460 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1461 return true;
1462 if (recursivelyOverrides(MD, BaseMD))
1463 return true;
1464 }
1465 return false;
1466}
1467
1468CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001469CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1470 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001471 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1472 return this;
1473
1474 // Lookup doesn't work for destructors, so handle them separately.
1475 if (isa<CXXDestructorDecl>(this)) {
1476 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001477 if (MD) {
1478 if (recursivelyOverrides(MD, this))
1479 return MD;
1480 if (MayBeBase && recursivelyOverrides(this, MD))
1481 return MD;
1482 }
Craig Topper36250ad2014-05-12 05:36:57 +00001483 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001484 }
1485
Richard Smith40c78062015-02-21 02:31:57 +00001486 for (auto *ND : RD->lookup(getDeclName())) {
1487 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001488 if (!MD)
1489 continue;
1490 if (recursivelyOverrides(MD, this))
1491 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001492 if (MayBeBase && recursivelyOverrides(this, MD))
1493 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001494 }
1495
Aaron Ballman574705e2014-03-13 15:41:46 +00001496 for (const auto &I : RD->bases()) {
1497 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001498 if (!RT)
1499 continue;
1500 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1501 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1502 if (T)
1503 return T;
1504 }
1505
Craig Topper36250ad2014-05-12 05:36:57 +00001506 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001507}
1508
Ted Kremenek21475702008-09-05 17:16:31 +00001509CXXMethodDecl *
1510CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001511 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001512 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001513 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001514 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001515 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001516 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1517 T, TInfo, SC, isInline, isConstexpr,
1518 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001519}
1520
Douglas Gregor72172e92012-01-05 21:55:30 +00001521CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001522 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001523 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001524 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001525}
1526
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001527bool CXXMethodDecl::isUsualDeallocationFunction() const {
1528 if (getOverloadedOperator() != OO_Delete &&
1529 getOverloadedOperator() != OO_Array_Delete)
1530 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001531
1532 // C++ [basic.stc.dynamic.deallocation]p2:
1533 // A template instance is never a usual deallocation function,
1534 // regardless of its signature.
1535 if (getPrimaryTemplate())
1536 return false;
1537
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001538 // C++ [basic.stc.dynamic.deallocation]p2:
1539 // If a class T has a member deallocation function named operator delete
1540 // with exactly one parameter, then that function is a usual (non-placement)
1541 // deallocation function. [...]
1542 if (getNumParams() == 1)
1543 return true;
1544
1545 // C++ [basic.stc.dynamic.deallocation]p2:
1546 // [...] If class T does not declare such an operator delete but does
1547 // declare a member deallocation function named operator delete with
1548 // exactly two parameters, the second of which has type std::size_t (18.1),
1549 // then this function is a usual deallocation function.
1550 ASTContext &Context = getASTContext();
1551 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001552 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1553 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001554 return false;
1555
1556 // This function is a usual deallocation function if there are no
1557 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001558 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1559 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001560 I != E; ++I) {
1561 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001562 if (FD->getNumParams() == 1)
1563 return false;
1564 }
1565
1566 return true;
1567}
1568
Douglas Gregorb139cd52010-05-01 20:49:11 +00001569bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001570 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001571 // A user-declared copy assignment operator X::operator= is a non-static
1572 // non-template member function of class X with exactly one parameter of
1573 // type X, X&, const X&, volatile X& or const volatile X&.
1574 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1575 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001576 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1577 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001578 return false;
1579
1580 QualType ParamType = getParamDecl(0)->getType();
1581 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1582 ParamType = Ref->getPointeeType();
1583
1584 ASTContext &Context = getASTContext();
1585 QualType ClassType
1586 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1587 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1588}
1589
Alexis Huntfcaeae42011-05-25 20:50:04 +00001590bool CXXMethodDecl::isMoveAssignmentOperator() const {
1591 // C++0x [class.copy]p19:
1592 // A user-declared move assignment operator X::operator= is a non-static
1593 // non-template member function of class X with exactly one parameter of type
1594 // X&&, const X&&, volatile X&&, or const volatile X&&.
1595 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001596 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1597 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001598 return false;
1599
1600 QualType ParamType = getParamDecl(0)->getType();
1601 if (!isa<RValueReferenceType>(ParamType))
1602 return false;
1603 ParamType = ParamType->getPointeeType();
1604
1605 ASTContext &Context = getASTContext();
1606 QualType ClassType
1607 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1608 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1609}
1610
Anders Carlsson36d87e12009-05-16 23:58:37 +00001611void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001612 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001613 assert(!MD->getParent()->isDependentContext() &&
1614 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001615 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001616
Douglas Gregor832940b2010-03-02 23:58:15 +00001617 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001618}
1619
1620CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001621 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001622 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001623}
1624
1625CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001626 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001627 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001628}
1629
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001630unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001631 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001632 return getASTContext().overridden_methods_size(this);
1633}
1634
Ted Kremenek21475702008-09-05 17:16:31 +00001635QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001636 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1637 // If the member function is declared const, the type of this is const X*,
1638 // if the member function is declared volatile, the type of this is
1639 // volatile X*, and if the member function is declared const volatile,
1640 // the type of this is const volatile X*.
1641
Ted Kremenek21475702008-09-05 17:16:31 +00001642 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001643
John McCalle78aac42010-03-10 03:28:59 +00001644 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001645 ClassTy = C.getQualifiedType(ClassTy,
1646 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001647 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001648}
1649
Eli Friedman71a26d82009-12-06 20:50:05 +00001650bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001651 // If this function is a template instantiation, look at the template from
1652 // which it was instantiated.
1653 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1654 if (!CheckFn)
1655 CheckFn = this;
1656
Eli Friedman71a26d82009-12-06 20:50:05 +00001657 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001658 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001659}
1660
Douglas Gregor355efbb2012-02-17 03:02:34 +00001661bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001662 const CXXRecordDecl *P = getParent();
1663 if (P->isLambda()) {
1664 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1665 if (StaticInvoker == this) return true;
1666 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1667 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1668 }
1669 }
1670 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001671}
1672
Alexis Hunt1d792652011-01-08 20:30:50 +00001673CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1674 TypeSourceInfo *TInfo, bool IsVirtual,
1675 SourceLocation L, Expr *Init,
1676 SourceLocation R,
1677 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001678 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001679 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1680 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001681{
Douglas Gregore8381c02008-11-05 04:29:56 +00001682}
1683
Alexis Hunt1d792652011-01-08 20:30:50 +00001684CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1685 FieldDecl *Member,
1686 SourceLocation MemberLoc,
1687 SourceLocation L, Expr *Init,
1688 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001689 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001690 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001691 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1692{
1693}
1694
Alexis Hunt1d792652011-01-08 20:30:50 +00001695CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1696 IndirectFieldDecl *Member,
1697 SourceLocation MemberLoc,
1698 SourceLocation L, Expr *Init,
1699 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001700 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001701 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001702 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001703{
Douglas Gregore8381c02008-11-05 04:29:56 +00001704}
1705
Alexis Hunt1d792652011-01-08 20:30:50 +00001706CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001707 TypeSourceInfo *TInfo,
1708 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001709 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001710 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1711 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001712 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1713{
1714}
1715
1716CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001717 FieldDecl *Member,
1718 SourceLocation MemberLoc,
1719 SourceLocation L, Expr *Init,
1720 SourceLocation R,
1721 VarDecl **Indices,
1722 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001723 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001724 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001725 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001726{
James Y Knight967eb202015-12-29 22:13:13 +00001727 std::uninitialized_copy(Indices, Indices + NumIndices,
1728 getTrailingObjects<VarDecl *>());
Douglas Gregor94f9a482010-05-05 05:51:00 +00001729}
1730
Alexis Hunt1d792652011-01-08 20:30:50 +00001731CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1732 FieldDecl *Member,
1733 SourceLocation MemberLoc,
1734 SourceLocation L, Expr *Init,
1735 SourceLocation R,
1736 VarDecl **Indices,
1737 unsigned NumIndices) {
James Y Knight967eb202015-12-29 22:13:13 +00001738 void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
Alexis Hunt1d792652011-01-08 20:30:50 +00001739 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001740 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1741 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001742}
1743
Alexis Hunt1d792652011-01-08 20:30:50 +00001744TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001745 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001746 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001747 else
1748 return TypeLoc();
1749}
1750
Alexis Hunt1d792652011-01-08 20:30:50 +00001751const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001752 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001753 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001754 else
Craig Topper36250ad2014-05-12 05:36:57 +00001755 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001756}
1757
Alexis Hunt1d792652011-01-08 20:30:50 +00001758SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001759 if (isInClassMemberInitializer())
1760 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001761
David Blaikiea81d4102015-01-18 00:12:58 +00001762 if (isAnyMemberInitializer())
1763 return getMemberLocation();
1764
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001765 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1766 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1767
1768 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001769}
1770
Alexis Hunt1d792652011-01-08 20:30:50 +00001771SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001772 if (isInClassMemberInitializer()) {
1773 FieldDecl *D = getAnyMember();
1774 if (Expr *I = D->getInClassInitializer())
1775 return I->getSourceRange();
1776 return SourceRange();
1777 }
1778
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001779 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001780}
1781
David Blaikie68e081d2011-12-20 02:48:34 +00001782void CXXConstructorDecl::anchor() { }
1783
Douglas Gregor61956c42008-10-31 09:07:45 +00001784CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001785CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001786 return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001787 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001788 nullptr, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001789}
1790
1791CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001792CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001793 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001794 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001795 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001796 bool isExplicit, bool isInline,
1797 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001798 assert(NameInfo.getName().getNameKind()
1799 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001800 "Name must refer to a constructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001801 return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001802 isExplicit, isInline,
1803 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001804}
1805
Richard Smithc2bb8182015-03-24 06:36:48 +00001806CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1807 return CtorInitializers.get(getASTContext().getExternalSource());
1808}
1809
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001810CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1811 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1812 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1813 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1814 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001815
1816 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001817}
1818
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001819bool CXXConstructorDecl::isDefaultConstructor() const {
1820 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001821 // A default constructor for a class X is a constructor of class
1822 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001823 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001824 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001825}
1826
Mike Stump11289f42009-09-09 15:08:12 +00001827bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001828CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001829 return isCopyOrMoveConstructor(TypeQuals) &&
1830 getParamDecl(0)->getType()->isLValueReferenceType();
1831}
1832
1833bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1834 return isCopyOrMoveConstructor(TypeQuals) &&
1835 getParamDecl(0)->getType()->isRValueReferenceType();
1836}
1837
1838/// \brief Determine whether this is a copy or move constructor.
1839bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001840 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001841 // A non-template constructor for class X is a copy constructor
1842 // if its first parameter is of type X&, const X&, volatile X& or
1843 // const volatile X&, and either there are no other parameters
1844 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001845 // C++0x [class.copy]p3:
1846 // A non-template constructor for class X is a move constructor if its
1847 // first parameter is of type X&&, const X&&, volatile X&&, or
1848 // const volatile X&&, and either there are no other parameters or else
1849 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001850 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001851 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001852 (getPrimaryTemplate() != nullptr) ||
1853 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001854 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001855
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001856 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001857
1858 // Do we have a reference type?
1859 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001860 if (!ParamRefType)
1861 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001862
Douglas Gregorff7028a2009-11-13 23:59:09 +00001863 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001864 ASTContext &Context = getASTContext();
1865
Douglas Gregorff7028a2009-11-13 23:59:09 +00001866 CanQualType PointeeType
1867 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001868 CanQualType ClassTy
1869 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001870 if (PointeeType.getUnqualifiedType() != ClassTy)
1871 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001872
John McCall8ccfcb52009-09-24 19:53:00 +00001873 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001874
1875 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001876 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001877 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001878}
1879
Anders Carlssond20e7952009-08-28 16:57:08 +00001880bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001881 // C++ [class.conv.ctor]p1:
1882 // A constructor declared without the function-specifier explicit
1883 // that can be called with a single parameter specifies a
1884 // conversion from the type of its first parameter to the type of
1885 // its class. Such a constructor is called a converting
1886 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001887 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001888 return false;
1889
Mike Stump11289f42009-09-09 15:08:12 +00001890 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001891 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001892 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001893 (getNumParams() > 1 &&
1894 (getParamDecl(1)->hasDefaultArg() ||
1895 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001896}
Douglas Gregor61956c42008-10-31 09:07:45 +00001897
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001898bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001899 if ((getNumParams() < 1) ||
1900 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001901 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001902 return false;
1903
1904 const ParmVarDecl *Param = getParamDecl(0);
1905
1906 ASTContext &Context = getASTContext();
1907 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1908
Douglas Gregorffe14e32009-11-14 01:20:54 +00001909 // Is it the same as our our class type?
1910 CanQualType ClassTy
1911 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1912 if (ParamType.getUnqualifiedType() != ClassTy)
1913 return false;
1914
1915 return true;
1916}
1917
Sebastian Redl08905022011-02-05 19:23:19 +00001918const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1919 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001920 method_iterator It = getASTContext().overridden_methods_begin(this);
1921 if (It == getASTContext().overridden_methods_end(this))
Craig Topper36250ad2014-05-12 05:36:57 +00001922 return nullptr;
Sebastian Redl08905022011-02-05 19:23:19 +00001923
1924 return cast<CXXConstructorDecl>(*It);
1925}
1926
1927void
1928CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1929 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001930 assert(getASTContext().overridden_methods_size(this) == 0 &&
1931 "Base ctor already set.");
1932 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001933}
1934
David Blaikie68e081d2011-12-20 02:48:34 +00001935void CXXDestructorDecl::anchor() { }
1936
Douglas Gregor831c93f2008-11-05 20:51:48 +00001937CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001938CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001939 return new (C, ID)
1940 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1941 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001942}
1943
1944CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001945CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001946 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001947 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001948 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001949 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001950 assert(NameInfo.getName().getNameKind()
1951 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001952 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001953 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001954 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001955}
1956
Richard Smithf8134002015-03-10 01:41:22 +00001957void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1958 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1959 if (OD && !First->OperatorDelete) {
1960 First->OperatorDelete = OD;
1961 if (auto *L = getASTMutationListener())
1962 L->ResolvedOperatorDelete(First, OD);
1963 }
1964}
1965
David Blaikie68e081d2011-12-20 02:48:34 +00001966void CXXConversionDecl::anchor() { }
1967
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001968CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001969CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001970 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001971 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001972 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00001973 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001974}
1975
1976CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001977CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001978 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001979 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001980 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001981 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001982 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001983 assert(NameInfo.getName().getNameKind()
1984 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001985 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00001986 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001987 isInline, isExplicit, isConstexpr,
1988 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001989}
1990
Douglas Gregord3b672c2012-02-16 01:06:16 +00001991bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1992 return isImplicit() && getParent()->isLambda() &&
1993 getConversionType()->isBlockPointerType();
1994}
1995
David Blaikie68e081d2011-12-20 02:48:34 +00001996void LinkageSpecDecl::anchor() { }
1997
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001998LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001999 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002000 SourceLocation ExternLoc,
2001 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002002 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002003 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002004 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002005}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002006
Richard Smithf7981722013-11-22 09:01:48 +00002007LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2008 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002009 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2010 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002011}
2012
David Blaikie68e081d2011-12-20 02:48:34 +00002013void UsingDirectiveDecl::anchor() { }
2014
Douglas Gregor889ceb72009-02-03 19:21:40 +00002015UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2016 SourceLocation L,
2017 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002018 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002019 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002020 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002021 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00002022 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
2023 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002024 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2025 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002026}
2027
Richard Smithf7981722013-11-22 09:01:48 +00002028UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2029 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002030 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2031 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002032 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002033 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002034}
2035
Sebastian Redla6602e92009-11-23 15:34:23 +00002036NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2037 if (NamespaceAliasDecl *NA =
2038 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2039 return NA->getNamespace();
2040 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2041}
2042
Richard Smith053f6c62014-05-16 23:01:30 +00002043NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2044 SourceLocation StartLoc, SourceLocation IdLoc,
2045 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2046 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2047 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2048 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002049 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002050
Douglas Gregore57e7522012-01-07 09:11:48 +00002051 if (PrevDecl)
2052 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2053}
2054
Douglas Gregor72172e92012-01-05 21:55:30 +00002055NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002056 bool Inline, SourceLocation StartLoc,
2057 SourceLocation IdLoc, IdentifierInfo *Id,
2058 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002059 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2060 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002061}
2062
2063NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002064 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002065 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002066}
2067
Chandler Carruth21c90602015-12-30 03:24:14 +00002068NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2069 if (isFirstDecl())
2070 return this;
2071
2072 return AnonOrFirstNamespaceAndInline.getPointer();
2073}
2074
2075const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2076 if (isFirstDecl())
2077 return this;
2078
2079 return AnonOrFirstNamespaceAndInline.getPointer();
2080}
2081
2082bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2083
Richard Smithd7af8a32014-05-10 01:17:36 +00002084NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2085 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002086}
2087NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2088 return getPreviousDecl();
2089}
2090NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2091 return getMostRecentDecl();
2092}
2093
David Blaikie68e081d2011-12-20 02:48:34 +00002094void NamespaceAliasDecl::anchor() { }
2095
Richard Smithf4634362014-09-03 23:11:22 +00002096NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2097 return getNextRedeclaration();
2098}
2099NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2100 return getPreviousDecl();
2101}
2102NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2103 return getMostRecentDecl();
2104}
2105
Mike Stump11289f42009-09-09 15:08:12 +00002106NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002107 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002108 SourceLocation AliasLoc,
2109 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002110 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002111 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002112 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002113 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002114 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2115 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002116 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002117 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002118}
2119
Douglas Gregor72172e92012-01-05 21:55:30 +00002120NamespaceAliasDecl *
2121NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002122 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002123 SourceLocation(), nullptr,
2124 NestedNameSpecifierLoc(),
2125 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002126}
2127
David Blaikie68e081d2011-12-20 02:48:34 +00002128void UsingShadowDecl::anchor() { }
2129
Douglas Gregor72172e92012-01-05 21:55:30 +00002130UsingShadowDecl *
2131UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002132 return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2133 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002134}
2135
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002136UsingDecl *UsingShadowDecl::getUsingDecl() const {
2137 const UsingShadowDecl *Shadow = this;
2138 while (const UsingShadowDecl *NextShadow =
2139 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2140 Shadow = NextShadow;
2141 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2142}
2143
David Blaikie68e081d2011-12-20 02:48:34 +00002144void UsingDecl::anchor() { }
2145
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002146void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2147 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2148 "declaration already in set");
2149 assert(S->getUsingDecl() == this);
2150
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002151 if (FirstUsingShadow.getPointer())
2152 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2153 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002154}
2155
2156void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2157 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2158 "declaration not in set");
2159 assert(S->getUsingDecl() == this);
2160
2161 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2162
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002163 if (FirstUsingShadow.getPointer() == S) {
2164 FirstUsingShadow.setPointer(
2165 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002166 S->UsingOrNextShadow = this;
2167 return;
2168 }
2169
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002170 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002171 while (Prev->UsingOrNextShadow != S)
2172 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2173 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2174 S->UsingOrNextShadow = this;
2175}
2176
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002177UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2178 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002179 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002180 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002181 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002182}
2183
Douglas Gregor72172e92012-01-05 21:55:30 +00002184UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002185 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2186 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2187 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002188}
2189
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002190SourceRange UsingDecl::getSourceRange() const {
2191 SourceLocation Begin = isAccessDeclaration()
2192 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2193 return SourceRange(Begin, getNameInfo().getEndLoc());
2194}
2195
David Blaikie68e081d2011-12-20 02:48:34 +00002196void UnresolvedUsingValueDecl::anchor() { }
2197
John McCalle61f2ba2009-11-18 02:36:19 +00002198UnresolvedUsingValueDecl *
2199UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2200 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002201 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002202 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002203 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2204 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002205}
2206
Douglas Gregor72172e92012-01-05 21:55:30 +00002207UnresolvedUsingValueDecl *
2208UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002209 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2210 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002211 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002212 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002213}
2214
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002215SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2216 SourceLocation Begin = isAccessDeclaration()
2217 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2218 return SourceRange(Begin, getNameInfo().getEndLoc());
2219}
2220
David Blaikie68e081d2011-12-20 02:48:34 +00002221void UnresolvedUsingTypenameDecl::anchor() { }
2222
John McCalle61f2ba2009-11-18 02:36:19 +00002223UnresolvedUsingTypenameDecl *
2224UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2225 SourceLocation UsingLoc,
2226 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002227 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002228 SourceLocation TargetNameLoc,
2229 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002230 return new (C, DC) UnresolvedUsingTypenameDecl(
2231 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2232 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002233}
2234
Douglas Gregor72172e92012-01-05 21:55:30 +00002235UnresolvedUsingTypenameDecl *
2236UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002237 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002238 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2239 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002240}
2241
David Blaikie68e081d2011-12-20 02:48:34 +00002242void StaticAssertDecl::anchor() { }
2243
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002244StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002245 SourceLocation StaticAssertLoc,
2246 Expr *AssertExpr,
2247 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002248 SourceLocation RParenLoc,
2249 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002250 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2251 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002252}
2253
Richard Smithf7981722013-11-22 09:01:48 +00002254StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002255 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002256 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2257 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002258}
2259
2260MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2261 SourceLocation L, DeclarationName N,
2262 QualType T, TypeSourceInfo *TInfo,
2263 SourceLocation StartL,
2264 IdentifierInfo *Getter,
2265 IdentifierInfo *Setter) {
2266 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2267}
2268
2269MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2270 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002271 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2272 DeclarationName(), QualType(), nullptr,
2273 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002274}
2275
Anders Carlsson6750d162009-03-26 23:46:50 +00002276static const char *getAccessName(AccessSpecifier AS) {
2277 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002278 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002279 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002280 case AS_public:
2281 return "public";
2282 case AS_private:
2283 return "private";
2284 case AS_protected:
2285 return "protected";
2286 }
David Blaikief47fa302012-01-17 02:30:50 +00002287 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002288}
2289
2290const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2291 AccessSpecifier AS) {
2292 return DB << getAccessName(AS);
2293}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002294
2295const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2296 AccessSpecifier AS) {
2297 return DB << getAccessName(AS);
2298}