blob: 4bd5ee1bb62ef98a1ebb2cd3130755aadb7b9fb9 [file] [log] [blame]
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001//===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
Ted Kremenek21475702008-09-05 17:16:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek21475702008-09-05 17:16:31 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the C++ related Decl classes.
10//
11//===----------------------------------------------------------------------===//
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000012
Ted Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000017#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000018#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000019#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000021#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000022#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000023#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000024#include "clang/AST/LambdaCapture.h"
25#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000026#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000027#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000028#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000029#include "clang/AST/UnresolvedSet.h"
30#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000031#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000032#include "clang/Basic/LLVM.h"
33#include "clang/Basic/LangOptions.h"
34#include "clang/Basic/OperatorKinds.h"
35#include "clang/Basic/PartialDiagnostic.h"
36#include "clang/Basic/SourceLocation.h"
37#include "clang/Basic/Specifiers.h"
38#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000040#include "llvm/ADT/SmallVector.h"
41#include "llvm/ADT/iterator_range.h"
42#include "llvm/Support/Casting.h"
43#include "llvm/Support/ErrorHandling.h"
44#include "llvm/Support/raw_ostream.h"
45#include <algorithm>
46#include <cassert>
47#include <cstddef>
48#include <cstdint>
49
Ted Kremenek21475702008-09-05 17:16:31 +000050using namespace clang;
51
52//===----------------------------------------------------------------------===//
53// Decl Allocation/Deallocation Method Implementations
54//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000055
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000056void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000057
Douglas Gregor72172e92012-01-05 21:55:30 +000058AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000059 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000060}
61
Richard Smitha4ba74c2013-08-30 04:46:40 +000062void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
63 ExternalASTSource *Source = C.getExternalSource();
64 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
65 assert(Source && "getFromExternalSource with no external source");
66
67 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
68 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
69 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
70 Impl.Decls.setLazy(false);
71}
72
John McCall67da35c2010-02-04 22:26:26 +000073CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000074 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
75 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000076 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
77 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000078 HasPrivateFields(false), HasProtectedFields(false),
79 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
80 HasOnlyCMembers(true), HasInClassInitializer(false),
81 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000082 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000083 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000084 NeedOverloadResolutionForMoveConstructor(false),
85 NeedOverloadResolutionForMoveAssignment(false),
86 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000087 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000088 DefaultedMoveConstructorIsDeleted(false),
89 DefaultedMoveAssignmentIsDeleted(false),
90 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000091 HasTrivialSpecialMembersForCall(SMF_All),
92 DeclaredNonTrivialSpecialMembers(0),
93 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000094 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000095 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000096 DefaultedDefaultConstructorIsConstexpr(true),
97 HasConstexprDefaultConstructor(false),
98 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
99 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +0000100 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
101 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +0000102 ImplicitCopyAssignmentHasConstParam(true),
103 HasDeclaredCopyConstructorWithConstParam(false),
104 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000105 IsParsingBaseSpecifiers(false), HasODRHash(false), Definition(D) {}
John McCall67da35c2010-02-04 22:26:26 +0000106
Benjamin Kramer300c0632012-07-04 17:03:33 +0000107CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
108 return Bases.get(Definition->getASTContext().getExternalSource());
109}
110
111CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
112 return VBases.get(Definition->getASTContext().getExternalSource());
113}
114
Richard Smith053f6c62014-05-16 23:01:30 +0000115CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
116 DeclContext *DC, SourceLocation StartLoc,
117 SourceLocation IdLoc, IdentifierInfo *Id,
118 CXXRecordDecl *PrevDecl)
119 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
120 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000121 : nullptr) {}
Douglas Gregorb6acda02008-11-12 23:21:09 +0000122
Jay Foad39c79802011-01-12 09:06:06 +0000123CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000124 DeclContext *DC, SourceLocation StartLoc,
125 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smithbecb92d2017-10-10 22:33:17 +0000126 CXXRecordDecl *PrevDecl,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000127 bool DelayTypeCreation) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000128 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
129 PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000130 R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
Mike Stump11289f42009-09-09 15:08:12 +0000131
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000132 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000133 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000134 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000135 return R;
136}
137
Richard Smith053f6c62014-05-16 23:01:30 +0000138CXXRecordDecl *
139CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
140 TypeSourceInfo *Info, SourceLocation Loc,
141 bool Dependent, bool IsGeneric,
142 LambdaCaptureDefault CaptureDefault) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000143 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
144 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000145 R->setBeingDefined(true);
Richard Smith64c06302014-05-22 23:19:02 +0000146 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000147 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000148 CaptureDefault);
Erich Keanef92f31c2018-08-01 20:48:16 +0000149 R->setMayHaveOutOfDateDef(false);
James Dennett8f60cdd2013-09-05 17:46:21 +0000150 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000151 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000152 return R;
153}
154
Douglas Gregor72172e92012-01-05 21:55:30 +0000155CXXRecordDecl *
156CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000157 auto *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000158 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000159 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000160 R->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000161 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000162}
163
Richard Smithb6070db2018-04-05 18:55:37 +0000164/// Determine whether a class has a repeated base class. This is intended for
165/// use when determining if a class is standard-layout, so makes no attempt to
166/// handle virtual bases.
167static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
168 llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
169 SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
170 while (!WorkList.empty()) {
171 const CXXRecordDecl *RD = WorkList.pop_back_val();
172 for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
173 if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
174 if (!SeenBaseTypes.insert(B).second)
175 return true;
176 WorkList.push_back(B);
177 }
178 }
179 }
180 return false;
181}
182
Mike Stump11289f42009-09-09 15:08:12 +0000183void
Craig Toppere6337e12015-12-25 00:36:02 +0000184CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
185 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000186 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000187
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000188 if (!data().Bases.isOffset() && data().NumBases > 0)
189 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000190
Craig Toppere6337e12015-12-25 00:36:02 +0000191 if (NumBases) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000192 if (!C.getLangOpts().CPlusPlus17) {
Richard Smith872307e2016-03-08 22:17:41 +0000193 // C++ [dcl.init.aggr]p1:
194 // An aggregate is [...] a class with [...] no base classes [...].
195 data().Aggregate = false;
196 }
Richard Smithaed32b42011-10-18 20:08:55 +0000197
198 // C++ [class]p4:
199 // A POD-struct is an aggregate class...
200 data().PlainOldData = false;
201 }
202
Anders Carlssonabb20e62010-03-29 05:13:12 +0000203 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000204 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Fangrui Song6907ce22018-07-30 19:24:48 +0000205
Anders Carlssonabb20e62010-03-29 05:13:12 +0000206 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000207 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Craig Toppere6337e12015-12-25 00:36:02 +0000209 data().Bases = new(C) CXXBaseSpecifier [NumBases];
210 data().NumBases = NumBases;
211 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000212 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000213 // Keep track of inherited vbases for this base class.
214 const CXXBaseSpecifier *Base = Bases[i];
215 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000216 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000217 if (BaseType->isDependentType())
218 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000219 auto *BaseClassDecl =
220 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000221
Richard Smithb6070db2018-04-05 18:55:37 +0000222 // C++2a [class]p7:
223 // A standard-layout class is a class that:
224 // [...]
225 // -- has all non-static data members and bit-fields in the class and
226 // its base classes first declared in the same class
227 if (BaseClassDecl->data().HasBasesWithFields ||
228 !BaseClassDecl->field_empty()) {
229 if (data().HasBasesWithFields)
230 // Two bases have members or bit-fields: not standard-layout.
Chandler Carruth583edf82011-04-30 10:07:30 +0000231 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000232 data().HasBasesWithFields = true;
233 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000234
Richard Smithb6070db2018-04-05 18:55:37 +0000235 // C++11 [class]p7:
236 // A standard-layout class is a class that:
237 // -- [...] has [...] at most one base class with non-static data
238 // members
239 if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
240 BaseClassDecl->hasDirectFields()) {
241 if (data().HasBasesWithNonStaticDataMembers)
242 data().IsCXX11StandardLayout = false;
243 data().HasBasesWithNonStaticDataMembers = true;
244 }
245
246 if (!BaseClassDecl->isEmpty()) {
David Majnemer5cfda6f2016-05-22 05:34:26 +0000247 // C++14 [meta.unary.prop]p4:
248 // T is a class type [...] with [...] no base class B for which
249 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000250 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000251 }
Richard Smithb6070db2018-04-05 18:55:37 +0000252
Richard Smith872307e2016-03-08 22:17:41 +0000253 // C++1z [dcl.init.agg]p1:
254 // An aggregate is a class with [...] no private or protected base classes
255 if (Base->getAccessSpecifier() != AS_public)
256 data().Aggregate = false;
257
Douglas Gregor11c024b2010-09-28 20:50:54 +0000258 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000259 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000260 // polymorphic class.
Richard Smithc00e4462018-06-14 20:03:22 +0000261 if (BaseClassDecl->isPolymorphic()) {
Douglas Gregor11c024b2010-09-28 20:50:54 +0000262 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000263
Richard Smithc00e4462018-06-14 20:03:22 +0000264 // An aggregate is a class with [...] no virtual functions.
265 data().Aggregate = false;
266 }
267
Chandler Carruthb1963742011-04-30 09:17:45 +0000268 // C++0x [class]p7:
269 // A standard-layout class is a class that: [...]
270 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000271 if (!BaseClassDecl->isStandardLayout())
272 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000273 if (!BaseClassDecl->isCXX11StandardLayout())
274 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000275
Chandler Carruthe71d0622011-04-24 02:49:34 +0000276 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000277 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000278 data().HasNonLiteralTypeFieldsOrBases = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000279
Anders Carlssonabb20e62010-03-29 05:13:12 +0000280 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000281 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000282 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000283 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000284 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000285
286 // C++11 [class.copy]p8:
287 // The implicitly-declared copy constructor for a class X will have
288 // the form 'X::X(const X&)' if each [...] virtual base class B of X
289 // has a copy constructor whose first parameter is of type
290 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000291 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000292 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000293 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000294
295 // C++1z [dcl.init.agg]p1:
296 // An aggregate is a class with [...] no virtual base classes
297 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000298 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000299 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000300
301 if (Base->isVirtual()) {
302 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000303 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000304 VBases.push_back(Base);
305
David Majnemer5cfda6f2016-05-22 05:34:26 +0000306 // C++14 [meta.unary.prop] is_empty:
307 // T is a class type, but not a union type, with ... no virtual base
308 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000309 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000310
Richard Smith872307e2016-03-08 22:17:41 +0000311 // C++1z [dcl.init.agg]p1:
312 // An aggregate is a class with [...] no virtual base classes
313 data().Aggregate = false;
314
Richard Smith328aae52012-11-30 05:11:39 +0000315 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
316 // A [default constructor, copy/move constructor, or copy/move assignment
317 // operator for a class X] is trivial [...] if:
318 // -- class X has [...] no virtual base classes
319 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000320 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000321
322 // C++0x [class]p7:
323 // A standard-layout class is a class that: [...]
324 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000325 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000326 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000327
328 // C++11 [dcl.constexpr]p4:
329 // In the definition of a constexpr constructor [...]
330 // -- the class shall not have any virtual base classes
331 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000332
333 // C++1z [class.copy]p8:
334 // The implicitly-declared copy constructor for a class X will have
335 // the form 'X::X(const X&)' if each potentially constructed subobject
336 // has a copy constructor whose first parameter is of type
337 // 'const B&' or 'const volatile B&' [...]
338 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
339 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000340 } else {
341 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000342 // A default constructor is trivial [...] if:
343 // -- all the direct base classes of its class have trivial default
344 // constructors.
345 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000346 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
347
Chandler Carruthad7d4042011-04-23 23:10:33 +0000348 // C++0x [class.copy]p13:
349 // A copy/move constructor for class X is trivial if [...]
350 // [...]
351 // -- the constructor selected to copy/move each direct base class
352 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000353 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000354 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000355
356 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
357 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
358
Richard Smith6b02d462012-12-08 08:32:28 +0000359 // If the base class doesn't have a simple move constructor, we'll eagerly
360 // declare it and perform overload resolution to determine which function
361 // it actually calls. If it does have a simple move constructor, this
362 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000363 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000364 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000365
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000366 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
367 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
368
Chandler Carruthad7d4042011-04-23 23:10:33 +0000369 // C++0x [class.copy]p27:
370 // A copy/move assignment operator for class X is trivial if [...]
371 // [...]
372 // -- the assignment operator selected to copy/move each direct base
373 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000374 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000375 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000376 // If the base class doesn't have a simple move assignment, we'll eagerly
377 // declare it and perform overload resolution to determine which function
378 // it actually calls. If it does have a simple move assignment, this
379 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000380 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000381 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000382
383 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000384 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000385 // requirements of a constexpr constructor, the implicitly-defined
386 // default constructor is constexpr.
387 if (!BaseClassDecl->hasConstexprDefaultConstructor())
388 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000389
390 // C++1z [class.copy]p8:
391 // The implicitly-declared copy constructor for a class X will have
392 // the form 'X::X(const X&)' if each potentially constructed subobject
393 // has a copy constructor whose first parameter is of type
394 // 'const B&' or 'const volatile B&' [...]
395 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
396 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000397 }
Richard Smith92f241f2012-12-08 02:53:02 +0000398
Douglas Gregor11c024b2010-09-28 20:50:54 +0000399 // C++ [class.ctor]p3:
400 // A destructor is trivial if all the direct base classes of its class
401 // have trivial destructors.
402 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000403 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000404
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000405 if (!BaseClassDecl->hasTrivialDestructorForCall())
406 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
407
Richard Smith561fb152012-02-25 07:33:38 +0000408 if (!BaseClassDecl->hasIrrelevantDestructor())
409 data().HasIrrelevantDestructor = false;
410
Richard Smith1c33fe82012-11-28 06:23:12 +0000411 // C++11 [class.copy]p18:
412 // The implicitly-declared copy assignment oeprator for a class X will
413 // have the form 'X& X::operator=(const X&)' if each direct base class B
414 // of X has a copy assignment operator whose parameter is of type 'const
415 // B&', 'const volatile B&', or 'B' [...]
416 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
417 data().ImplicitCopyAssignmentHasConstParam = false;
418
John McCall31168b02011-06-15 23:02:42 +0000419 // A class has an Objective-C object member if... or any of its bases
420 // has an Objective-C object member.
421 if (BaseClassDecl->hasObjectMember())
422 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000423
Fariborz Jahanian78652202013-01-25 23:57:05 +0000424 if (BaseClassDecl->hasVolatileMember())
425 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000426
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000427 if (BaseClassDecl->getArgPassingRestrictions() ==
428 RecordDecl::APK_CanNeverPassInRegs)
429 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
430
Douglas Gregor61226d32011-05-13 01:05:07 +0000431 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000432 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000433 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000434 data().NeedOverloadResolutionForCopyConstructor = true;
435 }
Richard Smith593f9932012-12-08 02:01:17 +0000436
437 if (BaseClassDecl->hasUninitializedReferenceMember())
438 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000439
Nico Weber6a6376b2016-02-19 01:52:46 +0000440 if (!BaseClassDecl->allowConstDefaultInit())
441 data().HasUninitializedFields = true;
442
Richard Smith6b02d462012-12-08 08:32:28 +0000443 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000444 }
Richard Smithb6070db2018-04-05 18:55:37 +0000445
446 // C++2a [class]p7:
447 // A class S is a standard-layout class if it:
448 // -- has at most one base class subobject of any given type
449 //
450 // Note that we only need to check this for classes with more than one base
451 // class. If there's only one base class, and it's standard layout, then
452 // we know there are no repeated base classes.
453 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
454 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000455
David Majnemer5ef4fe72014-06-13 06:43:46 +0000456 if (VBases.empty()) {
457 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000458 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000459 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000460
461 // Create base specifier for any direct or indirect virtual bases.
462 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
463 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000464 for (int I = 0, E = VBases.size(); I != E; ++I) {
465 QualType Type = VBases[I]->getType();
466 if (!Type->isDependentType())
467 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000468 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000469 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000470
471 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000472}
473
Richard Trieufd1acbb2017-04-11 21:31:00 +0000474unsigned CXXRecordDecl::getODRHash() const {
475 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000476
Richard Trieufd1acbb2017-04-11 21:31:00 +0000477 // Previously calculated hash is stored in DefinitionData.
478 if (DefinitionData->HasODRHash)
479 return DefinitionData->ODRHash;
480
481 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000482 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000483 Hash.AddCXXRecordDecl(getDefinition());
484 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000485 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000486
487 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000488}
Richard Trieub6adf542017-02-18 02:09:28 +0000489
Richard Smith6b02d462012-12-08 08:32:28 +0000490void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
491 // C++11 [class.copy]p11:
492 // A defaulted copy/move constructor for a class X is defined as
493 // deleted if X has:
494 // -- a direct or virtual base class B that cannot be copied/moved [...]
495 // -- a non-static data member of class type M (or array thereof)
496 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000497 if (!Subobj->hasSimpleCopyConstructor())
498 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000499 if (!Subobj->hasSimpleMoveConstructor())
500 data().NeedOverloadResolutionForMoveConstructor = true;
501
502 // C++11 [class.copy]p23:
503 // A defaulted copy/move assignment operator for a class X is defined as
504 // deleted if X has:
505 // -- a direct or virtual base class B that cannot be copied/moved [...]
506 // -- a non-static data member of class type M (or array thereof)
507 // that cannot be copied or moved [...]
508 if (!Subobj->hasSimpleMoveAssignment())
509 data().NeedOverloadResolutionForMoveAssignment = true;
510
511 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
512 // A defaulted [ctor or dtor] for a class X is defined as
513 // deleted if X has:
514 // -- any direct or virtual base class [...] has a type with a destructor
515 // that is deleted or inaccessible from the defaulted [ctor or dtor].
516 // -- any non-static data member has a type with a destructor
517 // that is deleted or inaccessible from the defaulted [ctor or dtor].
518 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000519 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000520 data().NeedOverloadResolutionForMoveConstructor = true;
521 data().NeedOverloadResolutionForDestructor = true;
522 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000523}
524
Douglas Gregord2e6a452010-01-14 17:47:39 +0000525bool CXXRecordDecl::hasAnyDependentBases() const {
526 if (!isDependentContext())
527 return false;
528
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000529 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000530}
531
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000532bool CXXRecordDecl::isTriviallyCopyable() const {
533 // C++0x [class]p5:
534 // A trivially copyable class is a class that:
535 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000536 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000537 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000538 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000539 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000540 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000541 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000542 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000543 // -- has a trivial destructor.
544 if (!hasTrivialDestructor()) return false;
545
546 return true;
547}
548
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000549void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000550 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000551 // A class is abstract if it has at least one pure virtual function.
552 data().Abstract = true;
553}
554
Richard Smithb6070db2018-04-05 18:55:37 +0000555bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
556 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
557 if (!getNumBases())
558 return false;
559
560 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
561 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
562 SmallVector<const CXXRecordDecl*, 8> WorkList;
563
564 // Visit a type that we have determined is an element of M(S).
565 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
566 RD = RD->getCanonicalDecl();
567
568 // C++2a [class]p8:
569 // A class S is a standard-layout class if it [...] has no element of the
570 // set M(S) of types as a base class.
571 //
572 // If we find a subobject of an empty type, it might also be a base class,
573 // so we'll need to walk the base classes to check.
574 if (!RD->data().HasBasesWithFields) {
575 // Walk the bases the first time, stopping if we find the type. Build a
576 // set of them so we don't need to walk them again.
577 if (Bases.empty()) {
578 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
579 Base = Base->getCanonicalDecl();
580 if (RD == Base)
581 return false;
582 Bases.insert(Base);
583 return true;
584 });
585 if (RDIsBase)
586 return true;
587 } else {
588 if (Bases.count(RD))
589 return true;
590 }
591 }
592
593 if (M.insert(RD).second)
594 WorkList.push_back(RD);
595 return false;
596 };
597
598 if (Visit(XFirst))
599 return true;
600
601 while (!WorkList.empty()) {
602 const CXXRecordDecl *X = WorkList.pop_back_val();
603
604 // FIXME: We don't check the bases of X. That matches the standard, but
605 // that sure looks like a wording bug.
606
607 // -- If X is a non-union class type with a non-static data member
608 // [recurse to] the first non-static data member of X
609 // -- If X is a union type, [recurse to union members]
610 for (auto *FD : X->fields()) {
611 // FIXME: Should we really care about the type of the first non-static
612 // data member of a non-union if there are preceding unnamed bit-fields?
613 if (FD->isUnnamedBitfield())
614 continue;
615
616 // -- If X is n array type, [visit the element type]
617 QualType T = Ctx.getBaseElementType(FD->getType());
618 if (auto *RD = T->getAsCXXRecordDecl())
619 if (Visit(RD))
620 return true;
621
622 if (!X->isUnion())
623 break;
624 }
625 }
626
627 return false;
628}
629
Richard Smith864949b2018-09-27 22:47:04 +0000630bool CXXRecordDecl::lambdaIsDefaultConstructibleAndAssignable() const {
631 assert(isLambda() && "not a lambda");
632
633 // C++2a [expr.prim.lambda.capture]p11:
634 // The closure type associated with a lambda-expression has no default
635 // constructor if the lambda-expression has a lambda-capture and a
636 // defaulted default constructor otherwise. It has a deleted copy
637 // assignment operator if the lambda-expression has a lambda-capture and
638 // defaulted copy and move assignment operators otherwise.
639 //
640 // C++17 [expr.prim.lambda]p21:
641 // The closure type associated with a lambda-expression has no default
642 // constructor and a deleted copy assignment operator.
643 if (getLambdaCaptureDefault() != LCD_None)
644 return false;
645 return getASTContext().getLangOpts().CPlusPlus2a;
646}
647
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000648void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000649 if (!D->isImplicit() &&
650 !isa<FieldDecl>(D) &&
651 !isa<IndirectFieldDecl>(D) &&
652 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
653 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
654 data().HasOnlyCMembers = false;
655
656 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000657 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000658 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000659
Eugene Zelenko7855e772018-04-03 00:11:50 +0000660 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000661 if (FunTmpl)
662 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000663
664 // FIXME: Pass NamedDecl* to addedMember?
665 Decl *DUnderlying = D;
666 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
667 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000668 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000669 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
670 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000671
Eugene Zelenko7855e772018-04-03 00:11:50 +0000672 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000673 if (Method->isVirtual()) {
674 // C++ [dcl.init.aggr]p1:
675 // An aggregate is an array or a class with [...] no virtual functions.
676 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000677
Douglas Gregora832d3e2010-09-28 19:45:33 +0000678 // C++ [class]p4:
679 // A POD-struct is an aggregate class...
680 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000681
David Majnemer5cfda6f2016-05-22 05:34:26 +0000682 // C++14 [meta.unary.prop]p4:
683 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000684 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000685
686 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000687 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000688 // polymorphic class.
689 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000690
Richard Smith328aae52012-11-30 05:11:39 +0000691 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
692 // A [default constructor, copy/move constructor, or copy/move
693 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000694 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000695 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000696 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000697
Chandler Carruthb1963742011-04-30 09:17:45 +0000698 // C++0x [class]p7:
699 // A standard-layout class is a class that: [...]
700 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000701 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000702 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000703 }
704 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000705
Richard Smith1c33fe82012-11-28 06:23:12 +0000706 // Notify the listener if an implicit member was added after the definition
707 // was completed.
708 if (!isBeingDefined() && D->isImplicit())
709 if (ASTMutationListener *L = getASTMutationListener())
710 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000711
Richard Smith328aae52012-11-30 05:11:39 +0000712 // The kind of special member this declaration is, if any.
713 unsigned SMKind = 0;
714
Richard Smith1c33fe82012-11-28 06:23:12 +0000715 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000716 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000717 if (!Constructor->isImplicit()) {
718 // Note that we have a user-declared constructor.
719 data().UserDeclaredConstructor = true;
720
721 // C++ [class]p4:
722 // A POD-struct is an aggregate class [...]
723 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
724 // type is technically an aggregate in C++0x since it wouldn't be in 03.
725 data().PlainOldData = false;
726 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000727
Alexis Hunt88c75c32011-05-09 21:45:35 +0000728 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000729 SMKind |= SMF_DefaultConstructor;
730
Richard Smith5179eb72016-06-28 19:03:57 +0000731 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000732 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000733 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000734 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000735 if (Constructor->isDefaulted())
736 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000737 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000738
Chandler Carruthad7d4042011-04-23 23:10:33 +0000739 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000740 unsigned Quals;
741 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000742 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000743
744 if (Quals & Qualifiers::Const)
745 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000746 } else if (Constructor->isMoveConstructor())
747 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000748 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000749
Eric Fiselier283d8d42016-12-03 01:26:47 +0000750 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith79c88c32018-09-26 19:00:16 +0000751 // An aggregate is an array or a class with no user-provided [or]
752 // explicit [...] constructors
753 // C++20 [dcl.init.aggr]p1:
754 // An aggregate is an array or a class with no user-declared [...]
755 // constructors
756 if (getASTContext().getLangOpts().CPlusPlus2a
757 ? !Constructor->isImplicit()
758 : (Constructor->isUserProvided() || Constructor->isExplicit()))
Eric Fiselier283d8d42016-12-03 01:26:47 +0000759 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000760 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000761
Richard Smith5179eb72016-06-28 19:03:57 +0000762 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000763 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000764 // Record if we see any constexpr constructors which are neither copy
765 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000766 // C++1z [basic.types]p10:
767 // [...] has at least one constexpr constructor or constructor template
768 // (possibly inherited from a base class) that is not a copy or move
769 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000770 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000771 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000772 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000773
Richard Smith1c33fe82012-11-28 06:23:12 +0000774 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000775 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000776 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000777
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000778 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000779 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000780 // If the destructor is explicitly defaulted and not trivial or not public
781 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
782 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000783
Richard Smith1c33fe82012-11-28 06:23:12 +0000784 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000785 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000786 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000787 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000788 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
789 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000790 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000791
792 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000793 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000794 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000795 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000796
Eugene Zelenko7855e772018-04-03 00:11:50 +0000797 const auto *ParamTy =
798 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000799 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
800 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000801 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000802
Richard Smith328aae52012-11-30 05:11:39 +0000803 if (Method->isMoveAssignmentOperator())
804 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000805
Douglas Gregor457104e2010-09-29 04:25:11 +0000806 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000807 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000808 // FIXME: We use the 'unsafe' accessor for the access specifier here,
809 // because Sema may not have set it yet. That's really just a misdesign
810 // in Sema. However, LLDB *will* have set the access specifier correctly,
811 // and adds declarations after the class is technically completed,
812 // so completeDefinition()'s overriding of the access specifiers doesn't
813 // work.
814 AccessSpecifier AS = Conversion->getAccessUnsafe();
815
Richard Smith328aae52012-11-30 05:11:39 +0000816 if (Conversion->getPrimaryTemplate()) {
817 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000818 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000819 ASTContext &Ctx = getASTContext();
820 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
821 NamedDecl *Primary =
822 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
823 if (Primary->getPreviousDecl())
824 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
825 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000826 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000827 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000828 }
829 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000830
Richard Smith328aae52012-11-30 05:11:39 +0000831 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000832 // If this is the first declaration of a special member, we no longer have
833 // an implicit trivial special member.
834 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000835 data().DeclaredSpecialMembers | ~SMKind;
836 data().HasTrivialSpecialMembersForCall &=
837 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000838
839 if (!Method->isImplicit() && !Method->isUserProvided()) {
840 // This method is user-declared but not user-provided. We can't work out
841 // whether it's trivial yet (not until we get to the end of the class).
842 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000843 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000844 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000845 data().HasTrivialSpecialMembersForCall |= SMKind;
846 } else if (Method->isTrivialForCall()) {
847 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000848 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000849 } else {
850 data().DeclaredNonTrivialSpecialMembers |= SMKind;
851 // If this is a user-provided function, do not set
852 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
853 // yet whether the method would be considered non-trivial for the
854 // purpose of calls (attribute "trivial_abi" can be dropped from the
855 // class later, which can change the special method's triviality).
856 if (!Method->isUserProvided())
857 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
858 }
Richard Smith92f241f2012-12-08 02:53:02 +0000859
Richard Smith328aae52012-11-30 05:11:39 +0000860 // Note when we have declared a declared special member, and suppress the
861 // implicit declaration of this special member.
862 data().DeclaredSpecialMembers |= SMKind;
863
864 if (!Method->isImplicit()) {
865 data().UserDeclaredSpecialMembers |= SMKind;
866
867 // C++03 [class]p4:
868 // A POD-struct is an aggregate class that has [...] no user-defined
869 // copy assignment operator and no user-defined destructor.
870 //
871 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
872 // aggregates could not have any constructors, clear it even for an
873 // explicitly defaulted or deleted constructor.
874 // type is technically an aggregate in C++0x since it wouldn't be in 03.
875 //
876 // Also, a user-declared move assignment operator makes a class non-POD.
877 // This is an extension in C++03.
878 data().PlainOldData = false;
879 }
Richard Smith328aae52012-11-30 05:11:39 +0000880 }
881
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000882 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000883 }
Richard Smith328aae52012-11-30 05:11:39 +0000884
Douglas Gregora832d3e2010-09-28 19:45:33 +0000885 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000886 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000887 ASTContext &Context = getASTContext();
888
Richard Smithb6070db2018-04-05 18:55:37 +0000889 // C++2a [class]p7:
890 // A standard-layout class is a class that:
891 // [...]
892 // -- has all non-static data members and bit-fields in the class and
893 // its base classes first declared in the same class
894 if (data().HasBasesWithFields)
895 data().IsStandardLayout = false;
896
Douglas Gregor556e5862011-10-10 17:22:13 +0000897 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000898 // A declaration for a bit-field that omits the identifier declares an
899 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000900 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000901 if (Field->isUnnamedBitfield()) {
902 // C++ [meta.unary.prop]p4: [LWG2358]
903 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
904 // length
905 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
906 Context.getLangOpts().getClangABICompat() >
907 LangOptions::ClangABI::Ver6)
908 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000909 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000910 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000911
Richard Smithb6070db2018-04-05 18:55:37 +0000912 // C++11 [class]p7:
913 // A standard-layout class is a class that:
914 // -- either has no non-static data members in the most derived class
915 // [...] or has no base classes with non-static data members
916 if (data().HasBasesWithNonStaticDataMembers)
917 data().IsCXX11StandardLayout = false;
918
Douglas Gregora832d3e2010-09-28 19:45:33 +0000919 // C++ [dcl.init.aggr]p1:
920 // An aggregate is an array or a class (clause 9) with [...] no
921 // private or protected non-static data members (clause 11).
922 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000923 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000924 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
925 data().Aggregate = false;
926 data().PlainOldData = false;
927 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000928
Richard Smithb6070db2018-04-05 18:55:37 +0000929 // Track whether this is the first field. We use this when checking
930 // whether the class is standard-layout below.
931 bool IsFirstField = !data().HasPrivateFields &&
932 !data().HasProtectedFields && !data().HasPublicFields;
933
Chandler Carruthb1963742011-04-30 09:17:45 +0000934 // C++0x [class]p7:
935 // A standard-layout class is a class that:
936 // [...]
937 // -- has the same access control for all non-static data members,
938 switch (D->getAccess()) {
939 case AS_private: data().HasPrivateFields = true; break;
940 case AS_protected: data().HasProtectedFields = true; break;
941 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000942 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000943 };
944 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000945 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000946 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000947 data().IsCXX11StandardLayout = false;
948 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000949
Douglas Gregor61226d32011-05-13 01:05:07 +0000950 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000951 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000952 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000953 data().NeedOverloadResolutionForCopyConstructor = true;
954 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000955
956 // C++11 [class.union]p8, DR1460:
957 // If X is a union, a non-static data member of X that is not an anonymous
958 // union is a variant member of X.
959 if (isUnion() && !Field->isAnonymousStructOrUnion())
960 data().HasVariantMembers = true;
961
Chandler Carruthad7d4042011-04-23 23:10:33 +0000962 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000963 // A POD struct is a class that is both a trivial class and a
964 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000965 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000966 //
967 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
968 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000969 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000970 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000971 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000972 // Objective-C Automatic Reference Counting:
973 // If a class has a non-static data member of Objective-C pointer
974 // type (or array thereof), it is a non-POD type and its
975 // default constructor (if any), copy constructor, move constructor,
976 // copy assignment operator, move assignment operator, and destructor are
977 // non-trivial.
978 setHasObjectMember(true);
979 struct DefinitionData &Data = data();
980 Data.PlainOldData = false;
981 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000982
983 // __strong or __weak fields do not make special functions non-trivial
984 // for the purpose of calls.
985 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
986 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
987 data().HasTrivialSpecialMembersForCall = 0;
988
989 // Structs with __weak fields should never be passed directly.
990 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000991 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000992
Ben Langmuir11eab612014-09-26 15:27:29 +0000993 Data.HasIrrelevantDestructor = false;
Akira Hatanakaa5048972019-02-02 02:23:40 +0000994
995 if (isUnion()) {
996 data().DefaultedCopyConstructorIsDeleted = true;
997 data().DefaultedMoveConstructorIsDeleted = true;
998 data().DefaultedMoveAssignmentIsDeleted = true;
999 data().DefaultedDestructorIsDeleted = true;
1000 data().NeedOverloadResolutionForCopyConstructor = true;
1001 data().NeedOverloadResolutionForMoveConstructor = true;
1002 data().NeedOverloadResolutionForMoveAssignment = true;
1003 data().NeedOverloadResolutionForDestructor = true;
1004 }
Brian Kelley036603a2017-03-29 17:31:42 +00001005 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
1006 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +00001007 }
Eli Friedmana5433322013-07-20 01:06:31 +00001008 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +00001009 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001010
Chandler Carruthb1963742011-04-30 09:17:45 +00001011 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +00001012 if (!Field->hasInClassInitializer())
1013 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +00001014
Chandler Carruthb1963742011-04-30 09:17:45 +00001015 // C++0x [class]p7:
1016 // A standard-layout class is a class that:
1017 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +00001018 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001019 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +00001020
1021 // C++1z [class.copy.ctor]p10:
1022 // A defaulted copy constructor for a class X is defined as deleted if X has:
1023 // -- a non-static data member of rvalue reference type
1024 if (T->isRValueReferenceType())
1025 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +00001026 }
1027
Nico Weber6a6376b2016-02-19 01:52:46 +00001028 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +00001029 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +00001030 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +00001031 data().HasUninitializedFields = true;
1032 } else {
1033 data().HasUninitializedFields = true;
1034 }
1035 }
1036
Richard Smith3607ffe2012-02-13 03:54:03 +00001037 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001038 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001039 data().HasNonLiteralTypeFieldsOrBases = true;
1040
Richard Smithab44d5b2013-12-10 08:25:00 +00001041 if (Field->hasInClassInitializer() ||
1042 (Field->isAnonymousStructOrUnion() &&
1043 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001044 data().HasInClassInitializer = true;
1045
1046 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001047 // A default constructor is trivial if [...] no non-static data member
1048 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001049 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001050
Richard Smithe2648ba2012-05-07 01:07:30 +00001051 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001052 // An aggregate is a [...] class with [...] no
1053 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001054 //
Richard Smith872307e2016-03-08 22:17:41 +00001055 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001056 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001057 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001058
Richard Smithe2648ba2012-05-07 01:07:30 +00001059 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001060 // A POD struct is [...] a trivial class.
1061 data().PlainOldData = false;
1062 }
1063
Richard Smith6b02d462012-12-08 08:32:28 +00001064 // C++11 [class.copy]p23:
1065 // A defaulted copy/move assignment operator for a class X is defined
1066 // as deleted if X has:
1067 // -- a non-static data member of reference type
1068 if (T->isReferenceType())
1069 data().DefaultedMoveAssignmentIsDeleted = true;
1070
Eugene Zelenko7855e772018-04-03 00:11:50 +00001071 if (const auto *RecordTy = T->getAs<RecordType>()) {
1072 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001073 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001074 addedClassSubobject(FieldRec);
1075
Richard Smithc91d12c2013-11-25 07:07:05 +00001076 // We may need to perform overload resolution to determine whether a
1077 // field can be moved if it's const or volatile qualified.
1078 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001079 // We need to care about 'const' for the copy constructor because an
1080 // implicit copy constructor might be declared with a non-const
1081 // parameter.
1082 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001083 data().NeedOverloadResolutionForMoveConstructor = true;
1084 data().NeedOverloadResolutionForMoveAssignment = true;
1085 }
1086
Richard Smith6b02d462012-12-08 08:32:28 +00001087 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1088 // A defaulted [special member] for a class X is defined as
1089 // deleted if:
1090 // -- X is a union-like class that has a variant member with a
1091 // non-trivial [corresponding special member]
1092 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001093 if (FieldRec->hasNonTrivialCopyConstructor())
1094 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001095 if (FieldRec->hasNonTrivialMoveConstructor())
1096 data().DefaultedMoveConstructorIsDeleted = true;
1097 if (FieldRec->hasNonTrivialMoveAssignment())
1098 data().DefaultedMoveAssignmentIsDeleted = true;
1099 if (FieldRec->hasNonTrivialDestructor())
1100 data().DefaultedDestructorIsDeleted = true;
1101 }
1102
Richard Smithdd5619f2016-08-16 00:13:47 +00001103 // For an anonymous union member, our overload resolution will perform
1104 // overload resolution for its members.
1105 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001106 data().NeedOverloadResolutionForCopyConstructor |=
1107 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001108 data().NeedOverloadResolutionForMoveConstructor |=
1109 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1110 data().NeedOverloadResolutionForMoveAssignment |=
1111 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1112 data().NeedOverloadResolutionForDestructor |=
1113 FieldRec->data().NeedOverloadResolutionForDestructor;
1114 }
1115
Alexis Huntf479f1b2011-05-09 18:22:59 +00001116 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001117 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001118 // -- for all the non-static data members of its class that are of
1119 // class type (or array thereof), each such class has a trivial
1120 // default constructor.
1121 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001122 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001123
1124 // C++0x [class.copy]p13:
1125 // A copy/move constructor for class X is trivial if [...]
1126 // [...]
1127 // -- for each non-static data member of X that is of class type (or
1128 // an array thereof), the constructor selected to copy/move that
1129 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001130 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001131 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001132
1133 if (!FieldRec->hasTrivialCopyConstructorForCall())
1134 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1135
Richard Smith6b02d462012-12-08 08:32:28 +00001136 // If the field doesn't have a simple move constructor, we'll eagerly
1137 // declare the move constructor for this class and we'll decide whether
1138 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001139 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001140 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001141
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001142 if (!FieldRec->hasTrivialMoveConstructorForCall())
1143 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1144
Chandler Carruthad7d4042011-04-23 23:10:33 +00001145 // C++0x [class.copy]p27:
1146 // A copy/move assignment operator for class X is trivial if [...]
1147 // [...]
1148 // -- for each non-static data member of X that is of class type (or
1149 // an array thereof), the assignment operator selected to
1150 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001151 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001152 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001153 // If the field doesn't have a simple move assignment, we'll eagerly
1154 // declare the move assignment for this class and we'll decide whether
1155 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001156 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001157 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001158
Douglas Gregor11c024b2010-09-28 20:50:54 +00001159 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001160 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001161 if (!FieldRec->hasTrivialDestructorForCall())
1162 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001163 if (!FieldRec->hasIrrelevantDestructor())
1164 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001165 if (FieldRec->hasObjectMember())
1166 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001167 if (FieldRec->hasVolatileMember())
1168 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001169 if (FieldRec->getArgPassingRestrictions() ==
1170 RecordDecl::APK_CanNeverPassInRegs)
1171 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001172
1173 // C++0x [class]p7:
1174 // A standard-layout class is a class that:
1175 // -- has no non-static data members of type non-standard-layout
1176 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001177 if (!FieldRec->isStandardLayout())
1178 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001179 if (!FieldRec->isCXX11StandardLayout())
1180 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001181
Richard Smithb6070db2018-04-05 18:55:37 +00001182 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001183 // A standard-layout class is a class that:
1184 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001185 // -- has no element of the set M(S) of types as a base class.
1186 if (data().IsStandardLayout && (isUnion() || IsFirstField) &&
1187 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1188 data().IsStandardLayout = false;
1189
1190 // C++11 [class]p7:
1191 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001192 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001193 // data member
1194 if (data().IsCXX11StandardLayout && IsFirstField) {
1195 // FIXME: We should check all base classes here, not just direct
1196 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001197 for (const auto &BI : bases()) {
1198 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001199 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001200 break;
1201 }
1202 }
1203 }
Richard Smithb6070db2018-04-05 18:55:37 +00001204
Douglas Gregor61226d32011-05-13 01:05:07 +00001205 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001206 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001207 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001208 data().NeedOverloadResolutionForCopyConstructor = true;
1209 }
Richard Smithcc36f692011-12-22 02:22:31 +00001210
1211 // C++11 [class.copy]p13:
1212 // If the implicitly-defined constructor would satisfy the
1213 // requirements of a constexpr constructor, the implicitly-defined
1214 // constructor is constexpr.
1215 // C++11 [dcl.constexpr]p4:
1216 // -- every constructor involved in initializing non-static data
1217 // members [...] shall be a constexpr constructor
1218 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001219 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001220 // The standard requires any in-class initializer to be a constant
1221 // expression. We consider this to be a defect.
1222 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001223
1224 // C++11 [class.copy]p8:
1225 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001226 // the form 'X::X(const X&)' if each potentially constructed subobject
1227 // of a class type M (or array thereof) has a copy constructor whose
1228 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001229 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001230 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001231
1232 // C++11 [class.copy]p18:
1233 // The implicitly-declared copy assignment oeprator for a class X will
1234 // have the form 'X& X::operator=(const X&)' if [...] for all the
1235 // non-static data members of X that are of a class type M (or array
1236 // thereof), each such class type has a copy assignment operator whose
1237 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1238 if (!FieldRec->hasCopyAssignmentWithConstParam())
1239 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001240
1241 if (FieldRec->hasUninitializedReferenceMember() &&
1242 !Field->hasInClassInitializer())
1243 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001244
1245 // C++11 [class.union]p8, DR1460:
1246 // a non-static data member of an anonymous union that is a member of
1247 // X is also a variant member of X.
1248 if (FieldRec->hasVariantMembers() &&
1249 Field->isAnonymousStructOrUnion())
1250 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001251 }
Richard Smithcc36f692011-12-22 02:22:31 +00001252 } else {
1253 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001254 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +00001255 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +00001256 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001257
1258 // C++11 [class.copy]p23:
1259 // A defaulted copy/move assignment operator for a class X is defined
1260 // as deleted if X has:
1261 // -- a non-static data member of const non-class type (or array
1262 // thereof)
1263 if (T.isConstQualified())
1264 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001265 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001266
David Majnemer5cfda6f2016-05-22 05:34:26 +00001267 // C++14 [meta.unary.prop]p4:
Richard Smithb5a317f2018-05-07 06:43:30 +00001268 // T is a class type [...] with [...] no non-static data members
1269 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001270 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001271
Douglas Gregor457104e2010-09-29 04:25:11 +00001272 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001273 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001274 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001275 == DeclarationName::CXXConversionFunctionName) {
1276 ASTContext &Ctx = getASTContext();
1277 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1278 }
1279 }
Richard Smith12e79312016-05-13 06:47:56 +00001280
Eugene Zelenko7855e772018-04-03 00:11:50 +00001281 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001282 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001283 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001284 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001285 // C++1z [dcl.init.aggr]p1:
1286 // An aggregate is [...] a class [...] with no inherited constructors
1287 data().Aggregate = false;
1288 }
Richard Smith12e79312016-05-13 06:47:56 +00001289
1290 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1291 data().HasInheritedAssignment = true;
1292 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001293}
1294
Richard Smith92f241f2012-12-08 02:53:02 +00001295void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1296 assert(!D->isImplicit() && !D->isUserProvided());
1297
1298 // The kind of special member this declaration is, if any.
1299 unsigned SMKind = 0;
1300
Eugene Zelenko7855e772018-04-03 00:11:50 +00001301 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001302 if (Constructor->isDefaultConstructor()) {
1303 SMKind |= SMF_DefaultConstructor;
1304 if (Constructor->isConstexpr())
1305 data().HasConstexprDefaultConstructor = true;
1306 }
1307 if (Constructor->isCopyConstructor())
1308 SMKind |= SMF_CopyConstructor;
1309 else if (Constructor->isMoveConstructor())
1310 SMKind |= SMF_MoveConstructor;
1311 else if (Constructor->isConstexpr())
1312 // We may now know that the constructor is constexpr.
1313 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001314 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001315 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001316 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1317 data().HasIrrelevantDestructor = false;
1318 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001319 SMKind |= SMF_CopyAssignment;
1320 else if (D->isMoveAssignmentOperator())
1321 SMKind |= SMF_MoveAssignment;
1322
1323 // Update which trivial / non-trivial special members we have.
1324 // addedMember will have skipped this step for this member.
1325 if (D->isTrivial())
1326 data().HasTrivialSpecialMembers |= SMKind;
1327 else
1328 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1329}
1330
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001331void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1332 unsigned SMKind = 0;
1333
Eugene Zelenko7855e772018-04-03 00:11:50 +00001334 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001335 if (Constructor->isCopyConstructor())
1336 SMKind = SMF_CopyConstructor;
1337 else if (Constructor->isMoveConstructor())
1338 SMKind = SMF_MoveConstructor;
1339 } else if (isa<CXXDestructorDecl>(D))
1340 SMKind = SMF_Destructor;
1341
1342 if (D->isTrivialForCall())
1343 data().HasTrivialSpecialMembersForCall |= SMKind;
1344 else
1345 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1346}
1347
Joao Matose9a3ed42012-08-31 22:18:20 +00001348bool CXXRecordDecl::isCLike() const {
1349 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1350 !TemplateOrInstantiation.isNull())
1351 return false;
1352 if (!hasDefinition())
1353 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001354
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001355 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001356}
Fangrui Song6907ce22018-07-30 19:24:48 +00001357
1358bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001359 if (!isLambda()) return false;
1360 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001361}
1362
Richard Smith24253382018-08-02 20:30:52 +00001363#ifndef NDEBUG
1364static bool allLookupResultsAreTheSame(const DeclContext::lookup_result &R) {
1365 for (auto *D : R)
1366 if (!declaresSameEntity(D, R.front()))
1367 return false;
1368 return true;
1369}
1370#endif
1371
Faisal Vali2b391ab2013-09-26 19:54:12 +00001372CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001373 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001374 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001375 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001376 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001377
1378 assert(!Calls.empty() && "Missing lambda call operator!");
Richard Smith24253382018-08-02 20:30:52 +00001379 assert(allLookupResultsAreTheSame(Calls) &&
1380 "More than one lambda call operator!");
Fangrui Song6907ce22018-07-30 19:24:48 +00001381
Faisal Vali850da1a2013-09-29 17:08:32 +00001382 NamedDecl *CallOp = Calls.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001383 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001384 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001385
Faisal Vali2b391ab2013-09-26 19:54:12 +00001386 return cast<CXXMethodDecl>(CallOp);
1387}
1388
1389CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001390 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001391 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001392 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001393 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001394 if (Invoker.empty()) return nullptr;
Richard Smith24253382018-08-02 20:30:52 +00001395 assert(allLookupResultsAreTheSame(Invoker) &&
1396 "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001397 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001398 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001399 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001400
1401 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001402}
1403
Douglas Gregor9c702202012-02-10 07:45:31 +00001404void CXXRecordDecl::getCaptureFields(
1405 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001406 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001407 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001408 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001409
Douglas Gregorc8a73492012-02-13 15:44:47 +00001410 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001411 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001412 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001413 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001414 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001415 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001416 else if (C->capturesVariable())
1417 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001418 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001419 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001420}
1421
Fangrui Song6907ce22018-07-30 19:24:48 +00001422TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001423CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001424 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001425 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001426 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1427 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001428 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001429}
Douglas Gregor9c702202012-02-10 07:45:31 +00001430
Richard Smith0bae6242016-08-25 00:34:00 +00001431Decl *CXXRecordDecl::getLambdaContextDecl() const {
1432 assert(isLambda() && "Not a lambda closure type!");
1433 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1434 return getLambdaData().ContextDecl.get(Source);
1435}
1436
John McCall1e3a1a72010-03-15 09:07:48 +00001437static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001438 QualType T =
1439 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1440 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001441 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001442}
1443
John McCall1e3a1a72010-03-15 09:07:48 +00001444/// Collect the visible conversions of a base class.
1445///
James Dennettb5d5f812012-06-15 22:28:09 +00001446/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001447/// \param InVirtual whether this base class is a virtual base (or a base
1448/// of a virtual base)
1449/// \param Access the access along the inheritance path to this base
1450/// \param ParentHiddenTypes the conversions provided by the inheritors
1451/// of this base
1452/// \param Output the set to which to add conversions from non-virtual bases
1453/// \param VOutput the set to which to add conversions from virtual bases
1454/// \param HiddenVBaseCs the set of conversions which were hidden in a
1455/// virtual base along some inheritance path
1456static void CollectVisibleConversions(ASTContext &Context,
1457 CXXRecordDecl *Record,
1458 bool InVirtual,
1459 AccessSpecifier Access,
1460 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001461 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001462 UnresolvedSetImpl &VOutput,
1463 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1464 // The set of types which have conversions in this class or its
1465 // subclasses. As an optimization, we don't copy the derived set
1466 // unless it might change.
1467 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1468 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1469
1470 // Collect the direct conversions and figure out which conversions
1471 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001472 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1473 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1474 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001475 HiddenTypesBuffer = ParentHiddenTypes;
1476 HiddenTypes = &HiddenTypesBuffer;
1477
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001478 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001479 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1480 bool Hidden = ParentHiddenTypes.count(ConvType);
1481 if (!Hidden)
1482 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001483
1484 // If this conversion is hidden and we're in a virtual base,
1485 // remember that it's hidden along some inheritance path.
1486 if (Hidden && InVirtual)
1487 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1488
1489 // If this conversion isn't hidden, add it to the appropriate output.
1490 else if (!Hidden) {
1491 AccessSpecifier IAccess
1492 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1493
1494 if (InVirtual)
1495 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001496 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001497 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001498 }
1499 }
1500 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001501
John McCall1e3a1a72010-03-15 09:07:48 +00001502 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001503 for (const auto &I : Record->bases()) {
1504 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001505 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001506
John McCall1e3a1a72010-03-15 09:07:48 +00001507 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001508 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1509 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001510
Eugene Zelenko7855e772018-04-03 00:11:50 +00001511 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001512 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1513 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001514 }
John McCall1e3a1a72010-03-15 09:07:48 +00001515}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001516
John McCall1e3a1a72010-03-15 09:07:48 +00001517/// Collect the visible conversions of a class.
1518///
1519/// This would be extremely straightforward if it weren't for virtual
1520/// bases. It might be worth special-casing that, really.
1521static void CollectVisibleConversions(ASTContext &Context,
1522 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001523 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001524 // The collection of all conversions in virtual bases that we've
1525 // found. These will be added to the output as long as they don't
1526 // appear in the hidden-conversions set.
1527 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001528
John McCall1e3a1a72010-03-15 09:07:48 +00001529 // The set of conversions in virtual bases that we've determined to
1530 // be hidden.
1531 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1532
1533 // The set of types hidden by classes derived from this one.
1534 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1535
1536 // Go ahead and collect the direct conversions and add them to the
1537 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001538 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1539 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001540 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001541 for (; ConvI != ConvE; ++ConvI)
1542 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001543
1544 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001545 for (const auto &I : Record->bases()) {
1546 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001547 if (!RT) continue;
1548
1549 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001550 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001551 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1552 }
1553
1554 // Add any unhidden conversions provided by virtual bases.
1555 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1556 I != E; ++I) {
1557 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001558 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001559 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001560}
1561
1562/// getVisibleConversionFunctions - get all conversion functions visible
1563/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001564llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001565CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001566 ASTContext &Ctx = getASTContext();
1567
1568 ASTUnresolvedSet *Set;
1569 if (bases_begin() == bases_end()) {
1570 // If root class, all conversions are visible.
1571 Set = &data().Conversions.get(Ctx);
1572 } else {
1573 Set = &data().VisibleConversions.get(Ctx);
1574 // If visible conversion list is not evaluated, evaluate it.
1575 if (!data().ComputedVisibleConversions) {
1576 CollectVisibleConversions(Ctx, this, *Set);
1577 data().ComputedVisibleConversions = true;
1578 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001579 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001580 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001581}
1582
John McCallda4458e2010-03-31 01:36:47 +00001583void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1584 // This operation is O(N) but extremely rare. Sema only uses it to
1585 // remove UsingShadowDecls in a class that were followed by a direct
1586 // declaration, e.g.:
1587 // class A : B {
1588 // using B::operator int;
1589 // operator int();
1590 // };
1591 // This is uncommon by itself and even more uncommon in conjunction
1592 // with sufficiently large numbers of directly-declared conversions
1593 // that asymptotic behavior matters.
1594
Richard Smitha4ba74c2013-08-30 04:46:40 +00001595 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001596 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1597 if (Convs[I].getDecl() == ConvDecl) {
1598 Convs.erase(I);
Fangrui Song75e74e02019-03-31 08:48:19 +00001599 assert(llvm::find(Convs, ConvDecl) == Convs.end() &&
1600 "conversion was found multiple times in unresolved set");
John McCallda4458e2010-03-31 01:36:47 +00001601 return;
1602 }
1603 }
1604
1605 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001606}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001607
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001608CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001609 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001610 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001611
1612 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001613}
1614
Chandler Carruth21c90602015-12-30 03:24:14 +00001615MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1616 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1617}
1618
Fangrui Song6907ce22018-07-30 19:24:48 +00001619void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001620CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1621 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001622 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001623 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001624 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001625 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001626 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1627}
1628
Chandler Carruth21c90602015-12-30 03:24:14 +00001629ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1630 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1631}
1632
1633void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1634 TemplateOrInstantiation = Template;
1635}
1636
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001637TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001638 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001639 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001640
Douglas Gregor06db9f52009-10-12 20:18:28 +00001641 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001642 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001643
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001644 return TSK_Undeclared;
1645}
1646
Fangrui Song6907ce22018-07-30 19:24:48 +00001647void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001648CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001649 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001650 Spec->setSpecializationKind(TSK);
1651 return;
1652 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001653
Douglas Gregor06db9f52009-10-12 20:18:28 +00001654 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001655 MSInfo->setTemplateSpecializationKind(TSK);
1656 return;
1657 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001658
David Blaikie83d382b2011-09-23 05:06:16 +00001659 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001660}
1661
Reid Klecknere7367d62014-10-14 20:28:40 +00001662const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001663 auto GetDefinitionOrSelf =
1664 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1665 if (auto *Def = D->getDefinition())
1666 return Def;
1667 return D;
1668 };
1669
Reid Klecknere7367d62014-10-14 20:28:40 +00001670 // If it's a class template specialization, find the template or partial
1671 // specialization from which it was instantiated.
1672 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1673 auto From = TD->getInstantiatedFrom();
1674 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1675 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1676 if (NewCTD->isMemberSpecialization())
1677 break;
1678 CTD = NewCTD;
1679 }
Richard Smith2195ec92017-04-21 01:15:13 +00001680 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001681 }
1682 if (auto *CTPSD =
1683 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1684 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1685 if (NewCTPSD->isMemberSpecialization())
1686 break;
1687 CTPSD = NewCTPSD;
1688 }
Richard Smith2195ec92017-04-21 01:15:13 +00001689 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001690 }
1691 }
1692
1693 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1694 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1695 const CXXRecordDecl *RD = this;
1696 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1697 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001698 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001699 }
1700 }
1701
1702 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1703 "couldn't find pattern for class template instantiation");
1704 return nullptr;
1705}
1706
Douglas Gregorbac74902010-07-01 14:13:13 +00001707CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1708 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001709 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001710
1711 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001712 = Context.DeclarationNames.getCXXDestructorName(
1713 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001714
Richard Smithcf4bdde2015-02-21 02:45:19 +00001715 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001716
Hubert Tong3cede512017-06-30 22:43:54 +00001717 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001718}
1719
Richard Trieu95a192a2015-05-28 00:14:02 +00001720bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1721 // Destructor is noreturn.
1722 if (const CXXDestructorDecl *Destructor = getDestructor())
1723 if (Destructor->isNoReturn())
1724 return true;
1725
1726 // Check base classes destructor for noreturn.
1727 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001728 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1729 if (RD->isAnyDestructorNoReturn())
1730 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001731
1732 // Check fields for noreturn.
1733 for (const auto *Field : fields())
1734 if (const CXXRecordDecl *RD =
1735 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1736 if (RD->isAnyDestructorNoReturn())
1737 return true;
1738
1739 // All destructors are not noreturn.
1740 return false;
1741}
1742
Erich Keane3e08f662017-09-29 21:06:00 +00001743static bool isDeclContextInNamespace(const DeclContext *DC) {
1744 while (!DC->isTranslationUnit()) {
1745 if (DC->isNamespace())
1746 return true;
1747 DC = DC->getParent();
1748 }
1749 return false;
1750}
1751
Erich Keane58bd6032017-09-15 16:03:35 +00001752bool CXXRecordDecl::isInterfaceLike() const {
1753 assert(hasDefinition() && "checking for interface-like without a definition");
1754 // All __interfaces are inheritently interface-like.
1755 if (isInterface())
1756 return true;
1757
1758 // Interface-like types cannot have a user declared constructor, destructor,
1759 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1760 // cannot be interface types.
1761 if (isLambda() || hasUserDeclaredConstructor() ||
1762 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1763 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1764 return false;
1765
1766 // No interface-like type can have a method with a definition.
1767 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001768 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001769 return false;
1770
1771 // Check "Special" types.
1772 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001773 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1774 // extern C++ block directly in the TU. These are only valid if in one
1775 // of these two situations.
1776 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1777 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001778 ((getName() == "IUnknown" &&
1779 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1780 (getName() == "IDispatch" &&
1781 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1782 if (getNumBases() > 0)
1783 return false;
1784 return true;
1785 }
1786
1787 // FIXME: Any access specifiers is supposed to make this no longer interface
1788 // like.
1789
1790 // If this isn't a 'special' type, it must have a single interface-like base.
1791 if (getNumBases() != 1)
1792 return false;
1793
1794 const auto BaseSpec = *bases_begin();
1795 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1796 return false;
1797 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1798 if (Base->isInterface() || !Base->isInterfaceLike())
1799 return false;
1800 return true;
1801}
1802
Douglas Gregorb11aad82011-02-19 18:51:44 +00001803void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001804 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001805}
1806
1807void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1808 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001809
Douglas Gregor8fb95122010-09-29 00:15:42 +00001810 // If the class may be abstract (but hasn't been marked as such), check for
1811 // any pure final overriders.
1812 if (mayBeAbstract()) {
1813 CXXFinalOverriderMap MyFinalOverriders;
1814 if (!FinalOverriders) {
1815 getFinalOverriders(MyFinalOverriders);
1816 FinalOverriders = &MyFinalOverriders;
1817 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001818
Douglas Gregor8fb95122010-09-29 00:15:42 +00001819 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001820 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001821 MEnd = FinalOverriders->end();
1822 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001823 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001824 SOEnd = M->second.end();
1825 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001826 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001827 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001828
Douglas Gregor8fb95122010-09-29 00:15:42 +00001829 // C++ [class.abstract]p4:
1830 // A class is abstract if it contains or inherits at least one
1831 // pure virtual function for which the final overrider is pure
1832 // virtual.
1833 if (SO->second.front().Method->isPure()) {
1834 data().Abstract = true;
1835 Done = true;
1836 break;
1837 }
1838 }
1839 }
1840 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001841
Douglas Gregor457104e2010-09-29 04:25:11 +00001842 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001843 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001844 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001845 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001846}
1847
1848bool CXXRecordDecl::mayBeAbstract() const {
1849 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1850 isDependentContext())
1851 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001852
Aaron Ballman574705e2014-03-13 15:41:46 +00001853 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001854 const auto *BaseDecl =
1855 cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001856 if (BaseDecl->isAbstract())
1857 return true;
1858 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001859
Douglas Gregor8fb95122010-09-29 00:15:42 +00001860 return false;
1861}
1862
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001863void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001864
1865CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1866 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1867 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1868 SourceLocation EndLocation) {
1869 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1870 NameInfo, T, TInfo, EndLocation);
1871}
1872
1873CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1874 unsigned ID) {
1875 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1876 DeclarationNameInfo(), QualType(),
1877 nullptr, SourceLocation());
1878}
1879
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001880void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001881
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001882bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001883 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001884
1885 if (MD->getStorageClass() == SC_Static)
1886 return true;
1887
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001888 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1889 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001890}
1891
Rafael Espindola49e860b2012-06-26 17:45:31 +00001892static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1893 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001894 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001895 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1896 return true;
1897 if (recursivelyOverrides(MD, BaseMD))
1898 return true;
1899 }
1900 return false;
1901}
1902
1903CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001904CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1905 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001906 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1907 return this;
1908
1909 // Lookup doesn't work for destructors, so handle them separately.
1910 if (isa<CXXDestructorDecl>(this)) {
1911 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001912 if (MD) {
1913 if (recursivelyOverrides(MD, this))
1914 return MD;
1915 if (MayBeBase && recursivelyOverrides(this, MD))
1916 return MD;
1917 }
Craig Topper36250ad2014-05-12 05:36:57 +00001918 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001919 }
1920
Richard Smith40c78062015-02-21 02:31:57 +00001921 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001922 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001923 if (!MD)
1924 continue;
1925 if (recursivelyOverrides(MD, this))
1926 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001927 if (MayBeBase && recursivelyOverrides(this, MD))
1928 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001929 }
1930
Aaron Ballman574705e2014-03-13 15:41:46 +00001931 for (const auto &I : RD->bases()) {
1932 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001933 if (!RT)
1934 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00001935 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00001936 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1937 if (T)
1938 return T;
1939 }
1940
Craig Topper36250ad2014-05-12 05:36:57 +00001941 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001942}
1943
Ted Kremenek21475702008-09-05 17:16:31 +00001944CXXMethodDecl *
1945CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001946 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001947 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001948 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001949 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001950 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001951 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1952 T, TInfo, SC, isInline, isConstexpr,
1953 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001954}
1955
Douglas Gregor72172e92012-01-05 21:55:30 +00001956CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001957 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001958 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001959 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001960}
1961
Akira Hatanaka22461672017-07-13 06:08:27 +00001962CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
1963 bool IsAppleKext) {
1964 assert(isVirtual() && "this method is expected to be virtual");
1965
1966 // When building with -fapple-kext, all calls must go through the vtable since
1967 // the kernel linker can do runtime patching of vtables.
1968 if (IsAppleKext)
1969 return nullptr;
1970
1971 // If the member function is marked 'final', we know that it can't be
1972 // overridden and can therefore devirtualize it unless it's pure virtual.
1973 if (hasAttr<FinalAttr>())
1974 return isPure() ? nullptr : this;
1975
1976 // If Base is unknown, we cannot devirtualize.
1977 if (!Base)
1978 return nullptr;
1979
1980 // If the base expression (after skipping derived-to-base conversions) is a
1981 // class prvalue, then we can devirtualize.
1982 Base = Base->getBestDynamicClassTypeExpr();
1983 if (Base->isRValue() && Base->getType()->isRecordType())
1984 return this;
1985
1986 // If we don't even know what we would call, we can't devirtualize.
1987 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
1988 if (!BestDynamicDecl)
1989 return nullptr;
1990
1991 // There may be a method corresponding to MD in a derived class.
1992 CXXMethodDecl *DevirtualizedMethod =
1993 getCorrespondingMethodInClass(BestDynamicDecl);
1994
1995 // If that method is pure virtual, we can't devirtualize. If this code is
1996 // reached, the result would be UB, not a direct call to the derived class
1997 // function, and we can't assume the derived class function is defined.
1998 if (DevirtualizedMethod->isPure())
1999 return nullptr;
2000
2001 // If that method is marked final, we can devirtualize it.
2002 if (DevirtualizedMethod->hasAttr<FinalAttr>())
2003 return DevirtualizedMethod;
2004
2005 // Similarly, if the class itself is marked 'final' it can't be overridden
2006 // and we can therefore devirtualize the member function call.
2007 if (BestDynamicDecl->hasAttr<FinalAttr>())
2008 return DevirtualizedMethod;
2009
Eugene Zelenko7855e772018-04-03 00:11:50 +00002010 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
2011 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00002012 if (VD->getType()->isRecordType())
2013 // This is a record decl. We know the type and can devirtualize it.
2014 return DevirtualizedMethod;
2015
2016 return nullptr;
2017 }
2018
2019 // We can devirtualize calls on an object accessed by a class member access
2020 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2021 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002022 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00002023 const ValueDecl *VD = ME->getMemberDecl();
2024 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
2025 }
Akira Hatanaka22461672017-07-13 06:08:27 +00002026
2027 // Likewise for calls on an object accessed by a (non-reference) pointer to
2028 // member access.
2029 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
2030 if (BO->isPtrMemOp()) {
2031 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
2032 if (MPT->getPointeeType()->isRecordType())
2033 return DevirtualizedMethod;
2034 }
2035 }
2036
2037 // We can't devirtualize the call.
2038 return nullptr;
2039}
2040
Artem Belevich78929ef2018-09-21 17:29:33 +00002041bool CXXMethodDecl::isUsualDeallocationFunction(
2042 SmallVectorImpl<const FunctionDecl *> &PreventedBy) const {
2043 assert(PreventedBy.empty() && "PreventedBy is expected to be empty");
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002044 if (getOverloadedOperator() != OO_Delete &&
2045 getOverloadedOperator() != OO_Array_Delete)
2046 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002047
2048 // C++ [basic.stc.dynamic.deallocation]p2:
2049 // A template instance is never a usual deallocation function,
2050 // regardless of its signature.
2051 if (getPrimaryTemplate())
2052 return false;
2053
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002054 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002055 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002056 // with exactly one parameter, then that function is a usual (non-placement)
2057 // deallocation function. [...]
2058 if (getNumParams() == 1)
2059 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002060 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002061
Richard Smith5b349582017-10-13 01:55:36 +00002062 // C++ P0722:
2063 // A destroying operator delete is a usual deallocation function if
2064 // removing the std::destroying_delete_t parameter and changing the
2065 // first parameter type from T* to void* results in the signature of
2066 // a usual deallocation function.
2067 if (isDestroyingOperatorDelete())
2068 ++UsualParams;
2069
Richard Smithb2f0f052016-10-10 18:54:32 +00002070 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002071 // [...] If class T does not declare such an operator delete but does
2072 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002073 // exactly two parameters, the second of which has type std::size_t (18.1),
2074 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002075 //
2076 // C++17 says a usual deallocation function is one with the signature
2077 // (void* [, size_t] [, std::align_val_t] [, ...])
2078 // and all such functions are usual deallocation functions. It's not clear
2079 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002080 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002081 if (UsualParams < getNumParams() &&
2082 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2083 Context.getSizeType()))
2084 ++UsualParams;
2085
2086 if (UsualParams < getNumParams() &&
2087 getParamDecl(UsualParams)->getType()->isAlignValT())
2088 ++UsualParams;
2089
2090 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002091 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002092
2093 // In C++17 onwards, all potential usual deallocation functions are actual
Eric Fiselier24a2a482019-02-03 03:44:31 +00002094 // usual deallocation functions. Honor this behavior when post-C++14
2095 // deallocation functions are offered as extensions too.
2096 // FIXME(EricWF): Destrying Delete should be a language option. How do we
2097 // handle when destroying delete is used prior to C++17?
2098 if (Context.getLangOpts().CPlusPlus17 ||
2099 Context.getLangOpts().AlignedAllocation ||
2100 isDestroyingOperatorDelete())
Richard Smithb2f0f052016-10-10 18:54:32 +00002101 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002102
2103 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002104 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002105 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
Artem Belevich78929ef2018-09-21 17:29:33 +00002106 bool Result = true;
2107 for (const auto *D : R) {
2108 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2109 if (FD->getNumParams() == 1) {
2110 PreventedBy.push_back(FD);
2111 Result = false;
2112 }
2113 }
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002114 }
Artem Belevich78929ef2018-09-21 17:29:33 +00002115 return Result;
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002116}
2117
Douglas Gregorb139cd52010-05-01 20:49:11 +00002118bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002119 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002120 // A user-declared copy assignment operator X::operator= is a non-static
2121 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002122 // type X, X&, const X&, volatile X& or const volatile X&.
2123 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002124 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002125 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2126 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002127 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002128
Douglas Gregorb139cd52010-05-01 20:49:11 +00002129 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002130 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002131 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002132
Douglas Gregorb139cd52010-05-01 20:49:11 +00002133 ASTContext &Context = getASTContext();
2134 QualType ClassType
2135 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2136 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2137}
2138
Alexis Huntfcaeae42011-05-25 20:50:04 +00002139bool CXXMethodDecl::isMoveAssignmentOperator() const {
2140 // C++0x [class.copy]p19:
2141 // A user-declared move assignment operator X::operator= is a non-static
2142 // non-template member function of class X with exactly one parameter of type
2143 // X&&, const X&&, volatile X&&, or const volatile X&&.
2144 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002145 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2146 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002147 return false;
2148
2149 QualType ParamType = getParamDecl(0)->getType();
2150 if (!isa<RValueReferenceType>(ParamType))
2151 return false;
2152 ParamType = ParamType->getPointeeType();
2153
2154 ASTContext &Context = getASTContext();
2155 QualType ClassType
2156 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2157 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2158}
2159
Anders Carlsson36d87e12009-05-16 23:58:37 +00002160void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002161 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002162 assert(!MD->getParent()->isDependentContext() &&
2163 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002164 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002165
Douglas Gregor832940b2010-03-02 23:58:15 +00002166 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002167}
2168
2169CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002170 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002171 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002172}
2173
2174CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002175 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002176 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002177}
2178
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002179unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002180 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002181 return getASTContext().overridden_methods_size(this);
2182}
2183
Clement Courbet6ecaec82016-07-05 07:49:31 +00002184CXXMethodDecl::overridden_method_range
2185CXXMethodDecl::overridden_methods() const {
2186 if (isa<CXXConstructorDecl>(this))
2187 return overridden_method_range(nullptr, nullptr);
2188 return getASTContext().overridden_methods(this);
2189}
2190
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002191QualType CXXMethodDecl::getThisType(const FunctionProtoType *FPT,
2192 const CXXRecordDecl *Decl) {
2193 ASTContext &C = Decl->getASTContext();
2194 QualType ClassTy = C.getTypeDeclType(Decl);
Anastasia Stulovac61eaa52019-01-28 11:37:49 +00002195 ClassTy = C.getQualifiedType(ClassTy, FPT->getMethodQuals());
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002196 return C.getPointerType(ClassTy);
2197}
2198
Brian Gesiak5488ab42019-01-11 01:54:53 +00002199QualType CXXMethodDecl::getThisType() const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002200 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2201 // If the member function is declared const, the type of this is const X*,
2202 // if the member function is declared volatile, the type of this is
2203 // volatile X*, and if the member function is declared const volatile,
2204 // the type of this is const volatile X*.
Ted Kremenek21475702008-09-05 17:16:31 +00002205 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002206
Mikael Nilsson9d2872d2018-12-13 10:15:27 +00002207 return CXXMethodDecl::getThisType(getType()->getAs<FunctionProtoType>(),
2208 getParent());
Ted Kremenek21475702008-09-05 17:16:31 +00002209}
2210
Eli Friedman71a26d82009-12-06 20:50:05 +00002211bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002212 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002213 // which it was instantiated.
2214 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2215 if (!CheckFn)
2216 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002217
Eli Friedman71a26d82009-12-06 20:50:05 +00002218 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002219 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2220 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002221}
2222
Douglas Gregor355efbb2012-02-17 03:02:34 +00002223bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002224 const CXXRecordDecl *P = getParent();
2225 if (P->isLambda()) {
2226 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2227 if (StaticInvoker == this) return true;
2228 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2229 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2230 }
2231 }
2232 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002233}
2234
Alexis Hunt1d792652011-01-08 20:30:50 +00002235CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2236 TypeSourceInfo *TInfo, bool IsVirtual,
2237 SourceLocation L, Expr *Init,
2238 SourceLocation R,
2239 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002240 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2241 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2242 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002243
Alexis Hunt1d792652011-01-08 20:30:50 +00002244CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2245 FieldDecl *Member,
2246 SourceLocation MemberLoc,
2247 SourceLocation L, Expr *Init,
2248 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002249 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2250 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2251 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002252
Alexis Hunt1d792652011-01-08 20:30:50 +00002253CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2254 IndirectFieldDecl *Member,
2255 SourceLocation MemberLoc,
2256 SourceLocation L, Expr *Init,
2257 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002258 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2259 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2260 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002261
Alexis Hunt1d792652011-01-08 20:30:50 +00002262CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002263 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002264 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002265 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002266 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2267 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002268
Artem Dergachevd3942be2018-12-03 22:15:34 +00002269int64_t CXXCtorInitializer::getID(const ASTContext &Context) const {
Artem Dergachev057647d2018-12-03 22:19:05 +00002270 return Context.getAllocator()
2271 .identifyKnownAlignedObject<CXXCtorInitializer>(this);
Artem Dergachevd3942be2018-12-03 22:15:34 +00002272}
2273
Alexis Hunt1d792652011-01-08 20:30:50 +00002274TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002275 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002276 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002277 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002278 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002279}
2280
Alexis Hunt1d792652011-01-08 20:30:50 +00002281const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002282 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002283 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002284 else
Craig Topper36250ad2014-05-12 05:36:57 +00002285 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002286}
2287
Alexis Hunt1d792652011-01-08 20:30:50 +00002288SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002289 if (isInClassMemberInitializer())
2290 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002291
David Blaikiea81d4102015-01-18 00:12:58 +00002292 if (isAnyMemberInitializer())
2293 return getMemberLocation();
2294
Eugene Zelenko7855e772018-04-03 00:11:50 +00002295 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002296 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002297
Eugene Zelenko7855e772018-04-03 00:11:50 +00002298 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002299}
2300
Alexis Hunt1d792652011-01-08 20:30:50 +00002301SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002302 if (isInClassMemberInitializer()) {
2303 FieldDecl *D = getAnyMember();
2304 if (Expr *I = D->getInClassInitializer())
2305 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002306 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002307 }
2308
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002309 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002310}
2311
Erich Keane9c665062018-08-01 21:02:40 +00002312CXXConstructorDecl::CXXConstructorDecl(
2313 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2314 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2315 bool isExplicitSpecified, bool isInline, bool isImplicitlyDeclared,
2316 bool isConstexpr, InheritedConstructor Inherited)
2317 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2318 SC_None, isInline, isConstexpr, SourceLocation()) {
2319 setNumCtorInitializers(0);
2320 setInheritingConstructor(static_cast<bool>(Inherited));
2321 setImplicit(isImplicitlyDeclared);
2322 if (Inherited)
2323 *getTrailingObjects<InheritedConstructor>() = Inherited;
2324 setExplicitSpecified(isExplicitSpecified);
2325}
2326
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002327void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002328
Richard Smith5179eb72016-06-28 19:03:57 +00002329CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2330 unsigned ID,
2331 bool Inherited) {
2332 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
2333 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2334 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2335 false, false, false, false, InheritedConstructor());
Erich Keane9c665062018-08-01 21:02:40 +00002336 Result->setInheritingConstructor(Inherited);
Richard Smith5179eb72016-06-28 19:03:57 +00002337 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002338}
2339
2340CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00002341CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002342 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002343 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002344 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002345 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00002346 bool isImplicitlyDeclared, bool isConstexpr,
2347 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002348 assert(NameInfo.getName().getNameKind()
2349 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002350 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002351 unsigned Extra =
2352 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
2353 return new (C, RD, Extra) CXXConstructorDecl(
2354 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
2355 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002356}
2357
Richard Smithc2bb8182015-03-24 06:36:48 +00002358CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2359 return CtorInitializers.get(getASTContext().getExternalSource());
2360}
2361
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002362CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2363 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2364 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002365 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002366 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002367
2368 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002369}
2370
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002371bool CXXConstructorDecl::isDefaultConstructor() const {
2372 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002373 // A default constructor for a class X is a constructor of class
2374 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002375 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002376 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002377}
2378
Mike Stump11289f42009-09-09 15:08:12 +00002379bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002380CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002381 return isCopyOrMoveConstructor(TypeQuals) &&
2382 getParamDecl(0)->getType()->isLValueReferenceType();
2383}
2384
2385bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2386 return isCopyOrMoveConstructor(TypeQuals) &&
2387 getParamDecl(0)->getType()->isRValueReferenceType();
2388}
2389
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002390/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002391bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002392 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002393 // A non-template constructor for class X is a copy constructor
2394 // if its first parameter is of type X&, const X&, volatile X& or
2395 // const volatile X&, and either there are no other parameters
2396 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002397 // C++0x [class.copy]p3:
2398 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002399 // first parameter is of type X&&, const X&&, volatile X&&, or
2400 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002401 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002402 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002403 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002404 (getPrimaryTemplate() != nullptr) ||
2405 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002406 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002407
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002408 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002409
2410 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002411 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002412 if (!ParamRefType)
2413 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002414
Douglas Gregorff7028a2009-11-13 23:59:09 +00002415 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002416 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002417
Douglas Gregorff7028a2009-11-13 23:59:09 +00002418 CanQualType PointeeType
2419 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002420 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002421 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002422 if (PointeeType.getUnqualifiedType() != ClassTy)
2423 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002424
John McCall8ccfcb52009-09-24 19:53:00 +00002425 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002426
Douglas Gregorf282a762011-01-21 19:38:21 +00002427 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002428 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002429 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002430}
2431
Anders Carlssond20e7952009-08-28 16:57:08 +00002432bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002433 // C++ [class.conv.ctor]p1:
2434 // A constructor declared without the function-specifier explicit
2435 // that can be called with a single parameter specifies a
2436 // conversion from the type of its first parameter to the type of
2437 // its class. Such a constructor is called a converting
2438 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002439 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002440 return false;
2441
Mike Stump11289f42009-09-09 15:08:12 +00002442 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00002443 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002444 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002445 (getNumParams() > 1 &&
2446 (getParamDecl(1)->hasDefaultArg() ||
2447 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002448}
Douglas Gregor61956c42008-10-31 09:07:45 +00002449
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002450bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002451 if ((getNumParams() < 1) ||
2452 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002453 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002454 return false;
2455
2456 const ParmVarDecl *Param = getParamDecl(0);
2457
2458 ASTContext &Context = getASTContext();
2459 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002460
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002461 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002462 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002463 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2464 if (ParamType.getUnqualifiedType() != ClassTy)
2465 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002466
2467 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002468}
2469
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002470void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002471
Douglas Gregor831c93f2008-11-05 20:51:48 +00002472CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002473CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002474 return new (C, ID)
2475 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2476 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00002477}
2478
2479CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002480CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002481 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002482 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002483 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002484 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002485 assert(NameInfo.getName().getNameKind()
2486 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002487 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002488 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002489 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002490}
2491
Richard Smith5b349582017-10-13 01:55:36 +00002492void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002493 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2494 if (OD && !First->OperatorDelete) {
2495 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002496 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002497 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002498 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002499 }
2500}
2501
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002502void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002503
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002504CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002505CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002506 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002507 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002508 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002509 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002510}
2511
2512CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002513CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002514 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002515 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002516 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002517 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002518 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002519 assert(NameInfo.getName().getNameKind()
2520 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002521 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002522 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002523 isInline, isExplicit, isConstexpr,
2524 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002525}
2526
Douglas Gregord3b672c2012-02-16 01:06:16 +00002527bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2528 return isImplicit() && getParent()->isLambda() &&
2529 getConversionType()->isBlockPointerType();
2530}
2531
Erich Keanec9d29902018-08-01 21:16:54 +00002532LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2533 SourceLocation LangLoc, LanguageIDs lang,
2534 bool HasBraces)
2535 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2536 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2537 setLanguage(lang);
2538 LinkageSpecDeclBits.HasBraces = HasBraces;
2539}
2540
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002541void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002542
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002543LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002544 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002545 SourceLocation ExternLoc,
2546 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002547 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002548 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002549 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002550}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002551
Richard Smithf7981722013-11-22 09:01:48 +00002552LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2553 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002554 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2555 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002556}
2557
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002558void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002559
Douglas Gregor889ceb72009-02-03 19:21:40 +00002560UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2561 SourceLocation L,
2562 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002563 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002564 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002565 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002566 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002567 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002568 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002569 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2570 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002571}
2572
Richard Smithf7981722013-11-22 09:01:48 +00002573UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2574 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002575 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2576 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002577 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002578 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002579}
2580
Sebastian Redla6602e92009-11-23 15:34:23 +00002581NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002582 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002583 return NA->getNamespace();
2584 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2585}
2586
Richard Smith053f6c62014-05-16 23:01:30 +00002587NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2588 SourceLocation StartLoc, SourceLocation IdLoc,
2589 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2590 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002591 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002592 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002593 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002594
Douglas Gregore57e7522012-01-07 09:11:48 +00002595 if (PrevDecl)
2596 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2597}
2598
Douglas Gregor72172e92012-01-05 21:55:30 +00002599NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002600 bool Inline, SourceLocation StartLoc,
2601 SourceLocation IdLoc, IdentifierInfo *Id,
2602 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002603 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2604 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002605}
2606
2607NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002608 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002609 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002610}
2611
Chandler Carruth21c90602015-12-30 03:24:14 +00002612NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2613 if (isFirstDecl())
2614 return this;
2615
2616 return AnonOrFirstNamespaceAndInline.getPointer();
2617}
2618
2619const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2620 if (isFirstDecl())
2621 return this;
2622
2623 return AnonOrFirstNamespaceAndInline.getPointer();
2624}
2625
2626bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2627
Richard Smithd7af8a32014-05-10 01:17:36 +00002628NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2629 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002630}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002631
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002632NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2633 return getPreviousDecl();
2634}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002635
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002636NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2637 return getMostRecentDecl();
2638}
2639
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002640void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002641
Richard Smithf4634362014-09-03 23:11:22 +00002642NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2643 return getNextRedeclaration();
2644}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002645
Richard Smithf4634362014-09-03 23:11:22 +00002646NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2647 return getPreviousDecl();
2648}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002649
Richard Smithf4634362014-09-03 23:11:22 +00002650NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2651 return getMostRecentDecl();
2652}
2653
Mike Stump11289f42009-09-09 15:08:12 +00002654NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002655 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002656 SourceLocation AliasLoc,
2657 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002658 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002659 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002660 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002661 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002662 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002663 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002664 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002665 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002666}
2667
Douglas Gregor72172e92012-01-05 21:55:30 +00002668NamespaceAliasDecl *
2669NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002670 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002671 SourceLocation(), nullptr,
2672 NestedNameSpecifierLoc(),
2673 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002674}
2675
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002676void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002677
Richard Smith5179eb72016-06-28 19:03:57 +00002678UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2679 SourceLocation Loc, UsingDecl *Using,
2680 NamedDecl *Target)
2681 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002682 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002683 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002684 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002685 setImplicit();
2686}
2687
2688UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2689 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002690 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002691
Douglas Gregor72172e92012-01-05 21:55:30 +00002692UsingShadowDecl *
2693UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002694 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002695}
2696
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002697UsingDecl *UsingShadowDecl::getUsingDecl() const {
2698 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002699 while (const auto *NextShadow =
2700 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002701 Shadow = NextShadow;
2702 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2703}
2704
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002705void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002706
2707ConstructorUsingShadowDecl *
2708ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2709 SourceLocation Loc, UsingDecl *Using,
2710 NamedDecl *Target, bool IsVirtual) {
2711 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2712 IsVirtual);
2713}
2714
2715ConstructorUsingShadowDecl *
2716ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2717 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2718}
2719
2720CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2721 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2722}
2723
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002724void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002725
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002726void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2727 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2728 "declaration already in set");
2729 assert(S->getUsingDecl() == this);
2730
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002731 if (FirstUsingShadow.getPointer())
2732 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2733 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002734}
2735
2736void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2737 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2738 "declaration not in set");
2739 assert(S->getUsingDecl() == this);
2740
2741 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2742
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002743 if (FirstUsingShadow.getPointer() == S) {
2744 FirstUsingShadow.setPointer(
2745 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002746 S->UsingOrNextShadow = this;
2747 return;
2748 }
2749
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002750 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002751 while (Prev->UsingOrNextShadow != S)
2752 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2753 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2754 S->UsingOrNextShadow = this;
2755}
2756
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002757UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2758 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002759 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002760 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002761 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002762}
2763
Douglas Gregor72172e92012-01-05 21:55:30 +00002764UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002765 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2766 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2767 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002768}
2769
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002770SourceRange UsingDecl::getSourceRange() const {
2771 SourceLocation Begin = isAccessDeclaration()
2772 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2773 return SourceRange(Begin, getNameInfo().getEndLoc());
2774}
2775
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002776void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002777
2778UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2779 NamedDecl *InstantiatedFrom,
2780 ArrayRef<NamedDecl *> UsingDecls) {
2781 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2782 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2783}
2784
2785UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2786 unsigned NumExpansions) {
2787 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2788 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2789 Result->NumExpansions = NumExpansions;
2790 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2791 for (unsigned I = 0; I != NumExpansions; ++I)
2792 new (Trail + I) NamedDecl*(nullptr);
2793 return Result;
2794}
2795
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002796void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002797
John McCalle61f2ba2009-11-18 02:36:19 +00002798UnresolvedUsingValueDecl *
2799UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2800 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002801 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002802 const DeclarationNameInfo &NameInfo,
2803 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002804 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002805 QualifierLoc, NameInfo,
2806 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002807}
2808
Douglas Gregor72172e92012-01-05 21:55:30 +00002809UnresolvedUsingValueDecl *
2810UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002811 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2812 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002813 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002814 DeclarationNameInfo(),
2815 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002816}
2817
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002818SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2819 SourceLocation Begin = isAccessDeclaration()
2820 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2821 return SourceRange(Begin, getNameInfo().getEndLoc());
2822}
2823
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002824void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002825
John McCalle61f2ba2009-11-18 02:36:19 +00002826UnresolvedUsingTypenameDecl *
2827UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2828 SourceLocation UsingLoc,
2829 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002830 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002831 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002832 DeclarationName TargetName,
2833 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002834 return new (C, DC) UnresolvedUsingTypenameDecl(
2835 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002836 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002837}
2838
Douglas Gregor72172e92012-01-05 21:55:30 +00002839UnresolvedUsingTypenameDecl *
2840UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002841 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002842 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002843 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002844}
2845
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002846void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002847
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002848StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002849 SourceLocation StaticAssertLoc,
2850 Expr *AssertExpr,
2851 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002852 SourceLocation RParenLoc,
2853 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002854 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2855 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002856}
2857
Richard Smithf7981722013-11-22 09:01:48 +00002858StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002859 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002860 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2861 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002862}
2863
Richard Smithbdb84f32016-07-22 23:36:59 +00002864void BindingDecl::anchor() {}
2865
2866BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2867 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002868 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002869}
2870
2871BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002872 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002873}
2874
Richard Smith97fcf4b2016-08-14 23:15:52 +00002875VarDecl *BindingDecl::getHoldingVar() const {
2876 Expr *B = getBinding();
2877 if (!B)
2878 return nullptr;
2879 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2880 if (!DRE)
2881 return nullptr;
2882
2883 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2884 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2885 return VD;
2886}
2887
Richard Smithbdb84f32016-07-22 23:36:59 +00002888void DecompositionDecl::anchor() {}
2889
2890DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2891 SourceLocation StartLoc,
2892 SourceLocation LSquareLoc,
2893 QualType T, TypeSourceInfo *TInfo,
2894 StorageClass SC,
2895 ArrayRef<BindingDecl *> Bindings) {
2896 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2897 return new (C, DC, Extra)
2898 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2899}
2900
2901DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2902 unsigned ID,
2903 unsigned NumBindings) {
2904 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002905 auto *Result = new (C, ID, Extra)
2906 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2907 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002908 // Set up and clean out the bindings array.
2909 Result->NumBindings = NumBindings;
2910 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2911 for (unsigned I = 0; I != NumBindings; ++I)
2912 new (Trail + I) BindingDecl*(nullptr);
2913 return Result;
2914}
2915
Richard Smith7873de02016-08-11 22:25:46 +00002916void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2917 os << '[';
2918 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002919 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00002920 if (Comma)
2921 os << ", ";
2922 B->printName(os);
2923 Comma = true;
2924 }
2925 os << ']';
2926}
2927
Richard Trieub3e902f2018-12-29 02:02:30 +00002928void MSPropertyDecl::anchor() {}
2929
Richard Smithf7981722013-11-22 09:01:48 +00002930MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2931 SourceLocation L, DeclarationName N,
2932 QualType T, TypeSourceInfo *TInfo,
2933 SourceLocation StartL,
2934 IdentifierInfo *Getter,
2935 IdentifierInfo *Setter) {
2936 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2937}
2938
2939MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2940 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002941 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2942 DeclarationName(), QualType(), nullptr,
2943 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002944}
2945
Anders Carlsson6750d162009-03-26 23:46:50 +00002946static const char *getAccessName(AccessSpecifier AS) {
2947 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002948 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002949 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002950 case AS_public:
2951 return "public";
2952 case AS_private:
2953 return "private";
2954 case AS_protected:
2955 return "protected";
2956 }
David Blaikief47fa302012-01-17 02:30:50 +00002957 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002958}
2959
2960const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2961 AccessSpecifier AS) {
2962 return DB << getAccessName(AS);
2963}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002964
2965const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2966 AccessSpecifier AS) {
2967 return DB << getAccessName(AS);
2968}