blob: 65bfc49daec431fa0f28cc938c61bafeaf72fbba [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),
56 NeedOverloadResolutionForMoveConstructor(false),
57 NeedOverloadResolutionForMoveAssignment(false),
58 NeedOverloadResolutionForDestructor(false),
59 DefaultedMoveConstructorIsDeleted(false),
60 DefaultedMoveAssignmentIsDeleted(false),
61 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
62 DeclaredNonTrivialSpecialMembers(0), HasIrrelevantDestructor(true),
63 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000064 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000065 DefaultedDefaultConstructorIsConstexpr(true),
66 HasConstexprDefaultConstructor(false),
67 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
68 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
69 ImplicitCopyConstructorHasConstParam(true),
70 ImplicitCopyAssignmentHasConstParam(true),
71 HasDeclaredCopyConstructorWithConstParam(false),
72 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
73 IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
74 VBases(), Definition(D), FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000075
Benjamin Kramer300c0632012-07-04 17:03:33 +000076CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
77 return Bases.get(Definition->getASTContext().getExternalSource());
78}
79
80CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
81 return VBases.get(Definition->getASTContext().getExternalSource());
82}
83
Richard Smith053f6c62014-05-16 23:01:30 +000084CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
85 DeclContext *DC, SourceLocation StartLoc,
86 SourceLocation IdLoc, IdentifierInfo *Id,
87 CXXRecordDecl *PrevDecl)
88 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
89 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smith64c06302014-05-22 23:19:02 +000090 : DefinitionDataPtr(this)),
Richard Smith053f6c62014-05-16 23:01:30 +000091 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000092
Jay Foad39c79802011-01-12 09:06:06 +000093CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000094 DeclContext *DC, SourceLocation StartLoc,
95 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000096 CXXRecordDecl* PrevDecl,
97 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +000098 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +000099 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000100 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000102 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000103 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000104 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000105 return R;
106}
107
Richard Smith053f6c62014-05-16 23:01:30 +0000108CXXRecordDecl *
109CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
110 TypeSourceInfo *Info, SourceLocation Loc,
111 bool Dependent, bool IsGeneric,
112 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000113 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000114 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
115 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000116 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000117 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000118 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000119 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000120 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000121 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000122 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000123 return R;
124}
125
Douglas Gregor72172e92012-01-05 21:55:30 +0000126CXXRecordDecl *
127CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000128 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000129 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000130 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000131 R->MayHaveOutOfDateDef = false;
132 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135void
Craig Toppere6337e12015-12-25 00:36:02 +0000136CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
137 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000138 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000139
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000140 if (!data().Bases.isOffset() && data().NumBases > 0)
141 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000142
Craig Toppere6337e12015-12-25 00:36:02 +0000143 if (NumBases) {
Richard Smithaed32b42011-10-18 20:08:55 +0000144 // C++ [dcl.init.aggr]p1:
145 // An aggregate is [...] a class with [...] no base classes [...].
146 data().Aggregate = false;
147
148 // C++ [class]p4:
149 // A POD-struct is an aggregate class...
150 data().PlainOldData = false;
151 }
152
Anders Carlssonabb20e62010-03-29 05:13:12 +0000153 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000154 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000155
156 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000157 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000158
Craig Toppere6337e12015-12-25 00:36:02 +0000159 data().Bases = new(C) CXXBaseSpecifier [NumBases];
160 data().NumBases = NumBases;
161 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000162 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000163 // Keep track of inherited vbases for this base class.
164 const CXXBaseSpecifier *Base = Bases[i];
165 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000166 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000167 if (BaseType->isDependentType())
168 continue;
169 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000170 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000171
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000172 // A class with a non-empty base class is not empty.
173 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000174 if (!BaseClassDecl->isEmpty()) {
175 if (!data().Empty) {
176 // C++0x [class]p7:
177 // A standard-layout class is a class that:
178 // [...]
179 // -- either has no non-static data members in the most derived
180 // class and at most one base class with non-static data members,
181 // or has no base classes with non-static data members, and
182 // If this is the second non-empty base, then neither of these two
183 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000184 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000185 }
186
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000187 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000188 data().HasNoNonEmptyBases = false;
189 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000190
Douglas Gregor11c024b2010-09-28 20:50:54 +0000191 // C++ [class.virtual]p1:
192 // A class that declares or inherits a virtual function is called a
193 // polymorphic class.
194 if (BaseClassDecl->isPolymorphic())
195 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000196
Chandler Carruthb1963742011-04-30 09:17:45 +0000197 // C++0x [class]p7:
198 // A standard-layout class is a class that: [...]
199 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000200 if (!BaseClassDecl->isStandardLayout())
201 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000202
Chandler Carruthe71d0622011-04-24 02:49:34 +0000203 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000204 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000205 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000206
Anders Carlssonabb20e62010-03-29 05:13:12 +0000207 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000208 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000209 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000210 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000211 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000212
213 // C++11 [class.copy]p8:
214 // The implicitly-declared copy constructor for a class X will have
215 // the form 'X::X(const X&)' if each [...] virtual base class B of X
216 // has a copy constructor whose first parameter is of type
217 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000218 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000219 if (!VBaseDecl->hasCopyConstructorWithConstParam())
220 data().ImplicitCopyConstructorHasConstParam = false;
221 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000222 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000223
224 if (Base->isVirtual()) {
225 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000226 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000227 VBases.push_back(Base);
228
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000229 // C++0x [meta.unary.prop] is_empty:
230 // T is a class type, but not a union type, with ... no virtual base
231 // classes
232 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000233
Richard Smith328aae52012-11-30 05:11:39 +0000234 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
235 // A [default constructor, copy/move constructor, or copy/move assignment
236 // operator for a class X] is trivial [...] if:
237 // -- class X has [...] no virtual base classes
238 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000239
240 // C++0x [class]p7:
241 // A standard-layout class is a class that: [...]
242 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000243 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000244
245 // C++11 [dcl.constexpr]p4:
246 // In the definition of a constexpr constructor [...]
247 // -- the class shall not have any virtual base classes
248 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000249 } else {
250 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000251 // A default constructor is trivial [...] if:
252 // -- all the direct base classes of its class have trivial default
253 // constructors.
254 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000255 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
256
Chandler Carruthad7d4042011-04-23 23:10:33 +0000257 // C++0x [class.copy]p13:
258 // A copy/move constructor for class X is trivial if [...]
259 // [...]
260 // -- the constructor selected to copy/move each direct base class
261 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000262 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000263 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000264 // If the base class doesn't have a simple move constructor, we'll eagerly
265 // declare it and perform overload resolution to determine which function
266 // it actually calls. If it does have a simple move constructor, this
267 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000268 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000269 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000270
271 // C++0x [class.copy]p27:
272 // A copy/move assignment operator for class X is trivial if [...]
273 // [...]
274 // -- the assignment operator selected to copy/move each direct base
275 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000276 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000277 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000278 // If the base class doesn't have a simple move assignment, we'll eagerly
279 // declare it and perform overload resolution to determine which function
280 // it actually calls. If it does have a simple move assignment, this
281 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000282 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000283 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000284
285 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000286 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000287 // requirements of a constexpr constructor, the implicitly-defined
288 // default constructor is constexpr.
289 if (!BaseClassDecl->hasConstexprDefaultConstructor())
290 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000291 }
Richard Smith92f241f2012-12-08 02:53:02 +0000292
Douglas Gregor11c024b2010-09-28 20:50:54 +0000293 // C++ [class.ctor]p3:
294 // A destructor is trivial if all the direct base classes of its class
295 // have trivial destructors.
296 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000297 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000298
299 if (!BaseClassDecl->hasIrrelevantDestructor())
300 data().HasIrrelevantDestructor = false;
301
Richard Smith1c33fe82012-11-28 06:23:12 +0000302 // C++11 [class.copy]p18:
303 // The implicitly-declared copy assignment oeprator for a class X will
304 // have the form 'X& X::operator=(const X&)' if each direct base class B
305 // of X has a copy assignment operator whose parameter is of type 'const
306 // B&', 'const volatile B&', or 'B' [...]
307 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
308 data().ImplicitCopyAssignmentHasConstParam = false;
309
310 // C++11 [class.copy]p8:
311 // The implicitly-declared copy constructor for a class X will have
312 // the form 'X::X(const X&)' if each direct [...] base class B of X
313 // has a copy constructor whose first parameter is of type
314 // 'const B&' or 'const volatile B&' [...]
315 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
316 data().ImplicitCopyConstructorHasConstParam = false;
317
John McCall31168b02011-06-15 23:02:42 +0000318 // A class has an Objective-C object member if... or any of its bases
319 // has an Objective-C object member.
320 if (BaseClassDecl->hasObjectMember())
321 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000322
323 if (BaseClassDecl->hasVolatileMember())
324 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000325
Douglas Gregor61226d32011-05-13 01:05:07 +0000326 // Keep track of the presence of mutable fields.
327 if (BaseClassDecl->hasMutableFields())
328 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000329
330 if (BaseClassDecl->hasUninitializedReferenceMember())
331 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000332
Nico Weber6a6376b2016-02-19 01:52:46 +0000333 if (!BaseClassDecl->allowConstDefaultInit())
334 data().HasUninitializedFields = true;
335
Richard Smith6b02d462012-12-08 08:32:28 +0000336 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000337 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000338
David Majnemer5ef4fe72014-06-13 06:43:46 +0000339 if (VBases.empty()) {
340 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000341 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000342 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000343
344 // Create base specifier for any direct or indirect virtual bases.
345 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
346 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000347 for (int I = 0, E = VBases.size(); I != E; ++I) {
348 QualType Type = VBases[I]->getType();
349 if (!Type->isDependentType())
350 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000351 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000352 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000353
354 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000355}
356
357void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
358 // C++11 [class.copy]p11:
359 // A defaulted copy/move constructor for a class X is defined as
360 // deleted if X has:
361 // -- a direct or virtual base class B that cannot be copied/moved [...]
362 // -- a non-static data member of class type M (or array thereof)
363 // that cannot be copied or moved [...]
364 if (!Subobj->hasSimpleMoveConstructor())
365 data().NeedOverloadResolutionForMoveConstructor = true;
366
367 // C++11 [class.copy]p23:
368 // A defaulted copy/move assignment operator for a class X is defined as
369 // deleted if X has:
370 // -- a direct or virtual base class B that cannot be copied/moved [...]
371 // -- a non-static data member of class type M (or array thereof)
372 // that cannot be copied or moved [...]
373 if (!Subobj->hasSimpleMoveAssignment())
374 data().NeedOverloadResolutionForMoveAssignment = true;
375
376 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
377 // A defaulted [ctor or dtor] for a class X is defined as
378 // deleted if X has:
379 // -- any direct or virtual base class [...] has a type with a destructor
380 // that is deleted or inaccessible from the defaulted [ctor or dtor].
381 // -- any non-static data member has a type with a destructor
382 // that is deleted or inaccessible from the defaulted [ctor or dtor].
383 if (!Subobj->hasSimpleDestructor()) {
384 data().NeedOverloadResolutionForMoveConstructor = true;
385 data().NeedOverloadResolutionForDestructor = true;
386 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000387}
388
Douglas Gregord2e6a452010-01-14 17:47:39 +0000389bool CXXRecordDecl::hasAnyDependentBases() const {
390 if (!isDependentContext())
391 return false;
392
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000393 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000394}
395
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000396bool CXXRecordDecl::isTriviallyCopyable() const {
397 // C++0x [class]p5:
398 // A trivially copyable class is a class that:
399 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000400 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000401 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000402 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000403 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000404 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000405 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000406 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000407 // -- has a trivial destructor.
408 if (!hasTrivialDestructor()) return false;
409
410 return true;
411}
412
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000413void CXXRecordDecl::markedVirtualFunctionPure() {
414 // C++ [class.abstract]p2:
415 // A class is abstract if it has at least one pure virtual function.
416 data().Abstract = true;
417}
418
419void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000420 if (!D->isImplicit() &&
421 !isa<FieldDecl>(D) &&
422 !isa<IndirectFieldDecl>(D) &&
423 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
424 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
425 data().HasOnlyCMembers = false;
426
427 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000428 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000429 return;
430
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000431 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
432 if (FunTmpl)
433 D = FunTmpl->getTemplatedDecl();
434
Douglas Gregora832d3e2010-09-28 19:45:33 +0000435 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
436 if (Method->isVirtual()) {
437 // C++ [dcl.init.aggr]p1:
438 // An aggregate is an array or a class with [...] no virtual functions.
439 data().Aggregate = false;
440
441 // C++ [class]p4:
442 // A POD-struct is an aggregate class...
443 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000444
445 // Virtual functions make the class non-empty.
446 // FIXME: Standard ref?
447 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000448
449 // C++ [class.virtual]p1:
450 // A class that declares or inherits a virtual function is called a
451 // polymorphic class.
452 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000453
Richard Smith328aae52012-11-30 05:11:39 +0000454 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
455 // A [default constructor, copy/move constructor, or copy/move
456 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000457 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000458 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000459
Chandler Carruthb1963742011-04-30 09:17:45 +0000460 // C++0x [class]p7:
461 // A standard-layout class is a class that: [...]
462 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000463 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000464 }
465 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000466
Richard Smith1c33fe82012-11-28 06:23:12 +0000467 // Notify the listener if an implicit member was added after the definition
468 // was completed.
469 if (!isBeingDefined() && D->isImplicit())
470 if (ASTMutationListener *L = getASTMutationListener())
471 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000472
Richard Smith328aae52012-11-30 05:11:39 +0000473 // The kind of special member this declaration is, if any.
474 unsigned SMKind = 0;
475
Richard Smith1c33fe82012-11-28 06:23:12 +0000476 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000477 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000478 if (!Constructor->isImplicit()) {
479 // Note that we have a user-declared constructor.
480 data().UserDeclaredConstructor = true;
481
482 // C++ [class]p4:
483 // A POD-struct is an aggregate class [...]
484 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
485 // type is technically an aggregate in C++0x since it wouldn't be in 03.
486 data().PlainOldData = false;
487 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000488
Richard Smitha5285072011-09-05 02:13:09 +0000489 // Technically, "user-provided" is only defined for special member
490 // functions, but the intent of the standard is clearly that it should apply
491 // to all functions.
492 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000493
Alexis Hunt88c75c32011-05-09 21:45:35 +0000494 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000495 SMKind |= SMF_DefaultConstructor;
496
497 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000498 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000499 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000500 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000501 if (Constructor->isDefaulted())
502 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000503 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000504
Chandler Carruthad7d4042011-04-23 23:10:33 +0000505 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000506 unsigned Quals;
507 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000508 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000509
510 if (Quals & Qualifiers::Const)
511 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000512 } else if (Constructor->isMoveConstructor())
513 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000514 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000515
516 // Record if we see any constexpr constructors which are neither copy
517 // nor move constructors.
518 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000519 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000520
Alexis Hunt88c75c32011-05-09 21:45:35 +0000521 // C++ [dcl.init.aggr]p1:
522 // An aggregate is an array or a class with no user-declared
523 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000524 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000525 // An aggregate is an array or a class with no user-provided
526 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000527 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000528 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000529 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000530 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000531
Richard Smith1c33fe82012-11-28 06:23:12 +0000532 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000533 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000534 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000535
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000536 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000537 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000538 // If the destructor is explicitly defaulted and not trivial or not public
539 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
540 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000541
Richard Smith1c33fe82012-11-28 06:23:12 +0000542 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000543 // A destructor is trivial if [...] the destructor is not virtual.
544 if (DD->isVirtual())
545 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000546 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000547
548 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000549 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000550 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000551 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000552
553 const ReferenceType *ParamTy =
554 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
555 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
556 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000557 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000558
Richard Smith328aae52012-11-30 05:11:39 +0000559 if (Method->isMoveAssignmentOperator())
560 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000561
Douglas Gregor457104e2010-09-29 04:25:11 +0000562 // Keep the list of conversion functions up-to-date.
563 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000564 // FIXME: We use the 'unsafe' accessor for the access specifier here,
565 // because Sema may not have set it yet. That's really just a misdesign
566 // in Sema. However, LLDB *will* have set the access specifier correctly,
567 // and adds declarations after the class is technically completed,
568 // so completeDefinition()'s overriding of the access specifiers doesn't
569 // work.
570 AccessSpecifier AS = Conversion->getAccessUnsafe();
571
Richard Smith328aae52012-11-30 05:11:39 +0000572 if (Conversion->getPrimaryTemplate()) {
573 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000574 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000575 ASTContext &Ctx = getASTContext();
576 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
577 NamedDecl *Primary =
578 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
579 if (Primary->getPreviousDecl())
580 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
581 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000582 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000583 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000584 }
585 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000586
Richard Smith328aae52012-11-30 05:11:39 +0000587 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000588 // If this is the first declaration of a special member, we no longer have
589 // an implicit trivial special member.
590 data().HasTrivialSpecialMembers &=
591 data().DeclaredSpecialMembers | ~SMKind;
592
593 if (!Method->isImplicit() && !Method->isUserProvided()) {
594 // This method is user-declared but not user-provided. We can't work out
595 // whether it's trivial yet (not until we get to the end of the class).
596 // We'll handle this method in finishedDefaultedOrDeletedMember.
597 } else if (Method->isTrivial())
598 data().HasTrivialSpecialMembers |= SMKind;
599 else
600 data().DeclaredNonTrivialSpecialMembers |= SMKind;
601
Richard Smith328aae52012-11-30 05:11:39 +0000602 // Note when we have declared a declared special member, and suppress the
603 // implicit declaration of this special member.
604 data().DeclaredSpecialMembers |= SMKind;
605
606 if (!Method->isImplicit()) {
607 data().UserDeclaredSpecialMembers |= SMKind;
608
609 // C++03 [class]p4:
610 // A POD-struct is an aggregate class that has [...] no user-defined
611 // copy assignment operator and no user-defined destructor.
612 //
613 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
614 // aggregates could not have any constructors, clear it even for an
615 // explicitly defaulted or deleted constructor.
616 // type is technically an aggregate in C++0x since it wouldn't be in 03.
617 //
618 // Also, a user-declared move assignment operator makes a class non-POD.
619 // This is an extension in C++03.
620 data().PlainOldData = false;
621 }
Richard Smith328aae52012-11-30 05:11:39 +0000622 }
623
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000624 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000625 }
Richard Smith328aae52012-11-30 05:11:39 +0000626
Douglas Gregora832d3e2010-09-28 19:45:33 +0000627 // Handle non-static data members.
628 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000629 // C++ [class.bit]p2:
630 // A declaration for a bit-field that omits the identifier declares an
631 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
632 // initialized.
633 if (Field->isUnnamedBitfield())
634 return;
635
Douglas Gregora832d3e2010-09-28 19:45:33 +0000636 // C++ [dcl.init.aggr]p1:
637 // An aggregate is an array or a class (clause 9) with [...] no
638 // private or protected non-static data members (clause 11).
639 //
640 // A POD must be an aggregate.
641 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
642 data().Aggregate = false;
643 data().PlainOldData = false;
644 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000645
646 // C++0x [class]p7:
647 // A standard-layout class is a class that:
648 // [...]
649 // -- has the same access control for all non-static data members,
650 switch (D->getAccess()) {
651 case AS_private: data().HasPrivateFields = true; break;
652 case AS_protected: data().HasProtectedFields = true; break;
653 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000654 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000655 };
656 if ((data().HasPrivateFields + data().HasProtectedFields +
657 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000658 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000659
Douglas Gregor61226d32011-05-13 01:05:07 +0000660 // Keep track of the presence of mutable fields.
661 if (Field->isMutable())
662 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000663
664 // C++11 [class.union]p8, DR1460:
665 // If X is a union, a non-static data member of X that is not an anonymous
666 // union is a variant member of X.
667 if (isUnion() && !Field->isAnonymousStructOrUnion())
668 data().HasVariantMembers = true;
669
Chandler Carruthad7d4042011-04-23 23:10:33 +0000670 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000671 // A POD struct is a class that is both a trivial class and a
672 // standard-layout class, and has no non-static data members of type
673 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000674 //
675 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
676 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000677 ASTContext &Context = getASTContext();
678 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000679 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000680 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000681 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000682 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
683 // Objective-C Automatic Reference Counting:
684 // If a class has a non-static data member of Objective-C pointer
685 // type (or array thereof), it is a non-POD type and its
686 // default constructor (if any), copy constructor, move constructor,
687 // copy assignment operator, move assignment operator, and destructor are
688 // non-trivial.
689 setHasObjectMember(true);
690 struct DefinitionData &Data = data();
691 Data.PlainOldData = false;
692 Data.HasTrivialSpecialMembers = 0;
693 Data.HasIrrelevantDestructor = false;
694 }
Eli Friedmana5433322013-07-20 01:06:31 +0000695 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000696 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000697
Chandler Carruthb1963742011-04-30 09:17:45 +0000698 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000699 if (!Field->hasInClassInitializer())
700 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000701
Chandler Carruthb1963742011-04-30 09:17:45 +0000702 // C++0x [class]p7:
703 // A standard-layout class is a class that:
704 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000705 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000706 }
707
Nico Weber6a6376b2016-02-19 01:52:46 +0000708 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
709 if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000710 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000711 data().HasUninitializedFields = true;
712 } else {
713 data().HasUninitializedFields = true;
714 }
715 }
716
Richard Smith3607ffe2012-02-13 03:54:03 +0000717 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000718 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000719 data().HasNonLiteralTypeFieldsOrBases = true;
720
Richard Smithab44d5b2013-12-10 08:25:00 +0000721 if (Field->hasInClassInitializer() ||
722 (Field->isAnonymousStructOrUnion() &&
723 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000724 data().HasInClassInitializer = true;
725
726 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000727 // A default constructor is trivial if [...] no non-static data member
728 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000729 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000730
Richard Smithe2648ba2012-05-07 01:07:30 +0000731 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000732 // An aggregate is a [...] class with [...] no
733 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000734 //
735 // This rule was removed in C++1y.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000736 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000737 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000738
Richard Smithe2648ba2012-05-07 01:07:30 +0000739 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000740 // A POD struct is [...] a trivial class.
741 data().PlainOldData = false;
742 }
743
Richard Smith6b02d462012-12-08 08:32:28 +0000744 // C++11 [class.copy]p23:
745 // A defaulted copy/move assignment operator for a class X is defined
746 // as deleted if X has:
747 // -- a non-static data member of reference type
748 if (T->isReferenceType())
749 data().DefaultedMoveAssignmentIsDeleted = true;
750
Douglas Gregor11c024b2010-09-28 20:50:54 +0000751 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
752 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
753 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000754 addedClassSubobject(FieldRec);
755
Richard Smithc91d12c2013-11-25 07:07:05 +0000756 // We may need to perform overload resolution to determine whether a
757 // field can be moved if it's const or volatile qualified.
758 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
759 data().NeedOverloadResolutionForMoveConstructor = true;
760 data().NeedOverloadResolutionForMoveAssignment = true;
761 }
762
Richard Smith6b02d462012-12-08 08:32:28 +0000763 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
764 // A defaulted [special member] for a class X is defined as
765 // deleted if:
766 // -- X is a union-like class that has a variant member with a
767 // non-trivial [corresponding special member]
768 if (isUnion()) {
769 if (FieldRec->hasNonTrivialMoveConstructor())
770 data().DefaultedMoveConstructorIsDeleted = true;
771 if (FieldRec->hasNonTrivialMoveAssignment())
772 data().DefaultedMoveAssignmentIsDeleted = true;
773 if (FieldRec->hasNonTrivialDestructor())
774 data().DefaultedDestructorIsDeleted = true;
775 }
776
Alexis Huntf479f1b2011-05-09 18:22:59 +0000777 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000778 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000779 // -- for all the non-static data members of its class that are of
780 // class type (or array thereof), each such class has a trivial
781 // default constructor.
782 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000783 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000784
785 // C++0x [class.copy]p13:
786 // A copy/move constructor for class X is trivial if [...]
787 // [...]
788 // -- for each non-static data member of X that is of class type (or
789 // an array thereof), the constructor selected to copy/move that
790 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000791 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000792 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000793 // If the field doesn't have a simple move constructor, we'll eagerly
794 // declare the move constructor for this class and we'll decide whether
795 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000796 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000797 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000798
799 // C++0x [class.copy]p27:
800 // A copy/move assignment operator for class X is trivial if [...]
801 // [...]
802 // -- for each non-static data member of X that is of class type (or
803 // an array thereof), the assignment operator selected to
804 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000805 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000806 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000807 // If the field doesn't have a simple move assignment, we'll eagerly
808 // declare the move assignment for this class and we'll decide whether
809 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000810 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000811 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000812
Douglas Gregor11c024b2010-09-28 20:50:54 +0000813 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000814 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000815 if (!FieldRec->hasIrrelevantDestructor())
816 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000817 if (FieldRec->hasObjectMember())
818 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000819 if (FieldRec->hasVolatileMember())
820 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000821
822 // C++0x [class]p7:
823 // A standard-layout class is a class that:
824 // -- has no non-static data members of type non-standard-layout
825 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000826 if (!FieldRec->isStandardLayout())
827 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000828
829 // C++0x [class]p7:
830 // A standard-layout class is a class that:
831 // [...]
832 // -- has no base classes of the same type as the first non-static
833 // data member.
834 // We don't want to expend bits in the state of the record decl
835 // tracking whether this is the first non-static data member so we
836 // cheat a bit and use some of the existing state: the empty bit.
837 // Virtual bases and virtual methods make a class non-empty, but they
838 // also make it non-standard-layout so we needn't check here.
839 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000840 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000841 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000842 // data member must come through here with Empty still true, and Empty
843 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000844 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000845 for (const auto &BI : bases()) {
846 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000847 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000848 break;
849 }
850 }
851 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000852
853 // Keep track of the presence of mutable fields.
854 if (FieldRec->hasMutableFields())
855 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000856
857 // C++11 [class.copy]p13:
858 // If the implicitly-defined constructor would satisfy the
859 // requirements of a constexpr constructor, the implicitly-defined
860 // constructor is constexpr.
861 // C++11 [dcl.constexpr]p4:
862 // -- every constructor involved in initializing non-static data
863 // members [...] shall be a constexpr constructor
864 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000865 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000866 // The standard requires any in-class initializer to be a constant
867 // expression. We consider this to be a defect.
868 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000869
870 // C++11 [class.copy]p8:
871 // The implicitly-declared copy constructor for a class X will have
872 // the form 'X::X(const X&)' if [...] for all the non-static data
873 // members of X that are of a class type M (or array thereof), each
874 // such class type has a copy constructor whose first parameter is
875 // of type 'const M&' or 'const volatile M&'.
876 if (!FieldRec->hasCopyConstructorWithConstParam())
877 data().ImplicitCopyConstructorHasConstParam = false;
878
879 // C++11 [class.copy]p18:
880 // The implicitly-declared copy assignment oeprator for a class X will
881 // have the form 'X& X::operator=(const X&)' if [...] for all the
882 // non-static data members of X that are of a class type M (or array
883 // thereof), each such class type has a copy assignment operator whose
884 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
885 if (!FieldRec->hasCopyAssignmentWithConstParam())
886 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000887
888 if (FieldRec->hasUninitializedReferenceMember() &&
889 !Field->hasInClassInitializer())
890 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000891
892 // C++11 [class.union]p8, DR1460:
893 // a non-static data member of an anonymous union that is a member of
894 // X is also a variant member of X.
895 if (FieldRec->hasVariantMembers() &&
896 Field->isAnonymousStructOrUnion())
897 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000898 }
Richard Smithcc36f692011-12-22 02:22:31 +0000899 } else {
900 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000901 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000902 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000903 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000904
905 // C++11 [class.copy]p23:
906 // A defaulted copy/move assignment operator for a class X is defined
907 // as deleted if X has:
908 // -- a non-static data member of const non-class type (or array
909 // thereof)
910 if (T.isConstQualified())
911 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000912 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000913
914 // C++0x [class]p7:
915 // A standard-layout class is a class that:
916 // [...]
917 // -- either has no non-static data members in the most derived
918 // class and at most one base class with non-static data members,
919 // or has no base classes with non-static data members, and
920 // At this point we know that we have a non-static data member, so the last
921 // clause holds.
922 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000923 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000924
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000925 // If this is not a zero-length bit-field, then the class is not empty.
926 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000927 if (!Field->isBitField() ||
928 (!Field->getBitWidth()->isTypeDependent() &&
929 !Field->getBitWidth()->isValueDependent() &&
930 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000931 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000932 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000933 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000934
935 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000936 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000937 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000938 == DeclarationName::CXXConversionFunctionName) {
939 ASTContext &Ctx = getASTContext();
940 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
941 }
942 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000943}
944
Richard Smith92f241f2012-12-08 02:53:02 +0000945void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
946 assert(!D->isImplicit() && !D->isUserProvided());
947
948 // The kind of special member this declaration is, if any.
949 unsigned SMKind = 0;
950
951 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
952 if (Constructor->isDefaultConstructor()) {
953 SMKind |= SMF_DefaultConstructor;
954 if (Constructor->isConstexpr())
955 data().HasConstexprDefaultConstructor = true;
956 }
957 if (Constructor->isCopyConstructor())
958 SMKind |= SMF_CopyConstructor;
959 else if (Constructor->isMoveConstructor())
960 SMKind |= SMF_MoveConstructor;
961 else if (Constructor->isConstexpr())
962 // We may now know that the constructor is constexpr.
963 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000964 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000965 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000966 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
967 data().HasIrrelevantDestructor = false;
968 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000969 SMKind |= SMF_CopyAssignment;
970 else if (D->isMoveAssignmentOperator())
971 SMKind |= SMF_MoveAssignment;
972
973 // Update which trivial / non-trivial special members we have.
974 // addedMember will have skipped this step for this member.
975 if (D->isTrivial())
976 data().HasTrivialSpecialMembers |= SMKind;
977 else
978 data().DeclaredNonTrivialSpecialMembers |= SMKind;
979}
980
Joao Matose9a3ed42012-08-31 22:18:20 +0000981bool CXXRecordDecl::isCLike() const {
982 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
983 !TemplateOrInstantiation.isNull())
984 return false;
985 if (!hasDefinition())
986 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000987
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000988 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000989}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000990
Faisal Vali2b391ab2013-09-26 19:54:12 +0000991bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000992 if (!isLambda()) return false;
993 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000994}
995
996CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000997 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +0000998 DeclarationName Name =
999 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001000 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001001
1002 assert(!Calls.empty() && "Missing lambda call operator!");
1003 assert(Calls.size() == 1 && "More than one lambda call operator!");
1004
1005 NamedDecl *CallOp = Calls.front();
1006 if (FunctionTemplateDecl *CallOpTmpl =
1007 dyn_cast<FunctionTemplateDecl>(CallOp))
1008 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001009
1010 return cast<CXXMethodDecl>(CallOp);
1011}
1012
1013CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001014 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001015 DeclarationName Name =
1016 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001017 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001018 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001019 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1020 NamedDecl *InvokerFun = Invoker.front();
1021 if (FunctionTemplateDecl *InvokerTemplate =
1022 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1023 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1024
Faisal Vali571df122013-09-29 08:45:24 +00001025 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001026}
1027
Douglas Gregor9c702202012-02-10 07:45:31 +00001028void CXXRecordDecl::getCaptureFields(
1029 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001030 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001031 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001032 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001033
Douglas Gregorc8a73492012-02-13 15:44:47 +00001034 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001035 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001036 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001037 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001038 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001039 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001040 else if (C->capturesVariable())
1041 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001042 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001043 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001044}
1045
Faisal Vali2b391ab2013-09-26 19:54:12 +00001046TemplateParameterList *
1047CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001048 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001049 CXXMethodDecl *CallOp = getLambdaCallOperator();
1050 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1051 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001052 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001053}
Douglas Gregor9c702202012-02-10 07:45:31 +00001054
John McCall1e3a1a72010-03-15 09:07:48 +00001055static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001056 QualType T =
1057 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1058 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001059 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001060}
1061
John McCall1e3a1a72010-03-15 09:07:48 +00001062/// Collect the visible conversions of a base class.
1063///
James Dennettb5d5f812012-06-15 22:28:09 +00001064/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001065/// \param InVirtual whether this base class is a virtual base (or a base
1066/// of a virtual base)
1067/// \param Access the access along the inheritance path to this base
1068/// \param ParentHiddenTypes the conversions provided by the inheritors
1069/// of this base
1070/// \param Output the set to which to add conversions from non-virtual bases
1071/// \param VOutput the set to which to add conversions from virtual bases
1072/// \param HiddenVBaseCs the set of conversions which were hidden in a
1073/// virtual base along some inheritance path
1074static void CollectVisibleConversions(ASTContext &Context,
1075 CXXRecordDecl *Record,
1076 bool InVirtual,
1077 AccessSpecifier Access,
1078 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001079 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001080 UnresolvedSetImpl &VOutput,
1081 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1082 // The set of types which have conversions in this class or its
1083 // subclasses. As an optimization, we don't copy the derived set
1084 // unless it might change.
1085 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1086 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1087
1088 // Collect the direct conversions and figure out which conversions
1089 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001090 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1091 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1092 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001093 HiddenTypesBuffer = ParentHiddenTypes;
1094 HiddenTypes = &HiddenTypesBuffer;
1095
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001096 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001097 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1098 bool Hidden = ParentHiddenTypes.count(ConvType);
1099 if (!Hidden)
1100 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001101
1102 // If this conversion is hidden and we're in a virtual base,
1103 // remember that it's hidden along some inheritance path.
1104 if (Hidden && InVirtual)
1105 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1106
1107 // If this conversion isn't hidden, add it to the appropriate output.
1108 else if (!Hidden) {
1109 AccessSpecifier IAccess
1110 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1111
1112 if (InVirtual)
1113 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001114 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001115 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001116 }
1117 }
1118 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001119
John McCall1e3a1a72010-03-15 09:07:48 +00001120 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001121 for (const auto &I : Record->bases()) {
1122 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001123 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001124
John McCall1e3a1a72010-03-15 09:07:48 +00001125 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001126 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1127 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001128
John McCall1e3a1a72010-03-15 09:07:48 +00001129 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1130 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1131 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001132 }
John McCall1e3a1a72010-03-15 09:07:48 +00001133}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001134
John McCall1e3a1a72010-03-15 09:07:48 +00001135/// Collect the visible conversions of a class.
1136///
1137/// This would be extremely straightforward if it weren't for virtual
1138/// bases. It might be worth special-casing that, really.
1139static void CollectVisibleConversions(ASTContext &Context,
1140 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001141 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001142 // The collection of all conversions in virtual bases that we've
1143 // found. These will be added to the output as long as they don't
1144 // appear in the hidden-conversions set.
1145 UnresolvedSet<8> VBaseCs;
1146
1147 // The set of conversions in virtual bases that we've determined to
1148 // be hidden.
1149 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1150
1151 // The set of types hidden by classes derived from this one.
1152 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1153
1154 // Go ahead and collect the direct conversions and add them to the
1155 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001156 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1157 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001158 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001159 for (; ConvI != ConvE; ++ConvI)
1160 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001161
1162 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001163 for (const auto &I : Record->bases()) {
1164 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001165 if (!RT) continue;
1166
1167 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001168 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001169 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1170 }
1171
1172 // Add any unhidden conversions provided by virtual bases.
1173 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1174 I != E; ++I) {
1175 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001176 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001177 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001178}
1179
1180/// getVisibleConversionFunctions - get all conversion functions visible
1181/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001182llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001183CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001184 ASTContext &Ctx = getASTContext();
1185
1186 ASTUnresolvedSet *Set;
1187 if (bases_begin() == bases_end()) {
1188 // If root class, all conversions are visible.
1189 Set = &data().Conversions.get(Ctx);
1190 } else {
1191 Set = &data().VisibleConversions.get(Ctx);
1192 // If visible conversion list is not evaluated, evaluate it.
1193 if (!data().ComputedVisibleConversions) {
1194 CollectVisibleConversions(Ctx, this, *Set);
1195 data().ComputedVisibleConversions = true;
1196 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001197 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001198 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001199}
1200
John McCallda4458e2010-03-31 01:36:47 +00001201void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1202 // This operation is O(N) but extremely rare. Sema only uses it to
1203 // remove UsingShadowDecls in a class that were followed by a direct
1204 // declaration, e.g.:
1205 // class A : B {
1206 // using B::operator int;
1207 // operator int();
1208 // };
1209 // This is uncommon by itself and even more uncommon in conjunction
1210 // with sufficiently large numbers of directly-declared conversions
1211 // that asymptotic behavior matters.
1212
Richard Smitha4ba74c2013-08-30 04:46:40 +00001213 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001214 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1215 if (Convs[I].getDecl() == ConvDecl) {
1216 Convs.erase(I);
1217 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1218 && "conversion was found multiple times in unresolved set");
1219 return;
1220 }
1221 }
1222
1223 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001224}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001225
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001226CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001227 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001228 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001229
1230 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001231}
1232
Chandler Carruth21c90602015-12-30 03:24:14 +00001233MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1234 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1235}
1236
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001237void
1238CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1239 TemplateSpecializationKind TSK) {
1240 assert(TemplateOrInstantiation.isNull() &&
1241 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001242 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001243 TemplateOrInstantiation
1244 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1245}
1246
Chandler Carruth21c90602015-12-30 03:24:14 +00001247ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1248 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1249}
1250
1251void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1252 TemplateOrInstantiation = Template;
1253}
1254
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001255TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1256 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001257 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1258 return Spec->getSpecializationKind();
1259
Douglas Gregor06db9f52009-10-12 20:18:28 +00001260 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001261 return MSInfo->getTemplateSpecializationKind();
1262
1263 return TSK_Undeclared;
1264}
1265
1266void
1267CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1268 if (ClassTemplateSpecializationDecl *Spec
1269 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1270 Spec->setSpecializationKind(TSK);
1271 return;
1272 }
1273
Douglas Gregor06db9f52009-10-12 20:18:28 +00001274 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001275 MSInfo->setTemplateSpecializationKind(TSK);
1276 return;
1277 }
1278
David Blaikie83d382b2011-09-23 05:06:16 +00001279 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001280}
1281
Reid Klecknere7367d62014-10-14 20:28:40 +00001282const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1283 // If it's a class template specialization, find the template or partial
1284 // specialization from which it was instantiated.
1285 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1286 auto From = TD->getInstantiatedFrom();
1287 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1288 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1289 if (NewCTD->isMemberSpecialization())
1290 break;
1291 CTD = NewCTD;
1292 }
Richard Smith7a591a42015-07-08 02:22:15 +00001293 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001294 }
1295 if (auto *CTPSD =
1296 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1297 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1298 if (NewCTPSD->isMemberSpecialization())
1299 break;
1300 CTPSD = NewCTPSD;
1301 }
Richard Smith7a591a42015-07-08 02:22:15 +00001302 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001303 }
1304 }
1305
1306 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1307 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1308 const CXXRecordDecl *RD = this;
1309 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1310 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001311 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001312 }
1313 }
1314
1315 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1316 "couldn't find pattern for class template instantiation");
1317 return nullptr;
1318}
1319
Douglas Gregorbac74902010-07-01 14:13:13 +00001320CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1321 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001322 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001323
1324 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001325 = Context.DeclarationNames.getCXXDestructorName(
1326 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001327
Richard Smithcf4bdde2015-02-21 02:45:19 +00001328 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001329 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001330 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001331
David Blaikieff7d47a2012-12-19 00:45:41 +00001332 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001333 return Dtor;
1334}
1335
Richard Trieu95a192a2015-05-28 00:14:02 +00001336bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1337 // Destructor is noreturn.
1338 if (const CXXDestructorDecl *Destructor = getDestructor())
1339 if (Destructor->isNoReturn())
1340 return true;
1341
1342 // Check base classes destructor for noreturn.
1343 for (const auto &Base : bases())
1344 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1345 return true;
1346
1347 // Check fields for noreturn.
1348 for (const auto *Field : fields())
1349 if (const CXXRecordDecl *RD =
1350 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1351 if (RD->isAnyDestructorNoReturn())
1352 return true;
1353
1354 // All destructors are not noreturn.
1355 return false;
1356}
1357
Douglas Gregorb11aad82011-02-19 18:51:44 +00001358void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001359 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001360}
1361
1362void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1363 RecordDecl::completeDefinition();
1364
Douglas Gregor8fb95122010-09-29 00:15:42 +00001365 // If the class may be abstract (but hasn't been marked as such), check for
1366 // any pure final overriders.
1367 if (mayBeAbstract()) {
1368 CXXFinalOverriderMap MyFinalOverriders;
1369 if (!FinalOverriders) {
1370 getFinalOverriders(MyFinalOverriders);
1371 FinalOverriders = &MyFinalOverriders;
1372 }
1373
1374 bool Done = false;
1375 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1376 MEnd = FinalOverriders->end();
1377 M != MEnd && !Done; ++M) {
1378 for (OverridingMethods::iterator SO = M->second.begin(),
1379 SOEnd = M->second.end();
1380 SO != SOEnd && !Done; ++SO) {
1381 assert(SO->second.size() > 0 &&
1382 "All virtual functions have overridding virtual functions");
1383
1384 // C++ [class.abstract]p4:
1385 // A class is abstract if it contains or inherits at least one
1386 // pure virtual function for which the final overrider is pure
1387 // virtual.
1388 if (SO->second.front().Method->isPure()) {
1389 data().Abstract = true;
1390 Done = true;
1391 break;
1392 }
1393 }
1394 }
1395 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001396
1397 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001398 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001399 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001400 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001401}
1402
1403bool CXXRecordDecl::mayBeAbstract() const {
1404 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1405 isDependentContext())
1406 return false;
1407
Aaron Ballman574705e2014-03-13 15:41:46 +00001408 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001409 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001410 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001411 if (BaseDecl->isAbstract())
1412 return true;
1413 }
1414
1415 return false;
1416}
1417
David Blaikie68e081d2011-12-20 02:48:34 +00001418void CXXMethodDecl::anchor() { }
1419
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001420bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001421 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001422
1423 if (MD->getStorageClass() == SC_Static)
1424 return true;
1425
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001426 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1427 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001428}
1429
Rafael Espindola49e860b2012-06-26 17:45:31 +00001430static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1431 const CXXMethodDecl *BaseMD) {
1432 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1433 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1434 const CXXMethodDecl *MD = *I;
1435 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1436 return true;
1437 if (recursivelyOverrides(MD, BaseMD))
1438 return true;
1439 }
1440 return false;
1441}
1442
1443CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001444CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1445 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001446 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1447 return this;
1448
1449 // Lookup doesn't work for destructors, so handle them separately.
1450 if (isa<CXXDestructorDecl>(this)) {
1451 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001452 if (MD) {
1453 if (recursivelyOverrides(MD, this))
1454 return MD;
1455 if (MayBeBase && recursivelyOverrides(this, MD))
1456 return MD;
1457 }
Craig Topper36250ad2014-05-12 05:36:57 +00001458 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001459 }
1460
Richard Smith40c78062015-02-21 02:31:57 +00001461 for (auto *ND : RD->lookup(getDeclName())) {
1462 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001463 if (!MD)
1464 continue;
1465 if (recursivelyOverrides(MD, this))
1466 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001467 if (MayBeBase && recursivelyOverrides(this, MD))
1468 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001469 }
1470
Aaron Ballman574705e2014-03-13 15:41:46 +00001471 for (const auto &I : RD->bases()) {
1472 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001473 if (!RT)
1474 continue;
1475 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1476 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1477 if (T)
1478 return T;
1479 }
1480
Craig Topper36250ad2014-05-12 05:36:57 +00001481 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001482}
1483
Ted Kremenek21475702008-09-05 17:16:31 +00001484CXXMethodDecl *
1485CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001486 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001487 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001488 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001489 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001490 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001491 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1492 T, TInfo, SC, isInline, isConstexpr,
1493 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001494}
1495
Douglas Gregor72172e92012-01-05 21:55:30 +00001496CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001497 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001498 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001499 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001500}
1501
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001502bool CXXMethodDecl::isUsualDeallocationFunction() const {
1503 if (getOverloadedOperator() != OO_Delete &&
1504 getOverloadedOperator() != OO_Array_Delete)
1505 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001506
1507 // C++ [basic.stc.dynamic.deallocation]p2:
1508 // A template instance is never a usual deallocation function,
1509 // regardless of its signature.
1510 if (getPrimaryTemplate())
1511 return false;
1512
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001513 // C++ [basic.stc.dynamic.deallocation]p2:
1514 // If a class T has a member deallocation function named operator delete
1515 // with exactly one parameter, then that function is a usual (non-placement)
1516 // deallocation function. [...]
1517 if (getNumParams() == 1)
1518 return true;
1519
1520 // C++ [basic.stc.dynamic.deallocation]p2:
1521 // [...] If class T does not declare such an operator delete but does
1522 // declare a member deallocation function named operator delete with
1523 // exactly two parameters, the second of which has type std::size_t (18.1),
1524 // then this function is a usual deallocation function.
1525 ASTContext &Context = getASTContext();
1526 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001527 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1528 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001529 return false;
1530
1531 // This function is a usual deallocation function if there are no
1532 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001533 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1534 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001535 I != E; ++I) {
1536 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001537 if (FD->getNumParams() == 1)
1538 return false;
1539 }
1540
1541 return true;
1542}
1543
Douglas Gregorb139cd52010-05-01 20:49:11 +00001544bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001545 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001546 // A user-declared copy assignment operator X::operator= is a non-static
1547 // non-template member function of class X with exactly one parameter of
1548 // type X, X&, const X&, volatile X& or const volatile X&.
1549 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1550 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001551 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1552 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001553 return false;
1554
1555 QualType ParamType = getParamDecl(0)->getType();
1556 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1557 ParamType = Ref->getPointeeType();
1558
1559 ASTContext &Context = getASTContext();
1560 QualType ClassType
1561 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1562 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1563}
1564
Alexis Huntfcaeae42011-05-25 20:50:04 +00001565bool CXXMethodDecl::isMoveAssignmentOperator() const {
1566 // C++0x [class.copy]p19:
1567 // A user-declared move assignment operator X::operator= is a non-static
1568 // non-template member function of class X with exactly one parameter of type
1569 // X&&, const X&&, volatile X&&, or const volatile X&&.
1570 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001571 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1572 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001573 return false;
1574
1575 QualType ParamType = getParamDecl(0)->getType();
1576 if (!isa<RValueReferenceType>(ParamType))
1577 return false;
1578 ParamType = ParamType->getPointeeType();
1579
1580 ASTContext &Context = getASTContext();
1581 QualType ClassType
1582 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1583 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1584}
1585
Anders Carlsson36d87e12009-05-16 23:58:37 +00001586void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001587 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001588 assert(!MD->getParent()->isDependentContext() &&
1589 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001590 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001591
Douglas Gregor832940b2010-03-02 23:58:15 +00001592 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001593}
1594
1595CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001596 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001597 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001598}
1599
1600CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001601 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001602 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001603}
1604
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001605unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001606 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001607 return getASTContext().overridden_methods_size(this);
1608}
1609
Ted Kremenek21475702008-09-05 17:16:31 +00001610QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001611 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1612 // If the member function is declared const, the type of this is const X*,
1613 // if the member function is declared volatile, the type of this is
1614 // volatile X*, and if the member function is declared const volatile,
1615 // the type of this is const volatile X*.
1616
Ted Kremenek21475702008-09-05 17:16:31 +00001617 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001618
John McCalle78aac42010-03-10 03:28:59 +00001619 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001620 ClassTy = C.getQualifiedType(ClassTy,
1621 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001622 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001623}
1624
Eli Friedman71a26d82009-12-06 20:50:05 +00001625bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001626 // If this function is a template instantiation, look at the template from
1627 // which it was instantiated.
1628 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1629 if (!CheckFn)
1630 CheckFn = this;
1631
Eli Friedman71a26d82009-12-06 20:50:05 +00001632 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001633 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001634}
1635
Douglas Gregor355efbb2012-02-17 03:02:34 +00001636bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001637 const CXXRecordDecl *P = getParent();
1638 if (P->isLambda()) {
1639 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1640 if (StaticInvoker == this) return true;
1641 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1642 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1643 }
1644 }
1645 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001646}
1647
Alexis Hunt1d792652011-01-08 20:30:50 +00001648CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1649 TypeSourceInfo *TInfo, bool IsVirtual,
1650 SourceLocation L, Expr *Init,
1651 SourceLocation R,
1652 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001653 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001654 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1655 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001656{
Douglas Gregore8381c02008-11-05 04:29:56 +00001657}
1658
Alexis Hunt1d792652011-01-08 20:30:50 +00001659CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1660 FieldDecl *Member,
1661 SourceLocation MemberLoc,
1662 SourceLocation L, Expr *Init,
1663 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001664 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001665 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001666 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1667{
1668}
1669
Alexis Hunt1d792652011-01-08 20:30:50 +00001670CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1671 IndirectFieldDecl *Member,
1672 SourceLocation MemberLoc,
1673 SourceLocation L, Expr *Init,
1674 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001675 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001676 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001677 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001678{
Douglas Gregore8381c02008-11-05 04:29:56 +00001679}
1680
Alexis Hunt1d792652011-01-08 20:30:50 +00001681CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001682 TypeSourceInfo *TInfo,
1683 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001684 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001685 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1686 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001687 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1688{
1689}
1690
1691CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001692 FieldDecl *Member,
1693 SourceLocation MemberLoc,
1694 SourceLocation L, Expr *Init,
1695 SourceLocation R,
1696 VarDecl **Indices,
1697 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001698 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001699 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001700 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001701{
James Y Knight967eb202015-12-29 22:13:13 +00001702 std::uninitialized_copy(Indices, Indices + NumIndices,
1703 getTrailingObjects<VarDecl *>());
Douglas Gregor94f9a482010-05-05 05:51:00 +00001704}
1705
Alexis Hunt1d792652011-01-08 20:30:50 +00001706CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1707 FieldDecl *Member,
1708 SourceLocation MemberLoc,
1709 SourceLocation L, Expr *Init,
1710 SourceLocation R,
1711 VarDecl **Indices,
1712 unsigned NumIndices) {
James Y Knight967eb202015-12-29 22:13:13 +00001713 void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
Alexis Hunt1d792652011-01-08 20:30:50 +00001714 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001715 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1716 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001717}
1718
Alexis Hunt1d792652011-01-08 20:30:50 +00001719TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001720 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001721 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001722 else
1723 return TypeLoc();
1724}
1725
Alexis Hunt1d792652011-01-08 20:30:50 +00001726const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001727 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001728 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001729 else
Craig Topper36250ad2014-05-12 05:36:57 +00001730 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001731}
1732
Alexis Hunt1d792652011-01-08 20:30:50 +00001733SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001734 if (isInClassMemberInitializer())
1735 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001736
David Blaikiea81d4102015-01-18 00:12:58 +00001737 if (isAnyMemberInitializer())
1738 return getMemberLocation();
1739
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001740 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1741 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1742
1743 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001744}
1745
Alexis Hunt1d792652011-01-08 20:30:50 +00001746SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001747 if (isInClassMemberInitializer()) {
1748 FieldDecl *D = getAnyMember();
1749 if (Expr *I = D->getInClassInitializer())
1750 return I->getSourceRange();
1751 return SourceRange();
1752 }
1753
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001754 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001755}
1756
David Blaikie68e081d2011-12-20 02:48:34 +00001757void CXXConstructorDecl::anchor() { }
1758
Douglas Gregor61956c42008-10-31 09:07:45 +00001759CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001760CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001761 return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001762 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001763 nullptr, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001764}
1765
1766CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001767CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001768 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001769 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001770 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001771 bool isExplicit, bool isInline,
1772 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001773 assert(NameInfo.getName().getNameKind()
1774 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001775 "Name must refer to a constructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001776 return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001777 isExplicit, isInline,
1778 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001779}
1780
Richard Smithc2bb8182015-03-24 06:36:48 +00001781CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1782 return CtorInitializers.get(getASTContext().getExternalSource());
1783}
1784
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001785CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1786 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1787 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1788 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1789 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001790
1791 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001792}
1793
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001794bool CXXConstructorDecl::isDefaultConstructor() const {
1795 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001796 // A default constructor for a class X is a constructor of class
1797 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001798 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001799 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001800}
1801
Mike Stump11289f42009-09-09 15:08:12 +00001802bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001803CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001804 return isCopyOrMoveConstructor(TypeQuals) &&
1805 getParamDecl(0)->getType()->isLValueReferenceType();
1806}
1807
1808bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1809 return isCopyOrMoveConstructor(TypeQuals) &&
1810 getParamDecl(0)->getType()->isRValueReferenceType();
1811}
1812
1813/// \brief Determine whether this is a copy or move constructor.
1814bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001815 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001816 // A non-template constructor for class X is a copy constructor
1817 // if its first parameter is of type X&, const X&, volatile X& or
1818 // const volatile X&, and either there are no other parameters
1819 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001820 // C++0x [class.copy]p3:
1821 // A non-template constructor for class X is a move constructor if its
1822 // first parameter is of type X&&, const X&&, volatile X&&, or
1823 // const volatile X&&, and either there are no other parameters or else
1824 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001825 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001826 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001827 (getPrimaryTemplate() != nullptr) ||
1828 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001829 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001830
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001831 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001832
1833 // Do we have a reference type?
1834 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001835 if (!ParamRefType)
1836 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001837
Douglas Gregorff7028a2009-11-13 23:59:09 +00001838 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001839 ASTContext &Context = getASTContext();
1840
Douglas Gregorff7028a2009-11-13 23:59:09 +00001841 CanQualType PointeeType
1842 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001843 CanQualType ClassTy
1844 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001845 if (PointeeType.getUnqualifiedType() != ClassTy)
1846 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001847
John McCall8ccfcb52009-09-24 19:53:00 +00001848 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001849
1850 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001851 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001852 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001853}
1854
Anders Carlssond20e7952009-08-28 16:57:08 +00001855bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001856 // C++ [class.conv.ctor]p1:
1857 // A constructor declared without the function-specifier explicit
1858 // that can be called with a single parameter specifies a
1859 // conversion from the type of its first parameter to the type of
1860 // its class. Such a constructor is called a converting
1861 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001862 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001863 return false;
1864
Mike Stump11289f42009-09-09 15:08:12 +00001865 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001866 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001867 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001868 (getNumParams() > 1 &&
1869 (getParamDecl(1)->hasDefaultArg() ||
1870 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001871}
Douglas Gregor61956c42008-10-31 09:07:45 +00001872
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001873bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001874 if ((getNumParams() < 1) ||
1875 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001876 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001877 return false;
1878
1879 const ParmVarDecl *Param = getParamDecl(0);
1880
1881 ASTContext &Context = getASTContext();
1882 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1883
Douglas Gregorffe14e32009-11-14 01:20:54 +00001884 // Is it the same as our our class type?
1885 CanQualType ClassTy
1886 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1887 if (ParamType.getUnqualifiedType() != ClassTy)
1888 return false;
1889
1890 return true;
1891}
1892
Sebastian Redl08905022011-02-05 19:23:19 +00001893const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1894 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001895 method_iterator It = getASTContext().overridden_methods_begin(this);
1896 if (It == getASTContext().overridden_methods_end(this))
Craig Topper36250ad2014-05-12 05:36:57 +00001897 return nullptr;
Sebastian Redl08905022011-02-05 19:23:19 +00001898
1899 return cast<CXXConstructorDecl>(*It);
1900}
1901
1902void
1903CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1904 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001905 assert(getASTContext().overridden_methods_size(this) == 0 &&
1906 "Base ctor already set.");
1907 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001908}
1909
David Blaikie68e081d2011-12-20 02:48:34 +00001910void CXXDestructorDecl::anchor() { }
1911
Douglas Gregor831c93f2008-11-05 20:51:48 +00001912CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001913CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001914 return new (C, ID)
1915 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1916 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001917}
1918
1919CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001920CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001921 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001922 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001923 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001924 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001925 assert(NameInfo.getName().getNameKind()
1926 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001927 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001928 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001929 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001930}
1931
Richard Smithf8134002015-03-10 01:41:22 +00001932void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1933 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1934 if (OD && !First->OperatorDelete) {
1935 First->OperatorDelete = OD;
1936 if (auto *L = getASTMutationListener())
1937 L->ResolvedOperatorDelete(First, OD);
1938 }
1939}
1940
David Blaikie68e081d2011-12-20 02:48:34 +00001941void CXXConversionDecl::anchor() { }
1942
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001943CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001944CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001945 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001946 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001947 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00001948 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001949}
1950
1951CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001952CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001953 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001954 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001955 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001956 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001957 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001958 assert(NameInfo.getName().getNameKind()
1959 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001960 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00001961 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001962 isInline, isExplicit, isConstexpr,
1963 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001964}
1965
Douglas Gregord3b672c2012-02-16 01:06:16 +00001966bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1967 return isImplicit() && getParent()->isLambda() &&
1968 getConversionType()->isBlockPointerType();
1969}
1970
David Blaikie68e081d2011-12-20 02:48:34 +00001971void LinkageSpecDecl::anchor() { }
1972
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001973LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001974 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001975 SourceLocation ExternLoc,
1976 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001977 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001978 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00001979 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001980}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001981
Richard Smithf7981722013-11-22 09:01:48 +00001982LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1983 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001984 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
1985 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001986}
1987
David Blaikie68e081d2011-12-20 02:48:34 +00001988void UsingDirectiveDecl::anchor() { }
1989
Douglas Gregor889ceb72009-02-03 19:21:40 +00001990UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1991 SourceLocation L,
1992 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001993 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001994 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001995 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001996 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001997 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1998 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001999 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2000 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002001}
2002
Richard Smithf7981722013-11-22 09:01:48 +00002003UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2004 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002005 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2006 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002007 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002008 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002009}
2010
Sebastian Redla6602e92009-11-23 15:34:23 +00002011NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2012 if (NamespaceAliasDecl *NA =
2013 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2014 return NA->getNamespace();
2015 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2016}
2017
Richard Smith053f6c62014-05-16 23:01:30 +00002018NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2019 SourceLocation StartLoc, SourceLocation IdLoc,
2020 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2021 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2022 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2023 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002024 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002025
Douglas Gregore57e7522012-01-07 09:11:48 +00002026 if (PrevDecl)
2027 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2028}
2029
Douglas Gregor72172e92012-01-05 21:55:30 +00002030NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002031 bool Inline, SourceLocation StartLoc,
2032 SourceLocation IdLoc, IdentifierInfo *Id,
2033 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002034 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2035 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002036}
2037
2038NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002039 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002040 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002041}
2042
Chandler Carruth21c90602015-12-30 03:24:14 +00002043NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2044 if (isFirstDecl())
2045 return this;
2046
2047 return AnonOrFirstNamespaceAndInline.getPointer();
2048}
2049
2050const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2051 if (isFirstDecl())
2052 return this;
2053
2054 return AnonOrFirstNamespaceAndInline.getPointer();
2055}
2056
2057bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2058
Richard Smithd7af8a32014-05-10 01:17:36 +00002059NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2060 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002061}
2062NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2063 return getPreviousDecl();
2064}
2065NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2066 return getMostRecentDecl();
2067}
2068
David Blaikie68e081d2011-12-20 02:48:34 +00002069void NamespaceAliasDecl::anchor() { }
2070
Richard Smithf4634362014-09-03 23:11:22 +00002071NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2072 return getNextRedeclaration();
2073}
2074NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2075 return getPreviousDecl();
2076}
2077NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2078 return getMostRecentDecl();
2079}
2080
Mike Stump11289f42009-09-09 15:08:12 +00002081NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002082 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002083 SourceLocation AliasLoc,
2084 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002085 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002086 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002087 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002088 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002089 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2090 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002091 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002092 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002093}
2094
Douglas Gregor72172e92012-01-05 21:55:30 +00002095NamespaceAliasDecl *
2096NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002097 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002098 SourceLocation(), nullptr,
2099 NestedNameSpecifierLoc(),
2100 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002101}
2102
David Blaikie68e081d2011-12-20 02:48:34 +00002103void UsingShadowDecl::anchor() { }
2104
Douglas Gregor72172e92012-01-05 21:55:30 +00002105UsingShadowDecl *
2106UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002107 return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2108 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002109}
2110
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002111UsingDecl *UsingShadowDecl::getUsingDecl() const {
2112 const UsingShadowDecl *Shadow = this;
2113 while (const UsingShadowDecl *NextShadow =
2114 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2115 Shadow = NextShadow;
2116 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2117}
2118
David Blaikie68e081d2011-12-20 02:48:34 +00002119void UsingDecl::anchor() { }
2120
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002121void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2122 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2123 "declaration already in set");
2124 assert(S->getUsingDecl() == this);
2125
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002126 if (FirstUsingShadow.getPointer())
2127 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2128 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002129}
2130
2131void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2132 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2133 "declaration not in set");
2134 assert(S->getUsingDecl() == this);
2135
2136 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2137
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002138 if (FirstUsingShadow.getPointer() == S) {
2139 FirstUsingShadow.setPointer(
2140 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002141 S->UsingOrNextShadow = this;
2142 return;
2143 }
2144
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002145 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002146 while (Prev->UsingOrNextShadow != S)
2147 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2148 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2149 S->UsingOrNextShadow = this;
2150}
2151
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002152UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2153 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002154 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002155 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002156 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002157}
2158
Douglas Gregor72172e92012-01-05 21:55:30 +00002159UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002160 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2161 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2162 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002163}
2164
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002165SourceRange UsingDecl::getSourceRange() const {
2166 SourceLocation Begin = isAccessDeclaration()
2167 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2168 return SourceRange(Begin, getNameInfo().getEndLoc());
2169}
2170
David Blaikie68e081d2011-12-20 02:48:34 +00002171void UnresolvedUsingValueDecl::anchor() { }
2172
John McCalle61f2ba2009-11-18 02:36:19 +00002173UnresolvedUsingValueDecl *
2174UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2175 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002176 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002177 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002178 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2179 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002180}
2181
Douglas Gregor72172e92012-01-05 21:55:30 +00002182UnresolvedUsingValueDecl *
2183UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002184 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2185 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002186 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002187 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002188}
2189
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002190SourceRange UnresolvedUsingValueDecl::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 UnresolvedUsingTypenameDecl::anchor() { }
2197
John McCalle61f2ba2009-11-18 02:36:19 +00002198UnresolvedUsingTypenameDecl *
2199UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2200 SourceLocation UsingLoc,
2201 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002202 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002203 SourceLocation TargetNameLoc,
2204 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002205 return new (C, DC) UnresolvedUsingTypenameDecl(
2206 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2207 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002208}
2209
Douglas Gregor72172e92012-01-05 21:55:30 +00002210UnresolvedUsingTypenameDecl *
2211UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002212 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002213 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2214 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002215}
2216
David Blaikie68e081d2011-12-20 02:48:34 +00002217void StaticAssertDecl::anchor() { }
2218
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002219StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002220 SourceLocation StaticAssertLoc,
2221 Expr *AssertExpr,
2222 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002223 SourceLocation RParenLoc,
2224 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002225 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2226 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002227}
2228
Richard Smithf7981722013-11-22 09:01:48 +00002229StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002230 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002231 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2232 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002233}
2234
2235MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2236 SourceLocation L, DeclarationName N,
2237 QualType T, TypeSourceInfo *TInfo,
2238 SourceLocation StartL,
2239 IdentifierInfo *Getter,
2240 IdentifierInfo *Setter) {
2241 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2242}
2243
2244MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2245 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002246 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2247 DeclarationName(), QualType(), nullptr,
2248 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002249}
2250
Anders Carlsson6750d162009-03-26 23:46:50 +00002251static const char *getAccessName(AccessSpecifier AS) {
2252 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002253 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002254 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002255 case AS_public:
2256 return "public";
2257 case AS_private:
2258 return "private";
2259 case AS_protected:
2260 return "protected";
2261 }
David Blaikief47fa302012-01-17 02:30:50 +00002262 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002263}
2264
2265const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2266 AccessSpecifier AS) {
2267 return DB << getAccessName(AS);
2268}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002269
2270const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2271 AccessSpecifier AS) {
2272 return DB << getAccessName(AS);
2273}