blob: 3cb186760a4f95e56f8675e0564c69dac2197d22 [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);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000131 R->MayHaveOutOfDateDef = 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);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000146 R->IsBeingDefined = 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);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000150 R->MayHaveOutOfDateDef = 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);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000161 R->MayHaveOutOfDateDef = false;
162 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;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000206
207 // 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:
260 // A class that declares or inherits a virtual function is called a
261 // polymorphic class.
262 if (BaseClassDecl->isPolymorphic())
263 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000264
Chandler Carruthb1963742011-04-30 09:17:45 +0000265 // C++0x [class]p7:
266 // A standard-layout class is a class that: [...]
267 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000268 if (!BaseClassDecl->isStandardLayout())
269 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000270 if (!BaseClassDecl->isCXX11StandardLayout())
271 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000272
Chandler Carruthe71d0622011-04-24 02:49:34 +0000273 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000274 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000275 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000276
Anders Carlssonabb20e62010-03-29 05:13:12 +0000277 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000278 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000279 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000280 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000281 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000282
283 // C++11 [class.copy]p8:
284 // The implicitly-declared copy constructor for a class X will have
285 // the form 'X::X(const X&)' if each [...] virtual base class B of X
286 // has a copy constructor whose first parameter is of type
287 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000288 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000289 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000290 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000291
292 // C++1z [dcl.init.agg]p1:
293 // An aggregate is a class with [...] no virtual base classes
294 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000295 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000296 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000297
298 if (Base->isVirtual()) {
299 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000300 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000301 VBases.push_back(Base);
302
David Majnemer5cfda6f2016-05-22 05:34:26 +0000303 // C++14 [meta.unary.prop] is_empty:
304 // T is a class type, but not a union type, with ... no virtual base
305 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000306 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000307
Richard Smith872307e2016-03-08 22:17:41 +0000308 // C++1z [dcl.init.agg]p1:
309 // An aggregate is a class with [...] no virtual base classes
310 data().Aggregate = false;
311
Richard Smith328aae52012-11-30 05:11:39 +0000312 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
313 // A [default constructor, copy/move constructor, or copy/move assignment
314 // operator for a class X] is trivial [...] if:
315 // -- class X has [...] no virtual base classes
316 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000317 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000318
319 // C++0x [class]p7:
320 // A standard-layout class is a class that: [...]
321 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000322 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000323 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000324
325 // C++11 [dcl.constexpr]p4:
326 // In the definition of a constexpr constructor [...]
327 // -- the class shall not have any virtual base classes
328 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000329
330 // C++1z [class.copy]p8:
331 // The implicitly-declared copy constructor for a class X will have
332 // the form 'X::X(const X&)' if each potentially constructed subobject
333 // has a copy constructor whose first parameter is of type
334 // 'const B&' or 'const volatile B&' [...]
335 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
336 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000337 } else {
338 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000339 // A default constructor is trivial [...] if:
340 // -- all the direct base classes of its class have trivial default
341 // constructors.
342 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000343 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
344
Chandler Carruthad7d4042011-04-23 23:10:33 +0000345 // C++0x [class.copy]p13:
346 // A copy/move constructor for class X is trivial if [...]
347 // [...]
348 // -- the constructor selected to copy/move each direct base class
349 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000350 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000351 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000352
353 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
354 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
355
Richard Smith6b02d462012-12-08 08:32:28 +0000356 // If the base class doesn't have a simple move constructor, we'll eagerly
357 // declare it and perform overload resolution to determine which function
358 // it actually calls. If it does have a simple move constructor, this
359 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000360 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000361 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000362
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000363 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
364 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
365
Chandler Carruthad7d4042011-04-23 23:10:33 +0000366 // C++0x [class.copy]p27:
367 // A copy/move assignment operator for class X is trivial if [...]
368 // [...]
369 // -- the assignment operator selected to copy/move each direct base
370 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000371 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000372 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000373 // If the base class doesn't have a simple move assignment, we'll eagerly
374 // declare it and perform overload resolution to determine which function
375 // it actually calls. If it does have a simple move assignment, this
376 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000377 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000378 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000379
380 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000381 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000382 // requirements of a constexpr constructor, the implicitly-defined
383 // default constructor is constexpr.
384 if (!BaseClassDecl->hasConstexprDefaultConstructor())
385 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000386
387 // C++1z [class.copy]p8:
388 // The implicitly-declared copy constructor for a class X will have
389 // the form 'X::X(const X&)' if each potentially constructed subobject
390 // has a copy constructor whose first parameter is of type
391 // 'const B&' or 'const volatile B&' [...]
392 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
393 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000394 }
Richard Smith92f241f2012-12-08 02:53:02 +0000395
Douglas Gregor11c024b2010-09-28 20:50:54 +0000396 // C++ [class.ctor]p3:
397 // A destructor is trivial if all the direct base classes of its class
398 // have trivial destructors.
399 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000400 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000401
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000402 if (!BaseClassDecl->hasTrivialDestructorForCall())
403 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
404
Richard Smith561fb152012-02-25 07:33:38 +0000405 if (!BaseClassDecl->hasIrrelevantDestructor())
406 data().HasIrrelevantDestructor = false;
407
Richard Smith1c33fe82012-11-28 06:23:12 +0000408 // C++11 [class.copy]p18:
409 // The implicitly-declared copy assignment oeprator for a class X will
410 // have the form 'X& X::operator=(const X&)' if each direct base class B
411 // of X has a copy assignment operator whose parameter is of type 'const
412 // B&', 'const volatile B&', or 'B' [...]
413 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
414 data().ImplicitCopyAssignmentHasConstParam = false;
415
John McCall31168b02011-06-15 23:02:42 +0000416 // A class has an Objective-C object member if... or any of its bases
417 // has an Objective-C object member.
418 if (BaseClassDecl->hasObjectMember())
419 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000420
421 if (BaseClassDecl->hasVolatileMember())
422 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000423
Douglas Gregor61226d32011-05-13 01:05:07 +0000424 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000425 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000426 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000427 data().NeedOverloadResolutionForCopyConstructor = true;
428 }
Richard Smith593f9932012-12-08 02:01:17 +0000429
430 if (BaseClassDecl->hasUninitializedReferenceMember())
431 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000432
Nico Weber6a6376b2016-02-19 01:52:46 +0000433 if (!BaseClassDecl->allowConstDefaultInit())
434 data().HasUninitializedFields = true;
435
Richard Smith6b02d462012-12-08 08:32:28 +0000436 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000437 }
Richard Smithb6070db2018-04-05 18:55:37 +0000438
439 // C++2a [class]p7:
440 // A class S is a standard-layout class if it:
441 // -- has at most one base class subobject of any given type
442 //
443 // Note that we only need to check this for classes with more than one base
444 // class. If there's only one base class, and it's standard layout, then
445 // we know there are no repeated base classes.
446 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
447 data().IsStandardLayout = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000448
David Majnemer5ef4fe72014-06-13 06:43:46 +0000449 if (VBases.empty()) {
450 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000451 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000452 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000453
454 // Create base specifier for any direct or indirect virtual bases.
455 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
456 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000457 for (int I = 0, E = VBases.size(); I != E; ++I) {
458 QualType Type = VBases[I]->getType();
459 if (!Type->isDependentType())
460 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000461 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000462 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000463
464 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000465}
466
Richard Trieufd1acbb2017-04-11 21:31:00 +0000467unsigned CXXRecordDecl::getODRHash() const {
468 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000469
Richard Trieufd1acbb2017-04-11 21:31:00 +0000470 // Previously calculated hash is stored in DefinitionData.
471 if (DefinitionData->HasODRHash)
472 return DefinitionData->ODRHash;
473
474 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000475 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000476 Hash.AddCXXRecordDecl(getDefinition());
477 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000478 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000479
480 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000481}
Richard Trieub6adf542017-02-18 02:09:28 +0000482
Richard Smith6b02d462012-12-08 08:32:28 +0000483void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
484 // C++11 [class.copy]p11:
485 // A defaulted copy/move constructor for a class X is defined as
486 // deleted if X has:
487 // -- a direct or virtual base class B that cannot be copied/moved [...]
488 // -- a non-static data member of class type M (or array thereof)
489 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000490 if (!Subobj->hasSimpleCopyConstructor())
491 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000492 if (!Subobj->hasSimpleMoveConstructor())
493 data().NeedOverloadResolutionForMoveConstructor = true;
494
495 // C++11 [class.copy]p23:
496 // A defaulted copy/move assignment operator for a class X is defined as
497 // deleted if X has:
498 // -- a direct or virtual base class B that cannot be copied/moved [...]
499 // -- a non-static data member of class type M (or array thereof)
500 // that cannot be copied or moved [...]
501 if (!Subobj->hasSimpleMoveAssignment())
502 data().NeedOverloadResolutionForMoveAssignment = true;
503
504 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
505 // A defaulted [ctor or dtor] for a class X is defined as
506 // deleted if X has:
507 // -- any direct or virtual base class [...] has a type with a destructor
508 // that is deleted or inaccessible from the defaulted [ctor or dtor].
509 // -- any non-static data member has a type with a destructor
510 // that is deleted or inaccessible from the defaulted [ctor or dtor].
511 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000512 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000513 data().NeedOverloadResolutionForMoveConstructor = true;
514 data().NeedOverloadResolutionForDestructor = true;
515 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000516}
517
Douglas Gregord2e6a452010-01-14 17:47:39 +0000518bool CXXRecordDecl::hasAnyDependentBases() const {
519 if (!isDependentContext())
520 return false;
521
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000522 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000523}
524
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000525bool CXXRecordDecl::isTriviallyCopyable() const {
526 // C++0x [class]p5:
527 // A trivially copyable class is a class that:
528 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000529 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000530 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000531 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000532 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000533 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000534 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000535 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000536 // -- has a trivial destructor.
537 if (!hasTrivialDestructor()) return false;
538
539 return true;
540}
541
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000542void CXXRecordDecl::markedVirtualFunctionPure() {
543 // C++ [class.abstract]p2:
544 // A class is abstract if it has at least one pure virtual function.
545 data().Abstract = true;
546}
547
Richard Smithb6070db2018-04-05 18:55:37 +0000548bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
549 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
550 if (!getNumBases())
551 return false;
552
553 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
554 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
555 SmallVector<const CXXRecordDecl*, 8> WorkList;
556
557 // Visit a type that we have determined is an element of M(S).
558 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
559 RD = RD->getCanonicalDecl();
560
561 // C++2a [class]p8:
562 // A class S is a standard-layout class if it [...] has no element of the
563 // set M(S) of types as a base class.
564 //
565 // If we find a subobject of an empty type, it might also be a base class,
566 // so we'll need to walk the base classes to check.
567 if (!RD->data().HasBasesWithFields) {
568 // Walk the bases the first time, stopping if we find the type. Build a
569 // set of them so we don't need to walk them again.
570 if (Bases.empty()) {
571 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
572 Base = Base->getCanonicalDecl();
573 if (RD == Base)
574 return false;
575 Bases.insert(Base);
576 return true;
577 });
578 if (RDIsBase)
579 return true;
580 } else {
581 if (Bases.count(RD))
582 return true;
583 }
584 }
585
586 if (M.insert(RD).second)
587 WorkList.push_back(RD);
588 return false;
589 };
590
591 if (Visit(XFirst))
592 return true;
593
594 while (!WorkList.empty()) {
595 const CXXRecordDecl *X = WorkList.pop_back_val();
596
597 // FIXME: We don't check the bases of X. That matches the standard, but
598 // that sure looks like a wording bug.
599
600 // -- If X is a non-union class type with a non-static data member
601 // [recurse to] the first non-static data member of X
602 // -- If X is a union type, [recurse to union members]
603 for (auto *FD : X->fields()) {
604 // FIXME: Should we really care about the type of the first non-static
605 // data member of a non-union if there are preceding unnamed bit-fields?
606 if (FD->isUnnamedBitfield())
607 continue;
608
609 // -- If X is n array type, [visit the element type]
610 QualType T = Ctx.getBaseElementType(FD->getType());
611 if (auto *RD = T->getAsCXXRecordDecl())
612 if (Visit(RD))
613 return true;
614
615 if (!X->isUnion())
616 break;
617 }
618 }
619
620 return false;
621}
622
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000623void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000624 if (!D->isImplicit() &&
625 !isa<FieldDecl>(D) &&
626 !isa<IndirectFieldDecl>(D) &&
627 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
628 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
629 data().HasOnlyCMembers = false;
630
631 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000632 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000633 return;
634
Eugene Zelenko7855e772018-04-03 00:11:50 +0000635 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000636 if (FunTmpl)
637 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000638
639 // FIXME: Pass NamedDecl* to addedMember?
640 Decl *DUnderlying = D;
641 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
642 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000643 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000644 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
645 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000646
Eugene Zelenko7855e772018-04-03 00:11:50 +0000647 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000648 if (Method->isVirtual()) {
649 // C++ [dcl.init.aggr]p1:
650 // An aggregate is an array or a class with [...] no virtual functions.
651 data().Aggregate = false;
652
653 // C++ [class]p4:
654 // A POD-struct is an aggregate class...
655 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000656
David Majnemer5cfda6f2016-05-22 05:34:26 +0000657 // C++14 [meta.unary.prop]p4:
658 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000659 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000660
661 // C++ [class.virtual]p1:
662 // A class that declares or inherits a virtual function is called a
663 // polymorphic class.
664 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000665
Richard Smith328aae52012-11-30 05:11:39 +0000666 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
667 // A [default constructor, copy/move constructor, or copy/move
668 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000669 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000670 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000671 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000672
Chandler Carruthb1963742011-04-30 09:17:45 +0000673 // C++0x [class]p7:
674 // A standard-layout class is a class that: [...]
675 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000676 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000677 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000678 }
679 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000680
Richard Smith1c33fe82012-11-28 06:23:12 +0000681 // Notify the listener if an implicit member was added after the definition
682 // was completed.
683 if (!isBeingDefined() && D->isImplicit())
684 if (ASTMutationListener *L = getASTMutationListener())
685 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000686
Richard Smith328aae52012-11-30 05:11:39 +0000687 // The kind of special member this declaration is, if any.
688 unsigned SMKind = 0;
689
Richard Smith1c33fe82012-11-28 06:23:12 +0000690 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000691 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000692 if (!Constructor->isImplicit()) {
693 // Note that we have a user-declared constructor.
694 data().UserDeclaredConstructor = true;
695
696 // C++ [class]p4:
697 // A POD-struct is an aggregate class [...]
698 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
699 // type is technically an aggregate in C++0x since it wouldn't be in 03.
700 data().PlainOldData = false;
701 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000702
Alexis Hunt88c75c32011-05-09 21:45:35 +0000703 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000704 SMKind |= SMF_DefaultConstructor;
705
Richard Smith5179eb72016-06-28 19:03:57 +0000706 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000707 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000708 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000709 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000710 if (Constructor->isDefaulted())
711 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000712 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000713
Chandler Carruthad7d4042011-04-23 23:10:33 +0000714 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000715 unsigned Quals;
716 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000717 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000718
719 if (Quals & Qualifiers::Const)
720 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000721 } else if (Constructor->isMoveConstructor())
722 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000723 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000724
Eric Fiselier283d8d42016-12-03 01:26:47 +0000725 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith505ef812016-12-21 01:57:02 +0000726 // An aggregate is an array or a class with no user-provided, explicit, or
727 // inherited constructors
728 if (Constructor->isUserProvided() || Constructor->isExplicit())
Eric Fiselier283d8d42016-12-03 01:26:47 +0000729 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000730 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000731
Richard Smith5179eb72016-06-28 19:03:57 +0000732 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000733 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000734 // Record if we see any constexpr constructors which are neither copy
735 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000736 // C++1z [basic.types]p10:
737 // [...] has at least one constexpr constructor or constructor template
738 // (possibly inherited from a base class) that is not a copy or move
739 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000740 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000741 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000742 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000743
Richard Smith1c33fe82012-11-28 06:23:12 +0000744 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000745 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000746 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000747
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000748 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000749 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000750 // If the destructor is explicitly defaulted and not trivial or not public
751 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
752 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000753
Richard Smith1c33fe82012-11-28 06:23:12 +0000754 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000755 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000756 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000757 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000758 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
759 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000760 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000761
762 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000763 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000764 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000765 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000766
Eugene Zelenko7855e772018-04-03 00:11:50 +0000767 const auto *ParamTy =
768 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000769 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
770 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000771 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000772
Richard Smith328aae52012-11-30 05:11:39 +0000773 if (Method->isMoveAssignmentOperator())
774 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000775
Douglas Gregor457104e2010-09-29 04:25:11 +0000776 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000777 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000778 // FIXME: We use the 'unsafe' accessor for the access specifier here,
779 // because Sema may not have set it yet. That's really just a misdesign
780 // in Sema. However, LLDB *will* have set the access specifier correctly,
781 // and adds declarations after the class is technically completed,
782 // so completeDefinition()'s overriding of the access specifiers doesn't
783 // work.
784 AccessSpecifier AS = Conversion->getAccessUnsafe();
785
Richard Smith328aae52012-11-30 05:11:39 +0000786 if (Conversion->getPrimaryTemplate()) {
787 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000788 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000789 ASTContext &Ctx = getASTContext();
790 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
791 NamedDecl *Primary =
792 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
793 if (Primary->getPreviousDecl())
794 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
795 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000796 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000797 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000798 }
799 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000800
Richard Smith328aae52012-11-30 05:11:39 +0000801 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000802 // If this is the first declaration of a special member, we no longer have
803 // an implicit trivial special member.
804 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000805 data().DeclaredSpecialMembers | ~SMKind;
806 data().HasTrivialSpecialMembersForCall &=
807 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000808
809 if (!Method->isImplicit() && !Method->isUserProvided()) {
810 // This method is user-declared but not user-provided. We can't work out
811 // whether it's trivial yet (not until we get to the end of the class).
812 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000813 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000814 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000815 data().HasTrivialSpecialMembersForCall |= SMKind;
816 } else if (Method->isTrivialForCall()) {
817 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000818 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000819 } else {
820 data().DeclaredNonTrivialSpecialMembers |= SMKind;
821 // If this is a user-provided function, do not set
822 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
823 // yet whether the method would be considered non-trivial for the
824 // purpose of calls (attribute "trivial_abi" can be dropped from the
825 // class later, which can change the special method's triviality).
826 if (!Method->isUserProvided())
827 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
828 }
Richard Smith92f241f2012-12-08 02:53:02 +0000829
Richard Smith328aae52012-11-30 05:11:39 +0000830 // Note when we have declared a declared special member, and suppress the
831 // implicit declaration of this special member.
832 data().DeclaredSpecialMembers |= SMKind;
833
834 if (!Method->isImplicit()) {
835 data().UserDeclaredSpecialMembers |= SMKind;
836
837 // C++03 [class]p4:
838 // A POD-struct is an aggregate class that has [...] no user-defined
839 // copy assignment operator and no user-defined destructor.
840 //
841 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
842 // aggregates could not have any constructors, clear it even for an
843 // explicitly defaulted or deleted constructor.
844 // type is technically an aggregate in C++0x since it wouldn't be in 03.
845 //
846 // Also, a user-declared move assignment operator makes a class non-POD.
847 // This is an extension in C++03.
848 data().PlainOldData = false;
849 }
Richard Smith328aae52012-11-30 05:11:39 +0000850 }
851
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000852 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000853 }
Richard Smith328aae52012-11-30 05:11:39 +0000854
Richard Smithb6070db2018-04-05 18:55:37 +0000855 ASTContext &Context = getASTContext();
856
Douglas Gregora832d3e2010-09-28 19:45:33 +0000857 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000858 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb6070db2018-04-05 18:55:37 +0000859 // C++2a [class]p7:
860 // A standard-layout class is a class that:
861 // [...]
862 // -- has all non-static data members and bit-fields in the class and
863 // its base classes first declared in the same class
864 if (data().HasBasesWithFields)
865 data().IsStandardLayout = false;
866
Douglas Gregor556e5862011-10-10 17:22:13 +0000867 // C++ [class.bit]p2:
868 // A declaration for a bit-field that omits the identifier declares an
869 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
870 // initialized.
871 if (Field->isUnnamedBitfield())
872 return;
873
Richard Smithb6070db2018-04-05 18:55:37 +0000874 // C++11 [class]p7:
875 // A standard-layout class is a class that:
876 // -- either has no non-static data members in the most derived class
877 // [...] or has no base classes with non-static data members
878 if (data().HasBasesWithNonStaticDataMembers)
879 data().IsCXX11StandardLayout = false;
880
Douglas Gregora832d3e2010-09-28 19:45:33 +0000881 // C++ [dcl.init.aggr]p1:
882 // An aggregate is an array or a class (clause 9) with [...] no
883 // private or protected non-static data members (clause 11).
884 //
885 // A POD must be an aggregate.
886 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
887 data().Aggregate = false;
888 data().PlainOldData = false;
889 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000890
Richard Smithb6070db2018-04-05 18:55:37 +0000891 // Track whether this is the first field. We use this when checking
892 // whether the class is standard-layout below.
893 bool IsFirstField = !data().HasPrivateFields &&
894 !data().HasProtectedFields && !data().HasPublicFields;
895
Chandler Carruthb1963742011-04-30 09:17:45 +0000896 // C++0x [class]p7:
897 // A standard-layout class is a class that:
898 // [...]
899 // -- has the same access control for all non-static data members,
900 switch (D->getAccess()) {
901 case AS_private: data().HasPrivateFields = true; break;
902 case AS_protected: data().HasProtectedFields = true; break;
903 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000904 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000905 };
906 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000907 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000908 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000909 data().IsCXX11StandardLayout = false;
910 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000911
Douglas Gregor61226d32011-05-13 01:05:07 +0000912 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000913 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000914 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000915 data().NeedOverloadResolutionForCopyConstructor = true;
916 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000917
918 // C++11 [class.union]p8, DR1460:
919 // If X is a union, a non-static data member of X that is not an anonymous
920 // union is a variant member of X.
921 if (isUnion() && !Field->isAnonymousStructOrUnion())
922 data().HasVariantMembers = true;
923
Chandler Carruthad7d4042011-04-23 23:10:33 +0000924 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000925 // A POD struct is a class that is both a trivial class and a
926 // standard-layout class, and has no non-static data members of type
927 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000928 //
929 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
930 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000931 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000932 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000933 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000934 // Objective-C Automatic Reference Counting:
935 // If a class has a non-static data member of Objective-C pointer
936 // type (or array thereof), it is a non-POD type and its
937 // default constructor (if any), copy constructor, move constructor,
938 // copy assignment operator, move assignment operator, and destructor are
939 // non-trivial.
940 setHasObjectMember(true);
941 struct DefinitionData &Data = data();
942 Data.PlainOldData = false;
943 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000944
945 // __strong or __weak fields do not make special functions non-trivial
946 // for the purpose of calls.
947 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
948 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
949 data().HasTrivialSpecialMembersForCall = 0;
950
951 // Structs with __weak fields should never be passed directly.
952 if (LT == Qualifiers::OCL_Weak)
953 setCanPassInRegisters(false);
954
Ben Langmuir11eab612014-09-26 15:27:29 +0000955 Data.HasIrrelevantDestructor = false;
Brian Kelley036603a2017-03-29 17:31:42 +0000956 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
957 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000958 }
Eli Friedmana5433322013-07-20 01:06:31 +0000959 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000960 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000961
Chandler Carruthb1963742011-04-30 09:17:45 +0000962 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000963 if (!Field->hasInClassInitializer())
964 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000965
Chandler Carruthb1963742011-04-30 09:17:45 +0000966 // C++0x [class]p7:
967 // A standard-layout class is a class that:
968 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000969 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000970 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +0000971
972 // C++1z [class.copy.ctor]p10:
973 // A defaulted copy constructor for a class X is defined as deleted if X has:
974 // -- a non-static data member of rvalue reference type
975 if (T->isRValueReferenceType())
976 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +0000977 }
978
Nico Weber6a6376b2016-02-19 01:52:46 +0000979 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +0000980 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000981 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000982 data().HasUninitializedFields = true;
983 } else {
984 data().HasUninitializedFields = true;
985 }
986 }
987
Richard Smith3607ffe2012-02-13 03:54:03 +0000988 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000989 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000990 data().HasNonLiteralTypeFieldsOrBases = true;
991
Richard Smithab44d5b2013-12-10 08:25:00 +0000992 if (Field->hasInClassInitializer() ||
993 (Field->isAnonymousStructOrUnion() &&
994 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000995 data().HasInClassInitializer = true;
996
997 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000998 // A default constructor is trivial if [...] no non-static data member
999 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001000 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001001
Richard Smithe2648ba2012-05-07 01:07:30 +00001002 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001003 // An aggregate is a [...] class with [...] no
1004 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001005 //
Richard Smith872307e2016-03-08 22:17:41 +00001006 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001007 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001008 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001009
Richard Smithe2648ba2012-05-07 01:07:30 +00001010 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001011 // A POD struct is [...] a trivial class.
1012 data().PlainOldData = false;
1013 }
1014
Richard Smith6b02d462012-12-08 08:32:28 +00001015 // C++11 [class.copy]p23:
1016 // A defaulted copy/move assignment operator for a class X is defined
1017 // as deleted if X has:
1018 // -- a non-static data member of reference type
1019 if (T->isReferenceType())
1020 data().DefaultedMoveAssignmentIsDeleted = true;
1021
Eugene Zelenko7855e772018-04-03 00:11:50 +00001022 if (const auto *RecordTy = T->getAs<RecordType>()) {
1023 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001024 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001025 addedClassSubobject(FieldRec);
1026
Richard Smithc91d12c2013-11-25 07:07:05 +00001027 // We may need to perform overload resolution to determine whether a
1028 // field can be moved if it's const or volatile qualified.
1029 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001030 // We need to care about 'const' for the copy constructor because an
1031 // implicit copy constructor might be declared with a non-const
1032 // parameter.
1033 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001034 data().NeedOverloadResolutionForMoveConstructor = true;
1035 data().NeedOverloadResolutionForMoveAssignment = true;
1036 }
1037
Richard Smith6b02d462012-12-08 08:32:28 +00001038 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1039 // A defaulted [special member] for a class X is defined as
1040 // deleted if:
1041 // -- X is a union-like class that has a variant member with a
1042 // non-trivial [corresponding special member]
1043 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001044 if (FieldRec->hasNonTrivialCopyConstructor())
1045 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001046 if (FieldRec->hasNonTrivialMoveConstructor())
1047 data().DefaultedMoveConstructorIsDeleted = true;
1048 if (FieldRec->hasNonTrivialMoveAssignment())
1049 data().DefaultedMoveAssignmentIsDeleted = true;
1050 if (FieldRec->hasNonTrivialDestructor())
1051 data().DefaultedDestructorIsDeleted = true;
1052 }
1053
Richard Smithdd5619f2016-08-16 00:13:47 +00001054 // For an anonymous union member, our overload resolution will perform
1055 // overload resolution for its members.
1056 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001057 data().NeedOverloadResolutionForCopyConstructor |=
1058 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001059 data().NeedOverloadResolutionForMoveConstructor |=
1060 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1061 data().NeedOverloadResolutionForMoveAssignment |=
1062 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1063 data().NeedOverloadResolutionForDestructor |=
1064 FieldRec->data().NeedOverloadResolutionForDestructor;
1065 }
1066
Alexis Huntf479f1b2011-05-09 18:22:59 +00001067 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001068 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001069 // -- for all the non-static data members of its class that are of
1070 // class type (or array thereof), each such class has a trivial
1071 // default constructor.
1072 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001073 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001074
1075 // C++0x [class.copy]p13:
1076 // A copy/move constructor for class X is trivial if [...]
1077 // [...]
1078 // -- for each non-static data member of X that is of class type (or
1079 // an array thereof), the constructor selected to copy/move that
1080 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001081 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001082 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001083
1084 if (!FieldRec->hasTrivialCopyConstructorForCall())
1085 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1086
Richard Smith6b02d462012-12-08 08:32:28 +00001087 // If the field doesn't have a simple move constructor, we'll eagerly
1088 // declare the move constructor for this class and we'll decide whether
1089 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001090 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001091 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001092
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001093 if (!FieldRec->hasTrivialMoveConstructorForCall())
1094 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1095
Chandler Carruthad7d4042011-04-23 23:10:33 +00001096 // C++0x [class.copy]p27:
1097 // A copy/move assignment operator for class X is trivial if [...]
1098 // [...]
1099 // -- for each non-static data member of X that is of class type (or
1100 // an array thereof), the assignment operator selected to
1101 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001102 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001103 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001104 // If the field doesn't have a simple move assignment, we'll eagerly
1105 // declare the move assignment for this class and we'll decide whether
1106 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001107 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001108 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001109
Douglas Gregor11c024b2010-09-28 20:50:54 +00001110 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001111 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001112 if (!FieldRec->hasTrivialDestructorForCall())
1113 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001114 if (!FieldRec->hasIrrelevantDestructor())
1115 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001116 if (FieldRec->hasObjectMember())
1117 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001118 if (FieldRec->hasVolatileMember())
1119 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +00001120
1121 // C++0x [class]p7:
1122 // A standard-layout class is a class that:
1123 // -- has no non-static data members of type non-standard-layout
1124 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001125 if (!FieldRec->isStandardLayout())
1126 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001127 if (!FieldRec->isCXX11StandardLayout())
1128 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001129
Richard Smithb6070db2018-04-05 18:55:37 +00001130 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001131 // A standard-layout class is a class that:
1132 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001133 // -- has no element of the set M(S) of types as a base class.
1134 if (data().IsStandardLayout && (isUnion() || IsFirstField) &&
1135 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1136 data().IsStandardLayout = false;
1137
1138 // C++11 [class]p7:
1139 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001140 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001141 // data member
1142 if (data().IsCXX11StandardLayout && IsFirstField) {
1143 // FIXME: We should check all base classes here, not just direct
1144 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001145 for (const auto &BI : bases()) {
1146 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001147 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001148 break;
1149 }
1150 }
1151 }
Richard Smithb6070db2018-04-05 18:55:37 +00001152
Douglas Gregor61226d32011-05-13 01:05:07 +00001153 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001154 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001155 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001156 data().NeedOverloadResolutionForCopyConstructor = true;
1157 }
Richard Smithcc36f692011-12-22 02:22:31 +00001158
1159 // C++11 [class.copy]p13:
1160 // If the implicitly-defined constructor would satisfy the
1161 // requirements of a constexpr constructor, the implicitly-defined
1162 // constructor is constexpr.
1163 // C++11 [dcl.constexpr]p4:
1164 // -- every constructor involved in initializing non-static data
1165 // members [...] shall be a constexpr constructor
1166 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001167 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001168 // The standard requires any in-class initializer to be a constant
1169 // expression. We consider this to be a defect.
1170 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001171
1172 // C++11 [class.copy]p8:
1173 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001174 // the form 'X::X(const X&)' if each potentially constructed subobject
1175 // of a class type M (or array thereof) has a copy constructor whose
1176 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001177 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001178 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001179
1180 // C++11 [class.copy]p18:
1181 // The implicitly-declared copy assignment oeprator for a class X will
1182 // have the form 'X& X::operator=(const X&)' if [...] for all the
1183 // non-static data members of X that are of a class type M (or array
1184 // thereof), each such class type has a copy assignment operator whose
1185 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1186 if (!FieldRec->hasCopyAssignmentWithConstParam())
1187 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001188
1189 if (FieldRec->hasUninitializedReferenceMember() &&
1190 !Field->hasInClassInitializer())
1191 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001192
1193 // C++11 [class.union]p8, DR1460:
1194 // a non-static data member of an anonymous union that is a member of
1195 // X is also a variant member of X.
1196 if (FieldRec->hasVariantMembers() &&
1197 Field->isAnonymousStructOrUnion())
1198 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001199 }
Richard Smithcc36f692011-12-22 02:22:31 +00001200 } else {
1201 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001202 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +00001203 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +00001204 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001205
1206 // C++11 [class.copy]p23:
1207 // A defaulted copy/move assignment operator for a class X is defined
1208 // as deleted if X has:
1209 // -- a non-static data member of const non-class type (or array
1210 // thereof)
1211 if (T.isConstQualified())
1212 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001213 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001214
David Majnemer5cfda6f2016-05-22 05:34:26 +00001215 // C++14 [meta.unary.prop]p4:
1216 // T is a class type [...] with [...] no non-static data members other
1217 // than bit-fields of length 0...
Douglas Gregor9d5938a2010-09-28 20:38:10 +00001218 if (data().Empty) {
Richard Smith866dee42018-04-02 18:29:43 +00001219 if (!Field->isZeroLengthBitField(Context))
Douglas Gregor9d5938a2010-09-28 20:38:10 +00001220 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +00001221 }
Douglas Gregora832d3e2010-09-28 19:45:33 +00001222 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001223
1224 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001225 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001226 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001227 == DeclarationName::CXXConversionFunctionName) {
1228 ASTContext &Ctx = getASTContext();
1229 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1230 }
1231 }
Richard Smith12e79312016-05-13 06:47:56 +00001232
Eugene Zelenko7855e772018-04-03 00:11:50 +00001233 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001234 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001235 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001236 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001237 // C++1z [dcl.init.aggr]p1:
1238 // An aggregate is [...] a class [...] with no inherited constructors
1239 data().Aggregate = false;
1240 }
Richard Smith12e79312016-05-13 06:47:56 +00001241
1242 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1243 data().HasInheritedAssignment = true;
1244 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001245}
1246
Richard Smith92f241f2012-12-08 02:53:02 +00001247void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1248 assert(!D->isImplicit() && !D->isUserProvided());
1249
1250 // The kind of special member this declaration is, if any.
1251 unsigned SMKind = 0;
1252
Eugene Zelenko7855e772018-04-03 00:11:50 +00001253 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001254 if (Constructor->isDefaultConstructor()) {
1255 SMKind |= SMF_DefaultConstructor;
1256 if (Constructor->isConstexpr())
1257 data().HasConstexprDefaultConstructor = true;
1258 }
1259 if (Constructor->isCopyConstructor())
1260 SMKind |= SMF_CopyConstructor;
1261 else if (Constructor->isMoveConstructor())
1262 SMKind |= SMF_MoveConstructor;
1263 else if (Constructor->isConstexpr())
1264 // We may now know that the constructor is constexpr.
1265 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001266 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001267 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001268 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1269 data().HasIrrelevantDestructor = false;
1270 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001271 SMKind |= SMF_CopyAssignment;
1272 else if (D->isMoveAssignmentOperator())
1273 SMKind |= SMF_MoveAssignment;
1274
1275 // Update which trivial / non-trivial special members we have.
1276 // addedMember will have skipped this step for this member.
1277 if (D->isTrivial())
1278 data().HasTrivialSpecialMembers |= SMKind;
1279 else
1280 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1281}
1282
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001283void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1284 unsigned SMKind = 0;
1285
Eugene Zelenko7855e772018-04-03 00:11:50 +00001286 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001287 if (Constructor->isCopyConstructor())
1288 SMKind = SMF_CopyConstructor;
1289 else if (Constructor->isMoveConstructor())
1290 SMKind = SMF_MoveConstructor;
1291 } else if (isa<CXXDestructorDecl>(D))
1292 SMKind = SMF_Destructor;
1293
1294 if (D->isTrivialForCall())
1295 data().HasTrivialSpecialMembersForCall |= SMKind;
1296 else
1297 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1298}
1299
Joao Matose9a3ed42012-08-31 22:18:20 +00001300bool CXXRecordDecl::isCLike() const {
1301 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1302 !TemplateOrInstantiation.isNull())
1303 return false;
1304 if (!hasDefinition())
1305 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001306
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001307 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001308}
Faisal Valic1a6dc42013-10-23 16:10:50 +00001309
Faisal Vali2b391ab2013-09-26 19:54:12 +00001310bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001311 if (!isLambda()) return false;
1312 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001313}
1314
1315CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001316 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001317 DeclarationName Name =
1318 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001319 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001320
1321 assert(!Calls.empty() && "Missing lambda call operator!");
1322 assert(Calls.size() == 1 && "More than one lambda call operator!");
1323
1324 NamedDecl *CallOp = Calls.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001325 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001326 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001327
1328 return cast<CXXMethodDecl>(CallOp);
1329}
1330
1331CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001332 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001333 DeclarationName Name =
1334 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001335 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001336 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001337 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1338 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001339 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001340 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1341
Faisal Vali571df122013-09-29 08:45:24 +00001342 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001343}
1344
Douglas Gregor9c702202012-02-10 07:45:31 +00001345void CXXRecordDecl::getCaptureFields(
1346 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001347 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001348 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001349 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001350
Douglas Gregorc8a73492012-02-13 15:44:47 +00001351 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001352 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001353 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001354 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001355 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001356 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001357 else if (C->capturesVariable())
1358 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001359 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001360 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001361}
1362
Faisal Vali2b391ab2013-09-26 19:54:12 +00001363TemplateParameterList *
1364CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001365 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001366 CXXMethodDecl *CallOp = getLambdaCallOperator();
1367 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1368 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001369 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001370}
Douglas Gregor9c702202012-02-10 07:45:31 +00001371
Richard Smith0bae6242016-08-25 00:34:00 +00001372Decl *CXXRecordDecl::getLambdaContextDecl() const {
1373 assert(isLambda() && "Not a lambda closure type!");
1374 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1375 return getLambdaData().ContextDecl.get(Source);
1376}
1377
John McCall1e3a1a72010-03-15 09:07:48 +00001378static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001379 QualType T =
1380 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1381 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001382 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001383}
1384
John McCall1e3a1a72010-03-15 09:07:48 +00001385/// Collect the visible conversions of a base class.
1386///
James Dennettb5d5f812012-06-15 22:28:09 +00001387/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001388/// \param InVirtual whether this base class is a virtual base (or a base
1389/// of a virtual base)
1390/// \param Access the access along the inheritance path to this base
1391/// \param ParentHiddenTypes the conversions provided by the inheritors
1392/// of this base
1393/// \param Output the set to which to add conversions from non-virtual bases
1394/// \param VOutput the set to which to add conversions from virtual bases
1395/// \param HiddenVBaseCs the set of conversions which were hidden in a
1396/// virtual base along some inheritance path
1397static void CollectVisibleConversions(ASTContext &Context,
1398 CXXRecordDecl *Record,
1399 bool InVirtual,
1400 AccessSpecifier Access,
1401 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001402 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001403 UnresolvedSetImpl &VOutput,
1404 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1405 // The set of types which have conversions in this class or its
1406 // subclasses. As an optimization, we don't copy the derived set
1407 // unless it might change.
1408 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1409 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1410
1411 // Collect the direct conversions and figure out which conversions
1412 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001413 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1414 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1415 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001416 HiddenTypesBuffer = ParentHiddenTypes;
1417 HiddenTypes = &HiddenTypesBuffer;
1418
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001419 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001420 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1421 bool Hidden = ParentHiddenTypes.count(ConvType);
1422 if (!Hidden)
1423 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001424
1425 // If this conversion is hidden and we're in a virtual base,
1426 // remember that it's hidden along some inheritance path.
1427 if (Hidden && InVirtual)
1428 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1429
1430 // If this conversion isn't hidden, add it to the appropriate output.
1431 else if (!Hidden) {
1432 AccessSpecifier IAccess
1433 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1434
1435 if (InVirtual)
1436 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001437 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001438 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001439 }
1440 }
1441 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001442
John McCall1e3a1a72010-03-15 09:07:48 +00001443 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001444 for (const auto &I : Record->bases()) {
1445 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001446 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001447
John McCall1e3a1a72010-03-15 09:07:48 +00001448 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001449 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1450 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001451
Eugene Zelenko7855e772018-04-03 00:11:50 +00001452 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001453 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1454 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001455 }
John McCall1e3a1a72010-03-15 09:07:48 +00001456}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001457
John McCall1e3a1a72010-03-15 09:07:48 +00001458/// Collect the visible conversions of a class.
1459///
1460/// This would be extremely straightforward if it weren't for virtual
1461/// bases. It might be worth special-casing that, really.
1462static void CollectVisibleConversions(ASTContext &Context,
1463 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001464 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001465 // The collection of all conversions in virtual bases that we've
1466 // found. These will be added to the output as long as they don't
1467 // appear in the hidden-conversions set.
1468 UnresolvedSet<8> VBaseCs;
1469
1470 // The set of conversions in virtual bases that we've determined to
1471 // be hidden.
1472 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1473
1474 // The set of types hidden by classes derived from this one.
1475 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1476
1477 // Go ahead and collect the direct conversions and add them to the
1478 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001479 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1480 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001481 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001482 for (; ConvI != ConvE; ++ConvI)
1483 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001484
1485 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001486 for (const auto &I : Record->bases()) {
1487 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001488 if (!RT) continue;
1489
1490 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001491 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001492 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1493 }
1494
1495 // Add any unhidden conversions provided by virtual bases.
1496 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1497 I != E; ++I) {
1498 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001499 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001500 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001501}
1502
1503/// getVisibleConversionFunctions - get all conversion functions visible
1504/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001505llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001506CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001507 ASTContext &Ctx = getASTContext();
1508
1509 ASTUnresolvedSet *Set;
1510 if (bases_begin() == bases_end()) {
1511 // If root class, all conversions are visible.
1512 Set = &data().Conversions.get(Ctx);
1513 } else {
1514 Set = &data().VisibleConversions.get(Ctx);
1515 // If visible conversion list is not evaluated, evaluate it.
1516 if (!data().ComputedVisibleConversions) {
1517 CollectVisibleConversions(Ctx, this, *Set);
1518 data().ComputedVisibleConversions = true;
1519 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001520 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001521 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001522}
1523
John McCallda4458e2010-03-31 01:36:47 +00001524void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1525 // This operation is O(N) but extremely rare. Sema only uses it to
1526 // remove UsingShadowDecls in a class that were followed by a direct
1527 // declaration, e.g.:
1528 // class A : B {
1529 // using B::operator int;
1530 // operator int();
1531 // };
1532 // This is uncommon by itself and even more uncommon in conjunction
1533 // with sufficiently large numbers of directly-declared conversions
1534 // that asymptotic behavior matters.
1535
Richard Smitha4ba74c2013-08-30 04:46:40 +00001536 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001537 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1538 if (Convs[I].getDecl() == ConvDecl) {
1539 Convs.erase(I);
1540 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1541 && "conversion was found multiple times in unresolved set");
1542 return;
1543 }
1544 }
1545
1546 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001547}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001548
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001549CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001550 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001551 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001552
1553 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001554}
1555
Chandler Carruth21c90602015-12-30 03:24:14 +00001556MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1557 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1558}
1559
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001560void
1561CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1562 TemplateSpecializationKind TSK) {
1563 assert(TemplateOrInstantiation.isNull() &&
1564 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001565 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001566 TemplateOrInstantiation
1567 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1568}
1569
Chandler Carruth21c90602015-12-30 03:24:14 +00001570ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1571 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1572}
1573
1574void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1575 TemplateOrInstantiation = Template;
1576}
1577
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001578TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001579 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001580 return Spec->getSpecializationKind();
1581
Douglas Gregor06db9f52009-10-12 20:18:28 +00001582 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001583 return MSInfo->getTemplateSpecializationKind();
1584
1585 return TSK_Undeclared;
1586}
1587
1588void
1589CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001590 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001591 Spec->setSpecializationKind(TSK);
1592 return;
1593 }
1594
Douglas Gregor06db9f52009-10-12 20:18:28 +00001595 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001596 MSInfo->setTemplateSpecializationKind(TSK);
1597 return;
1598 }
1599
David Blaikie83d382b2011-09-23 05:06:16 +00001600 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001601}
1602
Reid Klecknere7367d62014-10-14 20:28:40 +00001603const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001604 auto GetDefinitionOrSelf =
1605 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1606 if (auto *Def = D->getDefinition())
1607 return Def;
1608 return D;
1609 };
1610
Reid Klecknere7367d62014-10-14 20:28:40 +00001611 // If it's a class template specialization, find the template or partial
1612 // specialization from which it was instantiated.
1613 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1614 auto From = TD->getInstantiatedFrom();
1615 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1616 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1617 if (NewCTD->isMemberSpecialization())
1618 break;
1619 CTD = NewCTD;
1620 }
Richard Smith2195ec92017-04-21 01:15:13 +00001621 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001622 }
1623 if (auto *CTPSD =
1624 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1625 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1626 if (NewCTPSD->isMemberSpecialization())
1627 break;
1628 CTPSD = NewCTPSD;
1629 }
Richard Smith2195ec92017-04-21 01:15:13 +00001630 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001631 }
1632 }
1633
1634 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1635 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1636 const CXXRecordDecl *RD = this;
1637 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1638 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001639 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001640 }
1641 }
1642
1643 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1644 "couldn't find pattern for class template instantiation");
1645 return nullptr;
1646}
1647
Douglas Gregorbac74902010-07-01 14:13:13 +00001648CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1649 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001650 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001651
1652 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001653 = Context.DeclarationNames.getCXXDestructorName(
1654 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001655
Richard Smithcf4bdde2015-02-21 02:45:19 +00001656 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001657
Hubert Tong3cede512017-06-30 22:43:54 +00001658 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001659}
1660
Richard Trieu95a192a2015-05-28 00:14:02 +00001661bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1662 // Destructor is noreturn.
1663 if (const CXXDestructorDecl *Destructor = getDestructor())
1664 if (Destructor->isNoReturn())
1665 return true;
1666
1667 // Check base classes destructor for noreturn.
1668 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001669 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1670 if (RD->isAnyDestructorNoReturn())
1671 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001672
1673 // Check fields for noreturn.
1674 for (const auto *Field : fields())
1675 if (const CXXRecordDecl *RD =
1676 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1677 if (RD->isAnyDestructorNoReturn())
1678 return true;
1679
1680 // All destructors are not noreturn.
1681 return false;
1682}
1683
Erich Keane3e08f662017-09-29 21:06:00 +00001684static bool isDeclContextInNamespace(const DeclContext *DC) {
1685 while (!DC->isTranslationUnit()) {
1686 if (DC->isNamespace())
1687 return true;
1688 DC = DC->getParent();
1689 }
1690 return false;
1691}
1692
Erich Keane58bd6032017-09-15 16:03:35 +00001693bool CXXRecordDecl::isInterfaceLike() const {
1694 assert(hasDefinition() && "checking for interface-like without a definition");
1695 // All __interfaces are inheritently interface-like.
1696 if (isInterface())
1697 return true;
1698
1699 // Interface-like types cannot have a user declared constructor, destructor,
1700 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1701 // cannot be interface types.
1702 if (isLambda() || hasUserDeclaredConstructor() ||
1703 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1704 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1705 return false;
1706
1707 // No interface-like type can have a method with a definition.
1708 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001709 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001710 return false;
1711
1712 // Check "Special" types.
1713 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001714 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1715 // extern C++ block directly in the TU. These are only valid if in one
1716 // of these two situations.
1717 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1718 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001719 ((getName() == "IUnknown" &&
1720 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1721 (getName() == "IDispatch" &&
1722 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1723 if (getNumBases() > 0)
1724 return false;
1725 return true;
1726 }
1727
1728 // FIXME: Any access specifiers is supposed to make this no longer interface
1729 // like.
1730
1731 // If this isn't a 'special' type, it must have a single interface-like base.
1732 if (getNumBases() != 1)
1733 return false;
1734
1735 const auto BaseSpec = *bases_begin();
1736 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1737 return false;
1738 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1739 if (Base->isInterface() || !Base->isInterfaceLike())
1740 return false;
1741 return true;
1742}
1743
Douglas Gregorb11aad82011-02-19 18:51:44 +00001744void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001745 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001746}
1747
1748void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1749 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001750
Douglas Gregor8fb95122010-09-29 00:15:42 +00001751 // If the class may be abstract (but hasn't been marked as such), check for
1752 // any pure final overriders.
1753 if (mayBeAbstract()) {
1754 CXXFinalOverriderMap MyFinalOverriders;
1755 if (!FinalOverriders) {
1756 getFinalOverriders(MyFinalOverriders);
1757 FinalOverriders = &MyFinalOverriders;
1758 }
1759
1760 bool Done = false;
1761 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1762 MEnd = FinalOverriders->end();
1763 M != MEnd && !Done; ++M) {
1764 for (OverridingMethods::iterator SO = M->second.begin(),
1765 SOEnd = M->second.end();
1766 SO != SOEnd && !Done; ++SO) {
1767 assert(SO->second.size() > 0 &&
1768 "All virtual functions have overridding virtual functions");
1769
1770 // C++ [class.abstract]p4:
1771 // A class is abstract if it contains or inherits at least one
1772 // pure virtual function for which the final overrider is pure
1773 // virtual.
1774 if (SO->second.front().Method->isPure()) {
1775 data().Abstract = true;
1776 Done = true;
1777 break;
1778 }
1779 }
1780 }
1781 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001782
1783 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001784 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001785 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001786 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001787}
1788
1789bool CXXRecordDecl::mayBeAbstract() const {
1790 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1791 isDependentContext())
1792 return false;
1793
Aaron Ballman574705e2014-03-13 15:41:46 +00001794 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001795 const auto *BaseDecl =
1796 cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001797 if (BaseDecl->isAbstract())
1798 return true;
1799 }
1800
1801 return false;
1802}
1803
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001804void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001805
1806CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1807 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1808 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1809 SourceLocation EndLocation) {
1810 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1811 NameInfo, T, TInfo, EndLocation);
1812}
1813
1814CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1815 unsigned ID) {
1816 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1817 DeclarationNameInfo(), QualType(),
1818 nullptr, SourceLocation());
1819}
1820
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001821void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001822
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001823bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001824 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001825
1826 if (MD->getStorageClass() == SC_Static)
1827 return true;
1828
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001829 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1830 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001831}
1832
Rafael Espindola49e860b2012-06-26 17:45:31 +00001833static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1834 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001835 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001836 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1837 return true;
1838 if (recursivelyOverrides(MD, BaseMD))
1839 return true;
1840 }
1841 return false;
1842}
1843
1844CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001845CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1846 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001847 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1848 return this;
1849
1850 // Lookup doesn't work for destructors, so handle them separately.
1851 if (isa<CXXDestructorDecl>(this)) {
1852 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001853 if (MD) {
1854 if (recursivelyOverrides(MD, this))
1855 return MD;
1856 if (MayBeBase && recursivelyOverrides(this, MD))
1857 return MD;
1858 }
Craig Topper36250ad2014-05-12 05:36:57 +00001859 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001860 }
1861
Richard Smith40c78062015-02-21 02:31:57 +00001862 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001863 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001864 if (!MD)
1865 continue;
1866 if (recursivelyOverrides(MD, this))
1867 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001868 if (MayBeBase && recursivelyOverrides(this, MD))
1869 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001870 }
1871
Aaron Ballman574705e2014-03-13 15:41:46 +00001872 for (const auto &I : RD->bases()) {
1873 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001874 if (!RT)
1875 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00001876 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00001877 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1878 if (T)
1879 return T;
1880 }
1881
Craig Topper36250ad2014-05-12 05:36:57 +00001882 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001883}
1884
Ted Kremenek21475702008-09-05 17:16:31 +00001885CXXMethodDecl *
1886CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001887 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001888 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001889 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001890 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001891 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001892 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1893 T, TInfo, SC, isInline, isConstexpr,
1894 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001895}
1896
Douglas Gregor72172e92012-01-05 21:55:30 +00001897CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001898 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001899 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001900 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001901}
1902
Akira Hatanaka22461672017-07-13 06:08:27 +00001903CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
1904 bool IsAppleKext) {
1905 assert(isVirtual() && "this method is expected to be virtual");
1906
1907 // When building with -fapple-kext, all calls must go through the vtable since
1908 // the kernel linker can do runtime patching of vtables.
1909 if (IsAppleKext)
1910 return nullptr;
1911
1912 // If the member function is marked 'final', we know that it can't be
1913 // overridden and can therefore devirtualize it unless it's pure virtual.
1914 if (hasAttr<FinalAttr>())
1915 return isPure() ? nullptr : this;
1916
1917 // If Base is unknown, we cannot devirtualize.
1918 if (!Base)
1919 return nullptr;
1920
1921 // If the base expression (after skipping derived-to-base conversions) is a
1922 // class prvalue, then we can devirtualize.
1923 Base = Base->getBestDynamicClassTypeExpr();
1924 if (Base->isRValue() && Base->getType()->isRecordType())
1925 return this;
1926
1927 // If we don't even know what we would call, we can't devirtualize.
1928 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
1929 if (!BestDynamicDecl)
1930 return nullptr;
1931
1932 // There may be a method corresponding to MD in a derived class.
1933 CXXMethodDecl *DevirtualizedMethod =
1934 getCorrespondingMethodInClass(BestDynamicDecl);
1935
1936 // If that method is pure virtual, we can't devirtualize. If this code is
1937 // reached, the result would be UB, not a direct call to the derived class
1938 // function, and we can't assume the derived class function is defined.
1939 if (DevirtualizedMethod->isPure())
1940 return nullptr;
1941
1942 // If that method is marked final, we can devirtualize it.
1943 if (DevirtualizedMethod->hasAttr<FinalAttr>())
1944 return DevirtualizedMethod;
1945
1946 // Similarly, if the class itself is marked 'final' it can't be overridden
1947 // and we can therefore devirtualize the member function call.
1948 if (BestDynamicDecl->hasAttr<FinalAttr>())
1949 return DevirtualizedMethod;
1950
Eugene Zelenko7855e772018-04-03 00:11:50 +00001951 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
1952 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00001953 if (VD->getType()->isRecordType())
1954 // This is a record decl. We know the type and can devirtualize it.
1955 return DevirtualizedMethod;
1956
1957 return nullptr;
1958 }
1959
1960 // We can devirtualize calls on an object accessed by a class member access
1961 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
1962 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001963 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001964 const ValueDecl *VD = ME->getMemberDecl();
1965 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
1966 }
Akira Hatanaka22461672017-07-13 06:08:27 +00001967
1968 // Likewise for calls on an object accessed by a (non-reference) pointer to
1969 // member access.
1970 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
1971 if (BO->isPtrMemOp()) {
1972 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
1973 if (MPT->getPointeeType()->isRecordType())
1974 return DevirtualizedMethod;
1975 }
1976 }
1977
1978 // We can't devirtualize the call.
1979 return nullptr;
1980}
1981
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001982bool CXXMethodDecl::isUsualDeallocationFunction() const {
1983 if (getOverloadedOperator() != OO_Delete &&
1984 getOverloadedOperator() != OO_Array_Delete)
1985 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001986
1987 // C++ [basic.stc.dynamic.deallocation]p2:
1988 // A template instance is never a usual deallocation function,
1989 // regardless of its signature.
1990 if (getPrimaryTemplate())
1991 return false;
1992
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001993 // C++ [basic.stc.dynamic.deallocation]p2:
1994 // If a class T has a member deallocation function named operator delete
1995 // with exactly one parameter, then that function is a usual (non-placement)
1996 // deallocation function. [...]
1997 if (getNumParams() == 1)
1998 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00001999 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002000
Richard Smith5b349582017-10-13 01:55:36 +00002001 // C++ P0722:
2002 // A destroying operator delete is a usual deallocation function if
2003 // removing the std::destroying_delete_t parameter and changing the
2004 // first parameter type from T* to void* results in the signature of
2005 // a usual deallocation function.
2006 if (isDestroyingOperatorDelete())
2007 ++UsualParams;
2008
Richard Smithb2f0f052016-10-10 18:54:32 +00002009 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002010 // [...] If class T does not declare such an operator delete but does
2011 // declare a member deallocation function named operator delete with
2012 // exactly two parameters, the second of which has type std::size_t (18.1),
2013 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002014 //
2015 // C++17 says a usual deallocation function is one with the signature
2016 // (void* [, size_t] [, std::align_val_t] [, ...])
2017 // and all such functions are usual deallocation functions. It's not clear
2018 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002019 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002020 if (UsualParams < getNumParams() &&
2021 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2022 Context.getSizeType()))
2023 ++UsualParams;
2024
2025 if (UsualParams < getNumParams() &&
2026 getParamDecl(UsualParams)->getType()->isAlignValT())
2027 ++UsualParams;
2028
2029 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002030 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002031
2032 // In C++17 onwards, all potential usual deallocation functions are actual
2033 // usual deallocation functions.
2034 if (Context.getLangOpts().AlignedAllocation)
2035 return true;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002036
2037 // This function is a usual deallocation function if there are no
2038 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002039 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
2040 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00002041 I != E; ++I) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002042 if (const auto *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002043 if (FD->getNumParams() == 1)
2044 return false;
2045 }
2046
2047 return true;
2048}
2049
Douglas Gregorb139cd52010-05-01 20:49:11 +00002050bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002051 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00002052 // A user-declared copy assignment operator X::operator= is a non-static
2053 // non-template member function of class X with exactly one parameter of
2054 // type X, X&, const X&, volatile X& or const volatile X&.
2055 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
2056 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002057 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2058 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002059 return false;
2060
2061 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002062 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002063 ParamType = Ref->getPointeeType();
2064
2065 ASTContext &Context = getASTContext();
2066 QualType ClassType
2067 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2068 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2069}
2070
Alexis Huntfcaeae42011-05-25 20:50:04 +00002071bool CXXMethodDecl::isMoveAssignmentOperator() const {
2072 // C++0x [class.copy]p19:
2073 // A user-declared move assignment operator X::operator= is a non-static
2074 // non-template member function of class X with exactly one parameter of type
2075 // X&&, const X&&, volatile X&&, or const volatile X&&.
2076 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002077 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2078 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002079 return false;
2080
2081 QualType ParamType = getParamDecl(0)->getType();
2082 if (!isa<RValueReferenceType>(ParamType))
2083 return false;
2084 ParamType = ParamType->getPointeeType();
2085
2086 ASTContext &Context = getASTContext();
2087 QualType ClassType
2088 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2089 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2090}
2091
Anders Carlsson36d87e12009-05-16 23:58:37 +00002092void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002093 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002094 assert(!MD->getParent()->isDependentContext() &&
2095 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002096 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002097
Douglas Gregor832940b2010-03-02 23:58:15 +00002098 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002099}
2100
2101CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002102 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002103 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002104}
2105
2106CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002107 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002108 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002109}
2110
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002111unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002112 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002113 return getASTContext().overridden_methods_size(this);
2114}
2115
Clement Courbet6ecaec82016-07-05 07:49:31 +00002116CXXMethodDecl::overridden_method_range
2117CXXMethodDecl::overridden_methods() const {
2118 if (isa<CXXConstructorDecl>(this))
2119 return overridden_method_range(nullptr, nullptr);
2120 return getASTContext().overridden_methods(this);
2121}
2122
Ted Kremenek21475702008-09-05 17:16:31 +00002123QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002124 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2125 // If the member function is declared const, the type of this is const X*,
2126 // if the member function is declared volatile, the type of this is
2127 // volatile X*, and if the member function is declared const volatile,
2128 // the type of this is const volatile X*.
2129
Ted Kremenek21475702008-09-05 17:16:31 +00002130 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002131
John McCalle78aac42010-03-10 03:28:59 +00002132 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00002133 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00002134 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00002135 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00002136}
2137
Eli Friedman71a26d82009-12-06 20:50:05 +00002138bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00002139 // If this function is a template instantiation, look at the template from
2140 // which it was instantiated.
2141 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2142 if (!CheckFn)
2143 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002144
Eli Friedman71a26d82009-12-06 20:50:05 +00002145 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002146 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2147 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002148}
2149
Douglas Gregor355efbb2012-02-17 03:02:34 +00002150bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002151 const CXXRecordDecl *P = getParent();
2152 if (P->isLambda()) {
2153 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2154 if (StaticInvoker == this) return true;
2155 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2156 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2157 }
2158 }
2159 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002160}
2161
Alexis Hunt1d792652011-01-08 20:30:50 +00002162CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2163 TypeSourceInfo *TInfo, bool IsVirtual,
2164 SourceLocation L, Expr *Init,
2165 SourceLocation R,
2166 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002167 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2168 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2169 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002170
Alexis Hunt1d792652011-01-08 20:30:50 +00002171CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2172 FieldDecl *Member,
2173 SourceLocation MemberLoc,
2174 SourceLocation L, Expr *Init,
2175 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002176 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2177 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2178 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002179
Alexis Hunt1d792652011-01-08 20:30:50 +00002180CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2181 IndirectFieldDecl *Member,
2182 SourceLocation MemberLoc,
2183 SourceLocation L, Expr *Init,
2184 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002185 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2186 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2187 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002188
Alexis Hunt1d792652011-01-08 20:30:50 +00002189CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002190 TypeSourceInfo *TInfo,
2191 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002192 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002193 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2194 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002195
Alexis Hunt1d792652011-01-08 20:30:50 +00002196TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002197 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002198 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002199 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002200 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002201}
2202
Alexis Hunt1d792652011-01-08 20:30:50 +00002203const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002204 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002205 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002206 else
Craig Topper36250ad2014-05-12 05:36:57 +00002207 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002208}
2209
Alexis Hunt1d792652011-01-08 20:30:50 +00002210SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002211 if (isInClassMemberInitializer())
2212 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002213
David Blaikiea81d4102015-01-18 00:12:58 +00002214 if (isAnyMemberInitializer())
2215 return getMemberLocation();
2216
Eugene Zelenko7855e772018-04-03 00:11:50 +00002217 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002218 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
2219
Eugene Zelenko7855e772018-04-03 00:11:50 +00002220 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002221}
2222
Alexis Hunt1d792652011-01-08 20:30:50 +00002223SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002224 if (isInClassMemberInitializer()) {
2225 FieldDecl *D = getAnyMember();
2226 if (Expr *I = D->getInClassInitializer())
2227 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002228 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002229 }
2230
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002231 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002232}
2233
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002234void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002235
Richard Smith5179eb72016-06-28 19:03:57 +00002236CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2237 unsigned ID,
2238 bool Inherited) {
2239 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
2240 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2241 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2242 false, false, false, false, InheritedConstructor());
2243 Result->IsInheritingConstructor = Inherited;
2244 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002245}
2246
2247CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00002248CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002249 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002250 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002251 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002252 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00002253 bool isImplicitlyDeclared, bool isConstexpr,
2254 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002255 assert(NameInfo.getName().getNameKind()
2256 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002257 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002258 unsigned Extra =
2259 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
2260 return new (C, RD, Extra) CXXConstructorDecl(
2261 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
2262 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002263}
2264
Richard Smithc2bb8182015-03-24 06:36:48 +00002265CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2266 return CtorInitializers.get(getASTContext().getExternalSource());
2267}
2268
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002269CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2270 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2271 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002272 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002273 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002274
2275 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002276}
2277
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002278bool CXXConstructorDecl::isDefaultConstructor() const {
2279 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002280 // A default constructor for a class X is a constructor of class
2281 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002282 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002283 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002284}
2285
Mike Stump11289f42009-09-09 15:08:12 +00002286bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002287CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002288 return isCopyOrMoveConstructor(TypeQuals) &&
2289 getParamDecl(0)->getType()->isLValueReferenceType();
2290}
2291
2292bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2293 return isCopyOrMoveConstructor(TypeQuals) &&
2294 getParamDecl(0)->getType()->isRValueReferenceType();
2295}
2296
2297/// \brief Determine whether this is a copy or move constructor.
2298bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002299 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002300 // A non-template constructor for class X is a copy constructor
2301 // if its first parameter is of type X&, const X&, volatile X& or
2302 // const volatile X&, and either there are no other parameters
2303 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002304 // C++0x [class.copy]p3:
2305 // A non-template constructor for class X is a move constructor if its
2306 // first parameter is of type X&&, const X&&, volatile X&&, or
2307 // const volatile X&&, and either there are no other parameters or else
2308 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002309 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002310 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002311 (getPrimaryTemplate() != nullptr) ||
2312 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002313 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00002314
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002315 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00002316
2317 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002318 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002319 if (!ParamRefType)
2320 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00002321
Douglas Gregorff7028a2009-11-13 23:59:09 +00002322 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002323 ASTContext &Context = getASTContext();
2324
Douglas Gregorff7028a2009-11-13 23:59:09 +00002325 CanQualType PointeeType
2326 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002327 CanQualType ClassTy
2328 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002329 if (PointeeType.getUnqualifiedType() != ClassTy)
2330 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00002331
John McCall8ccfcb52009-09-24 19:53:00 +00002332 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00002333
2334 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002335 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00002336 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002337}
2338
Anders Carlssond20e7952009-08-28 16:57:08 +00002339bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002340 // C++ [class.conv.ctor]p1:
2341 // A constructor declared without the function-specifier explicit
2342 // that can be called with a single parameter specifies a
2343 // conversion from the type of its first parameter to the type of
2344 // its class. Such a constructor is called a converting
2345 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002346 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002347 return false;
2348
Mike Stump11289f42009-09-09 15:08:12 +00002349 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00002350 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002351 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002352 (getNumParams() > 1 &&
2353 (getParamDecl(1)->hasDefaultArg() ||
2354 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002355}
Douglas Gregor61956c42008-10-31 09:07:45 +00002356
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002357bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002358 if ((getNumParams() < 1) ||
2359 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002360 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002361 return false;
2362
2363 const ParmVarDecl *Param = getParamDecl(0);
2364
2365 ASTContext &Context = getASTContext();
2366 CanQualType ParamType = Context.getCanonicalType(Param->getType());
2367
Douglas Gregorffe14e32009-11-14 01:20:54 +00002368 // Is it the same as our our class type?
2369 CanQualType ClassTy
2370 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2371 if (ParamType.getUnqualifiedType() != ClassTy)
2372 return false;
2373
2374 return true;
2375}
2376
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002377void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002378
Douglas Gregor831c93f2008-11-05 20:51:48 +00002379CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002380CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002381 return new (C, ID)
2382 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2383 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00002384}
2385
2386CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002387CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002388 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002389 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002390 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002391 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002392 assert(NameInfo.getName().getNameKind()
2393 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002394 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002395 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002396 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002397}
2398
Richard Smith5b349582017-10-13 01:55:36 +00002399void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002400 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2401 if (OD && !First->OperatorDelete) {
2402 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002403 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002404 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002405 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002406 }
2407}
2408
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002409void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002410
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002411CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002412CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002413 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002414 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002415 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002416 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002417}
2418
2419CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002420CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002421 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002422 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002423 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002424 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002425 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002426 assert(NameInfo.getName().getNameKind()
2427 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002428 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002429 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002430 isInline, isExplicit, isConstexpr,
2431 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002432}
2433
Douglas Gregord3b672c2012-02-16 01:06:16 +00002434bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2435 return isImplicit() && getParent()->isLambda() &&
2436 getConversionType()->isBlockPointerType();
2437}
2438
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002439void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002440
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002441LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002442 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002443 SourceLocation ExternLoc,
2444 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002445 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002446 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002447 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002448}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002449
Richard Smithf7981722013-11-22 09:01:48 +00002450LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2451 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002452 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2453 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002454}
2455
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002456void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002457
Douglas Gregor889ceb72009-02-03 19:21:40 +00002458UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2459 SourceLocation L,
2460 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002461 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002462 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002463 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002464 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002465 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002466 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002467 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2468 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002469}
2470
Richard Smithf7981722013-11-22 09:01:48 +00002471UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2472 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002473 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2474 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002475 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002476 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002477}
2478
Sebastian Redla6602e92009-11-23 15:34:23 +00002479NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002480 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002481 return NA->getNamespace();
2482 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2483}
2484
Richard Smith053f6c62014-05-16 23:01:30 +00002485NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2486 SourceLocation StartLoc, SourceLocation IdLoc,
2487 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2488 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002489 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002490 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002491 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002492
Douglas Gregore57e7522012-01-07 09:11:48 +00002493 if (PrevDecl)
2494 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2495}
2496
Douglas Gregor72172e92012-01-05 21:55:30 +00002497NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002498 bool Inline, SourceLocation StartLoc,
2499 SourceLocation IdLoc, IdentifierInfo *Id,
2500 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002501 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2502 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002503}
2504
2505NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002506 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002507 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002508}
2509
Chandler Carruth21c90602015-12-30 03:24:14 +00002510NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2511 if (isFirstDecl())
2512 return this;
2513
2514 return AnonOrFirstNamespaceAndInline.getPointer();
2515}
2516
2517const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2518 if (isFirstDecl())
2519 return this;
2520
2521 return AnonOrFirstNamespaceAndInline.getPointer();
2522}
2523
2524bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2525
Richard Smithd7af8a32014-05-10 01:17:36 +00002526NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2527 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002528}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002529
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002530NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2531 return getPreviousDecl();
2532}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002533
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002534NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2535 return getMostRecentDecl();
2536}
2537
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002538void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002539
Richard Smithf4634362014-09-03 23:11:22 +00002540NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2541 return getNextRedeclaration();
2542}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002543
Richard Smithf4634362014-09-03 23:11:22 +00002544NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2545 return getPreviousDecl();
2546}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002547
Richard Smithf4634362014-09-03 23:11:22 +00002548NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2549 return getMostRecentDecl();
2550}
2551
Mike Stump11289f42009-09-09 15:08:12 +00002552NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002553 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002554 SourceLocation AliasLoc,
2555 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002556 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002557 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002558 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002559 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002560 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002561 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002562 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002563 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002564}
2565
Douglas Gregor72172e92012-01-05 21:55:30 +00002566NamespaceAliasDecl *
2567NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002568 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002569 SourceLocation(), nullptr,
2570 NestedNameSpecifierLoc(),
2571 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002572}
2573
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002574void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002575
Richard Smith5179eb72016-06-28 19:03:57 +00002576UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2577 SourceLocation Loc, UsingDecl *Using,
2578 NamedDecl *Target)
2579 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002580 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002581 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002582 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002583 setImplicit();
2584}
2585
2586UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2587 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002588 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002589
Douglas Gregor72172e92012-01-05 21:55:30 +00002590UsingShadowDecl *
2591UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002592 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002593}
2594
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002595UsingDecl *UsingShadowDecl::getUsingDecl() const {
2596 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002597 while (const auto *NextShadow =
2598 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002599 Shadow = NextShadow;
2600 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2601}
2602
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002603void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002604
2605ConstructorUsingShadowDecl *
2606ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2607 SourceLocation Loc, UsingDecl *Using,
2608 NamedDecl *Target, bool IsVirtual) {
2609 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2610 IsVirtual);
2611}
2612
2613ConstructorUsingShadowDecl *
2614ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2615 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2616}
2617
2618CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2619 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2620}
2621
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002622void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002623
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002624void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2625 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2626 "declaration already in set");
2627 assert(S->getUsingDecl() == this);
2628
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002629 if (FirstUsingShadow.getPointer())
2630 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2631 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002632}
2633
2634void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2635 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2636 "declaration not in set");
2637 assert(S->getUsingDecl() == this);
2638
2639 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2640
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002641 if (FirstUsingShadow.getPointer() == S) {
2642 FirstUsingShadow.setPointer(
2643 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002644 S->UsingOrNextShadow = this;
2645 return;
2646 }
2647
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002648 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002649 while (Prev->UsingOrNextShadow != S)
2650 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2651 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2652 S->UsingOrNextShadow = this;
2653}
2654
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002655UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2656 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002657 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002658 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002659 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002660}
2661
Douglas Gregor72172e92012-01-05 21:55:30 +00002662UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002663 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2664 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2665 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002666}
2667
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002668SourceRange UsingDecl::getSourceRange() const {
2669 SourceLocation Begin = isAccessDeclaration()
2670 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2671 return SourceRange(Begin, getNameInfo().getEndLoc());
2672}
2673
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002674void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002675
2676UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2677 NamedDecl *InstantiatedFrom,
2678 ArrayRef<NamedDecl *> UsingDecls) {
2679 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2680 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2681}
2682
2683UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2684 unsigned NumExpansions) {
2685 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2686 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2687 Result->NumExpansions = NumExpansions;
2688 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2689 for (unsigned I = 0; I != NumExpansions; ++I)
2690 new (Trail + I) NamedDecl*(nullptr);
2691 return Result;
2692}
2693
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002694void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002695
John McCalle61f2ba2009-11-18 02:36:19 +00002696UnresolvedUsingValueDecl *
2697UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2698 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002699 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002700 const DeclarationNameInfo &NameInfo,
2701 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002702 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002703 QualifierLoc, NameInfo,
2704 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002705}
2706
Douglas Gregor72172e92012-01-05 21:55:30 +00002707UnresolvedUsingValueDecl *
2708UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002709 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2710 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002711 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002712 DeclarationNameInfo(),
2713 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002714}
2715
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002716SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2717 SourceLocation Begin = isAccessDeclaration()
2718 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2719 return SourceRange(Begin, getNameInfo().getEndLoc());
2720}
2721
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002722void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002723
John McCalle61f2ba2009-11-18 02:36:19 +00002724UnresolvedUsingTypenameDecl *
2725UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2726 SourceLocation UsingLoc,
2727 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002728 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002729 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002730 DeclarationName TargetName,
2731 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002732 return new (C, DC) UnresolvedUsingTypenameDecl(
2733 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002734 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002735}
2736
Douglas Gregor72172e92012-01-05 21:55:30 +00002737UnresolvedUsingTypenameDecl *
2738UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002739 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002740 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002741 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002742}
2743
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002744void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002745
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002746StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002747 SourceLocation StaticAssertLoc,
2748 Expr *AssertExpr,
2749 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002750 SourceLocation RParenLoc,
2751 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002752 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2753 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002754}
2755
Richard Smithf7981722013-11-22 09:01:48 +00002756StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002757 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002758 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2759 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002760}
2761
Richard Smithbdb84f32016-07-22 23:36:59 +00002762void BindingDecl::anchor() {}
2763
2764BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2765 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002766 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002767}
2768
2769BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002770 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002771}
2772
Richard Smith97fcf4b2016-08-14 23:15:52 +00002773VarDecl *BindingDecl::getHoldingVar() const {
2774 Expr *B = getBinding();
2775 if (!B)
2776 return nullptr;
2777 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2778 if (!DRE)
2779 return nullptr;
2780
2781 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2782 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2783 return VD;
2784}
2785
Richard Smithbdb84f32016-07-22 23:36:59 +00002786void DecompositionDecl::anchor() {}
2787
2788DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2789 SourceLocation StartLoc,
2790 SourceLocation LSquareLoc,
2791 QualType T, TypeSourceInfo *TInfo,
2792 StorageClass SC,
2793 ArrayRef<BindingDecl *> Bindings) {
2794 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2795 return new (C, DC, Extra)
2796 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2797}
2798
2799DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2800 unsigned ID,
2801 unsigned NumBindings) {
2802 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002803 auto *Result = new (C, ID, Extra)
2804 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2805 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002806 // Set up and clean out the bindings array.
2807 Result->NumBindings = NumBindings;
2808 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2809 for (unsigned I = 0; I != NumBindings; ++I)
2810 new (Trail + I) BindingDecl*(nullptr);
2811 return Result;
2812}
2813
Richard Smith7873de02016-08-11 22:25:46 +00002814void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2815 os << '[';
2816 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002817 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00002818 if (Comma)
2819 os << ", ";
2820 B->printName(os);
2821 Comma = true;
2822 }
2823 os << ']';
2824}
2825
Richard Smithf7981722013-11-22 09:01:48 +00002826MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2827 SourceLocation L, DeclarationName N,
2828 QualType T, TypeSourceInfo *TInfo,
2829 SourceLocation StartL,
2830 IdentifierInfo *Getter,
2831 IdentifierInfo *Setter) {
2832 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2833}
2834
2835MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2836 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002837 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2838 DeclarationName(), QualType(), nullptr,
2839 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002840}
2841
Anders Carlsson6750d162009-03-26 23:46:50 +00002842static const char *getAccessName(AccessSpecifier AS) {
2843 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002844 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002845 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002846 case AS_public:
2847 return "public";
2848 case AS_private:
2849 return "private";
2850 case AS_protected:
2851 return "protected";
2852 }
David Blaikief47fa302012-01-17 02:30:50 +00002853 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002854}
2855
2856const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2857 AccessSpecifier AS) {
2858 return DB << getAccessName(AS);
2859}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002860
2861const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2862 AccessSpecifier AS) {
2863 return DB << getAccessName(AS);
2864}