blob: b7f0fab397696c105ff6511486ee4a6df683e42c [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),
Richard Smith1c33fe82012-11-28 06:23:12 +000074 IsLambda(false), NumBases(0), NumVBases(0), Bases(), VBases(),
Richard Smith68ad0e72013-06-26 02:41:25 +000075 Definition(D), FirstFriend() {
John McCall67da35c2010-02-04 22:26:26 +000076}
77
Benjamin Kramer300c0632012-07-04 17:03:33 +000078CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const {
79 return Bases.get(Definition->getASTContext().getExternalSource());
80}
81
82CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const {
83 return VBases.get(Definition->getASTContext().getExternalSource());
84}
85
John McCall67da35c2010-02-04 22:26:26 +000086CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000087 SourceLocation StartLoc, SourceLocation IdLoc,
88 IdentifierInfo *Id, CXXRecordDecl *PrevDecl)
89 : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl),
John McCall67da35c2010-02-04 22:26:26 +000090 DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0),
Douglas Gregor8ea8fd42009-03-25 21:17:03 +000091 TemplateOrInstantiation() { }
Douglas Gregorb6acda02008-11-12 23:21:09 +000092
Jay Foad39c79802011-01-12 09:06:06 +000093CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK,
Abramo Bagnara29c2d462011-03-09 14:09:51 +000094 DeclContext *DC, SourceLocation StartLoc,
95 SourceLocation IdLoc, IdentifierInfo *Id,
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +000096 CXXRecordDecl* PrevDecl,
97 bool DelayTypeCreation) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +000098 CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc,
99 Id, PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000100 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Mike Stump11289f42009-09-09 15:08:12 +0000101
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000102 // FIXME: DelayTypeCreation seems like such a hack
Douglas Gregor1ec5e9f2009-05-15 19:11:46 +0000103 if (!DelayTypeCreation)
Mike Stump11289f42009-09-09 15:08:12 +0000104 C.getTypeDeclType(R, PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +0000105 return R;
106}
107
Douglas Gregorc8a73492012-02-13 15:44:47 +0000108CXXRecordDecl *CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC,
Eli Friedmand564afb2012-09-19 01:18:11 +0000109 TypeSourceInfo *Info, SourceLocation Loc,
Faisal Valic1a6dc42013-10-23 16:10:50 +0000110 bool Dependent, bool IsGeneric,
111 LambdaCaptureDefault CaptureDefault) {
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;
Faisal Valic1a6dc42013-10-23 16:10:50 +0000115 R->DefinitionData = new (C) struct LambdaDefinitionData(R, Info,
116 Dependent,
117 IsGeneric,
118 CaptureDefault);
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000119 R->MayHaveOutOfDateDef = false;
James Dennett8f60cdd2013-09-05 17:46:21 +0000120 R->setImplicit(true);
Douglas Gregorc8a73492012-02-13 15:44:47 +0000121 C.getTypeDeclType(R, /*PrevDecl=*/0);
122 return R;
123}
124
Douglas Gregor72172e92012-01-05 21:55:30 +0000125CXXRecordDecl *
126CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
127 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +0000128 CXXRecordDecl *R = new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0,
129 SourceLocation(), SourceLocation(),
130 0, 0);
131 R->MayHaveOutOfDateDef = false;
132 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +0000133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135void
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000136CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases,
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000137 unsigned NumBases) {
Douglas Gregor4a62bdf2010-02-11 01:30:34 +0000138 ASTContext &C = getASTContext();
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +0000139
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000140 if (!data().Bases.isOffset() && data().NumBases > 0)
141 C.Deallocate(data().getBases());
Mike Stump11289f42009-09-09 15:08:12 +0000142
Richard Smithaed32b42011-10-18 20:08:55 +0000143 if (NumBases) {
144 // C++ [dcl.init.aggr]p1:
145 // An aggregate is [...] a class with [...] no base classes [...].
146 data().Aggregate = false;
147
148 // C++ [class]p4:
149 // A POD-struct is an aggregate class...
150 data().PlainOldData = false;
151 }
152
Anders Carlssonabb20e62010-03-29 05:13:12 +0000153 // The set of seen virtual base types.
Anders Carlssone47380f2010-03-29 19:49:09 +0000154 llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000155
156 // The virtual bases of this class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000157 SmallVector<const CXXBaseSpecifier *, 8> VBases;
Mike Stump11289f42009-09-09 15:08:12 +0000158
John McCall67da35c2010-02-04 22:26:26 +0000159 data().Bases = new(C) CXXBaseSpecifier [NumBases];
160 data().NumBases = NumBases;
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000161 for (unsigned i = 0; i < NumBases; ++i) {
Douglas Gregord4c5ed02010-10-29 22:39:52 +0000162 data().getBases()[i] = *Bases[i];
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000163 // Keep track of inherited vbases for this base class.
164 const CXXBaseSpecifier *Base = Bases[i];
165 QualType BaseType = Base->getType();
Douglas Gregorbeab56e2010-02-27 00:25:28 +0000166 // Skip dependent types; we can't do any checking on them now.
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000167 if (BaseType->isDependentType())
168 continue;
169 CXXRecordDecl *BaseClassDecl
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000170 = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
Anders Carlssonabb20e62010-03-29 05:13:12 +0000171
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000172 // A class with a non-empty base class is not empty.
173 // FIXME: Standard ref?
Chandler Carruthb1963742011-04-30 09:17:45 +0000174 if (!BaseClassDecl->isEmpty()) {
175 if (!data().Empty) {
176 // C++0x [class]p7:
177 // A standard-layout class is a class that:
178 // [...]
179 // -- either has no non-static data members in the most derived
180 // class and at most one base class with non-static data members,
181 // or has no base classes with non-static data members, and
182 // If this is the second non-empty base, then neither of these two
183 // clauses can be true.
Chandler Carruth583edf82011-04-30 10:07:30 +0000184 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000185 }
186
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000187 data().Empty = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000188 data().HasNoNonEmptyBases = false;
189 }
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000190
Douglas Gregor11c024b2010-09-28 20:50:54 +0000191 // C++ [class.virtual]p1:
192 // A class that declares or inherits a virtual function is called a
193 // polymorphic class.
194 if (BaseClassDecl->isPolymorphic())
195 data().Polymorphic = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000196
Chandler Carruthb1963742011-04-30 09:17:45 +0000197 // C++0x [class]p7:
198 // A standard-layout class is a class that: [...]
199 // -- has no non-standard-layout base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000200 if (!BaseClassDecl->isStandardLayout())
201 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000202
Chandler Carruthe71d0622011-04-24 02:49:34 +0000203 // Record if this base is the first non-literal field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000204 if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C))
Chandler Carruthe71d0622011-04-24 02:49:34 +0000205 data().HasNonLiteralTypeFieldsOrBases = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000206
Anders Carlssonabb20e62010-03-29 05:13:12 +0000207 // Now go through all virtual bases of this base and add them.
Mike Stump11289f42009-09-09 15:08:12 +0000208 for (CXXRecordDecl::base_class_iterator VBase =
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000209 BaseClassDecl->vbases_begin(),
210 E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000211 // Add this base if it's not already in the list.
Richard Smith1c33fe82012-11-28 06:23:12 +0000212 if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) {
Anders Carlssonabb20e62010-03-29 05:13:12 +0000213 VBases.push_back(VBase);
Richard Smith1c33fe82012-11-28 06:23:12 +0000214
215 // C++11 [class.copy]p8:
216 // The implicitly-declared copy constructor for a class X will have
217 // the form 'X::X(const X&)' if each [...] virtual base class B of X
218 // has a copy constructor whose first parameter is of type
219 // 'const B&' or 'const volatile B&' [...]
220 if (CXXRecordDecl *VBaseDecl = VBase->getType()->getAsCXXRecordDecl())
221 if (!VBaseDecl->hasCopyConstructorWithConstParam())
222 data().ImplicitCopyConstructorHasConstParam = false;
223 }
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000224 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000225
226 if (Base->isVirtual()) {
227 // Add this base if it's not already in the list.
Anders Carlssone47380f2010-03-29 19:49:09 +0000228 if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)))
Richard Smith1c33fe82012-11-28 06:23:12 +0000229 VBases.push_back(Base);
230
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000231 // C++0x [meta.unary.prop] is_empty:
232 // T is a class type, but not a union type, with ... no virtual base
233 // classes
234 data().Empty = false;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000235
Richard Smith328aae52012-11-30 05:11:39 +0000236 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
237 // A [default constructor, copy/move constructor, or copy/move assignment
238 // operator for a class X] is trivial [...] if:
239 // -- class X has [...] no virtual base classes
240 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthb1963742011-04-30 09:17:45 +0000241
242 // C++0x [class]p7:
243 // A standard-layout class is a class that: [...]
244 // -- has [...] no virtual base classes
Chandler Carruth583edf82011-04-30 10:07:30 +0000245 data().IsStandardLayout = false;
Richard Smithcc36f692011-12-22 02:22:31 +0000246
247 // C++11 [dcl.constexpr]p4:
248 // In the definition of a constexpr constructor [...]
249 // -- the class shall not have any virtual base classes
250 data().DefaultedDefaultConstructorIsConstexpr = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000251 } else {
252 // C++ [class.ctor]p5:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000253 // A default constructor is trivial [...] if:
254 // -- all the direct base classes of its class have trivial default
255 // constructors.
256 if (!BaseClassDecl->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000257 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
258
Chandler Carruthad7d4042011-04-23 23:10:33 +0000259 // C++0x [class.copy]p13:
260 // A copy/move constructor for class X is trivial if [...]
261 // [...]
262 // -- the constructor selected to copy/move each direct base class
263 // subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000264 if (!BaseClassDecl->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000265 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000266 // If the base class doesn't have a simple move constructor, we'll eagerly
267 // declare it and perform overload resolution to determine which function
268 // it actually calls. If it does have a simple move constructor, this
269 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000270 if (!BaseClassDecl->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000271 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000272
273 // C++0x [class.copy]p27:
274 // A copy/move assignment operator for class X is trivial if [...]
275 // [...]
276 // -- the assignment operator selected to copy/move each direct base
277 // class subobject is trivial, and
Douglas Gregor11c024b2010-09-28 20:50:54 +0000278 if (!BaseClassDecl->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000279 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000280 // If the base class doesn't have a simple move assignment, we'll eagerly
281 // declare it and perform overload resolution to determine which function
282 // it actually calls. If it does have a simple move assignment, this
283 // check is correct.
Richard Smith16488472012-11-16 00:53:38 +0000284 if (!BaseClassDecl->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000285 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Richard Smithcc36f692011-12-22 02:22:31 +0000286
287 // C++11 [class.ctor]p6:
Richard Smithc101e612012-01-11 18:26:05 +0000288 // If that user-written default constructor would satisfy the
Richard Smithcc36f692011-12-22 02:22:31 +0000289 // requirements of a constexpr constructor, the implicitly-defined
290 // default constructor is constexpr.
291 if (!BaseClassDecl->hasConstexprDefaultConstructor())
292 data().DefaultedDefaultConstructorIsConstexpr = false;
Anders Carlssonabb20e62010-03-29 05:13:12 +0000293 }
Richard Smith92f241f2012-12-08 02:53:02 +0000294
Douglas Gregor11c024b2010-09-28 20:50:54 +0000295 // C++ [class.ctor]p3:
296 // A destructor is trivial if all the direct base classes of its class
297 // have trivial destructors.
298 if (!BaseClassDecl->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000299 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000300
301 if (!BaseClassDecl->hasIrrelevantDestructor())
302 data().HasIrrelevantDestructor = false;
303
Richard Smith1c33fe82012-11-28 06:23:12 +0000304 // C++11 [class.copy]p18:
305 // The implicitly-declared copy assignment oeprator for a class X will
306 // have the form 'X& X::operator=(const X&)' if each direct base class B
307 // of X has a copy assignment operator whose parameter is of type 'const
308 // B&', 'const volatile B&', or 'B' [...]
309 if (!BaseClassDecl->hasCopyAssignmentWithConstParam())
310 data().ImplicitCopyAssignmentHasConstParam = false;
311
312 // C++11 [class.copy]p8:
313 // The implicitly-declared copy constructor for a class X will have
314 // the form 'X::X(const X&)' if each direct [...] base class B of X
315 // has a copy constructor whose first parameter is of type
316 // 'const B&' or 'const volatile B&' [...]
317 if (!BaseClassDecl->hasCopyConstructorWithConstParam())
318 data().ImplicitCopyConstructorHasConstParam = false;
319
John McCall31168b02011-06-15 23:02:42 +0000320 // A class has an Objective-C object member if... or any of its bases
321 // has an Objective-C object member.
322 if (BaseClassDecl->hasObjectMember())
323 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000324
325 if (BaseClassDecl->hasVolatileMember())
326 setHasVolatileMember(true);
John McCall31168b02011-06-15 23:02:42 +0000327
Douglas Gregor61226d32011-05-13 01:05:07 +0000328 // Keep track of the presence of mutable fields.
329 if (BaseClassDecl->hasMutableFields())
330 data().HasMutableFields = true;
Richard Smith593f9932012-12-08 02:01:17 +0000331
332 if (BaseClassDecl->hasUninitializedReferenceMember())
333 data().HasUninitializedReferenceMember = true;
Richard Smith6b02d462012-12-08 08:32:28 +0000334
335 addedClassSubobject(BaseClassDecl);
Fariborz Jahanian3554b5a2009-07-10 20:13:23 +0000336 }
Anders Carlssonabb20e62010-03-29 05:13:12 +0000337
338 if (VBases.empty())
339 return;
340
341 // Create base specifier for any direct or indirect virtual bases.
342 data().VBases = new (C) CXXBaseSpecifier[VBases.size()];
343 data().NumVBases = VBases.size();
Richard Smith6b02d462012-12-08 08:32:28 +0000344 for (int I = 0, E = VBases.size(); I != E; ++I) {
345 QualType Type = VBases[I]->getType();
346 if (!Type->isDependentType())
347 addedClassSubobject(Type->getAsCXXRecordDecl());
Richard Smith26935e62011-07-12 23:49:11 +0000348 data().getVBases()[I] = *VBases[I];
Richard Smith6b02d462012-12-08 08:32:28 +0000349 }
350}
351
352void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) {
353 // C++11 [class.copy]p11:
354 // A defaulted copy/move constructor for a class X is defined as
355 // deleted if X has:
356 // -- a direct or virtual base class B that cannot be copied/moved [...]
357 // -- a non-static data member of class type M (or array thereof)
358 // that cannot be copied or moved [...]
359 if (!Subobj->hasSimpleMoveConstructor())
360 data().NeedOverloadResolutionForMoveConstructor = true;
361
362 // C++11 [class.copy]p23:
363 // A defaulted copy/move assignment operator for a class X is defined as
364 // deleted if X has:
365 // -- a direct or virtual base class B that cannot be copied/moved [...]
366 // -- a non-static data member of class type M (or array thereof)
367 // that cannot be copied or moved [...]
368 if (!Subobj->hasSimpleMoveAssignment())
369 data().NeedOverloadResolutionForMoveAssignment = true;
370
371 // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5:
372 // A defaulted [ctor or dtor] for a class X is defined as
373 // deleted if X has:
374 // -- any direct or virtual base class [...] has a type with a destructor
375 // that is deleted or inaccessible from the defaulted [ctor or dtor].
376 // -- any non-static data member has a type with a destructor
377 // that is deleted or inaccessible from the defaulted [ctor or dtor].
378 if (!Subobj->hasSimpleDestructor()) {
379 data().NeedOverloadResolutionForMoveConstructor = true;
380 data().NeedOverloadResolutionForDestructor = true;
381 }
Douglas Gregor9d6290b2008-10-23 18:13:27 +0000382}
383
Douglas Gregord2e6a452010-01-14 17:47:39 +0000384/// Callback function for CXXRecordDecl::forallBases that acknowledges
385/// that it saw a base class.
386static bool SawBase(const CXXRecordDecl *, void *) {
387 return true;
388}
389
390bool CXXRecordDecl::hasAnyDependentBases() const {
391 if (!isDependentContext())
392 return false;
393
394 return !forallBases(SawBase, 0);
395}
396
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000397bool CXXRecordDecl::isTriviallyCopyable() const {
398 // C++0x [class]p5:
399 // A trivially copyable class is a class that:
400 // -- has no non-trivial copy constructors,
Richard Smith16488472012-11-16 00:53:38 +0000401 if (hasNonTrivialCopyConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000402 // -- has no non-trivial move constructors,
Richard Smith16488472012-11-16 00:53:38 +0000403 if (hasNonTrivialMoveConstructor()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000404 // -- has no non-trivial copy assignment operators,
Richard Smith16488472012-11-16 00:53:38 +0000405 if (hasNonTrivialCopyAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000406 // -- has no non-trivial move assignment operators, and
Richard Smith16488472012-11-16 00:53:38 +0000407 if (hasNonTrivialMoveAssignment()) return false;
Chandler Carrutha3e1f9a2011-04-23 10:47:28 +0000408 // -- has a trivial destructor.
409 if (!hasTrivialDestructor()) return false;
410
411 return true;
412}
413
Douglas Gregor7d9120c2010-09-28 21:55:22 +0000414void CXXRecordDecl::markedVirtualFunctionPure() {
415 // C++ [class.abstract]p2:
416 // A class is abstract if it has at least one pure virtual function.
417 data().Abstract = true;
418}
419
420void CXXRecordDecl::addedMember(Decl *D) {
Joao Matose9a3ed42012-08-31 22:18:20 +0000421 if (!D->isImplicit() &&
422 !isa<FieldDecl>(D) &&
423 !isa<IndirectFieldDecl>(D) &&
424 (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class ||
425 cast<TagDecl>(D)->getTagKind() == TTK_Interface))
426 data().HasOnlyCMembers = false;
427
428 // Ignore friends and invalid declarations.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000429 if (D->getFriendObjectKind() || D->isInvalidDecl())
Douglas Gregord30e79f2010-09-27 21:17:54 +0000430 return;
431
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000432 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D);
433 if (FunTmpl)
434 D = FunTmpl->getTemplatedDecl();
435
Douglas Gregora832d3e2010-09-28 19:45:33 +0000436 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
437 if (Method->isVirtual()) {
438 // C++ [dcl.init.aggr]p1:
439 // An aggregate is an array or a class with [...] no virtual functions.
440 data().Aggregate = false;
441
442 // C++ [class]p4:
443 // A POD-struct is an aggregate class...
444 data().PlainOldData = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000445
446 // Virtual functions make the class non-empty.
447 // FIXME: Standard ref?
448 data().Empty = false;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000449
450 // C++ [class.virtual]p1:
451 // A class that declares or inherits a virtual function is called a
452 // polymorphic class.
453 data().Polymorphic = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000454
Richard Smith328aae52012-11-30 05:11:39 +0000455 // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25:
456 // A [default constructor, copy/move constructor, or copy/move
457 // assignment operator for a class X] is trivial [...] if:
Chandler Carruthad7d4042011-04-23 23:10:33 +0000458 // -- class X has no virtual functions [...]
Richard Smith328aae52012-11-30 05:11:39 +0000459 data().HasTrivialSpecialMembers &= SMF_Destructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000460
Chandler Carruthb1963742011-04-30 09:17:45 +0000461 // C++0x [class]p7:
462 // A standard-layout class is a class that: [...]
463 // -- has no virtual functions
Chandler Carruth583edf82011-04-30 10:07:30 +0000464 data().IsStandardLayout = false;
Douglas Gregora832d3e2010-09-28 19:45:33 +0000465 }
466 }
Argyrios Kyrtzidis00f52662010-10-20 23:48:42 +0000467
Richard Smith1c33fe82012-11-28 06:23:12 +0000468 // Notify the listener if an implicit member was added after the definition
469 // was completed.
470 if (!isBeingDefined() && D->isImplicit())
471 if (ASTMutationListener *L = getASTMutationListener())
472 L->AddedCXXImplicitMember(data().Definition, D);
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000473
Richard Smith328aae52012-11-30 05:11:39 +0000474 // The kind of special member this declaration is, if any.
475 unsigned SMKind = 0;
476
Richard Smith1c33fe82012-11-28 06:23:12 +0000477 // Handle constructors.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000478 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000479 if (!Constructor->isImplicit()) {
480 // Note that we have a user-declared constructor.
481 data().UserDeclaredConstructor = true;
482
483 // C++ [class]p4:
484 // A POD-struct is an aggregate class [...]
485 // Since the POD bit is meant to be C++03 POD-ness, clear it even if the
486 // type is technically an aggregate in C++0x since it wouldn't be in 03.
487 data().PlainOldData = false;
488 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000489
Richard Smitha5285072011-09-05 02:13:09 +0000490 // Technically, "user-provided" is only defined for special member
491 // functions, but the intent of the standard is clearly that it should apply
492 // to all functions.
493 bool UserProvided = Constructor->isUserProvided();
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000494
Alexis Hunt88c75c32011-05-09 21:45:35 +0000495 if (Constructor->isDefaultConstructor()) {
Richard Smith328aae52012-11-30 05:11:39 +0000496 SMKind |= SMF_DefaultConstructor;
497
498 if (UserProvided)
Alexis Huntea6f0322011-05-11 22:34:38 +0000499 data().UserProvidedDefaultConstructor = true;
Richard Smith1c33fe82012-11-28 06:23:12 +0000500 if (Constructor->isConstexpr())
Richard Smithcc36f692011-12-22 02:22:31 +0000501 data().HasConstexprDefaultConstructor = true;
Alexis Hunt88c75c32011-05-09 21:45:35 +0000502 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000503
Chandler Carruthad7d4042011-04-23 23:10:33 +0000504 if (!FunTmpl) {
Richard Smith1c33fe82012-11-28 06:23:12 +0000505 unsigned Quals;
506 if (Constructor->isCopyConstructor(Quals)) {
Richard Smith328aae52012-11-30 05:11:39 +0000507 SMKind |= SMF_CopyConstructor;
Richard Smith1c33fe82012-11-28 06:23:12 +0000508
509 if (Quals & Qualifiers::Const)
510 data().HasDeclaredCopyConstructorWithConstParam = true;
Richard Smith328aae52012-11-30 05:11:39 +0000511 } else if (Constructor->isMoveConstructor())
512 SMKind |= SMF_MoveConstructor;
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000513 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000514
515 // Record if we see any constexpr constructors which are neither copy
516 // nor move constructors.
517 if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor())
Richard Smith111af8d2011-08-10 18:11:37 +0000518 data().HasConstexprNonCopyMoveConstructor = true;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000519
Alexis Hunt88c75c32011-05-09 21:45:35 +0000520 // C++ [dcl.init.aggr]p1:
521 // An aggregate is an array or a class with no user-declared
522 // constructors [...].
Richard Smith852c9db2013-04-20 22:23:05 +0000523 // C++11 [dcl.init.aggr]p1:
Alexis Hunt88c75c32011-05-09 21:45:35 +0000524 // An aggregate is an array or a class with no user-provided
525 // constructors [...].
Richard Smith2bf7fdb2013-01-02 11:42:31 +0000526 if (getASTContext().getLangOpts().CPlusPlus11
Richard Smith1c33fe82012-11-28 06:23:12 +0000527 ? UserProvided : !Constructor->isImplicit())
Alexis Hunt88c75c32011-05-09 21:45:35 +0000528 data().Aggregate = false;
Douglas Gregord30e79f2010-09-27 21:17:54 +0000529 }
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000530
Richard Smith1c33fe82012-11-28 06:23:12 +0000531 // Handle destructors.
Alexis Hunt97ab5542011-05-16 22:41:40 +0000532 if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
Richard Smith328aae52012-11-30 05:11:39 +0000533 SMKind |= SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000534
Richard Smith328aae52012-11-30 05:11:39 +0000535 if (!DD->isImplicit())
Richard Smith1c33fe82012-11-28 06:23:12 +0000536 data().HasIrrelevantDestructor = false;
537
Richard Smith1c33fe82012-11-28 06:23:12 +0000538 // C++11 [class.dtor]p5:
Richard Smith328aae52012-11-30 05:11:39 +0000539 // A destructor is trivial if [...] the destructor is not virtual.
540 if (DD->isVirtual())
541 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Douglas Gregor8f9ebe52010-09-27 22:48:58 +0000542 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000543
544 // Handle member functions.
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000545 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Alexis Huntfcaeae42011-05-25 20:50:04 +0000546 if (Method->isCopyAssignmentOperator()) {
Richard Smith328aae52012-11-30 05:11:39 +0000547 SMKind |= SMF_CopyAssignment;
Richard Smith1c33fe82012-11-28 06:23:12 +0000548
549 const ReferenceType *ParamTy =
550 Method->getParamDecl(0)->getType()->getAs<ReferenceType>();
551 if (!ParamTy || ParamTy->getPointeeType().isConstQualified())
552 data().HasDeclaredCopyAssignmentWithConstParam = true;
Alexis Huntfcaeae42011-05-25 20:50:04 +0000553 }
Alexis Huntfcaeae42011-05-25 20:50:04 +0000554
Richard Smith328aae52012-11-30 05:11:39 +0000555 if (Method->isMoveAssignmentOperator())
556 SMKind |= SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000557
Douglas Gregor457104e2010-09-29 04:25:11 +0000558 // Keep the list of conversion functions up-to-date.
559 if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) {
Douglas Gregor92fde2f2013-04-08 17:12:58 +0000560 // FIXME: We use the 'unsafe' accessor for the access specifier here,
561 // because Sema may not have set it yet. That's really just a misdesign
562 // in Sema. However, LLDB *will* have set the access specifier correctly,
563 // and adds declarations after the class is technically completed,
564 // so completeDefinition()'s overriding of the access specifiers doesn't
565 // work.
566 AccessSpecifier AS = Conversion->getAccessUnsafe();
567
Richard Smith328aae52012-11-30 05:11:39 +0000568 if (Conversion->getPrimaryTemplate()) {
569 // We don't record specializations.
Douglas Gregor457104e2010-09-29 04:25:11 +0000570 } else {
Richard Smitha4ba74c2013-08-30 04:46:40 +0000571 ASTContext &Ctx = getASTContext();
572 ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx);
573 NamedDecl *Primary =
574 FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion);
575 if (Primary->getPreviousDecl())
576 Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()),
577 Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000578 else
Richard Smitha4ba74c2013-08-30 04:46:40 +0000579 Conversions.addDecl(Ctx, Primary, AS);
Douglas Gregor457104e2010-09-29 04:25:11 +0000580 }
581 }
Richard Smith1c33fe82012-11-28 06:23:12 +0000582
Richard Smith328aae52012-11-30 05:11:39 +0000583 if (SMKind) {
Richard Smith92f241f2012-12-08 02:53:02 +0000584 // If this is the first declaration of a special member, we no longer have
585 // an implicit trivial special member.
586 data().HasTrivialSpecialMembers &=
587 data().DeclaredSpecialMembers | ~SMKind;
588
589 if (!Method->isImplicit() && !Method->isUserProvided()) {
590 // This method is user-declared but not user-provided. We can't work out
591 // whether it's trivial yet (not until we get to the end of the class).
592 // We'll handle this method in finishedDefaultedOrDeletedMember.
593 } else if (Method->isTrivial())
594 data().HasTrivialSpecialMembers |= SMKind;
595 else
596 data().DeclaredNonTrivialSpecialMembers |= SMKind;
597
Richard Smith328aae52012-11-30 05:11:39 +0000598 // Note when we have declared a declared special member, and suppress the
599 // implicit declaration of this special member.
600 data().DeclaredSpecialMembers |= SMKind;
601
602 if (!Method->isImplicit()) {
603 data().UserDeclaredSpecialMembers |= SMKind;
604
605 // C++03 [class]p4:
606 // A POD-struct is an aggregate class that has [...] no user-defined
607 // copy assignment operator and no user-defined destructor.
608 //
609 // Since the POD bit is meant to be C++03 POD-ness, and in C++03,
610 // aggregates could not have any constructors, clear it even for an
611 // explicitly defaulted or deleted constructor.
612 // type is technically an aggregate in C++0x since it wouldn't be in 03.
613 //
614 // Also, a user-declared move assignment operator makes a class non-POD.
615 // This is an extension in C++03.
616 data().PlainOldData = false;
617 }
Richard Smith328aae52012-11-30 05:11:39 +0000618 }
619
Douglas Gregora1ce1f82010-09-27 22:06:20 +0000620 return;
Douglas Gregor8a273912009-07-22 18:25:24 +0000621 }
Richard Smith328aae52012-11-30 05:11:39 +0000622
Douglas Gregora832d3e2010-09-28 19:45:33 +0000623 // Handle non-static data members.
624 if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
Douglas Gregor556e5862011-10-10 17:22:13 +0000625 // C++ [class.bit]p2:
626 // A declaration for a bit-field that omits the identifier declares an
627 // unnamed bit-field. Unnamed bit-fields are not members and cannot be
628 // initialized.
629 if (Field->isUnnamedBitfield())
630 return;
631
Douglas Gregora832d3e2010-09-28 19:45:33 +0000632 // C++ [dcl.init.aggr]p1:
633 // An aggregate is an array or a class (clause 9) with [...] no
634 // private or protected non-static data members (clause 11).
635 //
636 // A POD must be an aggregate.
637 if (D->getAccess() == AS_private || D->getAccess() == AS_protected) {
638 data().Aggregate = false;
639 data().PlainOldData = false;
640 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000641
642 // C++0x [class]p7:
643 // A standard-layout class is a class that:
644 // [...]
645 // -- has the same access control for all non-static data members,
646 switch (D->getAccess()) {
647 case AS_private: data().HasPrivateFields = true; break;
648 case AS_protected: data().HasProtectedFields = true; break;
649 case AS_public: data().HasPublicFields = true; break;
David Blaikie83d382b2011-09-23 05:06:16 +0000650 case AS_none: llvm_unreachable("Invalid access specifier");
Chandler Carruthb1963742011-04-30 09:17:45 +0000651 };
652 if ((data().HasPrivateFields + data().HasProtectedFields +
653 data().HasPublicFields) > 1)
Chandler Carruth583edf82011-04-30 10:07:30 +0000654 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000655
Douglas Gregor61226d32011-05-13 01:05:07 +0000656 // Keep track of the presence of mutable fields.
657 if (Field->isMutable())
658 data().HasMutableFields = true;
659
Chandler Carruthad7d4042011-04-23 23:10:33 +0000660 // C++0x [class]p9:
Douglas Gregora832d3e2010-09-28 19:45:33 +0000661 // A POD struct is a class that is both a trivial class and a
662 // standard-layout class, and has no non-static data members of type
663 // non-POD struct, non-POD union (or array of such types).
John McCall31168b02011-06-15 23:02:42 +0000664 //
665 // Automatic Reference Counting: the presence of a member of Objective-C pointer type
666 // that does not explicitly have no lifetime makes the class a non-POD.
667 // However, we delay setting PlainOldData to false in this case so that
668 // Sema has a chance to diagnostic causes where the same class will be
Douglas Gregor6fa69422012-07-23 04:23:39 +0000669 // non-POD with Automatic Reference Counting but a POD without ARC.
John McCall31168b02011-06-15 23:02:42 +0000670 // In this case, the class will become a non-POD class when we complete
671 // the definition.
Douglas Gregora832d3e2010-09-28 19:45:33 +0000672 ASTContext &Context = getASTContext();
673 QualType T = Context.getBaseElementType(Field->getType());
John McCall31168b02011-06-15 23:02:42 +0000674 if (T->isObjCRetainableType() || T.isObjCGCStrong()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000675 if (!Context.getLangOpts().ObjCAutoRefCount ||
John McCall31168b02011-06-15 23:02:42 +0000676 T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone)
677 setHasObjectMember(true);
Eli Friedmana5433322013-07-20 01:06:31 +0000678 } else if (!T.isCXX98PODType(Context))
Douglas Gregora832d3e2010-09-28 19:45:33 +0000679 data().PlainOldData = false;
John McCall31168b02011-06-15 23:02:42 +0000680
Chandler Carruthb1963742011-04-30 09:17:45 +0000681 if (T->isReferenceType()) {
Richard Smith593f9932012-12-08 02:01:17 +0000682 if (!Field->hasInClassInitializer())
683 data().HasUninitializedReferenceMember = true;
Chandler Carruthe71d0622011-04-24 02:49:34 +0000684
Chandler Carruthb1963742011-04-30 09:17:45 +0000685 // C++0x [class]p7:
686 // A standard-layout class is a class that:
687 // -- has no non-static data members of type [...] reference,
Chandler Carruth583edf82011-04-30 10:07:30 +0000688 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000689 }
690
Richard Smith3607ffe2012-02-13 03:54:03 +0000691 // Record if this field is the first non-literal or volatile field or base.
Richard Smithd9f663b2013-04-22 15:31:51 +0000692 if (!T->isLiteralType(Context) || T.isVolatileQualified())
Chandler Carruthe71d0622011-04-24 02:49:34 +0000693 data().HasNonLiteralTypeFieldsOrBases = true;
694
Richard Smith938f40b2011-06-11 17:19:42 +0000695 if (Field->hasInClassInitializer()) {
Richard Smithe2648ba2012-05-07 01:07:30 +0000696 data().HasInClassInitializer = true;
697
698 // C++11 [class]p5:
Richard Smith938f40b2011-06-11 17:19:42 +0000699 // A default constructor is trivial if [...] no non-static data member
700 // of its class has a brace-or-equal-initializer.
Richard Smith328aae52012-11-30 05:11:39 +0000701 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Richard Smith938f40b2011-06-11 17:19:42 +0000702
Richard Smithe2648ba2012-05-07 01:07:30 +0000703 // C++11 [dcl.init.aggr]p1:
Richard Smith938f40b2011-06-11 17:19:42 +0000704 // An aggregate is a [...] class with [...] no
705 // brace-or-equal-initializers for non-static data members.
Richard Smith852c9db2013-04-20 22:23:05 +0000706 //
707 // This rule was removed in C++1y.
708 if (!getASTContext().getLangOpts().CPlusPlus1y)
709 data().Aggregate = false;
Richard Smith938f40b2011-06-11 17:19:42 +0000710
Richard Smithe2648ba2012-05-07 01:07:30 +0000711 // C++11 [class]p10:
Richard Smith938f40b2011-06-11 17:19:42 +0000712 // A POD struct is [...] a trivial class.
713 data().PlainOldData = false;
714 }
715
Richard Smith6b02d462012-12-08 08:32:28 +0000716 // C++11 [class.copy]p23:
717 // A defaulted copy/move assignment operator for a class X is defined
718 // as deleted if X has:
719 // -- a non-static data member of reference type
720 if (T->isReferenceType())
721 data().DefaultedMoveAssignmentIsDeleted = true;
722
Douglas Gregor11c024b2010-09-28 20:50:54 +0000723 if (const RecordType *RecordTy = T->getAs<RecordType>()) {
724 CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl());
725 if (FieldRec->getDefinition()) {
Richard Smith6b02d462012-12-08 08:32:28 +0000726 addedClassSubobject(FieldRec);
727
728 // C++11 [class.ctor]p5, C++11 [class.copy]p11:
729 // A defaulted [special member] for a class X is defined as
730 // deleted if:
731 // -- X is a union-like class that has a variant member with a
732 // non-trivial [corresponding special member]
733 if (isUnion()) {
734 if (FieldRec->hasNonTrivialMoveConstructor())
735 data().DefaultedMoveConstructorIsDeleted = true;
736 if (FieldRec->hasNonTrivialMoveAssignment())
737 data().DefaultedMoveAssignmentIsDeleted = true;
738 if (FieldRec->hasNonTrivialDestructor())
739 data().DefaultedDestructorIsDeleted = true;
740 }
741
Alexis Huntf479f1b2011-05-09 18:22:59 +0000742 // C++0x [class.ctor]p5:
Richard Smithcc36f692011-12-22 02:22:31 +0000743 // A default constructor is trivial [...] if:
Alexis Huntf479f1b2011-05-09 18:22:59 +0000744 // -- for all the non-static data members of its class that are of
745 // class type (or array thereof), each such class has a trivial
746 // default constructor.
747 if (!FieldRec->hasTrivialDefaultConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000748 data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000749
750 // C++0x [class.copy]p13:
751 // A copy/move constructor for class X is trivial if [...]
752 // [...]
753 // -- for each non-static data member of X that is of class type (or
754 // an array thereof), the constructor selected to copy/move that
755 // member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000756 if (!FieldRec->hasTrivialCopyConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000757 data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor;
Richard Smith6b02d462012-12-08 08:32:28 +0000758 // If the field doesn't have a simple move constructor, we'll eagerly
759 // declare the move constructor for this class and we'll decide whether
760 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000761 if (!FieldRec->hasTrivialMoveConstructor())
Richard Smith328aae52012-11-30 05:11:39 +0000762 data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000763
764 // C++0x [class.copy]p27:
765 // A copy/move assignment operator for class X is trivial if [...]
766 // [...]
767 // -- for each non-static data member of X that is of class type (or
768 // an array thereof), the assignment operator selected to
769 // copy/move that member is trivial;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000770 if (!FieldRec->hasTrivialCopyAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000771 data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment;
Richard Smith6b02d462012-12-08 08:32:28 +0000772 // If the field doesn't have a simple move assignment, we'll eagerly
773 // declare the move assignment for this class and we'll decide whether
774 // it's trivial then.
Richard Smith16488472012-11-16 00:53:38 +0000775 if (!FieldRec->hasTrivialMoveAssignment())
Richard Smith328aae52012-11-30 05:11:39 +0000776 data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment;
Chandler Carruthad7d4042011-04-23 23:10:33 +0000777
Douglas Gregor11c024b2010-09-28 20:50:54 +0000778 if (!FieldRec->hasTrivialDestructor())
Richard Smith328aae52012-11-30 05:11:39 +0000779 data().HasTrivialSpecialMembers &= ~SMF_Destructor;
Richard Smith561fb152012-02-25 07:33:38 +0000780 if (!FieldRec->hasIrrelevantDestructor())
781 data().HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +0000782 if (FieldRec->hasObjectMember())
783 setHasObjectMember(true);
Fariborz Jahanian78652202013-01-25 23:57:05 +0000784 if (FieldRec->hasVolatileMember())
785 setHasVolatileMember(true);
Chandler Carruthb1963742011-04-30 09:17:45 +0000786
787 // C++0x [class]p7:
788 // A standard-layout class is a class that:
789 // -- has no non-static data members of type non-standard-layout
790 // class (or array of such types) [...]
Chandler Carruth583edf82011-04-30 10:07:30 +0000791 if (!FieldRec->isStandardLayout())
792 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000793
794 // C++0x [class]p7:
795 // A standard-layout class is a class that:
796 // [...]
797 // -- has no base classes of the same type as the first non-static
798 // data member.
799 // We don't want to expend bits in the state of the record decl
800 // tracking whether this is the first non-static data member so we
801 // cheat a bit and use some of the existing state: the empty bit.
802 // Virtual bases and virtual methods make a class non-empty, but they
803 // also make it non-standard-layout so we needn't check here.
804 // A non-empty base class may leave the class standard-layout, but not
805 // if we have arrived here, and have at least on non-static data
Chandler Carruth583edf82011-04-30 10:07:30 +0000806 // member. If IsStandardLayout remains true, then the first non-static
Chandler Carruthb1963742011-04-30 09:17:45 +0000807 // data member must come through here with Empty still true, and Empty
808 // will subsequently be set to false below.
Chandler Carruth583edf82011-04-30 10:07:30 +0000809 if (data().IsStandardLayout && data().Empty) {
Chandler Carruthb1963742011-04-30 09:17:45 +0000810 for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(),
811 BE = bases_end();
812 BI != BE; ++BI) {
813 if (Context.hasSameUnqualifiedType(BI->getType(), T)) {
Chandler Carruth583edf82011-04-30 10:07:30 +0000814 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000815 break;
816 }
817 }
818 }
Douglas Gregor61226d32011-05-13 01:05:07 +0000819
820 // Keep track of the presence of mutable fields.
821 if (FieldRec->hasMutableFields())
822 data().HasMutableFields = true;
Richard Smithcc36f692011-12-22 02:22:31 +0000823
824 // C++11 [class.copy]p13:
825 // If the implicitly-defined constructor would satisfy the
826 // requirements of a constexpr constructor, the implicitly-defined
827 // constructor is constexpr.
828 // C++11 [dcl.constexpr]p4:
829 // -- every constructor involved in initializing non-static data
830 // members [...] shall be a constexpr constructor
831 if (!Field->hasInClassInitializer() &&
Richard Smithe2648ba2012-05-07 01:07:30 +0000832 !FieldRec->hasConstexprDefaultConstructor() && !isUnion())
Richard Smithcc36f692011-12-22 02:22:31 +0000833 // The standard requires any in-class initializer to be a constant
834 // expression. We consider this to be a defect.
835 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith1c33fe82012-11-28 06:23:12 +0000836
837 // C++11 [class.copy]p8:
838 // The implicitly-declared copy constructor for a class X will have
839 // the form 'X::X(const X&)' if [...] for all the non-static data
840 // members of X that are of a class type M (or array thereof), each
841 // such class type has a copy constructor whose first parameter is
842 // of type 'const M&' or 'const volatile M&'.
843 if (!FieldRec->hasCopyConstructorWithConstParam())
844 data().ImplicitCopyConstructorHasConstParam = false;
845
846 // C++11 [class.copy]p18:
847 // The implicitly-declared copy assignment oeprator for a class X will
848 // have the form 'X& X::operator=(const X&)' if [...] for all the
849 // non-static data members of X that are of a class type M (or array
850 // thereof), each such class type has a copy assignment operator whose
851 // parameter is of type 'const M&', 'const volatile M&' or 'M'.
852 if (!FieldRec->hasCopyAssignmentWithConstParam())
853 data().ImplicitCopyAssignmentHasConstParam = false;
Richard Smith593f9932012-12-08 02:01:17 +0000854
855 if (FieldRec->hasUninitializedReferenceMember() &&
856 !Field->hasInClassInitializer())
857 data().HasUninitializedReferenceMember = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000858 }
Richard Smithcc36f692011-12-22 02:22:31 +0000859 } else {
860 // Base element type of field is a non-class type.
Richard Smithd9f663b2013-04-22 15:31:51 +0000861 if (!T->isLiteralType(Context) ||
Richard Smith4086a132012-06-10 07:07:24 +0000862 (!Field->hasInClassInitializer() && !isUnion()))
Richard Smithcc36f692011-12-22 02:22:31 +0000863 data().DefaultedDefaultConstructorIsConstexpr = false;
Richard Smith6b02d462012-12-08 08:32:28 +0000864
865 // C++11 [class.copy]p23:
866 // A defaulted copy/move assignment operator for a class X is defined
867 // as deleted if X has:
868 // -- a non-static data member of const non-class type (or array
869 // thereof)
870 if (T.isConstQualified())
871 data().DefaultedMoveAssignmentIsDeleted = true;
Douglas Gregor11c024b2010-09-28 20:50:54 +0000872 }
Chandler Carruthb1963742011-04-30 09:17:45 +0000873
874 // C++0x [class]p7:
875 // A standard-layout class is a class that:
876 // [...]
877 // -- either has no non-static data members in the most derived
878 // class and at most one base class with non-static data members,
879 // or has no base classes with non-static data members, and
880 // At this point we know that we have a non-static data member, so the last
881 // clause holds.
882 if (!data().HasNoNonEmptyBases)
Chandler Carruth583edf82011-04-30 10:07:30 +0000883 data().IsStandardLayout = false;
Chandler Carruthb1963742011-04-30 09:17:45 +0000884
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000885 // If this is not a zero-length bit-field, then the class is not empty.
886 if (data().Empty) {
Richard Smithcaf33902011-10-10 18:28:20 +0000887 if (!Field->isBitField() ||
888 (!Field->getBitWidth()->isTypeDependent() &&
889 !Field->getBitWidth()->isValueDependent() &&
890 Field->getBitWidthValue(Context) != 0))
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000891 data().Empty = false;
Douglas Gregor9d5938a2010-09-28 20:38:10 +0000892 }
Douglas Gregora832d3e2010-09-28 19:45:33 +0000893 }
Douglas Gregor457104e2010-09-29 04:25:11 +0000894
895 // Handle using declarations of conversion functions.
Richard Smitha4ba74c2013-08-30 04:46:40 +0000896 if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) {
Douglas Gregor457104e2010-09-29 04:25:11 +0000897 if (Shadow->getDeclName().getNameKind()
Richard Smitha4ba74c2013-08-30 04:46:40 +0000898 == DeclarationName::CXXConversionFunctionName) {
899 ASTContext &Ctx = getASTContext();
900 data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess());
901 }
902 }
Joao Matose9a3ed42012-08-31 22:18:20 +0000903}
904
Richard Smith92f241f2012-12-08 02:53:02 +0000905void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) {
906 assert(!D->isImplicit() && !D->isUserProvided());
907
908 // The kind of special member this declaration is, if any.
909 unsigned SMKind = 0;
910
911 if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
912 if (Constructor->isDefaultConstructor()) {
913 SMKind |= SMF_DefaultConstructor;
914 if (Constructor->isConstexpr())
915 data().HasConstexprDefaultConstructor = true;
916 }
917 if (Constructor->isCopyConstructor())
918 SMKind |= SMF_CopyConstructor;
919 else if (Constructor->isMoveConstructor())
920 SMKind |= SMF_MoveConstructor;
921 else if (Constructor->isConstexpr())
922 // We may now know that the constructor is constexpr.
923 data().HasConstexprNonCopyMoveConstructor = true;
924 } else if (isa<CXXDestructorDecl>(D))
925 SMKind |= SMF_Destructor;
926 else if (D->isCopyAssignmentOperator())
927 SMKind |= SMF_CopyAssignment;
928 else if (D->isMoveAssignmentOperator())
929 SMKind |= SMF_MoveAssignment;
930
931 // Update which trivial / non-trivial special members we have.
932 // addedMember will have skipped this step for this member.
933 if (D->isTrivial())
934 data().HasTrivialSpecialMembers |= SMKind;
935 else
936 data().DeclaredNonTrivialSpecialMembers |= SMKind;
937}
938
Joao Matose9a3ed42012-08-31 22:18:20 +0000939bool CXXRecordDecl::isCLike() const {
940 if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface ||
941 !TemplateOrInstantiation.isNull())
942 return false;
943 if (!hasDefinition())
944 return true;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000945
Argyrios Kyrtzidisc3c9ee52012-02-01 06:36:44 +0000946 return isPOD() && data().HasOnlyCMembers;
Argyrios Kyrtzidis5c4e0652012-01-23 16:58:45 +0000947}
Faisal Valic1a6dc42013-10-23 16:10:50 +0000948
Faisal Vali2b391ab2013-09-26 19:54:12 +0000949bool CXXRecordDecl::isGenericLambda() const {
Faisal Valic1a6dc42013-10-23 16:10:50 +0000950 if (!isLambda()) return false;
951 return getLambdaData().IsGenericLambda;
Faisal Vali2b391ab2013-09-26 19:54:12 +0000952}
953
954CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const {
955 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000956 DeclarationName Name =
957 getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000958 DeclContext::lookup_const_result Calls = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000959
960 assert(!Calls.empty() && "Missing lambda call operator!");
961 assert(Calls.size() == 1 && "More than one lambda call operator!");
962
963 NamedDecl *CallOp = Calls.front();
964 if (FunctionTemplateDecl *CallOpTmpl =
965 dyn_cast<FunctionTemplateDecl>(CallOp))
966 return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000967
968 return cast<CXXMethodDecl>(CallOp);
969}
970
971CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const {
972 if (!isLambda()) return 0;
Faisal Vali850da1a2013-09-29 17:08:32 +0000973 DeclarationName Name =
974 &getASTContext().Idents.get(getLambdaStaticInvokerName());
Faisal Vali2b391ab2013-09-26 19:54:12 +0000975 DeclContext::lookup_const_result Invoker = lookup(Name);
Faisal Vali850da1a2013-09-29 17:08:32 +0000976 if (Invoker.empty()) return 0;
977 assert(Invoker.size() == 1 && "More than one static invoker operator!");
978 NamedDecl *InvokerFun = Invoker.front();
979 if (FunctionTemplateDecl *InvokerTemplate =
980 dyn_cast<FunctionTemplateDecl>(InvokerFun))
981 return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl());
982
Faisal Vali571df122013-09-29 08:45:24 +0000983 return cast<CXXMethodDecl>(InvokerFun);
Faisal Vali2b391ab2013-09-26 19:54:12 +0000984}
985
Douglas Gregor9c702202012-02-10 07:45:31 +0000986void CXXRecordDecl::getCaptureFields(
987 llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
Eli Friedman7e7da2d2012-02-11 00:18:00 +0000988 FieldDecl *&ThisCapture) const {
Douglas Gregor9c702202012-02-10 07:45:31 +0000989 Captures.clear();
990 ThisCapture = 0;
991
Douglas Gregorc8a73492012-02-13 15:44:47 +0000992 LambdaDefinitionData &Lambda = getLambdaData();
Douglas Gregor9c702202012-02-10 07:45:31 +0000993 RecordDecl::field_iterator Field = field_begin();
Douglas Gregore5561632012-02-13 17:20:40 +0000994 for (LambdaExpr::Capture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures;
Douglas Gregor9c702202012-02-10 07:45:31 +0000995 C != CEnd; ++C, ++Field) {
Richard Smithba71c082013-05-16 06:20:58 +0000996 if (C->capturesThis())
David Blaikie40ed2972012-06-06 20:45:41 +0000997 ThisCapture = *Field;
Richard Smithba71c082013-05-16 06:20:58 +0000998 else if (C->capturesVariable())
999 Captures[C->getCapturedVar()] = *Field;
Douglas Gregor9c702202012-02-10 07:45:31 +00001000 }
Richard Smithbb13c9a2013-09-28 04:02:39 +00001001 assert(Field == field_end());
Douglas Gregor9c702202012-02-10 07:45:31 +00001002}
1003
Faisal Vali2b391ab2013-09-26 19:54:12 +00001004TemplateParameterList *
1005CXXRecordDecl::getGenericLambdaTemplateParameterList() const {
1006 if (!isLambda()) return 0;
1007 CXXMethodDecl *CallOp = getLambdaCallOperator();
1008 if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate())
1009 return Tmpl->getTemplateParameters();
1010 return 0;
1011}
Douglas Gregor9c702202012-02-10 07:45:31 +00001012
John McCall1e3a1a72010-03-15 09:07:48 +00001013static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
1014 QualType T;
John McCallda4458e2010-03-31 01:36:47 +00001015 if (isa<UsingShadowDecl>(Conv))
1016 Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl();
John McCall1e3a1a72010-03-15 09:07:48 +00001017 if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv))
1018 T = ConvTemp->getTemplatedDecl()->getResultType();
1019 else
1020 T = cast<CXXConversionDecl>(Conv)->getConversionType();
1021 return Context.getCanonicalType(T);
Fariborz Jahanian3ee21f12009-10-07 20:43:36 +00001022}
1023
John McCall1e3a1a72010-03-15 09:07:48 +00001024/// Collect the visible conversions of a base class.
1025///
James Dennettb5d5f812012-06-15 22:28:09 +00001026/// \param Record a base class of the class we're considering
John McCall1e3a1a72010-03-15 09:07:48 +00001027/// \param InVirtual whether this base class is a virtual base (or a base
1028/// of a virtual base)
1029/// \param Access the access along the inheritance path to this base
1030/// \param ParentHiddenTypes the conversions provided by the inheritors
1031/// of this base
1032/// \param Output the set to which to add conversions from non-virtual bases
1033/// \param VOutput the set to which to add conversions from virtual bases
1034/// \param HiddenVBaseCs the set of conversions which were hidden in a
1035/// virtual base along some inheritance path
1036static void CollectVisibleConversions(ASTContext &Context,
1037 CXXRecordDecl *Record,
1038 bool InVirtual,
1039 AccessSpecifier Access,
1040 const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001041 ASTUnresolvedSet &Output,
John McCall1e3a1a72010-03-15 09:07:48 +00001042 UnresolvedSetImpl &VOutput,
1043 llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
1044 // The set of types which have conversions in this class or its
1045 // subclasses. As an optimization, we don't copy the derived set
1046 // unless it might change.
1047 const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes;
1048 llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer;
1049
1050 // Collect the direct conversions and figure out which conversions
1051 // will be hidden in the subclasses.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001052 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1053 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
1054 if (ConvI != ConvE) {
John McCall1e3a1a72010-03-15 09:07:48 +00001055 HiddenTypesBuffer = ParentHiddenTypes;
1056 HiddenTypes = &HiddenTypesBuffer;
1057
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001058 for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) {
Richard Smith99fdf8d2012-05-06 00:04:32 +00001059 CanQualType ConvType(GetConversionType(Context, I.getDecl()));
1060 bool Hidden = ParentHiddenTypes.count(ConvType);
1061 if (!Hidden)
1062 HiddenTypesBuffer.insert(ConvType);
John McCall1e3a1a72010-03-15 09:07:48 +00001063
1064 // If this conversion is hidden and we're in a virtual base,
1065 // remember that it's hidden along some inheritance path.
1066 if (Hidden && InVirtual)
1067 HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()));
1068
1069 // If this conversion isn't hidden, add it to the appropriate output.
1070 else if (!Hidden) {
1071 AccessSpecifier IAccess
1072 = CXXRecordDecl::MergeAccess(Access, I.getAccess());
1073
1074 if (InVirtual)
1075 VOutput.addDecl(I.getDecl(), IAccess);
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001076 else
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001077 Output.addDecl(Context, I.getDecl(), IAccess);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001078 }
1079 }
1080 }
Sebastian Redl1054fae2009-10-25 17:03:50 +00001081
John McCall1e3a1a72010-03-15 09:07:48 +00001082 // Collect information recursively from any base classes.
1083 for (CXXRecordDecl::base_class_iterator
1084 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1085 const RecordType *RT = I->getType()->getAs<RecordType>();
1086 if (!RT) continue;
Sebastian Redl1054fae2009-10-25 17:03:50 +00001087
John McCall1e3a1a72010-03-15 09:07:48 +00001088 AccessSpecifier BaseAccess
1089 = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier());
1090 bool BaseInVirtual = InVirtual || I->isVirtual();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001091
John McCall1e3a1a72010-03-15 09:07:48 +00001092 CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1093 CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess,
1094 *HiddenTypes, Output, VOutput, HiddenVBaseCs);
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001095 }
John McCall1e3a1a72010-03-15 09:07:48 +00001096}
Sebastian Redl1054fae2009-10-25 17:03:50 +00001097
John McCall1e3a1a72010-03-15 09:07:48 +00001098/// Collect the visible conversions of a class.
1099///
1100/// This would be extremely straightforward if it weren't for virtual
1101/// bases. It might be worth special-casing that, really.
1102static void CollectVisibleConversions(ASTContext &Context,
1103 CXXRecordDecl *Record,
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001104 ASTUnresolvedSet &Output) {
John McCall1e3a1a72010-03-15 09:07:48 +00001105 // The collection of all conversions in virtual bases that we've
1106 // found. These will be added to the output as long as they don't
1107 // appear in the hidden-conversions set.
1108 UnresolvedSet<8> VBaseCs;
1109
1110 // The set of conversions in virtual bases that we've determined to
1111 // be hidden.
1112 llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs;
1113
1114 // The set of types hidden by classes derived from this one.
1115 llvm::SmallPtrSet<CanQualType, 8> HiddenTypes;
1116
1117 // Go ahead and collect the direct conversions and add them to the
1118 // hidden-types set.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001119 CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
1120 CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001121 Output.append(Context, ConvI, ConvE);
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001122 for (; ConvI != ConvE; ++ConvI)
1123 HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
John McCall1e3a1a72010-03-15 09:07:48 +00001124
1125 // Recursively collect conversions from base classes.
1126 for (CXXRecordDecl::base_class_iterator
1127 I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) {
1128 const RecordType *RT = I->getType()->getAs<RecordType>();
1129 if (!RT) continue;
1130
1131 CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()),
1132 I->isVirtual(), I->getAccessSpecifier(),
1133 HiddenTypes, Output, VBaseCs, HiddenVBaseCs);
1134 }
1135
1136 // Add any unhidden conversions provided by virtual bases.
1137 for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
1138 I != E; ++I) {
1139 if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001140 Output.addDecl(Context, I.getDecl(), I.getAccess());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001141 }
Fariborz Jahanianb394f502009-09-12 18:26:03 +00001142}
1143
1144/// getVisibleConversionFunctions - get all conversion functions visible
1145/// in current class; including conversion function templates.
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001146std::pair<CXXRecordDecl::conversion_iterator,CXXRecordDecl::conversion_iterator>
1147CXXRecordDecl::getVisibleConversionFunctions() {
Richard Smitha4ba74c2013-08-30 04:46:40 +00001148 ASTContext &Ctx = getASTContext();
1149
1150 ASTUnresolvedSet *Set;
1151 if (bases_begin() == bases_end()) {
1152 // If root class, all conversions are visible.
1153 Set = &data().Conversions.get(Ctx);
1154 } else {
1155 Set = &data().VisibleConversions.get(Ctx);
1156 // If visible conversion list is not evaluated, evaluate it.
1157 if (!data().ComputedVisibleConversions) {
1158 CollectVisibleConversions(Ctx, this, *Set);
1159 data().ComputedVisibleConversions = true;
1160 }
Argyrios Kyrtzidisa6567c42012-11-28 03:56:09 +00001161 }
Richard Smitha4ba74c2013-08-30 04:46:40 +00001162 return std::make_pair(Set->begin(), Set->end());
Fariborz Jahanianb54ccb22009-09-11 21:44:33 +00001163}
1164
John McCallda4458e2010-03-31 01:36:47 +00001165void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
1166 // This operation is O(N) but extremely rare. Sema only uses it to
1167 // remove UsingShadowDecls in a class that were followed by a direct
1168 // declaration, e.g.:
1169 // class A : B {
1170 // using B::operator int;
1171 // operator int();
1172 // };
1173 // This is uncommon by itself and even more uncommon in conjunction
1174 // with sufficiently large numbers of directly-declared conversions
1175 // that asymptotic behavior matters.
1176
Richard Smitha4ba74c2013-08-30 04:46:40 +00001177 ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext());
John McCallda4458e2010-03-31 01:36:47 +00001178 for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
1179 if (Convs[I].getDecl() == ConvDecl) {
1180 Convs.erase(I);
1181 assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end()
1182 && "conversion was found multiple times in unresolved set");
1183 return;
1184 }
1185 }
1186
1187 llvm_unreachable("conversion not found in set!");
Douglas Gregor05155d82009-08-21 23:19:43 +00001188}
Fariborz Jahanian423a81f2009-06-19 19:55:27 +00001189
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001190CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001191 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001192 return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom());
1193
1194 return 0;
1195}
1196
1197void
1198CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD,
1199 TemplateSpecializationKind TSK) {
1200 assert(TemplateOrInstantiation.isNull() &&
1201 "Previous template or instantiation?");
1202 assert(!isa<ClassTemplateSpecializationDecl>(this));
1203 TemplateOrInstantiation
1204 = new (getASTContext()) MemberSpecializationInfo(RD, TSK);
1205}
1206
Anders Carlsson27cfc6e2009-12-07 06:33:48 +00001207TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{
1208 if (const ClassTemplateSpecializationDecl *Spec
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001209 = dyn_cast<ClassTemplateSpecializationDecl>(this))
1210 return Spec->getSpecializationKind();
1211
Douglas Gregor06db9f52009-10-12 20:18:28 +00001212 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo())
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001213 return MSInfo->getTemplateSpecializationKind();
1214
1215 return TSK_Undeclared;
1216}
1217
1218void
1219CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) {
1220 if (ClassTemplateSpecializationDecl *Spec
1221 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1222 Spec->setSpecializationKind(TSK);
1223 return;
1224 }
1225
Douglas Gregor06db9f52009-10-12 20:18:28 +00001226 if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) {
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001227 MSInfo->setTemplateSpecializationKind(TSK);
1228 return;
1229 }
1230
David Blaikie83d382b2011-09-23 05:06:16 +00001231 llvm_unreachable("Not a class template or member class specialization");
Douglas Gregorbbe8f462009-10-08 15:14:33 +00001232}
1233
Douglas Gregorbac74902010-07-01 14:13:13 +00001234CXXDestructorDecl *CXXRecordDecl::getDestructor() const {
1235 ASTContext &Context = getASTContext();
Anders Carlsson0a637412009-05-29 21:03:38 +00001236 QualType ClassType = Context.getTypeDeclType(this);
Mike Stump11289f42009-09-09 15:08:12 +00001237
1238 DeclarationName Name
Douglas Gregor2211d342009-08-05 05:36:45 +00001239 = Context.DeclarationNames.getCXXDestructorName(
1240 Context.getCanonicalType(ClassType));
Anders Carlsson0a637412009-05-29 21:03:38 +00001241
David Blaikieff7d47a2012-12-19 00:45:41 +00001242 DeclContext::lookup_const_result R = lookup(Name);
1243 if (R.empty())
Sebastian Redlb469afb2010-09-02 23:19:42 +00001244 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001245
David Blaikieff7d47a2012-12-19 00:45:41 +00001246 CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(R.front());
Anders Carlsson0a637412009-05-29 21:03:38 +00001247 return Dtor;
1248}
1249
Douglas Gregorb11aad82011-02-19 18:51:44 +00001250void CXXRecordDecl::completeDefinition() {
1251 completeDefinition(0);
1252}
1253
1254void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
1255 RecordDecl::completeDefinition();
1256
David Blaikiebbafb8a2012-03-11 07:00:24 +00001257 if (hasObjectMember() && getASTContext().getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +00001258 // Objective-C Automatic Reference Counting:
1259 // If a class has a non-static data member of Objective-C pointer
1260 // type (or array thereof), it is a non-POD type and its
Douglas Gregor6fa69422012-07-23 04:23:39 +00001261 // default constructor (if any), copy constructor, move constructor,
1262 // copy assignment operator, move assignment operator, and destructor are
1263 // non-trivial.
John McCall31168b02011-06-15 23:02:42 +00001264 struct DefinitionData &Data = data();
1265 Data.PlainOldData = false;
Richard Smith328aae52012-11-30 05:11:39 +00001266 Data.HasTrivialSpecialMembers = 0;
Richard Smith561fb152012-02-25 07:33:38 +00001267 Data.HasIrrelevantDestructor = false;
John McCall31168b02011-06-15 23:02:42 +00001268 }
1269
Douglas Gregor8fb95122010-09-29 00:15:42 +00001270 // If the class may be abstract (but hasn't been marked as such), check for
1271 // any pure final overriders.
1272 if (mayBeAbstract()) {
1273 CXXFinalOverriderMap MyFinalOverriders;
1274 if (!FinalOverriders) {
1275 getFinalOverriders(MyFinalOverriders);
1276 FinalOverriders = &MyFinalOverriders;
1277 }
1278
1279 bool Done = false;
1280 for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(),
1281 MEnd = FinalOverriders->end();
1282 M != MEnd && !Done; ++M) {
1283 for (OverridingMethods::iterator SO = M->second.begin(),
1284 SOEnd = M->second.end();
1285 SO != SOEnd && !Done; ++SO) {
1286 assert(SO->second.size() > 0 &&
1287 "All virtual functions have overridding virtual functions");
1288
1289 // C++ [class.abstract]p4:
1290 // A class is abstract if it contains or inherits at least one
1291 // pure virtual function for which the final overrider is pure
1292 // virtual.
1293 if (SO->second.front().Method->isPure()) {
1294 data().Abstract = true;
1295 Done = true;
1296 break;
1297 }
1298 }
1299 }
1300 }
Douglas Gregor457104e2010-09-29 04:25:11 +00001301
1302 // Set access bits correctly on the directly-declared conversions.
Richard Smitha4ba74c2013-08-30 04:46:40 +00001303 for (conversion_iterator I = conversion_begin(), E = conversion_end();
Douglas Gregor457104e2010-09-29 04:25:11 +00001304 I != E; ++I)
Argyrios Kyrtzidis0f05fb92012-11-28 03:56:16 +00001305 I.setAccess((*I)->getAccess());
Douglas Gregor8fb95122010-09-29 00:15:42 +00001306}
1307
1308bool CXXRecordDecl::mayBeAbstract() const {
1309 if (data().Abstract || isInvalidDecl() || !data().Polymorphic ||
1310 isDependentContext())
1311 return false;
1312
1313 for (CXXRecordDecl::base_class_const_iterator B = bases_begin(),
1314 BEnd = bases_end();
1315 B != BEnd; ++B) {
1316 CXXRecordDecl *BaseDecl
1317 = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl());
1318 if (BaseDecl->isAbstract())
1319 return true;
1320 }
1321
1322 return false;
1323}
1324
David Blaikie68e081d2011-12-20 02:48:34 +00001325void CXXMethodDecl::anchor() { }
1326
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001327bool CXXMethodDecl::isStatic() const {
Rafael Espindola29cda592013-04-15 12:38:20 +00001328 const CXXMethodDecl *MD = getCanonicalDecl();
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001329
1330 if (MD->getStorageClass() == SC_Static)
1331 return true;
1332
Reid Kleckner9a7f3e62013-10-08 00:58:57 +00001333 OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator();
1334 return isStaticOverloadedOperator(OOK);
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001335}
1336
Rafael Espindola49e860b2012-06-26 17:45:31 +00001337static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD,
1338 const CXXMethodDecl *BaseMD) {
1339 for (CXXMethodDecl::method_iterator I = DerivedMD->begin_overridden_methods(),
1340 E = DerivedMD->end_overridden_methods(); I != E; ++I) {
1341 const CXXMethodDecl *MD = *I;
1342 if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl())
1343 return true;
1344 if (recursivelyOverrides(MD, BaseMD))
1345 return true;
1346 }
1347 return false;
1348}
1349
1350CXXMethodDecl *
Jordan Rose5fc5da02012-08-15 20:07:17 +00001351CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD,
1352 bool MayBeBase) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001353 if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl())
1354 return this;
1355
1356 // Lookup doesn't work for destructors, so handle them separately.
1357 if (isa<CXXDestructorDecl>(this)) {
1358 CXXMethodDecl *MD = RD->getDestructor();
Jordan Rose5fc5da02012-08-15 20:07:17 +00001359 if (MD) {
1360 if (recursivelyOverrides(MD, this))
1361 return MD;
1362 if (MayBeBase && recursivelyOverrides(this, MD))
1363 return MD;
1364 }
Rafael Espindola49e860b2012-06-26 17:45:31 +00001365 return NULL;
1366 }
1367
1368 lookup_const_result Candidates = RD->lookup(getDeclName());
David Blaikieff7d47a2012-12-19 00:45:41 +00001369 for (NamedDecl * const * I = Candidates.begin(); I != Candidates.end(); ++I) {
Rafael Espindola49e860b2012-06-26 17:45:31 +00001370 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(*I);
1371 if (!MD)
1372 continue;
1373 if (recursivelyOverrides(MD, this))
1374 return MD;
Jordan Rose5fc5da02012-08-15 20:07:17 +00001375 if (MayBeBase && recursivelyOverrides(this, MD))
1376 return MD;
Rafael Espindola49e860b2012-06-26 17:45:31 +00001377 }
1378
1379 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1380 E = RD->bases_end(); I != E; ++I) {
1381 const RecordType *RT = I->getType()->getAs<RecordType>();
1382 if (!RT)
1383 continue;
1384 const CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl());
1385 CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base);
1386 if (T)
1387 return T;
1388 }
1389
1390 return NULL;
1391}
1392
Ted Kremenek21475702008-09-05 17:16:31 +00001393CXXMethodDecl *
1394CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001395 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001396 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001397 QualType T, TypeSourceInfo *TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001398 StorageClass SC, bool isInline,
Richard Smitha77a0a62011-08-15 21:04:07 +00001399 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001400 return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001401 SC, isInline, isConstexpr,
Richard Smitha77a0a62011-08-15 21:04:07 +00001402 EndLocation);
Ted Kremenek21475702008-09-05 17:16:31 +00001403}
1404
Douglas Gregor72172e92012-01-05 21:55:30 +00001405CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1406 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl));
1407 return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(),
1408 DeclarationNameInfo(), QualType(),
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001409 0, SC_None, false, false,
Douglas Gregor72172e92012-01-05 21:55:30 +00001410 SourceLocation());
1411}
1412
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001413bool CXXMethodDecl::isUsualDeallocationFunction() const {
1414 if (getOverloadedOperator() != OO_Delete &&
1415 getOverloadedOperator() != OO_Array_Delete)
1416 return false;
Douglas Gregor6642ca22010-02-26 05:06:18 +00001417
1418 // C++ [basic.stc.dynamic.deallocation]p2:
1419 // A template instance is never a usual deallocation function,
1420 // regardless of its signature.
1421 if (getPrimaryTemplate())
1422 return false;
1423
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001424 // C++ [basic.stc.dynamic.deallocation]p2:
1425 // If a class T has a member deallocation function named operator delete
1426 // with exactly one parameter, then that function is a usual (non-placement)
1427 // deallocation function. [...]
1428 if (getNumParams() == 1)
1429 return true;
1430
1431 // C++ [basic.stc.dynamic.deallocation]p2:
1432 // [...] If class T does not declare such an operator delete but does
1433 // declare a member deallocation function named operator delete with
1434 // exactly two parameters, the second of which has type std::size_t (18.1),
1435 // then this function is a usual deallocation function.
1436 ASTContext &Context = getASTContext();
1437 if (getNumParams() != 2 ||
Chandler Carruth75cc3592010-02-08 18:54:05 +00001438 !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(),
1439 Context.getSizeType()))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001440 return false;
1441
1442 // This function is a usual deallocation function if there are no
1443 // single-parameter deallocation functions of the same kind.
David Blaikieff7d47a2012-12-19 00:45:41 +00001444 DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName());
1445 for (DeclContext::lookup_const_result::iterator I = R.begin(), E = R.end();
1446 I != E; ++I) {
1447 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I))
Douglas Gregorbb3e12f2009-09-29 18:16:17 +00001448 if (FD->getNumParams() == 1)
1449 return false;
1450 }
1451
1452 return true;
1453}
1454
Douglas Gregorb139cd52010-05-01 20:49:11 +00001455bool CXXMethodDecl::isCopyAssignmentOperator() const {
Alexis Huntfcaeae42011-05-25 20:50:04 +00001456 // C++0x [class.copy]p17:
Douglas Gregorb139cd52010-05-01 20:49:11 +00001457 // A user-declared copy assignment operator X::operator= is a non-static
1458 // non-template member function of class X with exactly one parameter of
1459 // type X, X&, const X&, volatile X& or const volatile X&.
1460 if (/*operator=*/getOverloadedOperator() != OO_Equal ||
1461 /*non-static*/ isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001462 /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1463 getNumParams() != 1)
Douglas Gregorb139cd52010-05-01 20:49:11 +00001464 return false;
1465
1466 QualType ParamType = getParamDecl(0)->getType();
1467 if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>())
1468 ParamType = Ref->getPointeeType();
1469
1470 ASTContext &Context = getASTContext();
1471 QualType ClassType
1472 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1473 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1474}
1475
Alexis Huntfcaeae42011-05-25 20:50:04 +00001476bool CXXMethodDecl::isMoveAssignmentOperator() const {
1477 // C++0x [class.copy]p19:
1478 // A user-declared move assignment operator X::operator= is a non-static
1479 // non-template member function of class X with exactly one parameter of type
1480 // X&&, const X&&, volatile X&&, or const volatile X&&.
1481 if (getOverloadedOperator() != OO_Equal || isStatic() ||
Eli Friedman84c0143e2013-07-11 23:55:07 +00001482 getPrimaryTemplate() || getDescribedFunctionTemplate() ||
1483 getNumParams() != 1)
Alexis Huntfcaeae42011-05-25 20:50:04 +00001484 return false;
1485
1486 QualType ParamType = getParamDecl(0)->getType();
1487 if (!isa<RValueReferenceType>(ParamType))
1488 return false;
1489 ParamType = ParamType->getPointeeType();
1490
1491 ASTContext &Context = getASTContext();
1492 QualType ClassType
1493 = Context.getCanonicalType(Context.getTypeDeclType(getParent()));
1494 return Context.hasSameUnqualifiedType(ClassType, ParamType);
1495}
1496
Anders Carlsson36d87e12009-05-16 23:58:37 +00001497void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001498 assert(MD->isCanonicalDecl() && "Method is not canonical!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001499 assert(!MD->getParent()->isDependentContext() &&
1500 "Can't add an overridden method to a class template!");
Eli Friedman91359022012-03-10 01:39:01 +00001501 assert(MD->isVirtual() && "Method is not virtual!");
Anders Carlssonbd32c432010-01-30 17:42:34 +00001502
Douglas Gregor832940b2010-03-02 23:58:15 +00001503 getASTContext().addOverriddenMethod(this, MD);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001504}
1505
1506CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001507 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001508 return getASTContext().overridden_methods_begin(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001509}
1510
1511CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001512 if (isa<CXXConstructorDecl>(this)) return 0;
Douglas Gregor832940b2010-03-02 23:58:15 +00001513 return getASTContext().overridden_methods_end(this);
Anders Carlsson36d87e12009-05-16 23:58:37 +00001514}
1515
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001516unsigned CXXMethodDecl::size_overridden_methods() const {
Eli Friedman91359022012-03-10 01:39:01 +00001517 if (isa<CXXConstructorDecl>(this)) return 0;
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +00001518 return getASTContext().overridden_methods_size(this);
1519}
1520
Ted Kremenek21475702008-09-05 17:16:31 +00001521QualType CXXMethodDecl::getThisType(ASTContext &C) const {
Argyrios Kyrtzidis962c20e2008-10-24 22:28:18 +00001522 // C++ 9.3.2p1: The type of this in a member function of a class X is X*.
1523 // If the member function is declared const, the type of this is const X*,
1524 // if the member function is declared volatile, the type of this is
1525 // volatile X*, and if the member function is declared const volatile,
1526 // the type of this is const volatile X*.
1527
Ted Kremenek21475702008-09-05 17:16:31 +00001528 assert(isInstance() && "No 'this' for static methods!");
Anders Carlsson20ee0ed2009-06-13 02:59:33 +00001529
John McCalle78aac42010-03-10 03:28:59 +00001530 QualType ClassTy = C.getTypeDeclType(getParent());
John McCall8ccfcb52009-09-24 19:53:00 +00001531 ClassTy = C.getQualifiedType(ClassTy,
1532 Qualifiers::fromCVRMask(getTypeQualifiers()));
Anders Carlsson7ca3f6f2009-07-10 21:35:09 +00001533 return C.getPointerType(ClassTy);
Ted Kremenek21475702008-09-05 17:16:31 +00001534}
1535
Eli Friedman71a26d82009-12-06 20:50:05 +00001536bool CXXMethodDecl::hasInlineBody() const {
Douglas Gregora318efd2010-01-05 19:06:31 +00001537 // If this function is a template instantiation, look at the template from
1538 // which it was instantiated.
1539 const FunctionDecl *CheckFn = getTemplateInstantiationPattern();
1540 if (!CheckFn)
1541 CheckFn = this;
1542
Eli Friedman71a26d82009-12-06 20:50:05 +00001543 const FunctionDecl *fn;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001544 return CheckFn->hasBody(fn) && !fn->isOutOfLine();
Eli Friedman71a26d82009-12-06 20:50:05 +00001545}
1546
Douglas Gregor355efbb2012-02-17 03:02:34 +00001547bool CXXMethodDecl::isLambdaStaticInvoker() const {
Faisal Vali571df122013-09-29 08:45:24 +00001548 const CXXRecordDecl *P = getParent();
1549 if (P->isLambda()) {
1550 if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) {
1551 if (StaticInvoker == this) return true;
1552 if (P->isGenericLambda() && this->isFunctionTemplateSpecialization())
1553 return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl();
1554 }
1555 }
1556 return false;
Douglas Gregor355efbb2012-02-17 03:02:34 +00001557}
1558
Alexis Hunt1d792652011-01-08 20:30:50 +00001559CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1560 TypeSourceInfo *TInfo, bool IsVirtual,
1561 SourceLocation L, Expr *Init,
1562 SourceLocation R,
1563 SourceLocation EllipsisLoc)
Alexis Hunta50dd462011-01-08 23:01:16 +00001564 : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001565 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual),
1566 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001567{
Douglas Gregore8381c02008-11-05 04:29:56 +00001568}
1569
Alexis Hunt1d792652011-01-08 20:30:50 +00001570CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1571 FieldDecl *Member,
1572 SourceLocation MemberLoc,
1573 SourceLocation L, Expr *Init,
1574 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001575 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001576 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Francois Pichetd583da02010-12-04 09:14:42 +00001577 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1578{
1579}
1580
Alexis Hunt1d792652011-01-08 20:30:50 +00001581CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
1582 IndirectFieldDecl *Member,
1583 SourceLocation MemberLoc,
1584 SourceLocation L, Expr *Init,
1585 SourceLocation R)
Alexis Hunta50dd462011-01-08 23:01:16 +00001586 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001587 LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001588 IsWritten(false), SourceOrderOrNumArrayIndices(0)
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001589{
Douglas Gregore8381c02008-11-05 04:29:56 +00001590}
1591
Alexis Hunt1d792652011-01-08 20:30:50 +00001592CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001593 TypeSourceInfo *TInfo,
1594 SourceLocation L, Expr *Init,
Alexis Huntc5575cc2011-02-26 19:13:13 +00001595 SourceLocation R)
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001596 : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init),
1597 LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false),
Alexis Huntc5575cc2011-02-26 19:13:13 +00001598 IsWritten(false), SourceOrderOrNumArrayIndices(0)
1599{
1600}
1601
1602CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context,
Alexis Hunt1d792652011-01-08 20:30:50 +00001603 FieldDecl *Member,
1604 SourceLocation MemberLoc,
1605 SourceLocation L, Expr *Init,
1606 SourceLocation R,
1607 VarDecl **Indices,
1608 unsigned NumIndices)
Alexis Hunta50dd462011-01-08 23:01:16 +00001609 : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init),
Francois Pichetd583da02010-12-04 09:14:42 +00001610 LParenLoc(L), RParenLoc(R), IsVirtual(false),
Abramo Bagnara341d7832010-05-26 18:09:23 +00001611 IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices)
Douglas Gregor94f9a482010-05-05 05:51:00 +00001612{
1613 VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1);
1614 memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *));
1615}
1616
Alexis Hunt1d792652011-01-08 20:30:50 +00001617CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context,
1618 FieldDecl *Member,
1619 SourceLocation MemberLoc,
1620 SourceLocation L, Expr *Init,
1621 SourceLocation R,
1622 VarDecl **Indices,
1623 unsigned NumIndices) {
1624 void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) +
Douglas Gregor94f9a482010-05-05 05:51:00 +00001625 sizeof(VarDecl *) * NumIndices,
Alexis Hunt1d792652011-01-08 20:30:50 +00001626 llvm::alignOf<CXXCtorInitializer>());
Alexis Hunta50dd462011-01-08 23:01:16 +00001627 return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R,
1628 Indices, NumIndices);
Douglas Gregor94f9a482010-05-05 05:51:00 +00001629}
1630
Alexis Hunt1d792652011-01-08 20:30:50 +00001631TypeLoc CXXCtorInitializer::getBaseClassLoc() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001632 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001633 return Initializee.get<TypeSourceInfo*>()->getTypeLoc();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001634 else
1635 return TypeLoc();
1636}
1637
Alexis Hunt1d792652011-01-08 20:30:50 +00001638const Type *CXXCtorInitializer::getBaseClass() const {
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001639 if (isBaseInitializer())
Alexis Hunta50dd462011-01-08 23:01:16 +00001640 return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001641 else
1642 return 0;
1643}
1644
Alexis Hunt1d792652011-01-08 20:30:50 +00001645SourceLocation CXXCtorInitializer::getSourceLocation() const {
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001646 if (isAnyMemberInitializer())
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001647 return getMemberLocation();
Richard Smith938f40b2011-06-11 17:19:42 +00001648
1649 if (isInClassMemberInitializer())
1650 return getAnyMember()->getLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001651
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001652 if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>())
1653 return TSInfo->getTypeLoc().getLocalSourceRange().getBegin();
1654
1655 return SourceLocation();
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001656}
1657
Alexis Hunt1d792652011-01-08 20:30:50 +00001658SourceRange CXXCtorInitializer::getSourceRange() const {
Richard Smith938f40b2011-06-11 17:19:42 +00001659 if (isInClassMemberInitializer()) {
1660 FieldDecl *D = getAnyMember();
1661 if (Expr *I = D->getInClassInitializer())
1662 return I->getSourceRange();
1663 return SourceRange();
1664 }
1665
Douglas Gregorc8c44b5d2009-12-02 22:36:29 +00001666 return SourceRange(getSourceLocation(), getRParenLoc());
Douglas Gregore8381c02008-11-05 04:29:56 +00001667}
1668
David Blaikie68e081d2011-12-20 02:48:34 +00001669void CXXConstructorDecl::anchor() { }
1670
Douglas Gregor61956c42008-10-31 09:07:45 +00001671CXXConstructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001672CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1673 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl));
1674 return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(),
1675 QualType(), 0, false, false, false,false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001676}
1677
1678CXXConstructorDecl *
Douglas Gregor61956c42008-10-31 09:07:45 +00001679CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001680 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001681 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001682 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001683 bool isExplicit, bool isInline,
1684 bool isImplicitlyDeclared, bool isConstexpr) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001685 assert(NameInfo.getName().getNameKind()
1686 == DeclarationName::CXXConstructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001687 "Name must refer to a constructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001688 return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001689 isExplicit, isInline, isImplicitlyDeclared,
1690 isConstexpr);
Douglas Gregor61956c42008-10-31 09:07:45 +00001691}
1692
Douglas Gregord73f3dd2011-11-01 01:16:03 +00001693CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const {
1694 assert(isDelegatingConstructor() && "Not a delegating constructor!");
1695 Expr *E = (*init_begin())->getInit()->IgnoreImplicit();
1696 if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E))
1697 return Construct->getConstructor();
1698
1699 return 0;
1700}
1701
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001702bool CXXConstructorDecl::isDefaultConstructor() const {
1703 // C++ [class.ctor]p5:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001704 // A default constructor for a class X is a constructor of class
1705 // X that can be called without an argument.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001706 return (getNumParams() == 0) ||
Anders Carlsson6eb55572009-08-25 05:12:04 +00001707 (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg());
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001708}
1709
Mike Stump11289f42009-09-09 15:08:12 +00001710bool
Douglas Gregor507eb872009-12-22 00:34:07 +00001711CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const {
Douglas Gregorf282a762011-01-21 19:38:21 +00001712 return isCopyOrMoveConstructor(TypeQuals) &&
1713 getParamDecl(0)->getType()->isLValueReferenceType();
1714}
1715
1716bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const {
1717 return isCopyOrMoveConstructor(TypeQuals) &&
1718 getParamDecl(0)->getType()->isRValueReferenceType();
1719}
1720
1721/// \brief Determine whether this is a copy or move constructor.
1722bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const {
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001723 // C++ [class.copy]p2:
Douglas Gregorcfd8ddc2008-11-05 16:20:31 +00001724 // A non-template constructor for class X is a copy constructor
1725 // if its first parameter is of type X&, const X&, volatile X& or
1726 // const volatile X&, and either there are no other parameters
1727 // or else all other parameters have default arguments (8.3.6).
Douglas Gregorf282a762011-01-21 19:38:21 +00001728 // C++0x [class.copy]p3:
1729 // A non-template constructor for class X is a move constructor if its
1730 // first parameter is of type X&&, const X&&, volatile X&&, or
1731 // const volatile X&&, and either there are no other parameters or else
1732 // all other parameters have default arguments.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001733 if ((getNumParams() < 1) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001734 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
Douglas Gregorff7028a2009-11-13 23:59:09 +00001735 (getPrimaryTemplate() != 0) ||
Douglas Gregora14b43b2009-10-13 23:45:19 +00001736 (getDescribedFunctionTemplate() != 0))
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001737 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001738
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001739 const ParmVarDecl *Param = getParamDecl(0);
Douglas Gregorf282a762011-01-21 19:38:21 +00001740
1741 // Do we have a reference type?
1742 const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>();
Douglas Gregorff7028a2009-11-13 23:59:09 +00001743 if (!ParamRefType)
1744 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001745
Douglas Gregorff7028a2009-11-13 23:59:09 +00001746 // Is it a reference to our class type?
Douglas Gregor507eb872009-12-22 00:34:07 +00001747 ASTContext &Context = getASTContext();
1748
Douglas Gregorff7028a2009-11-13 23:59:09 +00001749 CanQualType PointeeType
1750 = Context.getCanonicalType(ParamRefType->getPointeeType());
Douglas Gregorf70b2b42009-09-15 20:50:23 +00001751 CanQualType ClassTy
1752 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001753 if (PointeeType.getUnqualifiedType() != ClassTy)
1754 return false;
Douglas Gregorf282a762011-01-21 19:38:21 +00001755
John McCall8ccfcb52009-09-24 19:53:00 +00001756 // FIXME: other qualifiers?
Douglas Gregorf282a762011-01-21 19:38:21 +00001757
1758 // We have a copy or move constructor.
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001759 TypeQuals = PointeeType.getCVRQualifiers();
Douglas Gregorf282a762011-01-21 19:38:21 +00001760 return true;
Douglas Gregoreebb5c12008-10-31 20:25:05 +00001761}
1762
Anders Carlssond20e7952009-08-28 16:57:08 +00001763bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const {
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001764 // C++ [class.conv.ctor]p1:
1765 // A constructor declared without the function-specifier explicit
1766 // that can be called with a single parameter specifies a
1767 // conversion from the type of its first parameter to the type of
1768 // its class. Such a constructor is called a converting
1769 // constructor.
Anders Carlssond20e7952009-08-28 16:57:08 +00001770 if (isExplicit() && !AllowExplicit)
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001771 return false;
1772
Mike Stump11289f42009-09-09 15:08:12 +00001773 return (getNumParams() == 0 &&
John McCall9dd450b2009-09-21 23:43:11 +00001774 getType()->getAs<FunctionProtoType>()->isVariadic()) ||
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001775 (getNumParams() == 1) ||
Douglas Gregorc65e1592012-06-05 23:44:51 +00001776 (getNumParams() > 1 &&
1777 (getParamDecl(1)->hasDefaultArg() ||
1778 getParamDecl(1)->isParameterPack()));
Douglas Gregor26bee0b2008-10-31 16:23:19 +00001779}
Douglas Gregor61956c42008-10-31 09:07:45 +00001780
Douglas Gregorbd6b17f2010-11-08 17:16:59 +00001781bool CXXConstructorDecl::isSpecializationCopyingObject() const {
Douglas Gregorffe14e32009-11-14 01:20:54 +00001782 if ((getNumParams() < 1) ||
1783 (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) ||
1784 (getPrimaryTemplate() == 0) ||
1785 (getDescribedFunctionTemplate() != 0))
1786 return false;
1787
1788 const ParmVarDecl *Param = getParamDecl(0);
1789
1790 ASTContext &Context = getASTContext();
1791 CanQualType ParamType = Context.getCanonicalType(Param->getType());
1792
Douglas Gregorffe14e32009-11-14 01:20:54 +00001793 // Is it the same as our our class type?
1794 CanQualType ClassTy
1795 = Context.getCanonicalType(Context.getTagDeclType(getParent()));
1796 if (ParamType.getUnqualifiedType() != ClassTy)
1797 return false;
1798
1799 return true;
1800}
1801
Sebastian Redl08905022011-02-05 19:23:19 +00001802const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const {
1803 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001804 method_iterator It = getASTContext().overridden_methods_begin(this);
1805 if (It == getASTContext().overridden_methods_end(this))
Sebastian Redl08905022011-02-05 19:23:19 +00001806 return 0;
1807
1808 return cast<CXXConstructorDecl>(*It);
1809}
1810
1811void
1812CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){
1813 // Hack: we store the inherited constructor in the overridden method table
Eli Friedman91359022012-03-10 01:39:01 +00001814 assert(getASTContext().overridden_methods_size(this) == 0 &&
1815 "Base ctor already set.");
1816 getASTContext().addOverriddenMethod(this, BaseCtor);
Sebastian Redl08905022011-02-05 19:23:19 +00001817}
1818
David Blaikie68e081d2011-12-20 02:48:34 +00001819void CXXDestructorDecl::anchor() { }
1820
Douglas Gregor831c93f2008-11-05 20:51:48 +00001821CXXDestructorDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001822CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1823 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl));
1824 return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001825 QualType(), 0, false, false);
Chris Lattnerca025db2010-05-07 21:43:38 +00001826}
1827
1828CXXDestructorDecl *
Douglas Gregor831c93f2008-11-05 20:51:48 +00001829CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001830 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001831 const DeclarationNameInfo &NameInfo,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00001832 QualType T, TypeSourceInfo *TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001833 bool isInline, bool isImplicitlyDeclared) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001834 assert(NameInfo.getName().getNameKind()
1835 == DeclarationName::CXXDestructorName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001836 "Name must refer to a destructor");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001837 return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001838 isImplicitlyDeclared);
Douglas Gregor831c93f2008-11-05 20:51:48 +00001839}
1840
David Blaikie68e081d2011-12-20 02:48:34 +00001841void CXXConversionDecl::anchor() { }
1842
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001843CXXConversionDecl *
Douglas Gregor72172e92012-01-05 21:55:30 +00001844CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1845 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl));
1846 return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(),
1847 QualType(), 0, false, false, false,
1848 SourceLocation());
Chris Lattnerca025db2010-05-07 21:43:38 +00001849}
1850
1851CXXConversionDecl *
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001852CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001853 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001854 const DeclarationNameInfo &NameInfo,
John McCallbcd03502009-12-07 02:54:59 +00001855 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorf2f08062011-03-08 17:10:18 +00001856 bool isInline, bool isExplicit,
Richard Smitha77a0a62011-08-15 21:04:07 +00001857 bool isConstexpr, SourceLocation EndLocation) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001858 assert(NameInfo.getName().getNameKind()
1859 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor77324f32008-11-17 14:58:09 +00001860 "Name must refer to a conversion function");
Abramo Bagnaradff19302011-03-08 08:55:46 +00001861 return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo,
Richard Smitha77a0a62011-08-15 21:04:07 +00001862 isInline, isExplicit, isConstexpr,
1863 EndLocation);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00001864}
1865
Douglas Gregord3b672c2012-02-16 01:06:16 +00001866bool CXXConversionDecl::isLambdaToBlockPointerConversion() const {
1867 return isImplicit() && getParent()->isLambda() &&
1868 getConversionType()->isBlockPointerType();
1869}
1870
David Blaikie68e081d2011-12-20 02:48:34 +00001871void LinkageSpecDecl::anchor() { }
1872
Chris Lattnerb8c18fa2008-11-04 16:51:42 +00001873LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C,
Mike Stump11289f42009-09-09 15:08:12 +00001874 DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00001875 SourceLocation ExternLoc,
1876 SourceLocation LangLoc,
Abramo Bagnara4a8cda82011-03-03 14:52:38 +00001877 LanguageIDs Lang,
Rafael Espindola327be3c2013-04-26 01:30:23 +00001878 bool HasBraces) {
1879 return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces);
Douglas Gregor29ff7d02008-12-16 22:23:02 +00001880}
Douglas Gregor889ceb72009-02-03 19:21:40 +00001881
Douglas Gregor72172e92012-01-05 21:55:30 +00001882LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1883 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl));
1884 return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(),
Rafael Espindola327be3c2013-04-26 01:30:23 +00001885 lang_c, false);
Douglas Gregor72172e92012-01-05 21:55:30 +00001886}
1887
David Blaikie68e081d2011-12-20 02:48:34 +00001888void UsingDirectiveDecl::anchor() { }
1889
Douglas Gregor889ceb72009-02-03 19:21:40 +00001890UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC,
1891 SourceLocation L,
1892 SourceLocation NamespaceLoc,
Douglas Gregor12441b32011-02-25 16:33:46 +00001893 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001894 SourceLocation IdentLoc,
Sebastian Redla6602e92009-11-23 15:34:23 +00001895 NamedDecl *Used,
Douglas Gregor889ceb72009-02-03 19:21:40 +00001896 DeclContext *CommonAncestor) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001897 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
1898 Used = NS->getOriginalNamespace();
Douglas Gregor12441b32011-02-25 16:33:46 +00001899 return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc,
1900 IdentLoc, Used, CommonAncestor);
Douglas Gregor889ceb72009-02-03 19:21:40 +00001901}
1902
Douglas Gregor72172e92012-01-05 21:55:30 +00001903UsingDirectiveDecl *
1904UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1905 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl));
1906 return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(),
1907 NestedNameSpecifierLoc(),
1908 SourceLocation(), 0, 0);
1909}
1910
Sebastian Redla6602e92009-11-23 15:34:23 +00001911NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
1912 if (NamespaceAliasDecl *NA =
1913 dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
1914 return NA->getNamespace();
1915 return cast_or_null<NamespaceDecl>(NominatedNamespace);
1916}
1917
Douglas Gregor72172e92012-01-05 21:55:30 +00001918void NamespaceDecl::anchor() { }
1919
Douglas Gregore57e7522012-01-07 09:11:48 +00001920NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline,
1921 SourceLocation StartLoc,
1922 SourceLocation IdLoc, IdentifierInfo *Id,
1923 NamespaceDecl *PrevDecl)
1924 : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace),
1925 LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline)
1926{
Rafael Espindola8db352d2013-10-17 15:37:26 +00001927 setPreviousDecl(PrevDecl);
Douglas Gregore57e7522012-01-07 09:11:48 +00001928
1929 if (PrevDecl)
1930 AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace());
1931}
1932
Douglas Gregor72172e92012-01-05 21:55:30 +00001933NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00001934 bool Inline, SourceLocation StartLoc,
1935 SourceLocation IdLoc, IdentifierInfo *Id,
1936 NamespaceDecl *PrevDecl) {
1937 return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl);
Douglas Gregor72172e92012-01-05 21:55:30 +00001938}
1939
1940NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1941 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl));
Douglas Gregore57e7522012-01-07 09:11:48 +00001942 return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(),
1943 0, 0);
Douglas Gregor72172e92012-01-05 21:55:30 +00001944}
1945
David Blaikie68e081d2011-12-20 02:48:34 +00001946void NamespaceAliasDecl::anchor() { }
1947
Mike Stump11289f42009-09-09 15:08:12 +00001948NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor01a430132010-09-01 03:07:18 +00001949 SourceLocation UsingLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001950 SourceLocation AliasLoc,
1951 IdentifierInfo *Alias,
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001952 NestedNameSpecifierLoc QualifierLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001953 SourceLocation IdentLoc,
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001954 NamedDecl *Namespace) {
Sebastian Redla6602e92009-11-23 15:34:23 +00001955 if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
1956 Namespace = NS->getOriginalNamespace();
Douglas Gregorc05ba2e2011-02-25 17:08:07 +00001957 return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias,
1958 QualifierLoc, IdentLoc, Namespace);
Anders Carlssonff25fdf2009-03-28 22:58:02 +00001959}
1960
Douglas Gregor72172e92012-01-05 21:55:30 +00001961NamespaceAliasDecl *
1962NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl));
1964 return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0,
1965 NestedNameSpecifierLoc(),
1966 SourceLocation(), 0);
1967}
1968
David Blaikie68e081d2011-12-20 02:48:34 +00001969void UsingShadowDecl::anchor() { }
1970
Douglas Gregor72172e92012-01-05 21:55:30 +00001971UsingShadowDecl *
1972UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1973 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl));
1974 return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0);
1975}
1976
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001977UsingDecl *UsingShadowDecl::getUsingDecl() const {
1978 const UsingShadowDecl *Shadow = this;
1979 while (const UsingShadowDecl *NextShadow =
1980 dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow))
1981 Shadow = NextShadow;
1982 return cast<UsingDecl>(Shadow->UsingOrNextShadow);
1983}
1984
David Blaikie68e081d2011-12-20 02:48:34 +00001985void UsingDecl::anchor() { }
1986
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001987void UsingDecl::addShadowDecl(UsingShadowDecl *S) {
1988 assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() &&
1989 "declaration already in set");
1990 assert(S->getUsingDecl() == this);
1991
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00001992 if (FirstUsingShadow.getPointer())
1993 S->UsingOrNextShadow = FirstUsingShadow.getPointer();
1994 FirstUsingShadow.setPointer(S);
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00001995}
1996
1997void UsingDecl::removeShadowDecl(UsingShadowDecl *S) {
1998 assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() &&
1999 "declaration not in set");
2000 assert(S->getUsingDecl() == this);
2001
2002 // Remove S from the shadow decl chain. This is O(n) but hopefully rare.
2003
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002004 if (FirstUsingShadow.getPointer() == S) {
2005 FirstUsingShadow.setPointer(
2006 dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow));
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002007 S->UsingOrNextShadow = this;
2008 return;
2009 }
2010
Benjamin Kramere78f8ee2012-01-07 19:09:05 +00002011 UsingShadowDecl *Prev = FirstUsingShadow.getPointer();
Argyrios Kyrtzidis2703beb2010-11-10 05:40:41 +00002012 while (Prev->UsingOrNextShadow != S)
2013 Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow);
2014 Prev->UsingOrNextShadow = S->UsingOrNextShadow;
2015 S->UsingOrNextShadow = this;
2016}
2017
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002018UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL,
2019 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002020 const DeclarationNameInfo &NameInfo,
Enea Zaffanellae05a3cf2013-07-22 10:54:09 +00002021 bool HasTypename) {
2022 return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename);
Douglas Gregorfec52632009-06-20 00:51:54 +00002023}
2024
Douglas Gregor72172e92012-01-05 21:55:30 +00002025UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2026 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl));
2027 return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(),
2028 DeclarationNameInfo(), false);
2029}
2030
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002031SourceRange UsingDecl::getSourceRange() const {
2032 SourceLocation Begin = isAccessDeclaration()
2033 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2034 return SourceRange(Begin, getNameInfo().getEndLoc());
2035}
2036
David Blaikie68e081d2011-12-20 02:48:34 +00002037void UnresolvedUsingValueDecl::anchor() { }
2038
John McCalle61f2ba2009-11-18 02:36:19 +00002039UnresolvedUsingValueDecl *
2040UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC,
2041 SourceLocation UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002042 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara8de74e92010-08-12 11:46:03 +00002043 const DeclarationNameInfo &NameInfo) {
John McCalle61f2ba2009-11-18 02:36:19 +00002044 return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002045 QualifierLoc, NameInfo);
John McCalle61f2ba2009-11-18 02:36:19 +00002046}
2047
Douglas Gregor72172e92012-01-05 21:55:30 +00002048UnresolvedUsingValueDecl *
2049UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2050 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl));
2051 return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(),
2052 NestedNameSpecifierLoc(),
2053 DeclarationNameInfo());
2054}
2055
Enea Zaffanellac70b2512013-07-17 17:28:56 +00002056SourceRange UnresolvedUsingValueDecl::getSourceRange() const {
2057 SourceLocation Begin = isAccessDeclaration()
2058 ? getQualifierLoc().getBeginLoc() : UsingLocation;
2059 return SourceRange(Begin, getNameInfo().getEndLoc());
2060}
2061
David Blaikie68e081d2011-12-20 02:48:34 +00002062void UnresolvedUsingTypenameDecl::anchor() { }
2063
John McCalle61f2ba2009-11-18 02:36:19 +00002064UnresolvedUsingTypenameDecl *
2065UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC,
2066 SourceLocation UsingLoc,
2067 SourceLocation TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002068 NestedNameSpecifierLoc QualifierLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002069 SourceLocation TargetNameLoc,
2070 DeclarationName TargetName) {
2071 return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc,
Douglas Gregora9d87bc2011-02-25 00:36:19 +00002072 QualifierLoc, TargetNameLoc,
John McCalle61f2ba2009-11-18 02:36:19 +00002073 TargetName.getAsIdentifierInfo());
Anders Carlsson8305c1f2009-08-28 05:30:28 +00002074}
2075
Douglas Gregor72172e92012-01-05 21:55:30 +00002076UnresolvedUsingTypenameDecl *
2077UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2078 void *Mem = AllocateDeserializedDecl(C, ID,
2079 sizeof(UnresolvedUsingTypenameDecl));
2080 return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(),
2081 SourceLocation(),
2082 NestedNameSpecifierLoc(),
2083 SourceLocation(),
2084 0);
2085}
2086
David Blaikie68e081d2011-12-20 02:48:34 +00002087void StaticAssertDecl::anchor() { }
2088
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002089StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraea947882011-03-08 16:41:52 +00002090 SourceLocation StaticAssertLoc,
2091 Expr *AssertExpr,
2092 StringLiteral *Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002093 SourceLocation RParenLoc,
2094 bool Failed) {
Abramo Bagnaraea947882011-03-08 16:41:52 +00002095 return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message,
Richard Smithded9c2e2012-07-11 22:37:56 +00002096 RParenLoc, Failed);
Anders Carlsson5bbe1d72009-03-14 00:25:26 +00002097}
2098
Douglas Gregor72172e92012-01-05 21:55:30 +00002099StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C,
2100 unsigned ID) {
2101 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl));
Richard Smithded9c2e2012-07-11 22:37:56 +00002102 return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,
2103 SourceLocation(), false);
Douglas Gregor72172e92012-01-05 21:55:30 +00002104}
2105
Anders Carlsson6750d162009-03-26 23:46:50 +00002106static const char *getAccessName(AccessSpecifier AS) {
2107 switch (AS) {
Anders Carlsson6750d162009-03-26 23:46:50 +00002108 case AS_none:
David Blaikie83d382b2011-09-23 05:06:16 +00002109 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002110 case AS_public:
2111 return "public";
2112 case AS_private:
2113 return "private";
2114 case AS_protected:
2115 return "protected";
2116 }
David Blaikief47fa302012-01-17 02:30:50 +00002117 llvm_unreachable("Invalid access specifier!");
Anders Carlsson6750d162009-03-26 23:46:50 +00002118}
2119
2120const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
2121 AccessSpecifier AS) {
2122 return DB << getAccessName(AS);
2123}
Richard Smith84f6dcf2012-02-02 01:16:57 +00002124
2125const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB,
2126 AccessSpecifier AS) {
2127 return DB << getAccessName(AS);
2128}