blob: c43bdfb58f497b6fccf846f6f8b5ae30bc3584c0 [file] [log] [blame]
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001//===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===//
Ted Kremenek21475702008-09-05 17:16:31 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the C++ related Decl classes.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000013
Ted Kremenek21475702008-09-05 17:16:31 +000014#include "clang/AST/DeclCXX.h"
15#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000016#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000017#include "clang/AST/ASTMutationListener.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000018#include "clang/AST/ASTUnresolvedSet.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000019#include "clang/AST/CXXInheritance.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000020#include "clang/AST/DeclBase.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/DeclTemplate.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000022#include "clang/AST/DeclarationName.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000023#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000024#include "clang/AST/ExprCXX.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000025#include "clang/AST/LambdaCapture.h"
26#include "clang/AST/NestedNameSpecifier.h"
Richard Trieue7f7ed22017-02-22 01:11:25 +000027#include "clang/AST/ODRHash.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000028#include "clang/AST/Type.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000029#include "clang/AST/TypeLoc.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000030#include "clang/AST/UnresolvedSet.h"
31#include "clang/Basic/Diagnostic.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000032#include "clang/Basic/IdentifierTable.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000033#include "clang/Basic/LLVM.h"
34#include "clang/Basic/LangOptions.h"
35#include "clang/Basic/OperatorKinds.h"
36#include "clang/Basic/PartialDiagnostic.h"
37#include "clang/Basic/SourceLocation.h"
38#include "clang/Basic/Specifiers.h"
39#include "llvm/ADT/None.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000040#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000041#include "llvm/ADT/SmallVector.h"
42#include "llvm/ADT/iterator_range.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
46#include <algorithm>
47#include <cassert>
48#include <cstddef>
49#include <cstdint>
50
Ted Kremenek21475702008-09-05 17:16:31 +000051using namespace clang;
52
53//===----------------------------------------------------------------------===//
54// Decl Allocation/Deallocation Method Implementations
55//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000056
Eugene Zelenko4a5354f2017-11-14 23:13:32 +000057void AccessSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +000058
Douglas Gregor72172e92012-01-05 21:55:30 +000059AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000060 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000061}
62
Richard Smitha4ba74c2013-08-30 04:46:40 +000063void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
64 ExternalASTSource *Source = C.getExternalSource();
65 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
66 assert(Source && "getFromExternalSource with no external source");
67
68 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
69 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
70 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
71 Impl.Decls.setLazy(false);
72}
73
John McCall67da35c2010-02-04 22:26:26 +000074CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Nico Weber6a6376b2016-02-19 01:52:46 +000075 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
76 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Richard Smithb6070db2018-04-05 18:55:37 +000077 Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true),
78 HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000079 HasPrivateFields(false), HasProtectedFields(false),
80 HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false),
81 HasOnlyCMembers(true), HasInClassInitializer(false),
82 HasUninitializedReferenceMember(false), HasUninitializedFields(false),
Richard Smith12e79312016-05-13 06:47:56 +000083 HasInheritedConstructor(false), HasInheritedAssignment(false),
Richard Smith96cd6712017-08-16 01:49:53 +000084 NeedOverloadResolutionForCopyConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000085 NeedOverloadResolutionForMoveConstructor(false),
86 NeedOverloadResolutionForMoveAssignment(false),
87 NeedOverloadResolutionForDestructor(false),
Richard Smith96cd6712017-08-16 01:49:53 +000088 DefaultedCopyConstructorIsDeleted(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000089 DefaultedMoveConstructorIsDeleted(false),
90 DefaultedMoveAssignmentIsDeleted(false),
91 DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All),
Akira Hatanaka02914dc2018-02-05 20:23:22 +000092 HasTrivialSpecialMembersForCall(SMF_All),
93 DeclaredNonTrivialSpecialMembers(0),
94 DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true),
Nico Weber6a6376b2016-02-19 01:52:46 +000095 HasConstexprNonCopyMoveConstructor(false),
Nico Weber72c57f42016-02-24 20:58:14 +000096 HasDefaultedDefaultConstructor(false),
Nico Weber6a6376b2016-02-19 01:52:46 +000097 DefaultedDefaultConstructorIsConstexpr(true),
98 HasConstexprDefaultConstructor(false),
99 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
100 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smithdf054d32017-02-25 23:53:05 +0000101 ImplicitCopyConstructorCanHaveConstParamForVBase(true),
102 ImplicitCopyConstructorCanHaveConstParamForNonVBase(true),
Nico Weber6a6376b2016-02-19 01:52:46 +0000103 ImplicitCopyAssignmentHasConstParam(true),
104 HasDeclaredCopyConstructorWithConstParam(false),
105 HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000106 IsParsingBaseSpecifiers(false), HasODRHash(false), Definition(D) {}
John McCall67da35c2010-02-04 22:26:26 +0000107
Benjamin Kramer300c0632012-07-04 17:03:33 +0000108CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
109 return Bases.get(Definition->getASTContext().getExternalSource());
110}
111
112CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
113 return VBases.get(Definition->getASTContext().getExternalSource());
114}
115
Richard Smith053f6c62014-05-16 23:01:30 +0000116CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
117 DeclContext *DC, SourceLocation StartLoc,
118 SourceLocation IdLoc, IdentifierInfo *Id,
119 CXXRecordDecl *PrevDecl)
120 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
121 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Eugene Zelenko4a5354f2017-11-14 23:13:32 +0000122 : nullptr) {}
Douglas Gregorb6acda02008-11-12 23:21:09 +0000123
Jay Foad39c79802011-01-12 09:06:06 +0000124CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +0000125 DeclContext *DC, SourceLocation StartLoc,
126 SourceLocation IdLoc, IdentifierInfo *Id,
Richard Smithbecb92d2017-10-10 22:33:17 +0000127 CXXRecordDecl *PrevDecl,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000128 bool DelayTypeCreation) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000129 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id,
130 PrevDecl);
Erich Keanef92f31c2018-08-01 20:48:16 +0000131 R->setMayHaveOutOfDateDef(C.getLangOpts().Modules);
Mike Stump11289f42009-09-09 15:08:12 +0000132
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000133 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000134 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000135 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000136 return R;
137}
138
Richard Smith053f6c62014-05-16 23:01:30 +0000139CXXRecordDecl *
140CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
141 TypeSourceInfo *Info, SourceLocation Loc,
142 bool Dependent, bool IsGeneric,
143 LambdaCaptureDefault CaptureDefault) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000144 auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
145 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000146 R->setBeingDefined(true);
Richard Smith64c06302014-05-22 23:19:02 +0000147 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000148 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000149 CaptureDefault);
Erich Keanef92f31c2018-08-01 20:48:16 +0000150 R->setMayHaveOutOfDateDef(false);
James Dennett8f60cdd2013-09-05 17:46:21 +0000151 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000152 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000153 return R;
154}
155
Douglas Gregor72172e92012-01-05 21:55:30 +0000156CXXRecordDecl *
157CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Eugene Zelenko7855e772018-04-03 00:11:50 +0000158 auto *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000159 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000160 nullptr, nullptr);
Erich Keanef92f31c2018-08-01 20:48:16 +0000161 R->setMayHaveOutOfDateDef(false);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000162 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000163}
164
Richard Smithb6070db2018-04-05 18:55:37 +0000165/// Determine whether a class has a repeated base class. This is intended for
166/// use when determining if a class is standard-layout, so makes no attempt to
167/// handle virtual bases.
168static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) {
169 llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes;
170 SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD};
171 while (!WorkList.empty()) {
172 const CXXRecordDecl *RD = WorkList.pop_back_val();
173 for (const CXXBaseSpecifier &BaseSpec : RD->bases()) {
174 if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) {
175 if (!SeenBaseTypes.insert(B).second)
176 return true;
177 WorkList.push_back(B);
178 }
179 }
180 }
181 return false;
182}
183
Mike Stump11289f42009-09-09 15:08:12 +0000184void
Craig Toppere6337e12015-12-25 00:36:02 +0000185CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
186 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000187 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000188
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000189 if (!data().Bases.isOffset() && data().NumBases > 0)
190 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000191
Craig Toppere6337e12015-12-25 00:36:02 +0000192 if (NumBases) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +0000193 if (!C.getLangOpts().CPlusPlus17) {
Richard Smith872307e2016-03-08 22:17:41 +0000194 // C++ [dcl.init.aggr]p1:
195 // An aggregate is [...] a class with [...] no base classes [...].
196 data().Aggregate = false;
197 }
Richard Smithaed32b42011-10-18 20:08:55 +0000198
199 // C++ [class]p4:
200 // A POD-struct is an aggregate class...
201 data().PlainOldData = false;
202 }
203
Anders Carlssonabb20e62010-03-29 05:13:12 +0000204 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000205 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Fangrui Song6907ce22018-07-30 19:24:48 +0000206
Anders Carlssonabb20e62010-03-29 05:13:12 +0000207 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000208 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000209
Craig Toppere6337e12015-12-25 00:36:02 +0000210 data().Bases = new(C) CXXBaseSpecifier [NumBases];
211 data().NumBases = NumBases;
212 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000213 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000214 // Keep track of inherited vbases for this base class.
215 const CXXBaseSpecifier *Base = Bases[i];
216 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000217 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000218 if (BaseType->isDependentType())
219 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +0000220 auto *BaseClassDecl =
221 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000222
Richard Smithb6070db2018-04-05 18:55:37 +0000223 // C++2a [class]p7:
224 // A standard-layout class is a class that:
225 // [...]
226 // -- has all non-static data members and bit-fields in the class and
227 // its base classes first declared in the same class
228 if (BaseClassDecl->data().HasBasesWithFields ||
229 !BaseClassDecl->field_empty()) {
230 if (data().HasBasesWithFields)
231 // Two bases have members or bit-fields: not standard-layout.
Chandler Carruth583edf82011-04-30 10:07:30 +0000232 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000233 data().HasBasesWithFields = true;
234 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000235
Richard Smithb6070db2018-04-05 18:55:37 +0000236 // C++11 [class]p7:
237 // A standard-layout class is a class that:
238 // -- [...] has [...] at most one base class with non-static data
239 // members
240 if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers ||
241 BaseClassDecl->hasDirectFields()) {
242 if (data().HasBasesWithNonStaticDataMembers)
243 data().IsCXX11StandardLayout = false;
244 data().HasBasesWithNonStaticDataMembers = true;
245 }
246
247 if (!BaseClassDecl->isEmpty()) {
David Majnemer5cfda6f2016-05-22 05:34:26 +0000248 // C++14 [meta.unary.prop]p4:
249 // T is a class type [...] with [...] no base class B for which
250 // is_empty<B>::value is false.
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000251 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000252 }
Richard Smithb6070db2018-04-05 18:55:37 +0000253
Richard Smith872307e2016-03-08 22:17:41 +0000254 // C++1z [dcl.init.agg]p1:
255 // An aggregate is a class with [...] no private or protected base classes
256 if (Base->getAccessSpecifier() != AS_public)
257 data().Aggregate = false;
258
Douglas Gregor11c024b2010-09-28 20:50:54 +0000259 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000260 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 // polymorphic class.
Richard Smithc00e4462018-06-14 20:03:22 +0000262 if (BaseClassDecl->isPolymorphic()) {
Douglas Gregor11c024b2010-09-28 20:50:54 +0000263 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000264
Richard Smithc00e4462018-06-14 20:03:22 +0000265 // An aggregate is a class with [...] no virtual functions.
266 data().Aggregate = false;
267 }
268
Chandler Carruthb1963742011-04-30 09:17:45 +0000269 // C++0x [class]p7:
270 // A standard-layout class is a class that: [...]
271 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000272 if (!BaseClassDecl->isStandardLayout())
273 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000274 if (!BaseClassDecl->isCXX11StandardLayout())
275 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000276
Chandler Carruthe71d0622011-04-24 02:49:34 +0000277 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000278 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000279 data().HasNonLiteralTypeFieldsOrBases = true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000280
Anders Carlssonabb20e62010-03-29 05:13:12 +0000281 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000282 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000283 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000284 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000285 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000286
287 // C++11 [class.copy]p8:
288 // The implicitly-declared copy constructor for a class X will have
289 // the form 'X::X(const X&)' if each [...] virtual base class B of X
290 // has a copy constructor whose first parameter is of type
291 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000292 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000293 if (!VBaseDecl->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +0000294 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Richard Smith872307e2016-03-08 22:17:41 +0000295
296 // C++1z [dcl.init.agg]p1:
297 // An aggregate is a class with [...] no virtual base classes
298 data().Aggregate = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000299 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000300 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000301
302 if (Base->isVirtual()) {
303 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000304 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000305 VBases.push_back(Base);
306
David Majnemer5cfda6f2016-05-22 05:34:26 +0000307 // C++14 [meta.unary.prop] is_empty:
308 // T is a class type, but not a union type, with ... no virtual base
309 // classes
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000310 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000311
Richard Smith872307e2016-03-08 22:17:41 +0000312 // C++1z [dcl.init.agg]p1:
313 // An aggregate is a class with [...] no virtual base classes
314 data().Aggregate = false;
315
Richard Smith328aae52012-11-30 05:11:39 +0000316 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
317 // A [default constructor, copy/move constructor, or copy/move assignment
318 // operator for a class X] is trivial [...] if:
319 // -- class X has [...] no virtual base classes
320 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000321 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000322
323 // C++0x [class]p7:
324 // A standard-layout class is a class that: [...]
325 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000326 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000327 data().IsCXX11StandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000328
329 // C++11 [dcl.constexpr]p4:
330 // In the definition of a constexpr constructor [...]
331 // -- the class shall not have any virtual base classes
332 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000333
334 // C++1z [class.copy]p8:
335 // The implicitly-declared copy constructor for a class X will have
336 // the form 'X::X(const X&)' if each potentially constructed subobject
337 // has a copy constructor whose first parameter is of type
338 // 'const B&' or 'const volatile B&' [...]
339 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
340 data().ImplicitCopyConstructorCanHaveConstParamForVBase = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000341 } else {
342 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000343 // A default constructor is trivial [...] if:
344 // -- all the direct base classes of its class have trivial default
345 // constructors.
346 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000347 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
348
Chandler Carruthad7d4042011-04-23 23:10:33 +0000349 // C++0x [class.copy]p13:
350 // A copy/move constructor for class X is trivial if [...]
351 // [...]
352 // -- the constructor selected to copy/move each direct base class
353 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000354 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000355 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000356
357 if (!BaseClassDecl->hasTrivialCopyConstructorForCall())
358 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
359
Richard Smith6b02d462012-12-08 08:32:28 +0000360 // If the base class doesn't have a simple move constructor, we'll eagerly
361 // declare it and perform overload resolution to determine which function
362 // it actually calls. If it does have a simple move constructor, this
363 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000364 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000365 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000366
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000367 if (!BaseClassDecl->hasTrivialMoveConstructorForCall())
368 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
369
Chandler Carruthad7d4042011-04-23 23:10:33 +0000370 // C++0x [class.copy]p27:
371 // A copy/move assignment operator for class X is trivial if [...]
372 // [...]
373 // -- the assignment operator selected to copy/move each direct base
374 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000375 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000376 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000377 // If the base class doesn't have a simple move assignment, we'll eagerly
378 // declare it and perform overload resolution to determine which function
379 // it actually calls. If it does have a simple move assignment, this
380 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000381 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000382 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000383
384 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000385 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000386 // requirements of a constexpr constructor, the implicitly-defined
387 // default constructor is constexpr.
388 if (!BaseClassDecl->hasConstexprDefaultConstructor())
389 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smithdf054d32017-02-25 23:53:05 +0000390
391 // C++1z [class.copy]p8:
392 // The implicitly-declared copy constructor for a class X will have
393 // the form 'X::X(const X&)' if each potentially constructed subobject
394 // has a copy constructor whose first parameter is of type
395 // 'const B&' or 'const volatile B&' [...]
396 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
397 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000398 }
Richard Smith92f241f2012-12-08 02:53:02 +0000399
Douglas Gregor11c024b2010-09-28 20:50:54 +0000400 // C++ [class.ctor]p3:
401 // A destructor is trivial if all the direct base classes of its class
402 // have trivial destructors.
403 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000404 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000405
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000406 if (!BaseClassDecl->hasTrivialDestructorForCall())
407 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
408
Richard Smith561fb152012-02-25 07:33:38 +0000409 if (!BaseClassDecl->hasIrrelevantDestructor())
410 data().HasIrrelevantDestructor = false;
411
Richard Smith1c33fe82012-11-28 06:23:12 +0000412 // C++11 [class.copy]p18:
413 // The implicitly-declared copy assignment oeprator for a class X will
414 // have the form 'X& X::operator=(const X&)' if each direct base class B
415 // of X has a copy assignment operator whose parameter is of type 'const
416 // B&', 'const volatile B&', or 'B' [...]
417 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
418 data().ImplicitCopyAssignmentHasConstParam = false;
419
John McCall31168b02011-06-15 23:02:42 +0000420 // A class has an Objective-C object member if... or any of its bases
421 // has an Objective-C object member.
422 if (BaseClassDecl->hasObjectMember())
423 setHasObjectMember(true);
Fangrui Song6907ce22018-07-30 19:24:48 +0000424
Fariborz Jahanian78652202013-01-25 23:57:05 +0000425 if (BaseClassDecl->hasVolatileMember())
426 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000427
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000428 if (BaseClassDecl->getArgPassingRestrictions() ==
429 RecordDecl::APK_CanNeverPassInRegs)
430 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
431
Douglas Gregor61226d32011-05-13 01:05:07 +0000432 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000433 if (BaseClassDecl->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000434 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000435 data().NeedOverloadResolutionForCopyConstructor = true;
436 }
Richard Smith593f9932012-12-08 02:01:17 +0000437
438 if (BaseClassDecl->hasUninitializedReferenceMember())
439 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000440
Nico Weber6a6376b2016-02-19 01:52:46 +0000441 if (!BaseClassDecl->allowConstDefaultInit())
442 data().HasUninitializedFields = true;
443
Richard Smith6b02d462012-12-08 08:32:28 +0000444 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000445 }
Richard Smithb6070db2018-04-05 18:55:37 +0000446
447 // C++2a [class]p7:
448 // A class S is a standard-layout class if it:
449 // -- has at most one base class subobject of any given type
450 //
451 // Note that we only need to check this for classes with more than one base
452 // class. If there's only one base class, and it's standard layout, then
453 // we know there are no repeated base classes.
454 if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this))
455 data().IsStandardLayout = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000456
David Majnemer5ef4fe72014-06-13 06:43:46 +0000457 if (VBases.empty()) {
458 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000459 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000460 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000461
462 // Create base specifier for any direct or indirect virtual bases.
463 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
464 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000465 for (int I = 0, E = VBases.size(); I != E; ++I) {
466 QualType Type = VBases[I]->getType();
467 if (!Type->isDependentType())
468 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000469 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000470 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000471
472 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000473}
474
Richard Trieufd1acbb2017-04-11 21:31:00 +0000475unsigned CXXRecordDecl::getODRHash() const {
476 assert(hasDefinition() && "ODRHash only for records with definitions");
Richard Trieue7f7ed22017-02-22 01:11:25 +0000477
Richard Trieufd1acbb2017-04-11 21:31:00 +0000478 // Previously calculated hash is stored in DefinitionData.
479 if (DefinitionData->HasODRHash)
480 return DefinitionData->ODRHash;
481
482 // Only calculate hash on first call of getODRHash per record.
Richard Trieue7f7ed22017-02-22 01:11:25 +0000483 ODRHash Hash;
Richard Trieufd1acbb2017-04-11 21:31:00 +0000484 Hash.AddCXXRecordDecl(getDefinition());
485 DefinitionData->HasODRHash = true;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000486 DefinitionData->ODRHash = Hash.CalculateHash();
Richard Trieufd1acbb2017-04-11 21:31:00 +0000487
488 return DefinitionData->ODRHash;
Richard Trieue7f7ed22017-02-22 01:11:25 +0000489}
Richard Trieub6adf542017-02-18 02:09:28 +0000490
Richard Smith6b02d462012-12-08 08:32:28 +0000491void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
492 // C++11 [class.copy]p11:
493 // A defaulted copy/move constructor for a class X is defined as
494 // deleted if X has:
495 // -- a direct or virtual base class B that cannot be copied/moved [...]
496 // -- a non-static data member of class type M (or array thereof)
497 // that cannot be copied or moved [...]
Richard Smith96cd6712017-08-16 01:49:53 +0000498 if (!Subobj->hasSimpleCopyConstructor())
499 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000500 if (!Subobj->hasSimpleMoveConstructor())
501 data().NeedOverloadResolutionForMoveConstructor = true;
502
503 // C++11 [class.copy]p23:
504 // A defaulted copy/move assignment operator for a class X is defined as
505 // deleted if X has:
506 // -- a direct or virtual base class B that cannot be copied/moved [...]
507 // -- a non-static data member of class type M (or array thereof)
508 // that cannot be copied or moved [...]
509 if (!Subobj->hasSimpleMoveAssignment())
510 data().NeedOverloadResolutionForMoveAssignment = true;
511
512 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
513 // A defaulted [ctor or dtor] for a class X is defined as
514 // deleted if X has:
515 // -- any direct or virtual base class [...] has a type with a destructor
516 // that is deleted or inaccessible from the defaulted [ctor or dtor].
517 // -- any non-static data member has a type with a destructor
518 // that is deleted or inaccessible from the defaulted [ctor or dtor].
519 if (!Subobj->hasSimpleDestructor()) {
Richard Smith96cd6712017-08-16 01:49:53 +0000520 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000521 data().NeedOverloadResolutionForMoveConstructor = true;
522 data().NeedOverloadResolutionForDestructor = true;
523 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000524}
525
Douglas Gregord2e6a452010-01-14 17:47:39 +0000526bool CXXRecordDecl::hasAnyDependentBases() const {
527 if (!isDependentContext())
528 return false;
529
Benjamin Kramer6e4f6e12015-07-25 15:07:25 +0000530 return !forallBases([](const CXXRecordDecl *) { return true; });
Douglas Gregord2e6a452010-01-14 17:47:39 +0000531}
532
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000533bool CXXRecordDecl::isTriviallyCopyable() const {
534 // C++0x [class]p5:
535 // A trivially copyable class is a class that:
536 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000537 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000538 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000539 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000540 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000541 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000542 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000543 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000544 // -- has a trivial destructor.
545 if (!hasTrivialDestructor()) return false;
546
547 return true;
548}
549
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000550void CXXRecordDecl::markedVirtualFunctionPure() {
Fangrui Song6907ce22018-07-30 19:24:48 +0000551 // C++ [class.abstract]p2:
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000552 // A class is abstract if it has at least one pure virtual function.
553 data().Abstract = true;
554}
555
Richard Smithb6070db2018-04-05 18:55:37 +0000556bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType(
557 ASTContext &Ctx, const CXXRecordDecl *XFirst) {
558 if (!getNumBases())
559 return false;
560
561 llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases;
562 llvm::SmallPtrSet<const CXXRecordDecl*, 8> M;
563 SmallVector<const CXXRecordDecl*, 8> WorkList;
564
565 // Visit a type that we have determined is an element of M(S).
566 auto Visit = [&](const CXXRecordDecl *RD) -> bool {
567 RD = RD->getCanonicalDecl();
568
569 // C++2a [class]p8:
570 // A class S is a standard-layout class if it [...] has no element of the
571 // set M(S) of types as a base class.
572 //
573 // If we find a subobject of an empty type, it might also be a base class,
574 // so we'll need to walk the base classes to check.
575 if (!RD->data().HasBasesWithFields) {
576 // Walk the bases the first time, stopping if we find the type. Build a
577 // set of them so we don't need to walk them again.
578 if (Bases.empty()) {
579 bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool {
580 Base = Base->getCanonicalDecl();
581 if (RD == Base)
582 return false;
583 Bases.insert(Base);
584 return true;
585 });
586 if (RDIsBase)
587 return true;
588 } else {
589 if (Bases.count(RD))
590 return true;
591 }
592 }
593
594 if (M.insert(RD).second)
595 WorkList.push_back(RD);
596 return false;
597 };
598
599 if (Visit(XFirst))
600 return true;
601
602 while (!WorkList.empty()) {
603 const CXXRecordDecl *X = WorkList.pop_back_val();
604
605 // FIXME: We don't check the bases of X. That matches the standard, but
606 // that sure looks like a wording bug.
607
608 // -- If X is a non-union class type with a non-static data member
609 // [recurse to] the first non-static data member of X
610 // -- If X is a union type, [recurse to union members]
611 for (auto *FD : X->fields()) {
612 // FIXME: Should we really care about the type of the first non-static
613 // data member of a non-union if there are preceding unnamed bit-fields?
614 if (FD->isUnnamedBitfield())
615 continue;
616
617 // -- If X is n array type, [visit the element type]
618 QualType T = Ctx.getBaseElementType(FD->getType());
619 if (auto *RD = T->getAsCXXRecordDecl())
620 if (Visit(RD))
621 return true;
622
623 if (!X->isUnion())
624 break;
625 }
626 }
627
628 return false;
629}
630
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000631void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000632 if (!D->isImplicit() &&
633 !isa<FieldDecl>(D) &&
634 !isa<IndirectFieldDecl>(D) &&
635 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
636 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
637 data().HasOnlyCMembers = false;
638
639 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000640 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000641 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000642
Eugene Zelenko7855e772018-04-03 00:11:50 +0000643 auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000644 if (FunTmpl)
645 D = FunTmpl->getTemplatedDecl();
Richard Smith5179eb72016-06-28 19:03:57 +0000646
647 // FIXME: Pass NamedDecl* to addedMember?
648 Decl *DUnderlying = D;
649 if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) {
650 DUnderlying = ND->getUnderlyingDecl();
Eugene Zelenko7855e772018-04-03 00:11:50 +0000651 if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying))
Richard Smith5179eb72016-06-28 19:03:57 +0000652 DUnderlying = UnderlyingFunTmpl->getTemplatedDecl();
653 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000654
Eugene Zelenko7855e772018-04-03 00:11:50 +0000655 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregora832d3e2010-09-28 19:45:33 +0000656 if (Method->isVirtual()) {
657 // C++ [dcl.init.aggr]p1:
658 // An aggregate is an array or a class with [...] no virtual functions.
659 data().Aggregate = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000660
Douglas Gregora832d3e2010-09-28 19:45:33 +0000661 // C++ [class]p4:
662 // A POD-struct is an aggregate class...
663 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000664
David Majnemer5cfda6f2016-05-22 05:34:26 +0000665 // C++14 [meta.unary.prop]p4:
666 // T is a class type [...] with [...] no virtual member functions...
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000667 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000668
669 // C++ [class.virtual]p1:
Fangrui Song6907ce22018-07-30 19:24:48 +0000670 // A class that declares or inherits a virtual function is called a
Douglas Gregor11c024b2010-09-28 20:50:54 +0000671 // polymorphic class.
672 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000673
Richard Smith328aae52012-11-30 05:11:39 +0000674 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
675 // A [default constructor, copy/move constructor, or copy/move
676 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000677 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000678 data().HasTrivialSpecialMembers &= SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000679 data().HasTrivialSpecialMembersForCall &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000680
Chandler Carruthb1963742011-04-30 09:17:45 +0000681 // C++0x [class]p7:
682 // A standard-layout class is a class that: [...]
683 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000684 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000685 data().IsCXX11StandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000686 }
687 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000688
Richard Smith1c33fe82012-11-28 06:23:12 +0000689 // Notify the listener if an implicit member was added after the definition
690 // was completed.
691 if (!isBeingDefined() && D->isImplicit())
692 if (ASTMutationListener *L = getASTMutationListener())
693 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000694
Richard Smith328aae52012-11-30 05:11:39 +0000695 // The kind of special member this declaration is, if any.
696 unsigned SMKind = 0;
697
Richard Smith1c33fe82012-11-28 06:23:12 +0000698 // Handle constructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000699 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000700 if (!Constructor->isImplicit()) {
701 // Note that we have a user-declared constructor.
702 data().UserDeclaredConstructor = true;
703
704 // C++ [class]p4:
705 // A POD-struct is an aggregate class [...]
706 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
707 // type is technically an aggregate in C++0x since it wouldn't be in 03.
708 data().PlainOldData = false;
709 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000710
Alexis Hunt88c75c32011-05-09 21:45:35 +0000711 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000712 SMKind |= SMF_DefaultConstructor;
713
Richard Smith5179eb72016-06-28 19:03:57 +0000714 if (Constructor->isUserProvided())
Alexis Huntea6f0322011-05-11 22:34:38 +0000715 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000716 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000717 data().HasConstexprDefaultConstructor = true;
Nico Weber72c57f42016-02-24 20:58:14 +0000718 if (Constructor->isDefaulted())
719 data().HasDefaultedDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000720 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000721
Chandler Carruthad7d4042011-04-23 23:10:33 +0000722 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000723 unsigned Quals;
724 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000725 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000726
727 if (Quals & Qualifiers::Const)
728 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000729 } else if (Constructor->isMoveConstructor())
730 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000731 }
Eric Fiselier283d8d42016-12-03 01:26:47 +0000732
Eric Fiselier283d8d42016-12-03 01:26:47 +0000733 // C++11 [dcl.init.aggr]p1: DR1518
Richard Smith505ef812016-12-21 01:57:02 +0000734 // An aggregate is an array or a class with no user-provided, explicit, or
735 // inherited constructors
736 if (Constructor->isUserProvided() || Constructor->isExplicit())
Eric Fiselier283d8d42016-12-03 01:26:47 +0000737 data().Aggregate = false;
Richard Smith5179eb72016-06-28 19:03:57 +0000738 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000739
Richard Smith5179eb72016-06-28 19:03:57 +0000740 // Handle constructors, including those inherited from base classes.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000741 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000742 // Record if we see any constexpr constructors which are neither copy
743 // nor move constructors.
Richard Smith5179eb72016-06-28 19:03:57 +0000744 // C++1z [basic.types]p10:
745 // [...] has at least one constexpr constructor or constructor template
746 // (possibly inherited from a base class) that is not a copy or move
747 // constructor [...]
Richard Smith1c33fe82012-11-28 06:23:12 +0000748 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000749 data().HasConstexprNonCopyMoveConstructor = true;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000750 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000751
Richard Smith1c33fe82012-11-28 06:23:12 +0000752 // Handle destructors.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000753 if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000754 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000755
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000756 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000757 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000758 // If the destructor is explicitly defaulted and not trivial or not public
759 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
760 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000761
Richard Smith1c33fe82012-11-28 06:23:12 +0000762 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000763 // A destructor is trivial if [...] the destructor is not virtual.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000764 if (DD->isVirtual()) {
Richard Smith328aae52012-11-30 05:11:39 +0000765 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000766 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
767 }
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000768 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000769
770 // Handle member functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000771 if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000772 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000773 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000774
Eugene Zelenko7855e772018-04-03 00:11:50 +0000775 const auto *ParamTy =
776 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
Richard Smith1c33fe82012-11-28 06:23:12 +0000777 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
778 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000779 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000780
Richard Smith328aae52012-11-30 05:11:39 +0000781 if (Method->isMoveAssignmentOperator())
782 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000783
Douglas Gregor457104e2010-09-29 04:25:11 +0000784 // Keep the list of conversion functions up-to-date.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000785 if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000786 // FIXME: We use the 'unsafe' accessor for the access specifier here,
787 // because Sema may not have set it yet. That's really just a misdesign
788 // in Sema. However, LLDB *will* have set the access specifier correctly,
789 // and adds declarations after the class is technically completed,
790 // so completeDefinition()'s overriding of the access specifiers doesn't
791 // work.
792 AccessSpecifier AS = Conversion->getAccessUnsafe();
793
Richard Smith328aae52012-11-30 05:11:39 +0000794 if (Conversion->getPrimaryTemplate()) {
795 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000796 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000797 ASTContext &Ctx = getASTContext();
798 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
799 NamedDecl *Primary =
800 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
801 if (Primary->getPreviousDecl())
802 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
803 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000804 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000805 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000806 }
807 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000808
Richard Smith328aae52012-11-30 05:11:39 +0000809 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000810 // If this is the first declaration of a special member, we no longer have
811 // an implicit trivial special member.
812 data().HasTrivialSpecialMembers &=
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000813 data().DeclaredSpecialMembers | ~SMKind;
814 data().HasTrivialSpecialMembersForCall &=
815 data().DeclaredSpecialMembers | ~SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000816
817 if (!Method->isImplicit() && !Method->isUserProvided()) {
818 // This method is user-declared but not user-provided. We can't work out
819 // whether it's trivial yet (not until we get to the end of the class).
820 // We'll handle this method in finishedDefaultedOrDeletedMember.
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000821 } else if (Method->isTrivial()) {
Richard Smith92f241f2012-12-08 02:53:02 +0000822 data().HasTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000823 data().HasTrivialSpecialMembersForCall |= SMKind;
824 } else if (Method->isTrivialForCall()) {
825 data().HasTrivialSpecialMembersForCall |= SMKind;
Richard Smith92f241f2012-12-08 02:53:02 +0000826 data().DeclaredNonTrivialSpecialMembers |= SMKind;
Akira Hatanaka02914dc2018-02-05 20:23:22 +0000827 } else {
828 data().DeclaredNonTrivialSpecialMembers |= SMKind;
829 // If this is a user-provided function, do not set
830 // DeclaredNonTrivialSpecialMembersForCall here since we don't know
831 // yet whether the method would be considered non-trivial for the
832 // purpose of calls (attribute "trivial_abi" can be dropped from the
833 // class later, which can change the special method's triviality).
834 if (!Method->isUserProvided())
835 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
836 }
Richard Smith92f241f2012-12-08 02:53:02 +0000837
Richard Smith328aae52012-11-30 05:11:39 +0000838 // Note when we have declared a declared special member, and suppress the
839 // implicit declaration of this special member.
840 data().DeclaredSpecialMembers |= SMKind;
841
842 if (!Method->isImplicit()) {
843 data().UserDeclaredSpecialMembers |= SMKind;
844
845 // C++03 [class]p4:
846 // A POD-struct is an aggregate class that has [...] no user-defined
847 // copy assignment operator and no user-defined destructor.
848 //
849 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
850 // aggregates could not have any constructors, clear it even for an
851 // explicitly defaulted or deleted constructor.
852 // type is technically an aggregate in C++0x since it wouldn't be in 03.
853 //
854 // Also, a user-declared move assignment operator makes a class non-POD.
855 // This is an extension in C++03.
856 data().PlainOldData = false;
857 }
Richard Smith328aae52012-11-30 05:11:39 +0000858 }
859
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000860 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000861 }
Richard Smith328aae52012-11-30 05:11:39 +0000862
Douglas Gregora832d3e2010-09-28 19:45:33 +0000863 // Handle non-static data members.
Eugene Zelenko7855e772018-04-03 00:11:50 +0000864 if (const auto *Field = dyn_cast<FieldDecl>(D)) {
Richard Smithb5a317f2018-05-07 06:43:30 +0000865 ASTContext &Context = getASTContext();
866
Richard Smithb6070db2018-04-05 18:55:37 +0000867 // C++2a [class]p7:
868 // A standard-layout class is a class that:
869 // [...]
870 // -- has all non-static data members and bit-fields in the class and
871 // its base classes first declared in the same class
872 if (data().HasBasesWithFields)
873 data().IsStandardLayout = false;
874
Douglas Gregor556e5862011-10-10 17:22:13 +0000875 // C++ [class.bit]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000876 // A declaration for a bit-field that omits the identifier declares an
877 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
Douglas Gregor556e5862011-10-10 17:22:13 +0000878 // initialized.
Richard Smithb5a317f2018-05-07 06:43:30 +0000879 if (Field->isUnnamedBitfield()) {
880 // C++ [meta.unary.prop]p4: [LWG2358]
881 // T is a class type [...] with [...] no unnamed bit-fields of non-zero
882 // length
883 if (data().Empty && !Field->isZeroLengthBitField(Context) &&
884 Context.getLangOpts().getClangABICompat() >
885 LangOptions::ClangABI::Ver6)
886 data().Empty = false;
Douglas Gregor556e5862011-10-10 17:22:13 +0000887 return;
Richard Smithb5a317f2018-05-07 06:43:30 +0000888 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000889
Richard Smithb6070db2018-04-05 18:55:37 +0000890 // C++11 [class]p7:
891 // A standard-layout class is a class that:
892 // -- either has no non-static data members in the most derived class
893 // [...] or has no base classes with non-static data members
894 if (data().HasBasesWithNonStaticDataMembers)
895 data().IsCXX11StandardLayout = false;
896
Douglas Gregora832d3e2010-09-28 19:45:33 +0000897 // C++ [dcl.init.aggr]p1:
898 // An aggregate is an array or a class (clause 9) with [...] no
899 // private or protected non-static data members (clause 11).
900 //
Fangrui Song6907ce22018-07-30 19:24:48 +0000901 // A POD must be an aggregate.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000902 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
903 data().Aggregate = false;
904 data().PlainOldData = false;
905 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000906
Richard Smithb6070db2018-04-05 18:55:37 +0000907 // Track whether this is the first field. We use this when checking
908 // whether the class is standard-layout below.
909 bool IsFirstField = !data().HasPrivateFields &&
910 !data().HasProtectedFields && !data().HasPublicFields;
911
Chandler Carruthb1963742011-04-30 09:17:45 +0000912 // C++0x [class]p7:
913 // A standard-layout class is a class that:
914 // [...]
915 // -- has the same access control for all non-static data members,
916 switch (D->getAccess()) {
917 case AS_private: data().HasPrivateFields = true; break;
918 case AS_protected: data().HasProtectedFields = true; break;
919 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000920 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000921 };
922 if ((data().HasPrivateFields + data().HasProtectedFields +
Richard Smithb6070db2018-04-05 18:55:37 +0000923 data().HasPublicFields) > 1) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000924 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000925 data().IsCXX11StandardLayout = false;
926 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000927
Douglas Gregor61226d32011-05-13 01:05:07 +0000928 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +0000929 if (Field->isMutable()) {
Douglas Gregor61226d32011-05-13 01:05:07 +0000930 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +0000931 data().NeedOverloadResolutionForCopyConstructor = true;
932 }
Richard Smithab44d5b2013-12-10 08:25:00 +0000933
934 // C++11 [class.union]p8, DR1460:
935 // If X is a union, a non-static data member of X that is not an anonymous
936 // union is a variant member of X.
937 if (isUnion() && !Field->isAnonymousStructOrUnion())
938 data().HasVariantMembers = true;
939
Chandler Carruthad7d4042011-04-23 23:10:33 +0000940 // C++0x [class]p9:
Fangrui Song6907ce22018-07-30 19:24:48 +0000941 // A POD struct is a class that is both a trivial class and a
942 // standard-layout class, and has no non-static data members of type
Douglas Gregora832d3e2010-09-28 19:45:33 +0000943 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000944 //
945 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
946 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000947 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000948 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Brian Kelley036603a2017-03-29 17:31:42 +0000949 if (T.hasNonTrivialObjCLifetime()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000950 // Objective-C Automatic Reference Counting:
951 // If a class has a non-static data member of Objective-C pointer
952 // type (or array thereof), it is a non-POD type and its
953 // default constructor (if any), copy constructor, move constructor,
954 // copy assignment operator, move assignment operator, and destructor are
955 // non-trivial.
956 setHasObjectMember(true);
957 struct DefinitionData &Data = data();
958 Data.PlainOldData = false;
959 Data.HasTrivialSpecialMembers = 0;
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000960
961 // __strong or __weak fields do not make special functions non-trivial
962 // for the purpose of calls.
963 Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime();
964 if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak)
965 data().HasTrivialSpecialMembersForCall = 0;
966
967 // Structs with __weak fields should never be passed directly.
968 if (LT == Qualifiers::OCL_Weak)
Akira Hatanakae6313ac2018-04-09 22:48:22 +0000969 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Akira Hatanakafcbe17c2018-03-28 21:13:14 +0000970
Ben Langmuir11eab612014-09-26 15:27:29 +0000971 Data.HasIrrelevantDestructor = false;
Brian Kelley036603a2017-03-29 17:31:42 +0000972 } else if (!Context.getLangOpts().ObjCAutoRefCount) {
973 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000974 }
Eli Friedmana5433322013-07-20 01:06:31 +0000975 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000976 data().PlainOldData = false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000977
Chandler Carruthb1963742011-04-30 09:17:45 +0000978 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000979 if (!Field->hasInClassInitializer())
980 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000981
Chandler Carruthb1963742011-04-30 09:17:45 +0000982 // C++0x [class]p7:
983 // A standard-layout class is a class that:
984 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000985 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +0000986 data().IsCXX11StandardLayout = false;
Richard Smith96cd6712017-08-16 01:49:53 +0000987
988 // C++1z [class.copy.ctor]p10:
989 // A defaulted copy constructor for a class X is defined as deleted if X has:
990 // -- a non-static data member of rvalue reference type
991 if (T->isRValueReferenceType())
992 data().DefaultedCopyConstructorIsDeleted = true;
Chandler Carruthb1963742011-04-30 09:17:45 +0000993 }
994
Nico Weber6a6376b2016-02-19 01:52:46 +0000995 if (!Field->hasInClassInitializer() && !Field->isMutable()) {
Richard Smith9b182172016-10-28 19:11:18 +0000996 if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) {
Nico Weber344abaa2016-02-19 02:51:07 +0000997 if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit())
Nico Weber6a6376b2016-02-19 01:52:46 +0000998 data().HasUninitializedFields = true;
999 } else {
1000 data().HasUninitializedFields = true;
1001 }
1002 }
1003
Richard Smith3607ffe2012-02-13 03:54:03 +00001004 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +00001005 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +00001006 data().HasNonLiteralTypeFieldsOrBases = true;
1007
Richard Smithab44d5b2013-12-10 08:25:00 +00001008 if (Field->hasInClassInitializer() ||
1009 (Field->isAnonymousStructOrUnion() &&
1010 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +00001011 data().HasInClassInitializer = true;
1012
1013 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +00001014 // A default constructor is trivial if [...] no non-static data member
1015 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +00001016 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +00001017
Richard Smithe2648ba2012-05-07 01:07:30 +00001018 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +00001019 // An aggregate is a [...] class with [...] no
1020 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +00001021 //
Richard Smith872307e2016-03-08 22:17:41 +00001022 // This rule was removed in C++14.
Aaron Ballmandd69ef32014-08-19 15:55:55 +00001023 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +00001024 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +00001025
Richard Smithe2648ba2012-05-07 01:07:30 +00001026 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +00001027 // A POD struct is [...] a trivial class.
1028 data().PlainOldData = false;
1029 }
1030
Richard Smith6b02d462012-12-08 08:32:28 +00001031 // C++11 [class.copy]p23:
1032 // A defaulted copy/move assignment operator for a class X is defined
1033 // as deleted if X has:
1034 // -- a non-static data member of reference type
1035 if (T->isReferenceType())
1036 data().DefaultedMoveAssignmentIsDeleted = true;
1037
Eugene Zelenko7855e772018-04-03 00:11:50 +00001038 if (const auto *RecordTy = T->getAs<RecordType>()) {
1039 auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
Douglas Gregor11c024b2010-09-28 20:50:54 +00001040 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +00001041 addedClassSubobject(FieldRec);
1042
Richard Smithc91d12c2013-11-25 07:07:05 +00001043 // We may need to perform overload resolution to determine whether a
1044 // field can be moved if it's const or volatile qualified.
1045 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
Richard Smith96cd6712017-08-16 01:49:53 +00001046 // We need to care about 'const' for the copy constructor because an
1047 // implicit copy constructor might be declared with a non-const
1048 // parameter.
1049 data().NeedOverloadResolutionForCopyConstructor = true;
Richard Smithc91d12c2013-11-25 07:07:05 +00001050 data().NeedOverloadResolutionForMoveConstructor = true;
1051 data().NeedOverloadResolutionForMoveAssignment = true;
1052 }
1053
Richard Smith6b02d462012-12-08 08:32:28 +00001054 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
1055 // A defaulted [special member] for a class X is defined as
1056 // deleted if:
1057 // -- X is a union-like class that has a variant member with a
1058 // non-trivial [corresponding special member]
1059 if (isUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001060 if (FieldRec->hasNonTrivialCopyConstructor())
1061 data().DefaultedCopyConstructorIsDeleted = true;
Richard Smith6b02d462012-12-08 08:32:28 +00001062 if (FieldRec->hasNonTrivialMoveConstructor())
1063 data().DefaultedMoveConstructorIsDeleted = true;
1064 if (FieldRec->hasNonTrivialMoveAssignment())
1065 data().DefaultedMoveAssignmentIsDeleted = true;
1066 if (FieldRec->hasNonTrivialDestructor())
1067 data().DefaultedDestructorIsDeleted = true;
1068 }
1069
Richard Smithdd5619f2016-08-16 00:13:47 +00001070 // For an anonymous union member, our overload resolution will perform
1071 // overload resolution for its members.
1072 if (Field->isAnonymousStructOrUnion()) {
Richard Smith96cd6712017-08-16 01:49:53 +00001073 data().NeedOverloadResolutionForCopyConstructor |=
1074 FieldRec->data().NeedOverloadResolutionForCopyConstructor;
Richard Smithdd5619f2016-08-16 00:13:47 +00001075 data().NeedOverloadResolutionForMoveConstructor |=
1076 FieldRec->data().NeedOverloadResolutionForMoveConstructor;
1077 data().NeedOverloadResolutionForMoveAssignment |=
1078 FieldRec->data().NeedOverloadResolutionForMoveAssignment;
1079 data().NeedOverloadResolutionForDestructor |=
1080 FieldRec->data().NeedOverloadResolutionForDestructor;
1081 }
1082
Alexis Huntf479f1b2011-05-09 18:22:59 +00001083 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +00001084 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +00001085 // -- for all the non-static data members of its class that are of
1086 // class type (or array thereof), each such class has a trivial
1087 // default constructor.
1088 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001089 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001090
1091 // C++0x [class.copy]p13:
1092 // A copy/move constructor for class X is trivial if [...]
1093 // [...]
1094 // -- for each non-static data member of X that is of class type (or
1095 // an array thereof), the constructor selected to copy/move that
1096 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001097 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001098 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001099
1100 if (!FieldRec->hasTrivialCopyConstructorForCall())
1101 data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor;
1102
Richard Smith6b02d462012-12-08 08:32:28 +00001103 // If the field doesn't have a simple move constructor, we'll eagerly
1104 // declare the move constructor for this class and we'll decide whether
1105 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001106 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +00001107 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001108
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001109 if (!FieldRec->hasTrivialMoveConstructorForCall())
1110 data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor;
1111
Chandler Carruthad7d4042011-04-23 23:10:33 +00001112 // C++0x [class.copy]p27:
1113 // A copy/move assignment operator for class X is trivial if [...]
1114 // [...]
1115 // -- for each non-static data member of X that is of class type (or
1116 // an array thereof), the assignment operator selected to
1117 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001118 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001119 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +00001120 // If the field doesn't have a simple move assignment, we'll eagerly
1121 // declare the move assignment for this class and we'll decide whether
1122 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +00001123 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +00001124 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +00001125
Douglas Gregor11c024b2010-09-28 20:50:54 +00001126 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +00001127 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001128 if (!FieldRec->hasTrivialDestructorForCall())
1129 data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +00001130 if (!FieldRec->hasIrrelevantDestructor())
1131 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001132 if (FieldRec->hasObjectMember())
1133 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +00001134 if (FieldRec->hasVolatileMember())
1135 setHasVolatileMember(true);
Akira Hatanakae6313ac2018-04-09 22:48:22 +00001136 if (FieldRec->getArgPassingRestrictions() ==
1137 RecordDecl::APK_CanNeverPassInRegs)
1138 setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs);
Chandler Carruthb1963742011-04-30 09:17:45 +00001139
1140 // C++0x [class]p7:
1141 // A standard-layout class is a class that:
1142 // -- has no non-static data members of type non-standard-layout
1143 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +00001144 if (!FieldRec->isStandardLayout())
1145 data().IsStandardLayout = false;
Richard Smithb6070db2018-04-05 18:55:37 +00001146 if (!FieldRec->isCXX11StandardLayout())
1147 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001148
Richard Smithb6070db2018-04-05 18:55:37 +00001149 // C++2a [class]p7:
Chandler Carruthb1963742011-04-30 09:17:45 +00001150 // A standard-layout class is a class that:
1151 // [...]
Richard Smithb6070db2018-04-05 18:55:37 +00001152 // -- has no element of the set M(S) of types as a base class.
1153 if (data().IsStandardLayout && (isUnion() || IsFirstField) &&
1154 hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec))
1155 data().IsStandardLayout = false;
1156
1157 // C++11 [class]p7:
1158 // A standard-layout class is a class that:
Chandler Carruthb1963742011-04-30 09:17:45 +00001159 // -- has no base classes of the same type as the first non-static
Richard Smithb6070db2018-04-05 18:55:37 +00001160 // data member
1161 if (data().IsCXX11StandardLayout && IsFirstField) {
1162 // FIXME: We should check all base classes here, not just direct
1163 // base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001164 for (const auto &BI : bases()) {
1165 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Richard Smithb6070db2018-04-05 18:55:37 +00001166 data().IsCXX11StandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +00001167 break;
1168 }
1169 }
1170 }
Richard Smithb6070db2018-04-05 18:55:37 +00001171
Douglas Gregor61226d32011-05-13 01:05:07 +00001172 // Keep track of the presence of mutable fields.
Richard Smith96cd6712017-08-16 01:49:53 +00001173 if (FieldRec->hasMutableFields()) {
Douglas Gregor61226d32011-05-13 01:05:07 +00001174 data().HasMutableFields = true;
Richard Smith96cd6712017-08-16 01:49:53 +00001175 data().NeedOverloadResolutionForCopyConstructor = true;
1176 }
Richard Smithcc36f692011-12-22 02:22:31 +00001177
1178 // C++11 [class.copy]p13:
1179 // If the implicitly-defined constructor would satisfy the
1180 // requirements of a constexpr constructor, the implicitly-defined
1181 // constructor is constexpr.
1182 // C++11 [dcl.constexpr]p4:
1183 // -- every constructor involved in initializing non-static data
1184 // members [...] shall be a constexpr constructor
1185 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +00001186 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +00001187 // The standard requires any in-class initializer to be a constant
1188 // expression. We consider this to be a defect.
1189 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001190
1191 // C++11 [class.copy]p8:
1192 // The implicitly-declared copy constructor for a class X will have
Richard Smithdf054d32017-02-25 23:53:05 +00001193 // the form 'X::X(const X&)' if each potentially constructed subobject
1194 // of a class type M (or array thereof) has a copy constructor whose
1195 // first parameter is of type 'const M&' or 'const volatile M&'.
Richard Smith1c33fe82012-11-28 06:23:12 +00001196 if (!FieldRec->hasCopyConstructorWithConstParam())
Richard Smithdf054d32017-02-25 23:53:05 +00001197 data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false;
Richard Smith1c33fe82012-11-28 06:23:12 +00001198
1199 // C++11 [class.copy]p18:
1200 // The implicitly-declared copy assignment oeprator for a class X will
1201 // have the form 'X& X::operator=(const X&)' if [...] for all the
1202 // non-static data members of X that are of a class type M (or array
1203 // thereof), each such class type has a copy assignment operator whose
1204 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
1205 if (!FieldRec->hasCopyAssignmentWithConstParam())
1206 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +00001207
1208 if (FieldRec->hasUninitializedReferenceMember() &&
1209 !Field->hasInClassInitializer())
1210 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +00001211
1212 // C++11 [class.union]p8, DR1460:
1213 // a non-static data member of an anonymous union that is a member of
1214 // X is also a variant member of X.
1215 if (FieldRec->hasVariantMembers() &&
1216 Field->isAnonymousStructOrUnion())
1217 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001218 }
Richard Smithcc36f692011-12-22 02:22:31 +00001219 } else {
1220 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +00001221 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +00001222 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +00001223 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +00001224
1225 // C++11 [class.copy]p23:
1226 // A defaulted copy/move assignment operator for a class X is defined
1227 // as deleted if X has:
1228 // -- a non-static data member of const non-class type (or array
1229 // thereof)
1230 if (T.isConstQualified())
1231 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +00001232 }
Chandler Carruthb1963742011-04-30 09:17:45 +00001233
David Majnemer5cfda6f2016-05-22 05:34:26 +00001234 // C++14 [meta.unary.prop]p4:
Richard Smithb5a317f2018-05-07 06:43:30 +00001235 // T is a class type [...] with [...] no non-static data members
1236 data().Empty = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +00001237 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001238
Douglas Gregor457104e2010-09-29 04:25:11 +00001239 // Handle using declarations of conversion functions.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001240 if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +00001241 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +00001242 == DeclarationName::CXXConversionFunctionName) {
1243 ASTContext &Ctx = getASTContext();
1244 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
1245 }
1246 }
Richard Smith12e79312016-05-13 06:47:56 +00001247
Eugene Zelenko7855e772018-04-03 00:11:50 +00001248 if (const auto *Using = dyn_cast<UsingDecl>(D)) {
Richard Smith12e79312016-05-13 06:47:56 +00001249 if (Using->getDeclName().getNameKind() ==
Eric Fiselier283d8d42016-12-03 01:26:47 +00001250 DeclarationName::CXXConstructorName) {
Richard Smith12e79312016-05-13 06:47:56 +00001251 data().HasInheritedConstructor = true;
Eric Fiselier283d8d42016-12-03 01:26:47 +00001252 // C++1z [dcl.init.aggr]p1:
1253 // An aggregate is [...] a class [...] with no inherited constructors
1254 data().Aggregate = false;
1255 }
Richard Smith12e79312016-05-13 06:47:56 +00001256
1257 if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal)
1258 data().HasInheritedAssignment = true;
1259 }
Joao Matose9a3ed42012-08-31 22:18:20 +00001260}
1261
Richard Smith92f241f2012-12-08 02:53:02 +00001262void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
1263 assert(!D->isImplicit() && !D->isUserProvided());
1264
1265 // The kind of special member this declaration is, if any.
1266 unsigned SMKind = 0;
1267
Eugene Zelenko7855e772018-04-03 00:11:50 +00001268 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001269 if (Constructor->isDefaultConstructor()) {
1270 SMKind |= SMF_DefaultConstructor;
1271 if (Constructor->isConstexpr())
1272 data().HasConstexprDefaultConstructor = true;
1273 }
1274 if (Constructor->isCopyConstructor())
1275 SMKind |= SMF_CopyConstructor;
1276 else if (Constructor->isMoveConstructor())
1277 SMKind |= SMF_MoveConstructor;
1278 else if (Constructor->isConstexpr())
1279 // We may now know that the constructor is constexpr.
1280 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001281 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +00001282 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +00001283 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
1284 data().HasIrrelevantDestructor = false;
1285 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +00001286 SMKind |= SMF_CopyAssignment;
1287 else if (D->isMoveAssignmentOperator())
1288 SMKind |= SMF_MoveAssignment;
1289
1290 // Update which trivial / non-trivial special members we have.
1291 // addedMember will have skipped this step for this member.
1292 if (D->isTrivial())
1293 data().HasTrivialSpecialMembers |= SMKind;
1294 else
1295 data().DeclaredNonTrivialSpecialMembers |= SMKind;
1296}
1297
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001298void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) {
1299 unsigned SMKind = 0;
1300
Eugene Zelenko7855e772018-04-03 00:11:50 +00001301 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Akira Hatanaka02914dc2018-02-05 20:23:22 +00001302 if (Constructor->isCopyConstructor())
1303 SMKind = SMF_CopyConstructor;
1304 else if (Constructor->isMoveConstructor())
1305 SMKind = SMF_MoveConstructor;
1306 } else if (isa<CXXDestructorDecl>(D))
1307 SMKind = SMF_Destructor;
1308
1309 if (D->isTrivialForCall())
1310 data().HasTrivialSpecialMembersForCall |= SMKind;
1311 else
1312 data().DeclaredNonTrivialSpecialMembersForCall |= SMKind;
1313}
1314
Joao Matose9a3ed42012-08-31 22:18:20 +00001315bool CXXRecordDecl::isCLike() const {
1316 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
1317 !TemplateOrInstantiation.isNull())
1318 return false;
1319 if (!hasDefinition())
1320 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001321
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +00001322 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +00001323}
Fangrui Song6907ce22018-07-30 19:24:48 +00001324
1325bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +00001326 if (!isLambda()) return false;
1327 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001328}
1329
1330CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001331 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001332 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001333 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Richard Smithcf4bdde2015-02-21 02:45:19 +00001334 DeclContext::lookup_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +00001335
1336 assert(!Calls.empty() && "Missing lambda call operator!");
Fangrui Song6907ce22018-07-30 19:24:48 +00001337 assert(Calls.size() == 1 && "More than one lambda call operator!");
1338
Faisal Vali850da1a2013-09-29 17:08:32 +00001339 NamedDecl *CallOp = Calls.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001340 if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp))
Faisal Vali850da1a2013-09-29 17:08:32 +00001341 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001342
Faisal Vali2b391ab2013-09-26 19:54:12 +00001343 return cast<CXXMethodDecl>(CallOp);
1344}
1345
1346CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001347 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001348 DeclarationName Name =
Faisal Vali850da1a2013-09-29 17:08:32 +00001349 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Richard Smithcf4bdde2015-02-21 02:45:19 +00001350 DeclContext::lookup_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001351 if (Invoker.empty()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001352 assert(Invoker.size() == 1 && "More than one static invoker operator!");
Faisal Vali850da1a2013-09-29 17:08:32 +00001353 NamedDecl *InvokerFun = Invoker.front();
Eugene Zelenko7855e772018-04-03 00:11:50 +00001354 if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun))
Faisal Vali850da1a2013-09-29 17:08:32 +00001355 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +00001356
1357 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001358}
1359
Douglas Gregor9c702202012-02-10 07:45:31 +00001360void CXXRecordDecl::getCaptureFields(
1361 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001362 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001363 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001364 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001365
Douglas Gregorc8a73492012-02-13 15:44:47 +00001366 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001367 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001368 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001369 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001370 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001371 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001372 else if (C->capturesVariable())
1373 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001374 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001375 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001376}
1377
Fangrui Song6907ce22018-07-30 19:24:48 +00001378TemplateParameterList *
Faisal Vali2b391ab2013-09-26 19:54:12 +00001379CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001380 if (!isLambda()) return nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001381 CXXMethodDecl *CallOp = getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001382 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1383 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001384 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001385}
Douglas Gregor9c702202012-02-10 07:45:31 +00001386
Richard Smith0bae6242016-08-25 00:34:00 +00001387Decl *CXXRecordDecl::getLambdaContextDecl() const {
1388 assert(isLambda() && "Not a lambda closure type!");
1389 ExternalASTSource *Source = getParentASTContext().getExternalSource();
1390 return getLambdaData().ContextDecl.get(Source);
1391}
1392
John McCall1e3a1a72010-03-15 09:07:48 +00001393static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001394 QualType T =
1395 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1396 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001397 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001398}
1399
John McCall1e3a1a72010-03-15 09:07:48 +00001400/// Collect the visible conversions of a base class.
1401///
James Dennettb5d5f812012-06-15 22:28:09 +00001402/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001403/// \param InVirtual whether this base class is a virtual base (or a base
1404/// of a virtual base)
1405/// \param Access the access along the inheritance path to this base
1406/// \param ParentHiddenTypes the conversions provided by the inheritors
1407/// of this base
1408/// \param Output the set to which to add conversions from non-virtual bases
1409/// \param VOutput the set to which to add conversions from virtual bases
1410/// \param HiddenVBaseCs the set of conversions which were hidden in a
1411/// virtual base along some inheritance path
1412static void CollectVisibleConversions(ASTContext &Context,
1413 CXXRecordDecl *Record,
1414 bool InVirtual,
1415 AccessSpecifier Access,
1416 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001417 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001418 UnresolvedSetImpl &VOutput,
1419 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1420 // The set of types which have conversions in this class or its
1421 // subclasses. As an optimization, we don't copy the derived set
1422 // unless it might change.
1423 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1424 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1425
1426 // Collect the direct conversions and figure out which conversions
1427 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001428 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1429 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1430 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001431 HiddenTypesBuffer = ParentHiddenTypes;
1432 HiddenTypes = &HiddenTypesBuffer;
1433
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001434 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001435 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1436 bool Hidden = ParentHiddenTypes.count(ConvType);
1437 if (!Hidden)
1438 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001439
1440 // If this conversion is hidden and we're in a virtual base,
1441 // remember that it's hidden along some inheritance path.
1442 if (Hidden && InVirtual)
1443 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1444
1445 // If this conversion isn't hidden, add it to the appropriate output.
1446 else if (!Hidden) {
1447 AccessSpecifier IAccess
1448 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1449
1450 if (InVirtual)
1451 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001452 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001453 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001454 }
1455 }
1456 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001457
John McCall1e3a1a72010-03-15 09:07:48 +00001458 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001459 for (const auto &I : Record->bases()) {
1460 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001461 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001462
John McCall1e3a1a72010-03-15 09:07:48 +00001463 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001464 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1465 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001466
Eugene Zelenko7855e772018-04-03 00:11:50 +00001467 auto *Base = cast<CXXRecordDecl>(RT->getDecl());
John McCall1e3a1a72010-03-15 09:07:48 +00001468 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1469 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001470 }
John McCall1e3a1a72010-03-15 09:07:48 +00001471}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001472
John McCall1e3a1a72010-03-15 09:07:48 +00001473/// Collect the visible conversions of a class.
1474///
1475/// This would be extremely straightforward if it weren't for virtual
1476/// bases. It might be worth special-casing that, really.
1477static void CollectVisibleConversions(ASTContext &Context,
1478 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001479 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001480 // The collection of all conversions in virtual bases that we've
1481 // found. These will be added to the output as long as they don't
1482 // appear in the hidden-conversions set.
1483 UnresolvedSet<8> VBaseCs;
Fangrui Song6907ce22018-07-30 19:24:48 +00001484
John McCall1e3a1a72010-03-15 09:07:48 +00001485 // The set of conversions in virtual bases that we've determined to
1486 // be hidden.
1487 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1488
1489 // The set of types hidden by classes derived from this one.
1490 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1491
1492 // Go ahead and collect the direct conversions and add them to the
1493 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001494 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1495 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001496 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001497 for (; ConvI != ConvE; ++ConvI)
1498 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001499
1500 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001501 for (const auto &I : Record->bases()) {
1502 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001503 if (!RT) continue;
1504
1505 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001506 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001507 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1508 }
1509
1510 // Add any unhidden conversions provided by virtual bases.
1511 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1512 I != E; ++I) {
1513 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001514 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001515 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001516}
1517
1518/// getVisibleConversionFunctions - get all conversion functions visible
1519/// in current class; including conversion function templates.
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001520llvm::iterator_range<CXXRecordDecl::conversion_iterator>
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001521CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001522 ASTContext &Ctx = getASTContext();
1523
1524 ASTUnresolvedSet *Set;
1525 if (bases_begin() == bases_end()) {
1526 // If root class, all conversions are visible.
1527 Set = &data().Conversions.get(Ctx);
1528 } else {
1529 Set = &data().VisibleConversions.get(Ctx);
1530 // If visible conversion list is not evaluated, evaluate it.
1531 if (!data().ComputedVisibleConversions) {
1532 CollectVisibleConversions(Ctx, this, *Set);
1533 data().ComputedVisibleConversions = true;
1534 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001535 }
Benjamin Kramerb4ef6682015-02-06 17:25:10 +00001536 return llvm::make_range(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001537}
1538
John McCallda4458e2010-03-31 01:36:47 +00001539void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1540 // This operation is O(N) but extremely rare. Sema only uses it to
1541 // remove UsingShadowDecls in a class that were followed by a direct
1542 // declaration, e.g.:
1543 // class A : B {
1544 // using B::operator int;
1545 // operator int();
1546 // };
1547 // This is uncommon by itself and even more uncommon in conjunction
1548 // with sufficiently large numbers of directly-declared conversions
1549 // that asymptotic behavior matters.
1550
Richard Smitha4ba74c2013-08-30 04:46:40 +00001551 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001552 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1553 if (Convs[I].getDecl() == ConvDecl) {
1554 Convs.erase(I);
1555 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1556 && "conversion was found multiple times in unresolved set");
1557 return;
1558 }
1559 }
1560
1561 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001562}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001563
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001564CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001565 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001566 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001567
1568 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001569}
1570
Chandler Carruth21c90602015-12-30 03:24:14 +00001571MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const {
1572 return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>();
1573}
1574
Fangrui Song6907ce22018-07-30 19:24:48 +00001575void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001576CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1577 TemplateSpecializationKind TSK) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001578 assert(TemplateOrInstantiation.isNull() &&
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001579 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001580 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Fangrui Song6907ce22018-07-30 19:24:48 +00001581 TemplateOrInstantiation
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001582 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1583}
1584
Chandler Carruth21c90602015-12-30 03:24:14 +00001585ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const {
1586 return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>();
1587}
1588
1589void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) {
1590 TemplateOrInstantiation = Template;
1591}
1592
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001593TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
Eugene Zelenko7855e772018-04-03 00:11:50 +00001594 if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this))
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001595 return Spec->getSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001596
Douglas Gregor06db9f52009-10-12 20:18:28 +00001597 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001598 return MSInfo->getTemplateSpecializationKind();
Fangrui Song6907ce22018-07-30 19:24:48 +00001599
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001600 return TSK_Undeclared;
1601}
1602
Fangrui Song6907ce22018-07-30 19:24:48 +00001603void
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001604CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001605 if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001606 Spec->setSpecializationKind(TSK);
1607 return;
1608 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001609
Douglas Gregor06db9f52009-10-12 20:18:28 +00001610 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001611 MSInfo->setTemplateSpecializationKind(TSK);
1612 return;
1613 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001614
David Blaikie83d382b2011-09-23 05:06:16 +00001615 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001616}
1617
Reid Klecknere7367d62014-10-14 20:28:40 +00001618const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
Richard Smith2195ec92017-04-21 01:15:13 +00001619 auto GetDefinitionOrSelf =
1620 [](const CXXRecordDecl *D) -> const CXXRecordDecl * {
1621 if (auto *Def = D->getDefinition())
1622 return Def;
1623 return D;
1624 };
1625
Reid Klecknere7367d62014-10-14 20:28:40 +00001626 // If it's a class template specialization, find the template or partial
1627 // specialization from which it was instantiated.
1628 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1629 auto From = TD->getInstantiatedFrom();
1630 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1631 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1632 if (NewCTD->isMemberSpecialization())
1633 break;
1634 CTD = NewCTD;
1635 }
Richard Smith2195ec92017-04-21 01:15:13 +00001636 return GetDefinitionOrSelf(CTD->getTemplatedDecl());
Reid Klecknere7367d62014-10-14 20:28:40 +00001637 }
1638 if (auto *CTPSD =
1639 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1640 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1641 if (NewCTPSD->isMemberSpecialization())
1642 break;
1643 CTPSD = NewCTPSD;
1644 }
Richard Smith2195ec92017-04-21 01:15:13 +00001645 return GetDefinitionOrSelf(CTPSD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001646 }
1647 }
1648
1649 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1650 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1651 const CXXRecordDecl *RD = this;
1652 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1653 RD = NewRD;
Richard Smith2195ec92017-04-21 01:15:13 +00001654 return GetDefinitionOrSelf(RD);
Reid Klecknere7367d62014-10-14 20:28:40 +00001655 }
1656 }
1657
1658 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1659 "couldn't find pattern for class template instantiation");
1660 return nullptr;
1661}
1662
Douglas Gregorbac74902010-07-01 14:13:13 +00001663CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1664 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001665 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001666
1667 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001668 = Context.DeclarationNames.getCXXDestructorName(
1669 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001670
Richard Smithcf4bdde2015-02-21 02:45:19 +00001671 DeclContext::lookup_result R = lookup(Name);
Mike Stump11289f42009-09-09 15:08:12 +00001672
Hubert Tong3cede512017-06-30 22:43:54 +00001673 return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001674}
1675
Richard Trieu95a192a2015-05-28 00:14:02 +00001676bool CXXRecordDecl::isAnyDestructorNoReturn() const {
1677 // Destructor is noreturn.
1678 if (const CXXDestructorDecl *Destructor = getDestructor())
1679 if (Destructor->isNoReturn())
1680 return true;
1681
1682 // Check base classes destructor for noreturn.
1683 for (const auto &Base : bases())
Alexander Kornienko29333342017-05-12 11:24:25 +00001684 if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl())
1685 if (RD->isAnyDestructorNoReturn())
1686 return true;
Richard Trieu95a192a2015-05-28 00:14:02 +00001687
1688 // Check fields for noreturn.
1689 for (const auto *Field : fields())
1690 if (const CXXRecordDecl *RD =
1691 Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl())
1692 if (RD->isAnyDestructorNoReturn())
1693 return true;
1694
1695 // All destructors are not noreturn.
1696 return false;
1697}
1698
Erich Keane3e08f662017-09-29 21:06:00 +00001699static bool isDeclContextInNamespace(const DeclContext *DC) {
1700 while (!DC->isTranslationUnit()) {
1701 if (DC->isNamespace())
1702 return true;
1703 DC = DC->getParent();
1704 }
1705 return false;
1706}
1707
Erich Keane58bd6032017-09-15 16:03:35 +00001708bool CXXRecordDecl::isInterfaceLike() const {
1709 assert(hasDefinition() && "checking for interface-like without a definition");
1710 // All __interfaces are inheritently interface-like.
1711 if (isInterface())
1712 return true;
1713
1714 // Interface-like types cannot have a user declared constructor, destructor,
1715 // friends, VBases, conversion functions, or fields. Additionally, lambdas
1716 // cannot be interface types.
1717 if (isLambda() || hasUserDeclaredConstructor() ||
1718 hasUserDeclaredDestructor() || !field_empty() || hasFriends() ||
1719 getNumVBases() > 0 || conversion_end() - conversion_begin() > 0)
1720 return false;
1721
1722 // No interface-like type can have a method with a definition.
1723 for (const auto *const Method : methods())
Erich Keane3e08f662017-09-29 21:06:00 +00001724 if (Method->isDefined() && !Method->isImplicit())
Erich Keane58bd6032017-09-15 16:03:35 +00001725 return false;
1726
1727 // Check "Special" types.
1728 const auto *Uuid = getAttr<UuidAttr>();
Erich Keane3e08f662017-09-29 21:06:00 +00001729 // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an
1730 // extern C++ block directly in the TU. These are only valid if in one
1731 // of these two situations.
1732 if (Uuid && isStruct() && !getDeclContext()->isExternCContext() &&
1733 !isDeclContextInNamespace(getDeclContext()) &&
Erich Keane58bd6032017-09-15 16:03:35 +00001734 ((getName() == "IUnknown" &&
1735 Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") ||
1736 (getName() == "IDispatch" &&
1737 Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) {
1738 if (getNumBases() > 0)
1739 return false;
1740 return true;
1741 }
1742
1743 // FIXME: Any access specifiers is supposed to make this no longer interface
1744 // like.
1745
1746 // If this isn't a 'special' type, it must have a single interface-like base.
1747 if (getNumBases() != 1)
1748 return false;
1749
1750 const auto BaseSpec = *bases_begin();
1751 if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public)
1752 return false;
1753 const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl();
1754 if (Base->isInterface() || !Base->isInterfaceLike())
1755 return false;
1756 return true;
1757}
1758
Douglas Gregorb11aad82011-02-19 18:51:44 +00001759void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001760 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001761}
1762
1763void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1764 RecordDecl::completeDefinition();
Richard Smith96cd6712017-08-16 01:49:53 +00001765
Douglas Gregor8fb95122010-09-29 00:15:42 +00001766 // If the class may be abstract (but hasn't been marked as such), check for
1767 // any pure final overriders.
1768 if (mayBeAbstract()) {
1769 CXXFinalOverriderMap MyFinalOverriders;
1770 if (!FinalOverriders) {
1771 getFinalOverriders(MyFinalOverriders);
1772 FinalOverriders = &MyFinalOverriders;
1773 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001774
Douglas Gregor8fb95122010-09-29 00:15:42 +00001775 bool Done = false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001776 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001777 MEnd = FinalOverriders->end();
1778 M != MEnd && !Done; ++M) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001779 for (OverridingMethods::iterator SO = M->second.begin(),
Douglas Gregor8fb95122010-09-29 00:15:42 +00001780 SOEnd = M->second.end();
1781 SO != SOEnd && !Done; ++SO) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001782 assert(SO->second.size() > 0 &&
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001783 "All virtual functions have overriding virtual functions");
Fangrui Song6907ce22018-07-30 19:24:48 +00001784
Douglas Gregor8fb95122010-09-29 00:15:42 +00001785 // C++ [class.abstract]p4:
1786 // A class is abstract if it contains or inherits at least one
1787 // pure virtual function for which the final overrider is pure
1788 // virtual.
1789 if (SO->second.front().Method->isPure()) {
1790 data().Abstract = true;
1791 Done = true;
1792 break;
1793 }
1794 }
1795 }
1796 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001797
Douglas Gregor457104e2010-09-29 04:25:11 +00001798 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001799 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001800 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001801 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001802}
1803
1804bool CXXRecordDecl::mayBeAbstract() const {
1805 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1806 isDependentContext())
1807 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001808
Aaron Ballman574705e2014-03-13 15:41:46 +00001809 for (const auto &B : bases()) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001810 const auto *BaseDecl =
1811 cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001812 if (BaseDecl->isAbstract())
1813 return true;
1814 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001815
Douglas Gregor8fb95122010-09-29 00:15:42 +00001816 return false;
1817}
1818
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001819void CXXDeductionGuideDecl::anchor() {}
Richard Smithbc491202017-02-17 20:05:37 +00001820
1821CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create(
1822 ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit,
1823 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
1824 SourceLocation EndLocation) {
1825 return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit,
1826 NameInfo, T, TInfo, EndLocation);
1827}
1828
1829CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C,
1830 unsigned ID) {
1831 return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false,
1832 DeclarationNameInfo(), QualType(),
1833 nullptr, SourceLocation());
1834}
1835
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00001836void CXXMethodDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00001837
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001838bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001839 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001840
1841 if (MD->getStorageClass() == SC_Static)
1842 return true;
1843
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001844 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1845 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001846}
1847
Rafael Espindola49e860b2012-06-26 17:45:31 +00001848static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1849 const CXXMethodDecl *BaseMD) {
Benjamin Krameracfa3392017-12-17 23:52:45 +00001850 for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001851 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1852 return true;
1853 if (recursivelyOverrides(MD, BaseMD))
1854 return true;
1855 }
1856 return false;
1857}
1858
1859CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001860CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1861 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001862 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1863 return this;
1864
1865 // Lookup doesn't work for destructors, so handle them separately.
1866 if (isa<CXXDestructorDecl>(this)) {
1867 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001868 if (MD) {
1869 if (recursivelyOverrides(MD, this))
1870 return MD;
1871 if (MayBeBase && recursivelyOverrides(this, MD))
1872 return MD;
1873 }
Craig Topper36250ad2014-05-12 05:36:57 +00001874 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001875 }
1876
Richard Smith40c78062015-02-21 02:31:57 +00001877 for (auto *ND : RD->lookup(getDeclName())) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00001878 auto *MD = dyn_cast<CXXMethodDecl>(ND);
Rafael Espindola49e860b2012-06-26 17:45:31 +00001879 if (!MD)
1880 continue;
1881 if (recursivelyOverrides(MD, this))
1882 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001883 if (MayBeBase && recursivelyOverrides(this, MD))
1884 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001885 }
1886
Aaron Ballman574705e2014-03-13 15:41:46 +00001887 for (const auto &I : RD->bases()) {
1888 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001889 if (!RT)
1890 continue;
Eugene Zelenko7855e772018-04-03 00:11:50 +00001891 const auto *Base = cast<CXXRecordDecl>(RT->getDecl());
Rafael Espindola49e860b2012-06-26 17:45:31 +00001892 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1893 if (T)
1894 return T;
1895 }
1896
Craig Topper36250ad2014-05-12 05:36:57 +00001897 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001898}
1899
Ted Kremenek21475702008-09-05 17:16:31 +00001900CXXMethodDecl *
1901CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001902 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001903 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001904 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001905 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001906 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001907 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1908 T, TInfo, SC, isInline, isConstexpr,
1909 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001910}
1911
Douglas Gregor72172e92012-01-05 21:55:30 +00001912CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001913 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001914 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001915 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001916}
1917
Akira Hatanaka22461672017-07-13 06:08:27 +00001918CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base,
1919 bool IsAppleKext) {
1920 assert(isVirtual() && "this method is expected to be virtual");
1921
1922 // When building with -fapple-kext, all calls must go through the vtable since
1923 // the kernel linker can do runtime patching of vtables.
1924 if (IsAppleKext)
1925 return nullptr;
1926
1927 // If the member function is marked 'final', we know that it can't be
1928 // overridden and can therefore devirtualize it unless it's pure virtual.
1929 if (hasAttr<FinalAttr>())
1930 return isPure() ? nullptr : this;
1931
1932 // If Base is unknown, we cannot devirtualize.
1933 if (!Base)
1934 return nullptr;
1935
1936 // If the base expression (after skipping derived-to-base conversions) is a
1937 // class prvalue, then we can devirtualize.
1938 Base = Base->getBestDynamicClassTypeExpr();
1939 if (Base->isRValue() && Base->getType()->isRecordType())
1940 return this;
1941
1942 // If we don't even know what we would call, we can't devirtualize.
1943 const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType();
1944 if (!BestDynamicDecl)
1945 return nullptr;
1946
1947 // There may be a method corresponding to MD in a derived class.
1948 CXXMethodDecl *DevirtualizedMethod =
1949 getCorrespondingMethodInClass(BestDynamicDecl);
1950
1951 // If that method is pure virtual, we can't devirtualize. If this code is
1952 // reached, the result would be UB, not a direct call to the derived class
1953 // function, and we can't assume the derived class function is defined.
1954 if (DevirtualizedMethod->isPure())
1955 return nullptr;
1956
1957 // If that method is marked final, we can devirtualize it.
1958 if (DevirtualizedMethod->hasAttr<FinalAttr>())
1959 return DevirtualizedMethod;
1960
1961 // Similarly, if the class itself is marked 'final' it can't be overridden
1962 // and we can therefore devirtualize the member function call.
1963 if (BestDynamicDecl->hasAttr<FinalAttr>())
1964 return DevirtualizedMethod;
1965
Eugene Zelenko7855e772018-04-03 00:11:50 +00001966 if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) {
1967 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Akira Hatanaka22461672017-07-13 06:08:27 +00001968 if (VD->getType()->isRecordType())
1969 // This is a record decl. We know the type and can devirtualize it.
1970 return DevirtualizedMethod;
1971
1972 return nullptr;
1973 }
1974
1975 // We can devirtualize calls on an object accessed by a class member access
1976 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
1977 // a derived class object constructed in the same location.
Eugene Zelenko7855e772018-04-03 00:11:50 +00001978 if (const auto *ME = dyn_cast<MemberExpr>(Base)) {
George Burgess IV00f70bd2018-03-01 05:43:23 +00001979 const ValueDecl *VD = ME->getMemberDecl();
1980 return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr;
1981 }
Akira Hatanaka22461672017-07-13 06:08:27 +00001982
1983 // Likewise for calls on an object accessed by a (non-reference) pointer to
1984 // member access.
1985 if (auto *BO = dyn_cast<BinaryOperator>(Base)) {
1986 if (BO->isPtrMemOp()) {
1987 auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>();
1988 if (MPT->getPointeeType()->isRecordType())
1989 return DevirtualizedMethod;
1990 }
1991 }
1992
1993 // We can't devirtualize the call.
1994 return nullptr;
1995}
1996
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001997bool CXXMethodDecl::isUsualDeallocationFunction() const {
1998 if (getOverloadedOperator() != OO_Delete &&
1999 getOverloadedOperator() != OO_Array_Delete)
2000 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00002001
2002 // C++ [basic.stc.dynamic.deallocation]p2:
2003 // A template instance is never a usual deallocation function,
2004 // regardless of its signature.
2005 if (getPrimaryTemplate())
2006 return false;
2007
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002008 // C++ [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002009 // If a class T has a member deallocation function named operator delete
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002010 // with exactly one parameter, then that function is a usual (non-placement)
2011 // deallocation function. [...]
2012 if (getNumParams() == 1)
2013 return true;
Richard Smithb2f0f052016-10-10 18:54:32 +00002014 unsigned UsualParams = 1;
Richard Smith96269c52016-09-29 22:49:46 +00002015
Richard Smith5b349582017-10-13 01:55:36 +00002016 // C++ P0722:
2017 // A destroying operator delete is a usual deallocation function if
2018 // removing the std::destroying_delete_t parameter and changing the
2019 // first parameter type from T* to void* results in the signature of
2020 // a usual deallocation function.
2021 if (isDestroyingOperatorDelete())
2022 ++UsualParams;
2023
Richard Smithb2f0f052016-10-10 18:54:32 +00002024 // C++ <=14 [basic.stc.dynamic.deallocation]p2:
Fangrui Song6907ce22018-07-30 19:24:48 +00002025 // [...] If class T does not declare such an operator delete but does
2026 // declare a member deallocation function named operator delete with
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002027 // exactly two parameters, the second of which has type std::size_t (18.1),
2028 // then this function is a usual deallocation function.
Richard Smithb2f0f052016-10-10 18:54:32 +00002029 //
2030 // C++17 says a usual deallocation function is one with the signature
2031 // (void* [, size_t] [, std::align_val_t] [, ...])
2032 // and all such functions are usual deallocation functions. It's not clear
2033 // that allowing varargs functions was intentional.
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002034 ASTContext &Context = getASTContext();
Richard Smithb2f0f052016-10-10 18:54:32 +00002035 if (UsualParams < getNumParams() &&
2036 Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(),
2037 Context.getSizeType()))
2038 ++UsualParams;
2039
2040 if (UsualParams < getNumParams() &&
2041 getParamDecl(UsualParams)->getType()->isAlignValT())
2042 ++UsualParams;
2043
2044 if (UsualParams != getNumParams())
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002045 return false;
Richard Smithb2f0f052016-10-10 18:54:32 +00002046
2047 // In C++17 onwards, all potential usual deallocation functions are actual
2048 // usual deallocation functions.
2049 if (Context.getLangOpts().AlignedAllocation)
2050 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00002051
2052 // This function is a usual deallocation function if there are no
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002053 // single-parameter deallocation functions of the same kind.
Richard Smithcf4bdde2015-02-21 02:45:19 +00002054 DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName());
2055 for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end();
David Blaikieff7d47a2012-12-19 00:45:41 +00002056 I != E; ++I) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002057 if (const auto *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002058 if (FD->getNumParams() == 1)
2059 return false;
2060 }
Fangrui Song6907ce22018-07-30 19:24:48 +00002061
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00002062 return true;
2063}
2064
Douglas Gregorb139cd52010-05-01 20:49:11 +00002065bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00002066 // C++0x [class.copy]p17:
Fangrui Song6907ce22018-07-30 19:24:48 +00002067 // A user-declared copy assignment operator X::operator= is a non-static
2068 // non-template member function of class X with exactly one parameter of
Douglas Gregorb139cd52010-05-01 20:49:11 +00002069 // type X, X&, const X&, volatile X& or const volatile X&.
2070 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
Fangrui Song6907ce22018-07-30 19:24:48 +00002071 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002072 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2073 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00002074 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002075
Douglas Gregorb139cd52010-05-01 20:49:11 +00002076 QualType ParamType = getParamDecl(0)->getType();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002077 if (const auto *Ref = ParamType->getAs<LValueReferenceType>())
Douglas Gregorb139cd52010-05-01 20:49:11 +00002078 ParamType = Ref->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +00002079
Douglas Gregorb139cd52010-05-01 20:49:11 +00002080 ASTContext &Context = getASTContext();
2081 QualType ClassType
2082 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2083 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2084}
2085
Alexis Huntfcaeae42011-05-25 20:50:04 +00002086bool CXXMethodDecl::isMoveAssignmentOperator() const {
2087 // C++0x [class.copy]p19:
2088 // A user-declared move assignment operator X::operator= is a non-static
2089 // non-template member function of class X with exactly one parameter of type
2090 // X&&, const X&&, volatile X&&, or const volatile X&&.
2091 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00002092 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
2093 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00002094 return false;
2095
2096 QualType ParamType = getParamDecl(0)->getType();
2097 if (!isa<RValueReferenceType>(ParamType))
2098 return false;
2099 ParamType = ParamType->getPointeeType();
2100
2101 ASTContext &Context = getASTContext();
2102 QualType ClassType
2103 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
2104 return Context.hasSameUnqualifiedType(ClassType, ParamType);
2105}
2106
Anders Carlsson36d87e12009-05-16 23:58:37 +00002107void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00002108 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002109 assert(!MD->getParent()->isDependentContext() &&
2110 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00002111 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00002112
Douglas Gregor832940b2010-03-02 23:58:15 +00002113 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002114}
2115
2116CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002117 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002118 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002119}
2120
2121CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00002122 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00002123 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00002124}
2125
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002126unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00002127 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00002128 return getASTContext().overridden_methods_size(this);
2129}
2130
Clement Courbet6ecaec82016-07-05 07:49:31 +00002131CXXMethodDecl::overridden_method_range
2132CXXMethodDecl::overridden_methods() const {
2133 if (isa<CXXConstructorDecl>(this))
2134 return overridden_method_range(nullptr, nullptr);
2135 return getASTContext().overridden_methods(this);
2136}
2137
Ted Kremenek21475702008-09-05 17:16:31 +00002138QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00002139 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
2140 // If the member function is declared const, the type of this is const X*,
2141 // if the member function is declared volatile, the type of this is
2142 // volatile X*, and if the member function is declared const volatile,
2143 // the type of this is const volatile X*.
2144
Ted Kremenek21475702008-09-05 17:16:31 +00002145 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00002146
John McCalle78aac42010-03-10 03:28:59 +00002147 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00002148 ClassTy = C.getQualifiedType(ClassTy,
Andrey Bokhanko67a41862016-05-26 10:06:01 +00002149 Qualifiers::fromCVRUMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00002150 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00002151}
2152
Eli Friedman71a26d82009-12-06 20:50:05 +00002153bool CXXMethodDecl::hasInlineBody() const {
Fangrui Song6907ce22018-07-30 19:24:48 +00002154 // If this function is a template instantiation, look at the template from
Douglas Gregora318efd2010-01-05 19:06:31 +00002155 // which it was instantiated.
2156 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
2157 if (!CheckFn)
2158 CheckFn = this;
Richard Smith6c7161162017-08-12 01:46:03 +00002159
Eli Friedman71a26d82009-12-06 20:50:05 +00002160 const FunctionDecl *fn;
Richard Smith6c7161162017-08-12 01:46:03 +00002161 return CheckFn->isDefined(fn) && !fn->isOutOfLine() &&
2162 (fn->doesThisDeclarationHaveABody() || fn->willHaveBody());
Eli Friedman71a26d82009-12-06 20:50:05 +00002163}
2164
Douglas Gregor355efbb2012-02-17 03:02:34 +00002165bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00002166 const CXXRecordDecl *P = getParent();
2167 if (P->isLambda()) {
2168 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
2169 if (StaticInvoker == this) return true;
2170 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
2171 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
2172 }
2173 }
2174 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00002175}
2176
Alexis Hunt1d792652011-01-08 20:30:50 +00002177CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2178 TypeSourceInfo *TInfo, bool IsVirtual,
2179 SourceLocation L, Expr *Init,
2180 SourceLocation R,
2181 SourceLocation EllipsisLoc)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002182 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
2183 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
2184 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002185
Alexis Hunt1d792652011-01-08 20:30:50 +00002186CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2187 FieldDecl *Member,
2188 SourceLocation MemberLoc,
2189 SourceLocation L, Expr *Init,
2190 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002191 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2192 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2193 IsWritten(false), SourceOrder(0) {}
Francois Pichetd583da02010-12-04 09:14:42 +00002194
Alexis Hunt1d792652011-01-08 20:30:50 +00002195CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
2196 IndirectFieldDecl *Member,
2197 SourceLocation MemberLoc,
2198 SourceLocation L, Expr *Init,
2199 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002200 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
2201 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
2202 IsWritten(false), SourceOrder(0) {}
Douglas Gregore8381c02008-11-05 04:29:56 +00002203
Alexis Hunt1d792652011-01-08 20:30:50 +00002204CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002205 TypeSourceInfo *TInfo,
Fangrui Song6907ce22018-07-30 19:24:48 +00002206 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00002207 SourceLocation R)
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002208 : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R),
2209 IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {}
Alexis Huntc5575cc2011-02-26 19:13:13 +00002210
Alexis Hunt1d792652011-01-08 20:30:50 +00002211TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002212 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002213 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002214 else
Eugene Zelenko7855e772018-04-03 00:11:50 +00002215 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002216}
2217
Alexis Hunt1d792652011-01-08 20:30:50 +00002218const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002219 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00002220 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002221 else
Craig Topper36250ad2014-05-12 05:36:57 +00002222 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002223}
2224
Alexis Hunt1d792652011-01-08 20:30:50 +00002225SourceLocation CXXCtorInitializer::getSourceLocation() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002226 if (isInClassMemberInitializer())
2227 return getAnyMember()->getLocation();
Fangrui Song6907ce22018-07-30 19:24:48 +00002228
David Blaikiea81d4102015-01-18 00:12:58 +00002229 if (isAnyMemberInitializer())
2230 return getMemberLocation();
2231
Eugene Zelenko7855e772018-04-03 00:11:50 +00002232 if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>())
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002233 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
Fangrui Song6907ce22018-07-30 19:24:48 +00002234
Eugene Zelenko7855e772018-04-03 00:11:50 +00002235 return {};
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002236}
2237
Alexis Hunt1d792652011-01-08 20:30:50 +00002238SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00002239 if (isInClassMemberInitializer()) {
2240 FieldDecl *D = getAnyMember();
2241 if (Expr *I = D->getInClassInitializer())
2242 return I->getSourceRange();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002243 return {};
Richard Smith938f40b2011-06-11 17:19:42 +00002244 }
2245
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00002246 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00002247}
2248
Erich Keane9c665062018-08-01 21:02:40 +00002249CXXConstructorDecl::CXXConstructorDecl(
2250 ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc,
2251 const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo,
2252 bool isExplicitSpecified, bool isInline, bool isImplicitlyDeclared,
2253 bool isConstexpr, InheritedConstructor Inherited)
2254 : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo,
2255 SC_None, isInline, isConstexpr, SourceLocation()) {
2256 setNumCtorInitializers(0);
2257 setInheritingConstructor(static_cast<bool>(Inherited));
2258 setImplicit(isImplicitlyDeclared);
2259 if (Inherited)
2260 *getTrailingObjects<InheritedConstructor>() = Inherited;
2261 setExplicitSpecified(isExplicitSpecified);
2262}
2263
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002264void CXXConstructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002265
Richard Smith5179eb72016-06-28 19:03:57 +00002266CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C,
2267 unsigned ID,
2268 bool Inherited) {
2269 unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited);
2270 auto *Result = new (C, ID, Extra) CXXConstructorDecl(
2271 C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr,
2272 false, false, false, false, InheritedConstructor());
Erich Keane9c665062018-08-01 21:02:40 +00002273 Result->setInheritingConstructor(Inherited);
Richard Smith5179eb72016-06-28 19:03:57 +00002274 return Result;
Chris Lattnerca025db2010-05-07 21:43:38 +00002275}
2276
2277CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00002278CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002279 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002280 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002281 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002282 bool isExplicit, bool isInline,
Richard Smith5179eb72016-06-28 19:03:57 +00002283 bool isImplicitlyDeclared, bool isConstexpr,
2284 InheritedConstructor Inherited) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002285 assert(NameInfo.getName().getNameKind()
2286 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002287 "Name must refer to a constructor");
Richard Smith5179eb72016-06-28 19:03:57 +00002288 unsigned Extra =
2289 additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0);
2290 return new (C, RD, Extra) CXXConstructorDecl(
2291 C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline,
2292 isImplicitlyDeclared, isConstexpr, Inherited);
Douglas Gregor61956c42008-10-31 09:07:45 +00002293}
2294
Richard Smithc2bb8182015-03-24 06:36:48 +00002295CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const {
2296 return CtorInitializers.get(getASTContext().getExternalSource());
2297}
2298
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002299CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
2300 assert(isDelegatingConstructor() && "Not a delegating constructor!");
2301 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
Eugene Zelenko7855e772018-04-03 00:11:50 +00002302 if (const auto *Construct = dyn_cast<CXXConstructExpr>(E))
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002303 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00002304
2305 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00002306}
2307
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002308bool CXXConstructorDecl::isDefaultConstructor() const {
2309 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002310 // A default constructor for a class X is a constructor of class
2311 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002312 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00002313 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002314}
2315
Mike Stump11289f42009-09-09 15:08:12 +00002316bool
Douglas Gregor507eb872009-12-22 00:34:07 +00002317CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00002318 return isCopyOrMoveConstructor(TypeQuals) &&
2319 getParamDecl(0)->getType()->isLValueReferenceType();
2320}
2321
2322bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
2323 return isCopyOrMoveConstructor(TypeQuals) &&
2324 getParamDecl(0)->getType()->isRValueReferenceType();
2325}
2326
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002327/// Determine whether this is a copy or move constructor.
Douglas Gregorf282a762011-01-21 19:38:21 +00002328bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002329 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00002330 // A non-template constructor for class X is a copy constructor
2331 // if its first parameter is of type X&, const X&, volatile X& or
2332 // const volatile X&, and either there are no other parameters
2333 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00002334 // C++0x [class.copy]p3:
2335 // A non-template constructor for class X is a move constructor if its
Fangrui Song6907ce22018-07-30 19:24:48 +00002336 // first parameter is of type X&&, const X&&, volatile X&&, or
2337 // const volatile X&&, and either there are no other parameters or else
Douglas Gregorf282a762011-01-21 19:38:21 +00002338 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002339 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00002340 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002341 (getPrimaryTemplate() != nullptr) ||
2342 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002343 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002344
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002345 const ParmVarDecl *Param = getParamDecl(0);
Fangrui Song6907ce22018-07-30 19:24:48 +00002346
2347 // Do we have a reference type?
Eugene Zelenko7855e772018-04-03 00:11:50 +00002348 const auto *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00002349 if (!ParamRefType)
2350 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002351
Douglas Gregorff7028a2009-11-13 23:59:09 +00002352 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00002353 ASTContext &Context = getASTContext();
Fangrui Song6907ce22018-07-30 19:24:48 +00002354
Douglas Gregorff7028a2009-11-13 23:59:09 +00002355 CanQualType PointeeType
2356 = Context.getCanonicalType(ParamRefType->getPointeeType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002357 CanQualType ClassTy
Douglas Gregorf70b2b42009-09-15 20:50:23 +00002358 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002359 if (PointeeType.getUnqualifiedType() != ClassTy)
2360 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002361
John McCall8ccfcb52009-09-24 19:53:00 +00002362 // FIXME: other qualifiers?
Fangrui Song6907ce22018-07-30 19:24:48 +00002363
Douglas Gregorf282a762011-01-21 19:38:21 +00002364 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002365 TypeQuals = PointeeType.getCVRQualifiers();
Fangrui Song6907ce22018-07-30 19:24:48 +00002366 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00002367}
2368
Anders Carlssond20e7952009-08-28 16:57:08 +00002369bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002370 // C++ [class.conv.ctor]p1:
2371 // A constructor declared without the function-specifier explicit
2372 // that can be called with a single parameter specifies a
2373 // conversion from the type of its first parameter to the type of
2374 // its class. Such a constructor is called a converting
2375 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00002376 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002377 return false;
2378
Mike Stump11289f42009-09-09 15:08:12 +00002379 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00002380 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002381 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00002382 (getNumParams() > 1 &&
2383 (getParamDecl(1)->hasDefaultArg() ||
2384 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00002385}
Douglas Gregor61956c42008-10-31 09:07:45 +00002386
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00002387bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00002388 if ((getNumParams() < 1) ||
2389 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00002390 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00002391 return false;
2392
2393 const ParmVarDecl *Param = getParamDecl(0);
2394
2395 ASTContext &Context = getASTContext();
2396 CanQualType ParamType = Context.getCanonicalType(Param->getType());
Fangrui Song6907ce22018-07-30 19:24:48 +00002397
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002398 // Is it the same as our class type?
Fangrui Song6907ce22018-07-30 19:24:48 +00002399 CanQualType ClassTy
Douglas Gregorffe14e32009-11-14 01:20:54 +00002400 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
2401 if (ParamType.getUnqualifiedType() != ClassTy)
2402 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00002403
2404 return true;
Douglas Gregorffe14e32009-11-14 01:20:54 +00002405}
2406
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002407void CXXDestructorDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002408
Douglas Gregor831c93f2008-11-05 20:51:48 +00002409CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002410CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002411 return new (C, ID)
2412 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
2413 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00002414}
2415
2416CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00002417CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002418 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002419 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002420 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00002421 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002422 assert(NameInfo.getName().getNameKind()
2423 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002424 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00002425 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002426 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00002427}
2428
Richard Smith5b349582017-10-13 01:55:36 +00002429void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) {
Richard Smithf8134002015-03-10 01:41:22 +00002430 auto *First = cast<CXXDestructorDecl>(getFirstDecl());
2431 if (OD && !First->OperatorDelete) {
2432 First->OperatorDelete = OD;
Richard Smith5b349582017-10-13 01:55:36 +00002433 First->OperatorDeleteThisArg = ThisArg;
Richard Smithf8134002015-03-10 01:41:22 +00002434 if (auto *L = getASTMutationListener())
Richard Smith5b349582017-10-13 01:55:36 +00002435 L->ResolvedOperatorDelete(First, OD, ThisArg);
Richard Smithf8134002015-03-10 01:41:22 +00002436 }
2437}
2438
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002439void CXXConversionDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002440
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002441CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00002442CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002443 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002444 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00002445 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00002446 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00002447}
2448
2449CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002450CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002451 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002452 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00002453 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00002454 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00002455 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002456 assert(NameInfo.getName().getNameKind()
2457 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00002458 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00002459 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00002460 isInline, isExplicit, isConstexpr,
2461 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002462}
2463
Douglas Gregord3b672c2012-02-16 01:06:16 +00002464bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
2465 return isImplicit() && getParent()->isLambda() &&
2466 getConversionType()->isBlockPointerType();
2467}
2468
Erich Keanec9d29902018-08-01 21:16:54 +00002469LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc,
2470 SourceLocation LangLoc, LanguageIDs lang,
2471 bool HasBraces)
2472 : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec),
2473 ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) {
2474 setLanguage(lang);
2475 LinkageSpecDeclBits.HasBraces = HasBraces;
2476}
2477
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002478void LinkageSpecDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002479
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00002480LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00002481 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002482 SourceLocation ExternLoc,
2483 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00002484 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00002485 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00002486 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00002487}
Douglas Gregor889ceb72009-02-03 19:21:40 +00002488
Richard Smithf7981722013-11-22 09:01:48 +00002489LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
2490 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002491 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
2492 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002493}
2494
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002495void UsingDirectiveDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002496
Douglas Gregor889ceb72009-02-03 19:21:40 +00002497UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
2498 SourceLocation L,
2499 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00002500 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002501 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00002502 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00002503 DeclContext *CommonAncestor) {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002504 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used))
Sebastian Redla6602e92009-11-23 15:34:23 +00002505 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00002506 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
2507 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00002508}
2509
Richard Smithf7981722013-11-22 09:01:48 +00002510UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
2511 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002512 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
2513 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002514 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002515 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002516}
2517
Sebastian Redla6602e92009-11-23 15:34:23 +00002518NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
Eugene Zelenko7855e772018-04-03 00:11:50 +00002519 if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002520 return NA->getNamespace();
2521 return cast_or_null<NamespaceDecl>(NominatedNamespace);
2522}
2523
Richard Smith053f6c62014-05-16 23:01:30 +00002524NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
2525 SourceLocation StartLoc, SourceLocation IdLoc,
2526 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
2527 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002528 redeclarable_base(C), LocStart(StartLoc),
Richard Smith053f6c62014-05-16 23:01:30 +00002529 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00002530 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00002531
Douglas Gregore57e7522012-01-07 09:11:48 +00002532 if (PrevDecl)
2533 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
2534}
2535
Douglas Gregor72172e92012-01-05 21:55:30 +00002536NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002537 bool Inline, SourceLocation StartLoc,
2538 SourceLocation IdLoc, IdentifierInfo *Id,
2539 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00002540 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
2541 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00002542}
2543
2544NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002545 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002546 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002547}
2548
Chandler Carruth21c90602015-12-30 03:24:14 +00002549NamespaceDecl *NamespaceDecl::getOriginalNamespace() {
2550 if (isFirstDecl())
2551 return this;
2552
2553 return AnonOrFirstNamespaceAndInline.getPointer();
2554}
2555
2556const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const {
2557 if (isFirstDecl())
2558 return this;
2559
2560 return AnonOrFirstNamespaceAndInline.getPointer();
2561}
2562
2563bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); }
2564
Richard Smithd7af8a32014-05-10 01:17:36 +00002565NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
2566 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002567}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002568
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002569NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
2570 return getPreviousDecl();
2571}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002572
Rafael Espindola8ef7f682013-11-26 15:12:20 +00002573NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2574 return getMostRecentDecl();
2575}
2576
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002577void NamespaceAliasDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002578
Richard Smithf4634362014-09-03 23:11:22 +00002579NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2580 return getNextRedeclaration();
2581}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002582
Richard Smithf4634362014-09-03 23:11:22 +00002583NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2584 return getPreviousDecl();
2585}
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002586
Richard Smithf4634362014-09-03 23:11:22 +00002587NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2588 return getMostRecentDecl();
2589}
2590
Mike Stump11289f42009-09-09 15:08:12 +00002591NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002592 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002593 SourceLocation AliasLoc,
2594 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002595 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002596 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002597 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002598 // FIXME: Preserve the aliased namespace as written.
Eugene Zelenko7855e772018-04-03 00:11:50 +00002599 if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
Sebastian Redla6602e92009-11-23 15:34:23 +00002600 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002601 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002602 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002603}
2604
Douglas Gregor72172e92012-01-05 21:55:30 +00002605NamespaceAliasDecl *
2606NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002607 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002608 SourceLocation(), nullptr,
2609 NestedNameSpecifierLoc(),
2610 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002611}
2612
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002613void UsingShadowDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002614
Richard Smith5179eb72016-06-28 19:03:57 +00002615UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC,
2616 SourceLocation Loc, UsingDecl *Using,
2617 NamedDecl *Target)
2618 : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()),
Eugene Zelenko7855e772018-04-03 00:11:50 +00002619 redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) {
Richard Smith5179eb72016-06-28 19:03:57 +00002620 if (Target)
Richard Smitha263c342018-01-06 01:07:05 +00002621 setTargetDecl(Target);
Richard Smith5179eb72016-06-28 19:03:57 +00002622 setImplicit();
2623}
2624
2625UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty)
2626 : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()),
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002627 redeclarable_base(C) {}
Richard Smith5179eb72016-06-28 19:03:57 +00002628
Douglas Gregor72172e92012-01-05 21:55:30 +00002629UsingShadowDecl *
2630UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith5179eb72016-06-28 19:03:57 +00002631 return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +00002632}
2633
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002634UsingDecl *UsingShadowDecl::getUsingDecl() const {
2635 const UsingShadowDecl *Shadow = this;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002636 while (const auto *NextShadow =
2637 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002638 Shadow = NextShadow;
2639 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2640}
2641
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002642void ConstructorUsingShadowDecl::anchor() {}
Richard Smith5179eb72016-06-28 19:03:57 +00002643
2644ConstructorUsingShadowDecl *
2645ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC,
2646 SourceLocation Loc, UsingDecl *Using,
2647 NamedDecl *Target, bool IsVirtual) {
2648 return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target,
2649 IsVirtual);
2650}
2651
2652ConstructorUsingShadowDecl *
2653ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2654 return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell());
2655}
2656
2657CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const {
2658 return getUsingDecl()->getQualifier()->getAsRecordDecl();
2659}
2660
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002661void UsingDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002662
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002663void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2664 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2665 "declaration already in set");
2666 assert(S->getUsingDecl() == this);
2667
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002668 if (FirstUsingShadow.getPointer())
2669 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2670 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002671}
2672
2673void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2674 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2675 "declaration not in set");
2676 assert(S->getUsingDecl() == this);
2677
2678 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2679
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002680 if (FirstUsingShadow.getPointer() == S) {
2681 FirstUsingShadow.setPointer(
2682 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002683 S->UsingOrNextShadow = this;
2684 return;
2685 }
2686
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002687 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002688 while (Prev->UsingOrNextShadow != S)
2689 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2690 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2691 S->UsingOrNextShadow = this;
2692}
2693
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002694UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2695 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002696 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002697 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002698 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002699}
2700
Douglas Gregor72172e92012-01-05 21:55:30 +00002701UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002702 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2703 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2704 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002705}
2706
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002707SourceRange UsingDecl::getSourceRange() const {
2708 SourceLocation Begin = isAccessDeclaration()
2709 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2710 return SourceRange(Begin, getNameInfo().getEndLoc());
2711}
2712
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002713void UsingPackDecl::anchor() {}
Richard Smith151c4562016-12-20 21:35:28 +00002714
2715UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC,
2716 NamedDecl *InstantiatedFrom,
2717 ArrayRef<NamedDecl *> UsingDecls) {
2718 size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size());
2719 return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls);
2720}
2721
2722UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID,
2723 unsigned NumExpansions) {
2724 size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions);
2725 auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None);
2726 Result->NumExpansions = NumExpansions;
2727 auto *Trail = Result->getTrailingObjects<NamedDecl *>();
2728 for (unsigned I = 0; I != NumExpansions; ++I)
2729 new (Trail + I) NamedDecl*(nullptr);
2730 return Result;
2731}
2732
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002733void UnresolvedUsingValueDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002734
John McCalle61f2ba2009-11-18 02:36:19 +00002735UnresolvedUsingValueDecl *
2736UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2737 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002738 NestedNameSpecifierLoc QualifierLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002739 const DeclarationNameInfo &NameInfo,
2740 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002741 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002742 QualifierLoc, NameInfo,
2743 EllipsisLoc);
John McCalle61f2ba2009-11-18 02:36:19 +00002744}
2745
Douglas Gregor72172e92012-01-05 21:55:30 +00002746UnresolvedUsingValueDecl *
2747UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002748 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2749 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002750 NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002751 DeclarationNameInfo(),
2752 SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002753}
2754
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002755SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2756 SourceLocation Begin = isAccessDeclaration()
2757 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2758 return SourceRange(Begin, getNameInfo().getEndLoc());
2759}
2760
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002761void UnresolvedUsingTypenameDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002762
John McCalle61f2ba2009-11-18 02:36:19 +00002763UnresolvedUsingTypenameDecl *
2764UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2765 SourceLocation UsingLoc,
2766 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002767 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002768 SourceLocation TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002769 DeclarationName TargetName,
2770 SourceLocation EllipsisLoc) {
Richard Smithf7981722013-11-22 09:01:48 +00002771 return new (C, DC) UnresolvedUsingTypenameDecl(
2772 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
Richard Smith151c4562016-12-20 21:35:28 +00002773 TargetName.getAsIdentifierInfo(), EllipsisLoc);
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002774}
2775
Douglas Gregor72172e92012-01-05 21:55:30 +00002776UnresolvedUsingTypenameDecl *
2777UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002778 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002779 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
Richard Smith151c4562016-12-20 21:35:28 +00002780 SourceLocation(), nullptr, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00002781}
2782
Eugene Zelenko4a5354f2017-11-14 23:13:32 +00002783void StaticAssertDecl::anchor() {}
David Blaikie68e081d2011-12-20 02:48:34 +00002784
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002785StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002786 SourceLocation StaticAssertLoc,
2787 Expr *AssertExpr,
2788 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002789 SourceLocation RParenLoc,
2790 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002791 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2792 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002793}
2794
Richard Smithf7981722013-11-22 09:01:48 +00002795StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002796 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002797 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2798 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002799}
2800
Richard Smithbdb84f32016-07-22 23:36:59 +00002801void BindingDecl::anchor() {}
2802
2803BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC,
2804 SourceLocation IdLoc, IdentifierInfo *Id) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002805 return new (C, DC) BindingDecl(DC, IdLoc, Id);
Richard Smithbdb84f32016-07-22 23:36:59 +00002806}
2807
2808BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith32cb8c92016-08-12 00:53:41 +00002809 return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr);
Richard Smithbdb84f32016-07-22 23:36:59 +00002810}
2811
Richard Smith97fcf4b2016-08-14 23:15:52 +00002812VarDecl *BindingDecl::getHoldingVar() const {
2813 Expr *B = getBinding();
2814 if (!B)
2815 return nullptr;
2816 auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit());
2817 if (!DRE)
2818 return nullptr;
2819
2820 auto *VD = dyn_cast<VarDecl>(DRE->getDecl());
2821 assert(VD->isImplicit() && "holding var for binding decl not implicit");
2822 return VD;
2823}
2824
Richard Smithbdb84f32016-07-22 23:36:59 +00002825void DecompositionDecl::anchor() {}
2826
2827DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
2828 SourceLocation StartLoc,
2829 SourceLocation LSquareLoc,
2830 QualType T, TypeSourceInfo *TInfo,
2831 StorageClass SC,
2832 ArrayRef<BindingDecl *> Bindings) {
2833 size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
2834 return new (C, DC, Extra)
2835 DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
2836}
2837
2838DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
2839 unsigned ID,
2840 unsigned NumBindings) {
2841 size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
Richard Smith7b76d812016-08-12 02:21:25 +00002842 auto *Result = new (C, ID, Extra)
2843 DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
2844 QualType(), nullptr, StorageClass(), None);
Richard Smithbdb84f32016-07-22 23:36:59 +00002845 // Set up and clean out the bindings array.
2846 Result->NumBindings = NumBindings;
2847 auto *Trail = Result->getTrailingObjects<BindingDecl *>();
2848 for (unsigned I = 0; I != NumBindings; ++I)
2849 new (Trail + I) BindingDecl*(nullptr);
2850 return Result;
2851}
2852
Richard Smith7873de02016-08-11 22:25:46 +00002853void DecompositionDecl::printName(llvm::raw_ostream &os) const {
2854 os << '[';
2855 bool Comma = false;
Eugene Zelenko7855e772018-04-03 00:11:50 +00002856 for (const auto *B : bindings()) {
Richard Smith7873de02016-08-11 22:25:46 +00002857 if (Comma)
2858 os << ", ";
2859 B->printName(os);
2860 Comma = true;
2861 }
2862 os << ']';
2863}
2864
Richard Smithf7981722013-11-22 09:01:48 +00002865MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2866 SourceLocation L, DeclarationName N,
2867 QualType T, TypeSourceInfo *TInfo,
2868 SourceLocation StartL,
2869 IdentifierInfo *Getter,
2870 IdentifierInfo *Setter) {
2871 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2872}
2873
2874MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2875 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002876 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2877 DeclarationName(), QualType(), nullptr,
2878 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002879}
2880
Anders Carlsson6750d162009-03-26 23:46:50 +00002881static const char *getAccessName(AccessSpecifier AS) {
2882 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002883 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002884 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002885 case AS_public:
2886 return "public";
2887 case AS_private:
2888 return "private";
2889 case AS_protected:
2890 return "protected";
2891 }
David Blaikief47fa302012-01-17 02:30:50 +00002892 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002893}
2894
2895const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2896 AccessSpecifier AS) {
2897 return DB << getAccessName(AS);
2898}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002899
2900const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2901 AccessSpecifier AS) {
2902 return DB << getAccessName(AS);
2903}