blob: 4c5028cc6f7c44618dfe1d77ac909e8e98cd2269 [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) {
34 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl));
35 return new (Mem) AccessSpecDecl(EmptyShell());
36}
37
Richard Smitha4ba74c2013-08-30 04:46:40 +000038void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const {
39 ExternalASTSource *Source = C.getExternalSource();
40 assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set");
41 assert(Source && "getFromExternalSource with no external source");
42
43 for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I)
44 I.setDecl(cast<NamedDecl>(Source->GetExternalDecl(
45 reinterpret_cast<uintptr_t>(I.getDecl()) >> 2)));
46 Impl.Decls.setLazy(false);
47}
48
John McCall67da35c2010-02-04 22:26:26 +000049CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
Richard Smith328aae52012-11-30 05:11:39 +000050 : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0),
Eli Friedmanc80ca682009-08-15 22:23:00 +000051 Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false),
Chandler Carruth583edf82011-04-30 10:07:30 +000052 Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
Chandler Carruthb1963742011-04-30 09:17:45 +000053 HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
Argyrios Kyrtzidisb2e31e42012-01-26 18:28:08 +000054 HasMutableFields(false), HasOnlyCMembers(true),
Richard Smith593f9932012-12-08 02:01:17 +000055 HasInClassInitializer(false), HasUninitializedReferenceMember(false),
Richard Smith6b02d462012-12-08 08:32:28 +000056 NeedOverloadResolutionForMoveConstructor(false),
57 NeedOverloadResolutionForMoveAssignment(false),
58 NeedOverloadResolutionForDestructor(false),
59 DefaultedMoveConstructorIsDeleted(false),
60 DefaultedMoveAssignmentIsDeleted(false),
61 DefaultedDestructorIsDeleted(false),
Richard Smith328aae52012-11-30 05:11:39 +000062 HasTrivialSpecialMembers(SMF_All),
Richard Smith92f241f2012-12-08 02:53:02 +000063 DeclaredNonTrivialSpecialMembers(0),
Richard Smith328aae52012-11-30 05:11:39 +000064 HasIrrelevantDestructor(true),
Richard Smithcc36f692011-12-22 02:22:31 +000065 HasConstexprNonCopyMoveConstructor(false),
66 DefaultedDefaultConstructorIsConstexpr(true),
Richard Smith328aae52012-11-30 05:11:39 +000067 HasConstexprDefaultConstructor(false),
Alexis Huntf479f1b2011-05-09 18:22:59 +000068 HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false),
Richard Smith328aae52012-11-30 05:11:39 +000069 UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0),
Richard Smith1c33fe82012-11-28 06:23:12 +000070 ImplicitCopyConstructorHasConstParam(true),
71 ImplicitCopyAssignmentHasConstParam(true),
72 HasDeclaredCopyConstructorWithConstParam(false),
73 HasDeclaredCopyAssignmentWithConstParam(false),
74 FailedImplicitMoveConstructor(false), FailedImplicitMoveAssignment(false),
75 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith68ad0e72013-06-26 02:41:25 +000076 Definition(D), FirstFriend() {
John McCall67da35c2010-02-04 22:26:26 +000077}
78
Benjamin Kramer300c0632012-07-04 17:03:33 +000079CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
80 return Bases.get(Definition->getASTContext().getExternalSource());
81}
82
83CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
84 return VBases.get(Definition->getASTContext().getExternalSource());
85}
86
John McCall67da35c2010-02-04 22:26:26 +000087CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000088 SourceLocation StartLoc, SourceLocation IdLoc,
89 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
90 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall67da35c2010-02-04 22:26:26 +000091 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000092 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000093
Jay Foad39c79802011-01-12 09:06:06 +000094CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000095 DeclContext *DC, SourceLocation StartLoc,
96 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000097 CXXRecordDecl* PrevDecl,
98 bool DelayTypeCreation) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +000099 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
100 Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000101 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000102
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000103 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000104 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000105 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000106 return R;
107}
108
Douglas Gregorc8a73492012-02-13 15:44:47 +0000109CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedmand564afb2012-09-19 01:18:11 +0000110 TypeSourceInfo *Info, SourceLocation Loc,
111 bool Dependent) {
Douglas Gregorc8a73492012-02-13 15:44:47 +0000112 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TTK_Class, DC, Loc, Loc,
113 0, 0);
114 R->IsBeingDefined = true;
Eli Friedmand564afb2012-09-19 01:18:11 +0000115 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info, Dependent);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000116 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000117 R->setImplicit(true);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000118 C.getTypeDeclType(R, /*PrevDecl=*/0);
119 return R;
120}
121
Douglas Gregor72172e92012-01-05 21:55:30 +0000122CXXRecordDecl *
123CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
124 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000125 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
126 SourceLocation(), SourceLocation(),
127 0, 0);
128 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.
Mike Stump11289f42009-09-09 15:08:12 +0000205 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000206 BaseClassDecl->vbases_begin(),
207 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000208 // Add this base if it's not already in the list.
Richard Smith1c33fe82012-11-28 06:23:12 +0000209 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000210 VBases.push_back(VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000211
212 // C++11 [class.copy]p8:
213 // The implicitly-declared copy constructor for a class X will have
214 // the form 'X::X(const X&)' if each [...] virtual base class B of X
215 // has a copy constructor whose first parameter is of type
216 // 'const B&' or 'const volatile B&' [...]
217 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
218 if (!VBaseDecl->hasCopyConstructorWithConstParam())
219 data().ImplicitCopyConstructorHasConstParam = false;
220 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000221 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000222
223 if (Base->isVirtual()) {
224 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000225 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smith1c33fe82012-11-28 06:23:12 +0000226 VBases.push_back(Base);
227
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000228 // C++0x [meta.unary.prop] is_empty:
229 // T is a class type, but not a union type, with ... no virtual base
230 // classes
231 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000232
Richard Smith328aae52012-11-30 05:11:39 +0000233 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
234 // A [default constructor, copy/move constructor, or copy/move assignment
235 // operator for a class X] is trivial [...] if:
236 // -- class X has [...] no virtual base classes
237 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000238
239 // C++0x [class]p7:
240 // A standard-layout class is a class that: [...]
241 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000242 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000243
244 // C++11 [dcl.constexpr]p4:
245 // In the definition of a constexpr constructor [...]
246 // -- the class shall not have any virtual base classes
247 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000248 } else {
249 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000250 // A default constructor is trivial [...] if:
251 // -- all the direct base classes of its class have trivial default
252 // constructors.
253 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000254 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
255
Chandler Carruthad7d4042011-04-23 23:10:33 +0000256 // C++0x [class.copy]p13:
257 // A copy/move constructor for class X is trivial if [...]
258 // [...]
259 // -- the constructor selected to copy/move each direct base class
260 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000261 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000262 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000263 // If the base class doesn't have a simple move constructor, we'll eagerly
264 // declare it and perform overload resolution to determine which function
265 // it actually calls. If it does have a simple move constructor, this
266 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000267 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000268 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000269
270 // C++0x [class.copy]p27:
271 // A copy/move assignment operator for class X is trivial if [...]
272 // [...]
273 // -- the assignment operator selected to copy/move each direct base
274 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000275 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000276 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000277 // If the base class doesn't have a simple move assignment, we'll eagerly
278 // declare it and perform overload resolution to determine which function
279 // it actually calls. If it does have a simple move assignment, this
280 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000281 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000282 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000283
284 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000285 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000286 // requirements of a constexpr constructor, the implicitly-defined
287 // default constructor is constexpr.
288 if (!BaseClassDecl->hasConstexprDefaultConstructor())
289 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000290 }
Richard Smith92f241f2012-12-08 02:53:02 +0000291
Douglas Gregor11c024b2010-09-28 20:50:54 +0000292 // C++ [class.ctor]p3:
293 // A destructor is trivial if all the direct base classes of its class
294 // have trivial destructors.
295 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000296 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000297
298 if (!BaseClassDecl->hasIrrelevantDestructor())
299 data().HasIrrelevantDestructor = false;
300
Richard Smith1c33fe82012-11-28 06:23:12 +0000301 // C++11 [class.copy]p18:
302 // The implicitly-declared copy assignment oeprator for a class X will
303 // have the form 'X& X::operator=(const X&)' if each direct base class B
304 // of X has a copy assignment operator whose parameter is of type 'const
305 // B&', 'const volatile B&', or 'B' [...]
306 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
307 data().ImplicitCopyAssignmentHasConstParam = false;
308
309 // C++11 [class.copy]p8:
310 // The implicitly-declared copy constructor for a class X will have
311 // the form 'X::X(const X&)' if each direct [...] base class B of X
312 // has a copy constructor whose first parameter is of type
313 // 'const B&' or 'const volatile B&' [...]
314 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
315 data().ImplicitCopyConstructorHasConstParam = false;
316
John McCall31168b02011-06-15 23:02:42 +0000317 // A class has an Objective-C object member if... or any of its bases
318 // has an Objective-C object member.
319 if (BaseClassDecl->hasObjectMember())
320 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000321
322 if (BaseClassDecl->hasVolatileMember())
323 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000324
Douglas Gregor61226d32011-05-13 01:05:07 +0000325 // Keep track of the presence of mutable fields.
326 if (BaseClassDecl->hasMutableFields())
327 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000328
329 if (BaseClassDecl->hasUninitializedReferenceMember())
330 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000331
332 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000333 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000334
335 if (VBases.empty())
336 return;
337
338 // Create base specifier for any direct or indirect virtual bases.
339 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
340 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000341 for (int I = 0, E = VBases.size(); I != E; ++I) {
342 QualType Type = VBases[I]->getType();
343 if (!Type->isDependentType())
344 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000345 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000346 }
347}
348
349void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
350 // C++11 [class.copy]p11:
351 // A defaulted copy/move constructor for a class X is defined as
352 // deleted if X has:
353 // -- a direct or virtual base class B that cannot be copied/moved [...]
354 // -- a non-static data member of class type M (or array thereof)
355 // that cannot be copied or moved [...]
356 if (!Subobj->hasSimpleMoveConstructor())
357 data().NeedOverloadResolutionForMoveConstructor = true;
358
359 // C++11 [class.copy]p23:
360 // A defaulted copy/move assignment operator for a class X is defined as
361 // deleted if X has:
362 // -- a direct or virtual base class B that cannot be copied/moved [...]
363 // -- a non-static data member of class type M (or array thereof)
364 // that cannot be copied or moved [...]
365 if (!Subobj->hasSimpleMoveAssignment())
366 data().NeedOverloadResolutionForMoveAssignment = true;
367
368 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
369 // A defaulted [ctor or dtor] for a class X is defined as
370 // deleted if X has:
371 // -- any direct or virtual base class [...] has a type with a destructor
372 // that is deleted or inaccessible from the defaulted [ctor or dtor].
373 // -- any non-static data member has a type with a destructor
374 // that is deleted or inaccessible from the defaulted [ctor or dtor].
375 if (!Subobj->hasSimpleDestructor()) {
376 data().NeedOverloadResolutionForMoveConstructor = true;
377 data().NeedOverloadResolutionForDestructor = true;
378 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000379}
380
Douglas Gregord2e6a452010-01-14 17:47:39 +0000381/// Callback function for CXXRecordDecl::forallBases that acknowledges
382/// that it saw a base class.
383static bool SawBase(const CXXRecordDecl *, void *) {
384 return true;
385}
386
387bool CXXRecordDecl::hasAnyDependentBases() const {
388 if (!isDependentContext())
389 return false;
390
391 return !forallBases(SawBase, 0);
392}
393
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000394bool CXXRecordDecl::isTriviallyCopyable() const {
395 // C++0x [class]p5:
396 // A trivially copyable class is a class that:
397 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000398 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000399 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000400 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000401 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000402 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000403 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000404 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000405 // -- has a trivial destructor.
406 if (!hasTrivialDestructor()) return false;
407
408 return true;
409}
410
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000411void CXXRecordDecl::markedVirtualFunctionPure() {
412 // C++ [class.abstract]p2:
413 // A class is abstract if it has at least one pure virtual function.
414 data().Abstract = true;
415}
416
417void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000418 if (!D->isImplicit() &&
419 !isa<FieldDecl>(D) &&
420 !isa<IndirectFieldDecl>(D) &&
421 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
422 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
423 data().HasOnlyCMembers = false;
424
425 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000426 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000427 return;
428
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000429 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
430 if (FunTmpl)
431 D = FunTmpl->getTemplatedDecl();
432
Douglas Gregora832d3e2010-09-28 19:45:33 +0000433 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
434 if (Method->isVirtual()) {
435 // C++ [dcl.init.aggr]p1:
436 // An aggregate is an array or a class with [...] no virtual functions.
437 data().Aggregate = false;
438
439 // C++ [class]p4:
440 // A POD-struct is an aggregate class...
441 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000442
443 // Virtual functions make the class non-empty.
444 // FIXME: Standard ref?
445 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000446
447 // C++ [class.virtual]p1:
448 // A class that declares or inherits a virtual function is called a
449 // polymorphic class.
450 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000451
Richard Smith328aae52012-11-30 05:11:39 +0000452 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
453 // A [default constructor, copy/move constructor, or copy/move
454 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000455 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000456 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000457
Chandler Carruthb1963742011-04-30 09:17:45 +0000458 // C++0x [class]p7:
459 // A standard-layout class is a class that: [...]
460 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000461 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000462 }
463 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000464
Richard Smith1c33fe82012-11-28 06:23:12 +0000465 // Notify the listener if an implicit member was added after the definition
466 // was completed.
467 if (!isBeingDefined() && D->isImplicit())
468 if (ASTMutationListener *L = getASTMutationListener())
469 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000470
Richard Smith328aae52012-11-30 05:11:39 +0000471 // The kind of special member this declaration is, if any.
472 unsigned SMKind = 0;
473
Richard Smith1c33fe82012-11-28 06:23:12 +0000474 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000475 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000476 if (!Constructor->isImplicit()) {
477 // Note that we have a user-declared constructor.
478 data().UserDeclaredConstructor = true;
479
480 // C++ [class]p4:
481 // A POD-struct is an aggregate class [...]
482 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
483 // type is technically an aggregate in C++0x since it wouldn't be in 03.
484 data().PlainOldData = false;
485 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000486
Richard Smitha5285072011-09-05 02:13:09 +0000487 // Technically, "user-provided" is only defined for special member
488 // functions, but the intent of the standard is clearly that it should apply
489 // to all functions.
490 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000491
Alexis Hunt88c75c32011-05-09 21:45:35 +0000492 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000493 SMKind |= SMF_DefaultConstructor;
494
495 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000496 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000497 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000498 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000499 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000500
Chandler Carruthad7d4042011-04-23 23:10:33 +0000501 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000502 unsigned Quals;
503 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000504 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000505
506 if (Quals & Qualifiers::Const)
507 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000508 } else if (Constructor->isMoveConstructor())
509 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000510 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000511
512 // Record if we see any constexpr constructors which are neither copy
513 // nor move constructors.
514 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000515 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000516
Alexis Hunt88c75c32011-05-09 21:45:35 +0000517 // C++ [dcl.init.aggr]p1:
518 // An aggregate is an array or a class with no user-declared
519 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000520 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000521 // An aggregate is an array or a class with no user-provided
522 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000523 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000524 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000525 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000526 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000527
Richard Smith1c33fe82012-11-28 06:23:12 +0000528 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000529 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000530 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000531
Richard Smith328aae52012-11-30 05:11:39 +0000532 if (!DD->isImplicit())
Richard Smith1c33fe82012-11-28 06:23:12 +0000533 data().HasIrrelevantDestructor = false;
534
Richard Smith1c33fe82012-11-28 06:23:12 +0000535 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000536 // A destructor is trivial if [...] the destructor is not virtual.
537 if (DD->isVirtual())
538 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000539 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000540
541 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000542 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000543 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000544 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000545
546 const ReferenceType *ParamTy =
547 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
548 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
549 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000550 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000551
Richard Smith328aae52012-11-30 05:11:39 +0000552 if (Method->isMoveAssignmentOperator())
553 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000554
Douglas Gregor457104e2010-09-29 04:25:11 +0000555 // Keep the list of conversion functions up-to-date.
556 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000557 // FIXME: We use the 'unsafe' accessor for the access specifier here,
558 // because Sema may not have set it yet. That's really just a misdesign
559 // in Sema. However, LLDB *will* have set the access specifier correctly,
560 // and adds declarations after the class is technically completed,
561 // so completeDefinition()'s overriding of the access specifiers doesn't
562 // work.
563 AccessSpecifier AS = Conversion->getAccessUnsafe();
564
Richard Smith328aae52012-11-30 05:11:39 +0000565 if (Conversion->getPrimaryTemplate()) {
566 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000567 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000568 ASTContext &Ctx = getASTContext();
569 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
570 NamedDecl *Primary =
571 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
572 if (Primary->getPreviousDecl())
573 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
574 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000575 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000576 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000577 }
578 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000579
Richard Smith328aae52012-11-30 05:11:39 +0000580 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000581 // If this is the first declaration of a special member, we no longer have
582 // an implicit trivial special member.
583 data().HasTrivialSpecialMembers &=
584 data().DeclaredSpecialMembers | ~SMKind;
585
586 if (!Method->isImplicit() && !Method->isUserProvided()) {
587 // This method is user-declared but not user-provided. We can't work out
588 // whether it's trivial yet (not until we get to the end of the class).
589 // We'll handle this method in finishedDefaultedOrDeletedMember.
590 } else if (Method->isTrivial())
591 data().HasTrivialSpecialMembers |= SMKind;
592 else
593 data().DeclaredNonTrivialSpecialMembers |= SMKind;
594
Richard Smith328aae52012-11-30 05:11:39 +0000595 // Note when we have declared a declared special member, and suppress the
596 // implicit declaration of this special member.
597 data().DeclaredSpecialMembers |= SMKind;
598
599 if (!Method->isImplicit()) {
600 data().UserDeclaredSpecialMembers |= SMKind;
601
602 // C++03 [class]p4:
603 // A POD-struct is an aggregate class that has [...] no user-defined
604 // copy assignment operator and no user-defined destructor.
605 //
606 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
607 // aggregates could not have any constructors, clear it even for an
608 // explicitly defaulted or deleted constructor.
609 // type is technically an aggregate in C++0x since it wouldn't be in 03.
610 //
611 // Also, a user-declared move assignment operator makes a class non-POD.
612 // This is an extension in C++03.
613 data().PlainOldData = false;
614 }
Richard Smith328aae52012-11-30 05:11:39 +0000615 }
616
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000617 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000618 }
Richard Smith328aae52012-11-30 05:11:39 +0000619
Douglas Gregora832d3e2010-09-28 19:45:33 +0000620 // Handle non-static data members.
621 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000622 // C++ [class.bit]p2:
623 // A declaration for a bit-field that omits the identifier declares an
624 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
625 // initialized.
626 if (Field->isUnnamedBitfield())
627 return;
628
Douglas Gregora832d3e2010-09-28 19:45:33 +0000629 // C++ [dcl.init.aggr]p1:
630 // An aggregate is an array or a class (clause 9) with [...] no
631 // private or protected non-static data members (clause 11).
632 //
633 // A POD must be an aggregate.
634 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
635 data().Aggregate = false;
636 data().PlainOldData = false;
637 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000638
639 // C++0x [class]p7:
640 // A standard-layout class is a class that:
641 // [...]
642 // -- has the same access control for all non-static data members,
643 switch (D->getAccess()) {
644 case AS_private: data().HasPrivateFields = true; break;
645 case AS_protected: data().HasProtectedFields = true; break;
646 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000647 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000648 };
649 if ((data().HasPrivateFields + data().HasProtectedFields +
650 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000651 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000652
Douglas Gregor61226d32011-05-13 01:05:07 +0000653 // Keep track of the presence of mutable fields.
654 if (Field->isMutable())
655 data().HasMutableFields = true;
656
Chandler Carruthad7d4042011-04-23 23:10:33 +0000657 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000658 // A POD struct is a class that is both a trivial class and a
659 // standard-layout class, and has no non-static data members of type
660 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000661 //
662 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
663 // that does not explicitly have no lifetime makes the class a non-POD.
664 // However, we delay setting PlainOldData to false in this case so that
665 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor6fa69422012-07-23 04:23:39 +0000666 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCall31168b02011-06-15 23:02:42 +0000667 // In this case, the class will become a non-POD class when we complete
668 // the definition.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000669 ASTContext &Context = getASTContext();
670 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000671 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000672 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCall31168b02011-06-15 23:02:42 +0000673 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
674 setHasObjectMember(true);
Eli Friedmana5433322013-07-20 01:06:31 +0000675 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000676 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000677
Chandler Carruthb1963742011-04-30 09:17:45 +0000678 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000679 if (!Field->hasInClassInitializer())
680 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000681
Chandler Carruthb1963742011-04-30 09:17:45 +0000682 // C++0x [class]p7:
683 // A standard-layout class is a class that:
684 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000685 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000686 }
687
Richard Smith3607ffe2012-02-13 03:54:03 +0000688 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000689 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000690 data().HasNonLiteralTypeFieldsOrBases = true;
691
Richard Smith938f40b2011-06-11 17:19:42 +0000692 if (Field->hasInClassInitializer()) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000693 data().HasInClassInitializer = true;
694
695 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000696 // A default constructor is trivial if [...] no non-static data member
697 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000698 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000699
Richard Smithe2648ba2012-05-07 01:07:30 +0000700 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000701 // An aggregate is a [...] class with [...] no
702 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000703 //
704 // This rule was removed in C++1y.
705 if (!getASTContext().getLangOpts().CPlusPlus1y)
706 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000707
Richard Smithe2648ba2012-05-07 01:07:30 +0000708 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000709 // A POD struct is [...] a trivial class.
710 data().PlainOldData = false;
711 }
712
Richard Smith6b02d462012-12-08 08:32:28 +0000713 // C++11 [class.copy]p23:
714 // A defaulted copy/move assignment operator for a class X is defined
715 // as deleted if X has:
716 // -- a non-static data member of reference type
717 if (T->isReferenceType())
718 data().DefaultedMoveAssignmentIsDeleted = true;
719
Douglas Gregor11c024b2010-09-28 20:50:54 +0000720 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
721 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
722 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000723 addedClassSubobject(FieldRec);
724
725 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
726 // A defaulted [special member] for a class X is defined as
727 // deleted if:
728 // -- X is a union-like class that has a variant member with a
729 // non-trivial [corresponding special member]
730 if (isUnion()) {
731 if (FieldRec->hasNonTrivialMoveConstructor())
732 data().DefaultedMoveConstructorIsDeleted = true;
733 if (FieldRec->hasNonTrivialMoveAssignment())
734 data().DefaultedMoveAssignmentIsDeleted = true;
735 if (FieldRec->hasNonTrivialDestructor())
736 data().DefaultedDestructorIsDeleted = true;
737 }
738
Alexis Huntf479f1b2011-05-09 18:22:59 +0000739 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000740 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000741 // -- for all the non-static data members of its class that are of
742 // class type (or array thereof), each such class has a trivial
743 // default constructor.
744 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000745 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000746
747 // C++0x [class.copy]p13:
748 // A copy/move constructor for class X is trivial if [...]
749 // [...]
750 // -- for each non-static data member of X that is of class type (or
751 // an array thereof), the constructor selected to copy/move that
752 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000753 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000754 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000755 // If the field doesn't have a simple move constructor, we'll eagerly
756 // declare the move constructor for this class and we'll decide whether
757 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000758 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000759 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000760
761 // C++0x [class.copy]p27:
762 // A copy/move assignment operator for class X is trivial if [...]
763 // [...]
764 // -- for each non-static data member of X that is of class type (or
765 // an array thereof), the assignment operator selected to
766 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000767 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000768 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000769 // If the field doesn't have a simple move assignment, we'll eagerly
770 // declare the move assignment for this class and we'll decide whether
771 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000772 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000773 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000774
Douglas Gregor11c024b2010-09-28 20:50:54 +0000775 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000776 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000777 if (!FieldRec->hasIrrelevantDestructor())
778 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000779 if (FieldRec->hasObjectMember())
780 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000781 if (FieldRec->hasVolatileMember())
782 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000783
784 // C++0x [class]p7:
785 // A standard-layout class is a class that:
786 // -- has no non-static data members of type non-standard-layout
787 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000788 if (!FieldRec->isStandardLayout())
789 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000790
791 // C++0x [class]p7:
792 // A standard-layout class is a class that:
793 // [...]
794 // -- has no base classes of the same type as the first non-static
795 // data member.
796 // We don't want to expend bits in the state of the record decl
797 // tracking whether this is the first non-static data member so we
798 // cheat a bit and use some of the existing state: the empty bit.
799 // Virtual bases and virtual methods make a class non-empty, but they
800 // also make it non-standard-layout so we needn't check here.
801 // A non-empty base class may leave the class standard-layout, but not
802 // if we have arrived here, and have at least on non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000803 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000804 // data member must come through here with Empty still true, and Empty
805 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000806 if (data().IsStandardLayout && data().Empty) {
Chandler Carruthb1963742011-04-30 09:17:45 +0000807 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
808 BE = bases_end();
809 BI != BE; ++BI) {
810 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000811 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000812 break;
813 }
814 }
815 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000816
817 // Keep track of the presence of mutable fields.
818 if (FieldRec->hasMutableFields())
819 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000820
821 // C++11 [class.copy]p13:
822 // If the implicitly-defined constructor would satisfy the
823 // requirements of a constexpr constructor, the implicitly-defined
824 // constructor is constexpr.
825 // C++11 [dcl.constexpr]p4:
826 // -- every constructor involved in initializing non-static data
827 // members [...] shall be a constexpr constructor
828 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000829 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000830 // The standard requires any in-class initializer to be a constant
831 // expression. We consider this to be a defect.
832 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000833
834 // C++11 [class.copy]p8:
835 // The implicitly-declared copy constructor for a class X will have
836 // the form 'X::X(const X&)' if [...] for all the non-static data
837 // members of X that are of a class type M (or array thereof), each
838 // such class type has a copy constructor whose first parameter is
839 // of type 'const M&' or 'const volatile M&'.
840 if (!FieldRec->hasCopyConstructorWithConstParam())
841 data().ImplicitCopyConstructorHasConstParam = false;
842
843 // C++11 [class.copy]p18:
844 // The implicitly-declared copy assignment oeprator for a class X will
845 // have the form 'X& X::operator=(const X&)' if [...] for all the
846 // non-static data members of X that are of a class type M (or array
847 // thereof), each such class type has a copy assignment operator whose
848 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
849 if (!FieldRec->hasCopyAssignmentWithConstParam())
850 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000851
852 if (FieldRec->hasUninitializedReferenceMember() &&
853 !Field->hasInClassInitializer())
854 data().HasUninitializedReferenceMember = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000855 }
Richard Smithcc36f692011-12-22 02:22:31 +0000856 } else {
857 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000858 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000859 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000860 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000861
862 // C++11 [class.copy]p23:
863 // A defaulted copy/move assignment operator for a class X is defined
864 // as deleted if X has:
865 // -- a non-static data member of const non-class type (or array
866 // thereof)
867 if (T.isConstQualified())
868 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000869 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000870
871 // C++0x [class]p7:
872 // A standard-layout class is a class that:
873 // [...]
874 // -- either has no non-static data members in the most derived
875 // class and at most one base class with non-static data members,
876 // or has no base classes with non-static data members, and
877 // At this point we know that we have a non-static data member, so the last
878 // clause holds.
879 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000880 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000881
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000882 // If this is not a zero-length bit-field, then the class is not empty.
883 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000884 if (!Field->isBitField() ||
885 (!Field->getBitWidth()->isTypeDependent() &&
886 !Field->getBitWidth()->isValueDependent() &&
887 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000888 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000889 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000890 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000891
892 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000893 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000894 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000895 == DeclarationName::CXXConversionFunctionName) {
896 ASTContext &Ctx = getASTContext();
897 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
898 }
899 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000900}
901
Richard Smith92f241f2012-12-08 02:53:02 +0000902void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
903 assert(!D->isImplicit() && !D->isUserProvided());
904
905 // The kind of special member this declaration is, if any.
906 unsigned SMKind = 0;
907
908 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
909 if (Constructor->isDefaultConstructor()) {
910 SMKind |= SMF_DefaultConstructor;
911 if (Constructor->isConstexpr())
912 data().HasConstexprDefaultConstructor = true;
913 }
914 if (Constructor->isCopyConstructor())
915 SMKind |= SMF_CopyConstructor;
916 else if (Constructor->isMoveConstructor())
917 SMKind |= SMF_MoveConstructor;
918 else if (Constructor->isConstexpr())
919 // We may now know that the constructor is constexpr.
920 data().HasConstexprNonCopyMoveConstructor = true;
921 } else if (isa<CXXDestructorDecl>(D))
922 SMKind |= SMF_Destructor;
923 else if (D->isCopyAssignmentOperator())
924 SMKind |= SMF_CopyAssignment;
925 else if (D->isMoveAssignmentOperator())
926 SMKind |= SMF_MoveAssignment;
927
928 // Update which trivial / non-trivial special members we have.
929 // addedMember will have skipped this step for this member.
930 if (D->isTrivial())
931 data().HasTrivialSpecialMembers |= SMKind;
932 else
933 data().DeclaredNonTrivialSpecialMembers |= SMKind;
934}
935
Joao Matose9a3ed42012-08-31 22:18:20 +0000936bool CXXRecordDecl::isCLike() const {
937 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
938 !TemplateOrInstantiation.isNull())
939 return false;
940 if (!hasDefinition())
941 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000942
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000943 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000944}
945
Faisal Vali2b391ab2013-09-26 19:54:12 +0000946bool CXXRecordDecl::isGenericLambda() const {
947 return isLambda() &&
948 getLambdaCallOperator()->getDescribedFunctionTemplate();
949}
950
951CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
952 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000953 DeclarationName Name =
954 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000955 DeclContext::lookup_const_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000956
957 assert(!Calls.empty() && "Missing lambda call operator!");
958 assert(Calls.size() == 1 && "More than one lambda call operator!");
959
960 NamedDecl *CallOp = Calls.front();
961 if (FunctionTemplateDecl *CallOpTmpl =
962 dyn_cast<FunctionTemplateDecl>(CallOp))
963 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000964
965 return cast<CXXMethodDecl>(CallOp);
966}
967
968CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
969 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000970 DeclarationName Name =
971 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000972 DeclContext::lookup_const_result Invoker = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000973 if (Invoker.empty()) return 0;
974 assert(Invoker.size() == 1 && "More than one static invoker operator!");
975 NamedDecl *InvokerFun = Invoker.front();
976 if (FunctionTemplateDecl *InvokerTemplate =
977 dyn_cast<FunctionTemplateDecl>(InvokerFun))
978 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
979
Faisal Vali571df122013-09-29 08:45:24 +0000980 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000981}
982
Douglas Gregor9c702202012-02-10 07:45:31 +0000983void CXXRecordDecl::getCaptureFields(
984 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +0000985 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +0000986 Captures.clear();
987 ThisCapture = 0;
988
Douglas Gregorc8a73492012-02-13 15:44:47 +0000989 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +0000990 RecordDecl::field_iterator Field = field_begin();
Douglas Gregore5561632012-02-13 17:20:40 +0000991 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +0000992 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +0000993 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +0000994 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +0000995 else if (C->capturesVariable())
996 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +0000997 }
Richard Smithbb13c9a2013-09-28 04:02:39 +0000998 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +0000999}
1000
Faisal Vali2b391ab2013-09-26 19:54:12 +00001001TemplateParameterList *
1002CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1003 if (!isLambda()) return 0;
1004 CXXMethodDecl *CallOp = getLambdaCallOperator();
1005 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1006 return Tmpl->getTemplateParameters();
1007 return 0;
1008}
Douglas Gregor9c702202012-02-10 07:45:31 +00001009
John McCall1e3a1a72010-03-15 09:07:48 +00001010static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1011 QualType T;
John McCallda4458e2010-03-31 01:36:47 +00001012 if (isa<UsingShadowDecl>(Conv))
1013 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +00001014 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1015 T = ConvTemp->getTemplatedDecl()->getResultType();
1016 else
1017 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1018 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001019}
1020
John McCall1e3a1a72010-03-15 09:07:48 +00001021/// Collect the visible conversions of a base class.
1022///
James Dennettb5d5f812012-06-15 22:28:09 +00001023/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001024/// \param InVirtual whether this base class is a virtual base (or a base
1025/// of a virtual base)
1026/// \param Access the access along the inheritance path to this base
1027/// \param ParentHiddenTypes the conversions provided by the inheritors
1028/// of this base
1029/// \param Output the set to which to add conversions from non-virtual bases
1030/// \param VOutput the set to which to add conversions from virtual bases
1031/// \param HiddenVBaseCs the set of conversions which were hidden in a
1032/// virtual base along some inheritance path
1033static void CollectVisibleConversions(ASTContext &Context,
1034 CXXRecordDecl *Record,
1035 bool InVirtual,
1036 AccessSpecifier Access,
1037 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001038 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001039 UnresolvedSetImpl &VOutput,
1040 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1041 // The set of types which have conversions in this class or its
1042 // subclasses. As an optimization, we don't copy the derived set
1043 // unless it might change.
1044 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1045 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1046
1047 // Collect the direct conversions and figure out which conversions
1048 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001049 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1050 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1051 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001052 HiddenTypesBuffer = ParentHiddenTypes;
1053 HiddenTypes = &HiddenTypesBuffer;
1054
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001055 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001056 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1057 bool Hidden = ParentHiddenTypes.count(ConvType);
1058 if (!Hidden)
1059 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001060
1061 // If this conversion is hidden and we're in a virtual base,
1062 // remember that it's hidden along some inheritance path.
1063 if (Hidden && InVirtual)
1064 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1065
1066 // If this conversion isn't hidden, add it to the appropriate output.
1067 else if (!Hidden) {
1068 AccessSpecifier IAccess
1069 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1070
1071 if (InVirtual)
1072 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001073 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001074 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001075 }
1076 }
1077 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001078
John McCall1e3a1a72010-03-15 09:07:48 +00001079 // Collect information recursively from any base classes.
1080 for (CXXRecordDecl::base_class_iterator
1081 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1082 const RecordType *RT = I->getType()->getAs<RecordType>();
1083 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001084
John McCall1e3a1a72010-03-15 09:07:48 +00001085 AccessSpecifier BaseAccess
1086 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1087 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001088
John McCall1e3a1a72010-03-15 09:07:48 +00001089 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1090 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1091 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001092 }
John McCall1e3a1a72010-03-15 09:07:48 +00001093}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001094
John McCall1e3a1a72010-03-15 09:07:48 +00001095/// Collect the visible conversions of a class.
1096///
1097/// This would be extremely straightforward if it weren't for virtual
1098/// bases. It might be worth special-casing that, really.
1099static void CollectVisibleConversions(ASTContext &Context,
1100 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001101 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001102 // The collection of all conversions in virtual bases that we've
1103 // found. These will be added to the output as long as they don't
1104 // appear in the hidden-conversions set.
1105 UnresolvedSet<8> VBaseCs;
1106
1107 // The set of conversions in virtual bases that we've determined to
1108 // be hidden.
1109 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1110
1111 // The set of types hidden by classes derived from this one.
1112 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1113
1114 // Go ahead and collect the direct conversions and add them to the
1115 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001116 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1117 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001118 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001119 for (; ConvI != ConvE; ++ConvI)
1120 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001121
1122 // Recursively collect conversions from base classes.
1123 for (CXXRecordDecl::base_class_iterator
1124 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1125 const RecordType *RT = I->getType()->getAs<RecordType>();
1126 if (!RT) continue;
1127
1128 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1129 I->isVirtual(), I->getAccessSpecifier(),
1130 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1131 }
1132
1133 // Add any unhidden conversions provided by virtual bases.
1134 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1135 I != E; ++I) {
1136 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001137 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001138 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001139}
1140
1141/// getVisibleConversionFunctions - get all conversion functions visible
1142/// in current class; including conversion function templates.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001143std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1144CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001145 ASTContext &Ctx = getASTContext();
1146
1147 ASTUnresolvedSet *Set;
1148 if (bases_begin() == bases_end()) {
1149 // If root class, all conversions are visible.
1150 Set = &data().Conversions.get(Ctx);
1151 } else {
1152 Set = &data().VisibleConversions.get(Ctx);
1153 // If visible conversion list is not evaluated, evaluate it.
1154 if (!data().ComputedVisibleConversions) {
1155 CollectVisibleConversions(Ctx, this, *Set);
1156 data().ComputedVisibleConversions = true;
1157 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001158 }
Richard Smitha4ba74c2013-08-30 04:46:40 +00001159 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001160}
1161
John McCallda4458e2010-03-31 01:36:47 +00001162void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1163 // This operation is O(N) but extremely rare. Sema only uses it to
1164 // remove UsingShadowDecls in a class that were followed by a direct
1165 // declaration, e.g.:
1166 // class A : B {
1167 // using B::operator int;
1168 // operator int();
1169 // };
1170 // This is uncommon by itself and even more uncommon in conjunction
1171 // with sufficiently large numbers of directly-declared conversions
1172 // that asymptotic behavior matters.
1173
Richard Smitha4ba74c2013-08-30 04:46:40 +00001174 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001175 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1176 if (Convs[I].getDecl() == ConvDecl) {
1177 Convs.erase(I);
1178 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1179 && "conversion was found multiple times in unresolved set");
1180 return;
1181 }
1182 }
1183
1184 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001185}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001186
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001187CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001188 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001189 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1190
1191 return 0;
1192}
1193
1194void
1195CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1196 TemplateSpecializationKind TSK) {
1197 assert(TemplateOrInstantiation.isNull() &&
1198 "Previous template or instantiation?");
1199 assert(!isa<ClassTemplateSpecializationDecl>(this));
1200 TemplateOrInstantiation
1201 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1202}
1203
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001204TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1205 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001206 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1207 return Spec->getSpecializationKind();
1208
Douglas Gregor06db9f52009-10-12 20:18:28 +00001209 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001210 return MSInfo->getTemplateSpecializationKind();
1211
1212 return TSK_Undeclared;
1213}
1214
1215void
1216CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1217 if (ClassTemplateSpecializationDecl *Spec
1218 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1219 Spec->setSpecializationKind(TSK);
1220 return;
1221 }
1222
Douglas Gregor06db9f52009-10-12 20:18:28 +00001223 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001224 MSInfo->setTemplateSpecializationKind(TSK);
1225 return;
1226 }
1227
David Blaikie83d382b2011-09-23 05:06:16 +00001228 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001229}
1230
Douglas Gregorbac74902010-07-01 14:13:13 +00001231CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1232 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001233 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001234
1235 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001236 = Context.DeclarationNames.getCXXDestructorName(
1237 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001238
David Blaikieff7d47a2012-12-19 00:45:41 +00001239 DeclContext::lookup_const_result R = lookup(Name);
1240 if (R.empty())
Sebastian Redlb469afb2010-09-02 23:19:42 +00001241 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001242
David Blaikieff7d47a2012-12-19 00:45:41 +00001243 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001244 return Dtor;
1245}
1246
Douglas Gregorb11aad82011-02-19 18:51:44 +00001247void CXXRecordDecl::completeDefinition() {
1248 completeDefinition(0);
1249}
1250
1251void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1252 RecordDecl::completeDefinition();
1253
David Blaikiebbafb8a2012-03-11 07:00:24 +00001254 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +00001255 // Objective-C Automatic Reference Counting:
1256 // If a class has a non-static data member of Objective-C pointer
1257 // type (or array thereof), it is a non-POD type and its
Douglas Gregor6fa69422012-07-23 04:23:39 +00001258 // default constructor (if any), copy constructor, move constructor,
1259 // copy assignment operator, move assignment operator, and destructor are
1260 // non-trivial.
John McCall31168b02011-06-15 23:02:42 +00001261 struct DefinitionData &Data = data();
1262 Data.PlainOldData = false;
Richard Smith328aae52012-11-30 05:11:39 +00001263 Data.HasTrivialSpecialMembers = 0;
Richard Smith561fb152012-02-25 07:33:38 +00001264 Data.HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001265 }
1266
Douglas Gregor8fb95122010-09-29 00:15:42 +00001267 // If the class may be abstract (but hasn't been marked as such), check for
1268 // any pure final overriders.
1269 if (mayBeAbstract()) {
1270 CXXFinalOverriderMap MyFinalOverriders;
1271 if (!FinalOverriders) {
1272 getFinalOverriders(MyFinalOverriders);
1273 FinalOverriders = &MyFinalOverriders;
1274 }
1275
1276 bool Done = false;
1277 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1278 MEnd = FinalOverriders->end();
1279 M != MEnd && !Done; ++M) {
1280 for (OverridingMethods::iterator SO = M->second.begin(),
1281 SOEnd = M->second.end();
1282 SO != SOEnd && !Done; ++SO) {
1283 assert(SO->second.size() > 0 &&
1284 "All virtual functions have overridding virtual functions");
1285
1286 // C++ [class.abstract]p4:
1287 // A class is abstract if it contains or inherits at least one
1288 // pure virtual function for which the final overrider is pure
1289 // virtual.
1290 if (SO->second.front().Method->isPure()) {
1291 data().Abstract = true;
1292 Done = true;
1293 break;
1294 }
1295 }
1296 }
1297 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001298
1299 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001300 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001301 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001302 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001303}
1304
1305bool CXXRecordDecl::mayBeAbstract() const {
1306 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1307 isDependentContext())
1308 return false;
1309
1310 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1311 BEnd = bases_end();
1312 B != BEnd; ++B) {
1313 CXXRecordDecl *BaseDecl
1314 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1315 if (BaseDecl->isAbstract())
1316 return true;
1317 }
1318
1319 return false;
1320}
1321
David Blaikie68e081d2011-12-20 02:48:34 +00001322void CXXMethodDecl::anchor() { }
1323
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001324bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001325 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001326
1327 if (MD->getStorageClass() == SC_Static)
1328 return true;
1329
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001330 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1331 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001332}
1333
Rafael Espindola49e860b2012-06-26 17:45:31 +00001334static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1335 const CXXMethodDecl *BaseMD) {
1336 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1337 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1338 const CXXMethodDecl *MD = *I;
1339 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1340 return true;
1341 if (recursivelyOverrides(MD, BaseMD))
1342 return true;
1343 }
1344 return false;
1345}
1346
1347CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001348CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1349 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001350 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1351 return this;
1352
1353 // Lookup doesn't work for destructors, so handle them separately.
1354 if (isa<CXXDestructorDecl>(this)) {
1355 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001356 if (MD) {
1357 if (recursivelyOverrides(MD, this))
1358 return MD;
1359 if (MayBeBase && recursivelyOverrides(this, MD))
1360 return MD;
1361 }
Rafael Espindola49e860b2012-06-26 17:45:31 +00001362 return NULL;
1363 }
1364
1365 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001366 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001367 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1368 if (!MD)
1369 continue;
1370 if (recursivelyOverrides(MD, this))
1371 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001372 if (MayBeBase && recursivelyOverrides(this, MD))
1373 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001374 }
1375
1376 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1377 E = RD->bases_end(); I != E; ++I) {
1378 const RecordType *RT = I->getType()->getAs<RecordType>();
1379 if (!RT)
1380 continue;
1381 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1382 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1383 if (T)
1384 return T;
1385 }
1386
1387 return NULL;
1388}
1389
Ted Kremenek21475702008-09-05 17:16:31 +00001390CXXMethodDecl *
1391CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001392 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001393 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001394 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001395 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001396 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001397 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001398 SC, isInline, isConstexpr,
Richard Smitha77a0a62011-08-15 21:04:07 +00001399 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001400}
1401
Douglas Gregor72172e92012-01-05 21:55:30 +00001402CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1403 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1404 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1405 DeclarationNameInfo(), QualType(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001406 0, SC_None, false, false,
Douglas Gregor72172e92012-01-05 21:55:30 +00001407 SourceLocation());
1408}
1409
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001410bool CXXMethodDecl::isUsualDeallocationFunction() const {
1411 if (getOverloadedOperator() != OO_Delete &&
1412 getOverloadedOperator() != OO_Array_Delete)
1413 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001414
1415 // C++ [basic.stc.dynamic.deallocation]p2:
1416 // A template instance is never a usual deallocation function,
1417 // regardless of its signature.
1418 if (getPrimaryTemplate())
1419 return false;
1420
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001421 // C++ [basic.stc.dynamic.deallocation]p2:
1422 // If a class T has a member deallocation function named operator delete
1423 // with exactly one parameter, then that function is a usual (non-placement)
1424 // deallocation function. [...]
1425 if (getNumParams() == 1)
1426 return true;
1427
1428 // C++ [basic.stc.dynamic.deallocation]p2:
1429 // [...] If class T does not declare such an operator delete but does
1430 // declare a member deallocation function named operator delete with
1431 // exactly two parameters, the second of which has type std::size_t (18.1),
1432 // then this function is a usual deallocation function.
1433 ASTContext &Context = getASTContext();
1434 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001435 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1436 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001437 return false;
1438
1439 // This function is a usual deallocation function if there are no
1440 // single-parameter deallocation functions of the same kind.
David Blaikieff7d47a2012-12-19 00:45:41 +00001441 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1442 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1443 I != E; ++I) {
1444 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001445 if (FD->getNumParams() == 1)
1446 return false;
1447 }
1448
1449 return true;
1450}
1451
Douglas Gregorb139cd52010-05-01 20:49:11 +00001452bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001453 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001454 // A user-declared copy assignment operator X::operator= is a non-static
1455 // non-template member function of class X with exactly one parameter of
1456 // type X, X&, const X&, volatile X& or const volatile X&.
1457 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1458 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001459 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1460 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001461 return false;
1462
1463 QualType ParamType = getParamDecl(0)->getType();
1464 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1465 ParamType = Ref->getPointeeType();
1466
1467 ASTContext &Context = getASTContext();
1468 QualType ClassType
1469 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1470 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1471}
1472
Alexis Huntfcaeae42011-05-25 20:50:04 +00001473bool CXXMethodDecl::isMoveAssignmentOperator() const {
1474 // C++0x [class.copy]p19:
1475 // A user-declared move assignment operator X::operator= is a non-static
1476 // non-template member function of class X with exactly one parameter of type
1477 // X&&, const X&&, volatile X&&, or const volatile X&&.
1478 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001479 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1480 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001481 return false;
1482
1483 QualType ParamType = getParamDecl(0)->getType();
1484 if (!isa<RValueReferenceType>(ParamType))
1485 return false;
1486 ParamType = ParamType->getPointeeType();
1487
1488 ASTContext &Context = getASTContext();
1489 QualType ClassType
1490 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1491 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1492}
1493
Anders Carlsson36d87e12009-05-16 23:58:37 +00001494void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001495 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001496 assert(!MD->getParent()->isDependentContext() &&
1497 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001498 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001499
Douglas Gregor832940b2010-03-02 23:58:15 +00001500 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001501}
1502
1503CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001504 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001505 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001506}
1507
1508CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001509 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001510 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001511}
1512
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001513unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001514 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001515 return getASTContext().overridden_methods_size(this);
1516}
1517
Ted Kremenek21475702008-09-05 17:16:31 +00001518QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001519 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1520 // If the member function is declared const, the type of this is const X*,
1521 // if the member function is declared volatile, the type of this is
1522 // volatile X*, and if the member function is declared const volatile,
1523 // the type of this is const volatile X*.
1524
Ted Kremenek21475702008-09-05 17:16:31 +00001525 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001526
John McCalle78aac42010-03-10 03:28:59 +00001527 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001528 ClassTy = C.getQualifiedType(ClassTy,
1529 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001530 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001531}
1532
Eli Friedman71a26d82009-12-06 20:50:05 +00001533bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001534 // If this function is a template instantiation, look at the template from
1535 // which it was instantiated.
1536 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1537 if (!CheckFn)
1538 CheckFn = this;
1539
Eli Friedman71a26d82009-12-06 20:50:05 +00001540 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001541 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001542}
1543
Douglas Gregor355efbb2012-02-17 03:02:34 +00001544bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001545 const CXXRecordDecl *P = getParent();
1546 if (P->isLambda()) {
1547 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1548 if (StaticInvoker == this) return true;
1549 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1550 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1551 }
1552 }
1553 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001554}
1555
Alexis Hunt1d792652011-01-08 20:30:50 +00001556CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1557 TypeSourceInfo *TInfo, bool IsVirtual,
1558 SourceLocation L, Expr *Init,
1559 SourceLocation R,
1560 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001561 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001562 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1563 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001564{
Douglas Gregore8381c02008-11-05 04:29:56 +00001565}
1566
Alexis Hunt1d792652011-01-08 20:30:50 +00001567CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1568 FieldDecl *Member,
1569 SourceLocation MemberLoc,
1570 SourceLocation L, Expr *Init,
1571 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001572 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001573 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001574 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1575{
1576}
1577
Alexis Hunt1d792652011-01-08 20:30:50 +00001578CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1579 IndirectFieldDecl *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),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001585 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001586{
Douglas Gregore8381c02008-11-05 04:29:56 +00001587}
1588
Alexis Hunt1d792652011-01-08 20:30:50 +00001589CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001590 TypeSourceInfo *TInfo,
1591 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001592 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001593 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1594 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001595 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1596{
1597}
1598
1599CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001600 FieldDecl *Member,
1601 SourceLocation MemberLoc,
1602 SourceLocation L, Expr *Init,
1603 SourceLocation R,
1604 VarDecl **Indices,
1605 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001606 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichetd583da02010-12-04 09:14:42 +00001607 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001608 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001609{
1610 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1611 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1612}
1613
Alexis Hunt1d792652011-01-08 20:30:50 +00001614CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1615 FieldDecl *Member,
1616 SourceLocation MemberLoc,
1617 SourceLocation L, Expr *Init,
1618 SourceLocation R,
1619 VarDecl **Indices,
1620 unsigned NumIndices) {
1621 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001622 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001623 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001624 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1625 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001626}
1627
Alexis Hunt1d792652011-01-08 20:30:50 +00001628TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001629 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001630 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001631 else
1632 return TypeLoc();
1633}
1634
Alexis Hunt1d792652011-01-08 20:30:50 +00001635const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001636 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001637 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001638 else
1639 return 0;
1640}
1641
Alexis Hunt1d792652011-01-08 20:30:50 +00001642SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001643 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001644 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001645
1646 if (isInClassMemberInitializer())
1647 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001648
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001649 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1650 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1651
1652 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001653}
1654
Alexis Hunt1d792652011-01-08 20:30:50 +00001655SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001656 if (isInClassMemberInitializer()) {
1657 FieldDecl *D = getAnyMember();
1658 if (Expr *I = D->getInClassInitializer())
1659 return I->getSourceRange();
1660 return SourceRange();
1661 }
1662
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001663 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001664}
1665
David Blaikie68e081d2011-12-20 02:48:34 +00001666void CXXConstructorDecl::anchor() { }
1667
Douglas Gregor61956c42008-10-31 09:07:45 +00001668CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001669CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1670 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1671 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1672 QualType(), 0, false, false, false,false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001673}
1674
1675CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001676CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001677 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001678 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001679 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001680 bool isExplicit, bool isInline,
1681 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001682 assert(NameInfo.getName().getNameKind()
1683 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001684 "Name must refer to a constructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001685 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001686 isExplicit, isInline, isImplicitlyDeclared,
1687 isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001688}
1689
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001690CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1691 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1692 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1693 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1694 return Construct->getConstructor();
1695
1696 return 0;
1697}
1698
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001699bool CXXConstructorDecl::isDefaultConstructor() const {
1700 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001701 // A default constructor for a class X is a constructor of class
1702 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001703 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001704 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001705}
1706
Mike Stump11289f42009-09-09 15:08:12 +00001707bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001708CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001709 return isCopyOrMoveConstructor(TypeQuals) &&
1710 getParamDecl(0)->getType()->isLValueReferenceType();
1711}
1712
1713bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1714 return isCopyOrMoveConstructor(TypeQuals) &&
1715 getParamDecl(0)->getType()->isRValueReferenceType();
1716}
1717
1718/// \brief Determine whether this is a copy or move constructor.
1719bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001720 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001721 // A non-template constructor for class X is a copy constructor
1722 // if its first parameter is of type X&, const X&, volatile X& or
1723 // const volatile X&, and either there are no other parameters
1724 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001725 // C++0x [class.copy]p3:
1726 // A non-template constructor for class X is a move constructor if its
1727 // first parameter is of type X&&, const X&&, volatile X&&, or
1728 // const volatile X&&, and either there are no other parameters or else
1729 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001730 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001731 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +00001732 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001733 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001734 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001735
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001736 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001737
1738 // Do we have a reference type?
1739 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001740 if (!ParamRefType)
1741 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001742
Douglas Gregorff7028a2009-11-13 23:59:09 +00001743 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001744 ASTContext &Context = getASTContext();
1745
Douglas Gregorff7028a2009-11-13 23:59:09 +00001746 CanQualType PointeeType
1747 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001748 CanQualType ClassTy
1749 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001750 if (PointeeType.getUnqualifiedType() != ClassTy)
1751 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001752
John McCall8ccfcb52009-09-24 19:53:00 +00001753 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001754
1755 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001756 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001757 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001758}
1759
Anders Carlssond20e7952009-08-28 16:57:08 +00001760bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001761 // C++ [class.conv.ctor]p1:
1762 // A constructor declared without the function-specifier explicit
1763 // that can be called with a single parameter specifies a
1764 // conversion from the type of its first parameter to the type of
1765 // its class. Such a constructor is called a converting
1766 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001767 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001768 return false;
1769
Mike Stump11289f42009-09-09 15:08:12 +00001770 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001771 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001772 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001773 (getNumParams() > 1 &&
1774 (getParamDecl(1)->hasDefaultArg() ||
1775 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001776}
Douglas Gregor61956c42008-10-31 09:07:45 +00001777
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001778bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001779 if ((getNumParams() < 1) ||
1780 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1781 (getPrimaryTemplate() == 0) ||
1782 (getDescribedFunctionTemplate() != 0))
1783 return false;
1784
1785 const ParmVarDecl *Param = getParamDecl(0);
1786
1787 ASTContext &Context = getASTContext();
1788 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1789
Douglas Gregorffe14e32009-11-14 01:20:54 +00001790 // Is it the same as our our class type?
1791 CanQualType ClassTy
1792 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1793 if (ParamType.getUnqualifiedType() != ClassTy)
1794 return false;
1795
1796 return true;
1797}
1798
Sebastian Redl08905022011-02-05 19:23:19 +00001799const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1800 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001801 method_iterator It = getASTContext().overridden_methods_begin(this);
1802 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redl08905022011-02-05 19:23:19 +00001803 return 0;
1804
1805 return cast<CXXConstructorDecl>(*It);
1806}
1807
1808void
1809CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1810 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001811 assert(getASTContext().overridden_methods_size(this) == 0 &&
1812 "Base ctor already set.");
1813 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001814}
1815
David Blaikie68e081d2011-12-20 02:48:34 +00001816void CXXDestructorDecl::anchor() { }
1817
Douglas Gregor831c93f2008-11-05 20:51:48 +00001818CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001819CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1820 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1821 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001822 QualType(), 0, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001823}
1824
1825CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001826CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001827 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001828 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001829 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001830 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001831 assert(NameInfo.getName().getNameKind()
1832 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001833 "Name must refer to a destructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001834 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001835 isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001836}
1837
David Blaikie68e081d2011-12-20 02:48:34 +00001838void CXXConversionDecl::anchor() { }
1839
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001840CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001841CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1842 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1843 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1844 QualType(), 0, false, false, false,
1845 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001846}
1847
1848CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001849CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001850 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001851 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001852 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001853 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001854 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001855 assert(NameInfo.getName().getNameKind()
1856 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001857 "Name must refer to a conversion function");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001858 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001859 isInline, isExplicit, isConstexpr,
1860 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001861}
1862
Douglas Gregord3b672c2012-02-16 01:06:16 +00001863bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1864 return isImplicit() && getParent()->isLambda() &&
1865 getConversionType()->isBlockPointerType();
1866}
1867
David Blaikie68e081d2011-12-20 02:48:34 +00001868void LinkageSpecDecl::anchor() { }
1869
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001870LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001871 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001872 SourceLocation ExternLoc,
1873 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001874 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001875 bool HasBraces) {
1876 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001877}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001878
Douglas Gregor72172e92012-01-05 21:55:30 +00001879LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1880 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1881 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindola327be3c2013-04-26 01:30:23 +00001882 lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001883}
1884
David Blaikie68e081d2011-12-20 02:48:34 +00001885void UsingDirectiveDecl::anchor() { }
1886
Douglas Gregor889ceb72009-02-03 19:21:40 +00001887UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1888 SourceLocation L,
1889 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001890 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001891 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001892 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001893 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001894 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1895 Used = NS->getOriginalNamespace();
Douglas Gregor12441b32011-02-25 16:33:46 +00001896 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1897 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001898}
1899
Douglas Gregor72172e92012-01-05 21:55:30 +00001900UsingDirectiveDecl *
1901UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1902 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1903 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1904 NestedNameSpecifierLoc(),
1905 SourceLocation(), 0, 0);
1906}
1907
Sebastian Redla6602e92009-11-23 15:34:23 +00001908NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1909 if (NamespaceAliasDecl *NA =
1910 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1911 return NA->getNamespace();
1912 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1913}
1914
Douglas Gregor72172e92012-01-05 21:55:30 +00001915void NamespaceDecl::anchor() { }
1916
Douglas Gregore57e7522012-01-07 09:11:48 +00001917NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1918 SourceLocation StartLoc,
1919 SourceLocation IdLoc, IdentifierInfo *Id,
1920 NamespaceDecl *PrevDecl)
1921 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1922 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1923{
1924 setPreviousDeclaration(PrevDecl);
1925
1926 if (PrevDecl)
1927 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1928}
1929
Douglas Gregor72172e92012-01-05 21:55:30 +00001930NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001931 bool Inline, SourceLocation StartLoc,
1932 SourceLocation IdLoc, IdentifierInfo *Id,
1933 NamespaceDecl *PrevDecl) {
1934 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001935}
1936
1937NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1938 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregore57e7522012-01-07 09:11:48 +00001939 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1940 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001941}
1942
David Blaikie68e081d2011-12-20 02:48:34 +00001943void NamespaceAliasDecl::anchor() { }
1944
Mike Stump11289f42009-09-09 15:08:12 +00001945NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00001946 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001947 SourceLocation AliasLoc,
1948 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001949 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001950 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001951 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001952 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1953 Namespace = NS->getOriginalNamespace();
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001954 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1955 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001956}
1957
Douglas Gregor72172e92012-01-05 21:55:30 +00001958NamespaceAliasDecl *
1959NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1960 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1961 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1962 NestedNameSpecifierLoc(),
1963 SourceLocation(), 0);
1964}
1965
David Blaikie68e081d2011-12-20 02:48:34 +00001966void UsingShadowDecl::anchor() { }
1967
Douglas Gregor72172e92012-01-05 21:55:30 +00001968UsingShadowDecl *
1969UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1970 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1971 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1972}
1973
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001974UsingDecl *UsingShadowDecl::getUsingDecl() const {
1975 const UsingShadowDecl *Shadow = this;
1976 while (const UsingShadowDecl *NextShadow =
1977 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1978 Shadow = NextShadow;
1979 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1980}
1981
David Blaikie68e081d2011-12-20 02:48:34 +00001982void UsingDecl::anchor() { }
1983
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001984void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1985 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1986 "declaration already in set");
1987 assert(S->getUsingDecl() == this);
1988
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00001989 if (FirstUsingShadow.getPointer())
1990 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1991 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001992}
1993
1994void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1995 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1996 "declaration not in set");
1997 assert(S->getUsingDecl() == this);
1998
1999 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2000
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002001 if (FirstUsingShadow.getPointer() == S) {
2002 FirstUsingShadow.setPointer(
2003 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002004 S->UsingOrNextShadow = this;
2005 return;
2006 }
2007
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002008 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002009 while (Prev->UsingOrNextShadow != S)
2010 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2011 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2012 S->UsingOrNextShadow = this;
2013}
2014
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002015UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2016 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002017 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002018 bool HasTypename) {
2019 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002020}
2021
Douglas Gregor72172e92012-01-05 21:55:30 +00002022UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2023 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
2024 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2025 DeclarationNameInfo(), false);
2026}
2027
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002028SourceRange UsingDecl::getSourceRange() const {
2029 SourceLocation Begin = isAccessDeclaration()
2030 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2031 return SourceRange(Begin, getNameInfo().getEndLoc());
2032}
2033
David Blaikie68e081d2011-12-20 02:48:34 +00002034void UnresolvedUsingValueDecl::anchor() { }
2035
John McCalle61f2ba2009-11-18 02:36:19 +00002036UnresolvedUsingValueDecl *
2037UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2038 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002039 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002040 const DeclarationNameInfo &NameInfo) {
John McCalle61f2ba2009-11-18 02:36:19 +00002041 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002042 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002043}
2044
Douglas Gregor72172e92012-01-05 21:55:30 +00002045UnresolvedUsingValueDecl *
2046UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2047 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2048 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2049 NestedNameSpecifierLoc(),
2050 DeclarationNameInfo());
2051}
2052
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002053SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2054 SourceLocation Begin = isAccessDeclaration()
2055 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2056 return SourceRange(Begin, getNameInfo().getEndLoc());
2057}
2058
David Blaikie68e081d2011-12-20 02:48:34 +00002059void UnresolvedUsingTypenameDecl::anchor() { }
2060
John McCalle61f2ba2009-11-18 02:36:19 +00002061UnresolvedUsingTypenameDecl *
2062UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2063 SourceLocation UsingLoc,
2064 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002065 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002066 SourceLocation TargetNameLoc,
2067 DeclarationName TargetName) {
2068 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002069 QualifierLoc, TargetNameLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002070 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002071}
2072
Douglas Gregor72172e92012-01-05 21:55:30 +00002073UnresolvedUsingTypenameDecl *
2074UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2075 void *Mem = AllocateDeserializedDecl(C, ID,
2076 sizeof(UnresolvedUsingTypenameDecl));
2077 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2078 SourceLocation(),
2079 NestedNameSpecifierLoc(),
2080 SourceLocation(),
2081 0);
2082}
2083
David Blaikie68e081d2011-12-20 02:48:34 +00002084void StaticAssertDecl::anchor() { }
2085
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002086StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002087 SourceLocation StaticAssertLoc,
2088 Expr *AssertExpr,
2089 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002090 SourceLocation RParenLoc,
2091 bool Failed) {
Abramo Bagnaraea947882011-03-08 16:41:52 +00002092 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002093 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002094}
2095
Douglas Gregor72172e92012-01-05 21:55:30 +00002096StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2097 unsigned ID) {
2098 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithded9c2e2012-07-11 22:37:56 +00002099 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2100 SourceLocation(), false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002101}
2102
Anders Carlsson6750d162009-03-26 23:46:50 +00002103static const char *getAccessName(AccessSpecifier AS) {
2104 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002105 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002106 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002107 case AS_public:
2108 return "public";
2109 case AS_private:
2110 return "private";
2111 case AS_protected:
2112 return "protected";
2113 }
David Blaikief47fa302012-01-17 02:30:50 +00002114 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002115}
2116
2117const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2118 AccessSpecifier AS) {
2119 return DB << getAccessName(AS);
2120}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002121
2122const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2123 AccessSpecifier AS) {
2124 return DB << getAccessName(AS);
2125}