blob: 12ec44fa027919087ce0535b4ac1ceba016ab096 [file] [log] [blame]
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001//===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
Ted Kremenek21475702008-09-05 17:16:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek21475702008-09-05 17:16:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the C++ related Decl classes.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000012
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"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000017#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000019#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000021#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000022#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000023#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000024#include "clang/AST/LambdaCapture.h"
25#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000026#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000027#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000028#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000029#include "clang/AST/UnresolvedSet.h"
30#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000031#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000032#include "clang/Basic/LLVM.h"
33#include "clang/Basic/LangOptions.h"
34#include "clang/Basic/OperatorKinds.h"
35#include "clang/Basic/PartialDiagnostic.h"
36#include "clang/Basic/SourceLocation.h"
37#include "clang/Basic/Specifiers.h"
38#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000040#include "llvm/ADT/SmallVector.h"
41#include "llvm/ADT/iterator_range.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
45#include <algorithm>
46#include <cassert>
47#include <cstddef>
48#include <cstdint>
49
Ted Kremenek21475702008-09-05 17:16:31 +000050using namespace clang;
51
52//===----------------------------------------------------------------------===//
53// Decl Allocation/Deallocation Method Implementations
54//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000055
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000056void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000057
Douglas Gregor72172e92012-01-05 21:55:30 +000058AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000059 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000060}
61
Richard Smitha4ba74c2013-08-30 04:46:40 +000062void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
63 ExternalASTSource *Source = C.getExternalSource();
64 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
65 assert(Source && "getFromExternalSource with no external source");
66
67 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
68 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
69 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
70 Impl.Decls.setLazy(false);
71}
72
John McCall67da35c2010-02-04 22:26:26 +000073CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000074 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
75 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000076 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
77 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000078 HasPrivateFields(false), HasProtectedFields(false),
79 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
80 HasOnlyCMembers(true), HasInClassInitializer(false),
81 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000082 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000083 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000084 NeedOverloadResolutionForMoveConstructor(false),
85 NeedOverloadResolutionForMoveAssignment(false),
86 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000087 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000088 DefaultedMoveConstructorIsDeleted(false),
89 DefaultedMoveAssignmentIsDeleted(false),
90 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000091 HasTrivialSpecialMembersForCall(SMF_All),
92 DeclaredNonTrivialSpecialMembers(0),
93 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000094 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000095 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000096 DefaultedDefaultConstructorIsConstexpr(true),
97 HasConstexprDefaultConstructor(false),
Richard Smith457226e2019-09-23 03:48:44 +000098 DefaultedDestructorIsConstexpr(true),
Richard Smith91aeacc2019-10-11 00:29:04 +000099 HasNonLiteralTypeFieldsOrBases(false),
Nico Weber6a6376b2016-02-19 01:52:46 +0000100 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +0000101 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
102 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +0000103 ImplicitCopyAssignmentHasConstParam(true),
104 HasDeclaredCopyConstructorWithConstParam(false),
105 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Richard Smith91aeacc2019-10-11 00:29:04 +0000106 IsParsingBaseSpecifiers(false), ComputedVisibleConversions(false),
107 HasODRHash(false), Definition(D) {}
John McCall67da35c2010-02-04 22:26:26 +0000108
Benjamin Kramer300c0632012-07-04 17:03:33 +0000109CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
110 return Bases.get(Definition->getASTContext().getExternalSource());
111}
112
113CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
114 return VBases.get(Definition->getASTContext().getExternalSource());
115}
116
Richard Smith053f6c62014-05-16 23:01:30 +0000117CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
118 DeclContext *DC, SourceLocation StartLoc,
119 SourceLocation IdLoc, IdentifierInfo *Id,
120 CXXRecordDecl *PrevDecl)
121 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
122 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000123 : nullptr) {}
Douglas Gregorb6acda02008-11-12 23:21:09 +0000124
Jay Foad39c79802011-01-12 09:06:06 +0000125CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000126 DeclContext *DC, SourceLocation StartLoc,
127 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smithbecb92d2017-10-10 22:33:17 +0000128 CXXRecordDecl *PrevDecl,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000129 bool DelayTypeCreation) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000130 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
131 PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000132 R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
Mike Stump11289f42009-09-09 15:08:12 +0000133
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000134 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000135 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000136 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000137 return R;
138}
139
Richard Smith053f6c62014-05-16 23:01:30 +0000140CXXRecordDecl *
141CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
142 TypeSourceInfo *Info, SourceLocation Loc,
143 bool Dependent, bool IsGeneric,
144 LambdaCaptureDefault CaptureDefault) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000145 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
146 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000147 R->setBeingDefined(true);
Richard Smith64c06302014-05-22 23:19:02 +0000148 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000149 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000150 CaptureDefault);
Erich Keanef92f31c2018-08-01 20:48:16 +0000151 R->setMayHaveOutOfDateDef(false);
James Dennett8f60cdd2013-09-05 17:46:21 +0000152 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000153 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000154 return R;
155}
156
Douglas Gregor72172e92012-01-05 21:55:30 +0000157CXXRecordDecl *
158CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000159 auto *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000160 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000161 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000162 R->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000163 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000164}
165
Richard Smithb6070db2018-04-05 18:55:37 +0000166/// Determine whether a class has a repeated base class. This is intended for
167/// use when determining if a class is standard-layout, so makes no attempt to
168/// handle virtual bases.
169static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
170 llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
171 SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
172 while (!WorkList.empty()) {
173 const CXXRecordDecl *RD = WorkList.pop_back_val();
174 for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
175 if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
176 if (!SeenBaseTypes.insert(B).second)
177 return true;
178 WorkList.push_back(B);
179 }
180 }
181 }
182 return false;
183}
184
Mike Stump11289f42009-09-09 15:08:12 +0000185void
Craig Toppere6337e12015-12-25 00:36:02 +0000186CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
187 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000188 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000189
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000190 if (!data().Bases.isOffset() && data().NumBases > 0)
191 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000192
Craig Toppere6337e12015-12-25 00:36:02 +0000193 if (NumBases) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000194 if (!C.getLangOpts().CPlusPlus17) {
Richard Smith872307e2016-03-08 22:17:41 +0000195 // C++ [dcl.init.aggr]p1:
196 // An aggregate is [...] a class with [...] no base classes [...].
197 data().Aggregate = false;
198 }
Richard Smithaed32b42011-10-18 20:08:55 +0000199
200 // C++ [class]p4:
201 // A POD-struct is an aggregate class...
202 data().PlainOldData = false;
203 }
204
Anders Carlssonabb20e62010-03-29 05:13:12 +0000205 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000206 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Fangrui Song6907ce22018-07-30 19:24:48 +0000207
Anders Carlssonabb20e62010-03-29 05:13:12 +0000208 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000209 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000210
Craig Toppere6337e12015-12-25 00:36:02 +0000211 data().Bases = new(C) CXXBaseSpecifier [NumBases];
212 data().NumBases = NumBases;
213 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000214 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000215 // Keep track of inherited vbases for this base class.
216 const CXXBaseSpecifier *Base = Bases[i];
217 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000218 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000219 if (BaseType->isDependentType())
220 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000221 auto *BaseClassDecl =
Simon Pilgrim1cd399c2019-10-03 11:22:48 +0000222 cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000223
Richard Smithb6070db2018-04-05 18:55:37 +0000224 // C++2a [class]p7:
225 // A standard-layout class is a class that:
226 // [...]
227 // -- has all non-static data members and bit-fields in the class and
228 // its base classes first declared in the same class
229 if (BaseClassDecl->data().HasBasesWithFields ||
230 !BaseClassDecl->field_empty()) {
231 if (data().HasBasesWithFields)
232 // Two bases have members or bit-fields: not standard-layout.
Chandler Carruth583edf82011-04-30 10:07:30 +0000233 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000234 data().HasBasesWithFields = true;
235 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000236
Richard Smithb6070db2018-04-05 18:55:37 +0000237 // C++11 [class]p7:
238 // A standard-layout class is a class that:
239 // -- [...] has [...] at most one base class with non-static data
240 // members
241 if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
242 BaseClassDecl->hasDirectFields()) {
243 if (data().HasBasesWithNonStaticDataMembers)
244 data().IsCXX11StandardLayout = false;
245 data().HasBasesWithNonStaticDataMembers = true;
246 }
247
248 if (!BaseClassDecl->isEmpty()) {
David Majnemer5cfda6f2016-05-22 05:34:26 +0000249 // C++14 [meta.unary.prop]p4:
250 // T is a class type [...] with [...] no base class B for which
251 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000252 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000253 }
Richard Smithb6070db2018-04-05 18:55:37 +0000254
Richard Smith872307e2016-03-08 22:17:41 +0000255 // C++1z [dcl.init.agg]p1:
256 // An aggregate is a class with [...] no private or protected base classes
257 if (Base->getAccessSpecifier() != AS_public)
258 data().Aggregate = false;
259
Douglas Gregor11c024b2010-09-28 20:50:54 +0000260 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000261 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000262 // polymorphic class.
Richard Smithc00e4462018-06-14 20:03:22 +0000263 if (BaseClassDecl->isPolymorphic()) {
Douglas Gregor11c024b2010-09-28 20:50:54 +0000264 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000265
Richard Smithc00e4462018-06-14 20:03:22 +0000266 // An aggregate is a class with [...] no virtual functions.
267 data().Aggregate = false;
268 }
269
Chandler Carruthb1963742011-04-30 09:17:45 +0000270 // C++0x [class]p7:
271 // A standard-layout class is a class that: [...]
272 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000273 if (!BaseClassDecl->isStandardLayout())
274 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000275 if (!BaseClassDecl->isCXX11StandardLayout())
276 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000277
Chandler Carruthe71d0622011-04-24 02:49:34 +0000278 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000279 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000280 data().HasNonLiteralTypeFieldsOrBases = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000281
Anders Carlssonabb20e62010-03-29 05:13:12 +0000282 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000283 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000284 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000285 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000286 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000287
288 // C++11 [class.copy]p8:
289 // The implicitly-declared copy constructor for a class X will have
290 // the form 'X::X(const X&)' if each [...] virtual base class B of X
291 // has a copy constructor whose first parameter is of type
292 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000293 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000294 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000295 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000296
297 // C++1z [dcl.init.agg]p1:
298 // An aggregate is a class with [...] no virtual base classes
299 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000300 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000301 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000302
303 if (Base->isVirtual()) {
304 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000305 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000306 VBases.push_back(Base);
307
David Majnemer5cfda6f2016-05-22 05:34:26 +0000308 // C++14 [meta.unary.prop] is_empty:
309 // T is a class type, but not a union type, with ... no virtual base
310 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000311 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000312
Richard Smith872307e2016-03-08 22:17:41 +0000313 // C++1z [dcl.init.agg]p1:
314 // An aggregate is a class with [...] no virtual base classes
315 data().Aggregate = false;
316
Richard Smith328aae52012-11-30 05:11:39 +0000317 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
318 // A [default constructor, copy/move constructor, or copy/move assignment
319 // operator for a class X] is trivial [...] if:
320 // -- class X has [...] no virtual base classes
321 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000322 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000323
324 // C++0x [class]p7:
325 // A standard-layout class is a class that: [...]
326 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000327 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000328 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000329
Richard Smith457226e2019-09-23 03:48:44 +0000330 // C++20 [dcl.constexpr]p3:
331 // In the definition of a constexpr function [...]
332 // -- if the function is a constructor or destructor,
333 // its class shall not have any virtual base classes
Richard Smithcc36f692011-12-22 02:22:31 +0000334 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith457226e2019-09-23 03:48:44 +0000335 data().DefaultedDestructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000336
337 // C++1z [class.copy]p8:
338 // The implicitly-declared copy constructor for a class X will have
339 // the form 'X::X(const X&)' if each potentially constructed subobject
340 // has a copy constructor whose first parameter is of type
341 // 'const B&' or 'const volatile B&' [...]
342 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
343 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000344 } else {
345 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000346 // A default constructor is trivial [...] if:
347 // -- all the direct base classes of its class have trivial default
348 // constructors.
349 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000350 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
351
Chandler Carruthad7d4042011-04-23 23:10:33 +0000352 // C++0x [class.copy]p13:
353 // A copy/move constructor for class X is trivial if [...]
354 // [...]
355 // -- the constructor selected to copy/move each direct base class
356 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000357 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000358 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000359
360 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
361 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
362
Richard Smith6b02d462012-12-08 08:32:28 +0000363 // If the base class doesn't have a simple move constructor, we'll eagerly
364 // declare it and perform overload resolution to determine which function
365 // it actually calls. If it does have a simple move constructor, this
366 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000367 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000368 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000369
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000370 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
371 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
372
Chandler Carruthad7d4042011-04-23 23:10:33 +0000373 // C++0x [class.copy]p27:
374 // A copy/move assignment operator for class X is trivial if [...]
375 // [...]
376 // -- the assignment operator selected to copy/move each direct base
377 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000378 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000379 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000380 // If the base class doesn't have a simple move assignment, we'll eagerly
381 // declare it and perform overload resolution to determine which function
382 // it actually calls. If it does have a simple move assignment, this
383 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000384 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000385 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000386
387 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000388 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000389 // requirements of a constexpr constructor, the implicitly-defined
390 // default constructor is constexpr.
391 if (!BaseClassDecl->hasConstexprDefaultConstructor())
392 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000393
394 // C++1z [class.copy]p8:
395 // The implicitly-declared copy constructor for a class X will have
396 // the form 'X::X(const X&)' if each potentially constructed subobject
397 // has a copy constructor whose first parameter is of type
398 // 'const B&' or 'const volatile B&' [...]
399 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
400 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000401 }
Richard Smith92f241f2012-12-08 02:53:02 +0000402
Douglas Gregor11c024b2010-09-28 20:50:54 +0000403 // C++ [class.ctor]p3:
404 // A destructor is trivial if all the direct base classes of its class
405 // have trivial destructors.
406 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000407 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000408
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000409 if (!BaseClassDecl->hasTrivialDestructorForCall())
410 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
411
Richard Smith561fb152012-02-25 07:33:38 +0000412 if (!BaseClassDecl->hasIrrelevantDestructor())
413 data().HasIrrelevantDestructor = false;
414
Richard Smith1c33fe82012-11-28 06:23:12 +0000415 // C++11 [class.copy]p18:
416 // The implicitly-declared copy assignment oeprator for a class X will
417 // have the form 'X& X::operator=(const X&)' if each direct base class B
418 // of X has a copy assignment operator whose parameter is of type 'const
419 // B&', 'const volatile B&', or 'B' [...]
420 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
421 data().ImplicitCopyAssignmentHasConstParam = false;
422
John McCall31168b02011-06-15 23:02:42 +0000423 // A class has an Objective-C object member if... or any of its bases
424 // has an Objective-C object member.
425 if (BaseClassDecl->hasObjectMember())
426 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000427
Fariborz Jahanian78652202013-01-25 23:57:05 +0000428 if (BaseClassDecl->hasVolatileMember())
429 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000430
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000431 if (BaseClassDecl->getArgPassingRestrictions() ==
432 RecordDecl::APK_CanNeverPassInRegs)
433 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
434
Douglas Gregor61226d32011-05-13 01:05:07 +0000435 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000436 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000437 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000438 data().NeedOverloadResolutionForCopyConstructor = true;
439 }
Richard Smith593f9932012-12-08 02:01:17 +0000440
441 if (BaseClassDecl->hasUninitializedReferenceMember())
442 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000443
Nico Weber6a6376b2016-02-19 01:52:46 +0000444 if (!BaseClassDecl->allowConstDefaultInit())
445 data().HasUninitializedFields = true;
446
Richard Smith6b02d462012-12-08 08:32:28 +0000447 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000448 }
Richard Smithb6070db2018-04-05 18:55:37 +0000449
450 // C++2a [class]p7:
451 // A class S is a standard-layout class if it:
452 // -- has at most one base class subobject of any given type
453 //
454 // Note that we only need to check this for classes with more than one base
455 // class. If there's only one base class, and it's standard layout, then
456 // we know there are no repeated base classes.
457 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
458 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000459
David Majnemer5ef4fe72014-06-13 06:43:46 +0000460 if (VBases.empty()) {
461 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000462 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000463 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000464
465 // Create base specifier for any direct or indirect virtual bases.
466 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
467 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000468 for (int I = 0, E = VBases.size(); I != E; ++I) {
469 QualType Type = VBases[I]->getType();
470 if (!Type->isDependentType())
471 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000472 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000473 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000474
475 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000476}
477
Richard Trieufd1acbb2017-04-11 21:31:00 +0000478unsigned CXXRecordDecl::getODRHash() const {
479 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000480
Richard Trieufd1acbb2017-04-11 21:31:00 +0000481 // Previously calculated hash is stored in DefinitionData.
482 if (DefinitionData->HasODRHash)
483 return DefinitionData->ODRHash;
484
485 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000486 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000487 Hash.AddCXXRecordDecl(getDefinition());
488 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000489 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000490
491 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000492}
Richard Trieub6adf542017-02-18 02:09:28 +0000493
Richard Smith6b02d462012-12-08 08:32:28 +0000494void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
495 // C++11 [class.copy]p11:
496 // A defaulted copy/move constructor for a class X is defined as
497 // deleted if X has:
498 // -- a direct or virtual base class B that cannot be copied/moved [...]
499 // -- a non-static data member of class type M (or array thereof)
500 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000501 if (!Subobj->hasSimpleCopyConstructor())
502 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000503 if (!Subobj->hasSimpleMoveConstructor())
504 data().NeedOverloadResolutionForMoveConstructor = true;
505
506 // C++11 [class.copy]p23:
507 // A defaulted copy/move assignment operator for a class X is defined as
508 // deleted if X has:
509 // -- a direct or virtual base class B that cannot be copied/moved [...]
510 // -- a non-static data member of class type M (or array thereof)
511 // that cannot be copied or moved [...]
512 if (!Subobj->hasSimpleMoveAssignment())
513 data().NeedOverloadResolutionForMoveAssignment = true;
514
515 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
516 // A defaulted [ctor or dtor] for a class X is defined as
517 // deleted if X has:
518 // -- any direct or virtual base class [...] has a type with a destructor
519 // that is deleted or inaccessible from the defaulted [ctor or dtor].
520 // -- any non-static data member has a type with a destructor
521 // that is deleted or inaccessible from the defaulted [ctor or dtor].
522 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000523 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000524 data().NeedOverloadResolutionForMoveConstructor = true;
525 data().NeedOverloadResolutionForDestructor = true;
526 }
Richard Smith457226e2019-09-23 03:48:44 +0000527
528 // C++2a [dcl.constexpr]p4:
529 // The definition of a constexpr destructor [shall] satisfy the
530 // following requirement:
531 // -- for every subobject of class type or (possibly multi-dimensional)
532 // array thereof, that class type shall have a constexpr destructor
533 if (!Subobj->hasConstexprDestructor())
534 data().DefaultedDestructorIsConstexpr = false;
535}
536
537bool CXXRecordDecl::hasConstexprDestructor() const {
538 auto *Dtor = getDestructor();
539 return Dtor ? Dtor->isConstexpr() : defaultedDestructorIsConstexpr();
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000540}
541
Douglas Gregord2e6a452010-01-14 17:47:39 +0000542bool CXXRecordDecl::hasAnyDependentBases() const {
543 if (!isDependentContext())
544 return false;
545
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000546 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000547}
548
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000549bool CXXRecordDecl::isTriviallyCopyable() const {
550 // C++0x [class]p5:
551 // A trivially copyable class is a class that:
552 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000553 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000554 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000555 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000556 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000557 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000558 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000559 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000560 // -- has a trivial destructor.
561 if (!hasTrivialDestructor()) return false;
562
563 return true;
564}
565
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000566void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000567 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000568 // A class is abstract if it has at least one pure virtual function.
569 data().Abstract = true;
570}
571
Richard Smithb6070db2018-04-05 18:55:37 +0000572bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
573 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
574 if (!getNumBases())
575 return false;
576
577 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
578 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
579 SmallVector<const CXXRecordDecl*, 8> WorkList;
580
581 // Visit a type that we have determined is an element of M(S).
582 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
583 RD = RD->getCanonicalDecl();
584
585 // C++2a [class]p8:
586 // A class S is a standard-layout class if it [...] has no element of the
587 // set M(S) of types as a base class.
588 //
589 // If we find a subobject of an empty type, it might also be a base class,
590 // so we'll need to walk the base classes to check.
591 if (!RD->data().HasBasesWithFields) {
592 // Walk the bases the first time, stopping if we find the type. Build a
593 // set of them so we don't need to walk them again.
594 if (Bases.empty()) {
595 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
596 Base = Base->getCanonicalDecl();
597 if (RD == Base)
598 return false;
599 Bases.insert(Base);
600 return true;
601 });
602 if (RDIsBase)
603 return true;
604 } else {
605 if (Bases.count(RD))
606 return true;
607 }
608 }
609
610 if (M.insert(RD).second)
611 WorkList.push_back(RD);
612 return false;
613 };
614
615 if (Visit(XFirst))
616 return true;
617
618 while (!WorkList.empty()) {
619 const CXXRecordDecl *X = WorkList.pop_back_val();
620
621 // FIXME: We don't check the bases of X. That matches the standard, but
622 // that sure looks like a wording bug.
623
624 // -- If X is a non-union class type with a non-static data member
Richard Smith78b239e2019-06-20 20:44:45 +0000625 // [recurse to each field] that is either of zero size or is the
626 // first non-static data member of X
Richard Smithb6070db2018-04-05 18:55:37 +0000627 // -- If X is a union type, [recurse to union members]
Richard Smith78b239e2019-06-20 20:44:45 +0000628 bool IsFirstField = true;
Richard Smithb6070db2018-04-05 18:55:37 +0000629 for (auto *FD : X->fields()) {
630 // FIXME: Should we really care about the type of the first non-static
631 // data member of a non-union if there are preceding unnamed bit-fields?
632 if (FD->isUnnamedBitfield())
633 continue;
634
Richard Smith78b239e2019-06-20 20:44:45 +0000635 if (!IsFirstField && !FD->isZeroSize(Ctx))
636 continue;
637
Richard Smithb6070db2018-04-05 18:55:37 +0000638 // -- If X is n array type, [visit the element type]
639 QualType T = Ctx.getBaseElementType(FD->getType());
640 if (auto *RD = T->getAsCXXRecordDecl())
641 if (Visit(RD))
642 return true;
643
644 if (!X->isUnion())
Richard Smith78b239e2019-06-20 20:44:45 +0000645 IsFirstField = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000646 }
647 }
648
649 return false;
650}
651
Richard Smith864949b2018-09-27 22:47:04 +0000652bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
653 assert(isLambda() && "not a lambda");
654
655 // C++2a [expr.prim.lambda.capture]p11:
656 // The closure type associated with a lambda-expression has no default
657 // constructor if the lambda-expression has a lambda-capture and a
658 // defaulted default constructor otherwise. It has a deleted copy
659 // assignment operator if the lambda-expression has a lambda-capture and
660 // defaulted copy and move assignment operators otherwise.
661 //
662 // C++17 [expr.prim.lambda]p21:
663 // The closure type associated with a lambda-expression has no default
664 // constructor and a deleted copy assignment operator.
David Blaikie793231c2019-07-08 23:24:41 +0000665 if (getLambdaCaptureDefault() != LCD_None ||
666 getLambdaData().NumCaptures != 0)
Richard Smith864949b2018-09-27 22:47:04 +0000667 return false;
668 return getASTContext().getLangOpts().CPlusPlus2a;
669}
670
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000671void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000672 if (!D->isImplicit() &&
673 !isa<FieldDecl>(D) &&
674 !isa<IndirectFieldDecl>(D) &&
675 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
676 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
677 data().HasOnlyCMembers = false;
678
679 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000680 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000681 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000682
Eugene Zelenko7855e772018-04-03 00:11:50 +0000683 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000684 if (FunTmpl)
685 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000686
687 // FIXME: Pass NamedDecl* to addedMember?
688 Decl *DUnderlying = D;
689 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
690 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000691 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000692 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
693 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000694
Eugene Zelenko7855e772018-04-03 00:11:50 +0000695 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000696 if (Method->isVirtual()) {
697 // C++ [dcl.init.aggr]p1:
698 // An aggregate is an array or a class with [...] no virtual functions.
699 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000700
Douglas Gregora832d3e2010-09-28 19:45:33 +0000701 // C++ [class]p4:
702 // A POD-struct is an aggregate class...
703 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000704
David Majnemer5cfda6f2016-05-22 05:34:26 +0000705 // C++14 [meta.unary.prop]p4:
706 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000707 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000708
709 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000710 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000711 // polymorphic class.
712 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000713
Richard Smith328aae52012-11-30 05:11:39 +0000714 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
715 // A [default constructor, copy/move constructor, or copy/move
716 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000717 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000718 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000719 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000720
Chandler Carruthb1963742011-04-30 09:17:45 +0000721 // C++0x [class]p7:
722 // A standard-layout class is a class that: [...]
723 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000724 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000725 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000726 }
727 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000728
Richard Smith1c33fe82012-11-28 06:23:12 +0000729 // Notify the listener if an implicit member was added after the definition
730 // was completed.
731 if (!isBeingDefined() && D->isImplicit())
732 if (ASTMutationListener *L = getASTMutationListener())
733 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000734
Richard Smith328aae52012-11-30 05:11:39 +0000735 // The kind of special member this declaration is, if any.
736 unsigned SMKind = 0;
737
Richard Smith1c33fe82012-11-28 06:23:12 +0000738 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000739 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000740 if (!Constructor->isImplicit()) {
741 // Note that we have a user-declared constructor.
742 data().UserDeclaredConstructor = true;
743
744 // C++ [class]p4:
745 // A POD-struct is an aggregate class [...]
746 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
747 // type is technically an aggregate in C++0x since it wouldn't be in 03.
748 data().PlainOldData = false;
749 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000750
Alexis Hunt88c75c32011-05-09 21:45:35 +0000751 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000752 SMKind |= SMF_DefaultConstructor;
753
Richard Smith5179eb72016-06-28 19:03:57 +0000754 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000755 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000756 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000757 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000758 if (Constructor->isDefaulted())
759 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000760 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000761
Chandler Carruthad7d4042011-04-23 23:10:33 +0000762 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000763 unsigned Quals;
764 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000765 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000766
767 if (Quals & Qualifiers::Const)
768 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000769 } else if (Constructor->isMoveConstructor())
770 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000771 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000772
Eric Fiselier283d8d42016-12-03 01:26:47 +0000773 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith79c88c32018-09-26 19:00:16 +0000774 // An aggregate is an array or a class with no user-provided [or]
775 // explicit [...] constructors
776 // C++20 [dcl.init.aggr]p1:
777 // An aggregate is an array or a class with no user-declared [...]
778 // constructors
779 if (getASTContext().getLangOpts().CPlusPlus2a
780 ? !Constructor->isImplicit()
781 : (Constructor->isUserProvided() || Constructor->isExplicit()))
Eric Fiselier283d8d42016-12-03 01:26:47 +0000782 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000783 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000784
Richard Smith5179eb72016-06-28 19:03:57 +0000785 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000786 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000787 // Record if we see any constexpr constructors which are neither copy
788 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000789 // C++1z [basic.types]p10:
790 // [...] has at least one constexpr constructor or constructor template
791 // (possibly inherited from a base class) that is not a copy or move
792 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000793 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000794 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000795 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000796
Richard Smith1c33fe82012-11-28 06:23:12 +0000797 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000798 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000799 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000800
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000801 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000802 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000803 // If the destructor is explicitly defaulted and not trivial or not public
804 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
805 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000806
Richard Smith1c33fe82012-11-28 06:23:12 +0000807 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000808 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000809 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000810 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000811 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
812 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000813 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000814
815 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000816 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000817 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000818 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000819
Eugene Zelenko7855e772018-04-03 00:11:50 +0000820 const auto *ParamTy =
821 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000822 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
823 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000824 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000825
Richard Smith328aae52012-11-30 05:11:39 +0000826 if (Method->isMoveAssignmentOperator())
827 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000828
Douglas Gregor457104e2010-09-29 04:25:11 +0000829 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000830 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000831 // FIXME: We use the 'unsafe' accessor for the access specifier here,
832 // because Sema may not have set it yet. That's really just a misdesign
833 // in Sema. However, LLDB *will* have set the access specifier correctly,
834 // and adds declarations after the class is technically completed,
835 // so completeDefinition()'s overriding of the access specifiers doesn't
836 // work.
837 AccessSpecifier AS = Conversion->getAccessUnsafe();
838
Richard Smith328aae52012-11-30 05:11:39 +0000839 if (Conversion->getPrimaryTemplate()) {
840 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000841 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000842 ASTContext &Ctx = getASTContext();
843 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
844 NamedDecl *Primary =
845 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
846 if (Primary->getPreviousDecl())
847 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
848 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000849 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000850 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000851 }
852 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000853
Richard Smith328aae52012-11-30 05:11:39 +0000854 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000855 // If this is the first declaration of a special member, we no longer have
856 // an implicit trivial special member.
857 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000858 data().DeclaredSpecialMembers | ~SMKind;
859 data().HasTrivialSpecialMembersForCall &=
860 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000861
862 if (!Method->isImplicit() && !Method->isUserProvided()) {
863 // This method is user-declared but not user-provided. We can't work out
864 // whether it's trivial yet (not until we get to the end of the class).
865 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000866 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000867 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000868 data().HasTrivialSpecialMembersForCall |= SMKind;
869 } else if (Method->isTrivialForCall()) {
870 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000871 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000872 } else {
873 data().DeclaredNonTrivialSpecialMembers |= SMKind;
874 // If this is a user-provided function, do not set
875 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
876 // yet whether the method would be considered non-trivial for the
877 // purpose of calls (attribute "trivial_abi" can be dropped from the
878 // class later, which can change the special method's triviality).
879 if (!Method->isUserProvided())
880 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
881 }
Richard Smith92f241f2012-12-08 02:53:02 +0000882
Richard Smith328aae52012-11-30 05:11:39 +0000883 // Note when we have declared a declared special member, and suppress the
884 // implicit declaration of this special member.
885 data().DeclaredSpecialMembers |= SMKind;
886
887 if (!Method->isImplicit()) {
888 data().UserDeclaredSpecialMembers |= SMKind;
889
890 // C++03 [class]p4:
891 // A POD-struct is an aggregate class that has [...] no user-defined
892 // copy assignment operator and no user-defined destructor.
893 //
894 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
895 // aggregates could not have any constructors, clear it even for an
896 // explicitly defaulted or deleted constructor.
897 // type is technically an aggregate in C++0x since it wouldn't be in 03.
898 //
899 // Also, a user-declared move assignment operator makes a class non-POD.
900 // This is an extension in C++03.
901 data().PlainOldData = false;
902 }
Richard Smith328aae52012-11-30 05:11:39 +0000903 }
904
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000905 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000906 }
Richard Smith328aae52012-11-30 05:11:39 +0000907
Douglas Gregora832d3e2010-09-28 19:45:33 +0000908 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000909 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000910 ASTContext &Context = getASTContext();
911
Richard Smithb6070db2018-04-05 18:55:37 +0000912 // C++2a [class]p7:
913 // A standard-layout class is a class that:
914 // [...]
915 // -- has all non-static data members and bit-fields in the class and
916 // its base classes first declared in the same class
917 if (data().HasBasesWithFields)
918 data().IsStandardLayout = false;
919
Douglas Gregor556e5862011-10-10 17:22:13 +0000920 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000921 // A declaration for a bit-field that omits the identifier declares an
922 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000923 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000924 if (Field->isUnnamedBitfield()) {
925 // C++ [meta.unary.prop]p4: [LWG2358]
926 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
927 // length
928 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
929 Context.getLangOpts().getClangABICompat() >
930 LangOptions::ClangABI::Ver6)
931 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000932 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000933 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000934
Richard Smithb6070db2018-04-05 18:55:37 +0000935 // C++11 [class]p7:
936 // A standard-layout class is a class that:
937 // -- either has no non-static data members in the most derived class
938 // [...] or has no base classes with non-static data members
939 if (data().HasBasesWithNonStaticDataMembers)
940 data().IsCXX11StandardLayout = false;
941
Douglas Gregora832d3e2010-09-28 19:45:33 +0000942 // C++ [dcl.init.aggr]p1:
943 // An aggregate is an array or a class (clause 9) with [...] no
944 // private or protected non-static data members (clause 11).
945 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000946 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000947 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
948 data().Aggregate = false;
949 data().PlainOldData = false;
950 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000951
Richard Smithb6070db2018-04-05 18:55:37 +0000952 // Track whether this is the first field. We use this when checking
953 // whether the class is standard-layout below.
954 bool IsFirstField = !data().HasPrivateFields &&
955 !data().HasProtectedFields && !data().HasPublicFields;
956
Chandler Carruthb1963742011-04-30 09:17:45 +0000957 // C++0x [class]p7:
958 // A standard-layout class is a class that:
959 // [...]
960 // -- has the same access control for all non-static data members,
961 switch (D->getAccess()) {
962 case AS_private: data().HasPrivateFields = true; break;
963 case AS_protected: data().HasProtectedFields = true; break;
964 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000965 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000966 };
967 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000968 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000969 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000970 data().IsCXX11StandardLayout = false;
971 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000972
Douglas Gregor61226d32011-05-13 01:05:07 +0000973 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000974 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000975 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000976 data().NeedOverloadResolutionForCopyConstructor = true;
977 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000978
979 // C++11 [class.union]p8, DR1460:
980 // If X is a union, a non-static data member of X that is not an anonymous
981 // union is a variant member of X.
982 if (isUnion() && !Field->isAnonymousStructOrUnion())
983 data().HasVariantMembers = true;
984
Chandler Carruthad7d4042011-04-23 23:10:33 +0000985 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000986 // A POD struct is a class that is both a trivial class and a
987 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000988 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000989 //
990 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
991 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000992 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000993 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000994 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000995 // Objective-C Automatic Reference Counting:
996 // If a class has a non-static data member of Objective-C pointer
997 // type (or array thereof), it is a non-POD type and its
998 // default constructor (if any), copy constructor, move constructor,
999 // copy assignment operator, move assignment operator, and destructor are
1000 // non-trivial.
1001 setHasObjectMember(true);
1002 struct DefinitionData &Data = data();
1003 Data.PlainOldData = false;
1004 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001005
1006 // __strong or __weak fields do not make special functions non-trivial
1007 // for the purpose of calls.
1008 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
1009 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
1010 data().HasTrivialSpecialMembersForCall = 0;
1011
1012 // Structs with __weak fields should never be passed directly.
1013 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001014 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001015
Ben Langmuir11eab612014-09-26 15:27:29 +00001016 Data.HasIrrelevantDestructor = false;
Akira Hatanakaa5048972019-02-02 02:23:40 +00001017
1018 if (isUnion()) {
1019 data().DefaultedCopyConstructorIsDeleted = true;
1020 data().DefaultedMoveConstructorIsDeleted = true;
1021 data().DefaultedMoveAssignmentIsDeleted = true;
1022 data().DefaultedDestructorIsDeleted = true;
1023 data().NeedOverloadResolutionForCopyConstructor = true;
1024 data().NeedOverloadResolutionForMoveConstructor = true;
1025 data().NeedOverloadResolutionForMoveAssignment = true;
1026 data().NeedOverloadResolutionForDestructor = true;
1027 }
Brian Kelley036603a2017-03-29 17:31:42 +00001028 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1029 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +00001030 }
Eli Friedmana5433322013-07-20 01:06:31 +00001031 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +00001032 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001033
Chandler Carruthb1963742011-04-30 09:17:45 +00001034 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +00001035 if (!Field->hasInClassInitializer())
1036 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +00001037
Chandler Carruthb1963742011-04-30 09:17:45 +00001038 // C++0x [class]p7:
1039 // A standard-layout class is a class that:
1040 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +00001041 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001042 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +00001043
1044 // C++1z [class.copy.ctor]p10:
1045 // A defaulted copy constructor for a class X is defined as deleted if X has:
1046 // -- a non-static data member of rvalue reference type
1047 if (T->isRValueReferenceType())
1048 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +00001049 }
1050
Nico Weber6a6376b2016-02-19 01:52:46 +00001051 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +00001052 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +00001053 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +00001054 data().HasUninitializedFields = true;
1055 } else {
1056 data().HasUninitializedFields = true;
1057 }
1058 }
1059
Richard Smith3607ffe2012-02-13 03:54:03 +00001060 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001061 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001062 data().HasNonLiteralTypeFieldsOrBases = true;
1063
Richard Smithab44d5b2013-12-10 08:25:00 +00001064 if (Field->hasInClassInitializer() ||
1065 (Field->isAnonymousStructOrUnion() &&
1066 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001067 data().HasInClassInitializer = true;
1068
1069 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001070 // A default constructor is trivial if [...] no non-static data member
1071 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001072 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001073
Richard Smithe2648ba2012-05-07 01:07:30 +00001074 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001075 // An aggregate is a [...] class with [...] no
1076 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001077 //
Richard Smith872307e2016-03-08 22:17:41 +00001078 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001079 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001080 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001081
Richard Smithe2648ba2012-05-07 01:07:30 +00001082 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001083 // A POD struct is [...] a trivial class.
1084 data().PlainOldData = false;
1085 }
1086
Richard Smith6b02d462012-12-08 08:32:28 +00001087 // C++11 [class.copy]p23:
1088 // A defaulted copy/move assignment operator for a class X is defined
1089 // as deleted if X has:
1090 // -- a non-static data member of reference type
1091 if (T->isReferenceType())
1092 data().DefaultedMoveAssignmentIsDeleted = true;
1093
Richard Smith78b239e2019-06-20 20:44:45 +00001094 // Bitfields of length 0 are also zero-sized, but we already bailed out for
1095 // those because they are always unnamed.
1096 bool IsZeroSize = Field->isZeroSize(Context);
1097
Eugene Zelenko7855e772018-04-03 00:11:50 +00001098 if (const auto *RecordTy = T->getAs<RecordType>()) {
1099 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001100 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001101 addedClassSubobject(FieldRec);
1102
Richard Smithc91d12c2013-11-25 07:07:05 +00001103 // We may need to perform overload resolution to determine whether a
1104 // field can be moved if it's const or volatile qualified.
1105 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001106 // We need to care about 'const' for the copy constructor because an
1107 // implicit copy constructor might be declared with a non-const
1108 // parameter.
1109 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001110 data().NeedOverloadResolutionForMoveConstructor = true;
1111 data().NeedOverloadResolutionForMoveAssignment = true;
1112 }
1113
Richard Smith6b02d462012-12-08 08:32:28 +00001114 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1115 // A defaulted [special member] for a class X is defined as
1116 // deleted if:
1117 // -- X is a union-like class that has a variant member with a
1118 // non-trivial [corresponding special member]
1119 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001120 if (FieldRec->hasNonTrivialCopyConstructor())
1121 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001122 if (FieldRec->hasNonTrivialMoveConstructor())
1123 data().DefaultedMoveConstructorIsDeleted = true;
1124 if (FieldRec->hasNonTrivialMoveAssignment())
1125 data().DefaultedMoveAssignmentIsDeleted = true;
1126 if (FieldRec->hasNonTrivialDestructor())
1127 data().DefaultedDestructorIsDeleted = true;
1128 }
1129
Richard Smithdd5619f2016-08-16 00:13:47 +00001130 // For an anonymous union member, our overload resolution will perform
1131 // overload resolution for its members.
1132 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001133 data().NeedOverloadResolutionForCopyConstructor |=
1134 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001135 data().NeedOverloadResolutionForMoveConstructor |=
1136 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1137 data().NeedOverloadResolutionForMoveAssignment |=
1138 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1139 data().NeedOverloadResolutionForDestructor |=
1140 FieldRec->data().NeedOverloadResolutionForDestructor;
1141 }
1142
Alexis Huntf479f1b2011-05-09 18:22:59 +00001143 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001144 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001145 // -- for all the non-static data members of its class that are of
1146 // class type (or array thereof), each such class has a trivial
1147 // default constructor.
1148 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001149 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001150
1151 // C++0x [class.copy]p13:
1152 // A copy/move constructor for class X is trivial if [...]
1153 // [...]
1154 // -- for each non-static data member of X that is of class type (or
1155 // an array thereof), the constructor selected to copy/move that
1156 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001157 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001158 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001159
1160 if (!FieldRec->hasTrivialCopyConstructorForCall())
1161 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1162
Richard Smith6b02d462012-12-08 08:32:28 +00001163 // If the field doesn't have a simple move constructor, we'll eagerly
1164 // declare the move constructor for this class and we'll decide whether
1165 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001166 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001167 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001168
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001169 if (!FieldRec->hasTrivialMoveConstructorForCall())
1170 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1171
Chandler Carruthad7d4042011-04-23 23:10:33 +00001172 // C++0x [class.copy]p27:
1173 // A copy/move assignment operator for class X is trivial if [...]
1174 // [...]
1175 // -- for each non-static data member of X that is of class type (or
1176 // an array thereof), the assignment operator selected to
1177 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001178 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001179 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001180 // If the field doesn't have a simple move assignment, we'll eagerly
1181 // declare the move assignment for this class and we'll decide whether
1182 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001183 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001184 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001185
Douglas Gregor11c024b2010-09-28 20:50:54 +00001186 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001187 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001188 if (!FieldRec->hasTrivialDestructorForCall())
1189 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001190 if (!FieldRec->hasIrrelevantDestructor())
1191 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001192 if (FieldRec->hasObjectMember())
1193 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001194 if (FieldRec->hasVolatileMember())
1195 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001196 if (FieldRec->getArgPassingRestrictions() ==
1197 RecordDecl::APK_CanNeverPassInRegs)
1198 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001199
1200 // C++0x [class]p7:
1201 // A standard-layout class is a class that:
1202 // -- has no non-static data members of type non-standard-layout
1203 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001204 if (!FieldRec->isStandardLayout())
1205 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001206 if (!FieldRec->isCXX11StandardLayout())
1207 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001208
Richard Smithb6070db2018-04-05 18:55:37 +00001209 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001210 // A standard-layout class is a class that:
1211 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001212 // -- has no element of the set M(S) of types as a base class.
Richard Smith78b239e2019-06-20 20:44:45 +00001213 if (data().IsStandardLayout &&
1214 (isUnion() || IsFirstField || IsZeroSize) &&
Richard Smithb6070db2018-04-05 18:55:37 +00001215 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1216 data().IsStandardLayout = false;
1217
1218 // C++11 [class]p7:
1219 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001220 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001221 // data member
1222 if (data().IsCXX11StandardLayout && IsFirstField) {
1223 // FIXME: We should check all base classes here, not just direct
1224 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001225 for (const auto &BI : bases()) {
1226 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001227 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001228 break;
1229 }
1230 }
1231 }
Richard Smithb6070db2018-04-05 18:55:37 +00001232
Douglas Gregor61226d32011-05-13 01:05:07 +00001233 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001234 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001235 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001236 data().NeedOverloadResolutionForCopyConstructor = true;
1237 }
Richard Smithcc36f692011-12-22 02:22:31 +00001238
1239 // C++11 [class.copy]p13:
1240 // If the implicitly-defined constructor would satisfy the
1241 // requirements of a constexpr constructor, the implicitly-defined
1242 // constructor is constexpr.
1243 // C++11 [dcl.constexpr]p4:
1244 // -- every constructor involved in initializing non-static data
1245 // members [...] shall be a constexpr constructor
1246 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001247 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001248 // The standard requires any in-class initializer to be a constant
1249 // expression. We consider this to be a defect.
1250 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001251
1252 // C++11 [class.copy]p8:
1253 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001254 // the form 'X::X(const X&)' if each potentially constructed subobject
1255 // of a class type M (or array thereof) has a copy constructor whose
1256 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001257 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001258 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001259
1260 // C++11 [class.copy]p18:
1261 // The implicitly-declared copy assignment oeprator for a class X will
1262 // have the form 'X& X::operator=(const X&)' if [...] for all the
1263 // non-static data members of X that are of a class type M (or array
1264 // thereof), each such class type has a copy assignment operator whose
1265 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1266 if (!FieldRec->hasCopyAssignmentWithConstParam())
1267 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001268
1269 if (FieldRec->hasUninitializedReferenceMember() &&
1270 !Field->hasInClassInitializer())
1271 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001272
1273 // C++11 [class.union]p8, DR1460:
1274 // a non-static data member of an anonymous union that is a member of
1275 // X is also a variant member of X.
1276 if (FieldRec->hasVariantMembers() &&
1277 Field->isAnonymousStructOrUnion())
1278 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001279 }
Richard Smithcc36f692011-12-22 02:22:31 +00001280 } else {
1281 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001282 if (!T->isLiteralType(Context) ||
Richard Smithc667cdc2019-09-18 17:37:44 +00001283 (!Field->hasInClassInitializer() && !isUnion() &&
1284 !Context.getLangOpts().CPlusPlus2a))
Richard Smithcc36f692011-12-22 02:22:31 +00001285 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001286
1287 // C++11 [class.copy]p23:
1288 // A defaulted copy/move assignment operator for a class X is defined
1289 // as deleted if X has:
1290 // -- a non-static data member of const non-class type (or array
1291 // thereof)
1292 if (T.isConstQualified())
1293 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001294 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001295
David Majnemer5cfda6f2016-05-22 05:34:26 +00001296 // C++14 [meta.unary.prop]p4:
Richard Smith78b239e2019-06-20 20:44:45 +00001297 // T is a class type [...] with [...] no non-static data members other
1298 // than subobjects of zero size
1299 if (data().Empty && !IsZeroSize)
1300 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001301 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001302
Douglas Gregor457104e2010-09-29 04:25:11 +00001303 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001304 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001305 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001306 == DeclarationName::CXXConversionFunctionName) {
1307 ASTContext &Ctx = getASTContext();
1308 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1309 }
1310 }
Richard Smith12e79312016-05-13 06:47:56 +00001311
Eugene Zelenko7855e772018-04-03 00:11:50 +00001312 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001313 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001314 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001315 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001316 // C++1z [dcl.init.aggr]p1:
1317 // An aggregate is [...] a class [...] with no inherited constructors
1318 data().Aggregate = false;
1319 }
Richard Smith12e79312016-05-13 06:47:56 +00001320
1321 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1322 data().HasInheritedAssignment = true;
1323 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001324}
1325
Richard Smith92f241f2012-12-08 02:53:02 +00001326void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1327 assert(!D->isImplicit() && !D->isUserProvided());
1328
1329 // The kind of special member this declaration is, if any.
1330 unsigned SMKind = 0;
1331
Eugene Zelenko7855e772018-04-03 00:11:50 +00001332 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001333 if (Constructor->isDefaultConstructor()) {
1334 SMKind |= SMF_DefaultConstructor;
1335 if (Constructor->isConstexpr())
1336 data().HasConstexprDefaultConstructor = true;
1337 }
1338 if (Constructor->isCopyConstructor())
1339 SMKind |= SMF_CopyConstructor;
1340 else if (Constructor->isMoveConstructor())
1341 SMKind |= SMF_MoveConstructor;
1342 else if (Constructor->isConstexpr())
1343 // We may now know that the constructor is constexpr.
1344 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001345 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001346 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001347 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1348 data().HasIrrelevantDestructor = false;
1349 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001350 SMKind |= SMF_CopyAssignment;
1351 else if (D->isMoveAssignmentOperator())
1352 SMKind |= SMF_MoveAssignment;
1353
1354 // Update which trivial / non-trivial special members we have.
1355 // addedMember will have skipped this step for this member.
1356 if (D->isTrivial())
1357 data().HasTrivialSpecialMembers |= SMKind;
1358 else
1359 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1360}
1361
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001362void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1363 unsigned SMKind = 0;
1364
Eugene Zelenko7855e772018-04-03 00:11:50 +00001365 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001366 if (Constructor->isCopyConstructor())
1367 SMKind = SMF_CopyConstructor;
1368 else if (Constructor->isMoveConstructor())
1369 SMKind = SMF_MoveConstructor;
1370 } else if (isa<CXXDestructorDecl>(D))
1371 SMKind = SMF_Destructor;
1372
1373 if (D->isTrivialForCall())
1374 data().HasTrivialSpecialMembersForCall |= SMKind;
1375 else
1376 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1377}
1378
Joao Matose9a3ed42012-08-31 22:18:20 +00001379bool CXXRecordDecl::isCLike() const {
1380 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1381 !TemplateOrInstantiation.isNull())
1382 return false;
1383 if (!hasDefinition())
1384 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001385
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001386 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001387}
Fangrui Song6907ce22018-07-30 19:24:48 +00001388
1389bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001390 if (!isLambda()) return false;
1391 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001392}
1393
Richard Smith24253382018-08-02 20:30:52 +00001394#ifndef NDEBUG
1395static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1396 for (auto *D : R)
1397 if (!declaresSameEntity(D, R.front()))
1398 return false;
1399 return true;
1400}
1401#endif
1402
Erich Keane98f08992019-09-30 21:24:04 +00001403static NamedDecl* getLambdaCallOperatorHelper(const CXXRecordDecl &RD) {
Erich Keane5c2c60d2019-09-30 19:12:29 +00001404 if (!RD.isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001405 DeclarationName Name =
Erich Keane5c2c60d2019-09-30 19:12:29 +00001406 RD.getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1407 DeclContext::lookup_result Calls = RD.lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001408
1409 assert(!Calls.empty() && "Missing lambda call operator!");
Richard Smith24253382018-08-02 20:30:52 +00001410 assert(allLookupResultsAreTheSame(Calls) &&
1411 "More than one lambda call operator!");
Erich Keane5c2c60d2019-09-30 19:12:29 +00001412 return Calls.front();
1413}
Fangrui Song6907ce22018-07-30 19:24:48 +00001414
Erich Keane5c2c60d2019-09-30 19:12:29 +00001415FunctionTemplateDecl* CXXRecordDecl::getDependentLambdaCallOperator() const {
1416 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001417 return dyn_cast_or_null<FunctionTemplateDecl>(CallOp);
Erich Keane5c2c60d2019-09-30 19:12:29 +00001418}
1419
1420CXXMethodDecl *CXXRecordDecl::getLambdaCallOperator() const {
1421 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001422
1423 if (CallOp == nullptr)
1424 return nullptr;
1425
Eugene Zelenko7855e772018-04-03 00:11:50 +00001426 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001427 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001428
Faisal Vali2b391ab2013-09-26 19:54:12 +00001429 return cast<CXXMethodDecl>(CallOp);
1430}
1431
1432CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001433 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001434 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001435 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001436 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001437 if (Invoker.empty()) return nullptr;
Richard Smith24253382018-08-02 20:30:52 +00001438 assert(allLookupResultsAreTheSame(Invoker) &&
1439 "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001440 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001441 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001442 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001443
1444 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001445}
1446
Douglas Gregor9c702202012-02-10 07:45:31 +00001447void CXXRecordDecl::getCaptureFields(
1448 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001449 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001450 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001451 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001452
Douglas Gregorc8a73492012-02-13 15:44:47 +00001453 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001454 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001455 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001456 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001457 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001458 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001459 else if (C->capturesVariable())
1460 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001461 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001462 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001463}
1464
Fangrui Song6907ce22018-07-30 19:24:48 +00001465TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001466CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Hamza Sood8205a812019-05-04 10:49:46 +00001467 if (!isGenericLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001468 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001469 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1470 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001471 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001472}
Douglas Gregor9c702202012-02-10 07:45:31 +00001473
Hamza Sood8205a812019-05-04 10:49:46 +00001474ArrayRef<NamedDecl *>
1475CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1476 TemplateParameterList *List = getGenericLambdaTemplateParameterList();
1477 if (!List)
1478 return {};
1479
1480 assert(std::is_partitioned(List->begin(), List->end(),
1481 [](const NamedDecl *D) { return !D->isImplicit(); })
1482 && "Explicit template params should be ordered before implicit ones");
1483
Fangrui Song7264a472019-07-03 08:13:17 +00001484 const auto ExplicitEnd = llvm::partition_point(
1485 *List, [](const NamedDecl *D) { return !D->isImplicit(); });
Hamza Sood8205a812019-05-04 10:49:46 +00001486 return llvm::makeArrayRef(List->begin(), ExplicitEnd);
1487}
1488
Richard Smith0bae6242016-08-25 00:34:00 +00001489Decl *CXXRecordDecl::getLambdaContextDecl() const {
1490 assert(isLambda() && "Not a lambda closure type!");
1491 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1492 return getLambdaData().ContextDecl.get(Source);
1493}
1494
John McCall1e3a1a72010-03-15 09:07:48 +00001495static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001496 QualType T =
1497 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1498 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001499 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001500}
1501
John McCall1e3a1a72010-03-15 09:07:48 +00001502/// Collect the visible conversions of a base class.
1503///
James Dennettb5d5f812012-06-15 22:28:09 +00001504/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001505/// \param InVirtual whether this base class is a virtual base (or a base
1506/// of a virtual base)
1507/// \param Access the access along the inheritance path to this base
1508/// \param ParentHiddenTypes the conversions provided by the inheritors
1509/// of this base
1510/// \param Output the set to which to add conversions from non-virtual bases
1511/// \param VOutput the set to which to add conversions from virtual bases
1512/// \param HiddenVBaseCs the set of conversions which were hidden in a
1513/// virtual base along some inheritance path
1514static void CollectVisibleConversions(ASTContext &Context,
1515 CXXRecordDecl *Record,
1516 bool InVirtual,
1517 AccessSpecifier Access,
1518 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001519 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001520 UnresolvedSetImpl &VOutput,
1521 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1522 // The set of types which have conversions in this class or its
1523 // subclasses. As an optimization, we don't copy the derived set
1524 // unless it might change.
1525 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1526 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1527
1528 // Collect the direct conversions and figure out which conversions
1529 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001530 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1531 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1532 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001533 HiddenTypesBuffer = ParentHiddenTypes;
1534 HiddenTypes = &HiddenTypesBuffer;
1535
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001536 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001537 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1538 bool Hidden = ParentHiddenTypes.count(ConvType);
1539 if (!Hidden)
1540 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001541
1542 // If this conversion is hidden and we're in a virtual base,
1543 // remember that it's hidden along some inheritance path.
1544 if (Hidden && InVirtual)
1545 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1546
1547 // If this conversion isn't hidden, add it to the appropriate output.
1548 else if (!Hidden) {
1549 AccessSpecifier IAccess
1550 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1551
1552 if (InVirtual)
1553 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001554 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001555 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001556 }
1557 }
1558 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001559
John McCall1e3a1a72010-03-15 09:07:48 +00001560 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001561 for (const auto &I : Record->bases()) {
1562 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001563 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001564
John McCall1e3a1a72010-03-15 09:07:48 +00001565 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001566 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1567 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001568
Eugene Zelenko7855e772018-04-03 00:11:50 +00001569 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001570 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1571 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001572 }
John McCall1e3a1a72010-03-15 09:07:48 +00001573}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001574
John McCall1e3a1a72010-03-15 09:07:48 +00001575/// Collect the visible conversions of a class.
1576///
1577/// This would be extremely straightforward if it weren't for virtual
1578/// bases. It might be worth special-casing that, really.
1579static void CollectVisibleConversions(ASTContext &Context,
1580 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001581 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001582 // The collection of all conversions in virtual bases that we've
1583 // found. These will be added to the output as long as they don't
1584 // appear in the hidden-conversions set.
1585 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001586
John McCall1e3a1a72010-03-15 09:07:48 +00001587 // The set of conversions in virtual bases that we've determined to
1588 // be hidden.
1589 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1590
1591 // The set of types hidden by classes derived from this one.
1592 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1593
1594 // Go ahead and collect the direct conversions and add them to the
1595 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001596 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1597 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001598 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001599 for (; ConvI != ConvE; ++ConvI)
1600 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001601
1602 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001603 for (const auto &I : Record->bases()) {
1604 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001605 if (!RT) continue;
1606
1607 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001608 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001609 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1610 }
1611
1612 // Add any unhidden conversions provided by virtual bases.
1613 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1614 I != E; ++I) {
1615 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001616 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001617 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001618}
1619
1620/// getVisibleConversionFunctions - get all conversion functions visible
1621/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001622llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001623CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001624 ASTContext &Ctx = getASTContext();
1625
1626 ASTUnresolvedSet *Set;
1627 if (bases_begin() == bases_end()) {
1628 // If root class, all conversions are visible.
1629 Set = &data().Conversions.get(Ctx);
1630 } else {
1631 Set = &data().VisibleConversions.get(Ctx);
1632 // If visible conversion list is not evaluated, evaluate it.
1633 if (!data().ComputedVisibleConversions) {
1634 CollectVisibleConversions(Ctx, this, *Set);
1635 data().ComputedVisibleConversions = true;
1636 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001637 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001638 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001639}
1640
John McCallda4458e2010-03-31 01:36:47 +00001641void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1642 // This operation is O(N) but extremely rare. Sema only uses it to
1643 // remove UsingShadowDecls in a class that were followed by a direct
1644 // declaration, e.g.:
1645 // class A : B {
1646 // using B::operator int;
1647 // operator int();
1648 // };
1649 // This is uncommon by itself and even more uncommon in conjunction
1650 // with sufficiently large numbers of directly-declared conversions
1651 // that asymptotic behavior matters.
1652
Richard Smitha4ba74c2013-08-30 04:46:40 +00001653 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001654 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1655 if (Convs[I].getDecl() == ConvDecl) {
1656 Convs.erase(I);
Fangrui Song75e74e02019-03-31 08:48:19 +00001657 assert(llvm::find(Convs, ConvDecl) == Convs.end() &&
1658 "conversion was found multiple times in unresolved set");
John McCallda4458e2010-03-31 01:36:47 +00001659 return;
1660 }
1661 }
1662
1663 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001664}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001665
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001666CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001667 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001668 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001669
1670 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001671}
1672
Chandler Carruth21c90602015-12-30 03:24:14 +00001673MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1674 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1675}
1676
Fangrui Song6907ce22018-07-30 19:24:48 +00001677void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001678CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1679 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001680 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001681 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001682 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001683 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001684 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1685}
1686
Chandler Carruth21c90602015-12-30 03:24:14 +00001687ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1688 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1689}
1690
1691void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1692 TemplateOrInstantiation = Template;
1693}
1694
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001695TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001696 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001697 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001698
Douglas Gregor06db9f52009-10-12 20:18:28 +00001699 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001700 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001701
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001702 return TSK_Undeclared;
1703}
1704
Fangrui Song6907ce22018-07-30 19:24:48 +00001705void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001706CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001707 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001708 Spec->setSpecializationKind(TSK);
1709 return;
1710 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001711
Douglas Gregor06db9f52009-10-12 20:18:28 +00001712 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001713 MSInfo->setTemplateSpecializationKind(TSK);
1714 return;
1715 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001716
David Blaikie83d382b2011-09-23 05:06:16 +00001717 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001718}
1719
Reid Klecknere7367d62014-10-14 20:28:40 +00001720const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001721 auto GetDefinitionOrSelf =
1722 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1723 if (auto *Def = D->getDefinition())
1724 return Def;
1725 return D;
1726 };
1727
Reid Klecknere7367d62014-10-14 20:28:40 +00001728 // If it's a class template specialization, find the template or partial
1729 // specialization from which it was instantiated.
1730 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1731 auto From = TD->getInstantiatedFrom();
1732 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1733 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1734 if (NewCTD->isMemberSpecialization())
1735 break;
1736 CTD = NewCTD;
1737 }
Richard Smith2195ec92017-04-21 01:15:13 +00001738 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001739 }
1740 if (auto *CTPSD =
1741 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1742 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1743 if (NewCTPSD->isMemberSpecialization())
1744 break;
1745 CTPSD = NewCTPSD;
1746 }
Richard Smith2195ec92017-04-21 01:15:13 +00001747 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001748 }
1749 }
1750
1751 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1752 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1753 const CXXRecordDecl *RD = this;
1754 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1755 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001756 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001757 }
1758 }
1759
1760 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1761 "couldn't find pattern for class template instantiation");
1762 return nullptr;
1763}
1764
Douglas Gregorbac74902010-07-01 14:13:13 +00001765CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1766 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001767 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001768
1769 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001770 = Context.DeclarationNames.getCXXDestructorName(
1771 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001772
Richard Smithcf4bdde2015-02-21 02:45:19 +00001773 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001774
Hubert Tong3cede512017-06-30 22:43:54 +00001775 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001776}
1777
Richard Trieu95a192a2015-05-28 00:14:02 +00001778bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1779 // Destructor is noreturn.
1780 if (const CXXDestructorDecl *Destructor = getDestructor())
1781 if (Destructor->isNoReturn())
1782 return true;
1783
1784 // Check base classes destructor for noreturn.
1785 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001786 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1787 if (RD->isAnyDestructorNoReturn())
1788 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001789
1790 // Check fields for noreturn.
1791 for (const auto *Field : fields())
1792 if (const CXXRecordDecl *RD =
1793 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1794 if (RD->isAnyDestructorNoReturn())
1795 return true;
1796
1797 // All destructors are not noreturn.
1798 return false;
1799}
1800
Erich Keane3e08f662017-09-29 21:06:00 +00001801static bool isDeclContextInNamespace(const DeclContext *DC) {
1802 while (!DC->isTranslationUnit()) {
1803 if (DC->isNamespace())
1804 return true;
1805 DC = DC->getParent();
1806 }
1807 return false;
1808}
1809
Erich Keane58bd6032017-09-15 16:03:35 +00001810bool CXXRecordDecl::isInterfaceLike() const {
1811 assert(hasDefinition() && "checking for interface-like without a definition");
1812 // All __interfaces are inheritently interface-like.
1813 if (isInterface())
1814 return true;
1815
1816 // Interface-like types cannot have a user declared constructor, destructor,
1817 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1818 // cannot be interface types.
1819 if (isLambda() || hasUserDeclaredConstructor() ||
1820 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1821 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1822 return false;
1823
1824 // No interface-like type can have a method with a definition.
1825 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001826 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001827 return false;
1828
1829 // Check "Special" types.
1830 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001831 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1832 // extern C++ block directly in the TU. These are only valid if in one
1833 // of these two situations.
1834 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1835 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001836 ((getName() == "IUnknown" &&
1837 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1838 (getName() == "IDispatch" &&
1839 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1840 if (getNumBases() > 0)
1841 return false;
1842 return true;
1843 }
1844
1845 // FIXME: Any access specifiers is supposed to make this no longer interface
1846 // like.
1847
1848 // If this isn't a 'special' type, it must have a single interface-like base.
1849 if (getNumBases() != 1)
1850 return false;
1851
1852 const auto BaseSpec = *bases_begin();
1853 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1854 return false;
1855 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1856 if (Base->isInterface() || !Base->isInterfaceLike())
1857 return false;
1858 return true;
1859}
1860
Douglas Gregorb11aad82011-02-19 18:51:44 +00001861void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001862 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001863}
1864
1865void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1866 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001867
Douglas Gregor8fb95122010-09-29 00:15:42 +00001868 // If the class may be abstract (but hasn't been marked as such), check for
1869 // any pure final overriders.
1870 if (mayBeAbstract()) {
1871 CXXFinalOverriderMap MyFinalOverriders;
1872 if (!FinalOverriders) {
1873 getFinalOverriders(MyFinalOverriders);
1874 FinalOverriders = &MyFinalOverriders;
1875 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001876
Douglas Gregor8fb95122010-09-29 00:15:42 +00001877 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001878 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001879 MEnd = FinalOverriders->end();
1880 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001881 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001882 SOEnd = M->second.end();
1883 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001884 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001885 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001886
Douglas Gregor8fb95122010-09-29 00:15:42 +00001887 // C++ [class.abstract]p4:
1888 // A class is abstract if it contains or inherits at least one
1889 // pure virtual function for which the final overrider is pure
1890 // virtual.
1891 if (SO->second.front().Method->isPure()) {
1892 data().Abstract = true;
1893 Done = true;
1894 break;
1895 }
1896 }
1897 }
1898 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001899
Douglas Gregor457104e2010-09-29 04:25:11 +00001900 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001901 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001902 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001903 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001904}
1905
1906bool CXXRecordDecl::mayBeAbstract() const {
1907 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1908 isDependentContext())
1909 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001910
Aaron Ballman574705e2014-03-13 15:41:46 +00001911 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001912 const auto *BaseDecl =
Simon Pilgrim1cd399c2019-10-03 11:22:48 +00001913 cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001914 if (BaseDecl->isAbstract())
1915 return true;
1916 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001917
Douglas Gregor8fb95122010-09-29 00:15:42 +00001918 return false;
1919}
1920
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001921void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001922
Richard Smith76b90272019-05-09 03:59:21 +00001923bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other) const {
1924 if ((getKind() != Other.getKind() ||
1925 getKind() == ExplicitSpecKind::Unresolved)) {
1926 if (getKind() == ExplicitSpecKind::Unresolved &&
1927 Other.getKind() == ExplicitSpecKind::Unresolved) {
1928 ODRHash SelfHash, OtherHash;
1929 SelfHash.AddStmt(getExpr());
1930 OtherHash.AddStmt(Other.getExpr());
1931 return SelfHash.CalculateHash() == OtherHash.CalculateHash();
1932 } else
1933 return false;
1934 }
1935 return true;
1936}
1937
1938ExplicitSpecifier ExplicitSpecifier::getFromDecl(FunctionDecl *Function) {
1939 switch (Function->getDeclKind()) {
1940 case Decl::Kind::CXXConstructor:
1941 return cast<CXXConstructorDecl>(Function)->getExplicitSpecifier();
1942 case Decl::Kind::CXXConversion:
1943 return cast<CXXConversionDecl>(Function)->getExplicitSpecifier();
1944 case Decl::Kind::CXXDeductionGuide:
1945 return cast<CXXDeductionGuideDecl>(Function)->getExplicitSpecifier();
1946 default:
1947 return {};
1948 }
1949}
1950
Richard Smithbc491202017-02-17 20:05:37 +00001951CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
Richard Smith76b90272019-05-09 03:59:21 +00001952 ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1953 ExplicitSpecifier ES, const DeclarationNameInfo &NameInfo, QualType T,
1954 TypeSourceInfo *TInfo, SourceLocation EndLocation) {
1955 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, ES, NameInfo, T,
1956 TInfo, EndLocation);
Richard Smithbc491202017-02-17 20:05:37 +00001957}
1958
1959CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1960 unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00001961 return new (C, ID) CXXDeductionGuideDecl(
1962 C, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
1963 QualType(), nullptr, SourceLocation());
Richard Smithbc491202017-02-17 20:05:37 +00001964}
1965
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001966void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001967
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001968bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001969 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001970
1971 if (MD->getStorageClass() == SC_Static)
1972 return true;
1973
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001974 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1975 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001976}
1977
Rafael Espindola49e860b2012-06-26 17:45:31 +00001978static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1979 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001980 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001981 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1982 return true;
1983 if (recursivelyOverrides(MD, BaseMD))
1984 return true;
1985 }
1986 return false;
1987}
1988
1989CXXMethodDecl *
Richard Smith921f1322019-05-13 23:35:21 +00001990CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
1991 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001992 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1993 return this;
1994
1995 // Lookup doesn't work for destructors, so handle them separately.
1996 if (isa<CXXDestructorDecl>(this)) {
1997 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001998 if (MD) {
1999 if (recursivelyOverrides(MD, this))
2000 return MD;
2001 if (MayBeBase && recursivelyOverrides(this, MD))
2002 return MD;
2003 }
Craig Topper36250ad2014-05-12 05:36:57 +00002004 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002005 }
2006
Richard Smith40c78062015-02-21 02:31:57 +00002007 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002008 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00002009 if (!MD)
2010 continue;
2011 if (recursivelyOverrides(MD, this))
2012 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00002013 if (MayBeBase && recursivelyOverrides(this, MD))
2014 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002015 }
2016
Richard Smith921f1322019-05-13 23:35:21 +00002017 return nullptr;
2018}
2019
2020CXXMethodDecl *
2021CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2022 bool MayBeBase) {
2023 if (auto *MD = getCorrespondingMethodDeclaredInClass(RD, MayBeBase))
2024 return MD;
2025
Aaron Ballman574705e2014-03-13 15:41:46 +00002026 for (const auto &I : RD->bases()) {
2027 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00002028 if (!RT)
2029 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002030 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00002031 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
2032 if (T)
2033 return T;
2034 }
2035
Craig Topper36250ad2014-05-12 05:36:57 +00002036 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002037}
2038
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002039CXXMethodDecl *CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
2040 SourceLocation StartLoc,
2041 const DeclarationNameInfo &NameInfo,
2042 QualType T, TypeSourceInfo *TInfo,
2043 StorageClass SC, bool isInline,
2044 ConstexprSpecKind ConstexprKind,
2045 SourceLocation EndLocation) {
2046 return new (C, RD)
2047 CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo, T, TInfo, SC,
2048 isInline, ConstexprKind, EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00002049}
2050
Douglas Gregor72172e92012-01-05 21:55:30 +00002051CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002052 return new (C, ID) CXXMethodDecl(
2053 CXXMethod, C, nullptr, SourceLocation(), DeclarationNameInfo(),
2054 QualType(), nullptr, SC_None, false, CSK_unspecified, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002055}
2056
Akira Hatanaka22461672017-07-13 06:08:27 +00002057CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
2058 bool IsAppleKext) {
2059 assert(isVirtual() && "this method is expected to be virtual");
2060
2061 // When building with -fapple-kext, all calls must go through the vtable since
2062 // the kernel linker can do runtime patching of vtables.
2063 if (IsAppleKext)
2064 return nullptr;
2065
2066 // If the member function is marked 'final', we know that it can't be
2067 // overridden and can therefore devirtualize it unless it's pure virtual.
2068 if (hasAttr<FinalAttr>())
2069 return isPure() ? nullptr : this;
2070
2071 // If Base is unknown, we cannot devirtualize.
2072 if (!Base)
2073 return nullptr;
2074
2075 // If the base expression (after skipping derived-to-base conversions) is a
2076 // class prvalue, then we can devirtualize.
2077 Base = Base->getBestDynamicClassTypeExpr();
2078 if (Base->isRValue() && Base->getType()->isRecordType())
2079 return this;
2080
2081 // If we don't even know what we would call, we can't devirtualize.
2082 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
2083 if (!BestDynamicDecl)
2084 return nullptr;
2085
2086 // There may be a method corresponding to MD in a derived class.
2087 CXXMethodDecl *DevirtualizedMethod =
2088 getCorrespondingMethodInClass(BestDynamicDecl);
2089
2090 // If that method is pure virtual, we can't devirtualize. If this code is
2091 // reached, the result would be UB, not a direct call to the derived class
2092 // function, and we can't assume the derived class function is defined.
2093 if (DevirtualizedMethod->isPure())
2094 return nullptr;
2095
2096 // If that method is marked final, we can devirtualize it.
2097 if (DevirtualizedMethod->hasAttr<FinalAttr>())
2098 return DevirtualizedMethod;
2099
David Bolvansky20b27082019-08-31 18:52:44 +00002100 // Similarly, if the class itself or its destructor is marked 'final',
2101 // the class can't be derived from and we can therefore devirtualize the
2102 // member function call.
Akira Hatanaka22461672017-07-13 06:08:27 +00002103 if (BestDynamicDecl->hasAttr<FinalAttr>())
2104 return DevirtualizedMethod;
David Bolvansky20b27082019-08-31 18:52:44 +00002105 if (const auto *dtor = BestDynamicDecl->getDestructor()) {
2106 if (dtor->hasAttr<FinalAttr>())
2107 return DevirtualizedMethod;
2108 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002109
Eugene Zelenko7855e772018-04-03 00:11:50 +00002110 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2111 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00002112 if (VD->getType()->isRecordType())
2113 // This is a record decl. We know the type and can devirtualize it.
2114 return DevirtualizedMethod;
2115
2116 return nullptr;
2117 }
2118
2119 // We can devirtualize calls on an object accessed by a class member access
2120 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2121 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002122 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002123 const ValueDecl *VD = ME->getMemberDecl();
2124 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2125 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002126
2127 // Likewise for calls on an object accessed by a (non-reference) pointer to
2128 // member access.
2129 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2130 if (BO->isPtrMemOp()) {
2131 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2132 if (MPT->getPointeeType()->isRecordType())
2133 return DevirtualizedMethod;
2134 }
2135 }
2136
2137 // We can't devirtualize the call.
2138 return nullptr;
2139}
2140
Artem Belevich78929ef2018-09-21 17:29:33 +00002141bool CXXMethodDecl::isUsualDeallocationFunction(
2142 SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2143 assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002144 if (getOverloadedOperator() != OO_Delete &&
2145 getOverloadedOperator() != OO_Array_Delete)
2146 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002147
2148 // C++ [basic.stc.dynamic.deallocation]p2:
2149 // A template instance is never a usual deallocation function,
2150 // regardless of its signature.
2151 if (getPrimaryTemplate())
2152 return false;
2153
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002154 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002155 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002156 // with exactly one parameter, then that function is a usual (non-placement)
2157 // deallocation function. [...]
2158 if (getNumParams() == 1)
2159 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002160 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002161
Richard Smith5b349582017-10-13 01:55:36 +00002162 // C++ P0722:
2163 // A destroying operator delete is a usual deallocation function if
2164 // removing the std::destroying_delete_t parameter and changing the
2165 // first parameter type from T* to void* results in the signature of
2166 // a usual deallocation function.
2167 if (isDestroyingOperatorDelete())
2168 ++UsualParams;
2169
Richard Smithb2f0f052016-10-10 18:54:32 +00002170 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002171 // [...] If class T does not declare such an operator delete but does
2172 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002173 // exactly two parameters, the second of which has type std::size_t (18.1),
2174 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002175 //
2176 // C++17 says a usual deallocation function is one with the signature
2177 // (void* [, size_t] [, std::align_val_t] [, ...])
2178 // and all such functions are usual deallocation functions. It's not clear
2179 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002180 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002181 if (UsualParams < getNumParams() &&
2182 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2183 Context.getSizeType()))
2184 ++UsualParams;
2185
2186 if (UsualParams < getNumParams() &&
2187 getParamDecl(UsualParams)->getType()->isAlignValT())
2188 ++UsualParams;
2189
2190 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002191 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002192
2193 // In C++17 onwards, all potential usual deallocation functions are actual
Eric Fiselier24a2a482019-02-03 03:44:31 +00002194 // usual deallocation functions. Honor this behavior when post-C++14
2195 // deallocation functions are offered as extensions too.
2196 // FIXME(EricWF): Destrying Delete should be a language option. How do we
2197 // handle when destroying delete is used prior to C++17?
2198 if (Context.getLangOpts().CPlusPlus17 ||
2199 Context.getLangOpts().AlignedAllocation ||
2200 isDestroyingOperatorDelete())
Richard Smithb2f0f052016-10-10 18:54:32 +00002201 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002202
2203 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002204 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002205 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
Artem Belevich78929ef2018-09-21 17:29:33 +00002206 bool Result = true;
2207 for (const auto *D : R) {
2208 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2209 if (FD->getNumParams() == 1) {
2210 PreventedBy.push_back(FD);
2211 Result = false;
2212 }
2213 }
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002214 }
Artem Belevich78929ef2018-09-21 17:29:33 +00002215 return Result;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002216}
2217
Douglas Gregorb139cd52010-05-01 20:49:11 +00002218bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002219 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002220 // A user-declared copy assignment operator X::operator= is a non-static
2221 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002222 // type X, X&, const X&, volatile X& or const volatile X&.
2223 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002224 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002225 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2226 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002227 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002228
Douglas Gregorb139cd52010-05-01 20:49:11 +00002229 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002230 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002231 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002232
Douglas Gregorb139cd52010-05-01 20:49:11 +00002233 ASTContext &Context = getASTContext();
2234 QualType ClassType
2235 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2236 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2237}
2238
Alexis Huntfcaeae42011-05-25 20:50:04 +00002239bool CXXMethodDecl::isMoveAssignmentOperator() const {
2240 // C++0x [class.copy]p19:
2241 // A user-declared move assignment operator X::operator= is a non-static
2242 // non-template member function of class X with exactly one parameter of type
2243 // X&&, const X&&, volatile X&&, or const volatile X&&.
2244 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002245 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2246 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002247 return false;
2248
2249 QualType ParamType = getParamDecl(0)->getType();
2250 if (!isa<RValueReferenceType>(ParamType))
2251 return false;
2252 ParamType = ParamType->getPointeeType();
2253
2254 ASTContext &Context = getASTContext();
2255 QualType ClassType
2256 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2257 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2258}
2259
Anders Carlsson36d87e12009-05-16 23:58:37 +00002260void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002261 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002262 assert(!MD->getParent()->isDependentContext() &&
2263 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002264 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002265
Douglas Gregor832940b2010-03-02 23:58:15 +00002266 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002267}
2268
2269CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002270 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002271 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002272}
2273
2274CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002275 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002276 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002277}
2278
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002279unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002280 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002281 return getASTContext().overridden_methods_size(this);
2282}
2283
Clement Courbet6ecaec82016-07-05 07:49:31 +00002284CXXMethodDecl::overridden_method_range
2285CXXMethodDecl::overridden_methods() const {
2286 if (isa<CXXConstructorDecl>(this))
2287 return overridden_method_range(nullptr, nullptr);
2288 return getASTContext().overridden_methods(this);
2289}
2290
Marco Antognini88559632019-07-22 09:39:13 +00002291static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2292 const CXXRecordDecl *Decl) {
2293 QualType ClassTy = C.getTypeDeclType(Decl);
2294 return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2295}
2296
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002297QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2298 const CXXRecordDecl *Decl) {
2299 ASTContext &C = Decl->getASTContext();
Marco Antognini88559632019-07-22 09:39:13 +00002300 QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2301 return C.getPointerType(ObjectTy);
2302}
2303
2304QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2305 const CXXRecordDecl *Decl) {
2306 ASTContext &C = Decl->getASTContext();
2307 return ::getThisObjectType(C, FPT, Decl);
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002308}
2309
Brian Gesiak5488ab42019-01-11 01:54:53 +00002310QualType CXXMethodDecl::getThisType() const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002311 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2312 // If the member function is declared const, the type of this is const X*,
2313 // if the member function is declared volatile, the type of this is
2314 // volatile X*, and if the member function is declared const volatile,
2315 // the type of this is const volatile X*.
Ted Kremenek21475702008-09-05 17:16:31 +00002316 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002317
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002318 return CXXMethodDecl::getThisType(getType()->getAs<FunctionProtoType>(),
2319 getParent());
Ted Kremenek21475702008-09-05 17:16:31 +00002320}
2321
Marco Antognini88559632019-07-22 09:39:13 +00002322QualType CXXMethodDecl::getThisObjectType() const {
2323 // Ditto getThisType.
2324 assert(isInstance() && "No 'this' for static methods!");
2325
2326 return CXXMethodDecl::getThisObjectType(getType()->getAs<FunctionProtoType>(),
2327 getParent());
2328}
2329
Eli Friedman71a26d82009-12-06 20:50:05 +00002330bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002331 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002332 // which it was instantiated.
2333 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2334 if (!CheckFn)
2335 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002336
Eli Friedman71a26d82009-12-06 20:50:05 +00002337 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002338 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2339 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002340}
2341
Douglas Gregor355efbb2012-02-17 03:02:34 +00002342bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002343 const CXXRecordDecl *P = getParent();
2344 if (P->isLambda()) {
2345 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2346 if (StaticInvoker == this) return true;
2347 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2348 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2349 }
2350 }
2351 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002352}
2353
Alexis Hunt1d792652011-01-08 20:30:50 +00002354CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2355 TypeSourceInfo *TInfo, bool IsVirtual,
2356 SourceLocation L, Expr *Init,
2357 SourceLocation R,
2358 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002359 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2360 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2361 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002362
Alexis Hunt1d792652011-01-08 20:30:50 +00002363CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2364 FieldDecl *Member,
2365 SourceLocation MemberLoc,
2366 SourceLocation L, Expr *Init,
2367 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002368 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2369 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2370 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002371
Alexis Hunt1d792652011-01-08 20:30:50 +00002372CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2373 IndirectFieldDecl *Member,
2374 SourceLocation MemberLoc,
2375 SourceLocation L, Expr *Init,
2376 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002377 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2378 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2379 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002380
Alexis Hunt1d792652011-01-08 20:30:50 +00002381CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002382 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002383 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002384 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002385 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2386 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002387
Artem Dergachevd3942be2018-12-03 22:15:34 +00002388int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +00002389 return Context.getAllocator()
2390 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
Artem Dergachevd3942be2018-12-03 22:15:34 +00002391}
2392
Alexis Hunt1d792652011-01-08 20:30:50 +00002393TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002394 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002395 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002396 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002397 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002398}
2399
Alexis Hunt1d792652011-01-08 20:30:50 +00002400const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002401 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002402 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002403 else
Craig Topper36250ad2014-05-12 05:36:57 +00002404 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002405}
2406
Alexis Hunt1d792652011-01-08 20:30:50 +00002407SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002408 if (isInClassMemberInitializer())
2409 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002410
David Blaikiea81d4102015-01-18 00:12:58 +00002411 if (isAnyMemberInitializer())
2412 return getMemberLocation();
2413
Eugene Zelenko7855e772018-04-03 00:11:50 +00002414 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002415 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002416
Eugene Zelenko7855e772018-04-03 00:11:50 +00002417 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002418}
2419
Alexis Hunt1d792652011-01-08 20:30:50 +00002420SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002421 if (isInClassMemberInitializer()) {
2422 FieldDecl *D = getAnyMember();
2423 if (Expr *I = D->getInClassInitializer())
2424 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002425 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002426 }
2427
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002428 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002429}
2430
Erich Keane9c665062018-08-01 21:02:40 +00002431CXXConstructorDecl::CXXConstructorDecl(
2432 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2433 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Richard Smith76b90272019-05-09 03:59:21 +00002434 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002435 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited)
Erich Keane9c665062018-08-01 21:02:40 +00002436 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002437 SC_None, isInline, ConstexprKind, SourceLocation()) {
Erich Keane9c665062018-08-01 21:02:40 +00002438 setNumCtorInitializers(0);
2439 setInheritingConstructor(static_cast<bool>(Inherited));
2440 setImplicit(isImplicitlyDeclared);
Richard Smith76b90272019-05-09 03:59:21 +00002441 CXXConstructorDeclBits.HasTrailingExplicitSpecifier = ES.getExpr() ? 1 : 0;
Erich Keane9c665062018-08-01 21:02:40 +00002442 if (Inherited)
2443 *getTrailingObjects<InheritedConstructor>() = Inherited;
Richard Smith76b90272019-05-09 03:59:21 +00002444 setExplicitSpecifier(ES);
Erich Keane9c665062018-08-01 21:02:40 +00002445}
2446
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002447void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002448
Richard Smith5179eb72016-06-28 19:03:57 +00002449CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2450 unsigned ID,
Richard Smith76b90272019-05-09 03:59:21 +00002451 uint64_t AllocKind) {
2452 bool hasTraillingExplicit = static_cast<bool>(AllocKind & TAKHasTailExplicit);
2453 bool isInheritingConstructor =
2454 static_cast<bool>(AllocKind & TAKInheritsConstructor);
2455 unsigned Extra =
2456 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2457 isInheritingConstructor, hasTraillingExplicit);
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002458 auto *Result = new (C, ID, Extra)
2459 CXXConstructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2460 QualType(), nullptr, ExplicitSpecifier(), false, false,
2461 CSK_unspecified, InheritedConstructor());
Richard Smith76b90272019-05-09 03:59:21 +00002462 Result->setInheritingConstructor(isInheritingConstructor);
2463 Result->CXXConstructorDeclBits.HasTrailingExplicitSpecifier =
2464 hasTraillingExplicit;
2465 Result->setExplicitSpecifier(ExplicitSpecifier());
Richard Smith5179eb72016-06-28 19:03:57 +00002466 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002467}
2468
Richard Smith76b90272019-05-09 03:59:21 +00002469CXXConstructorDecl *CXXConstructorDecl::Create(
2470 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2471 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2472 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002473 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002474 assert(NameInfo.getName().getNameKind()
2475 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002476 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002477 unsigned Extra =
Richard Smith76b90272019-05-09 03:59:21 +00002478 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2479 Inherited ? 1 : 0, ES.getExpr() ? 1 : 0);
2480 return new (C, RD, Extra)
2481 CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, ES, isInline,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002482 isImplicitlyDeclared, ConstexprKind, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002483}
2484
Richard Smithc2bb8182015-03-24 06:36:48 +00002485CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2486 return CtorInitializers.get(getASTContext().getExternalSource());
2487}
2488
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002489CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2490 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2491 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002492 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002493 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002494
2495 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002496}
2497
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002498bool CXXConstructorDecl::isDefaultConstructor() const {
2499 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002500 // A default constructor for a class X is a constructor of class
2501 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002502 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002503 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002504}
2505
Mike Stump11289f42009-09-09 15:08:12 +00002506bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002507CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002508 return isCopyOrMoveConstructor(TypeQuals) &&
2509 getParamDecl(0)->getType()->isLValueReferenceType();
2510}
2511
2512bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2513 return isCopyOrMoveConstructor(TypeQuals) &&
2514 getParamDecl(0)->getType()->isRValueReferenceType();
2515}
2516
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002517/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002518bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002519 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002520 // A non-template constructor for class X is a copy constructor
2521 // if its first parameter is of type X&, const X&, volatile X& or
2522 // const volatile X&, and either there are no other parameters
2523 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002524 // C++0x [class.copy]p3:
2525 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002526 // first parameter is of type X&&, const X&&, volatile X&&, or
2527 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002528 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002529 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002530 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002531 (getPrimaryTemplate() != nullptr) ||
2532 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002533 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002534
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002535 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002536
2537 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002538 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002539 if (!ParamRefType)
2540 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002541
Douglas Gregorff7028a2009-11-13 23:59:09 +00002542 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002543 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002544
Douglas Gregorff7028a2009-11-13 23:59:09 +00002545 CanQualType PointeeType
2546 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002547 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002548 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002549 if (PointeeType.getUnqualifiedType() != ClassTy)
2550 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002551
John McCall8ccfcb52009-09-24 19:53:00 +00002552 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002553
Douglas Gregorf282a762011-01-21 19:38:21 +00002554 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002555 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002556 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002557}
2558
Anders Carlssond20e7952009-08-28 16:57:08 +00002559bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002560 // C++ [class.conv.ctor]p1:
2561 // A constructor declared without the function-specifier explicit
2562 // that can be called with a single parameter specifies a
2563 // conversion from the type of its first parameter to the type of
2564 // its class. Such a constructor is called a converting
2565 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002566 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002567 return false;
2568
Mike Stump11289f42009-09-09 15:08:12 +00002569 return (getNumParams() == 0 &&
Simon Pilgrim86976c92019-10-03 16:58:01 +00002570 getType()->castAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002571 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002572 (getNumParams() > 1 &&
2573 (getParamDecl(1)->hasDefaultArg() ||
2574 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002575}
Douglas Gregor61956c42008-10-31 09:07:45 +00002576
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002577bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002578 if ((getNumParams() < 1) ||
2579 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002580 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002581 return false;
2582
2583 const ParmVarDecl *Param = getParamDecl(0);
2584
2585 ASTContext &Context = getASTContext();
2586 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002587
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002588 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002589 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002590 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2591 if (ParamType.getUnqualifiedType() != ClassTy)
2592 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002593
2594 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002595}
2596
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002597void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002598
Douglas Gregor831c93f2008-11-05 20:51:48 +00002599CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002600CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002601 return new (C, ID)
2602 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
Richard Smith457226e2019-09-23 03:48:44 +00002603 QualType(), nullptr, false, false, CSK_unspecified);
Chris Lattnerca025db2010-05-07 21:43:38 +00002604}
2605
Richard Smith457226e2019-09-23 03:48:44 +00002606CXXDestructorDecl *CXXDestructorDecl::Create(
2607 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2608 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2609 bool isInline, bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002610 assert(NameInfo.getName().getNameKind()
2611 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002612 "Name must refer to a destructor");
Richard Smith457226e2019-09-23 03:48:44 +00002613 return new (C, RD)
2614 CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline,
2615 isImplicitlyDeclared, ConstexprKind);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002616}
2617
Richard Smith5b349582017-10-13 01:55:36 +00002618void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002619 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2620 if (OD && !First->OperatorDelete) {
2621 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002622 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002623 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002624 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002625 }
2626}
2627
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002628void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002629
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002630CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002631CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00002632 return new (C, ID) CXXConversionDecl(
2633 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002634 false, ExplicitSpecifier(), CSK_unspecified, SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002635}
2636
Richard Smith76b90272019-05-09 03:59:21 +00002637CXXConversionDecl *CXXConversionDecl::Create(
2638 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2639 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002640 bool isInline, ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind,
Richard Smith76b90272019-05-09 03:59:21 +00002641 SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002642 assert(NameInfo.getName().getNameKind()
2643 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002644 "Name must refer to a conversion function");
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002645 return new (C, RD)
2646 CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline, ES,
2647 ConstexprKind, EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002648}
2649
Douglas Gregord3b672c2012-02-16 01:06:16 +00002650bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2651 return isImplicit() && getParent()->isLambda() &&
2652 getConversionType()->isBlockPointerType();
2653}
2654
Erich Keanec9d29902018-08-01 21:16:54 +00002655LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2656 SourceLocation LangLoc, LanguageIDs lang,
2657 bool HasBraces)
2658 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2659 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2660 setLanguage(lang);
2661 LinkageSpecDeclBits.HasBraces = HasBraces;
2662}
2663
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002664void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002665
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002666LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002667 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002668 SourceLocation ExternLoc,
2669 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002670 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002671 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002672 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002673}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002674
Richard Smithf7981722013-11-22 09:01:48 +00002675LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2676 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002677 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2678 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002679}
2680
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002681void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002682
Douglas Gregor889ceb72009-02-03 19:21:40 +00002683UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2684 SourceLocation L,
2685 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002686 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002687 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002688 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002689 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002690 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002691 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002692 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2693 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002694}
2695
Richard Smithf7981722013-11-22 09:01:48 +00002696UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2697 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002698 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2699 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002700 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002701 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002702}
2703
Sebastian Redla6602e92009-11-23 15:34:23 +00002704NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002705 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002706 return NA->getNamespace();
2707 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2708}
2709
Richard Smith053f6c62014-05-16 23:01:30 +00002710NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2711 SourceLocation StartLoc, SourceLocation IdLoc,
2712 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2713 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002714 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002715 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002716 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002717
Douglas Gregore57e7522012-01-07 09:11:48 +00002718 if (PrevDecl)
2719 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2720}
2721
Douglas Gregor72172e92012-01-05 21:55:30 +00002722NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002723 bool Inline, SourceLocation StartLoc,
2724 SourceLocation IdLoc, IdentifierInfo *Id,
2725 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002726 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2727 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002728}
2729
2730NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002731 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002732 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002733}
2734
Chandler Carruth21c90602015-12-30 03:24:14 +00002735NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2736 if (isFirstDecl())
2737 return this;
2738
2739 return AnonOrFirstNamespaceAndInline.getPointer();
2740}
2741
2742const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2743 if (isFirstDecl())
2744 return this;
2745
2746 return AnonOrFirstNamespaceAndInline.getPointer();
2747}
2748
2749bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2750
Richard Smithd7af8a32014-05-10 01:17:36 +00002751NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2752 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002753}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002754
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002755NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2756 return getPreviousDecl();
2757}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002758
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002759NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2760 return getMostRecentDecl();
2761}
2762
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002763void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002764
Richard Smithf4634362014-09-03 23:11:22 +00002765NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2766 return getNextRedeclaration();
2767}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002768
Richard Smithf4634362014-09-03 23:11:22 +00002769NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2770 return getPreviousDecl();
2771}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002772
Richard Smithf4634362014-09-03 23:11:22 +00002773NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2774 return getMostRecentDecl();
2775}
2776
Mike Stump11289f42009-09-09 15:08:12 +00002777NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002778 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002779 SourceLocation AliasLoc,
2780 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002781 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002782 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002783 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002784 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002785 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002786 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002787 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002788 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002789}
2790
Douglas Gregor72172e92012-01-05 21:55:30 +00002791NamespaceAliasDecl *
2792NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002793 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002794 SourceLocation(), nullptr,
2795 NestedNameSpecifierLoc(),
2796 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002797}
2798
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002799void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002800
Richard Smith5179eb72016-06-28 19:03:57 +00002801UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2802 SourceLocation Loc, UsingDecl *Using,
2803 NamedDecl *Target)
2804 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002805 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002806 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002807 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002808 setImplicit();
2809}
2810
2811UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2812 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002813 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002814
Douglas Gregor72172e92012-01-05 21:55:30 +00002815UsingShadowDecl *
2816UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002817 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002818}
2819
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002820UsingDecl *UsingShadowDecl::getUsingDecl() const {
2821 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002822 while (const auto *NextShadow =
2823 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002824 Shadow = NextShadow;
2825 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2826}
2827
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002828void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002829
2830ConstructorUsingShadowDecl *
2831ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2832 SourceLocation Loc, UsingDecl *Using,
2833 NamedDecl *Target, bool IsVirtual) {
2834 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2835 IsVirtual);
2836}
2837
2838ConstructorUsingShadowDecl *
2839ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2840 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2841}
2842
2843CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2844 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2845}
2846
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002847void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002848
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002849void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2850 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2851 "declaration already in set");
2852 assert(S->getUsingDecl() == this);
2853
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002854 if (FirstUsingShadow.getPointer())
2855 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2856 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002857}
2858
2859void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2860 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2861 "declaration not in set");
2862 assert(S->getUsingDecl() == this);
2863
2864 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2865
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002866 if (FirstUsingShadow.getPointer() == S) {
2867 FirstUsingShadow.setPointer(
2868 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002869 S->UsingOrNextShadow = this;
2870 return;
2871 }
2872
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002873 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002874 while (Prev->UsingOrNextShadow != S)
2875 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2876 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2877 S->UsingOrNextShadow = this;
2878}
2879
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002880UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2881 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002882 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002883 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002884 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002885}
2886
Douglas Gregor72172e92012-01-05 21:55:30 +00002887UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002888 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2889 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2890 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002891}
2892
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002893SourceRange UsingDecl::getSourceRange() const {
2894 SourceLocation Begin = isAccessDeclaration()
2895 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2896 return SourceRange(Begin, getNameInfo().getEndLoc());
2897}
2898
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002899void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002900
2901UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2902 NamedDecl *InstantiatedFrom,
2903 ArrayRef<NamedDecl *> UsingDecls) {
2904 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2905 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2906}
2907
2908UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2909 unsigned NumExpansions) {
2910 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2911 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2912 Result->NumExpansions = NumExpansions;
2913 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2914 for (unsigned I = 0; I != NumExpansions; ++I)
2915 new (Trail + I) NamedDecl*(nullptr);
2916 return Result;
2917}
2918
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002919void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002920
John McCalle61f2ba2009-11-18 02:36:19 +00002921UnresolvedUsingValueDecl *
2922UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2923 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002924 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002925 const DeclarationNameInfo &NameInfo,
2926 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002927 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002928 QualifierLoc, NameInfo,
2929 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002930}
2931
Douglas Gregor72172e92012-01-05 21:55:30 +00002932UnresolvedUsingValueDecl *
2933UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002934 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2935 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002936 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002937 DeclarationNameInfo(),
2938 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002939}
2940
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002941SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2942 SourceLocation Begin = isAccessDeclaration()
2943 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2944 return SourceRange(Begin, getNameInfo().getEndLoc());
2945}
2946
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002947void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002948
John McCalle61f2ba2009-11-18 02:36:19 +00002949UnresolvedUsingTypenameDecl *
2950UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2951 SourceLocation UsingLoc,
2952 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002953 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002954 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002955 DeclarationName TargetName,
2956 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002957 return new (C, DC) UnresolvedUsingTypenameDecl(
2958 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002959 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002960}
2961
Douglas Gregor72172e92012-01-05 21:55:30 +00002962UnresolvedUsingTypenameDecl *
2963UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002964 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002965 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002966 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002967}
2968
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002969void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002970
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002971StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002972 SourceLocation StaticAssertLoc,
2973 Expr *AssertExpr,
2974 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002975 SourceLocation RParenLoc,
2976 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002977 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2978 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002979}
2980
Richard Smithf7981722013-11-22 09:01:48 +00002981StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002982 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002983 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2984 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002985}
2986
Richard Smithbdb84f32016-07-22 23:36:59 +00002987void BindingDecl::anchor() {}
2988
2989BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2990 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002991 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002992}
2993
2994BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002995 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002996}
2997
Richard Smith0353e5a2019-05-25 01:04:17 +00002998ValueDecl *BindingDecl::getDecomposedDecl() const {
2999 ExternalASTSource *Source =
3000 Decomp.isOffset() ? getASTContext().getExternalSource() : nullptr;
3001 return cast_or_null<ValueDecl>(Decomp.get(Source));
3002}
3003
Richard Smith97fcf4b2016-08-14 23:15:52 +00003004VarDecl *BindingDecl::getHoldingVar() const {
3005 Expr *B = getBinding();
3006 if (!B)
3007 return nullptr;
3008 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
3009 if (!DRE)
3010 return nullptr;
3011
3012 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
3013 assert(VD->isImplicit() && "holding var for binding decl not implicit");
3014 return VD;
3015}
3016
Richard Smithbdb84f32016-07-22 23:36:59 +00003017void DecompositionDecl::anchor() {}
3018
3019DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
3020 SourceLocation StartLoc,
3021 SourceLocation LSquareLoc,
3022 QualType T, TypeSourceInfo *TInfo,
3023 StorageClass SC,
3024 ArrayRef<BindingDecl *> Bindings) {
3025 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3026 return new (C, DC, Extra)
3027 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
3028}
3029
3030DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3031 unsigned ID,
3032 unsigned NumBindings) {
3033 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00003034 auto *Result = new (C, ID, Extra)
3035 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3036 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00003037 // Set up and clean out the bindings array.
3038 Result->NumBindings = NumBindings;
3039 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
3040 for (unsigned I = 0; I != NumBindings; ++I)
3041 new (Trail + I) BindingDecl*(nullptr);
3042 return Result;
3043}
3044
Richard Smith7873de02016-08-11 22:25:46 +00003045void DecompositionDecl::printName(llvm::raw_ostream &os) const {
3046 os << '[';
3047 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00003048 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00003049 if (Comma)
3050 os << ", ";
3051 B->printName(os);
3052 Comma = true;
3053 }
3054 os << ']';
3055}
3056
Richard Trieub3e902f2018-12-29 02:02:30 +00003057void MSPropertyDecl::anchor() {}
3058
Richard Smithf7981722013-11-22 09:01:48 +00003059MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
3060 SourceLocation L, DeclarationName N,
3061 QualType T, TypeSourceInfo *TInfo,
3062 SourceLocation StartL,
3063 IdentifierInfo *Getter,
3064 IdentifierInfo *Setter) {
3065 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
3066}
3067
3068MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3069 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003070 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
3071 DeclarationName(), QualType(), nullptr,
3072 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00003073}
3074
Anders Carlsson6750d162009-03-26 23:46:50 +00003075static const char *getAccessName(AccessSpecifier AS) {
3076 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00003077 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00003078 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003079 case AS_public:
3080 return "public";
3081 case AS_private:
3082 return "private";
3083 case AS_protected:
3084 return "protected";
3085 }
David Blaikief47fa302012-01-17 02:30:50 +00003086 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003087}
3088
3089const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
3090 AccessSpecifier AS) {
3091 return DB << getAccessName(AS);
3092}
Richard Smith84f6dcf2012-02-02 01:16:57 +00003093
3094const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
3095 AccessSpecifier AS) {
3096 return DB << getAccessName(AS);
3097}