blob: 95b14e494a3b5f3c694dfcf1c55dd7bcb79be218 [file] [log] [blame]
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001//===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
Ted Kremenek21475702008-09-05 17:16:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek21475702008-09-05 17:16:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the C++ related Decl classes.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000012
Ted Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000017#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000019#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000021#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000022#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000023#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000024#include "clang/AST/LambdaCapture.h"
25#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000026#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000027#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000028#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000029#include "clang/AST/UnresolvedSet.h"
30#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000031#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000032#include "clang/Basic/LLVM.h"
33#include "clang/Basic/LangOptions.h"
34#include "clang/Basic/OperatorKinds.h"
35#include "clang/Basic/PartialDiagnostic.h"
36#include "clang/Basic/SourceLocation.h"
37#include "clang/Basic/Specifiers.h"
38#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000040#include "llvm/ADT/SmallVector.h"
41#include "llvm/ADT/iterator_range.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
45#include <algorithm>
46#include <cassert>
47#include <cstddef>
48#include <cstdint>
49
Ted Kremenek21475702008-09-05 17:16:31 +000050using namespace clang;
51
52//===----------------------------------------------------------------------===//
53// Decl Allocation/Deallocation Method Implementations
54//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000055
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000056void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000057
Douglas Gregor72172e92012-01-05 21:55:30 +000058AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000059 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000060}
61
Richard Smitha4ba74c2013-08-30 04:46:40 +000062void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
63 ExternalASTSource *Source = C.getExternalSource();
64 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
65 assert(Source && "getFromExternalSource with no external source");
66
67 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
68 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
69 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
70 Impl.Decls.setLazy(false);
71}
72
John McCall67da35c2010-02-04 22:26:26 +000073CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000074 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
75 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000076 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
77 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000078 HasPrivateFields(false), HasProtectedFields(false),
79 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
80 HasOnlyCMembers(true), HasInClassInitializer(false),
81 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000082 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000083 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000084 NeedOverloadResolutionForMoveConstructor(false),
85 NeedOverloadResolutionForMoveAssignment(false),
86 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000087 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000088 DefaultedMoveConstructorIsDeleted(false),
89 DefaultedMoveAssignmentIsDeleted(false),
90 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000091 HasTrivialSpecialMembersForCall(SMF_All),
92 DeclaredNonTrivialSpecialMembers(0),
93 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000094 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000095 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000096 DefaultedDefaultConstructorIsConstexpr(true),
97 HasConstexprDefaultConstructor(false),
Richard Smith457226e2019-09-23 03:48:44 +000098 DefaultedDestructorIsConstexpr(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000099 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
100 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +0000101 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
102 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +0000103 ImplicitCopyAssignmentHasConstParam(true),
104 HasDeclaredCopyConstructorWithConstParam(false),
105 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000106 IsParsingBaseSpecifiers(false), HasODRHash(false), Definition(D) {}
John McCall67da35c2010-02-04 22:26:26 +0000107
Benjamin Kramer300c0632012-07-04 17:03:33 +0000108CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
109 return Bases.get(Definition->getASTContext().getExternalSource());
110}
111
112CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
113 return VBases.get(Definition->getASTContext().getExternalSource());
114}
115
Richard Smith053f6c62014-05-16 23:01:30 +0000116CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
117 DeclContext *DC, SourceLocation StartLoc,
118 SourceLocation IdLoc, IdentifierInfo *Id,
119 CXXRecordDecl *PrevDecl)
120 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
121 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000122 : nullptr) {}
Douglas Gregorb6acda02008-11-12 23:21:09 +0000123
Jay Foad39c79802011-01-12 09:06:06 +0000124CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000125 DeclContext *DC, SourceLocation StartLoc,
126 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smithbecb92d2017-10-10 22:33:17 +0000127 CXXRecordDecl *PrevDecl,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000128 bool DelayTypeCreation) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000129 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
130 PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000131 R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000133 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000134 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000135 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000136 return R;
137}
138
Richard Smith053f6c62014-05-16 23:01:30 +0000139CXXRecordDecl *
140CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
141 TypeSourceInfo *Info, SourceLocation Loc,
142 bool Dependent, bool IsGeneric,
143 LambdaCaptureDefault CaptureDefault) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000144 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
145 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000146 R->setBeingDefined(true);
Richard Smith64c06302014-05-22 23:19:02 +0000147 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000148 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000149 CaptureDefault);
Erich Keanef92f31c2018-08-01 20:48:16 +0000150 R->setMayHaveOutOfDateDef(false);
James Dennett8f60cdd2013-09-05 17:46:21 +0000151 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000152 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000153 return R;
154}
155
Douglas Gregor72172e92012-01-05 21:55:30 +0000156CXXRecordDecl *
157CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000158 auto *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000159 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000160 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000161 R->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000162 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000163}
164
Richard Smithb6070db2018-04-05 18:55:37 +0000165/// Determine whether a class has a repeated base class. This is intended for
166/// use when determining if a class is standard-layout, so makes no attempt to
167/// handle virtual bases.
168static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
169 llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
170 SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
171 while (!WorkList.empty()) {
172 const CXXRecordDecl *RD = WorkList.pop_back_val();
173 for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
174 if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
175 if (!SeenBaseTypes.insert(B).second)
176 return true;
177 WorkList.push_back(B);
178 }
179 }
180 }
181 return false;
182}
183
Mike Stump11289f42009-09-09 15:08:12 +0000184void
Craig Toppere6337e12015-12-25 00:36:02 +0000185CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
186 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000187 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000188
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000189 if (!data().Bases.isOffset() && data().NumBases > 0)
190 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000191
Craig Toppere6337e12015-12-25 00:36:02 +0000192 if (NumBases) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000193 if (!C.getLangOpts().CPlusPlus17) {
Richard Smith872307e2016-03-08 22:17:41 +0000194 // C++ [dcl.init.aggr]p1:
195 // An aggregate is [...] a class with [...] no base classes [...].
196 data().Aggregate = false;
197 }
Richard Smithaed32b42011-10-18 20:08:55 +0000198
199 // C++ [class]p4:
200 // A POD-struct is an aggregate class...
201 data().PlainOldData = false;
202 }
203
Anders Carlssonabb20e62010-03-29 05:13:12 +0000204 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000205 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Fangrui Song6907ce22018-07-30 19:24:48 +0000206
Anders Carlssonabb20e62010-03-29 05:13:12 +0000207 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000209
Craig Toppere6337e12015-12-25 00:36:02 +0000210 data().Bases = new(C) CXXBaseSpecifier [NumBases];
211 data().NumBases = NumBases;
212 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000213 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000214 // Keep track of inherited vbases for this base class.
215 const CXXBaseSpecifier *Base = Bases[i];
216 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000217 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000218 if (BaseType->isDependentType())
219 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000220 auto *BaseClassDecl =
221 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000222
Richard Smithb6070db2018-04-05 18:55:37 +0000223 // C++2a [class]p7:
224 // A standard-layout class is a class that:
225 // [...]
226 // -- has all non-static data members and bit-fields in the class and
227 // its base classes first declared in the same class
228 if (BaseClassDecl->data().HasBasesWithFields ||
229 !BaseClassDecl->field_empty()) {
230 if (data().HasBasesWithFields)
231 // Two bases have members or bit-fields: not standard-layout.
Chandler Carruth583edf82011-04-30 10:07:30 +0000232 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000233 data().HasBasesWithFields = true;
234 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000235
Richard Smithb6070db2018-04-05 18:55:37 +0000236 // C++11 [class]p7:
237 // A standard-layout class is a class that:
238 // -- [...] has [...] at most one base class with non-static data
239 // members
240 if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
241 BaseClassDecl->hasDirectFields()) {
242 if (data().HasBasesWithNonStaticDataMembers)
243 data().IsCXX11StandardLayout = false;
244 data().HasBasesWithNonStaticDataMembers = true;
245 }
246
247 if (!BaseClassDecl->isEmpty()) {
David Majnemer5cfda6f2016-05-22 05:34:26 +0000248 // C++14 [meta.unary.prop]p4:
249 // T is a class type [...] with [...] no base class B for which
250 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000251 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000252 }
Richard Smithb6070db2018-04-05 18:55:37 +0000253
Richard Smith872307e2016-03-08 22:17:41 +0000254 // C++1z [dcl.init.agg]p1:
255 // An aggregate is a class with [...] no private or protected base classes
256 if (Base->getAccessSpecifier() != AS_public)
257 data().Aggregate = false;
258
Douglas Gregor11c024b2010-09-28 20:50:54 +0000259 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000260 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 // polymorphic class.
Richard Smithc00e4462018-06-14 20:03:22 +0000262 if (BaseClassDecl->isPolymorphic()) {
Douglas Gregor11c024b2010-09-28 20:50:54 +0000263 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000264
Richard Smithc00e4462018-06-14 20:03:22 +0000265 // An aggregate is a class with [...] no virtual functions.
266 data().Aggregate = false;
267 }
268
Chandler Carruthb1963742011-04-30 09:17:45 +0000269 // C++0x [class]p7:
270 // A standard-layout class is a class that: [...]
271 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000272 if (!BaseClassDecl->isStandardLayout())
273 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000274 if (!BaseClassDecl->isCXX11StandardLayout())
275 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000276
Chandler Carruthe71d0622011-04-24 02:49:34 +0000277 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000278 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000279 data().HasNonLiteralTypeFieldsOrBases = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000280
Anders Carlssonabb20e62010-03-29 05:13:12 +0000281 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000282 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000283 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000284 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000285 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000286
287 // C++11 [class.copy]p8:
288 // The implicitly-declared copy constructor for a class X will have
289 // the form 'X::X(const X&)' if each [...] virtual base class B of X
290 // has a copy constructor whose first parameter is of type
291 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000292 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000293 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000294 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000295
296 // C++1z [dcl.init.agg]p1:
297 // An aggregate is a class with [...] no virtual base classes
298 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000299 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000300 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000301
302 if (Base->isVirtual()) {
303 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000304 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000305 VBases.push_back(Base);
306
David Majnemer5cfda6f2016-05-22 05:34:26 +0000307 // C++14 [meta.unary.prop] is_empty:
308 // T is a class type, but not a union type, with ... no virtual base
309 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000310 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000311
Richard Smith872307e2016-03-08 22:17:41 +0000312 // C++1z [dcl.init.agg]p1:
313 // An aggregate is a class with [...] no virtual base classes
314 data().Aggregate = false;
315
Richard Smith328aae52012-11-30 05:11:39 +0000316 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
317 // A [default constructor, copy/move constructor, or copy/move assignment
318 // operator for a class X] is trivial [...] if:
319 // -- class X has [...] no virtual base classes
320 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000321 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000322
323 // C++0x [class]p7:
324 // A standard-layout class is a class that: [...]
325 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000326 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000327 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000328
Richard Smith457226e2019-09-23 03:48:44 +0000329 // C++20 [dcl.constexpr]p3:
330 // In the definition of a constexpr function [...]
331 // -- if the function is a constructor or destructor,
332 // its class shall not have any virtual base classes
Richard Smithcc36f692011-12-22 02:22:31 +0000333 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith457226e2019-09-23 03:48:44 +0000334 data().DefaultedDestructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000335
336 // C++1z [class.copy]p8:
337 // The implicitly-declared copy constructor for a class X will have
338 // the form 'X::X(const X&)' if each potentially constructed subobject
339 // has a copy constructor whose first parameter is of type
340 // 'const B&' or 'const volatile B&' [...]
341 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
342 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000343 } else {
344 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000345 // A default constructor is trivial [...] if:
346 // -- all the direct base classes of its class have trivial default
347 // constructors.
348 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000349 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
350
Chandler Carruthad7d4042011-04-23 23:10:33 +0000351 // C++0x [class.copy]p13:
352 // A copy/move constructor for class X is trivial if [...]
353 // [...]
354 // -- the constructor selected to copy/move each direct base class
355 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000356 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000357 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000358
359 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
360 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
361
Richard Smith6b02d462012-12-08 08:32:28 +0000362 // If the base class doesn't have a simple move constructor, we'll eagerly
363 // declare it and perform overload resolution to determine which function
364 // it actually calls. If it does have a simple move constructor, this
365 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000366 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000367 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000368
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000369 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
370 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
371
Chandler Carruthad7d4042011-04-23 23:10:33 +0000372 // C++0x [class.copy]p27:
373 // A copy/move assignment operator for class X is trivial if [...]
374 // [...]
375 // -- the assignment operator selected to copy/move each direct base
376 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000377 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000378 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000379 // If the base class doesn't have a simple move assignment, we'll eagerly
380 // declare it and perform overload resolution to determine which function
381 // it actually calls. If it does have a simple move assignment, this
382 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000383 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000384 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000385
386 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000387 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000388 // requirements of a constexpr constructor, the implicitly-defined
389 // default constructor is constexpr.
390 if (!BaseClassDecl->hasConstexprDefaultConstructor())
391 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000392
393 // C++1z [class.copy]p8:
394 // The implicitly-declared copy constructor for a class X will have
395 // the form 'X::X(const X&)' if each potentially constructed subobject
396 // has a copy constructor whose first parameter is of type
397 // 'const B&' or 'const volatile B&' [...]
398 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
399 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000400 }
Richard Smith92f241f2012-12-08 02:53:02 +0000401
Douglas Gregor11c024b2010-09-28 20:50:54 +0000402 // C++ [class.ctor]p3:
403 // A destructor is trivial if all the direct base classes of its class
404 // have trivial destructors.
405 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000406 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000407
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000408 if (!BaseClassDecl->hasTrivialDestructorForCall())
409 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
410
Richard Smith561fb152012-02-25 07:33:38 +0000411 if (!BaseClassDecl->hasIrrelevantDestructor())
412 data().HasIrrelevantDestructor = false;
413
Richard Smith1c33fe82012-11-28 06:23:12 +0000414 // C++11 [class.copy]p18:
415 // The implicitly-declared copy assignment oeprator for a class X will
416 // have the form 'X& X::operator=(const X&)' if each direct base class B
417 // of X has a copy assignment operator whose parameter is of type 'const
418 // B&', 'const volatile B&', or 'B' [...]
419 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
420 data().ImplicitCopyAssignmentHasConstParam = false;
421
John McCall31168b02011-06-15 23:02:42 +0000422 // A class has an Objective-C object member if... or any of its bases
423 // has an Objective-C object member.
424 if (BaseClassDecl->hasObjectMember())
425 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000426
Fariborz Jahanian78652202013-01-25 23:57:05 +0000427 if (BaseClassDecl->hasVolatileMember())
428 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000429
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000430 if (BaseClassDecl->getArgPassingRestrictions() ==
431 RecordDecl::APK_CanNeverPassInRegs)
432 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
433
Douglas Gregor61226d32011-05-13 01:05:07 +0000434 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000435 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000436 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000437 data().NeedOverloadResolutionForCopyConstructor = true;
438 }
Richard Smith593f9932012-12-08 02:01:17 +0000439
440 if (BaseClassDecl->hasUninitializedReferenceMember())
441 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000442
Nico Weber6a6376b2016-02-19 01:52:46 +0000443 if (!BaseClassDecl->allowConstDefaultInit())
444 data().HasUninitializedFields = true;
445
Richard Smith6b02d462012-12-08 08:32:28 +0000446 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000447 }
Richard Smithb6070db2018-04-05 18:55:37 +0000448
449 // C++2a [class]p7:
450 // A class S is a standard-layout class if it:
451 // -- has at most one base class subobject of any given type
452 //
453 // Note that we only need to check this for classes with more than one base
454 // class. If there's only one base class, and it's standard layout, then
455 // we know there are no repeated base classes.
456 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
457 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000458
David Majnemer5ef4fe72014-06-13 06:43:46 +0000459 if (VBases.empty()) {
460 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000461 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000462 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000463
464 // Create base specifier for any direct or indirect virtual bases.
465 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
466 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000467 for (int I = 0, E = VBases.size(); I != E; ++I) {
468 QualType Type = VBases[I]->getType();
469 if (!Type->isDependentType())
470 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000471 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000472 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000473
474 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000475}
476
Richard Trieufd1acbb2017-04-11 21:31:00 +0000477unsigned CXXRecordDecl::getODRHash() const {
478 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000479
Richard Trieufd1acbb2017-04-11 21:31:00 +0000480 // Previously calculated hash is stored in DefinitionData.
481 if (DefinitionData->HasODRHash)
482 return DefinitionData->ODRHash;
483
484 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000485 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000486 Hash.AddCXXRecordDecl(getDefinition());
487 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000488 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000489
490 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000491}
Richard Trieub6adf542017-02-18 02:09:28 +0000492
Richard Smith6b02d462012-12-08 08:32:28 +0000493void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
494 // C++11 [class.copy]p11:
495 // A defaulted copy/move constructor for a class X is defined as
496 // deleted if X has:
497 // -- a direct or virtual base class B that cannot be copied/moved [...]
498 // -- a non-static data member of class type M (or array thereof)
499 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000500 if (!Subobj->hasSimpleCopyConstructor())
501 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000502 if (!Subobj->hasSimpleMoveConstructor())
503 data().NeedOverloadResolutionForMoveConstructor = true;
504
505 // C++11 [class.copy]p23:
506 // A defaulted copy/move assignment operator for a class X is defined as
507 // deleted if X has:
508 // -- a direct or virtual base class B that cannot be copied/moved [...]
509 // -- a non-static data member of class type M (or array thereof)
510 // that cannot be copied or moved [...]
511 if (!Subobj->hasSimpleMoveAssignment())
512 data().NeedOverloadResolutionForMoveAssignment = true;
513
514 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
515 // A defaulted [ctor or dtor] for a class X is defined as
516 // deleted if X has:
517 // -- any direct or virtual base class [...] has a type with a destructor
518 // that is deleted or inaccessible from the defaulted [ctor or dtor].
519 // -- any non-static data member has a type with a destructor
520 // that is deleted or inaccessible from the defaulted [ctor or dtor].
521 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000522 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000523 data().NeedOverloadResolutionForMoveConstructor = true;
524 data().NeedOverloadResolutionForDestructor = true;
525 }
Richard Smith457226e2019-09-23 03:48:44 +0000526
527 // C++2a [dcl.constexpr]p4:
528 // The definition of a constexpr destructor [shall] satisfy the
529 // following requirement:
530 // -- for every subobject of class type or (possibly multi-dimensional)
531 // array thereof, that class type shall have a constexpr destructor
532 if (!Subobj->hasConstexprDestructor())
533 data().DefaultedDestructorIsConstexpr = false;
534}
535
536bool CXXRecordDecl::hasConstexprDestructor() const {
537 auto *Dtor = getDestructor();
538 return Dtor ? Dtor->isConstexpr() : defaultedDestructorIsConstexpr();
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000539}
540
Douglas Gregord2e6a452010-01-14 17:47:39 +0000541bool CXXRecordDecl::hasAnyDependentBases() const {
542 if (!isDependentContext())
543 return false;
544
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000545 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000546}
547
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000548bool CXXRecordDecl::isTriviallyCopyable() const {
549 // C++0x [class]p5:
550 // A trivially copyable class is a class that:
551 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000552 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000553 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000554 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000555 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000556 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000557 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000558 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000559 // -- has a trivial destructor.
560 if (!hasTrivialDestructor()) return false;
561
562 return true;
563}
564
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000565void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000566 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000567 // A class is abstract if it has at least one pure virtual function.
568 data().Abstract = true;
569}
570
Richard Smithb6070db2018-04-05 18:55:37 +0000571bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
572 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
573 if (!getNumBases())
574 return false;
575
576 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
577 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
578 SmallVector<const CXXRecordDecl*, 8> WorkList;
579
580 // Visit a type that we have determined is an element of M(S).
581 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
582 RD = RD->getCanonicalDecl();
583
584 // C++2a [class]p8:
585 // A class S is a standard-layout class if it [...] has no element of the
586 // set M(S) of types as a base class.
587 //
588 // If we find a subobject of an empty type, it might also be a base class,
589 // so we'll need to walk the base classes to check.
590 if (!RD->data().HasBasesWithFields) {
591 // Walk the bases the first time, stopping if we find the type. Build a
592 // set of them so we don't need to walk them again.
593 if (Bases.empty()) {
594 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
595 Base = Base->getCanonicalDecl();
596 if (RD == Base)
597 return false;
598 Bases.insert(Base);
599 return true;
600 });
601 if (RDIsBase)
602 return true;
603 } else {
604 if (Bases.count(RD))
605 return true;
606 }
607 }
608
609 if (M.insert(RD).second)
610 WorkList.push_back(RD);
611 return false;
612 };
613
614 if (Visit(XFirst))
615 return true;
616
617 while (!WorkList.empty()) {
618 const CXXRecordDecl *X = WorkList.pop_back_val();
619
620 // FIXME: We don't check the bases of X. That matches the standard, but
621 // that sure looks like a wording bug.
622
623 // -- If X is a non-union class type with a non-static data member
Richard Smith78b239e2019-06-20 20:44:45 +0000624 // [recurse to each field] that is either of zero size or is the
625 // first non-static data member of X
Richard Smithb6070db2018-04-05 18:55:37 +0000626 // -- If X is a union type, [recurse to union members]
Richard Smith78b239e2019-06-20 20:44:45 +0000627 bool IsFirstField = true;
Richard Smithb6070db2018-04-05 18:55:37 +0000628 for (auto *FD : X->fields()) {
629 // FIXME: Should we really care about the type of the first non-static
630 // data member of a non-union if there are preceding unnamed bit-fields?
631 if (FD->isUnnamedBitfield())
632 continue;
633
Richard Smith78b239e2019-06-20 20:44:45 +0000634 if (!IsFirstField && !FD->isZeroSize(Ctx))
635 continue;
636
Richard Smithb6070db2018-04-05 18:55:37 +0000637 // -- If X is n array type, [visit the element type]
638 QualType T = Ctx.getBaseElementType(FD->getType());
639 if (auto *RD = T->getAsCXXRecordDecl())
640 if (Visit(RD))
641 return true;
642
643 if (!X->isUnion())
Richard Smith78b239e2019-06-20 20:44:45 +0000644 IsFirstField = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000645 }
646 }
647
648 return false;
649}
650
Richard Smith864949b2018-09-27 22:47:04 +0000651bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
652 assert(isLambda() && "not a lambda");
653
654 // C++2a [expr.prim.lambda.capture]p11:
655 // The closure type associated with a lambda-expression has no default
656 // constructor if the lambda-expression has a lambda-capture and a
657 // defaulted default constructor otherwise. It has a deleted copy
658 // assignment operator if the lambda-expression has a lambda-capture and
659 // defaulted copy and move assignment operators otherwise.
660 //
661 // C++17 [expr.prim.lambda]p21:
662 // The closure type associated with a lambda-expression has no default
663 // constructor and a deleted copy assignment operator.
David Blaikie793231c2019-07-08 23:24:41 +0000664 if (getLambdaCaptureDefault() != LCD_None ||
665 getLambdaData().NumCaptures != 0)
Richard Smith864949b2018-09-27 22:47:04 +0000666 return false;
667 return getASTContext().getLangOpts().CPlusPlus2a;
668}
669
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000670void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000671 if (!D->isImplicit() &&
672 !isa<FieldDecl>(D) &&
673 !isa<IndirectFieldDecl>(D) &&
674 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
675 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
676 data().HasOnlyCMembers = false;
677
678 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000679 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000680 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000681
Eugene Zelenko7855e772018-04-03 00:11:50 +0000682 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000683 if (FunTmpl)
684 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000685
686 // FIXME: Pass NamedDecl* to addedMember?
687 Decl *DUnderlying = D;
688 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
689 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000690 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000691 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
692 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000693
Eugene Zelenko7855e772018-04-03 00:11:50 +0000694 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000695 if (Method->isVirtual()) {
696 // C++ [dcl.init.aggr]p1:
697 // An aggregate is an array or a class with [...] no virtual functions.
698 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000699
Douglas Gregora832d3e2010-09-28 19:45:33 +0000700 // C++ [class]p4:
701 // A POD-struct is an aggregate class...
702 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000703
David Majnemer5cfda6f2016-05-22 05:34:26 +0000704 // C++14 [meta.unary.prop]p4:
705 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000706 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000707
708 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000709 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000710 // polymorphic class.
711 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000712
Richard Smith328aae52012-11-30 05:11:39 +0000713 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
714 // A [default constructor, copy/move constructor, or copy/move
715 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000716 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000717 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000718 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000719
Chandler Carruthb1963742011-04-30 09:17:45 +0000720 // C++0x [class]p7:
721 // A standard-layout class is a class that: [...]
722 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000723 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000724 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000725 }
726 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000727
Richard Smith1c33fe82012-11-28 06:23:12 +0000728 // Notify the listener if an implicit member was added after the definition
729 // was completed.
730 if (!isBeingDefined() && D->isImplicit())
731 if (ASTMutationListener *L = getASTMutationListener())
732 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000733
Richard Smith328aae52012-11-30 05:11:39 +0000734 // The kind of special member this declaration is, if any.
735 unsigned SMKind = 0;
736
Richard Smith1c33fe82012-11-28 06:23:12 +0000737 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000738 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000739 if (!Constructor->isImplicit()) {
740 // Note that we have a user-declared constructor.
741 data().UserDeclaredConstructor = true;
742
743 // C++ [class]p4:
744 // A POD-struct is an aggregate class [...]
745 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
746 // type is technically an aggregate in C++0x since it wouldn't be in 03.
747 data().PlainOldData = false;
748 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000749
Alexis Hunt88c75c32011-05-09 21:45:35 +0000750 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000751 SMKind |= SMF_DefaultConstructor;
752
Richard Smith5179eb72016-06-28 19:03:57 +0000753 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000754 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000755 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000756 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000757 if (Constructor->isDefaulted())
758 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000759 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000760
Chandler Carruthad7d4042011-04-23 23:10:33 +0000761 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000762 unsigned Quals;
763 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000764 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000765
766 if (Quals & Qualifiers::Const)
767 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000768 } else if (Constructor->isMoveConstructor())
769 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000770 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000771
Eric Fiselier283d8d42016-12-03 01:26:47 +0000772 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith79c88c32018-09-26 19:00:16 +0000773 // An aggregate is an array or a class with no user-provided [or]
774 // explicit [...] constructors
775 // C++20 [dcl.init.aggr]p1:
776 // An aggregate is an array or a class with no user-declared [...]
777 // constructors
778 if (getASTContext().getLangOpts().CPlusPlus2a
779 ? !Constructor->isImplicit()
780 : (Constructor->isUserProvided() || Constructor->isExplicit()))
Eric Fiselier283d8d42016-12-03 01:26:47 +0000781 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000782 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000783
Richard Smith5179eb72016-06-28 19:03:57 +0000784 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000785 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000786 // Record if we see any constexpr constructors which are neither copy
787 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000788 // C++1z [basic.types]p10:
789 // [...] has at least one constexpr constructor or constructor template
790 // (possibly inherited from a base class) that is not a copy or move
791 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000792 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000793 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000794 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000795
Richard Smith1c33fe82012-11-28 06:23:12 +0000796 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000797 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000798 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000799
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000800 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000801 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000802 // If the destructor is explicitly defaulted and not trivial or not public
803 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
804 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000805
Richard Smith1c33fe82012-11-28 06:23:12 +0000806 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000807 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000808 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000809 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000810 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
811 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000812 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000813
814 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000815 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000816 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000817 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000818
Eugene Zelenko7855e772018-04-03 00:11:50 +0000819 const auto *ParamTy =
820 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000821 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
822 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000823 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000824
Richard Smith328aae52012-11-30 05:11:39 +0000825 if (Method->isMoveAssignmentOperator())
826 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000827
Douglas Gregor457104e2010-09-29 04:25:11 +0000828 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000829 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000830 // FIXME: We use the 'unsafe' accessor for the access specifier here,
831 // because Sema may not have set it yet. That's really just a misdesign
832 // in Sema. However, LLDB *will* have set the access specifier correctly,
833 // and adds declarations after the class is technically completed,
834 // so completeDefinition()'s overriding of the access specifiers doesn't
835 // work.
836 AccessSpecifier AS = Conversion->getAccessUnsafe();
837
Richard Smith328aae52012-11-30 05:11:39 +0000838 if (Conversion->getPrimaryTemplate()) {
839 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000840 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000841 ASTContext &Ctx = getASTContext();
842 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
843 NamedDecl *Primary =
844 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
845 if (Primary->getPreviousDecl())
846 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
847 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000848 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000849 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000850 }
851 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000852
Richard Smith328aae52012-11-30 05:11:39 +0000853 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000854 // If this is the first declaration of a special member, we no longer have
855 // an implicit trivial special member.
856 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000857 data().DeclaredSpecialMembers | ~SMKind;
858 data().HasTrivialSpecialMembersForCall &=
859 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000860
861 if (!Method->isImplicit() && !Method->isUserProvided()) {
862 // This method is user-declared but not user-provided. We can't work out
863 // whether it's trivial yet (not until we get to the end of the class).
864 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000865 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000866 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000867 data().HasTrivialSpecialMembersForCall |= SMKind;
868 } else if (Method->isTrivialForCall()) {
869 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000870 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000871 } else {
872 data().DeclaredNonTrivialSpecialMembers |= SMKind;
873 // If this is a user-provided function, do not set
874 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
875 // yet whether the method would be considered non-trivial for the
876 // purpose of calls (attribute "trivial_abi" can be dropped from the
877 // class later, which can change the special method's triviality).
878 if (!Method->isUserProvided())
879 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
880 }
Richard Smith92f241f2012-12-08 02:53:02 +0000881
Richard Smith328aae52012-11-30 05:11:39 +0000882 // Note when we have declared a declared special member, and suppress the
883 // implicit declaration of this special member.
884 data().DeclaredSpecialMembers |= SMKind;
885
886 if (!Method->isImplicit()) {
887 data().UserDeclaredSpecialMembers |= SMKind;
888
889 // C++03 [class]p4:
890 // A POD-struct is an aggregate class that has [...] no user-defined
891 // copy assignment operator and no user-defined destructor.
892 //
893 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
894 // aggregates could not have any constructors, clear it even for an
895 // explicitly defaulted or deleted constructor.
896 // type is technically an aggregate in C++0x since it wouldn't be in 03.
897 //
898 // Also, a user-declared move assignment operator makes a class non-POD.
899 // This is an extension in C++03.
900 data().PlainOldData = false;
901 }
Richard Smith328aae52012-11-30 05:11:39 +0000902 }
903
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000904 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000905 }
Richard Smith328aae52012-11-30 05:11:39 +0000906
Douglas Gregora832d3e2010-09-28 19:45:33 +0000907 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000908 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000909 ASTContext &Context = getASTContext();
910
Richard Smithb6070db2018-04-05 18:55:37 +0000911 // C++2a [class]p7:
912 // A standard-layout class is a class that:
913 // [...]
914 // -- has all non-static data members and bit-fields in the class and
915 // its base classes first declared in the same class
916 if (data().HasBasesWithFields)
917 data().IsStandardLayout = false;
918
Douglas Gregor556e5862011-10-10 17:22:13 +0000919 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000920 // A declaration for a bit-field that omits the identifier declares an
921 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000922 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000923 if (Field->isUnnamedBitfield()) {
924 // C++ [meta.unary.prop]p4: [LWG2358]
925 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
926 // length
927 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
928 Context.getLangOpts().getClangABICompat() >
929 LangOptions::ClangABI::Ver6)
930 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000931 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000932 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000933
Richard Smithb6070db2018-04-05 18:55:37 +0000934 // C++11 [class]p7:
935 // A standard-layout class is a class that:
936 // -- either has no non-static data members in the most derived class
937 // [...] or has no base classes with non-static data members
938 if (data().HasBasesWithNonStaticDataMembers)
939 data().IsCXX11StandardLayout = false;
940
Douglas Gregora832d3e2010-09-28 19:45:33 +0000941 // C++ [dcl.init.aggr]p1:
942 // An aggregate is an array or a class (clause 9) with [...] no
943 // private or protected non-static data members (clause 11).
944 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000945 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000946 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
947 data().Aggregate = false;
948 data().PlainOldData = false;
949 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000950
Richard Smithb6070db2018-04-05 18:55:37 +0000951 // Track whether this is the first field. We use this when checking
952 // whether the class is standard-layout below.
953 bool IsFirstField = !data().HasPrivateFields &&
954 !data().HasProtectedFields && !data().HasPublicFields;
955
Chandler Carruthb1963742011-04-30 09:17:45 +0000956 // C++0x [class]p7:
957 // A standard-layout class is a class that:
958 // [...]
959 // -- has the same access control for all non-static data members,
960 switch (D->getAccess()) {
961 case AS_private: data().HasPrivateFields = true; break;
962 case AS_protected: data().HasProtectedFields = true; break;
963 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000964 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000965 };
966 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000967 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000968 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000969 data().IsCXX11StandardLayout = false;
970 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000971
Douglas Gregor61226d32011-05-13 01:05:07 +0000972 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000973 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000974 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000975 data().NeedOverloadResolutionForCopyConstructor = true;
976 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000977
978 // C++11 [class.union]p8, DR1460:
979 // If X is a union, a non-static data member of X that is not an anonymous
980 // union is a variant member of X.
981 if (isUnion() && !Field->isAnonymousStructOrUnion())
982 data().HasVariantMembers = true;
983
Chandler Carruthad7d4042011-04-23 23:10:33 +0000984 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000985 // A POD struct is a class that is both a trivial class and a
986 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000987 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000988 //
989 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
990 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000991 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000992 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000993 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000994 // Objective-C Automatic Reference Counting:
995 // If a class has a non-static data member of Objective-C pointer
996 // type (or array thereof), it is a non-POD type and its
997 // default constructor (if any), copy constructor, move constructor,
998 // copy assignment operator, move assignment operator, and destructor are
999 // non-trivial.
1000 setHasObjectMember(true);
1001 struct DefinitionData &Data = data();
1002 Data.PlainOldData = false;
1003 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001004
1005 // __strong or __weak fields do not make special functions non-trivial
1006 // for the purpose of calls.
1007 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
1008 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
1009 data().HasTrivialSpecialMembersForCall = 0;
1010
1011 // Structs with __weak fields should never be passed directly.
1012 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001013 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +00001014
Ben Langmuir11eab612014-09-26 15:27:29 +00001015 Data.HasIrrelevantDestructor = false;
Akira Hatanakaa5048972019-02-02 02:23:40 +00001016
1017 if (isUnion()) {
1018 data().DefaultedCopyConstructorIsDeleted = true;
1019 data().DefaultedMoveConstructorIsDeleted = true;
1020 data().DefaultedMoveAssignmentIsDeleted = true;
1021 data().DefaultedDestructorIsDeleted = true;
1022 data().NeedOverloadResolutionForCopyConstructor = true;
1023 data().NeedOverloadResolutionForMoveConstructor = true;
1024 data().NeedOverloadResolutionForMoveAssignment = true;
1025 data().NeedOverloadResolutionForDestructor = true;
1026 }
Brian Kelley036603a2017-03-29 17:31:42 +00001027 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1028 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +00001029 }
Eli Friedmana5433322013-07-20 01:06:31 +00001030 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +00001031 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001032
Chandler Carruthb1963742011-04-30 09:17:45 +00001033 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +00001034 if (!Field->hasInClassInitializer())
1035 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +00001036
Chandler Carruthb1963742011-04-30 09:17:45 +00001037 // C++0x [class]p7:
1038 // A standard-layout class is a class that:
1039 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +00001040 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001041 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +00001042
1043 // C++1z [class.copy.ctor]p10:
1044 // A defaulted copy constructor for a class X is defined as deleted if X has:
1045 // -- a non-static data member of rvalue reference type
1046 if (T->isRValueReferenceType())
1047 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +00001048 }
1049
Nico Weber6a6376b2016-02-19 01:52:46 +00001050 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +00001051 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +00001052 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +00001053 data().HasUninitializedFields = true;
1054 } else {
1055 data().HasUninitializedFields = true;
1056 }
1057 }
1058
Richard Smith3607ffe2012-02-13 03:54:03 +00001059 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001060 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001061 data().HasNonLiteralTypeFieldsOrBases = true;
1062
Richard Smithab44d5b2013-12-10 08:25:00 +00001063 if (Field->hasInClassInitializer() ||
1064 (Field->isAnonymousStructOrUnion() &&
1065 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001066 data().HasInClassInitializer = true;
1067
1068 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001069 // A default constructor is trivial if [...] no non-static data member
1070 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001071 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001072
Richard Smithe2648ba2012-05-07 01:07:30 +00001073 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001074 // An aggregate is a [...] class with [...] no
1075 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001076 //
Richard Smith872307e2016-03-08 22:17:41 +00001077 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001078 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001079 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001080
Richard Smithe2648ba2012-05-07 01:07:30 +00001081 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001082 // A POD struct is [...] a trivial class.
1083 data().PlainOldData = false;
1084 }
1085
Richard Smith6b02d462012-12-08 08:32:28 +00001086 // C++11 [class.copy]p23:
1087 // A defaulted copy/move assignment operator for a class X is defined
1088 // as deleted if X has:
1089 // -- a non-static data member of reference type
1090 if (T->isReferenceType())
1091 data().DefaultedMoveAssignmentIsDeleted = true;
1092
Richard Smith78b239e2019-06-20 20:44:45 +00001093 // Bitfields of length 0 are also zero-sized, but we already bailed out for
1094 // those because they are always unnamed.
1095 bool IsZeroSize = Field->isZeroSize(Context);
1096
Eugene Zelenko7855e772018-04-03 00:11:50 +00001097 if (const auto *RecordTy = T->getAs<RecordType>()) {
1098 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001099 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001100 addedClassSubobject(FieldRec);
1101
Richard Smithc91d12c2013-11-25 07:07:05 +00001102 // We may need to perform overload resolution to determine whether a
1103 // field can be moved if it's const or volatile qualified.
1104 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001105 // We need to care about 'const' for the copy constructor because an
1106 // implicit copy constructor might be declared with a non-const
1107 // parameter.
1108 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001109 data().NeedOverloadResolutionForMoveConstructor = true;
1110 data().NeedOverloadResolutionForMoveAssignment = true;
1111 }
1112
Richard Smith6b02d462012-12-08 08:32:28 +00001113 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1114 // A defaulted [special member] for a class X is defined as
1115 // deleted if:
1116 // -- X is a union-like class that has a variant member with a
1117 // non-trivial [corresponding special member]
1118 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001119 if (FieldRec->hasNonTrivialCopyConstructor())
1120 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001121 if (FieldRec->hasNonTrivialMoveConstructor())
1122 data().DefaultedMoveConstructorIsDeleted = true;
1123 if (FieldRec->hasNonTrivialMoveAssignment())
1124 data().DefaultedMoveAssignmentIsDeleted = true;
1125 if (FieldRec->hasNonTrivialDestructor())
1126 data().DefaultedDestructorIsDeleted = true;
1127 }
1128
Richard Smithdd5619f2016-08-16 00:13:47 +00001129 // For an anonymous union member, our overload resolution will perform
1130 // overload resolution for its members.
1131 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001132 data().NeedOverloadResolutionForCopyConstructor |=
1133 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001134 data().NeedOverloadResolutionForMoveConstructor |=
1135 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1136 data().NeedOverloadResolutionForMoveAssignment |=
1137 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1138 data().NeedOverloadResolutionForDestructor |=
1139 FieldRec->data().NeedOverloadResolutionForDestructor;
1140 }
1141
Alexis Huntf479f1b2011-05-09 18:22:59 +00001142 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001143 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001144 // -- for all the non-static data members of its class that are of
1145 // class type (or array thereof), each such class has a trivial
1146 // default constructor.
1147 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001148 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001149
1150 // C++0x [class.copy]p13:
1151 // A copy/move constructor for class X is trivial if [...]
1152 // [...]
1153 // -- for each non-static data member of X that is of class type (or
1154 // an array thereof), the constructor selected to copy/move that
1155 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001156 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001157 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001158
1159 if (!FieldRec->hasTrivialCopyConstructorForCall())
1160 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1161
Richard Smith6b02d462012-12-08 08:32:28 +00001162 // If the field doesn't have a simple move constructor, we'll eagerly
1163 // declare the move constructor for this class and we'll decide whether
1164 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001165 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001166 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001167
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001168 if (!FieldRec->hasTrivialMoveConstructorForCall())
1169 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1170
Chandler Carruthad7d4042011-04-23 23:10:33 +00001171 // C++0x [class.copy]p27:
1172 // A copy/move assignment operator for class X is trivial if [...]
1173 // [...]
1174 // -- for each non-static data member of X that is of class type (or
1175 // an array thereof), the assignment operator selected to
1176 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001177 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001178 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001179 // If the field doesn't have a simple move assignment, we'll eagerly
1180 // declare the move assignment for this class and we'll decide whether
1181 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001182 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001183 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001184
Douglas Gregor11c024b2010-09-28 20:50:54 +00001185 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001186 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001187 if (!FieldRec->hasTrivialDestructorForCall())
1188 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001189 if (!FieldRec->hasIrrelevantDestructor())
1190 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001191 if (FieldRec->hasObjectMember())
1192 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001193 if (FieldRec->hasVolatileMember())
1194 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001195 if (FieldRec->getArgPassingRestrictions() ==
1196 RecordDecl::APK_CanNeverPassInRegs)
1197 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001198
1199 // C++0x [class]p7:
1200 // A standard-layout class is a class that:
1201 // -- has no non-static data members of type non-standard-layout
1202 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001203 if (!FieldRec->isStandardLayout())
1204 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001205 if (!FieldRec->isCXX11StandardLayout())
1206 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001207
Richard Smithb6070db2018-04-05 18:55:37 +00001208 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001209 // A standard-layout class is a class that:
1210 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001211 // -- has no element of the set M(S) of types as a base class.
Richard Smith78b239e2019-06-20 20:44:45 +00001212 if (data().IsStandardLayout &&
1213 (isUnion() || IsFirstField || IsZeroSize) &&
Richard Smithb6070db2018-04-05 18:55:37 +00001214 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1215 data().IsStandardLayout = false;
1216
1217 // C++11 [class]p7:
1218 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001219 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001220 // data member
1221 if (data().IsCXX11StandardLayout && IsFirstField) {
1222 // FIXME: We should check all base classes here, not just direct
1223 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001224 for (const auto &BI : bases()) {
1225 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001226 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001227 break;
1228 }
1229 }
1230 }
Richard Smithb6070db2018-04-05 18:55:37 +00001231
Douglas Gregor61226d32011-05-13 01:05:07 +00001232 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001233 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001234 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001235 data().NeedOverloadResolutionForCopyConstructor = true;
1236 }
Richard Smithcc36f692011-12-22 02:22:31 +00001237
1238 // C++11 [class.copy]p13:
1239 // If the implicitly-defined constructor would satisfy the
1240 // requirements of a constexpr constructor, the implicitly-defined
1241 // constructor is constexpr.
1242 // C++11 [dcl.constexpr]p4:
1243 // -- every constructor involved in initializing non-static data
1244 // members [...] shall be a constexpr constructor
1245 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001246 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001247 // The standard requires any in-class initializer to be a constant
1248 // expression. We consider this to be a defect.
1249 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001250
1251 // C++11 [class.copy]p8:
1252 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001253 // the form 'X::X(const X&)' if each potentially constructed subobject
1254 // of a class type M (or array thereof) has a copy constructor whose
1255 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001256 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001257 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001258
1259 // C++11 [class.copy]p18:
1260 // The implicitly-declared copy assignment oeprator for a class X will
1261 // have the form 'X& X::operator=(const X&)' if [...] for all the
1262 // non-static data members of X that are of a class type M (or array
1263 // thereof), each such class type has a copy assignment operator whose
1264 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1265 if (!FieldRec->hasCopyAssignmentWithConstParam())
1266 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001267
1268 if (FieldRec->hasUninitializedReferenceMember() &&
1269 !Field->hasInClassInitializer())
1270 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001271
1272 // C++11 [class.union]p8, DR1460:
1273 // a non-static data member of an anonymous union that is a member of
1274 // X is also a variant member of X.
1275 if (FieldRec->hasVariantMembers() &&
1276 Field->isAnonymousStructOrUnion())
1277 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001278 }
Richard Smithcc36f692011-12-22 02:22:31 +00001279 } else {
1280 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001281 if (!T->isLiteralType(Context) ||
Richard Smithc667cdc2019-09-18 17:37:44 +00001282 (!Field->hasInClassInitializer() && !isUnion() &&
1283 !Context.getLangOpts().CPlusPlus2a))
Richard Smithcc36f692011-12-22 02:22:31 +00001284 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001285
1286 // C++11 [class.copy]p23:
1287 // A defaulted copy/move assignment operator for a class X is defined
1288 // as deleted if X has:
1289 // -- a non-static data member of const non-class type (or array
1290 // thereof)
1291 if (T.isConstQualified())
1292 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001293 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001294
David Majnemer5cfda6f2016-05-22 05:34:26 +00001295 // C++14 [meta.unary.prop]p4:
Richard Smith78b239e2019-06-20 20:44:45 +00001296 // T is a class type [...] with [...] no non-static data members other
1297 // than subobjects of zero size
1298 if (data().Empty && !IsZeroSize)
1299 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001300 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001301
Douglas Gregor457104e2010-09-29 04:25:11 +00001302 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001303 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001304 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001305 == DeclarationName::CXXConversionFunctionName) {
1306 ASTContext &Ctx = getASTContext();
1307 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1308 }
1309 }
Richard Smith12e79312016-05-13 06:47:56 +00001310
Eugene Zelenko7855e772018-04-03 00:11:50 +00001311 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001312 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001313 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001314 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001315 // C++1z [dcl.init.aggr]p1:
1316 // An aggregate is [...] a class [...] with no inherited constructors
1317 data().Aggregate = false;
1318 }
Richard Smith12e79312016-05-13 06:47:56 +00001319
1320 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1321 data().HasInheritedAssignment = true;
1322 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001323}
1324
Richard Smith92f241f2012-12-08 02:53:02 +00001325void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1326 assert(!D->isImplicit() && !D->isUserProvided());
1327
1328 // The kind of special member this declaration is, if any.
1329 unsigned SMKind = 0;
1330
Eugene Zelenko7855e772018-04-03 00:11:50 +00001331 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001332 if (Constructor->isDefaultConstructor()) {
1333 SMKind |= SMF_DefaultConstructor;
1334 if (Constructor->isConstexpr())
1335 data().HasConstexprDefaultConstructor = true;
1336 }
1337 if (Constructor->isCopyConstructor())
1338 SMKind |= SMF_CopyConstructor;
1339 else if (Constructor->isMoveConstructor())
1340 SMKind |= SMF_MoveConstructor;
1341 else if (Constructor->isConstexpr())
1342 // We may now know that the constructor is constexpr.
1343 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001344 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001345 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001346 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1347 data().HasIrrelevantDestructor = false;
1348 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001349 SMKind |= SMF_CopyAssignment;
1350 else if (D->isMoveAssignmentOperator())
1351 SMKind |= SMF_MoveAssignment;
1352
1353 // Update which trivial / non-trivial special members we have.
1354 // addedMember will have skipped this step for this member.
1355 if (D->isTrivial())
1356 data().HasTrivialSpecialMembers |= SMKind;
1357 else
1358 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1359}
1360
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001361void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1362 unsigned SMKind = 0;
1363
Eugene Zelenko7855e772018-04-03 00:11:50 +00001364 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001365 if (Constructor->isCopyConstructor())
1366 SMKind = SMF_CopyConstructor;
1367 else if (Constructor->isMoveConstructor())
1368 SMKind = SMF_MoveConstructor;
1369 } else if (isa<CXXDestructorDecl>(D))
1370 SMKind = SMF_Destructor;
1371
1372 if (D->isTrivialForCall())
1373 data().HasTrivialSpecialMembersForCall |= SMKind;
1374 else
1375 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1376}
1377
Joao Matose9a3ed42012-08-31 22:18:20 +00001378bool CXXRecordDecl::isCLike() const {
1379 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1380 !TemplateOrInstantiation.isNull())
1381 return false;
1382 if (!hasDefinition())
1383 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001384
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001385 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001386}
Fangrui Song6907ce22018-07-30 19:24:48 +00001387
1388bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001389 if (!isLambda()) return false;
1390 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001391}
1392
Richard Smith24253382018-08-02 20:30:52 +00001393#ifndef NDEBUG
1394static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1395 for (auto *D : R)
1396 if (!declaresSameEntity(D, R.front()))
1397 return false;
1398 return true;
1399}
1400#endif
1401
Erich Keane98f08992019-09-30 21:24:04 +00001402static NamedDecl* getLambdaCallOperatorHelper(const CXXRecordDecl &RD) {
Erich Keane5c2c60d2019-09-30 19:12:29 +00001403 if (!RD.isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001404 DeclarationName Name =
Erich Keane5c2c60d2019-09-30 19:12:29 +00001405 RD.getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
1406 DeclContext::lookup_result Calls = RD.lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001407
1408 assert(!Calls.empty() && "Missing lambda call operator!");
Richard Smith24253382018-08-02 20:30:52 +00001409 assert(allLookupResultsAreTheSame(Calls) &&
1410 "More than one lambda call operator!");
Erich Keane5c2c60d2019-09-30 19:12:29 +00001411 return Calls.front();
1412}
Fangrui Song6907ce22018-07-30 19:24:48 +00001413
Erich Keane5c2c60d2019-09-30 19:12:29 +00001414FunctionTemplateDecl* CXXRecordDecl::getDependentLambdaCallOperator() const {
1415 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001416 return dyn_cast_or_null<FunctionTemplateDecl>(CallOp);
Erich Keane5c2c60d2019-09-30 19:12:29 +00001417}
1418
1419CXXMethodDecl *CXXRecordDecl::getLambdaCallOperator() const {
1420 NamedDecl *CallOp = getLambdaCallOperatorHelper(*this);
Erich Keanea60ef722019-09-30 20:45:12 +00001421
1422 if (CallOp == nullptr)
1423 return nullptr;
1424
Eugene Zelenko7855e772018-04-03 00:11:50 +00001425 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001426 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001427
Faisal Vali2b391ab2013-09-26 19:54:12 +00001428 return cast<CXXMethodDecl>(CallOp);
1429}
1430
1431CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001432 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001433 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001434 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001435 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001436 if (Invoker.empty()) return nullptr;
Richard Smith24253382018-08-02 20:30:52 +00001437 assert(allLookupResultsAreTheSame(Invoker) &&
1438 "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001439 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001440 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001441 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001442
1443 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001444}
1445
Douglas Gregor9c702202012-02-10 07:45:31 +00001446void CXXRecordDecl::getCaptureFields(
1447 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001448 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001449 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001450 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001451
Douglas Gregorc8a73492012-02-13 15:44:47 +00001452 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001453 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001454 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001455 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001456 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001457 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001458 else if (C->capturesVariable())
1459 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001460 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001461 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001462}
1463
Fangrui Song6907ce22018-07-30 19:24:48 +00001464TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001465CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Hamza Sood8205a812019-05-04 10:49:46 +00001466 if (!isGenericLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001467 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001468 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1469 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001470 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001471}
Douglas Gregor9c702202012-02-10 07:45:31 +00001472
Hamza Sood8205a812019-05-04 10:49:46 +00001473ArrayRef<NamedDecl *>
1474CXXRecordDecl::getLambdaExplicitTemplateParameters() const {
1475 TemplateParameterList *List = getGenericLambdaTemplateParameterList();
1476 if (!List)
1477 return {};
1478
1479 assert(std::is_partitioned(List->begin(), List->end(),
1480 [](const NamedDecl *D) { return !D->isImplicit(); })
1481 && "Explicit template params should be ordered before implicit ones");
1482
Fangrui Song7264a472019-07-03 08:13:17 +00001483 const auto ExplicitEnd = llvm::partition_point(
1484 *List, [](const NamedDecl *D) { return !D->isImplicit(); });
Hamza Sood8205a812019-05-04 10:49:46 +00001485 return llvm::makeArrayRef(List->begin(), ExplicitEnd);
1486}
1487
Richard Smith0bae6242016-08-25 00:34:00 +00001488Decl *CXXRecordDecl::getLambdaContextDecl() const {
1489 assert(isLambda() && "Not a lambda closure type!");
1490 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1491 return getLambdaData().ContextDecl.get(Source);
1492}
1493
John McCall1e3a1a72010-03-15 09:07:48 +00001494static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001495 QualType T =
1496 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1497 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001498 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001499}
1500
John McCall1e3a1a72010-03-15 09:07:48 +00001501/// Collect the visible conversions of a base class.
1502///
James Dennettb5d5f812012-06-15 22:28:09 +00001503/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001504/// \param InVirtual whether this base class is a virtual base (or a base
1505/// of a virtual base)
1506/// \param Access the access along the inheritance path to this base
1507/// \param ParentHiddenTypes the conversions provided by the inheritors
1508/// of this base
1509/// \param Output the set to which to add conversions from non-virtual bases
1510/// \param VOutput the set to which to add conversions from virtual bases
1511/// \param HiddenVBaseCs the set of conversions which were hidden in a
1512/// virtual base along some inheritance path
1513static void CollectVisibleConversions(ASTContext &Context,
1514 CXXRecordDecl *Record,
1515 bool InVirtual,
1516 AccessSpecifier Access,
1517 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001518 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001519 UnresolvedSetImpl &VOutput,
1520 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1521 // The set of types which have conversions in this class or its
1522 // subclasses. As an optimization, we don't copy the derived set
1523 // unless it might change.
1524 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1525 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1526
1527 // Collect the direct conversions and figure out which conversions
1528 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001529 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1530 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1531 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001532 HiddenTypesBuffer = ParentHiddenTypes;
1533 HiddenTypes = &HiddenTypesBuffer;
1534
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001535 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001536 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1537 bool Hidden = ParentHiddenTypes.count(ConvType);
1538 if (!Hidden)
1539 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001540
1541 // If this conversion is hidden and we're in a virtual base,
1542 // remember that it's hidden along some inheritance path.
1543 if (Hidden && InVirtual)
1544 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1545
1546 // If this conversion isn't hidden, add it to the appropriate output.
1547 else if (!Hidden) {
1548 AccessSpecifier IAccess
1549 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1550
1551 if (InVirtual)
1552 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001553 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001554 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001555 }
1556 }
1557 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001558
John McCall1e3a1a72010-03-15 09:07:48 +00001559 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001560 for (const auto &I : Record->bases()) {
1561 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001562 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001563
John McCall1e3a1a72010-03-15 09:07:48 +00001564 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001565 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1566 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001567
Eugene Zelenko7855e772018-04-03 00:11:50 +00001568 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001569 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1570 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001571 }
John McCall1e3a1a72010-03-15 09:07:48 +00001572}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001573
John McCall1e3a1a72010-03-15 09:07:48 +00001574/// Collect the visible conversions of a class.
1575///
1576/// This would be extremely straightforward if it weren't for virtual
1577/// bases. It might be worth special-casing that, really.
1578static void CollectVisibleConversions(ASTContext &Context,
1579 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001580 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001581 // The collection of all conversions in virtual bases that we've
1582 // found. These will be added to the output as long as they don't
1583 // appear in the hidden-conversions set.
1584 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001585
John McCall1e3a1a72010-03-15 09:07:48 +00001586 // The set of conversions in virtual bases that we've determined to
1587 // be hidden.
1588 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1589
1590 // The set of types hidden by classes derived from this one.
1591 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1592
1593 // Go ahead and collect the direct conversions and add them to the
1594 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001595 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1596 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001597 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001598 for (; ConvI != ConvE; ++ConvI)
1599 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001600
1601 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001602 for (const auto &I : Record->bases()) {
1603 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001604 if (!RT) continue;
1605
1606 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001607 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001608 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1609 }
1610
1611 // Add any unhidden conversions provided by virtual bases.
1612 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1613 I != E; ++I) {
1614 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001615 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001616 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001617}
1618
1619/// getVisibleConversionFunctions - get all conversion functions visible
1620/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001621llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001622CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001623 ASTContext &Ctx = getASTContext();
1624
1625 ASTUnresolvedSet *Set;
1626 if (bases_begin() == bases_end()) {
1627 // If root class, all conversions are visible.
1628 Set = &data().Conversions.get(Ctx);
1629 } else {
1630 Set = &data().VisibleConversions.get(Ctx);
1631 // If visible conversion list is not evaluated, evaluate it.
1632 if (!data().ComputedVisibleConversions) {
1633 CollectVisibleConversions(Ctx, this, *Set);
1634 data().ComputedVisibleConversions = true;
1635 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001636 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001637 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001638}
1639
John McCallda4458e2010-03-31 01:36:47 +00001640void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1641 // This operation is O(N) but extremely rare. Sema only uses it to
1642 // remove UsingShadowDecls in a class that were followed by a direct
1643 // declaration, e.g.:
1644 // class A : B {
1645 // using B::operator int;
1646 // operator int();
1647 // };
1648 // This is uncommon by itself and even more uncommon in conjunction
1649 // with sufficiently large numbers of directly-declared conversions
1650 // that asymptotic behavior matters.
1651
Richard Smitha4ba74c2013-08-30 04:46:40 +00001652 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001653 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1654 if (Convs[I].getDecl() == ConvDecl) {
1655 Convs.erase(I);
Fangrui Song75e74e02019-03-31 08:48:19 +00001656 assert(llvm::find(Convs, ConvDecl) == Convs.end() &&
1657 "conversion was found multiple times in unresolved set");
John McCallda4458e2010-03-31 01:36:47 +00001658 return;
1659 }
1660 }
1661
1662 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001663}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001664
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001665CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001666 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001667 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001668
1669 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001670}
1671
Chandler Carruth21c90602015-12-30 03:24:14 +00001672MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1673 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1674}
1675
Fangrui Song6907ce22018-07-30 19:24:48 +00001676void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001677CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1678 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001679 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001680 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001681 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001682 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001683 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1684}
1685
Chandler Carruth21c90602015-12-30 03:24:14 +00001686ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1687 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1688}
1689
1690void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1691 TemplateOrInstantiation = Template;
1692}
1693
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001694TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001695 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001696 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001697
Douglas Gregor06db9f52009-10-12 20:18:28 +00001698 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001699 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001700
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001701 return TSK_Undeclared;
1702}
1703
Fangrui Song6907ce22018-07-30 19:24:48 +00001704void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001705CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001706 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001707 Spec->setSpecializationKind(TSK);
1708 return;
1709 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001710
Douglas Gregor06db9f52009-10-12 20:18:28 +00001711 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001712 MSInfo->setTemplateSpecializationKind(TSK);
1713 return;
1714 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001715
David Blaikie83d382b2011-09-23 05:06:16 +00001716 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001717}
1718
Reid Klecknere7367d62014-10-14 20:28:40 +00001719const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001720 auto GetDefinitionOrSelf =
1721 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1722 if (auto *Def = D->getDefinition())
1723 return Def;
1724 return D;
1725 };
1726
Reid Klecknere7367d62014-10-14 20:28:40 +00001727 // If it's a class template specialization, find the template or partial
1728 // specialization from which it was instantiated.
1729 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1730 auto From = TD->getInstantiatedFrom();
1731 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1732 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1733 if (NewCTD->isMemberSpecialization())
1734 break;
1735 CTD = NewCTD;
1736 }
Richard Smith2195ec92017-04-21 01:15:13 +00001737 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001738 }
1739 if (auto *CTPSD =
1740 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1741 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1742 if (NewCTPSD->isMemberSpecialization())
1743 break;
1744 CTPSD = NewCTPSD;
1745 }
Richard Smith2195ec92017-04-21 01:15:13 +00001746 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001747 }
1748 }
1749
1750 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1751 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1752 const CXXRecordDecl *RD = this;
1753 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1754 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001755 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001756 }
1757 }
1758
1759 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1760 "couldn't find pattern for class template instantiation");
1761 return nullptr;
1762}
1763
Douglas Gregorbac74902010-07-01 14:13:13 +00001764CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1765 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001766 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001767
1768 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001769 = Context.DeclarationNames.getCXXDestructorName(
1770 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001771
Richard Smithcf4bdde2015-02-21 02:45:19 +00001772 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001773
Hubert Tong3cede512017-06-30 22:43:54 +00001774 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001775}
1776
Richard Trieu95a192a2015-05-28 00:14:02 +00001777bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1778 // Destructor is noreturn.
1779 if (const CXXDestructorDecl *Destructor = getDestructor())
1780 if (Destructor->isNoReturn())
1781 return true;
1782
1783 // Check base classes destructor for noreturn.
1784 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001785 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1786 if (RD->isAnyDestructorNoReturn())
1787 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001788
1789 // Check fields for noreturn.
1790 for (const auto *Field : fields())
1791 if (const CXXRecordDecl *RD =
1792 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1793 if (RD->isAnyDestructorNoReturn())
1794 return true;
1795
1796 // All destructors are not noreturn.
1797 return false;
1798}
1799
Erich Keane3e08f662017-09-29 21:06:00 +00001800static bool isDeclContextInNamespace(const DeclContext *DC) {
1801 while (!DC->isTranslationUnit()) {
1802 if (DC->isNamespace())
1803 return true;
1804 DC = DC->getParent();
1805 }
1806 return false;
1807}
1808
Erich Keane58bd6032017-09-15 16:03:35 +00001809bool CXXRecordDecl::isInterfaceLike() const {
1810 assert(hasDefinition() && "checking for interface-like without a definition");
1811 // All __interfaces are inheritently interface-like.
1812 if (isInterface())
1813 return true;
1814
1815 // Interface-like types cannot have a user declared constructor, destructor,
1816 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1817 // cannot be interface types.
1818 if (isLambda() || hasUserDeclaredConstructor() ||
1819 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1820 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1821 return false;
1822
1823 // No interface-like type can have a method with a definition.
1824 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001825 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001826 return false;
1827
1828 // Check "Special" types.
1829 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001830 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1831 // extern C++ block directly in the TU. These are only valid if in one
1832 // of these two situations.
1833 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1834 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001835 ((getName() == "IUnknown" &&
1836 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1837 (getName() == "IDispatch" &&
1838 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1839 if (getNumBases() > 0)
1840 return false;
1841 return true;
1842 }
1843
1844 // FIXME: Any access specifiers is supposed to make this no longer interface
1845 // like.
1846
1847 // If this isn't a 'special' type, it must have a single interface-like base.
1848 if (getNumBases() != 1)
1849 return false;
1850
1851 const auto BaseSpec = *bases_begin();
1852 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1853 return false;
1854 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1855 if (Base->isInterface() || !Base->isInterfaceLike())
1856 return false;
1857 return true;
1858}
1859
Douglas Gregorb11aad82011-02-19 18:51:44 +00001860void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001861 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001862}
1863
1864void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1865 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001866
Douglas Gregor8fb95122010-09-29 00:15:42 +00001867 // If the class may be abstract (but hasn't been marked as such), check for
1868 // any pure final overriders.
1869 if (mayBeAbstract()) {
1870 CXXFinalOverriderMap MyFinalOverriders;
1871 if (!FinalOverriders) {
1872 getFinalOverriders(MyFinalOverriders);
1873 FinalOverriders = &MyFinalOverriders;
1874 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001875
Douglas Gregor8fb95122010-09-29 00:15:42 +00001876 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001877 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001878 MEnd = FinalOverriders->end();
1879 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001880 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001881 SOEnd = M->second.end();
1882 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001883 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001884 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001885
Douglas Gregor8fb95122010-09-29 00:15:42 +00001886 // C++ [class.abstract]p4:
1887 // A class is abstract if it contains or inherits at least one
1888 // pure virtual function for which the final overrider is pure
1889 // virtual.
1890 if (SO->second.front().Method->isPure()) {
1891 data().Abstract = true;
1892 Done = true;
1893 break;
1894 }
1895 }
1896 }
1897 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001898
Douglas Gregor457104e2010-09-29 04:25:11 +00001899 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001900 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001901 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001902 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001903}
1904
1905bool CXXRecordDecl::mayBeAbstract() const {
1906 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1907 isDependentContext())
1908 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001909
Aaron Ballman574705e2014-03-13 15:41:46 +00001910 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001911 const auto *BaseDecl =
1912 cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001913 if (BaseDecl->isAbstract())
1914 return true;
1915 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001916
Douglas Gregor8fb95122010-09-29 00:15:42 +00001917 return false;
1918}
1919
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001920void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001921
Richard Smith76b90272019-05-09 03:59:21 +00001922bool ExplicitSpecifier::isEquivalent(const ExplicitSpecifier Other) const {
1923 if ((getKind() != Other.getKind() ||
1924 getKind() == ExplicitSpecKind::Unresolved)) {
1925 if (getKind() == ExplicitSpecKind::Unresolved &&
1926 Other.getKind() == ExplicitSpecKind::Unresolved) {
1927 ODRHash SelfHash, OtherHash;
1928 SelfHash.AddStmt(getExpr());
1929 OtherHash.AddStmt(Other.getExpr());
1930 return SelfHash.CalculateHash() == OtherHash.CalculateHash();
1931 } else
1932 return false;
1933 }
1934 return true;
1935}
1936
1937ExplicitSpecifier ExplicitSpecifier::getFromDecl(FunctionDecl *Function) {
1938 switch (Function->getDeclKind()) {
1939 case Decl::Kind::CXXConstructor:
1940 return cast<CXXConstructorDecl>(Function)->getExplicitSpecifier();
1941 case Decl::Kind::CXXConversion:
1942 return cast<CXXConversionDecl>(Function)->getExplicitSpecifier();
1943 case Decl::Kind::CXXDeductionGuide:
1944 return cast<CXXDeductionGuideDecl>(Function)->getExplicitSpecifier();
1945 default:
1946 return {};
1947 }
1948}
1949
Richard Smithbc491202017-02-17 20:05:37 +00001950CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
Richard Smith76b90272019-05-09 03:59:21 +00001951 ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
1952 ExplicitSpecifier ES, const DeclarationNameInfo &NameInfo, QualType T,
1953 TypeSourceInfo *TInfo, SourceLocation EndLocation) {
1954 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, ES, NameInfo, T,
1955 TInfo, EndLocation);
Richard Smithbc491202017-02-17 20:05:37 +00001956}
1957
1958CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1959 unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00001960 return new (C, ID) CXXDeductionGuideDecl(
1961 C, nullptr, SourceLocation(), ExplicitSpecifier(), DeclarationNameInfo(),
1962 QualType(), nullptr, SourceLocation());
Richard Smithbc491202017-02-17 20:05:37 +00001963}
1964
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001965void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001966
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001967bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001968 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001969
1970 if (MD->getStorageClass() == SC_Static)
1971 return true;
1972
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001973 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1974 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001975}
1976
Rafael Espindola49e860b2012-06-26 17:45:31 +00001977static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1978 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001979 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001980 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1981 return true;
1982 if (recursivelyOverrides(MD, BaseMD))
1983 return true;
1984 }
1985 return false;
1986}
1987
1988CXXMethodDecl *
Richard Smith921f1322019-05-13 23:35:21 +00001989CXXMethodDecl::getCorrespondingMethodDeclaredInClass(const CXXRecordDecl *RD,
1990 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001991 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1992 return this;
1993
1994 // Lookup doesn't work for destructors, so handle them separately.
1995 if (isa<CXXDestructorDecl>(this)) {
1996 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001997 if (MD) {
1998 if (recursivelyOverrides(MD, this))
1999 return MD;
2000 if (MayBeBase && recursivelyOverrides(this, MD))
2001 return MD;
2002 }
Craig Topper36250ad2014-05-12 05:36:57 +00002003 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002004 }
2005
Richard Smith40c78062015-02-21 02:31:57 +00002006 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002007 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00002008 if (!MD)
2009 continue;
2010 if (recursivelyOverrides(MD, this))
2011 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00002012 if (MayBeBase && recursivelyOverrides(this, MD))
2013 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002014 }
2015
Richard Smith921f1322019-05-13 23:35:21 +00002016 return nullptr;
2017}
2018
2019CXXMethodDecl *
2020CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
2021 bool MayBeBase) {
2022 if (auto *MD = getCorrespondingMethodDeclaredInClass(RD, MayBeBase))
2023 return MD;
2024
Aaron Ballman574705e2014-03-13 15:41:46 +00002025 for (const auto &I : RD->bases()) {
2026 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00002027 if (!RT)
2028 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002029 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00002030 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
2031 if (T)
2032 return T;
2033 }
2034
Craig Topper36250ad2014-05-12 05:36:57 +00002035 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00002036}
2037
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002038CXXMethodDecl *CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
2039 SourceLocation StartLoc,
2040 const DeclarationNameInfo &NameInfo,
2041 QualType T, TypeSourceInfo *TInfo,
2042 StorageClass SC, bool isInline,
2043 ConstexprSpecKind ConstexprKind,
2044 SourceLocation EndLocation) {
2045 return new (C, RD)
2046 CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo, T, TInfo, SC,
2047 isInline, ConstexprKind, EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00002048}
2049
Douglas Gregor72172e92012-01-05 21:55:30 +00002050CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002051 return new (C, ID) CXXMethodDecl(
2052 CXXMethod, C, nullptr, SourceLocation(), DeclarationNameInfo(),
2053 QualType(), nullptr, SC_None, false, CSK_unspecified, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002054}
2055
Akira Hatanaka22461672017-07-13 06:08:27 +00002056CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
2057 bool IsAppleKext) {
2058 assert(isVirtual() && "this method is expected to be virtual");
2059
2060 // When building with -fapple-kext, all calls must go through the vtable since
2061 // the kernel linker can do runtime patching of vtables.
2062 if (IsAppleKext)
2063 return nullptr;
2064
2065 // If the member function is marked 'final', we know that it can't be
2066 // overridden and can therefore devirtualize it unless it's pure virtual.
2067 if (hasAttr<FinalAttr>())
2068 return isPure() ? nullptr : this;
2069
2070 // If Base is unknown, we cannot devirtualize.
2071 if (!Base)
2072 return nullptr;
2073
2074 // If the base expression (after skipping derived-to-base conversions) is a
2075 // class prvalue, then we can devirtualize.
2076 Base = Base->getBestDynamicClassTypeExpr();
2077 if (Base->isRValue() && Base->getType()->isRecordType())
2078 return this;
2079
2080 // If we don't even know what we would call, we can't devirtualize.
2081 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
2082 if (!BestDynamicDecl)
2083 return nullptr;
2084
2085 // There may be a method corresponding to MD in a derived class.
2086 CXXMethodDecl *DevirtualizedMethod =
2087 getCorrespondingMethodInClass(BestDynamicDecl);
2088
2089 // If that method is pure virtual, we can't devirtualize. If this code is
2090 // reached, the result would be UB, not a direct call to the derived class
2091 // function, and we can't assume the derived class function is defined.
2092 if (DevirtualizedMethod->isPure())
2093 return nullptr;
2094
2095 // If that method is marked final, we can devirtualize it.
2096 if (DevirtualizedMethod->hasAttr<FinalAttr>())
2097 return DevirtualizedMethod;
2098
David Bolvansky20b27082019-08-31 18:52:44 +00002099 // Similarly, if the class itself or its destructor is marked 'final',
2100 // the class can't be derived from and we can therefore devirtualize the
2101 // member function call.
Akira Hatanaka22461672017-07-13 06:08:27 +00002102 if (BestDynamicDecl->hasAttr<FinalAttr>())
2103 return DevirtualizedMethod;
David Bolvansky20b27082019-08-31 18:52:44 +00002104 if (const auto *dtor = BestDynamicDecl->getDestructor()) {
2105 if (dtor->hasAttr<FinalAttr>())
2106 return DevirtualizedMethod;
2107 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002108
Eugene Zelenko7855e772018-04-03 00:11:50 +00002109 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2110 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00002111 if (VD->getType()->isRecordType())
2112 // This is a record decl. We know the type and can devirtualize it.
2113 return DevirtualizedMethod;
2114
2115 return nullptr;
2116 }
2117
2118 // We can devirtualize calls on an object accessed by a class member access
2119 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2120 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002121 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002122 const ValueDecl *VD = ME->getMemberDecl();
2123 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2124 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002125
2126 // Likewise for calls on an object accessed by a (non-reference) pointer to
2127 // member access.
2128 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2129 if (BO->isPtrMemOp()) {
2130 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2131 if (MPT->getPointeeType()->isRecordType())
2132 return DevirtualizedMethod;
2133 }
2134 }
2135
2136 // We can't devirtualize the call.
2137 return nullptr;
2138}
2139
Artem Belevich78929ef2018-09-21 17:29:33 +00002140bool CXXMethodDecl::isUsualDeallocationFunction(
2141 SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2142 assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002143 if (getOverloadedOperator() != OO_Delete &&
2144 getOverloadedOperator() != OO_Array_Delete)
2145 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002146
2147 // C++ [basic.stc.dynamic.deallocation]p2:
2148 // A template instance is never a usual deallocation function,
2149 // regardless of its signature.
2150 if (getPrimaryTemplate())
2151 return false;
2152
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002153 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002154 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002155 // with exactly one parameter, then that function is a usual (non-placement)
2156 // deallocation function. [...]
2157 if (getNumParams() == 1)
2158 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002159 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002160
Richard Smith5b349582017-10-13 01:55:36 +00002161 // C++ P0722:
2162 // A destroying operator delete is a usual deallocation function if
2163 // removing the std::destroying_delete_t parameter and changing the
2164 // first parameter type from T* to void* results in the signature of
2165 // a usual deallocation function.
2166 if (isDestroyingOperatorDelete())
2167 ++UsualParams;
2168
Richard Smithb2f0f052016-10-10 18:54:32 +00002169 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002170 // [...] If class T does not declare such an operator delete but does
2171 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002172 // exactly two parameters, the second of which has type std::size_t (18.1),
2173 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002174 //
2175 // C++17 says a usual deallocation function is one with the signature
2176 // (void* [, size_t] [, std::align_val_t] [, ...])
2177 // and all such functions are usual deallocation functions. It's not clear
2178 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002179 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002180 if (UsualParams < getNumParams() &&
2181 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2182 Context.getSizeType()))
2183 ++UsualParams;
2184
2185 if (UsualParams < getNumParams() &&
2186 getParamDecl(UsualParams)->getType()->isAlignValT())
2187 ++UsualParams;
2188
2189 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002190 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002191
2192 // In C++17 onwards, all potential usual deallocation functions are actual
Eric Fiselier24a2a482019-02-03 03:44:31 +00002193 // usual deallocation functions. Honor this behavior when post-C++14
2194 // deallocation functions are offered as extensions too.
2195 // FIXME(EricWF): Destrying Delete should be a language option. How do we
2196 // handle when destroying delete is used prior to C++17?
2197 if (Context.getLangOpts().CPlusPlus17 ||
2198 Context.getLangOpts().AlignedAllocation ||
2199 isDestroyingOperatorDelete())
Richard Smithb2f0f052016-10-10 18:54:32 +00002200 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002201
2202 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002203 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002204 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
Artem Belevich78929ef2018-09-21 17:29:33 +00002205 bool Result = true;
2206 for (const auto *D : R) {
2207 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2208 if (FD->getNumParams() == 1) {
2209 PreventedBy.push_back(FD);
2210 Result = false;
2211 }
2212 }
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002213 }
Artem Belevich78929ef2018-09-21 17:29:33 +00002214 return Result;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002215}
2216
Douglas Gregorb139cd52010-05-01 20:49:11 +00002217bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002218 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002219 // A user-declared copy assignment operator X::operator= is a non-static
2220 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002221 // type X, X&, const X&, volatile X& or const volatile X&.
2222 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002223 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002224 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2225 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002226 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002227
Douglas Gregorb139cd52010-05-01 20:49:11 +00002228 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002229 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002230 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002231
Douglas Gregorb139cd52010-05-01 20:49:11 +00002232 ASTContext &Context = getASTContext();
2233 QualType ClassType
2234 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2235 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2236}
2237
Alexis Huntfcaeae42011-05-25 20:50:04 +00002238bool CXXMethodDecl::isMoveAssignmentOperator() const {
2239 // C++0x [class.copy]p19:
2240 // A user-declared move assignment operator X::operator= is a non-static
2241 // non-template member function of class X with exactly one parameter of type
2242 // X&&, const X&&, volatile X&&, or const volatile X&&.
2243 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002244 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2245 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002246 return false;
2247
2248 QualType ParamType = getParamDecl(0)->getType();
2249 if (!isa<RValueReferenceType>(ParamType))
2250 return false;
2251 ParamType = ParamType->getPointeeType();
2252
2253 ASTContext &Context = getASTContext();
2254 QualType ClassType
2255 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2256 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2257}
2258
Anders Carlsson36d87e12009-05-16 23:58:37 +00002259void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002260 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002261 assert(!MD->getParent()->isDependentContext() &&
2262 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002263 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002264
Douglas Gregor832940b2010-03-02 23:58:15 +00002265 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002266}
2267
2268CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002269 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002270 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002271}
2272
2273CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002274 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002275 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002276}
2277
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002278unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002279 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002280 return getASTContext().overridden_methods_size(this);
2281}
2282
Clement Courbet6ecaec82016-07-05 07:49:31 +00002283CXXMethodDecl::overridden_method_range
2284CXXMethodDecl::overridden_methods() const {
2285 if (isa<CXXConstructorDecl>(this))
2286 return overridden_method_range(nullptr, nullptr);
2287 return getASTContext().overridden_methods(this);
2288}
2289
Marco Antognini88559632019-07-22 09:39:13 +00002290static QualType getThisObjectType(ASTContext &C, const FunctionProtoType *FPT,
2291 const CXXRecordDecl *Decl) {
2292 QualType ClassTy = C.getTypeDeclType(Decl);
2293 return C.getQualifiedType(ClassTy, FPT->getMethodQuals());
2294}
2295
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002296QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2297 const CXXRecordDecl *Decl) {
2298 ASTContext &C = Decl->getASTContext();
Marco Antognini88559632019-07-22 09:39:13 +00002299 QualType ObjectTy = ::getThisObjectType(C, FPT, Decl);
2300 return C.getPointerType(ObjectTy);
2301}
2302
2303QualType CXXMethodDecl::getThisObjectType(const FunctionProtoType *FPT,
2304 const CXXRecordDecl *Decl) {
2305 ASTContext &C = Decl->getASTContext();
2306 return ::getThisObjectType(C, FPT, Decl);
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002307}
2308
Brian Gesiak5488ab42019-01-11 01:54:53 +00002309QualType CXXMethodDecl::getThisType() const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002310 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2311 // If the member function is declared const, the type of this is const X*,
2312 // if the member function is declared volatile, the type of this is
2313 // volatile X*, and if the member function is declared const volatile,
2314 // the type of this is const volatile X*.
Ted Kremenek21475702008-09-05 17:16:31 +00002315 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002316
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002317 return CXXMethodDecl::getThisType(getType()->getAs<FunctionProtoType>(),
2318 getParent());
Ted Kremenek21475702008-09-05 17:16:31 +00002319}
2320
Marco Antognini88559632019-07-22 09:39:13 +00002321QualType CXXMethodDecl::getThisObjectType() const {
2322 // Ditto getThisType.
2323 assert(isInstance() && "No 'this' for static methods!");
2324
2325 return CXXMethodDecl::getThisObjectType(getType()->getAs<FunctionProtoType>(),
2326 getParent());
2327}
2328
Eli Friedman71a26d82009-12-06 20:50:05 +00002329bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002330 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002331 // which it was instantiated.
2332 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2333 if (!CheckFn)
2334 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002335
Eli Friedman71a26d82009-12-06 20:50:05 +00002336 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002337 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2338 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002339}
2340
Douglas Gregor355efbb2012-02-17 03:02:34 +00002341bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002342 const CXXRecordDecl *P = getParent();
2343 if (P->isLambda()) {
2344 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2345 if (StaticInvoker == this) return true;
2346 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2347 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2348 }
2349 }
2350 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002351}
2352
Alexis Hunt1d792652011-01-08 20:30:50 +00002353CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2354 TypeSourceInfo *TInfo, bool IsVirtual,
2355 SourceLocation L, Expr *Init,
2356 SourceLocation R,
2357 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002358 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2359 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2360 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002361
Alexis Hunt1d792652011-01-08 20:30:50 +00002362CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2363 FieldDecl *Member,
2364 SourceLocation MemberLoc,
2365 SourceLocation L, Expr *Init,
2366 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002367 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2368 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2369 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002370
Alexis Hunt1d792652011-01-08 20:30:50 +00002371CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2372 IndirectFieldDecl *Member,
2373 SourceLocation MemberLoc,
2374 SourceLocation L, Expr *Init,
2375 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002376 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2377 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2378 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002379
Alexis Hunt1d792652011-01-08 20:30:50 +00002380CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002381 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002382 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002383 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002384 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2385 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002386
Artem Dergachevd3942be2018-12-03 22:15:34 +00002387int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +00002388 return Context.getAllocator()
2389 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
Artem Dergachevd3942be2018-12-03 22:15:34 +00002390}
2391
Alexis Hunt1d792652011-01-08 20:30:50 +00002392TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002393 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002394 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002395 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002396 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002397}
2398
Alexis Hunt1d792652011-01-08 20:30:50 +00002399const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002400 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002401 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002402 else
Craig Topper36250ad2014-05-12 05:36:57 +00002403 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002404}
2405
Alexis Hunt1d792652011-01-08 20:30:50 +00002406SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002407 if (isInClassMemberInitializer())
2408 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002409
David Blaikiea81d4102015-01-18 00:12:58 +00002410 if (isAnyMemberInitializer())
2411 return getMemberLocation();
2412
Eugene Zelenko7855e772018-04-03 00:11:50 +00002413 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002414 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002415
Eugene Zelenko7855e772018-04-03 00:11:50 +00002416 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002417}
2418
Alexis Hunt1d792652011-01-08 20:30:50 +00002419SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002420 if (isInClassMemberInitializer()) {
2421 FieldDecl *D = getAnyMember();
2422 if (Expr *I = D->getInClassInitializer())
2423 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002424 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002425 }
2426
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002427 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002428}
2429
Erich Keane9c665062018-08-01 21:02:40 +00002430CXXConstructorDecl::CXXConstructorDecl(
2431 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2432 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Richard Smith76b90272019-05-09 03:59:21 +00002433 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002434 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited)
Erich Keane9c665062018-08-01 21:02:40 +00002435 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002436 SC_None, isInline, ConstexprKind, SourceLocation()) {
Erich Keane9c665062018-08-01 21:02:40 +00002437 setNumCtorInitializers(0);
2438 setInheritingConstructor(static_cast<bool>(Inherited));
2439 setImplicit(isImplicitlyDeclared);
Richard Smith76b90272019-05-09 03:59:21 +00002440 CXXConstructorDeclBits.HasTrailingExplicitSpecifier = ES.getExpr() ? 1 : 0;
Erich Keane9c665062018-08-01 21:02:40 +00002441 if (Inherited)
2442 *getTrailingObjects<InheritedConstructor>() = Inherited;
Richard Smith76b90272019-05-09 03:59:21 +00002443 setExplicitSpecifier(ES);
Erich Keane9c665062018-08-01 21:02:40 +00002444}
2445
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002446void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002447
Richard Smith5179eb72016-06-28 19:03:57 +00002448CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2449 unsigned ID,
Richard Smith76b90272019-05-09 03:59:21 +00002450 uint64_t AllocKind) {
2451 bool hasTraillingExplicit = static_cast<bool>(AllocKind & TAKHasTailExplicit);
2452 bool isInheritingConstructor =
2453 static_cast<bool>(AllocKind & TAKInheritsConstructor);
2454 unsigned Extra =
2455 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2456 isInheritingConstructor, hasTraillingExplicit);
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002457 auto *Result = new (C, ID, Extra)
2458 CXXConstructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2459 QualType(), nullptr, ExplicitSpecifier(), false, false,
2460 CSK_unspecified, InheritedConstructor());
Richard Smith76b90272019-05-09 03:59:21 +00002461 Result->setInheritingConstructor(isInheritingConstructor);
2462 Result->CXXConstructorDeclBits.HasTrailingExplicitSpecifier =
2463 hasTraillingExplicit;
2464 Result->setExplicitSpecifier(ExplicitSpecifier());
Richard Smith5179eb72016-06-28 19:03:57 +00002465 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002466}
2467
Richard Smith76b90272019-05-09 03:59:21 +00002468CXXConstructorDecl *CXXConstructorDecl::Create(
2469 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2470 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2471 ExplicitSpecifier ES, bool isInline, bool isImplicitlyDeclared,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002472 ConstexprSpecKind ConstexprKind, InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002473 assert(NameInfo.getName().getNameKind()
2474 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002475 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002476 unsigned Extra =
Richard Smith76b90272019-05-09 03:59:21 +00002477 additionalSizeToAlloc<InheritedConstructor, ExplicitSpecifier>(
2478 Inherited ? 1 : 0, ES.getExpr() ? 1 : 0);
2479 return new (C, RD, Extra)
2480 CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, ES, isInline,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002481 isImplicitlyDeclared, ConstexprKind, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002482}
2483
Richard Smithc2bb8182015-03-24 06:36:48 +00002484CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2485 return CtorInitializers.get(getASTContext().getExternalSource());
2486}
2487
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002488CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2489 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2490 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002491 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002492 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002493
2494 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002495}
2496
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002497bool CXXConstructorDecl::isDefaultConstructor() const {
2498 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002499 // A default constructor for a class X is a constructor of class
2500 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002501 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002502 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002503}
2504
Mike Stump11289f42009-09-09 15:08:12 +00002505bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002506CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002507 return isCopyOrMoveConstructor(TypeQuals) &&
2508 getParamDecl(0)->getType()->isLValueReferenceType();
2509}
2510
2511bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2512 return isCopyOrMoveConstructor(TypeQuals) &&
2513 getParamDecl(0)->getType()->isRValueReferenceType();
2514}
2515
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002516/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002517bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002518 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002519 // A non-template constructor for class X is a copy constructor
2520 // if its first parameter is of type X&, const X&, volatile X& or
2521 // const volatile X&, and either there are no other parameters
2522 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002523 // C++0x [class.copy]p3:
2524 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002525 // first parameter is of type X&&, const X&&, volatile X&&, or
2526 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002527 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002528 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002529 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002530 (getPrimaryTemplate() != nullptr) ||
2531 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002532 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002533
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002534 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002535
2536 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002537 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002538 if (!ParamRefType)
2539 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002540
Douglas Gregorff7028a2009-11-13 23:59:09 +00002541 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002542 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002543
Douglas Gregorff7028a2009-11-13 23:59:09 +00002544 CanQualType PointeeType
2545 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002546 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002547 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002548 if (PointeeType.getUnqualifiedType() != ClassTy)
2549 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002550
John McCall8ccfcb52009-09-24 19:53:00 +00002551 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002552
Douglas Gregorf282a762011-01-21 19:38:21 +00002553 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002554 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002555 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002556}
2557
Anders Carlssond20e7952009-08-28 16:57:08 +00002558bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002559 // C++ [class.conv.ctor]p1:
2560 // A constructor declared without the function-specifier explicit
2561 // that can be called with a single parameter specifies a
2562 // conversion from the type of its first parameter to the type of
2563 // its class. Such a constructor is called a converting
2564 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002565 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002566 return false;
2567
Mike Stump11289f42009-09-09 15:08:12 +00002568 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00002569 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002570 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002571 (getNumParams() > 1 &&
2572 (getParamDecl(1)->hasDefaultArg() ||
2573 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002574}
Douglas Gregor61956c42008-10-31 09:07:45 +00002575
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002576bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002577 if ((getNumParams() < 1) ||
2578 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002579 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002580 return false;
2581
2582 const ParmVarDecl *Param = getParamDecl(0);
2583
2584 ASTContext &Context = getASTContext();
2585 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002586
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002587 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002588 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002589 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2590 if (ParamType.getUnqualifiedType() != ClassTy)
2591 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002592
2593 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002594}
2595
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002596void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002597
Douglas Gregor831c93f2008-11-05 20:51:48 +00002598CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002599CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002600 return new (C, ID)
2601 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
Richard Smith457226e2019-09-23 03:48:44 +00002602 QualType(), nullptr, false, false, CSK_unspecified);
Chris Lattnerca025db2010-05-07 21:43:38 +00002603}
2604
Richard Smith457226e2019-09-23 03:48:44 +00002605CXXDestructorDecl *CXXDestructorDecl::Create(
2606 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2607 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2608 bool isInline, bool isImplicitlyDeclared, ConstexprSpecKind ConstexprKind) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002609 assert(NameInfo.getName().getNameKind()
2610 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002611 "Name must refer to a destructor");
Richard Smith457226e2019-09-23 03:48:44 +00002612 return new (C, RD)
2613 CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline,
2614 isImplicitlyDeclared, ConstexprKind);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002615}
2616
Richard Smith5b349582017-10-13 01:55:36 +00002617void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002618 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2619 if (OD && !First->OperatorDelete) {
2620 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002621 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002622 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002623 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002624 }
2625}
2626
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002627void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002628
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002629CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002630CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith76b90272019-05-09 03:59:21 +00002631 return new (C, ID) CXXConversionDecl(
2632 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002633 false, ExplicitSpecifier(), CSK_unspecified, SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002634}
2635
Richard Smith76b90272019-05-09 03:59:21 +00002636CXXConversionDecl *CXXConversionDecl::Create(
2637 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2638 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002639 bool isInline, ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind,
Richard Smith76b90272019-05-09 03:59:21 +00002640 SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002641 assert(NameInfo.getName().getNameKind()
2642 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002643 "Name must refer to a conversion function");
Gauthier Harnisch796ed032019-06-14 08:56:20 +00002644 return new (C, RD)
2645 CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo, isInline, ES,
2646 ConstexprKind, EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002647}
2648
Douglas Gregord3b672c2012-02-16 01:06:16 +00002649bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2650 return isImplicit() && getParent()->isLambda() &&
2651 getConversionType()->isBlockPointerType();
2652}
2653
Erich Keanec9d29902018-08-01 21:16:54 +00002654LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2655 SourceLocation LangLoc, LanguageIDs lang,
2656 bool HasBraces)
2657 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2658 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2659 setLanguage(lang);
2660 LinkageSpecDeclBits.HasBraces = HasBraces;
2661}
2662
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002663void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002664
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002665LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002666 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002667 SourceLocation ExternLoc,
2668 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002669 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002670 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002671 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002672}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002673
Richard Smithf7981722013-11-22 09:01:48 +00002674LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2675 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002676 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2677 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002678}
2679
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002680void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002681
Douglas Gregor889ceb72009-02-03 19:21:40 +00002682UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2683 SourceLocation L,
2684 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002685 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002686 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002687 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002688 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002689 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002690 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002691 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2692 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002693}
2694
Richard Smithf7981722013-11-22 09:01:48 +00002695UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2696 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002697 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2698 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002699 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002700 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002701}
2702
Sebastian Redla6602e92009-11-23 15:34:23 +00002703NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002704 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002705 return NA->getNamespace();
2706 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2707}
2708
Richard Smith053f6c62014-05-16 23:01:30 +00002709NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2710 SourceLocation StartLoc, SourceLocation IdLoc,
2711 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2712 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002713 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002714 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002715 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002716
Douglas Gregore57e7522012-01-07 09:11:48 +00002717 if (PrevDecl)
2718 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2719}
2720
Douglas Gregor72172e92012-01-05 21:55:30 +00002721NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002722 bool Inline, SourceLocation StartLoc,
2723 SourceLocation IdLoc, IdentifierInfo *Id,
2724 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002725 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2726 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002727}
2728
2729NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002730 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002731 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002732}
2733
Chandler Carruth21c90602015-12-30 03:24:14 +00002734NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2735 if (isFirstDecl())
2736 return this;
2737
2738 return AnonOrFirstNamespaceAndInline.getPointer();
2739}
2740
2741const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2742 if (isFirstDecl())
2743 return this;
2744
2745 return AnonOrFirstNamespaceAndInline.getPointer();
2746}
2747
2748bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2749
Richard Smithd7af8a32014-05-10 01:17:36 +00002750NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2751 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002752}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002753
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002754NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2755 return getPreviousDecl();
2756}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002757
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002758NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2759 return getMostRecentDecl();
2760}
2761
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002762void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002763
Richard Smithf4634362014-09-03 23:11:22 +00002764NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2765 return getNextRedeclaration();
2766}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002767
Richard Smithf4634362014-09-03 23:11:22 +00002768NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2769 return getPreviousDecl();
2770}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002771
Richard Smithf4634362014-09-03 23:11:22 +00002772NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2773 return getMostRecentDecl();
2774}
2775
Mike Stump11289f42009-09-09 15:08:12 +00002776NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002777 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002778 SourceLocation AliasLoc,
2779 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002780 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002781 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002782 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002783 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002784 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002785 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002786 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002787 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002788}
2789
Douglas Gregor72172e92012-01-05 21:55:30 +00002790NamespaceAliasDecl *
2791NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002792 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002793 SourceLocation(), nullptr,
2794 NestedNameSpecifierLoc(),
2795 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002796}
2797
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002798void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002799
Richard Smith5179eb72016-06-28 19:03:57 +00002800UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2801 SourceLocation Loc, UsingDecl *Using,
2802 NamedDecl *Target)
2803 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002804 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002805 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002806 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002807 setImplicit();
2808}
2809
2810UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2811 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002812 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002813
Douglas Gregor72172e92012-01-05 21:55:30 +00002814UsingShadowDecl *
2815UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002816 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002817}
2818
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002819UsingDecl *UsingShadowDecl::getUsingDecl() const {
2820 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002821 while (const auto *NextShadow =
2822 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002823 Shadow = NextShadow;
2824 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2825}
2826
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002827void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002828
2829ConstructorUsingShadowDecl *
2830ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2831 SourceLocation Loc, UsingDecl *Using,
2832 NamedDecl *Target, bool IsVirtual) {
2833 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2834 IsVirtual);
2835}
2836
2837ConstructorUsingShadowDecl *
2838ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2839 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2840}
2841
2842CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2843 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2844}
2845
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002846void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002847
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002848void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2849 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2850 "declaration already in set");
2851 assert(S->getUsingDecl() == this);
2852
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002853 if (FirstUsingShadow.getPointer())
2854 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2855 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002856}
2857
2858void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2859 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2860 "declaration not in set");
2861 assert(S->getUsingDecl() == this);
2862
2863 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2864
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002865 if (FirstUsingShadow.getPointer() == S) {
2866 FirstUsingShadow.setPointer(
2867 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002868 S->UsingOrNextShadow = this;
2869 return;
2870 }
2871
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002872 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002873 while (Prev->UsingOrNextShadow != S)
2874 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2875 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2876 S->UsingOrNextShadow = this;
2877}
2878
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002879UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2880 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002881 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002882 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002883 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002884}
2885
Douglas Gregor72172e92012-01-05 21:55:30 +00002886UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002887 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2888 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2889 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002890}
2891
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002892SourceRange UsingDecl::getSourceRange() const {
2893 SourceLocation Begin = isAccessDeclaration()
2894 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2895 return SourceRange(Begin, getNameInfo().getEndLoc());
2896}
2897
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002898void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002899
2900UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2901 NamedDecl *InstantiatedFrom,
2902 ArrayRef<NamedDecl *> UsingDecls) {
2903 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2904 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2905}
2906
2907UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2908 unsigned NumExpansions) {
2909 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2910 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2911 Result->NumExpansions = NumExpansions;
2912 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2913 for (unsigned I = 0; I != NumExpansions; ++I)
2914 new (Trail + I) NamedDecl*(nullptr);
2915 return Result;
2916}
2917
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002918void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002919
John McCalle61f2ba2009-11-18 02:36:19 +00002920UnresolvedUsingValueDecl *
2921UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2922 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002923 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002924 const DeclarationNameInfo &NameInfo,
2925 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002926 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002927 QualifierLoc, NameInfo,
2928 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002929}
2930
Douglas Gregor72172e92012-01-05 21:55:30 +00002931UnresolvedUsingValueDecl *
2932UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002933 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2934 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002935 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002936 DeclarationNameInfo(),
2937 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002938}
2939
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002940SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2941 SourceLocation Begin = isAccessDeclaration()
2942 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2943 return SourceRange(Begin, getNameInfo().getEndLoc());
2944}
2945
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002946void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002947
John McCalle61f2ba2009-11-18 02:36:19 +00002948UnresolvedUsingTypenameDecl *
2949UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2950 SourceLocation UsingLoc,
2951 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002952 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002953 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002954 DeclarationName TargetName,
2955 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002956 return new (C, DC) UnresolvedUsingTypenameDecl(
2957 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002958 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002959}
2960
Douglas Gregor72172e92012-01-05 21:55:30 +00002961UnresolvedUsingTypenameDecl *
2962UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002963 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002964 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002965 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002966}
2967
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002968void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002969
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002970StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002971 SourceLocation StaticAssertLoc,
2972 Expr *AssertExpr,
2973 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002974 SourceLocation RParenLoc,
2975 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002976 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2977 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002978}
2979
Richard Smithf7981722013-11-22 09:01:48 +00002980StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002981 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002982 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2983 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002984}
2985
Richard Smithbdb84f32016-07-22 23:36:59 +00002986void BindingDecl::anchor() {}
2987
2988BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2989 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002990 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002991}
2992
2993BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002994 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002995}
2996
Richard Smith0353e5a2019-05-25 01:04:17 +00002997ValueDecl *BindingDecl::getDecomposedDecl() const {
2998 ExternalASTSource *Source =
2999 Decomp.isOffset() ? getASTContext().getExternalSource() : nullptr;
3000 return cast_or_null<ValueDecl>(Decomp.get(Source));
3001}
3002
Richard Smith97fcf4b2016-08-14 23:15:52 +00003003VarDecl *BindingDecl::getHoldingVar() const {
3004 Expr *B = getBinding();
3005 if (!B)
3006 return nullptr;
3007 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
3008 if (!DRE)
3009 return nullptr;
3010
3011 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
3012 assert(VD->isImplicit() && "holding var for binding decl not implicit");
3013 return VD;
3014}
3015
Richard Smithbdb84f32016-07-22 23:36:59 +00003016void DecompositionDecl::anchor() {}
3017
3018DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
3019 SourceLocation StartLoc,
3020 SourceLocation LSquareLoc,
3021 QualType T, TypeSourceInfo *TInfo,
3022 StorageClass SC,
3023 ArrayRef<BindingDecl *> Bindings) {
3024 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3025 return new (C, DC, Extra)
3026 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
3027}
3028
3029DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3030 unsigned ID,
3031 unsigned NumBindings) {
3032 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00003033 auto *Result = new (C, ID, Extra)
3034 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3035 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00003036 // Set up and clean out the bindings array.
3037 Result->NumBindings = NumBindings;
3038 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
3039 for (unsigned I = 0; I != NumBindings; ++I)
3040 new (Trail + I) BindingDecl*(nullptr);
3041 return Result;
3042}
3043
Richard Smith7873de02016-08-11 22:25:46 +00003044void DecompositionDecl::printName(llvm::raw_ostream &os) const {
3045 os << '[';
3046 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00003047 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00003048 if (Comma)
3049 os << ", ";
3050 B->printName(os);
3051 Comma = true;
3052 }
3053 os << ']';
3054}
3055
Richard Trieub3e902f2018-12-29 02:02:30 +00003056void MSPropertyDecl::anchor() {}
3057
Richard Smithf7981722013-11-22 09:01:48 +00003058MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
3059 SourceLocation L, DeclarationName N,
3060 QualType T, TypeSourceInfo *TInfo,
3061 SourceLocation StartL,
3062 IdentifierInfo *Getter,
3063 IdentifierInfo *Setter) {
3064 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
3065}
3066
3067MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
3068 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00003069 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
3070 DeclarationName(), QualType(), nullptr,
3071 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00003072}
3073
Anders Carlsson6750d162009-03-26 23:46:50 +00003074static const char *getAccessName(AccessSpecifier AS) {
3075 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00003076 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00003077 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003078 case AS_public:
3079 return "public";
3080 case AS_private:
3081 return "private";
3082 case AS_protected:
3083 return "protected";
3084 }
David Blaikief47fa302012-01-17 02:30:50 +00003085 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00003086}
3087
3088const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
3089 AccessSpecifier AS) {
3090 return DB << getAccessName(AS);
3091}
Richard Smith84f6dcf2012-02-02 01:16:57 +00003092
3093const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
3094 AccessSpecifier AS) {
3095 return DB << getAccessName(AS);
3096}