blob: 48e310e858b269cea4c083c823846f3a53660bdb [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"
Reid Kleckner60573ae2019-11-15 17:31:55 -080018#include "clang/AST/Attr.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000019#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000020#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000022#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000023#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000024#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000025#include "clang/AST/LambdaCapture.h"
26#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000027#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000028#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000029#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000030#include "clang/AST/UnresolvedSet.h"
31#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000032#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000033#include "clang/Basic/LLVM.h"
34#include "clang/Basic/LangOptions.h"
35#include "clang/Basic/OperatorKinds.h"
36#include "clang/Basic/PartialDiagnostic.h"
37#include "clang/Basic/SourceLocation.h"
38#include "clang/Basic/Specifiers.h"
39#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000040#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000041#include "llvm/ADT/SmallVector.h"
42#include "llvm/ADT/iterator_range.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
46#include <algorithm>
47#include <cassert>
48#include <cstddef>
49#include <cstdint>
50
Ted Kremenek21475702008-09-05 17:16:31 +000051using namespace clang;
52
53//===----------------------------------------------------------------------===//
54// Decl Allocation/Deallocation Method Implementations
55//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000056
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000057void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000058
Douglas Gregor72172e92012-01-05 21:55:30 +000059AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000060 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000061}
62
Richard Smitha4ba74c2013-08-30 04:46:40 +000063void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
64 ExternalASTSource *Source = C.getExternalSource();
65 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
66 assert(Source && "getFromExternalSource with no external source");
67
68 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
69 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
70 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
71 Impl.Decls.setLazy(false);
72}
73
John McCall67da35c2010-02-04 22:26:26 +000074CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000075 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
76 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000077 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
78 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000079 HasPrivateFields(false), HasProtectedFields(false),
80 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
81 HasOnlyCMembers(true), HasInClassInitializer(false),
82 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000083 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000084 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000085 NeedOverloadResolutionForMoveConstructor(false),
86 NeedOverloadResolutionForMoveAssignment(false),
87 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000088 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000089 DefaultedMoveConstructorIsDeleted(false),
90 DefaultedMoveAssignmentIsDeleted(false),
91 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000092 HasTrivialSpecialMembersForCall(SMF_All),
93 DeclaredNonTrivialSpecialMembers(0),
94 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000095 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000096 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000097 DefaultedDefaultConstructorIsConstexpr(true),
98 HasConstexprDefaultConstructor(false),
Richard Smith457226e2019-09-23 03:48:44 +000099 DefaultedDestructorIsConstexpr(true),
Richard Smith91aeacc2019-10-11 00:29:04 +0000100 HasNonLiteralTypeFieldsOrBases(false),
Nico Weber6a6376b2016-02-19 01:52:46 +0000101 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +0000102 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
103 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +0000104 ImplicitCopyAssignmentHasConstParam(true),
105 HasDeclaredCopyConstructorWithConstParam(false),
106 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Richard Smith91aeacc2019-10-11 00:29:04 +0000107 IsParsingBaseSpecifiers(false), ComputedVisibleConversions(false),
108 HasODRHash(false), Definition(D) {}
John McCall67da35c2010-02-04 22:26:26 +0000109
Benjamin Kramer300c0632012-07-04 17:03:33 +0000110CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
111 return Bases.get(Definition->getASTContext().getExternalSource());
112}
113
114CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
115 return VBases.get(Definition->getASTContext().getExternalSource());
116}
117
Richard Smith053f6c62014-05-16 23:01:30 +0000118CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
119 DeclContext *DC, SourceLocation StartLoc,
120 SourceLocation IdLoc, IdentifierInfo *Id,
121 CXXRecordDecl *PrevDecl)
122 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
123 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000124 : nullptr) {}
Douglas Gregorb6acda02008-11-12 23:21:09 +0000125
Jay Foad39c79802011-01-12 09:06:06 +0000126CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000127 DeclContext *DC, SourceLocation StartLoc,
128 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smithbecb92d2017-10-10 22:33:17 +0000129 CXXRecordDecl *PrevDecl,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000130 bool DelayTypeCreation) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000131 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
132 PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000133 R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
Mike Stump11289f42009-09-09 15:08:12 +0000134
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000135 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000136 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000137 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000138 return R;
139}
140
Richard Smith053f6c62014-05-16 23:01:30 +0000141CXXRecordDecl *
142CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
143 TypeSourceInfo *Info, SourceLocation Loc,
144 bool Dependent, bool IsGeneric,
145 LambdaCaptureDefault CaptureDefault) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000146 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
147 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000148 R->setBeingDefined(true);
Richard Smith64c06302014-05-22 23:19:02 +0000149 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000150 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000151 CaptureDefault);
Erich Keanef92f31c2018-08-01 20:48:16 +0000152 R->setMayHaveOutOfDateDef(false);
James Dennett8f60cdd2013-09-05 17:46:21 +0000153 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000154 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000155 return R;
156}
157
Douglas Gregor72172e92012-01-05 21:55:30 +0000158CXXRecordDecl *
159CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000160 auto *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000161 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000162 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000163 R->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000164 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000165}
166
Richard Smithb6070db2018-04-05 18:55:37 +0000167/// Determine whether a class has a repeated base class. This is intended for
168/// use when determining if a class is standard-layout, so makes no attempt to
169/// handle virtual bases.
170static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
171 llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
172 SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
173 while (!WorkList.empty()) {
174 const CXXRecordDecl *RD = WorkList.pop_back_val();
175 for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
176 if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
177 if (!SeenBaseTypes.insert(B).second)
178 return true;
179 WorkList.push_back(B);
180 }
181 }
182 }
183 return false;
184}
185
Mike Stump11289f42009-09-09 15:08:12 +0000186void
Craig Toppere6337e12015-12-25 00:36:02 +0000187CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
188 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000189 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000190
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000191 if (!data().Bases.isOffset() && data().NumBases > 0)
192 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000193
Craig Toppere6337e12015-12-25 00:36:02 +0000194 if (NumBases) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000195 if (!C.getLangOpts().CPlusPlus17) {
Richard Smith872307e2016-03-08 22:17:41 +0000196 // C++ [dcl.init.aggr]p1:
197 // An aggregate is [...] a class with [...] no base classes [...].
198 data().Aggregate = false;
199 }
Richard Smithaed32b42011-10-18 20:08:55 +0000200
201 // C++ [class]p4:
202 // A POD-struct is an aggregate class...
203 data().PlainOldData = false;
204 }
205
Anders Carlssonabb20e62010-03-29 05:13:12 +0000206 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000207 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Fangrui Song6907ce22018-07-30 19:24:48 +0000208
Anders Carlssonabb20e62010-03-29 05:13:12 +0000209 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000210 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000211
Craig Toppere6337e12015-12-25 00:36:02 +0000212 data().Bases = new(C) CXXBaseSpecifier [NumBases];
213 data().NumBases = NumBases;
214 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000215 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000216 // Keep track of inherited vbases for this base class.
217 const CXXBaseSpecifier *Base = Bases[i];
218 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000219 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000220 if (BaseType->isDependentType())
221 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000222 auto *BaseClassDecl =
Simon Pilgrim1cd399c2019-10-03 11:22:48 +0000223 cast<CXXRecordDecl>(BaseType->castAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000224
Richard Smithb6070db2018-04-05 18:55:37 +0000225 // C++2a [class]p7:
226 // A standard-layout class is a class that:
227 // [...]
228 // -- has all non-static data members and bit-fields in the class and
229 // its base classes first declared in the same class
230 if (BaseClassDecl->data().HasBasesWithFields ||
231 !BaseClassDecl->field_empty()) {
232 if (data().HasBasesWithFields)
233 // Two bases have members or bit-fields: not standard-layout.
Chandler Carruth583edf82011-04-30 10:07:30 +0000234 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000235 data().HasBasesWithFields = true;
236 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000237
Richard Smithb6070db2018-04-05 18:55:37 +0000238 // C++11 [class]p7:
239 // A standard-layout class is a class that:
240 // -- [...] has [...] at most one base class with non-static data
241 // members
242 if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
243 BaseClassDecl->hasDirectFields()) {
244 if (data().HasBasesWithNonStaticDataMembers)
245 data().IsCXX11StandardLayout = false;
246 data().HasBasesWithNonStaticDataMembers = true;
247 }
248
249 if (!BaseClassDecl->isEmpty()) {
David Majnemer5cfda6f2016-05-22 05:34:26 +0000250 // C++14 [meta.unary.prop]p4:
251 // T is a class type [...] with [...] no base class B for which
252 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000253 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000254 }
Richard Smithb6070db2018-04-05 18:55:37 +0000255
Richard Smith872307e2016-03-08 22:17:41 +0000256 // C++1z [dcl.init.agg]p1:
257 // An aggregate is a class with [...] no private or protected base classes
258 if (Base->getAccessSpecifier() != AS_public)
259 data().Aggregate = false;
260
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000262 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000263 // polymorphic class.
Richard Smithc00e4462018-06-14 20:03:22 +0000264 if (BaseClassDecl->isPolymorphic()) {
Douglas Gregor11c024b2010-09-28 20:50:54 +0000265 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000266
Richard Smithc00e4462018-06-14 20:03:22 +0000267 // An aggregate is a class with [...] no virtual functions.
268 data().Aggregate = false;
269 }
270
Chandler Carruthb1963742011-04-30 09:17:45 +0000271 // C++0x [class]p7:
272 // A standard-layout class is a class that: [...]
273 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000274 if (!BaseClassDecl->isStandardLayout())
275 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000276 if (!BaseClassDecl->isCXX11StandardLayout())
277 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000278
Chandler Carruthe71d0622011-04-24 02:49:34 +0000279 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000280 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000281 data().HasNonLiteralTypeFieldsOrBases = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000282
Anders Carlssonabb20e62010-03-29 05:13:12 +0000283 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000284 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000285 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000286 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000287 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000288
289 // C++11 [class.copy]p8:
290 // The implicitly-declared copy constructor for a class X will have
291 // the form 'X::X(const X&)' if each [...] virtual base class B of X
292 // has a copy constructor whose first parameter is of type
293 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000294 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000295 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000296 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000297
298 // C++1z [dcl.init.agg]p1:
299 // An aggregate is a class with [...] no virtual base classes
300 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000301 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000302 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000303
304 if (Base->isVirtual()) {
305 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000306 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000307 VBases.push_back(Base);
308
David Majnemer5cfda6f2016-05-22 05:34:26 +0000309 // C++14 [meta.unary.prop] is_empty:
310 // T is a class type, but not a union type, with ... no virtual base
311 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000312 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000313
Richard Smith872307e2016-03-08 22:17:41 +0000314 // C++1z [dcl.init.agg]p1:
315 // An aggregate is a class with [...] no virtual base classes
316 data().Aggregate = false;
317
Richard Smith328aae52012-11-30 05:11:39 +0000318 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
319 // A [default constructor, copy/move constructor, or copy/move assignment
320 // operator for a class X] is trivial [...] if:
321 // -- class X has [...] no virtual base classes
322 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000323 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000324
325 // C++0x [class]p7:
326 // A standard-layout class is a class that: [...]
327 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000328 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000329 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000330
Richard Smith457226e2019-09-23 03:48:44 +0000331 // C++20 [dcl.constexpr]p3:
332 // In the definition of a constexpr function [...]
333 // -- if the function is a constructor or destructor,
334 // its class shall not have any virtual base classes
Richard Smithcc36f692011-12-22 02:22:31 +0000335 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith457226e2019-09-23 03:48:44 +0000336 data().DefaultedDestructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000337
338 // C++1z [class.copy]p8:
339 // The implicitly-declared copy constructor for a class X will have
340 // the form 'X::X(const X&)' if each potentially constructed subobject
341 // has a copy constructor whose first parameter is of type
342 // 'const B&' or 'const volatile B&' [...]
343 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
344 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000345 } else {
346 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000347 // A default constructor is trivial [...] if:
348 // -- all the direct base classes of its class have trivial default
349 // constructors.
350 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000351 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
352
Chandler Carruthad7d4042011-04-23 23:10:33 +0000353 // C++0x [class.copy]p13:
354 // A copy/move constructor for class X is trivial if [...]
355 // [...]
356 // -- the constructor selected to copy/move each direct base class
357 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000358 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000359 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000360
361 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
362 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
363
Richard Smith6b02d462012-12-08 08:32:28 +0000364 // If the base class doesn't have a simple move constructor, we'll eagerly
365 // declare it and perform overload resolution to determine which function
366 // it actually calls. If it does have a simple move constructor, this
367 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000368 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000369 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000370
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000371 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
372 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
373
Chandler Carruthad7d4042011-04-23 23:10:33 +0000374 // C++0x [class.copy]p27:
375 // A copy/move assignment operator for class X is trivial if [...]
376 // [...]
377 // -- the assignment operator selected to copy/move each direct base
378 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000379 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000380 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000381 // If the base class doesn't have a simple move assignment, we'll eagerly
382 // declare it and perform overload resolution to determine which function
383 // it actually calls. If it does have a simple move assignment, this
384 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000385 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000386 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000387
388 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000389 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000390 // requirements of a constexpr constructor, the implicitly-defined
391 // default constructor is constexpr.
392 if (!BaseClassDecl->hasConstexprDefaultConstructor())
393 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000394
395 // C++1z [class.copy]p8:
396 // The implicitly-declared copy constructor for a class X will have
397 // the form 'X::X(const X&)' if each potentially constructed subobject
398 // has a copy constructor whose first parameter is of type
399 // 'const B&' or 'const volatile B&' [...]
400 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
401 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000402 }
Richard Smith92f241f2012-12-08 02:53:02 +0000403
Douglas Gregor11c024b2010-09-28 20:50:54 +0000404 // C++ [class.ctor]p3:
405 // A destructor is trivial if all the direct base classes of its class
406 // have trivial destructors.
407 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000408 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000409
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000410 if (!BaseClassDecl->hasTrivialDestructorForCall())
411 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
412
Richard Smith561fb152012-02-25 07:33:38 +0000413 if (!BaseClassDecl->hasIrrelevantDestructor())
414 data().HasIrrelevantDestructor = false;
415
Richard Smith1c33fe82012-11-28 06:23:12 +0000416 // C++11 [class.copy]p18:
Mark de Wever403739b2019-11-04 22:27:53 +0100417 // The implicitly-declared copy assignment operator for a class X will
Richard Smith1c33fe82012-11-28 06:23:12 +0000418 // have the form 'X& X::operator=(const X&)' if each direct base class B
419 // of X has a copy assignment operator whose parameter is of type 'const
420 // B&', 'const volatile B&', or 'B' [...]
421 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
422 data().ImplicitCopyAssignmentHasConstParam = false;
423
John McCall31168b02011-06-15 23:02:42 +0000424 // A class has an Objective-C object member if... or any of its bases
425 // has an Objective-C object member.
426 if (BaseClassDecl->hasObjectMember())
427 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000428
Fariborz Jahanian78652202013-01-25 23:57:05 +0000429 if (BaseClassDecl->hasVolatileMember())
430 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000431
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000432 if (BaseClassDecl->getArgPassingRestrictions() ==
433 RecordDecl::APK_CanNeverPassInRegs)
434 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
435
Douglas Gregor61226d32011-05-13 01:05:07 +0000436 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000437 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000438 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000439 data().NeedOverloadResolutionForCopyConstructor = true;
440 }
Richard Smith593f9932012-12-08 02:01:17 +0000441
442 if (BaseClassDecl->hasUninitializedReferenceMember())
443 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000444
Nico Weber6a6376b2016-02-19 01:52:46 +0000445 if (!BaseClassDecl->allowConstDefaultInit())
446 data().HasUninitializedFields = true;
447
Richard Smith6b02d462012-12-08 08:32:28 +0000448 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000449 }
Richard Smithb6070db2018-04-05 18:55:37 +0000450
451 // C++2a [class]p7:
452 // A class S is a standard-layout class if it:
453 // -- has at most one base class subobject of any given type
454 //
455 // Note that we only need to check this for classes with more than one base
456 // class. If there's only one base class, and it's standard layout, then
457 // we know there are no repeated base classes.
458 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
459 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000460
David Majnemer5ef4fe72014-06-13 06:43:46 +0000461 if (VBases.empty()) {
462 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000463 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000464 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000465
466 // Create base specifier for any direct or indirect virtual bases.
467 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
468 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000469 for (int I = 0, E = VBases.size(); I != E; ++I) {
470 QualType Type = VBases[I]->getType();
471 if (!Type->isDependentType())
472 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000473 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000474 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000475
476 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000477}
478
Richard Trieufd1acbb2017-04-11 21:31:00 +0000479unsigned CXXRecordDecl::getODRHash() const {
480 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000481
Richard Trieufd1acbb2017-04-11 21:31:00 +0000482 // Previously calculated hash is stored in DefinitionData.
483 if (DefinitionData->HasODRHash)
484 return DefinitionData->ODRHash;
485
486 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000487 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000488 Hash.AddCXXRecordDecl(getDefinition());
489 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000490 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000491
492 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000493}
Richard Trieub6adf542017-02-18 02:09:28 +0000494
Richard Smith6b02d462012-12-08 08:32:28 +0000495void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
496 // C++11 [class.copy]p11:
497 // A defaulted copy/move constructor for a class X is defined as
498 // deleted if X has:
499 // -- a direct or virtual base class B that cannot be copied/moved [...]
500 // -- a non-static data member of class type M (or array thereof)
501 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000502 if (!Subobj->hasSimpleCopyConstructor())
503 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000504 if (!Subobj->hasSimpleMoveConstructor())
505 data().NeedOverloadResolutionForMoveConstructor = true;
506
507 // C++11 [class.copy]p23:
508 // A defaulted copy/move assignment operator for a class X is defined as
509 // deleted if X has:
510 // -- a direct or virtual base class B that cannot be copied/moved [...]
511 // -- a non-static data member of class type M (or array thereof)
512 // that cannot be copied or moved [...]
513 if (!Subobj->hasSimpleMoveAssignment())
514 data().NeedOverloadResolutionForMoveAssignment = true;
515
516 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
517 // A defaulted [ctor or dtor] for a class X is defined as
518 // deleted if X has:
519 // -- any direct or virtual base class [...] has a type with a destructor
520 // that is deleted or inaccessible from the defaulted [ctor or dtor].
521 // -- any non-static data member has a type with a destructor
522 // that is deleted or inaccessible from the defaulted [ctor or dtor].
523 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000524 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000525 data().NeedOverloadResolutionForMoveConstructor = true;
526 data().NeedOverloadResolutionForDestructor = true;
527 }
Richard Smith457226e2019-09-23 03:48:44 +0000528
529 // C++2a [dcl.constexpr]p4:
530 // The definition of a constexpr destructor [shall] satisfy the
531 // following requirement:
532 // -- for every subobject of class type or (possibly multi-dimensional)
533 // array thereof, that class type shall have a constexpr destructor
534 if (!Subobj->hasConstexprDestructor())
535 data().DefaultedDestructorIsConstexpr = false;
536}
537
538bool CXXRecordDecl::hasConstexprDestructor() const {
539 auto *Dtor = getDestructor();
540 return Dtor ? Dtor->isConstexpr() : defaultedDestructorIsConstexpr();
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000541}
542
Douglas Gregord2e6a452010-01-14 17:47:39 +0000543bool CXXRecordDecl::hasAnyDependentBases() const {
544 if (!isDependentContext())
545 return false;
546
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000547 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000548}
549
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000550bool CXXRecordDecl::isTriviallyCopyable() const {
551 // C++0x [class]p5:
552 // A trivially copyable class is a class that:
553 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000554 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000555 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000556 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000557 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000558 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000559 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000560 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000561 // -- has a trivial destructor.
562 if (!hasTrivialDestructor()) return false;
563
564 return true;
565}
566
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000567void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000568 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000569 // A class is abstract if it has at least one pure virtual function.
570 data().Abstract = true;
571}
572
Richard Smithb6070db2018-04-05 18:55:37 +0000573bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
574 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
575 if (!getNumBases())
576 return false;
577
578 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
579 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
580 SmallVector<const CXXRecordDecl*, 8> WorkList;
581
582 // Visit a type that we have determined is an element of M(S).
583 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
584 RD = RD->getCanonicalDecl();
585
586 // C++2a [class]p8:
587 // A class S is a standard-layout class if it [...] has no element of the
588 // set M(S) of types as a base class.
589 //
590 // If we find a subobject of an empty type, it might also be a base class,
591 // so we'll need to walk the base classes to check.
592 if (!RD->data().HasBasesWithFields) {
593 // Walk the bases the first time, stopping if we find the type. Build a
594 // set of them so we don't need to walk them again.
595 if (Bases.empty()) {
596 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
597 Base = Base->getCanonicalDecl();
598 if (RD == Base)
599 return false;
600 Bases.insert(Base);
601 return true;
602 });
603 if (RDIsBase)
604 return true;
605 } else {
606 if (Bases.count(RD))
607 return true;
608 }
609 }
610
611 if (M.insert(RD).second)
612 WorkList.push_back(RD);
613 return false;
614 };
615
616 if (Visit(XFirst))
617 return true;
618
619 while (!WorkList.empty()) {
620 const CXXRecordDecl *X = WorkList.pop_back_val();
621
622 // FIXME: We don't check the bases of X. That matches the standard, but
623 // that sure looks like a wording bug.
624
625 // -- If X is a non-union class type with a non-static data member
Richard Smith78b239e2019-06-20 20:44:45 +0000626 // [recurse to each field] that is either of zero size or is the
627 // first non-static data member of X
Richard Smithb6070db2018-04-05 18:55:37 +0000628 // -- If X is a union type, [recurse to union members]
Richard Smith78b239e2019-06-20 20:44:45 +0000629 bool IsFirstField = true;
Richard Smithb6070db2018-04-05 18:55:37 +0000630 for (auto *FD : X->fields()) {
631 // FIXME: Should we really care about the type of the first non-static
632 // data member of a non-union if there are preceding unnamed bit-fields?
633 if (FD->isUnnamedBitfield())
634 continue;
635
Richard Smith78b239e2019-06-20 20:44:45 +0000636 if (!IsFirstField && !FD->isZeroSize(Ctx))
637 continue;
638
Richard Smithb6070db2018-04-05 18:55:37 +0000639 // -- If X is n array type, [visit the element type]
640 QualType T = Ctx.getBaseElementType(FD->getType());
641 if (auto *RD = T->getAsCXXRecordDecl())
642 if (Visit(RD))
643 return true;
644
645 if (!X->isUnion())
Richard Smith78b239e2019-06-20 20:44:45 +0000646 IsFirstField = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000647 }
648 }
649
650 return false;
651}
652
Richard Smith864949b2018-09-27 22:47:04 +0000653bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
654 assert(isLambda() && "not a lambda");
655
656 // C++2a [expr.prim.lambda.capture]p11:
657 // The closure type associated with a lambda-expression has no default
658 // constructor if the lambda-expression has a lambda-capture and a
659 // defaulted default constructor otherwise. It has a deleted copy
660 // assignment operator if the lambda-expression has a lambda-capture and
661 // defaulted copy and move assignment operators otherwise.
662 //
663 // C++17 [expr.prim.lambda]p21:
664 // The closure type associated with a lambda-expression has no default
665 // constructor and a deleted copy assignment operator.
David Blaikie793231c2019-07-08 23:24:41 +0000666 if (getLambdaCaptureDefault() != LCD_None ||
667 getLambdaData().NumCaptures != 0)
Richard Smith864949b2018-09-27 22:47:04 +0000668 return false;
669 return getASTContext().getLangOpts().CPlusPlus2a;
670}
671
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000672void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000673 if (!D->isImplicit() &&
674 !isa<FieldDecl>(D) &&
675 !isa<IndirectFieldDecl>(D) &&
676 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
677 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
678 data().HasOnlyCMembers = false;
679
680 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000681 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000682 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000683
Eugene Zelenko7855e772018-04-03 00:11:50 +0000684 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000685 if (FunTmpl)
686 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000687
688 // FIXME: Pass NamedDecl* to addedMember?
689 Decl *DUnderlying = D;
690 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
691 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000692 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000693 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
694 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000695
Eugene Zelenko7855e772018-04-03 00:11:50 +0000696 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000697 if (Method->isVirtual()) {
698 // C++ [dcl.init.aggr]p1:
699 // An aggregate is an array or a class with [...] no virtual functions.
700 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000701
Douglas Gregora832d3e2010-09-28 19:45:33 +0000702 // C++ [class]p4:
703 // A POD-struct is an aggregate class...
704 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000705
David Majnemer5cfda6f2016-05-22 05:34:26 +0000706 // C++14 [meta.unary.prop]p4:
707 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000708 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000709
710 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000711 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000712 // polymorphic class.
713 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000714
Richard Smith328aae52012-11-30 05:11:39 +0000715 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
716 // A [default constructor, copy/move constructor, or copy/move
717 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000718 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000719 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000720 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000721
Chandler Carruthb1963742011-04-30 09:17:45 +0000722 // C++0x [class]p7:
723 // A standard-layout class is a class that: [...]
724 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000725 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000726 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000727 }
728 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000729
Richard Smith1c33fe82012-11-28 06:23:12 +0000730 // Notify the listener if an implicit member was added after the definition
731 // was completed.
732 if (!isBeingDefined() && D->isImplicit())
733 if (ASTMutationListener *L = getASTMutationListener())
734 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000735
Richard Smith328aae52012-11-30 05:11:39 +0000736 // The kind of special member this declaration is, if any.
737 unsigned SMKind = 0;
738
Richard Smith1c33fe82012-11-28 06:23:12 +0000739 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000740 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1b5404a2020-01-14 19:29:50 -0800741 if (Constructor->isInheritingConstructor()) {
742 // Ignore constructor shadow declarations. They are lazily created and
743 // so shouldn't affect any properties of the class.
744 } else {
745 if (!Constructor->isImplicit()) {
746 // Note that we have a user-declared constructor.
747 data().UserDeclaredConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000748
Richard Smith1b5404a2020-01-14 19:29:50 -0800749 // C++ [class]p4:
750 // A POD-struct is an aggregate class [...]
751 // Since the POD bit is meant to be C++03 POD-ness, clear it even if
752 // the type is technically an aggregate in C++0x since it wouldn't be
753 // in 03.
754 data().PlainOldData = false;
755 }
756
757 if (Constructor->isDefaultConstructor()) {
758 SMKind |= SMF_DefaultConstructor;
759
760 if (Constructor->isUserProvided())
761 data().UserProvidedDefaultConstructor = true;
762 if (Constructor->isConstexpr())
763 data().HasConstexprDefaultConstructor = true;
764 if (Constructor->isDefaulted())
765 data().HasDefaultedDefaultConstructor = true;
766 }
767
768 if (!FunTmpl) {
769 unsigned Quals;
770 if (Constructor->isCopyConstructor(Quals)) {
771 SMKind |= SMF_CopyConstructor;
772
773 if (Quals & Qualifiers::Const)
774 data().HasDeclaredCopyConstructorWithConstParam = true;
775 } else if (Constructor->isMoveConstructor())
776 SMKind |= SMF_MoveConstructor;
777 }
778
779 // C++11 [dcl.init.aggr]p1: DR1518
780 // An aggregate is an array or a class with no user-provided [or]
781 // explicit [...] constructors
782 // C++20 [dcl.init.aggr]p1:
783 // An aggregate is an array or a class with no user-declared [...]
784 // constructors
785 if (getASTContext().getLangOpts().CPlusPlus2a
786 ? !Constructor->isImplicit()
787 : (Constructor->isUserProvided() || Constructor->isExplicit()))
788 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000789 }
Richard Smith5179eb72016-06-28 19:03:57 +0000790 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000791
Richard Smith5179eb72016-06-28 19:03:57 +0000792 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000793 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000794 // Record if we see any constexpr constructors which are neither copy
795 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000796 // C++1z [basic.types]p10:
797 // [...] has at least one constexpr constructor or constructor template
798 // (possibly inherited from a base class) that is not a copy or move
799 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000800 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000801 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000802 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000803
Richard Smith1c33fe82012-11-28 06:23:12 +0000804 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000805 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000806 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000807
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000808 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000809 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000810 // If the destructor is explicitly defaulted and not trivial or not public
811 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
812 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000813
Richard Smith1c33fe82012-11-28 06:23:12 +0000814 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000815 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000816 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000817 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000818 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
819 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000820 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000821
822 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000823 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000824 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000825 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000826
Eugene Zelenko7855e772018-04-03 00:11:50 +0000827 const auto *ParamTy =
828 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000829 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
830 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000831 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000832
Richard Smith328aae52012-11-30 05:11:39 +0000833 if (Method->isMoveAssignmentOperator())
834 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000835
Douglas Gregor457104e2010-09-29 04:25:11 +0000836 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000837 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000838 // FIXME: We use the 'unsafe' accessor for the access specifier here,
839 // because Sema may not have set it yet. That's really just a misdesign
840 // in Sema. However, LLDB *will* have set the access specifier correctly,
841 // and adds declarations after the class is technically completed,
842 // so completeDefinition()'s overriding of the access specifiers doesn't
843 // work.
844 AccessSpecifier AS = Conversion->getAccessUnsafe();
845
Richard Smith328aae52012-11-30 05:11:39 +0000846 if (Conversion->getPrimaryTemplate()) {
847 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000848 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000849 ASTContext &Ctx = getASTContext();
850 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
851 NamedDecl *Primary =
852 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
853 if (Primary->getPreviousDecl())
854 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
855 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000856 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000857 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000858 }
859 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000860
Richard Smith328aae52012-11-30 05:11:39 +0000861 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000862 // If this is the first declaration of a special member, we no longer have
863 // an implicit trivial special member.
864 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000865 data().DeclaredSpecialMembers | ~SMKind;
866 data().HasTrivialSpecialMembersForCall &=
867 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000868
869 if (!Method->isImplicit() && !Method->isUserProvided()) {
870 // This method is user-declared but not user-provided. We can't work out
871 // whether it's trivial yet (not until we get to the end of the class).
872 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000873 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000874 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000875 data().HasTrivialSpecialMembersForCall |= SMKind;
876 } else if (Method->isTrivialForCall()) {
877 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000878 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000879 } else {
880 data().DeclaredNonTrivialSpecialMembers |= SMKind;
881 // If this is a user-provided function, do not set
882 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
883 // yet whether the method would be considered non-trivial for the
884 // purpose of calls (attribute "trivial_abi" can be dropped from the
885 // class later, which can change the special method's triviality).
886 if (!Method->isUserProvided())
887 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
888 }
Richard Smith92f241f2012-12-08 02:53:02 +0000889
Richard Smith328aae52012-11-30 05:11:39 +0000890 // Note when we have declared a declared special member, and suppress the
891 // implicit declaration of this special member.
892 data().DeclaredSpecialMembers |= SMKind;
893
894 if (!Method->isImplicit()) {
895 data().UserDeclaredSpecialMembers |= SMKind;
896
897 // C++03 [class]p4:
898 // A POD-struct is an aggregate class that has [...] no user-defined
899 // copy assignment operator and no user-defined destructor.
900 //
901 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
902 // aggregates could not have any constructors, clear it even for an
903 // explicitly defaulted or deleted constructor.
904 // type is technically an aggregate in C++0x since it wouldn't be in 03.
905 //
906 // Also, a user-declared move assignment operator makes a class non-POD.
907 // This is an extension in C++03.
908 data().PlainOldData = false;
909 }
Richard Smith328aae52012-11-30 05:11:39 +0000910 }
911
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000912 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000913 }
Richard Smith328aae52012-11-30 05:11:39 +0000914
Douglas Gregora832d3e2010-09-28 19:45:33 +0000915 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000916 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000917 ASTContext &Context = getASTContext();
918
Richard Smithb6070db2018-04-05 18:55:37 +0000919 // C++2a [class]p7:
920 // A standard-layout class is a class that:
921 // [...]
922 // -- has all non-static data members and bit-fields in the class and
923 // its base classes first declared in the same class
924 if (data().HasBasesWithFields)
925 data().IsStandardLayout = false;
926
Douglas Gregor556e5862011-10-10 17:22:13 +0000927 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000928 // A declaration for a bit-field that omits the identifier declares an
929 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000930 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000931 if (Field->isUnnamedBitfield()) {
932 // C++ [meta.unary.prop]p4: [LWG2358]
933 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
934 // length
935 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
936 Context.getLangOpts().getClangABICompat() >
937 LangOptions::ClangABI::Ver6)
938 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000939 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000940 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000941
Richard Smithb6070db2018-04-05 18:55:37 +0000942 // C++11 [class]p7:
943 // A standard-layout class is a class that:
944 // -- either has no non-static data members in the most derived class
945 // [...] or has no base classes with non-static data members
946 if (data().HasBasesWithNonStaticDataMembers)
947 data().IsCXX11StandardLayout = false;
948
Douglas Gregora832d3e2010-09-28 19:45:33 +0000949 // C++ [dcl.init.aggr]p1:
950 // An aggregate is an array or a class (clause 9) with [...] no
951 // private or protected non-static data members (clause 11).
952 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000953 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000954 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
955 data().Aggregate = false;
956 data().PlainOldData = false;
957 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000958
Richard Smithb6070db2018-04-05 18:55:37 +0000959 // Track whether this is the first field. We use this when checking
960 // whether the class is standard-layout below.
961 bool IsFirstField = !data().HasPrivateFields &&
962 !data().HasProtectedFields && !data().HasPublicFields;
963
Chandler Carruthb1963742011-04-30 09:17:45 +0000964 // C++0x [class]p7:
965 // A standard-layout class is a class that:
966 // [...]
967 // -- has the same access control for all non-static data members,
968 switch (D->getAccess()) {
969 case AS_private: data().HasPrivateFields = true; break;
970 case AS_protected: data().HasProtectedFields = true; break;
971 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000972 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000973 };
974 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000975 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000976 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000977 data().IsCXX11StandardLayout = false;
978 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000979
Douglas Gregor61226d32011-05-13 01:05:07 +0000980 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000981 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000982 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000983 data().NeedOverloadResolutionForCopyConstructor = true;
984 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000985
986 // C++11 [class.union]p8, DR1460:
987 // If X is a union, a non-static data member of X that is not an anonymous
988 // union is a variant member of X.
989 if (isUnion() && !Field->isAnonymousStructOrUnion())
990 data().HasVariantMembers = true;
991
Chandler Carruthad7d4042011-04-23 23:10:33 +0000992 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000993 // A POD struct is a class that is both a trivial class and a
994 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000995 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000996 //
997 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
998 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000999 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +00001000 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +00001001 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +00001002 // Objective-C Automatic Reference Counting:
1003 // If a class has a non-static data member of Objective-C pointer
1004 // type (or array thereof), it is a non-POD type and its
1005 // default constructor (if any), copy constructor, move constructor,
1006 // copy assignment operator, move assignment operator, and destructor are
1007 // non-trivial.
1008 setHasObjectMember(true);
1009 struct DefinitionData &Data = data();
1010 Data.PlainOldData = false;
1011 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001012
1013 // __strong or __weak fields do not make special functions non-trivial
1014 // for the purpose of calls.
1015 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
1016 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
1017 data().HasTrivialSpecialMembersForCall = 0;
1018
1019 // Structs with __weak fields should never be passed directly.
1020 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001021 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001022
Ben Langmuir11eab612014-09-26 15:27:29 +00001023 Data.HasIrrelevantDestructor = false;
Akira Hatanakaa5048972019-02-02 02:23:40 +00001024
1025 if (isUnion()) {
1026 data().DefaultedCopyConstructorIsDeleted = true;
1027 data().DefaultedMoveConstructorIsDeleted = true;
1028 data().DefaultedMoveAssignmentIsDeleted = true;
1029 data().DefaultedDestructorIsDeleted = true;
1030 data().NeedOverloadResolutionForCopyConstructor = true;
1031 data().NeedOverloadResolutionForMoveConstructor = true;
1032 data().NeedOverloadResolutionForMoveAssignment = true;
1033 data().NeedOverloadResolutionForDestructor = true;
1034 }
Brian Kelley036603a2017-03-29 17:31:42 +00001035 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1036 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +00001037 }
Eli Friedmana5433322013-07-20 01:06:31 +00001038 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +00001039 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001040
Chandler Carruthb1963742011-04-30 09:17:45 +00001041 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +00001042 if (!Field->hasInClassInitializer())
1043 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +00001044
Chandler Carruthb1963742011-04-30 09:17:45 +00001045 // C++0x [class]p7:
1046 // A standard-layout class is a class that:
1047 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +00001048 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001049 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +00001050
1051 // C++1z [class.copy.ctor]p10:
1052 // A defaulted copy constructor for a class X is defined as deleted if X has:
1053 // -- a non-static data member of rvalue reference type
1054 if (T->isRValueReferenceType())
1055 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +00001056 }
1057
Nico Weber6a6376b2016-02-19 01:52:46 +00001058 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +00001059 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +00001060 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +00001061 data().HasUninitializedFields = true;
1062 } else {
1063 data().HasUninitializedFields = true;
1064 }
1065 }
1066
Richard Smith3607ffe2012-02-13 03:54:03 +00001067 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001068 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001069 data().HasNonLiteralTypeFieldsOrBases = true;
1070
Richard Smithab44d5b2013-12-10 08:25:00 +00001071 if (Field->hasInClassInitializer() ||
1072 (Field->isAnonymousStructOrUnion() &&
1073 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001074 data().HasInClassInitializer = true;
1075
1076 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001077 // A default constructor is trivial if [...] no non-static data member
1078 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001079 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001080
Richard Smithe2648ba2012-05-07 01:07:30 +00001081 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001082 // An aggregate is a [...] class with [...] no
1083 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001084 //
Richard Smith872307e2016-03-08 22:17:41 +00001085 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001086 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001087 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001088
Richard Smithe2648ba2012-05-07 01:07:30 +00001089 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001090 // A POD struct is [...] a trivial class.
1091 data().PlainOldData = false;
1092 }
1093
Richard Smith6b02d462012-12-08 08:32:28 +00001094 // C++11 [class.copy]p23:
1095 // A defaulted copy/move assignment operator for a class X is defined
1096 // as deleted if X has:
1097 // -- a non-static data member of reference type
1098 if (T->isReferenceType())
1099 data().DefaultedMoveAssignmentIsDeleted = true;
1100
Richard Smith78b239e2019-06-20 20:44:45 +00001101 // Bitfields of length 0 are also zero-sized, but we already bailed out for
1102 // those because they are always unnamed.
1103 bool IsZeroSize = Field->isZeroSize(Context);
1104
Eugene Zelenko7855e772018-04-03 00:11:50 +00001105 if (const auto *RecordTy = T->getAs<RecordType>()) {
1106 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001107 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001108 addedClassSubobject(FieldRec);
1109
Richard Smithc91d12c2013-11-25 07:07:05 +00001110 // We may need to perform overload resolution to determine whether a
1111 // field can be moved if it's const or volatile qualified.
1112 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001113 // We need to care about 'const' for the copy constructor because an
1114 // implicit copy constructor might be declared with a non-const
1115 // parameter.
1116 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001117 data().NeedOverloadResolutionForMoveConstructor = true;
1118 data().NeedOverloadResolutionForMoveAssignment = true;
1119 }
1120
Richard Smith6b02d462012-12-08 08:32:28 +00001121 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1122 // A defaulted [special member] for a class X is defined as
1123 // deleted if:
1124 // -- X is a union-like class that has a variant member with a
1125 // non-trivial [corresponding special member]
1126 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001127 if (FieldRec->hasNonTrivialCopyConstructor())
1128 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001129 if (FieldRec->hasNonTrivialMoveConstructor())
1130 data().DefaultedMoveConstructorIsDeleted = true;
1131 if (FieldRec->hasNonTrivialMoveAssignment())
1132 data().DefaultedMoveAssignmentIsDeleted = true;
1133 if (FieldRec->hasNonTrivialDestructor())
1134 data().DefaultedDestructorIsDeleted = true;
1135 }
1136
Richard Smithdd5619f2016-08-16 00:13:47 +00001137 // For an anonymous union member, our overload resolution will perform
1138 // overload resolution for its members.
1139 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001140 data().NeedOverloadResolutionForCopyConstructor |=
1141 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001142 data().NeedOverloadResolutionForMoveConstructor |=
1143 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1144 data().NeedOverloadResolutionForMoveAssignment |=
1145 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1146 data().NeedOverloadResolutionForDestructor |=
1147 FieldRec->data().NeedOverloadResolutionForDestructor;
1148 }
1149
Alexis Huntf479f1b2011-05-09 18:22:59 +00001150 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001151 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001152 // -- for all the non-static data members of its class that are of
1153 // class type (or array thereof), each such class has a trivial
1154 // default constructor.
1155 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001156 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001157
1158 // C++0x [class.copy]p13:
1159 // A copy/move constructor for class X is trivial if [...]
1160 // [...]
1161 // -- for each non-static data member of X that is of class type (or
1162 // an array thereof), the constructor selected to copy/move that
1163 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001164 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001165 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001166
1167 if (!FieldRec->hasTrivialCopyConstructorForCall())
1168 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1169
Richard Smith6b02d462012-12-08 08:32:28 +00001170 // If the field doesn't have a simple move constructor, we'll eagerly
1171 // declare the move constructor for this class and we'll decide whether
1172 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001173 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001174 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001175
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001176 if (!FieldRec->hasTrivialMoveConstructorForCall())
1177 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1178
Chandler Carruthad7d4042011-04-23 23:10:33 +00001179 // C++0x [class.copy]p27:
1180 // A copy/move assignment operator for class X is trivial if [...]
1181 // [...]
1182 // -- for each non-static data member of X that is of class type (or
1183 // an array thereof), the assignment operator selected to
1184 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001185 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001186 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001187 // If the field doesn't have a simple move assignment, we'll eagerly
1188 // declare the move assignment for this class and we'll decide whether
1189 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001190 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001191 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001192
Douglas Gregor11c024b2010-09-28 20:50:54 +00001193 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001194 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001195 if (!FieldRec->hasTrivialDestructorForCall())
1196 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001197 if (!FieldRec->hasIrrelevantDestructor())
1198 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001199 if (FieldRec->hasObjectMember())
1200 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001201 if (FieldRec->hasVolatileMember())
1202 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001203 if (FieldRec->getArgPassingRestrictions() ==
1204 RecordDecl::APK_CanNeverPassInRegs)
1205 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001206
1207 // C++0x [class]p7:
1208 // A standard-layout class is a class that:
1209 // -- has no non-static data members of type non-standard-layout
1210 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001211 if (!FieldRec->isStandardLayout())
1212 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001213 if (!FieldRec->isCXX11StandardLayout())
1214 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001215
Richard Smithb6070db2018-04-05 18:55:37 +00001216 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001217 // A standard-layout class is a class that:
1218 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001219 // -- has no element of the set M(S) of types as a base class.
Richard Smith78b239e2019-06-20 20:44:45 +00001220 if (data().IsStandardLayout &&
1221 (isUnion() || IsFirstField || IsZeroSize) &&
Richard Smithb6070db2018-04-05 18:55:37 +00001222 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1223 data().IsStandardLayout = false;
1224
1225 // C++11 [class]p7:
1226 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001227 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001228 // data member
1229 if (data().IsCXX11StandardLayout && IsFirstField) {
1230 // FIXME: We should check all base classes here, not just direct
1231 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001232 for (const auto &BI : bases()) {
1233 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001234 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001235 break;
1236 }
1237 }
1238 }
Richard Smithb6070db2018-04-05 18:55:37 +00001239
Douglas Gregor61226d32011-05-13 01:05:07 +00001240 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001241 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001242 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001243 data().NeedOverloadResolutionForCopyConstructor = true;
1244 }
Richard Smithcc36f692011-12-22 02:22:31 +00001245
1246 // C++11 [class.copy]p13:
1247 // If the implicitly-defined constructor would satisfy the
1248 // requirements of a constexpr constructor, the implicitly-defined
1249 // constructor is constexpr.
1250 // C++11 [dcl.constexpr]p4:
1251 // -- every constructor involved in initializing non-static data
1252 // members [...] shall be a constexpr constructor
1253 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001254 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001255 // The standard requires any in-class initializer to be a constant
1256 // expression. We consider this to be a defect.
1257 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001258
1259 // C++11 [class.copy]p8:
1260 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001261 // the form 'X::X(const X&)' if each potentially constructed subobject
1262 // of a class type M (or array thereof) has a copy constructor whose
1263 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001264 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001265 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001266
1267 // C++11 [class.copy]p18:
1268 // The implicitly-declared copy assignment oeprator for a class X will
1269 // have the form 'X& X::operator=(const X&)' if [...] for all the
1270 // non-static data members of X that are of a class type M (or array
1271 // thereof), each such class type has a copy assignment operator whose
1272 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1273 if (!FieldRec->hasCopyAssignmentWithConstParam())
1274 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001275
1276 if (FieldRec->hasUninitializedReferenceMember() &&
1277 !Field->hasInClassInitializer())
1278 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001279
1280 // C++11 [class.union]p8, DR1460:
1281 // a non-static data member of an anonymous union that is a member of
1282 // X is also a variant member of X.
1283 if (FieldRec->hasVariantMembers() &&
1284 Field->isAnonymousStructOrUnion())
1285 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001286 }
Richard Smithcc36f692011-12-22 02:22:31 +00001287 } else {
1288 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001289 if (!T->isLiteralType(Context) ||
Richard Smithc667cdc2019-09-18 17:37:44 +00001290 (!Field->hasInClassInitializer() && !isUnion() &&
1291 !Context.getLangOpts().CPlusPlus2a))
Richard Smithcc36f692011-12-22 02:22:31 +00001292 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001293
1294 // C++11 [class.copy]p23:
1295 // A defaulted copy/move assignment operator for a class X is defined
1296 // as deleted if X has:
1297 // -- a non-static data member of const non-class type (or array
1298 // thereof)
1299 if (T.isConstQualified())
1300 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001301 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001302
David Majnemer5cfda6f2016-05-22 05:34:26 +00001303 // C++14 [meta.unary.prop]p4:
Richard Smith78b239e2019-06-20 20:44:45 +00001304 // T is a class type [...] with [...] no non-static data members other
1305 // than subobjects of zero size
1306 if (data().Empty && !IsZeroSize)
1307 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001308 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001309
Douglas Gregor457104e2010-09-29 04:25:11 +00001310 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001311 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001312 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001313 == DeclarationName::CXXConversionFunctionName) {
1314 ASTContext &Ctx = getASTContext();
1315 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1316 }
1317 }
Richard Smith12e79312016-05-13 06:47:56 +00001318
Eugene Zelenko7855e772018-04-03 00:11:50 +00001319 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001320 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001321 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001322 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001323 // C++1z [dcl.init.aggr]p1:
1324 // An aggregate is [...] a class [...] with no inherited constructors
1325 data().Aggregate = false;
1326 }
Richard Smith12e79312016-05-13 06:47:56 +00001327
1328 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1329 data().HasInheritedAssignment = true;
1330 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001331}
1332
Richard Smith92f241f2012-12-08 02:53:02 +00001333void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1334 assert(!D->isImplicit() && !D->isUserProvided());
1335
1336 // The kind of special member this declaration is, if any.
1337 unsigned SMKind = 0;
1338
Eugene Zelenko7855e772018-04-03 00:11:50 +00001339 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001340 if (Constructor->isDefaultConstructor()) {
1341 SMKind |= SMF_DefaultConstructor;
1342 if (Constructor->isConstexpr())
1343 data().HasConstexprDefaultConstructor = true;
1344 }
1345 if (Constructor->isCopyConstructor())
1346 SMKind |= SMF_CopyConstructor;
1347 else if (Constructor->isMoveConstructor())
1348 SMKind |= SMF_MoveConstructor;
1349 else if (Constructor->isConstexpr())
1350 // We may now know that the constructor is constexpr.
1351 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001352 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001353 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001354 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1355 data().HasIrrelevantDestructor = false;
1356 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001357 SMKind |= SMF_CopyAssignment;
1358 else if (D->isMoveAssignmentOperator())
1359 SMKind |= SMF_MoveAssignment;
1360
1361 // Update which trivial / non-trivial special members we have.
1362 // addedMember will have skipped this step for this member.
1363 if (D->isTrivial())
1364 data().HasTrivialSpecialMembers |= SMKind;
1365 else
1366 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1367}
1368
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001369void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1370 unsigned SMKind = 0;
1371
Eugene Zelenko7855e772018-04-03 00:11:50 +00001372 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001373 if (Constructor->isCopyConstructor())
1374 SMKind = SMF_CopyConstructor;
1375 else if (Constructor->isMoveConstructor())
1376 SMKind = SMF_MoveConstructor;
1377 } else if (isa<CXXDestructorDecl>(D))
1378 SMKind = SMF_Destructor;
1379
1380 if (D->isTrivialForCall())
1381 data().HasTrivialSpecialMembersForCall |= SMKind;
1382 else
1383 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1384}
1385
Joao Matose9a3ed42012-08-31 22:18:20 +00001386bool CXXRecordDecl::isCLike() const {
1387 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1388 !TemplateOrInstantiation.isNull())
1389 return false;
1390 if (!hasDefinition())
1391 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001392
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001393 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001394}
Fangrui Song6907ce22018-07-30 19:24:48 +00001395
1396bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001397 if (!isLambda()) return false;
1398 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001399}
1400
Richard Smith24253382018-08-02 20:30:52 +00001401#ifndef NDEBUG
1402static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1403 for (auto *D : R)
1404 if (!declaresSameEntity(D, R.front()))
1405 return false;
1406 return true;
1407}
1408#endif
1409
Erich Keane98f08992019-09-30 21:24:04 +00001410static NamedDecl* getLambdaCallOperatorHelper(const CXXRecordDecl &RD) {
Erich Keane5c2c60d2019-09-30 19:12:29 +00001411 if (!RD.isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001412 DeclarationName Name =
Erich Keane5c2c60d2019-09-30 19:12:29 +00001413 RD.getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1414 DeclContext::lookup_result Calls = RD.lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001415
1416 assert(!Calls.empty() && "Missing lambda call operator!");
Richard Smith24253382018-08-02 20:30:52 +00001417 assert(allLookupResultsAreTheSame(Calls) &&
1418 "More than one lambda call operator!");
Erich Keane5c2c60d2019-09-30 19:12:29 +00001419 return Calls.front();
1420}
Fangrui Song6907ce22018-07-30 19:24:48 +00001421
Erich Keane5c2c60d2019-09-30 19:12:29 +00001422FunctionTemplateDecl* CXXRecordDecl::getDependentLambdaCallOperator() const {
1423 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001424 return dyn_cast_or_null<FunctionTemplateDecl>(CallOp);
Erich Keane5c2c60d2019-09-30 19:12:29 +00001425}
1426
1427CXXMethodDecl *CXXRecordDecl::getLambdaCallOperator() const {
1428 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001429
1430 if (CallOp == nullptr)
1431 return nullptr;
1432
Eugene Zelenko7855e772018-04-03 00:11:50 +00001433 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001434 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001435
Faisal Vali2b391ab2013-09-26 19:54:12 +00001436 return cast<CXXMethodDecl>(CallOp);
1437}
1438
1439CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001440 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001441 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001442 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001443 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001444 if (Invoker.empty()) return nullptr;
Richard Smith24253382018-08-02 20:30:52 +00001445 assert(allLookupResultsAreTheSame(Invoker) &&
1446 "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001447 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001448 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001449 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001450
1451 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001452}
1453
Douglas Gregor9c702202012-02-10 07:45:31 +00001454void CXXRecordDecl::getCaptureFields(
1455 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001456 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001457 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001458 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001459
Douglas Gregorc8a73492012-02-13 15:44:47 +00001460 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001461 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001462 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001463 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001464 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001465 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001466 else if (C->capturesVariable())
1467 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001468 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001469 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001470}
1471
Fangrui Song6907ce22018-07-30 19:24:48 +00001472TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001473CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Hamza Sood8205a812019-05-04 10:49:46 +00001474 if (!isGenericLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001475 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001476 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1477 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001478 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001479}
Douglas Gregor9c702202012-02-10 07:45:31 +00001480
Hamza Sood8205a812019-05-04 10:49:46 +00001481ArrayRef<NamedDecl *>
1482CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1483 TemplateParameterList *List = getGenericLambdaTemplateParameterList();
1484 if (!List)
1485 return {};
1486
1487 assert(std::is_partitioned(List->begin(), List->end(),
1488 [](const NamedDecl *D) { return !D->isImplicit(); })
1489 && "Explicit template params should be ordered before implicit ones");
1490
Fangrui Song7264a472019-07-03 08:13:17 +00001491 const auto ExplicitEnd = llvm::partition_point(
1492 *List, [](const NamedDecl *D) { return !D->isImplicit(); });
Hamza Sood8205a812019-05-04 10:49:46 +00001493 return llvm::makeArrayRef(List->begin(), ExplicitEnd);
1494}
1495
Richard Smith0bae6242016-08-25 00:34:00 +00001496Decl *CXXRecordDecl::getLambdaContextDecl() const {
1497 assert(isLambda() && "Not a lambda closure type!");
1498 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1499 return getLambdaData().ContextDecl.get(Source);
1500}
1501
John McCall1e3a1a72010-03-15 09:07:48 +00001502static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001503 QualType T =
1504 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1505 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001506 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001507}
1508
John McCall1e3a1a72010-03-15 09:07:48 +00001509/// Collect the visible conversions of a base class.
1510///
James Dennettb5d5f812012-06-15 22:28:09 +00001511/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001512/// \param InVirtual whether this base class is a virtual base (or a base
1513/// of a virtual base)
1514/// \param Access the access along the inheritance path to this base
1515/// \param ParentHiddenTypes the conversions provided by the inheritors
1516/// of this base
1517/// \param Output the set to which to add conversions from non-virtual bases
1518/// \param VOutput the set to which to add conversions from virtual bases
1519/// \param HiddenVBaseCs the set of conversions which were hidden in a
1520/// virtual base along some inheritance path
Whisperity002e0722019-12-24 11:09:24 +01001521static void CollectVisibleConversions(
1522 ASTContext &Context, const CXXRecordDecl *Record, bool InVirtual,
1523 AccessSpecifier Access,
1524 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
1525 ASTUnresolvedSet &Output, UnresolvedSetImpl &VOutput,
1526 llvm::SmallPtrSet<NamedDecl *, 8> &HiddenVBaseCs) {
John McCall1e3a1a72010-03-15 09:07:48 +00001527 // The set of types which have conversions in this class or its
1528 // subclasses. As an optimization, we don't copy the derived set
1529 // unless it might change.
1530 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1531 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1532
1533 // Collect the direct conversions and figure out which conversions
1534 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001535 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1536 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1537 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001538 HiddenTypesBuffer = ParentHiddenTypes;
1539 HiddenTypes = &HiddenTypesBuffer;
1540
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001541 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001542 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1543 bool Hidden = ParentHiddenTypes.count(ConvType);
1544 if (!Hidden)
1545 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001546
1547 // If this conversion is hidden and we're in a virtual base,
1548 // remember that it's hidden along some inheritance path.
1549 if (Hidden && InVirtual)
1550 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1551
1552 // If this conversion isn't hidden, add it to the appropriate output.
1553 else if (!Hidden) {
1554 AccessSpecifier IAccess
1555 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1556
1557 if (InVirtual)
1558 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001559 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001560 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001561 }
1562 }
1563 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001564
John McCall1e3a1a72010-03-15 09:07:48 +00001565 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001566 for (const auto &I : Record->bases()) {
Whisperity002e0722019-12-24 11:09:24 +01001567 const auto *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001568 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001569
John McCall1e3a1a72010-03-15 09:07:48 +00001570 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001571 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1572 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001573
Eugene Zelenko7855e772018-04-03 00:11:50 +00001574 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001575 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1576 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001577 }
John McCall1e3a1a72010-03-15 09:07:48 +00001578}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001579
John McCall1e3a1a72010-03-15 09:07:48 +00001580/// Collect the visible conversions of a class.
1581///
1582/// This would be extremely straightforward if it weren't for virtual
1583/// bases. It might be worth special-casing that, really.
1584static void CollectVisibleConversions(ASTContext &Context,
Whisperity002e0722019-12-24 11:09:24 +01001585 const CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001586 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001587 // The collection of all conversions in virtual bases that we've
1588 // found. These will be added to the output as long as they don't
1589 // appear in the hidden-conversions set.
1590 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001591
John McCall1e3a1a72010-03-15 09:07:48 +00001592 // The set of conversions in virtual bases that we've determined to
1593 // be hidden.
1594 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1595
1596 // The set of types hidden by classes derived from this one.
1597 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1598
1599 // Go ahead and collect the direct conversions and add them to the
1600 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001601 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1602 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001603 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001604 for (; ConvI != ConvE; ++ConvI)
1605 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001606
1607 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001608 for (const auto &I : Record->bases()) {
Whisperity002e0722019-12-24 11:09:24 +01001609 const auto *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001610 if (!RT) continue;
1611
1612 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001613 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001614 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1615 }
1616
1617 // Add any unhidden conversions provided by virtual bases.
1618 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1619 I != E; ++I) {
1620 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001621 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001622 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001623}
1624
1625/// getVisibleConversionFunctions - get all conversion functions visible
1626/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001627llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Whisperity002e0722019-12-24 11:09:24 +01001628CXXRecordDecl::getVisibleConversionFunctions() const {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001629 ASTContext &Ctx = getASTContext();
1630
1631 ASTUnresolvedSet *Set;
1632 if (bases_begin() == bases_end()) {
1633 // If root class, all conversions are visible.
1634 Set = &data().Conversions.get(Ctx);
1635 } else {
1636 Set = &data().VisibleConversions.get(Ctx);
1637 // If visible conversion list is not evaluated, evaluate it.
1638 if (!data().ComputedVisibleConversions) {
1639 CollectVisibleConversions(Ctx, this, *Set);
1640 data().ComputedVisibleConversions = true;
1641 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001642 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001643 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001644}
1645
John McCallda4458e2010-03-31 01:36:47 +00001646void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1647 // This operation is O(N) but extremely rare. Sema only uses it to
1648 // remove UsingShadowDecls in a class that were followed by a direct
1649 // declaration, e.g.:
1650 // class A : B {
1651 // using B::operator int;
1652 // operator int();
1653 // };
1654 // This is uncommon by itself and even more uncommon in conjunction
1655 // with sufficiently large numbers of directly-declared conversions
1656 // that asymptotic behavior matters.
1657
Richard Smitha4ba74c2013-08-30 04:46:40 +00001658 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001659 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1660 if (Convs[I].getDecl() == ConvDecl) {
1661 Convs.erase(I);
Fangrui Song75e74e02019-03-31 08:48:19 +00001662 assert(llvm::find(Convs, ConvDecl) == Convs.end() &&
1663 "conversion was found multiple times in unresolved set");
John McCallda4458e2010-03-31 01:36:47 +00001664 return;
1665 }
1666 }
1667
1668 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001669}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001670
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001671CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001672 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001673 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001674
1675 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001676}
1677
Chandler Carruth21c90602015-12-30 03:24:14 +00001678MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1679 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1680}
1681
Fangrui Song6907ce22018-07-30 19:24:48 +00001682void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001683CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1684 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001685 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001686 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001687 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001688 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001689 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1690}
1691
Chandler Carruth21c90602015-12-30 03:24:14 +00001692ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1693 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1694}
1695
1696void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1697 TemplateOrInstantiation = Template;
1698}
1699
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001700TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001701 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001702 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001703
Douglas Gregor06db9f52009-10-12 20:18:28 +00001704 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001705 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001706
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001707 return TSK_Undeclared;
1708}
1709
Fangrui Song6907ce22018-07-30 19:24:48 +00001710void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001711CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001712 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001713 Spec->setSpecializationKind(TSK);
1714 return;
1715 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001716
Douglas Gregor06db9f52009-10-12 20:18:28 +00001717 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001718 MSInfo->setTemplateSpecializationKind(TSK);
1719 return;
1720 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001721
David Blaikie83d382b2011-09-23 05:06:16 +00001722 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001723}
1724
Reid Klecknere7367d62014-10-14 20:28:40 +00001725const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001726 auto GetDefinitionOrSelf =
1727 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1728 if (auto *Def = D->getDefinition())
1729 return Def;
1730 return D;
1731 };
1732
Reid Klecknere7367d62014-10-14 20:28:40 +00001733 // If it's a class template specialization, find the template or partial
1734 // specialization from which it was instantiated.
1735 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1736 auto From = TD->getInstantiatedFrom();
1737 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1738 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1739 if (NewCTD->isMemberSpecialization())
1740 break;
1741 CTD = NewCTD;
1742 }
Richard Smith2195ec92017-04-21 01:15:13 +00001743 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001744 }
1745 if (auto *CTPSD =
1746 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1747 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1748 if (NewCTPSD->isMemberSpecialization())
1749 break;
1750 CTPSD = NewCTPSD;
1751 }
Richard Smith2195ec92017-04-21 01:15:13 +00001752 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001753 }
1754 }
1755
1756 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1757 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1758 const CXXRecordDecl *RD = this;
1759 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1760 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001761 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001762 }
1763 }
1764
1765 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1766 "couldn't find pattern for class template instantiation");
1767 return nullptr;
1768}
1769
Douglas Gregorbac74902010-07-01 14:13:13 +00001770CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1771 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001772 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001773
1774 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001775 = Context.DeclarationNames.getCXXDestructorName(
1776 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001777
Richard Smithcf4bdde2015-02-21 02:45:19 +00001778 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001779
Hubert Tong3cede512017-06-30 22:43:54 +00001780 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001781}
1782
Richard Trieu95a192a2015-05-28 00:14:02 +00001783bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1784 // Destructor is noreturn.
1785 if (const CXXDestructorDecl *Destructor = getDestructor())
1786 if (Destructor->isNoReturn())
1787 return true;
1788
1789 // Check base classes destructor for noreturn.
1790 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001791 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1792 if (RD->isAnyDestructorNoReturn())
1793 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001794
1795 // Check fields for noreturn.
1796 for (const auto *Field : fields())
1797 if (const CXXRecordDecl *RD =
1798 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1799 if (RD->isAnyDestructorNoReturn())
1800 return true;
1801
1802 // All destructors are not noreturn.
1803 return false;
1804}
1805
Erich Keane3e08f662017-09-29 21:06:00 +00001806static bool isDeclContextInNamespace(const DeclContext *DC) {
1807 while (!DC->isTranslationUnit()) {
1808 if (DC->isNamespace())
1809 return true;
1810 DC = DC->getParent();
1811 }
1812 return false;
1813}
1814
Erich Keane58bd6032017-09-15 16:03:35 +00001815bool CXXRecordDecl::isInterfaceLike() const {
1816 assert(hasDefinition() && "checking for interface-like without a definition");
1817 // All __interfaces are inheritently interface-like.
1818 if (isInterface())
1819 return true;
1820
1821 // Interface-like types cannot have a user declared constructor, destructor,
1822 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1823 // cannot be interface types.
1824 if (isLambda() || hasUserDeclaredConstructor() ||
1825 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1826 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1827 return false;
1828
1829 // No interface-like type can have a method with a definition.
1830 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001831 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001832 return false;
1833
1834 // Check "Special" types.
1835 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001836 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1837 // extern C++ block directly in the TU. These are only valid if in one
1838 // of these two situations.
1839 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1840 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001841 ((getName() == "IUnknown" &&
1842 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1843 (getName() == "IDispatch" &&
1844 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1845 if (getNumBases() > 0)
1846 return false;
1847 return true;
1848 }
1849
1850 // FIXME: Any access specifiers is supposed to make this no longer interface
1851 // like.
1852
1853 // If this isn't a 'special' type, it must have a single interface-like base.
1854 if (getNumBases() != 1)
1855 return false;
1856
1857 const auto BaseSpec = *bases_begin();
1858 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1859 return false;
1860 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1861 if (Base->isInterface() || !Base->isInterfaceLike())
1862 return false;
1863 return true;
1864}
1865
Douglas Gregorb11aad82011-02-19 18:51:44 +00001866void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001867 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001868}
1869
1870void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1871 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001872
Douglas Gregor8fb95122010-09-29 00:15:42 +00001873 // If the class may be abstract (but hasn't been marked as such), check for
1874 // any pure final overriders.
1875 if (mayBeAbstract()) {
1876 CXXFinalOverriderMap MyFinalOverriders;
1877 if (!FinalOverriders) {
1878 getFinalOverriders(MyFinalOverriders);
1879 FinalOverriders = &MyFinalOverriders;
1880 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001881
Douglas Gregor8fb95122010-09-29 00:15:42 +00001882 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001883 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001884 MEnd = FinalOverriders->end();
1885 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001886 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001887 SOEnd = M->second.end();
1888 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001889 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001890 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001891
Douglas Gregor8fb95122010-09-29 00:15:42 +00001892 // C++ [class.abstract]p4:
1893 // A class is abstract if it contains or inherits at least one
1894 // pure virtual function for which the final overrider is pure
1895 // virtual.
1896 if (SO->second.front().Method->isPure()) {
1897 data().Abstract = true;
1898 Done = true;
1899 break;
1900 }
1901 }
1902 }
1903 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001904
Douglas Gregor457104e2010-09-29 04:25:11 +00001905 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001906 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001907 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001908 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001909}
1910
1911bool CXXRecordDecl::mayBeAbstract() const {
1912 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1913 isDependentContext())
1914 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001915
Aaron Ballman574705e2014-03-13 15:41:46 +00001916 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001917 const auto *BaseDecl =
Simon Pilgrim1cd399c2019-10-03 11:22:48 +00001918 cast<CXXRecordDecl>(B.getType()->castAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001919 if (BaseDecl->isAbstract())
1920 return true;
1921 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001922
Douglas Gregor8fb95122010-09-29 00:15:42 +00001923 return false;
1924}
1925
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001926void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001927
Richard Smith76b90272019-05-09 03:59:21 +00001928bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other) const {
1929 if ((getKind() != Other.getKind() ||
1930 getKind() == ExplicitSpecKind::Unresolved)) {
1931 if (getKind() == ExplicitSpecKind::Unresolved &&
1932 Other.getKind() == ExplicitSpecKind::Unresolved) {
1933 ODRHash SelfHash, OtherHash;
1934 SelfHash.AddStmt(getExpr());
1935 OtherHash.AddStmt(Other.getExpr());
1936 return SelfHash.CalculateHash() == OtherHash.CalculateHash();
1937 } else
1938 return false;
1939 }
1940 return true;
1941}
1942
1943ExplicitSpecifier ExplicitSpecifier::getFromDecl(FunctionDecl *Function) {
1944 switch (Function->getDeclKind()) {
1945 case Decl::Kind::CXXConstructor:
1946 return cast<CXXConstructorDecl>(Function)->getExplicitSpecifier();
1947 case Decl::Kind::CXXConversion:
1948 return cast<CXXConversionDecl>(Function)->getExplicitSpecifier();
1949 case Decl::Kind::CXXDeductionGuide:
1950 return cast<CXXDeductionGuideDecl>(Function)->getExplicitSpecifier();
1951 default:
1952 return {};
1953 }
1954}
1955
Richard Smithbc491202017-02-17 20:05:37 +00001956CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
Richard Smith76b90272019-05-09 03:59:21 +00001957 ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1958 ExplicitSpecifier ES, const DeclarationNameInfo &NameInfo, QualType T,
1959 TypeSourceInfo *TInfo, SourceLocation EndLocation) {
1960 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, ES, NameInfo, T,
1961 TInfo, EndLocation);
Richard Smithbc491202017-02-17 20:05:37 +00001962}
1963
1964CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1965 unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00001966 return new (C, ID) CXXDeductionGuideDecl(
1967 C, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
1968 QualType(), nullptr, SourceLocation());
Richard Smithbc491202017-02-17 20:05:37 +00001969}
1970
Saar Raza0f50d72020-01-18 09:11:43 +02001971RequiresExprBodyDecl *RequiresExprBodyDecl::Create(
1972 ASTContext &C, DeclContext *DC, SourceLocation StartLoc) {
1973 return new (C, DC) RequiresExprBodyDecl(C, DC, StartLoc);
1974}
1975
1976RequiresExprBodyDecl *RequiresExprBodyDecl::CreateDeserialized(ASTContext &C,
1977 unsigned ID) {
1978 return new (C, ID) RequiresExprBodyDecl(C, nullptr, SourceLocation());
1979}
1980
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001981void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001982
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001983bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001984 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001985
1986 if (MD->getStorageClass() == SC_Static)
1987 return true;
1988
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001989 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1990 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001991}
1992
Rafael Espindola49e860b2012-06-26 17:45:31 +00001993static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1994 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001995 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001996 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1997 return true;
1998 if (recursivelyOverrides(MD, BaseMD))
1999 return true;
2000 }
2001 return false;
2002}
2003
2004CXXMethodDecl *
Richard Smith921f1322019-05-13 23:35:21 +00002005CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
2006 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00002007 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
2008 return this;
2009
2010 // Lookup doesn't work for destructors, so handle them separately.
2011 if (isa<CXXDestructorDecl>(this)) {
2012 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00002013 if (MD) {
2014 if (recursivelyOverrides(MD, this))
2015 return MD;
2016 if (MayBeBase && recursivelyOverrides(this, MD))
2017 return MD;
2018 }
Craig Topper36250ad2014-05-12 05:36:57 +00002019 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002020 }
2021
Richard Smith40c78062015-02-21 02:31:57 +00002022 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002023 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00002024 if (!MD)
2025 continue;
2026 if (recursivelyOverrides(MD, this))
2027 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00002028 if (MayBeBase && recursivelyOverrides(this, MD))
2029 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002030 }
2031
Richard Smith921f1322019-05-13 23:35:21 +00002032 return nullptr;
2033}
2034
2035CXXMethodDecl *
2036CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2037 bool MayBeBase) {
2038 if (auto *MD = getCorrespondingMethodDeclaredInClass(RD, MayBeBase))
2039 return MD;
2040
Aaron Ballman574705e2014-03-13 15:41:46 +00002041 for (const auto &I : RD->bases()) {
2042 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00002043 if (!RT)
2044 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002045 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00002046 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
2047 if (T)
2048 return T;
2049 }
2050
Craig Topper36250ad2014-05-12 05:36:57 +00002051 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002052}
2053
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002054CXXMethodDecl *CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
2055 SourceLocation StartLoc,
2056 const DeclarationNameInfo &NameInfo,
2057 QualType T, TypeSourceInfo *TInfo,
2058 StorageClass SC, bool isInline,
2059 ConstexprSpecKind ConstexprKind,
Saar Razb65b1f32020-01-09 15:07:51 +02002060 SourceLocation EndLocation,
2061 Expr *TrailingRequiresClause) {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002062 return new (C, RD)
2063 CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo, T, TInfo, SC,
Saar Razb65b1f32020-01-09 15:07:51 +02002064 isInline, ConstexprKind, EndLocation,
2065 TrailingRequiresClause);
Ted Kremenek21475702008-09-05 17:16:31 +00002066}
2067
Douglas Gregor72172e92012-01-05 21:55:30 +00002068CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002069 return new (C, ID) CXXMethodDecl(
2070 CXXMethod, C, nullptr, SourceLocation(), DeclarationNameInfo(),
Saar Razb65b1f32020-01-09 15:07:51 +02002071 QualType(), nullptr, SC_None, false, CSK_unspecified, SourceLocation(),
2072 nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002073}
2074
Akira Hatanaka22461672017-07-13 06:08:27 +00002075CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
2076 bool IsAppleKext) {
2077 assert(isVirtual() && "this method is expected to be virtual");
2078
2079 // When building with -fapple-kext, all calls must go through the vtable since
2080 // the kernel linker can do runtime patching of vtables.
2081 if (IsAppleKext)
2082 return nullptr;
2083
2084 // If the member function is marked 'final', we know that it can't be
2085 // overridden and can therefore devirtualize it unless it's pure virtual.
2086 if (hasAttr<FinalAttr>())
2087 return isPure() ? nullptr : this;
2088
2089 // If Base is unknown, we cannot devirtualize.
2090 if (!Base)
2091 return nullptr;
2092
2093 // If the base expression (after skipping derived-to-base conversions) is a
2094 // class prvalue, then we can devirtualize.
2095 Base = Base->getBestDynamicClassTypeExpr();
2096 if (Base->isRValue() && Base->getType()->isRecordType())
2097 return this;
2098
2099 // If we don't even know what we would call, we can't devirtualize.
2100 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
2101 if (!BestDynamicDecl)
2102 return nullptr;
2103
2104 // There may be a method corresponding to MD in a derived class.
2105 CXXMethodDecl *DevirtualizedMethod =
2106 getCorrespondingMethodInClass(BestDynamicDecl);
2107
2108 // If that method is pure virtual, we can't devirtualize. If this code is
2109 // reached, the result would be UB, not a direct call to the derived class
2110 // function, and we can't assume the derived class function is defined.
2111 if (DevirtualizedMethod->isPure())
2112 return nullptr;
2113
2114 // If that method is marked final, we can devirtualize it.
2115 if (DevirtualizedMethod->hasAttr<FinalAttr>())
2116 return DevirtualizedMethod;
2117
David Bolvansky20b27082019-08-31 18:52:44 +00002118 // Similarly, if the class itself or its destructor is marked 'final',
2119 // the class can't be derived from and we can therefore devirtualize the
2120 // member function call.
Akira Hatanaka22461672017-07-13 06:08:27 +00002121 if (BestDynamicDecl->hasAttr<FinalAttr>())
2122 return DevirtualizedMethod;
David Bolvansky20b27082019-08-31 18:52:44 +00002123 if (const auto *dtor = BestDynamicDecl->getDestructor()) {
2124 if (dtor->hasAttr<FinalAttr>())
2125 return DevirtualizedMethod;
2126 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002127
Eugene Zelenko7855e772018-04-03 00:11:50 +00002128 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2129 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00002130 if (VD->getType()->isRecordType())
2131 // This is a record decl. We know the type and can devirtualize it.
2132 return DevirtualizedMethod;
2133
2134 return nullptr;
2135 }
2136
2137 // We can devirtualize calls on an object accessed by a class member access
2138 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2139 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002140 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002141 const ValueDecl *VD = ME->getMemberDecl();
2142 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2143 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002144
2145 // Likewise for calls on an object accessed by a (non-reference) pointer to
2146 // member access.
2147 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2148 if (BO->isPtrMemOp()) {
2149 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2150 if (MPT->getPointeeType()->isRecordType())
2151 return DevirtualizedMethod;
2152 }
2153 }
2154
2155 // We can't devirtualize the call.
2156 return nullptr;
2157}
2158
Artem Belevich78929ef2018-09-21 17:29:33 +00002159bool CXXMethodDecl::isUsualDeallocationFunction(
2160 SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2161 assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002162 if (getOverloadedOperator() != OO_Delete &&
2163 getOverloadedOperator() != OO_Array_Delete)
2164 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002165
2166 // C++ [basic.stc.dynamic.deallocation]p2:
2167 // A template instance is never a usual deallocation function,
2168 // regardless of its signature.
2169 if (getPrimaryTemplate())
2170 return false;
2171
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002172 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002173 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002174 // with exactly one parameter, then that function is a usual (non-placement)
2175 // deallocation function. [...]
2176 if (getNumParams() == 1)
2177 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002178 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002179
Richard Smith5b349582017-10-13 01:55:36 +00002180 // C++ P0722:
2181 // A destroying operator delete is a usual deallocation function if
2182 // removing the std::destroying_delete_t parameter and changing the
2183 // first parameter type from T* to void* results in the signature of
2184 // a usual deallocation function.
2185 if (isDestroyingOperatorDelete())
2186 ++UsualParams;
2187
Richard Smithb2f0f052016-10-10 18:54:32 +00002188 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002189 // [...] If class T does not declare such an operator delete but does
2190 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002191 // exactly two parameters, the second of which has type std::size_t (18.1),
2192 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002193 //
2194 // C++17 says a usual deallocation function is one with the signature
2195 // (void* [, size_t] [, std::align_val_t] [, ...])
2196 // and all such functions are usual deallocation functions. It's not clear
2197 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002198 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002199 if (UsualParams < getNumParams() &&
2200 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2201 Context.getSizeType()))
2202 ++UsualParams;
2203
2204 if (UsualParams < getNumParams() &&
2205 getParamDecl(UsualParams)->getType()->isAlignValT())
2206 ++UsualParams;
2207
2208 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002209 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002210
2211 // In C++17 onwards, all potential usual deallocation functions are actual
Eric Fiselier24a2a482019-02-03 03:44:31 +00002212 // usual deallocation functions. Honor this behavior when post-C++14
2213 // deallocation functions are offered as extensions too.
2214 // FIXME(EricWF): Destrying Delete should be a language option. How do we
2215 // handle when destroying delete is used prior to C++17?
2216 if (Context.getLangOpts().CPlusPlus17 ||
2217 Context.getLangOpts().AlignedAllocation ||
2218 isDestroyingOperatorDelete())
Richard Smithb2f0f052016-10-10 18:54:32 +00002219 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002220
2221 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002222 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002223 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
Artem Belevich78929ef2018-09-21 17:29:33 +00002224 bool Result = true;
2225 for (const auto *D : R) {
2226 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2227 if (FD->getNumParams() == 1) {
2228 PreventedBy.push_back(FD);
2229 Result = false;
2230 }
2231 }
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002232 }
Artem Belevich78929ef2018-09-21 17:29:33 +00002233 return Result;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002234}
2235
Douglas Gregorb139cd52010-05-01 20:49:11 +00002236bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002237 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002238 // A user-declared copy assignment operator X::operator= is a non-static
2239 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002240 // type X, X&, const X&, volatile X& or const volatile X&.
2241 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002242 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002243 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2244 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002245 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002246
Douglas Gregorb139cd52010-05-01 20:49:11 +00002247 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002248 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002249 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002250
Douglas Gregorb139cd52010-05-01 20:49:11 +00002251 ASTContext &Context = getASTContext();
2252 QualType ClassType
2253 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2254 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2255}
2256
Alexis Huntfcaeae42011-05-25 20:50:04 +00002257bool CXXMethodDecl::isMoveAssignmentOperator() const {
2258 // C++0x [class.copy]p19:
2259 // A user-declared move assignment operator X::operator= is a non-static
2260 // non-template member function of class X with exactly one parameter of type
2261 // X&&, const X&&, volatile X&&, or const volatile X&&.
2262 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002263 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2264 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002265 return false;
2266
2267 QualType ParamType = getParamDecl(0)->getType();
2268 if (!isa<RValueReferenceType>(ParamType))
2269 return false;
2270 ParamType = ParamType->getPointeeType();
2271
2272 ASTContext &Context = getASTContext();
2273 QualType ClassType
2274 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2275 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2276}
2277
Anders Carlsson36d87e12009-05-16 23:58:37 +00002278void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002279 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002280 assert(!MD->getParent()->isDependentContext() &&
2281 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002282 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002283
Douglas Gregor832940b2010-03-02 23:58:15 +00002284 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002285}
2286
2287CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002288 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002289 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002290}
2291
2292CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002293 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002294 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002295}
2296
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002297unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002298 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002299 return getASTContext().overridden_methods_size(this);
2300}
2301
Clement Courbet6ecaec82016-07-05 07:49:31 +00002302CXXMethodDecl::overridden_method_range
2303CXXMethodDecl::overridden_methods() const {
2304 if (isa<CXXConstructorDecl>(this))
2305 return overridden_method_range(nullptr, nullptr);
2306 return getASTContext().overridden_methods(this);
2307}
2308
Marco Antognini88559632019-07-22 09:39:13 +00002309static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2310 const CXXRecordDecl *Decl) {
2311 QualType ClassTy = C.getTypeDeclType(Decl);
2312 return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2313}
2314
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002315QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2316 const CXXRecordDecl *Decl) {
2317 ASTContext &C = Decl->getASTContext();
Marco Antognini88559632019-07-22 09:39:13 +00002318 QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2319 return C.getPointerType(ObjectTy);
2320}
2321
2322QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2323 const CXXRecordDecl *Decl) {
2324 ASTContext &C = Decl->getASTContext();
2325 return ::getThisObjectType(C, FPT, Decl);
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002326}
2327
Brian Gesiak5488ab42019-01-11 01:54:53 +00002328QualType CXXMethodDecl::getThisType() const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002329 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2330 // If the member function is declared const, the type of this is const X*,
2331 // if the member function is declared volatile, the type of this is
2332 // volatile X*, and if the member function is declared const volatile,
2333 // the type of this is const volatile X*.
Ted Kremenek21475702008-09-05 17:16:31 +00002334 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002335
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002336 return CXXMethodDecl::getThisType(getType()->getAs<FunctionProtoType>(),
2337 getParent());
Ted Kremenek21475702008-09-05 17:16:31 +00002338}
2339
Marco Antognini88559632019-07-22 09:39:13 +00002340QualType CXXMethodDecl::getThisObjectType() const {
2341 // Ditto getThisType.
2342 assert(isInstance() && "No 'this' for static methods!");
2343
2344 return CXXMethodDecl::getThisObjectType(getType()->getAs<FunctionProtoType>(),
2345 getParent());
2346}
2347
Eli Friedman71a26d82009-12-06 20:50:05 +00002348bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002349 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002350 // which it was instantiated.
2351 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2352 if (!CheckFn)
2353 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002354
Eli Friedman71a26d82009-12-06 20:50:05 +00002355 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002356 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2357 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002358}
2359
Douglas Gregor355efbb2012-02-17 03:02:34 +00002360bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002361 const CXXRecordDecl *P = getParent();
2362 if (P->isLambda()) {
2363 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2364 if (StaticInvoker == this) return true;
2365 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2366 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2367 }
2368 }
2369 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002370}
2371
Alexis Hunt1d792652011-01-08 20:30:50 +00002372CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2373 TypeSourceInfo *TInfo, bool IsVirtual,
2374 SourceLocation L, Expr *Init,
2375 SourceLocation R,
2376 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002377 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2378 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2379 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002380
Alexis Hunt1d792652011-01-08 20:30:50 +00002381CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2382 FieldDecl *Member,
2383 SourceLocation MemberLoc,
2384 SourceLocation L, Expr *Init,
2385 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002386 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2387 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2388 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002389
Alexis Hunt1d792652011-01-08 20:30:50 +00002390CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2391 IndirectFieldDecl *Member,
2392 SourceLocation MemberLoc,
2393 SourceLocation L, Expr *Init,
2394 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002395 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2396 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2397 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002398
Alexis Hunt1d792652011-01-08 20:30:50 +00002399CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002400 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002401 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002402 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002403 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2404 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002405
Artem Dergachevd3942be2018-12-03 22:15:34 +00002406int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +00002407 return Context.getAllocator()
2408 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
Artem Dergachevd3942be2018-12-03 22:15:34 +00002409}
2410
Alexis Hunt1d792652011-01-08 20:30:50 +00002411TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002412 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002413 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002414 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002415 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002416}
2417
Alexis Hunt1d792652011-01-08 20:30:50 +00002418const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002419 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002420 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002421 else
Craig Topper36250ad2014-05-12 05:36:57 +00002422 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002423}
2424
Alexis Hunt1d792652011-01-08 20:30:50 +00002425SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002426 if (isInClassMemberInitializer())
2427 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002428
David Blaikiea81d4102015-01-18 00:12:58 +00002429 if (isAnyMemberInitializer())
2430 return getMemberLocation();
2431
Eugene Zelenko7855e772018-04-03 00:11:50 +00002432 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002433 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002434
Eugene Zelenko7855e772018-04-03 00:11:50 +00002435 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002436}
2437
Alexis Hunt1d792652011-01-08 20:30:50 +00002438SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002439 if (isInClassMemberInitializer()) {
2440 FieldDecl *D = getAnyMember();
2441 if (Expr *I = D->getInClassInitializer())
2442 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002443 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002444 }
2445
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002446 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002447}
2448
Erich Keane9c665062018-08-01 21:02:40 +00002449CXXConstructorDecl::CXXConstructorDecl(
2450 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2451 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Richard Smith76b90272019-05-09 03:59:21 +00002452 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Saar Razb65b1f32020-01-09 15:07:51 +02002453 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited,
2454 Expr *TrailingRequiresClause)
Erich Keane9c665062018-08-01 21:02:40 +00002455 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
Saar Razb65b1f32020-01-09 15:07:51 +02002456 SC_None, isInline, ConstexprKind, SourceLocation(),
2457 TrailingRequiresClause) {
Erich Keane9c665062018-08-01 21:02:40 +00002458 setNumCtorInitializers(0);
2459 setInheritingConstructor(static_cast<bool>(Inherited));
2460 setImplicit(isImplicitlyDeclared);
Richard Smith76b90272019-05-09 03:59:21 +00002461 CXXConstructorDeclBits.HasTrailingExplicitSpecifier = ES.getExpr() ? 1 : 0;
Erich Keane9c665062018-08-01 21:02:40 +00002462 if (Inherited)
2463 *getTrailingObjects<InheritedConstructor>() = Inherited;
Richard Smith76b90272019-05-09 03:59:21 +00002464 setExplicitSpecifier(ES);
Erich Keane9c665062018-08-01 21:02:40 +00002465}
2466
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002467void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002468
Richard Smith5179eb72016-06-28 19:03:57 +00002469CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2470 unsigned ID,
Richard Smith76b90272019-05-09 03:59:21 +00002471 uint64_t AllocKind) {
2472 bool hasTraillingExplicit = static_cast<bool>(AllocKind & TAKHasTailExplicit);
2473 bool isInheritingConstructor =
2474 static_cast<bool>(AllocKind & TAKInheritsConstructor);
2475 unsigned Extra =
2476 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2477 isInheritingConstructor, hasTraillingExplicit);
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002478 auto *Result = new (C, ID, Extra)
2479 CXXConstructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2480 QualType(), nullptr, ExplicitSpecifier(), false, false,
Saar Razb65b1f32020-01-09 15:07:51 +02002481 CSK_unspecified, InheritedConstructor(), nullptr);
Richard Smith76b90272019-05-09 03:59:21 +00002482 Result->setInheritingConstructor(isInheritingConstructor);
2483 Result->CXXConstructorDeclBits.HasTrailingExplicitSpecifier =
2484 hasTraillingExplicit;
2485 Result->setExplicitSpecifier(ExplicitSpecifier());
Richard Smith5179eb72016-06-28 19:03:57 +00002486 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002487}
2488
Richard Smith76b90272019-05-09 03:59:21 +00002489CXXConstructorDecl *CXXConstructorDecl::Create(
2490 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2491 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2492 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Saar Razb65b1f32020-01-09 15:07:51 +02002493 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited,
2494 Expr *TrailingRequiresClause) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002495 assert(NameInfo.getName().getNameKind()
2496 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002497 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002498 unsigned Extra =
Richard Smith76b90272019-05-09 03:59:21 +00002499 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2500 Inherited ? 1 : 0, ES.getExpr() ? 1 : 0);
2501 return new (C, RD, Extra)
2502 CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, ES, isInline,
Saar Razb65b1f32020-01-09 15:07:51 +02002503 isImplicitlyDeclared, ConstexprKind, Inherited,
2504 TrailingRequiresClause);
Douglas Gregor61956c42008-10-31 09:07:45 +00002505}
2506
Richard Smithc2bb8182015-03-24 06:36:48 +00002507CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2508 return CtorInitializers.get(getASTContext().getExternalSource());
2509}
2510
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002511CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2512 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2513 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002514 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002515 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002516
2517 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002518}
2519
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002520bool CXXConstructorDecl::isDefaultConstructor() const {
2521 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002522 // A default constructor for a class X is a constructor of class
2523 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002524 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002525 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002526}
2527
Mike Stump11289f42009-09-09 15:08:12 +00002528bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002529CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002530 return isCopyOrMoveConstructor(TypeQuals) &&
2531 getParamDecl(0)->getType()->isLValueReferenceType();
2532}
2533
2534bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2535 return isCopyOrMoveConstructor(TypeQuals) &&
2536 getParamDecl(0)->getType()->isRValueReferenceType();
2537}
2538
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002539/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002540bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002541 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002542 // A non-template constructor for class X is a copy constructor
2543 // if its first parameter is of type X&, const X&, volatile X& or
2544 // const volatile X&, and either there are no other parameters
2545 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002546 // C++0x [class.copy]p3:
2547 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002548 // first parameter is of type X&&, const X&&, volatile X&&, or
2549 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002550 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002551 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002552 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002553 (getPrimaryTemplate() != nullptr) ||
2554 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002555 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002556
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002557 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002558
2559 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002560 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002561 if (!ParamRefType)
2562 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002563
Douglas Gregorff7028a2009-11-13 23:59:09 +00002564 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002565 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002566
Douglas Gregorff7028a2009-11-13 23:59:09 +00002567 CanQualType PointeeType
2568 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002569 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002570 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002571 if (PointeeType.getUnqualifiedType() != ClassTy)
2572 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002573
John McCall8ccfcb52009-09-24 19:53:00 +00002574 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002575
Douglas Gregorf282a762011-01-21 19:38:21 +00002576 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002577 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002578 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002579}
2580
Anders Carlssond20e7952009-08-28 16:57:08 +00002581bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002582 // C++ [class.conv.ctor]p1:
2583 // A constructor declared without the function-specifier explicit
2584 // that can be called with a single parameter specifies a
2585 // conversion from the type of its first parameter to the type of
2586 // its class. Such a constructor is called a converting
2587 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002588 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002589 return false;
2590
Mike Stump11289f42009-09-09 15:08:12 +00002591 return (getNumParams() == 0 &&
Simon Pilgrim86976c92019-10-03 16:58:01 +00002592 getType()->castAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002593 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002594 (getNumParams() > 1 &&
2595 (getParamDecl(1)->hasDefaultArg() ||
2596 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002597}
Douglas Gregor61956c42008-10-31 09:07:45 +00002598
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002599bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002600 if ((getNumParams() < 1) ||
2601 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002602 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002603 return false;
2604
2605 const ParmVarDecl *Param = getParamDecl(0);
2606
2607 ASTContext &Context = getASTContext();
2608 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002609
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002610 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002611 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002612 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2613 if (ParamType.getUnqualifiedType() != ClassTy)
2614 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002615
2616 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002617}
2618
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002619void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002620
Douglas Gregor831c93f2008-11-05 20:51:48 +00002621CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002622CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002623 return new (C, ID)
2624 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
Saar Razb65b1f32020-01-09 15:07:51 +02002625 QualType(), nullptr, false, false, CSK_unspecified,
2626 nullptr);
Chris Lattnerca025db2010-05-07 21:43:38 +00002627}
2628
Richard Smith457226e2019-09-23 03:48:44 +00002629CXXDestructorDecl *CXXDestructorDecl::Create(
2630 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2631 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Saar Razb65b1f32020-01-09 15:07:51 +02002632 bool isInline, bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind,
2633 Expr *TrailingRequiresClause) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002634 assert(NameInfo.getName().getNameKind()
2635 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002636 "Name must refer to a destructor");
Richard Smith457226e2019-09-23 03:48:44 +00002637 return new (C, RD)
2638 CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline,
Saar Razb65b1f32020-01-09 15:07:51 +02002639 isImplicitlyDeclared, ConstexprKind,
2640 TrailingRequiresClause);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002641}
2642
Richard Smith5b349582017-10-13 01:55:36 +00002643void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002644 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2645 if (OD && !First->OperatorDelete) {
2646 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002647 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002648 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002649 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002650 }
2651}
2652
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002653void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002654
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002655CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002656CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00002657 return new (C, ID) CXXConversionDecl(
2658 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
Saar Razb65b1f32020-01-09 15:07:51 +02002659 false, ExplicitSpecifier(), CSK_unspecified, SourceLocation(), nullptr);
Chris Lattnerca025db2010-05-07 21:43:38 +00002660}
2661
Richard Smith76b90272019-05-09 03:59:21 +00002662CXXConversionDecl *CXXConversionDecl::Create(
2663 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2664 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002665 bool isInline, ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind,
Saar Razb65b1f32020-01-09 15:07:51 +02002666 SourceLocation EndLocation, Expr *TrailingRequiresClause) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002667 assert(NameInfo.getName().getNameKind()
2668 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002669 "Name must refer to a conversion function");
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002670 return new (C, RD)
2671 CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline, ES,
Saar Razb65b1f32020-01-09 15:07:51 +02002672 ConstexprKind, EndLocation, TrailingRequiresClause);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002673}
2674
Douglas Gregord3b672c2012-02-16 01:06:16 +00002675bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2676 return isImplicit() && getParent()->isLambda() &&
2677 getConversionType()->isBlockPointerType();
2678}
2679
Erich Keanec9d29902018-08-01 21:16:54 +00002680LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2681 SourceLocation LangLoc, LanguageIDs lang,
2682 bool HasBraces)
2683 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2684 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2685 setLanguage(lang);
2686 LinkageSpecDeclBits.HasBraces = HasBraces;
2687}
2688
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002689void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002690
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002691LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002692 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002693 SourceLocation ExternLoc,
2694 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002695 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002696 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002697 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002698}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002699
Richard Smithf7981722013-11-22 09:01:48 +00002700LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2701 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002702 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2703 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002704}
2705
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002706void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002707
Douglas Gregor889ceb72009-02-03 19:21:40 +00002708UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2709 SourceLocation L,
2710 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002711 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002712 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002713 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002714 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002715 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002716 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002717 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2718 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002719}
2720
Richard Smithf7981722013-11-22 09:01:48 +00002721UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2722 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002723 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2724 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002725 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002726 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002727}
2728
Sebastian Redla6602e92009-11-23 15:34:23 +00002729NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002730 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002731 return NA->getNamespace();
2732 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2733}
2734
Richard Smith053f6c62014-05-16 23:01:30 +00002735NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2736 SourceLocation StartLoc, SourceLocation IdLoc,
2737 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2738 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002739 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002740 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002741 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002742
Douglas Gregore57e7522012-01-07 09:11:48 +00002743 if (PrevDecl)
2744 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2745}
2746
Douglas Gregor72172e92012-01-05 21:55:30 +00002747NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002748 bool Inline, SourceLocation StartLoc,
2749 SourceLocation IdLoc, IdentifierInfo *Id,
2750 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002751 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2752 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002753}
2754
2755NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002756 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002757 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002758}
2759
Chandler Carruth21c90602015-12-30 03:24:14 +00002760NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2761 if (isFirstDecl())
2762 return this;
2763
2764 return AnonOrFirstNamespaceAndInline.getPointer();
2765}
2766
2767const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2768 if (isFirstDecl())
2769 return this;
2770
2771 return AnonOrFirstNamespaceAndInline.getPointer();
2772}
2773
2774bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2775
Richard Smithd7af8a32014-05-10 01:17:36 +00002776NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2777 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002778}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002779
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002780NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2781 return getPreviousDecl();
2782}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002783
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002784NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2785 return getMostRecentDecl();
2786}
2787
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002788void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002789
Richard Smithf4634362014-09-03 23:11:22 +00002790NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2791 return getNextRedeclaration();
2792}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002793
Richard Smithf4634362014-09-03 23:11:22 +00002794NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2795 return getPreviousDecl();
2796}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002797
Richard Smithf4634362014-09-03 23:11:22 +00002798NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2799 return getMostRecentDecl();
2800}
2801
Mike Stump11289f42009-09-09 15:08:12 +00002802NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002803 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002804 SourceLocation AliasLoc,
2805 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002806 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002807 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002808 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002809 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002810 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002811 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002812 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002813 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002814}
2815
Douglas Gregor72172e92012-01-05 21:55:30 +00002816NamespaceAliasDecl *
2817NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002818 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002819 SourceLocation(), nullptr,
2820 NestedNameSpecifierLoc(),
2821 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002822}
2823
Tykerb0561b32019-11-17 11:41:55 +01002824void LifetimeExtendedTemporaryDecl::anchor() {}
2825
2826/// Retrieve the storage duration for the materialized temporary.
2827StorageDuration LifetimeExtendedTemporaryDecl::getStorageDuration() const {
2828 const ValueDecl *ExtendingDecl = getExtendingDecl();
2829 if (!ExtendingDecl)
2830 return SD_FullExpression;
2831 // FIXME: This is not necessarily correct for a temporary materialized
2832 // within a default initializer.
2833 if (isa<FieldDecl>(ExtendingDecl))
2834 return SD_Automatic;
2835 // FIXME: This only works because storage class specifiers are not allowed
2836 // on decomposition declarations.
2837 if (isa<BindingDecl>(ExtendingDecl))
2838 return ExtendingDecl->getDeclContext()->isFunctionOrMethod() ? SD_Automatic
2839 : SD_Static;
2840 return cast<VarDecl>(ExtendingDecl)->getStorageDuration();
2841}
2842
2843APValue *LifetimeExtendedTemporaryDecl::getOrCreateValue(bool MayCreate) const {
2844 assert(getStorageDuration() == SD_Static &&
2845 "don't need to cache the computed value for this temporary");
Tykerc444a012019-11-19 21:05:30 +01002846 if (MayCreate && !Value) {
Tykerb0561b32019-11-17 11:41:55 +01002847 Value = (new (getASTContext()) APValue);
Tykerc444a012019-11-19 21:05:30 +01002848 getASTContext().addDestruction(Value);
2849 }
Tykerb0561b32019-11-17 11:41:55 +01002850 assert(Value && "may not be null");
2851 return Value;
2852}
2853
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002854void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002855
Richard Smith5179eb72016-06-28 19:03:57 +00002856UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2857 SourceLocation Loc, UsingDecl *Using,
2858 NamedDecl *Target)
2859 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002860 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002861 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002862 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002863 setImplicit();
2864}
2865
2866UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2867 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002868 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002869
Douglas Gregor72172e92012-01-05 21:55:30 +00002870UsingShadowDecl *
2871UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002872 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002873}
2874
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002875UsingDecl *UsingShadowDecl::getUsingDecl() const {
2876 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002877 while (const auto *NextShadow =
2878 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002879 Shadow = NextShadow;
2880 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2881}
2882
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002883void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002884
2885ConstructorUsingShadowDecl *
2886ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2887 SourceLocation Loc, UsingDecl *Using,
2888 NamedDecl *Target, bool IsVirtual) {
2889 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2890 IsVirtual);
2891}
2892
2893ConstructorUsingShadowDecl *
2894ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2895 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2896}
2897
2898CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2899 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2900}
2901
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002902void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002903
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002904void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2905 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2906 "declaration already in set");
2907 assert(S->getUsingDecl() == this);
2908
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002909 if (FirstUsingShadow.getPointer())
2910 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2911 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002912}
2913
2914void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2915 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2916 "declaration not in set");
2917 assert(S->getUsingDecl() == this);
2918
2919 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2920
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002921 if (FirstUsingShadow.getPointer() == S) {
2922 FirstUsingShadow.setPointer(
2923 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002924 S->UsingOrNextShadow = this;
2925 return;
2926 }
2927
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002928 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002929 while (Prev->UsingOrNextShadow != S)
2930 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2931 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2932 S->UsingOrNextShadow = this;
2933}
2934
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002935UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2936 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002937 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002938 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002939 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002940}
2941
Douglas Gregor72172e92012-01-05 21:55:30 +00002942UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002943 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2944 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2945 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002946}
2947
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002948SourceRange UsingDecl::getSourceRange() const {
2949 SourceLocation Begin = isAccessDeclaration()
2950 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2951 return SourceRange(Begin, getNameInfo().getEndLoc());
2952}
2953
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002954void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002955
2956UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2957 NamedDecl *InstantiatedFrom,
2958 ArrayRef<NamedDecl *> UsingDecls) {
2959 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2960 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2961}
2962
2963UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2964 unsigned NumExpansions) {
2965 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2966 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2967 Result->NumExpansions = NumExpansions;
2968 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2969 for (unsigned I = 0; I != NumExpansions; ++I)
2970 new (Trail + I) NamedDecl*(nullptr);
2971 return Result;
2972}
2973
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002974void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002975
John McCalle61f2ba2009-11-18 02:36:19 +00002976UnresolvedUsingValueDecl *
2977UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2978 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002979 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002980 const DeclarationNameInfo &NameInfo,
2981 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002982 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002983 QualifierLoc, NameInfo,
2984 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002985}
2986
Douglas Gregor72172e92012-01-05 21:55:30 +00002987UnresolvedUsingValueDecl *
2988UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002989 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2990 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002991 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002992 DeclarationNameInfo(),
2993 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002994}
2995
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002996SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2997 SourceLocation Begin = isAccessDeclaration()
2998 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2999 return SourceRange(Begin, getNameInfo().getEndLoc());
3000}
3001
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00003002void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00003003
John McCalle61f2ba2009-11-18 02:36:19 +00003004UnresolvedUsingTypenameDecl *
3005UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
3006 SourceLocation UsingLoc,
3007 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003008 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00003009 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00003010 DeclarationName TargetName,
3011 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00003012 return new (C, DC) UnresolvedUsingTypenameDecl(
3013 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00003014 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00003015}
3016
Douglas Gregor72172e92012-01-05 21:55:30 +00003017UnresolvedUsingTypenameDecl *
3018UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00003019 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00003020 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00003021 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00003022}
3023
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00003024void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00003025
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00003026StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00003027 SourceLocation StaticAssertLoc,
3028 Expr *AssertExpr,
3029 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00003030 SourceLocation RParenLoc,
3031 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00003032 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
3033 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00003034}
3035
Richard Smithf7981722013-11-22 09:01:48 +00003036StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00003037 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003038 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
3039 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00003040}
3041
Richard Smithbdb84f32016-07-22 23:36:59 +00003042void BindingDecl::anchor() {}
3043
3044BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
3045 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00003046 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00003047}
3048
3049BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00003050 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00003051}
3052
Richard Smith0353e5a2019-05-25 01:04:17 +00003053ValueDecl *BindingDecl::getDecomposedDecl() const {
3054 ExternalASTSource *Source =
3055 Decomp.isOffset() ? getASTContext().getExternalSource() : nullptr;
3056 return cast_or_null<ValueDecl>(Decomp.get(Source));
3057}
3058
Richard Smith97fcf4b2016-08-14 23:15:52 +00003059VarDecl *BindingDecl::getHoldingVar() const {
3060 Expr *B = getBinding();
3061 if (!B)
3062 return nullptr;
3063 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
3064 if (!DRE)
3065 return nullptr;
3066
3067 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
3068 assert(VD->isImplicit() && "holding var for binding decl not implicit");
3069 return VD;
3070}
3071
Richard Smithbdb84f32016-07-22 23:36:59 +00003072void DecompositionDecl::anchor() {}
3073
3074DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
3075 SourceLocation StartLoc,
3076 SourceLocation LSquareLoc,
3077 QualType T, TypeSourceInfo *TInfo,
3078 StorageClass SC,
3079 ArrayRef<BindingDecl *> Bindings) {
3080 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3081 return new (C, DC, Extra)
3082 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
3083}
3084
3085DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3086 unsigned ID,
3087 unsigned NumBindings) {
3088 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00003089 auto *Result = new (C, ID, Extra)
3090 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3091 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00003092 // Set up and clean out the bindings array.
3093 Result->NumBindings = NumBindings;
3094 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
3095 for (unsigned I = 0; I != NumBindings; ++I)
3096 new (Trail + I) BindingDecl*(nullptr);
3097 return Result;
3098}
3099
Richard Smith7873de02016-08-11 22:25:46 +00003100void DecompositionDecl::printName(llvm::raw_ostream &os) const {
3101 os << '[';
3102 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00003103 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00003104 if (Comma)
3105 os << ", ";
3106 B->printName(os);
3107 Comma = true;
3108 }
3109 os << ']';
3110}
3111
Richard Trieub3e902f2018-12-29 02:02:30 +00003112void MSPropertyDecl::anchor() {}
3113
Richard Smithf7981722013-11-22 09:01:48 +00003114MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
3115 SourceLocation L, DeclarationName N,
3116 QualType T, TypeSourceInfo *TInfo,
3117 SourceLocation StartL,
3118 IdentifierInfo *Getter,
3119 IdentifierInfo *Setter) {
3120 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
3121}
3122
3123MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3124 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003125 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
3126 DeclarationName(), QualType(), nullptr,
3127 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00003128}
3129
Anders Carlsson6750d162009-03-26 23:46:50 +00003130static const char *getAccessName(AccessSpecifier AS) {
3131 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00003132 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00003133 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003134 case AS_public:
3135 return "public";
3136 case AS_private:
3137 return "private";
3138 case AS_protected:
3139 return "protected";
3140 }
David Blaikief47fa302012-01-17 02:30:50 +00003141 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003142}
3143
3144const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
3145 AccessSpecifier AS) {
3146 return DB << getAccessName(AS);
3147}
Richard Smith84f6dcf2012-02-02 01:16:57 +00003148
3149const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
3150 AccessSpecifier AS) {
3151 return DB << getAccessName(AS);
3152}