blob: 2893fca859ed6095aeb42cda52a2636fc82f8179 [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//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000013
Ted Kremenek21475702008-09-05 17:16:31 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000016#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000018#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000019#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000020#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000022#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000023#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000024#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000025#include "clang/AST/LambdaCapture.h"
26#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000027#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000028#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000029#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000030#include "clang/AST/UnresolvedSet.h"
31#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000032#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000033#include "clang/Basic/LLVM.h"
34#include "clang/Basic/LangOptions.h"
35#include "clang/Basic/OperatorKinds.h"
36#include "clang/Basic/PartialDiagnostic.h"
37#include "clang/Basic/SourceLocation.h"
38#include "clang/Basic/Specifiers.h"
39#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000040#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000041#include "llvm/ADT/SmallVector.h"
42#include "llvm/ADT/iterator_range.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
46#include <algorithm>
47#include <cassert>
48#include <cstddef>
49#include <cstdint>
50
Ted Kremenek21475702008-09-05 17:16:31 +000051using namespace clang;
52
53//===----------------------------------------------------------------------===//
54// Decl Allocation/Deallocation Method Implementations
55//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000056
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000057void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000058
Douglas Gregor72172e92012-01-05 21:55:30 +000059AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000060 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000061}
62
Richard Smitha4ba74c2013-08-30 04:46:40 +000063void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
64 ExternalASTSource *Source = C.getExternalSource();
65 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
66 assert(Source && "getFromExternalSource with no external source");
67
68 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
69 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
70 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
71 Impl.Decls.setLazy(false);
72}
73
John McCall67da35c2010-02-04 22:26:26 +000074CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000075 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
76 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000077 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
78 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000079 HasPrivateFields(false), HasProtectedFields(false),
80 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
81 HasOnlyCMembers(true), HasInClassInitializer(false),
82 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000083 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000084 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000085 NeedOverloadResolutionForMoveConstructor(false),
86 NeedOverloadResolutionForMoveAssignment(false),
87 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000088 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000089 DefaultedMoveConstructorIsDeleted(false),
90 DefaultedMoveAssignmentIsDeleted(false),
91 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000092 HasTrivialSpecialMembersForCall(SMF_All),
93 DeclaredNonTrivialSpecialMembers(0),
94 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000095 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000096 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000097 DefaultedDefaultConstructorIsConstexpr(true),
98 HasConstexprDefaultConstructor(false),
99 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
329 // C++11 [dcl.constexpr]p4:
330 // In the definition of a constexpr constructor [...]
331 // -- the class shall not have any virtual base classes
332 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000333
334 // C++1z [class.copy]p8:
335 // The implicitly-declared copy constructor for a class X will have
336 // the form 'X::X(const X&)' if each potentially constructed subobject
337 // has a copy constructor whose first parameter is of type
338 // 'const B&' or 'const volatile B&' [...]
339 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
340 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000341 } else {
342 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000343 // A default constructor is trivial [...] if:
344 // -- all the direct base classes of its class have trivial default
345 // constructors.
346 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000347 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
348
Chandler Carruthad7d4042011-04-23 23:10:33 +0000349 // C++0x [class.copy]p13:
350 // A copy/move constructor for class X is trivial if [...]
351 // [...]
352 // -- the constructor selected to copy/move each direct base class
353 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000354 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000355 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000356
357 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
358 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
359
Richard Smith6b02d462012-12-08 08:32:28 +0000360 // If the base class doesn't have a simple move constructor, we'll eagerly
361 // declare it and perform overload resolution to determine which function
362 // it actually calls. If it does have a simple move constructor, this
363 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000364 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000365 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000366
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000367 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
368 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
369
Chandler Carruthad7d4042011-04-23 23:10:33 +0000370 // C++0x [class.copy]p27:
371 // A copy/move assignment operator for class X is trivial if [...]
372 // [...]
373 // -- the assignment operator selected to copy/move each direct base
374 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000375 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000376 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000377 // If the base class doesn't have a simple move assignment, we'll eagerly
378 // declare it and perform overload resolution to determine which function
379 // it actually calls. If it does have a simple move assignment, this
380 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000381 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000382 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000383
384 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000385 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000386 // requirements of a constexpr constructor, the implicitly-defined
387 // default constructor is constexpr.
388 if (!BaseClassDecl->hasConstexprDefaultConstructor())
389 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000390
391 // C++1z [class.copy]p8:
392 // The implicitly-declared copy constructor for a class X will have
393 // the form 'X::X(const X&)' if each potentially constructed subobject
394 // has a copy constructor whose first parameter is of type
395 // 'const B&' or 'const volatile B&' [...]
396 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
397 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000398 }
Richard Smith92f241f2012-12-08 02:53:02 +0000399
Douglas Gregor11c024b2010-09-28 20:50:54 +0000400 // C++ [class.ctor]p3:
401 // A destructor is trivial if all the direct base classes of its class
402 // have trivial destructors.
403 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000404 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000405
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000406 if (!BaseClassDecl->hasTrivialDestructorForCall())
407 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
408
Richard Smith561fb152012-02-25 07:33:38 +0000409 if (!BaseClassDecl->hasIrrelevantDestructor())
410 data().HasIrrelevantDestructor = false;
411
Richard Smith1c33fe82012-11-28 06:23:12 +0000412 // C++11 [class.copy]p18:
413 // The implicitly-declared copy assignment oeprator for a class X will
414 // have the form 'X& X::operator=(const X&)' if each direct base class B
415 // of X has a copy assignment operator whose parameter is of type 'const
416 // B&', 'const volatile B&', or 'B' [...]
417 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
418 data().ImplicitCopyAssignmentHasConstParam = false;
419
John McCall31168b02011-06-15 23:02:42 +0000420 // A class has an Objective-C object member if... or any of its bases
421 // has an Objective-C object member.
422 if (BaseClassDecl->hasObjectMember())
423 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000424
Fariborz Jahanian78652202013-01-25 23:57:05 +0000425 if (BaseClassDecl->hasVolatileMember())
426 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000427
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000428 if (BaseClassDecl->getArgPassingRestrictions() ==
429 RecordDecl::APK_CanNeverPassInRegs)
430 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
431
Douglas Gregor61226d32011-05-13 01:05:07 +0000432 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000433 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000434 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000435 data().NeedOverloadResolutionForCopyConstructor = true;
436 }
Richard Smith593f9932012-12-08 02:01:17 +0000437
438 if (BaseClassDecl->hasUninitializedReferenceMember())
439 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000440
Nico Weber6a6376b2016-02-19 01:52:46 +0000441 if (!BaseClassDecl->allowConstDefaultInit())
442 data().HasUninitializedFields = true;
443
Richard Smith6b02d462012-12-08 08:32:28 +0000444 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000445 }
Richard Smithb6070db2018-04-05 18:55:37 +0000446
447 // C++2a [class]p7:
448 // A class S is a standard-layout class if it:
449 // -- has at most one base class subobject of any given type
450 //
451 // Note that we only need to check this for classes with more than one base
452 // class. If there's only one base class, and it's standard layout, then
453 // we know there are no repeated base classes.
454 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
455 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000456
David Majnemer5ef4fe72014-06-13 06:43:46 +0000457 if (VBases.empty()) {
458 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000459 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000460 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000461
462 // Create base specifier for any direct or indirect virtual bases.
463 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
464 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000465 for (int I = 0, E = VBases.size(); I != E; ++I) {
466 QualType Type = VBases[I]->getType();
467 if (!Type->isDependentType())
468 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000469 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000470 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000471
472 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000473}
474
Richard Trieufd1acbb2017-04-11 21:31:00 +0000475unsigned CXXRecordDecl::getODRHash() const {
476 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000477
Richard Trieufd1acbb2017-04-11 21:31:00 +0000478 // Previously calculated hash is stored in DefinitionData.
479 if (DefinitionData->HasODRHash)
480 return DefinitionData->ODRHash;
481
482 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000483 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000484 Hash.AddCXXRecordDecl(getDefinition());
485 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000486 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000487
488 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000489}
Richard Trieub6adf542017-02-18 02:09:28 +0000490
Richard Smith6b02d462012-12-08 08:32:28 +0000491void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
492 // C++11 [class.copy]p11:
493 // A defaulted copy/move constructor for a class X is defined as
494 // deleted if X has:
495 // -- a direct or virtual base class B that cannot be copied/moved [...]
496 // -- a non-static data member of class type M (or array thereof)
497 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000498 if (!Subobj->hasSimpleCopyConstructor())
499 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000500 if (!Subobj->hasSimpleMoveConstructor())
501 data().NeedOverloadResolutionForMoveConstructor = true;
502
503 // C++11 [class.copy]p23:
504 // A defaulted copy/move assignment operator for a class X is defined as
505 // deleted if X has:
506 // -- a direct or virtual base class B that cannot be copied/moved [...]
507 // -- a non-static data member of class type M (or array thereof)
508 // that cannot be copied or moved [...]
509 if (!Subobj->hasSimpleMoveAssignment())
510 data().NeedOverloadResolutionForMoveAssignment = true;
511
512 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
513 // A defaulted [ctor or dtor] for a class X is defined as
514 // deleted if X has:
515 // -- any direct or virtual base class [...] has a type with a destructor
516 // that is deleted or inaccessible from the defaulted [ctor or dtor].
517 // -- any non-static data member has a type with a destructor
518 // that is deleted or inaccessible from the defaulted [ctor or dtor].
519 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000520 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000521 data().NeedOverloadResolutionForMoveConstructor = true;
522 data().NeedOverloadResolutionForDestructor = true;
523 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000524}
525
Douglas Gregord2e6a452010-01-14 17:47:39 +0000526bool CXXRecordDecl::hasAnyDependentBases() const {
527 if (!isDependentContext())
528 return false;
529
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000530 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000531}
532
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000533bool CXXRecordDecl::isTriviallyCopyable() const {
534 // C++0x [class]p5:
535 // A trivially copyable class is a class that:
536 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000537 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000538 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000539 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000540 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000541 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000542 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000543 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000544 // -- has a trivial destructor.
545 if (!hasTrivialDestructor()) return false;
546
547 return true;
548}
549
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000550void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000551 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000552 // A class is abstract if it has at least one pure virtual function.
553 data().Abstract = true;
554}
555
Richard Smithb6070db2018-04-05 18:55:37 +0000556bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
557 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
558 if (!getNumBases())
559 return false;
560
561 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
562 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
563 SmallVector<const CXXRecordDecl*, 8> WorkList;
564
565 // Visit a type that we have determined is an element of M(S).
566 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
567 RD = RD->getCanonicalDecl();
568
569 // C++2a [class]p8:
570 // A class S is a standard-layout class if it [...] has no element of the
571 // set M(S) of types as a base class.
572 //
573 // If we find a subobject of an empty type, it might also be a base class,
574 // so we'll need to walk the base classes to check.
575 if (!RD->data().HasBasesWithFields) {
576 // Walk the bases the first time, stopping if we find the type. Build a
577 // set of them so we don't need to walk them again.
578 if (Bases.empty()) {
579 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
580 Base = Base->getCanonicalDecl();
581 if (RD == Base)
582 return false;
583 Bases.insert(Base);
584 return true;
585 });
586 if (RDIsBase)
587 return true;
588 } else {
589 if (Bases.count(RD))
590 return true;
591 }
592 }
593
594 if (M.insert(RD).second)
595 WorkList.push_back(RD);
596 return false;
597 };
598
599 if (Visit(XFirst))
600 return true;
601
602 while (!WorkList.empty()) {
603 const CXXRecordDecl *X = WorkList.pop_back_val();
604
605 // FIXME: We don't check the bases of X. That matches the standard, but
606 // that sure looks like a wording bug.
607
608 // -- If X is a non-union class type with a non-static data member
609 // [recurse to] the first non-static data member of X
610 // -- If X is a union type, [recurse to union members]
611 for (auto *FD : X->fields()) {
612 // FIXME: Should we really care about the type of the first non-static
613 // data member of a non-union if there are preceding unnamed bit-fields?
614 if (FD->isUnnamedBitfield())
615 continue;
616
617 // -- If X is n array type, [visit the element type]
618 QualType T = Ctx.getBaseElementType(FD->getType());
619 if (auto *RD = T->getAsCXXRecordDecl())
620 if (Visit(RD))
621 return true;
622
623 if (!X->isUnion())
624 break;
625 }
626 }
627
628 return false;
629}
630
Richard Smith864949b2018-09-27 22:47:04 +0000631bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
632 assert(isLambda() && "not a lambda");
633
634 // C++2a [expr.prim.lambda.capture]p11:
635 // The closure type associated with a lambda-expression has no default
636 // constructor if the lambda-expression has a lambda-capture and a
637 // defaulted default constructor otherwise. It has a deleted copy
638 // assignment operator if the lambda-expression has a lambda-capture and
639 // defaulted copy and move assignment operators otherwise.
640 //
641 // C++17 [expr.prim.lambda]p21:
642 // The closure type associated with a lambda-expression has no default
643 // constructor and a deleted copy assignment operator.
644 if (getLambdaCaptureDefault() != LCD_None)
645 return false;
646 return getASTContext().getLangOpts().CPlusPlus2a;
647}
648
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000649void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000650 if (!D->isImplicit() &&
651 !isa<FieldDecl>(D) &&
652 !isa<IndirectFieldDecl>(D) &&
653 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
654 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
655 data().HasOnlyCMembers = false;
656
657 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000658 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000659 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000660
Eugene Zelenko7855e772018-04-03 00:11:50 +0000661 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000662 if (FunTmpl)
663 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000664
665 // FIXME: Pass NamedDecl* to addedMember?
666 Decl *DUnderlying = D;
667 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
668 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000669 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000670 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
671 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000672
Eugene Zelenko7855e772018-04-03 00:11:50 +0000673 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000674 if (Method->isVirtual()) {
675 // C++ [dcl.init.aggr]p1:
676 // An aggregate is an array or a class with [...] no virtual functions.
677 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000678
Douglas Gregora832d3e2010-09-28 19:45:33 +0000679 // C++ [class]p4:
680 // A POD-struct is an aggregate class...
681 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000682
David Majnemer5cfda6f2016-05-22 05:34:26 +0000683 // C++14 [meta.unary.prop]p4:
684 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000685 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000686
687 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000688 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000689 // polymorphic class.
690 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000691
Richard Smith328aae52012-11-30 05:11:39 +0000692 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
693 // A [default constructor, copy/move constructor, or copy/move
694 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000695 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000696 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000697 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000698
Chandler Carruthb1963742011-04-30 09:17:45 +0000699 // C++0x [class]p7:
700 // A standard-layout class is a class that: [...]
701 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000702 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000703 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000704 }
705 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000706
Richard Smith1c33fe82012-11-28 06:23:12 +0000707 // Notify the listener if an implicit member was added after the definition
708 // was completed.
709 if (!isBeingDefined() && D->isImplicit())
710 if (ASTMutationListener *L = getASTMutationListener())
711 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000712
Richard Smith328aae52012-11-30 05:11:39 +0000713 // The kind of special member this declaration is, if any.
714 unsigned SMKind = 0;
715
Richard Smith1c33fe82012-11-28 06:23:12 +0000716 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000717 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000718 if (!Constructor->isImplicit()) {
719 // Note that we have a user-declared constructor.
720 data().UserDeclaredConstructor = true;
721
722 // C++ [class]p4:
723 // A POD-struct is an aggregate class [...]
724 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
725 // type is technically an aggregate in C++0x since it wouldn't be in 03.
726 data().PlainOldData = false;
727 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000728
Alexis Hunt88c75c32011-05-09 21:45:35 +0000729 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000730 SMKind |= SMF_DefaultConstructor;
731
Richard Smith5179eb72016-06-28 19:03:57 +0000732 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000733 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000734 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000735 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000736 if (Constructor->isDefaulted())
737 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000738 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000739
Chandler Carruthad7d4042011-04-23 23:10:33 +0000740 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000741 unsigned Quals;
742 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000743 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000744
745 if (Quals & Qualifiers::Const)
746 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000747 } else if (Constructor->isMoveConstructor())
748 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000749 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000750
Eric Fiselier283d8d42016-12-03 01:26:47 +0000751 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith79c88c32018-09-26 19:00:16 +0000752 // An aggregate is an array or a class with no user-provided [or]
753 // explicit [...] constructors
754 // C++20 [dcl.init.aggr]p1:
755 // An aggregate is an array or a class with no user-declared [...]
756 // constructors
757 if (getASTContext().getLangOpts().CPlusPlus2a
758 ? !Constructor->isImplicit()
759 : (Constructor->isUserProvided() || Constructor->isExplicit()))
Eric Fiselier283d8d42016-12-03 01:26:47 +0000760 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000761 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000762
Richard Smith5179eb72016-06-28 19:03:57 +0000763 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000764 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000765 // Record if we see any constexpr constructors which are neither copy
766 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000767 // C++1z [basic.types]p10:
768 // [...] has at least one constexpr constructor or constructor template
769 // (possibly inherited from a base class) that is not a copy or move
770 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000771 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000772 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000773 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000774
Richard Smith1c33fe82012-11-28 06:23:12 +0000775 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000776 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000777 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000778
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000779 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000780 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000781 // If the destructor is explicitly defaulted and not trivial or not public
782 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
783 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000784
Richard Smith1c33fe82012-11-28 06:23:12 +0000785 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000786 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000787 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000788 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000789 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
790 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000791 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000792
793 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000794 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000795 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000796 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000797
Eugene Zelenko7855e772018-04-03 00:11:50 +0000798 const auto *ParamTy =
799 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000800 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
801 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000802 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000803
Richard Smith328aae52012-11-30 05:11:39 +0000804 if (Method->isMoveAssignmentOperator())
805 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000806
Douglas Gregor457104e2010-09-29 04:25:11 +0000807 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000808 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000809 // FIXME: We use the 'unsafe' accessor for the access specifier here,
810 // because Sema may not have set it yet. That's really just a misdesign
811 // in Sema. However, LLDB *will* have set the access specifier correctly,
812 // and adds declarations after the class is technically completed,
813 // so completeDefinition()'s overriding of the access specifiers doesn't
814 // work.
815 AccessSpecifier AS = Conversion->getAccessUnsafe();
816
Richard Smith328aae52012-11-30 05:11:39 +0000817 if (Conversion->getPrimaryTemplate()) {
818 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000819 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000820 ASTContext &Ctx = getASTContext();
821 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
822 NamedDecl *Primary =
823 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
824 if (Primary->getPreviousDecl())
825 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
826 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000827 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000828 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000829 }
830 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000831
Richard Smith328aae52012-11-30 05:11:39 +0000832 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000833 // If this is the first declaration of a special member, we no longer have
834 // an implicit trivial special member.
835 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000836 data().DeclaredSpecialMembers | ~SMKind;
837 data().HasTrivialSpecialMembersForCall &=
838 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000839
840 if (!Method->isImplicit() && !Method->isUserProvided()) {
841 // This method is user-declared but not user-provided. We can't work out
842 // whether it's trivial yet (not until we get to the end of the class).
843 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000844 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000845 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000846 data().HasTrivialSpecialMembersForCall |= SMKind;
847 } else if (Method->isTrivialForCall()) {
848 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000849 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000850 } else {
851 data().DeclaredNonTrivialSpecialMembers |= SMKind;
852 // If this is a user-provided function, do not set
853 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
854 // yet whether the method would be considered non-trivial for the
855 // purpose of calls (attribute "trivial_abi" can be dropped from the
856 // class later, which can change the special method's triviality).
857 if (!Method->isUserProvided())
858 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
859 }
Richard Smith92f241f2012-12-08 02:53:02 +0000860
Richard Smith328aae52012-11-30 05:11:39 +0000861 // Note when we have declared a declared special member, and suppress the
862 // implicit declaration of this special member.
863 data().DeclaredSpecialMembers |= SMKind;
864
865 if (!Method->isImplicit()) {
866 data().UserDeclaredSpecialMembers |= SMKind;
867
868 // C++03 [class]p4:
869 // A POD-struct is an aggregate class that has [...] no user-defined
870 // copy assignment operator and no user-defined destructor.
871 //
872 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
873 // aggregates could not have any constructors, clear it even for an
874 // explicitly defaulted or deleted constructor.
875 // type is technically an aggregate in C++0x since it wouldn't be in 03.
876 //
877 // Also, a user-declared move assignment operator makes a class non-POD.
878 // This is an extension in C++03.
879 data().PlainOldData = false;
880 }
Richard Smith328aae52012-11-30 05:11:39 +0000881 }
882
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000883 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000884 }
Richard Smith328aae52012-11-30 05:11:39 +0000885
Douglas Gregora832d3e2010-09-28 19:45:33 +0000886 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000887 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000888 ASTContext &Context = getASTContext();
889
Richard Smithb6070db2018-04-05 18:55:37 +0000890 // C++2a [class]p7:
891 // A standard-layout class is a class that:
892 // [...]
893 // -- has all non-static data members and bit-fields in the class and
894 // its base classes first declared in the same class
895 if (data().HasBasesWithFields)
896 data().IsStandardLayout = false;
897
Douglas Gregor556e5862011-10-10 17:22:13 +0000898 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000899 // A declaration for a bit-field that omits the identifier declares an
900 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000901 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000902 if (Field->isUnnamedBitfield()) {
903 // C++ [meta.unary.prop]p4: [LWG2358]
904 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
905 // length
906 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
907 Context.getLangOpts().getClangABICompat() >
908 LangOptions::ClangABI::Ver6)
909 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000910 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000911 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000912
Richard Smithb6070db2018-04-05 18:55:37 +0000913 // C++11 [class]p7:
914 // A standard-layout class is a class that:
915 // -- either has no non-static data members in the most derived class
916 // [...] or has no base classes with non-static data members
917 if (data().HasBasesWithNonStaticDataMembers)
918 data().IsCXX11StandardLayout = false;
919
Douglas Gregora832d3e2010-09-28 19:45:33 +0000920 // C++ [dcl.init.aggr]p1:
921 // An aggregate is an array or a class (clause 9) with [...] no
922 // private or protected non-static data members (clause 11).
923 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000924 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000925 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
926 data().Aggregate = false;
927 data().PlainOldData = false;
928 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000929
Richard Smithb6070db2018-04-05 18:55:37 +0000930 // Track whether this is the first field. We use this when checking
931 // whether the class is standard-layout below.
932 bool IsFirstField = !data().HasPrivateFields &&
933 !data().HasProtectedFields && !data().HasPublicFields;
934
Chandler Carruthb1963742011-04-30 09:17:45 +0000935 // C++0x [class]p7:
936 // A standard-layout class is a class that:
937 // [...]
938 // -- has the same access control for all non-static data members,
939 switch (D->getAccess()) {
940 case AS_private: data().HasPrivateFields = true; break;
941 case AS_protected: data().HasProtectedFields = true; break;
942 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000943 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000944 };
945 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000946 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000947 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000948 data().IsCXX11StandardLayout = false;
949 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000950
Douglas Gregor61226d32011-05-13 01:05:07 +0000951 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000952 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000953 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000954 data().NeedOverloadResolutionForCopyConstructor = true;
955 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000956
957 // C++11 [class.union]p8, DR1460:
958 // If X is a union, a non-static data member of X that is not an anonymous
959 // union is a variant member of X.
960 if (isUnion() && !Field->isAnonymousStructOrUnion())
961 data().HasVariantMembers = true;
962
Chandler Carruthad7d4042011-04-23 23:10:33 +0000963 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000964 // A POD struct is a class that is both a trivial class and a
965 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000966 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000967 //
968 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
969 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000970 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000971 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000972 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000973 // Objective-C Automatic Reference Counting:
974 // If a class has a non-static data member of Objective-C pointer
975 // type (or array thereof), it is a non-POD type and its
976 // default constructor (if any), copy constructor, move constructor,
977 // copy assignment operator, move assignment operator, and destructor are
978 // non-trivial.
979 setHasObjectMember(true);
980 struct DefinitionData &Data = data();
981 Data.PlainOldData = false;
982 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000983
984 // __strong or __weak fields do not make special functions non-trivial
985 // for the purpose of calls.
986 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
987 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
988 data().HasTrivialSpecialMembersForCall = 0;
989
990 // Structs with __weak fields should never be passed directly.
991 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000992 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000993
Ben Langmuir11eab612014-09-26 15:27:29 +0000994 Data.HasIrrelevantDestructor = false;
Brian Kelley036603a2017-03-29 17:31:42 +0000995 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
996 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000997 }
Eli Friedmana5433322013-07-20 01:06:31 +0000998 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000999 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001000
Chandler Carruthb1963742011-04-30 09:17:45 +00001001 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +00001002 if (!Field->hasInClassInitializer())
1003 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +00001004
Chandler Carruthb1963742011-04-30 09:17:45 +00001005 // C++0x [class]p7:
1006 // A standard-layout class is a class that:
1007 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +00001008 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001009 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +00001010
1011 // C++1z [class.copy.ctor]p10:
1012 // A defaulted copy constructor for a class X is defined as deleted if X has:
1013 // -- a non-static data member of rvalue reference type
1014 if (T->isRValueReferenceType())
1015 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +00001016 }
1017
Nico Weber6a6376b2016-02-19 01:52:46 +00001018 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +00001019 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +00001020 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +00001021 data().HasUninitializedFields = true;
1022 } else {
1023 data().HasUninitializedFields = true;
1024 }
1025 }
1026
Richard Smith3607ffe2012-02-13 03:54:03 +00001027 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001028 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001029 data().HasNonLiteralTypeFieldsOrBases = true;
1030
Richard Smithab44d5b2013-12-10 08:25:00 +00001031 if (Field->hasInClassInitializer() ||
1032 (Field->isAnonymousStructOrUnion() &&
1033 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001034 data().HasInClassInitializer = true;
1035
1036 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001037 // A default constructor is trivial if [...] no non-static data member
1038 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001039 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001040
Richard Smithe2648ba2012-05-07 01:07:30 +00001041 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001042 // An aggregate is a [...] class with [...] no
1043 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001044 //
Richard Smith872307e2016-03-08 22:17:41 +00001045 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001046 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001047 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001048
Richard Smithe2648ba2012-05-07 01:07:30 +00001049 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001050 // A POD struct is [...] a trivial class.
1051 data().PlainOldData = false;
1052 }
1053
Richard Smith6b02d462012-12-08 08:32:28 +00001054 // C++11 [class.copy]p23:
1055 // A defaulted copy/move assignment operator for a class X is defined
1056 // as deleted if X has:
1057 // -- a non-static data member of reference type
1058 if (T->isReferenceType())
1059 data().DefaultedMoveAssignmentIsDeleted = true;
1060
Eugene Zelenko7855e772018-04-03 00:11:50 +00001061 if (const auto *RecordTy = T->getAs<RecordType>()) {
1062 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001063 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001064 addedClassSubobject(FieldRec);
1065
Richard Smithc91d12c2013-11-25 07:07:05 +00001066 // We may need to perform overload resolution to determine whether a
1067 // field can be moved if it's const or volatile qualified.
1068 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001069 // We need to care about 'const' for the copy constructor because an
1070 // implicit copy constructor might be declared with a non-const
1071 // parameter.
1072 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001073 data().NeedOverloadResolutionForMoveConstructor = true;
1074 data().NeedOverloadResolutionForMoveAssignment = true;
1075 }
1076
Richard Smith6b02d462012-12-08 08:32:28 +00001077 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1078 // A defaulted [special member] for a class X is defined as
1079 // deleted if:
1080 // -- X is a union-like class that has a variant member with a
1081 // non-trivial [corresponding special member]
1082 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001083 if (FieldRec->hasNonTrivialCopyConstructor())
1084 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001085 if (FieldRec->hasNonTrivialMoveConstructor())
1086 data().DefaultedMoveConstructorIsDeleted = true;
1087 if (FieldRec->hasNonTrivialMoveAssignment())
1088 data().DefaultedMoveAssignmentIsDeleted = true;
1089 if (FieldRec->hasNonTrivialDestructor())
1090 data().DefaultedDestructorIsDeleted = true;
1091 }
1092
Richard Smithdd5619f2016-08-16 00:13:47 +00001093 // For an anonymous union member, our overload resolution will perform
1094 // overload resolution for its members.
1095 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001096 data().NeedOverloadResolutionForCopyConstructor |=
1097 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001098 data().NeedOverloadResolutionForMoveConstructor |=
1099 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1100 data().NeedOverloadResolutionForMoveAssignment |=
1101 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1102 data().NeedOverloadResolutionForDestructor |=
1103 FieldRec->data().NeedOverloadResolutionForDestructor;
1104 }
1105
Alexis Huntf479f1b2011-05-09 18:22:59 +00001106 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001107 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001108 // -- for all the non-static data members of its class that are of
1109 // class type (or array thereof), each such class has a trivial
1110 // default constructor.
1111 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001112 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001113
1114 // C++0x [class.copy]p13:
1115 // A copy/move constructor for class X is trivial if [...]
1116 // [...]
1117 // -- for each non-static data member of X that is of class type (or
1118 // an array thereof), the constructor selected to copy/move that
1119 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001120 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001121 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001122
1123 if (!FieldRec->hasTrivialCopyConstructorForCall())
1124 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1125
Richard Smith6b02d462012-12-08 08:32:28 +00001126 // If the field doesn't have a simple move constructor, we'll eagerly
1127 // declare the move constructor for this class and we'll decide whether
1128 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001129 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001130 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001131
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001132 if (!FieldRec->hasTrivialMoveConstructorForCall())
1133 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1134
Chandler Carruthad7d4042011-04-23 23:10:33 +00001135 // C++0x [class.copy]p27:
1136 // A copy/move assignment operator for class X is trivial if [...]
1137 // [...]
1138 // -- for each non-static data member of X that is of class type (or
1139 // an array thereof), the assignment operator selected to
1140 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001141 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001142 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001143 // If the field doesn't have a simple move assignment, we'll eagerly
1144 // declare the move assignment for this class and we'll decide whether
1145 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001146 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001147 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001148
Douglas Gregor11c024b2010-09-28 20:50:54 +00001149 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001150 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001151 if (!FieldRec->hasTrivialDestructorForCall())
1152 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001153 if (!FieldRec->hasIrrelevantDestructor())
1154 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001155 if (FieldRec->hasObjectMember())
1156 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001157 if (FieldRec->hasVolatileMember())
1158 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001159 if (FieldRec->getArgPassingRestrictions() ==
1160 RecordDecl::APK_CanNeverPassInRegs)
1161 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001162
1163 // C++0x [class]p7:
1164 // A standard-layout class is a class that:
1165 // -- has no non-static data members of type non-standard-layout
1166 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001167 if (!FieldRec->isStandardLayout())
1168 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001169 if (!FieldRec->isCXX11StandardLayout())
1170 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001171
Richard Smithb6070db2018-04-05 18:55:37 +00001172 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001173 // A standard-layout class is a class that:
1174 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001175 // -- has no element of the set M(S) of types as a base class.
1176 if (data().IsStandardLayout && (isUnion() || IsFirstField) &&
1177 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1178 data().IsStandardLayout = false;
1179
1180 // C++11 [class]p7:
1181 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001182 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001183 // data member
1184 if (data().IsCXX11StandardLayout && IsFirstField) {
1185 // FIXME: We should check all base classes here, not just direct
1186 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001187 for (const auto &BI : bases()) {
1188 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001189 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001190 break;
1191 }
1192 }
1193 }
Richard Smithb6070db2018-04-05 18:55:37 +00001194
Douglas Gregor61226d32011-05-13 01:05:07 +00001195 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001196 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001197 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001198 data().NeedOverloadResolutionForCopyConstructor = true;
1199 }
Richard Smithcc36f692011-12-22 02:22:31 +00001200
1201 // C++11 [class.copy]p13:
1202 // If the implicitly-defined constructor would satisfy the
1203 // requirements of a constexpr constructor, the implicitly-defined
1204 // constructor is constexpr.
1205 // C++11 [dcl.constexpr]p4:
1206 // -- every constructor involved in initializing non-static data
1207 // members [...] shall be a constexpr constructor
1208 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001209 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001210 // The standard requires any in-class initializer to be a constant
1211 // expression. We consider this to be a defect.
1212 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001213
1214 // C++11 [class.copy]p8:
1215 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001216 // the form 'X::X(const X&)' if each potentially constructed subobject
1217 // of a class type M (or array thereof) has a copy constructor whose
1218 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001219 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001220 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001221
1222 // C++11 [class.copy]p18:
1223 // The implicitly-declared copy assignment oeprator for a class X will
1224 // have the form 'X& X::operator=(const X&)' if [...] for all the
1225 // non-static data members of X that are of a class type M (or array
1226 // thereof), each such class type has a copy assignment operator whose
1227 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1228 if (!FieldRec->hasCopyAssignmentWithConstParam())
1229 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001230
1231 if (FieldRec->hasUninitializedReferenceMember() &&
1232 !Field->hasInClassInitializer())
1233 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001234
1235 // C++11 [class.union]p8, DR1460:
1236 // a non-static data member of an anonymous union that is a member of
1237 // X is also a variant member of X.
1238 if (FieldRec->hasVariantMembers() &&
1239 Field->isAnonymousStructOrUnion())
1240 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001241 }
Richard Smithcc36f692011-12-22 02:22:31 +00001242 } else {
1243 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001244 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +00001245 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +00001246 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001247
1248 // C++11 [class.copy]p23:
1249 // A defaulted copy/move assignment operator for a class X is defined
1250 // as deleted if X has:
1251 // -- a non-static data member of const non-class type (or array
1252 // thereof)
1253 if (T.isConstQualified())
1254 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001255 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001256
David Majnemer5cfda6f2016-05-22 05:34:26 +00001257 // C++14 [meta.unary.prop]p4:
Richard Smithb5a317f2018-05-07 06:43:30 +00001258 // T is a class type [...] with [...] no non-static data members
1259 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001260 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001261
Douglas Gregor457104e2010-09-29 04:25:11 +00001262 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001263 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001264 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001265 == DeclarationName::CXXConversionFunctionName) {
1266 ASTContext &Ctx = getASTContext();
1267 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1268 }
1269 }
Richard Smith12e79312016-05-13 06:47:56 +00001270
Eugene Zelenko7855e772018-04-03 00:11:50 +00001271 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001272 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001273 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001274 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001275 // C++1z [dcl.init.aggr]p1:
1276 // An aggregate is [...] a class [...] with no inherited constructors
1277 data().Aggregate = false;
1278 }
Richard Smith12e79312016-05-13 06:47:56 +00001279
1280 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1281 data().HasInheritedAssignment = true;
1282 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001283}
1284
Richard Smith92f241f2012-12-08 02:53:02 +00001285void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1286 assert(!D->isImplicit() && !D->isUserProvided());
1287
1288 // The kind of special member this declaration is, if any.
1289 unsigned SMKind = 0;
1290
Eugene Zelenko7855e772018-04-03 00:11:50 +00001291 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001292 if (Constructor->isDefaultConstructor()) {
1293 SMKind |= SMF_DefaultConstructor;
1294 if (Constructor->isConstexpr())
1295 data().HasConstexprDefaultConstructor = true;
1296 }
1297 if (Constructor->isCopyConstructor())
1298 SMKind |= SMF_CopyConstructor;
1299 else if (Constructor->isMoveConstructor())
1300 SMKind |= SMF_MoveConstructor;
1301 else if (Constructor->isConstexpr())
1302 // We may now know that the constructor is constexpr.
1303 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001304 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001305 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001306 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1307 data().HasIrrelevantDestructor = false;
1308 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001309 SMKind |= SMF_CopyAssignment;
1310 else if (D->isMoveAssignmentOperator())
1311 SMKind |= SMF_MoveAssignment;
1312
1313 // Update which trivial / non-trivial special members we have.
1314 // addedMember will have skipped this step for this member.
1315 if (D->isTrivial())
1316 data().HasTrivialSpecialMembers |= SMKind;
1317 else
1318 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1319}
1320
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001321void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1322 unsigned SMKind = 0;
1323
Eugene Zelenko7855e772018-04-03 00:11:50 +00001324 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001325 if (Constructor->isCopyConstructor())
1326 SMKind = SMF_CopyConstructor;
1327 else if (Constructor->isMoveConstructor())
1328 SMKind = SMF_MoveConstructor;
1329 } else if (isa<CXXDestructorDecl>(D))
1330 SMKind = SMF_Destructor;
1331
1332 if (D->isTrivialForCall())
1333 data().HasTrivialSpecialMembersForCall |= SMKind;
1334 else
1335 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1336}
1337
Joao Matose9a3ed42012-08-31 22:18:20 +00001338bool CXXRecordDecl::isCLike() const {
1339 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1340 !TemplateOrInstantiation.isNull())
1341 return false;
1342 if (!hasDefinition())
1343 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001344
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001345 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001346}
Fangrui Song6907ce22018-07-30 19:24:48 +00001347
1348bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001349 if (!isLambda()) return false;
1350 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001351}
1352
Richard Smith24253382018-08-02 20:30:52 +00001353#ifndef NDEBUG
1354static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1355 for (auto *D : R)
1356 if (!declaresSameEntity(D, R.front()))
1357 return false;
1358 return true;
1359}
1360#endif
1361
Faisal Vali2b391ab2013-09-26 19:54:12 +00001362CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001363 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001364 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001365 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001366 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001367
1368 assert(!Calls.empty() && "Missing lambda call operator!");
Richard Smith24253382018-08-02 20:30:52 +00001369 assert(allLookupResultsAreTheSame(Calls) &&
1370 "More than one lambda call operator!");
Fangrui Song6907ce22018-07-30 19:24:48 +00001371
Faisal Vali850da1a2013-09-29 17:08:32 +00001372 NamedDecl *CallOp = Calls.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001373 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001374 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001375
Faisal Vali2b391ab2013-09-26 19:54:12 +00001376 return cast<CXXMethodDecl>(CallOp);
1377}
1378
1379CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001380 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001381 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001382 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001383 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001384 if (Invoker.empty()) return nullptr;
Richard Smith24253382018-08-02 20:30:52 +00001385 assert(allLookupResultsAreTheSame(Invoker) &&
1386 "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001387 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001388 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001389 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001390
1391 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001392}
1393
Douglas Gregor9c702202012-02-10 07:45:31 +00001394void CXXRecordDecl::getCaptureFields(
1395 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001396 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001397 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001398 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001399
Douglas Gregorc8a73492012-02-13 15:44:47 +00001400 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001401 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001402 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001403 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001404 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001405 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001406 else if (C->capturesVariable())
1407 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001408 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001409 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001410}
1411
Fangrui Song6907ce22018-07-30 19:24:48 +00001412TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001413CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001414 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001415 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001416 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1417 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001418 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001419}
Douglas Gregor9c702202012-02-10 07:45:31 +00001420
Richard Smith0bae6242016-08-25 00:34:00 +00001421Decl *CXXRecordDecl::getLambdaContextDecl() const {
1422 assert(isLambda() && "Not a lambda closure type!");
1423 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1424 return getLambdaData().ContextDecl.get(Source);
1425}
1426
John McCall1e3a1a72010-03-15 09:07:48 +00001427static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001428 QualType T =
1429 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1430 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001431 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001432}
1433
John McCall1e3a1a72010-03-15 09:07:48 +00001434/// Collect the visible conversions of a base class.
1435///
James Dennettb5d5f812012-06-15 22:28:09 +00001436/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001437/// \param InVirtual whether this base class is a virtual base (or a base
1438/// of a virtual base)
1439/// \param Access the access along the inheritance path to this base
1440/// \param ParentHiddenTypes the conversions provided by the inheritors
1441/// of this base
1442/// \param Output the set to which to add conversions from non-virtual bases
1443/// \param VOutput the set to which to add conversions from virtual bases
1444/// \param HiddenVBaseCs the set of conversions which were hidden in a
1445/// virtual base along some inheritance path
1446static void CollectVisibleConversions(ASTContext &Context,
1447 CXXRecordDecl *Record,
1448 bool InVirtual,
1449 AccessSpecifier Access,
1450 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001451 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001452 UnresolvedSetImpl &VOutput,
1453 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1454 // The set of types which have conversions in this class or its
1455 // subclasses. As an optimization, we don't copy the derived set
1456 // unless it might change.
1457 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1458 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1459
1460 // Collect the direct conversions and figure out which conversions
1461 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001462 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1463 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1464 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001465 HiddenTypesBuffer = ParentHiddenTypes;
1466 HiddenTypes = &HiddenTypesBuffer;
1467
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001468 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001469 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1470 bool Hidden = ParentHiddenTypes.count(ConvType);
1471 if (!Hidden)
1472 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001473
1474 // If this conversion is hidden and we're in a virtual base,
1475 // remember that it's hidden along some inheritance path.
1476 if (Hidden && InVirtual)
1477 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1478
1479 // If this conversion isn't hidden, add it to the appropriate output.
1480 else if (!Hidden) {
1481 AccessSpecifier IAccess
1482 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1483
1484 if (InVirtual)
1485 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001486 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001487 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001488 }
1489 }
1490 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001491
John McCall1e3a1a72010-03-15 09:07:48 +00001492 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001493 for (const auto &I : Record->bases()) {
1494 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001495 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001496
John McCall1e3a1a72010-03-15 09:07:48 +00001497 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001498 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1499 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001500
Eugene Zelenko7855e772018-04-03 00:11:50 +00001501 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001502 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1503 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001504 }
John McCall1e3a1a72010-03-15 09:07:48 +00001505}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001506
John McCall1e3a1a72010-03-15 09:07:48 +00001507/// Collect the visible conversions of a class.
1508///
1509/// This would be extremely straightforward if it weren't for virtual
1510/// bases. It might be worth special-casing that, really.
1511static void CollectVisibleConversions(ASTContext &Context,
1512 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001513 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001514 // The collection of all conversions in virtual bases that we've
1515 // found. These will be added to the output as long as they don't
1516 // appear in the hidden-conversions set.
1517 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001518
John McCall1e3a1a72010-03-15 09:07:48 +00001519 // The set of conversions in virtual bases that we've determined to
1520 // be hidden.
1521 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1522
1523 // The set of types hidden by classes derived from this one.
1524 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1525
1526 // Go ahead and collect the direct conversions and add them to the
1527 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001528 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1529 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001530 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001531 for (; ConvI != ConvE; ++ConvI)
1532 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001533
1534 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001535 for (const auto &I : Record->bases()) {
1536 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001537 if (!RT) continue;
1538
1539 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001540 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001541 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1542 }
1543
1544 // Add any unhidden conversions provided by virtual bases.
1545 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1546 I != E; ++I) {
1547 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001548 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001549 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001550}
1551
1552/// getVisibleConversionFunctions - get all conversion functions visible
1553/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001554llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001555CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001556 ASTContext &Ctx = getASTContext();
1557
1558 ASTUnresolvedSet *Set;
1559 if (bases_begin() == bases_end()) {
1560 // If root class, all conversions are visible.
1561 Set = &data().Conversions.get(Ctx);
1562 } else {
1563 Set = &data().VisibleConversions.get(Ctx);
1564 // If visible conversion list is not evaluated, evaluate it.
1565 if (!data().ComputedVisibleConversions) {
1566 CollectVisibleConversions(Ctx, this, *Set);
1567 data().ComputedVisibleConversions = true;
1568 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001569 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001570 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001571}
1572
John McCallda4458e2010-03-31 01:36:47 +00001573void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1574 // This operation is O(N) but extremely rare. Sema only uses it to
1575 // remove UsingShadowDecls in a class that were followed by a direct
1576 // declaration, e.g.:
1577 // class A : B {
1578 // using B::operator int;
1579 // operator int();
1580 // };
1581 // This is uncommon by itself and even more uncommon in conjunction
1582 // with sufficiently large numbers of directly-declared conversions
1583 // that asymptotic behavior matters.
1584
Richard Smitha4ba74c2013-08-30 04:46:40 +00001585 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001586 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1587 if (Convs[I].getDecl() == ConvDecl) {
1588 Convs.erase(I);
1589 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1590 && "conversion was found multiple times in unresolved set");
1591 return;
1592 }
1593 }
1594
1595 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001596}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001597
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001598CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001599 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001600 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001601
1602 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001603}
1604
Chandler Carruth21c90602015-12-30 03:24:14 +00001605MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1606 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1607}
1608
Fangrui Song6907ce22018-07-30 19:24:48 +00001609void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001610CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1611 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001612 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001613 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001614 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001615 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001616 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1617}
1618
Chandler Carruth21c90602015-12-30 03:24:14 +00001619ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1620 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1621}
1622
1623void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1624 TemplateOrInstantiation = Template;
1625}
1626
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001627TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001628 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001629 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001630
Douglas Gregor06db9f52009-10-12 20:18:28 +00001631 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001632 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001633
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001634 return TSK_Undeclared;
1635}
1636
Fangrui Song6907ce22018-07-30 19:24:48 +00001637void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001638CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001639 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001640 Spec->setSpecializationKind(TSK);
1641 return;
1642 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001643
Douglas Gregor06db9f52009-10-12 20:18:28 +00001644 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001645 MSInfo->setTemplateSpecializationKind(TSK);
1646 return;
1647 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001648
David Blaikie83d382b2011-09-23 05:06:16 +00001649 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001650}
1651
Reid Klecknere7367d62014-10-14 20:28:40 +00001652const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001653 auto GetDefinitionOrSelf =
1654 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1655 if (auto *Def = D->getDefinition())
1656 return Def;
1657 return D;
1658 };
1659
Reid Klecknere7367d62014-10-14 20:28:40 +00001660 // If it's a class template specialization, find the template or partial
1661 // specialization from which it was instantiated.
1662 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1663 auto From = TD->getInstantiatedFrom();
1664 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1665 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1666 if (NewCTD->isMemberSpecialization())
1667 break;
1668 CTD = NewCTD;
1669 }
Richard Smith2195ec92017-04-21 01:15:13 +00001670 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001671 }
1672 if (auto *CTPSD =
1673 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1674 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1675 if (NewCTPSD->isMemberSpecialization())
1676 break;
1677 CTPSD = NewCTPSD;
1678 }
Richard Smith2195ec92017-04-21 01:15:13 +00001679 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001680 }
1681 }
1682
1683 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1684 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1685 const CXXRecordDecl *RD = this;
1686 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1687 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001688 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001689 }
1690 }
1691
1692 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1693 "couldn't find pattern for class template instantiation");
1694 return nullptr;
1695}
1696
Douglas Gregorbac74902010-07-01 14:13:13 +00001697CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1698 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001699 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001700
1701 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001702 = Context.DeclarationNames.getCXXDestructorName(
1703 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001704
Richard Smithcf4bdde2015-02-21 02:45:19 +00001705 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001706
Hubert Tong3cede512017-06-30 22:43:54 +00001707 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001708}
1709
Richard Trieu95a192a2015-05-28 00:14:02 +00001710bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1711 // Destructor is noreturn.
1712 if (const CXXDestructorDecl *Destructor = getDestructor())
1713 if (Destructor->isNoReturn())
1714 return true;
1715
1716 // Check base classes destructor for noreturn.
1717 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001718 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1719 if (RD->isAnyDestructorNoReturn())
1720 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001721
1722 // Check fields for noreturn.
1723 for (const auto *Field : fields())
1724 if (const CXXRecordDecl *RD =
1725 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1726 if (RD->isAnyDestructorNoReturn())
1727 return true;
1728
1729 // All destructors are not noreturn.
1730 return false;
1731}
1732
Erich Keane3e08f662017-09-29 21:06:00 +00001733static bool isDeclContextInNamespace(const DeclContext *DC) {
1734 while (!DC->isTranslationUnit()) {
1735 if (DC->isNamespace())
1736 return true;
1737 DC = DC->getParent();
1738 }
1739 return false;
1740}
1741
Erich Keane58bd6032017-09-15 16:03:35 +00001742bool CXXRecordDecl::isInterfaceLike() const {
1743 assert(hasDefinition() && "checking for interface-like without a definition");
1744 // All __interfaces are inheritently interface-like.
1745 if (isInterface())
1746 return true;
1747
1748 // Interface-like types cannot have a user declared constructor, destructor,
1749 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1750 // cannot be interface types.
1751 if (isLambda() || hasUserDeclaredConstructor() ||
1752 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1753 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1754 return false;
1755
1756 // No interface-like type can have a method with a definition.
1757 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001758 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001759 return false;
1760
1761 // Check "Special" types.
1762 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001763 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1764 // extern C++ block directly in the TU. These are only valid if in one
1765 // of these two situations.
1766 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1767 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001768 ((getName() == "IUnknown" &&
1769 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1770 (getName() == "IDispatch" &&
1771 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1772 if (getNumBases() > 0)
1773 return false;
1774 return true;
1775 }
1776
1777 // FIXME: Any access specifiers is supposed to make this no longer interface
1778 // like.
1779
1780 // If this isn't a 'special' type, it must have a single interface-like base.
1781 if (getNumBases() != 1)
1782 return false;
1783
1784 const auto BaseSpec = *bases_begin();
1785 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1786 return false;
1787 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1788 if (Base->isInterface() || !Base->isInterfaceLike())
1789 return false;
1790 return true;
1791}
1792
Douglas Gregorb11aad82011-02-19 18:51:44 +00001793void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001794 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001795}
1796
1797void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1798 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001799
Douglas Gregor8fb95122010-09-29 00:15:42 +00001800 // If the class may be abstract (but hasn't been marked as such), check for
1801 // any pure final overriders.
1802 if (mayBeAbstract()) {
1803 CXXFinalOverriderMap MyFinalOverriders;
1804 if (!FinalOverriders) {
1805 getFinalOverriders(MyFinalOverriders);
1806 FinalOverriders = &MyFinalOverriders;
1807 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001808
Douglas Gregor8fb95122010-09-29 00:15:42 +00001809 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001810 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001811 MEnd = FinalOverriders->end();
1812 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001813 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001814 SOEnd = M->second.end();
1815 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001816 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001817 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001818
Douglas Gregor8fb95122010-09-29 00:15:42 +00001819 // C++ [class.abstract]p4:
1820 // A class is abstract if it contains or inherits at least one
1821 // pure virtual function for which the final overrider is pure
1822 // virtual.
1823 if (SO->second.front().Method->isPure()) {
1824 data().Abstract = true;
1825 Done = true;
1826 break;
1827 }
1828 }
1829 }
1830 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001831
Douglas Gregor457104e2010-09-29 04:25:11 +00001832 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001833 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001834 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001835 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001836}
1837
1838bool CXXRecordDecl::mayBeAbstract() const {
1839 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1840 isDependentContext())
1841 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001842
Aaron Ballman574705e2014-03-13 15:41:46 +00001843 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001844 const auto *BaseDecl =
1845 cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001846 if (BaseDecl->isAbstract())
1847 return true;
1848 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001849
Douglas Gregor8fb95122010-09-29 00:15:42 +00001850 return false;
1851}
1852
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001853void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001854
1855CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1856 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1857 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1858 SourceLocation EndLocation) {
1859 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1860 NameInfo, T, TInfo, EndLocation);
1861}
1862
1863CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1864 unsigned ID) {
1865 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1866 DeclarationNameInfo(), QualType(),
1867 nullptr, SourceLocation());
1868}
1869
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001870void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001871
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001872bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001873 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001874
1875 if (MD->getStorageClass() == SC_Static)
1876 return true;
1877
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001878 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1879 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001880}
1881
Rafael Espindola49e860b2012-06-26 17:45:31 +00001882static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1883 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001884 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001885 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1886 return true;
1887 if (recursivelyOverrides(MD, BaseMD))
1888 return true;
1889 }
1890 return false;
1891}
1892
1893CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001894CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1895 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001896 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1897 return this;
1898
1899 // Lookup doesn't work for destructors, so handle them separately.
1900 if (isa<CXXDestructorDecl>(this)) {
1901 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001902 if (MD) {
1903 if (recursivelyOverrides(MD, this))
1904 return MD;
1905 if (MayBeBase && recursivelyOverrides(this, MD))
1906 return MD;
1907 }
Craig Topper36250ad2014-05-12 05:36:57 +00001908 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001909 }
1910
Richard Smith40c78062015-02-21 02:31:57 +00001911 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001912 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001913 if (!MD)
1914 continue;
1915 if (recursivelyOverrides(MD, this))
1916 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001917 if (MayBeBase && recursivelyOverrides(this, MD))
1918 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001919 }
1920
Aaron Ballman574705e2014-03-13 15:41:46 +00001921 for (const auto &I : RD->bases()) {
1922 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001923 if (!RT)
1924 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00001925 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00001926 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1927 if (T)
1928 return T;
1929 }
1930
Craig Topper36250ad2014-05-12 05:36:57 +00001931 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001932}
1933
Ted Kremenek21475702008-09-05 17:16:31 +00001934CXXMethodDecl *
1935CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001936 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001937 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001938 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001939 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001940 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001941 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1942 T, TInfo, SC, isInline, isConstexpr,
1943 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001944}
1945
Douglas Gregor72172e92012-01-05 21:55:30 +00001946CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001947 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001948 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001949 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001950}
1951
Akira Hatanaka22461672017-07-13 06:08:27 +00001952CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
1953 bool IsAppleKext) {
1954 assert(isVirtual() && "this method is expected to be virtual");
1955
1956 // When building with -fapple-kext, all calls must go through the vtable since
1957 // the kernel linker can do runtime patching of vtables.
1958 if (IsAppleKext)
1959 return nullptr;
1960
1961 // If the member function is marked 'final', we know that it can't be
1962 // overridden and can therefore devirtualize it unless it's pure virtual.
1963 if (hasAttr<FinalAttr>())
1964 return isPure() ? nullptr : this;
1965
1966 // If Base is unknown, we cannot devirtualize.
1967 if (!Base)
1968 return nullptr;
1969
1970 // If the base expression (after skipping derived-to-base conversions) is a
1971 // class prvalue, then we can devirtualize.
1972 Base = Base->getBestDynamicClassTypeExpr();
1973 if (Base->isRValue() && Base->getType()->isRecordType())
1974 return this;
1975
1976 // If we don't even know what we would call, we can't devirtualize.
1977 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
1978 if (!BestDynamicDecl)
1979 return nullptr;
1980
1981 // There may be a method corresponding to MD in a derived class.
1982 CXXMethodDecl *DevirtualizedMethod =
1983 getCorrespondingMethodInClass(BestDynamicDecl);
1984
1985 // If that method is pure virtual, we can't devirtualize. If this code is
1986 // reached, the result would be UB, not a direct call to the derived class
1987 // function, and we can't assume the derived class function is defined.
1988 if (DevirtualizedMethod->isPure())
1989 return nullptr;
1990
1991 // If that method is marked final, we can devirtualize it.
1992 if (DevirtualizedMethod->hasAttr<FinalAttr>())
1993 return DevirtualizedMethod;
1994
1995 // Similarly, if the class itself is marked 'final' it can't be overridden
1996 // and we can therefore devirtualize the member function call.
1997 if (BestDynamicDecl->hasAttr<FinalAttr>())
1998 return DevirtualizedMethod;
1999
Eugene Zelenko7855e772018-04-03 00:11:50 +00002000 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2001 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00002002 if (VD->getType()->isRecordType())
2003 // This is a record decl. We know the type and can devirtualize it.
2004 return DevirtualizedMethod;
2005
2006 return nullptr;
2007 }
2008
2009 // We can devirtualize calls on an object accessed by a class member access
2010 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2011 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002012 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002013 const ValueDecl *VD = ME->getMemberDecl();
2014 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2015 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002016
2017 // Likewise for calls on an object accessed by a (non-reference) pointer to
2018 // member access.
2019 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2020 if (BO->isPtrMemOp()) {
2021 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2022 if (MPT->getPointeeType()->isRecordType())
2023 return DevirtualizedMethod;
2024 }
2025 }
2026
2027 // We can't devirtualize the call.
2028 return nullptr;
2029}
2030
Artem Belevich78929ef2018-09-21 17:29:33 +00002031bool CXXMethodDecl::isUsualDeallocationFunction(
2032 SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2033 assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002034 if (getOverloadedOperator() != OO_Delete &&
2035 getOverloadedOperator() != OO_Array_Delete)
2036 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002037
2038 // C++ [basic.stc.dynamic.deallocation]p2:
2039 // A template instance is never a usual deallocation function,
2040 // regardless of its signature.
2041 if (getPrimaryTemplate())
2042 return false;
2043
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002044 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002045 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002046 // with exactly one parameter, then that function is a usual (non-placement)
2047 // deallocation function. [...]
2048 if (getNumParams() == 1)
2049 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002050 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002051
Richard Smith5b349582017-10-13 01:55:36 +00002052 // C++ P0722:
2053 // A destroying operator delete is a usual deallocation function if
2054 // removing the std::destroying_delete_t parameter and changing the
2055 // first parameter type from T* to void* results in the signature of
2056 // a usual deallocation function.
2057 if (isDestroyingOperatorDelete())
2058 ++UsualParams;
2059
Richard Smithb2f0f052016-10-10 18:54:32 +00002060 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002061 // [...] If class T does not declare such an operator delete but does
2062 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002063 // exactly two parameters, the second of which has type std::size_t (18.1),
2064 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002065 //
2066 // C++17 says a usual deallocation function is one with the signature
2067 // (void* [, size_t] [, std::align_val_t] [, ...])
2068 // and all such functions are usual deallocation functions. It's not clear
2069 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002070 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002071 if (UsualParams < getNumParams() &&
2072 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2073 Context.getSizeType()))
2074 ++UsualParams;
2075
2076 if (UsualParams < getNumParams() &&
2077 getParamDecl(UsualParams)->getType()->isAlignValT())
2078 ++UsualParams;
2079
2080 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002081 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002082
2083 // In C++17 onwards, all potential usual deallocation functions are actual
2084 // usual deallocation functions.
2085 if (Context.getLangOpts().AlignedAllocation)
2086 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002087
2088 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002089 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002090 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
Artem Belevich78929ef2018-09-21 17:29:33 +00002091 bool Result = true;
2092 for (const auto *D : R) {
2093 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2094 if (FD->getNumParams() == 1) {
2095 PreventedBy.push_back(FD);
2096 Result = false;
2097 }
2098 }
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002099 }
Artem Belevich78929ef2018-09-21 17:29:33 +00002100 return Result;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002101}
2102
Douglas Gregorb139cd52010-05-01 20:49:11 +00002103bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002104 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002105 // A user-declared copy assignment operator X::operator= is a non-static
2106 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002107 // type X, X&, const X&, volatile X& or const volatile X&.
2108 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002109 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002110 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2111 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002112 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002113
Douglas Gregorb139cd52010-05-01 20:49:11 +00002114 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002115 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002116 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002117
Douglas Gregorb139cd52010-05-01 20:49:11 +00002118 ASTContext &Context = getASTContext();
2119 QualType ClassType
2120 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2121 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2122}
2123
Alexis Huntfcaeae42011-05-25 20:50:04 +00002124bool CXXMethodDecl::isMoveAssignmentOperator() const {
2125 // C++0x [class.copy]p19:
2126 // A user-declared move assignment operator X::operator= is a non-static
2127 // non-template member function of class X with exactly one parameter of type
2128 // X&&, const X&&, volatile X&&, or const volatile X&&.
2129 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002130 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2131 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002132 return false;
2133
2134 QualType ParamType = getParamDecl(0)->getType();
2135 if (!isa<RValueReferenceType>(ParamType))
2136 return false;
2137 ParamType = ParamType->getPointeeType();
2138
2139 ASTContext &Context = getASTContext();
2140 QualType ClassType
2141 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2142 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2143}
2144
Anders Carlsson36d87e12009-05-16 23:58:37 +00002145void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002146 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002147 assert(!MD->getParent()->isDependentContext() &&
2148 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002149 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002150
Douglas Gregor832940b2010-03-02 23:58:15 +00002151 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002152}
2153
2154CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002155 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002156 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002157}
2158
2159CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002160 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002161 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002162}
2163
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002164unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002165 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002166 return getASTContext().overridden_methods_size(this);
2167}
2168
Clement Courbet6ecaec82016-07-05 07:49:31 +00002169CXXMethodDecl::overridden_method_range
2170CXXMethodDecl::overridden_methods() const {
2171 if (isa<CXXConstructorDecl>(this))
2172 return overridden_method_range(nullptr, nullptr);
2173 return getASTContext().overridden_methods(this);
2174}
2175
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002176QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2177 const CXXRecordDecl *Decl) {
2178 ASTContext &C = Decl->getASTContext();
2179 QualType ClassTy = C.getTypeDeclType(Decl);
2180 ClassTy = C.getQualifiedType(ClassTy, FPT->getTypeQuals());
2181 return C.getPointerType(ClassTy);
2182}
2183
Ted Kremenek21475702008-09-05 17:16:31 +00002184QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002185 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2186 // If the member function is declared const, the type of this is const X*,
2187 // if the member function is declared volatile, the type of this is
2188 // volatile X*, and if the member function is declared const volatile,
2189 // the type of this is const volatile X*.
Ted Kremenek21475702008-09-05 17:16:31 +00002190 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002191
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002192 return CXXMethodDecl::getThisType(getType()->getAs<FunctionProtoType>(),
2193 getParent());
Ted Kremenek21475702008-09-05 17:16:31 +00002194}
2195
Eli Friedman71a26d82009-12-06 20:50:05 +00002196bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002197 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002198 // which it was instantiated.
2199 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2200 if (!CheckFn)
2201 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002202
Eli Friedman71a26d82009-12-06 20:50:05 +00002203 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002204 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2205 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002206}
2207
Douglas Gregor355efbb2012-02-17 03:02:34 +00002208bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002209 const CXXRecordDecl *P = getParent();
2210 if (P->isLambda()) {
2211 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2212 if (StaticInvoker == this) return true;
2213 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2214 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2215 }
2216 }
2217 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002218}
2219
Alexis Hunt1d792652011-01-08 20:30:50 +00002220CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2221 TypeSourceInfo *TInfo, bool IsVirtual,
2222 SourceLocation L, Expr *Init,
2223 SourceLocation R,
2224 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002225 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2226 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2227 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002228
Alexis Hunt1d792652011-01-08 20:30:50 +00002229CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2230 FieldDecl *Member,
2231 SourceLocation MemberLoc,
2232 SourceLocation L, Expr *Init,
2233 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002234 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2235 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2236 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002237
Alexis Hunt1d792652011-01-08 20:30:50 +00002238CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2239 IndirectFieldDecl *Member,
2240 SourceLocation MemberLoc,
2241 SourceLocation L, Expr *Init,
2242 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002243 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2244 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2245 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002246
Alexis Hunt1d792652011-01-08 20:30:50 +00002247CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002248 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002249 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002250 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002251 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2252 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002253
Artem Dergachevd3942be2018-12-03 22:15:34 +00002254int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +00002255 return Context.getAllocator()
2256 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
Artem Dergachevd3942be2018-12-03 22:15:34 +00002257}
2258
Alexis Hunt1d792652011-01-08 20:30:50 +00002259TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002260 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002261 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002262 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002263 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002264}
2265
Alexis Hunt1d792652011-01-08 20:30:50 +00002266const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002267 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002268 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002269 else
Craig Topper36250ad2014-05-12 05:36:57 +00002270 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002271}
2272
Alexis Hunt1d792652011-01-08 20:30:50 +00002273SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002274 if (isInClassMemberInitializer())
2275 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002276
David Blaikiea81d4102015-01-18 00:12:58 +00002277 if (isAnyMemberInitializer())
2278 return getMemberLocation();
2279
Eugene Zelenko7855e772018-04-03 00:11:50 +00002280 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002281 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002282
Eugene Zelenko7855e772018-04-03 00:11:50 +00002283 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002284}
2285
Alexis Hunt1d792652011-01-08 20:30:50 +00002286SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002287 if (isInClassMemberInitializer()) {
2288 FieldDecl *D = getAnyMember();
2289 if (Expr *I = D->getInClassInitializer())
2290 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002291 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002292 }
2293
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002294 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002295}
2296
Erich Keane9c665062018-08-01 21:02:40 +00002297CXXConstructorDecl::CXXConstructorDecl(
2298 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2299 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2300 bool isExplicitSpecified, bool isInline, bool isImplicitlyDeclared,
2301 bool isConstexpr, InheritedConstructor Inherited)
2302 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2303 SC_None, isInline, isConstexpr, SourceLocation()) {
2304 setNumCtorInitializers(0);
2305 setInheritingConstructor(static_cast<bool>(Inherited));
2306 setImplicit(isImplicitlyDeclared);
2307 if (Inherited)
2308 *getTrailingObjects<InheritedConstructor>() = Inherited;
2309 setExplicitSpecified(isExplicitSpecified);
2310}
2311
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002312void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002313
Richard Smith5179eb72016-06-28 19:03:57 +00002314CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2315 unsigned ID,
2316 bool Inherited) {
2317 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
2318 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2319 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2320 false, false, false, false, InheritedConstructor());
Erich Keane9c665062018-08-01 21:02:40 +00002321 Result->setInheritingConstructor(Inherited);
Richard Smith5179eb72016-06-28 19:03:57 +00002322 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002323}
2324
2325CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00002326CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002327 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002328 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002329 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002330 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00002331 bool isImplicitlyDeclared, bool isConstexpr,
2332 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002333 assert(NameInfo.getName().getNameKind()
2334 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002335 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002336 unsigned Extra =
2337 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
2338 return new (C, RD, Extra) CXXConstructorDecl(
2339 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
2340 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002341}
2342
Richard Smithc2bb8182015-03-24 06:36:48 +00002343CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2344 return CtorInitializers.get(getASTContext().getExternalSource());
2345}
2346
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002347CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2348 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2349 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002350 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002351 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002352
2353 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002354}
2355
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002356bool CXXConstructorDecl::isDefaultConstructor() const {
2357 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002358 // A default constructor for a class X is a constructor of class
2359 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002360 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002361 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002362}
2363
Mike Stump11289f42009-09-09 15:08:12 +00002364bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002365CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002366 return isCopyOrMoveConstructor(TypeQuals) &&
2367 getParamDecl(0)->getType()->isLValueReferenceType();
2368}
2369
2370bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2371 return isCopyOrMoveConstructor(TypeQuals) &&
2372 getParamDecl(0)->getType()->isRValueReferenceType();
2373}
2374
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002375/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002376bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002377 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002378 // A non-template constructor for class X is a copy constructor
2379 // if its first parameter is of type X&, const X&, volatile X& or
2380 // const volatile X&, and either there are no other parameters
2381 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002382 // C++0x [class.copy]p3:
2383 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002384 // first parameter is of type X&&, const X&&, volatile X&&, or
2385 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002386 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002387 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002388 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002389 (getPrimaryTemplate() != nullptr) ||
2390 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002391 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002392
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002393 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002394
2395 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002396 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002397 if (!ParamRefType)
2398 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002399
Douglas Gregorff7028a2009-11-13 23:59:09 +00002400 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002401 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002402
Douglas Gregorff7028a2009-11-13 23:59:09 +00002403 CanQualType PointeeType
2404 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002405 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002406 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002407 if (PointeeType.getUnqualifiedType() != ClassTy)
2408 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002409
John McCall8ccfcb52009-09-24 19:53:00 +00002410 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002411
Douglas Gregorf282a762011-01-21 19:38:21 +00002412 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002413 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002414 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002415}
2416
Anders Carlssond20e7952009-08-28 16:57:08 +00002417bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002418 // C++ [class.conv.ctor]p1:
2419 // A constructor declared without the function-specifier explicit
2420 // that can be called with a single parameter specifies a
2421 // conversion from the type of its first parameter to the type of
2422 // its class. Such a constructor is called a converting
2423 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002424 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002425 return false;
2426
Mike Stump11289f42009-09-09 15:08:12 +00002427 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00002428 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002429 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002430 (getNumParams() > 1 &&
2431 (getParamDecl(1)->hasDefaultArg() ||
2432 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002433}
Douglas Gregor61956c42008-10-31 09:07:45 +00002434
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002435bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002436 if ((getNumParams() < 1) ||
2437 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002438 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002439 return false;
2440
2441 const ParmVarDecl *Param = getParamDecl(0);
2442
2443 ASTContext &Context = getASTContext();
2444 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002445
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002446 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002447 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002448 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2449 if (ParamType.getUnqualifiedType() != ClassTy)
2450 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002451
2452 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002453}
2454
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002455void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002456
Douglas Gregor831c93f2008-11-05 20:51:48 +00002457CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002458CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002459 return new (C, ID)
2460 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2461 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00002462}
2463
2464CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002465CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002466 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002467 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002468 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002469 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002470 assert(NameInfo.getName().getNameKind()
2471 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002472 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002473 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002474 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002475}
2476
Richard Smith5b349582017-10-13 01:55:36 +00002477void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002478 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2479 if (OD && !First->OperatorDelete) {
2480 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002481 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002482 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002483 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002484 }
2485}
2486
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002487void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002488
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002489CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002490CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002491 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002492 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002493 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002494 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002495}
2496
2497CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002498CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002499 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002500 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002501 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002502 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002503 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002504 assert(NameInfo.getName().getNameKind()
2505 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002506 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002507 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002508 isInline, isExplicit, isConstexpr,
2509 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002510}
2511
Douglas Gregord3b672c2012-02-16 01:06:16 +00002512bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2513 return isImplicit() && getParent()->isLambda() &&
2514 getConversionType()->isBlockPointerType();
2515}
2516
Erich Keanec9d29902018-08-01 21:16:54 +00002517LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2518 SourceLocation LangLoc, LanguageIDs lang,
2519 bool HasBraces)
2520 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2521 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2522 setLanguage(lang);
2523 LinkageSpecDeclBits.HasBraces = HasBraces;
2524}
2525
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002526void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002527
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002528LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002529 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002530 SourceLocation ExternLoc,
2531 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002532 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002533 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002534 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002535}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002536
Richard Smithf7981722013-11-22 09:01:48 +00002537LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2538 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002539 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2540 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002541}
2542
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002543void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002544
Douglas Gregor889ceb72009-02-03 19:21:40 +00002545UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2546 SourceLocation L,
2547 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002548 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002549 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002550 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002551 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002552 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002553 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002554 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2555 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002556}
2557
Richard Smithf7981722013-11-22 09:01:48 +00002558UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2559 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002560 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2561 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002562 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002563 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002564}
2565
Sebastian Redla6602e92009-11-23 15:34:23 +00002566NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002567 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002568 return NA->getNamespace();
2569 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2570}
2571
Richard Smith053f6c62014-05-16 23:01:30 +00002572NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2573 SourceLocation StartLoc, SourceLocation IdLoc,
2574 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2575 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002576 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002577 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002578 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002579
Douglas Gregore57e7522012-01-07 09:11:48 +00002580 if (PrevDecl)
2581 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2582}
2583
Douglas Gregor72172e92012-01-05 21:55:30 +00002584NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002585 bool Inline, SourceLocation StartLoc,
2586 SourceLocation IdLoc, IdentifierInfo *Id,
2587 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002588 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2589 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002590}
2591
2592NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002593 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002594 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002595}
2596
Chandler Carruth21c90602015-12-30 03:24:14 +00002597NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2598 if (isFirstDecl())
2599 return this;
2600
2601 return AnonOrFirstNamespaceAndInline.getPointer();
2602}
2603
2604const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2605 if (isFirstDecl())
2606 return this;
2607
2608 return AnonOrFirstNamespaceAndInline.getPointer();
2609}
2610
2611bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2612
Richard Smithd7af8a32014-05-10 01:17:36 +00002613NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2614 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002615}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002616
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002617NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2618 return getPreviousDecl();
2619}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002620
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002621NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2622 return getMostRecentDecl();
2623}
2624
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002625void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002626
Richard Smithf4634362014-09-03 23:11:22 +00002627NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2628 return getNextRedeclaration();
2629}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002630
Richard Smithf4634362014-09-03 23:11:22 +00002631NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2632 return getPreviousDecl();
2633}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002634
Richard Smithf4634362014-09-03 23:11:22 +00002635NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2636 return getMostRecentDecl();
2637}
2638
Mike Stump11289f42009-09-09 15:08:12 +00002639NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002640 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002641 SourceLocation AliasLoc,
2642 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002643 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002644 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002645 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002646 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002647 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002648 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002649 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002650 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002651}
2652
Douglas Gregor72172e92012-01-05 21:55:30 +00002653NamespaceAliasDecl *
2654NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002655 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002656 SourceLocation(), nullptr,
2657 NestedNameSpecifierLoc(),
2658 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002659}
2660
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002661void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002662
Richard Smith5179eb72016-06-28 19:03:57 +00002663UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2664 SourceLocation Loc, UsingDecl *Using,
2665 NamedDecl *Target)
2666 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002667 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002668 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002669 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002670 setImplicit();
2671}
2672
2673UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2674 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002675 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002676
Douglas Gregor72172e92012-01-05 21:55:30 +00002677UsingShadowDecl *
2678UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002679 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002680}
2681
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002682UsingDecl *UsingShadowDecl::getUsingDecl() const {
2683 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002684 while (const auto *NextShadow =
2685 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002686 Shadow = NextShadow;
2687 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2688}
2689
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002690void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002691
2692ConstructorUsingShadowDecl *
2693ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2694 SourceLocation Loc, UsingDecl *Using,
2695 NamedDecl *Target, bool IsVirtual) {
2696 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2697 IsVirtual);
2698}
2699
2700ConstructorUsingShadowDecl *
2701ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2702 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2703}
2704
2705CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2706 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2707}
2708
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002709void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002710
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002711void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2712 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2713 "declaration already in set");
2714 assert(S->getUsingDecl() == this);
2715
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002716 if (FirstUsingShadow.getPointer())
2717 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2718 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002719}
2720
2721void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2722 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2723 "declaration not in set");
2724 assert(S->getUsingDecl() == this);
2725
2726 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2727
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002728 if (FirstUsingShadow.getPointer() == S) {
2729 FirstUsingShadow.setPointer(
2730 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002731 S->UsingOrNextShadow = this;
2732 return;
2733 }
2734
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002735 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002736 while (Prev->UsingOrNextShadow != S)
2737 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2738 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2739 S->UsingOrNextShadow = this;
2740}
2741
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002742UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2743 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002744 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002745 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002746 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002747}
2748
Douglas Gregor72172e92012-01-05 21:55:30 +00002749UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002750 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2751 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2752 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002753}
2754
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002755SourceRange UsingDecl::getSourceRange() const {
2756 SourceLocation Begin = isAccessDeclaration()
2757 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2758 return SourceRange(Begin, getNameInfo().getEndLoc());
2759}
2760
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002761void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002762
2763UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2764 NamedDecl *InstantiatedFrom,
2765 ArrayRef<NamedDecl *> UsingDecls) {
2766 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2767 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2768}
2769
2770UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2771 unsigned NumExpansions) {
2772 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2773 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2774 Result->NumExpansions = NumExpansions;
2775 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2776 for (unsigned I = 0; I != NumExpansions; ++I)
2777 new (Trail + I) NamedDecl*(nullptr);
2778 return Result;
2779}
2780
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002781void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002782
John McCalle61f2ba2009-11-18 02:36:19 +00002783UnresolvedUsingValueDecl *
2784UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2785 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002786 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002787 const DeclarationNameInfo &NameInfo,
2788 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002789 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002790 QualifierLoc, NameInfo,
2791 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002792}
2793
Douglas Gregor72172e92012-01-05 21:55:30 +00002794UnresolvedUsingValueDecl *
2795UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002796 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2797 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002798 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002799 DeclarationNameInfo(),
2800 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002801}
2802
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002803SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2804 SourceLocation Begin = isAccessDeclaration()
2805 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2806 return SourceRange(Begin, getNameInfo().getEndLoc());
2807}
2808
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002809void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002810
John McCalle61f2ba2009-11-18 02:36:19 +00002811UnresolvedUsingTypenameDecl *
2812UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2813 SourceLocation UsingLoc,
2814 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002815 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002816 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002817 DeclarationName TargetName,
2818 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002819 return new (C, DC) UnresolvedUsingTypenameDecl(
2820 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002821 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002822}
2823
Douglas Gregor72172e92012-01-05 21:55:30 +00002824UnresolvedUsingTypenameDecl *
2825UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002826 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002827 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002828 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002829}
2830
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002831void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002832
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002833StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002834 SourceLocation StaticAssertLoc,
2835 Expr *AssertExpr,
2836 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002837 SourceLocation RParenLoc,
2838 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002839 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2840 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002841}
2842
Richard Smithf7981722013-11-22 09:01:48 +00002843StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002844 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002845 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2846 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002847}
2848
Richard Smithbdb84f32016-07-22 23:36:59 +00002849void BindingDecl::anchor() {}
2850
2851BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2852 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002853 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002854}
2855
2856BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002857 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002858}
2859
Richard Smith97fcf4b2016-08-14 23:15:52 +00002860VarDecl *BindingDecl::getHoldingVar() const {
2861 Expr *B = getBinding();
2862 if (!B)
2863 return nullptr;
2864 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2865 if (!DRE)
2866 return nullptr;
2867
2868 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2869 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2870 return VD;
2871}
2872
Richard Smithbdb84f32016-07-22 23:36:59 +00002873void DecompositionDecl::anchor() {}
2874
2875DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2876 SourceLocation StartLoc,
2877 SourceLocation LSquareLoc,
2878 QualType T, TypeSourceInfo *TInfo,
2879 StorageClass SC,
2880 ArrayRef<BindingDecl *> Bindings) {
2881 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2882 return new (C, DC, Extra)
2883 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2884}
2885
2886DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2887 unsigned ID,
2888 unsigned NumBindings) {
2889 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002890 auto *Result = new (C, ID, Extra)
2891 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2892 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002893 // Set up and clean out the bindings array.
2894 Result->NumBindings = NumBindings;
2895 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2896 for (unsigned I = 0; I != NumBindings; ++I)
2897 new (Trail + I) BindingDecl*(nullptr);
2898 return Result;
2899}
2900
Richard Smith7873de02016-08-11 22:25:46 +00002901void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2902 os << '[';
2903 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002904 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00002905 if (Comma)
2906 os << ", ";
2907 B->printName(os);
2908 Comma = true;
2909 }
2910 os << ']';
2911}
2912
Richard Trieub3e902f2018-12-29 02:02:30 +00002913void MSPropertyDecl::anchor() {}
2914
Richard Smithf7981722013-11-22 09:01:48 +00002915MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2916 SourceLocation L, DeclarationName N,
2917 QualType T, TypeSourceInfo *TInfo,
2918 SourceLocation StartL,
2919 IdentifierInfo *Getter,
2920 IdentifierInfo *Setter) {
2921 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2922}
2923
2924MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2925 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002926 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2927 DeclarationName(), QualType(), nullptr,
2928 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002929}
2930
Anders Carlsson6750d162009-03-26 23:46:50 +00002931static const char *getAccessName(AccessSpecifier AS) {
2932 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002933 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002934 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002935 case AS_public:
2936 return "public";
2937 case AS_private:
2938 return "private";
2939 case AS_protected:
2940 return "protected";
2941 }
David Blaikief47fa302012-01-17 02:30:50 +00002942 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002943}
2944
2945const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2946 AccessSpecifier AS) {
2947 return DB << getAccessName(AS);
2948}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002949
2950const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2951 AccessSpecifier AS) {
2952 return DB << getAccessName(AS);
2953}