blob: 0ee3e3e90407226fa4abcef895b8b632f3129b46 [file] [log] [blame]
Ted Kremenek21475702008-09-05 17:16:31 +00001//===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===//
2//
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//===----------------------------------------------------------------------===//
Ted Kremenek21475702008-09-05 17:16:31 +000013#include "clang/AST/DeclCXX.h"
14#include "clang/AST/ASTContext.h"
Faisal Vali2b391ab2013-09-26 19:54:12 +000015#include "clang/AST/ASTLambda.h"
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +000016#include "clang/AST/ASTMutationListener.h"
Douglas Gregor8fb95122010-09-29 00:15:42 +000017#include "clang/AST/CXXInheritance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclTemplate.h"
Anders Carlsson5bbe1d72009-03-14 00:25:26 +000019#include "clang/AST/Expr.h"
Douglas Gregord73f3dd2011-11-01 01:16:03 +000020#include "clang/AST/ExprCXX.h"
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +000021#include "clang/AST/TypeLoc.h"
Douglas Gregorb6acda02008-11-12 23:21:09 +000022#include "clang/Basic/IdentifierTable.h"
Douglas Gregor74a34442008-12-23 21:31:30 +000023#include "llvm/ADT/STLExtras.h"
Fariborz Jahaniana9540492009-09-12 19:52:10 +000024#include "llvm/ADT/SmallPtrSet.h"
Ted Kremenek21475702008-09-05 17:16:31 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Decl Allocation/Deallocation Method Implementations
29//===----------------------------------------------------------------------===//
Douglas Gregor5101c242008-12-05 18:15:24 +000030
David Blaikie68e081d2011-12-20 02:48:34 +000031void AccessSpecDecl::anchor() { }
32
Douglas Gregor72172e92012-01-05 21:55:30 +000033AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +000034 return new (C, ID) AccessSpecDecl(EmptyShell());
Douglas Gregor72172e92012-01-05 21:55:30 +000035}
36
Richard Smitha4ba74c2013-08-30 04:46:40 +000037void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
38 ExternalASTSource *Source = C.getExternalSource();
39 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
40 assert(Source && "getFromExternalSource with no external source");
41
42 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
43 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
44 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
45 Impl.Decls.setLazy(false);
46}
47
John McCall67da35c2010-02-04 22:26:26 +000048CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith328aae52012-11-30 05:11:39 +000049 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedmanc80ca682009-08-15 22:23:00 +000050 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruth583edf82011-04-30 10:07:30 +000051 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carruthb1963742011-04-30 09:17:45 +000052 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Richard Smithab44d5b2013-12-10 08:25:00 +000053 HasMutableFields(false), HasVariantMembers(false), HasOnlyCMembers(true),
Richard Smith593f9932012-12-08 02:01:17 +000054 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smith6b02d462012-12-08 08:32:28 +000055 NeedOverloadResolutionForMoveConstructor(false),
56 NeedOverloadResolutionForMoveAssignment(false),
57 NeedOverloadResolutionForDestructor(false),
58 DefaultedMoveConstructorIsDeleted(false),
59 DefaultedMoveAssignmentIsDeleted(false),
60 DefaultedDestructorIsDeleted(false),
Richard Smith328aae52012-11-30 05:11:39 +000061 HasTrivialSpecialMembers(SMF_All),
Richard Smith92f241f2012-12-08 02:53:02 +000062 DeclaredNonTrivialSpecialMembers(0),
Richard Smith328aae52012-11-30 05:11:39 +000063 HasIrrelevantDestructor(true),
Richard Smithcc36f692011-12-22 02:22:31 +000064 HasConstexprNonCopyMoveConstructor(false),
65 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith328aae52012-11-30 05:11:39 +000066 HasConstexprDefaultConstructor(false),
Alexis Huntf479f1b2011-05-09 18:22:59 +000067 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith328aae52012-11-30 05:11:39 +000068 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smith1c33fe82012-11-28 06:23:12 +000069 ImplicitCopyConstructorHasConstParam(true),
70 ImplicitCopyAssignmentHasConstParam(true),
71 HasDeclaredCopyConstructorWithConstParam(false),
72 HasDeclaredCopyAssignmentWithConstParam(false),
David Majnemer5ef4fe72014-06-13 06:43:46 +000073 IsLambda(false), IsParsingBaseSpecifiers(false), NumBases(0), NumVBases(0),
74 Bases(), VBases(),
Richard Smith68ad0e72013-06-26 02:41:25 +000075 Definition(D), FirstFriend() {
John McCall67da35c2010-02-04 22:26:26 +000076}
77
Benjamin Kramer300c0632012-07-04 17:03:33 +000078CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
79 return Bases.get(Definition->getASTContext().getExternalSource());
80}
81
82CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
83 return VBases.get(Definition->getASTContext().getExternalSource());
84}
85
Richard Smith053f6c62014-05-16 23:01:30 +000086CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C,
87 DeclContext *DC, SourceLocation StartLoc,
88 SourceLocation IdLoc, IdentifierInfo *Id,
89 CXXRecordDecl *PrevDecl)
90 : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl),
91 DefinitionData(PrevDecl ? PrevDecl->DefinitionData
Richard Smith64c06302014-05-22 23:19:02 +000092 : DefinitionDataPtr(this)),
Richard Smith053f6c62014-05-16 23:01:30 +000093 TemplateOrInstantiation() {}
Douglas Gregorb6acda02008-11-12 23:21:09 +000094
Jay Foad39c79802011-01-12 09:06:06 +000095CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000096 DeclContext *DC, SourceLocation StartLoc,
97 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000098 CXXRecordDecl* PrevDecl,
99 bool DelayTypeCreation) {
Richard Smith053f6c62014-05-16 23:01:30 +0000100 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc,
Richard Smithf7981722013-11-22 09:01:48 +0000101 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000102 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000103
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000104 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000105 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000106 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000107 return R;
108}
109
Richard Smith053f6c62014-05-16 23:01:30 +0000110CXXRecordDecl *
111CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
112 TypeSourceInfo *Info, SourceLocation Loc,
113 bool Dependent, bool IsGeneric,
114 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000115 CXXRecordDecl *R =
Richard Smith053f6c62014-05-16 23:01:30 +0000116 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc,
117 nullptr, nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000118 R->IsBeingDefined = true;
Richard Smith64c06302014-05-22 23:19:02 +0000119 R->DefinitionData =
Richard Smith053f6c62014-05-16 23:01:30 +0000120 new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric,
Richard Smith64c06302014-05-22 23:19:02 +0000121 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000122 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000123 R->setImplicit(true);
Craig Topper36250ad2014-05-12 05:36:57 +0000124 C.getTypeDeclType(R, /*PrevDecl=*/nullptr);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000125 return R;
126}
127
Douglas Gregor72172e92012-01-05 21:55:30 +0000128CXXRecordDecl *
129CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000130 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
Richard Smith053f6c62014-05-16 23:01:30 +0000131 CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000132 nullptr, nullptr);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000133 R->MayHaveOutOfDateDef = false;
134 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000135}
136
Mike Stump11289f42009-09-09 15:08:12 +0000137void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000138CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000139 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000140 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000141
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000142 if (!data().Bases.isOffset() && data().NumBases > 0)
143 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000144
Richard Smithaed32b42011-10-18 20:08:55 +0000145 if (NumBases) {
146 // C++ [dcl.init.aggr]p1:
147 // An aggregate is [...] a class with [...] no base classes [...].
148 data().Aggregate = false;
149
150 // C++ [class]p4:
151 // A POD-struct is an aggregate class...
152 data().PlainOldData = false;
153 }
154
Anders Carlssonabb20e62010-03-29 05:13:12 +0000155 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000156 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000157
158 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000159 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000160
John McCall67da35c2010-02-04 22:26:26 +0000161 data().Bases = new(C) CXXBaseSpecifier [NumBases];
162 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000163 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000164 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000165 // Keep track of inherited vbases for this base class.
166 const CXXBaseSpecifier *Base = Bases[i];
167 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000168 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000169 if (BaseType->isDependentType())
170 continue;
171 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000172 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000173
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000174 // A class with a non-empty base class is not empty.
175 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000176 if (!BaseClassDecl->isEmpty()) {
177 if (!data().Empty) {
178 // C++0x [class]p7:
179 // A standard-layout class is a class that:
180 // [...]
181 // -- either has no non-static data members in the most derived
182 // class and at most one base class with non-static data members,
183 // or has no base classes with non-static data members, and
184 // If this is the second non-empty base, then neither of these two
185 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000186 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000187 }
188
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000189 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000190 data().HasNoNonEmptyBases = false;
191 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000192
Douglas Gregor11c024b2010-09-28 20:50:54 +0000193 // C++ [class.virtual]p1:
194 // A class that declares or inherits a virtual function is called a
195 // polymorphic class.
196 if (BaseClassDecl->isPolymorphic())
197 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000198
Chandler Carruthb1963742011-04-30 09:17:45 +0000199 // C++0x [class]p7:
200 // A standard-layout class is a class that: [...]
201 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000202 if (!BaseClassDecl->isStandardLayout())
203 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000204
Chandler Carruthe71d0622011-04-24 02:49:34 +0000205 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000206 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000207 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000208
Anders Carlssonabb20e62010-03-29 05:13:12 +0000209 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000210 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000211 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000212 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) {
Aaron Ballman445a9392014-03-13 16:15:17 +0000213 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000214
215 // C++11 [class.copy]p8:
216 // The implicitly-declared copy constructor for a class X will have
217 // the form 'X::X(const X&)' if each [...] virtual base class B of X
218 // has a copy constructor whose first parameter is of type
219 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000220 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000221 if (!VBaseDecl->hasCopyConstructorWithConstParam())
222 data().ImplicitCopyConstructorHasConstParam = false;
223 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000224 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000225
226 if (Base->isVirtual()) {
227 // Add this base if it's not already in the list.
David Blaikie82e95a32014-11-19 07:49:47 +0000228 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second)
Richard Smith1c33fe82012-11-28 06:23:12 +0000229 VBases.push_back(Base);
230
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000231 // C++0x [meta.unary.prop] is_empty:
232 // T is a class type, but not a union type, with ... no virtual base
233 // classes
234 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000235
Richard Smith328aae52012-11-30 05:11:39 +0000236 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
237 // A [default constructor, copy/move constructor, or copy/move assignment
238 // operator for a class X] is trivial [...] if:
239 // -- class X has [...] no virtual base classes
240 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000241
242 // C++0x [class]p7:
243 // A standard-layout class is a class that: [...]
244 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000245 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000246
247 // C++11 [dcl.constexpr]p4:
248 // In the definition of a constexpr constructor [...]
249 // -- the class shall not have any virtual base classes
250 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000251 } else {
252 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000253 // A default constructor is trivial [...] if:
254 // -- all the direct base classes of its class have trivial default
255 // constructors.
256 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000257 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
258
Chandler Carruthad7d4042011-04-23 23:10:33 +0000259 // C++0x [class.copy]p13:
260 // A copy/move constructor for class X is trivial if [...]
261 // [...]
262 // -- the constructor selected to copy/move each direct base class
263 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000264 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000265 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000266 // If the base class doesn't have a simple move constructor, we'll eagerly
267 // declare it and perform overload resolution to determine which function
268 // it actually calls. If it does have a simple move constructor, this
269 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000270 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000271 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000272
273 // C++0x [class.copy]p27:
274 // A copy/move assignment operator for class X is trivial if [...]
275 // [...]
276 // -- the assignment operator selected to copy/move each direct base
277 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000278 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000279 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000280 // If the base class doesn't have a simple move assignment, we'll eagerly
281 // declare it and perform overload resolution to determine which function
282 // it actually calls. If it does have a simple move assignment, this
283 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000284 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000285 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000286
287 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000288 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000289 // requirements of a constexpr constructor, the implicitly-defined
290 // default constructor is constexpr.
291 if (!BaseClassDecl->hasConstexprDefaultConstructor())
292 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000293 }
Richard Smith92f241f2012-12-08 02:53:02 +0000294
Douglas Gregor11c024b2010-09-28 20:50:54 +0000295 // C++ [class.ctor]p3:
296 // A destructor is trivial if all the direct base classes of its class
297 // have trivial destructors.
298 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000299 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000300
301 if (!BaseClassDecl->hasIrrelevantDestructor())
302 data().HasIrrelevantDestructor = false;
303
Richard Smith1c33fe82012-11-28 06:23:12 +0000304 // C++11 [class.copy]p18:
305 // The implicitly-declared copy assignment oeprator for a class X will
306 // have the form 'X& X::operator=(const X&)' if each direct base class B
307 // of X has a copy assignment operator whose parameter is of type 'const
308 // B&', 'const volatile B&', or 'B' [...]
309 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
310 data().ImplicitCopyAssignmentHasConstParam = false;
311
312 // C++11 [class.copy]p8:
313 // The implicitly-declared copy constructor for a class X will have
314 // the form 'X::X(const X&)' if each direct [...] base class B of X
315 // has a copy constructor whose first parameter is of type
316 // 'const B&' or 'const volatile B&' [...]
317 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
318 data().ImplicitCopyConstructorHasConstParam = false;
319
John McCall31168b02011-06-15 23:02:42 +0000320 // A class has an Objective-C object member if... or any of its bases
321 // has an Objective-C object member.
322 if (BaseClassDecl->hasObjectMember())
323 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000324
325 if (BaseClassDecl->hasVolatileMember())
326 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000327
Douglas Gregor61226d32011-05-13 01:05:07 +0000328 // Keep track of the presence of mutable fields.
329 if (BaseClassDecl->hasMutableFields())
330 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000331
332 if (BaseClassDecl->hasUninitializedReferenceMember())
333 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000334
335 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000336 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000337
David Majnemer5ef4fe72014-06-13 06:43:46 +0000338 if (VBases.empty()) {
339 data().IsParsingBaseSpecifiers = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000340 return;
David Majnemer5ef4fe72014-06-13 06:43:46 +0000341 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000342
343 // Create base specifier for any direct or indirect virtual bases.
344 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
345 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000346 for (int I = 0, E = VBases.size(); I != E; ++I) {
347 QualType Type = VBases[I]->getType();
348 if (!Type->isDependentType())
349 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000350 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000351 }
David Majnemer5ef4fe72014-06-13 06:43:46 +0000352
353 data().IsParsingBaseSpecifiers = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000354}
355
356void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
357 // C++11 [class.copy]p11:
358 // A defaulted copy/move constructor for a class X is defined as
359 // deleted if X has:
360 // -- a direct or virtual base class B that cannot be copied/moved [...]
361 // -- a non-static data member of class type M (or array thereof)
362 // that cannot be copied or moved [...]
363 if (!Subobj->hasSimpleMoveConstructor())
364 data().NeedOverloadResolutionForMoveConstructor = true;
365
366 // C++11 [class.copy]p23:
367 // A defaulted copy/move assignment operator for a class X is defined as
368 // deleted if X has:
369 // -- a direct or virtual base class B that cannot be copied/moved [...]
370 // -- a non-static data member of class type M (or array thereof)
371 // that cannot be copied or moved [...]
372 if (!Subobj->hasSimpleMoveAssignment())
373 data().NeedOverloadResolutionForMoveAssignment = true;
374
375 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
376 // A defaulted [ctor or dtor] for a class X is defined as
377 // deleted if X has:
378 // -- any direct or virtual base class [...] has a type with a destructor
379 // that is deleted or inaccessible from the defaulted [ctor or dtor].
380 // -- any non-static data member has a type with a destructor
381 // that is deleted or inaccessible from the defaulted [ctor or dtor].
382 if (!Subobj->hasSimpleDestructor()) {
383 data().NeedOverloadResolutionForMoveConstructor = true;
384 data().NeedOverloadResolutionForDestructor = true;
385 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000386}
387
Douglas Gregord2e6a452010-01-14 17:47:39 +0000388/// Callback function for CXXRecordDecl::forallBases that acknowledges
389/// that it saw a base class.
390static bool SawBase(const CXXRecordDecl *, void *) {
391 return true;
392}
393
394bool CXXRecordDecl::hasAnyDependentBases() const {
395 if (!isDependentContext())
396 return false;
397
Craig Topper36250ad2014-05-12 05:36:57 +0000398 return !forallBases(SawBase, nullptr);
Douglas Gregord2e6a452010-01-14 17:47:39 +0000399}
400
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000401bool CXXRecordDecl::isTriviallyCopyable() const {
402 // C++0x [class]p5:
403 // A trivially copyable class is a class that:
404 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000405 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000406 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000407 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000408 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000409 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000410 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000411 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000412 // -- has a trivial destructor.
413 if (!hasTrivialDestructor()) return false;
414
415 return true;
416}
417
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000418void CXXRecordDecl::markedVirtualFunctionPure() {
419 // C++ [class.abstract]p2:
420 // A class is abstract if it has at least one pure virtual function.
421 data().Abstract = true;
422}
423
424void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000425 if (!D->isImplicit() &&
426 !isa<FieldDecl>(D) &&
427 !isa<IndirectFieldDecl>(D) &&
428 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
429 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
430 data().HasOnlyCMembers = false;
431
432 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000433 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000434 return;
435
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000436 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
437 if (FunTmpl)
438 D = FunTmpl->getTemplatedDecl();
439
Douglas Gregora832d3e2010-09-28 19:45:33 +0000440 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
441 if (Method->isVirtual()) {
442 // C++ [dcl.init.aggr]p1:
443 // An aggregate is an array or a class with [...] no virtual functions.
444 data().Aggregate = false;
445
446 // C++ [class]p4:
447 // A POD-struct is an aggregate class...
448 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000449
450 // Virtual functions make the class non-empty.
451 // FIXME: Standard ref?
452 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000453
454 // C++ [class.virtual]p1:
455 // A class that declares or inherits a virtual function is called a
456 // polymorphic class.
457 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000458
Richard Smith328aae52012-11-30 05:11:39 +0000459 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
460 // A [default constructor, copy/move constructor, or copy/move
461 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000462 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000463 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000464
Chandler Carruthb1963742011-04-30 09:17:45 +0000465 // C++0x [class]p7:
466 // A standard-layout class is a class that: [...]
467 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000468 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000469 }
470 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000471
Richard Smith1c33fe82012-11-28 06:23:12 +0000472 // Notify the listener if an implicit member was added after the definition
473 // was completed.
474 if (!isBeingDefined() && D->isImplicit())
475 if (ASTMutationListener *L = getASTMutationListener())
476 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000477
Richard Smith328aae52012-11-30 05:11:39 +0000478 // The kind of special member this declaration is, if any.
479 unsigned SMKind = 0;
480
Richard Smith1c33fe82012-11-28 06:23:12 +0000481 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000482 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000483 if (!Constructor->isImplicit()) {
484 // Note that we have a user-declared constructor.
485 data().UserDeclaredConstructor = true;
486
487 // C++ [class]p4:
488 // A POD-struct is an aggregate class [...]
489 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
490 // type is technically an aggregate in C++0x since it wouldn't be in 03.
491 data().PlainOldData = false;
492 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000493
Richard Smitha5285072011-09-05 02:13:09 +0000494 // Technically, "user-provided" is only defined for special member
495 // functions, but the intent of the standard is clearly that it should apply
496 // to all functions.
497 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000498
Alexis Hunt88c75c32011-05-09 21:45:35 +0000499 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000500 SMKind |= SMF_DefaultConstructor;
501
502 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000503 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000504 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000505 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000506 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000507
Chandler Carruthad7d4042011-04-23 23:10:33 +0000508 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000509 unsigned Quals;
510 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000511 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000512
513 if (Quals & Qualifiers::Const)
514 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000515 } else if (Constructor->isMoveConstructor())
516 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000517 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000518
519 // Record if we see any constexpr constructors which are neither copy
520 // nor move constructors.
521 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000522 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000523
Alexis Hunt88c75c32011-05-09 21:45:35 +0000524 // C++ [dcl.init.aggr]p1:
525 // An aggregate is an array or a class with no user-declared
526 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000527 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000528 // An aggregate is an array or a class with no user-provided
529 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000530 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000531 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000532 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000533 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000534
Richard Smith1c33fe82012-11-28 06:23:12 +0000535 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000536 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000537 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000538
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000539 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000540 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000541 // If the destructor is explicitly defaulted and not trivial or not public
542 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
543 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000544
Richard Smith1c33fe82012-11-28 06:23:12 +0000545 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000546 // A destructor is trivial if [...] the destructor is not virtual.
547 if (DD->isVirtual())
548 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000549 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000550
551 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000552 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000553 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000554 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000555
556 const ReferenceType *ParamTy =
557 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
558 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
559 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000560 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000561
Richard Smith328aae52012-11-30 05:11:39 +0000562 if (Method->isMoveAssignmentOperator())
563 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000564
Douglas Gregor457104e2010-09-29 04:25:11 +0000565 // Keep the list of conversion functions up-to-date.
566 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000567 // FIXME: We use the 'unsafe' accessor for the access specifier here,
568 // because Sema may not have set it yet. That's really just a misdesign
569 // in Sema. However, LLDB *will* have set the access specifier correctly,
570 // and adds declarations after the class is technically completed,
571 // so completeDefinition()'s overriding of the access specifiers doesn't
572 // work.
573 AccessSpecifier AS = Conversion->getAccessUnsafe();
574
Richard Smith328aae52012-11-30 05:11:39 +0000575 if (Conversion->getPrimaryTemplate()) {
576 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000577 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000578 ASTContext &Ctx = getASTContext();
579 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
580 NamedDecl *Primary =
581 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
582 if (Primary->getPreviousDecl())
583 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
584 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000585 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000586 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000587 }
588 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000589
Richard Smith328aae52012-11-30 05:11:39 +0000590 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000591 // If this is the first declaration of a special member, we no longer have
592 // an implicit trivial special member.
593 data().HasTrivialSpecialMembers &=
594 data().DeclaredSpecialMembers | ~SMKind;
595
596 if (!Method->isImplicit() && !Method->isUserProvided()) {
597 // This method is user-declared but not user-provided. We can't work out
598 // whether it's trivial yet (not until we get to the end of the class).
599 // We'll handle this method in finishedDefaultedOrDeletedMember.
600 } else if (Method->isTrivial())
601 data().HasTrivialSpecialMembers |= SMKind;
602 else
603 data().DeclaredNonTrivialSpecialMembers |= SMKind;
604
Richard Smith328aae52012-11-30 05:11:39 +0000605 // Note when we have declared a declared special member, and suppress the
606 // implicit declaration of this special member.
607 data().DeclaredSpecialMembers |= SMKind;
608
609 if (!Method->isImplicit()) {
610 data().UserDeclaredSpecialMembers |= SMKind;
611
612 // C++03 [class]p4:
613 // A POD-struct is an aggregate class that has [...] no user-defined
614 // copy assignment operator and no user-defined destructor.
615 //
616 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
617 // aggregates could not have any constructors, clear it even for an
618 // explicitly defaulted or deleted constructor.
619 // type is technically an aggregate in C++0x since it wouldn't be in 03.
620 //
621 // Also, a user-declared move assignment operator makes a class non-POD.
622 // This is an extension in C++03.
623 data().PlainOldData = false;
624 }
Richard Smith328aae52012-11-30 05:11:39 +0000625 }
626
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000627 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000628 }
Richard Smith328aae52012-11-30 05:11:39 +0000629
Douglas Gregora832d3e2010-09-28 19:45:33 +0000630 // Handle non-static data members.
631 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000632 // C++ [class.bit]p2:
633 // A declaration for a bit-field that omits the identifier declares an
634 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
635 // initialized.
636 if (Field->isUnnamedBitfield())
637 return;
638
Douglas Gregora832d3e2010-09-28 19:45:33 +0000639 // C++ [dcl.init.aggr]p1:
640 // An aggregate is an array or a class (clause 9) with [...] no
641 // private or protected non-static data members (clause 11).
642 //
643 // A POD must be an aggregate.
644 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
645 data().Aggregate = false;
646 data().PlainOldData = false;
647 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000648
649 // C++0x [class]p7:
650 // A standard-layout class is a class that:
651 // [...]
652 // -- has the same access control for all non-static data members,
653 switch (D->getAccess()) {
654 case AS_private: data().HasPrivateFields = true; break;
655 case AS_protected: data().HasProtectedFields = true; break;
656 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000657 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000658 };
659 if ((data().HasPrivateFields + data().HasProtectedFields +
660 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000661 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000662
Douglas Gregor61226d32011-05-13 01:05:07 +0000663 // Keep track of the presence of mutable fields.
664 if (Field->isMutable())
665 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000666
667 // C++11 [class.union]p8, DR1460:
668 // If X is a union, a non-static data member of X that is not an anonymous
669 // union is a variant member of X.
670 if (isUnion() && !Field->isAnonymousStructOrUnion())
671 data().HasVariantMembers = true;
672
Chandler Carruthad7d4042011-04-23 23:10:33 +0000673 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000674 // A POD struct is a class that is both a trivial class and a
675 // standard-layout class, and has no non-static data members of type
676 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000677 //
678 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
679 // that does not explicitly have no lifetime makes the class a non-POD.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000680 ASTContext &Context = getASTContext();
681 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000682 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
Ben Langmuir11eab612014-09-26 15:27:29 +0000683 if (!Context.getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000684 setHasObjectMember(true);
Ben Langmuir11eab612014-09-26 15:27:29 +0000685 } else if (T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) {
686 // Objective-C Automatic Reference Counting:
687 // If a class has a non-static data member of Objective-C pointer
688 // type (or array thereof), it is a non-POD type and its
689 // default constructor (if any), copy constructor, move constructor,
690 // copy assignment operator, move assignment operator, and destructor are
691 // non-trivial.
692 setHasObjectMember(true);
693 struct DefinitionData &Data = data();
694 Data.PlainOldData = false;
695 Data.HasTrivialSpecialMembers = 0;
696 Data.HasIrrelevantDestructor = false;
697 }
Eli Friedmana5433322013-07-20 01:06:31 +0000698 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000699 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000700
Chandler Carruthb1963742011-04-30 09:17:45 +0000701 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000702 if (!Field->hasInClassInitializer())
703 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000704
Chandler Carruthb1963742011-04-30 09:17:45 +0000705 // C++0x [class]p7:
706 // A standard-layout class is a class that:
707 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000708 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000709 }
710
Richard Smith3607ffe2012-02-13 03:54:03 +0000711 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000712 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000713 data().HasNonLiteralTypeFieldsOrBases = true;
714
Richard Smithab44d5b2013-12-10 08:25:00 +0000715 if (Field->hasInClassInitializer() ||
716 (Field->isAnonymousStructOrUnion() &&
717 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000718 data().HasInClassInitializer = true;
719
720 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000721 // A default constructor is trivial if [...] no non-static data member
722 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000723 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000724
Richard Smithe2648ba2012-05-07 01:07:30 +0000725 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000726 // An aggregate is a [...] class with [...] no
727 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000728 //
729 // This rule was removed in C++1y.
Aaron Ballmandd69ef32014-08-19 15:55:55 +0000730 if (!getASTContext().getLangOpts().CPlusPlus14)
Richard Smith852c9db2013-04-20 22:23:05 +0000731 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000732
Richard Smithe2648ba2012-05-07 01:07:30 +0000733 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000734 // A POD struct is [...] a trivial class.
735 data().PlainOldData = false;
736 }
737
Richard Smith6b02d462012-12-08 08:32:28 +0000738 // C++11 [class.copy]p23:
739 // A defaulted copy/move assignment operator for a class X is defined
740 // as deleted if X has:
741 // -- a non-static data member of reference type
742 if (T->isReferenceType())
743 data().DefaultedMoveAssignmentIsDeleted = true;
744
Douglas Gregor11c024b2010-09-28 20:50:54 +0000745 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
746 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
747 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000748 addedClassSubobject(FieldRec);
749
Richard Smithc91d12c2013-11-25 07:07:05 +0000750 // We may need to perform overload resolution to determine whether a
751 // field can be moved if it's const or volatile qualified.
752 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
753 data().NeedOverloadResolutionForMoveConstructor = true;
754 data().NeedOverloadResolutionForMoveAssignment = true;
755 }
756
Richard Smith6b02d462012-12-08 08:32:28 +0000757 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
758 // A defaulted [special member] for a class X is defined as
759 // deleted if:
760 // -- X is a union-like class that has a variant member with a
761 // non-trivial [corresponding special member]
762 if (isUnion()) {
763 if (FieldRec->hasNonTrivialMoveConstructor())
764 data().DefaultedMoveConstructorIsDeleted = true;
765 if (FieldRec->hasNonTrivialMoveAssignment())
766 data().DefaultedMoveAssignmentIsDeleted = true;
767 if (FieldRec->hasNonTrivialDestructor())
768 data().DefaultedDestructorIsDeleted = true;
769 }
770
Alexis Huntf479f1b2011-05-09 18:22:59 +0000771 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000772 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000773 // -- for all the non-static data members of its class that are of
774 // class type (or array thereof), each such class has a trivial
775 // default constructor.
776 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000777 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000778
779 // C++0x [class.copy]p13:
780 // A copy/move constructor for class X is trivial if [...]
781 // [...]
782 // -- for each non-static data member of X that is of class type (or
783 // an array thereof), the constructor selected to copy/move that
784 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000785 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000786 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000787 // If the field doesn't have a simple move constructor, we'll eagerly
788 // declare the move constructor for this class and we'll decide whether
789 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000790 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000791 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000792
793 // C++0x [class.copy]p27:
794 // A copy/move assignment operator for class X is trivial if [...]
795 // [...]
796 // -- for each non-static data member of X that is of class type (or
797 // an array thereof), the assignment operator selected to
798 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000799 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000800 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000801 // If the field doesn't have a simple move assignment, we'll eagerly
802 // declare the move assignment for this class and we'll decide whether
803 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000804 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000805 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000806
Douglas Gregor11c024b2010-09-28 20:50:54 +0000807 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000808 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000809 if (!FieldRec->hasIrrelevantDestructor())
810 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000811 if (FieldRec->hasObjectMember())
812 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000813 if (FieldRec->hasVolatileMember())
814 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000815
816 // C++0x [class]p7:
817 // A standard-layout class is a class that:
818 // -- has no non-static data members of type non-standard-layout
819 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000820 if (!FieldRec->isStandardLayout())
821 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000822
823 // C++0x [class]p7:
824 // A standard-layout class is a class that:
825 // [...]
826 // -- has no base classes of the same type as the first non-static
827 // data member.
828 // We don't want to expend bits in the state of the record decl
829 // tracking whether this is the first non-static data member so we
830 // cheat a bit and use some of the existing state: the empty bit.
831 // Virtual bases and virtual methods make a class non-empty, but they
832 // also make it non-standard-layout so we needn't check here.
833 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000834 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000835 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000836 // data member must come through here with Empty still true, and Empty
837 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000838 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000839 for (const auto &BI : bases()) {
840 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000841 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000842 break;
843 }
844 }
845 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000846
847 // Keep track of the presence of mutable fields.
848 if (FieldRec->hasMutableFields())
849 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000850
851 // C++11 [class.copy]p13:
852 // If the implicitly-defined constructor would satisfy the
853 // requirements of a constexpr constructor, the implicitly-defined
854 // constructor is constexpr.
855 // C++11 [dcl.constexpr]p4:
856 // -- every constructor involved in initializing non-static data
857 // members [...] shall be a constexpr constructor
858 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000859 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000860 // The standard requires any in-class initializer to be a constant
861 // expression. We consider this to be a defect.
862 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000863
864 // C++11 [class.copy]p8:
865 // The implicitly-declared copy constructor for a class X will have
866 // the form 'X::X(const X&)' if [...] for all the non-static data
867 // members of X that are of a class type M (or array thereof), each
868 // such class type has a copy constructor whose first parameter is
869 // of type 'const M&' or 'const volatile M&'.
870 if (!FieldRec->hasCopyConstructorWithConstParam())
871 data().ImplicitCopyConstructorHasConstParam = false;
872
873 // C++11 [class.copy]p18:
874 // The implicitly-declared copy assignment oeprator for a class X will
875 // have the form 'X& X::operator=(const X&)' if [...] for all the
876 // non-static data members of X that are of a class type M (or array
877 // thereof), each such class type has a copy assignment operator whose
878 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
879 if (!FieldRec->hasCopyAssignmentWithConstParam())
880 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000881
882 if (FieldRec->hasUninitializedReferenceMember() &&
883 !Field->hasInClassInitializer())
884 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000885
886 // C++11 [class.union]p8, DR1460:
887 // a non-static data member of an anonymous union that is a member of
888 // X is also a variant member of X.
889 if (FieldRec->hasVariantMembers() &&
890 Field->isAnonymousStructOrUnion())
891 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000892 }
Richard Smithcc36f692011-12-22 02:22:31 +0000893 } else {
894 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000895 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000896 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000897 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000898
899 // C++11 [class.copy]p23:
900 // A defaulted copy/move assignment operator for a class X is defined
901 // as deleted if X has:
902 // -- a non-static data member of const non-class type (or array
903 // thereof)
904 if (T.isConstQualified())
905 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000906 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000907
908 // C++0x [class]p7:
909 // A standard-layout class is a class that:
910 // [...]
911 // -- either has no non-static data members in the most derived
912 // class and at most one base class with non-static data members,
913 // or has no base classes with non-static data members, and
914 // At this point we know that we have a non-static data member, so the last
915 // clause holds.
916 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000917 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000918
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000919 // If this is not a zero-length bit-field, then the class is not empty.
920 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000921 if (!Field->isBitField() ||
922 (!Field->getBitWidth()->isTypeDependent() &&
923 !Field->getBitWidth()->isValueDependent() &&
924 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000925 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000926 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000927 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000928
929 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000930 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000931 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000932 == DeclarationName::CXXConversionFunctionName) {
933 ASTContext &Ctx = getASTContext();
934 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
935 }
936 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000937}
938
Richard Smith92f241f2012-12-08 02:53:02 +0000939void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
940 assert(!D->isImplicit() && !D->isUserProvided());
941
942 // The kind of special member this declaration is, if any.
943 unsigned SMKind = 0;
944
945 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
946 if (Constructor->isDefaultConstructor()) {
947 SMKind |= SMF_DefaultConstructor;
948 if (Constructor->isConstexpr())
949 data().HasConstexprDefaultConstructor = true;
950 }
951 if (Constructor->isCopyConstructor())
952 SMKind |= SMF_CopyConstructor;
953 else if (Constructor->isMoveConstructor())
954 SMKind |= SMF_MoveConstructor;
955 else if (Constructor->isConstexpr())
956 // We may now know that the constructor is constexpr.
957 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000958 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000959 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000960 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
961 data().HasIrrelevantDestructor = false;
962 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000963 SMKind |= SMF_CopyAssignment;
964 else if (D->isMoveAssignmentOperator())
965 SMKind |= SMF_MoveAssignment;
966
967 // Update which trivial / non-trivial special members we have.
968 // addedMember will have skipped this step for this member.
969 if (D->isTrivial())
970 data().HasTrivialSpecialMembers |= SMKind;
971 else
972 data().DeclaredNonTrivialSpecialMembers |= SMKind;
973}
974
Joao Matose9a3ed42012-08-31 22:18:20 +0000975bool CXXRecordDecl::isCLike() const {
976 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
977 !TemplateOrInstantiation.isNull())
978 return false;
979 if (!hasDefinition())
980 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000981
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000982 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000983}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000984
Faisal Vali2b391ab2013-09-26 19:54:12 +0000985bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000986 if (!isLambda()) return false;
987 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000988}
989
990CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
Craig Topper36250ad2014-05-12 05:36:57 +0000991 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +0000992 DeclarationName Name =
993 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000994 DeclContext::lookup_const_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000995
996 assert(!Calls.empty() && "Missing lambda call operator!");
997 assert(Calls.size() == 1 && "More than one lambda call operator!");
998
999 NamedDecl *CallOp = Calls.front();
1000 if (FunctionTemplateDecl *CallOpTmpl =
1001 dyn_cast<FunctionTemplateDecl>(CallOp))
1002 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001003
1004 return cast<CXXMethodDecl>(CallOp);
1005}
1006
1007CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001008 if (!isLambda()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001009 DeclarationName Name =
1010 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Faisal Vali2b391ab2013-09-26 19:54:12 +00001011 DeclContext::lookup_const_result Invoker = lookup(Name);
Craig Topper36250ad2014-05-12 05:36:57 +00001012 if (Invoker.empty()) return nullptr;
Faisal Vali850da1a2013-09-29 17:08:32 +00001013 assert(Invoker.size() == 1 && "More than one static invoker operator!");
1014 NamedDecl *InvokerFun = Invoker.front();
1015 if (FunctionTemplateDecl *InvokerTemplate =
1016 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1017 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1018
Faisal Vali571df122013-09-29 08:45:24 +00001019 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001020}
1021
Douglas Gregor9c702202012-02-10 07:45:31 +00001022void CXXRecordDecl::getCaptureFields(
1023 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001024 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001025 Captures.clear();
Craig Topper36250ad2014-05-12 05:36:57 +00001026 ThisCapture = nullptr;
Douglas Gregor9c702202012-02-10 07:45:31 +00001027
Douglas Gregorc8a73492012-02-13 15:44:47 +00001028 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001029 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001030 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001031 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001032 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001033 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001034 else if (C->capturesVariable())
1035 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001036 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001037 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001038}
1039
Faisal Vali2b391ab2013-09-26 19:54:12 +00001040TemplateParameterList *
1041CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001042 if (!isLambda()) return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001043 CXXMethodDecl *CallOp = getLambdaCallOperator();
1044 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1045 return Tmpl->getTemplateParameters();
Craig Topper36250ad2014-05-12 05:36:57 +00001046 return nullptr;
Faisal Vali2b391ab2013-09-26 19:54:12 +00001047}
Douglas Gregor9c702202012-02-10 07:45:31 +00001048
John McCall1e3a1a72010-03-15 09:07:48 +00001049static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001050 QualType T =
1051 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1052 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001053 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001054}
1055
John McCall1e3a1a72010-03-15 09:07:48 +00001056/// Collect the visible conversions of a base class.
1057///
James Dennettb5d5f812012-06-15 22:28:09 +00001058/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001059/// \param InVirtual whether this base class is a virtual base (or a base
1060/// of a virtual base)
1061/// \param Access the access along the inheritance path to this base
1062/// \param ParentHiddenTypes the conversions provided by the inheritors
1063/// of this base
1064/// \param Output the set to which to add conversions from non-virtual bases
1065/// \param VOutput the set to which to add conversions from virtual bases
1066/// \param HiddenVBaseCs the set of conversions which were hidden in a
1067/// virtual base along some inheritance path
1068static void CollectVisibleConversions(ASTContext &Context,
1069 CXXRecordDecl *Record,
1070 bool InVirtual,
1071 AccessSpecifier Access,
1072 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001073 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001074 UnresolvedSetImpl &VOutput,
1075 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1076 // The set of types which have conversions in this class or its
1077 // subclasses. As an optimization, we don't copy the derived set
1078 // unless it might change.
1079 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1080 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1081
1082 // Collect the direct conversions and figure out which conversions
1083 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001084 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1085 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1086 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001087 HiddenTypesBuffer = ParentHiddenTypes;
1088 HiddenTypes = &HiddenTypesBuffer;
1089
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001090 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001091 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1092 bool Hidden = ParentHiddenTypes.count(ConvType);
1093 if (!Hidden)
1094 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001095
1096 // If this conversion is hidden and we're in a virtual base,
1097 // remember that it's hidden along some inheritance path.
1098 if (Hidden && InVirtual)
1099 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1100
1101 // If this conversion isn't hidden, add it to the appropriate output.
1102 else if (!Hidden) {
1103 AccessSpecifier IAccess
1104 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1105
1106 if (InVirtual)
1107 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001108 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001109 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001110 }
1111 }
1112 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001113
John McCall1e3a1a72010-03-15 09:07:48 +00001114 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001115 for (const auto &I : Record->bases()) {
1116 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001117 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001118
John McCall1e3a1a72010-03-15 09:07:48 +00001119 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001120 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1121 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001122
John McCall1e3a1a72010-03-15 09:07:48 +00001123 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1124 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1125 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001126 }
John McCall1e3a1a72010-03-15 09:07:48 +00001127}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001128
John McCall1e3a1a72010-03-15 09:07:48 +00001129/// Collect the visible conversions of a class.
1130///
1131/// This would be extremely straightforward if it weren't for virtual
1132/// bases. It might be worth special-casing that, really.
1133static void CollectVisibleConversions(ASTContext &Context,
1134 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001135 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001136 // The collection of all conversions in virtual bases that we've
1137 // found. These will be added to the output as long as they don't
1138 // appear in the hidden-conversions set.
1139 UnresolvedSet<8> VBaseCs;
1140
1141 // The set of conversions in virtual bases that we've determined to
1142 // be hidden.
1143 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1144
1145 // The set of types hidden by classes derived from this one.
1146 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1147
1148 // Go ahead and collect the direct conversions and add them to the
1149 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001150 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1151 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001152 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001153 for (; ConvI != ConvE; ++ConvI)
1154 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001155
1156 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001157 for (const auto &I : Record->bases()) {
1158 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001159 if (!RT) continue;
1160
1161 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001162 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001163 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1164 }
1165
1166 // Add any unhidden conversions provided by virtual bases.
1167 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1168 I != E; ++I) {
1169 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001170 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001171 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001172}
1173
1174/// getVisibleConversionFunctions - get all conversion functions visible
1175/// in current class; including conversion function templates.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001176std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1177CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001178 ASTContext &Ctx = getASTContext();
1179
1180 ASTUnresolvedSet *Set;
1181 if (bases_begin() == bases_end()) {
1182 // If root class, all conversions are visible.
1183 Set = &data().Conversions.get(Ctx);
1184 } else {
1185 Set = &data().VisibleConversions.get(Ctx);
1186 // If visible conversion list is not evaluated, evaluate it.
1187 if (!data().ComputedVisibleConversions) {
1188 CollectVisibleConversions(Ctx, this, *Set);
1189 data().ComputedVisibleConversions = true;
1190 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001191 }
Richard Smitha4ba74c2013-08-30 04:46:40 +00001192 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001193}
1194
John McCallda4458e2010-03-31 01:36:47 +00001195void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1196 // This operation is O(N) but extremely rare. Sema only uses it to
1197 // remove UsingShadowDecls in a class that were followed by a direct
1198 // declaration, e.g.:
1199 // class A : B {
1200 // using B::operator int;
1201 // operator int();
1202 // };
1203 // This is uncommon by itself and even more uncommon in conjunction
1204 // with sufficiently large numbers of directly-declared conversions
1205 // that asymptotic behavior matters.
1206
Richard Smitha4ba74c2013-08-30 04:46:40 +00001207 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001208 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1209 if (Convs[I].getDecl() == ConvDecl) {
1210 Convs.erase(I);
1211 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1212 && "conversion was found multiple times in unresolved set");
1213 return;
1214 }
1215 }
1216
1217 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001218}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001219
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001220CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001221 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001222 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
Craig Topper36250ad2014-05-12 05:36:57 +00001223
1224 return nullptr;
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001225}
1226
1227void
1228CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1229 TemplateSpecializationKind TSK) {
1230 assert(TemplateOrInstantiation.isNull() &&
1231 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001232 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001233 TemplateOrInstantiation
1234 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1235}
1236
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001237TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1238 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001239 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1240 return Spec->getSpecializationKind();
1241
Douglas Gregor06db9f52009-10-12 20:18:28 +00001242 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001243 return MSInfo->getTemplateSpecializationKind();
1244
1245 return TSK_Undeclared;
1246}
1247
1248void
1249CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1250 if (ClassTemplateSpecializationDecl *Spec
1251 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1252 Spec->setSpecializationKind(TSK);
1253 return;
1254 }
1255
Douglas Gregor06db9f52009-10-12 20:18:28 +00001256 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001257 MSInfo->setTemplateSpecializationKind(TSK);
1258 return;
1259 }
1260
David Blaikie83d382b2011-09-23 05:06:16 +00001261 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001262}
1263
Reid Klecknere7367d62014-10-14 20:28:40 +00001264const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const {
1265 // If it's a class template specialization, find the template or partial
1266 // specialization from which it was instantiated.
1267 if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1268 auto From = TD->getInstantiatedFrom();
1269 if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) {
1270 while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) {
1271 if (NewCTD->isMemberSpecialization())
1272 break;
1273 CTD = NewCTD;
1274 }
1275 return CTD->getTemplatedDecl();
1276 }
1277 if (auto *CTPSD =
1278 From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) {
1279 while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) {
1280 if (NewCTPSD->isMemberSpecialization())
1281 break;
1282 CTPSD = NewCTPSD;
1283 }
1284 return CTPSD;
1285 }
1286 }
1287
1288 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
1289 if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) {
1290 const CXXRecordDecl *RD = this;
1291 while (auto *NewRD = RD->getInstantiatedFromMemberClass())
1292 RD = NewRD;
1293 return RD;
1294 }
1295 }
1296
1297 assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) &&
1298 "couldn't find pattern for class template instantiation");
1299 return nullptr;
1300}
1301
Douglas Gregorbac74902010-07-01 14:13:13 +00001302CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1303 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001304 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001305
1306 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001307 = Context.DeclarationNames.getCXXDestructorName(
1308 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001309
David Blaikieff7d47a2012-12-19 00:45:41 +00001310 DeclContext::lookup_const_result R = lookup(Name);
1311 if (R.empty())
Craig Topper36250ad2014-05-12 05:36:57 +00001312 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001313
David Blaikieff7d47a2012-12-19 00:45:41 +00001314 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001315 return Dtor;
1316}
1317
Douglas Gregorb11aad82011-02-19 18:51:44 +00001318void CXXRecordDecl::completeDefinition() {
Craig Topper36250ad2014-05-12 05:36:57 +00001319 completeDefinition(nullptr);
Douglas Gregorb11aad82011-02-19 18:51:44 +00001320}
1321
1322void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1323 RecordDecl::completeDefinition();
1324
Douglas Gregor8fb95122010-09-29 00:15:42 +00001325 // If the class may be abstract (but hasn't been marked as such), check for
1326 // any pure final overriders.
1327 if (mayBeAbstract()) {
1328 CXXFinalOverriderMap MyFinalOverriders;
1329 if (!FinalOverriders) {
1330 getFinalOverriders(MyFinalOverriders);
1331 FinalOverriders = &MyFinalOverriders;
1332 }
1333
1334 bool Done = false;
1335 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1336 MEnd = FinalOverriders->end();
1337 M != MEnd && !Done; ++M) {
1338 for (OverridingMethods::iterator SO = M->second.begin(),
1339 SOEnd = M->second.end();
1340 SO != SOEnd && !Done; ++SO) {
1341 assert(SO->second.size() > 0 &&
1342 "All virtual functions have overridding virtual functions");
1343
1344 // C++ [class.abstract]p4:
1345 // A class is abstract if it contains or inherits at least one
1346 // pure virtual function for which the final overrider is pure
1347 // virtual.
1348 if (SO->second.front().Method->isPure()) {
1349 data().Abstract = true;
1350 Done = true;
1351 break;
1352 }
1353 }
1354 }
1355 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001356
1357 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001358 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001359 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001360 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001361}
1362
1363bool CXXRecordDecl::mayBeAbstract() const {
1364 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1365 isDependentContext())
1366 return false;
1367
Aaron Ballman574705e2014-03-13 15:41:46 +00001368 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001369 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001370 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001371 if (BaseDecl->isAbstract())
1372 return true;
1373 }
1374
1375 return false;
1376}
1377
David Blaikie68e081d2011-12-20 02:48:34 +00001378void CXXMethodDecl::anchor() { }
1379
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001380bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001381 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001382
1383 if (MD->getStorageClass() == SC_Static)
1384 return true;
1385
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001386 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1387 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001388}
1389
Rafael Espindola49e860b2012-06-26 17:45:31 +00001390static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1391 const CXXMethodDecl *BaseMD) {
1392 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1393 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1394 const CXXMethodDecl *MD = *I;
1395 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1396 return true;
1397 if (recursivelyOverrides(MD, BaseMD))
1398 return true;
1399 }
1400 return false;
1401}
1402
1403CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001404CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1405 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001406 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1407 return this;
1408
1409 // Lookup doesn't work for destructors, so handle them separately.
1410 if (isa<CXXDestructorDecl>(this)) {
1411 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001412 if (MD) {
1413 if (recursivelyOverrides(MD, this))
1414 return MD;
1415 if (MayBeBase && recursivelyOverrides(this, MD))
1416 return MD;
1417 }
Craig Topper36250ad2014-05-12 05:36:57 +00001418 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001419 }
1420
1421 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001422 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001423 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1424 if (!MD)
1425 continue;
1426 if (recursivelyOverrides(MD, this))
1427 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001428 if (MayBeBase && recursivelyOverrides(this, MD))
1429 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001430 }
1431
Aaron Ballman574705e2014-03-13 15:41:46 +00001432 for (const auto &I : RD->bases()) {
1433 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001434 if (!RT)
1435 continue;
1436 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1437 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1438 if (T)
1439 return T;
1440 }
1441
Craig Topper36250ad2014-05-12 05:36:57 +00001442 return nullptr;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001443}
1444
Ted Kremenek21475702008-09-05 17:16:31 +00001445CXXMethodDecl *
1446CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001447 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001448 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001449 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001450 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001451 bool isConstexpr, SourceLocation EndLocation) {
Richard Smith053f6c62014-05-16 23:01:30 +00001452 return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo,
1453 T, TInfo, SC, isInline, isConstexpr,
1454 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001455}
1456
Douglas Gregor72172e92012-01-05 21:55:30 +00001457CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001458 return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001459 DeclarationNameInfo(), QualType(), nullptr,
Richard Smithf7981722013-11-22 09:01:48 +00001460 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001461}
1462
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001463bool CXXMethodDecl::isUsualDeallocationFunction() const {
1464 if (getOverloadedOperator() != OO_Delete &&
1465 getOverloadedOperator() != OO_Array_Delete)
1466 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001467
1468 // C++ [basic.stc.dynamic.deallocation]p2:
1469 // A template instance is never a usual deallocation function,
1470 // regardless of its signature.
1471 if (getPrimaryTemplate())
1472 return false;
1473
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001474 // C++ [basic.stc.dynamic.deallocation]p2:
1475 // If a class T has a member deallocation function named operator delete
1476 // with exactly one parameter, then that function is a usual (non-placement)
1477 // deallocation function. [...]
1478 if (getNumParams() == 1)
1479 return true;
1480
1481 // C++ [basic.stc.dynamic.deallocation]p2:
1482 // [...] If class T does not declare such an operator delete but does
1483 // declare a member deallocation function named operator delete with
1484 // exactly two parameters, the second of which has type std::size_t (18.1),
1485 // then this function is a usual deallocation function.
1486 ASTContext &Context = getASTContext();
1487 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001488 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1489 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001490 return false;
1491
1492 // This function is a usual deallocation function if there are no
1493 // single-parameter deallocation functions of the same kind.
David Blaikieff7d47a2012-12-19 00:45:41 +00001494 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1495 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1496 I != E; ++I) {
1497 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001498 if (FD->getNumParams() == 1)
1499 return false;
1500 }
1501
1502 return true;
1503}
1504
Douglas Gregorb139cd52010-05-01 20:49:11 +00001505bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001506 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001507 // A user-declared copy assignment operator X::operator= is a non-static
1508 // non-template member function of class X with exactly one parameter of
1509 // type X, X&, const X&, volatile X& or const volatile X&.
1510 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1511 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001512 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1513 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001514 return false;
1515
1516 QualType ParamType = getParamDecl(0)->getType();
1517 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1518 ParamType = Ref->getPointeeType();
1519
1520 ASTContext &Context = getASTContext();
1521 QualType ClassType
1522 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1523 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1524}
1525
Alexis Huntfcaeae42011-05-25 20:50:04 +00001526bool CXXMethodDecl::isMoveAssignmentOperator() const {
1527 // C++0x [class.copy]p19:
1528 // A user-declared move assignment operator X::operator= is a non-static
1529 // non-template member function of class X with exactly one parameter of type
1530 // X&&, const X&&, volatile X&&, or const volatile X&&.
1531 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001532 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1533 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001534 return false;
1535
1536 QualType ParamType = getParamDecl(0)->getType();
1537 if (!isa<RValueReferenceType>(ParamType))
1538 return false;
1539 ParamType = ParamType->getPointeeType();
1540
1541 ASTContext &Context = getASTContext();
1542 QualType ClassType
1543 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1544 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1545}
1546
Anders Carlsson36d87e12009-05-16 23:58:37 +00001547void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001548 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001549 assert(!MD->getParent()->isDependentContext() &&
1550 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001551 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001552
Douglas Gregor832940b2010-03-02 23:58:15 +00001553 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001554}
1555
1556CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001557 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001558 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001559}
1560
1561CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001562 if (isa<CXXConstructorDecl>(this)) return nullptr;
Douglas Gregor832940b2010-03-02 23:58:15 +00001563 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001564}
1565
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001566unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001567 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001568 return getASTContext().overridden_methods_size(this);
1569}
1570
Ted Kremenek21475702008-09-05 17:16:31 +00001571QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001572 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1573 // If the member function is declared const, the type of this is const X*,
1574 // if the member function is declared volatile, the type of this is
1575 // volatile X*, and if the member function is declared const volatile,
1576 // the type of this is const volatile X*.
1577
Ted Kremenek21475702008-09-05 17:16:31 +00001578 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001579
John McCalle78aac42010-03-10 03:28:59 +00001580 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001581 ClassTy = C.getQualifiedType(ClassTy,
1582 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001583 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001584}
1585
Eli Friedman71a26d82009-12-06 20:50:05 +00001586bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001587 // If this function is a template instantiation, look at the template from
1588 // which it was instantiated.
1589 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1590 if (!CheckFn)
1591 CheckFn = this;
1592
Eli Friedman71a26d82009-12-06 20:50:05 +00001593 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001594 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001595}
1596
Douglas Gregor355efbb2012-02-17 03:02:34 +00001597bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001598 const CXXRecordDecl *P = getParent();
1599 if (P->isLambda()) {
1600 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1601 if (StaticInvoker == this) return true;
1602 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1603 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1604 }
1605 }
1606 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001607}
1608
Alexis Hunt1d792652011-01-08 20:30:50 +00001609CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1610 TypeSourceInfo *TInfo, bool IsVirtual,
1611 SourceLocation L, Expr *Init,
1612 SourceLocation R,
1613 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001614 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001615 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1616 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001617{
Douglas Gregore8381c02008-11-05 04:29:56 +00001618}
1619
Alexis Hunt1d792652011-01-08 20:30:50 +00001620CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1621 FieldDecl *Member,
1622 SourceLocation MemberLoc,
1623 SourceLocation L, Expr *Init,
1624 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001625 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001626 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001627 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1628{
1629}
1630
Alexis Hunt1d792652011-01-08 20:30:50 +00001631CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1632 IndirectFieldDecl *Member,
1633 SourceLocation MemberLoc,
1634 SourceLocation L, Expr *Init,
1635 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001636 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001637 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001638 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001639{
Douglas Gregore8381c02008-11-05 04:29:56 +00001640}
1641
Alexis Hunt1d792652011-01-08 20:30:50 +00001642CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001643 TypeSourceInfo *TInfo,
1644 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001645 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001646 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1647 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001648 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1649{
1650}
1651
1652CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001653 FieldDecl *Member,
1654 SourceLocation MemberLoc,
1655 SourceLocation L, Expr *Init,
1656 SourceLocation R,
1657 VarDecl **Indices,
1658 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001659 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Richard Smith22fdae92014-07-21 05:27:31 +00001660 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001661 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001662{
1663 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1664 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1665}
1666
Alexis Hunt1d792652011-01-08 20:30:50 +00001667CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1668 FieldDecl *Member,
1669 SourceLocation MemberLoc,
1670 SourceLocation L, Expr *Init,
1671 SourceLocation R,
1672 VarDecl **Indices,
1673 unsigned NumIndices) {
1674 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001675 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001676 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001677 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1678 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001679}
1680
Alexis Hunt1d792652011-01-08 20:30:50 +00001681TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001682 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001683 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001684 else
1685 return TypeLoc();
1686}
1687
Alexis Hunt1d792652011-01-08 20:30:50 +00001688const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001689 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001690 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001691 else
Craig Topper36250ad2014-05-12 05:36:57 +00001692 return nullptr;
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001693}
1694
Alexis Hunt1d792652011-01-08 20:30:50 +00001695SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001696 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001697 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001698
1699 if (isInClassMemberInitializer())
1700 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001701
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001702 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1703 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1704
1705 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001706}
1707
Alexis Hunt1d792652011-01-08 20:30:50 +00001708SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001709 if (isInClassMemberInitializer()) {
1710 FieldDecl *D = getAnyMember();
1711 if (Expr *I = D->getInClassInitializer())
1712 return I->getSourceRange();
1713 return SourceRange();
1714 }
1715
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001716 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001717}
1718
David Blaikie68e081d2011-12-20 02:48:34 +00001719void CXXConstructorDecl::anchor() { }
1720
Douglas Gregor61956c42008-10-31 09:07:45 +00001721CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001722CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001723 return new (C, ID) CXXConstructorDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001724 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001725 nullptr, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001726}
1727
1728CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001729CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001730 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001731 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001732 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001733 bool isExplicit, bool isInline,
1734 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001735 assert(NameInfo.getName().getNameKind()
1736 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001737 "Name must refer to a constructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001738 return new (C, RD) CXXConstructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001739 isExplicit, isInline,
1740 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001741}
1742
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001743CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1744 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1745 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1746 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1747 return Construct->getConstructor();
Craig Topper36250ad2014-05-12 05:36:57 +00001748
1749 return nullptr;
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001750}
1751
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001752bool CXXConstructorDecl::isDefaultConstructor() const {
1753 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001754 // A default constructor for a class X is a constructor of class
1755 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001756 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001757 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001758}
1759
Mike Stump11289f42009-09-09 15:08:12 +00001760bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001761CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001762 return isCopyOrMoveConstructor(TypeQuals) &&
1763 getParamDecl(0)->getType()->isLValueReferenceType();
1764}
1765
1766bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1767 return isCopyOrMoveConstructor(TypeQuals) &&
1768 getParamDecl(0)->getType()->isRValueReferenceType();
1769}
1770
1771/// \brief Determine whether this is a copy or move constructor.
1772bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001773 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001774 // A non-template constructor for class X is a copy constructor
1775 // if its first parameter is of type X&, const X&, volatile X& or
1776 // const volatile X&, and either there are no other parameters
1777 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001778 // C++0x [class.copy]p3:
1779 // A non-template constructor for class X is a move constructor if its
1780 // first parameter is of type X&&, const X&&, volatile X&&, or
1781 // const volatile X&&, and either there are no other parameters or else
1782 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001783 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001784 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001785 (getPrimaryTemplate() != nullptr) ||
1786 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001787 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001788
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001789 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001790
1791 // Do we have a reference type?
1792 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001793 if (!ParamRefType)
1794 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001795
Douglas Gregorff7028a2009-11-13 23:59:09 +00001796 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001797 ASTContext &Context = getASTContext();
1798
Douglas Gregorff7028a2009-11-13 23:59:09 +00001799 CanQualType PointeeType
1800 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001801 CanQualType ClassTy
1802 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001803 if (PointeeType.getUnqualifiedType() != ClassTy)
1804 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001805
John McCall8ccfcb52009-09-24 19:53:00 +00001806 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001807
1808 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001809 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001810 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001811}
1812
Anders Carlssond20e7952009-08-28 16:57:08 +00001813bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001814 // C++ [class.conv.ctor]p1:
1815 // A constructor declared without the function-specifier explicit
1816 // that can be called with a single parameter specifies a
1817 // conversion from the type of its first parameter to the type of
1818 // its class. Such a constructor is called a converting
1819 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001820 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001821 return false;
1822
Mike Stump11289f42009-09-09 15:08:12 +00001823 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001824 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001825 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001826 (getNumParams() > 1 &&
1827 (getParamDecl(1)->hasDefaultArg() ||
1828 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001829}
Douglas Gregor61956c42008-10-31 09:07:45 +00001830
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001831bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001832 if ((getNumParams() < 1) ||
1833 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Craig Topper36250ad2014-05-12 05:36:57 +00001834 (getPrimaryTemplate() == nullptr) ||
1835 (getDescribedFunctionTemplate() != nullptr))
Douglas Gregorffe14e32009-11-14 01:20:54 +00001836 return false;
1837
1838 const ParmVarDecl *Param = getParamDecl(0);
1839
1840 ASTContext &Context = getASTContext();
1841 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1842
Douglas Gregorffe14e32009-11-14 01:20:54 +00001843 // Is it the same as our our class type?
1844 CanQualType ClassTy
1845 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1846 if (ParamType.getUnqualifiedType() != ClassTy)
1847 return false;
1848
1849 return true;
1850}
1851
Sebastian Redl08905022011-02-05 19:23:19 +00001852const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1853 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001854 method_iterator It = getASTContext().overridden_methods_begin(this);
1855 if (It == getASTContext().overridden_methods_end(this))
Craig Topper36250ad2014-05-12 05:36:57 +00001856 return nullptr;
Sebastian Redl08905022011-02-05 19:23:19 +00001857
1858 return cast<CXXConstructorDecl>(*It);
1859}
1860
1861void
1862CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1863 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001864 assert(getASTContext().overridden_methods_size(this) == 0 &&
1865 "Base ctor already set.");
1866 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001867}
1868
David Blaikie68e081d2011-12-20 02:48:34 +00001869void CXXDestructorDecl::anchor() { }
1870
Douglas Gregor831c93f2008-11-05 20:51:48 +00001871CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001872CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001873 return new (C, ID)
1874 CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(),
1875 QualType(), nullptr, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001876}
1877
1878CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001879CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001880 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001881 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001882 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001883 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001884 assert(NameInfo.getName().getNameKind()
1885 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001886 "Name must refer to a destructor");
Richard Smith053f6c62014-05-16 23:01:30 +00001887 return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001888 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001889}
1890
David Blaikie68e081d2011-12-20 02:48:34 +00001891void CXXConversionDecl::anchor() { }
1892
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001893CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001894CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001895 return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001896 DeclarationNameInfo(), QualType(),
Craig Topper36250ad2014-05-12 05:36:57 +00001897 nullptr, false, false, false,
Richard Smithf7981722013-11-22 09:01:48 +00001898 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001899}
1900
1901CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001902CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001903 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001904 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001905 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001906 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001907 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001908 assert(NameInfo.getName().getNameKind()
1909 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001910 "Name must refer to a conversion function");
Richard Smith053f6c62014-05-16 23:01:30 +00001911 return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo,
Richard Smithf7981722013-11-22 09:01:48 +00001912 isInline, isExplicit, isConstexpr,
1913 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001914}
1915
Douglas Gregord3b672c2012-02-16 01:06:16 +00001916bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1917 return isImplicit() && getParent()->isLambda() &&
1918 getConversionType()->isBlockPointerType();
1919}
1920
David Blaikie68e081d2011-12-20 02:48:34 +00001921void LinkageSpecDecl::anchor() { }
1922
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001923LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001924 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001925 SourceLocation ExternLoc,
1926 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001927 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001928 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00001929 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001930}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001931
Richard Smithf7981722013-11-22 09:01:48 +00001932LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1933 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001934 return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(),
1935 SourceLocation(), lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001936}
1937
David Blaikie68e081d2011-12-20 02:48:34 +00001938void UsingDirectiveDecl::anchor() { }
1939
Douglas Gregor889ceb72009-02-03 19:21:40 +00001940UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1941 SourceLocation L,
1942 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001943 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001944 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001945 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001946 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001947 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1948 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001949 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1950 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001951}
1952
Richard Smithf7981722013-11-22 09:01:48 +00001953UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
1954 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00001955 return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(),
1956 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00001957 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001958 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001959}
1960
Sebastian Redla6602e92009-11-23 15:34:23 +00001961NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1962 if (NamespaceAliasDecl *NA =
1963 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1964 return NA->getNamespace();
1965 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1966}
1967
Richard Smith053f6c62014-05-16 23:01:30 +00001968NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline,
1969 SourceLocation StartLoc, SourceLocation IdLoc,
1970 IdentifierInfo *Id, NamespaceDecl *PrevDecl)
1971 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1972 redeclarable_base(C), LocStart(StartLoc), RBraceLoc(),
1973 AnonOrFirstNamespaceAndInline(nullptr, Inline) {
Rafael Espindola8db352d2013-10-17 15:37:26 +00001974 setPreviousDecl(PrevDecl);
Richard Smith053f6c62014-05-16 23:01:30 +00001975
Douglas Gregore57e7522012-01-07 09:11:48 +00001976 if (PrevDecl)
1977 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1978}
1979
Douglas Gregor72172e92012-01-05 21:55:30 +00001980NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001981 bool Inline, SourceLocation StartLoc,
1982 SourceLocation IdLoc, IdentifierInfo *Id,
1983 NamespaceDecl *PrevDecl) {
Richard Smith053f6c62014-05-16 23:01:30 +00001984 return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id,
1985 PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001986}
1987
1988NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00001989 return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001990 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00001991}
1992
Richard Smithd7af8a32014-05-10 01:17:36 +00001993NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
1994 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00001995}
1996NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
1997 return getPreviousDecl();
1998}
1999NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
2000 return getMostRecentDecl();
2001}
2002
David Blaikie68e081d2011-12-20 02:48:34 +00002003void NamespaceAliasDecl::anchor() { }
2004
Richard Smithf4634362014-09-03 23:11:22 +00002005NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() {
2006 return getNextRedeclaration();
2007}
2008NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() {
2009 return getPreviousDecl();
2010}
2011NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() {
2012 return getMostRecentDecl();
2013}
2014
Mike Stump11289f42009-09-09 15:08:12 +00002015NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00002016 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002017 SourceLocation AliasLoc,
2018 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00002019 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00002020 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002021 NamedDecl *Namespace) {
Richard Smithf4634362014-09-03 23:11:22 +00002022 // FIXME: Preserve the aliased namespace as written.
Sebastian Redla6602e92009-11-23 15:34:23 +00002023 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
2024 Namespace = NS->getOriginalNamespace();
Richard Smithf4634362014-09-03 23:11:22 +00002025 return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias,
Richard Smithf7981722013-11-22 09:01:48 +00002026 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00002027}
2028
Douglas Gregor72172e92012-01-05 21:55:30 +00002029NamespaceAliasDecl *
2030NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf4634362014-09-03 23:11:22 +00002031 return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00002032 SourceLocation(), nullptr,
2033 NestedNameSpecifierLoc(),
2034 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002035}
2036
David Blaikie68e081d2011-12-20 02:48:34 +00002037void UsingShadowDecl::anchor() { }
2038
Douglas Gregor72172e92012-01-05 21:55:30 +00002039UsingShadowDecl *
2040UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smith053f6c62014-05-16 23:01:30 +00002041 return new (C, ID) UsingShadowDecl(C, nullptr, SourceLocation(),
2042 nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002043}
2044
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002045UsingDecl *UsingShadowDecl::getUsingDecl() const {
2046 const UsingShadowDecl *Shadow = this;
2047 while (const UsingShadowDecl *NextShadow =
2048 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
2049 Shadow = NextShadow;
2050 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
2051}
2052
David Blaikie68e081d2011-12-20 02:48:34 +00002053void UsingDecl::anchor() { }
2054
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002055void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
2056 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2057 "declaration already in set");
2058 assert(S->getUsingDecl() == this);
2059
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002060 if (FirstUsingShadow.getPointer())
2061 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2062 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002063}
2064
2065void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2066 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2067 "declaration not in set");
2068 assert(S->getUsingDecl() == this);
2069
2070 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2071
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002072 if (FirstUsingShadow.getPointer() == S) {
2073 FirstUsingShadow.setPointer(
2074 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002075 S->UsingOrNextShadow = this;
2076 return;
2077 }
2078
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002079 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002080 while (Prev->UsingOrNextShadow != S)
2081 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2082 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2083 S->UsingOrNextShadow = this;
2084}
2085
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002086UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2087 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002088 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002089 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002090 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002091}
2092
Douglas Gregor72172e92012-01-05 21:55:30 +00002093UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002094 return new (C, ID) UsingDecl(nullptr, SourceLocation(),
2095 NestedNameSpecifierLoc(), DeclarationNameInfo(),
2096 false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002097}
2098
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002099SourceRange UsingDecl::getSourceRange() const {
2100 SourceLocation Begin = isAccessDeclaration()
2101 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2102 return SourceRange(Begin, getNameInfo().getEndLoc());
2103}
2104
David Blaikie68e081d2011-12-20 02:48:34 +00002105void UnresolvedUsingValueDecl::anchor() { }
2106
John McCalle61f2ba2009-11-18 02:36:19 +00002107UnresolvedUsingValueDecl *
2108UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2109 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002110 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002111 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002112 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2113 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002114}
2115
Douglas Gregor72172e92012-01-05 21:55:30 +00002116UnresolvedUsingValueDecl *
2117UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002118 return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(),
2119 SourceLocation(),
Richard Smithf7981722013-11-22 09:01:48 +00002120 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00002121 DeclarationNameInfo());
Douglas Gregor72172e92012-01-05 21:55:30 +00002122}
2123
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002124SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2125 SourceLocation Begin = isAccessDeclaration()
2126 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2127 return SourceRange(Begin, getNameInfo().getEndLoc());
2128}
2129
David Blaikie68e081d2011-12-20 02:48:34 +00002130void UnresolvedUsingTypenameDecl::anchor() { }
2131
John McCalle61f2ba2009-11-18 02:36:19 +00002132UnresolvedUsingTypenameDecl *
2133UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2134 SourceLocation UsingLoc,
2135 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002136 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002137 SourceLocation TargetNameLoc,
2138 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002139 return new (C, DC) UnresolvedUsingTypenameDecl(
2140 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2141 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002142}
2143
Douglas Gregor72172e92012-01-05 21:55:30 +00002144UnresolvedUsingTypenameDecl *
2145UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002146 return new (C, ID) UnresolvedUsingTypenameDecl(
Craig Topper36250ad2014-05-12 05:36:57 +00002147 nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2148 SourceLocation(), nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002149}
2150
David Blaikie68e081d2011-12-20 02:48:34 +00002151void StaticAssertDecl::anchor() { }
2152
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002153StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002154 SourceLocation StaticAssertLoc,
2155 Expr *AssertExpr,
2156 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002157 SourceLocation RParenLoc,
2158 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002159 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2160 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002161}
2162
Richard Smithf7981722013-11-22 09:01:48 +00002163StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002164 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002165 return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr,
2166 nullptr, SourceLocation(), false);
Richard Smithf7981722013-11-22 09:01:48 +00002167}
2168
2169MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2170 SourceLocation L, DeclarationName N,
2171 QualType T, TypeSourceInfo *TInfo,
2172 SourceLocation StartL,
2173 IdentifierInfo *Getter,
2174 IdentifierInfo *Setter) {
2175 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2176}
2177
2178MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2179 unsigned ID) {
Craig Topper36250ad2014-05-12 05:36:57 +00002180 return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(),
2181 DeclarationName(), QualType(), nullptr,
2182 SourceLocation(), nullptr, nullptr);
Douglas Gregor72172e92012-01-05 21:55:30 +00002183}
2184
Anders Carlsson6750d162009-03-26 23:46:50 +00002185static const char *getAccessName(AccessSpecifier AS) {
2186 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002187 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002188 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002189 case AS_public:
2190 return "public";
2191 case AS_private:
2192 return "private";
2193 case AS_protected:
2194 return "protected";
2195 }
David Blaikief47fa302012-01-17 02:30:50 +00002196 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002197}
2198
2199const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2200 AccessSpecifier AS) {
2201 return DB << getAccessName(AS);
2202}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002203
2204const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2205 AccessSpecifier AS) {
2206 return DB << getAccessName(AS);
2207}