blob: 8d183b2534a30f64a22f60a003a3f22eb4fbaa4b [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),
Richard Smith1c33fe82012-11-28 06:23:12 +000073 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith68ad0e72013-06-26 02:41:25 +000074 Definition(D), FirstFriend() {
John McCall67da35c2010-02-04 22:26:26 +000075}
76
Benjamin Kramer300c0632012-07-04 17:03:33 +000077CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
78 return Bases.get(Definition->getASTContext().getExternalSource());
79}
80
81CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
82 return VBases.get(Definition->getASTContext().getExternalSource());
83}
84
John McCall67da35c2010-02-04 22:26:26 +000085CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000086 SourceLocation StartLoc, SourceLocation IdLoc,
87 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
88 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall67da35c2010-02-04 22:26:26 +000089 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000090 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000091
Jay Foad39c79802011-01-12 09:06:06 +000092CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000093 DeclContext *DC, SourceLocation StartLoc,
94 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000095 CXXRecordDecl* PrevDecl,
96 bool DelayTypeCreation) {
Richard Smithf7981722013-11-22 09:01:48 +000097 CXXRecordDecl *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, DC, StartLoc,
98 IdLoc, Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +000099 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000100
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000101 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000102 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000103 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000104 return R;
105}
106
Douglas Gregorc8a73492012-02-13 15:44:47 +0000107CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedmand564afb2012-09-19 01:18:11 +0000108 TypeSourceInfo *Info, SourceLocation Loc,
Faisal Valic1a6dc42013-10-23 16:10:50 +0000109 bool Dependent, bool IsGeneric,
110 LambdaCaptureDefault CaptureDefault) {
Richard Smithf7981722013-11-22 09:01:48 +0000111 CXXRecordDecl *R =
112 new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc, 0, 0);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000113 R->IsBeingDefined = true;
Faisal Valic1a6dc42013-10-23 16:10:50 +0000114 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info,
115 Dependent,
116 IsGeneric,
117 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000118 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000119 R->setImplicit(true);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000120 C.getTypeDeclType(R, /*PrevDecl=*/0);
121 return R;
122}
123
Douglas Gregor72172e92012-01-05 21:55:30 +0000124CXXRecordDecl *
125CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +0000126 CXXRecordDecl *R = new (C, ID) CXXRecordDecl(
127 CXXRecord, TTK_Struct, 0, SourceLocation(), SourceLocation(), 0, 0);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000128 R->MayHaveOutOfDateDef = false;
129 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000130}
131
Mike Stump11289f42009-09-09 15:08:12 +0000132void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000133CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000134 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000135 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000136
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000137 if (!data().Bases.isOffset() && data().NumBases > 0)
138 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000139
Richard Smithaed32b42011-10-18 20:08:55 +0000140 if (NumBases) {
141 // C++ [dcl.init.aggr]p1:
142 // An aggregate is [...] a class with [...] no base classes [...].
143 data().Aggregate = false;
144
145 // C++ [class]p4:
146 // A POD-struct is an aggregate class...
147 data().PlainOldData = false;
148 }
149
Anders Carlssonabb20e62010-03-29 05:13:12 +0000150 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000151 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000152
153 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000154 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000155
John McCall67da35c2010-02-04 22:26:26 +0000156 data().Bases = new(C) CXXBaseSpecifier [NumBases];
157 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000158 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000159 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000160 // Keep track of inherited vbases for this base class.
161 const CXXBaseSpecifier *Base = Bases[i];
162 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000163 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000164 if (BaseType->isDependentType())
165 continue;
166 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000167 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000168
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000169 // A class with a non-empty base class is not empty.
170 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000171 if (!BaseClassDecl->isEmpty()) {
172 if (!data().Empty) {
173 // C++0x [class]p7:
174 // A standard-layout class is a class that:
175 // [...]
176 // -- either has no non-static data members in the most derived
177 // class and at most one base class with non-static data members,
178 // or has no base classes with non-static data members, and
179 // If this is the second non-empty base, then neither of these two
180 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000181 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000182 }
183
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000184 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000185 data().HasNoNonEmptyBases = false;
186 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000187
Douglas Gregor11c024b2010-09-28 20:50:54 +0000188 // C++ [class.virtual]p1:
189 // A class that declares or inherits a virtual function is called a
190 // polymorphic class.
191 if (BaseClassDecl->isPolymorphic())
192 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000193
Chandler Carruthb1963742011-04-30 09:17:45 +0000194 // C++0x [class]p7:
195 // A standard-layout class is a class that: [...]
196 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000197 if (!BaseClassDecl->isStandardLayout())
198 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000199
Chandler Carruthe71d0622011-04-24 02:49:34 +0000200 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000201 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000202 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000203
Anders Carlssonabb20e62010-03-29 05:13:12 +0000204 // Now go through all virtual bases of this base and add them.
Aaron Ballman445a9392014-03-13 16:15:17 +0000205 for (const auto &VBase : BaseClassDecl->vbases()) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000206 // Add this base if it's not already in the list.
Aaron Ballman445a9392014-03-13 16:15:17 +0000207 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType()))) {
208 VBases.push_back(&VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000209
210 // C++11 [class.copy]p8:
211 // The implicitly-declared copy constructor for a class X will have
212 // the form 'X::X(const X&)' if each [...] virtual base class B of X
213 // has a copy constructor whose first parameter is of type
214 // 'const B&' or 'const volatile B&' [...]
Aaron Ballman445a9392014-03-13 16:15:17 +0000215 if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl())
Richard Smith1c33fe82012-11-28 06:23:12 +0000216 if (!VBaseDecl->hasCopyConstructorWithConstParam())
217 data().ImplicitCopyConstructorHasConstParam = false;
218 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000219 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000220
221 if (Base->isVirtual()) {
222 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000223 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smith1c33fe82012-11-28 06:23:12 +0000224 VBases.push_back(Base);
225
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000226 // C++0x [meta.unary.prop] is_empty:
227 // T is a class type, but not a union type, with ... no virtual base
228 // classes
229 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000230
Richard Smith328aae52012-11-30 05:11:39 +0000231 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
232 // A [default constructor, copy/move constructor, or copy/move assignment
233 // operator for a class X] is trivial [...] if:
234 // -- class X has [...] no virtual base classes
235 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000236
237 // C++0x [class]p7:
238 // A standard-layout class is a class that: [...]
239 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000240 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000241
242 // C++11 [dcl.constexpr]p4:
243 // In the definition of a constexpr constructor [...]
244 // -- the class shall not have any virtual base classes
245 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000246 } else {
247 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000248 // A default constructor is trivial [...] if:
249 // -- all the direct base classes of its class have trivial default
250 // constructors.
251 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000252 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
253
Chandler Carruthad7d4042011-04-23 23:10:33 +0000254 // C++0x [class.copy]p13:
255 // A copy/move constructor for class X is trivial if [...]
256 // [...]
257 // -- the constructor selected to copy/move each direct base class
258 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000259 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000260 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000261 // If the base class doesn't have a simple move constructor, we'll eagerly
262 // declare it and perform overload resolution to determine which function
263 // it actually calls. If it does have a simple move constructor, this
264 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000265 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000266 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000267
268 // C++0x [class.copy]p27:
269 // A copy/move assignment operator for class X is trivial if [...]
270 // [...]
271 // -- the assignment operator selected to copy/move each direct base
272 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000273 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000274 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000275 // If the base class doesn't have a simple move assignment, we'll eagerly
276 // declare it and perform overload resolution to determine which function
277 // it actually calls. If it does have a simple move assignment, this
278 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000279 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000280 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000281
282 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000283 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000284 // requirements of a constexpr constructor, the implicitly-defined
285 // default constructor is constexpr.
286 if (!BaseClassDecl->hasConstexprDefaultConstructor())
287 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000288 }
Richard Smith92f241f2012-12-08 02:53:02 +0000289
Douglas Gregor11c024b2010-09-28 20:50:54 +0000290 // C++ [class.ctor]p3:
291 // A destructor is trivial if all the direct base classes of its class
292 // have trivial destructors.
293 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000294 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000295
296 if (!BaseClassDecl->hasIrrelevantDestructor())
297 data().HasIrrelevantDestructor = false;
298
Richard Smith1c33fe82012-11-28 06:23:12 +0000299 // C++11 [class.copy]p18:
300 // The implicitly-declared copy assignment oeprator for a class X will
301 // have the form 'X& X::operator=(const X&)' if each direct base class B
302 // of X has a copy assignment operator whose parameter is of type 'const
303 // B&', 'const volatile B&', or 'B' [...]
304 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
305 data().ImplicitCopyAssignmentHasConstParam = false;
306
307 // C++11 [class.copy]p8:
308 // The implicitly-declared copy constructor for a class X will have
309 // the form 'X::X(const X&)' if each direct [...] base class B of X
310 // has a copy constructor whose first parameter is of type
311 // 'const B&' or 'const volatile B&' [...]
312 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
313 data().ImplicitCopyConstructorHasConstParam = false;
314
John McCall31168b02011-06-15 23:02:42 +0000315 // A class has an Objective-C object member if... or any of its bases
316 // has an Objective-C object member.
317 if (BaseClassDecl->hasObjectMember())
318 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000319
320 if (BaseClassDecl->hasVolatileMember())
321 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000322
Douglas Gregor61226d32011-05-13 01:05:07 +0000323 // Keep track of the presence of mutable fields.
324 if (BaseClassDecl->hasMutableFields())
325 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000326
327 if (BaseClassDecl->hasUninitializedReferenceMember())
328 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000329
330 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000331 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000332
333 if (VBases.empty())
334 return;
335
336 // Create base specifier for any direct or indirect virtual bases.
337 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
338 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000339 for (int I = 0, E = VBases.size(); I != E; ++I) {
340 QualType Type = VBases[I]->getType();
341 if (!Type->isDependentType())
342 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000343 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000344 }
345}
346
347void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
348 // C++11 [class.copy]p11:
349 // A defaulted copy/move constructor for a class X is defined as
350 // deleted if X has:
351 // -- a direct or virtual base class B that cannot be copied/moved [...]
352 // -- a non-static data member of class type M (or array thereof)
353 // that cannot be copied or moved [...]
354 if (!Subobj->hasSimpleMoveConstructor())
355 data().NeedOverloadResolutionForMoveConstructor = true;
356
357 // C++11 [class.copy]p23:
358 // A defaulted copy/move assignment operator 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->hasSimpleMoveAssignment())
364 data().NeedOverloadResolutionForMoveAssignment = true;
365
366 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
367 // A defaulted [ctor or dtor] for a class X is defined as
368 // deleted if X has:
369 // -- any direct or virtual base class [...] has a type with a destructor
370 // that is deleted or inaccessible from the defaulted [ctor or dtor].
371 // -- any non-static data member has a type with a destructor
372 // that is deleted or inaccessible from the defaulted [ctor or dtor].
373 if (!Subobj->hasSimpleDestructor()) {
374 data().NeedOverloadResolutionForMoveConstructor = true;
375 data().NeedOverloadResolutionForDestructor = true;
376 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000377}
378
Douglas Gregord2e6a452010-01-14 17:47:39 +0000379/// Callback function for CXXRecordDecl::forallBases that acknowledges
380/// that it saw a base class.
381static bool SawBase(const CXXRecordDecl *, void *) {
382 return true;
383}
384
385bool CXXRecordDecl::hasAnyDependentBases() const {
386 if (!isDependentContext())
387 return false;
388
389 return !forallBases(SawBase, 0);
390}
391
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000392bool CXXRecordDecl::isTriviallyCopyable() const {
393 // C++0x [class]p5:
394 // A trivially copyable class is a class that:
395 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000396 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000397 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000398 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000399 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000400 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000401 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000402 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000403 // -- has a trivial destructor.
404 if (!hasTrivialDestructor()) return false;
405
406 return true;
407}
408
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000409void CXXRecordDecl::markedVirtualFunctionPure() {
410 // C++ [class.abstract]p2:
411 // A class is abstract if it has at least one pure virtual function.
412 data().Abstract = true;
413}
414
415void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000416 if (!D->isImplicit() &&
417 !isa<FieldDecl>(D) &&
418 !isa<IndirectFieldDecl>(D) &&
419 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
420 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
421 data().HasOnlyCMembers = false;
422
423 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000424 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000425 return;
426
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000427 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
428 if (FunTmpl)
429 D = FunTmpl->getTemplatedDecl();
430
Douglas Gregora832d3e2010-09-28 19:45:33 +0000431 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
432 if (Method->isVirtual()) {
433 // C++ [dcl.init.aggr]p1:
434 // An aggregate is an array or a class with [...] no virtual functions.
435 data().Aggregate = false;
436
437 // C++ [class]p4:
438 // A POD-struct is an aggregate class...
439 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000440
441 // Virtual functions make the class non-empty.
442 // FIXME: Standard ref?
443 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000444
445 // C++ [class.virtual]p1:
446 // A class that declares or inherits a virtual function is called a
447 // polymorphic class.
448 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000449
Richard Smith328aae52012-11-30 05:11:39 +0000450 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
451 // A [default constructor, copy/move constructor, or copy/move
452 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000453 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000454 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000455
Chandler Carruthb1963742011-04-30 09:17:45 +0000456 // C++0x [class]p7:
457 // A standard-layout class is a class that: [...]
458 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000459 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000460 }
461 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000462
Richard Smith1c33fe82012-11-28 06:23:12 +0000463 // Notify the listener if an implicit member was added after the definition
464 // was completed.
465 if (!isBeingDefined() && D->isImplicit())
466 if (ASTMutationListener *L = getASTMutationListener())
467 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000468
Richard Smith328aae52012-11-30 05:11:39 +0000469 // The kind of special member this declaration is, if any.
470 unsigned SMKind = 0;
471
Richard Smith1c33fe82012-11-28 06:23:12 +0000472 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000473 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000474 if (!Constructor->isImplicit()) {
475 // Note that we have a user-declared constructor.
476 data().UserDeclaredConstructor = true;
477
478 // C++ [class]p4:
479 // A POD-struct is an aggregate class [...]
480 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
481 // type is technically an aggregate in C++0x since it wouldn't be in 03.
482 data().PlainOldData = false;
483 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000484
Richard Smitha5285072011-09-05 02:13:09 +0000485 // Technically, "user-provided" is only defined for special member
486 // functions, but the intent of the standard is clearly that it should apply
487 // to all functions.
488 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000489
Alexis Hunt88c75c32011-05-09 21:45:35 +0000490 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000491 SMKind |= SMF_DefaultConstructor;
492
493 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000494 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000495 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000496 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000497 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000498
Chandler Carruthad7d4042011-04-23 23:10:33 +0000499 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000500 unsigned Quals;
501 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000502 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000503
504 if (Quals & Qualifiers::Const)
505 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000506 } else if (Constructor->isMoveConstructor())
507 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000508 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000509
510 // Record if we see any constexpr constructors which are neither copy
511 // nor move constructors.
512 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000513 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000514
Alexis Hunt88c75c32011-05-09 21:45:35 +0000515 // C++ [dcl.init.aggr]p1:
516 // An aggregate is an array or a class with no user-declared
517 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000518 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000519 // An aggregate is an array or a class with no user-provided
520 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000521 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000522 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000523 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000524 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000525
Richard Smith1c33fe82012-11-28 06:23:12 +0000526 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000527 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000528 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000529
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000530 if (DD->isUserProvided())
Richard Smith1c33fe82012-11-28 06:23:12 +0000531 data().HasIrrelevantDestructor = false;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000532 // If the destructor is explicitly defaulted and not trivial or not public
533 // or if the destructor is deleted, we clear HasIrrelevantDestructor in
534 // finishedDefaultedOrDeletedMember.
Richard Smith1c33fe82012-11-28 06:23:12 +0000535
Richard Smith1c33fe82012-11-28 06:23:12 +0000536 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000537 // A destructor is trivial if [...] the destructor is not virtual.
538 if (DD->isVirtual())
539 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000540 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000541
542 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000543 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000544 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000545 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000546
547 const ReferenceType *ParamTy =
548 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
549 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
550 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000551 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000552
Richard Smith328aae52012-11-30 05:11:39 +0000553 if (Method->isMoveAssignmentOperator())
554 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000555
Douglas Gregor457104e2010-09-29 04:25:11 +0000556 // Keep the list of conversion functions up-to-date.
557 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000558 // FIXME: We use the 'unsafe' accessor for the access specifier here,
559 // because Sema may not have set it yet. That's really just a misdesign
560 // in Sema. However, LLDB *will* have set the access specifier correctly,
561 // and adds declarations after the class is technically completed,
562 // so completeDefinition()'s overriding of the access specifiers doesn't
563 // work.
564 AccessSpecifier AS = Conversion->getAccessUnsafe();
565
Richard Smith328aae52012-11-30 05:11:39 +0000566 if (Conversion->getPrimaryTemplate()) {
567 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000568 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000569 ASTContext &Ctx = getASTContext();
570 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
571 NamedDecl *Primary =
572 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
573 if (Primary->getPreviousDecl())
574 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
575 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000576 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000577 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000578 }
579 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000580
Richard Smith328aae52012-11-30 05:11:39 +0000581 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000582 // If this is the first declaration of a special member, we no longer have
583 // an implicit trivial special member.
584 data().HasTrivialSpecialMembers &=
585 data().DeclaredSpecialMembers | ~SMKind;
586
587 if (!Method->isImplicit() && !Method->isUserProvided()) {
588 // This method is user-declared but not user-provided. We can't work out
589 // whether it's trivial yet (not until we get to the end of the class).
590 // We'll handle this method in finishedDefaultedOrDeletedMember.
591 } else if (Method->isTrivial())
592 data().HasTrivialSpecialMembers |= SMKind;
593 else
594 data().DeclaredNonTrivialSpecialMembers |= SMKind;
595
Richard Smith328aae52012-11-30 05:11:39 +0000596 // Note when we have declared a declared special member, and suppress the
597 // implicit declaration of this special member.
598 data().DeclaredSpecialMembers |= SMKind;
599
600 if (!Method->isImplicit()) {
601 data().UserDeclaredSpecialMembers |= SMKind;
602
603 // C++03 [class]p4:
604 // A POD-struct is an aggregate class that has [...] no user-defined
605 // copy assignment operator and no user-defined destructor.
606 //
607 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
608 // aggregates could not have any constructors, clear it even for an
609 // explicitly defaulted or deleted constructor.
610 // type is technically an aggregate in C++0x since it wouldn't be in 03.
611 //
612 // Also, a user-declared move assignment operator makes a class non-POD.
613 // This is an extension in C++03.
614 data().PlainOldData = false;
615 }
Richard Smith328aae52012-11-30 05:11:39 +0000616 }
617
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000618 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000619 }
Richard Smith328aae52012-11-30 05:11:39 +0000620
Douglas Gregora832d3e2010-09-28 19:45:33 +0000621 // Handle non-static data members.
622 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000623 // C++ [class.bit]p2:
624 // A declaration for a bit-field that omits the identifier declares an
625 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
626 // initialized.
627 if (Field->isUnnamedBitfield())
628 return;
629
Douglas Gregora832d3e2010-09-28 19:45:33 +0000630 // C++ [dcl.init.aggr]p1:
631 // An aggregate is an array or a class (clause 9) with [...] no
632 // private or protected non-static data members (clause 11).
633 //
634 // A POD must be an aggregate.
635 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
636 data().Aggregate = false;
637 data().PlainOldData = false;
638 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000639
640 // C++0x [class]p7:
641 // A standard-layout class is a class that:
642 // [...]
643 // -- has the same access control for all non-static data members,
644 switch (D->getAccess()) {
645 case AS_private: data().HasPrivateFields = true; break;
646 case AS_protected: data().HasProtectedFields = true; break;
647 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000648 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000649 };
650 if ((data().HasPrivateFields + data().HasProtectedFields +
651 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000652 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000653
Douglas Gregor61226d32011-05-13 01:05:07 +0000654 // Keep track of the presence of mutable fields.
655 if (Field->isMutable())
656 data().HasMutableFields = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000657
658 // C++11 [class.union]p8, DR1460:
659 // If X is a union, a non-static data member of X that is not an anonymous
660 // union is a variant member of X.
661 if (isUnion() && !Field->isAnonymousStructOrUnion())
662 data().HasVariantMembers = true;
663
Chandler Carruthad7d4042011-04-23 23:10:33 +0000664 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000665 // A POD struct is a class that is both a trivial class and a
666 // standard-layout class, and has no non-static data members of type
667 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000668 //
669 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
670 // that does not explicitly have no lifetime makes the class a non-POD.
671 // However, we delay setting PlainOldData to false in this case so that
672 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor6fa69422012-07-23 04:23:39 +0000673 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCall31168b02011-06-15 23:02:42 +0000674 // In this case, the class will become a non-POD class when we complete
675 // the definition.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000676 ASTContext &Context = getASTContext();
677 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000678 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000679 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCall31168b02011-06-15 23:02:42 +0000680 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
681 setHasObjectMember(true);
Eli Friedmana5433322013-07-20 01:06:31 +0000682 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000683 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000684
Chandler Carruthb1963742011-04-30 09:17:45 +0000685 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000686 if (!Field->hasInClassInitializer())
687 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000688
Chandler Carruthb1963742011-04-30 09:17:45 +0000689 // C++0x [class]p7:
690 // A standard-layout class is a class that:
691 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000692 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000693 }
694
Richard Smith3607ffe2012-02-13 03:54:03 +0000695 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000696 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000697 data().HasNonLiteralTypeFieldsOrBases = true;
698
Richard Smithab44d5b2013-12-10 08:25:00 +0000699 if (Field->hasInClassInitializer() ||
700 (Field->isAnonymousStructOrUnion() &&
701 Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000702 data().HasInClassInitializer = true;
703
704 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000705 // A default constructor is trivial if [...] no non-static data member
706 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000707 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000708
Richard Smithe2648ba2012-05-07 01:07:30 +0000709 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000710 // An aggregate is a [...] class with [...] no
711 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000712 //
713 // This rule was removed in C++1y.
714 if (!getASTContext().getLangOpts().CPlusPlus1y)
715 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000716
Richard Smithe2648ba2012-05-07 01:07:30 +0000717 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000718 // A POD struct is [...] a trivial class.
719 data().PlainOldData = false;
720 }
721
Richard Smith6b02d462012-12-08 08:32:28 +0000722 // C++11 [class.copy]p23:
723 // A defaulted copy/move assignment operator for a class X is defined
724 // as deleted if X has:
725 // -- a non-static data member of reference type
726 if (T->isReferenceType())
727 data().DefaultedMoveAssignmentIsDeleted = true;
728
Douglas Gregor11c024b2010-09-28 20:50:54 +0000729 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
730 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
731 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000732 addedClassSubobject(FieldRec);
733
Richard Smithc91d12c2013-11-25 07:07:05 +0000734 // We may need to perform overload resolution to determine whether a
735 // field can be moved if it's const or volatile qualified.
736 if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) {
737 data().NeedOverloadResolutionForMoveConstructor = true;
738 data().NeedOverloadResolutionForMoveAssignment = true;
739 }
740
Richard Smith6b02d462012-12-08 08:32:28 +0000741 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
742 // A defaulted [special member] for a class X is defined as
743 // deleted if:
744 // -- X is a union-like class that has a variant member with a
745 // non-trivial [corresponding special member]
746 if (isUnion()) {
747 if (FieldRec->hasNonTrivialMoveConstructor())
748 data().DefaultedMoveConstructorIsDeleted = true;
749 if (FieldRec->hasNonTrivialMoveAssignment())
750 data().DefaultedMoveAssignmentIsDeleted = true;
751 if (FieldRec->hasNonTrivialDestructor())
752 data().DefaultedDestructorIsDeleted = true;
753 }
754
Alexis Huntf479f1b2011-05-09 18:22:59 +0000755 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000756 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000757 // -- for all the non-static data members of its class that are of
758 // class type (or array thereof), each such class has a trivial
759 // default constructor.
760 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000761 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000762
763 // C++0x [class.copy]p13:
764 // A copy/move constructor for class X is trivial if [...]
765 // [...]
766 // -- for each non-static data member of X that is of class type (or
767 // an array thereof), the constructor selected to copy/move that
768 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000769 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000770 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000771 // If the field doesn't have a simple move constructor, we'll eagerly
772 // declare the move constructor for this class and we'll decide whether
773 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000774 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000775 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000776
777 // C++0x [class.copy]p27:
778 // A copy/move assignment operator for class X is trivial if [...]
779 // [...]
780 // -- for each non-static data member of X that is of class type (or
781 // an array thereof), the assignment operator selected to
782 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000783 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000784 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000785 // If the field doesn't have a simple move assignment, we'll eagerly
786 // declare the move assignment for this class and we'll decide whether
787 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000788 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000789 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000790
Douglas Gregor11c024b2010-09-28 20:50:54 +0000791 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000792 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000793 if (!FieldRec->hasIrrelevantDestructor())
794 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000795 if (FieldRec->hasObjectMember())
796 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000797 if (FieldRec->hasVolatileMember())
798 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000799
800 // C++0x [class]p7:
801 // A standard-layout class is a class that:
802 // -- has no non-static data members of type non-standard-layout
803 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000804 if (!FieldRec->isStandardLayout())
805 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000806
807 // C++0x [class]p7:
808 // A standard-layout class is a class that:
809 // [...]
810 // -- has no base classes of the same type as the first non-static
811 // data member.
812 // We don't want to expend bits in the state of the record decl
813 // tracking whether this is the first non-static data member so we
814 // cheat a bit and use some of the existing state: the empty bit.
815 // Virtual bases and virtual methods make a class non-empty, but they
816 // also make it non-standard-layout so we needn't check here.
817 // A non-empty base class may leave the class standard-layout, but not
Richard Smithab44d5b2013-12-10 08:25:00 +0000818 // if we have arrived here, and have at least one non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000819 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000820 // data member must come through here with Empty still true, and Empty
821 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000822 if (data().IsStandardLayout && data().Empty) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000823 for (const auto &BI : bases()) {
824 if (Context.hasSameUnqualifiedType(BI.getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000825 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000826 break;
827 }
828 }
829 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000830
831 // Keep track of the presence of mutable fields.
832 if (FieldRec->hasMutableFields())
833 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000834
835 // C++11 [class.copy]p13:
836 // If the implicitly-defined constructor would satisfy the
837 // requirements of a constexpr constructor, the implicitly-defined
838 // constructor is constexpr.
839 // C++11 [dcl.constexpr]p4:
840 // -- every constructor involved in initializing non-static data
841 // members [...] shall be a constexpr constructor
842 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000843 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000844 // The standard requires any in-class initializer to be a constant
845 // expression. We consider this to be a defect.
846 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000847
848 // C++11 [class.copy]p8:
849 // The implicitly-declared copy constructor for a class X will have
850 // the form 'X::X(const X&)' if [...] for all the non-static data
851 // members of X that are of a class type M (or array thereof), each
852 // such class type has a copy constructor whose first parameter is
853 // of type 'const M&' or 'const volatile M&'.
854 if (!FieldRec->hasCopyConstructorWithConstParam())
855 data().ImplicitCopyConstructorHasConstParam = false;
856
857 // C++11 [class.copy]p18:
858 // The implicitly-declared copy assignment oeprator for a class X will
859 // have the form 'X& X::operator=(const X&)' if [...] for all the
860 // non-static data members of X that are of a class type M (or array
861 // thereof), each such class type has a copy assignment operator whose
862 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
863 if (!FieldRec->hasCopyAssignmentWithConstParam())
864 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000865
866 if (FieldRec->hasUninitializedReferenceMember() &&
867 !Field->hasInClassInitializer())
868 data().HasUninitializedReferenceMember = true;
Richard Smithab44d5b2013-12-10 08:25:00 +0000869
870 // C++11 [class.union]p8, DR1460:
871 // a non-static data member of an anonymous union that is a member of
872 // X is also a variant member of X.
873 if (FieldRec->hasVariantMembers() &&
874 Field->isAnonymousStructOrUnion())
875 data().HasVariantMembers = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000876 }
Richard Smithcc36f692011-12-22 02:22:31 +0000877 } else {
878 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000879 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000880 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000881 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000882
883 // C++11 [class.copy]p23:
884 // A defaulted copy/move assignment operator for a class X is defined
885 // as deleted if X has:
886 // -- a non-static data member of const non-class type (or array
887 // thereof)
888 if (T.isConstQualified())
889 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000890 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000891
892 // C++0x [class]p7:
893 // A standard-layout class is a class that:
894 // [...]
895 // -- either has no non-static data members in the most derived
896 // class and at most one base class with non-static data members,
897 // or has no base classes with non-static data members, and
898 // At this point we know that we have a non-static data member, so the last
899 // clause holds.
900 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000901 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000902
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000903 // If this is not a zero-length bit-field, then the class is not empty.
904 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000905 if (!Field->isBitField() ||
906 (!Field->getBitWidth()->isTypeDependent() &&
907 !Field->getBitWidth()->isValueDependent() &&
908 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000909 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000910 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000911 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000912
913 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000914 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000915 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000916 == DeclarationName::CXXConversionFunctionName) {
917 ASTContext &Ctx = getASTContext();
918 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
919 }
920 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000921}
922
Richard Smith92f241f2012-12-08 02:53:02 +0000923void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
924 assert(!D->isImplicit() && !D->isUserProvided());
925
926 // The kind of special member this declaration is, if any.
927 unsigned SMKind = 0;
928
929 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
930 if (Constructor->isDefaultConstructor()) {
931 SMKind |= SMF_DefaultConstructor;
932 if (Constructor->isConstexpr())
933 data().HasConstexprDefaultConstructor = true;
934 }
935 if (Constructor->isCopyConstructor())
936 SMKind |= SMF_CopyConstructor;
937 else if (Constructor->isMoveConstructor())
938 SMKind |= SMF_MoveConstructor;
939 else if (Constructor->isConstexpr())
940 // We may now know that the constructor is constexpr.
941 data().HasConstexprNonCopyMoveConstructor = true;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000942 } else if (isa<CXXDestructorDecl>(D)) {
Richard Smith92f241f2012-12-08 02:53:02 +0000943 SMKind |= SMF_Destructor;
Stephan Tolksdorfd85fa232014-03-27 20:23:12 +0000944 if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted())
945 data().HasIrrelevantDestructor = false;
946 } else if (D->isCopyAssignmentOperator())
Richard Smith92f241f2012-12-08 02:53:02 +0000947 SMKind |= SMF_CopyAssignment;
948 else if (D->isMoveAssignmentOperator())
949 SMKind |= SMF_MoveAssignment;
950
951 // Update which trivial / non-trivial special members we have.
952 // addedMember will have skipped this step for this member.
953 if (D->isTrivial())
954 data().HasTrivialSpecialMembers |= SMKind;
955 else
956 data().DeclaredNonTrivialSpecialMembers |= SMKind;
957}
958
Joao Matose9a3ed42012-08-31 22:18:20 +0000959bool CXXRecordDecl::isCLike() const {
960 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
961 !TemplateOrInstantiation.isNull())
962 return false;
963 if (!hasDefinition())
964 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000965
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000966 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000967}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000968
Faisal Vali2b391ab2013-09-26 19:54:12 +0000969bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000970 if (!isLambda()) return false;
971 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000972}
973
974CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
975 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000976 DeclarationName Name =
977 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000978 DeclContext::lookup_const_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000979
980 assert(!Calls.empty() && "Missing lambda call operator!");
981 assert(Calls.size() == 1 && "More than one lambda call operator!");
982
983 NamedDecl *CallOp = Calls.front();
984 if (FunctionTemplateDecl *CallOpTmpl =
985 dyn_cast<FunctionTemplateDecl>(CallOp))
986 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000987
988 return cast<CXXMethodDecl>(CallOp);
989}
990
991CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
992 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000993 DeclarationName Name =
994 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000995 DeclContext::lookup_const_result Invoker = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000996 if (Invoker.empty()) return 0;
997 assert(Invoker.size() == 1 && "More than one static invoker operator!");
998 NamedDecl *InvokerFun = Invoker.front();
999 if (FunctionTemplateDecl *InvokerTemplate =
1000 dyn_cast<FunctionTemplateDecl>(InvokerFun))
1001 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
1002
Faisal Vali571df122013-09-29 08:45:24 +00001003 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +00001004}
1005
Douglas Gregor9c702202012-02-10 07:45:31 +00001006void CXXRecordDecl::getCaptureFields(
1007 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +00001008 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +00001009 Captures.clear();
1010 ThisCapture = 0;
1011
Douglas Gregorc8a73492012-02-13 15:44:47 +00001012 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +00001013 RecordDecl::field_iterator Field = field_begin();
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001014 for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +00001015 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +00001016 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +00001017 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +00001018 else if (C->capturesVariable())
1019 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001020 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001021 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001022}
1023
Faisal Vali2b391ab2013-09-26 19:54:12 +00001024TemplateParameterList *
1025CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1026 if (!isLambda()) return 0;
1027 CXXMethodDecl *CallOp = getLambdaCallOperator();
1028 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1029 return Tmpl->getTemplateParameters();
1030 return 0;
1031}
Douglas Gregor9c702202012-02-10 07:45:31 +00001032
John McCall1e3a1a72010-03-15 09:07:48 +00001033static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
Alp Tokera2794f92014-01-22 07:29:52 +00001034 QualType T =
1035 cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction())
1036 ->getConversionType();
John McCall1e3a1a72010-03-15 09:07:48 +00001037 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001038}
1039
John McCall1e3a1a72010-03-15 09:07:48 +00001040/// Collect the visible conversions of a base class.
1041///
James Dennettb5d5f812012-06-15 22:28:09 +00001042/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001043/// \param InVirtual whether this base class is a virtual base (or a base
1044/// of a virtual base)
1045/// \param Access the access along the inheritance path to this base
1046/// \param ParentHiddenTypes the conversions provided by the inheritors
1047/// of this base
1048/// \param Output the set to which to add conversions from non-virtual bases
1049/// \param VOutput the set to which to add conversions from virtual bases
1050/// \param HiddenVBaseCs the set of conversions which were hidden in a
1051/// virtual base along some inheritance path
1052static void CollectVisibleConversions(ASTContext &Context,
1053 CXXRecordDecl *Record,
1054 bool InVirtual,
1055 AccessSpecifier Access,
1056 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001057 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001058 UnresolvedSetImpl &VOutput,
1059 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1060 // The set of types which have conversions in this class or its
1061 // subclasses. As an optimization, we don't copy the derived set
1062 // unless it might change.
1063 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1064 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1065
1066 // Collect the direct conversions and figure out which conversions
1067 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001068 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1069 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1070 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001071 HiddenTypesBuffer = ParentHiddenTypes;
1072 HiddenTypes = &HiddenTypesBuffer;
1073
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001074 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001075 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1076 bool Hidden = ParentHiddenTypes.count(ConvType);
1077 if (!Hidden)
1078 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001079
1080 // If this conversion is hidden and we're in a virtual base,
1081 // remember that it's hidden along some inheritance path.
1082 if (Hidden && InVirtual)
1083 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1084
1085 // If this conversion isn't hidden, add it to the appropriate output.
1086 else if (!Hidden) {
1087 AccessSpecifier IAccess
1088 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1089
1090 if (InVirtual)
1091 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001092 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001093 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001094 }
1095 }
1096 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001097
John McCall1e3a1a72010-03-15 09:07:48 +00001098 // Collect information recursively from any base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001099 for (const auto &I : Record->bases()) {
1100 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001101 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001102
John McCall1e3a1a72010-03-15 09:07:48 +00001103 AccessSpecifier BaseAccess
Aaron Ballman574705e2014-03-13 15:41:46 +00001104 = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier());
1105 bool BaseInVirtual = InVirtual || I.isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001106
John McCall1e3a1a72010-03-15 09:07:48 +00001107 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1108 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1109 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001110 }
John McCall1e3a1a72010-03-15 09:07:48 +00001111}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001112
John McCall1e3a1a72010-03-15 09:07:48 +00001113/// Collect the visible conversions of a class.
1114///
1115/// This would be extremely straightforward if it weren't for virtual
1116/// bases. It might be worth special-casing that, really.
1117static void CollectVisibleConversions(ASTContext &Context,
1118 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001119 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001120 // The collection of all conversions in virtual bases that we've
1121 // found. These will be added to the output as long as they don't
1122 // appear in the hidden-conversions set.
1123 UnresolvedSet<8> VBaseCs;
1124
1125 // The set of conversions in virtual bases that we've determined to
1126 // be hidden.
1127 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1128
1129 // The set of types hidden by classes derived from this one.
1130 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1131
1132 // Go ahead and collect the direct conversions and add them to the
1133 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001134 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1135 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001136 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001137 for (; ConvI != ConvE; ++ConvI)
1138 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001139
1140 // Recursively collect conversions from base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +00001141 for (const auto &I : Record->bases()) {
1142 const RecordType *RT = I.getType()->getAs<RecordType>();
John McCall1e3a1a72010-03-15 09:07:48 +00001143 if (!RT) continue;
1144
1145 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
Aaron Ballman574705e2014-03-13 15:41:46 +00001146 I.isVirtual(), I.getAccessSpecifier(),
John McCall1e3a1a72010-03-15 09:07:48 +00001147 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1148 }
1149
1150 // Add any unhidden conversions provided by virtual bases.
1151 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1152 I != E; ++I) {
1153 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001154 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001155 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001156}
1157
1158/// getVisibleConversionFunctions - get all conversion functions visible
1159/// in current class; including conversion function templates.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001160std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1161CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001162 ASTContext &Ctx = getASTContext();
1163
1164 ASTUnresolvedSet *Set;
1165 if (bases_begin() == bases_end()) {
1166 // If root class, all conversions are visible.
1167 Set = &data().Conversions.get(Ctx);
1168 } else {
1169 Set = &data().VisibleConversions.get(Ctx);
1170 // If visible conversion list is not evaluated, evaluate it.
1171 if (!data().ComputedVisibleConversions) {
1172 CollectVisibleConversions(Ctx, this, *Set);
1173 data().ComputedVisibleConversions = true;
1174 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001175 }
Richard Smitha4ba74c2013-08-30 04:46:40 +00001176 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001177}
1178
John McCallda4458e2010-03-31 01:36:47 +00001179void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1180 // This operation is O(N) but extremely rare. Sema only uses it to
1181 // remove UsingShadowDecls in a class that were followed by a direct
1182 // declaration, e.g.:
1183 // class A : B {
1184 // using B::operator int;
1185 // operator int();
1186 // };
1187 // This is uncommon by itself and even more uncommon in conjunction
1188 // with sufficiently large numbers of directly-declared conversions
1189 // that asymptotic behavior matters.
1190
Richard Smitha4ba74c2013-08-30 04:46:40 +00001191 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001192 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1193 if (Convs[I].getDecl() == ConvDecl) {
1194 Convs.erase(I);
1195 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1196 && "conversion was found multiple times in unresolved set");
1197 return;
1198 }
1199 }
1200
1201 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001202}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001203
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001204CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001205 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001206 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1207
1208 return 0;
1209}
1210
1211void
1212CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1213 TemplateSpecializationKind TSK) {
1214 assert(TemplateOrInstantiation.isNull() &&
1215 "Previous template or instantiation?");
Richard Smith8a0dde72013-12-14 01:04:22 +00001216 assert(!isa<ClassTemplatePartialSpecializationDecl>(this));
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001217 TemplateOrInstantiation
1218 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1219}
1220
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001221TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1222 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001223 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1224 return Spec->getSpecializationKind();
1225
Douglas Gregor06db9f52009-10-12 20:18:28 +00001226 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001227 return MSInfo->getTemplateSpecializationKind();
1228
1229 return TSK_Undeclared;
1230}
1231
1232void
1233CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1234 if (ClassTemplateSpecializationDecl *Spec
1235 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1236 Spec->setSpecializationKind(TSK);
1237 return;
1238 }
1239
Douglas Gregor06db9f52009-10-12 20:18:28 +00001240 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001241 MSInfo->setTemplateSpecializationKind(TSK);
1242 return;
1243 }
1244
David Blaikie83d382b2011-09-23 05:06:16 +00001245 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001246}
1247
Douglas Gregorbac74902010-07-01 14:13:13 +00001248CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1249 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001250 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001251
1252 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001253 = Context.DeclarationNames.getCXXDestructorName(
1254 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001255
David Blaikieff7d47a2012-12-19 00:45:41 +00001256 DeclContext::lookup_const_result R = lookup(Name);
1257 if (R.empty())
Sebastian Redlb469afb2010-09-02 23:19:42 +00001258 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001259
David Blaikieff7d47a2012-12-19 00:45:41 +00001260 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001261 return Dtor;
1262}
1263
Douglas Gregorb11aad82011-02-19 18:51:44 +00001264void CXXRecordDecl::completeDefinition() {
1265 completeDefinition(0);
1266}
1267
1268void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1269 RecordDecl::completeDefinition();
1270
David Blaikiebbafb8a2012-03-11 07:00:24 +00001271 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +00001272 // Objective-C Automatic Reference Counting:
1273 // If a class has a non-static data member of Objective-C pointer
1274 // type (or array thereof), it is a non-POD type and its
Douglas Gregor6fa69422012-07-23 04:23:39 +00001275 // default constructor (if any), copy constructor, move constructor,
1276 // copy assignment operator, move assignment operator, and destructor are
1277 // non-trivial.
John McCall31168b02011-06-15 23:02:42 +00001278 struct DefinitionData &Data = data();
1279 Data.PlainOldData = false;
Richard Smith328aae52012-11-30 05:11:39 +00001280 Data.HasTrivialSpecialMembers = 0;
Richard Smith561fb152012-02-25 07:33:38 +00001281 Data.HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001282 }
1283
Douglas Gregor8fb95122010-09-29 00:15:42 +00001284 // If the class may be abstract (but hasn't been marked as such), check for
1285 // any pure final overriders.
1286 if (mayBeAbstract()) {
1287 CXXFinalOverriderMap MyFinalOverriders;
1288 if (!FinalOverriders) {
1289 getFinalOverriders(MyFinalOverriders);
1290 FinalOverriders = &MyFinalOverriders;
1291 }
1292
1293 bool Done = false;
1294 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1295 MEnd = FinalOverriders->end();
1296 M != MEnd && !Done; ++M) {
1297 for (OverridingMethods::iterator SO = M->second.begin(),
1298 SOEnd = M->second.end();
1299 SO != SOEnd && !Done; ++SO) {
1300 assert(SO->second.size() > 0 &&
1301 "All virtual functions have overridding virtual functions");
1302
1303 // C++ [class.abstract]p4:
1304 // A class is abstract if it contains or inherits at least one
1305 // pure virtual function for which the final overrider is pure
1306 // virtual.
1307 if (SO->second.front().Method->isPure()) {
1308 data().Abstract = true;
1309 Done = true;
1310 break;
1311 }
1312 }
1313 }
1314 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001315
1316 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001317 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001318 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001319 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001320}
1321
1322bool CXXRecordDecl::mayBeAbstract() const {
1323 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1324 isDependentContext())
1325 return false;
1326
Aaron Ballman574705e2014-03-13 15:41:46 +00001327 for (const auto &B : bases()) {
Douglas Gregor8fb95122010-09-29 00:15:42 +00001328 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00001329 = cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001330 if (BaseDecl->isAbstract())
1331 return true;
1332 }
1333
1334 return false;
1335}
1336
David Blaikie68e081d2011-12-20 02:48:34 +00001337void CXXMethodDecl::anchor() { }
1338
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001339bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001340 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001341
1342 if (MD->getStorageClass() == SC_Static)
1343 return true;
1344
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001345 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1346 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001347}
1348
Rafael Espindola49e860b2012-06-26 17:45:31 +00001349static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1350 const CXXMethodDecl *BaseMD) {
1351 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1352 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1353 const CXXMethodDecl *MD = *I;
1354 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1355 return true;
1356 if (recursivelyOverrides(MD, BaseMD))
1357 return true;
1358 }
1359 return false;
1360}
1361
1362CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001363CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1364 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001365 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1366 return this;
1367
1368 // Lookup doesn't work for destructors, so handle them separately.
1369 if (isa<CXXDestructorDecl>(this)) {
1370 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001371 if (MD) {
1372 if (recursivelyOverrides(MD, this))
1373 return MD;
1374 if (MayBeBase && recursivelyOverrides(this, MD))
1375 return MD;
1376 }
Rafael Espindola49e860b2012-06-26 17:45:31 +00001377 return NULL;
1378 }
1379
1380 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001381 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001382 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1383 if (!MD)
1384 continue;
1385 if (recursivelyOverrides(MD, this))
1386 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001387 if (MayBeBase && recursivelyOverrides(this, MD))
1388 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001389 }
1390
Aaron Ballman574705e2014-03-13 15:41:46 +00001391 for (const auto &I : RD->bases()) {
1392 const RecordType *RT = I.getType()->getAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +00001393 if (!RT)
1394 continue;
1395 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1396 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1397 if (T)
1398 return T;
1399 }
1400
1401 return NULL;
1402}
1403
Ted Kremenek21475702008-09-05 17:16:31 +00001404CXXMethodDecl *
1405CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001406 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001407 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001408 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001409 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001410 bool isConstexpr, SourceLocation EndLocation) {
Richard Smithf7981722013-11-22 09:01:48 +00001411 return new (C, RD) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
1412 SC, isInline, isConstexpr, EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001413}
1414
Douglas Gregor72172e92012-01-05 21:55:30 +00001415CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001416 return new (C, ID) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1417 DeclarationNameInfo(), QualType(), 0,
1418 SC_None, false, false, SourceLocation());
Douglas Gregor72172e92012-01-05 21:55:30 +00001419}
1420
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001421bool CXXMethodDecl::isUsualDeallocationFunction() const {
1422 if (getOverloadedOperator() != OO_Delete &&
1423 getOverloadedOperator() != OO_Array_Delete)
1424 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001425
1426 // C++ [basic.stc.dynamic.deallocation]p2:
1427 // A template instance is never a usual deallocation function,
1428 // regardless of its signature.
1429 if (getPrimaryTemplate())
1430 return false;
1431
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001432 // C++ [basic.stc.dynamic.deallocation]p2:
1433 // If a class T has a member deallocation function named operator delete
1434 // with exactly one parameter, then that function is a usual (non-placement)
1435 // deallocation function. [...]
1436 if (getNumParams() == 1)
1437 return true;
1438
1439 // C++ [basic.stc.dynamic.deallocation]p2:
1440 // [...] If class T does not declare such an operator delete but does
1441 // declare a member deallocation function named operator delete with
1442 // exactly two parameters, the second of which has type std::size_t (18.1),
1443 // then this function is a usual deallocation function.
1444 ASTContext &Context = getASTContext();
1445 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001446 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1447 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001448 return false;
1449
1450 // This function is a usual deallocation function if there are no
1451 // single-parameter deallocation functions of the same kind.
David Blaikieff7d47a2012-12-19 00:45:41 +00001452 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1453 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1454 I != E; ++I) {
1455 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001456 if (FD->getNumParams() == 1)
1457 return false;
1458 }
1459
1460 return true;
1461}
1462
Douglas Gregorb139cd52010-05-01 20:49:11 +00001463bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001464 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001465 // A user-declared copy assignment operator X::operator= is a non-static
1466 // non-template member function of class X with exactly one parameter of
1467 // type X, X&, const X&, volatile X& or const volatile X&.
1468 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1469 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001470 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1471 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001472 return false;
1473
1474 QualType ParamType = getParamDecl(0)->getType();
1475 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1476 ParamType = Ref->getPointeeType();
1477
1478 ASTContext &Context = getASTContext();
1479 QualType ClassType
1480 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1481 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1482}
1483
Alexis Huntfcaeae42011-05-25 20:50:04 +00001484bool CXXMethodDecl::isMoveAssignmentOperator() const {
1485 // C++0x [class.copy]p19:
1486 // A user-declared move assignment operator X::operator= is a non-static
1487 // non-template member function of class X with exactly one parameter of type
1488 // X&&, const X&&, volatile X&&, or const volatile X&&.
1489 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001490 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1491 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001492 return false;
1493
1494 QualType ParamType = getParamDecl(0)->getType();
1495 if (!isa<RValueReferenceType>(ParamType))
1496 return false;
1497 ParamType = ParamType->getPointeeType();
1498
1499 ASTContext &Context = getASTContext();
1500 QualType ClassType
1501 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1502 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1503}
1504
Anders Carlsson36d87e12009-05-16 23:58:37 +00001505void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001506 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001507 assert(!MD->getParent()->isDependentContext() &&
1508 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001509 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001510
Douglas Gregor832940b2010-03-02 23:58:15 +00001511 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001512}
1513
1514CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001515 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001516 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001517}
1518
1519CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001520 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001521 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001522}
1523
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001524unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001525 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001526 return getASTContext().overridden_methods_size(this);
1527}
1528
Ted Kremenek21475702008-09-05 17:16:31 +00001529QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001530 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1531 // If the member function is declared const, the type of this is const X*,
1532 // if the member function is declared volatile, the type of this is
1533 // volatile X*, and if the member function is declared const volatile,
1534 // the type of this is const volatile X*.
1535
Ted Kremenek21475702008-09-05 17:16:31 +00001536 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001537
John McCalle78aac42010-03-10 03:28:59 +00001538 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001539 ClassTy = C.getQualifiedType(ClassTy,
1540 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001541 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001542}
1543
Eli Friedman71a26d82009-12-06 20:50:05 +00001544bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001545 // If this function is a template instantiation, look at the template from
1546 // which it was instantiated.
1547 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1548 if (!CheckFn)
1549 CheckFn = this;
1550
Eli Friedman71a26d82009-12-06 20:50:05 +00001551 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001552 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001553}
1554
Douglas Gregor355efbb2012-02-17 03:02:34 +00001555bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001556 const CXXRecordDecl *P = getParent();
1557 if (P->isLambda()) {
1558 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1559 if (StaticInvoker == this) return true;
1560 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1561 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1562 }
1563 }
1564 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001565}
1566
Alexis Hunt1d792652011-01-08 20:30:50 +00001567CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1568 TypeSourceInfo *TInfo, bool IsVirtual,
1569 SourceLocation L, Expr *Init,
1570 SourceLocation R,
1571 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001572 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001573 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1574 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001575{
Douglas Gregore8381c02008-11-05 04:29:56 +00001576}
1577
Alexis Hunt1d792652011-01-08 20:30:50 +00001578CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1579 FieldDecl *Member,
1580 SourceLocation MemberLoc,
1581 SourceLocation L, Expr *Init,
1582 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001583 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001584 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001585 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1586{
1587}
1588
Alexis Hunt1d792652011-01-08 20:30:50 +00001589CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1590 IndirectFieldDecl *Member,
1591 SourceLocation MemberLoc,
1592 SourceLocation L, Expr *Init,
1593 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001594 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001595 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001596 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001597{
Douglas Gregore8381c02008-11-05 04:29:56 +00001598}
1599
Alexis Hunt1d792652011-01-08 20:30:50 +00001600CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001601 TypeSourceInfo *TInfo,
1602 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001603 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001604 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1605 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001606 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1607{
1608}
1609
1610CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001611 FieldDecl *Member,
1612 SourceLocation MemberLoc,
1613 SourceLocation L, Expr *Init,
1614 SourceLocation R,
1615 VarDecl **Indices,
1616 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001617 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichetd583da02010-12-04 09:14:42 +00001618 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001619 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001620{
1621 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1622 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1623}
1624
Alexis Hunt1d792652011-01-08 20:30:50 +00001625CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1626 FieldDecl *Member,
1627 SourceLocation MemberLoc,
1628 SourceLocation L, Expr *Init,
1629 SourceLocation R,
1630 VarDecl **Indices,
1631 unsigned NumIndices) {
1632 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001633 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001634 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001635 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1636 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001637}
1638
Alexis Hunt1d792652011-01-08 20:30:50 +00001639TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001640 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001641 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001642 else
1643 return TypeLoc();
1644}
1645
Alexis Hunt1d792652011-01-08 20:30:50 +00001646const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001647 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001648 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001649 else
1650 return 0;
1651}
1652
Alexis Hunt1d792652011-01-08 20:30:50 +00001653SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001654 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001655 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001656
1657 if (isInClassMemberInitializer())
1658 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001659
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001660 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1661 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1662
1663 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001664}
1665
Alexis Hunt1d792652011-01-08 20:30:50 +00001666SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001667 if (isInClassMemberInitializer()) {
1668 FieldDecl *D = getAnyMember();
1669 if (Expr *I = D->getInClassInitializer())
1670 return I->getSourceRange();
1671 return SourceRange();
1672 }
1673
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001674 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001675}
1676
David Blaikie68e081d2011-12-20 02:48:34 +00001677void CXXConstructorDecl::anchor() { }
1678
Douglas Gregor61956c42008-10-31 09:07:45 +00001679CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001680CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001681 return new (C, ID) CXXConstructorDecl(0, SourceLocation(),
1682 DeclarationNameInfo(), QualType(),
1683 0, false, false, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001684}
1685
1686CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001687CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001688 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001689 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001690 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001691 bool isExplicit, bool isInline,
1692 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001693 assert(NameInfo.getName().getNameKind()
1694 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001695 "Name must refer to a constructor");
Richard Smithf7981722013-11-22 09:01:48 +00001696 return new (C, RD) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1697 isExplicit, isInline,
1698 isImplicitlyDeclared, isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001699}
1700
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001701CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1702 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1703 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1704 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1705 return Construct->getConstructor();
1706
1707 return 0;
1708}
1709
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001710bool CXXConstructorDecl::isDefaultConstructor() const {
1711 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001712 // A default constructor for a class X is a constructor of class
1713 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001714 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001715 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001716}
1717
Mike Stump11289f42009-09-09 15:08:12 +00001718bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001719CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001720 return isCopyOrMoveConstructor(TypeQuals) &&
1721 getParamDecl(0)->getType()->isLValueReferenceType();
1722}
1723
1724bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1725 return isCopyOrMoveConstructor(TypeQuals) &&
1726 getParamDecl(0)->getType()->isRValueReferenceType();
1727}
1728
1729/// \brief Determine whether this is a copy or move constructor.
1730bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001731 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001732 // A non-template constructor for class X is a copy constructor
1733 // if its first parameter is of type X&, const X&, volatile X& or
1734 // const volatile X&, and either there are no other parameters
1735 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001736 // C++0x [class.copy]p3:
1737 // A non-template constructor for class X is a move constructor if its
1738 // first parameter is of type X&&, const X&&, volatile X&&, or
1739 // const volatile X&&, and either there are no other parameters or else
1740 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001741 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001742 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +00001743 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001744 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001745 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001746
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001747 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001748
1749 // Do we have a reference type?
1750 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001751 if (!ParamRefType)
1752 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001753
Douglas Gregorff7028a2009-11-13 23:59:09 +00001754 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001755 ASTContext &Context = getASTContext();
1756
Douglas Gregorff7028a2009-11-13 23:59:09 +00001757 CanQualType PointeeType
1758 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001759 CanQualType ClassTy
1760 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001761 if (PointeeType.getUnqualifiedType() != ClassTy)
1762 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001763
John McCall8ccfcb52009-09-24 19:53:00 +00001764 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001765
1766 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001767 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001768 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001769}
1770
Anders Carlssond20e7952009-08-28 16:57:08 +00001771bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001772 // C++ [class.conv.ctor]p1:
1773 // A constructor declared without the function-specifier explicit
1774 // that can be called with a single parameter specifies a
1775 // conversion from the type of its first parameter to the type of
1776 // its class. Such a constructor is called a converting
1777 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001778 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001779 return false;
1780
Mike Stump11289f42009-09-09 15:08:12 +00001781 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001782 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001783 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001784 (getNumParams() > 1 &&
1785 (getParamDecl(1)->hasDefaultArg() ||
1786 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001787}
Douglas Gregor61956c42008-10-31 09:07:45 +00001788
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001789bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001790 if ((getNumParams() < 1) ||
1791 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1792 (getPrimaryTemplate() == 0) ||
1793 (getDescribedFunctionTemplate() != 0))
1794 return false;
1795
1796 const ParmVarDecl *Param = getParamDecl(0);
1797
1798 ASTContext &Context = getASTContext();
1799 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1800
Douglas Gregorffe14e32009-11-14 01:20:54 +00001801 // Is it the same as our our class type?
1802 CanQualType ClassTy
1803 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1804 if (ParamType.getUnqualifiedType() != ClassTy)
1805 return false;
1806
1807 return true;
1808}
1809
Sebastian Redl08905022011-02-05 19:23:19 +00001810const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1811 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001812 method_iterator It = getASTContext().overridden_methods_begin(this);
1813 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redl08905022011-02-05 19:23:19 +00001814 return 0;
1815
1816 return cast<CXXConstructorDecl>(*It);
1817}
1818
1819void
1820CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1821 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001822 assert(getASTContext().overridden_methods_size(this) == 0 &&
1823 "Base ctor already set.");
1824 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001825}
1826
David Blaikie68e081d2011-12-20 02:48:34 +00001827void CXXDestructorDecl::anchor() { }
1828
Douglas Gregor831c93f2008-11-05 20:51:48 +00001829CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001830CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001831 return new (C, ID) CXXDestructorDecl(
1832 0, SourceLocation(), DeclarationNameInfo(), QualType(), 0, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001833}
1834
1835CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001836CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001837 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001838 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001839 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001840 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001841 assert(NameInfo.getName().getNameKind()
1842 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001843 "Name must refer to a destructor");
Richard Smithf7981722013-11-22 09:01:48 +00001844 return new (C, RD) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo,
1845 isInline, isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001846}
1847
David Blaikie68e081d2011-12-20 02:48:34 +00001848void CXXConversionDecl::anchor() { }
1849
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001850CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001851CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001852 return new (C, ID) CXXConversionDecl(0, SourceLocation(),
1853 DeclarationNameInfo(), QualType(),
1854 0, false, false, false,
1855 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001856}
1857
1858CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001859CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001860 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001861 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001862 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001863 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001864 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001865 assert(NameInfo.getName().getNameKind()
1866 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001867 "Name must refer to a conversion function");
Richard Smithf7981722013-11-22 09:01:48 +00001868 return new (C, RD) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
1869 isInline, isExplicit, isConstexpr,
1870 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001871}
1872
Douglas Gregord3b672c2012-02-16 01:06:16 +00001873bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1874 return isImplicit() && getParent()->isLambda() &&
1875 getConversionType()->isBlockPointerType();
1876}
1877
David Blaikie68e081d2011-12-20 02:48:34 +00001878void LinkageSpecDecl::anchor() { }
1879
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001880LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001881 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001882 SourceLocation ExternLoc,
1883 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001884 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001885 bool HasBraces) {
Richard Smithf7981722013-11-22 09:01:48 +00001886 return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001887}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001888
Richard Smithf7981722013-11-22 09:01:48 +00001889LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C,
1890 unsigned ID) {
1891 return new (C, ID) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
1892 lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001893}
1894
David Blaikie68e081d2011-12-20 02:48:34 +00001895void UsingDirectiveDecl::anchor() { }
1896
Douglas Gregor889ceb72009-02-03 19:21:40 +00001897UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1898 SourceLocation L,
1899 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001900 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001901 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001902 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001903 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001904 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1905 Used = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001906 return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1907 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001908}
1909
Richard Smithf7981722013-11-22 09:01:48 +00001910UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C,
1911 unsigned ID) {
1912 return new (C, ID) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1913 NestedNameSpecifierLoc(),
1914 SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001915}
1916
Sebastian Redla6602e92009-11-23 15:34:23 +00001917NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1918 if (NamespaceAliasDecl *NA =
1919 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1920 return NA->getNamespace();
1921 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1922}
1923
Douglas Gregore57e7522012-01-07 09:11:48 +00001924NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1925 SourceLocation StartLoc,
1926 SourceLocation IdLoc, IdentifierInfo *Id,
1927 NamespaceDecl *PrevDecl)
1928 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1929 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1930{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001931 setPreviousDecl(PrevDecl);
Douglas Gregore57e7522012-01-07 09:11:48 +00001932
1933 if (PrevDecl)
1934 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1935}
1936
Douglas Gregor72172e92012-01-05 21:55:30 +00001937NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001938 bool Inline, SourceLocation StartLoc,
1939 SourceLocation IdLoc, IdentifierInfo *Id,
1940 NamespaceDecl *PrevDecl) {
Richard Smithf7981722013-11-22 09:01:48 +00001941 return new (C, DC) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001942}
1943
1944NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001945 return new (C, ID) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1946 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001947}
1948
Richard Smithd7af8a32014-05-10 01:17:36 +00001949NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() {
1950 return getNextRedeclaration();
Rafael Espindola8ef7f682013-11-26 15:12:20 +00001951}
1952NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() {
1953 return getPreviousDecl();
1954}
1955NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() {
1956 return getMostRecentDecl();
1957}
1958
David Blaikie68e081d2011-12-20 02:48:34 +00001959void NamespaceAliasDecl::anchor() { }
1960
Mike Stump11289f42009-09-09 15:08:12 +00001961NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00001962 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001963 SourceLocation AliasLoc,
1964 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001965 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001966 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001967 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001968 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1969 Namespace = NS->getOriginalNamespace();
Richard Smithf7981722013-11-22 09:01:48 +00001970 return new (C, DC) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1971 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001972}
1973
Douglas Gregor72172e92012-01-05 21:55:30 +00001974NamespaceAliasDecl *
1975NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001976 return new (C, ID) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(),
1977 0, NestedNameSpecifierLoc(),
1978 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001979}
1980
David Blaikie68e081d2011-12-20 02:48:34 +00001981void UsingShadowDecl::anchor() { }
1982
Douglas Gregor72172e92012-01-05 21:55:30 +00001983UsingShadowDecl *
1984UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00001985 return new (C, ID) UsingShadowDecl(0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001986}
1987
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001988UsingDecl *UsingShadowDecl::getUsingDecl() const {
1989 const UsingShadowDecl *Shadow = this;
1990 while (const UsingShadowDecl *NextShadow =
1991 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1992 Shadow = NextShadow;
1993 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1994}
1995
David Blaikie68e081d2011-12-20 02:48:34 +00001996void UsingDecl::anchor() { }
1997
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001998void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1999 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
2000 "declaration already in set");
2001 assert(S->getUsingDecl() == this);
2002
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002003 if (FirstUsingShadow.getPointer())
2004 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
2005 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002006}
2007
2008void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
2009 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
2010 "declaration not in set");
2011 assert(S->getUsingDecl() == this);
2012
2013 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2014
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002015 if (FirstUsingShadow.getPointer() == S) {
2016 FirstUsingShadow.setPointer(
2017 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002018 S->UsingOrNextShadow = this;
2019 return;
2020 }
2021
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002022 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002023 while (Prev->UsingOrNextShadow != S)
2024 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2025 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2026 S->UsingOrNextShadow = this;
2027}
2028
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002029UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2030 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002031 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002032 bool HasTypename) {
Richard Smithf7981722013-11-22 09:01:48 +00002033 return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002034}
2035
Douglas Gregor72172e92012-01-05 21:55:30 +00002036UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002037 return new (C, ID) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2038 DeclarationNameInfo(), false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002039}
2040
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002041SourceRange UsingDecl::getSourceRange() const {
2042 SourceLocation Begin = isAccessDeclaration()
2043 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2044 return SourceRange(Begin, getNameInfo().getEndLoc());
2045}
2046
David Blaikie68e081d2011-12-20 02:48:34 +00002047void UnresolvedUsingValueDecl::anchor() { }
2048
John McCalle61f2ba2009-11-18 02:36:19 +00002049UnresolvedUsingValueDecl *
2050UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2051 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002052 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002053 const DeclarationNameInfo &NameInfo) {
Richard Smithf7981722013-11-22 09:01:48 +00002054 return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
2055 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002056}
2057
Douglas Gregor72172e92012-01-05 21:55:30 +00002058UnresolvedUsingValueDecl *
2059UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002060 return new (C, ID) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2061 NestedNameSpecifierLoc(),
Douglas Gregor72172e92012-01-05 21:55:30 +00002062 DeclarationNameInfo());
2063}
2064
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002065SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2066 SourceLocation Begin = isAccessDeclaration()
2067 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2068 return SourceRange(Begin, getNameInfo().getEndLoc());
2069}
2070
David Blaikie68e081d2011-12-20 02:48:34 +00002071void UnresolvedUsingTypenameDecl::anchor() { }
2072
John McCalle61f2ba2009-11-18 02:36:19 +00002073UnresolvedUsingTypenameDecl *
2074UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2075 SourceLocation UsingLoc,
2076 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002077 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002078 SourceLocation TargetNameLoc,
2079 DeclarationName TargetName) {
Richard Smithf7981722013-11-22 09:01:48 +00002080 return new (C, DC) UnresolvedUsingTypenameDecl(
2081 DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc,
2082 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002083}
2084
Douglas Gregor72172e92012-01-05 21:55:30 +00002085UnresolvedUsingTypenameDecl *
2086UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002087 return new (C, ID) UnresolvedUsingTypenameDecl(
2088 0, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(),
2089 SourceLocation(), 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00002090}
2091
David Blaikie68e081d2011-12-20 02:48:34 +00002092void StaticAssertDecl::anchor() { }
2093
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002094StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002095 SourceLocation StaticAssertLoc,
2096 Expr *AssertExpr,
2097 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002098 SourceLocation RParenLoc,
2099 bool Failed) {
Richard Smithf7981722013-11-22 09:01:48 +00002100 return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
2101 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002102}
2103
Richard Smithf7981722013-11-22 09:01:48 +00002104StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
Douglas Gregor72172e92012-01-05 21:55:30 +00002105 unsigned ID) {
Richard Smithf7981722013-11-22 09:01:48 +00002106 return new (C, ID) StaticAssertDecl(0, SourceLocation(), 0, 0,
2107 SourceLocation(), false);
2108}
2109
2110MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2111 SourceLocation L, DeclarationName N,
2112 QualType T, TypeSourceInfo *TInfo,
2113 SourceLocation StartL,
2114 IdentifierInfo *Getter,
2115 IdentifierInfo *Setter) {
2116 return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter);
2117}
2118
2119MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
2120 unsigned ID) {
2121 return new (C, ID) MSPropertyDecl(0, SourceLocation(), DeclarationName(),
2122 QualType(), 0, SourceLocation(), 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00002123}
2124
Anders Carlsson6750d162009-03-26 23:46:50 +00002125static const char *getAccessName(AccessSpecifier AS) {
2126 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002127 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002128 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002129 case AS_public:
2130 return "public";
2131 case AS_private:
2132 return "private";
2133 case AS_protected:
2134 return "protected";
2135 }
David Blaikief47fa302012-01-17 02:30:50 +00002136 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002137}
2138
2139const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2140 AccessSpecifier AS) {
2141 return DB << getAccessName(AS);
2142}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002143
2144const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2145 AccessSpecifier AS) {
2146 return DB << getAccessName(AS);
2147}