blob: c8edd5a1b4fa0067a3494723d13ccb8fc1b4b4f5 [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),
64 DefaultedDefaultConstructorIsConstexpr(true),
65 HasConstexprDefaultConstructor(false),
66 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
67 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
68 ImplicitCopyConstructorHasConstParam(true),
69 ImplicitCopyAssignmentHasConstParam(true),
70 HasDeclaredCopyConstructorWithConstParam(false),
71 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
72 IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0), Bases(),
73 VBases(), Definition(D), FirstFriend() {}
John McCall67da35c2010-02-04 22:26:26 +000074
Benjamin Kramer300c0632012-07-04 17:03:33 +000075CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
76 return Bases.get(Definition->getASTContext().getExternalSource());
77}
78
79CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
80 return VBases.get(Definition->getASTContext().getExternalSource());
81}
82
Richard Smith053f6c62014-05-16 23:01:30 +000083CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
84 DeclContext *DC, SourceLocation StartLoc,
85 SourceLocation IdLoc, IdentifierInfo *Id,
86 CXXRecordDecl *PrevDecl)
87 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
88 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smith64c06302014-05-22 23:19:02 +000089 : DefinitionDataPtr(this)),
Richard Smith053f6c62014-05-16 23:01:30 +000090 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000091
Jay Foad39c79802011-01-12 09:06:06 +000092CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000093 DeclContext *DC, SourceLocation StartLoc,
94 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000095 CXXRecordDecl* PrevDecl,
96 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +000097 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +000098 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +000099 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000101 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000102 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000103 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000104 return R;
105}
106
Richard Smith053f6c62014-05-16 23:01:30 +0000107CXXRecordDecl *
108CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
109 TypeSourceInfo *Info, SourceLocation Loc,
110 bool Dependent, bool IsGeneric,
111 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000112 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000113 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
114 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000115 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000116 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000117 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000118 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000119 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000120 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000121 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000122 return R;
123}
124
Douglas Gregor72172e92012-01-05 21:55:30 +0000125CXXRecordDecl *
126CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000127 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000128 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000129 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000130 R->MayHaveOutOfDateDef = false;
131 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000132}
133
Mike Stump11289f42009-09-09 15:08:12 +0000134void
Craig Toppere6337e12015-12-25 00:36:02 +0000135CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
136 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000137 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000138
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000139 if (!data().Bases.isOffset() && data().NumBases > 0)
140 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000141
Craig Toppere6337e12015-12-25 00:36:02 +0000142 if (NumBases) {
Richard Smithaed32b42011-10-18 20:08:55 +0000143 // C++ [dcl.init.aggr]p1:
144 // An aggregate is [...] a class with [...] no base classes [...].
145 data().Aggregate = false;
146
147 // C++ [class]p4:
148 // A POD-struct is an aggregate class...
149 data().PlainOldData = false;
150 }
151
Anders Carlssonabb20e62010-03-29 05:13:12 +0000152 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000153 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000154
155 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000156 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000157
Craig Toppere6337e12015-12-25 00:36:02 +0000158 data().Bases = new(C) CXXBaseSpecifier [NumBases];
159 data().NumBases = NumBases;
160 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000161 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000162 // Keep track of inherited vbases for this base class.
163 const CXXBaseSpecifier *Base = Bases[i];
164 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000165 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000166 if (BaseType->isDependentType())
167 continue;
168 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000169 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000170
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000171 // A class with a non-empty base class is not empty.
172 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000173 if (!BaseClassDecl->isEmpty()) {
174 if (!data().Empty) {
175 // C++0x [class]p7:
176 // A standard-layout class is a class that:
177 // [...]
178 // -- either has no non-static data members in the most derived
179 // class and at most one base class with non-static data members,
180 // or has no base classes with non-static data members, and
181 // If this is the second non-empty base, then neither of these two
182 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000183 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000184 }
185
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000186 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000187 data().HasNoNonEmptyBases = false;
188 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000189
Douglas Gregor11c024b2010-09-28 20:50:54 +0000190 // C++ [class.virtual]p1:
191 // A class that declares or inherits a virtual function is called a
192 // polymorphic class.
193 if (BaseClassDecl->isPolymorphic())
194 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000195
Chandler Carruthb1963742011-04-30 09:17:45 +0000196 // C++0x [class]p7:
197 // A standard-layout class is a class that: [...]
198 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000199 if (!BaseClassDecl->isStandardLayout())
200 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000201
Chandler Carruthe71d0622011-04-24 02:49:34 +0000202 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000203 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000204 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000205
Anders Carlssonabb20e62010-03-29 05:13:12 +0000206 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000207 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000208 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000210 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000211
212 // C++11 [class.copy]p8:
213 // The implicitly-declared copy constructor for a class X will have
214 // the form 'X::X(const X&)' if each [...] virtual base class B of X
215 // has a copy constructor whose first parameter is of type
216 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000217 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000218 if (!VBaseDecl->hasCopyConstructorWithConstParam())
219 data().ImplicitCopyConstructorHasConstParam = false;
220 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000221 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000222
223 if (Base->isVirtual()) {
224 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000225 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000226 VBases.push_back(Base);
227
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000228 // C++0x [meta.unary.prop] is_empty:
229 // T is a class type, but not a union type, with ... no virtual base
230 // classes
231 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000232
Richard Smith328aae52012-11-30 05:11:39 +0000233 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
234 // A [default constructor, copy/move constructor, or copy/move assignment
235 // operator for a class X] is trivial [...] if:
236 // -- class X has [...] no virtual base classes
237 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000238
239 // C++0x [class]p7:
240 // A standard-layout class is a class that: [...]
241 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000242 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000243
244 // C++11 [dcl.constexpr]p4:
245 // In the definition of a constexpr constructor [...]
246 // -- the class shall not have any virtual base classes
247 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000248 } else {
249 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000250 // A default constructor is trivial [...] if:
251 // -- all the direct base classes of its class have trivial default
252 // constructors.
253 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
255
Chandler Carruthad7d4042011-04-23 23:10:33 +0000256 // C++0x [class.copy]p13:
257 // A copy/move constructor for class X is trivial if [...]
258 // [...]
259 // -- the constructor selected to copy/move each direct base class
260 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000262 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000263 // If the base class doesn't have a simple move constructor, we'll eagerly
264 // declare it and perform overload resolution to determine which function
265 // it actually calls. If it does have a simple move constructor, this
266 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000267 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000268 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000269
270 // C++0x [class.copy]p27:
271 // A copy/move assignment operator for class X is trivial if [...]
272 // [...]
273 // -- the assignment operator selected to copy/move each direct base
274 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000275 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000276 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000277 // If the base class doesn't have a simple move assignment, we'll eagerly
278 // declare it and perform overload resolution to determine which function
279 // it actually calls. If it does have a simple move assignment, this
280 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000281 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000282 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000283
284 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000285 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000286 // requirements of a constexpr constructor, the implicitly-defined
287 // default constructor is constexpr.
288 if (!BaseClassDecl->hasConstexprDefaultConstructor())
289 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000290 }
Richard Smith92f241f2012-12-08 02:53:02 +0000291
Douglas Gregor11c024b2010-09-28 20:50:54 +0000292 // C++ [class.ctor]p3:
293 // A destructor is trivial if all the direct base classes of its class
294 // have trivial destructors.
295 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000296 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000297
298 if (!BaseClassDecl->hasIrrelevantDestructor())
299 data().HasIrrelevantDestructor = false;
300
Richard Smith1c33fe82012-11-28 06:23:12 +0000301 // C++11 [class.copy]p18:
302 // The implicitly-declared copy assignment oeprator for a class X will
303 // have the form 'X& X::operator=(const X&)' if each direct base class B
304 // of X has a copy assignment operator whose parameter is of type 'const
305 // B&', 'const volatile B&', or 'B' [...]
306 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
307 data().ImplicitCopyAssignmentHasConstParam = false;
308
309 // C++11 [class.copy]p8:
310 // The implicitly-declared copy constructor for a class X will have
311 // the form 'X::X(const X&)' if each direct [...] base class B of X
312 // has a copy constructor whose first parameter is of type
313 // 'const B&' or 'const volatile B&' [...]
314 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
315 data().ImplicitCopyConstructorHasConstParam = false;
316
John McCall31168b02011-06-15 23:02:42 +0000317 // A class has an Objective-C object member if... or any of its bases
318 // has an Objective-C object member.
319 if (BaseClassDecl->hasObjectMember())
320 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000321
322 if (BaseClassDecl->hasVolatileMember())
323 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000324
Douglas Gregor61226d32011-05-13 01:05:07 +0000325 // Keep track of the presence of mutable fields.
326 if (BaseClassDecl->hasMutableFields())
327 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000328
329 if (BaseClassDecl->hasUninitializedReferenceMember())
330 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000331
Nico Weber6a6376b2016-02-19 01:52:46 +0000332 if (!BaseClassDecl->allowConstDefaultInit())
333 data().HasUninitializedFields = true;
334
Richard Smith6b02d462012-12-08 08:32:28 +0000335 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000336 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000337
David Majnemer5ef4fe72014-06-13 06:43:46 +0000338 if (VBases.empty()) {
339 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000340 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000341 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000342
343 // Create base specifier for any direct or indirect virtual bases.
344 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
345 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000346 for (int I = 0, E = VBases.size(); I != E; ++I) {
347 QualType Type = VBases[I]->getType();
348 if (!Type->isDependentType())
349 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000350 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000351 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000352
353 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000354}
355
356void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
357 // C++11 [class.copy]p11:
358 // A defaulted copy/move constructor for a class X is defined as
359 // deleted if X has:
360 // -- a direct or virtual base class B that cannot be copied/moved [...]
361 // -- a non-static data member of class type M (or array thereof)
362 // that cannot be copied or moved [...]
363 if (!Subobj->hasSimpleMoveConstructor())
364 data().NeedOverloadResolutionForMoveConstructor = true;
365
366 // C++11 [class.copy]p23:
367 // A defaulted copy/move assignment operator for a class X is defined as
368 // deleted if X has:
369 // -- a direct or virtual base class B that cannot be copied/moved [...]
370 // -- a non-static data member of class type M (or array thereof)
371 // that cannot be copied or moved [...]
372 if (!Subobj->hasSimpleMoveAssignment())
373 data().NeedOverloadResolutionForMoveAssignment = true;
374
375 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
376 // A defaulted [ctor or dtor] for a class X is defined as
377 // deleted if X has:
378 // -- any direct or virtual base class [...] has a type with a destructor
379 // that is deleted or inaccessible from the defaulted [ctor or dtor].
380 // -- any non-static data member has a type with a destructor
381 // that is deleted or inaccessible from the defaulted [ctor or dtor].
382 if (!Subobj->hasSimpleDestructor()) {
383 data().NeedOverloadResolutionForMoveConstructor = true;
384 data().NeedOverloadResolutionForDestructor = true;
385 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000386}
387
Douglas Gregord2e6a452010-01-14 17:47:39 +0000388bool CXXRecordDecl::hasAnyDependentBases() const {
389 if (!isDependentContext())
390 return false;
391
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000392 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000393}
394
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000395bool CXXRecordDecl::isTriviallyCopyable() const {
396 // C++0x [class]p5:
397 // A trivially copyable class is a class that:
398 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000399 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000400 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000401 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000402 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000403 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000404 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000405 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000406 // -- has a trivial destructor.
407 if (!hasTrivialDestructor()) return false;
408
409 return true;
410}
411
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000412void CXXRecordDecl::markedVirtualFunctionPure() {
413 // C++ [class.abstract]p2:
414 // A class is abstract if it has at least one pure virtual function.
415 data().Abstract = true;
416}
417
418void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000419 if (!D->isImplicit() &&
420 !isa<FieldDecl>(D) &&
421 !isa<IndirectFieldDecl>(D) &&
422 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
423 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
424 data().HasOnlyCMembers = false;
425
426 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000427 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000428 return;
429
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000430 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
431 if (FunTmpl)
432 D = FunTmpl->getTemplatedDecl();
433
Douglas Gregora832d3e2010-09-28 19:45:33 +0000434 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
435 if (Method->isVirtual()) {
436 // C++ [dcl.init.aggr]p1:
437 // An aggregate is an array or a class with [...] no virtual functions.
438 data().Aggregate = false;
439
440 // C++ [class]p4:
441 // A POD-struct is an aggregate class...
442 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000443
444 // Virtual functions make the class non-empty.
445 // FIXME: Standard ref?
446 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000447
448 // C++ [class.virtual]p1:
449 // A class that declares or inherits a virtual function is called a
450 // polymorphic class.
451 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000452
Richard Smith328aae52012-11-30 05:11:39 +0000453 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
454 // A [default constructor, copy/move constructor, or copy/move
455 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000456 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000457 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000458
Chandler Carruthb1963742011-04-30 09:17:45 +0000459 // C++0x [class]p7:
460 // A standard-layout class is a class that: [...]
461 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000462 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000463 }
464 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000465
Richard Smith1c33fe82012-11-28 06:23:12 +0000466 // Notify the listener if an implicit member was added after the definition
467 // was completed.
468 if (!isBeingDefined() && D->isImplicit())
469 if (ASTMutationListener *L = getASTMutationListener())
470 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000471
Richard Smith328aae52012-11-30 05:11:39 +0000472 // The kind of special member this declaration is, if any.
473 unsigned SMKind = 0;
474
Richard Smith1c33fe82012-11-28 06:23:12 +0000475 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000476 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000477 if (!Constructor->isImplicit()) {
478 // Note that we have a user-declared constructor.
479 data().UserDeclaredConstructor = true;
480
481 // C++ [class]p4:
482 // A POD-struct is an aggregate class [...]
483 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
484 // type is technically an aggregate in C++0x since it wouldn't be in 03.
485 data().PlainOldData = false;
486 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000487
Richard Smitha5285072011-09-05 02:13:09 +0000488 // Technically, "user-provided" is only defined for special member
489 // functions, but the intent of the standard is clearly that it should apply
490 // to all functions.
491 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000492
Alexis Hunt88c75c32011-05-09 21:45:35 +0000493 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000494 SMKind |= SMF_DefaultConstructor;
495
496 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000497 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000498 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000499 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000500 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000501
Chandler Carruthad7d4042011-04-23 23:10:33 +0000502 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000503 unsigned Quals;
504 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000505 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000506
507 if (Quals & Qualifiers::Const)
508 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000509 } else if (Constructor->isMoveConstructor())
510 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000511 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000512
513 // Record if we see any constexpr constructors which are neither copy
514 // nor move constructors.
515 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000516 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000517
Alexis Hunt88c75c32011-05-09 21:45:35 +0000518 // C++ [dcl.init.aggr]p1:
519 // An aggregate is an array or a class with no user-declared
520 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000521 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000522 // An aggregate is an array or a class with no user-provided
523 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000524 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000525 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000526 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000527 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000528
Richard Smith1c33fe82012-11-28 06:23:12 +0000529 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000530 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000531 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000532
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000533 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000534 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000535 // If the destructor is explicitly defaulted and not trivial or not public
536 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
537 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000538
Richard Smith1c33fe82012-11-28 06:23:12 +0000539 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000540 // A destructor is trivial if [...] the destructor is not virtual.
541 if (DD->isVirtual())
542 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000543 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000544
545 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000546 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000547 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000548 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000549
550 const ReferenceType *ParamTy =
551 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
552 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
553 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000554 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000555
Richard Smith328aae52012-11-30 05:11:39 +0000556 if (Method->isMoveAssignmentOperator())
557 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000558
Douglas Gregor457104e2010-09-29 04:25:11 +0000559 // Keep the list of conversion functions up-to-date.
560 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000561 // FIXME: We use the 'unsafe' accessor for the access specifier here,
562 // because Sema may not have set it yet. That's really just a misdesign
563 // in Sema. However, LLDB *will* have set the access specifier correctly,
564 // and adds declarations after the class is technically completed,
565 // so completeDefinition()'s overriding of the access specifiers doesn't
566 // work.
567 AccessSpecifier AS = Conversion->getAccessUnsafe();
568
Richard Smith328aae52012-11-30 05:11:39 +0000569 if (Conversion->getPrimaryTemplate()) {
570 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000571 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000572 ASTContext &Ctx = getASTContext();
573 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
574 NamedDecl *Primary =
575 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
576 if (Primary->getPreviousDecl())
577 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
578 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000579 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000580 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000581 }
582 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000583
Richard Smith328aae52012-11-30 05:11:39 +0000584 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000585 // If this is the first declaration of a special member, we no longer have
586 // an implicit trivial special member.
587 data().HasTrivialSpecialMembers &=
588 data().DeclaredSpecialMembers | ~SMKind;
589
590 if (!Method->isImplicit() && !Method->isUserProvided()) {
591 // This method is user-declared but not user-provided. We can't work out
592 // whether it's trivial yet (not until we get to the end of the class).
593 // We'll handle this method in finishedDefaultedOrDeletedMember.
594 } else if (Method->isTrivial())
595 data().HasTrivialSpecialMembers |= SMKind;
596 else
597 data().DeclaredNonTrivialSpecialMembers |= SMKind;
598
Richard Smith328aae52012-11-30 05:11:39 +0000599 // Note when we have declared a declared special member, and suppress the
600 // implicit declaration of this special member.
601 data().DeclaredSpecialMembers |= SMKind;
602
603 if (!Method->isImplicit()) {
604 data().UserDeclaredSpecialMembers |= SMKind;
605
606 // C++03 [class]p4:
607 // A POD-struct is an aggregate class that has [...] no user-defined
608 // copy assignment operator and no user-defined destructor.
609 //
610 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
611 // aggregates could not have any constructors, clear it even for an
612 // explicitly defaulted or deleted constructor.
613 // type is technically an aggregate in C++0x since it wouldn't be in 03.
614 //
615 // Also, a user-declared move assignment operator makes a class non-POD.
616 // This is an extension in C++03.
617 data().PlainOldData = false;
618 }
Richard Smith328aae52012-11-30 05:11:39 +0000619 }
620
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000621 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000622 }
Richard Smith328aae52012-11-30 05:11:39 +0000623
Douglas Gregora832d3e2010-09-28 19:45:33 +0000624 // Handle non-static data members.
625 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000626 // C++ [class.bit]p2:
627 // A declaration for a bit-field that omits the identifier declares an
628 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
629 // initialized.
630 if (Field->isUnnamedBitfield())
631 return;
632
Douglas Gregora832d3e2010-09-28 19:45:33 +0000633 // C++ [dcl.init.aggr]p1:
634 // An aggregate is an array or a class (clause 9) with [...] no
635 // private or protected non-static data members (clause 11).
636 //
637 // A POD must be an aggregate.
638 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
639 data().Aggregate = false;
640 data().PlainOldData = false;
641 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000642
643 // C++0x [class]p7:
644 // A standard-layout class is a class that:
645 // [...]
646 // -- has the same access control for all non-static data members,
647 switch (D->getAccess()) {
648 case AS_private: data().HasPrivateFields = true; break;
649 case AS_protected: data().HasProtectedFields = true; break;
650 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000651 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000652 };
653 if ((data().HasPrivateFields + data().HasProtectedFields +
654 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000655 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000656
Douglas Gregor61226d32011-05-13 01:05:07 +0000657 // Keep track of the presence of mutable fields.
658 if (Field->isMutable())
659 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000660
661 // C++11 [class.union]p8, DR1460:
662 // If X is a union, a non-static data member of X that is not an anonymous
663 // union is a variant member of X.
664 if (isUnion() && !Field->isAnonymousStructOrUnion())
665 data().HasVariantMembers = true;
666
Chandler Carruthad7d4042011-04-23 23:10:33 +0000667 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000668 // A POD struct is a class that is both a trivial class and a
669 // standard-layout class, and has no non-static data members of type
670 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000671 //
672 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
673 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000674 ASTContext &Context = getASTContext();
675 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000676 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000677 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000678 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000679 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
680 // Objective-C Automatic Reference Counting:
681 // If a class has a non-static data member of Objective-C pointer
682 // type (or array thereof), it is a non-POD type and its
683 // default constructor (if any), copy constructor, move constructor,
684 // copy assignment operator, move assignment operator, and destructor are
685 // non-trivial.
686 setHasObjectMember(true);
687 struct DefinitionData &Data = data();
688 Data.PlainOldData = false;
689 Data.HasTrivialSpecialMembers = 0;
690 Data.HasIrrelevantDestructor = false;
691 }
Eli Friedmana5433322013-07-20 01:06:31 +0000692 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000693 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000694
Chandler Carruthb1963742011-04-30 09:17:45 +0000695 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000696 if (!Field->hasInClassInitializer())
697 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000698
Chandler Carruthb1963742011-04-30 09:17:45 +0000699 // C++0x [class]p7:
700 // A standard-layout class is a class that:
701 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000702 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000703 }
704
Nico Weber6a6376b2016-02-19 01:52:46 +0000705 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
706 if (CXXRecordDecl *FieldType = Field->getType()->getAsCXXRecordDecl()) {
707 if (!FieldType->allowConstDefaultInit())
708 data().HasUninitializedFields = true;
709 } else {
710 data().HasUninitializedFields = true;
711 }
712 }
713
Richard Smith3607ffe2012-02-13 03:54:03 +0000714 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000715 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000716 data().HasNonLiteralTypeFieldsOrBases = true;
717
Richard Smithab44d5b2013-12-10 08:25:00 +0000718 if (Field->hasInClassInitializer() ||
719 (Field->isAnonymousStructOrUnion() &&
720 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000721 data().HasInClassInitializer = true;
722
723 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000724 // A default constructor is trivial if [...] no non-static data member
725 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000726 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000727
Richard Smithe2648ba2012-05-07 01:07:30 +0000728 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000729 // An aggregate is a [...] class with [...] no
730 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000731 //
732 // This rule was removed in C++1y.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000733 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000734 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000735
Richard Smithe2648ba2012-05-07 01:07:30 +0000736 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000737 // A POD struct is [...] a trivial class.
738 data().PlainOldData = false;
739 }
740
Richard Smith6b02d462012-12-08 08:32:28 +0000741 // C++11 [class.copy]p23:
742 // A defaulted copy/move assignment operator for a class X is defined
743 // as deleted if X has:
744 // -- a non-static data member of reference type
745 if (T->isReferenceType())
746 data().DefaultedMoveAssignmentIsDeleted = true;
747
Douglas Gregor11c024b2010-09-28 20:50:54 +0000748 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
749 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
750 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000751 addedClassSubobject(FieldRec);
752
Richard Smithc91d12c2013-11-25 07:07:05 +0000753 // We may need to perform overload resolution to determine whether a
754 // field can be moved if it's const or volatile qualified.
755 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
756 data().NeedOverloadResolutionForMoveConstructor = true;
757 data().NeedOverloadResolutionForMoveAssignment = true;
758 }
759
Richard Smith6b02d462012-12-08 08:32:28 +0000760 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
761 // A defaulted [special member] for a class X is defined as
762 // deleted if:
763 // -- X is a union-like class that has a variant member with a
764 // non-trivial [corresponding special member]
765 if (isUnion()) {
766 if (FieldRec->hasNonTrivialMoveConstructor())
767 data().DefaultedMoveConstructorIsDeleted = true;
768 if (FieldRec->hasNonTrivialMoveAssignment())
769 data().DefaultedMoveAssignmentIsDeleted = true;
770 if (FieldRec->hasNonTrivialDestructor())
771 data().DefaultedDestructorIsDeleted = true;
772 }
773
Alexis Huntf479f1b2011-05-09 18:22:59 +0000774 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000775 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000776 // -- for all the non-static data members of its class that are of
777 // class type (or array thereof), each such class has a trivial
778 // default constructor.
779 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000780 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000781
782 // C++0x [class.copy]p13:
783 // A copy/move constructor for class X is trivial if [...]
784 // [...]
785 // -- for each non-static data member of X that is of class type (or
786 // an array thereof), the constructor selected to copy/move that
787 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000788 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000789 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000790 // If the field doesn't have a simple move constructor, we'll eagerly
791 // declare the move constructor for this class and we'll decide whether
792 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000793 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000794 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000795
796 // C++0x [class.copy]p27:
797 // A copy/move assignment operator for class X is trivial if [...]
798 // [...]
799 // -- for each non-static data member of X that is of class type (or
800 // an array thereof), the assignment operator selected to
801 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000802 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000803 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000804 // If the field doesn't have a simple move assignment, we'll eagerly
805 // declare the move assignment for this class and we'll decide whether
806 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000807 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000808 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000809
Douglas Gregor11c024b2010-09-28 20:50:54 +0000810 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000811 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000812 if (!FieldRec->hasIrrelevantDestructor())
813 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000814 if (FieldRec->hasObjectMember())
815 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000816 if (FieldRec->hasVolatileMember())
817 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000818
819 // C++0x [class]p7:
820 // A standard-layout class is a class that:
821 // -- has no non-static data members of type non-standard-layout
822 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000823 if (!FieldRec->isStandardLayout())
824 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000825
826 // C++0x [class]p7:
827 // A standard-layout class is a class that:
828 // [...]
829 // -- has no base classes of the same type as the first non-static
830 // data member.
831 // We don't want to expend bits in the state of the record decl
832 // tracking whether this is the first non-static data member so we
833 // cheat a bit and use some of the existing state: the empty bit.
834 // Virtual bases and virtual methods make a class non-empty, but they
835 // also make it non-standard-layout so we needn't check here.
836 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000837 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000838 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000839 // data member must come through here with Empty still true, and Empty
840 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000841 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000842 for (const auto &BI : bases()) {
843 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000844 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000845 break;
846 }
847 }
848 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000849
850 // Keep track of the presence of mutable fields.
851 if (FieldRec->hasMutableFields())
852 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000853
854 // C++11 [class.copy]p13:
855 // If the implicitly-defined constructor would satisfy the
856 // requirements of a constexpr constructor, the implicitly-defined
857 // constructor is constexpr.
858 // C++11 [dcl.constexpr]p4:
859 // -- every constructor involved in initializing non-static data
860 // members [...] shall be a constexpr constructor
861 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000862 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000863 // The standard requires any in-class initializer to be a constant
864 // expression. We consider this to be a defect.
865 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000866
867 // C++11 [class.copy]p8:
868 // The implicitly-declared copy constructor for a class X will have
869 // the form 'X::X(const X&)' if [...] for all the non-static data
870 // members of X that are of a class type M (or array thereof), each
871 // such class type has a copy constructor whose first parameter is
872 // of type 'const M&' or 'const volatile M&'.
873 if (!FieldRec->hasCopyConstructorWithConstParam())
874 data().ImplicitCopyConstructorHasConstParam = false;
875
876 // C++11 [class.copy]p18:
877 // The implicitly-declared copy assignment oeprator for a class X will
878 // have the form 'X& X::operator=(const X&)' if [...] for all the
879 // non-static data members of X that are of a class type M (or array
880 // thereof), each such class type has a copy assignment operator whose
881 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
882 if (!FieldRec->hasCopyAssignmentWithConstParam())
883 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000884
885 if (FieldRec->hasUninitializedReferenceMember() &&
886 !Field->hasInClassInitializer())
887 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000888
889 // C++11 [class.union]p8, DR1460:
890 // a non-static data member of an anonymous union that is a member of
891 // X is also a variant member of X.
892 if (FieldRec->hasVariantMembers() &&
893 Field->isAnonymousStructOrUnion())
894 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000895 }
Richard Smithcc36f692011-12-22 02:22:31 +0000896 } else {
897 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000898 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000899 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000900 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000901
902 // C++11 [class.copy]p23:
903 // A defaulted copy/move assignment operator for a class X is defined
904 // as deleted if X has:
905 // -- a non-static data member of const non-class type (or array
906 // thereof)
907 if (T.isConstQualified())
908 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000909 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000910
911 // C++0x [class]p7:
912 // A standard-layout class is a class that:
913 // [...]
914 // -- either has no non-static data members in the most derived
915 // class and at most one base class with non-static data members,
916 // or has no base classes with non-static data members, and
917 // At this point we know that we have a non-static data member, so the last
918 // clause holds.
919 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000920 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000921
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000922 // If this is not a zero-length bit-field, then the class is not empty.
923 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000924 if (!Field->isBitField() ||
925 (!Field->getBitWidth()->isTypeDependent() &&
926 !Field->getBitWidth()->isValueDependent() &&
927 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000928 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000929 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000930 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000931
932 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000933 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000934 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000935 == DeclarationName::CXXConversionFunctionName) {
936 ASTContext &Ctx = getASTContext();
937 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
938 }
939 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000940}
941
Richard Smith92f241f2012-12-08 02:53:02 +0000942void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
943 assert(!D->isImplicit() && !D->isUserProvided());
944
945 // The kind of special member this declaration is, if any.
946 unsigned SMKind = 0;
947
948 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
949 if (Constructor->isDefaultConstructor()) {
950 SMKind |= SMF_DefaultConstructor;
951 if (Constructor->isConstexpr())
952 data().HasConstexprDefaultConstructor = true;
953 }
954 if (Constructor->isCopyConstructor())
955 SMKind |= SMF_CopyConstructor;
956 else if (Constructor->isMoveConstructor())
957 SMKind |= SMF_MoveConstructor;
958 else if (Constructor->isConstexpr())
959 // We may now know that the constructor is constexpr.
960 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000961 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000962 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000963 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
964 data().HasIrrelevantDestructor = false;
965 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000966 SMKind |= SMF_CopyAssignment;
967 else if (D->isMoveAssignmentOperator())
968 SMKind |= SMF_MoveAssignment;
969
970 // Update which trivial / non-trivial special members we have.
971 // addedMember will have skipped this step for this member.
972 if (D->isTrivial())
973 data().HasTrivialSpecialMembers |= SMKind;
974 else
975 data().DeclaredNonTrivialSpecialMembers |= SMKind;
976}
977
Joao Matose9a3ed42012-08-31 22:18:20 +0000978bool CXXRecordDecl::isCLike() const {
979 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
980 !TemplateOrInstantiation.isNull())
981 return false;
982 if (!hasDefinition())
983 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000984
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000985 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000986}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000987
Faisal Vali2b391ab2013-09-26 19:54:12 +0000988bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000989 if (!isLambda()) return false;
990 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000991}
992
993CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000994 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +0000995 DeclarationName Name =
996 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +0000997 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000998
999 assert(!Calls.empty() && "Missing lambda call operator!");
1000 assert(Calls.size() == 1 && "More than one lambda call operator!");
1001
1002 NamedDecl *CallOp = Calls.front();
1003 if (FunctionTemplateDecl *CallOpTmpl =
1004 dyn_cast<FunctionTemplateDecl>(CallOp))
1005 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001006
1007 return cast<CXXMethodDecl>(CallOp);
1008}
1009
1010CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001011 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001012 DeclarationName Name =
1013 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001014 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001015 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001016 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1017 NamedDecl *InvokerFun = Invoker.front();
1018 if (FunctionTemplateDecl *InvokerTemplate =
1019 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1020 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1021
Faisal Vali571df122013-09-29 08:45:24 +00001022 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001023}
1024
Douglas Gregor9c702202012-02-10 07:45:31 +00001025void CXXRecordDecl::getCaptureFields(
1026 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001027 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001028 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001029 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001030
Douglas Gregorc8a73492012-02-13 15:44:47 +00001031 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001032 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001033 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001034 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001035 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001036 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001037 else if (C->capturesVariable())
1038 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001039 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001040 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001041}
1042
Faisal Vali2b391ab2013-09-26 19:54:12 +00001043TemplateParameterList *
1044CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001045 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001046 CXXMethodDecl *CallOp = getLambdaCallOperator();
1047 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1048 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001049 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001050}
Douglas Gregor9c702202012-02-10 07:45:31 +00001051
John McCall1e3a1a72010-03-15 09:07:48 +00001052static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001053 QualType T =
1054 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1055 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001056 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001057}
1058
John McCall1e3a1a72010-03-15 09:07:48 +00001059/// Collect the visible conversions of a base class.
1060///
James Dennettb5d5f812012-06-15 22:28:09 +00001061/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001062/// \param InVirtual whether this base class is a virtual base (or a base
1063/// of a virtual base)
1064/// \param Access the access along the inheritance path to this base
1065/// \param ParentHiddenTypes the conversions provided by the inheritors
1066/// of this base
1067/// \param Output the set to which to add conversions from non-virtual bases
1068/// \param VOutput the set to which to add conversions from virtual bases
1069/// \param HiddenVBaseCs the set of conversions which were hidden in a
1070/// virtual base along some inheritance path
1071static void CollectVisibleConversions(ASTContext &Context,
1072 CXXRecordDecl *Record,
1073 bool InVirtual,
1074 AccessSpecifier Access,
1075 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001076 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001077 UnresolvedSetImpl &VOutput,
1078 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1079 // The set of types which have conversions in this class or its
1080 // subclasses. As an optimization, we don't copy the derived set
1081 // unless it might change.
1082 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1083 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1084
1085 // Collect the direct conversions and figure out which conversions
1086 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001087 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1088 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1089 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001090 HiddenTypesBuffer = ParentHiddenTypes;
1091 HiddenTypes = &HiddenTypesBuffer;
1092
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001093 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001094 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1095 bool Hidden = ParentHiddenTypes.count(ConvType);
1096 if (!Hidden)
1097 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001098
1099 // If this conversion is hidden and we're in a virtual base,
1100 // remember that it's hidden along some inheritance path.
1101 if (Hidden && InVirtual)
1102 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1103
1104 // If this conversion isn't hidden, add it to the appropriate output.
1105 else if (!Hidden) {
1106 AccessSpecifier IAccess
1107 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1108
1109 if (InVirtual)
1110 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001111 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001112 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001113 }
1114 }
1115 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001116
John McCall1e3a1a72010-03-15 09:07:48 +00001117 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001118 for (const auto &I : Record->bases()) {
1119 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001120 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001121
John McCall1e3a1a72010-03-15 09:07:48 +00001122 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001123 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1124 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001125
John McCall1e3a1a72010-03-15 09:07:48 +00001126 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1127 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1128 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001129 }
John McCall1e3a1a72010-03-15 09:07:48 +00001130}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001131
John McCall1e3a1a72010-03-15 09:07:48 +00001132/// Collect the visible conversions of a class.
1133///
1134/// This would be extremely straightforward if it weren't for virtual
1135/// bases. It might be worth special-casing that, really.
1136static void CollectVisibleConversions(ASTContext &Context,
1137 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001138 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001139 // The collection of all conversions in virtual bases that we've
1140 // found. These will be added to the output as long as they don't
1141 // appear in the hidden-conversions set.
1142 UnresolvedSet<8> VBaseCs;
1143
1144 // The set of conversions in virtual bases that we've determined to
1145 // be hidden.
1146 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1147
1148 // The set of types hidden by classes derived from this one.
1149 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1150
1151 // Go ahead and collect the direct conversions and add them to the
1152 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001153 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1154 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001155 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001156 for (; ConvI != ConvE; ++ConvI)
1157 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001158
1159 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001160 for (const auto &I : Record->bases()) {
1161 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001162 if (!RT) continue;
1163
1164 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001165 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001166 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1167 }
1168
1169 // Add any unhidden conversions provided by virtual bases.
1170 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1171 I != E; ++I) {
1172 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001173 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001174 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001175}
1176
1177/// getVisibleConversionFunctions - get all conversion functions visible
1178/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001179llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001180CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001181 ASTContext &Ctx = getASTContext();
1182
1183 ASTUnresolvedSet *Set;
1184 if (bases_begin() == bases_end()) {
1185 // If root class, all conversions are visible.
1186 Set = &data().Conversions.get(Ctx);
1187 } else {
1188 Set = &data().VisibleConversions.get(Ctx);
1189 // If visible conversion list is not evaluated, evaluate it.
1190 if (!data().ComputedVisibleConversions) {
1191 CollectVisibleConversions(Ctx, this, *Set);
1192 data().ComputedVisibleConversions = true;
1193 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001194 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001195 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001196}
1197
John McCallda4458e2010-03-31 01:36:47 +00001198void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1199 // This operation is O(N) but extremely rare. Sema only uses it to
1200 // remove UsingShadowDecls in a class that were followed by a direct
1201 // declaration, e.g.:
1202 // class A : B {
1203 // using B::operator int;
1204 // operator int();
1205 // };
1206 // This is uncommon by itself and even more uncommon in conjunction
1207 // with sufficiently large numbers of directly-declared conversions
1208 // that asymptotic behavior matters.
1209
Richard Smitha4ba74c2013-08-30 04:46:40 +00001210 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001211 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1212 if (Convs[I].getDecl() == ConvDecl) {
1213 Convs.erase(I);
1214 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1215 && "conversion was found multiple times in unresolved set");
1216 return;
1217 }
1218 }
1219
1220 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001221}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001222
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001223CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001224 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001225 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001226
1227 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001228}
1229
Chandler Carruth21c90602015-12-30 03:24:14 +00001230MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1231 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1232}
1233
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001234void
1235CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1236 TemplateSpecializationKind TSK) {
1237 assert(TemplateOrInstantiation.isNull() &&
1238 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001239 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001240 TemplateOrInstantiation
1241 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1242}
1243
Chandler Carruth21c90602015-12-30 03:24:14 +00001244ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1245 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1246}
1247
1248void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1249 TemplateOrInstantiation = Template;
1250}
1251
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001252TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1253 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001254 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1255 return Spec->getSpecializationKind();
1256
Douglas Gregor06db9f52009-10-12 20:18:28 +00001257 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001258 return MSInfo->getTemplateSpecializationKind();
1259
1260 return TSK_Undeclared;
1261}
1262
1263void
1264CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1265 if (ClassTemplateSpecializationDecl *Spec
1266 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1267 Spec->setSpecializationKind(TSK);
1268 return;
1269 }
1270
Douglas Gregor06db9f52009-10-12 20:18:28 +00001271 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001272 MSInfo->setTemplateSpecializationKind(TSK);
1273 return;
1274 }
1275
David Blaikie83d382b2011-09-23 05:06:16 +00001276 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001277}
1278
Reid Klecknere7367d62014-10-14 20:28:40 +00001279const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1280 // If it's a class template specialization, find the template or partial
1281 // specialization from which it was instantiated.
1282 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1283 auto From = TD->getInstantiatedFrom();
1284 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1285 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1286 if (NewCTD->isMemberSpecialization())
1287 break;
1288 CTD = NewCTD;
1289 }
Richard Smith7a591a42015-07-08 02:22:15 +00001290 return CTD->getTemplatedDecl()->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001291 }
1292 if (auto *CTPSD =
1293 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1294 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1295 if (NewCTPSD->isMemberSpecialization())
1296 break;
1297 CTPSD = NewCTPSD;
1298 }
Richard Smith7a591a42015-07-08 02:22:15 +00001299 return CTPSD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001300 }
1301 }
1302
1303 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1304 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1305 const CXXRecordDecl *RD = this;
1306 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1307 RD = NewRD;
Richard Smith7a591a42015-07-08 02:22:15 +00001308 return RD->getDefinition();
Reid Klecknere7367d62014-10-14 20:28:40 +00001309 }
1310 }
1311
1312 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1313 "couldn't find pattern for class template instantiation");
1314 return nullptr;
1315}
1316
Douglas Gregorbac74902010-07-01 14:13:13 +00001317CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1318 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001319 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001320
1321 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001322 = Context.DeclarationNames.getCXXDestructorName(
1323 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001324
Richard Smithcf4bdde2015-02-21 02:45:19 +00001325 DeclContext::lookup_result R = lookup(Name);
David Blaikieff7d47a2012-12-19 00:45:41 +00001326 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001327 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001328
David Blaikieff7d47a2012-12-19 00:45:41 +00001329 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001330 return Dtor;
1331}
1332
Richard Trieu95a192a2015-05-28 00:14:02 +00001333bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1334 // Destructor is noreturn.
1335 if (const CXXDestructorDecl *Destructor = getDestructor())
1336 if (Destructor->isNoReturn())
1337 return true;
1338
1339 // Check base classes destructor for noreturn.
1340 for (const auto &Base : bases())
1341 if (Base.getType()->getAsCXXRecordDecl()->isAnyDestructorNoReturn())
1342 return true;
1343
1344 // Check fields for noreturn.
1345 for (const auto *Field : fields())
1346 if (const CXXRecordDecl *RD =
1347 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1348 if (RD->isAnyDestructorNoReturn())
1349 return true;
1350
1351 // All destructors are not noreturn.
1352 return false;
1353}
1354
Douglas Gregorb11aad82011-02-19 18:51:44 +00001355void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001356 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001357}
1358
1359void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1360 RecordDecl::completeDefinition();
1361
Douglas Gregor8fb95122010-09-29 00:15:42 +00001362 // If the class may be abstract (but hasn't been marked as such), check for
1363 // any pure final overriders.
1364 if (mayBeAbstract()) {
1365 CXXFinalOverriderMap MyFinalOverriders;
1366 if (!FinalOverriders) {
1367 getFinalOverriders(MyFinalOverriders);
1368 FinalOverriders = &MyFinalOverriders;
1369 }
1370
1371 bool Done = false;
1372 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1373 MEnd = FinalOverriders->end();
1374 M != MEnd && !Done; ++M) {
1375 for (OverridingMethods::iterator SO = M->second.begin(),
1376 SOEnd = M->second.end();
1377 SO != SOEnd && !Done; ++SO) {
1378 assert(SO->second.size() > 0 &&
1379 "All virtual functions have overridding virtual functions");
1380
1381 // C++ [class.abstract]p4:
1382 // A class is abstract if it contains or inherits at least one
1383 // pure virtual function for which the final overrider is pure
1384 // virtual.
1385 if (SO->second.front().Method->isPure()) {
1386 data().Abstract = true;
1387 Done = true;
1388 break;
1389 }
1390 }
1391 }
1392 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001393
1394 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001395 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001396 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001397 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001398}
1399
1400bool CXXRecordDecl::mayBeAbstract() const {
1401 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1402 isDependentContext())
1403 return false;
1404
Aaron Ballman574705e2014-03-13 15:41:46 +00001405 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001406 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001407 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001408 if (BaseDecl->isAbstract())
1409 return true;
1410 }
1411
1412 return false;
1413}
1414
David Blaikie68e081d2011-12-20 02:48:34 +00001415void CXXMethodDecl::anchor() { }
1416
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001417bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001418 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001419
1420 if (MD->getStorageClass() == SC_Static)
1421 return true;
1422
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001423 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1424 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001425}
1426
Rafael Espindola49e860b2012-06-26 17:45:31 +00001427static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1428 const CXXMethodDecl *BaseMD) {
1429 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1430 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1431 const CXXMethodDecl *MD = *I;
1432 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1433 return true;
1434 if (recursivelyOverrides(MD, BaseMD))
1435 return true;
1436 }
1437 return false;
1438}
1439
1440CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001441CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1442 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001443 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1444 return this;
1445
1446 // Lookup doesn't work for destructors, so handle them separately.
1447 if (isa<CXXDestructorDecl>(this)) {
1448 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001449 if (MD) {
1450 if (recursivelyOverrides(MD, this))
1451 return MD;
1452 if (MayBeBase && recursivelyOverrides(this, MD))
1453 return MD;
1454 }
Craig Topper36250ad2014-05-12 05:36:57 +00001455 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001456 }
1457
Richard Smith40c78062015-02-21 02:31:57 +00001458 for (auto *ND : RD->lookup(getDeclName())) {
1459 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001460 if (!MD)
1461 continue;
1462 if (recursivelyOverrides(MD, this))
1463 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001464 if (MayBeBase && recursivelyOverrides(this, MD))
1465 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001466 }
1467
Aaron Ballman574705e2014-03-13 15:41:46 +00001468 for (const auto &I : RD->bases()) {
1469 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001470 if (!RT)
1471 continue;
1472 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1473 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1474 if (T)
1475 return T;
1476 }
1477
Craig Topper36250ad2014-05-12 05:36:57 +00001478 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001479}
1480
Ted Kremenek21475702008-09-05 17:16:31 +00001481CXXMethodDecl *
1482CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001483 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001484 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001485 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001486 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001487 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001488 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1489 T, TInfo, SC, isInline, isConstexpr,
1490 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001491}
1492
Douglas Gregor72172e92012-01-05 21:55:30 +00001493CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001494 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001495 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001496 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001497}
1498
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001499bool CXXMethodDecl::isUsualDeallocationFunction() const {
1500 if (getOverloadedOperator() != OO_Delete &&
1501 getOverloadedOperator() != OO_Array_Delete)
1502 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001503
1504 // C++ [basic.stc.dynamic.deallocation]p2:
1505 // A template instance is never a usual deallocation function,
1506 // regardless of its signature.
1507 if (getPrimaryTemplate())
1508 return false;
1509
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001510 // C++ [basic.stc.dynamic.deallocation]p2:
1511 // If a class T has a member deallocation function named operator delete
1512 // with exactly one parameter, then that function is a usual (non-placement)
1513 // deallocation function. [...]
1514 if (getNumParams() == 1)
1515 return true;
1516
1517 // C++ [basic.stc.dynamic.deallocation]p2:
1518 // [...] If class T does not declare such an operator delete but does
1519 // declare a member deallocation function named operator delete with
1520 // exactly two parameters, the second of which has type std::size_t (18.1),
1521 // then this function is a usual deallocation function.
1522 ASTContext &Context = getASTContext();
1523 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001524 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1525 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001526 return false;
1527
1528 // This function is a usual deallocation function if there are no
1529 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00001530 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
1531 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00001532 I != E; ++I) {
1533 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001534 if (FD->getNumParams() == 1)
1535 return false;
1536 }
1537
1538 return true;
1539}
1540
Douglas Gregorb139cd52010-05-01 20:49:11 +00001541bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001542 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001543 // A user-declared copy assignment operator X::operator= is a non-static
1544 // non-template member function of class X with exactly one parameter of
1545 // type X, X&, const X&, volatile X& or const volatile X&.
1546 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1547 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001548 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1549 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001550 return false;
1551
1552 QualType ParamType = getParamDecl(0)->getType();
1553 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1554 ParamType = Ref->getPointeeType();
1555
1556 ASTContext &Context = getASTContext();
1557 QualType ClassType
1558 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1559 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1560}
1561
Alexis Huntfcaeae42011-05-25 20:50:04 +00001562bool CXXMethodDecl::isMoveAssignmentOperator() const {
1563 // C++0x [class.copy]p19:
1564 // A user-declared move assignment operator X::operator= is a non-static
1565 // non-template member function of class X with exactly one parameter of type
1566 // X&&, const X&&, volatile X&&, or const volatile X&&.
1567 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001568 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1569 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001570 return false;
1571
1572 QualType ParamType = getParamDecl(0)->getType();
1573 if (!isa<RValueReferenceType>(ParamType))
1574 return false;
1575 ParamType = ParamType->getPointeeType();
1576
1577 ASTContext &Context = getASTContext();
1578 QualType ClassType
1579 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1580 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1581}
1582
Anders Carlsson36d87e12009-05-16 23:58:37 +00001583void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001584 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001585 assert(!MD->getParent()->isDependentContext() &&
1586 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001587 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001588
Douglas Gregor832940b2010-03-02 23:58:15 +00001589 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001590}
1591
1592CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001593 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001594 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001595}
1596
1597CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001598 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001599 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001600}
1601
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001602unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001603 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001604 return getASTContext().overridden_methods_size(this);
1605}
1606
Ted Kremenek21475702008-09-05 17:16:31 +00001607QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001608 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1609 // If the member function is declared const, the type of this is const X*,
1610 // if the member function is declared volatile, the type of this is
1611 // volatile X*, and if the member function is declared const volatile,
1612 // the type of this is const volatile X*.
1613
Ted Kremenek21475702008-09-05 17:16:31 +00001614 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001615
John McCalle78aac42010-03-10 03:28:59 +00001616 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001617 ClassTy = C.getQualifiedType(ClassTy,
1618 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001619 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001620}
1621
Eli Friedman71a26d82009-12-06 20:50:05 +00001622bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001623 // If this function is a template instantiation, look at the template from
1624 // which it was instantiated.
1625 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1626 if (!CheckFn)
1627 CheckFn = this;
1628
Eli Friedman71a26d82009-12-06 20:50:05 +00001629 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001630 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001631}
1632
Douglas Gregor355efbb2012-02-17 03:02:34 +00001633bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001634 const CXXRecordDecl *P = getParent();
1635 if (P->isLambda()) {
1636 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1637 if (StaticInvoker == this) return true;
1638 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1639 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1640 }
1641 }
1642 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001643}
1644
Alexis Hunt1d792652011-01-08 20:30:50 +00001645CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1646 TypeSourceInfo *TInfo, bool IsVirtual,
1647 SourceLocation L, Expr *Init,
1648 SourceLocation R,
1649 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001650 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001651 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1652 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001653{
Douglas Gregore8381c02008-11-05 04:29:56 +00001654}
1655
Alexis Hunt1d792652011-01-08 20:30:50 +00001656CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1657 FieldDecl *Member,
1658 SourceLocation MemberLoc,
1659 SourceLocation L, Expr *Init,
1660 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001661 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001662 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001663 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1664{
1665}
1666
Alexis Hunt1d792652011-01-08 20:30:50 +00001667CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1668 IndirectFieldDecl *Member,
1669 SourceLocation MemberLoc,
1670 SourceLocation L, Expr *Init,
1671 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001672 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001673 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001674 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001675{
Douglas Gregore8381c02008-11-05 04:29:56 +00001676}
1677
Alexis Hunt1d792652011-01-08 20:30:50 +00001678CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001679 TypeSourceInfo *TInfo,
1680 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001681 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001682 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1683 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001684 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1685{
1686}
1687
1688CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001689 FieldDecl *Member,
1690 SourceLocation MemberLoc,
1691 SourceLocation L, Expr *Init,
1692 SourceLocation R,
1693 VarDecl **Indices,
1694 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001695 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001696 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001697 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001698{
James Y Knight967eb202015-12-29 22:13:13 +00001699 std::uninitialized_copy(Indices, Indices + NumIndices,
1700 getTrailingObjects<VarDecl *>());
Douglas Gregor94f9a482010-05-05 05:51:00 +00001701}
1702
Alexis Hunt1d792652011-01-08 20:30:50 +00001703CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1704 FieldDecl *Member,
1705 SourceLocation MemberLoc,
1706 SourceLocation L, Expr *Init,
1707 SourceLocation R,
1708 VarDecl **Indices,
1709 unsigned NumIndices) {
James Y Knight967eb202015-12-29 22:13:13 +00001710 void *Mem = Context.Allocate(totalSizeToAlloc<VarDecl *>(NumIndices),
Alexis Hunt1d792652011-01-08 20:30:50 +00001711 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001712 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1713 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001714}
1715
Alexis Hunt1d792652011-01-08 20:30:50 +00001716TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001717 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001718 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001719 else
1720 return TypeLoc();
1721}
1722
Alexis Hunt1d792652011-01-08 20:30:50 +00001723const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001724 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001725 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001726 else
Craig Topper36250ad2014-05-12 05:36:57 +00001727 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001728}
1729
Alexis Hunt1d792652011-01-08 20:30:50 +00001730SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001731 if (isInClassMemberInitializer())
1732 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001733
David Blaikiea81d4102015-01-18 00:12:58 +00001734 if (isAnyMemberInitializer())
1735 return getMemberLocation();
1736
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001737 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1738 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1739
1740 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001741}
1742
Alexis Hunt1d792652011-01-08 20:30:50 +00001743SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001744 if (isInClassMemberInitializer()) {
1745 FieldDecl *D = getAnyMember();
1746 if (Expr *I = D->getInClassInitializer())
1747 return I->getSourceRange();
1748 return SourceRange();
1749 }
1750
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001751 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001752}
1753
David Blaikie68e081d2011-12-20 02:48:34 +00001754void CXXConstructorDecl::anchor() { }
1755
Douglas Gregor61956c42008-10-31 09:07:45 +00001756CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001757CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001758 return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001759 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001760 nullptr, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001761}
1762
1763CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001764CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001765 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001766 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001767 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001768 bool isExplicit, bool isInline,
1769 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001770 assert(NameInfo.getName().getNameKind()
1771 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001772 "Name must refer to a constructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001773 return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001774 isExplicit, isInline,
1775 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001776}
1777
Richard Smithc2bb8182015-03-24 06:36:48 +00001778CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
1779 return CtorInitializers.get(getASTContext().getExternalSource());
1780}
1781
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001782CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1783 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1784 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1785 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1786 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001787
1788 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001789}
1790
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001791bool CXXConstructorDecl::isDefaultConstructor() const {
1792 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001793 // A default constructor for a class X is a constructor of class
1794 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001795 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001796 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001797}
1798
Mike Stump11289f42009-09-09 15:08:12 +00001799bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001800CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001801 return isCopyOrMoveConstructor(TypeQuals) &&
1802 getParamDecl(0)->getType()->isLValueReferenceType();
1803}
1804
1805bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1806 return isCopyOrMoveConstructor(TypeQuals) &&
1807 getParamDecl(0)->getType()->isRValueReferenceType();
1808}
1809
1810/// \brief Determine whether this is a copy or move constructor.
1811bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001812 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001813 // A non-template constructor for class X is a copy constructor
1814 // if its first parameter is of type X&, const X&, volatile X& or
1815 // const volatile X&, and either there are no other parameters
1816 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001817 // C++0x [class.copy]p3:
1818 // A non-template constructor for class X is a move constructor if its
1819 // first parameter is of type X&&, const X&&, volatile X&&, or
1820 // const volatile X&&, and either there are no other parameters or else
1821 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001822 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001823 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001824 (getPrimaryTemplate() != nullptr) ||
1825 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001826 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001827
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001828 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001829
1830 // Do we have a reference type?
1831 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001832 if (!ParamRefType)
1833 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001834
Douglas Gregorff7028a2009-11-13 23:59:09 +00001835 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001836 ASTContext &Context = getASTContext();
1837
Douglas Gregorff7028a2009-11-13 23:59:09 +00001838 CanQualType PointeeType
1839 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001840 CanQualType ClassTy
1841 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001842 if (PointeeType.getUnqualifiedType() != ClassTy)
1843 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001844
John McCall8ccfcb52009-09-24 19:53:00 +00001845 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001846
1847 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001848 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001849 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001850}
1851
Anders Carlssond20e7952009-08-28 16:57:08 +00001852bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001853 // C++ [class.conv.ctor]p1:
1854 // A constructor declared without the function-specifier explicit
1855 // that can be called with a single parameter specifies a
1856 // conversion from the type of its first parameter to the type of
1857 // its class. Such a constructor is called a converting
1858 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001859 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001860 return false;
1861
Mike Stump11289f42009-09-09 15:08:12 +00001862 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001863 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001864 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001865 (getNumParams() > 1 &&
1866 (getParamDecl(1)->hasDefaultArg() ||
1867 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001868}
Douglas Gregor61956c42008-10-31 09:07:45 +00001869
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001870bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001871 if ((getNumParams() < 1) ||
1872 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001873 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001874 return false;
1875
1876 const ParmVarDecl *Param = getParamDecl(0);
1877
1878 ASTContext &Context = getASTContext();
1879 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1880
Douglas Gregorffe14e32009-11-14 01:20:54 +00001881 // Is it the same as our our class type?
1882 CanQualType ClassTy
1883 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1884 if (ParamType.getUnqualifiedType() != ClassTy)
1885 return false;
1886
1887 return true;
1888}
1889
Sebastian Redl08905022011-02-05 19:23:19 +00001890const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1891 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001892 method_iterator It = getASTContext().overridden_methods_begin(this);
1893 if (It == getASTContext().overridden_methods_end(this))
Craig Topper36250ad2014-05-12 05:36:57 +00001894 return nullptr;
Sebastian Redl08905022011-02-05 19:23:19 +00001895
1896 return cast<CXXConstructorDecl>(*It);
1897}
1898
1899void
1900CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1901 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001902 assert(getASTContext().overridden_methods_size(this) == 0 &&
1903 "Base ctor already set.");
1904 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001905}
1906
David Blaikie68e081d2011-12-20 02:48:34 +00001907void CXXDestructorDecl::anchor() { }
1908
Douglas Gregor831c93f2008-11-05 20:51:48 +00001909CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001910CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001911 return new (C, ID)
1912 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1913 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001914}
1915
1916CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001917CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001918 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001919 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001920 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001921 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001922 assert(NameInfo.getName().getNameKind()
1923 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001924 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001925 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001926 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001927}
1928
Richard Smithf8134002015-03-10 01:41:22 +00001929void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD) {
1930 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
1931 if (OD && !First->OperatorDelete) {
1932 First->OperatorDelete = OD;
1933 if (auto *L = getASTMutationListener())
1934 L->ResolvedOperatorDelete(First, OD);
1935 }
1936}
1937
David Blaikie68e081d2011-12-20 02:48:34 +00001938void CXXConversionDecl::anchor() { }
1939
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001940CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001941CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001942 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001943 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001944 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00001945 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001946}
1947
1948CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001949CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001950 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001951 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001952 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001953 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001954 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001955 assert(NameInfo.getName().getNameKind()
1956 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001957 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00001958 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001959 isInline, isExplicit, isConstexpr,
1960 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001961}
1962
Douglas Gregord3b672c2012-02-16 01:06:16 +00001963bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1964 return isImplicit() && getParent()->isLambda() &&
1965 getConversionType()->isBlockPointerType();
1966}
1967
David Blaikie68e081d2011-12-20 02:48:34 +00001968void LinkageSpecDecl::anchor() { }
1969
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001970LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001971 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001972 SourceLocation ExternLoc,
1973 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001974 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001975 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00001976 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001977}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001978
Richard Smithf7981722013-11-22 09:01:48 +00001979LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1980 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001981 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
1982 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001983}
1984
David Blaikie68e081d2011-12-20 02:48:34 +00001985void UsingDirectiveDecl::anchor() { }
1986
Douglas Gregor889ceb72009-02-03 19:21:40 +00001987UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1988 SourceLocation L,
1989 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001990 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001991 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001992 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001993 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001994 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1995 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001996 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1997 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001998}
1999
Richard Smithf7981722013-11-22 09:01:48 +00002000UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2001 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002002 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2003 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002004 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002005 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002006}
2007
Sebastian Redla6602e92009-11-23 15:34:23 +00002008NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
2009 if (NamespaceAliasDecl *NA =
2010 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
2011 return NA->getNamespace();
2012 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2013}
2014
Richard Smith053f6c62014-05-16 23:01:30 +00002015NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2016 SourceLocation StartLoc, SourceLocation IdLoc,
2017 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2018 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
2019 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
2020 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002021 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002022
Douglas Gregore57e7522012-01-07 09:11:48 +00002023 if (PrevDecl)
2024 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2025}
2026
Douglas Gregor72172e92012-01-05 21:55:30 +00002027NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002028 bool Inline, SourceLocation StartLoc,
2029 SourceLocation IdLoc, IdentifierInfo *Id,
2030 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002031 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2032 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002033}
2034
2035NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002036 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002037 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002038}
2039
Chandler Carruth21c90602015-12-30 03:24:14 +00002040NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2041 if (isFirstDecl())
2042 return this;
2043
2044 return AnonOrFirstNamespaceAndInline.getPointer();
2045}
2046
2047const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2048 if (isFirstDecl())
2049 return this;
2050
2051 return AnonOrFirstNamespaceAndInline.getPointer();
2052}
2053
2054bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2055
Richard Smithd7af8a32014-05-10 01:17:36 +00002056NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2057 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002058}
2059NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2060 return getPreviousDecl();
2061}
2062NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2063 return getMostRecentDecl();
2064}
2065
David Blaikie68e081d2011-12-20 02:48:34 +00002066void NamespaceAliasDecl::anchor() { }
2067
Richard Smithf4634362014-09-03 23:11:22 +00002068NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2069 return getNextRedeclaration();
2070}
2071NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2072 return getPreviousDecl();
2073}
2074NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2075 return getMostRecentDecl();
2076}
2077
Mike Stump11289f42009-09-09 15:08:12 +00002078NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002079 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002080 SourceLocation AliasLoc,
2081 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002082 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002083 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002084 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002085 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002086 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2087 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002088 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002089 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002090}
2091
Douglas Gregor72172e92012-01-05 21:55:30 +00002092NamespaceAliasDecl *
2093NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002094 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002095 SourceLocation(), nullptr,
2096 NestedNameSpecifierLoc(),
2097 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002098}
2099
David Blaikie68e081d2011-12-20 02:48:34 +00002100void UsingShadowDecl::anchor() { }
2101
Douglas Gregor72172e92012-01-05 21:55:30 +00002102UsingShadowDecl *
2103UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002104 return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2105 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002106}
2107
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002108UsingDecl *UsingShadowDecl::getUsingDecl() const {
2109 const UsingShadowDecl *Shadow = this;
2110 while (const UsingShadowDecl *NextShadow =
2111 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2112 Shadow = NextShadow;
2113 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2114}
2115
David Blaikie68e081d2011-12-20 02:48:34 +00002116void UsingDecl::anchor() { }
2117
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002118void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2119 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2120 "declaration already in set");
2121 assert(S->getUsingDecl() == this);
2122
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002123 if (FirstUsingShadow.getPointer())
2124 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2125 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002126}
2127
2128void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2129 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2130 "declaration not in set");
2131 assert(S->getUsingDecl() == this);
2132
2133 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2134
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002135 if (FirstUsingShadow.getPointer() == S) {
2136 FirstUsingShadow.setPointer(
2137 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002138 S->UsingOrNextShadow = this;
2139 return;
2140 }
2141
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002142 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002143 while (Prev->UsingOrNextShadow != S)
2144 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2145 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2146 S->UsingOrNextShadow = this;
2147}
2148
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002149UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2150 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002151 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002152 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002153 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002154}
2155
Douglas Gregor72172e92012-01-05 21:55:30 +00002156UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002157 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2158 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2159 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002160}
2161
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002162SourceRange UsingDecl::getSourceRange() const {
2163 SourceLocation Begin = isAccessDeclaration()
2164 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2165 return SourceRange(Begin, getNameInfo().getEndLoc());
2166}
2167
David Blaikie68e081d2011-12-20 02:48:34 +00002168void UnresolvedUsingValueDecl::anchor() { }
2169
John McCalle61f2ba2009-11-18 02:36:19 +00002170UnresolvedUsingValueDecl *
2171UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2172 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002173 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002174 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002175 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2176 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002177}
2178
Douglas Gregor72172e92012-01-05 21:55:30 +00002179UnresolvedUsingValueDecl *
2180UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002181 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2182 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002183 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002184 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002185}
2186
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002187SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2188 SourceLocation Begin = isAccessDeclaration()
2189 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2190 return SourceRange(Begin, getNameInfo().getEndLoc());
2191}
2192
David Blaikie68e081d2011-12-20 02:48:34 +00002193void UnresolvedUsingTypenameDecl::anchor() { }
2194
John McCalle61f2ba2009-11-18 02:36:19 +00002195UnresolvedUsingTypenameDecl *
2196UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2197 SourceLocation UsingLoc,
2198 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002199 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002200 SourceLocation TargetNameLoc,
2201 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002202 return new (C, DC) UnresolvedUsingTypenameDecl(
2203 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2204 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002205}
2206
Douglas Gregor72172e92012-01-05 21:55:30 +00002207UnresolvedUsingTypenameDecl *
2208UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002209 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002210 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2211 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002212}
2213
David Blaikie68e081d2011-12-20 02:48:34 +00002214void StaticAssertDecl::anchor() { }
2215
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002216StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002217 SourceLocation StaticAssertLoc,
2218 Expr *AssertExpr,
2219 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002220 SourceLocation RParenLoc,
2221 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002222 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2223 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002224}
2225
Richard Smithf7981722013-11-22 09:01:48 +00002226StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002227 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002228 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2229 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002230}
2231
2232MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2233 SourceLocation L, DeclarationName N,
2234 QualType T, TypeSourceInfo *TInfo,
2235 SourceLocation StartL,
2236 IdentifierInfo *Getter,
2237 IdentifierInfo *Setter) {
2238 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2239}
2240
2241MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2242 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002243 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2244 DeclarationName(), QualType(), nullptr,
2245 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002246}
2247
Anders Carlsson6750d162009-03-26 23:46:50 +00002248static const char *getAccessName(AccessSpecifier AS) {
2249 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002250 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002251 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002252 case AS_public:
2253 return "public";
2254 case AS_private:
2255 return "private";
2256 case AS_protected:
2257 return "protected";
2258 }
David Blaikief47fa302012-01-17 02:30:50 +00002259 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002260}
2261
2262const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2263 AccessSpecifier AS) {
2264 return DB << getAccessName(AS);
2265}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002266
2267const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2268 AccessSpecifier AS) {
2269 return DB << getAccessName(AS);
2270}